liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <unistd.h> |
| 4 | #include <errno.h> |
| 5 | #include <sys/socket.h> |
| 6 | #include <sys/un.h> |
| 7 | #include <netinet/in.h> |
| 8 | #include <pthread.h> |
| 9 | #include <sys/epoll.h> |
| 10 | #include <string.h> |
| 11 | #include <fcntl.h> |
| 12 | #include <signal.h> |
| 13 | |
| 14 | #include "ql/ql_dev.h" |
| 15 | #include "mbtk_log.h" |
| 16 | |
| 17 | static void help() |
| 18 | { |
| 19 | printf("version: Get version.\n"); |
| 20 | printf("imei: Get IMEI.\n"); |
| 21 | printf("sn: Get SN.\n"); |
| 22 | printf("model: Get Model.\n"); |
| 23 | printf("cfun: Get radio state.\n"); |
| 24 | printf("cfun <state> <rest>: Set radio state.\n"); |
| 25 | } |
| 26 | |
| 27 | static int proc_exit() |
| 28 | { |
| 29 | QL_DEV_ERROR_CODE err = ql_dev_release(); |
| 30 | if(QL_DEV_SUCCESS != err) |
| 31 | { |
| 32 | printf("ql_dev_release fail."); |
| 33 | return -1; |
| 34 | } |
| 35 | return 0; |
| 36 | } |
| 37 | |
| 38 | static void sig_process(int sig) |
| 39 | { |
| 40 | LOGI("I got signal %d\n", sig); |
| 41 | switch(sig) |
| 42 | { |
| 43 | case SIGINT: // Ctrl + C |
| 44 | { |
| 45 | LOGI("Exit by SIGINT.\n"); |
| 46 | proc_exit(); |
| 47 | exit(0); |
| 48 | } |
| 49 | case SIGQUIT: // Ctrl + \ (类似 SIGINT ,但要产生core文件) |
| 50 | { |
| 51 | LOGI("Exit by SIGQUIT.\n"); |
| 52 | proc_exit(); |
| 53 | exit(0); |
| 54 | } |
| 55 | case SIGTERM:// 默认kill (同 SIGKILL ,但 SIGKILL 不可捕获) |
| 56 | { |
| 57 | LOGI("Exit by SIGTERM.\n"); |
| 58 | proc_exit(); |
| 59 | exit(0); |
| 60 | } |
| 61 | case SIGTSTP:// Ctrl + Z (同 SIGSTOP ,但 SIGSTOP 不可捕获) |
| 62 | { |
| 63 | LOGI("Exit by SIGTSTP.\n"); |
| 64 | exit(0); |
| 65 | } |
| 66 | case SIGSEGV: // 如空指针 |
| 67 | { |
| 68 | LOGI("Exit by SIGSEGV.\n"); |
| 69 | exit(0); |
| 70 | } |
| 71 | default: |
| 72 | { |
| 73 | LOGI("Unknown sig:%d\n",sig); |
| 74 | break; |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | int main(int argc, char *argv[]) |
| 80 | { |
| 81 | signal(SIGINT, sig_process); |
| 82 | signal(SIGQUIT, sig_process); |
| 83 | signal(SIGTERM, sig_process); |
| 84 | //signal(SIGTSTP, sig_process); |
| 85 | //signal(SIGSEGV, sig_process); |
| 86 | |
| 87 | mbtk_log_init(NULL,"MBTK_QL_TEST"); |
| 88 | |
| 89 | //test2(0, "192.168.1.198"); |
| 90 | //test2(1, "2409:8162:140:cd3c:1:2:1494:72ba"); |
| 91 | //test2(1, "254.128.0.0.0.0.0.0.0.1.0.2.144.5.212.239"); |
| 92 | //test2(1, "2400:3200::1"); |
| 93 | |
| 94 | QL_DEV_ERROR_CODE err = ql_dev_init(); |
| 95 | if(QL_DEV_SUCCESS != err) |
| 96 | { |
| 97 | printf("ql_dev_init fail."); |
| 98 | return -1; |
| 99 | } |
| 100 | |
| 101 | printf(">>>>>>>>>>>>>>>>>>>>>>>>Enter cmd:\n"); |
| 102 | char cmd[100]; |
| 103 | while(1) |
| 104 | { |
| 105 | memset(cmd, 0, 100); |
| 106 | if(fgets(cmd, 100, stdin)) |
| 107 | { |
| 108 | char *ptr = cmd + strlen(cmd) - 1; |
| 109 | while(ptr >= cmd && (*ptr == '\r' || *ptr == '\n')) |
| 110 | { |
| 111 | *ptr-- = '\0'; |
| 112 | } |
| 113 | |
| 114 | if(!strncasecmp(cmd, "version", 7)) |
| 115 | { |
| 116 | char version[50] = {0}; |
| 117 | err = ql_dev_get_firmware_version(version); |
| 118 | if(err) { |
| 119 | printf("Error : %d\n", err); |
| 120 | } else { |
| 121 | printf("Version : %s\n", version); |
| 122 | } |
| 123 | } else if(!strncasecmp(cmd, "imei", 4)){ |
| 124 | char imei[50] = {0}; |
| 125 | err = ql_dev_get_imei(imei); |
| 126 | if(err) { |
| 127 | printf("Error : %d\n", err); |
| 128 | } else { |
| 129 | printf("IMEI : %s\n", imei); |
| 130 | } |
| 131 | } else if(!strncasecmp(cmd, "sn", 2)){ |
| 132 | char sn[50] = {0}; |
| 133 | err = ql_dev_get_sn(sn); |
| 134 | if(err) { |
| 135 | printf("Error : %d\n", err); |
| 136 | } else { |
| 137 | printf("SN : %s\n", sn); |
| 138 | } |
| 139 | } else if(!strncasecmp(cmd, "model", 5)){ |
| 140 | char model[50] = {0}; |
| 141 | err = ql_dev_get_model(model); |
| 142 | if(err) { |
| 143 | printf("Error : %d\n", err); |
| 144 | } else { |
| 145 | printf("Model : %s\n", model); |
| 146 | } |
| 147 | } else if(!strncasecmp(cmd, "cfun", 4)){ |
| 148 | int cfun; |
| 149 | if(!strcasecmp(cmd, "cfun")) { // Get |
| 150 | err = ql_dev_get_modem_fun(&cfun); |
| 151 | if(err) { |
| 152 | printf("Error : %d\n", err); |
| 153 | } else { |
| 154 | printf("Cfun : %d\n", cfun); |
| 155 | } |
| 156 | } else { // Set |
| 157 | char *ptr = strstr(cmd, " "); |
| 158 | if(ptr == NULL) |
| 159 | continue; |
| 160 | while(*ptr != '\0' && *ptr == ' ') |
| 161 | ptr++; |
| 162 | cfun = atoi(ptr); |
| 163 | |
| 164 | ptr = strstr(ptr, " "); |
| 165 | if(ptr == NULL) |
| 166 | continue; |
| 167 | while(*ptr != '\0' && *ptr == ' ') |
| 168 | ptr++; |
| 169 | int rst = atoi(ptr); |
| 170 | err = ql_dev_set_modem_fun((QL_DEV_MODEM_FUNCTION)cfun, rst); |
| 171 | if(err) { |
| 172 | printf("Error : %d\n", err); |
| 173 | } else { |
| 174 | printf("cfun set success\n"); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | else if(!strcasecmp(cmd, "h") || !strcasecmp(cmd, "help")) { |
| 179 | help(); |
| 180 | } else if(!strcasecmp(cmd, "q")) { |
| 181 | break; |
| 182 | } else { |
| 183 | printf("\n"); |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | proc_exit(); |
| 189 | |
| 190 | LOGI("Client exec complete."); |
| 191 | #if 1 |
| 192 | while(1) |
| 193 | { |
| 194 | sleep(1000 * 365 * 24 * 60 * 60); |
| 195 | } |
| 196 | #else |
| 197 | sleep(1); |
| 198 | #endif |
| 199 | return 0; |
| 200 | } |
| 201 | |