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