blob: 2e06e3db5e201bc19485555179e45f4ff871081e [file] [log] [blame]
liubin281ac462023-07-19 14:22:54 +08001#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 <fcntl.h>
11#include <signal.h>
12#include <cutils/properties.h>
13
14#include "mbtk_type.h"
15#include "mbtk_info.h"
16#include "mbtk_queue.h"
17#include "atchannel.h"
18#include "at_tok.h"
19#include "mbtk_utils.h"
20#include "mbtk_ifc.h"
21#include "info_data.h"
22
23static int sock_listen_fd = -1;
24static int epoll_fd = -1;
25static list_node_t *sock_client_list = NULL;
26static mbtk_queue_node_t info_queue;
27static pthread_cond_t info_cond;
28static pthread_mutex_t info_mutex;
29static mbtk_queue_node_t urc_queue;
30static pthread_cond_t urc_cond;
31static pthread_mutex_t urc_mutex;
32
33static mbtk_band_info_t band_support;
34net_info_t net_info;
35mbtK_cell_pack_info_t cell_info;
36info_cgact_wait_t cgact_wait;
37static int cid_active[MBTK_APN_CID_MAX + 1] = {0};
38bool at_process = false;
39//mbtk wyq for data_call_ex add start
40// |2----7|
41//"00000000"+'\0'
42static char cid_bootconn[MBTK_APN_CID_MAX + 2] = {0};
43#define DATA_CALL_BOOTCONN_FD 0x5f6f7f8f
44//mbtk wyq for data_call_ex add end
45
46//mbtk wyq for server_ready_status add start
47static char server_ready_status = 0;
48//mbtk wyq for server_ready_status add end
49
r.xiaoec113d12024-01-12 02:13:28 -080050
51/*
52AT*POWERIND=0"
53or
54AT*POWERIND=1~31"
55
56OK
57
58AT*POWERIND=31,就相当于设置NETWORK、SIM、SMS、CS CALL、PS DATA变化时都不主动上报,
59其中PS DATA目前暂时不支持,只是保留了这个标志位,0 means resume all.
60
61AP power state: 1~31 means suspend,
62bitmap:
63bit0 - NETWORK;
64bit1 - SIM;
65bit2 - SMS;
66bit3 - CS CALL
67bit4 - PS DATA
68
69*/
70static int req_powerind_set(uint32 state, int *cme_err)
71{
72 ATResponse *response = NULL;
73 char cmd[100] = {0};
74
75 if (state >= 0 && state < 32)
76 {
77 sprintf(cmd, "AT*POWERIND=%d", state);
78 LOG("Set the powerind command is = %s.\n", cmd);
79 }
80 int err = at_send_command(cmd, &response);
81 if (err < 0 || response->success == 0){
82 *cme_err = at_get_cme_error(response);
83 goto exit;
84 }
85
86exit:
87 at_response_free(response);
88 return err;
89}
90
91/*
92AT+OOSPP=1,20,30,40 //AtOospp()
93 param1:mode
94 param2:oosPhasePeriod[0] //5 times, 5s by default;
95 param3:oosPhasePeriod[1] //5 times, 5s by default;
96 param4:oosPhasePeriod[2] //unlimited, 5s by default;
97
98
99BTW
1001, 如果只输入mode=1,其余参数不设置,相当于这个功能打开,时间间隔是这个功能的默认值。
1012, 如果当mode=1加上其余设置参数后,功能打开,时间间隔是本次设置的值;
1023,如果再设置mode=0,相当于这个功能关闭,是走默认的搜网间隔。
103
104*/
105static int req_oos_set(char* state, int *cme_err)
106{
107 ATResponse *response = NULL;
108 char cmd[100] = {0};
109 int mode;
110
111 mode = atoi(state);
112 if (mode == 1 || mode == 0)//只有一个值0/1
113 {
114 sprintf(cmd, "AT+OOSPP=%d", mode);
115 }
116 else
117 {
118 sprintf(cmd, "AT+OOSPP=%s", state);
119 }
120
121 LOG("Set the oos command is = %s.\n", cmd);
122 int err = at_send_command(cmd, &response);
123 if (err < 0 || response->success == 0){
124 *cme_err = at_get_cme_error(response);
125 goto exit;
126 }
127
128exit:
129 at_response_free(response);
130 return err;
131}
132
133
134/*
135AT+OOSPP?
136开:
137+OOSPP:5,5,5
138关:
139+OOSPP:0
140*/
141static int req_oos_get(mbtk_oos_info *req, int *cme_err)
142{
143 ATResponse *response = NULL;
144
145 int err = at_send_command_singleline("AT+CSCA?", "+OOSPP:", &response);
146
147 if (err < 0 || response->success == 0 || !response->p_intermediates){
148 *cme_err = at_get_cme_error(response);
149 goto exit;
150 }
151
152 char *line = response->p_intermediates->line;
153
154 char *tmp_str = NULL;
155 err = at_tok_nextstr(&line, &tmp_str);
156 if (err < 0)
157 {
158 goto exit;
159 }
160
161 LOG("[xiaorui] >>> req_oos_get =[%s]",tmp_str);
162
163 int mode = atoi(tmp_str);
164 if (mode == 0)//关闭状态
165 {
166 req->mode = (uint8)mode;
167 }
168 else//开状态
169 {
170 req->mode = 1;
171
172 req->oosPhase[0] = (uint8)tmp_str;
173
174 err = at_tok_nextstr(&line, &tmp_str);
175 if (err < 0)
176 {
177 goto exit;
178 }
179 req->oosPhase[1] = (uint8)tmp_str;
180
181 err = at_tok_nextstr(&line, &tmp_str);
182 if (err < 0)
183 {
184 goto exit;
185 }
186 req->oosPhase[2] = (uint8)tmp_str;
187 }
188
189 memcpy(req, tmp_str, strlen(tmp_str));
190
191exit:
192 at_response_free(response);
193 return err;
194}
195
liubin281ac462023-07-19 14:22:54 +0800196static void sock_cli_free_func(void *data)
197{
198 if (data)
199 {
200 sock_client_info_t *info = (sock_client_info_t*) data;
201 LOG("Free Socket client[fd = %d].", info->fd);
202 free(info);
203 }
204}
205
206static void cli_close(sock_client_info_t* client)
207{
208 struct epoll_event ev;
209 memset(&ev,0,sizeof(struct epoll_event));
210 ev.data.fd = client->fd;
211 ev.events = EPOLLIN | EPOLLERR | EPOLLET;
212 epoll_ctl(epoll_fd, EPOLL_CTL_DEL, client->fd, &ev);
213
214 close(client->fd);
215
216 if(list_remove(sock_client_list, client))
217 {
218 sock_cli_free_func(client);
219 }
220}
221
222static void pack_error_send(int fd, int info_id, int err)
223{
224 mbtk_info_pack_t* pack = mbtk_info_pack_creat(info_id);
225 if(pack)
226 {
227 pack->info_err = (uint16)err;
228 mbtk_info_pack_send(fd, pack);
229 mbtk_info_pack_free(&pack);
230 }
231 else
232 {
233 LOG("mbtk_info_pack_creat() fail.");
234 }
235}
236
237void pack_rsp_send(int fd, int info_id, const void* data, int data_len)
238{
239 mbtk_info_pack_t* pack = mbtk_info_pack_creat(info_id);
240 if(pack)
241 {
242 pack->info_err = (uint16)MBTK_INFO_ERR_SUCCESS;
243 if(data != NULL && data_len > 0)
244 {
245 //mbtk_info_pack_data_set(pack, data, data_len);
246 pack->data_len = (uint16)data_len;
247 pack->data = (const uint8*)data;
248 }
249 mbtk_info_pack_send(fd, pack);
250 mbtk_info_pack_free(&pack);
251 }
252 else
253 {
254 LOG("mbtk_info_pack_creat() fail.");
255 }
256}
257
258static int apn_prop_set(mbtk_apn_info_t *apn)
259{
260 char prop_name[20] = {0};
261 char prop_data[300] = {0};
262 sprintf(prop_name, "%s_%d",MBTK_APN_PROP,apn->cid);
263 snprintf(prop_data, 300, "%d,%s,%s,%s,%s", apn->ip_type, apn->apn,
264 str_empty(apn->user) ? "NULL" : apn->user,
265 str_empty(apn->pass) ? "NULL" : apn->pass,
266 str_empty(apn->auth) ? "NULL" : apn->auth);
267
268 return property_set(prop_name, prop_data);
269}
270
271/*
272AT*BAND=?
273*BAND:(0-18),79,147,482,524503
274
275
276OK
277
278AT*BAND=15,78,147,482,134742231
279
280
281*/
282static void band_support_get()
283{
284 // Support band has get.
285 if(band_support.net_pref != 0xFF) {
286 return;
287 }
288
289#if 1
290 // 79,147,482,524503
291 band_support.gsm_band = (uint16)79;
292 band_support.umts_band = (uint16)147;
293 band_support.tdlte_band = (uint32)482;
294#if MBTK_LTE_B28_SUPPORT
295 band_support.fddlte_band = (uint32)134742231;
296#else
297 band_support.fddlte_band = (uint32)524503;
298#endif
299 band_support.net_pref = (uint8)0;
300#else
301 ATResponse *response = NULL;
302 int tmp_int;
303 char *tmp_str;
304 int err = at_send_command_singleline("AT*BAND=?", "*BAND:", &response);
305
306 if (err < 0 || response->success == 0 || !response->p_intermediates)
307 goto exit;
308
309 char *line = response->p_intermediates->line;
310 err = at_tok_start(&line);
311 if (err < 0)
312 {
313 goto exit;
314 }
315
316 err = at_tok_nextstr(&line, &tmp_str);
317 if (err < 0)
318 {
319 goto exit;
320 }
321
322 err = at_tok_nextint(&line, &tmp_int);
323 if (err < 0)
324 {
325 goto exit;
326 }
327 band_support.gsm_band = (uint16)tmp_int;
328
329 err = at_tok_nextint(&line, &tmp_int);
330 if (err < 0)
331 {
332 goto exit;
333 }
334 band_support.umts_band = (uint16)tmp_int;
335
336 err = at_tok_nextint(&line, &tmp_int);
337 if (err < 0)
338 {
339 goto exit;
340 }
341 band_support.tdlte_band = (uint32)tmp_int;
342
343 err = at_tok_nextint(&line, &tmp_int);
344 if (err < 0)
345 {
346 goto exit;
347 }
348 band_support.fddlte_band = (uint32)tmp_int;
349 band_support.net_pref = (uint8)0;
350exit:
351 at_response_free(response);
352#endif
353}
354
355static int parseRegistrationState(char *str, int *items, int **response)
356{
357 int err;
358 char *line = str, *p;
359 int *resp = NULL;
360 int skip;
361 int commas;
362
363 LOGD("parseRegistrationState. Parsing: %s",str);
364 err = at_tok_start(&line);
365 if (err < 0) goto error;
366
367 /* Ok you have to be careful here
368 * The solicited version of the CREG response is
369 * +CREG: n, stat, [lac, cid]
370 * and the unsolicited version is
371 * +CREG: stat, [lac, cid]
372 * The <n> parameter is basically "is unsolicited creg on?"
373 * which it should always be
374 *
375 * Now we should normally get the solicited version here,
376 * but the unsolicited version could have snuck in
377 * so we have to handle both
378 *
379 * Also since the LAC and CID are only reported when registered,
380 * we can have 1, 2, 3, or 4 arguments here
381 *
382 * finally, a +CGREG: answer may have a fifth value that corresponds
383 * to the network type, as in;
384 *
385 * +CGREG: n, stat [,lac, cid [,networkType]]
386 */
387
388 /* count number of commas */
389 commas = 0;
390 for (p = line ; *p != '\0' ; p++)
391 {
392 if (*p == ',') commas++;
393 }
394
395 resp = (int *)calloc(commas + 1, sizeof(int));
396 if (!resp) goto error;
397 switch (commas)
398 {
399 case 0: /* +CREG: <stat> */
400 err = at_tok_nextint(&line, &resp[0]);
401 if (err < 0) goto error;
402 //resp[1] = -1;
403 //resp[2] = -1;
404 break;
405
406 case 1: /* +CREG: <n>, <stat> */
407 err = at_tok_nextint(&line, &skip);
408 if (err < 0) goto error;
409 err = at_tok_nextint(&line, &resp[0]);
410 if (err < 0) goto error;
411 resp[1] = -1;
412 //resp[2] = -1;
413 if (err < 0) goto error;
414 break;
415
416 case 2: /* +CREG: <stat>, <lac>, <cid> */
417 err = at_tok_nextint(&line, &resp[0]);
418 if (err < 0) goto error;
419 err = at_tok_nexthexint(&line, &resp[1]);
420 if (err < 0) goto error;
421 err = at_tok_nexthexint(&line, &resp[2]);
422 if (err < 0) goto error;
423 break;
424 case 3: /* +CREG: <n>, <stat>, <lac>, <cid> */
425 /* +CEREG: 1,"8330","06447340",7 */
426#if 0
427 err = at_tok_nextint(&line, &skip);
428 if (err < 0) goto error;
429 err = at_tok_nextint(&line, &resp[0]);
430 if (err < 0) goto error;
431 err = at_tok_nexthexint(&line, &resp[1]);
432 if (err < 0) goto error;
433 err = at_tok_nexthexint(&line, &resp[2]);
434 if (err < 0) goto error;
435#else
436 err = at_tok_nextint(&line, &resp[0]);
437 if (err < 0) goto error;
438 err = at_tok_nextint(&line, &resp[1]);
439 if (err < 0) goto error;
440 err = at_tok_nexthexint(&line, &resp[2]);
441 if (err < 0) goto error;
442 err = at_tok_nextint(&line, &resp[3]);
443 if (err < 0) goto error;
444#endif
445 break;
446 /* special case for CGREG, there is a fourth parameter
447 * that is the network type (unknown/gprs/edge/umts)
448 */
449 case 4: /* +CGREG: <n>, <stat>, <lac>, <cid>, <networkType> */
450 /* +CEREG: 2,1,"8330","06447340",7 */
451 err = at_tok_nextint(&line, &skip);
452 if (err < 0) goto error;
453 err = at_tok_nextint(&line, &resp[0]);
454 if (err < 0) goto error;
455 err = at_tok_nexthexint(&line, &resp[1]);
456 if (err < 0) goto error;
457 err = at_tok_nexthexint(&line, &resp[2]);
458 if (err < 0) goto error;
459 err = at_tok_nexthexint(&line, &resp[3]);
460 if (err < 0) goto error;
461 break;
462 default:
463 goto error;
464 }
465 /*
466 if(commas > 1) {
467 s_lac = resp[1];
468 s_cid = resp[2];
469 }*/
470 if (response)
471 *response = resp;
472 if (items)
473 *items = commas + 1;
474 return 0;
475error:
476 free(resp);
477 return -1;
478}
479
480/*
4810: minimum functionality
4821: full functionality
4833: disable phone receive RF circuits.
4844: disable phone both transmit and receive RF circuits
4855: disable SIM
4866: turn off full secondary receive.
487-1: fail
488*/
489static int isRadioOn()
490{
491 ATResponse *p_response = NULL;
492 int err;
493 char *line;
494 int ret;
495
496 err = at_send_command_singleline("AT+CFUN?", "+CFUN:", &p_response);
497
498 if (err < 0 || p_response->success == 0 || !p_response->p_intermediates)
499 {
500 // assume radio is off
501 goto error;
502 }
503
504 line = p_response->p_intermediates->line;
505
506 err = at_tok_start(&line);
507 if (err < 0) goto error;
508
509 err = at_tok_nextint(&line, &ret);
510 if (err < 0) goto error;
511
512 at_response_free(p_response);
513
514 if(ret == 1) {
515 net_info.radio_state = MBTK_RADIO_STATE_ON;
516 } else {
517 net_info.radio_state = MBTK_RADIO_STATE_OFF;
518 }
519
520 return ret;
521
522error:
523
524 at_response_free(p_response);
525 return -1;
526}
527
528/** Returns SIM_NOT_READY on error */
529static mbtk_sim_state_enum getSIMStatus()
530{
531 ATResponse *p_response = NULL;
532 int err;
533 mbtk_sim_state_enum ret;
534 char *cpinLine;
535 char *cpinResult;
536
537 err = at_send_command_singleline("AT+CPIN?", "+CPIN:", &p_response);
538
539 if (err < 0 || p_response->success == 0 || !p_response->p_intermediates)
540 {
541 ret = MBTK_SIM_NOT_READY;
542 goto done;
543 }
544
545 switch (at_get_cme_error(p_response))
546 {
547 case CME_SUCCESS:
548 break;
549
550 case CME_SIM_NOT_INSERTED:
551 ret = MBTK_SIM_ABSENT;
552 goto done;
553
554 default:
555 ret = MBTK_SIM_NOT_READY;
556 goto done;
557 }
558
559 /* CPIN? has succeeded, now look at the result */
560
561 cpinLine = p_response->p_intermediates->line;
562 err = at_tok_start (&cpinLine);
563
564 if (err < 0)
565 {
566 ret = MBTK_SIM_NOT_READY;
567 goto done;
568 }
569
570 err = at_tok_nextstr(&cpinLine, &cpinResult);
571
572 if (err < 0)
573 {
574 ret = MBTK_SIM_NOT_READY;
575 goto done;
576 }
577
578 if (0 == strcmp (cpinResult, "SIM PIN"))
579 {
580 ret = MBTK_SIM_PIN;
581 goto done;
582 }
583 else if (0 == strcmp (cpinResult, "SIM PUK"))
584 {
585 ret = MBTK_SIM_PUK;
586 goto done;
587 }
588 else if (0 == strcmp (cpinResult, "PH-NET PIN"))
589 {
590 return MBTK_SIM_NETWORK_PERSONALIZATION;
591 }
592 else if (0 != strcmp (cpinResult, "READY"))
593 {
594 /* we're treating unsupported lock types as "sim absent" */
595 ret = MBTK_SIM_ABSENT;
596 goto done;
597 }
598
599 at_response_free(p_response);
600 p_response = NULL;
601 cpinResult = NULL;
602
603 // ret = net_info.radio_state == MBTK_RADIO_STATE_ON ? MBTK_SIM_READY : MBTK_SIM_NOT_READY;
604 net_info.sim_state = MBTK_SIM_READY;
605 return MBTK_SIM_READY;
606done:
607 at_response_free(p_response);
608 net_info.sim_state = ret;
609 return ret;
610}
611
612void setRadioPower(int isOn)
613{
614 int err;
615 ATResponse *p_response = NULL;
616
617 LOGI("RadioPower - %s", isOn == 0 ? "OFF" : "ON");
618
619 if(isOn == 0 && net_info.radio_state == MBTK_RADIO_STATE_ON)
620 {
621 err = at_send_command("AT+CFUN=0", &p_response);
622 if (err || !p_response->success)
623 goto error;
624
625 net_info.radio_state = MBTK_RADIO_STATE_OFF;
626 }
627 else if(isOn && net_info.radio_state != MBTK_RADIO_STATE_ON)
628 {
629 err = at_send_command("AT+CFUN=1", &p_response);
630 if (err || !p_response->success)
631 {
632 if(isRadioOn() == 1)
633 {
634 net_info.radio_state = MBTK_RADIO_STATE_ON;
635 }
636 goto error;
637 }
638
639 net_info.radio_state = MBTK_RADIO_STATE_ON;
640 }
641
642 at_response_free(p_response);
643 return;
644error:
645 at_response_free(p_response);
646}
647
648static int apn_user_pass_set_by_cid(int cid, mbtk_apn_info_t *apn)
649{
650 char prop_name[20] = {0};
651 char prop_data[300] = {0};
652 sprintf(prop_name, "%s_%d",MBTK_APN_PROP,cid);
653 if(property_get(prop_name, prop_data, "") > 0 && !str_empty(prop_data)) {
654 char apn_name[128] = {0};
655 char *ptr_1 = prop_data;
656 mbtk_ip_type_enum ip_type = (mbtk_ip_type_enum)atoi(ptr_1);
657 ptr_1 = strstr(ptr_1, ",");
658 if(!ptr_1) {
659 return -1;
660 }
661 ptr_1++; // Jump ',' to apn
662
663 char *ptr_2 = strstr(ptr_1, ",");
664 if(!ptr_2) {
665 return -1;
666 }
667 memcpy(apn_name, ptr_1, ptr_2 - ptr_1); // apn
668
669 // Check ip_type and apn_name
670 if(ip_type != apn->ip_type || strcmp(apn_name, apn->apn)) {
671 LOGD("APN Changed, not get user/pass/auth.");
672 return -1;
673 }
674
675 ptr_2++; // Jump ',' to user
676 ptr_1 = strstr(ptr_2, ",");
677 if(!ptr_1) {
678 return -1;
679 }
680 if(memcmp(ptr_2, "NULL", 4)) { // Not "NULL"
681 memcpy(apn->user, ptr_2, ptr_1 - ptr_2); // user
682 }
683
684 ptr_1++; // Jump ',' to pass
685 ptr_2 = strstr(ptr_1, ",");
686 if(!ptr_2) {
687 return -1;
688 }
689 if(memcmp(ptr_1, "NULL", 4)) { // Not "NULL"
690 memcpy(apn->pass, ptr_1, ptr_2 - ptr_1); // pass
691 }
692
693 ptr_2++; // Jump ',' to auth (Is last item)
694 if(memcmp(ptr_2, "NULL", 4)) { // Not "NULL"
695 memcpy(apn->auth, ptr_2, strlen(ptr_2)); // auth
696 }
697
698 return 0;
699 }
700 return -1;
701}
702
703
704/*
705AT+CPOL?
706*EUICC: 1
707
708OK
709*/
710static int req_plmn_get(mbtk_plmn_info *type, int *cme_err)
711{
712 ATResponse *response = NULL;
713 char *tmp_ptr = NULL;
714 int err = at_send_command_multiline("AT+CPOL?", "+CPOL:", &response);
715
716 if (err < 0 || response->success == 0 || !response->p_intermediates){
717 *cme_err = at_get_cme_error(response);
718 goto exit;
719 }
720
721 int mccmnc_type = -1;
722 int count = -1;
723 char *mccmnc_name = NULL;
724 ATLine* lines_ptr = response->p_intermediates;
725 char *line = NULL;
726 while(lines_ptr)
727 {
728 line = lines_ptr->line;
729 //if(strStartsWith(line, "+CPOL:"))
730 {
731 err = at_tok_start(&line);
732 if (err < 0)
733 {
734 goto exit;
735 }
736 err = at_tok_nextint(&line, &count);
737 if (err < 0)
738 {
739 goto exit;
740 }
741 type->count = count;
742
743 err = at_tok_nextint(&line, &mccmnc_type);
744 if (err < 0)
745 {
746 goto exit;
747 }
748 type->mbtk_plmn_name[count-1].format = mccmnc_type;
749
750 err = at_tok_nextstr(&line, &mccmnc_name);
751 if (err < 0)
752 {
753 goto exit;
754 }
755 memcpy(type->mbtk_plmn_name[count-1].plmn_name, mccmnc_name, strlen(mccmnc_name));
756 mccmnc_name = NULL;
757 }
758 lines_ptr = lines_ptr->p_next;
759 }
760
761exit:
762 at_response_free(response);
763 return err;
764}
765
766
767/*
768AT*EUICC?
769*EUICC: 1
770
771OK
772*/
773static int req_sim_card_type_get(uint8 *type, int *cme_err)
774{
775 ATResponse *response = NULL;
776 char *tmp_ptr = NULL;
777 int err = at_send_command_singleline("AT*EUICC?", "*EUICC:", &response);
778
779 if (err < 0 || response->success == 0 || !response->p_intermediates){
780 *cme_err = at_get_cme_error(response);
781 goto exit;
782 }
783
784 char *line = response->p_intermediates->line;
785 err = at_tok_start(&line);
786 if (err < 0)
787 {
788 goto exit;
789 }
790 int sim_card_type = -1;
791 err = at_tok_nextint(&line, &sim_card_type);
792 if (err < 0)
793 {
794 goto exit;
795 }
796 if(sim_card_type != -1)
797 *type = sim_card_type;
798 goto exit;
799exit:
800 at_response_free(response);
801 return err;
802}
803
804/*
805AT+EPIN?
806+EPIN: 3,0,10,0
807
808OK
809*/
810static int req_pin_puk_last_times_get(mbtk_pin_puk_last_times *times, int *cme_err)
811{
812 ATResponse *response = NULL;
813 char *tmp_ptr = NULL;
814 int err = at_send_command_singleline("AT+EPIN?", "+EPIN:", &response);
815
816 if (err < 0 || response->success == 0 || !response->p_intermediates){
817 *cme_err = at_get_cme_error(response);
818 goto exit;
819 }
820
821 char *line = response->p_intermediates->line;
822 err = at_tok_start(&line);
823 if (err < 0)
824 {
825 goto exit;
826 }
827 mbtk_pin_puk_last_times last_times={0};
828 err = at_tok_nextint(&line, &(last_times.p1_retry));
829 if (err < 0)
830 {
831 goto exit;
832 }
833 times->p1_retry = last_times.p1_retry;
834 err = at_tok_nextint(&line, &(last_times.p2_retry));
835 if (err < 0)
836 {
837 goto exit;
838 }
839 times->p2_retry = last_times.p2_retry;
840 err = at_tok_nextint(&line, &(last_times.puk1_retry));
841 if (err < 0)
842 {
843 goto exit;
844 }
845 times->puk1_retry = last_times.puk1_retry;
846 err = at_tok_nextint(&line, &(last_times.puk2_retry));
847 if (err < 0)
848 {
849 goto exit;
850 }
851 times->puk2_retry = last_times.puk2_retry;
852
853exit:
854 at_response_free(response);
855 return err;
856}
857
858
859/*
860AT+CGSN
861864788050901201
862
863OK
864*/
865static int req_imei_get(void *data, int *cme_err)
866{
867 ATResponse *response = NULL;
868 int err = at_send_command_numeric("AT+CGSN", &response);
869
870 if (err < 0 || response->success == 0 || !response->p_intermediates) {
871 *cme_err = at_get_cme_error(response);
872 goto exit;
873 }
874
875 memcpy(data, response->p_intermediates->line, strlen(response->p_intermediates->line));
876exit:
877 at_response_free(response);
878 return err;
879}
880
881/*
882AT+MRD_SN=R
883+MRD_SN:0101,Thu Nov 12 00:00:00 2020,G4M32301020006
884
885OK
886
887*/
888static int req_sn_get(void *data, int *cme_err)
889{
890 ATResponse *response = NULL;
891 char *tmp_ptr = NULL;
892 int err = at_send_command_singleline("AT+MRD_SN=R", "+MRD_SN:", &response);
893
894 if (err < 0 || response->success == 0 || !response->p_intermediates){
895 *cme_err = at_get_cme_error(response);
896 goto exit;
897 }
898
899 char *line = response->p_intermediates->line;
900 err = at_tok_start(&line);
901 if (err < 0)
902 {
903 goto exit;
904 }
905
906 err = at_tok_nextstr(&line, &tmp_ptr);
907 if (err < 0)
908 {
909 goto exit;
910 }
911
912 err = at_tok_nextstr(&line, &tmp_ptr);
913 if (err < 0)
914 {
915 goto exit;
916 }
917
918 err = at_tok_nextstr(&line, &tmp_ptr);
919 if (err < 0)
920 {
921 goto exit;
922 }
923
924 memcpy(data, tmp_ptr, strlen(tmp_ptr));
925
926 goto exit;
927exit:
928 at_response_free(response);
929 return err;
930}
931
932/*
933AT+SYSTIME?
934+SYSTIME: 1
935
936OK
937
938*/
939static int req_time_get(int *data, int *cme_err)
940{
941 ATResponse *response = NULL;
942 int tmp_int;
943 int err = at_send_command_singleline("AT+SYSTIME?", "+SYSTIME:", &response);
944
945 if (err < 0 || response->success == 0 || !response->p_intermediates){
946 *cme_err = at_get_cme_error(response);
947 goto exit;
948 }
949
950 char *line = response->p_intermediates->line;
951 err = at_tok_start(&line);
952 if (err < 0)
953 {
954 goto exit;
955 }
956 err = at_tok_nextint(&line, &tmp_int);
957 if (err < 0)
958 {
959 goto exit;
960 }
961 *data = tmp_int;
962
963exit:
964 at_response_free(response);
965 return err;
966}
967
968/*
969AT+CCLK?
970+CCLK: "23/03/20,01:58:00+32"
971
972OK
973
974*/
975static int req_net_time_get(char *data, int *cme_err)
976{
977 ATResponse *response = NULL;
978 char *tmp_ptr = NULL;
979 int err = at_send_command_singleline("AT+CCLK?", "+CCLK:", &response);
980
981 if (err < 0 || response->success == 0 || !response->p_intermediates){
982 *cme_err = at_get_cme_error(response);
983 goto exit;
984 }
985
986 char *line = response->p_intermediates->line;
987 err = at_tok_start(&line);
988 if (err < 0)
989 {
990 goto exit;
991 }
992 err = at_tok_nextstr(&line, &tmp_ptr);
993 if (err < 0)
994 {
995 goto exit;
996 }
997 memcpy(data, tmp_ptr, strlen(tmp_ptr));
998
999exit:
1000 at_response_free(response);
1001 return err;
1002}
1003
1004/*
1005AT+CFUN?
1006+CFUN: 1
1007OK
1008*/
1009static int req_modem_get(int *data, int *cme_err)
1010{
1011 ATResponse *response = NULL;
1012 int modem;
1013 int err = at_send_command_singleline("AT+CFUN?", "+CFUN:", &response);
1014
1015 if (err < 0 || response->success == 0 || !response->p_intermediates){
1016 *cme_err = at_get_cme_error(response);
1017 goto exit;
1018 }
1019
1020 char *line = response->p_intermediates->line;
1021 err = at_tok_start(&line);
1022 if (err < 0)
1023 {
1024 goto exit;
1025 }
1026 err = at_tok_nextint(&line, &modem);
1027 if (err < 0)
1028 {
1029 goto exit;
1030 }
1031 *data = modem;
1032
1033exit:
1034 at_response_free(response);
1035 return err;
1036}
1037
1038/*
1039AT+CFUN=<fun>[,<rst>]
1040OK
1041*/
1042static int req_modem_set(mbtk_modem_info_t* modem, int *cme_err)
1043{
1044 ATResponse *response = NULL;
1045 char cmd[30] = {0};
1046 int err = -1;
1047
1048 sprintf(cmd, "AT+CFUN=%d,%d", modem->fun, modem->rst);
1049 err = at_send_command(cmd, &response);
1050
1051 if (err < 0 || response->success == 0){
1052 *cme_err = at_get_cme_error(response);
1053 goto exit;
1054 }
1055/*
1056 ATResponse *response = NULL;
1057 int err = at_send_command_multiline(cmd, "", &response);
1058
1059 if (err < 0 || response->success == 0 || !response->p_intermediates){
1060 *cme_err = at_get_cme_error(response);
1061 goto exit;
1062 }
1063
1064 ATLine* lines_ptr = response->p_intermediates;
1065 char *line = NULL;
1066 while(lines_ptr)
1067 {
1068 line = lines_ptr->line;
1069 if(strStartsWith(line, "Revision"))
1070 {
1071 err = at_tok_start(&line);
1072 if (err < 0)
1073 {
1074 goto exit;
1075 }
1076 memcpy(data, line, strlen(line));
1077 break;
1078 }
1079 lines_ptr = lines_ptr->p_next;
1080 }
1081
1082 goto exit;
1083*/
1084exit:
1085 at_response_free(response);
1086 return err;
1087}
1088
1089
1090/*
1091AT+SYSTIME=0,"2022-01-25-11:15:30"
1092OK
1093
1094AT+SYSTIME=1
1095OK
1096*/
1097static int req_time_set(int type, char *time, int *cme_err)
1098{
1099 ATResponse *response = NULL;
1100 int tmp_int;
1101 char cmd[200] = {0};
1102 if(str_empty(time)){
1103 sprintf(cmd, "AT+SYSTIME=%d", type);
1104 } else {
1105 sprintf(cmd, "AT+SYSTIME=%d,\"%s\"", type, time);
1106 }
1107 int err = at_send_command(cmd, &response);
1108
1109 if (err < 0 || response->success == 0){
1110 *cme_err = at_get_cme_error(response);
1111 goto exit;
1112 }
1113
1114exit:
1115 at_response_free(response);
1116 return err;
1117}
1118
1119
1120/*
1121ATI
1122Manufacturer:"LYNQ"
1123Model:"LYNQ_L508TLC"
1124Revision:L508TLCv02.01b01.00
1125IMEI:864788050901201
1126
1127OK
1128
1129*/
1130static int req_version_get(void *data, int *cme_err)
1131{
1132 ATResponse *response = NULL;
1133 int err = at_send_command_multiline("ATI", "", &response);
1134
1135 if (err < 0 || response->success == 0 || !response->p_intermediates){
1136 *cme_err = at_get_cme_error(response);
1137 goto exit;
1138 }
1139
1140 ATLine* lines_ptr = response->p_intermediates;
1141 char *line = NULL;
1142 while(lines_ptr)
1143 {
1144 line = lines_ptr->line;
1145 if(strStartsWith(line, "Revision"))
1146 {
1147 err = at_tok_start(&line);
1148 if (err < 0)
1149 {
1150 goto exit;
1151 }
1152 memcpy(data, line, strlen(line));
1153 break;
1154 }
1155 lines_ptr = lines_ptr->p_next;
1156 }
1157
1158 goto exit;
1159exit:
1160 at_response_free(response);
1161 return err;
1162}
1163
1164/*
1165ATI
1166Manufacturer:"LYNQ"
1167Model:"LYNQ_L508TLC"
1168Revision:L508TLCv02.01b01.00
1169IMEI:864788050901201
1170
1171OK
1172
1173*/
1174static int req_model_get(void *data, int *cme_err)
1175{
1176 ATResponse *response = NULL;
1177 int err = at_send_command_multiline("ATI", "", &response);
1178
1179 if (err < 0 || response->success == 0 || !response->p_intermediates){
1180 *cme_err = at_get_cme_error(response);
1181 goto exit;
1182 }
1183
1184 ATLine* lines_ptr = response->p_intermediates;
1185 char *line = NULL;
1186 while(lines_ptr)
1187 {
1188 line = lines_ptr->line;
1189 if(strStartsWith(line, "Model"))
1190 {
1191 err = at_tok_start(&line);
1192 if (err < 0)
1193 {
1194 goto exit;
1195 }
1196 memcpy(data, line, strlen(line));
1197 break;
1198 }
1199 lines_ptr = lines_ptr->p_next;
1200 }
1201
1202 goto exit;
1203exit:
1204 at_response_free(response);
1205 return err;
1206}
1207
1208/*
1209AT+ACONFIG="IMSD=1"
1210or
1211AT+ACONFIG="IMSD=0"
1212
1213OK
1214*/
1215static int req_volte_set(int state, int *cme_err)
1216{
1217 ATResponse *response = NULL;
1218 char cmd[30] = {0};
1219 if(state)
1220 {
1221 strcpy(cmd, "AT+ACONFIG=\"IMSD=1\"");
1222 }
1223 else
1224 {
1225 strcpy(cmd, "AT+ACONFIG=\"IMSD=0\"");
1226 }
1227 int err = at_send_command(cmd, &response);
1228
1229 if (err < 0 || response->success == 0) {
1230 *cme_err = at_get_cme_error(response);
1231 goto exit;
1232 }
1233
1234 err = 0;
1235exit:
1236 at_response_free(response);
1237 return err;
1238}
1239
1240/*
1241AT+ACONFIG?
1242PID=0,VID=0,IMSD=1,PIPE=0,FAST=0,RDUP=1,NOCP=0,GEFL=-1237040617
1243
1244OK
1245*/
1246static int req_volte_get(int *state, int *cme_err)
1247{
1248 ATResponse *response = NULL;
1249 char *tmp_ptr = NULL;
1250 int err = at_send_command_singleline("AT+ACONFIG?", "", &response);
1251
1252 if (err < 0 || response->success == 0 || !response->p_intermediates){
1253 *cme_err = at_get_cme_error(response);
1254 goto exit;
1255 }
1256
1257 char *line = response->p_intermediates->line;
1258 char* ptr = strstr(line, "IMSD=");
1259 if(ptr)
1260 {
1261 *state = atoi(ptr + strlen("IMSD="));
1262 }
1263 else
1264 {
1265 err = -1;
1266 }
1267exit:
1268 at_response_free(response);
1269 return err;
1270}
1271
1272
1273/*
1274* Get system temperature.
1275*
1276* type[IN]:
1277* 0: Soc temperature.
1278* 1: RF temperature.
1279* temp[OUT]:
1280* temperature in celsius.
1281*
1282
1283AT*RFTEMP
1284*RFTEMP:0,28
1285OK
1286
1287AT*SOCTEMP
1288*SOCTEMP:24000
1289OK
1290
1291*/
1292static int req_temp_get(int type, int *temp, int *cme_err)
1293{
1294 ATResponse *response = NULL;
1295 int err = -1;
1296 int tmp_int;
1297 if(type == 0) { // Soc
1298 err = at_send_command_singleline("AT*SOCTEMP", "*SOCTEMP:", &response);
1299 } else { // RF
1300 err = at_send_command_singleline("AT*RFTEMP", "*RFTEMP:", &response);
1301 }
1302
1303 if (err < 0 || response->success == 0 || !response->p_intermediates){
1304 *cme_err = at_get_cme_error(response);
1305 goto exit;
1306 }
1307
1308 char *line = response->p_intermediates->line;
1309 err = at_tok_start(&line);
1310 if (err < 0)
1311 {
1312 goto exit;
1313 }
1314 err = at_tok_nextint(&line, &tmp_int);
1315 if (err < 0)
1316 {
1317 goto exit;
1318 }
1319
1320 if(type == 1) { // RF
1321 err = at_tok_nextint(&line, &tmp_int);
1322 if (err < 0)
1323 {
1324 goto exit;
1325 }
1326 *temp = tmp_int;
1327 } else {
1328 *temp = tmp_int / 1000;
1329 }
1330
1331exit:
1332 at_response_free(response);
1333 return err;
1334}
1335
1336/*
1337AT*BAND=15
1338OK
1339
1340*/
1341static int req_band_set(mbtk_band_info_t* band, int *cme_err)
1342{
1343 ATResponse *response = NULL;
1344 char cmd[30] = {0};
1345 int err = -1;
1346
1347 if(band->gsm_band == 0 && band->umts_band == 0
1348 && band->tdlte_band == 0 && band->fddlte_band == 0) {
1349 sprintf(cmd, "AT*BAND=%d", band->net_pref);
1350 } else {
1351 band_support_get();
1352
1353 log_hex("BAND_SUPPORT", &band_support, sizeof(mbtk_band_info_t));
1354 log_hex("BAND", band, sizeof(mbtk_band_info_t));
1355
1356 if(band->gsm_band == 0) {
1357 band->gsm_band = band_support.gsm_band;
1358 }
1359 if(band->umts_band == 0) {
1360 band->umts_band = band_support.umts_band;
1361 }
1362 if(band->tdlte_band == 0) {
1363 band->tdlte_band = band_support.tdlte_band;
1364 }
1365 if(band->fddlte_band == 0) {
1366 band->fddlte_band = band_support.fddlte_band;
1367 }
1368
1369 if((band->gsm_band & band_support.gsm_band) != band->gsm_band) {
1370 LOG("GSM band error.");
1371 goto exit;
1372 }
1373
1374 if((band->umts_band & band_support.umts_band) != band->umts_band) {
1375 LOG("UMTS band error.");
1376 goto exit;
1377 }
1378
1379 if((band->tdlte_band & band_support.tdlte_band) != band->tdlte_band) {
1380 LOG("TDLTE band error.");
1381 goto exit;
1382 }
1383
1384 if((band->fddlte_band & band_support.fddlte_band) != band->fddlte_band) {
1385 LOG("FDDLTE band error.");
1386 goto exit;
1387 }
1388
1389 if(band->net_pref == 0xFF) { // No change net_pref.
1390 int tmp_int;
1391 err = at_send_command_singleline("AT*BAND?", "*BAND:", &response);
1392 if (err < 0 || response->success == 0 || !response->p_intermediates){
1393 *cme_err = at_get_cme_error(response);
1394 goto exit;
1395 }
1396
1397 char *line = response->p_intermediates->line;
1398 err = at_tok_start(&line);
1399 if (err < 0)
1400 {
1401 goto exit;
1402 }
1403
1404 err = at_tok_nextint(&line, &tmp_int);
1405 if (err < 0)
1406 {
1407 goto exit;
1408 }
1409 band->net_pref = (uint8)tmp_int; // Set to current net_pref.
1410
1411 at_response_free(response);
1412 }
1413
1414 sprintf(cmd, "AT*BAND=%d,%d,%d,%d,%d", band->net_pref, band->gsm_band, band->umts_band, band->tdlte_band, band->fddlte_band);
1415 }
1416 err = at_send_command(cmd, &response);
1417
1418 if (err < 0 || response->success == 0){
1419 *cme_err = at_get_cme_error(response);
1420 goto exit;
1421 }
1422
1423 err = 0;
1424exit:
1425 at_response_free(response);
1426 return err;
1427}
1428
1429/*
1430// ???????
1431AT*BAND=?
1432*BAND:(0-18),79,147,482,524503
1433
1434OK
1435
1436// ???????????
1437AT*BAND?
1438*BAND: 15, 78, 147, 482, 524503, 0, 2, 2, 0, 0
1439
1440OK
1441
1442// ?????????
1443AT*BAND=5,79,147,128,1
1444OK
1445
1446net_prefferred??
1447 0 : GSM only
1448 1 : UMTS only
1449 2 : GSM/UMTS(auto)
1450 3 : GSM/UMTS(GSM preferred)
1451 4 : GSM/UMTS(UMTS preferred)
1452 5 : LTE only
1453 6 : GSM/LTE(auto)
1454 7 : GSM/LTE(GSM preferred)
1455 8 : GSM/LTE(LTE preferred)
1456 9 : UMTS/LTE(auto)
1457 10 : UMTS/LTE(UMTS preferred)
1458 11 : UMTS/LTE(LTE preferred)
1459 12 : GSM/UMTS/LTE(auto)
1460 13 : GSM/UMTS/LTE(GSM preferred)
1461 14 : GSM/UMTS/LTE(UMTS preferred)
1462 15 : GSM/UMTS/LTE(LTE preferred)
1463GSM band??
1464 1 ?C PGSM 900 (standard or primary)
1465 2 ?C DCS GSM 1800
1466 4 ?C PCS GSM 1900
1467 8 ?C EGSM 900 (extended)
1468 16 ?C GSM 450
1469 32 ?C GSM 480
1470 64 ?C GSM 850
1471 512 - BAND_LOCK_BIT // used for GSM band setting
1472UMTS band??
1473 1 ?C UMTS_BAND_1
1474 2 ?C UMTS_BAND_2
1475 4 ?C UMTS_BAND_3
1476 8 ?C UMTS_BAND_4
1477 16 ?C UMTS_BAND_5
1478 32 ?C UMTS_BAND_6
1479 64 ?C UMTS_BAND_7
1480 128 ?C UMTS_BAND_8
1481 256 ?C UMTS_BAND_9
1482LTEbandH(TDD-LTE band)
1483 32 ?C TDLTE_BAND_38
1484 64 ?C TDLTE_BAND_39
1485 128 ?C TDLTE_BAND_40
1486 256 ?C TDLTE_BAND_41
1487LTEbandL(FDD-LTE band)
1488 1 ?C FDDLTE_BAND_1
1489 4 ?C FDDLTE _BAND_3
1490 8 ?C FDDLTE _BAND_4
1491 64 ?C FDDLTE _BAND_7
1492 65536 ?C FDDLTE _BAND_17
1493 524288 ?C FDDLTE _BAND_20
1494*/
1495static int req_band_get(mbtk_band_info_t *band, int *cme_err)
1496{
1497 ATResponse *response = NULL;
1498 int tmp_int;
1499
1500 band_support_get();
1501
1502 log_hex("BAND_SUPPORT", &band_support, sizeof(mbtk_band_info_t));
1503 int err = at_send_command_singleline("AT*BAND?", "*BAND:", &response);
1504 if (err < 0 || response->success == 0 || !response->p_intermediates){
1505 *cme_err = at_get_cme_error(response);
1506 goto exit;
1507 }
1508
1509 char *line = response->p_intermediates->line;
1510 err = at_tok_start(&line);
1511 if (err < 0)
1512 {
1513 goto exit;
1514 }
1515
1516 err = at_tok_nextint(&line, &tmp_int);
1517 if (err < 0)
1518 {
1519 goto exit;
1520 }
1521 band->net_pref = (uint8)tmp_int;
1522
1523 err = at_tok_nextint(&line, &tmp_int);
1524 if (err < 0)
1525 {
1526 goto exit;
1527 }
1528 band->gsm_band = (uint16)tmp_int;
1529
1530 err = at_tok_nextint(&line, &tmp_int);
1531 if (err < 0)
1532 {
1533 goto exit;
1534 }
1535 band->umts_band = (uint16)tmp_int;
1536
1537 err = at_tok_nextint(&line, &tmp_int);
1538 if (err < 0)
1539 {
1540 goto exit;
1541 }
1542 band->tdlte_band = (uint32)tmp_int;
1543
1544 err = at_tok_nextint(&line, &tmp_int);
1545 if (err < 0)
1546 {
1547 goto exit;
1548 }
1549 band->fddlte_band = (uint32)tmp_int;
1550
1551 log_hex("BAND", band, sizeof(mbtk_band_info_t));
1552
1553exit:
1554 at_response_free(response);
1555 return err;
1556}
1557
1558/*
1559AT+ICCID
1560+ICCID: 89860621330065648041
1561
1562OK
1563*/
1564static int req_iccid_get(void *data, int *cme_err)
1565{
1566 ATResponse *response = NULL;
1567 char *tmp_ptr = NULL;
1568 int err = at_send_command_singleline("AT+ICCID", "+ICCID:", &response);
1569
1570 if (err < 0 || response->success == 0 || !response->p_intermediates){
1571 *cme_err = at_get_cme_error(response);
1572 goto exit;
1573 }
1574
1575 char *line = response->p_intermediates->line;
1576 err = at_tok_start(&line);
1577 if (err < 0)
1578 {
1579 goto exit;
1580 }
1581
1582 err = at_tok_nextstr(&line, &tmp_ptr);
1583 if (err < 0)
1584 {
1585 goto exit;
1586 }
1587
1588 memcpy(data, tmp_ptr, strlen(tmp_ptr));
1589exit:
1590 at_response_free(response);
1591 return err;
1592}
1593
1594/*
1595AT+CNUM?
1596+CNUM: "","13980414101",129
1597
1598OK
1599
1600*/
1601static int req_phone_number_get(void *data, int *cme_err)
1602{
1603 ATResponse *response = NULL;
1604 char *tmp_ptr = NULL;
1605 int err = at_send_command_singleline("AT+CNUM?", "+CNUM:", &response);
1606 if (err < 0 || response == NULL || response->success == 0 || !response->p_intermediates){
1607 if(response) {
1608 *cme_err = at_get_cme_error(response);
1609 }
1610 LOGD("AT+CNUM? fail.");
1611 goto exit;
1612 }
1613
1614 char *line = response->p_intermediates->line;
1615 if(line == NULL) {
1616 LOGD("line is NULL");
1617 goto exit;
1618 }
1619 err = at_tok_start(&line);
1620 if (err < 0)
1621 {
1622 goto exit;
1623 }
1624
1625 err = at_tok_nextstr(&line, &tmp_ptr);
1626 if (err < 0)
1627 {
1628 goto exit;
1629 }
1630
1631 err = at_tok_nextstr(&line, &tmp_ptr);
1632 if (err < 0)
1633 {
1634 goto exit;
1635 }
1636
1637 memcpy(data, tmp_ptr, strlen(tmp_ptr));
1638exit:
1639 at_response_free(response);
1640 return err;
1641}
1642
1643
1644/*
1645AT+CIMI
1646460068103383304
1647
1648OK
1649
1650*/
1651static int req_imsi_get(void *data, int *cme_err)
1652{
1653 ATResponse *response = NULL;
1654 int err = at_send_command_numeric("AT+CIMI", &response);
1655
1656 if (err < 0 || response->success == 0 || !response->p_intermediates){
1657 *cme_err = at_get_cme_error(response);
1658 goto exit;
1659 }
1660
1661 memcpy(data, response->p_intermediates->line, strlen(response->p_intermediates->line));
1662exit:
1663 at_response_free(response);
1664 return err;
1665}
1666
1667
1668/*
1669AT+CLCK=SC,1/0,1234
1670+CLCK:1/0
1671
1672OK
1673
1674*/
1675static int req_pin_enable(mbtk_enable_pin_info *data, int *cme_err)
1676{
1677 ATResponse *response = NULL;
1678 char cmd[64]={0};
1679 sprintf(cmd, "AT+CLCK=SC,%d,%s", data->enable, data->pin_value);
1680
1681 int err = at_send_command_singleline(cmd, "+CLCK:", &response);
1682 if (err < 0 || response == NULL || response->success == 0 || !response->p_intermediates){
1683 if(response) {
1684 *cme_err = at_get_cme_error(response);
1685 }
1686 LOGD("AT+CLCK? fail.");
1687 goto exit;
1688 }
1689
1690 char *line = response->p_intermediates->line;
1691 if(line == NULL) {
1692 LOGD("line is NULL");
1693 goto exit;
1694 }
1695 err = at_tok_start(&line);
1696 if (err < 0)
1697 {
1698 goto exit;
1699 }
1700 int clck;
1701 err = at_tok_nextint(&line, &clck);
1702 if (err < 0)
1703 {
1704 goto exit;
1705 }
1706
1707exit:
1708 at_response_free(response);
1709 return err;
1710}
1711
1712/*
1713AT+CPIN=1234
1714
1715OK
1716
1717*/
1718static int req_pin_verify(char *data, int *cme_err)
1719{
1720 ATResponse *response = NULL;
1721 char cmd[64]={0};
1722 sprintf(cmd, "AT+CPIN=%s", data);
1723 int err = at_send_command(cmd, &response);
1724 if (err < 0 || response->success == 0){
1725 if(cme_err) {
1726 *cme_err = at_get_cme_error(response);
1727 }
1728 goto exit;
1729 }
1730
1731exit:
1732 at_response_free(response);
1733 return err;
1734}
1735
1736/*
1737AT+CLCK=SC,2
1738+CLCK: 1
1739
1740OK
1741
1742AT+CLCK="SC",1,"1234"
1743+CLCK:1
1744
1745OK
1746
1747AT+CPWD="SC","1234","4321"
1748
1749OK
1750
1751*/
1752static int req_pin_change(mbtk_change_pin_info *data, int *cme_err)
1753{
1754 ATResponse *response = NULL;
1755 char cmd[64]={0};
1756
1757 int err = at_send_command_singleline("AT+CLCK=SC,2", "+CLCK:", &response);
1758 if (err < 0 || response == NULL || response->success == 0 || !response->p_intermediates){
1759 if(response) {
1760 *cme_err = at_get_cme_error(response);
1761 }
1762 LOGD("AT+CLCK fail.");
1763 goto exit;
1764 }
1765
1766 char *line = response->p_intermediates->line;
1767 if(line == NULL) {
1768 LOGD("line is NULL");
1769 goto exit;
1770 }
1771 err = at_tok_start(&line);
1772 if (err < 0)
1773 {
1774 goto exit;
1775 }
1776 int clck;
1777 err = at_tok_nextint(&line, &clck);
1778 if (err < 0)
1779 {
1780 goto exit;
1781 }
1782 at_response_free(response);
1783
1784 if(clck==0)
1785 {
1786 sprintf(cmd, "AT+CLCK=SC,1,%s", data->old_pin_value);
1787 err = at_send_command_singleline(cmd, "+CLCK:", &response);
1788 if (err < 0 || response == NULL || response->success == 0 || !response->p_intermediates){
1789 if(response) {
1790 *cme_err = at_get_cme_error(response);
1791 }
1792 LOGD("AT+CLCK fail.");
1793 goto exit;
1794 }
1795 line = response->p_intermediates->line;
1796 if(line == NULL) {
1797 LOGD("line is NULL");
1798 goto exit;
1799 }
1800 err = at_tok_start(&line);
1801 if (err < 0)
1802 {
1803 goto exit;
1804 }
1805 clck = -1;
1806 err = at_tok_nextint(&line, &clck);
1807 if (err < 0)
1808 {
1809 goto exit;
1810 }
1811 at_response_free(response);
1812 if(clck != 1)
1813 return err;
1814 }
1815 memset(cmd, 0, 64);
1816 sprintf(cmd, "AT+CPWD=SC,%s,%s", data->old_pin_value,data->new_pin_value);
1817 err = at_send_command(cmd, &response);
1818 if (err < 0 || response->success == 0){
1819 if(cme_err) {
1820 *cme_err = at_get_cme_error(response);
1821 }
1822 goto exit;
1823 }
1824
1825exit:
1826 at_response_free(response);
1827 return err;
1828}
1829
1830/*
1831AT+CPIN?
1832+CPIN:SIM PUK
1833
1834OK
1835
1836AT+CPIN="PUK","PIN"
1837+CPIN: READY
1838
1839OK
1840*/
1841static int req_puk_unlock_pin(mbtk_unlock_pin_info *data, int *cme_err)
1842{
1843 ATResponse *response = NULL;
1844 char cmd[64]={0};
1845#if 0
1846 int err = at_send_command_singleline("AT+CPIN?", "+CPIN:", &response);
1847 if (err < 0 || response == NULL || response->success == 0 || !response->p_intermediates){
1848 if(response) {
1849 *cme_err = at_get_cme_error(response);
1850 }
1851 LOGD("AT+CNUM? fail.");
1852 goto exit;
1853 }
1854
1855 char *line = response->p_intermediates->line;
1856 if(line == NULL) {
1857 LOGD("line is NULL");
1858 goto exit;
1859 }
1860 err = at_tok_start(&line);
1861 if (err < 0)
1862 {
1863 goto exit;
1864 }
1865 char *tmp_ptr = NULL;
1866 err = at_tok_nextstr(&line, &tmp_ptr);
1867 if (err < 0)
1868 {
1869 goto exit;
1870 }
1871 at_response_free(response);
1872
1873 if(!strstr(tmp_ptr,"SIM PUK"))
1874 {
1875 sprintf(cmd, "AT+CPIN=%s,%s", data->puk_value, data->pin_value);
1876 err = at_send_command_singleline(cmd, "+CPIN:", &response);
1877 if (err < 0 || response == NULL || response->success == 0 || !response->p_intermediates){
1878 if(response) {
1879 *cme_err = at_get_cme_error(response);
1880 }
1881 LOGD("AT+CNUM? fail.");
1882 goto exit;
1883 }
1884 line = response->p_intermediates->line;
1885 if(line == NULL) {
1886 LOGD("line is NULL");
1887 goto exit;
1888 }
1889 err = at_tok_start(&line);
1890 if (err < 0)
1891 {
1892 goto exit;
1893 }
1894 memset(tmp_ptr, 0, strlen(tmp_ptr));
1895 err = at_tok_nextstr(&line, &tmp_ptr);
1896 if (err < 0)
1897 {
1898 goto exit;
1899 }
1900 at_response_free(response);
1901 if(strstr(tmp_ptr, "READY"))
1902 return err;
1903 }
1904 else
1905 return err;
1906#else
1907 sprintf(cmd, "AT+CPIN=%s,%s", data->puk_value, data->pin_value);
1908 int err = at_send_command_singleline(cmd, "+CPIN: READY:", &response);
1909 if (err < 0 || response == NULL || response->success == 0 || !response->p_intermediates){
1910 if(response) {
1911 *cme_err = at_get_cme_error(response);
1912 }
1913 LOGD("AT+CPIN fail.");
1914 goto exit;
1915 }
1916#endif
1917exit:
1918 at_response_free(response);
1919 return err;
1920}
1921
1922/*
1923AT+COPS=?
1924
1925+COPS: (2, "CHINA MOBILE", "CMCC", "46000", 7),(3, "CHN-CT", "CT", "46011", 7),(3, "CHN-UNICOM", "UNICOM", "46001", 7),(1, "460 15", "460 15", "46015", 7),,(0,1,2,3,4),(0,1,2)
1926
1927OK
1928
1929// Return [sel_mode(uint8)type(uint8)plmn(uint32)...sel_mode(uint8)type(uint8)plmn(uint32)]
1930*/
1931#if 0
1932static int req_available_net_get(mbtk_net_array_info_t *data_ptr)
1933{
1934 ATResponse *response = NULL;
1935 char *tmp_ptr = NULL;
1936 int tmp_int;
1937 int err = at_send_command_singleline("AT+COPS=?", "+COPS:", &response);
1938
1939 if (err < 0 || response->success == 0 || !response->p_intermediates)
1940 goto exit;
1941#if 1
1942 char *line_ptr = response->p_intermediates->line;
1943 if(line_ptr == NULL) {
1944 LOG("line is NULL");
1945 goto exit;
1946 }
1947 //LOG("Line:%s",line_ptr);
1948 line_ptr = strstr(line_ptr, "(");
1949 while(line_ptr) {
1950 line_ptr++;
1951 // Only for available/current net.
1952 if(*line_ptr == '1' || *line_ptr == '2') {
1953 //LOG("Temp:%s",line_ptr);
1954 //sleep(1);
1955 line_ptr = strstr(line_ptr, ",");
1956 if(line_ptr == NULL)
1957 goto exit;
1958 line_ptr++;
1959
1960 line_ptr = strstr(line_ptr, ",");
1961 if(line_ptr == NULL)
1962 goto exit;
1963 line_ptr++;
1964
1965 line_ptr = strstr(line_ptr, ",");
1966 if(line_ptr == NULL)
1967 goto exit;
1968
1969 while(*line_ptr != '\0' && (*line_ptr == ',' || *line_ptr == ' ' || *line_ptr == '"'))
1970 line_ptr++;
1971
1972 mbtk_net_info_t *net = (mbtk_net_info_t*)malloc(sizeof(mbtk_net_info_t));
1973 if(net == NULL) {
1974 LOG("malloc() fail.");
1975 goto exit;
1976 }
1977 memset(net, 0, sizeof(mbtk_net_info_t));
1978
1979 // Point to "46000"
1980 //LOG("PLMN:%s",line_ptr);
1981 //sleep(1);
1982 net->plmn = (uint32)atoi(line_ptr);
1983
1984 line_ptr = strstr(line_ptr, ",");
1985 if(line_ptr == NULL)
1986 goto exit;
1987
1988 while(*line_ptr != '\0' && (*line_ptr == ',' || *line_ptr == ' '))
1989 line_ptr++;
1990
1991 // Point to "7"
1992 if(*line_ptr == '\0') {
1993 free(net);
1994 goto exit;
1995 }
1996 //LOG("Type:%s",line_ptr);
1997 //sleep(1);
1998 net->net_type = (uint8)atoi(line_ptr);
1999 list_add(data_ptr->net_list, net);
2000 data_ptr->count++;
2001 }
2002
2003 line_ptr = strstr(line_ptr, "(");
2004 }
2005#endif
2006exit:
2007 at_response_free(response);
2008 return err;
2009}
2010#else
2011static int req_available_net_get(void* buff, int *cme_err)
2012{
2013 ATResponse *response = NULL;
2014 char *tmp_ptr = NULL;
2015 int tmp_int;
2016 int buff_size = 0;
2017 int err = at_send_command_singleline("AT+COPS=?", "+COPS:", &response);
2018
2019 if (err < 0 || response->success == 0 || !response->p_intermediates){
2020 *cme_err = at_get_cme_error(response);
2021 goto exit;
2022 }
2023 char *line_ptr = response->p_intermediates->line;
2024 if(line_ptr == NULL) {
2025 LOG("line is NULL");
2026 goto exit;
2027 }
2028 uint8* buff_ptr = (uint8*)buff;
2029 //LOG("Line:%s",line_ptr);
2030 line_ptr = strstr(line_ptr, "(");
2031 while(line_ptr) {
2032 line_ptr++;
2033 // Only for available/current net.
2034 if(*line_ptr == '1' || *line_ptr == '2' || *line_ptr == '3') {
2035 *(buff_ptr + 2) = (uint8)atoi(line_ptr); // net_state
2036
2037 line_ptr = strstr(line_ptr, ",");
2038 if(line_ptr == NULL)
2039 goto exit;
2040 line_ptr++;
2041
2042 line_ptr = strstr(line_ptr, ",");
2043 if(line_ptr == NULL)
2044 goto exit;
2045 line_ptr++;
2046
2047 line_ptr = strstr(line_ptr, ",");
2048 if(line_ptr == NULL)
2049 goto exit;
2050
2051 while(*line_ptr != '\0' && (*line_ptr == ',' || *line_ptr == ' ' || *line_ptr == '"'))
2052 line_ptr++;
2053
2054 // set sel_mode to 0
2055 *buff_ptr = (uint8)0;
2056 // Point to "46000"
2057 //LOG("PLMN:%s",line_ptr);
2058 //sleep(1);
2059 uint32_2_byte((uint32)atoi(line_ptr), buff_ptr + 3, false); // plmn
2060
2061 line_ptr = strstr(line_ptr, ",");
2062 if(line_ptr == NULL)
2063 goto exit;
2064
2065 while(*line_ptr != '\0' && (*line_ptr == ',' || *line_ptr == ' '))
2066 line_ptr++;
2067
2068 // Point to "7"
2069 if(*line_ptr == '\0') {
2070 goto exit;
2071 }
2072 //LOG("Type:%s",line_ptr);
2073 //sleep(1);
2074 *(buff_ptr + 1) = (uint8)atoi(line_ptr); // net_type
2075
2076 buff_size += sizeof(mbtk_net_info_t);
2077 buff_ptr += sizeof(mbtk_net_info_t);
2078 }
2079
2080 line_ptr = strstr(line_ptr, "(");
2081 }
2082exit:
2083 at_response_free(response);
2084 return buff_size;
2085}
2086#endif
2087
2088/*
2089AT+COPS?
2090+COPS: 1
2091
2092OK
2093
2094or
2095
2096AT+COPS?
2097+COPS: 0,2,"46001",7
2098
2099OK
2100
2101*/
2102static int req_net_sel_mode_get(mbtk_net_info_t *net, int *cme_err)
2103{
2104 //LOG("req_net_sel_mode_get() 0");
2105 //sleep(1);
2106 ATResponse *response = NULL;
2107 int tmp_int;
2108 char *tmp_ptr = NULL;
2109 int err = at_send_command_singleline("AT+COPS?", "+COPS:", &response);
2110 //LOG("req_net_sel_mode_get() 00");
2111 //sleep(1);
2112 if (err < 0 || response->success == 0 || !response->p_intermediates){
2113 if(cme_err != NULL)
2114 *cme_err = at_get_cme_error(response);
2115 err = -1;
2116 goto exit;
2117 }
2118 //LOG("req_net_sel_mode_get() 1");
2119 //sleep(1);
2120 char *line = response->p_intermediates->line;
2121 if(line == NULL) {
2122 LOG("line is NULL");
2123 goto exit;
2124 }
2125 //LOG("req_net_sel_mode_get() 2");
2126 //sleep(1);
2127 err = at_tok_start(&line);
2128 if (err < 0)
2129 {
2130 goto exit;
2131 }
2132 //LOG("req_net_sel_mode_get() 3");
2133 //sleep(1);
2134 err = at_tok_nextint(&line, &tmp_int);
2135 if (err < 0)
2136 {
2137 goto exit;
2138 }
2139 net->net_sel_mode = (uint8)tmp_int;
2140 //LOG("req_net_sel_mode_get() 4");
2141 //sleep(1);
2142 // +COPS: 1
2143 if(!at_tok_hasmore(&line)) {
2144 goto exit;
2145 }
2146 //LOG("req_net_sel_mode_get() 5");
2147 //sleep(1);
2148 err = at_tok_nextint(&line, &tmp_int);
2149 if (err < 0)
2150 {
2151 goto exit;
2152 }
2153 //LOG("req_net_sel_mode_get() 6");
2154 //sleep(1);
2155 err = at_tok_nextstr(&line, &tmp_ptr);
2156 if (err < 0)
2157 {
2158 goto exit;
2159 }
2160 // memcpy(net->plmn, tmp_ptr, strlen(tmp_ptr));
2161 net->plmn = (uint32)atoi(tmp_ptr);
2162 //LOG("req_net_sel_mode_get() 7");
2163 //sleep(1);
2164 err = at_tok_nextint(&line, &tmp_int);
2165 if (err < 0)
2166 {
2167 goto exit;
2168 }
2169 net->net_type = (uint8)tmp_int;
2170
2171 net->net_state = (uint8)MBTK_NET_AVIL_STATE_CURRENT;
2172
2173exit:
2174 //LOG("req_net_sel_mode_get() 8");
2175 //sleep(1);
2176 at_response_free(response);
2177 return err;
2178}
2179
2180/*
2181AT+COPS=0
2182or
2183AT+COPS=1,2,"46000",7
2184
2185OK
2186
2187*/
2188static int req_net_sel_mode_set(mbtk_net_info_t* net, int *cme_err)
2189{
2190 ATResponse *response = NULL;
2191 char cmd[50] = {0};
2192 char* cmp_ptr = cmd;
2193 if(net == NULL) {
2194 cmp_ptr += sprintf(cmp_ptr, "AT+COPS=0");
2195 } else {
2196 if(net->net_sel_mode == 0) {
2197 cmp_ptr += sprintf(cmp_ptr, "AT+COPS=0");
2198 } else if(net->net_type == 0xFF) {
2199 cmp_ptr += sprintf(cmp_ptr, "AT+COPS=1,2,\"%d\"",net->plmn);
2200 } else {
2201 cmp_ptr += sprintf(cmp_ptr, "AT+COPS=1,2,\"%d\",%d",net->plmn, net->net_type);
2202 }
2203 }
2204
2205 int err = at_send_command(cmd, &response);
2206
2207 if (err < 0 || response->success == 0) {
2208 *cme_err = at_get_cme_error(response);
2209 goto exit;
2210 }
2211
2212exit:
2213 at_response_free(response);
2214 return err;
2215}
2216
2217/*
2218AT+EEMOPT=1
2219OK
2220
2221// LTE
2222AT+EEMGINFO?
2223// <mcc>, <length of mnc>, <mnc>, <tac>, <PCI>, <dlEuarfcn>, < ulEuarfcn >, <band>, <dlBandwidth>,
2224// <rsrp>,<rsrq>, <sinr>,
2225// errcModeState,emmState,serviceState,IsSingleEmmRejectCause,EMMRejectCause,mmeGroupId,mmeCode,mTmsi,
2226// cellId,subFrameAssignType,specialSubframePatterns,transMode
2227// mainRsrp,diversityRsrp,mainRsrq,diversityRsrq,rssi,cqi,pathLoss,tb0DlTpt,tb1DlTpt,tb0DlPeakTpt,tb1DlPeakTpt,tb0UlPeakTpt,
2228// tb1UlPeakTpt,dlThroughPut,dlPeakThroughPut,averDlPRB,averCQITb0,averCQITb1,rankIndex,grantTotal,ulThroughPut,ulPeakThroughPut,currPuschTxPower,averUlPRB,
2229// dlBer, ulBer,
2230// diversitySinr, diversityRssi
2231+EEMLTESVC: 1120, 2, 0, 33584, 430, 40936, 40936, 41, 20,
22320, 0, 0,
22331, 10, 0, 1, 0, 1059, 78, 3959566565,
2234105149248, 2, 7, 7,
22350, 0, 0, 0, 0, 0, 0, 1190919, 0, 0, 0, 16779777,
22360, 5112867, 3959566565, 2, 0, 0, 0, 0, 0, 0, 0, 0,
22370, 0,
22387, 44
2239
2240// index,phyCellId,euArfcn,rsrp,rsrq
2241+EEMLTEINTER: 0, 65535, 38950, 0, 0
2242
2243+EEMLTEINTER: 1, 0, 0, 0, 0
2244
2245+EEMLTEINTER: 2, 0, 4294967295, 255, 255
2246
2247+EEMLTEINTER: 3, 65535, 1300, 0, 0
2248
2249+EEMLTEINTER: 4, 0, 0, 0, 0
2250
2251+EEMLTEINTER: 5, 0, 4294967295, 247, 0
2252
2253+EEMLTEINTER: 6, 197, 41332, 24, 9
2254
2255+EEMLTEINTER: 7, 0, 0, 0, 0
2256
2257+EEMLTEINTER: 8, 0, 0, 0, 0
2258
2259+EEMLTEINTRA: 0, 429, 40936, 56, 12
2260
2261+EEMLTEINTERRAT: 0,0
2262
2263+EEMLTEINTERRAT: 1,0
2264
2265+EEMGINFO: 3, 2 // <state>:
2266 // 0: ME in Idle mode
2267 // 1: ME in Dedicated mode
2268 // 2: ME in PS PTM mode
2269 // 3: invalid state
2270 // <nw_type>:
2271 // 0: GSM 1: UMTS 2: LTE
2272
2273OK
2274
2275// WCDMA
2276AT+EEMGINFO?
2277// Mode, sCMeasPresent, sCParamPresent, ueOpStatusPresent,
2278
2279// if sCMeasPresent == 1
2280// cpichRSCP, utraRssi, cpichEcN0, sQual, sRxLev, txPower,
2281// endif
2282
2283// if sCParamPresent == 1
2284// rac, nom, mcc, mnc_len, mnc, lac, ci,
2285// uraId, psc, arfcn, t3212, t3312, hcsUsed, attDetAllowed,
2286// csDrxCycleLen, psDrxCycleLen, utranDrxCycleLen, HSDPASupport, HSUPASupport,
2287// endif
2288
2289// if ueOpStatusPresent == 1
2290// rrcState, numLinks, srncId, sRnti,
2291// algPresent, cipherAlg, cipherOn, algPresent, cipherAlg, cipherOn,
2292// HSDPAActive, HSUPAActive, MccLastRegisteredNetwork, MncLastRegisteredNetwork, TMSI, PTMSI, IsSingleMmRejectCause, IsSingleGmmRejectCause,
2293// MMRejectCause, GMMRejectCause, mmState, gmmState, gprsReadyState, readyTimerValueInSecs, NumActivePDPContext, ULThroughput, DLThroughput,
2294// serviceStatus, pmmState, LAU_status, LAU_count, RAU_status, RAU_count
2295// endif
2296//
2297+EEMUMTSSVC: 3, 1, 1, 1,
2298-80, 27, -6, -18, -115, -32768,
22991, 1, 1120, 2, 1, 61697, 168432821,
230015, 24, 10763, 0, 0, 0, 0,
2301128, 128, 65535, 0, 0,
23022, 255, 65535, 4294967295,
23030, 0, 0, 0, 0, 0,
23040, 0, 0, 0, 0, 0, 1, 1,
230528672, 28672, 0, 0, 0, 0, 0, 0, 0,
23060, 0, 0, 0, 0, 0
2307
2308// index, cpichRSCP, utraRssi, cpichEcN0, sQual, sRxLev ,mcc, mnc, lac, ci, arfcn, psc
2309+EEMUMTSINTRA: 0, -32768, -1, -32768, -18, -115, 0, 0, 65534, 1, 10763, 32
2310
2311+EEMUMTSINTRA: 1, -1, -32768, -18, -115, 0, 0, 65534, 2, 10763, 40, 32768
2312
2313+EEMUMTSINTRA: 2, -32768, -18, -115, 0, 0, 65534, 3, 10763, 278, 32768, 65535
2314
2315+EEMUMTSINTRA: 3, -18, -115, 0, 0, -2, 4, 10763, 28, 32768, 65535, 32768
2316
2317+EEMUMTSINTRA: 4, -115, 0, 0, -2, 5, 10763, 270, 32768, 65535, 32768, 65518
2318
2319+EEMUMTSINTRA: 5, 0, 0, -2, 6, 10763, 286, 32768, 65535, 32768, 65518, 65421
2320
2321+EEMUMTSINTRA: 6, 0, -2, 7, 10763, 80, 32768, 65535, 32768, 65518, 65421, 0
2322
2323+EEMUMTSINTRA: 7, -2, 8, 10763, 206, -32768, 65535, 32768, 65518, 65421, 0, 0
2324
2325+EEMUMTSINTRA: 8, 9, 10763, 11, -32768, -1, 32768, 65518, 65421, 0, 0, 65534
2326
2327+EEMUMTSINTRA: 9, 10763, 19, -32768, -1, -32768, 65518, 65421, 0, 0, 65534, 11
2328
2329+EEMUMTSINTRA: 10, 232, -32768, -1, -32768, -18, 65421, 0, 0, 65534, 12, 10763
2330
2331+EEMUMTSINTRA: 11, -32768, -1, -32768, -18, -115, 0, 0, 65534, 13, 10763, 66
2332
2333+EEMUMTSINTRA: 12, -1, -32768, -18, -115, 0, 0, 65534, 14, 10763, 216, 32768
2334
2335+EEMUMTSINTRA: 13, -32768, -18, -115, 0, 0, 65534, 15, 10763, 183, 32768, 65535
2336
2337+EEMUMTSINTRA: 14, -18, -115, 0, 0, -2, 16, 10763, 165, 32768, 65535, 32768
2338
2339+EEMUMTSINTRA: 15, -115, 0, 0, -2, 17, 10763, 151, 32768, 65535, 32768, 65518
2340
2341+EEMUMTSINTRA: 16, 0, 0, -2, 18, 10763, 43, 32768, 65535, 32768, 65518, 65421
2342
2343+EEMUMTSINTRA: 17, 0, -2, 19, 10763, 72, 32768, 65535, 32768, 65518, 65421, 0
2344
2345+EEMUMTSINTRA: 18, -2, 20, 10763, 157, -32768, 65535, 32768, 65518, 65421, 0, 0
2346
2347+EEMUMTSINTRA: 19, 21, 10763, 165, -32768, -1, 32768, 65518, 65421, 0, 0, 65534
2348
2349+EEMUMTSINTRA: 20, 10763, 301, -32768, -1, -32768, 65518, 65421, 0, 0, 65534, 23
2350
2351+EEMUMTSINTRA: 21, 23, -32768, -1, -32768, -18, 65421, 0, 0, 65534, 24, 10763
2352
2353+EEMUMTSINTRA: 22, -32768, -1, -32768, -18, -115, 0, 0, 65534, 25, 10763, 0
2354
2355+EEMUMTSINTRA: 23, -1, -32768, -18, -115, 0, 0, 65534, 26, 10763, 167, 32768
2356
2357+EEMUMTSINTRA: 24, -32768, -18, -115, 0, 0, 65534, 27, 10763, 34, 32768, 65535
2358
2359+EEMUMTSINTRA: 25, -18, -115, 0, 0, -2, 28, 10763, 313, 32768, 65535, 32768
2360
2361+EEMUMTSINTRA: 26, -115, 0, 0, -2, 29, 10763, 152, 32768, 65535, 32768, 65518
2362
2363+EEMUMTSINTRA: 27, 0, 0, -2, 30, 10763, 239, 0, 0, 0, 0, 0
2364
2365+EEMUMTSINTRA: 28, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2366
2367+EEMUMTSINTRA: 29, 0, 0, 0, 0, -115, 0, 0, 65534, 30, 10763, 239
2368
2369// index,gsmRssi,rxLev,C1,C2,mcc,mnc,lac,ci,arfcn,bsic
2370+EEMUMTSINTERRAT: 0, -32768, -107, -1, -1, 0, 0, 65534, 0, 117, 36
2371
2372+EEMUMTSINTERRAT: 1, -107, -1, -1, 0, 0, 65534, 1, 72, 49, 0
2373
2374+EEMUMTSINTERRAT: 2, -1, -1, 0, 0, 65534, 2, 119, 15, 32768, 149
2375
2376+EEMUMTSINTERRAT: 3, -1, 0, 0, -2, 3, 121, 23, 0, 0, 0
2377
2378+EEMGINFO: 3, 1
2379
2380OK
2381
2382
2383// GSM
2384AT+EEMGINFO?
2385+EEMGINFOBASIC: 2
2386
2387// mcc, mnc_len, mnc, lac, ci, nom, nco,
2388// bsic, C1, C2, TA, TxPwr,
2389// RxSig, RxSigFull, RxSigSub, RxQualFull, RxQualSub,
2390// ARFCB_tch, hopping_chnl, chnl_type, TS, PacketIdle, rac, arfcn,
2391// bs_pa_mfrms, C31, C32, t3212, t3312, pbcch_support, EDGE_support,
2392// ncc_permitted, rl_timeout, ho_count, ho_succ, chnl_access_count, chnl_access_succ_count,
2393// gsmBand,channelMode
2394+EEMGINFOSVC: 1120, 2, 0, 32784, 24741, 2, 0,
239563, 36, 146, 1, 7,
239646, 42, 42, 7, 0,
239753, 0, 8, 0, 1, 6, 53,
23982, 0, 146, 42, 54, 0, 1,
23991, 32, 0, 0, 0, 0,
24000, 0
2401
2402// PS_attached, attach_type, service_type, tx_power, c_value,
2403// ul_ts, dl_ts, ul_cs, dl_cs, ul_modulation, dl_modulation,
2404// gmsk_cv_bep, 8psk_cv_bep, gmsk_mean_bep, 8psk_mean_bep, EDGE_bep_period, single_gmm_rej_cause
2405// pdp_active_num, mac_mode, network_control, network_mode, EDGE_slq_measurement_mode, edge_status
2406+EEMGINFOPS: 1, 255, 0, 0, 0,
24070, 0, 268435501, 1, 0, 0,
24084, 0, 96, 0, 0, 0,
24090, 0, 0, 65535, 0, 13350
2410
2411+EEMGINFO: 0, 0
2412
2413OK
2414
2415*/
2416static int req_cell_info_get(int *cme_err)
2417{
2418 ATResponse *response = NULL;
2419 int tmp_int;
2420 int buff_size = 0;
2421 // AT+EEMOPT=1 in the first.
2422 int err = at_send_command("AT+EEMOPT=1", &response);
2423 if (err < 0 || response->success == 0){
2424 *cme_err = at_get_cme_error(response);
2425 goto exit;
2426 }
2427
2428 // Reset buffer in the first.
2429 memset(&cell_info, 0xFF, sizeof(mbtK_cell_pack_info_t));
2430 cell_info.running = true;
2431 cell_info.cell_num = 0;
2432
2433 err = at_send_command_singleline("AT+EEMGINFO?", "+EEMGINFO:", &response);
2434 if (err < 0 || response->success == 0 || !response->p_intermediates){
2435 *cme_err = at_get_cme_error(response);
2436 goto exit;
2437 }
2438
2439 // Now, cell infomation has get from URC message.
2440
2441 char *line = response->p_intermediates->line;
2442 err = at_tok_start(&line);
2443 if (err < 0)
2444 {
2445 goto exit;
2446 }
2447 err = at_tok_nextint(&line, &tmp_int);
2448 if (err < 0)
2449 {
2450 goto exit;
2451 }
2452 err = at_tok_nextint(&line, &tmp_int);
2453 if (err < 0)
2454 {
2455 goto exit;
2456 }
2457
2458 cell_info.type = (uint8)tmp_int;
2459 cell_info.running = false;
2460
2461#if 0
2462 while(lines_ptr)
2463 {
2464 // LTE
2465 if(strStartsWith(line, "+EEMLTESVC:")) // LTE Server Cell
2466 {
2467
2468 }
2469 else if(strStartsWith(line, "+EEMLTEINTER:")) // LTE ???§³??
2470 {
2471
2472 }
2473 else if(strStartsWith(line, "+EEMLTEINTRA:")) // LTE ??§³??
2474 {
2475
2476 }
2477 else if(strStartsWith(line, "+EEMLTEINTERRAT:")) // LTE RAT§³?????
2478 {
2479
2480 }
2481 else if(strStartsWith(line, "+EEMGINFO:")) // <state>: 0: ME in Idle mode 1: ME in Dedicated mode 2: ME in PS PTM mode 3: invalid state
2482 // <nw_type>: 0: GSM 1: UMTS 2: LTE
2483 {
2484
2485 }
2486 // WCDMA
2487 else if(strStartsWith(line, "+EEMUMTSSVC:")) // WCDMA Server Cell
2488 {
2489
2490 }
2491 else if(strStartsWith(line, "+EEMUMTSINTRA:")) // WCDMA???§³??
2492 {
2493
2494 }
2495 else if(strStartsWith(line, "+EEMUMTSINTERRAT:")) // WCDMA RAT§³?????
2496 {
2497
2498 }
2499 // GSM
2500 else if(strStartsWith(line, "+EEMGINFOBASIC:")) // Basic information in GSM
2501 // 0: ME in Idle mode 1: ME in Dedicated mode 2: ME in PS PTM mode
2502 {
2503
2504 }
2505 else if(strStartsWith(line, "+EEMGINFOSVC:")) // GSM Server Cell
2506 {
2507
2508 }
2509 else if(strStartsWith(line, "+EEMGINFOPS:")) // PS???
2510 {
2511
2512 }
2513
2514
2515 lines_ptr = lines_ptr->p_next;
2516 }
2517#endif
2518
2519exit:
2520 at_response_free(response);
2521 return buff_size;
2522}
2523
2524static int req_cell_info_set(const char *cmgl, char *reg, int len, int *cme_err)
2525{
2526 printf("req_cmgl_set(2)-----------------start\n");
2527 printf("cmgl:%s\n", cmgl);
2528 ATResponse *response = NULL;
2529 char cmd[30] = {0};
2530 char data[218] = {0};
2531 int err = 0;
2532
2533 memcpy(data, cmgl, len);
2534
2535 sprintf(cmd, "at*cell=%s", data);
2536 printf("cmd:%s\n", cmd);
2537
2538 if(strlen(cmd) > 0)
2539 {
2540 err = at_send_command_multiline(cmd, "", &response);
2541 if (err < 0 || response->success == 0 || !response->p_intermediates){
2542 *cme_err = at_get_cme_error(response);
2543 // printf("at_send_command_multiline() is err-----------------\n");
2544 goto exit;
2545 }
2546
2547 ATLine* lines_ptr = response->p_intermediates;
2548 char *line = NULL;
2549 int reg_len = 0;
2550 bool flag = false;
2551 while(lines_ptr)
2552 {
2553 line = lines_ptr->line;
2554 if(line ==NULL)
2555 {
2556 printf("line is null----------------------\n");
2557 }
2558 printf("-----line:%s\n", line);
2559
2560 lines_ptr = lines_ptr->p_next;
2561 }
2562 }
2563 err = 0;
2564 memcpy(reg, "req_cell_info_set succss", strlen("req_cell_info_set succss"));
2565exit:
2566 at_response_free(response);
2567 printf("req_cell_info_set()-----------------end\n");
2568 return err;
2569}
2570
2571
2572
2573/*
2574AT+CSQ
2575+CSQ: 31,99
2576
2577OK
2578
2579AT+CESQ
2580+CESQ: 60,99,255,255,20,61
2581
2582OK
2583
2584AT+COPS?
2585+COPS: 0,2,"46001",7
2586
2587OK
2588
2589*/
2590static int req_net_signal_get(mbtk_signal_info_t *signal, int *cme_err)
2591{
2592 ATResponse *response = NULL;
2593 int tmp_int;
2594 char *tmp_ptr = NULL;
2595 // AT+EEMOPT=1 in the first.
2596 int err = at_send_command_singleline("AT+CSQ", "+CSQ:", &response);
2597 if (err < 0 || response->success == 0 || !response->p_intermediates){
2598 if(cme_err != NULL)
2599 *cme_err = at_get_cme_error(response);
2600 err = -1;
2601 goto exit;
2602 }
2603
2604 char *line = response->p_intermediates->line;
2605 err = at_tok_start(&line);
2606 if (err < 0)
2607 {
2608 goto exit;
2609 }
2610 err = at_tok_nextint(&line, &tmp_int);
2611 if (err < 0)
2612 {
2613 goto exit;
2614 }
2615 signal->rssi = (uint8)tmp_int;
2616 at_response_free(response);
2617
2618 err = at_send_command_singleline("AT+CESQ", "+CESQ:", &response);
2619 if (err < 0 || response->success == 0 || !response->p_intermediates){
2620 if(cme_err != NULL)
2621 *cme_err = at_get_cme_error(response);
2622 err = -1;
2623 goto exit;
2624 }
2625
2626 line = response->p_intermediates->line;
2627 err = at_tok_start(&line);
2628 if (err < 0)
2629 {
2630 goto exit;
2631 }
2632 err = at_tok_nextint(&line, &tmp_int);
2633 if (err < 0)
2634 {
2635 goto exit;
2636 }
2637 signal->rxlev = (uint8)tmp_int;
2638
2639 err = at_tok_nextint(&line, &tmp_int);
2640 if (err < 0)
2641 {
2642 goto exit;
2643 }
2644 signal->ber = (uint8)tmp_int;
2645
2646 err = at_tok_nextint(&line, &tmp_int);
2647 if (err < 0)
2648 {
2649 goto exit;
2650 }
2651 signal->rscp = (uint8)tmp_int;
2652
2653 err = at_tok_nextint(&line, &tmp_int);
2654 if (err < 0)
2655 {
2656 goto exit;
2657 }
2658 signal->ecno = (uint8)tmp_int;
2659
2660 err = at_tok_nextint(&line, &tmp_int);
2661 if (err < 0)
2662 {
2663 goto exit;
2664 }
2665 signal->rsrq = (uint8)tmp_int;
2666
2667 err = at_tok_nextint(&line, &tmp_int);
2668 if (err < 0)
2669 {
2670 goto exit;
2671 }
2672 signal->rsrp = (uint8)tmp_int;
2673
2674 at_response_free(response);
2675 err = at_send_command_singleline("AT+COPS?", "+COPS:", &response);
2676 if (err < 0 || response->success == 0 || !response->p_intermediates){
2677 if(cme_err != NULL)
2678 *cme_err = at_get_cme_error(response);
2679 err = -1;
2680 goto exit;
2681 }
2682 line = response->p_intermediates->line;
2683 err = at_tok_start(&line);
2684 if (err < 0)
2685 {
2686 goto exit;
2687 }
2688 err = at_tok_nextint(&line, &tmp_int);
2689 if (err < 0)
2690 {
2691 goto exit;
2692 }
2693 if(!at_tok_hasmore(&line)) {
2694 goto exit;
2695 }
2696 err = at_tok_nextint(&line, &tmp_int);
2697 if (err < 0)
2698 {
2699 goto exit;
2700 }
2701 err = at_tok_nextstr(&line, &tmp_ptr);
2702 if (err < 0)
2703 {
2704 goto exit;
2705 }
2706 err = at_tok_nextint(&line, &tmp_int);
2707 if (err < 0)
2708 {
2709 goto exit;
2710 }
2711 signal->type = (uint8)tmp_int;
2712 net_info.net_type = signal->type;
2713
2714exit:
2715 at_response_free(response);
2716 return err;
2717}
2718
2719/*
2720AT+CREG=3
2721OK
2722
2723AT+CREG?
2724+CREG: 3,1,"8330","06447340",7
2725
2726OK
2727
2728AT+CREG?
2729+CREG: 3,0
2730
2731OK
2732
2733AT+CEREG?
2734+CEREG: 3,1,"8330","06447340",7
2735
2736OK
2737
2738
2739AT+CIREG?
2740+CIREG: 2,1,15
2741
2742OK
2743
2744AT+CIREG?
2745+CIREG: 0
2746
2747OK
2748
2749
2750*/
2751static int req_net_reg_get(mbtk_net_reg_info_t *reg, int *cme_err)
2752{
2753 ATResponse *response = NULL;
2754 int tmp_int;
2755 char *tmp_str = NULL;
2756 int err = at_send_command("AT+CREG=3", &response);
2757 if (err < 0 || response->success == 0){
2758 *cme_err = at_get_cme_error(response);
2759 goto exit;
2760 }
2761 at_response_free(response);
2762
2763 err = at_send_command_multiline("AT+CREG?", "+CREG:", &response);
2764 if (err < 0 || response->success == 0 || !response->p_intermediates){
2765 *cme_err = at_get_cme_error(response);
2766 goto exit;
2767 }
2768
2769 char *line = response->p_intermediates->line;
2770 err = at_tok_start(&line);
2771 if (err < 0)
2772 {
2773 goto exit;
2774 }
2775 err = at_tok_nextint(&line, &tmp_int); // n
2776 if (err < 0)
2777 {
2778 goto exit;
2779 }
2780 err = at_tok_nextint(&line, &tmp_int);// stat
2781 if (err < 0)
2782 {
2783 goto exit;
2784 }
2785 reg->call_state = (uint8)tmp_int;
2786
2787 if(at_tok_hasmore(&line)) {
2788 err = at_tok_nextstr(&line, &tmp_str); // lac
2789 if (err < 0)
2790 {
2791 goto exit;
2792 }
2793 reg->lac = strtol(tmp_str, NULL, 16);
2794
2795 err = at_tok_nextstr(&line, &tmp_str); // ci
2796 if (err < 0)
2797 {
2798 goto exit;
2799 }
2800 reg->ci = strtol(tmp_str, NULL, 16);
2801
2802 err = at_tok_nextint(&line, &tmp_int);// AcT
2803 if (err < 0)
2804 {
2805 goto exit;
2806 }
2807 reg->type = (uint8)tmp_int;
2808 }
2809 at_response_free(response);
2810
2811 err = at_send_command_multiline("AT+CEREG?", "+CEREG:", &response);
2812 if (err < 0 || response->success == 0 || !response->p_intermediates){
2813 *cme_err = at_get_cme_error(response);
2814 goto exit;
2815 }
2816
2817 line = response->p_intermediates->line;
2818 err = at_tok_start(&line);
2819 if (err < 0)
2820 {
2821 goto exit;
2822 }
2823 err = at_tok_nextint(&line, &tmp_int); // n
2824 if (err < 0)
2825 {
2826 goto exit;
2827 }
2828 err = at_tok_nextint(&line, &tmp_int);// stat
2829 if (err < 0)
2830 {
2831 goto exit;
2832 }
2833 reg->data_state = (uint8)tmp_int;
2834
2835 if(reg->lac == 0 && at_tok_hasmore(&line)) {
2836 err = at_tok_nextstr(&line, &tmp_str); // lac
2837 if (err < 0)
2838 {
2839 goto exit;
2840 }
2841 reg->lac = strtol(tmp_str, NULL, 16);
2842
2843 err = at_tok_nextstr(&line, &tmp_str); // ci
2844 if (err < 0)
2845 {
2846 goto exit;
2847 }
2848 reg->ci = strtol(tmp_str, NULL, 16);
2849
2850 err = at_tok_nextint(&line, &tmp_int);// AcT
2851 if (err < 0)
2852 {
2853 goto exit;
2854 }
2855 reg->type = (uint8)tmp_int;
2856 }
2857 at_response_free(response);
2858
2859 err = at_send_command_multiline("AT+CIREG?", "+CIREG:", &response);
2860 if (err < 0 || response->success == 0 || !response->p_intermediates){
2861 reg->ims_state = (uint8)0;
2862 err = 0;
2863 goto exit;
2864 }
2865 line = response->p_intermediates->line;
2866 err = at_tok_start(&line);
2867 if (err < 0)
2868 {
2869 goto exit;
2870 }
2871 err = at_tok_nextint(&line, &tmp_int); // n/stat
2872 if (err < 0)
2873 {
2874 goto exit;
2875 }
2876 if(at_tok_hasmore(&line)) {
2877 err = at_tok_nextint(&line, &tmp_int);// stat
2878 if (err < 0)
2879 {
2880 goto exit;
2881 }
2882 reg->ims_state = (uint8)tmp_int;
2883 } else {
2884 reg->ims_state = (uint8)tmp_int;
2885 }
2886
2887exit:
2888 at_response_free(response);
2889 return err;
2890}
2891
2892/*
2893AT+CGDCONT?
2894+CGDCONT: 1,"IPV4V6","cmnet.MNC000.MCC460.GPRS","10.131.67.146 254.128.0.0.0.0.0.0.0.1.0.2.200.2.158.0",0,0,,,,
2895
2896+CGDCONT: 8,"IPV4V6","IMS","254.128.0.0.0.0.0.0.0.1.0.2.200.2.160.160",0,0,0,2,1,1
2897
2898OK
2899
2900
2901*/
wangyouqianged88c722023-11-22 16:33:43 +08002902#ifdef MBTK_AF_SUPPORT
2903mbtk_ip_type_enum default_iptype = MBTK_IP_TYPE_IPV4V6;
2904#endif
2905
liubin281ac462023-07-19 14:22:54 +08002906static int req_apn_get(void *data, int *data_len, int *cme_err)
2907{
2908 ATResponse *response = NULL;
2909 int err = at_send_command_multiline("AT+CGDCONT?", "+CGDCONT:", &response);
2910
2911 if (err < 0 || response->success == 0 || !response->p_intermediates){
2912 *cme_err = at_get_cme_error(response);
2913 goto exit;
2914 }
2915
2916 ATLine* lines_ptr = response->p_intermediates;
2917 char *line = NULL;
2918 int tmp_int;
2919 char *tmp_str = NULL;
2920 /*
2921 <apn_num[1]><cid[1]><ip_type[1]><apn_len[2]><apn><user_len[2]><user><pass_len[2]><pass><auth_len[2]><auth>...
2922 <cid[1]><ip_type[1]><apn_len[2]><apn><user_len[2]><user><pass_len[2]><pass><auth_len[2]><auth>
2923 */
2924 uint8* apn_num = (uint8*)data;
2925 uint8* data_ptr = (uint8*)(data + sizeof(uint8)); // Jump apn_num[1]
2926 mbtk_apn_info_t apn;
2927 while(lines_ptr)
2928 {
2929 line = lines_ptr->line;
2930 err = at_tok_start(&line);
2931 if (err < 0)
2932 {
2933 goto exit;
2934 }
2935
2936 err = at_tok_nextint(&line, &tmp_int); // cid
2937 if (err < 0)
2938 {
2939 goto exit;
2940 }
2941 // Only get CID 1-7
2942 if(tmp_int >= MBTK_APN_CID_MIN && tmp_int <= MBTK_APN_CID_MAX) {
2943 memset(&apn, 0x0, sizeof(mbtk_apn_info_t));
2944 apn.cid = tmp_int;
2945 *data_ptr++ = (uint8)tmp_int; // cid
2946
2947 err = at_tok_nextstr(&line, &tmp_str);// ip type
2948 if (err < 0)
2949 {
2950 goto exit;
2951 }
2952 if(!strcasecmp(tmp_str, "IP")) {
2953 *data_ptr++ = (uint8)MBTK_IP_TYPE_IP;
2954 apn.ip_type = MBTK_IP_TYPE_IP;
2955 } else if(!strcasecmp(tmp_str, "IPV6")) {
2956 *data_ptr++ = (uint8)MBTK_IP_TYPE_IPV6;
2957 apn.ip_type = MBTK_IP_TYPE_IPV6;
2958 } else if(!strcasecmp(tmp_str, "IPV4V6")) {
2959 *data_ptr++ = (uint8)MBTK_IP_TYPE_IPV4V6;
2960 apn.ip_type = MBTK_IP_TYPE_IPV4V6;
2961 } else {
2962 *data_ptr++ = (uint8)MBTK_IP_TYPE_PPP;
2963 apn.ip_type = MBTK_IP_TYPE_PPP;
2964 }
2965
wangyouqianged88c722023-11-22 16:33:43 +08002966#ifdef MBTK_AF_SUPPORT
2967 if(apn.cid == 1)
2968 {
2969 default_iptype = apn.ip_type;
2970 }
2971#endif
liubin281ac462023-07-19 14:22:54 +08002972 err = at_tok_nextstr(&line, &tmp_str); // apn
2973 if (err < 0)
2974 {
2975 goto exit;
2976 }
2977 if(str_empty(tmp_str)) {
2978 uint16_2_byte((uint16)0, data_ptr, false);
2979 data_ptr += sizeof(uint16);
2980 } else {
2981 uint16_2_byte((uint16)strlen(tmp_str), data_ptr, false);
2982 data_ptr += sizeof(uint16);
2983 memcpy(data_ptr, tmp_str, strlen(tmp_str));
2984 data_ptr += strlen(tmp_str);
2985 memcpy(apn.apn, tmp_str, strlen(tmp_str));
2986 }
2987
2988 if(apn_user_pass_set_by_cid(apn.cid, &apn)) {
2989 // user
2990 uint16_2_byte((uint16)0, data_ptr, false);
2991 data_ptr += sizeof(uint16);
2992
2993 // pass
2994 uint16_2_byte((uint16)0, data_ptr, false);
2995 data_ptr += sizeof(uint16);
2996
2997 // auth
2998 uint16_2_byte((uint16)0, data_ptr, false);
2999 data_ptr += sizeof(uint16);
3000 } else {
3001 // user
3002 if(str_empty(apn.user)) {
3003 uint16_2_byte((uint16)0, data_ptr, false);
3004 data_ptr += sizeof(uint16);
3005 } else {
3006 uint16_2_byte((uint16)strlen(apn.user), data_ptr, false);
3007 data_ptr += sizeof(uint16);
3008 memcpy(data_ptr, apn.user, strlen(apn.user));
3009 data_ptr += strlen(apn.user);
3010 }
3011
3012 // pass
3013 if(str_empty(apn.pass)) {
3014 uint16_2_byte((uint16)0, data_ptr, false);
3015 data_ptr += sizeof(uint16);
3016 } else {
3017 uint16_2_byte((uint16)strlen(apn.pass), data_ptr, false);
3018 data_ptr += sizeof(uint16);
3019 memcpy(data_ptr, apn.pass, strlen(apn.pass));
3020 data_ptr += strlen(apn.pass);
3021 }
3022
3023 // auth
3024 if(str_empty(apn.auth)) {
3025 uint16_2_byte((uint16)0, data_ptr, false);
3026 data_ptr += sizeof(uint16);
3027 } else {
3028 uint16_2_byte((uint16)strlen(apn.auth), data_ptr, false);
3029 data_ptr += sizeof(uint16);
3030 memcpy(data_ptr, apn.auth, strlen(apn.auth));
3031 data_ptr += strlen(apn.auth);
3032 }
3033 }
3034
3035 (*apn_num)++;
3036 }
3037
3038 lines_ptr = lines_ptr->p_next;
3039 }
3040
3041 *data_len = data_ptr - (uint8*)data;
3042
3043 goto exit;
3044exit:
3045 at_response_free(response);
3046 return err;
3047}
3048
3049#if 0
3050/*
3051LTE APN
3052AT+CFUN=4
3053AT*CGDFLT=1,IP,"private.vpdn",1,0,1,0,0,0,0,0,0,0,1,0,0,1,0,0,0,1
3054AT*CGDFAUTH=1,2,"noc@njcc.vpdn.js","123456"
3055AT+CFUN=1
3056AT+CEREG?
3057AT+CGDCONT?
3058
30592/3G APN
3060AT+CGREG?
3061AT+CGDCONT=6,IP,"private.vpdn"
3062AT*AUTHREQ=6,2,"noc@njcc.vpdn.js","123456"
3063AT+CGDATA="",6
3064AT+CGDCONT?
3065*/
3066static int req_apn_set_username(mbtk_apn_info_t *apn, int *cme_err)
3067{
3068 ATResponse *response = NULL;
3069 char cmd[400] = {0};
3070 int index = 0;
3071 int err = 0;
3072
3073 err = at_send_command_singleline("AT+COPS?", "+COPS:", &response);
3074 if (err < 0 || response->success == 0 || !response->p_intermediates){
3075 if(cme_err != NULL)
3076 *cme_err = at_get_cme_error(response);
3077 err = -1;
3078 goto apn_set;
3079 }
3080
3081 int tmp_int = 0;
3082 int state=0;
3083 char cmd_buf[64];
3084 char *line = response->p_intermediates->line;
3085 err = at_tok_start(&line);
3086 if (err < 0)
3087 {
3088 goto apn_set;
3089 }
3090 err = at_tok_nextint(&line, &tmp_int);
3091 if (err < 0)
3092 {
3093 goto apn_set;
3094 }
3095 err = at_tok_nextint(&line, &tmp_int);
3096 if (err < 0)
3097 {
3098 goto apn_set;
3099 }
3100 err = at_tok_nextstr(&line, &cmd_buf);
3101 if (err < 0)
3102 {
3103 goto apn_set;
3104 }
3105 err = at_tok_nextint(&line, &tmp_int);
3106 if (err < 0)
3107 {
3108 goto apn_set;
3109 }
3110 else
3111 state = tmp_int;
3112
3113apn_set:
3114 at_response_free(response);
3115 *cme_err = MBTK_INFO_ERR_CME_NON;
3116 //if(state == 7 && apn->cid == 1) //LTE && cid = 1
3117 if(0) //LTE && cid = 1
3118 {
3119 err = at_send_command("AT+CFUN=4", &response);
3120 if (err < 0 || response->success == 0){
3121 *cme_err = at_get_cme_error(response);
3122 goto exit;
3123 }
3124 at_response_free(response);
3125
3126 memset(cmd, 0, 400);
3127 index = 0;
3128 index += sprintf(cmd, "AT*CGDFLT=%d,", 1);
3129 switch(apn->ip_type) {
3130 case MBTK_IP_TYPE_IP: {
3131 index += sprintf(cmd + index,"\"IP\",");
3132 break;
3133 }
3134 case MBTK_IP_TYPE_IPV6: {
3135 index += sprintf(cmd + index,"\"IPV6\",");
3136 break;
3137 }
3138 case MBTK_IP_TYPE_IPV4V6: {
3139 index += sprintf(cmd + index,"\"IPV4V6\",");
3140 break;
3141 }
3142 default: {
3143 index += sprintf(cmd + index,"\"PPP\",");
3144 break;
3145 }
3146 }
3147
3148 index += sprintf(cmd + index,"\"%s\",1,0,1,0,0,0,0,0,0,0,1,0,0,1,0,0,0,1",apn->apn);
3149 err = at_send_command(cmd, &response);
3150 if (err < 0 || response->success == 0){
3151 *cme_err = at_get_cme_error(response);
3152 goto exit;
3153 }
3154 at_response_free(response);
3155
3156 memset(cmd, 0, 400);
3157 int cmd_auth=0;
3158 if(strstr(apn->auth,"NONE"))
3159 cmd_auth = 0;
3160 else if(strstr(apn->auth,"PAP"))
3161 cmd_auth = 1;
3162 else if(strstr(apn->auth,"CHAP"))
3163 cmd_auth = 2;
3164 else if(strstr(apn->auth,"PAP AND CHAP"))
3165 cmd_auth = 3;
3166 else
3167 goto exit;
3168
3169 sprintf(cmd,"AT*CGDFAUTH=1,%d,%s,%s",cmd_auth,apn->user,apn->pass);
3170
3171 err = at_send_command(cmd, &response);
3172 if (err < 0 || response->success == 0){
3173 *cme_err = at_get_cme_error(response);
3174 goto exit;
3175 }
3176 }
3177 else //2/3G
3178 {
3179 memset(cmd,0,400);
3180 index = 0;
3181 index += sprintf(cmd, "AT+CGDCONT=%d,", apn->cid);
3182 switch(apn->ip_type) {
3183 case MBTK_IP_TYPE_IP: {
3184 index += sprintf(cmd + index,"\"IP\",");
3185 break;
3186 }
3187 case MBTK_IP_TYPE_IPV6: {
3188 index += sprintf(cmd + index,"\"IPV6\",");
3189 break;
3190 }
3191 case MBTK_IP_TYPE_IPV4V6: {
3192 index += sprintf(cmd + index,"\"IPV4V6\",");
3193 break;
3194 }
3195 default: {
3196 index += sprintf(cmd + index,"\"PPP\",");
3197 break;
3198 }
3199 }
3200 index += sprintf(cmd + index, "\"%s\"", apn->apn);
3201
3202 err = at_send_command(cmd, &response);
3203 if (err < 0 || response->success == 0){
3204 *cme_err = at_get_cme_error(response);
3205 goto exit;
3206 }
3207 at_response_free(response);
3208
3209 memset(cmd,0,400);
3210 int cmd_auth=0;
3211 if(strstr(apn->auth,"NONE"))
3212 cmd_auth = 0;
3213 else if(strstr(apn->auth,"PAP"))
3214 cmd_auth = 1;
3215 else if(strstr(apn->auth,"CHAP"))
3216 cmd_auth = 2;
3217 else if(strstr(apn->auth,"PAP AND CHAP"))
3218 cmd_auth = 3;
3219 else
3220 goto exit;
3221
3222 sprintf(cmd, "AT*AUTHREQ=%d,%d,%s,%s",apn->cid,cmd_auth,apn->user,apn->pass);
3223 err = at_send_command(cmd, &response);
3224 if (err < 0 || response->success == 0){
3225 *cme_err = at_get_cme_error(response);
3226 goto exit;
3227 }
3228 }
3229
3230exit:
3231 at_response_free(response);
3232 return err;
3233}
3234#endif
3235
3236/*
3237AT+CGDCONT=1,"IPV4V6","cmnet"
3238OK
3239
3240AT*CGDFLT=1,"IPv4v6","reliance.grevpdn.zj",,,,,,,,,,,,,,,,,,1
3241OK
3242
3243*/
3244static int req_apn_set(mbtk_apn_info_t *apn, int *cme_err)
3245{
3246 ATResponse *response = NULL;
3247 char cmd[400] = {0};
3248 int index = 0;
3249 int err = 0;
3250
3251 index += sprintf(cmd, "AT+CGDCONT=%d,", apn->cid);
3252 switch(apn->ip_type) {
3253 case MBTK_IP_TYPE_IP: {
3254 index += sprintf(cmd + index,"\"IP\",");
3255 break;
3256 }
3257 case MBTK_IP_TYPE_IPV6: {
3258 index += sprintf(cmd + index,"\"IPV6\",");
3259 break;
3260 }
3261 case MBTK_IP_TYPE_IPV4V6: {
3262 index += sprintf(cmd + index,"\"IPV4V6\",");
3263 break;
3264 }
3265 default: {
3266 index += sprintf(cmd + index,"\"PPP\",");
3267 break;
3268 }
3269 }
3270 if(strlen(apn->apn) > 0) {
3271 index += sprintf(cmd + index,"\"%s\"", apn->apn);
3272 } else {
3273 LOGE("No set APN.");
3274 err = -1;
3275 goto exit;
3276 }
3277
3278 err = at_send_command(cmd, &response);
3279 if (err < 0 || response->success == 0){
3280 if(cme_err) {
3281 *cme_err = at_get_cme_error(response);
3282 }
3283 goto exit;
3284 }
3285
3286 if(!str_empty(apn->user) || !str_empty(apn->pass)) {
3287 at_response_free(response);
3288
3289 memset(cmd,0,400);
3290 int cmd_auth=0;
3291 if(strstr(apn->auth,"NONE"))
3292 cmd_auth = 0;
3293 else if(strstr(apn->auth,"PAP"))
3294 cmd_auth = 1;
3295 else if(strstr(apn->auth,"CHAP"))
3296 cmd_auth = 2;
3297#if 0
3298 else if(strstr(apn->auth,"PAP AND CHAP"))
3299 cmd_auth = 3;
3300#endif
3301 else
3302 goto exit;
3303
3304 sprintf(cmd, "AT*AUTHREQ=%d,%d,%s,%s",apn->cid,cmd_auth,apn->user,apn->pass);
3305 err = at_send_command(cmd, &response);
3306 if (err < 0 || response->success == 0){
3307 *cme_err = at_get_cme_error(response);
3308 goto exit;
3309 }
3310 }
3311
3312exit:
3313 at_response_free(response);
3314 return err;
3315}
3316
3317int wait_cgact_complete(int timeout)
3318{
3319 int count = timeout * 10; // timeout * 1000 / 100
3320 int i = 0;
3321
3322 while(cgact_wait.waitting && i < count) {
3323 i++;
3324 usleep(100000); // 100ms
3325 }
3326
3327 if(i == count) { // Timeout
3328 return -1;
3329 } else {
3330 return 0;
3331 }
3332}
3333
3334/*
3335AT+CGDATA="",6
3336CONNECT
3337
3338OK
3339
3340AT+CFUN=1
3341
3342OK
3343
3344*/
3345static int req_data_call_user_start(int cid, int *cme_err)
3346{
3347 ATResponse *response = NULL;
3348 char cmd[400] = {0};
3349 int index = 0;
3350 int err = 0;
3351
3352 err = at_send_command_singleline("AT+COPS?", "+COPS:", &response);
3353 if (err < 0 || response->success == 0 || !response->p_intermediates){
3354 if(cme_err != NULL)
3355 *cme_err = at_get_cme_error(response);
3356 err = -1;
3357 goto exit;
3358 }
3359
3360 int tmp_int;
3361 char cmd_buf[64];
3362 char *line = response->p_intermediates->line;
3363 err = at_tok_start(&line);
3364 if (err < 0)
3365 {
3366 goto exit;
3367 }
3368 err = at_tok_nextint(&line, &tmp_int);
3369 if (err < 0)
3370 {
3371 goto exit;
3372 }
3373 err = at_tok_nextint(&line, &tmp_int);
3374 if (err < 0)
3375 {
3376 goto exit;
3377 }
3378 err = at_tok_nextstr(&line, &cmd_buf);
3379 if (err < 0)
3380 {
3381 goto exit;
3382 }
3383 err = at_tok_nextint(&line, &tmp_int);
3384 if (err < 0)
3385 {
3386 goto exit;
3387 }
3388 at_response_free(response);
3389
3390 if(tmp_int == 7 && cid == 1) //LTE && cid = 1
3391 {
3392 ATResponse *response = NULL;
3393 char cmd[400] = {0};
3394 int err = 0;
3395
3396 err = at_send_command("AT+CFUN=1", &response);
3397 if (err < 0 || response->success == 0){
3398 if(cme_err) {
3399 *cme_err = at_get_cme_error(response);
3400 }
3401 goto exit;
3402 }
3403 }
3404 else
3405 {
3406 ATResponse *response = NULL;
3407 char cmd[400] = {0};
3408 int err = 0;
3409 sprintf(cmd, "AT+CGDATA=\"\",%d", cid);
3410 err = at_send_command(cmd, &response);
3411 if (err < 0 || response->success == 0){
3412 if(cme_err) {
3413 *cme_err = at_get_cme_error(response);
3414 }
3415 goto exit;
3416 }
3417 }
3418
3419exit:
3420 at_response_free(response);
3421 return err;
3422}
3423
3424/*
3425AT+CGACT?
3426+CGACT: 1,1
3427+CGACT: 8,1
3428OK
3429
3430AT+CGACT=1,<cid>
3431OK
3432
3433*/
3434static int req_data_call_start(int cid, int *cme_err)
3435{
3436 ATResponse *response = NULL;
3437 char cmd[400] = {0};
3438 int err = 0;
3439#if 0
3440 err = at_send_command_multiline("AT+CGACT?", "+CGACT:", &response);
3441 if (err < 0 || response->success == 0 || !response->p_intermediates){
3442 *cme_err = at_get_cme_error(response);
3443 goto exit;
3444 }
3445 ATLine* lines_ptr = response->p_intermediates;
3446 char *line = NULL;
3447 int tmp_int;
3448 while(lines_ptr)
3449 {
3450 line = lines_ptr->line;
3451 err = at_tok_start(&line);
3452 if (err < 0)
3453 {
3454 goto exit;
3455 }
3456
3457 err = at_tok_nextint(&line, &tmp_int); // cid
3458 if (err < 0)
3459 {
3460 goto exit;
3461 }
3462 if(tmp_int == cid) { // Found cid
3463 err = at_tok_nextint(&line, &tmp_int); // cid
3464 if (err < 0)
3465 {
3466 goto exit;
3467 }
3468 if(tmp_int == 1) { // This cid has active.
3469 goto net_config;
3470 } else {
3471 goto cid_active;
3472 }
3473 break;
3474 }
3475
3476 lines_ptr = lines_ptr->p_next;
3477 }
3478
3479 if(lines_ptr == NULL) { // No found this cid.
3480 LOGE("No found cid : %d", cid);
3481 goto exit;
3482 }
3483 at_response_free(response);
3484
3485 // Start active cid.
3486cid_active:
3487#endif
3488
3489 sprintf(cmd, "AT+CGACT=1,%d", cid);
3490 err = at_send_command(cmd, &response);
3491 if (err < 0 || response->success == 0){
3492 if(cme_err) {
3493 *cme_err = at_get_cme_error(response);
3494 }
3495 goto exit;
3496 }
3497
3498exit:
3499 at_response_free(response);
3500 return err;
3501}
3502
3503/*
3504AT+CGACT=0,<cid>
3505OK
3506
3507*/
3508static int req_data_call_stop(int cid, int *cme_err)
3509{
3510 ATResponse *response = NULL;
3511 char cmd[400] = {0};
3512 int err = 0;
3513#if 0
3514 err = at_send_command_multiline("AT+CGACT?", "+CGACT:", &response);
3515 if (err < 0 || response->success == 0 || !response->p_intermediates){
3516 *cme_err = at_get_cme_error(response);
3517 goto exit;
3518 }
3519 ATLine* lines_ptr = response->p_intermediates;
3520 char *line = NULL;
3521 int tmp_int;
3522 while(lines_ptr)
3523 {
3524 line = lines_ptr->line;
3525 err = at_tok_start(&line);
3526 if (err < 0)
3527 {
3528 goto exit;
3529 }
3530
3531 err = at_tok_nextint(&line, &tmp_int); // cid
3532 if (err < 0)
3533 {
3534 goto exit;
3535 }
3536 if(tmp_int == cid) { // Found cid
3537 err = at_tok_nextint(&line, &tmp_int); // cid
3538 if (err < 0)
3539 {
3540 goto exit;
3541 }
3542 if(tmp_int == 1) { // This cid has active.
3543 goto net_config;
3544 } else {
3545 goto cid_active;
3546 }
3547 break;
3548 }
3549
3550 lines_ptr = lines_ptr->p_next;
3551 }
3552
3553 if(lines_ptr == NULL) { // No found this cid.
3554 LOGE("No found cid : %d", cid);
3555 goto exit;
3556 }
3557 at_response_free(response);
3558
3559 // Start active cid.
3560cid_active:
3561#endif
3562
3563 sprintf(cmd, "AT+CGACT=0,%d", cid);
3564 err = at_send_command(cmd, &response);
3565 if (err < 0 || response->success == 0){
3566 *cme_err = at_get_cme_error(response);
3567 goto exit;
3568 }
3569
3570exit:
3571 at_response_free(response);
3572 return err;
3573}
3574
3575/*
3576IPv4 : 10.255.74.26
3577IPv6 : 254.128.0.0.0.0.0.0.0.1.0.2.144.5.212.239
3578*/
3579static bool is_ipv4(const char *ip)
3580{
3581 const char *ptr = ip;
3582 int count = 0;
3583 while(*ptr) {
3584 if(*ptr == '.')
3585 count++;
3586 ptr++;
3587 }
3588
3589 if(count == 3) {
3590 return true;
3591 } else {
3592 return false;
3593 }
3594}
3595
3596/*
3597AT+CGCONTRDP=1
3598+CGCONTRDP: 1,7,"cmnet-2.MNC000.MCC460.GPRS","10.255.74.26","","223.87.253.100","223.87.253.253","","",0,0
3599+CGCONTRDP: 1,7,"cmnet-2.MNC000.MCC460.GPRS","254.128.0.0.0.0.0.0.0.1.0.2.144.5.212.239","","36.9.128.98.32.0.0.2.0.0.0.0.0.0.0.1","36.9.128.98.32.0.0.2.0.0.0.0.0.0.0.2","","",0,0
3600
3601OK
3602
3603*/
3604static int req_data_call_state_get(int cid, mbtk_ipv4_info_t *ipv4, mbtk_ipv6_info_t *ipv6, int *cme_err)
3605{
3606 ATResponse *response = NULL;
3607 char cmd[50] = {0};
3608 int err = 0;
3609
3610 sprintf(cmd, "AT+CGCONTRDP=%d", cid);
3611
3612 err = at_send_command_multiline(cmd, "+CGCONTRDP:", &response);
3613 if (err < 0 || response->success == 0 || !response->p_intermediates){
3614 *cme_err = at_get_cme_error(response);
3615 goto exit;
3616 }
3617 ATLine* lines_ptr = response->p_intermediates;
3618 char *line = NULL;
3619 int tmp_int;
3620 char *tmp_ptr = NULL;
3621 while(lines_ptr)
3622 {
3623 line = lines_ptr->line;
3624 err = at_tok_start(&line);
3625 if (err < 0)
3626 {
3627 goto exit;
3628 }
3629
3630 err = at_tok_nextint(&line, &tmp_int); // cid
3631 if (err < 0)
3632 {
3633 goto exit;
3634 }
3635 err = at_tok_nextint(&line, &tmp_int); // bearer_id
3636 if (err < 0)
3637 {
3638 goto exit;
3639 }
3640 err = at_tok_nextstr(&line, &tmp_ptr); // APN
3641 if (err < 0)
3642 {
3643 goto exit;
3644 }
3645
3646 err = at_tok_nextstr(&line, &tmp_ptr); // IP
3647 if (err < 0 || str_empty(tmp_ptr))
3648 {
3649 goto exit;
3650 }
3651 if(is_ipv4(tmp_ptr)) {
3652 if(inet_pton(AF_INET, tmp_ptr, &(ipv4->IPAddr)) < 0) {
3653 LOGE("inet_pton() fail.");
3654 err = -1;
3655 goto exit;
3656 }
3657
3658 ipv4->valid = true;
3659 //log_hex("IPv4", &(ipv4->IPAddr), sizeof(struct in_addr));
3660 } else {
3661 if(str_2_ipv6(tmp_ptr, &(ipv6->IPV6Addr))) {
3662 LOGE("str_2_ipv6() fail.");
3663 err = -1;
3664 goto exit;
3665 }
3666
3667 ipv6->valid = true;
3668 //log_hex("IPv6", &(ipv6->IPV6Addr), 16);
3669 }
3670
3671 err = at_tok_nextstr(&line, &tmp_ptr); // Gateway
3672 if (err < 0)
3673 {
3674 goto exit;
3675 }
3676 if(!str_empty(tmp_ptr)) { // No found gateway
3677 if(is_ipv4(tmp_ptr)) {
3678 if(inet_pton(AF_INET, tmp_ptr, &(ipv4->GateWay)) < 0) {
3679 LOGE("inet_pton() fail.");
3680 err = -1;
3681 goto exit;
3682 }
3683
3684 //log_hex("IPv4", &(ipv4->GateWay), sizeof(struct in_addr));
3685 } else {
3686 if(str_2_ipv6(tmp_ptr, &(ipv6->GateWay))) {
3687 LOGE("str_2_ipv6() fail.");
3688 err = -1;
3689 goto exit;
3690 }
3691
3692 //log_hex("IPv6", &(ipv6->GateWay), 16);
3693 }
3694 }
3695
3696 err = at_tok_nextstr(&line, &tmp_ptr); // prim_DNS
3697 if (err < 0)
3698 {
3699 goto exit;
3700 }
3701 if(!str_empty(tmp_ptr)) { // No found Primary DNS
3702 if(is_ipv4(tmp_ptr)) {
3703 if(inet_pton(AF_INET, tmp_ptr, &(ipv4->PrimaryDNS)) < 0) {
3704 LOGE("inet_pton() fail.");
3705 err = -1;
3706 goto exit;
3707 }
3708
3709 //log_hex("IPv4", &(ipv4->PrimaryDNS), sizeof(struct in_addr));
3710 } else {
3711 if(str_2_ipv6(tmp_ptr, &(ipv6->PrimaryDNS))) {
3712 LOGE("str_2_ipv6() fail.");
3713 err = -1;
3714 goto exit;
3715 }
3716
3717 //log_hex("IPv6", &(ipv6->PrimaryDNS), 16);
3718 }
3719 }
3720
3721 err = at_tok_nextstr(&line, &tmp_ptr); // sec_DNS
3722 if (err < 0)
3723 {
3724 goto exit;
3725 }
3726 if(!str_empty(tmp_ptr)) { // No found Secondary DNS
3727 if(is_ipv4(tmp_ptr)) {
3728 if(inet_pton(AF_INET, tmp_ptr, &(ipv4->SecondaryDNS)) < 0) {
3729 LOGE("inet_pton() fail.");
3730 err = -1;
3731 goto exit;
3732 }
3733
3734 //log_hex("IPv4", &(ipv4->SecondaryDNS), sizeof(struct in_addr));
3735 } else {
3736 if(str_2_ipv6(tmp_ptr, &(ipv6->SecondaryDNS))) {
3737 LOGE("str_2_ipv6() fail.");
3738 err = -1;
3739 goto exit;
3740 }
3741
3742 //log_hex("IPv6", &(ipv6->SecondaryDNS), 16);
3743 }
3744 }
3745
3746 lines_ptr = lines_ptr->p_next;
3747 }
3748
3749exit:
3750 at_response_free(response);
3751 return err;
3752}
3753
3754/*
3755AT+CGCONTRDP
3756+CGCONTRDP: 1,5,"cmnet.MNC000.MCC460.GPRS","10.156.238.86","","223.87.253.100","223.87.253.253","","",0,0
3757+CGCONTRDP: 1,5,"cmnet.MNC000.MCC460.GPRS","254.128.0.0.0.0.0.0.0.1.0.1.181.5.77.221","","36.9.128.98.32.0.0.2.0.0.0.0.0.0.0.1","36.9.128.98.32.0.0.2.0,0,0
3758+CGCONTRDP: 8,6,"IMS.MNC000.MCC460.GPRS","254.128.0.0.0.0.0.0.0.1.0.1.181.5.79.116","","","","36.9.128.98.80.2.0.87.0.0.0.0.0.3.31.1","36.9.128.98.80.2,1,0
3759OK
3760
3761*/
3762static int apn_state_get(list_node_t **apn_list)
3763{
3764 ATResponse *response = NULL;
3765 int err = 0;
3766 *apn_list = list_create(NULL);
3767 if(*apn_list == NULL)
3768 {
3769 LOG("list_create() fail.");
3770 return -1;
3771 }
3772
3773 err = at_send_command_multiline("AT+CGCONTRDP", "+CGCONTRDP:", &response);
3774 if (err < 0 || response->success == 0 || !response->p_intermediates){
3775 goto exit;
3776 }
3777 ATLine* lines_ptr = response->p_intermediates;
3778 char *line = NULL;
3779 int tmp_int;
3780 char *tmp_ptr = NULL;
3781 int cid_current = 0;
3782 info_apn_ip_t *apn = NULL;
3783 while(lines_ptr)
3784 {
3785 line = lines_ptr->line;
3786 err = at_tok_start(&line);
3787 if (err < 0)
3788 {
3789 goto exit;
3790 }
3791
3792 err = at_tok_nextint(&line, &tmp_int); // cid
3793 if (err < 0)
3794 {
3795 goto exit;
3796 }
3797
3798 if(tmp_int != 1 && tmp_int != 8) { // Not process cid 1 and 8.
3799 if(cid_current != tmp_int) { // New cid.
3800 apn = (info_apn_ip_t*)malloc(sizeof(info_apn_ip_t));
3801 if(apn == NULL) {
3802 goto exit;
3803 }
3804 memset(apn, 0, sizeof(info_apn_ip_t));
3805 apn->cid = tmp_int;
3806 cid_current = tmp_int;
3807
3808 list_add(*apn_list, apn);
3809 }
3810 err = at_tok_nextint(&line, &tmp_int); // bearer_id
3811 if (err < 0)
3812 {
3813 goto exit;
3814 }
3815 err = at_tok_nextstr(&line, &tmp_ptr); // APN
3816 if (err < 0)
3817 {
3818 goto exit;
3819 }
3820
3821 err = at_tok_nextstr(&line, &tmp_ptr); // IP
3822 if (err < 0 || str_empty(tmp_ptr))
3823 {
3824 goto exit;
3825 }
3826 if(is_ipv4(tmp_ptr)) {
3827 apn->ipv4_valid = true;
3828 memcpy(apn->ipv4, tmp_ptr, strlen(tmp_ptr));
3829 } else {
3830 apn->ipv6_valid = true;
3831 uint8 tmp_ipv6[16];
3832 if(str_2_ipv6(tmp_ptr, tmp_ipv6)) {
3833 LOGE("str_2_ipv6() fail.");
3834 err = -1;
3835 goto exit;
3836 }
3837
3838 if(inet_ntop(AF_INET6, tmp_ipv6, apn->ipv6, 50) == NULL) {
3839 err = -1;
3840 LOGE("inet_ntop ipv6 ip fail.");
3841 goto exit;
3842 }
3843 }
3844 }
3845
3846 lines_ptr = lines_ptr->p_next;
3847 }
3848
3849exit:
3850 at_response_free(response);
3851 return err;
3852}
3853
3854mbtk_info_err_enum call_pack_req_process(sock_client_info_t* cli_info, mbtk_info_pack_t* pack);
3855mbtk_info_err_enum sms_pack_req_process(sock_client_info_t* cli_info, mbtk_info_pack_t* pack);
3856mbtk_info_err_enum pb_pack_req_process(sock_client_info_t* cli_info, mbtk_info_pack_t* pack);
r.xiaoec113d12024-01-12 02:13:28 -08003857
liubin281ac462023-07-19 14:22:54 +08003858//mbtk wyq for data_call_ex add start
3859void data_call_bootconn_save(int cid, int bootconn);
3860//mbtk wyq for data_call_ex add end
3861
3862//void net_list_free(void *data);
3863// Return MBTK_INFO_ERR_SUCCESS,will call pack_error_send() to send RSP.
3864// Otherwise, do not call pack_error_send().
3865static mbtk_info_err_enum pack_req_process(sock_client_info_t* cli_info, mbtk_info_pack_t* pack)
3866{
3867 if(pack->info_id > MBTK_INFO_ID_CALL_BEGIN && pack->info_id < MBTK_INFO_ID_CALL_END) {
3868 return call_pack_req_process(cli_info, pack);
3869 } else if(pack->info_id > MBTK_INFO_ID_SMS_BEGIN && pack->info_id < MBTK_INFO_ID_SMS_END) {
3870 return sms_pack_req_process(cli_info, pack);
3871 } else if(pack->info_id > MBTK_INFO_ID_PB_BEGIN && pack->info_id < MBTK_INFO_ID_PB_END) {
3872 return pb_pack_req_process(cli_info, pack);
3873 } else {
3874 mbtk_info_err_enum err = MBTK_INFO_ERR_SUCCESS;
3875 int cme_err = MBTK_INFO_ERR_CME_NON;
3876 switch(pack->info_id)
3877 {
3878 case MBTK_INFO_ID_DEV_IMEI_REQ: // <string> IMEI
3879 {
3880 if(pack->data_len == 0 || pack->data == NULL) // Get IMEI
3881 {
3882 char imei[20] = {0};
3883 if(req_imei_get(imei, &cme_err) || strlen(imei) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
3884 {
3885 if(cme_err != MBTK_INFO_ERR_CME_NON) {
3886 err = MBTK_INFO_ERR_CME + cme_err;
3887 } else {
3888 err = MBTK_INFO_ERR_UNKNOWN;
3889 }
3890 LOG("Get IMEI fail.");
3891 }
3892 else
3893 {
3894 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_IMEI_RSP, imei, strlen(imei));
3895 }
3896 }
3897 else // Set IMEI(Unsupport).
3898 {
3899 err = MBTK_INFO_ERR_UNSUPPORTED;
3900 LOG("Unsupport set IMEI.");
3901 }
3902 break;
3903 }
3904 case MBTK_INFO_ID_DEV_SN_REQ: // <string> SN
3905 {
3906 if(pack->data_len == 0 || pack->data == NULL) // Get SN
3907 {
3908 char sn[20] = {0};
3909 if(req_sn_get(sn, &cme_err) || strlen(sn) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
3910 {
3911 if(cme_err != MBTK_INFO_ERR_CME_NON) {
3912 err = MBTK_INFO_ERR_CME + cme_err;
3913 } else {
3914 err = MBTK_INFO_ERR_UNKNOWN;
3915 }
3916 LOG("Get SN fail.");
3917 }
3918 else
3919 {
3920 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_SN_RSP, sn, strlen(sn));
3921 }
3922 }
3923 else // Set SN(Unsupport).
3924 {
3925 err = MBTK_INFO_ERR_UNSUPPORTED;
3926 LOG("Unsupport set SN.");
3927 }
3928 break;
3929 }
3930 case MBTK_INFO_ID_DEV_MEID_REQ: // <string> MEID (Only for CDMA)
3931 {
3932 if(pack->data_len == 0 || pack->data == NULL) // Get MEID
3933 {
3934 err = MBTK_INFO_ERR_UNSUPPORTED;
3935 LOG("Support only for CDMA.");
3936 }
3937 else // Set MEID(Unsupport).
3938 {
3939 err = MBTK_INFO_ERR_UNSUPPORTED;
3940 LOG("Unsupport set MEID.");
3941 }
3942 break;
3943 }
3944 case MBTK_INFO_ID_DEV_VERSION_REQ: // <string> VERSION
3945 {
3946 if(pack->data_len == 0 || pack->data == NULL) // Get VERSION
3947 {
3948 char version[50] = {0};
3949 if(req_version_get(version, &cme_err) || strlen(version) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
3950 {
3951 if(cme_err != MBTK_INFO_ERR_CME_NON) {
3952 err = MBTK_INFO_ERR_CME + cme_err;
3953 } else {
3954 err = MBTK_INFO_ERR_UNKNOWN;
3955 }
3956 LOG("Get Version fail.");
3957 }
3958 else
3959 {
3960 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_VERSION_RSP, version, strlen(version));
3961 }
3962 }
3963 else // Set VERSION(Unsupport).
3964 {
3965 err = MBTK_INFO_ERR_UNSUPPORTED;
3966 LOG("Unsupport set VERSION.");
3967 }
3968 break;
3969 }
3970 case MBTK_INFO_ID_DEV_MODEL_REQ: //MODEL
3971 {
3972 if(pack->data_len == 0 || pack->data == NULL) // Get MODEL
3973 {
3974 char model[50] = {0};
3975 if(req_model_get(model, &cme_err) || strlen(model) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
3976 {
3977 if(cme_err != MBTK_INFO_ERR_CME_NON) {
3978 err = MBTK_INFO_ERR_CME + cme_err;
3979 } else {
3980 err = MBTK_INFO_ERR_UNKNOWN;
3981 }
3982 LOG("Get model fail.");
3983 }
3984 else
3985 {
3986 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_VERSION_RSP, model, strlen(model));
3987 }
3988 }
3989 else // Set model(Unsupport).
3990 {
3991 err = MBTK_INFO_ERR_UNSUPPORTED;
3992 LOG("Unsupport set model.");
3993 }
3994 break;
3995 }
3996 case MBTK_INFO_ID_DEV_MODEM_REQ: //MODEM
3997 {
3998 if(pack->data_len == 0 || pack->data == NULL) // Get MODEM
3999 {
4000 int modem = -1;
4001 if(req_modem_get(&modem, &cme_err) || modem < 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4002 {
4003 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4004 err = MBTK_INFO_ERR_CME + cme_err;
4005 } else {
4006 err = MBTK_INFO_ERR_UNKNOWN;
4007 }
4008 LOG("Get modem fail.");
4009 }
4010 else
4011 {
4012 uint8 modem_type = (uint8)modem;
4013 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_MODEM_RSP, &modem_type, sizeof(uint8));
4014 }
4015 }
4016 else // Set modem
4017 {
4018 mbtk_modem_info_t *modem = (mbtk_modem_info_t *)pack->data;
4019 if(pack->data_len != sizeof(mbtk_modem_info_t))
4020 {
4021 err = MBTK_INFO_ERR_REQ_PARAMETER;
4022 LOG("Set modem error.");
4023 break;
4024 }
4025 if(req_modem_set(modem, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4026 {
4027 LOG("Set modem fail.");
4028 err = MBTK_INFO_ERR_FORMAT;
4029 } else {
4030 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_MODEM_RSP, NULL, 0);
4031 }
4032 }
4033 break;
4034 }
4035 case MBTK_INFO_ID_DEV_TIME_REQ: // <uint8><string> YYYY-MM-DD-HH:MM:SS
4036 {
4037 if(pack->data_len == 0 || pack->data == NULL) // Get Time
4038 {
4039 int type = -1;
4040 if(req_time_get(&type, &cme_err) || type < 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4041 {
4042 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4043 err = MBTK_INFO_ERR_CME + cme_err;
4044 } else {
4045 err = MBTK_INFO_ERR_UNKNOWN;
4046 }
4047 LOG("Get Time fail.");
4048 }
4049 else
4050 {
4051 uint8 time_type = (uint8)type;
4052 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_TIME_RSP, &time_type, sizeof(uint8));
4053 }
4054 }
4055 else // Set Time
4056 {
4057 if(pack->data_len == sizeof(uint8)) {
4058 if(req_time_set(*(pack->data), NULL, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4059 {
4060 LOG("Set Time fail.");
4061 err = MBTK_INFO_ERR_FORMAT;
4062 } else {
4063 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_TIME_RSP, NULL, 0);
4064 }
4065 } else {
4066 char time_ptr[100] = {0};
4067 memcpy(time_ptr, pack->data + sizeof(uint8), pack->data_len - sizeof(uint8));
4068 if(req_time_set(*(pack->data), time_ptr, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4069 {
4070 LOG("Set Time fail.");
4071 err = MBTK_INFO_ERR_FORMAT;
4072 } else {
4073 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_TIME_RSP, NULL, 0);
4074 }
4075 }
4076 }
4077 break;
4078 }
4079 case MBTK_INFO_ID_NET_TIME_REQ: // <string> YYYY-MM-DD-HH:MM:SS
4080 {
4081 if(pack->data_len == 0 || pack->data == NULL) // Get Time
4082 {
4083 char time[100];
4084 if(req_net_time_get(time, &cme_err) || strlen(time) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4085 {
4086 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4087 err = MBTK_INFO_ERR_CME + cme_err;
4088 } else {
4089 err = MBTK_INFO_ERR_UNKNOWN;
4090 }
4091 LOG("Get Time fail.");
4092 }
4093 else
4094 {
4095 char time_ser[100]={0};
4096 memcpy(time_ser,time,strlen(time));
4097 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_TIME_RSP, time_ser, strlen(time_ser));
4098 }
4099 }
4100 else // Set Time
4101 {
4102 err = MBTK_INFO_ERR_UNSUPPORTED;
4103 LOG("Unsupport set TIME.");
4104 }
4105 break;
4106 }
4107 case MBTK_INFO_ID_DEV_VOLTE_REQ: // <uint8> 0:Close 1:Open
4108 {
4109 if(pack->data_len == 0 || pack->data == NULL) // Get VoLTE state.
4110 {
4111 int state;
4112 if(req_volte_get(&state, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4113 {
4114 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4115 err = MBTK_INFO_ERR_CME + cme_err;
4116 } else {
4117 err = MBTK_INFO_ERR_UNKNOWN;
4118 }
4119 LOG("Get VoLTE state fail.");
4120 }
4121 else
4122 {
4123 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_VOLTE_RSP, &state, sizeof(uint8));
4124 }
4125 }
4126 else // Set VoLTE state.
4127 {
4128 uint8 on = *(pack->data);
4129 if(pack->data_len != sizeof(uint8) || (on != 0 && on != 1))
4130 {
4131 err = MBTK_INFO_ERR_REQ_PARAMETER;
4132 LOG("Set VOLTE parameter error.");
4133 break;
4134 }
4135
4136 if(req_volte_set(on, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4137 {
4138 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4139 err = MBTK_INFO_ERR_CME + cme_err;
4140 } else {
4141 err = MBTK_INFO_ERR_UNKNOWN;
4142 }
4143 LOG("Set VoLTE state fail.");
4144 }
4145 else
4146 {
4147 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_VOLTE_RSP, NULL, 0);
4148
4149 // Restart is required to take effect.
4150 LOG("Will reboot system...");
4151 }
4152 }
4153 break;
4154 }
4155 case MBTK_INFO_ID_DEV_TEMP_REQ: // <string> Temperature
4156 {
4157 if(pack->data_len == sizeof(uint8) && pack->data) {
4158 int temp;
4159 if(req_temp_get(*(pack->data), &temp, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4160 {
4161 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4162 err = MBTK_INFO_ERR_CME + cme_err;
4163 } else {
4164 err = MBTK_INFO_ERR_UNKNOWN;
4165 }
4166 LOG("Get temperature fail.");
4167 }
4168 else
4169 {
4170 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_TEMP_RSP, &temp, sizeof(uint8));
4171 }
4172 } else {
4173 err = MBTK_INFO_ERR_FORMAT;
4174 LOG("Unsupport get Temperature.");
4175 }
4176 break;
4177 }
4178 case MBTK_INFO_ID_SIM_PLMN_REQ: // plmn
4179 {
4180 if(pack->data_len == 0 || pack->data == NULL) // plmn
4181 {
4182 mbtk_plmn_info plmn;
4183 if(req_plmn_get(&plmn, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4184 {
4185 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4186 err = MBTK_INFO_ERR_CME + cme_err;
4187 } else {
4188 err = MBTK_INFO_ERR_UNKNOWN;
4189 }
4190 LOG("Get PLMN fail.");
4191 }
4192 else
4193 {
4194 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_PLMN_RSP, &plmn, sizeof(mbtk_plmn_info));
4195 }
4196 }
4197 else // Set
4198 {
4199 err = MBTK_INFO_ERR_UNSUPPORTED;
4200 LOG("Set sim state fail.");
4201 }
4202 break;
4203 }
4204 case MBTK_INFO_ID_SIM_STATE_REQ: // mbtk_sim_state_enum
4205 {
4206 if(pack->data_len == 0 || pack->data == NULL) // Get sim state.
4207 {
4208 uint8 sim_state = (uint8)getSIMStatus();
4209 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_STATE_RSP, &sim_state, sizeof(uint8));
4210 }
4211 else // Set
4212 {
4213 err = MBTK_INFO_ERR_UNSUPPORTED;
4214 LOG("Set sim state fail.");
4215 }
4216 break;
4217 }
4218 case MBTK_INFO_ID_SIM_STYPE_REQ: // mbtk_sim_card_type_enum
4219 {
4220 if(pack->data_len == 0 || pack->data == NULL) // Get sim card type
4221 {
4222 uint8 sim_card_type;
4223 if(req_sim_card_type_get(&sim_card_type, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4224 {
4225 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4226 err = MBTK_INFO_ERR_CME + cme_err;
4227 } else {
4228 err = MBTK_INFO_ERR_UNKNOWN;
4229 }
4230 LOG("Get IMSI fail.");
4231 }
4232 else
4233 {
4234 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_STYPE_RSP, &sim_card_type, sizeof(uint8));
4235 }
4236 }
4237 else // Set
4238 {
4239 err = MBTK_INFO_ERR_UNSUPPORTED;
4240 LOG("Set sim state fail.");
4241 }
4242 break;
4243 }
4244 case MBTK_INFO_ID_SIM_PINPUK_TIMES_REQ: // mbtk_pin_puk_last_times
4245 {
4246 if(pack->data_len == 0 || pack->data == NULL) // Get sim card type
4247 {
4248 mbtk_pin_puk_last_times pin_puk_last_times;
4249 if(req_pin_puk_last_times_get(&pin_puk_last_times, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4250 {
4251 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4252 err = MBTK_INFO_ERR_CME + cme_err;
4253 } else {
4254 err = MBTK_INFO_ERR_UNKNOWN;
4255 }
4256 LOG("Get IMSI fail.");
4257 }
4258 else
4259 {
4260 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_PINPUK_TIMES_RSP, &pin_puk_last_times, sizeof(mbtk_pin_puk_last_times));
4261 }
4262 }
4263 else // Set
4264 {
4265 err = MBTK_INFO_ERR_UNSUPPORTED;
4266 LOG("Set sim state fail.");
4267 }
4268 break;
4269 }
4270 case MBTK_INFO_ID_SIM_ENABLE_PIN_REQ: // <string> PIN
4271 {
4272 if(pack->data_len == 0 || pack->data == NULL) // Enable PIN
4273 {
4274 err = MBTK_INFO_ERR_UNSUPPORTED;
4275 LOG("Unsupport GET PIN.");
4276 }
4277 else // Enable PIN
4278 {
4279 mbtk_enable_pin_info *pin = NULL;
4280 pin = (mbtk_enable_pin_info *)pack->data;
4281 if(req_pin_enable(pin, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4282 {
4283 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4284 err = MBTK_INFO_ERR_CME + cme_err;
4285 } else {
4286 err = MBTK_INFO_ERR_UNKNOWN;
4287 }
4288 LOG("Get IMSI fail.");
4289 }
4290 else
4291 {
4292 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_ENABLE_PIN_RSP, NULL, 0);
4293 }
4294 }
4295 break;
4296 }
4297 case MBTK_INFO_ID_SIM_PIN_REQ: // <string> PIN
4298 {
4299 if(pack->data_len == 0 || pack->data == NULL) // PIN
4300 {
4301 err = MBTK_INFO_ERR_UNSUPPORTED;
4302 LOG("Unsupport GET PIN.");
4303 }
4304 else // Set PIN
4305 {
4306 char pin[16] = {0};
4307 memcpy(pin, pack->data, pack->data_len);
4308 if(req_pin_verify(pin, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4309 {
4310 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4311 err = MBTK_INFO_ERR_CME + cme_err;
4312 } else {
4313 err = MBTK_INFO_ERR_UNKNOWN;
4314 }
4315 LOG("Set PIN fail.");
4316 }
4317 else
4318 {
4319 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_PIN_RSP, NULL, 0);
4320 }
4321 }
4322 break;
4323 }
4324 case MBTK_INFO_ID_SIM_PUK_REQ: // <string> PUK
4325 {
4326 if(pack->data_len == 0 || pack->data == NULL)
4327 {
4328 err = MBTK_INFO_ERR_UNSUPPORTED;
4329 LOG("Unsupport.");
4330 }
4331 else // change PIN
4332 {
4333 mbtk_unlock_pin_info *pin_info = NULL;
4334 pin_info = (mbtk_unlock_pin_info *)pack->data;
4335 if(req_puk_unlock_pin(pin_info, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4336 {
4337 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4338 err = MBTK_INFO_ERR_CME + cme_err;
4339 } else {
4340 err = MBTK_INFO_ERR_UNKNOWN;
4341 }
4342 LOG("Get IMSI fail.");
4343 }
4344 else
4345 {
4346 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_CHANGE_PIN_RSP, NULL, 0);
4347 }
4348 }
4349 break;
4350 }
4351 case MBTK_INFO_ID_SIM_CHANGE_PIN_REQ: // <string> <string> old_PIN new_PIN
4352 {
4353 if(pack->data_len == 0 || pack->data == NULL)
4354 {
4355 err = MBTK_INFO_ERR_UNSUPPORTED;
4356 LOG("Unsupport.");
4357 }
4358 else // change PIN
4359 {
4360 mbtk_change_pin_info *pin_info = NULL;
4361 pin_info = (mbtk_change_pin_info *)pack->data;
4362 if(req_pin_change(pin_info, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4363 {
4364 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4365 err = MBTK_INFO_ERR_CME + cme_err;
4366 } else {
4367 err = MBTK_INFO_ERR_UNKNOWN;
4368 }
4369 LOG("Get IMSI fail.");
4370 }
4371 else
4372 {
4373 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_CHANGE_PIN_RSP, NULL, 0);
4374 }
4375 }
4376 break;
4377 }
4378 case MBTK_INFO_ID_SIM_IMSI_REQ: // <string> IMSI
4379 {
4380 if(pack->data_len == 0 || pack->data == NULL) // Get IMSI
4381 {
4382 char imsi[20] = {0};
4383 if(req_imsi_get(imsi, &cme_err) || strlen(imsi) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4384 {
4385 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4386 err = MBTK_INFO_ERR_CME + cme_err;
4387 } else {
4388 err = MBTK_INFO_ERR_UNKNOWN;
4389 }
4390 LOG("Get IMSI fail.");
4391 }
4392 else
4393 {
4394 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_IMSI_RSP, imsi, strlen(imsi));
4395 }
4396 }
4397 else // Set IMSI(Unsupport).
4398 {
4399 err = MBTK_INFO_ERR_UNSUPPORTED;
4400 LOG("Unsupport set IMSI.");
4401 }
4402 break;
4403 }
4404 case MBTK_INFO_ID_SIM_ICCID_REQ: // <string> ICCID
4405 {
4406 if(pack->data_len == 0 || pack->data == NULL) // Get ICCID
4407 {
4408 //log_hex("pack", pack, sizeof(mbtk_info_pack_t));
4409 //sleep(1);
4410 char iccid[50] = {0};
4411 if(req_iccid_get(iccid, &cme_err) || strlen(iccid) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4412 {
4413 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4414 err = MBTK_INFO_ERR_CME + cme_err;
4415 } else {
4416 err = MBTK_INFO_ERR_UNKNOWN;
4417 }
4418 LOG("Get ICCID fail.");
4419 }
4420 else
4421 {
4422 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_ICCID_RSP, iccid, strlen(iccid));
4423 }
4424 }
4425 else // Set ICCID(Unsupport).
4426 {
4427 err = MBTK_INFO_ERR_UNSUPPORTED;
4428 LOG("Unsupport set ICCID.");
4429 }
4430 break;
4431 }
4432 case MBTK_INFO_ID_SIM_PN_REQ: // <string> Phone Number
4433 {
4434 if(pack->data_len == 0 || pack->data == NULL) // Get Phone Number
4435 {
4436 //log_hex("pack", pack, sizeof(mbtk_info_pack_t));
4437 //sleep(1);
4438 char phone_number[50] = {0};
4439 if(req_phone_number_get(phone_number, &cme_err) || strlen(phone_number) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4440 {
4441 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4442 err = MBTK_INFO_ERR_CME + cme_err;
4443 } else {
4444 err = MBTK_INFO_ERR_UNKNOWN;
4445 }
4446 LOG("Get Phone Number fail.");
4447 }
4448 else
4449 {
4450 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_PN_RSP, phone_number, strlen(phone_number));
4451 }
4452 }
4453 else // Set Phone Number(Unsupport).
4454 {
4455 err = MBTK_INFO_ERR_UNSUPPORTED;
4456 LOG("Unsupport set Phone Number.");
4457 }
4458 break;
4459 }
4460 case MBTK_INFO_ID_NET_SEL_MODE_REQ: // <mbtk_net_info_t> Operator
4461 {
4462 if(pack->data_len == 0 || pack->data == NULL) // Get
4463 {
4464 mbtk_net_info_t info;
4465 memset(&info, 0, sizeof(mbtk_net_info_t));
4466 if(req_net_sel_mode_get(&info, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4467 {
4468 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4469 err = MBTK_INFO_ERR_CME + cme_err;
4470 } else {
4471 err = MBTK_INFO_ERR_UNKNOWN;
4472 }
4473 LOG("Get Net select mode fail.");
4474 }
4475 else
4476 {
4477 LOG("NET : %d, %d, %d, %d", info.net_sel_mode, info.net_type, info.net_state, info.plmn);
4478 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_SEL_MODE_RSP, &info, sizeof(mbtk_net_info_t));
4479 }
4480 }
4481 else // Set
4482 {
4483 //LOG("1 pack-%p,data-%p,data_len-%d", pack, pack->data, pack->data_len);
4484 //log_hex("pack", pack, sizeof(mbtk_info_pack_t));
4485 //log_hex("data", pack->data, pack->data_len);
4486
4487 mbtk_net_info_t* info = (mbtk_net_info_t*)pack->data;//(mbtk_net_info_t*)mbtk_info_pack_data_get(pack, NULL);
4488 if(info == NULL) {
4489 err = MBTK_INFO_ERR_FORMAT;
4490 LOG("Get Net select mode fail.");
4491 } else {
4492 LOG("NET : %d, %d, %d", info->net_sel_mode, info->net_type, info->plmn);
4493 if(req_net_sel_mode_set(info, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON) {
4494 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4495 err = MBTK_INFO_ERR_CME + cme_err;
4496 } else {
4497 err = MBTK_INFO_ERR_UNKNOWN;
4498 }
4499 LOG("Get Net select mode fail.");
4500 } else {
4501 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_SEL_MODE_RSP, NULL, 0);
4502 }
4503 }
4504 }
4505 break;
4506 }
4507 case MBTK_INFO_ID_NET_AVAILABLE_REQ:// sel_mode(uint8)type(uint8)plmn(uint32)...sel_mode(uint8)type(uint8)plmn(uint32)
4508 {
4509 if(pack->data_len == 0 || pack->data == NULL) // Get Available Net.
4510 {
4511 int buffer_size;
4512 uint8 buffer[SOCK_MSG_LEN_MAX];
4513 memset(buffer, 0, SOCK_MSG_LEN_MAX);
4514 if((buffer_size = req_available_net_get(buffer, &cme_err)) <= 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4515 {
4516 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4517 err = MBTK_INFO_ERR_CME + cme_err;
4518 } else {
4519 err = MBTK_INFO_ERR_UNKNOWN;
4520 }
4521 LOG("Get Available Net fail.");
4522 }
4523 else
4524 {
4525 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_AVAILABLE_RSP, buffer, buffer_size);
4526 }
4527 }
4528 else // Set Available Net(Unsupport).
4529 {
4530 err = MBTK_INFO_ERR_UNSUPPORTED;
4531 LOG("Unsupport set available net.");
4532 }
4533 break;
4534 }
4535 case MBTK_INFO_ID_NET_BAND_REQ:
4536 {
4537 if(pack->data_len == 0 || pack->data == NULL)
4538 {
4539 err = MBTK_INFO_ERR_REQ_PARAMETER;
4540 LOG("No data found.");
4541 }
4542 else // Get support/current bands.
4543 {
4544 if(pack->data_len == sizeof(uint8)) {
4545 if(*(pack->data)) { // Get current bands.
4546 mbtk_band_info_t band;
4547 if(req_band_get(&band, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4548 {
4549 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4550 err = MBTK_INFO_ERR_CME + cme_err;
4551 } else {
4552 err = MBTK_INFO_ERR_UNKNOWN;
4553 }
4554 LOG("Get net band fail.");
4555 }
4556 else
4557 {
4558 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_BAND_RSP, &band, sizeof(mbtk_band_info_t));
4559 }
4560 } else { // Get support bands.
4561 band_support_get();
4562 if(band_support.net_pref != 0)
4563 {
4564 err = MBTK_INFO_ERR_UNKNOWN;
4565 LOG("Get support bands fail.");
4566 }
4567 else
4568 {
4569 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_BAND_RSP, &band_support, sizeof(mbtk_band_info_t));
4570 }
4571 }
4572 } else { // Set current bands.
4573 mbtk_band_info_t* band = (mbtk_band_info_t*)pack->data;
4574 if(pack->data_len != sizeof(mbtk_band_info_t))
4575 {
4576 err = MBTK_INFO_ERR_REQ_PARAMETER;
4577 LOG("Set net band error.");
4578 break;
4579 }
4580
4581 if(req_band_set(band, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4582 {
4583 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4584 err = MBTK_INFO_ERR_CME + cme_err;
4585 } else {
4586 err = MBTK_INFO_ERR_UNKNOWN;
4587 }
4588 LOG("Set net band fail.");
4589 }
4590 else
4591 {
4592 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_BAND_RSP, NULL, 0);
4593 }
4594 }
4595 }
4596 break;
4597 }
4598 case MBTK_INFO_ID_NET_CELL_REQ: // mbtk_cell_info_t[]
4599 {
4600 if(pack->data_len == 0 || pack->data == NULL) // Get net cell.
4601 {
4602 if(req_cell_info_get(&cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4603 {
4604 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4605 err = MBTK_INFO_ERR_CME + cme_err;
4606 } else {
4607 err = MBTK_INFO_ERR_UNKNOWN;
4608 }
4609 LOG("Get net cell fail.");
4610 }
4611 else
4612 {
4613 LOG("req_cell_info_get() success,cell number: %d", cell_info.cell_num);
4614 //sleep(1);
4615 // mbtK_cell_pack_info_t
4616 if(cell_info.cell_num > 0 && cell_info.cell_num <= CELL_NUM_MAX && cell_info.type >= 0 && cell_info.type <= 2) {
4617 uint8 *data = (uint8*)malloc(sizeof(uint8) + sizeof(mbtk_cell_info_t) * cell_info.cell_num);
4618 if(data == NULL){
4619 err = MBTK_INFO_ERR_MEMORY;
4620 LOG("Get net cell fail.");
4621 } else {
4622 *data = cell_info.type; // Set network type.
4623 // Copy cell info item.
4624 #if 0
4625 int i = 0;
4626 while(i < cell_info.cell_num) {
4627 memcpy(data + sizeof(uint8) + sizeof(mbtk_cell_info_t) * i,
4628 &(cell_info.cell[i]),
4629 sizeof(mbtk_cell_info_t));
4630 i++;
4631 }
4632 #else
4633 memcpy(data + sizeof(uint8),
4634 &(cell_info.cell),
4635 sizeof(mbtk_cell_info_t) * cell_info.cell_num);
4636 #endif
4637 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_CELL_RSP, data, sizeof(uint8) + sizeof(mbtk_cell_info_t) * cell_info.cell_num);
4638 free(data);
4639 }
4640 } else {
4641 err = MBTK_INFO_ERR_UNKNOWN;
4642 LOG("Get net cell fail.");
4643 }
4644 }
4645 }
4646 else // Lock cell
4647 {
4648 char *mem = (char*)(pack->data);
4649 int len = pack->data_len;
4650 char reg[100] = {0};
4651 printf("mem:%s, len:%d", pack->data, pack->data_len);
4652
4653 if(req_cell_info_set(mem, reg, len, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4654 {
4655 // printf("cpms_set fail\n");
4656 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4657 err = MBTK_INFO_ERR_CME + cme_err;
4658 } else {
4659 err = MBTK_INFO_ERR_UNKNOWN;
4660 }
4661 // LOG("Set req_cell_info_set fail.");
4662 }
4663 else
4664 {
4665
4666 printf("req_cell_info_set success, reg:%s\n", reg);
4667 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_CELL_RSP, reg, strlen(reg));
4668
4669 // Restart is required to take effect.
4670 LOG("Will reboot system...");
4671 }
4672 }
4673 break;
4674 }
4675 case MBTK_INFO_ID_NET_RADIO_REQ: // <uint8> 0:OFF 1:ON
4676 {
4677 if(pack->data_len == 0 || pack->data == NULL) // Get
4678 {
4679 uint8 radio_on = (uint8)isRadioOn();
4680 if(radio_on < 0)
4681 {
4682 err = MBTK_INFO_ERR_UNKNOWN;
4683 LOG("Get radio state fail.");
4684 }
4685 else
4686 {
4687 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_RADIO_RSP, &radio_on, sizeof(uint8));
4688 }
4689 }
4690 else // Set
4691 {
4692 uint8 radio_on = *(pack->data);
4693 if(radio_on != 0 && radio_on != 1)
4694 {
4695 err = MBTK_INFO_ERR_REQ_PARAMETER;
4696 LOG("Set radio state fail.");
4697 }
4698 else
4699 {
4700 setRadioPower(radio_on);
4701 if((radio_on && net_info.radio_state == MBTK_RADIO_STATE_ON)
4702 || (!radio_on && net_info.radio_state == MBTK_RADIO_STATE_OFF)) {
4703 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_RADIO_RSP, NULL, 0);
4704 } else {
4705 err = MBTK_INFO_ERR_UNKNOWN;
4706 LOG("Set radio state fail.");
4707 }
4708 }
4709 }
4710 break;
4711 }
4712 case MBTK_INFO_ID_NET_SIGNAL_REQ: // mbtk_signal_info_t
4713 {
4714 if(pack->data_len == 0 || pack->data == NULL) // Get net signal.
4715 {
4716 mbtk_signal_info_t signal;
4717 memset(&signal, 0, sizeof(mbtk_signal_info_t));
4718 if(req_net_signal_get(&signal, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4719 {
4720 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4721 err = MBTK_INFO_ERR_CME + cme_err;
4722 } else {
4723 err = MBTK_INFO_ERR_UNKNOWN;
4724 }
4725 LOG("Get net signal fail.");
4726 }
4727 else
4728 {
4729 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_SIGNAL_RSP, &signal, sizeof(mbtk_signal_info_t));
4730 }
4731 }
4732 else // Set
4733 {
4734 err = MBTK_INFO_ERR_UNSUPPORTED;
4735 LOG("Set net signal fail.");
4736 }
4737 break;
4738 }
4739 case MBTK_INFO_ID_NET_REG_REQ: // mbtk_net_reg_info_t
4740 {
4741 if(pack->data_len == 0 || pack->data == NULL) // Get net reg.
4742 {
4743 mbtk_net_reg_info_t reg;
4744 memset(&reg, 0, sizeof(mbtk_net_reg_info_t));
4745 if(req_net_reg_get(&reg, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4746 {
4747 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4748 err = MBTK_INFO_ERR_CME + cme_err;
4749 } else {
4750 err = MBTK_INFO_ERR_UNKNOWN;
4751 }
4752 LOG("Get net reg fail.");
4753 }
4754 else
4755 {
4756 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_REG_RSP, &reg, sizeof(mbtk_net_reg_info_t));
4757 }
4758 }
4759 else // Set
4760 {
4761 err = MBTK_INFO_ERR_UNSUPPORTED;
4762 LOG("Set net reg fail.");
4763 }
4764 break;
4765 }
4766 case MBTK_INFO_ID_NET_APN_REQ: // mbtk_apn_info_t
4767 {
4768 if(pack->data_len == 0 || pack->data == NULL) // Get APN
4769 {
4770 uint8 buff[SOCK_MSG_LEN_MAX];
4771 memset(buff, 0, SOCK_MSG_LEN_MAX);
4772 int data_len = 0;
4773 if(req_apn_get(buff, &data_len, &cme_err) || data_len <= 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4774 {
4775 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4776 err = MBTK_INFO_ERR_CME + cme_err;
4777 } else {
4778 err = MBTK_INFO_ERR_UNKNOWN;
4779 }
4780 LOG("Get APN fail.");
4781 }
4782 else
4783 {
4784 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_APN_RSP, buff, data_len);
4785 }
4786 }
4787 else // Set
4788 {
4789 // <cid[1]><ip_type[1]><apn_len[2]><apn><user_len[2]><user><pass_len[2]><pass><auth_len[2]><auth>
4790 const uint8* ptr = pack->data;
4791 mbtk_apn_info_t apn;
4792 int len;
4793 memset(&apn, 0, sizeof(mbtk_apn_info_t));
4794 // cid
4795 apn.cid = *ptr++;
4796
4797 // ip_type
4798 apn.ip_type = (mbtk_ip_type_enum)(*ptr++);
4799
4800 // apn
4801 len = byte_2_uint16(ptr, false);
4802 ptr += sizeof(uint16);
4803 if(len > 0) {
4804 memcpy(apn.apn, ptr, len);
4805 ptr += len;
4806 }
4807
4808 // user
4809 len = byte_2_uint16(ptr, false);
4810 ptr += sizeof(uint16);
4811 if(len > 0) {
4812 memcpy(apn.user, ptr, len);
4813 ptr += len;
4814 }
4815
4816 // pass
4817 len = byte_2_uint16(ptr, false);
4818 ptr += sizeof(uint16);
4819 if(len > 0) {
4820 memcpy(apn.pass, ptr, len);
4821 ptr += len;
4822 }
4823
4824 // auth
4825 len = byte_2_uint16(ptr, false);
4826 ptr += sizeof(uint16);
4827 if(len > 0) {
4828 memcpy(apn.auth, ptr, len);
4829 ptr += len;
4830 }
4831
4832 LOGD("APN : %d, %d, %s, %s, %s, %s", apn.cid, apn.ip_type, str_empty(apn.apn) ? "NULL" : apn.apn,
4833 str_empty(apn.user) ? "NULL" : apn.user, str_empty(apn.pass) ? "NULL" : apn.pass, str_empty(apn.auth) ? "NULL" : apn.auth);
4834 if(req_apn_set(&apn, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4835 {
4836 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4837 err = MBTK_INFO_ERR_CME + cme_err;
4838 } else {
4839 err = MBTK_INFO_ERR_UNKNOWN;
4840 }
4841 LOG("Set APN fail.");
4842 }
4843 else
4844 {
4845 // Save apn.
wangyouqianged88c722023-11-22 16:33:43 +08004846#ifdef MBTK_AF_SUPPORT
4847 if(apn.cid == 1)
4848 {
4849 default_iptype = apn.ip_type;
4850 }
4851#endif
liubin281ac462023-07-19 14:22:54 +08004852 apn_prop_set(&apn);
4853
4854 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_APN_RSP, NULL, 0);
4855 }
4856 }
4857 break;
4858 }
4859 case MBTK_INFO_ID_NET_DATA_CALL_REQ:
4860 {
4861 if(pack->data_len == 0 || pack->data == NULL)
4862 {
4863 err = MBTK_INFO_ERR_UNSUPPORTED;
4864 }
4865 else
4866 {
4867 /* <call_type[1]><cid[1]><auto_conn_interval[1]><boot_conn[1]><timeout[1]>
4868 call_type : mbtk_data_call_type_enum
4869 cid : 2 - 7
4870 timeout : second
4871 */
4872 mbtk_data_call_type_enum call_type = (mbtk_data_call_type_enum)pack->data[0];
4873 int cid = pack->data[1];
4874 int reconn = 0;
4875
4876 if(cid < MBTK_APN_CID_MIN || cid > MBTK_APN_CID_MAX) {
4877 err = MBTK_INFO_ERR_CID;
4878 break;
4879 }
4880
4881 LOG("cid_active[%d] = %d", cid, cid_active[cid]);
4882 memset(&cgact_wait, 0, sizeof(info_cgact_wait_t));
4883 switch(call_type) {
4884 case MBTK_DATA_CALL_START: {
4885 //mbtk wyq for data_call_ex add start
4886 int auto_conn_interval = pack->data[2];
4887 int boot_conn = pack->data[3];
4888 int timeout = pack->data[4];
4889 data_call_bootconn_save(cid, boot_conn);
4890
4891 if(net_info.net_type != MBTK_RADIO_TECH_E_UTRAN)
4892 {
4893 err = MBTK_INFO_ERR_NET_NO_INIT;
4894 break;
4895 }
4896
4897 if(cid_active[cid] == 1)
4898 {
4899 err = MBTK_INFO_ERR_CID_EXIST;
4900 break;
4901 }
4902
4903 data_call_reconn:
4904 //mbtk wyq for data_call_ex add end
4905 cgact_wait.waitting = true;
4906 cgact_wait.cid = cid;
4907 cgact_wait.act = true;
4908 if(req_data_call_start(cid, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4909 {
4910 //mbtk wyq for data_call_ex add start
4911 if(reconn < 5 && auto_conn_interval > 0)
4912 {
4913 sleep(auto_conn_interval);
4914 reconn++;
4915 cme_err = MBTK_INFO_ERR_CME_NON;
4916 LOG("data_call restart call.");
4917 goto data_call_reconn;
4918 }
4919 //mbtk wyq for data_call_ex add end
4920 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4921 err = MBTK_INFO_ERR_CME + cme_err;
4922 } else {
4923 err = MBTK_INFO_ERR_UNKNOWN;
4924 }
4925 LOG("%d active fail.", cid);
4926 }
4927 else
4928 {
4929 // Wait for "CONNECT" or "+CGEV:"
4930 if(wait_cgact_complete(timeout)) { // Timeout
4931 err = MBTK_INFO_ERR_TIMEOUT;
4932 break;
4933 }
4934
4935 // Get IP information.
4936 mbtk_ipv4_info_t ipv4;
4937 mbtk_ipv6_info_t ipv6;
4938 memset(&ipv4, 0, sizeof(mbtk_ipv4_info_t));
4939 memset(&ipv6, 0, sizeof(mbtk_ipv6_info_t));
4940 if(req_data_call_state_get(cid, &ipv4, &ipv6, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4941 {
4942 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4943 err = MBTK_INFO_ERR_CME + cme_err;
4944 } else {
4945 err = MBTK_INFO_ERR_UNKNOWN;
4946 }
4947 LOG("Get %d state fail.", cid);
4948 }
4949 else
4950 {
4951 // Config IPv4 address.
wangyouqianged88c722023-11-22 16:33:43 +08004952#ifdef MBTK_AF_SUPPORT
4953 if(cid == 1)
4954 {
4955 //uint8 pdp_data = cid;
4956 //pack_rsp_send(cli_info->fd , MBTK_INFO_ID_IND_PDP_STATE_CHANGE, &pdp_data, sizeof(uint8));
4957 ipv4.valid = false;
4958 ipv6.valid = false;
4959 if(default_iptype == MBTK_IP_TYPE_IP)
4960 {
4961 ipv4.valid = true;
4962 }
4963 else if(default_iptype == MBTK_IP_TYPE_IPV6)
4964 {
4965 ipv6.valid = true;
4966 }
4967 else
4968 {
4969 ipv4.valid = true;
4970 ipv6.valid = true;
4971 }
4972 }
4973#endif
liubin281ac462023-07-19 14:22:54 +08004974#if 1
4975 if(ipv4.valid) {
4976 char dev[20] = {0};
4977 sprintf(dev, "ccinet%d", cid - 1);
4978
4979 char ip[20] = {0};
4980 char gateway[20] = {0};
4981 char *gateway_ptr = NULL;
4982 char netmask[20] = {0};
4983 char *netmask_ptr = NULL;
4984 if(inet_ntop(AF_INET, &(ipv4.IPAddr), ip, 20) == NULL) {
4985 err = MBTK_INFO_ERR_UNKNOWN;
4986 LOGE("inet_ntop ipv4 ip fail.");
4987 log_hex("IPv4", &(ipv4.IPAddr), 4);
4988 break;
4989 }
4990
4991 if(ipv4.GateWay) {
4992 if(inet_ntop(AF_INET, &(ipv4.GateWay), gateway, 20) == NULL) {
4993 err = MBTK_INFO_ERR_UNKNOWN;
4994 LOGE("inet_ntop ipv4 gateway fail.");
4995 log_hex("IPv4", &(ipv4.IPAddr), 4);
4996 break;
4997 } else {
4998 gateway_ptr = gateway;
4999 }
5000 }
5001
5002 if(ipv4.NetMask) {
5003 if(inet_ntop(AF_INET, &(ipv4.NetMask), netmask, 20) == NULL) {
5004 err = MBTK_INFO_ERR_UNKNOWN;
5005 LOGE("inet_ntop ipv4 netmask fail.");
5006 log_hex("IPv4", &(ipv4.IPAddr), 4);
5007 break;
5008 } else {
5009 netmask_ptr = netmask;
5010 }
5011 }
5012
5013 if(netmask_ptr == NULL) {
5014 netmask_ptr = netmask;
5015 memcpy(netmask_ptr, "255.255.255.0", strlen("255.255.255.0"));
5016 }
5017
5018 if(mbtk_ifc_configure2(dev, ip, 0, gateway_ptr, netmask_ptr)) {
5019 LOGD("Config %s IPv4 %s fail.", dev, ip);
5020 } else {
5021 LOGD("Config %s IPv4 %s success.", dev, ip);
5022 }
5023
5024 }
5025#endif
5026 // Config IPv6 address.
5027 if(ipv6.valid) {
5028 char ip[50] = {0};
5029 char dev[20] = {0};
5030 sprintf(dev, "ccinet%d", cid - 1);
5031
5032 if(inet_ntop(AF_INET6, &(ipv6.IPV6Addr), ip, 50) == NULL) {
5033 err = MBTK_INFO_ERR_UNKNOWN;
5034 LOGE("inet_ntop ipv6 ip fail.");
5035 log_hex("IPv6", &(ipv6.IPV6Addr), 16);
5036 break;
5037 }
5038
5039 if(mbtk_ipv6_config(dev, ip, 64)) {
5040 LOGD("Config %s IPv6 %s fail.", dev, ip);
5041 } else {
5042 LOGD("Config %s IPv6 %s success.", dev, ip);
5043 }
5044 }
5045
5046 cid_active[cid] = 1;
5047 if(cli_info->fd != DATA_CALL_BOOTCONN_FD)
5048 {
5049 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_DATA_CALL_RSP, NULL, 0);
5050 }
5051 else
5052 {
5053 free(pack->data);
5054 free(cli_info);
5055 LOG("data_call bootconn success.");
5056 }
5057 }
5058 }
5059 break;
5060 }
5061 case MBTK_DATA_CALL_STOP: {
5062 //mbtk wyq for data_call_ex add start
5063 if(cid_active[cid] == 0)
5064 {
5065 err = MBTK_INFO_ERR_CID_NO_EXIST;
5066 break;
5067 }
5068
5069 int timeout = pack->data[2];
5070 //mbtk wyq for data_call_ex add end
wangyouqianged88c722023-11-22 16:33:43 +08005071#ifdef MBTK_AF_SUPPORT
5072 if(cid == 1)
5073 {
5074 char dev[20] = {0};
5075 uint8 pdp_data = cid + 100;
5076 pack_rsp_send(cli_info->fd , MBTK_INFO_ID_IND_PDP_STATE_CHANGE, &pdp_data, sizeof(uint8));
5077
5078 sprintf(dev, "ccinet%d", cid - 1);
5079
5080 // Config network.
5081 if(mbtk_ifc_configure2(dev, NULL, 0, NULL, NULL)) {
5082 LOGD("Config %s IPv4 0 fail.", dev);
5083 } else {
5084 LOGD("Config %s IPv4 0 success.", dev);
5085 }
5086 cid_active[cid] = 0;
5087 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_DATA_CALL_RSP, NULL, 0);
5088 break;
5089 }
5090#endif
liubin281ac462023-07-19 14:22:54 +08005091 cgact_wait.waitting = true;
5092 cgact_wait.cid = cid;
5093 cgact_wait.act = false;
5094 if(req_data_call_stop(cid, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5095 {
5096 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5097 err = MBTK_INFO_ERR_CME + cme_err;
5098 } else {
5099 err = MBTK_INFO_ERR_UNKNOWN;
5100 }
5101 LOG("%d deactive fail.", cid);
5102 }
5103 else
5104 {
5105 // Wait for "CONNECT" or "+CGEV:"
5106 if(wait_cgact_complete(timeout)) { // Timeout
5107 err = MBTK_INFO_ERR_TIMEOUT;
5108 break;
5109 }
5110 char dev[20] = {0};
5111 sprintf(dev, "ccinet%d", cid - 1);
5112
5113 // Config network.
5114 if(mbtk_ifc_configure2(dev, NULL, 0, NULL, NULL)) {
5115 LOGD("Config %s IPv4 0 fail.", dev);
5116 } else {
5117 LOGD("Config %s IPv4 0 success.", dev);
5118 }
5119
5120#if 0
5121 if(mbtk_ipv6_config(dev, NULL, 64)) {
5122 LOGD("Config %s IPv6 0 fail.", dev);
5123 } else {
5124 LOGD("Config %s IPv6 0 success.", dev);
5125 }
5126#endif
5127 cid_active[cid] = 0;
5128 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_DATA_CALL_RSP, NULL, 0);
5129 }
5130 break;
5131 }
5132 case MBTK_DATA_CALL_STATE: {
wangyouqianged88c722023-11-22 16:33:43 +08005133 if(cid_active[cid] == 0)
5134 {
5135 err = MBTK_INFO_ERR_CID_NO_EXIST;
5136 break;
5137 }
liubin281ac462023-07-19 14:22:54 +08005138 mbtk_ipv4_info_t ipv4;
5139 mbtk_ipv6_info_t ipv6;
5140 memset(&ipv4, 0, sizeof(mbtk_ipv4_info_t));
5141 memset(&ipv6, 0, sizeof(mbtk_ipv6_info_t));
5142 if(req_data_call_state_get(cid, &ipv4, &ipv6, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5143 {
5144 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5145 err = MBTK_INFO_ERR_CME + cme_err;
5146 } else {
5147 err = MBTK_INFO_ERR_UNKNOWN;
5148 }
5149 LOG("Get %d state fail.", cid);
5150 }
5151 else
5152 {
5153 uint8 buff[SOCK_MSG_LEN_MAX] = {0};
5154 int buff_len = 0;
wangyouqianged88c722023-11-22 16:33:43 +08005155#ifdef MBTK_AF_SUPPORT
5156 if(cid == 1)
5157 {
5158 ipv4.valid = false;
5159 ipv6.valid = false;
5160 if(default_iptype == MBTK_IP_TYPE_IP)
5161 {
5162 ipv4.valid = true;
5163 }
5164 else if(default_iptype == MBTK_IP_TYPE_IPV6)
5165 {
5166 ipv6.valid = true;
5167 }
5168 else
5169 {
5170 ipv4.valid = true;
5171 ipv6.valid = true;
5172 }
5173 }
5174#endif
liubin281ac462023-07-19 14:22:54 +08005175 if(ipv4.valid && ipv6.valid) {
5176 buff[0] = (uint8)2;
5177 buff_len++;
5178
5179 memcpy(buff + buff_len, &ipv4, sizeof(mbtk_ipv4_info_t));
5180 buff_len += sizeof(mbtk_ipv4_info_t);
5181 memcpy(buff + buff_len, &ipv6, sizeof(mbtk_ipv6_info_t));
5182 buff_len += sizeof(mbtk_ipv6_info_t);
5183 } else if(ipv4.valid) {
5184 buff[0] = (uint8)0;
5185 buff_len++;
5186
5187 memcpy(buff + buff_len, &ipv4, sizeof(mbtk_ipv4_info_t));
5188 buff_len += sizeof(mbtk_ipv4_info_t);
5189 } else if(ipv6.valid) {
5190 buff[0] = (uint8)1;
5191 buff_len++;
5192
5193 memcpy(buff + buff_len, &ipv6, sizeof(mbtk_ipv6_info_t));
5194 buff_len += sizeof(mbtk_ipv6_info_t);
5195 } else {
5196 LOGE("Get IPv4/IPv6 fail.");
5197 err = MBTK_INFO_ERR_UNKNOWN;
5198 break;
5199 }
5200 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_DATA_CALL_RSP, buff, buff_len);
5201 }
5202 break;
5203 }
5204 default: {
5205 err = MBTK_INFO_ERR_FORMAT;
5206 break;
5207 }
5208 }
5209 }
5210 break;
5211 }
r.xiaoec113d12024-01-12 02:13:28 -08005212 case MBTK_INFO_ID_WAKEUP_STA_REQ:
5213 {
5214 if(pack->data_len == 0 || pack->data == NULL)
5215 {
5216 err = MBTK_INFO_ERR_UNSUPPORTED;
5217 LOG("Get POWERIND state UNSUPPORTED.");
5218 }
5219 else // Set powerind state.
5220 {
5221 uint32 state = *(pack->data);
5222 if(req_powerind_set(state, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5223 {
5224 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5225 err = MBTK_INFO_ERR_CME + cme_err;
5226 } else {
5227 err = MBTK_INFO_ERR_UNKNOWN;
5228 }
5229 LOG("Set POWERIND state fail.");
5230 }
5231 else
5232 {
5233 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_WAKEUP_STA_RSP, NULL, 0);
5234 }
5235 }
5236 break;
5237 }
5238 case MBTK_INFO_ID_OOS_STA_REQ:
5239 {
5240 if(pack->data_len == 0 || pack->data == NULL)
5241 {
5242 mbtk_oos_info oos_t;
5243 if(req_oos_get(&oos_t, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5244 {
5245 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5246 err = MBTK_INFO_ERR_CME + cme_err;
5247 } else {
5248 err = MBTK_INFO_ERR_UNKNOWN;
5249 }
5250 LOG("Get SMS OOS fail.");
5251 printf("Get OOS fail\n");
5252 }
5253 else
5254 {
5255 printf("Get OOS suscess\n");
5256 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_OOS_STA_RSP, &oos_t, sizeof(mbtk_oos_info));
5257 }
5258 }
5259 else // Set OOS
5260 {
5261 char* state = pack->data;
5262 if(req_oos_set(state, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5263 {
5264 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5265 err = MBTK_INFO_ERR_CME + cme_err;
5266 } else {
5267 err = MBTK_INFO_ERR_UNKNOWN;
5268 }
5269 LOG("Set OOS fail.");
5270 }
5271 else
5272 {
5273 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_OOS_STA_RSP, NULL, 0);
5274 }
5275 }
5276 break;
5277 }
5278
liubin281ac462023-07-19 14:22:54 +08005279 default:
5280 {
5281 err = MBTK_INFO_ERR_REQ_UNKNOWN;
5282 LOG("Unknown request : %s", id2str(pack->info_id));
5283 break;
5284 }
5285 }
5286
5287 return err;
5288 }
5289}
5290
5291// Process AT URC data
5292static int send_pack_to_queue(sock_client_info_t* cli_info, void* pack)
5293{
5294 if(info_queue.count >= PACK_PROCESS_QUEUE_MAX)
5295 {
5296 LOG("Packet process queue is full");
5297 return -1;
5298 }
5299
5300 info_queue_item_t *item = (info_queue_item_t*)malloc(sizeof(info_queue_item_t));
5301 if(!item)
5302 {
5303 LOG("malloc() fail[%d].", errno);
5304 return -1;
5305 }
5306 item->cli_info = cli_info;
5307 item->pack = pack;
5308 mbtk_queue_put(&info_queue, item);
5309
5310 // If thread is waitting,continue it.
5311 if(1/*!is_running*/)
5312 {
5313 pthread_mutex_lock(&info_mutex);
5314 pthread_cond_signal(&info_cond);
5315 pthread_mutex_unlock(&info_mutex);
5316 }
5317 else
5318 {
5319 LOG("Packet process thread is process...");
5320 }
5321
5322 return 0;
5323}
5324
5325static void radio_state_change(void *data, int data_len)
5326{
5327 uint8 *data_ptr = (uint8*)data;
5328 if(data_ptr[0]) {
5329 net_info.radio_state = MBTK_RADIO_STATE_ON;
5330 } else {
5331 net_info.radio_state = MBTK_RADIO_STATE_OFF;
5332 }
5333
5334 sock_client_info_t *cli = NULL;
5335 list_first(sock_client_list);
5336 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
5337 {
5338 if(cli->ind_num > 0) {
5339 int i;
5340 for(i = 0; i < IND_REGISTER_MAX; i++) {
5341 // Registe MBTK_INFO_ID_IND_RADIO_STATE_CHANGE
5342 if(cli->ind_register[i] == MBTK_INFO_ID_IND_RADIO_STATE_CHANGE) {
5343 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_RADIO_STATE_CHANGE, data, data_len);
5344 break;
5345 }
5346 }
5347 }
5348 }
5349}
5350
5351static void pdp_state_change(void *data, int data_len)
5352{
5353 sock_client_info_t *cli = NULL;
5354 list_first(sock_client_list);
5355 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
5356 {
5357 if(cli->ind_num > 0) {
5358 int i;
5359 for(i = 0; i < IND_REGISTER_MAX; i++) {
5360 if(cli->ind_register[i] == MBTK_INFO_ID_IND_PDP_STATE_CHANGE) {
5361 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_PDP_STATE_CHANGE, data, data_len);
5362 break;
5363 }
5364 }
5365 }
5366 }
5367}
5368
5369static void net_state_change(void *data, int data_len)
5370{
5371 sock_client_info_t *cli = NULL;
5372 list_first(sock_client_list);
5373 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
5374 {
5375 if(cli->ind_num > 0) {
5376 int i;
5377 for(i = 0; i < IND_REGISTER_MAX; i++) {
5378 // Registe MBTK_INFO_ID_IND_NET_STATE_CHANGE
5379 if(cli->ind_register[i] == MBTK_INFO_ID_IND_NET_STATE_CHANGE) {
5380 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_NET_STATE_CHANGE, data, data_len);
5381 break;
5382 }
5383 }
5384 }
5385 }
5386}
5387
5388static void call_state_change(void *data, int data_len)
5389{
5390 sock_client_info_t *cli = NULL;
5391 list_first(sock_client_list);
5392 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
5393 {
5394 if(cli->ind_num > 0) {
5395 int i;
5396 for(i = 0; i < IND_REGISTER_MAX; i++) {
5397 // Registed MBTK_INFO_ID_IND_RADIO_STATE_CHANGE
5398 if(cli->ind_register[i] == MBTK_INFO_ID_IND_CALL_STATE_CHANGE) {
5399 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_CALL_STATE_CHANGE, data, data_len);
5400 break;
5401 }
5402 }
5403 }
5404 }
5405}
5406
5407static void sim_state_change(void *data, int data_len)
5408{
5409 sock_client_info_t *cli = NULL;
5410 list_first(sock_client_list);
5411 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
5412 {
5413 if(cli->ind_num > 0) {
5414 int i;
5415 for(i = 0; i < IND_REGISTER_MAX; i++) {
5416 // Registed MBTK_INFO_ID_IND_RADIO_STATE_CHANGE
5417 if(cli->ind_register[i] == MBTK_INFO_ID_IND_SIM_STATE_CHANGE) {
5418 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_SIM_STATE_CHANGE, data, data_len);
5419 break;
5420 }
5421 }
5422 }
5423 }
5424}
5425
5426static void sms_state_change(void *data, int data_len)
5427{
5428 sock_client_info_t *cli = NULL;
5429 list_first(sock_client_list);
5430 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
5431 {
5432 if(cli->ind_num > 0) {
5433 int i;
5434 for(i = 0; i < IND_REGISTER_MAX; i++) {
5435 // Registed MBTK_INFO_ID_IND_SMS_STATE_CHANGE
5436 if(cli->ind_register[i] == MBTK_INFO_ID_IND_SMS_STATE_CHANGE) {
5437 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_SMS_STATE_CHANGE, data, data_len);
5438 break;
5439 }
5440 }
5441 }
5442 }
5443}
5444
5445int urc_msg_distribute(bool async_process, info_urc_msg_id_enum msg, void *data, int data_len)
5446{
5447#if 0
5448 if(urc_queue.count >= PACK_PROCESS_QUEUE_MAX)
5449 {
5450 LOG("Packet process queue is full");
5451 return -1;
5452 }
5453
5454 info_urc_msg_t *urc = (info_urc_msg_t*)malloc(sizeof(info_urc_msg_t));
5455 if(!urc)
5456 {
5457 LOG("malloc() fail[%d].", errno);
5458 return -1;
5459 }
5460 urc->msg = msg;
5461 urc->data = memdup(data, data_len);
5462 urc->data_len = data_len;
5463
5464 mbtk_queue_put(&urc_queue, urc);
5465
5466 // If thread is waitting,continue it.
5467 if(1/*!is_running*/)
5468 {
5469 pthread_mutex_lock(&urc_mutex);
5470 pthread_cond_signal(&urc_cond);
5471 pthread_mutex_unlock(&urc_mutex);
5472 }
5473 else
5474 {
5475 LOG("Packet process thread is process...");
5476 }
5477
5478 return 0;
5479#else
5480 if(async_process) {
5481 info_urc_msg_t *urc = (info_urc_msg_t*)malloc(sizeof(info_urc_msg_t));
5482 if(!urc)
5483 {
5484 LOG("malloc() fail[%d].", errno);
5485 return -1;
5486 }
5487 urc->msg = msg;
5488 if(data && data_len > 0) {
5489 urc->data = memdup(data, data_len);
5490 urc->data_len = data_len;
5491 } else {
5492 urc->data = NULL;
5493 urc->data_len = 0;
5494 }
5495 return send_pack_to_queue(NULL, urc);
5496 } else {
5497 switch(msg) {
5498 case INFO_URC_MSG_NET_CS_REG_STATE:
5499 {
5500 net_state_change(data, data_len);
5501 break;
5502 }
5503 case INFO_URC_MSG_CALL_STATE:
5504 {
5505 call_state_change(data, data_len);
5506 break;
5507 }
5508 case INFO_URC_MSG_SMS_STATE:
5509 {
5510 sms_state_change(data, data_len);
5511 break;
5512 }
5513 case INFO_URC_MSG_SIM_STATE:
5514 {
5515 sim_state_change(data, data_len);
5516 break;
5517 }
5518 case INFO_URC_MSG_PDP_STATE:
5519 {
5520 pdp_state_change(data, data_len);
5521 break;
5522 }
5523 default: {
5524 LOGE("Unknown msg : %d", msg);
5525 break;
5526 }
5527 }
5528
5529 return 0;
5530 }
5531#endif
5532}
5533
5534
5535static void ind_regisger(sock_client_info_t* cli_info, uint16 ind)
5536{
5537 uint32 i = 0;
5538 while(i < cli_info->ind_num)
5539 {
5540 if(cli_info->ind_register[i] == ind)
5541 break;
5542 i++;
5543 }
5544
5545 if(i == cli_info->ind_num) // No found IND
5546 {
5547 cli_info->ind_register[i] = ind;
5548 cli_info->ind_num++;
5549 LOG("Register IND : %s", id2str(ind));
5550 }
5551 else
5552 {
5553 LOG("IND had exist.");
5554 }
5555}
5556
5557static void pack_distribute(sock_client_info_t* cli_info, mbtk_info_pack_t* pack)
5558{
5559 // Register IND Message.
5560 if(mbtk_info_type_get(pack->info_id) == MBTK_INFO_TYPE_IND)
5561 {
5562 mbtk_info_err_enum err = MBTK_INFO_ERR_SUCCESS;
5563 if(cli_info->ind_num >= IND_REGISTER_MAX)
5564 {
5565 LOG("IND if full.");
5566 err = MBTK_INFO_ERR_IND_FULL;
5567 }
5568 else
5569 {
5570 ind_regisger(cli_info, pack->info_id);
5571 }
5572
5573 pack_error_send(cli_info->fd, pack->info_id, err);
5574
5575 mbtk_info_pack_free(&pack);
5576 }
5577 else // Request Information.
5578 {
5579 LOG("Start process REQ(%s), Length : %d", id2str(pack->info_id), pack->data_len);
5580 if(0 && pack->data_len > 0)
5581 {
5582 log_hex("DATA", pack->data, pack->data_len);
5583 }
5584
5585 // Send to REQ_process_thread process.
5586 send_pack_to_queue(cli_info, pack);
5587
5588 // For test.
5589 // pack_error_send(cli_info->fd, pack->info_id + 1, MBTK_INFO_ERR_SUCCESS);
5590 }
5591}
5592
5593static sock_client_info_t* cli_find(int fd)
5594{
5595 sock_client_info_t *result = NULL;
5596 list_first(sock_client_list);
5597 while ((result = (sock_client_info_t*) list_next(sock_client_list)))
5598 {
5599 if (result->fd == fd)
5600 return result;
5601 }
5602
5603 return NULL;
5604}
5605
5606//mbtk wyq for server_ready_status add start
5607void server_ready_set(void)
5608{
5609 server_ready_status = 1;
5610}
5611
5612char server_ready_get(void)
5613{
5614 return server_ready_status;
5615}
5616
5617static void server_state_send(void)
5618{
5619 sock_client_info_t *cli = NULL;
5620 list_first(sock_client_list);
5621 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
5622 {
5623 if(cli->ind_num > 0) {
5624 if(cli->ind_register[0] == MBTK_INFO_ID_IND_SERVER_STATE_CHANGE) {
5625 cli->ind_num = 0;
5626 cli->ind_register[0] = 0;
5627 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_SERVER_STATE_CHANGE, "1", 1);
5628 break;
5629 }
5630 }
5631 else
5632 {
5633 break;
5634 }
5635 }
5636 LOG("handshake message send ok.");
5637}
5638
5639//mbtk wyq for server_ready_status add end
5640
5641//mbtk wyq for data_call_ex add start
5642//Save the cid that "DATA_CALL" needs to be automatically connected after startup
5643void data_call_bootconn_save(int cid, int bootconn)
5644{
5645 if(cid_bootconn[cid] == bootconn + '0')
5646 {
5647 return;
5648 }
5649 cid_bootconn[cid] = bootconn + '0';
5650
5651 LOG("data_call_bootconn_set cid_bootconn = %s", cid_bootconn);
5652 property_set("persist.mbtk.datacall.bootconn", cid_bootconn);
5653}
5654
5655static void* data_call_bootconn_pthread(void *arg)
5656{
5657 UNUSED(arg);
5658 LOG("data_call_bootconn_pthread enter.");
5659 int i = 0;
5660 int send_sum = 0;
5661 int bootconn = 0;
5662
5663 while(1)
5664 {
5665 if(server_ready_get() && send_sum == 0)
5666 {
5667 server_state_send();
5668 send_sum = 1;
5669 }
5670
5671 if(net_info.sim_state == MBTK_SIM_READY && net_info.net_type == MBTK_RADIO_TECH_E_UTRAN && bootconn == 0)
5672 {
5673 //data_call_bootconn_exec();
5674 property_get("persist.mbtk.datacall.bootconn", cid_bootconn, "00000000");
5675 LOG("data_call_bootconn_exec cid_bootconn = %s", cid_bootconn);
5676
5677 for(i = MBTK_APN_CID_MIN; i < MBTK_APN_CID_MAX + 1; i++)
5678 {
5679 if(cid_bootconn[i] == '1')
5680 {
5681 sock_client_info_t *info = (sock_client_info_t*)malloc(sizeof(sock_client_info_t));
5682 if(info == NULL)
5683 {
5684 LOG("clinent_info malloc() fail.");
5685 continue;
5686 }
5687 memset(info, 0, sizeof(sock_client_info_t));
5688 info->fd = DATA_CALL_BOOTCONN_FD;
5689
5690 mbtk_info_pack_t* pack = mbtk_info_pack_creat(MBTK_INFO_ID_NET_DATA_CALL_REQ);
5691 if(pack == NULL)
5692 {
5693 free(info);
5694 LOG("Packet malloc() fail.");
5695 continue;
5696 }
5697
5698 // "info_err"
5699 //pack->info_err = byte_2_uint16(ptr, false)
5700
5701 // "data_len"
5702 pack->data_len = 5;
5703
5704 char *p = (char *)malloc(5);
5705 p[0] = MBTK_DATA_CALL_START;
5706 p[1] = i;
5707 p[2] = 0;
5708 p[3] = 1;
5709 p[4] = 10;
5710 pack->data = p;
5711 send_pack_to_queue(info, pack);
5712 }
5713 }
5714
5715 bootconn = 1;
5716 }
5717
5718 if(bootconn == 1 && send_sum == 1)
5719 {
5720 break;
5721 }
5722 else
5723 {
5724 sleep(1);
5725 }
5726 }
5727
5728 LOG("data_call_bootconn_pthread exit.");
5729 return NULL;
5730}
5731
5732//mbtk wyq for data_call_ex add end
5733
5734static void* info_main_pthread(void* arg)
5735{
5736 UNUSED(arg);
5737 epoll_fd = epoll_create(SOCK_CLIENT_MAX + 1);
5738 if(epoll_fd < 0)
5739 {
5740 LOG("epoll_create() fail[%d].", errno);
5741 return NULL;
5742 }
5743
5744 uint32 event = EPOLLIN | EPOLLET;
5745 struct epoll_event ev;
5746 ev.data.fd = sock_listen_fd;
5747 ev.events = event; //EPOLLIN | EPOLLERR | EPOLLET;
5748 epoll_ctl(epoll_fd,EPOLL_CTL_ADD,sock_listen_fd,&ev);
5749
5750 int nready = -1;
5751 struct epoll_event epoll_events[EPOLL_LISTEN_MAX];
5752 while(1)
5753 {
5754 nready = epoll_wait(epoll_fd, epoll_events, EPOLL_LISTEN_MAX, -1);
5755 if(nready > 0)
5756 {
5757 sock_client_info_t *cli_info = NULL;
5758 int i;
5759 for(i = 0; i < nready; i++)
5760 {
5761 LOG("fd[%d] event = %x",epoll_events[i].data.fd, epoll_events[i].events);
5762 if(epoll_events[i].events & EPOLLHUP) // Client Close.
5763 {
5764 if((cli_info = cli_find(epoll_events[i].data.fd)) != NULL)
5765 {
5766 cli_close(cli_info);
5767 }
5768 else
5769 {
5770 LOG("Unknown client[fd = %d].", epoll_events[i].data.fd);
5771 }
5772 }
5773 else if(epoll_events[i].events & EPOLLIN)
5774 {
5775 if(epoll_events[i].data.fd == sock_listen_fd) // New clients connected.
5776 {
5777 int client_fd = -1;
5778 while(1)
5779 {
5780 struct sockaddr_in cliaddr;
5781 socklen_t clilen = sizeof(cliaddr);
5782 client_fd = accept(epoll_events[i].data.fd, (struct sockaddr *) &cliaddr, &clilen);
5783 if(client_fd < 0)
5784 {
5785 if(errno == EAGAIN)
5786 {
5787 LOG("All client connect get.");
5788 }
5789 else
5790 {
5791 LOG("accept() error[%d].", errno);
5792 }
5793 break;
5794 }
5795 // Set O_NONBLOCK
5796 int flags = fcntl(client_fd, F_GETFL, 0);
5797 if (flags > 0)
5798 {
5799 flags |= O_NONBLOCK;
5800 if (fcntl(client_fd, F_SETFL, flags) < 0)
5801 {
5802 LOG("Set flags error:%d", errno);
5803 }
5804 }
5805
5806 memset(&ev,0,sizeof(struct epoll_event));
5807 ev.data.fd = client_fd;
5808 ev.events = event;//EPOLLIN | EPOLLERR | EPOLLET;
5809 epoll_ctl(epoll_fd, EPOLL_CTL_ADD, client_fd, &ev);
5810
5811 sock_client_info_t *info = (sock_client_info_t*)malloc(sizeof(sock_client_info_t));
5812 if(info)
5813 {
5814 memset(info, 0, sizeof(sock_client_info_t));
5815 info->fd = client_fd;
5816 if(server_ready_get() == 1)
5817 {
5818 info->ind_num = 0;
5819 pack_rsp_send(info->fd , MBTK_INFO_ID_IND_SERVER_STATE_CHANGE, "1", 1);
5820 LOG("server ready ok.");
5821 }
5822 else
5823 {
5824 info->ind_num = 1;
5825 info->ind_register[0] = MBTK_INFO_ID_IND_SERVER_STATE_CHANGE;
5826 LOG("server ready no.");
5827 }
5828 list_add(sock_client_list, info);
5829 LOG("Add New Client FD Into List.");
5830 }
5831 else
5832 {
5833 LOG("malloc() fail.");
5834 }
5835 }
5836 }
5837 else if((cli_info = cli_find(epoll_events[i].data.fd)) != NULL) // Client data arrive.
5838 {
5839 // Read and process every message.
5840 mbtk_info_err_enum err = MBTK_INFO_ERR_SUCCESS;
5841 mbtk_info_pack_t** pack = mbtk_info_pack_recv(cli_info->fd, true, &err);
5842
5843 // Parse packet error,send error response to client.
5844 if(pack == NULL)
5845 {
5846 if(err != MBTK_INFO_ERR_SUCCESS)
5847 {
5848 pack_error_send(cli_info->fd, MBTK_INFO_ID_REQ_UNKNOWN, err);
5849 }
5850 }
5851 else
5852 {
5853#if 0
5854 int i = 0;
5855 while(pack[i] != NULL)
5856 {
5857 pack_distribute(cli_info, pack[i]);
5858 // Not free,will free in pack_process() or packet process thread.
5859 //mbtk_info_pack_free(&(pack[i]));
5860 i++;
5861 }
5862 free(pack);
5863#else
5864 mbtk_info_pack_t** pack_ptr = pack;
5865 while(*pack_ptr)
5866 {
5867 pack_distribute(cli_info, *pack_ptr);
5868 // Not free,will free in pack_process() or packet process thread.
5869 //mbtk_info_pack_free(pack_ptr);
5870 pack_ptr++;
5871 }
5872
5873 free(pack);
5874#endif
5875 }
5876 }
5877 else
5878 {
5879 LOG("Unknown socket : %d", epoll_events[i].data.fd);
5880 }
5881 }
5882 else
5883 {
5884 LOG("Unknown event : %x", epoll_events[i].events);
5885 }
5886 }
5887 }
5888 else
5889 {
5890 LOG("epoll_wait() fail[%d].", errno);
5891 }
5892 }
5893
5894 return NULL;
5895}
5896
5897static void data_call_restart()
5898{
5899#if 0
5900 // Waitting for network ok.
5901 mbtk_net_info_t info;
5902 int cme_err;
5903 int i = 0;
5904 while(i < 15) { // 15s timeout
5905 cme_err = MBTK_INFO_ERR_CME_NON;
5906 memset(&info, 0, sizeof(mbtk_net_info_t));
5907 if(!req_net_sel_mode_get(&info, &cme_err) && cme_err == MBTK_INFO_ERR_CME_NON)
5908 {
5909 if(info.net_type >= 2) {
5910 break;
5911 }
5912 }
5913
5914 sleep(1);
5915 i++;
5916 }
5917#endif
5918 // +CGACT
5919 int cid;
5920 LOGD("Start active APN.");
5921 for(cid = MBTK_APN_CID_MIN; cid <= MBTK_APN_CID_MAX && cid_active[cid]; cid++) {
5922 LOG("Active cid : %d", cid);
5923 req_data_call_start(cid, NULL);
5924 }
5925}
5926
5927/*
5928void mbtk_radio_ready_cb()
5929{
5930 pthread_t radio_pid;
5931 pthread_attr_t thread_attr;
5932 pthread_attr_init(&thread_attr);
5933 if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
5934 {
5935 LOG("pthread_attr_setdetachstate() fail.");
5936 return;
5937 }
5938
5939 if(pthread_create(&radio_pid, &thread_attr, radio_ready_thread, NULL))
5940 {
5941 LOG("pthread_create() fail.");
5942 }
5943
5944 pthread_attr_destroy(&thread_attr);
5945}
5946*/
5947
5948static void net_ifc_state_change(bool act, int cid)
5949{
5950 if(cid < MBTK_APN_CID_MIN || cid > MBTK_APN_CID_MAX) { // No nothing for cid 1 and 8
5951 return;
5952 }
5953
wangyouqiang65884152023-10-25 19:54:15 +08005954 if(act)
5955 {
5956 cid_active[cid] = 1;
5957 }
5958 else
5959 {
5960 cid_active[cid] = 0;
5961 }
liubin281ac462023-07-19 14:22:54 +08005962 char dev[20] = {0};
5963 sprintf(dev, "ccinet%d", cid - 1);
5964 if(act) { // Config IP.
5965 // Get IP information.
5966 mbtk_ipv4_info_t ipv4;
5967 mbtk_ipv6_info_t ipv6;
5968 memset(&ipv4, 0, sizeof(mbtk_ipv4_info_t));
5969 memset(&ipv6, 0, sizeof(mbtk_ipv6_info_t));
5970 int cme_err = MBTK_INFO_ERR_CME_NON;
5971 if(!req_data_call_state_get(cid, &ipv4, &ipv6, &cme_err) && cme_err == MBTK_INFO_ERR_CME_NON)
5972 {
wangyouqianged88c722023-11-22 16:33:43 +08005973#ifdef MBTK_AF_SUPPORT
5974 if(cid == 1)
5975 {
5976 ipv4.valid = false;
5977 ipv6.valid = false;
5978 if(default_iptype == MBTK_IP_TYPE_IP)
5979 {
5980 ipv4.valid = true;
5981 }
5982 else if(default_iptype == MBTK_IP_TYPE_IPV6)
5983 {
5984 ipv6.valid = true;
5985 }
5986 else
5987 {
5988 ipv4.valid = true;
5989 ipv6.valid = true;
5990 }
5991 }
5992#endif
liubin281ac462023-07-19 14:22:54 +08005993 // Config IPv4 address.
5994 if(ipv4.valid) {
5995 char ip[20] = {0};
5996 if(inet_ntop(AF_INET, &(ipv4.IPAddr), ip, 20) == NULL) {
5997 LOGE("inet_ntop ipv4 ip fail.");
5998 return;
5999 }
6000
6001 if(mbtk_ifc_configure2(dev, ip, 0, NULL, "255.255.255.0")) {
6002 LOGD("Config %s IPv4 %s fail.", dev, ip);
6003 } else {
6004 LOGD("Config %s IPv4 %s success.", dev, ip);
6005 }
6006 }
6007
6008 // Config IPv6 address.
6009 if(ipv6.valid) {
6010 char ip[50] = {0};
6011
6012 if(inet_ntop(AF_INET6, &(ipv6.IPV6Addr), ip, 50) == NULL) {
6013 LOGE("inet_ntop ipv6 ip fail.");
6014 return;
6015 }
6016
6017 if(mbtk_ipv6_config(dev, ip, 64)) {
6018 LOGD("Config %s IPv6 %s fail.", dev, ip);
6019 } else {
6020 LOGD("Config %s IPv6 %s success.", dev, ip);
6021 }
6022 }
6023 }
6024 } else { // Del IP
6025 if(mbtk_ifc_configure2(dev, NULL, 0, NULL, NULL)) {
6026 LOGD("Config %s IPv4 0 fail.", dev);
6027 } else {
6028 LOGD("Config %s IPv4 0 success.", dev);
6029 }
6030 }
6031}
6032
6033static void urc_msg_process(info_urc_msg_t *msg)
6034{
6035 uint8 *data = NULL;
6036 if(msg->data) {
6037 data = (uint8*)msg->data;
6038 }
6039 switch(msg->msg) {
6040 case INFO_URC_MSG_RADIO_STATE:
6041 {
6042 radio_state_change(msg->data, msg->data_len);
6043 // Reconfig APN while radio on.
6044 if(data[0]) {
6045 apn_prop_get();
6046 }
6047 break;
6048 }
6049 case INFO_URC_MSG_CGEV:
6050 {
6051 bool act = data[0];
6052 int cid = data[1];
6053 if(cid > 0) {
6054 net_ifc_state_change(act, cid);
6055 }
6056 break;
6057 }
6058 case INFO_URC_MSG_NET_PS_REG_STATE:
6059 {
6060 uint8 net_data[3];
6061 net_data[0] = (uint8)MBTK_NET_PS_STATE;
6062 net_data[1] = data[0];
6063 mbtk_net_reg_state_enum state = (mbtk_net_reg_state_enum)data[0];
6064 if(state == MBTK_NET_REG_STATE_HOME
6065 || state == MBTK_NET_REG_STATE_ROAMING) { // Registered, home network or roaming.
6066 mbtk_net_info_t info;
6067 int cme_err = MBTK_INFO_ERR_CME_NON;
6068 memset(&info, 0, sizeof(mbtk_net_info_t));
6069 if(!req_net_sel_mode_get(&info, &cme_err) && cme_err == MBTK_INFO_ERR_CME_NON)
6070 {
6071 net_data[2] = info.net_type;
6072 net_state_change(net_data, sizeof(net_data));
6073
6074 if(info.net_type >= MBTK_RADIO_TECH_UTRAN) {
6075 data_call_restart();
6076 }
6077 } else {
6078 net_data[2] = (uint8)0xFF;
6079 net_state_change(net_data, sizeof(net_data));
6080 }
6081 } else {
6082 net_data[2] = (uint8)0xFF;
6083 net_state_change(net_data, sizeof(net_data));
6084 }
6085 break;
6086 }
6087 case INFO_URC_MSG_NET_STATE_LOG:
6088 {
6089 // Get network state and signal.
6090 char buff[256] = {0};
6091 mbtk_signal_info_t signal;
6092 memset(&signal, 0xFF, sizeof(mbtk_signal_info_t));
6093 if(!req_net_signal_get(&signal, NULL)) {
6094 char tmp[50] = {0};
6095 struct timeval log_time;
6096 gettimeofday(&log_time, NULL);
6097 struct tm* tm_t = localtime(&(log_time.tv_sec));
6098 strftime(tmp, 50, "%F %T", tm_t);
6099 snprintf(buff, sizeof(buff), "%s:%d,%d,%d,%d,%d,%d,%d,%d", tmp, signal.type, signal.rssi, signal.rxlev, signal.ber, signal.rscp, signal.ecno,
6100 signal.rsrq, signal.rsrp);
6101 mbtk_signal_log(buff);
6102 }
6103 //
6104 break;
6105 }
6106 default:
6107 {
6108 LOGE("Unknown URC : %d", msg->msg);
6109 break;
6110 }
6111 }
6112}
6113
6114static void* pack_process_thread(void* arg)
6115{
6116 UNUSED(arg);
6117 info_queue_item_t* item = NULL;
6118 mbtk_queue_init(&info_queue);
6119 pthread_mutex_init(&info_mutex, NULL);
6120 pthread_cond_init(&info_cond, NULL);
6121
6122 memset(&band_support, 0xFF, sizeof(mbtk_band_info_t));
6123
6124 pthread_mutex_lock(&info_mutex);
6125 while(TRUE)
6126 {
6127 if(mbtk_queue_empty(&info_queue))
6128 {
6129 LOG("Packet process wait...");
6130 pthread_cond_wait(&info_cond, &info_mutex);
6131 LOG("Packet process continue...");
6132 }
6133 else
6134 {
6135 LOG("Packet process queue not empty,continue...");
6136 }
6137
6138 // Process all information request.
6139 mbtk_info_err_enum err;
6140 while((item = (info_queue_item_t*)mbtk_queue_get(&info_queue)) != NULL)
6141 {
6142 if(item->cli_info) { // REQ form client.
6143 mbtk_info_pack_t *pack = (mbtk_info_pack_t*)item->pack;
6144 LOG("Process REQ %s.", id2str(pack->info_id));
6145 at_process = true;
6146 err = pack_req_process(item->cli_info, pack);
6147 if(err != MBTK_INFO_ERR_SUCCESS)
6148 {
6149 if(item->cli_info->fd != DATA_CALL_BOOTCONN_FD)
6150 {
6151 pack_error_send(item->cli_info->fd, pack->info_id + 1, err);
6152 }
6153 else
6154 {
6155 free(pack->data);
6156 free(item->cli_info);
6157 }
6158 }
6159 at_process = false;
6160 mbtk_info_pack_free(&pack);
6161 free(item);
6162 } else { // REQ from myself.
6163 info_urc_msg_t *urc = (info_urc_msg_t*)item->pack;
6164 LOG("Process URC %d.", urc->msg);
6165 urc_msg_process(urc);
6166 if(!urc->data)
6167 free(urc->data);
6168 free(urc);
6169 }
6170 }
6171 }
6172 pthread_mutex_unlock(&info_mutex);
6173 return NULL;
6174}
6175
6176void apn_prop_get()
6177{
6178 char prop_name[20];
6179 char prop_data[300];
6180 // cid : 2 - 7
6181 int cid = MBTK_APN_CID_MIN;
6182 mbtk_apn_info_t apn;
6183 for(; cid <= MBTK_APN_CID_MAX; cid++) {
6184 memset(prop_name, 0, 20);
6185 memset(prop_data, 0, 300);
6186 memset(&apn, 0, sizeof(mbtk_apn_info_t));
6187 sprintf(prop_name, "%s_%d",MBTK_APN_PROP,cid);
6188 if(property_get(prop_name, prop_data, "") > 0 && !str_empty(prop_data)) {
6189 apn.cid = cid;
6190 char *ptr_1 = prop_data;
6191 apn.ip_type = (mbtk_ip_type_enum)atoi(ptr_1);
6192 ptr_1 = strstr(ptr_1, ",");
6193 if(!ptr_1) {
6194 continue;
6195 }
6196 ptr_1++; // Jump ',' to apn
6197
6198 char *ptr_2 = strstr(ptr_1, ",");
6199 if(!ptr_2) {
6200 continue;
6201 }
6202 memcpy(apn.apn, ptr_1, ptr_2 - ptr_1); // apn
6203
6204 ptr_2++; // Jump ',' to user
6205 ptr_1 = strstr(ptr_2, ",");
6206 if(!ptr_1) {
6207 continue;
6208 }
6209 if(memcmp(ptr_2, "NULL", 4)) { // Not "NULL"
6210 memcpy(apn.user, ptr_2, ptr_1 - ptr_2); // user
6211 }
6212
6213 ptr_1++; // Jump ',' to pass
6214 ptr_2 = strstr(ptr_1, ",");
6215 if(!ptr_2) {
6216 continue;
6217 }
6218 if(memcmp(ptr_1, "NULL", 4)) { // Not "NULL"
6219 memcpy(apn.pass, ptr_1, ptr_2 - ptr_1); // pass
6220 }
6221
6222 ptr_2++; // Jump ',' to auth (Is last item)
6223 if(memcmp(ptr_2, "NULL", 4)) { // Not "NULL"
6224 memcpy(apn.auth, ptr_2, strlen(ptr_2)); // auth
6225 }
6226
6227 req_apn_set(&apn, NULL);
6228 }
6229 }
6230}
6231
6232/*
6233AT*BAND=15,78,147,482,134742231
6234
6235OK
6236*/
6237static int lte_b28_set()
6238{
6239 int err = -1;
6240 ATResponse *p_response = NULL;
6241 char *line;
6242 char *ipv4 = NULL, *ipv6 = NULL;
6243 err = at_send_command("AT*BAND=15,78,147,482,134742231", &p_response);
6244 if ((err < 0) || (p_response == NULL) || (p_response->success == 0))
6245 {
6246 LOGE("*BAND exec error.");
6247 err = -1;
6248 goto error;
6249 }
6250
6251 LOGD("Set B28 Success.");
6252 err = 0;
6253
6254error:
6255 at_response_free(p_response);
6256 return err;
6257}
6258
6259static void* net_monitor_thread(void* arg)
6260{
6261 UNUSED(arg);
6262 // Start network monitor
6263 int cid;
6264 while(1) {
6265#if 0
6266 // Config IP
6267 list_node_t* apn_list = NULL;
6268 if(!apn_state_get(&apn_list) && apn_list != NULL) {
6269 info_apn_ip_t *apn = NULL;
6270 for(cid = MBTK_APN_CID_MIN; cid <= MBTK_APN_CID_MAX && cid_active[cid]; cid++) {
6271 bool ip_found = false;
6272 list_first(apn_list);
6273 while ((apn = (info_apn_ip_t*) list_next(apn_list))) {
6274 if(cid == apn->cid) {
6275 ip_found = true;
6276 break;
6277 }
6278 }
6279
6280 char dev[20] = {0};
6281 sprintf(dev, "ccinet%d", cid - 1);
6282 if(ip_found) { // Ip ok,set IP.
6283 if(apn->ipv4_valid) {
6284 if(mbtk_ifc_configure2(dev, (char*)apn->ipv4, 0, NULL, "255.255.255.0")) {
6285 LOGD("Config %s IPv4 %s fail.", dev, apn->ipv4);
6286 } else {
6287 LOGD("Config %s IPv4 %s success.", dev, apn->ipv4);
6288 }
6289 }
6290
6291 if(apn->ipv6_valid) {
6292 if(mbtk_ipv6_config(dev, (char*)apn->ipv6, 64)) {
6293 LOGD("Config %s IPv6 %s fail.", dev, apn->ipv6);
6294 } else {
6295 LOGD("Config %s IPv6 %s success.", dev, apn->ipv6);
6296 }
6297 }
6298 } else { // No ip
6299 if(mbtk_ifc_configure2(dev, NULL, 0, NULL, NULL)) {
6300 LOGD("Config %s IPv4 0 fail.", dev);
6301 } else {
6302 LOGD("Config %s IPv4 0 success.", dev);
6303 }
6304 }
6305 }
6306
6307 list_free(apn_list);
6308 }
6309#endif
6310
6311 if(net_info.radio_state == MBTK_RADIO_STATE_ON && net_info.sim_state == MBTK_SIM_READY) {
6312#if 0
6313 urc_msg_distribute(true, INFO_URC_MSG_NET_CS_REG_STATE, NULL, 0);
6314#else
6315 info_urc_msg_t *urc = (info_urc_msg_t*)malloc(sizeof(info_urc_msg_t));
6316 if(!urc)
6317 {
6318 LOG("malloc() fail[%d].", errno);
6319 } else {
6320 urc->msg = INFO_URC_MSG_NET_STATE_LOG;
6321 urc->data = NULL;
6322 urc->data_len = 0;
6323 send_pack_to_queue(NULL, urc);
6324 }
6325#endif
6326 }
6327
6328 sleep(15);
6329 }
6330
6331 LOGD("monitor_thread exit.");
6332 return NULL;
6333}
6334
6335static void* urc_process_thread(void* arg)
6336{
6337 UNUSED(arg);
6338 info_urc_msg_t* item = NULL;
6339 mbtk_queue_init(&urc_queue);
6340 pthread_mutex_init(&urc_mutex, NULL);
6341 pthread_cond_init(&urc_cond, NULL);
6342
6343 pthread_mutex_lock(&urc_mutex);
6344 while(TRUE)
6345 {
6346 if(mbtk_queue_empty(&urc_queue))
6347 {
6348 LOG("URC process wait...");
6349 pthread_cond_wait(&urc_cond, &urc_mutex);
6350 LOG("URC process continue...");
6351 }
6352 else
6353 {
6354 LOG("URC process queue not empty,continue...");
6355 }
6356
6357 // Process all information request.
6358 while((item = (info_urc_msg_t*)mbtk_queue_get(&urc_queue)) != NULL)
6359 {
6360 LOG("Process URC %d.", item->msg);
6361 uint8 *data = (uint8*)item->data;
6362 switch(item->msg) {
6363 case INFO_URC_MSG_RADIO_STATE:
6364 {
6365 radio_state_change(item->data, item->data_len);
6366 break;
6367 }
6368 case INFO_URC_MSG_CGEV:
6369 {
6370 bool act = data[0];
6371 int cid = data[1];
6372 if(cid > 0) {
6373 net_ifc_state_change(act, cid);
6374 }
6375 break;
6376 }
6377 default:
6378 {
6379 LOGE("Unknown URC : %d", item->msg);
6380 break;
6381 }
6382 }
6383 if(!item->data)
6384 free(item->data);
6385 free(item);
6386 }
6387 }
6388 pthread_mutex_unlock(&urc_mutex);
6389
6390 return NULL;
6391}
6392
6393static void ril_at_ready_process()
6394{
6395 net_info.radio_state = (isRadioOn() == 1) ? MBTK_RADIO_STATE_ON : MBTK_RADIO_STATE_OFF;
6396#if 1
6397 if (net_info.radio_state != MBTK_RADIO_STATE_ON)
6398 {
6399 setRadioPower(1);
6400 } else { // Radio has ON
6401 apn_prop_get();
6402 }
6403
6404 if(net_info.radio_state == MBTK_RADIO_STATE_ON)
6405 {
6406 at_send_command("AT+CEREG=2", NULL);
6407 }
6408
6409 int count = 0;
6410#endif
6411 net_info.sim_state = getSIMStatus();
6412#if 0
6413 while (net_info.sim_state != MBTK_SIM_READY && count < 30)
6414 {
6415 if(net_info.radio_state != MBTK_RADIO_STATE_ON)
6416 {
6417 setRadioPower(1);
6418 }
6419 LOGD("Waitting for SIM READY...");
6420 sleep(1);
6421 net_info.sim_state = getSIMStatus();
6422 count++;
6423 }
6424#endif
6425 if(net_info.sim_state == MBTK_SIM_READY)
6426 {
6427 LOGD("SIM READY!");
6428 }
6429 else
6430 {
6431 LOGE("SIM NOT READY!");
6432 }
6433
6434 // Set B28
6435 // AT*BAND=15,78,147,482,134742231
6436#if MBTK_LTE_B28_SUPPORT
6437 char b28_config[10];
6438 memset(b28_config, 0, 10);
6439 property_get("persist.mbtk.b28_config", b28_config, "0");
6440 if(atoi(b28_config) == 0) {
6441 if(!lte_b28_set()) { // Set B28 success.
6442 property_set("persist.mbtk.b28_config", "1");
6443 }
6444 }
6445#endif
6446}
6447
6448int mbtk_info_server_start()
6449{
6450 signal(SIGPIPE, SIG_IGN);
6451
6452 if(sock_listen_fd > 0)
6453 {
6454 LOG("Information Server Has Started.");
6455 return -1;
6456 }
6457
6458 struct sockaddr_un server_addr;
6459 sock_listen_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
6460 if(sock_listen_fd < 0)
6461 {
6462 LOG("socket() fail[%d].", errno);
6463 return -1;
6464 }
6465
6466 // Set O_NONBLOCK
6467 int flags = fcntl(sock_listen_fd, F_GETFL, 0);
6468 if (flags < 0)
6469 {
6470 LOG("Get flags error:%d", errno);
6471 goto error;
6472 }
6473 flags |= O_NONBLOCK;
6474 if (fcntl(sock_listen_fd, F_SETFL, flags) < 0)
6475 {
6476 LOG("Set flags error:%d", errno);
6477 goto error;
6478 }
6479
6480 unlink(SOCK_INFO_PATH);
6481 memset(&server_addr, 0, sizeof(struct sockaddr_un));
6482 server_addr.sun_family = AF_LOCAL;
6483 strcpy(server_addr.sun_path, SOCK_INFO_PATH);
6484 if(bind(sock_listen_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)))
6485 {
6486 LOG("bind() fail[%d].", errno);
6487 goto error;
6488 }
6489
6490 if(listen(sock_listen_fd, SOCK_CLIENT_MAX))
6491 {
6492 LOG("listen() fail[%d].", errno);
6493 goto error;
6494 }
6495
6496 sock_client_list = list_create(sock_cli_free_func);
6497 if(sock_client_list == NULL)
6498 {
6499 LOG("list_create() fail.");
6500 goto error;
6501 }
6502
6503 pthread_t info_pid, pack_pid, monitor_pid, urc_pid, bootconn_pid;
6504 pthread_attr_t thread_attr;
6505 pthread_attr_init(&thread_attr);
6506 if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
6507 {
6508 LOG("pthread_attr_setdetachstate() fail.");
6509 goto error;
6510 }
6511
6512 if(pthread_create(&info_pid, &thread_attr, info_main_pthread, NULL))
6513 {
6514 LOG("pthread_create() fail.");
6515 goto error;
6516 }
6517
6518 if(pthread_create(&pack_pid, &thread_attr, pack_process_thread, NULL))
6519 {
6520 LOG("pthread_create() fail.");
6521 goto error;
6522 }
6523
6524#if 0
6525 if(pthread_create(&urc_pid, &thread_attr, urc_process_thread, NULL))
6526 {
6527 LOG("pthread_create() fail.");
6528 goto error;
6529 }
6530#endif
6531
6532 ril_at_ready_process();
6533
6534 if(pthread_create(&monitor_pid, &thread_attr, net_monitor_thread, NULL))
6535 {
6536 LOG("pthread_create() fail.");
6537 }
6538
6539 //mbtk wyq for data_call_ex add start
6540 if(pthread_create(&bootconn_pid, &thread_attr, data_call_bootconn_pthread, NULL))
6541 {
6542 LOG("pthread_create() fail.");
6543 }
6544 //mbtk wyq for data_call_ex add end
6545
6546 pthread_attr_destroy(&thread_attr);
6547
6548 LOG("MBTK Information Server Start...");
6549
6550 return 0;
6551
6552error:
6553 close(sock_listen_fd);
6554 sock_listen_fd = -1;
6555 return -1;
6556}
6557
6558#if 0
6559int main(int argc, char *argv[])
6560{
6561 if(mbtk_info_server_start())
6562 {
6563 return -1;
6564 }
6565
6566 while(1)
6567 {
6568 sleep(24 * 60 * 60);
6569 }
6570
6571 return 0;
6572}
6573#endif
6574
6575