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, |
| 36 | GNSS_CMD_DEINIT |
| 37 | } gnss_cmd_enum; |
| 38 | |
| 39 | static void help() |
| 40 | { |
| 41 | printf("gnss_cli gnss_init xxx\n"); |
| 42 | printf("gnss_cli gnss_deinit\n"); |
| 43 | } |
| 44 | |
| 45 | static int cmd_process(gnss_cmd_enum cmd, void *arg) |
| 46 | { |
| 47 | if(sock_listen_fd < 0) { |
| 48 | sock_listen_fd = socket(AF_LOCAL, SOCK_STREAM, 0); |
| 49 | if(sock_listen_fd < 0) |
| 50 | { |
| 51 | printf("socket() fail[%d].\n", errno); |
| 52 | return -1; |
| 53 | } |
| 54 | |
| 55 | struct sockaddr_un cli_addr; |
| 56 | memset(&cli_addr, 0, sizeof(cli_addr)); |
| 57 | cli_addr.sun_family = AF_LOCAL; |
| 58 | strcpy(cli_addr.sun_path, GNSS_SOCK_PATH); |
| 59 | if(connect(sock_listen_fd, (struct sockaddr *)&cli_addr, sizeof(cli_addr))) |
| 60 | { |
| 61 | printf("connect() fail[%d].\n", errno); |
| 62 | close(sock_listen_fd); |
| 63 | sock_listen_fd = -1; |
| 64 | return -1; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | char buff[100] = {0}; |
| 69 | if(cmd == GNSS_CMD_INIT) { |
| 70 | if(arg) { |
| 71 | int type = atoi((char*)arg); |
| 72 | sprintf(buff, "gnss_init:%d", type); |
| 73 | } else { |
| 74 | return -1; |
| 75 | } |
| 76 | } else if(cmd == GNSS_CMD_DEINIT) { |
| 77 | sprintf(buff, "gnss_deinit"); |
| 78 | } else { |
| 79 | printf("Unknown cmd.\n"); |
| 80 | return -1; |
| 81 | } |
| 82 | |
| 83 | write(sock_listen_fd, buff, strlen(buff)); |
| 84 | |
| 85 | int len = 0; |
| 86 | while(1) { |
| 87 | memset(buff, 0, sizeof(buff)); |
| 88 | len = read(sock_listen_fd, buff, sizeof(buff)); |
| 89 | if(len > 0) { |
| 90 | printf("RSP : %s\n", buff); |
| 91 | if(cmd == GNSS_CMD_INIT) { |
| 92 | if(memcmp(buff, "gnss_init", 9) == 0) { |
| 93 | return atoi(buff + 10); |
| 94 | } else { |
| 95 | printf("gnss_init response error.\n"); |
| 96 | return -1; |
| 97 | } |
| 98 | } else if(cmd == GNSS_CMD_DEINIT) { |
| 99 | if(memcmp(buff, "gnss_deinit", 11) == 0) { |
| 100 | return atoi(buff + 12); |
| 101 | } else { |
| 102 | printf("gnss_deinit response error.\n"); |
| 103 | return -1; |
| 104 | } |
| 105 | } else { |
| 106 | printf("Unknown response.\n"); |
| 107 | return -1; |
| 108 | } |
| 109 | } else { |
| 110 | printf("read = %d:errno = %d\n", len, errno); |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | int main(int argc, char *argv[]) |
| 116 | { |
| 117 | int ret = 0; |
| 118 | if(argc == 2) { |
| 119 | if(strcmp(argv[1], "gnss_deinit")) { |
| 120 | help(); |
| 121 | return -1; |
| 122 | } |
| 123 | |
| 124 | ret = cmd_process(GNSS_CMD_DEINIT, NULL); |
| 125 | } else if(argc == 3) { |
| 126 | if(strcmp(argv[1], "gnss_init")) { |
| 127 | help(); |
| 128 | return -1; |
| 129 | } |
| 130 | |
| 131 | ret = cmd_process(GNSS_CMD_INIT, argv[2]); |
| 132 | } else { |
| 133 | help(); |
| 134 | return -1; |
| 135 | } |
| 136 | |
| 137 | printf("Result : %d\n", ret); |
| 138 | return 0; |
| 139 | } |
| 140 | |