blob: 40d827cac168180b421a848a5fd8c8aea26f988a [file] [log] [blame]
b.liu5f950c52024-06-15 20:13:12 +08001/*
2* gnss_ipc.c
3*
4* MBTK GNSS IPC service source.
5*
6*/
7/******************************************************************************
8
9 EDIT HISTORY FOR FILE
10
11 WHEN WHO WHAT,WHERE,WHY
12-------- -------- -------------------------------------------------------
132024/6/15 LiuBin Initial version
14
15******************************************************************************/
16#include <stdio.h>
17#include <stdlib.h>
18#include <unistd.h>
19#include <errno.h>
20#include <fcntl.h>
21#include <sys/socket.h>
22#include <sys/un.h>
23#include <netinet/in.h>
24#include <pthread.h>
25#include <sys/epoll.h>
26
27#include "mbtk_log.h"
28#include "mbtk_type.h"
29
30#define GNSS_SOCK_PATH "/tmp/mbtk_gnss_sock"
31
32static int sock_listen_fd = -1;
33
34typedef enum {
35 GNSS_CMD_INIT = 0,
b.liud0ba7152024-06-19 14:47:21 +080036 GNSS_CMD_DEINIT,
b.liudbc3f4b2024-06-25 18:22:24 +080037 GNSS_CMD_SETTING,
38 GNSS_CMD_DL
b.liu5f950c52024-06-15 20:13:12 +080039} gnss_cmd_enum;
40
41static void help()
42{
b.liud0ba7152024-06-19 14:47:21 +080043 printf("gnss_cli gnss_init <0-15>\n");
b.liu5f950c52024-06-15 20:13:12 +080044 printf("gnss_cli gnss_deinit\n");
b.liud0ba7152024-06-19 14:47:21 +080045 printf("gnss_cli gnss_setting cmd\n");
b.liudbc3f4b2024-06-25 18:22:24 +080046 printf("gnss_cli gnss_dl fw_name\n");
b.liu5f950c52024-06-15 20:13:12 +080047}
48
49static int cmd_process(gnss_cmd_enum cmd, void *arg)
50{
51 if(sock_listen_fd < 0) {
52 sock_listen_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
53 if(sock_listen_fd < 0)
54 {
55 printf("socket() fail[%d].\n", errno);
56 return -1;
57 }
58
59 struct sockaddr_un cli_addr;
60 memset(&cli_addr, 0, sizeof(cli_addr));
61 cli_addr.sun_family = AF_LOCAL;
62 strcpy(cli_addr.sun_path, GNSS_SOCK_PATH);
63 if(connect(sock_listen_fd, (struct sockaddr *)&cli_addr, sizeof(cli_addr)))
64 {
65 printf("connect() fail[%d].\n", errno);
66 close(sock_listen_fd);
67 sock_listen_fd = -1;
68 return -1;
69 }
70 }
71
72 char buff[100] = {0};
73 if(cmd == GNSS_CMD_INIT) {
74 if(arg) {
75 int type = atoi((char*)arg);
76 sprintf(buff, "gnss_init:%d", type);
77 } else {
78 return -1;
79 }
80 } else if(cmd == GNSS_CMD_DEINIT) {
81 sprintf(buff, "gnss_deinit");
b.liud0ba7152024-06-19 14:47:21 +080082 } else if(cmd == GNSS_CMD_SETTING) {
83 sprintf(buff, "gnss_setting:%s", arg);
b.liudbc3f4b2024-06-25 18:22:24 +080084 } else if(cmd == GNSS_CMD_DL) {
85 sprintf(buff, "gnss_dl:%s", arg);
b.liu5f950c52024-06-15 20:13:12 +080086 } else {
87 printf("Unknown cmd.\n");
88 return -1;
89 }
90
91 write(sock_listen_fd, buff, strlen(buff));
92
93 int len = 0;
94 while(1) {
95 memset(buff, 0, sizeof(buff));
96 len = read(sock_listen_fd, buff, sizeof(buff));
97 if(len > 0) {
98 printf("RSP : %s\n", buff);
99 if(cmd == GNSS_CMD_INIT) {
100 if(memcmp(buff, "gnss_init", 9) == 0) {
101 return atoi(buff + 10);
102 } else {
103 printf("gnss_init response error.\n");
104 return -1;
105 }
106 } else if(cmd == GNSS_CMD_DEINIT) {
107 if(memcmp(buff, "gnss_deinit", 11) == 0) {
108 return atoi(buff + 12);
109 } else {
110 printf("gnss_deinit response error.\n");
111 return -1;
112 }
b.liud0ba7152024-06-19 14:47:21 +0800113 } else if(cmd == GNSS_CMD_SETTING) {
114 if(memcmp(buff, "gnss_setting", 12) == 0) {
115 return atoi(buff + 13);
116 } else {
117 printf("gnss_setting response error.\n");
118 return -1;
119 }
b.liudbc3f4b2024-06-25 18:22:24 +0800120 } else if(cmd == GNSS_CMD_DL) {
121 if(memcmp(buff, "gnss_dl", 7) == 0) {
122 return atoi(buff + 8);
123 } else {
124 printf("gnss_dl response error.\n");
125 return -1;
126 }
b.liu5f950c52024-06-15 20:13:12 +0800127 } else {
128 printf("Unknown response.\n");
129 return -1;
130 }
b.liudbc3f4b2024-06-25 18:22:24 +0800131 } else if(len == 0) {
132 printf("RSP is null.\n");
133 return -1;
b.liu5f950c52024-06-15 20:13:12 +0800134 } else {
135 printf("read = %d:errno = %d\n", len, errno);
136 }
137 }
138}
139
140int main(int argc, char *argv[])
141{
142 int ret = 0;
143 if(argc == 2) {
144 if(strcmp(argv[1], "gnss_deinit")) {
145 help();
146 return -1;
147 }
148
149 ret = cmd_process(GNSS_CMD_DEINIT, NULL);
150 } else if(argc == 3) {
b.liud0ba7152024-06-19 14:47:21 +0800151 if(strcmp(argv[1], "gnss_init") == 0) {
152 ret = cmd_process(GNSS_CMD_INIT, argv[2]);
153 } else if(strcmp(argv[1], "gnss_setting") == 0) {
154 ret = cmd_process(GNSS_CMD_SETTING, argv[2]);
b.liudbc3f4b2024-06-25 18:22:24 +0800155 } else if(strcmp(argv[1], "gnss_dl") == 0) {
156 ret = cmd_process(GNSS_CMD_DL, argv[2]);
b.liud0ba7152024-06-19 14:47:21 +0800157 } else {
b.liu5f950c52024-06-15 20:13:12 +0800158 help();
159 return -1;
160 }
b.liu5f950c52024-06-15 20:13:12 +0800161 } else {
162 help();
163 return -1;
164 }
165
b.liud0ba7152024-06-19 14:47:21 +0800166 // printf("Error:%s\n", strerror(EBADF));
167
b.liu5f950c52024-06-15 20:13:12 +0800168 printf("Result : %d\n", ret);
169 return 0;
170}
171