b.liu | 68a94c9 | 2025-05-24 12:53:41 +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[1024]; |
| 111 | while(1) |
| 112 | { |
| 113 | memset(cmd, 0, 1024); |
| 114 | if(fgets(cmd, 1024, 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 | int i = 0; |
| 148 | for(i = 0; i < apn_list.cnt; i++) |
| 149 | { |
| 150 | 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); |
| 151 | } |
| 152 | } |
| 153 | } else if(!strncasecmp(cmd, "apn_del", 7)){ |
| 154 | char *ptr = strstr(cmd, " "); |
| 155 | if(ptr == NULL) |
| 156 | continue; |
| 157 | while(*ptr != '\0' && *ptr == ' ') |
| 158 | ptr++; |
| 159 | char profile_idx = atoi(ptr); |
| 160 | err = qser_apn_del(profile_idx); |
| 161 | if(err) |
| 162 | { |
| 163 | printf("Error : %d\n", err); |
| 164 | } |
| 165 | else |
| 166 | { |
| 167 | printf("APN set success.\n"); |
| 168 | } |
| 169 | } else if(!strncasecmp(cmd, "apn_add", 7)){ |
| 170 | char qser_idx = 0; |
| 171 | qser_apn_add_s qser_apn = {0}; |
| 172 | |
| 173 | char buf1[2] = {0}; |
| 174 | char buf2[2] = {0}; |
| 175 | int ret = sscanf(cmd, "apn_add %s %s %s %s %s %s", buf1, qser_apn.apn_name, qser_apn.apn_type, qser_apn.username, qser_apn.password, buf2); |
| 176 | if(strlen(buf1)) |
| 177 | { |
| 178 | qser_apn.pdp_type = buf1[0] - '0'; |
| 179 | } |
| 180 | |
| 181 | if(strlen(buf2)) |
| 182 | { |
| 183 | qser_apn.auth_proto = buf2[0] - '0'; |
| 184 | } |
| 185 | printf("param is [%d]\r\n", ret); |
| 186 | printf("apn_add:%d %s %s %s %s %d\r\n", qser_apn.pdp_type, qser_apn.apn_name, qser_apn.apn_type, qser_apn.username, qser_apn.password, qser_apn.auth_proto); |
| 187 | if(ret >= 3) |
| 188 | { |
| 189 | err = qser_apn_add(&qser_apn, &qser_idx); |
| 190 | } |
| 191 | |
| 192 | if(err) { |
| 193 | printf("Error : %d\n", err); |
| 194 | } else { |
| 195 | printf("APN set success. get idx = [%d]\n", qser_idx); |
| 196 | } |
| 197 | } |
| 198 | else { // apn <cid> <0/1/2> <apn> <apn_type> [<user> <pass> <auth>] |
| 199 | qser_apn_info_s qser_apn = {0}; |
| 200 | |
| 201 | char buf0[2] = {0}; |
| 202 | char buf1[2] = {0}; |
| 203 | char buf2[2] = {0}; |
| 204 | int ret = sscanf(cmd, "apn_set %s %s %s %s %s %s %s", buf0, buf1, qser_apn.apn_name, qser_apn.apn_type, qser_apn.username, qser_apn.password, buf2); |
| 205 | if(strlen(buf0)) |
| 206 | { |
| 207 | qser_apn.profile_idx = buf0[0] - '0'; |
| 208 | } |
| 209 | |
| 210 | if(strlen(buf1)) |
| 211 | { |
| 212 | qser_apn.pdp_type = buf1[0] - '0'; |
| 213 | } |
| 214 | |
| 215 | if(strlen(buf2)) |
| 216 | { |
| 217 | qser_apn.auth_proto = buf2[0] - '0'; |
| 218 | } |
| 219 | printf("param is [%d]\r\n", ret); |
| 220 | printf("apn_set:%d %d %s %s %s %s %d\r\n", qser_apn.profile_idx, qser_apn.pdp_type, qser_apn.apn_name, qser_apn.apn_type, qser_apn.username, qser_apn.password, qser_apn.auth_proto); |
| 221 | if(ret >= 4) |
| 222 | { |
| 223 | err = qser_apn_set(&qser_apn); |
| 224 | } |
| 225 | |
| 226 | if(err) { |
| 227 | printf("Error : %d\n", err); |
| 228 | } else { |
| 229 | printf("APN set success\n"); |
| 230 | } |
| 231 | } |
| 232 | } else if(!strncasecmp(cmd, "data_call", 9)){ // data_call <0/1/2> <cid> <timeout> |
| 233 | // data_call <0/1/2> <cid> <type> <re-con> <user> <pass> |
| 234 | qser_data_call_s qser_data_call = {0}; |
| 235 | char *ptr = strstr(cmd, " "); |
| 236 | if(ptr == NULL) |
| 237 | continue; |
| 238 | while(*ptr != '\0' && *ptr == ' ') |
| 239 | ptr++; |
| 240 | int type = atoi(ptr); |
| 241 | |
| 242 | ptr = strstr(ptr, " "); |
| 243 | if(ptr == NULL) |
| 244 | continue; |
| 245 | while(*ptr != '\0' && *ptr == ' ') |
| 246 | ptr++; |
| 247 | int cid = atoi(ptr); |
| 248 | |
| 249 | ptr = strstr(ptr, " "); |
| 250 | if(ptr == NULL) |
| 251 | continue; |
| 252 | while(*ptr != '\0' && *ptr == ' ') |
| 253 | ptr++; |
| 254 | qser_data_call_ip_family_e ip_type = atoi(ptr); |
| 255 | |
| 256 | ptr = strstr(ptr, " "); |
| 257 | if(ptr == NULL) |
| 258 | { |
| 259 | |
| 260 | } |
| 261 | else |
| 262 | { |
| 263 | while(*ptr != '\0' && *ptr == ' ') |
| 264 | ptr++; |
| 265 | qser_data_call.reconnect = atoi(ptr); |
| 266 | |
| 267 | ptr = strstr(ptr, " "); |
| 268 | if(ptr == NULL) |
| 269 | continue; |
| 270 | while(*ptr != '\0' && *ptr == ' ') |
| 271 | ptr++; |
| 272 | memcpy(qser_data_call.cdma_username, ptr, strlen(ptr)); |
| 273 | char *tmp = qser_data_call.cdma_username; |
| 274 | while(*tmp) { |
| 275 | if(*tmp == ' ') { |
| 276 | *tmp = '\0'; |
| 277 | break; |
| 278 | } |
| 279 | tmp++; |
| 280 | } |
| 281 | |
| 282 | ptr = strstr(ptr, " "); |
| 283 | if(ptr == NULL) |
| 284 | continue; |
| 285 | while(*ptr != '\0' && *ptr == ' ') |
| 286 | ptr++; |
| 287 | memcpy(qser_data_call.cdma_password, ptr, strlen(ptr)); |
| 288 | tmp = qser_data_call.cdma_password; |
| 289 | while(*tmp) { |
| 290 | if(*tmp == ' ') { |
| 291 | *tmp = '\0'; |
| 292 | break; |
| 293 | } |
| 294 | tmp++; |
| 295 | } |
| 296 | } |
| 297 | #if 1 |
| 298 | qser_data_call_error_e qser_err; |
| 299 | switch (type) |
| 300 | { |
| 301 | case 0: |
| 302 | err = qser_data_call_stop(cid, ip_type, &qser_err); |
| 303 | break; |
| 304 | case 1: |
| 305 | qser_data_call.profile_idx = cid; |
| 306 | qser_data_call.ip_family = ip_type; |
| 307 | err = qser_data_call_start(&qser_data_call, &qser_err); |
| 308 | break; |
| 309 | case 2: { |
| 310 | qser_data_call_info_s info; |
| 311 | err = qser_data_call_info_get(cid, ip_type, &info, &qser_err); |
| 312 | if(!err) { |
| 313 | printf("cid : %d, ip_type : %d\n", info.profile_idx, info.ip_family); |
| 314 | if(info.v4.state) { |
| 315 | LOGE("[qser_data_call] IP: 0x%08x pri_DNS: 0x%08x sec_DNS: 0x%08x.", info.v4.addr.ip.s_addr, info.v4.addr.pri_dns.s_addr, info.v4.addr.sec_dns.s_addr); |
| 316 | //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)); |
| 317 | printf("%s: %s ", info.v4.name, inet_ntoa(info.v4.addr.ip)); |
| 318 | printf("%s ", inet_ntoa(info.v4.addr.pri_dns)); |
| 319 | printf("%s\n", inet_ntoa(info.v4.addr.sec_dns)); |
| 320 | } else { |
| 321 | printf("IPV4 not available.\n"); |
| 322 | } |
| 323 | |
| 324 | if(info.v6.state) { |
| 325 | char IP_buf[128] = {0}; |
| 326 | char pri_dns_buf[128] = {0}; |
| 327 | char sec_dns_buf[128] = {0}; |
| 328 | ipv6_2_str(&(info.v6.addr.ip), IP_buf); |
| 329 | ipv6_2_str(&(info.v6.addr.pri_dns), pri_dns_buf); |
| 330 | ipv6_2_str(&(info.v6.addr.sec_dns), sec_dns_buf); |
| 331 | printf("%s: %s, %s, %s\n", info.v6.name, IP_buf, pri_dns_buf, sec_dns_buf); |
| 332 | } else { |
| 333 | printf("IPV6 not available.\n"); |
| 334 | } |
| 335 | } |
| 336 | break; |
| 337 | } |
| 338 | case 3: |
| 339 | { |
| 340 | qser_data_call.profile_idx = cid; |
| 341 | qser_data_call.ip_family = ip_type; |
| 342 | err = qser_data_call_start_async(&qser_data_call, &qser_err); |
| 343 | break; |
| 344 | } |
| 345 | default: |
| 346 | printf("Type error:%d\n", type); |
| 347 | break; |
| 348 | } |
| 349 | #endif |
| 350 | printf("qser_err: %d\n", qser_err); |
| 351 | if(err) { |
| 352 | printf("Error : %d\n", err); |
| 353 | } else { |
| 354 | printf("DATA_CALL success\n"); |
| 355 | } |
| 356 | } |
| 357 | else if(!strcasecmp(cmd, "h") || !strcasecmp(cmd, "help")) { |
| 358 | help(); |
| 359 | } else if(!strcasecmp(cmd, "q")) { |
| 360 | break; |
| 361 | } else { |
| 362 | printf("\n"); |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | proc_exit(); |
| 368 | |
| 369 | LOGI("Client exec complete."); |
| 370 | #if 1 |
| 371 | while(1) |
| 372 | { |
| 373 | sleep(1000 * 365 * 24 * 60 * 60); |
| 374 | } |
| 375 | #else |
| 376 | sleep(1); |
| 377 | #endif |
| 378 | return 0; |
| 379 | } |
| 380 | |