blob: 825d5015c94d81e0745f5669a1b08483a067e084 [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"
b.liue77ac3a2024-07-17 17:36:57 +080029#include "mbtk_gnss.h"
b.liu9e8584b2024-11-06 19:21:28 +080030#include "mbtk_utils.h"
b.liu5f950c52024-06-15 20:13:12 +080031
32#define GNSS_SOCK_PATH "/tmp/mbtk_gnss_sock"
33
34static int sock_listen_fd = -1;
35
36typedef enum {
37 GNSS_CMD_INIT = 0,
b.liud0ba7152024-06-19 14:47:21 +080038 GNSS_CMD_DEINIT,
b.liudbc3f4b2024-06-25 18:22:24 +080039 GNSS_CMD_SETTING,
40 GNSS_CMD_DL
b.liu5f950c52024-06-15 20:13:12 +080041} gnss_cmd_enum;
42
43static void help()
44{
b.liud0ba7152024-06-19 14:47:21 +080045 printf("gnss_cli gnss_init <0-15>\n");
b.liu5f950c52024-06-15 20:13:12 +080046 printf("gnss_cli gnss_deinit\n");
b.liud0ba7152024-06-19 14:47:21 +080047 printf("gnss_cli gnss_setting cmd\n");
b.liudbc3f4b2024-06-25 18:22:24 +080048 printf("gnss_cli gnss_dl fw_name\n");
b.liu5f950c52024-06-15 20:13:12 +080049}
50
51static int cmd_process(gnss_cmd_enum cmd, void *arg)
52{
53 if(sock_listen_fd < 0) {
54 sock_listen_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
55 if(sock_listen_fd < 0)
56 {
57 printf("socket() fail[%d].\n", errno);
58 return -1;
59 }
60
61 struct sockaddr_un cli_addr;
62 memset(&cli_addr, 0, sizeof(cli_addr));
63 cli_addr.sun_family = AF_LOCAL;
64 strcpy(cli_addr.sun_path, GNSS_SOCK_PATH);
65 if(connect(sock_listen_fd, (struct sockaddr *)&cli_addr, sizeof(cli_addr)))
66 {
67 printf("connect() fail[%d].\n", errno);
68 close(sock_listen_fd);
69 sock_listen_fd = -1;
70 return -1;
71 }
72 }
73
74 char buff[100] = {0};
75 if(cmd == GNSS_CMD_INIT) {
76 if(arg) {
77 int type = atoi((char*)arg);
78 sprintf(buff, "gnss_init:%d", type);
79 } else {
80 return -1;
81 }
82 } else if(cmd == GNSS_CMD_DEINIT) {
83 sprintf(buff, "gnss_deinit");
b.liud0ba7152024-06-19 14:47:21 +080084 } else if(cmd == GNSS_CMD_SETTING) {
b.liu9e8584b2024-11-06 19:21:28 +080085 sprintf(buff, "gnss_setting:%s", (char*)arg);
b.liudbc3f4b2024-06-25 18:22:24 +080086 } else if(cmd == GNSS_CMD_DL) {
b.liu9e8584b2024-11-06 19:21:28 +080087 sprintf(buff, "gnss_dl:%s", (char*)arg);
b.liu5f950c52024-06-15 20:13:12 +080088 } else {
89 printf("Unknown cmd.\n");
90 return -1;
91 }
92
b.liu9e8584b2024-11-06 19:21:28 +080093 mbtk_write(sock_listen_fd, buff, strlen(buff));
b.liu5f950c52024-06-15 20:13:12 +080094
95 int len = 0;
b.liue77ac3a2024-07-17 17:36:57 +080096 char *rsp = NULL;
b.liu5f950c52024-06-15 20:13:12 +080097 while(1) {
98 memset(buff, 0, sizeof(buff));
99 len = read(sock_listen_fd, buff, sizeof(buff));
100 if(len > 0) {
b.liue77ac3a2024-07-17 17:36:57 +0800101 rsp = buff;
102 if(rsp[len - 1] == MBTK_IND_END_FLAG) {
103 rsp[len - 1] = '\0';
104 }
105 if(rsp[0] == MBTK_IND_START_FLAG) {
106 rsp++;
107 }
108 printf("RSP : %s\n", rsp);
b.liu5f950c52024-06-15 20:13:12 +0800109 if(cmd == GNSS_CMD_INIT) {
b.liue77ac3a2024-07-17 17:36:57 +0800110 if(memcmp(rsp, "gnss_init", 9) == 0) {
111 return atoi(rsp + 10);
b.liu5f950c52024-06-15 20:13:12 +0800112 } else {
113 printf("gnss_init response error.\n");
114 return -1;
115 }
116 } else if(cmd == GNSS_CMD_DEINIT) {
b.liue77ac3a2024-07-17 17:36:57 +0800117 if(memcmp(rsp, "gnss_deinit", 11) == 0) {
118 return atoi(rsp + 12);
b.liu5f950c52024-06-15 20:13:12 +0800119 } else {
120 printf("gnss_deinit response error.\n");
121 return -1;
122 }
b.liud0ba7152024-06-19 14:47:21 +0800123 } else if(cmd == GNSS_CMD_SETTING) {
b.liue77ac3a2024-07-17 17:36:57 +0800124 if(memcmp(rsp, "gnss_setting", 12) == 0) {
125 return atoi(rsp + 13);
b.liud0ba7152024-06-19 14:47:21 +0800126 } else {
127 printf("gnss_setting response error.\n");
128 return -1;
129 }
b.liudbc3f4b2024-06-25 18:22:24 +0800130 } else if(cmd == GNSS_CMD_DL) {
b.liue77ac3a2024-07-17 17:36:57 +0800131 if(memcmp(rsp, "gnss_dl", 7) == 0) {
132 return atoi(rsp + 8);
b.liudbc3f4b2024-06-25 18:22:24 +0800133 } else {
134 printf("gnss_dl response error.\n");
135 return -1;
136 }
b.liu5f950c52024-06-15 20:13:12 +0800137 } else {
138 printf("Unknown response.\n");
139 return -1;
140 }
b.liudbc3f4b2024-06-25 18:22:24 +0800141 } else if(len == 0) {
142 printf("RSP is null.\n");
143 return -1;
b.liu5f950c52024-06-15 20:13:12 +0800144 } else {
145 printf("read = %d:errno = %d\n", len, errno);
146 }
147 }
148}
149
150int main(int argc, char *argv[])
151{
152 int ret = 0;
153 if(argc == 2) {
154 if(strcmp(argv[1], "gnss_deinit")) {
155 help();
156 return -1;
157 }
158
159 ret = cmd_process(GNSS_CMD_DEINIT, NULL);
160 } else if(argc == 3) {
b.liud0ba7152024-06-19 14:47:21 +0800161 if(strcmp(argv[1], "gnss_init") == 0) {
162 ret = cmd_process(GNSS_CMD_INIT, argv[2]);
163 } else if(strcmp(argv[1], "gnss_setting") == 0) {
164 ret = cmd_process(GNSS_CMD_SETTING, argv[2]);
b.liudbc3f4b2024-06-25 18:22:24 +0800165 } else if(strcmp(argv[1], "gnss_dl") == 0) {
166 ret = cmd_process(GNSS_CMD_DL, argv[2]);
b.liud0ba7152024-06-19 14:47:21 +0800167 } else {
b.liu5f950c52024-06-15 20:13:12 +0800168 help();
169 return -1;
170 }
b.liu5f950c52024-06-15 20:13:12 +0800171 } else {
172 help();
173 return -1;
174 }
175
b.liud0ba7152024-06-19 14:47:21 +0800176 // printf("Error:%s\n", strerror(EBADF));
177
b.liu5f950c52024-06-15 20:13:12 +0800178 printf("Result : %d\n", ret);
179 return 0;
180}
181