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