blob: 91c4e7c63739a3f999e20f0640480ee0a9ffa9f5 [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,
37 GNSS_CMD_SETTING
b.liu5f950c52024-06-15 20:13:12 +080038} gnss_cmd_enum;
39
40static void help()
41{
b.liud0ba7152024-06-19 14:47:21 +080042 printf("gnss_cli gnss_init <0-15>\n");
b.liu5f950c52024-06-15 20:13:12 +080043 printf("gnss_cli gnss_deinit\n");
b.liud0ba7152024-06-19 14:47:21 +080044 printf("gnss_cli gnss_setting cmd\n");
b.liu5f950c52024-06-15 20:13:12 +080045}
46
47static int cmd_process(gnss_cmd_enum cmd, void *arg)
48{
49 if(sock_listen_fd < 0) {
50 sock_listen_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
51 if(sock_listen_fd < 0)
52 {
53 printf("socket() fail[%d].\n", errno);
54 return -1;
55 }
56
57 struct sockaddr_un cli_addr;
58 memset(&cli_addr, 0, sizeof(cli_addr));
59 cli_addr.sun_family = AF_LOCAL;
60 strcpy(cli_addr.sun_path, GNSS_SOCK_PATH);
61 if(connect(sock_listen_fd, (struct sockaddr *)&cli_addr, sizeof(cli_addr)))
62 {
63 printf("connect() fail[%d].\n", errno);
64 close(sock_listen_fd);
65 sock_listen_fd = -1;
66 return -1;
67 }
68 }
69
70 char buff[100] = {0};
71 if(cmd == GNSS_CMD_INIT) {
72 if(arg) {
73 int type = atoi((char*)arg);
74 sprintf(buff, "gnss_init:%d", type);
75 } else {
76 return -1;
77 }
78 } else if(cmd == GNSS_CMD_DEINIT) {
79 sprintf(buff, "gnss_deinit");
b.liud0ba7152024-06-19 14:47:21 +080080 } else if(cmd == GNSS_CMD_SETTING) {
81 sprintf(buff, "gnss_setting:%s", arg);
b.liu5f950c52024-06-15 20:13:12 +080082 } else {
83 printf("Unknown cmd.\n");
84 return -1;
85 }
86
87 write(sock_listen_fd, buff, strlen(buff));
88
89 int len = 0;
90 while(1) {
91 memset(buff, 0, sizeof(buff));
92 len = read(sock_listen_fd, buff, sizeof(buff));
93 if(len > 0) {
94 printf("RSP : %s\n", buff);
95 if(cmd == GNSS_CMD_INIT) {
96 if(memcmp(buff, "gnss_init", 9) == 0) {
97 return atoi(buff + 10);
98 } else {
99 printf("gnss_init response error.\n");
100 return -1;
101 }
102 } else if(cmd == GNSS_CMD_DEINIT) {
103 if(memcmp(buff, "gnss_deinit", 11) == 0) {
104 return atoi(buff + 12);
105 } else {
106 printf("gnss_deinit response error.\n");
107 return -1;
108 }
b.liud0ba7152024-06-19 14:47:21 +0800109 } else if(cmd == GNSS_CMD_SETTING) {
110 if(memcmp(buff, "gnss_setting", 12) == 0) {
111 return atoi(buff + 13);
112 } else {
113 printf("gnss_setting response error.\n");
114 return -1;
115 }
b.liu5f950c52024-06-15 20:13:12 +0800116 } else {
117 printf("Unknown response.\n");
118 return -1;
119 }
120 } else {
121 printf("read = %d:errno = %d\n", len, errno);
122 }
123 }
124}
125
126int main(int argc, char *argv[])
127{
128 int ret = 0;
129 if(argc == 2) {
130 if(strcmp(argv[1], "gnss_deinit")) {
131 help();
132 return -1;
133 }
134
135 ret = cmd_process(GNSS_CMD_DEINIT, NULL);
136 } else if(argc == 3) {
b.liud0ba7152024-06-19 14:47:21 +0800137 if(strcmp(argv[1], "gnss_init") == 0) {
138 ret = cmd_process(GNSS_CMD_INIT, argv[2]);
139 } else if(strcmp(argv[1], "gnss_setting") == 0) {
140 ret = cmd_process(GNSS_CMD_SETTING, argv[2]);
141 } else {
b.liu5f950c52024-06-15 20:13:12 +0800142 help();
143 return -1;
144 }
b.liu5f950c52024-06-15 20:13:12 +0800145 } else {
146 help();
147 return -1;
148 }
149
b.liud0ba7152024-06-19 14:47:21 +0800150 // printf("Error:%s\n", strerror(EBADF));
151
b.liu5f950c52024-06-15 20:13:12 +0800152 printf("Result : %d\n", ret);
153 return 0;
154}
155