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" |
| 29 | |
| 30 | #define GNSS_SOCK_PATH "/tmp/mbtk_gnss_sock" |
| 31 | |
| 32 | static int sock_listen_fd = -1; |
| 33 | |
| 34 | typedef enum { |
| 35 | GNSS_CMD_INIT = 0, |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 36 | GNSS_CMD_DEINIT, |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame^] | 37 | GNSS_CMD_SETTING, |
| 38 | GNSS_CMD_DL |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 39 | } gnss_cmd_enum; |
| 40 | |
| 41 | static void help() |
| 42 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 43 | printf("gnss_cli gnss_init <0-15>\n"); |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 44 | printf("gnss_cli gnss_deinit\n"); |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 45 | printf("gnss_cli gnss_setting cmd\n"); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame^] | 46 | printf("gnss_cli gnss_dl fw_name\n"); |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | static int cmd_process(gnss_cmd_enum cmd, void *arg) |
| 50 | { |
| 51 | if(sock_listen_fd < 0) { |
| 52 | sock_listen_fd = socket(AF_LOCAL, SOCK_STREAM, 0); |
| 53 | if(sock_listen_fd < 0) |
| 54 | { |
| 55 | printf("socket() fail[%d].\n", errno); |
| 56 | return -1; |
| 57 | } |
| 58 | |
| 59 | struct sockaddr_un cli_addr; |
| 60 | memset(&cli_addr, 0, sizeof(cli_addr)); |
| 61 | cli_addr.sun_family = AF_LOCAL; |
| 62 | strcpy(cli_addr.sun_path, GNSS_SOCK_PATH); |
| 63 | if(connect(sock_listen_fd, (struct sockaddr *)&cli_addr, sizeof(cli_addr))) |
| 64 | { |
| 65 | printf("connect() fail[%d].\n", errno); |
| 66 | close(sock_listen_fd); |
| 67 | sock_listen_fd = -1; |
| 68 | return -1; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | char buff[100] = {0}; |
| 73 | if(cmd == GNSS_CMD_INIT) { |
| 74 | if(arg) { |
| 75 | int type = atoi((char*)arg); |
| 76 | sprintf(buff, "gnss_init:%d", type); |
| 77 | } else { |
| 78 | return -1; |
| 79 | } |
| 80 | } else if(cmd == GNSS_CMD_DEINIT) { |
| 81 | sprintf(buff, "gnss_deinit"); |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 82 | } else if(cmd == GNSS_CMD_SETTING) { |
| 83 | sprintf(buff, "gnss_setting:%s", arg); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame^] | 84 | } else if(cmd == GNSS_CMD_DL) { |
| 85 | sprintf(buff, "gnss_dl:%s", arg); |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 86 | } else { |
| 87 | printf("Unknown cmd.\n"); |
| 88 | return -1; |
| 89 | } |
| 90 | |
| 91 | write(sock_listen_fd, buff, strlen(buff)); |
| 92 | |
| 93 | int len = 0; |
| 94 | while(1) { |
| 95 | memset(buff, 0, sizeof(buff)); |
| 96 | len = read(sock_listen_fd, buff, sizeof(buff)); |
| 97 | if(len > 0) { |
| 98 | printf("RSP : %s\n", buff); |
| 99 | if(cmd == GNSS_CMD_INIT) { |
| 100 | if(memcmp(buff, "gnss_init", 9) == 0) { |
| 101 | return atoi(buff + 10); |
| 102 | } else { |
| 103 | printf("gnss_init response error.\n"); |
| 104 | return -1; |
| 105 | } |
| 106 | } else if(cmd == GNSS_CMD_DEINIT) { |
| 107 | if(memcmp(buff, "gnss_deinit", 11) == 0) { |
| 108 | return atoi(buff + 12); |
| 109 | } else { |
| 110 | printf("gnss_deinit response error.\n"); |
| 111 | return -1; |
| 112 | } |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 113 | } else if(cmd == GNSS_CMD_SETTING) { |
| 114 | if(memcmp(buff, "gnss_setting", 12) == 0) { |
| 115 | return atoi(buff + 13); |
| 116 | } else { |
| 117 | printf("gnss_setting response error.\n"); |
| 118 | return -1; |
| 119 | } |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame^] | 120 | } else if(cmd == GNSS_CMD_DL) { |
| 121 | if(memcmp(buff, "gnss_dl", 7) == 0) { |
| 122 | return atoi(buff + 8); |
| 123 | } else { |
| 124 | printf("gnss_dl response error.\n"); |
| 125 | return -1; |
| 126 | } |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 127 | } else { |
| 128 | printf("Unknown response.\n"); |
| 129 | return -1; |
| 130 | } |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame^] | 131 | } else if(len == 0) { |
| 132 | printf("RSP is null.\n"); |
| 133 | return -1; |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 134 | } else { |
| 135 | printf("read = %d:errno = %d\n", len, errno); |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | int main(int argc, char *argv[]) |
| 141 | { |
| 142 | int ret = 0; |
| 143 | if(argc == 2) { |
| 144 | if(strcmp(argv[1], "gnss_deinit")) { |
| 145 | help(); |
| 146 | return -1; |
| 147 | } |
| 148 | |
| 149 | ret = cmd_process(GNSS_CMD_DEINIT, NULL); |
| 150 | } else if(argc == 3) { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 151 | if(strcmp(argv[1], "gnss_init") == 0) { |
| 152 | ret = cmd_process(GNSS_CMD_INIT, argv[2]); |
| 153 | } else if(strcmp(argv[1], "gnss_setting") == 0) { |
| 154 | ret = cmd_process(GNSS_CMD_SETTING, argv[2]); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame^] | 155 | } else if(strcmp(argv[1], "gnss_dl") == 0) { |
| 156 | ret = cmd_process(GNSS_CMD_DL, argv[2]); |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 157 | } else { |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 158 | help(); |
| 159 | return -1; |
| 160 | } |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 161 | } else { |
| 162 | help(); |
| 163 | return -1; |
| 164 | } |
| 165 | |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 166 | // printf("Error:%s\n", strerror(EBADF)); |
| 167 | |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 168 | printf("Result : %d\n", ret); |
| 169 | return 0; |
| 170 | } |
| 171 | |