b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 1 | /* |
| 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 | -------- -------- ------------------------------------------------------- |
| 13 | 2024/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.liu | e77ac3a | 2024-07-17 17:36:57 +0800 | [diff] [blame^] | 29 | #include "mbtk_gnss.h" |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 30 | |
| 31 | #define GNSS_SOCK_PATH "/tmp/mbtk_gnss_sock" |
| 32 | |
| 33 | static int sock_listen_fd = -1; |
| 34 | |
| 35 | typedef enum { |
| 36 | GNSS_CMD_INIT = 0, |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 37 | GNSS_CMD_DEINIT, |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 38 | GNSS_CMD_SETTING, |
| 39 | GNSS_CMD_DL |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 40 | } gnss_cmd_enum; |
| 41 | |
| 42 | static void help() |
| 43 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 44 | printf("gnss_cli gnss_init <0-15>\n"); |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 45 | printf("gnss_cli gnss_deinit\n"); |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 46 | printf("gnss_cli gnss_setting cmd\n"); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 47 | printf("gnss_cli gnss_dl fw_name\n"); |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | static 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.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 83 | } else if(cmd == GNSS_CMD_SETTING) { |
| 84 | sprintf(buff, "gnss_setting:%s", arg); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 85 | } else if(cmd == GNSS_CMD_DL) { |
| 86 | sprintf(buff, "gnss_dl:%s", arg); |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 87 | } 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.liu | e77ac3a | 2024-07-17 17:36:57 +0800 | [diff] [blame^] | 95 | char *rsp = NULL; |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 96 | while(1) { |
| 97 | memset(buff, 0, sizeof(buff)); |
| 98 | len = read(sock_listen_fd, buff, sizeof(buff)); |
| 99 | if(len > 0) { |
b.liu | e77ac3a | 2024-07-17 17:36:57 +0800 | [diff] [blame^] | 100 | 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.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 108 | if(cmd == GNSS_CMD_INIT) { |
b.liu | e77ac3a | 2024-07-17 17:36:57 +0800 | [diff] [blame^] | 109 | if(memcmp(rsp, "gnss_init", 9) == 0) { |
| 110 | return atoi(rsp + 10); |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 111 | } else { |
| 112 | printf("gnss_init response error.\n"); |
| 113 | return -1; |
| 114 | } |
| 115 | } else if(cmd == GNSS_CMD_DEINIT) { |
b.liu | e77ac3a | 2024-07-17 17:36:57 +0800 | [diff] [blame^] | 116 | if(memcmp(rsp, "gnss_deinit", 11) == 0) { |
| 117 | return atoi(rsp + 12); |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 118 | } else { |
| 119 | printf("gnss_deinit response error.\n"); |
| 120 | return -1; |
| 121 | } |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 122 | } else if(cmd == GNSS_CMD_SETTING) { |
b.liu | e77ac3a | 2024-07-17 17:36:57 +0800 | [diff] [blame^] | 123 | if(memcmp(rsp, "gnss_setting", 12) == 0) { |
| 124 | return atoi(rsp + 13); |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 125 | } else { |
| 126 | printf("gnss_setting response error.\n"); |
| 127 | return -1; |
| 128 | } |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 129 | } else if(cmd == GNSS_CMD_DL) { |
b.liu | e77ac3a | 2024-07-17 17:36:57 +0800 | [diff] [blame^] | 130 | if(memcmp(rsp, "gnss_dl", 7) == 0) { |
| 131 | return atoi(rsp + 8); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 132 | } else { |
| 133 | printf("gnss_dl response error.\n"); |
| 134 | return -1; |
| 135 | } |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 136 | } else { |
| 137 | printf("Unknown response.\n"); |
| 138 | return -1; |
| 139 | } |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 140 | } else if(len == 0) { |
| 141 | printf("RSP is null.\n"); |
| 142 | return -1; |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 143 | } else { |
| 144 | printf("read = %d:errno = %d\n", len, errno); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | int 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.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 160 | 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.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 164 | } else if(strcmp(argv[1], "gnss_dl") == 0) { |
| 165 | ret = cmd_process(GNSS_CMD_DL, argv[2]); |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 166 | } else { |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 167 | help(); |
| 168 | return -1; |
| 169 | } |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 170 | } else { |
| 171 | help(); |
| 172 | return -1; |
| 173 | } |
| 174 | |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 175 | // printf("Error:%s\n", strerror(EBADF)); |
| 176 | |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 177 | printf("Result : %d\n", ret); |
| 178 | return 0; |
| 179 | } |
| 180 | |