blob: 8699a393d4f701c764aba4de2c64ce69808584d9 [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.liu5f950c52024-06-15 20:13:12 +080030
31#define GNSS_SOCK_PATH "/tmp/mbtk_gnss_sock"
32
33static int sock_listen_fd = -1;
34
35typedef enum {
36 GNSS_CMD_INIT = 0,
b.liud0ba7152024-06-19 14:47:21 +080037 GNSS_CMD_DEINIT,
b.liudbc3f4b2024-06-25 18:22:24 +080038 GNSS_CMD_SETTING,
39 GNSS_CMD_DL
b.liu5f950c52024-06-15 20:13:12 +080040} gnss_cmd_enum;
41
42static void help()
43{
b.liud0ba7152024-06-19 14:47:21 +080044 printf("gnss_cli gnss_init <0-15>\n");
b.liu5f950c52024-06-15 20:13:12 +080045 printf("gnss_cli gnss_deinit\n");
b.liud0ba7152024-06-19 14:47:21 +080046 printf("gnss_cli gnss_setting cmd\n");
b.liudbc3f4b2024-06-25 18:22:24 +080047 printf("gnss_cli gnss_dl fw_name\n");
b.liu5f950c52024-06-15 20:13:12 +080048}
49
50static int cmd_process(gnss_cmd_enum cmd, void *arg)
51{
52 if(sock_listen_fd < 0) {
53 sock_listen_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
54 if(sock_listen_fd < 0)
55 {
56 printf("socket() fail[%d].\n", errno);
57 return -1;
58 }
59
60 struct sockaddr_un cli_addr;
61 memset(&cli_addr, 0, sizeof(cli_addr));
62 cli_addr.sun_family = AF_LOCAL;
63 strcpy(cli_addr.sun_path, GNSS_SOCK_PATH);
64 if(connect(sock_listen_fd, (struct sockaddr *)&cli_addr, sizeof(cli_addr)))
65 {
66 printf("connect() fail[%d].\n", errno);
67 close(sock_listen_fd);
68 sock_listen_fd = -1;
69 return -1;
70 }
71 }
72
73 char buff[100] = {0};
74 if(cmd == GNSS_CMD_INIT) {
75 if(arg) {
76 int type = atoi((char*)arg);
77 sprintf(buff, "gnss_init:%d", type);
78 } else {
79 return -1;
80 }
81 } else if(cmd == GNSS_CMD_DEINIT) {
82 sprintf(buff, "gnss_deinit");
b.liud0ba7152024-06-19 14:47:21 +080083 } else if(cmd == GNSS_CMD_SETTING) {
84 sprintf(buff, "gnss_setting:%s", arg);
b.liudbc3f4b2024-06-25 18:22:24 +080085 } else if(cmd == GNSS_CMD_DL) {
86 sprintf(buff, "gnss_dl:%s", arg);
b.liu5f950c52024-06-15 20:13:12 +080087 } else {
88 printf("Unknown cmd.\n");
89 return -1;
90 }
91
92 write(sock_listen_fd, buff, strlen(buff));
93
94 int len = 0;
b.liue77ac3a2024-07-17 17:36:57 +080095 char *rsp = NULL;
b.liu5f950c52024-06-15 20:13:12 +080096 while(1) {
97 memset(buff, 0, sizeof(buff));
98 len = read(sock_listen_fd, buff, sizeof(buff));
99 if(len > 0) {
b.liue77ac3a2024-07-17 17:36:57 +0800100 rsp = buff;
101 if(rsp[len - 1] == MBTK_IND_END_FLAG) {
102 rsp[len - 1] = '\0';
103 }
104 if(rsp[0] == MBTK_IND_START_FLAG) {
105 rsp++;
106 }
107 printf("RSP : %s\n", rsp);
b.liu5f950c52024-06-15 20:13:12 +0800108 if(cmd == GNSS_CMD_INIT) {
b.liue77ac3a2024-07-17 17:36:57 +0800109 if(memcmp(rsp, "gnss_init", 9) == 0) {
110 return atoi(rsp + 10);
b.liu5f950c52024-06-15 20:13:12 +0800111 } else {
112 printf("gnss_init response error.\n");
113 return -1;
114 }
115 } else if(cmd == GNSS_CMD_DEINIT) {
b.liue77ac3a2024-07-17 17:36:57 +0800116 if(memcmp(rsp, "gnss_deinit", 11) == 0) {
117 return atoi(rsp + 12);
b.liu5f950c52024-06-15 20:13:12 +0800118 } else {
119 printf("gnss_deinit response error.\n");
120 return -1;
121 }
b.liud0ba7152024-06-19 14:47:21 +0800122 } else if(cmd == GNSS_CMD_SETTING) {
b.liue77ac3a2024-07-17 17:36:57 +0800123 if(memcmp(rsp, "gnss_setting", 12) == 0) {
124 return atoi(rsp + 13);
b.liud0ba7152024-06-19 14:47:21 +0800125 } else {
126 printf("gnss_setting response error.\n");
127 return -1;
128 }
b.liudbc3f4b2024-06-25 18:22:24 +0800129 } else if(cmd == GNSS_CMD_DL) {
b.liue77ac3a2024-07-17 17:36:57 +0800130 if(memcmp(rsp, "gnss_dl", 7) == 0) {
131 return atoi(rsp + 8);
b.liudbc3f4b2024-06-25 18:22:24 +0800132 } else {
133 printf("gnss_dl response error.\n");
134 return -1;
135 }
b.liu5f950c52024-06-15 20:13:12 +0800136 } else {
137 printf("Unknown response.\n");
138 return -1;
139 }
b.liudbc3f4b2024-06-25 18:22:24 +0800140 } else if(len == 0) {
141 printf("RSP is null.\n");
142 return -1;
b.liu5f950c52024-06-15 20:13:12 +0800143 } else {
144 printf("read = %d:errno = %d\n", len, errno);
145 }
146 }
147}
148
149int main(int argc, char *argv[])
150{
151 int ret = 0;
152 if(argc == 2) {
153 if(strcmp(argv[1], "gnss_deinit")) {
154 help();
155 return -1;
156 }
157
158 ret = cmd_process(GNSS_CMD_DEINIT, NULL);
159 } else if(argc == 3) {
b.liud0ba7152024-06-19 14:47:21 +0800160 if(strcmp(argv[1], "gnss_init") == 0) {
161 ret = cmd_process(GNSS_CMD_INIT, argv[2]);
162 } else if(strcmp(argv[1], "gnss_setting") == 0) {
163 ret = cmd_process(GNSS_CMD_SETTING, argv[2]);
b.liudbc3f4b2024-06-25 18:22:24 +0800164 } else if(strcmp(argv[1], "gnss_dl") == 0) {
165 ret = cmd_process(GNSS_CMD_DL, argv[2]);
b.liud0ba7152024-06-19 14:47:21 +0800166 } else {
b.liu5f950c52024-06-15 20:13:12 +0800167 help();
168 return -1;
169 }
b.liu5f950c52024-06-15 20:13:12 +0800170 } else {
171 help();
172 return -1;
173 }
174
b.liud0ba7152024-06-19 14:47:21 +0800175 // printf("Error:%s\n", strerror(EBADF));
176
b.liu5f950c52024-06-15 20:13:12 +0800177 printf("Result : %d\n", ret);
178 return 0;
179}
180