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_sim.h" |
| 15 | #include "mbtk_log.h" |
| 16 | |
| 17 | static void help() |
| 18 | { |
| 19 | printf("imsi : Get IMSI.\n"); |
| 20 | printf("iccid : Get ICCID.\n"); |
| 21 | printf("pn : Get phone number.\n"); |
| 22 | printf("pin_en <pin> : Enable pin.\n"); |
| 23 | printf("pin_dis <pin> : Disable pin.\n"); |
| 24 | printf("pin_ch <old_pin> <new_pin> : Change pin.\n"); |
| 25 | printf("pin_verify <pin> : Verify pin.\n"); |
| 26 | printf("puk_unlock <puk> <new_pin> : Unlock using PUK.\n"); |
| 27 | printf("sim : Get sim state.\n"); |
| 28 | } |
| 29 | |
| 30 | static int proc_exit() |
| 31 | { |
| 32 | QL_SIM_ERROR_CODE err = ql_sim_release(); |
| 33 | if(QL_SIM_SUCCESS != err) |
| 34 | { |
| 35 | printf("ql_sim_release fail."); |
| 36 | return -1; |
| 37 | } |
| 38 | return 0; |
| 39 | } |
| 40 | |
| 41 | static void sig_process(int sig) |
| 42 | { |
| 43 | LOGI("I got signal %d\n", sig); |
| 44 | switch(sig) |
| 45 | { |
| 46 | case SIGINT: // Ctrl + C |
| 47 | { |
| 48 | LOGI("Exit by SIGINT.\n"); |
| 49 | proc_exit(); |
| 50 | exit(0); |
| 51 | } |
| 52 | case SIGQUIT: // Ctrl + \ (类似 SIGINT ,但要产生core文件) |
| 53 | { |
| 54 | LOGI("Exit by SIGQUIT.\n"); |
| 55 | proc_exit(); |
| 56 | exit(0); |
| 57 | } |
| 58 | case SIGTERM:// 默认kill (同 SIGKILL ,但 SIGKILL 不可捕获) |
| 59 | { |
| 60 | LOGI("Exit by SIGTERM.\n"); |
| 61 | proc_exit(); |
| 62 | exit(0); |
| 63 | } |
| 64 | case SIGTSTP:// Ctrl + Z (同 SIGSTOP ,但 SIGSTOP 不可捕获) |
| 65 | { |
| 66 | LOGI("Exit by SIGTSTP.\n"); |
| 67 | exit(0); |
| 68 | } |
| 69 | case SIGSEGV: // 如空指针 |
| 70 | { |
| 71 | LOGI("Exit by SIGSEGV.\n"); |
| 72 | exit(0); |
| 73 | } |
| 74 | default: |
| 75 | { |
| 76 | LOGI("Unknown sig:%d\n",sig); |
| 77 | break; |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | int main(int argc, char *argv[]) |
| 83 | { |
| 84 | signal(SIGINT, sig_process); |
| 85 | signal(SIGQUIT, sig_process); |
| 86 | signal(SIGTERM, sig_process); |
| 87 | //signal(SIGTSTP, sig_process); |
| 88 | //signal(SIGSEGV, sig_process); |
| 89 | |
| 90 | mbtk_log_init(NULL,"MBTK_QL_TEST"); |
| 91 | |
| 92 | //test2(0, "192.168.1.198"); |
| 93 | //test2(1, "2409:8162:140:cd3c:1:2:1494:72ba"); |
| 94 | //test2(1, "254.128.0.0.0.0.0.0.0.1.0.2.144.5.212.239"); |
| 95 | //test2(1, "2400:3200::1"); |
| 96 | |
| 97 | QL_SIM_ERROR_CODE err = ql_sim_init(); |
| 98 | if(QL_SIM_SUCCESS != err) |
| 99 | { |
| 100 | printf("ql_sim_init fail."); |
| 101 | return -1; |
| 102 | } |
| 103 | |
| 104 | printf(">>>>>>>>>>>>>>>>>>>>>>>>Enter cmd:\n"); |
| 105 | char cmd[100]; |
| 106 | while(1) |
| 107 | { |
| 108 | memset(cmd, 0, 100); |
| 109 | if(fgets(cmd, 100, stdin)) |
| 110 | { |
| 111 | char *ptr = cmd + strlen(cmd) - 1; |
| 112 | while(ptr >= cmd && (*ptr == '\r' || *ptr == '\n')) |
| 113 | { |
| 114 | *ptr-- = '\0'; |
| 115 | } |
| 116 | |
| 117 | if(!strncasecmp(cmd, "imsi", 4)){ |
| 118 | char imsi[30]; |
| 119 | err = ql_sim_get_imsi(imsi, 30); |
| 120 | if(err) { |
| 121 | printf("Error : %d\n", err); |
| 122 | } else { |
| 123 | printf("IMSI : %s\n", imsi); |
| 124 | } |
| 125 | }else if(!strncasecmp(cmd, "iccid", 5)){ |
| 126 | char iccid[30]; |
| 127 | err = ql_sim_get_iccid(iccid, 30); |
| 128 | if(err) { |
| 129 | printf("Error : %d\n", err); |
| 130 | } else { |
| 131 | printf("ICCID : %s\n", iccid); |
| 132 | } |
| 133 | }else if(!strncasecmp(cmd, "pn", 2)){ |
| 134 | char phonenumber[30]; |
| 135 | err = ql_sim_get_phonenumber(phonenumber, 30); |
| 136 | if(err) { |
| 137 | printf("Error : %d\n", err); |
| 138 | } else { |
| 139 | printf("PhoneNumber : %s\n", phonenumber); |
| 140 | } |
| 141 | }else if(!strncasecmp(cmd, "pin_en", 6)){ // pin_en <pin> |
| 142 | QL_SIM_VERIFY_PIN_INFO pin = {0}; |
| 143 | char *ptr = strstr(cmd, " "); |
| 144 | if(ptr == NULL) |
| 145 | continue; |
| 146 | while(*ptr != '\0' && *ptr == ' ') |
| 147 | ptr++; |
| 148 | memcpy(pin.pin_value, ptr, strlen(ptr)); |
| 149 | |
| 150 | err = ql_sim_enable_pin(&pin); |
| 151 | if(err) { |
| 152 | printf("Error : %d\n", err); |
| 153 | } else { |
| 154 | printf("Enable PIN(%s) success.\n", pin.pin_value); |
| 155 | } |
| 156 | }else if(!strncasecmp(cmd, "pin_dis", 7)){ // pin_dis <pin> |
| 157 | QL_SIM_VERIFY_PIN_INFO pin = {0}; |
| 158 | char *ptr = strstr(cmd, " "); |
| 159 | if(ptr == NULL) |
| 160 | continue; |
| 161 | while(*ptr != '\0' && *ptr == ' ') |
| 162 | ptr++; |
| 163 | memcpy(pin.pin_value, ptr, strlen(ptr)); |
| 164 | |
| 165 | err = ql_sim_disable_pin(&pin); |
| 166 | if(err) { |
| 167 | printf("Error : %d\n", err); |
| 168 | } else { |
| 169 | printf("Disable PIN(%s) success.\n", pin.pin_value); |
| 170 | } |
| 171 | }else if(!strncasecmp(cmd, "pin_ch", 6)){ // pin_ch <old_pin> <new_pin> |
| 172 | QL_SIM_CHANGE_PIN_INFO pin = {0}; |
| 173 | char *ptr = strstr(cmd, " "); |
| 174 | if(ptr == NULL) |
| 175 | continue; |
| 176 | while(*ptr != '\0' && *ptr == ' ') |
| 177 | ptr++; |
| 178 | |
| 179 | char *tmp = pin.old_pin_value; |
| 180 | while(*ptr != '\0' && *ptr != ' ' && *ptr != '\r' && *ptr != '\n') { |
| 181 | *tmp++ = *ptr++; |
| 182 | } |
| 183 | *tmp = '\0'; |
| 184 | while(*ptr != '\0' && *ptr == ' ') |
| 185 | ptr++; |
| 186 | |
| 187 | tmp = pin.new_pin_value; |
| 188 | while(*ptr != '\0' && *ptr != ' ' && *ptr != '\r' && *ptr != '\n') { |
| 189 | *tmp++ = *ptr++; |
| 190 | } |
| 191 | *tmp = '\0'; |
| 192 | |
| 193 | err = ql_sim_change_pin(&pin); |
| 194 | if(err) { |
| 195 | printf("Error : %d\n", err); |
| 196 | } else { |
| 197 | printf("Change PIN(%s -> %s) success.\n", pin.old_pin_value, pin.new_pin_value); |
| 198 | } |
| 199 | }else if(!strncasecmp(cmd, "pin_verify", 10)){ // pin_verify <pin> |
| 200 | QL_SIM_VERIFY_PIN_INFO pin = {0}; |
| 201 | char *ptr = strstr(cmd, " "); |
| 202 | if(ptr == NULL) |
| 203 | continue; |
| 204 | while(*ptr != '\0' && *ptr == ' ') |
| 205 | ptr++; |
| 206 | memcpy(pin.pin_value, ptr, strlen(ptr)); |
| 207 | |
| 208 | err = ql_sim_verify_pin(&pin); |
| 209 | if(err) { |
| 210 | printf("Error : %d\n", err); |
| 211 | } else { |
| 212 | printf("Verify PIN(%s) success.\n", pin.pin_value); |
| 213 | } |
| 214 | }else if(!strncasecmp(cmd, "puk_unlock", 10)){ // puk_unlock <puk> <new_pin> |
| 215 | QL_SIM_UNBLOCK_PIN_INFO pin = {0}; |
| 216 | char *ptr = strstr(cmd, " "); |
| 217 | if(ptr == NULL) |
| 218 | continue; |
| 219 | while(*ptr != '\0' && *ptr == ' ') |
| 220 | ptr++; |
| 221 | |
| 222 | char *tmp = pin.puk_value; |
| 223 | while(*ptr != '\0' && *ptr != ' ' && *ptr != '\r' && *ptr != '\n') { |
| 224 | *tmp++ = *ptr++; |
| 225 | } |
| 226 | *tmp = '\0'; |
| 227 | while(*ptr != '\0' && *ptr == ' ') |
| 228 | ptr++; |
| 229 | |
| 230 | tmp = pin.new_pin_value; |
| 231 | while(*ptr != '\0' && *ptr != ' ' && *ptr != '\r' && *ptr != '\n') { |
| 232 | *tmp++ = *ptr++; |
| 233 | } |
| 234 | *tmp = '\0'; |
| 235 | |
| 236 | err = ql_sim_unblock_pin(&pin); |
| 237 | if(err) { |
| 238 | printf("Error : %d\n", err); |
| 239 | } else { |
| 240 | printf("PUI unlock(PUK:%s PIN:%s) success.\n", pin.puk_value, pin.new_pin_value); |
| 241 | } |
| 242 | }else if(!strncasecmp(cmd, "sim", 3)){ |
| 243 | QL_SIM_CARD_STATUS_INFO sim; |
| 244 | err = ql_sim_get_card_status(&sim); |
| 245 | if(err) { |
| 246 | printf("Error : %d\n", err); |
| 247 | } else { |
| 248 | printf("Sim type:%d, state:%d, PIN:%d,%d,%d,%d\n", sim.card_type, sim.card_state, sim.card_pin_info.pin1_num_retries, sim.card_pin_info.pin2_num_retries, sim.card_pin_info.puk1_num_retries, sim.card_pin_info.puk2_num_retries); |
| 249 | } |
| 250 | } |
| 251 | else if(!strcasecmp(cmd, "h") || !strcasecmp(cmd, "help")) { |
| 252 | help(); |
| 253 | } else if(!strcasecmp(cmd, "q")) { |
| 254 | break; |
| 255 | } else { |
| 256 | printf("\n"); |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | proc_exit(); |
| 262 | |
| 263 | LOGI("Client exec complete."); |
| 264 | #if 1 |
| 265 | while(1) |
| 266 | { |
| 267 | sleep(1000 * 365 * 24 * 60 * 60); |
| 268 | } |
| 269 | #else |
| 270 | sleep(1); |
| 271 | #endif |
| 272 | return 0; |
| 273 | } |
| 274 | |