wangyouqiang | e2498f1 | 2024-01-06 17:55:36 +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 "lynq-qser-data.h" |
| 15 | #include "mbtk_log.h" |
| 16 | |
| 17 | static void help() |
| 18 | { |
| 19 | printf("apn_get <cid> : Get current apns.\n"); |
| 20 | printf("apn_del <cid> : del apns.\n"); |
| 21 | printf("apn_list <cid> : list apns.\n"); |
| 22 | printf("apn_set <cid> <0/1/2/3> <apn> <apn_type> [<user> <pass> <(0 1)/2/3>] : \n"); |
| 23 | printf("Set apn (1-6) (IPV4/PPP/IPV6/IPV4V6) (150) (iot_net_i) (127) (127) (NONE/PAP/CHAP).\n"); |
| 24 | printf("apn_add <0/1/2/3> <apn> <apn_type> [<user> <pass> <(0 1)/2/3>] : \n"); |
| 25 | printf("data_call <0/1/2/3> <cid> <type> <re-con> <user> <pass>: Stop/Start/State data call.\n"); |
| 26 | printf("data_call <0/1/2/3> <cid> <IPV4/IPV6/IPV4V6> [<0/1> <user> <pass>].\n"); |
| 27 | } |
| 28 | |
| 29 | static int proc_exit() |
| 30 | { |
| 31 | qser_data_call_destroy(); |
| 32 | return 0; |
| 33 | } |
| 34 | |
| 35 | static void sig_process(int sig) |
| 36 | { |
| 37 | LOGI("I got signal %d\n", sig); |
| 38 | switch(sig) |
| 39 | { |
| 40 | case SIGINT: // Ctrl + C |
| 41 | { |
| 42 | LOGI("Exit by SIGINT.\n"); |
| 43 | proc_exit(); |
| 44 | exit(0); |
| 45 | } |
| 46 | case SIGQUIT: // Ctrl + \ (类似 SIGINT ,但要产生core文件) |
| 47 | { |
| 48 | LOGI("Exit by SIGQUIT.\n"); |
| 49 | proc_exit(); |
| 50 | exit(0); |
| 51 | } |
| 52 | case SIGTERM:// 默认kill (同 SIGKILL ,但 SIGKILL 不可捕获) |
| 53 | { |
| 54 | LOGI("Exit by SIGTERM.\n"); |
| 55 | proc_exit(); |
| 56 | exit(0); |
| 57 | } |
| 58 | case SIGTSTP:// Ctrl + Z (同 SIGSTOP ,但 SIGSTOP 不可捕获) |
| 59 | { |
| 60 | LOGI("Exit by SIGTSTP.\n"); |
| 61 | exit(0); |
| 62 | } |
| 63 | case SIGSEGV: // 如空指针 |
| 64 | { |
| 65 | LOGI("Exit by SIGSEGV.\n"); |
| 66 | exit(0); |
| 67 | } |
| 68 | default: |
| 69 | { |
| 70 | LOGI("Unknown sig:%d\n",sig); |
| 71 | break; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | static void data_call_status_cb(qser_data_call_state_s *state) |
| 77 | { |
| 78 | printf("entry data_call_status_cb\n"); |
| 79 | if(state == NULL) |
| 80 | { |
| 81 | printf("state is NULL\n"); |
| 82 | } |
| 83 | printf("DATA_DEMO_CALL_BACK: profile_idx=%d, name=%s, ip_family=%d, state=%d, error=%d\n" |
| 84 | , state->profile_idx, state->name, state->ip_family, state->state, state->err); |
| 85 | } |
| 86 | |
| 87 | int main(int argc, char *argv[]) |
| 88 | { |
| 89 | signal(SIGINT, sig_process); |
| 90 | signal(SIGQUIT, sig_process); |
| 91 | signal(SIGTERM, sig_process); |
| 92 | //signal(SIGTSTP, sig_process); |
| 93 | //signal(SIGSEGV, sig_process); |
| 94 | |
| 95 | mbtk_log_init(NULL,"MBTK_QSER_DATA_CALL_TEST"); |
| 96 | |
| 97 | //test2(0, "192.168.1.198"); |
| 98 | //test2(1, "2409:8162:140:cd3c:1:2:1494:72ba"); |
| 99 | //test2(1, "254.128.0.0.0.0.0.0.0.1.0.2.144.5.212.239"); |
| 100 | //test2(1, "2400:3200::1"); |
| 101 | |
| 102 | int err = qser_data_call_init(data_call_status_cb); |
| 103 | if(err) |
| 104 | { |
| 105 | printf("qser_data_call_init fail."); |
| 106 | return -1; |
| 107 | } |
| 108 | |
| 109 | printf(">>>>>>>>>>>>>>>>>>>>>>>>Enter cmd:\n"); |
| 110 | char cmd[100]; |
| 111 | while(1) |
| 112 | { |
| 113 | memset(cmd, 0, 100); |
| 114 | if(fgets(cmd, 100, stdin)) |
| 115 | { |
| 116 | char *ptr = cmd + strlen(cmd) - 1; |
| 117 | while(ptr >= cmd && (*ptr == '\r' || *ptr == '\n')) |
| 118 | { |
| 119 | *ptr-- = '\0'; |
| 120 | } |
| 121 | |
| 122 | if(!strncasecmp(cmd, "apn", 3)){ |
| 123 | if(!strncasecmp(cmd, "apn_get", 7)) { // Get apn |
| 124 | char *ptr = strstr(cmd, " "); |
| 125 | if(ptr == NULL) |
| 126 | continue; |
| 127 | while(*ptr != '\0' && *ptr == ' ') |
| 128 | ptr++; |
| 129 | int cid = atoi(ptr); |
| 130 | qser_apn_info_s qser_apn = {0}; |
| 131 | err = qser_apn_get(cid, &qser_apn); |
| 132 | if(err) { |
| 133 | printf("Error : %d\n", err); |
| 134 | } else { |
| 135 | printf("APN : %d, %d, %s, %s, %s, %d, %s.\n",qser_apn.profile_idx, qser_apn.pdp_type, qser_apn.apn_name, qser_apn.username, qser_apn.password, qser_apn.auth_proto, qser_apn.apn_type); |
| 136 | } |
| 137 | } else if(!strncasecmp(cmd, "apn_list", 8)){ |
| 138 | qser_apn_info_list_s apn_list = {0}; |
| 139 | err = qser_apn_get_list(&apn_list); |
| 140 | if(err) |
| 141 | { |
| 142 | printf("Error : %d\n", err); |
| 143 | } |
| 144 | else |
| 145 | { |
| 146 | printf("cnt: %d.\n", apn_list.cnt); |
| 147 | for(int i = 0; i < apn_list.cnt; i++) |
| 148 | { |
| 149 | printf("APN : %d, %d, %s, %s, %s, %d, %s.\n", apn_list.apn[i].profile_idx, apn_list.apn[i].pdp_type, apn_list.apn[i].apn_name, apn_list.apn[i].username, apn_list.apn[i].password, apn_list.apn[i].auth_proto, apn_list.apn[i].apn_type); |
| 150 | } |
| 151 | } |
| 152 | } else if(!strncasecmp(cmd, "apn_del", 7)){ |
| 153 | char *ptr = strstr(cmd, " "); |
| 154 | if(ptr == NULL) |
| 155 | continue; |
| 156 | while(*ptr != '\0' && *ptr == ' ') |
| 157 | ptr++; |
| 158 | char profile_idx = atoi(ptr); |
| 159 | err = qser_apn_del(profile_idx); |
| 160 | if(err) |
| 161 | { |
| 162 | printf("Error : %d\n", err); |
| 163 | } |
| 164 | else |
| 165 | { |
| 166 | printf("APN set success.\n"); |
| 167 | } |
| 168 | } else if(!strncasecmp(cmd, "apn_add", 7)){ |
| 169 | char qser_idx = 0; |
| 170 | qser_apn_add_s qser_apn = {0}; |
| 171 | char *ptr = strstr(cmd, " "); |
| 172 | if(ptr == NULL) |
| 173 | continue; |
| 174 | while(*ptr != '\0' && *ptr == ' ') |
| 175 | ptr++; |
| 176 | qser_apn.pdp_type = (qser_apn_pdp_type_e)atoi(ptr); |
| 177 | |
| 178 | ptr = strstr(ptr, " "); |
| 179 | if(ptr == NULL) |
| 180 | continue; |
| 181 | while(*ptr != '\0' && *ptr == ' ') |
| 182 | ptr++; |
| 183 | memcpy(qser_apn.apn_name, ptr, strlen(ptr)); |
| 184 | char *tmp = qser_apn.apn_name; |
| 185 | while(*tmp) { |
| 186 | if(*tmp == ' ') { |
| 187 | *tmp = '\0'; |
| 188 | break; |
| 189 | } |
| 190 | tmp++; |
| 191 | } |
| 192 | |
| 193 | ptr = strstr(ptr, " "); |
| 194 | if(ptr == NULL) |
| 195 | continue; |
| 196 | while(*ptr != '\0' && *ptr == ' ') |
| 197 | ptr++; |
| 198 | memcpy(qser_apn.apn_type, ptr, strlen(ptr)); |
| 199 | tmp = qser_apn.apn_type; |
| 200 | while(*tmp) { |
| 201 | if(*tmp == ' ') { |
| 202 | *tmp = '\0'; |
| 203 | break; |
| 204 | } |
| 205 | tmp++; |
| 206 | } |
| 207 | |
| 208 | ptr = strstr(ptr, " "); |
| 209 | if(ptr == NULL) { |
| 210 | err = qser_apn_add(&qser_apn, &qser_idx); |
| 211 | } else { |
| 212 | while(*ptr != '\0' && *ptr == ' ') |
| 213 | ptr++; |
| 214 | memcpy(qser_apn.username, ptr, strlen(ptr)); |
| 215 | tmp = qser_apn.username; |
| 216 | while(*tmp) { |
| 217 | if(*tmp == ' ') { |
| 218 | *tmp = '\0'; |
| 219 | break; |
| 220 | } |
| 221 | tmp++; |
| 222 | } |
| 223 | |
| 224 | ptr = strstr(ptr, " "); |
| 225 | if(ptr == NULL) |
| 226 | continue; |
| 227 | while(*ptr != '\0' && *ptr == ' ') |
| 228 | ptr++; |
| 229 | memcpy(qser_apn.password, ptr, strlen(ptr)); |
| 230 | tmp = qser_apn.password; |
| 231 | while(*tmp) { |
| 232 | if(*tmp == ' ') { |
| 233 | *tmp = '\0'; |
| 234 | break; |
| 235 | } |
| 236 | tmp++; |
| 237 | } |
| 238 | |
| 239 | ptr = strstr(ptr, " "); |
| 240 | if(ptr == NULL) |
| 241 | continue; |
| 242 | while(*ptr != '\0' && *ptr == ' ') |
| 243 | ptr++; |
| 244 | qser_apn.auth_proto = (qser_apn_auth_proto_e)atoi(ptr); |
| 245 | |
| 246 | err = qser_apn_add(&qser_apn, &qser_idx); |
| 247 | } |
| 248 | if(err) { |
| 249 | printf("Error : %d\n", err); |
| 250 | } else { |
| 251 | printf("APN set success. get idx = [%d]\n", qser_idx); |
| 252 | } |
| 253 | } |
| 254 | else { // apn <cid> <0/1/2> <apn> [<user> <pass> <auth>] |
| 255 | qser_apn_info_s qser_apn = {0}; |
| 256 | char *ptr = strstr(cmd, " "); |
| 257 | if(ptr == NULL) |
| 258 | continue; |
| 259 | while(*ptr != '\0' && *ptr == ' ') |
| 260 | ptr++; |
| 261 | qser_apn.profile_idx = atoi(ptr); |
| 262 | |
| 263 | ptr = strstr(ptr, " "); |
| 264 | if(ptr == NULL) |
| 265 | continue; |
| 266 | while(*ptr != '\0' && *ptr == ' ') |
| 267 | ptr++; |
| 268 | qser_apn.pdp_type = (qser_apn_pdp_type_e)atoi(ptr); |
| 269 | |
| 270 | ptr = strstr(ptr, " "); |
| 271 | if(ptr == NULL) |
| 272 | continue; |
| 273 | while(*ptr != '\0' && *ptr == ' ') |
| 274 | ptr++; |
| 275 | memcpy(qser_apn.apn_name, ptr, strlen(ptr)); |
| 276 | char *tmp = qser_apn.apn_name; |
| 277 | while(*tmp) { |
| 278 | if(*tmp == ' ') { |
| 279 | *tmp = '\0'; |
| 280 | break; |
| 281 | } |
| 282 | tmp++; |
| 283 | } |
| 284 | |
| 285 | ptr = strstr(ptr, " "); |
| 286 | if(ptr == NULL) |
| 287 | continue; |
| 288 | while(*ptr != '\0' && *ptr == ' ') |
| 289 | ptr++; |
| 290 | memcpy(qser_apn.apn_type, ptr, strlen(ptr)); |
| 291 | tmp = qser_apn.apn_type; |
| 292 | while(*tmp) { |
| 293 | if(*tmp == ' ') { |
| 294 | *tmp = '\0'; |
| 295 | break; |
| 296 | } |
| 297 | tmp++; |
| 298 | } |
| 299 | |
| 300 | ptr = strstr(ptr, " "); |
| 301 | if(ptr == NULL) { |
| 302 | err = qser_apn_set(&qser_apn); |
| 303 | } else { |
| 304 | while(*ptr != '\0' && *ptr == ' ') |
| 305 | ptr++; |
| 306 | memcpy(qser_apn.username, ptr, strlen(ptr)); |
| 307 | tmp = qser_apn.username; |
| 308 | while(*tmp) { |
| 309 | if(*tmp == ' ') { |
| 310 | *tmp = '\0'; |
| 311 | break; |
| 312 | } |
| 313 | tmp++; |
| 314 | } |
| 315 | |
| 316 | ptr = strstr(ptr, " "); |
| 317 | if(ptr == NULL) |
| 318 | continue; |
| 319 | while(*ptr != '\0' && *ptr == ' ') |
| 320 | ptr++; |
| 321 | memcpy(qser_apn.password, ptr, strlen(ptr)); |
| 322 | tmp = qser_apn.password; |
| 323 | while(*tmp) { |
| 324 | if(*tmp == ' ') { |
| 325 | *tmp = '\0'; |
| 326 | break; |
| 327 | } |
| 328 | tmp++; |
| 329 | } |
| 330 | |
| 331 | ptr = strstr(ptr, " "); |
| 332 | if(ptr == NULL) |
| 333 | continue; |
| 334 | while(*ptr != '\0' && *ptr == ' ') |
| 335 | ptr++; |
| 336 | qser_apn.auth_proto = (qser_apn_auth_proto_e)atoi(ptr); |
| 337 | |
| 338 | err = qser_apn_set(&qser_apn); |
| 339 | } |
| 340 | if(err) { |
| 341 | printf("Error : %d\n", err); |
| 342 | } else { |
| 343 | printf("APN set success\n"); |
| 344 | } |
| 345 | } |
| 346 | } else if(!strncasecmp(cmd, "data_call", 9)){ // data_call <0/1/2> <cid> <timeout> |
| 347 | // data_call <0/1/2> <cid> <type> <re-con> <user> <pass> |
| 348 | qser_data_call_s qser_data_call = {0}; |
| 349 | char *ptr = strstr(cmd, " "); |
| 350 | if(ptr == NULL) |
| 351 | continue; |
| 352 | while(*ptr != '\0' && *ptr == ' ') |
| 353 | ptr++; |
| 354 | int type = atoi(ptr); |
| 355 | |
| 356 | ptr = strstr(ptr, " "); |
| 357 | if(ptr == NULL) |
| 358 | continue; |
| 359 | while(*ptr != '\0' && *ptr == ' ') |
| 360 | ptr++; |
| 361 | int cid = atoi(ptr); |
| 362 | |
| 363 | ptr = strstr(ptr, " "); |
| 364 | if(ptr == NULL) |
| 365 | continue; |
| 366 | while(*ptr != '\0' && *ptr == ' ') |
| 367 | ptr++; |
| 368 | qser_data_call_ip_family_e ip_type = atoi(ptr); |
| 369 | |
| 370 | ptr = strstr(ptr, " "); |
| 371 | if(ptr == NULL) |
| 372 | { |
| 373 | |
| 374 | } |
| 375 | else |
| 376 | { |
| 377 | while(*ptr != '\0' && *ptr == ' ') |
| 378 | ptr++; |
| 379 | qser_data_call.reconnect = atoi(ptr); |
| 380 | |
| 381 | ptr = strstr(ptr, " "); |
| 382 | if(ptr == NULL) |
| 383 | continue; |
| 384 | while(*ptr != '\0' && *ptr == ' ') |
| 385 | ptr++; |
| 386 | memcpy(qser_data_call.cdma_username, ptr, strlen(ptr)); |
| 387 | char *tmp = qser_data_call.cdma_username; |
| 388 | while(*tmp) { |
| 389 | if(*tmp == ' ') { |
| 390 | *tmp = '\0'; |
| 391 | break; |
| 392 | } |
| 393 | tmp++; |
| 394 | } |
| 395 | |
| 396 | ptr = strstr(ptr, " "); |
| 397 | if(ptr == NULL) |
| 398 | continue; |
| 399 | while(*ptr != '\0' && *ptr == ' ') |
| 400 | ptr++; |
| 401 | memcpy(qser_data_call.cdma_password, ptr, strlen(ptr)); |
| 402 | tmp = qser_data_call.cdma_password; |
| 403 | while(*tmp) { |
| 404 | if(*tmp == ' ') { |
| 405 | *tmp = '\0'; |
| 406 | break; |
| 407 | } |
| 408 | tmp++; |
| 409 | } |
| 410 | } |
| 411 | #if 1 |
| 412 | qser_data_call_error_e qser_err; |
| 413 | switch (type) |
| 414 | { |
| 415 | case 0: |
| 416 | err = qser_data_call_stop(cid, ip_type, &qser_err); |
| 417 | break; |
| 418 | case 1: |
| 419 | qser_data_call.profile_idx = cid; |
| 420 | qser_data_call.ip_family = ip_type; |
| 421 | err = qser_data_call_start(&qser_data_call, &qser_err); |
| 422 | break; |
| 423 | case 2: { |
| 424 | qser_data_call_info_s info; |
| 425 | err = qser_data_call_info_get(cid, ip_type, &info, &qser_err); |
| 426 | if(!err) { |
| 427 | printf("cid : %d, ip_type : %d\n", info.profile_idx, info.ip_family); |
| 428 | if(info.v4.state) { |
| 429 | printf("%s: %s, %s, %s\n", info.v4.name, inet_ntoa(info.v4.addr.ip), inet_ntoa(info.v4.addr.pri_dns), inet_ntoa(info.v4.addr.sec_dns)); |
| 430 | } else { |
| 431 | printf("IPV4 not available.\n"); |
| 432 | } |
| 433 | |
| 434 | if(info.v6.state) { |
| 435 | char IP_buf[128] = {0}; |
| 436 | char pri_dns_buf[128] = {0}; |
| 437 | char sec_dns_buf[128] = {0}; |
| 438 | ipv6_2_str(&(info.v6.addr.ip), IP_buf); |
| 439 | ipv6_2_str(&(info.v6.addr.pri_dns), pri_dns_buf); |
| 440 | ipv6_2_str(&(info.v6.addr.sec_dns), sec_dns_buf); |
| 441 | printf("%s: %s, %s, %s\n", info.v6.name, IP_buf, pri_dns_buf, sec_dns_buf); |
| 442 | } else { |
| 443 | printf("IPV6 not available.\n"); |
| 444 | } |
| 445 | } |
| 446 | break; |
| 447 | } |
| 448 | case 3: |
| 449 | { |
| 450 | qser_data_call.profile_idx = cid; |
| 451 | qser_data_call.ip_family = ip_type; |
| 452 | err = qser_data_call_start_async(&qser_data_call, &qser_err); |
| 453 | break; |
| 454 | } |
| 455 | default: |
| 456 | printf("Type error:%d\n", type); |
| 457 | break; |
| 458 | } |
| 459 | #endif |
| 460 | printf("qser_err: %d\n", qser_err); |
| 461 | if(err) { |
| 462 | printf("Error : %d\n", err); |
| 463 | } else { |
| 464 | printf("DATA_CALL success\n"); |
| 465 | } |
| 466 | } |
| 467 | else if(!strcasecmp(cmd, "h") || !strcasecmp(cmd, "help")) { |
| 468 | help(); |
| 469 | } else if(!strcasecmp(cmd, "q")) { |
| 470 | break; |
| 471 | } else { |
| 472 | printf("\n"); |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | proc_exit(); |
| 478 | |
| 479 | LOGI("Client exec complete."); |
| 480 | #if 1 |
| 481 | while(1) |
| 482 | { |
| 483 | sleep(1000 * 365 * 24 * 60 * 60); |
| 484 | } |
| 485 | #else |
| 486 | sleep(1); |
| 487 | #endif |
| 488 | return 0; |
| 489 | } |
| 490 | |