blob: 341c9c9bcecf7c95b0e179873829e5d105890140 [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*/
2756static int req_apn_get(void *data, int *data_len, int *cme_err)
2757{
2758 ATResponse *response = NULL;
2759 int err = at_send_command_multiline("AT+CGDCONT?", "+CGDCONT:", &response);
2760
2761 if (err < 0 || response->success == 0 || !response->p_intermediates){
2762 *cme_err = at_get_cme_error(response);
2763 goto exit;
2764 }
2765
2766 ATLine* lines_ptr = response->p_intermediates;
2767 char *line = NULL;
2768 int tmp_int;
2769 char *tmp_str = NULL;
2770 /*
2771 <apn_num[1]><cid[1]><ip_type[1]><apn_len[2]><apn><user_len[2]><user><pass_len[2]><pass><auth_len[2]><auth>...
2772 <cid[1]><ip_type[1]><apn_len[2]><apn><user_len[2]><user><pass_len[2]><pass><auth_len[2]><auth>
2773 */
2774 uint8* apn_num = (uint8*)data;
2775 uint8* data_ptr = (uint8*)(data + sizeof(uint8)); // Jump apn_num[1]
2776 mbtk_apn_info_t apn;
2777 while(lines_ptr)
2778 {
2779 line = lines_ptr->line;
2780 err = at_tok_start(&line);
2781 if (err < 0)
2782 {
2783 goto exit;
2784 }
2785
2786 err = at_tok_nextint(&line, &tmp_int); // cid
2787 if (err < 0)
2788 {
2789 goto exit;
2790 }
2791 // Only get CID 1-7
2792 if(tmp_int >= MBTK_APN_CID_MIN && tmp_int <= MBTK_APN_CID_MAX) {
2793 memset(&apn, 0x0, sizeof(mbtk_apn_info_t));
2794 apn.cid = tmp_int;
2795 *data_ptr++ = (uint8)tmp_int; // cid
2796
2797 err = at_tok_nextstr(&line, &tmp_str);// ip type
2798 if (err < 0)
2799 {
2800 goto exit;
2801 }
2802 if(!strcasecmp(tmp_str, "IP")) {
2803 *data_ptr++ = (uint8)MBTK_IP_TYPE_IP;
2804 apn.ip_type = MBTK_IP_TYPE_IP;
2805 } else if(!strcasecmp(tmp_str, "IPV6")) {
2806 *data_ptr++ = (uint8)MBTK_IP_TYPE_IPV6;
2807 apn.ip_type = MBTK_IP_TYPE_IPV6;
2808 } else if(!strcasecmp(tmp_str, "IPV4V6")) {
2809 *data_ptr++ = (uint8)MBTK_IP_TYPE_IPV4V6;
2810 apn.ip_type = MBTK_IP_TYPE_IPV4V6;
2811 } else {
2812 *data_ptr++ = (uint8)MBTK_IP_TYPE_PPP;
2813 apn.ip_type = MBTK_IP_TYPE_PPP;
2814 }
2815
2816 err = at_tok_nextstr(&line, &tmp_str); // apn
2817 if (err < 0)
2818 {
2819 goto exit;
2820 }
2821 if(str_empty(tmp_str)) {
2822 uint16_2_byte((uint16)0, data_ptr, false);
2823 data_ptr += sizeof(uint16);
2824 } else {
2825 uint16_2_byte((uint16)strlen(tmp_str), data_ptr, false);
2826 data_ptr += sizeof(uint16);
2827 memcpy(data_ptr, tmp_str, strlen(tmp_str));
2828 data_ptr += strlen(tmp_str);
2829 memcpy(apn.apn, tmp_str, strlen(tmp_str));
2830 }
2831
2832 if(apn_user_pass_set_by_cid(apn.cid, &apn)) {
2833 // user
2834 uint16_2_byte((uint16)0, data_ptr, false);
2835 data_ptr += sizeof(uint16);
2836
2837 // pass
2838 uint16_2_byte((uint16)0, data_ptr, false);
2839 data_ptr += sizeof(uint16);
2840
2841 // auth
2842 uint16_2_byte((uint16)0, data_ptr, false);
2843 data_ptr += sizeof(uint16);
2844 } else {
2845 // user
2846 if(str_empty(apn.user)) {
2847 uint16_2_byte((uint16)0, data_ptr, false);
2848 data_ptr += sizeof(uint16);
2849 } else {
2850 uint16_2_byte((uint16)strlen(apn.user), data_ptr, false);
2851 data_ptr += sizeof(uint16);
2852 memcpy(data_ptr, apn.user, strlen(apn.user));
2853 data_ptr += strlen(apn.user);
2854 }
2855
2856 // pass
2857 if(str_empty(apn.pass)) {
2858 uint16_2_byte((uint16)0, data_ptr, false);
2859 data_ptr += sizeof(uint16);
2860 } else {
2861 uint16_2_byte((uint16)strlen(apn.pass), data_ptr, false);
2862 data_ptr += sizeof(uint16);
2863 memcpy(data_ptr, apn.pass, strlen(apn.pass));
2864 data_ptr += strlen(apn.pass);
2865 }
2866
2867 // auth
2868 if(str_empty(apn.auth)) {
2869 uint16_2_byte((uint16)0, data_ptr, false);
2870 data_ptr += sizeof(uint16);
2871 } else {
2872 uint16_2_byte((uint16)strlen(apn.auth), data_ptr, false);
2873 data_ptr += sizeof(uint16);
2874 memcpy(data_ptr, apn.auth, strlen(apn.auth));
2875 data_ptr += strlen(apn.auth);
2876 }
2877 }
2878
2879 (*apn_num)++;
2880 }
2881
2882 lines_ptr = lines_ptr->p_next;
2883 }
2884
2885 *data_len = data_ptr - (uint8*)data;
2886
2887 goto exit;
2888exit:
2889 at_response_free(response);
2890 return err;
2891}
2892
2893#if 0
2894/*
2895LTE APN
2896AT+CFUN=4
2897AT*CGDFLT=1,IP,"private.vpdn",1,0,1,0,0,0,0,0,0,0,1,0,0,1,0,0,0,1
2898AT*CGDFAUTH=1,2,"noc@njcc.vpdn.js","123456"
2899AT+CFUN=1
2900AT+CEREG?
2901AT+CGDCONT?
2902
29032/3G APN
2904AT+CGREG?
2905AT+CGDCONT=6,IP,"private.vpdn"
2906AT*AUTHREQ=6,2,"noc@njcc.vpdn.js","123456"
2907AT+CGDATA="",6
2908AT+CGDCONT?
2909*/
2910static int req_apn_set_username(mbtk_apn_info_t *apn, int *cme_err)
2911{
2912 ATResponse *response = NULL;
2913 char cmd[400] = {0};
2914 int index = 0;
2915 int err = 0;
2916
2917 err = at_send_command_singleline("AT+COPS?", "+COPS:", &response);
2918 if (err < 0 || response->success == 0 || !response->p_intermediates){
2919 if(cme_err != NULL)
2920 *cme_err = at_get_cme_error(response);
2921 err = -1;
2922 goto apn_set;
2923 }
2924
2925 int tmp_int = 0;
2926 int state=0;
2927 char cmd_buf[64];
2928 char *line = response->p_intermediates->line;
2929 err = at_tok_start(&line);
2930 if (err < 0)
2931 {
2932 goto apn_set;
2933 }
2934 err = at_tok_nextint(&line, &tmp_int);
2935 if (err < 0)
2936 {
2937 goto apn_set;
2938 }
2939 err = at_tok_nextint(&line, &tmp_int);
2940 if (err < 0)
2941 {
2942 goto apn_set;
2943 }
2944 err = at_tok_nextstr(&line, &cmd_buf);
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 else
2955 state = tmp_int;
2956
2957apn_set:
2958 at_response_free(response);
2959 *cme_err = MBTK_INFO_ERR_CME_NON;
2960 //if(state == 7 && apn->cid == 1) //LTE && cid = 1
2961 if(0) //LTE && cid = 1
2962 {
2963 err = at_send_command("AT+CFUN=4", &response);
2964 if (err < 0 || response->success == 0){
2965 *cme_err = at_get_cme_error(response);
2966 goto exit;
2967 }
2968 at_response_free(response);
2969
2970 memset(cmd, 0, 400);
2971 index = 0;
2972 index += sprintf(cmd, "AT*CGDFLT=%d,", 1);
2973 switch(apn->ip_type) {
2974 case MBTK_IP_TYPE_IP: {
2975 index += sprintf(cmd + index,"\"IP\",");
2976 break;
2977 }
2978 case MBTK_IP_TYPE_IPV6: {
2979 index += sprintf(cmd + index,"\"IPV6\",");
2980 break;
2981 }
2982 case MBTK_IP_TYPE_IPV4V6: {
2983 index += sprintf(cmd + index,"\"IPV4V6\",");
2984 break;
2985 }
2986 default: {
2987 index += sprintf(cmd + index,"\"PPP\",");
2988 break;
2989 }
2990 }
2991
2992 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);
2993 err = at_send_command(cmd, &response);
2994 if (err < 0 || response->success == 0){
2995 *cme_err = at_get_cme_error(response);
2996 goto exit;
2997 }
2998 at_response_free(response);
2999
3000 memset(cmd, 0, 400);
3001 int cmd_auth=0;
3002 if(strstr(apn->auth,"NONE"))
3003 cmd_auth = 0;
3004 else if(strstr(apn->auth,"PAP"))
3005 cmd_auth = 1;
3006 else if(strstr(apn->auth,"CHAP"))
3007 cmd_auth = 2;
3008 else if(strstr(apn->auth,"PAP AND CHAP"))
3009 cmd_auth = 3;
3010 else
3011 goto exit;
3012
3013 sprintf(cmd,"AT*CGDFAUTH=1,%d,%s,%s",cmd_auth,apn->user,apn->pass);
3014
3015 err = at_send_command(cmd, &response);
3016 if (err < 0 || response->success == 0){
3017 *cme_err = at_get_cme_error(response);
3018 goto exit;
3019 }
3020 }
3021 else //2/3G
3022 {
3023 memset(cmd,0,400);
3024 index = 0;
3025 index += sprintf(cmd, "AT+CGDCONT=%d,", apn->cid);
3026 switch(apn->ip_type) {
3027 case MBTK_IP_TYPE_IP: {
3028 index += sprintf(cmd + index,"\"IP\",");
3029 break;
3030 }
3031 case MBTK_IP_TYPE_IPV6: {
3032 index += sprintf(cmd + index,"\"IPV6\",");
3033 break;
3034 }
3035 case MBTK_IP_TYPE_IPV4V6: {
3036 index += sprintf(cmd + index,"\"IPV4V6\",");
3037 break;
3038 }
3039 default: {
3040 index += sprintf(cmd + index,"\"PPP\",");
3041 break;
3042 }
3043 }
3044 index += sprintf(cmd + index, "\"%s\"", apn->apn);
3045
3046 err = at_send_command(cmd, &response);
3047 if (err < 0 || response->success == 0){
3048 *cme_err = at_get_cme_error(response);
3049 goto exit;
3050 }
3051 at_response_free(response);
3052
3053 memset(cmd,0,400);
3054 int cmd_auth=0;
3055 if(strstr(apn->auth,"NONE"))
3056 cmd_auth = 0;
3057 else if(strstr(apn->auth,"PAP"))
3058 cmd_auth = 1;
3059 else if(strstr(apn->auth,"CHAP"))
3060 cmd_auth = 2;
3061 else if(strstr(apn->auth,"PAP AND CHAP"))
3062 cmd_auth = 3;
3063 else
3064 goto exit;
3065
3066 sprintf(cmd, "AT*AUTHREQ=%d,%d,%s,%s",apn->cid,cmd_auth,apn->user,apn->pass);
3067 err = at_send_command(cmd, &response);
3068 if (err < 0 || response->success == 0){
3069 *cme_err = at_get_cme_error(response);
3070 goto exit;
3071 }
3072 }
3073
3074exit:
3075 at_response_free(response);
3076 return err;
3077}
3078#endif
3079
3080/*
3081AT+CGDCONT=1,"IPV4V6","cmnet"
3082OK
3083
3084AT*CGDFLT=1,"IPv4v6","reliance.grevpdn.zj",,,,,,,,,,,,,,,,,,1
3085OK
3086
3087*/
3088static int req_apn_set(mbtk_apn_info_t *apn, int *cme_err)
3089{
3090 ATResponse *response = NULL;
3091 char cmd[400] = {0};
3092 int index = 0;
3093 int err = 0;
3094
3095 index += sprintf(cmd, "AT+CGDCONT=%d,", apn->cid);
3096 switch(apn->ip_type) {
3097 case MBTK_IP_TYPE_IP: {
3098 index += sprintf(cmd + index,"\"IP\",");
3099 break;
3100 }
3101 case MBTK_IP_TYPE_IPV6: {
3102 index += sprintf(cmd + index,"\"IPV6\",");
3103 break;
3104 }
3105 case MBTK_IP_TYPE_IPV4V6: {
3106 index += sprintf(cmd + index,"\"IPV4V6\",");
3107 break;
3108 }
3109 default: {
3110 index += sprintf(cmd + index,"\"PPP\",");
3111 break;
3112 }
3113 }
3114 if(strlen(apn->apn) > 0) {
3115 index += sprintf(cmd + index,"\"%s\"", apn->apn);
3116 } else {
3117 LOGE("No set APN.");
3118 err = -1;
3119 goto exit;
3120 }
3121
3122 err = at_send_command(cmd, &response);
3123 if (err < 0 || response->success == 0){
3124 if(cme_err) {
3125 *cme_err = at_get_cme_error(response);
3126 }
3127 goto exit;
3128 }
3129
3130 if(!str_empty(apn->user) || !str_empty(apn->pass)) {
3131 at_response_free(response);
3132
3133 memset(cmd,0,400);
3134 int cmd_auth=0;
3135 if(strstr(apn->auth,"NONE"))
3136 cmd_auth = 0;
3137 else if(strstr(apn->auth,"PAP"))
3138 cmd_auth = 1;
3139 else if(strstr(apn->auth,"CHAP"))
3140 cmd_auth = 2;
3141#if 0
3142 else if(strstr(apn->auth,"PAP AND CHAP"))
3143 cmd_auth = 3;
3144#endif
3145 else
3146 goto exit;
3147
3148 sprintf(cmd, "AT*AUTHREQ=%d,%d,%s,%s",apn->cid,cmd_auth,apn->user,apn->pass);
3149 err = at_send_command(cmd, &response);
3150 if (err < 0 || response->success == 0){
3151 *cme_err = at_get_cme_error(response);
3152 goto exit;
3153 }
3154 }
3155
3156exit:
3157 at_response_free(response);
3158 return err;
3159}
3160
3161int wait_cgact_complete(int timeout)
3162{
3163 int count = timeout * 10; // timeout * 1000 / 100
3164 int i = 0;
3165
3166 while(cgact_wait.waitting && i < count) {
3167 i++;
3168 usleep(100000); // 100ms
3169 }
3170
3171 if(i == count) { // Timeout
3172 return -1;
3173 } else {
3174 return 0;
3175 }
3176}
3177
3178/*
3179AT+CGDATA="",6
3180CONNECT
3181
3182OK
3183
3184AT+CFUN=1
3185
3186OK
3187
3188*/
3189static int req_data_call_user_start(int cid, int *cme_err)
3190{
3191 ATResponse *response = NULL;
3192 char cmd[400] = {0};
3193 int index = 0;
3194 int err = 0;
3195
3196 err = at_send_command_singleline("AT+COPS?", "+COPS:", &response);
3197 if (err < 0 || response->success == 0 || !response->p_intermediates){
3198 if(cme_err != NULL)
3199 *cme_err = at_get_cme_error(response);
3200 err = -1;
3201 goto exit;
3202 }
3203
3204 int tmp_int;
3205 char cmd_buf[64];
3206 char *line = response->p_intermediates->line;
3207 err = at_tok_start(&line);
3208 if (err < 0)
3209 {
3210 goto exit;
3211 }
3212 err = at_tok_nextint(&line, &tmp_int);
3213 if (err < 0)
3214 {
3215 goto exit;
3216 }
3217 err = at_tok_nextint(&line, &tmp_int);
3218 if (err < 0)
3219 {
3220 goto exit;
3221 }
3222 err = at_tok_nextstr(&line, &cmd_buf);
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 at_response_free(response);
3233
3234 if(tmp_int == 7 && cid == 1) //LTE && cid = 1
3235 {
3236 ATResponse *response = NULL;
3237 char cmd[400] = {0};
3238 int err = 0;
3239
3240 err = at_send_command("AT+CFUN=1", &response);
3241 if (err < 0 || response->success == 0){
3242 if(cme_err) {
3243 *cme_err = at_get_cme_error(response);
3244 }
3245 goto exit;
3246 }
3247 }
3248 else
3249 {
3250 ATResponse *response = NULL;
3251 char cmd[400] = {0};
3252 int err = 0;
3253 sprintf(cmd, "AT+CGDATA=\"\",%d", cid);
3254 err = at_send_command(cmd, &response);
3255 if (err < 0 || response->success == 0){
3256 if(cme_err) {
3257 *cme_err = at_get_cme_error(response);
3258 }
3259 goto exit;
3260 }
3261 }
3262
3263exit:
3264 at_response_free(response);
3265 return err;
3266}
3267
3268/*
3269AT+CGACT?
3270+CGACT: 1,1
3271+CGACT: 8,1
3272OK
3273
3274AT+CGACT=1,<cid>
3275OK
3276
3277*/
3278static int req_data_call_start(int cid, int *cme_err)
3279{
3280 ATResponse *response = NULL;
3281 char cmd[400] = {0};
3282 int err = 0;
3283#if 0
3284 err = at_send_command_multiline("AT+CGACT?", "+CGACT:", &response);
3285 if (err < 0 || response->success == 0 || !response->p_intermediates){
3286 *cme_err = at_get_cme_error(response);
3287 goto exit;
3288 }
3289 ATLine* lines_ptr = response->p_intermediates;
3290 char *line = NULL;
3291 int tmp_int;
3292 while(lines_ptr)
3293 {
3294 line = lines_ptr->line;
3295 err = at_tok_start(&line);
3296 if (err < 0)
3297 {
3298 goto exit;
3299 }
3300
3301 err = at_tok_nextint(&line, &tmp_int); // cid
3302 if (err < 0)
3303 {
3304 goto exit;
3305 }
3306 if(tmp_int == cid) { // Found cid
3307 err = at_tok_nextint(&line, &tmp_int); // cid
3308 if (err < 0)
3309 {
3310 goto exit;
3311 }
3312 if(tmp_int == 1) { // This cid has active.
3313 goto net_config;
3314 } else {
3315 goto cid_active;
3316 }
3317 break;
3318 }
3319
3320 lines_ptr = lines_ptr->p_next;
3321 }
3322
3323 if(lines_ptr == NULL) { // No found this cid.
3324 LOGE("No found cid : %d", cid);
3325 goto exit;
3326 }
3327 at_response_free(response);
3328
3329 // Start active cid.
3330cid_active:
3331#endif
3332
3333 sprintf(cmd, "AT+CGACT=1,%d", cid);
3334 err = at_send_command(cmd, &response);
3335 if (err < 0 || response->success == 0){
3336 if(cme_err) {
3337 *cme_err = at_get_cme_error(response);
3338 }
3339 goto exit;
3340 }
3341
3342exit:
3343 at_response_free(response);
3344 return err;
3345}
3346
3347/*
3348AT+CGACT=0,<cid>
3349OK
3350
3351*/
3352static int req_data_call_stop(int cid, int *cme_err)
3353{
3354 ATResponse *response = NULL;
3355 char cmd[400] = {0};
3356 int err = 0;
3357#if 0
3358 err = at_send_command_multiline("AT+CGACT?", "+CGACT:", &response);
3359 if (err < 0 || response->success == 0 || !response->p_intermediates){
3360 *cme_err = at_get_cme_error(response);
3361 goto exit;
3362 }
3363 ATLine* lines_ptr = response->p_intermediates;
3364 char *line = NULL;
3365 int tmp_int;
3366 while(lines_ptr)
3367 {
3368 line = lines_ptr->line;
3369 err = at_tok_start(&line);
3370 if (err < 0)
3371 {
3372 goto exit;
3373 }
3374
3375 err = at_tok_nextint(&line, &tmp_int); // cid
3376 if (err < 0)
3377 {
3378 goto exit;
3379 }
3380 if(tmp_int == cid) { // Found cid
3381 err = at_tok_nextint(&line, &tmp_int); // cid
3382 if (err < 0)
3383 {
3384 goto exit;
3385 }
3386 if(tmp_int == 1) { // This cid has active.
3387 goto net_config;
3388 } else {
3389 goto cid_active;
3390 }
3391 break;
3392 }
3393
3394 lines_ptr = lines_ptr->p_next;
3395 }
3396
3397 if(lines_ptr == NULL) { // No found this cid.
3398 LOGE("No found cid : %d", cid);
3399 goto exit;
3400 }
3401 at_response_free(response);
3402
3403 // Start active cid.
3404cid_active:
3405#endif
3406
3407 sprintf(cmd, "AT+CGACT=0,%d", cid);
3408 err = at_send_command(cmd, &response);
3409 if (err < 0 || response->success == 0){
3410 *cme_err = at_get_cme_error(response);
3411 goto exit;
3412 }
3413
3414exit:
3415 at_response_free(response);
3416 return err;
3417}
3418
3419/*
3420IPv4 : 10.255.74.26
3421IPv6 : 254.128.0.0.0.0.0.0.0.1.0.2.144.5.212.239
3422*/
3423static bool is_ipv4(const char *ip)
3424{
3425 const char *ptr = ip;
3426 int count = 0;
3427 while(*ptr) {
3428 if(*ptr == '.')
3429 count++;
3430 ptr++;
3431 }
3432
3433 if(count == 3) {
3434 return true;
3435 } else {
3436 return false;
3437 }
3438}
3439
3440/*
3441AT+CGCONTRDP=1
3442+CGCONTRDP: 1,7,"cmnet-2.MNC000.MCC460.GPRS","10.255.74.26","","223.87.253.100","223.87.253.253","","",0,0
3443+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
3444
3445OK
3446
3447*/
3448static int req_data_call_state_get(int cid, mbtk_ipv4_info_t *ipv4, mbtk_ipv6_info_t *ipv6, int *cme_err)
3449{
3450 ATResponse *response = NULL;
3451 char cmd[50] = {0};
3452 int err = 0;
3453
3454 sprintf(cmd, "AT+CGCONTRDP=%d", cid);
3455
3456 err = at_send_command_multiline(cmd, "+CGCONTRDP:", &response);
3457 if (err < 0 || response->success == 0 || !response->p_intermediates){
3458 *cme_err = at_get_cme_error(response);
3459 goto exit;
3460 }
3461 ATLine* lines_ptr = response->p_intermediates;
3462 char *line = NULL;
3463 int tmp_int;
3464 char *tmp_ptr = NULL;
3465 while(lines_ptr)
3466 {
3467 line = lines_ptr->line;
3468 err = at_tok_start(&line);
3469 if (err < 0)
3470 {
3471 goto exit;
3472 }
3473
3474 err = at_tok_nextint(&line, &tmp_int); // cid
3475 if (err < 0)
3476 {
3477 goto exit;
3478 }
3479 err = at_tok_nextint(&line, &tmp_int); // bearer_id
3480 if (err < 0)
3481 {
3482 goto exit;
3483 }
3484 err = at_tok_nextstr(&line, &tmp_ptr); // APN
3485 if (err < 0)
3486 {
3487 goto exit;
3488 }
3489
3490 err = at_tok_nextstr(&line, &tmp_ptr); // IP
3491 if (err < 0 || str_empty(tmp_ptr))
3492 {
3493 goto exit;
3494 }
3495 if(is_ipv4(tmp_ptr)) {
3496 if(inet_pton(AF_INET, tmp_ptr, &(ipv4->IPAddr)) < 0) {
3497 LOGE("inet_pton() fail.");
3498 err = -1;
3499 goto exit;
3500 }
3501
3502 ipv4->valid = true;
3503 //log_hex("IPv4", &(ipv4->IPAddr), sizeof(struct in_addr));
3504 } else {
3505 if(str_2_ipv6(tmp_ptr, &(ipv6->IPV6Addr))) {
3506 LOGE("str_2_ipv6() fail.");
3507 err = -1;
3508 goto exit;
3509 }
3510
3511 ipv6->valid = true;
3512 //log_hex("IPv6", &(ipv6->IPV6Addr), 16);
3513 }
3514
3515 err = at_tok_nextstr(&line, &tmp_ptr); // Gateway
3516 if (err < 0)
3517 {
3518 goto exit;
3519 }
3520 if(!str_empty(tmp_ptr)) { // No found gateway
3521 if(is_ipv4(tmp_ptr)) {
3522 if(inet_pton(AF_INET, tmp_ptr, &(ipv4->GateWay)) < 0) {
3523 LOGE("inet_pton() fail.");
3524 err = -1;
3525 goto exit;
3526 }
3527
3528 //log_hex("IPv4", &(ipv4->GateWay), sizeof(struct in_addr));
3529 } else {
3530 if(str_2_ipv6(tmp_ptr, &(ipv6->GateWay))) {
3531 LOGE("str_2_ipv6() fail.");
3532 err = -1;
3533 goto exit;
3534 }
3535
3536 //log_hex("IPv6", &(ipv6->GateWay), 16);
3537 }
3538 }
3539
3540 err = at_tok_nextstr(&line, &tmp_ptr); // prim_DNS
3541 if (err < 0)
3542 {
3543 goto exit;
3544 }
3545 if(!str_empty(tmp_ptr)) { // No found Primary DNS
3546 if(is_ipv4(tmp_ptr)) {
3547 if(inet_pton(AF_INET, tmp_ptr, &(ipv4->PrimaryDNS)) < 0) {
3548 LOGE("inet_pton() fail.");
3549 err = -1;
3550 goto exit;
3551 }
3552
3553 //log_hex("IPv4", &(ipv4->PrimaryDNS), sizeof(struct in_addr));
3554 } else {
3555 if(str_2_ipv6(tmp_ptr, &(ipv6->PrimaryDNS))) {
3556 LOGE("str_2_ipv6() fail.");
3557 err = -1;
3558 goto exit;
3559 }
3560
3561 //log_hex("IPv6", &(ipv6->PrimaryDNS), 16);
3562 }
3563 }
3564
3565 err = at_tok_nextstr(&line, &tmp_ptr); // sec_DNS
3566 if (err < 0)
3567 {
3568 goto exit;
3569 }
3570 if(!str_empty(tmp_ptr)) { // No found Secondary DNS
3571 if(is_ipv4(tmp_ptr)) {
3572 if(inet_pton(AF_INET, tmp_ptr, &(ipv4->SecondaryDNS)) < 0) {
3573 LOGE("inet_pton() fail.");
3574 err = -1;
3575 goto exit;
3576 }
3577
3578 //log_hex("IPv4", &(ipv4->SecondaryDNS), sizeof(struct in_addr));
3579 } else {
3580 if(str_2_ipv6(tmp_ptr, &(ipv6->SecondaryDNS))) {
3581 LOGE("str_2_ipv6() fail.");
3582 err = -1;
3583 goto exit;
3584 }
3585
3586 //log_hex("IPv6", &(ipv6->SecondaryDNS), 16);
3587 }
3588 }
3589
3590 lines_ptr = lines_ptr->p_next;
3591 }
3592
3593exit:
3594 at_response_free(response);
3595 return err;
3596}
3597
3598/*
3599AT+CGCONTRDP
3600+CGCONTRDP: 1,5,"cmnet.MNC000.MCC460.GPRS","10.156.238.86","","223.87.253.100","223.87.253.253","","",0,0
3601+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
3602+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
3603OK
3604
3605*/
3606static int apn_state_get(list_node_t **apn_list)
3607{
3608 ATResponse *response = NULL;
3609 int err = 0;
3610 *apn_list = list_create(NULL);
3611 if(*apn_list == NULL)
3612 {
3613 LOG("list_create() fail.");
3614 return -1;
3615 }
3616
3617 err = at_send_command_multiline("AT+CGCONTRDP", "+CGCONTRDP:", &response);
3618 if (err < 0 || response->success == 0 || !response->p_intermediates){
3619 goto exit;
3620 }
3621 ATLine* lines_ptr = response->p_intermediates;
3622 char *line = NULL;
3623 int tmp_int;
3624 char *tmp_ptr = NULL;
3625 int cid_current = 0;
3626 info_apn_ip_t *apn = NULL;
3627 while(lines_ptr)
3628 {
3629 line = lines_ptr->line;
3630 err = at_tok_start(&line);
3631 if (err < 0)
3632 {
3633 goto exit;
3634 }
3635
3636 err = at_tok_nextint(&line, &tmp_int); // cid
3637 if (err < 0)
3638 {
3639 goto exit;
3640 }
3641
3642 if(tmp_int != 1 && tmp_int != 8) { // Not process cid 1 and 8.
3643 if(cid_current != tmp_int) { // New cid.
3644 apn = (info_apn_ip_t*)malloc(sizeof(info_apn_ip_t));
3645 if(apn == NULL) {
3646 goto exit;
3647 }
3648 memset(apn, 0, sizeof(info_apn_ip_t));
3649 apn->cid = tmp_int;
3650 cid_current = tmp_int;
3651
3652 list_add(*apn_list, apn);
3653 }
3654 err = at_tok_nextint(&line, &tmp_int); // bearer_id
3655 if (err < 0)
3656 {
3657 goto exit;
3658 }
3659 err = at_tok_nextstr(&line, &tmp_ptr); // APN
3660 if (err < 0)
3661 {
3662 goto exit;
3663 }
3664
3665 err = at_tok_nextstr(&line, &tmp_ptr); // IP
3666 if (err < 0 || str_empty(tmp_ptr))
3667 {
3668 goto exit;
3669 }
3670 if(is_ipv4(tmp_ptr)) {
3671 apn->ipv4_valid = true;
3672 memcpy(apn->ipv4, tmp_ptr, strlen(tmp_ptr));
3673 } else {
3674 apn->ipv6_valid = true;
3675 uint8 tmp_ipv6[16];
3676 if(str_2_ipv6(tmp_ptr, tmp_ipv6)) {
3677 LOGE("str_2_ipv6() fail.");
3678 err = -1;
3679 goto exit;
3680 }
3681
3682 if(inet_ntop(AF_INET6, tmp_ipv6, apn->ipv6, 50) == NULL) {
3683 err = -1;
3684 LOGE("inet_ntop ipv6 ip fail.");
3685 goto exit;
3686 }
3687 }
3688 }
3689
3690 lines_ptr = lines_ptr->p_next;
3691 }
3692
3693exit:
3694 at_response_free(response);
3695 return err;
3696}
3697
3698mbtk_info_err_enum call_pack_req_process(sock_client_info_t* cli_info, mbtk_info_pack_t* pack);
3699mbtk_info_err_enum sms_pack_req_process(sock_client_info_t* cli_info, mbtk_info_pack_t* pack);
3700mbtk_info_err_enum pb_pack_req_process(sock_client_info_t* cli_info, mbtk_info_pack_t* pack);
3701//mbtk wyq for data_call_ex add start
3702void data_call_bootconn_save(int cid, int bootconn);
3703//mbtk wyq for data_call_ex add end
3704
3705//void net_list_free(void *data);
3706// Return MBTK_INFO_ERR_SUCCESS,will call pack_error_send() to send RSP.
3707// Otherwise, do not call pack_error_send().
3708static mbtk_info_err_enum pack_req_process(sock_client_info_t* cli_info, mbtk_info_pack_t* pack)
3709{
3710 if(pack->info_id > MBTK_INFO_ID_CALL_BEGIN && pack->info_id < MBTK_INFO_ID_CALL_END) {
3711 return call_pack_req_process(cli_info, pack);
3712 } else if(pack->info_id > MBTK_INFO_ID_SMS_BEGIN && pack->info_id < MBTK_INFO_ID_SMS_END) {
3713 return sms_pack_req_process(cli_info, pack);
3714 } else if(pack->info_id > MBTK_INFO_ID_PB_BEGIN && pack->info_id < MBTK_INFO_ID_PB_END) {
3715 return pb_pack_req_process(cli_info, pack);
3716 } else {
3717 mbtk_info_err_enum err = MBTK_INFO_ERR_SUCCESS;
3718 int cme_err = MBTK_INFO_ERR_CME_NON;
3719 switch(pack->info_id)
3720 {
3721 case MBTK_INFO_ID_DEV_IMEI_REQ: // <string> IMEI
3722 {
3723 if(pack->data_len == 0 || pack->data == NULL) // Get IMEI
3724 {
3725 char imei[20] = {0};
3726 if(req_imei_get(imei, &cme_err) || strlen(imei) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
3727 {
3728 if(cme_err != MBTK_INFO_ERR_CME_NON) {
3729 err = MBTK_INFO_ERR_CME + cme_err;
3730 } else {
3731 err = MBTK_INFO_ERR_UNKNOWN;
3732 }
3733 LOG("Get IMEI fail.");
3734 }
3735 else
3736 {
3737 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_IMEI_RSP, imei, strlen(imei));
3738 }
3739 }
3740 else // Set IMEI(Unsupport).
3741 {
3742 err = MBTK_INFO_ERR_UNSUPPORTED;
3743 LOG("Unsupport set IMEI.");
3744 }
3745 break;
3746 }
3747 case MBTK_INFO_ID_DEV_SN_REQ: // <string> SN
3748 {
3749 if(pack->data_len == 0 || pack->data == NULL) // Get SN
3750 {
3751 char sn[20] = {0};
3752 if(req_sn_get(sn, &cme_err) || strlen(sn) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
3753 {
3754 if(cme_err != MBTK_INFO_ERR_CME_NON) {
3755 err = MBTK_INFO_ERR_CME + cme_err;
3756 } else {
3757 err = MBTK_INFO_ERR_UNKNOWN;
3758 }
3759 LOG("Get SN fail.");
3760 }
3761 else
3762 {
3763 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_SN_RSP, sn, strlen(sn));
3764 }
3765 }
3766 else // Set SN(Unsupport).
3767 {
3768 err = MBTK_INFO_ERR_UNSUPPORTED;
3769 LOG("Unsupport set SN.");
3770 }
3771 break;
3772 }
3773 case MBTK_INFO_ID_DEV_MEID_REQ: // <string> MEID (Only for CDMA)
3774 {
3775 if(pack->data_len == 0 || pack->data == NULL) // Get MEID
3776 {
3777 err = MBTK_INFO_ERR_UNSUPPORTED;
3778 LOG("Support only for CDMA.");
3779 }
3780 else // Set MEID(Unsupport).
3781 {
3782 err = MBTK_INFO_ERR_UNSUPPORTED;
3783 LOG("Unsupport set MEID.");
3784 }
3785 break;
3786 }
3787 case MBTK_INFO_ID_DEV_VERSION_REQ: // <string> VERSION
3788 {
3789 if(pack->data_len == 0 || pack->data == NULL) // Get VERSION
3790 {
3791 char version[50] = {0};
3792 if(req_version_get(version, &cme_err) || strlen(version) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
3793 {
3794 if(cme_err != MBTK_INFO_ERR_CME_NON) {
3795 err = MBTK_INFO_ERR_CME + cme_err;
3796 } else {
3797 err = MBTK_INFO_ERR_UNKNOWN;
3798 }
3799 LOG("Get Version fail.");
3800 }
3801 else
3802 {
3803 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_VERSION_RSP, version, strlen(version));
3804 }
3805 }
3806 else // Set VERSION(Unsupport).
3807 {
3808 err = MBTK_INFO_ERR_UNSUPPORTED;
3809 LOG("Unsupport set VERSION.");
3810 }
3811 break;
3812 }
3813 case MBTK_INFO_ID_DEV_MODEL_REQ: //MODEL
3814 {
3815 if(pack->data_len == 0 || pack->data == NULL) // Get MODEL
3816 {
3817 char model[50] = {0};
3818 if(req_model_get(model, &cme_err) || strlen(model) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
3819 {
3820 if(cme_err != MBTK_INFO_ERR_CME_NON) {
3821 err = MBTK_INFO_ERR_CME + cme_err;
3822 } else {
3823 err = MBTK_INFO_ERR_UNKNOWN;
3824 }
3825 LOG("Get model fail.");
3826 }
3827 else
3828 {
3829 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_VERSION_RSP, model, strlen(model));
3830 }
3831 }
3832 else // Set model(Unsupport).
3833 {
3834 err = MBTK_INFO_ERR_UNSUPPORTED;
3835 LOG("Unsupport set model.");
3836 }
3837 break;
3838 }
3839 case MBTK_INFO_ID_DEV_MODEM_REQ: //MODEM
3840 {
3841 if(pack->data_len == 0 || pack->data == NULL) // Get MODEM
3842 {
3843 int modem = -1;
3844 if(req_modem_get(&modem, &cme_err) || modem < 0 || cme_err != MBTK_INFO_ERR_CME_NON)
3845 {
3846 if(cme_err != MBTK_INFO_ERR_CME_NON) {
3847 err = MBTK_INFO_ERR_CME + cme_err;
3848 } else {
3849 err = MBTK_INFO_ERR_UNKNOWN;
3850 }
3851 LOG("Get modem fail.");
3852 }
3853 else
3854 {
3855 uint8 modem_type = (uint8)modem;
3856 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_MODEM_RSP, &modem_type, sizeof(uint8));
3857 }
3858 }
3859 else // Set modem
3860 {
3861 mbtk_modem_info_t *modem = (mbtk_modem_info_t *)pack->data;
3862 if(pack->data_len != sizeof(mbtk_modem_info_t))
3863 {
3864 err = MBTK_INFO_ERR_REQ_PARAMETER;
3865 LOG("Set modem error.");
3866 break;
3867 }
3868 if(req_modem_set(modem, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
3869 {
3870 LOG("Set modem fail.");
3871 err = MBTK_INFO_ERR_FORMAT;
3872 } else {
3873 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_MODEM_RSP, NULL, 0);
3874 }
3875 }
3876 break;
3877 }
3878 case MBTK_INFO_ID_DEV_TIME_REQ: // <uint8><string> YYYY-MM-DD-HH:MM:SS
3879 {
3880 if(pack->data_len == 0 || pack->data == NULL) // Get Time
3881 {
3882 int type = -1;
3883 if(req_time_get(&type, &cme_err) || type < 0 || cme_err != MBTK_INFO_ERR_CME_NON)
3884 {
3885 if(cme_err != MBTK_INFO_ERR_CME_NON) {
3886 err = MBTK_INFO_ERR_CME + cme_err;
3887 } else {
3888 err = MBTK_INFO_ERR_UNKNOWN;
3889 }
3890 LOG("Get Time fail.");
3891 }
3892 else
3893 {
3894 uint8 time_type = (uint8)type;
3895 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_TIME_RSP, &time_type, sizeof(uint8));
3896 }
3897 }
3898 else // Set Time
3899 {
3900 if(pack->data_len == sizeof(uint8)) {
3901 if(req_time_set(*(pack->data), NULL, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
3902 {
3903 LOG("Set Time fail.");
3904 err = MBTK_INFO_ERR_FORMAT;
3905 } else {
3906 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_TIME_RSP, NULL, 0);
3907 }
3908 } else {
3909 char time_ptr[100] = {0};
3910 memcpy(time_ptr, pack->data + sizeof(uint8), pack->data_len - sizeof(uint8));
3911 if(req_time_set(*(pack->data), time_ptr, &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 }
3919 }
3920 break;
3921 }
3922 case MBTK_INFO_ID_NET_TIME_REQ: // <string> YYYY-MM-DD-HH:MM:SS
3923 {
3924 if(pack->data_len == 0 || pack->data == NULL) // Get Time
3925 {
3926 char time[100];
3927 if(req_net_time_get(time, &cme_err) || strlen(time) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
3928 {
3929 if(cme_err != MBTK_INFO_ERR_CME_NON) {
3930 err = MBTK_INFO_ERR_CME + cme_err;
3931 } else {
3932 err = MBTK_INFO_ERR_UNKNOWN;
3933 }
3934 LOG("Get Time fail.");
3935 }
3936 else
3937 {
3938 char time_ser[100]={0};
3939 memcpy(time_ser,time,strlen(time));
3940 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_TIME_RSP, time_ser, strlen(time_ser));
3941 }
3942 }
3943 else // Set Time
3944 {
3945 err = MBTK_INFO_ERR_UNSUPPORTED;
3946 LOG("Unsupport set TIME.");
3947 }
3948 break;
3949 }
3950 case MBTK_INFO_ID_DEV_VOLTE_REQ: // <uint8> 0:Close 1:Open
3951 {
3952 if(pack->data_len == 0 || pack->data == NULL) // Get VoLTE state.
3953 {
3954 int state;
3955 if(req_volte_get(&state, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
3956 {
3957 if(cme_err != MBTK_INFO_ERR_CME_NON) {
3958 err = MBTK_INFO_ERR_CME + cme_err;
3959 } else {
3960 err = MBTK_INFO_ERR_UNKNOWN;
3961 }
3962 LOG("Get VoLTE state fail.");
3963 }
3964 else
3965 {
3966 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_VOLTE_RSP, &state, sizeof(uint8));
3967 }
3968 }
3969 else // Set VoLTE state.
3970 {
3971 uint8 on = *(pack->data);
3972 if(pack->data_len != sizeof(uint8) || (on != 0 && on != 1))
3973 {
3974 err = MBTK_INFO_ERR_REQ_PARAMETER;
3975 LOG("Set VOLTE parameter error.");
3976 break;
3977 }
3978
3979 if(req_volte_set(on, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
3980 {
3981 if(cme_err != MBTK_INFO_ERR_CME_NON) {
3982 err = MBTK_INFO_ERR_CME + cme_err;
3983 } else {
3984 err = MBTK_INFO_ERR_UNKNOWN;
3985 }
3986 LOG("Set VoLTE state fail.");
3987 }
3988 else
3989 {
3990 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_VOLTE_RSP, NULL, 0);
3991
3992 // Restart is required to take effect.
3993 LOG("Will reboot system...");
3994 }
3995 }
3996 break;
3997 }
3998 case MBTK_INFO_ID_DEV_TEMP_REQ: // <string> Temperature
3999 {
4000 if(pack->data_len == sizeof(uint8) && pack->data) {
4001 int temp;
4002 if(req_temp_get(*(pack->data), &temp, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4003 {
4004 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4005 err = MBTK_INFO_ERR_CME + cme_err;
4006 } else {
4007 err = MBTK_INFO_ERR_UNKNOWN;
4008 }
4009 LOG("Get temperature fail.");
4010 }
4011 else
4012 {
4013 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_TEMP_RSP, &temp, sizeof(uint8));
4014 }
4015 } else {
4016 err = MBTK_INFO_ERR_FORMAT;
4017 LOG("Unsupport get Temperature.");
4018 }
4019 break;
4020 }
4021 case MBTK_INFO_ID_SIM_PLMN_REQ: // plmn
4022 {
4023 if(pack->data_len == 0 || pack->data == NULL) // plmn
4024 {
4025 mbtk_plmn_info plmn;
4026 if(req_plmn_get(&plmn, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4027 {
4028 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4029 err = MBTK_INFO_ERR_CME + cme_err;
4030 } else {
4031 err = MBTK_INFO_ERR_UNKNOWN;
4032 }
4033 LOG("Get PLMN fail.");
4034 }
4035 else
4036 {
4037 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_PLMN_RSP, &plmn, sizeof(mbtk_plmn_info));
4038 }
4039 }
4040 else // Set
4041 {
4042 err = MBTK_INFO_ERR_UNSUPPORTED;
4043 LOG("Set sim state fail.");
4044 }
4045 break;
4046 }
4047 case MBTK_INFO_ID_SIM_STATE_REQ: // mbtk_sim_state_enum
4048 {
4049 if(pack->data_len == 0 || pack->data == NULL) // Get sim state.
4050 {
4051 uint8 sim_state = (uint8)getSIMStatus();
4052 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_STATE_RSP, &sim_state, sizeof(uint8));
4053 }
4054 else // Set
4055 {
4056 err = MBTK_INFO_ERR_UNSUPPORTED;
4057 LOG("Set sim state fail.");
4058 }
4059 break;
4060 }
4061 case MBTK_INFO_ID_SIM_STYPE_REQ: // mbtk_sim_card_type_enum
4062 {
4063 if(pack->data_len == 0 || pack->data == NULL) // Get sim card type
4064 {
4065 uint8 sim_card_type;
4066 if(req_sim_card_type_get(&sim_card_type, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4067 {
4068 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4069 err = MBTK_INFO_ERR_CME + cme_err;
4070 } else {
4071 err = MBTK_INFO_ERR_UNKNOWN;
4072 }
4073 LOG("Get IMSI fail.");
4074 }
4075 else
4076 {
4077 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_STYPE_RSP, &sim_card_type, sizeof(uint8));
4078 }
4079 }
4080 else // Set
4081 {
4082 err = MBTK_INFO_ERR_UNSUPPORTED;
4083 LOG("Set sim state fail.");
4084 }
4085 break;
4086 }
4087 case MBTK_INFO_ID_SIM_PINPUK_TIMES_REQ: // mbtk_pin_puk_last_times
4088 {
4089 if(pack->data_len == 0 || pack->data == NULL) // Get sim card type
4090 {
4091 mbtk_pin_puk_last_times pin_puk_last_times;
4092 if(req_pin_puk_last_times_get(&pin_puk_last_times, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4093 {
4094 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4095 err = MBTK_INFO_ERR_CME + cme_err;
4096 } else {
4097 err = MBTK_INFO_ERR_UNKNOWN;
4098 }
4099 LOG("Get IMSI fail.");
4100 }
4101 else
4102 {
4103 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_PINPUK_TIMES_RSP, &pin_puk_last_times, sizeof(mbtk_pin_puk_last_times));
4104 }
4105 }
4106 else // Set
4107 {
4108 err = MBTK_INFO_ERR_UNSUPPORTED;
4109 LOG("Set sim state fail.");
4110 }
4111 break;
4112 }
4113 case MBTK_INFO_ID_SIM_ENABLE_PIN_REQ: // <string> PIN
4114 {
4115 if(pack->data_len == 0 || pack->data == NULL) // Enable PIN
4116 {
4117 err = MBTK_INFO_ERR_UNSUPPORTED;
4118 LOG("Unsupport GET PIN.");
4119 }
4120 else // Enable PIN
4121 {
4122 mbtk_enable_pin_info *pin = NULL;
4123 pin = (mbtk_enable_pin_info *)pack->data;
4124 if(req_pin_enable(pin, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4125 {
4126 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4127 err = MBTK_INFO_ERR_CME + cme_err;
4128 } else {
4129 err = MBTK_INFO_ERR_UNKNOWN;
4130 }
4131 LOG("Get IMSI fail.");
4132 }
4133 else
4134 {
4135 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_ENABLE_PIN_RSP, NULL, 0);
4136 }
4137 }
4138 break;
4139 }
4140 case MBTK_INFO_ID_SIM_PIN_REQ: // <string> PIN
4141 {
4142 if(pack->data_len == 0 || pack->data == NULL) // PIN
4143 {
4144 err = MBTK_INFO_ERR_UNSUPPORTED;
4145 LOG("Unsupport GET PIN.");
4146 }
4147 else // Set PIN
4148 {
4149 char pin[16] = {0};
4150 memcpy(pin, pack->data, pack->data_len);
4151 if(req_pin_verify(pin, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4152 {
4153 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4154 err = MBTK_INFO_ERR_CME + cme_err;
4155 } else {
4156 err = MBTK_INFO_ERR_UNKNOWN;
4157 }
4158 LOG("Set PIN fail.");
4159 }
4160 else
4161 {
4162 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_PIN_RSP, NULL, 0);
4163 }
4164 }
4165 break;
4166 }
4167 case MBTK_INFO_ID_SIM_PUK_REQ: // <string> PUK
4168 {
4169 if(pack->data_len == 0 || pack->data == NULL)
4170 {
4171 err = MBTK_INFO_ERR_UNSUPPORTED;
4172 LOG("Unsupport.");
4173 }
4174 else // change PIN
4175 {
4176 mbtk_unlock_pin_info *pin_info = NULL;
4177 pin_info = (mbtk_unlock_pin_info *)pack->data;
4178 if(req_puk_unlock_pin(pin_info, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4179 {
4180 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4181 err = MBTK_INFO_ERR_CME + cme_err;
4182 } else {
4183 err = MBTK_INFO_ERR_UNKNOWN;
4184 }
4185 LOG("Get IMSI fail.");
4186 }
4187 else
4188 {
4189 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_CHANGE_PIN_RSP, NULL, 0);
4190 }
4191 }
4192 break;
4193 }
4194 case MBTK_INFO_ID_SIM_CHANGE_PIN_REQ: // <string> <string> old_PIN new_PIN
4195 {
4196 if(pack->data_len == 0 || pack->data == NULL)
4197 {
4198 err = MBTK_INFO_ERR_UNSUPPORTED;
4199 LOG("Unsupport.");
4200 }
4201 else // change PIN
4202 {
4203 mbtk_change_pin_info *pin_info = NULL;
4204 pin_info = (mbtk_change_pin_info *)pack->data;
4205 if(req_pin_change(pin_info, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4206 {
4207 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4208 err = MBTK_INFO_ERR_CME + cme_err;
4209 } else {
4210 err = MBTK_INFO_ERR_UNKNOWN;
4211 }
4212 LOG("Get IMSI fail.");
4213 }
4214 else
4215 {
4216 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_CHANGE_PIN_RSP, NULL, 0);
4217 }
4218 }
4219 break;
4220 }
4221 case MBTK_INFO_ID_SIM_IMSI_REQ: // <string> IMSI
4222 {
4223 if(pack->data_len == 0 || pack->data == NULL) // Get IMSI
4224 {
4225 char imsi[20] = {0};
4226 if(req_imsi_get(imsi, &cme_err) || strlen(imsi) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4227 {
4228 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4229 err = MBTK_INFO_ERR_CME + cme_err;
4230 } else {
4231 err = MBTK_INFO_ERR_UNKNOWN;
4232 }
4233 LOG("Get IMSI fail.");
4234 }
4235 else
4236 {
4237 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_IMSI_RSP, imsi, strlen(imsi));
4238 }
4239 }
4240 else // Set IMSI(Unsupport).
4241 {
4242 err = MBTK_INFO_ERR_UNSUPPORTED;
4243 LOG("Unsupport set IMSI.");
4244 }
4245 break;
4246 }
4247 case MBTK_INFO_ID_SIM_ICCID_REQ: // <string> ICCID
4248 {
4249 if(pack->data_len == 0 || pack->data == NULL) // Get ICCID
4250 {
4251 //log_hex("pack", pack, sizeof(mbtk_info_pack_t));
4252 //sleep(1);
4253 char iccid[50] = {0};
4254 if(req_iccid_get(iccid, &cme_err) || strlen(iccid) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4255 {
4256 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4257 err = MBTK_INFO_ERR_CME + cme_err;
4258 } else {
4259 err = MBTK_INFO_ERR_UNKNOWN;
4260 }
4261 LOG("Get ICCID fail.");
4262 }
4263 else
4264 {
4265 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_ICCID_RSP, iccid, strlen(iccid));
4266 }
4267 }
4268 else // Set ICCID(Unsupport).
4269 {
4270 err = MBTK_INFO_ERR_UNSUPPORTED;
4271 LOG("Unsupport set ICCID.");
4272 }
4273 break;
4274 }
4275 case MBTK_INFO_ID_SIM_PN_REQ: // <string> Phone Number
4276 {
4277 if(pack->data_len == 0 || pack->data == NULL) // Get Phone Number
4278 {
4279 //log_hex("pack", pack, sizeof(mbtk_info_pack_t));
4280 //sleep(1);
4281 char phone_number[50] = {0};
4282 if(req_phone_number_get(phone_number, &cme_err) || strlen(phone_number) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4283 {
4284 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4285 err = MBTK_INFO_ERR_CME + cme_err;
4286 } else {
4287 err = MBTK_INFO_ERR_UNKNOWN;
4288 }
4289 LOG("Get Phone Number fail.");
4290 }
4291 else
4292 {
4293 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_PN_RSP, phone_number, strlen(phone_number));
4294 }
4295 }
4296 else // Set Phone Number(Unsupport).
4297 {
4298 err = MBTK_INFO_ERR_UNSUPPORTED;
4299 LOG("Unsupport set Phone Number.");
4300 }
4301 break;
4302 }
4303 case MBTK_INFO_ID_NET_SEL_MODE_REQ: // <mbtk_net_info_t> Operator
4304 {
4305 if(pack->data_len == 0 || pack->data == NULL) // Get
4306 {
4307 mbtk_net_info_t info;
4308 memset(&info, 0, sizeof(mbtk_net_info_t));
4309 if(req_net_sel_mode_get(&info, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4310 {
4311 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4312 err = MBTK_INFO_ERR_CME + cme_err;
4313 } else {
4314 err = MBTK_INFO_ERR_UNKNOWN;
4315 }
4316 LOG("Get Net select mode fail.");
4317 }
4318 else
4319 {
4320 LOG("NET : %d, %d, %d, %d", info.net_sel_mode, info.net_type, info.net_state, info.plmn);
4321 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_SEL_MODE_RSP, &info, sizeof(mbtk_net_info_t));
4322 }
4323 }
4324 else // Set
4325 {
4326 //LOG("1 pack-%p,data-%p,data_len-%d", pack, pack->data, pack->data_len);
4327 //log_hex("pack", pack, sizeof(mbtk_info_pack_t));
4328 //log_hex("data", pack->data, pack->data_len);
4329
4330 mbtk_net_info_t* info = (mbtk_net_info_t*)pack->data;//(mbtk_net_info_t*)mbtk_info_pack_data_get(pack, NULL);
4331 if(info == NULL) {
4332 err = MBTK_INFO_ERR_FORMAT;
4333 LOG("Get Net select mode fail.");
4334 } else {
4335 LOG("NET : %d, %d, %d", info->net_sel_mode, info->net_type, info->plmn);
4336 if(req_net_sel_mode_set(info, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON) {
4337 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4338 err = MBTK_INFO_ERR_CME + cme_err;
4339 } else {
4340 err = MBTK_INFO_ERR_UNKNOWN;
4341 }
4342 LOG("Get Net select mode fail.");
4343 } else {
4344 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_SEL_MODE_RSP, NULL, 0);
4345 }
4346 }
4347 }
4348 break;
4349 }
4350 case MBTK_INFO_ID_NET_AVAILABLE_REQ:// sel_mode(uint8)type(uint8)plmn(uint32)...sel_mode(uint8)type(uint8)plmn(uint32)
4351 {
4352 if(pack->data_len == 0 || pack->data == NULL) // Get Available Net.
4353 {
4354 int buffer_size;
4355 uint8 buffer[SOCK_MSG_LEN_MAX];
4356 memset(buffer, 0, SOCK_MSG_LEN_MAX);
4357 if((buffer_size = req_available_net_get(buffer, &cme_err)) <= 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4358 {
4359 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4360 err = MBTK_INFO_ERR_CME + cme_err;
4361 } else {
4362 err = MBTK_INFO_ERR_UNKNOWN;
4363 }
4364 LOG("Get Available Net fail.");
4365 }
4366 else
4367 {
4368 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_AVAILABLE_RSP, buffer, buffer_size);
4369 }
4370 }
4371 else // Set Available Net(Unsupport).
4372 {
4373 err = MBTK_INFO_ERR_UNSUPPORTED;
4374 LOG("Unsupport set available net.");
4375 }
4376 break;
4377 }
4378 case MBTK_INFO_ID_NET_BAND_REQ:
4379 {
4380 if(pack->data_len == 0 || pack->data == NULL)
4381 {
4382 err = MBTK_INFO_ERR_REQ_PARAMETER;
4383 LOG("No data found.");
4384 }
4385 else // Get support/current bands.
4386 {
4387 if(pack->data_len == sizeof(uint8)) {
4388 if(*(pack->data)) { // Get current bands.
4389 mbtk_band_info_t band;
4390 if(req_band_get(&band, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4391 {
4392 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4393 err = MBTK_INFO_ERR_CME + cme_err;
4394 } else {
4395 err = MBTK_INFO_ERR_UNKNOWN;
4396 }
4397 LOG("Get net band fail.");
4398 }
4399 else
4400 {
4401 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_BAND_RSP, &band, sizeof(mbtk_band_info_t));
4402 }
4403 } else { // Get support bands.
4404 band_support_get();
4405 if(band_support.net_pref != 0)
4406 {
4407 err = MBTK_INFO_ERR_UNKNOWN;
4408 LOG("Get support bands fail.");
4409 }
4410 else
4411 {
4412 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_BAND_RSP, &band_support, sizeof(mbtk_band_info_t));
4413 }
4414 }
4415 } else { // Set current bands.
4416 mbtk_band_info_t* band = (mbtk_band_info_t*)pack->data;
4417 if(pack->data_len != sizeof(mbtk_band_info_t))
4418 {
4419 err = MBTK_INFO_ERR_REQ_PARAMETER;
4420 LOG("Set net band error.");
4421 break;
4422 }
4423
4424 if(req_band_set(band, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4425 {
4426 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4427 err = MBTK_INFO_ERR_CME + cme_err;
4428 } else {
4429 err = MBTK_INFO_ERR_UNKNOWN;
4430 }
4431 LOG("Set net band fail.");
4432 }
4433 else
4434 {
4435 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_BAND_RSP, NULL, 0);
4436 }
4437 }
4438 }
4439 break;
4440 }
4441 case MBTK_INFO_ID_NET_CELL_REQ: // mbtk_cell_info_t[]
4442 {
4443 if(pack->data_len == 0 || pack->data == NULL) // Get net cell.
4444 {
4445 if(req_cell_info_get(&cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4446 {
4447 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4448 err = MBTK_INFO_ERR_CME + cme_err;
4449 } else {
4450 err = MBTK_INFO_ERR_UNKNOWN;
4451 }
4452 LOG("Get net cell fail.");
4453 }
4454 else
4455 {
4456 LOG("req_cell_info_get() success,cell number: %d", cell_info.cell_num);
4457 //sleep(1);
4458 // mbtK_cell_pack_info_t
4459 if(cell_info.cell_num > 0 && cell_info.cell_num <= CELL_NUM_MAX && cell_info.type >= 0 && cell_info.type <= 2) {
4460 uint8 *data = (uint8*)malloc(sizeof(uint8) + sizeof(mbtk_cell_info_t) * cell_info.cell_num);
4461 if(data == NULL){
4462 err = MBTK_INFO_ERR_MEMORY;
4463 LOG("Get net cell fail.");
4464 } else {
4465 *data = cell_info.type; // Set network type.
4466 // Copy cell info item.
4467 #if 0
4468 int i = 0;
4469 while(i < cell_info.cell_num) {
4470 memcpy(data + sizeof(uint8) + sizeof(mbtk_cell_info_t) * i,
4471 &(cell_info.cell[i]),
4472 sizeof(mbtk_cell_info_t));
4473 i++;
4474 }
4475 #else
4476 memcpy(data + sizeof(uint8),
4477 &(cell_info.cell),
4478 sizeof(mbtk_cell_info_t) * cell_info.cell_num);
4479 #endif
4480 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_CELL_RSP, data, sizeof(uint8) + sizeof(mbtk_cell_info_t) * cell_info.cell_num);
4481 free(data);
4482 }
4483 } else {
4484 err = MBTK_INFO_ERR_UNKNOWN;
4485 LOG("Get net cell fail.");
4486 }
4487 }
4488 }
4489 else // Lock cell
4490 {
4491 char *mem = (char*)(pack->data);
4492 int len = pack->data_len;
4493 char reg[100] = {0};
4494 printf("mem:%s, len:%d", pack->data, pack->data_len);
4495
4496 if(req_cell_info_set(mem, reg, len, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4497 {
4498 // printf("cpms_set fail\n");
4499 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4500 err = MBTK_INFO_ERR_CME + cme_err;
4501 } else {
4502 err = MBTK_INFO_ERR_UNKNOWN;
4503 }
4504 // LOG("Set req_cell_info_set fail.");
4505 }
4506 else
4507 {
4508
4509 printf("req_cell_info_set success, reg:%s\n", reg);
4510 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_CELL_RSP, reg, strlen(reg));
4511
4512 // Restart is required to take effect.
4513 LOG("Will reboot system...");
4514 }
4515 }
4516 break;
4517 }
4518 case MBTK_INFO_ID_NET_RADIO_REQ: // <uint8> 0:OFF 1:ON
4519 {
4520 if(pack->data_len == 0 || pack->data == NULL) // Get
4521 {
4522 uint8 radio_on = (uint8)isRadioOn();
4523 if(radio_on < 0)
4524 {
4525 err = MBTK_INFO_ERR_UNKNOWN;
4526 LOG("Get radio state fail.");
4527 }
4528 else
4529 {
4530 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_RADIO_RSP, &radio_on, sizeof(uint8));
4531 }
4532 }
4533 else // Set
4534 {
4535 uint8 radio_on = *(pack->data);
4536 if(radio_on != 0 && radio_on != 1)
4537 {
4538 err = MBTK_INFO_ERR_REQ_PARAMETER;
4539 LOG("Set radio state fail.");
4540 }
4541 else
4542 {
4543 setRadioPower(radio_on);
4544 if((radio_on && net_info.radio_state == MBTK_RADIO_STATE_ON)
4545 || (!radio_on && net_info.radio_state == MBTK_RADIO_STATE_OFF)) {
4546 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_RADIO_RSP, NULL, 0);
4547 } else {
4548 err = MBTK_INFO_ERR_UNKNOWN;
4549 LOG("Set radio state fail.");
4550 }
4551 }
4552 }
4553 break;
4554 }
4555 case MBTK_INFO_ID_NET_SIGNAL_REQ: // mbtk_signal_info_t
4556 {
4557 if(pack->data_len == 0 || pack->data == NULL) // Get net signal.
4558 {
4559 mbtk_signal_info_t signal;
4560 memset(&signal, 0, sizeof(mbtk_signal_info_t));
4561 if(req_net_signal_get(&signal, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4562 {
4563 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4564 err = MBTK_INFO_ERR_CME + cme_err;
4565 } else {
4566 err = MBTK_INFO_ERR_UNKNOWN;
4567 }
4568 LOG("Get net signal fail.");
4569 }
4570 else
4571 {
4572 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_SIGNAL_RSP, &signal, sizeof(mbtk_signal_info_t));
4573 }
4574 }
4575 else // Set
4576 {
4577 err = MBTK_INFO_ERR_UNSUPPORTED;
4578 LOG("Set net signal fail.");
4579 }
4580 break;
4581 }
4582 case MBTK_INFO_ID_NET_REG_REQ: // mbtk_net_reg_info_t
4583 {
4584 if(pack->data_len == 0 || pack->data == NULL) // Get net reg.
4585 {
4586 mbtk_net_reg_info_t reg;
4587 memset(&reg, 0, sizeof(mbtk_net_reg_info_t));
4588 if(req_net_reg_get(&reg, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4589 {
4590 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4591 err = MBTK_INFO_ERR_CME + cme_err;
4592 } else {
4593 err = MBTK_INFO_ERR_UNKNOWN;
4594 }
4595 LOG("Get net reg fail.");
4596 }
4597 else
4598 {
4599 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_REG_RSP, &reg, sizeof(mbtk_net_reg_info_t));
4600 }
4601 }
4602 else // Set
4603 {
4604 err = MBTK_INFO_ERR_UNSUPPORTED;
4605 LOG("Set net reg fail.");
4606 }
4607 break;
4608 }
4609 case MBTK_INFO_ID_NET_APN_REQ: // mbtk_apn_info_t
4610 {
4611 if(pack->data_len == 0 || pack->data == NULL) // Get APN
4612 {
4613 uint8 buff[SOCK_MSG_LEN_MAX];
4614 memset(buff, 0, SOCK_MSG_LEN_MAX);
4615 int data_len = 0;
4616 if(req_apn_get(buff, &data_len, &cme_err) || data_len <= 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4617 {
4618 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4619 err = MBTK_INFO_ERR_CME + cme_err;
4620 } else {
4621 err = MBTK_INFO_ERR_UNKNOWN;
4622 }
4623 LOG("Get APN fail.");
4624 }
4625 else
4626 {
4627 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_APN_RSP, buff, data_len);
4628 }
4629 }
4630 else // Set
4631 {
4632 // <cid[1]><ip_type[1]><apn_len[2]><apn><user_len[2]><user><pass_len[2]><pass><auth_len[2]><auth>
4633 const uint8* ptr = pack->data;
4634 mbtk_apn_info_t apn;
4635 int len;
4636 memset(&apn, 0, sizeof(mbtk_apn_info_t));
4637 // cid
4638 apn.cid = *ptr++;
4639
4640 // ip_type
4641 apn.ip_type = (mbtk_ip_type_enum)(*ptr++);
4642
4643 // apn
4644 len = byte_2_uint16(ptr, false);
4645 ptr += sizeof(uint16);
4646 if(len > 0) {
4647 memcpy(apn.apn, ptr, len);
4648 ptr += len;
4649 }
4650
4651 // user
4652 len = byte_2_uint16(ptr, false);
4653 ptr += sizeof(uint16);
4654 if(len > 0) {
4655 memcpy(apn.user, ptr, len);
4656 ptr += len;
4657 }
4658
4659 // pass
4660 len = byte_2_uint16(ptr, false);
4661 ptr += sizeof(uint16);
4662 if(len > 0) {
4663 memcpy(apn.pass, ptr, len);
4664 ptr += len;
4665 }
4666
4667 // auth
4668 len = byte_2_uint16(ptr, false);
4669 ptr += sizeof(uint16);
4670 if(len > 0) {
4671 memcpy(apn.auth, ptr, len);
4672 ptr += len;
4673 }
4674
4675 LOGD("APN : %d, %d, %s, %s, %s, %s", apn.cid, apn.ip_type, str_empty(apn.apn) ? "NULL" : apn.apn,
4676 str_empty(apn.user) ? "NULL" : apn.user, str_empty(apn.pass) ? "NULL" : apn.pass, str_empty(apn.auth) ? "NULL" : apn.auth);
4677 if(req_apn_set(&apn, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4678 {
4679 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4680 err = MBTK_INFO_ERR_CME + cme_err;
4681 } else {
4682 err = MBTK_INFO_ERR_UNKNOWN;
4683 }
4684 LOG("Set APN fail.");
4685 }
4686 else
4687 {
4688 // Save apn.
4689 apn_prop_set(&apn);
4690
4691 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_APN_RSP, NULL, 0);
4692 }
4693 }
4694 break;
4695 }
4696 case MBTK_INFO_ID_NET_DATA_CALL_REQ:
4697 {
4698 if(pack->data_len == 0 || pack->data == NULL)
4699 {
4700 err = MBTK_INFO_ERR_UNSUPPORTED;
4701 }
4702 else
4703 {
4704 /* <call_type[1]><cid[1]><auto_conn_interval[1]><boot_conn[1]><timeout[1]>
4705 call_type : mbtk_data_call_type_enum
4706 cid : 2 - 7
4707 timeout : second
4708 */
4709 mbtk_data_call_type_enum call_type = (mbtk_data_call_type_enum)pack->data[0];
4710 int cid = pack->data[1];
4711 int reconn = 0;
4712
4713 if(cid < MBTK_APN_CID_MIN || cid > MBTK_APN_CID_MAX) {
4714 err = MBTK_INFO_ERR_CID;
4715 break;
4716 }
4717
4718 LOG("cid_active[%d] = %d", cid, cid_active[cid]);
4719 memset(&cgact_wait, 0, sizeof(info_cgact_wait_t));
4720 switch(call_type) {
4721 case MBTK_DATA_CALL_START: {
4722 //mbtk wyq for data_call_ex add start
4723 int auto_conn_interval = pack->data[2];
4724 int boot_conn = pack->data[3];
4725 int timeout = pack->data[4];
4726 data_call_bootconn_save(cid, boot_conn);
4727
4728 if(net_info.net_type != MBTK_RADIO_TECH_E_UTRAN)
4729 {
4730 err = MBTK_INFO_ERR_NET_NO_INIT;
4731 break;
4732 }
4733
4734 if(cid_active[cid] == 1)
4735 {
4736 err = MBTK_INFO_ERR_CID_EXIST;
4737 break;
4738 }
4739
4740 data_call_reconn:
4741 //mbtk wyq for data_call_ex add end
4742 cgact_wait.waitting = true;
4743 cgact_wait.cid = cid;
4744 cgact_wait.act = true;
4745 if(req_data_call_start(cid, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4746 {
4747 //mbtk wyq for data_call_ex add start
4748 if(reconn < 5 && auto_conn_interval > 0)
4749 {
4750 sleep(auto_conn_interval);
4751 reconn++;
4752 cme_err = MBTK_INFO_ERR_CME_NON;
4753 LOG("data_call restart call.");
4754 goto data_call_reconn;
4755 }
4756 //mbtk wyq for data_call_ex add end
4757 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4758 err = MBTK_INFO_ERR_CME + cme_err;
4759 } else {
4760 err = MBTK_INFO_ERR_UNKNOWN;
4761 }
4762 LOG("%d active fail.", cid);
4763 }
4764 else
4765 {
4766 // Wait for "CONNECT" or "+CGEV:"
4767 if(wait_cgact_complete(timeout)) { // Timeout
4768 err = MBTK_INFO_ERR_TIMEOUT;
4769 break;
4770 }
4771
4772 // Get IP information.
4773 mbtk_ipv4_info_t ipv4;
4774 mbtk_ipv6_info_t ipv6;
4775 memset(&ipv4, 0, sizeof(mbtk_ipv4_info_t));
4776 memset(&ipv6, 0, sizeof(mbtk_ipv6_info_t));
4777 if(req_data_call_state_get(cid, &ipv4, &ipv6, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4778 {
4779 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4780 err = MBTK_INFO_ERR_CME + cme_err;
4781 } else {
4782 err = MBTK_INFO_ERR_UNKNOWN;
4783 }
4784 LOG("Get %d state fail.", cid);
4785 }
4786 else
4787 {
4788 // Config IPv4 address.
4789#if 1
4790 if(ipv4.valid) {
4791 char dev[20] = {0};
4792 sprintf(dev, "ccinet%d", cid - 1);
4793
4794 char ip[20] = {0};
4795 char gateway[20] = {0};
4796 char *gateway_ptr = NULL;
4797 char netmask[20] = {0};
4798 char *netmask_ptr = NULL;
4799 if(inet_ntop(AF_INET, &(ipv4.IPAddr), ip, 20) == NULL) {
4800 err = MBTK_INFO_ERR_UNKNOWN;
4801 LOGE("inet_ntop ipv4 ip fail.");
4802 log_hex("IPv4", &(ipv4.IPAddr), 4);
4803 break;
4804 }
4805
4806 if(ipv4.GateWay) {
4807 if(inet_ntop(AF_INET, &(ipv4.GateWay), gateway, 20) == NULL) {
4808 err = MBTK_INFO_ERR_UNKNOWN;
4809 LOGE("inet_ntop ipv4 gateway fail.");
4810 log_hex("IPv4", &(ipv4.IPAddr), 4);
4811 break;
4812 } else {
4813 gateway_ptr = gateway;
4814 }
4815 }
4816
4817 if(ipv4.NetMask) {
4818 if(inet_ntop(AF_INET, &(ipv4.NetMask), netmask, 20) == NULL) {
4819 err = MBTK_INFO_ERR_UNKNOWN;
4820 LOGE("inet_ntop ipv4 netmask fail.");
4821 log_hex("IPv4", &(ipv4.IPAddr), 4);
4822 break;
4823 } else {
4824 netmask_ptr = netmask;
4825 }
4826 }
4827
4828 if(netmask_ptr == NULL) {
4829 netmask_ptr = netmask;
4830 memcpy(netmask_ptr, "255.255.255.0", strlen("255.255.255.0"));
4831 }
4832
4833 if(mbtk_ifc_configure2(dev, ip, 0, gateway_ptr, netmask_ptr)) {
4834 LOGD("Config %s IPv4 %s fail.", dev, ip);
4835 } else {
4836 LOGD("Config %s IPv4 %s success.", dev, ip);
4837 }
4838
4839 }
4840#endif
4841 // Config IPv6 address.
4842 if(ipv6.valid) {
4843 char ip[50] = {0};
4844 char dev[20] = {0};
4845 sprintf(dev, "ccinet%d", cid - 1);
4846
4847 if(inet_ntop(AF_INET6, &(ipv6.IPV6Addr), ip, 50) == NULL) {
4848 err = MBTK_INFO_ERR_UNKNOWN;
4849 LOGE("inet_ntop ipv6 ip fail.");
4850 log_hex("IPv6", &(ipv6.IPV6Addr), 16);
4851 break;
4852 }
4853
4854 if(mbtk_ipv6_config(dev, ip, 64)) {
4855 LOGD("Config %s IPv6 %s fail.", dev, ip);
4856 } else {
4857 LOGD("Config %s IPv6 %s success.", dev, ip);
4858 }
4859 }
4860
4861 cid_active[cid] = 1;
4862 if(cli_info->fd != DATA_CALL_BOOTCONN_FD)
4863 {
4864 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_DATA_CALL_RSP, NULL, 0);
4865 }
4866 else
4867 {
4868 free(pack->data);
4869 free(cli_info);
4870 LOG("data_call bootconn success.");
4871 }
4872 }
4873 }
4874 break;
4875 }
4876 case MBTK_DATA_CALL_STOP: {
4877 //mbtk wyq for data_call_ex add start
4878 if(cid_active[cid] == 0)
4879 {
4880 err = MBTK_INFO_ERR_CID_NO_EXIST;
4881 break;
4882 }
4883
4884 int timeout = pack->data[2];
4885 //mbtk wyq for data_call_ex add end
4886
4887 cgact_wait.waitting = true;
4888 cgact_wait.cid = cid;
4889 cgact_wait.act = false;
4890 if(req_data_call_stop(cid, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4891 {
4892 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4893 err = MBTK_INFO_ERR_CME + cme_err;
4894 } else {
4895 err = MBTK_INFO_ERR_UNKNOWN;
4896 }
4897 LOG("%d deactive fail.", cid);
4898 }
4899 else
4900 {
4901 // Wait for "CONNECT" or "+CGEV:"
4902 if(wait_cgact_complete(timeout)) { // Timeout
4903 err = MBTK_INFO_ERR_TIMEOUT;
4904 break;
4905 }
4906 char dev[20] = {0};
4907 sprintf(dev, "ccinet%d", cid - 1);
4908
4909 // Config network.
4910 if(mbtk_ifc_configure2(dev, NULL, 0, NULL, NULL)) {
4911 LOGD("Config %s IPv4 0 fail.", dev);
4912 } else {
4913 LOGD("Config %s IPv4 0 success.", dev);
4914 }
4915
4916#if 0
4917 if(mbtk_ipv6_config(dev, NULL, 64)) {
4918 LOGD("Config %s IPv6 0 fail.", dev);
4919 } else {
4920 LOGD("Config %s IPv6 0 success.", dev);
4921 }
4922#endif
4923 cid_active[cid] = 0;
4924 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_DATA_CALL_RSP, NULL, 0);
4925 }
4926 break;
4927 }
4928 case MBTK_DATA_CALL_STATE: {
4929 mbtk_ipv4_info_t ipv4;
4930 mbtk_ipv6_info_t ipv6;
4931 memset(&ipv4, 0, sizeof(mbtk_ipv4_info_t));
4932 memset(&ipv6, 0, sizeof(mbtk_ipv6_info_t));
4933 if(req_data_call_state_get(cid, &ipv4, &ipv6, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4934 {
4935 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4936 err = MBTK_INFO_ERR_CME + cme_err;
4937 } else {
4938 err = MBTK_INFO_ERR_UNKNOWN;
4939 }
4940 LOG("Get %d state fail.", cid);
4941 }
4942 else
4943 {
4944 uint8 buff[SOCK_MSG_LEN_MAX] = {0};
4945 int buff_len = 0;
4946 if(ipv4.valid && ipv6.valid) {
4947 buff[0] = (uint8)2;
4948 buff_len++;
4949
4950 memcpy(buff + buff_len, &ipv4, sizeof(mbtk_ipv4_info_t));
4951 buff_len += sizeof(mbtk_ipv4_info_t);
4952 memcpy(buff + buff_len, &ipv6, sizeof(mbtk_ipv6_info_t));
4953 buff_len += sizeof(mbtk_ipv6_info_t);
4954 } else if(ipv4.valid) {
4955 buff[0] = (uint8)0;
4956 buff_len++;
4957
4958 memcpy(buff + buff_len, &ipv4, sizeof(mbtk_ipv4_info_t));
4959 buff_len += sizeof(mbtk_ipv4_info_t);
4960 } else if(ipv6.valid) {
4961 buff[0] = (uint8)1;
4962 buff_len++;
4963
4964 memcpy(buff + buff_len, &ipv6, sizeof(mbtk_ipv6_info_t));
4965 buff_len += sizeof(mbtk_ipv6_info_t);
4966 } else {
4967 LOGE("Get IPv4/IPv6 fail.");
4968 err = MBTK_INFO_ERR_UNKNOWN;
4969 break;
4970 }
4971 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_DATA_CALL_RSP, buff, buff_len);
4972 }
4973 break;
4974 }
4975 default: {
4976 err = MBTK_INFO_ERR_FORMAT;
4977 break;
4978 }
4979 }
4980 }
4981 break;
4982 }
4983 default:
4984 {
4985 err = MBTK_INFO_ERR_REQ_UNKNOWN;
4986 LOG("Unknown request : %s", id2str(pack->info_id));
4987 break;
4988 }
4989 }
4990
4991 return err;
4992 }
4993}
4994
4995// Process AT URC data
4996static int send_pack_to_queue(sock_client_info_t* cli_info, void* pack)
4997{
4998 if(info_queue.count >= PACK_PROCESS_QUEUE_MAX)
4999 {
5000 LOG("Packet process queue is full");
5001 return -1;
5002 }
5003
5004 info_queue_item_t *item = (info_queue_item_t*)malloc(sizeof(info_queue_item_t));
5005 if(!item)
5006 {
5007 LOG("malloc() fail[%d].", errno);
5008 return -1;
5009 }
5010 item->cli_info = cli_info;
5011 item->pack = pack;
5012 mbtk_queue_put(&info_queue, item);
5013
5014 // If thread is waitting,continue it.
5015 if(1/*!is_running*/)
5016 {
5017 pthread_mutex_lock(&info_mutex);
5018 pthread_cond_signal(&info_cond);
5019 pthread_mutex_unlock(&info_mutex);
5020 }
5021 else
5022 {
5023 LOG("Packet process thread is process...");
5024 }
5025
5026 return 0;
5027}
5028
5029static void radio_state_change(void *data, int data_len)
5030{
5031 uint8 *data_ptr = (uint8*)data;
5032 if(data_ptr[0]) {
5033 net_info.radio_state = MBTK_RADIO_STATE_ON;
5034 } else {
5035 net_info.radio_state = MBTK_RADIO_STATE_OFF;
5036 }
5037
5038 sock_client_info_t *cli = NULL;
5039 list_first(sock_client_list);
5040 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
5041 {
5042 if(cli->ind_num > 0) {
5043 int i;
5044 for(i = 0; i < IND_REGISTER_MAX; i++) {
5045 // Registe MBTK_INFO_ID_IND_RADIO_STATE_CHANGE
5046 if(cli->ind_register[i] == MBTK_INFO_ID_IND_RADIO_STATE_CHANGE) {
5047 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_RADIO_STATE_CHANGE, data, data_len);
5048 break;
5049 }
5050 }
5051 }
5052 }
5053}
5054
5055static void pdp_state_change(void *data, int data_len)
5056{
5057 sock_client_info_t *cli = NULL;
5058 list_first(sock_client_list);
5059 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
5060 {
5061 if(cli->ind_num > 0) {
5062 int i;
5063 for(i = 0; i < IND_REGISTER_MAX; i++) {
5064 if(cli->ind_register[i] == MBTK_INFO_ID_IND_PDP_STATE_CHANGE) {
5065 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_PDP_STATE_CHANGE, data, data_len);
5066 break;
5067 }
5068 }
5069 }
5070 }
5071}
5072
5073static void net_state_change(void *data, int data_len)
5074{
5075 sock_client_info_t *cli = NULL;
5076 list_first(sock_client_list);
5077 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
5078 {
5079 if(cli->ind_num > 0) {
5080 int i;
5081 for(i = 0; i < IND_REGISTER_MAX; i++) {
5082 // Registe MBTK_INFO_ID_IND_NET_STATE_CHANGE
5083 if(cli->ind_register[i] == MBTK_INFO_ID_IND_NET_STATE_CHANGE) {
5084 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_NET_STATE_CHANGE, data, data_len);
5085 break;
5086 }
5087 }
5088 }
5089 }
5090}
5091
5092static void call_state_change(void *data, int data_len)
5093{
5094 sock_client_info_t *cli = NULL;
5095 list_first(sock_client_list);
5096 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
5097 {
5098 if(cli->ind_num > 0) {
5099 int i;
5100 for(i = 0; i < IND_REGISTER_MAX; i++) {
5101 // Registed MBTK_INFO_ID_IND_RADIO_STATE_CHANGE
5102 if(cli->ind_register[i] == MBTK_INFO_ID_IND_CALL_STATE_CHANGE) {
5103 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_CALL_STATE_CHANGE, data, data_len);
5104 break;
5105 }
5106 }
5107 }
5108 }
5109}
5110
5111static void sim_state_change(void *data, int data_len)
5112{
5113 sock_client_info_t *cli = NULL;
5114 list_first(sock_client_list);
5115 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
5116 {
5117 if(cli->ind_num > 0) {
5118 int i;
5119 for(i = 0; i < IND_REGISTER_MAX; i++) {
5120 // Registed MBTK_INFO_ID_IND_RADIO_STATE_CHANGE
5121 if(cli->ind_register[i] == MBTK_INFO_ID_IND_SIM_STATE_CHANGE) {
5122 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_SIM_STATE_CHANGE, data, data_len);
5123 break;
5124 }
5125 }
5126 }
5127 }
5128}
5129
5130static void sms_state_change(void *data, int data_len)
5131{
5132 sock_client_info_t *cli = NULL;
5133 list_first(sock_client_list);
5134 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
5135 {
5136 if(cli->ind_num > 0) {
5137 int i;
5138 for(i = 0; i < IND_REGISTER_MAX; i++) {
5139 // Registed MBTK_INFO_ID_IND_SMS_STATE_CHANGE
5140 if(cli->ind_register[i] == MBTK_INFO_ID_IND_SMS_STATE_CHANGE) {
5141 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_SMS_STATE_CHANGE, data, data_len);
5142 break;
5143 }
5144 }
5145 }
5146 }
5147}
5148
5149int urc_msg_distribute(bool async_process, info_urc_msg_id_enum msg, void *data, int data_len)
5150{
5151#if 0
5152 if(urc_queue.count >= PACK_PROCESS_QUEUE_MAX)
5153 {
5154 LOG("Packet process queue is full");
5155 return -1;
5156 }
5157
5158 info_urc_msg_t *urc = (info_urc_msg_t*)malloc(sizeof(info_urc_msg_t));
5159 if(!urc)
5160 {
5161 LOG("malloc() fail[%d].", errno);
5162 return -1;
5163 }
5164 urc->msg = msg;
5165 urc->data = memdup(data, data_len);
5166 urc->data_len = data_len;
5167
5168 mbtk_queue_put(&urc_queue, urc);
5169
5170 // If thread is waitting,continue it.
5171 if(1/*!is_running*/)
5172 {
5173 pthread_mutex_lock(&urc_mutex);
5174 pthread_cond_signal(&urc_cond);
5175 pthread_mutex_unlock(&urc_mutex);
5176 }
5177 else
5178 {
5179 LOG("Packet process thread is process...");
5180 }
5181
5182 return 0;
5183#else
5184 if(async_process) {
5185 info_urc_msg_t *urc = (info_urc_msg_t*)malloc(sizeof(info_urc_msg_t));
5186 if(!urc)
5187 {
5188 LOG("malloc() fail[%d].", errno);
5189 return -1;
5190 }
5191 urc->msg = msg;
5192 if(data && data_len > 0) {
5193 urc->data = memdup(data, data_len);
5194 urc->data_len = data_len;
5195 } else {
5196 urc->data = NULL;
5197 urc->data_len = 0;
5198 }
5199 return send_pack_to_queue(NULL, urc);
5200 } else {
5201 switch(msg) {
5202 case INFO_URC_MSG_NET_CS_REG_STATE:
5203 {
5204 net_state_change(data, data_len);
5205 break;
5206 }
5207 case INFO_URC_MSG_CALL_STATE:
5208 {
5209 call_state_change(data, data_len);
5210 break;
5211 }
5212 case INFO_URC_MSG_SMS_STATE:
5213 {
5214 sms_state_change(data, data_len);
5215 break;
5216 }
5217 case INFO_URC_MSG_SIM_STATE:
5218 {
5219 sim_state_change(data, data_len);
5220 break;
5221 }
5222 case INFO_URC_MSG_PDP_STATE:
5223 {
5224 pdp_state_change(data, data_len);
5225 break;
5226 }
5227 default: {
5228 LOGE("Unknown msg : %d", msg);
5229 break;
5230 }
5231 }
5232
5233 return 0;
5234 }
5235#endif
5236}
5237
5238
5239static void ind_regisger(sock_client_info_t* cli_info, uint16 ind)
5240{
5241 uint32 i = 0;
5242 while(i < cli_info->ind_num)
5243 {
5244 if(cli_info->ind_register[i] == ind)
5245 break;
5246 i++;
5247 }
5248
5249 if(i == cli_info->ind_num) // No found IND
5250 {
5251 cli_info->ind_register[i] = ind;
5252 cli_info->ind_num++;
5253 LOG("Register IND : %s", id2str(ind));
5254 }
5255 else
5256 {
5257 LOG("IND had exist.");
5258 }
5259}
5260
5261static void pack_distribute(sock_client_info_t* cli_info, mbtk_info_pack_t* pack)
5262{
5263 // Register IND Message.
5264 if(mbtk_info_type_get(pack->info_id) == MBTK_INFO_TYPE_IND)
5265 {
5266 mbtk_info_err_enum err = MBTK_INFO_ERR_SUCCESS;
5267 if(cli_info->ind_num >= IND_REGISTER_MAX)
5268 {
5269 LOG("IND if full.");
5270 err = MBTK_INFO_ERR_IND_FULL;
5271 }
5272 else
5273 {
5274 ind_regisger(cli_info, pack->info_id);
5275 }
5276
5277 pack_error_send(cli_info->fd, pack->info_id, err);
5278
5279 mbtk_info_pack_free(&pack);
5280 }
5281 else // Request Information.
5282 {
5283 LOG("Start process REQ(%s), Length : %d", id2str(pack->info_id), pack->data_len);
5284 if(0 && pack->data_len > 0)
5285 {
5286 log_hex("DATA", pack->data, pack->data_len);
5287 }
5288
5289 // Send to REQ_process_thread process.
5290 send_pack_to_queue(cli_info, pack);
5291
5292 // For test.
5293 // pack_error_send(cli_info->fd, pack->info_id + 1, MBTK_INFO_ERR_SUCCESS);
5294 }
5295}
5296
5297static sock_client_info_t* cli_find(int fd)
5298{
5299 sock_client_info_t *result = NULL;
5300 list_first(sock_client_list);
5301 while ((result = (sock_client_info_t*) list_next(sock_client_list)))
5302 {
5303 if (result->fd == fd)
5304 return result;
5305 }
5306
5307 return NULL;
5308}
5309
5310//mbtk wyq for server_ready_status add start
5311void server_ready_set(void)
5312{
5313 server_ready_status = 1;
5314}
5315
5316char server_ready_get(void)
5317{
5318 return server_ready_status;
5319}
5320
5321static void server_state_send(void)
5322{
5323 sock_client_info_t *cli = NULL;
5324 list_first(sock_client_list);
5325 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
5326 {
5327 if(cli->ind_num > 0) {
5328 if(cli->ind_register[0] == MBTK_INFO_ID_IND_SERVER_STATE_CHANGE) {
5329 cli->ind_num = 0;
5330 cli->ind_register[0] = 0;
5331 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_SERVER_STATE_CHANGE, "1", 1);
5332 break;
5333 }
5334 }
5335 else
5336 {
5337 break;
5338 }
5339 }
5340 LOG("handshake message send ok.");
5341}
5342
5343//mbtk wyq for server_ready_status add end
5344
5345//mbtk wyq for data_call_ex add start
5346//Save the cid that "DATA_CALL" needs to be automatically connected after startup
5347void data_call_bootconn_save(int cid, int bootconn)
5348{
5349 if(cid_bootconn[cid] == bootconn + '0')
5350 {
5351 return;
5352 }
5353 cid_bootconn[cid] = bootconn + '0';
5354
5355 LOG("data_call_bootconn_set cid_bootconn = %s", cid_bootconn);
5356 property_set("persist.mbtk.datacall.bootconn", cid_bootconn);
5357}
5358
5359static void* data_call_bootconn_pthread(void *arg)
5360{
5361 UNUSED(arg);
5362 LOG("data_call_bootconn_pthread enter.");
5363 int i = 0;
5364 int send_sum = 0;
5365 int bootconn = 0;
5366
5367 while(1)
5368 {
5369 if(server_ready_get() && send_sum == 0)
5370 {
5371 server_state_send();
5372 send_sum = 1;
5373 }
5374
5375 if(net_info.sim_state == MBTK_SIM_READY && net_info.net_type == MBTK_RADIO_TECH_E_UTRAN && bootconn == 0)
5376 {
5377 //data_call_bootconn_exec();
5378 property_get("persist.mbtk.datacall.bootconn", cid_bootconn, "00000000");
5379 LOG("data_call_bootconn_exec cid_bootconn = %s", cid_bootconn);
5380
5381 for(i = MBTK_APN_CID_MIN; i < MBTK_APN_CID_MAX + 1; i++)
5382 {
5383 if(cid_bootconn[i] == '1')
5384 {
5385 sock_client_info_t *info = (sock_client_info_t*)malloc(sizeof(sock_client_info_t));
5386 if(info == NULL)
5387 {
5388 LOG("clinent_info malloc() fail.");
5389 continue;
5390 }
5391 memset(info, 0, sizeof(sock_client_info_t));
5392 info->fd = DATA_CALL_BOOTCONN_FD;
5393
5394 mbtk_info_pack_t* pack = mbtk_info_pack_creat(MBTK_INFO_ID_NET_DATA_CALL_REQ);
5395 if(pack == NULL)
5396 {
5397 free(info);
5398 LOG("Packet malloc() fail.");
5399 continue;
5400 }
5401
5402 // "info_err"
5403 //pack->info_err = byte_2_uint16(ptr, false)
5404
5405 // "data_len"
5406 pack->data_len = 5;
5407
5408 char *p = (char *)malloc(5);
5409 p[0] = MBTK_DATA_CALL_START;
5410 p[1] = i;
5411 p[2] = 0;
5412 p[3] = 1;
5413 p[4] = 10;
5414 pack->data = p;
5415 send_pack_to_queue(info, pack);
5416 }
5417 }
5418
5419 bootconn = 1;
5420 }
5421
5422 if(bootconn == 1 && send_sum == 1)
5423 {
5424 break;
5425 }
5426 else
5427 {
5428 sleep(1);
5429 }
5430 }
5431
5432 LOG("data_call_bootconn_pthread exit.");
5433 return NULL;
5434}
5435
5436//mbtk wyq for data_call_ex add end
5437
5438static void* info_main_pthread(void* arg)
5439{
5440 UNUSED(arg);
5441 epoll_fd = epoll_create(SOCK_CLIENT_MAX + 1);
5442 if(epoll_fd < 0)
5443 {
5444 LOG("epoll_create() fail[%d].", errno);
5445 return NULL;
5446 }
5447
5448 uint32 event = EPOLLIN | EPOLLET;
5449 struct epoll_event ev;
5450 ev.data.fd = sock_listen_fd;
5451 ev.events = event; //EPOLLIN | EPOLLERR | EPOLLET;
5452 epoll_ctl(epoll_fd,EPOLL_CTL_ADD,sock_listen_fd,&ev);
5453
5454 int nready = -1;
5455 struct epoll_event epoll_events[EPOLL_LISTEN_MAX];
5456 while(1)
5457 {
5458 nready = epoll_wait(epoll_fd, epoll_events, EPOLL_LISTEN_MAX, -1);
5459 if(nready > 0)
5460 {
5461 sock_client_info_t *cli_info = NULL;
5462 int i;
5463 for(i = 0; i < nready; i++)
5464 {
5465 LOG("fd[%d] event = %x",epoll_events[i].data.fd, epoll_events[i].events);
5466 if(epoll_events[i].events & EPOLLHUP) // Client Close.
5467 {
5468 if((cli_info = cli_find(epoll_events[i].data.fd)) != NULL)
5469 {
5470 cli_close(cli_info);
5471 }
5472 else
5473 {
5474 LOG("Unknown client[fd = %d].", epoll_events[i].data.fd);
5475 }
5476 }
5477 else if(epoll_events[i].events & EPOLLIN)
5478 {
5479 if(epoll_events[i].data.fd == sock_listen_fd) // New clients connected.
5480 {
5481 int client_fd = -1;
5482 while(1)
5483 {
5484 struct sockaddr_in cliaddr;
5485 socklen_t clilen = sizeof(cliaddr);
5486 client_fd = accept(epoll_events[i].data.fd, (struct sockaddr *) &cliaddr, &clilen);
5487 if(client_fd < 0)
5488 {
5489 if(errno == EAGAIN)
5490 {
5491 LOG("All client connect get.");
5492 }
5493 else
5494 {
5495 LOG("accept() error[%d].", errno);
5496 }
5497 break;
5498 }
5499 // Set O_NONBLOCK
5500 int flags = fcntl(client_fd, F_GETFL, 0);
5501 if (flags > 0)
5502 {
5503 flags |= O_NONBLOCK;
5504 if (fcntl(client_fd, F_SETFL, flags) < 0)
5505 {
5506 LOG("Set flags error:%d", errno);
5507 }
5508 }
5509
5510 memset(&ev,0,sizeof(struct epoll_event));
5511 ev.data.fd = client_fd;
5512 ev.events = event;//EPOLLIN | EPOLLERR | EPOLLET;
5513 epoll_ctl(epoll_fd, EPOLL_CTL_ADD, client_fd, &ev);
5514
5515 sock_client_info_t *info = (sock_client_info_t*)malloc(sizeof(sock_client_info_t));
5516 if(info)
5517 {
5518 memset(info, 0, sizeof(sock_client_info_t));
5519 info->fd = client_fd;
5520 if(server_ready_get() == 1)
5521 {
5522 info->ind_num = 0;
5523 pack_rsp_send(info->fd , MBTK_INFO_ID_IND_SERVER_STATE_CHANGE, "1", 1);
5524 LOG("server ready ok.");
5525 }
5526 else
5527 {
5528 info->ind_num = 1;
5529 info->ind_register[0] = MBTK_INFO_ID_IND_SERVER_STATE_CHANGE;
5530 LOG("server ready no.");
5531 }
5532 list_add(sock_client_list, info);
5533 LOG("Add New Client FD Into List.");
5534 }
5535 else
5536 {
5537 LOG("malloc() fail.");
5538 }
5539 }
5540 }
5541 else if((cli_info = cli_find(epoll_events[i].data.fd)) != NULL) // Client data arrive.
5542 {
5543 // Read and process every message.
5544 mbtk_info_err_enum err = MBTK_INFO_ERR_SUCCESS;
5545 mbtk_info_pack_t** pack = mbtk_info_pack_recv(cli_info->fd, true, &err);
5546
5547 // Parse packet error,send error response to client.
5548 if(pack == NULL)
5549 {
5550 if(err != MBTK_INFO_ERR_SUCCESS)
5551 {
5552 pack_error_send(cli_info->fd, MBTK_INFO_ID_REQ_UNKNOWN, err);
5553 }
5554 }
5555 else
5556 {
5557#if 0
5558 int i = 0;
5559 while(pack[i] != NULL)
5560 {
5561 pack_distribute(cli_info, pack[i]);
5562 // Not free,will free in pack_process() or packet process thread.
5563 //mbtk_info_pack_free(&(pack[i]));
5564 i++;
5565 }
5566 free(pack);
5567#else
5568 mbtk_info_pack_t** pack_ptr = pack;
5569 while(*pack_ptr)
5570 {
5571 pack_distribute(cli_info, *pack_ptr);
5572 // Not free,will free in pack_process() or packet process thread.
5573 //mbtk_info_pack_free(pack_ptr);
5574 pack_ptr++;
5575 }
5576
5577 free(pack);
5578#endif
5579 }
5580 }
5581 else
5582 {
5583 LOG("Unknown socket : %d", epoll_events[i].data.fd);
5584 }
5585 }
5586 else
5587 {
5588 LOG("Unknown event : %x", epoll_events[i].events);
5589 }
5590 }
5591 }
5592 else
5593 {
5594 LOG("epoll_wait() fail[%d].", errno);
5595 }
5596 }
5597
5598 return NULL;
5599}
5600
5601static void data_call_restart()
5602{
5603#if 0
5604 // Waitting for network ok.
5605 mbtk_net_info_t info;
5606 int cme_err;
5607 int i = 0;
5608 while(i < 15) { // 15s timeout
5609 cme_err = MBTK_INFO_ERR_CME_NON;
5610 memset(&info, 0, sizeof(mbtk_net_info_t));
5611 if(!req_net_sel_mode_get(&info, &cme_err) && cme_err == MBTK_INFO_ERR_CME_NON)
5612 {
5613 if(info.net_type >= 2) {
5614 break;
5615 }
5616 }
5617
5618 sleep(1);
5619 i++;
5620 }
5621#endif
5622 // +CGACT
5623 int cid;
5624 LOGD("Start active APN.");
5625 for(cid = MBTK_APN_CID_MIN; cid <= MBTK_APN_CID_MAX && cid_active[cid]; cid++) {
5626 LOG("Active cid : %d", cid);
5627 req_data_call_start(cid, NULL);
5628 }
5629}
5630
5631/*
5632void mbtk_radio_ready_cb()
5633{
5634 pthread_t radio_pid;
5635 pthread_attr_t thread_attr;
5636 pthread_attr_init(&thread_attr);
5637 if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
5638 {
5639 LOG("pthread_attr_setdetachstate() fail.");
5640 return;
5641 }
5642
5643 if(pthread_create(&radio_pid, &thread_attr, radio_ready_thread, NULL))
5644 {
5645 LOG("pthread_create() fail.");
5646 }
5647
5648 pthread_attr_destroy(&thread_attr);
5649}
5650*/
5651
5652static void net_ifc_state_change(bool act, int cid)
5653{
5654 if(cid < MBTK_APN_CID_MIN || cid > MBTK_APN_CID_MAX) { // No nothing for cid 1 and 8
5655 return;
5656 }
5657
wangyouqiang65884152023-10-25 19:54:15 +08005658 if(act)
5659 {
5660 cid_active[cid] = 1;
5661 }
5662 else
5663 {
5664 cid_active[cid] = 0;
5665 }
liubin281ac462023-07-19 14:22:54 +08005666 char dev[20] = {0};
5667 sprintf(dev, "ccinet%d", cid - 1);
5668 if(act) { // Config IP.
5669 // Get IP information.
5670 mbtk_ipv4_info_t ipv4;
5671 mbtk_ipv6_info_t ipv6;
5672 memset(&ipv4, 0, sizeof(mbtk_ipv4_info_t));
5673 memset(&ipv6, 0, sizeof(mbtk_ipv6_info_t));
5674 int cme_err = MBTK_INFO_ERR_CME_NON;
5675 if(!req_data_call_state_get(cid, &ipv4, &ipv6, &cme_err) && cme_err == MBTK_INFO_ERR_CME_NON)
5676 {
5677 // Config IPv4 address.
5678 if(ipv4.valid) {
5679 char ip[20] = {0};
5680 if(inet_ntop(AF_INET, &(ipv4.IPAddr), ip, 20) == NULL) {
5681 LOGE("inet_ntop ipv4 ip fail.");
5682 return;
5683 }
5684
5685 if(mbtk_ifc_configure2(dev, ip, 0, NULL, "255.255.255.0")) {
5686 LOGD("Config %s IPv4 %s fail.", dev, ip);
5687 } else {
5688 LOGD("Config %s IPv4 %s success.", dev, ip);
5689 }
5690 }
5691
5692 // Config IPv6 address.
5693 if(ipv6.valid) {
5694 char ip[50] = {0};
5695
5696 if(inet_ntop(AF_INET6, &(ipv6.IPV6Addr), ip, 50) == NULL) {
5697 LOGE("inet_ntop ipv6 ip fail.");
5698 return;
5699 }
5700
5701 if(mbtk_ipv6_config(dev, ip, 64)) {
5702 LOGD("Config %s IPv6 %s fail.", dev, ip);
5703 } else {
5704 LOGD("Config %s IPv6 %s success.", dev, ip);
5705 }
5706 }
5707 }
5708 } else { // Del IP
5709 if(mbtk_ifc_configure2(dev, NULL, 0, NULL, NULL)) {
5710 LOGD("Config %s IPv4 0 fail.", dev);
5711 } else {
5712 LOGD("Config %s IPv4 0 success.", dev);
5713 }
5714 }
5715}
5716
5717static void urc_msg_process(info_urc_msg_t *msg)
5718{
5719 uint8 *data = NULL;
5720 if(msg->data) {
5721 data = (uint8*)msg->data;
5722 }
5723 switch(msg->msg) {
5724 case INFO_URC_MSG_RADIO_STATE:
5725 {
5726 radio_state_change(msg->data, msg->data_len);
5727 // Reconfig APN while radio on.
5728 if(data[0]) {
5729 apn_prop_get();
5730 }
5731 break;
5732 }
5733 case INFO_URC_MSG_CGEV:
5734 {
5735 bool act = data[0];
5736 int cid = data[1];
5737 if(cid > 0) {
5738 net_ifc_state_change(act, cid);
5739 }
5740 break;
5741 }
5742 case INFO_URC_MSG_NET_PS_REG_STATE:
5743 {
5744 uint8 net_data[3];
5745 net_data[0] = (uint8)MBTK_NET_PS_STATE;
5746 net_data[1] = data[0];
5747 mbtk_net_reg_state_enum state = (mbtk_net_reg_state_enum)data[0];
5748 if(state == MBTK_NET_REG_STATE_HOME
5749 || state == MBTK_NET_REG_STATE_ROAMING) { // Registered, home network or roaming.
5750 mbtk_net_info_t info;
5751 int cme_err = MBTK_INFO_ERR_CME_NON;
5752 memset(&info, 0, sizeof(mbtk_net_info_t));
5753 if(!req_net_sel_mode_get(&info, &cme_err) && cme_err == MBTK_INFO_ERR_CME_NON)
5754 {
5755 net_data[2] = info.net_type;
5756 net_state_change(net_data, sizeof(net_data));
5757
5758 if(info.net_type >= MBTK_RADIO_TECH_UTRAN) {
5759 data_call_restart();
5760 }
5761 } else {
5762 net_data[2] = (uint8)0xFF;
5763 net_state_change(net_data, sizeof(net_data));
5764 }
5765 } else {
5766 net_data[2] = (uint8)0xFF;
5767 net_state_change(net_data, sizeof(net_data));
5768 }
5769 break;
5770 }
5771 case INFO_URC_MSG_NET_STATE_LOG:
5772 {
5773 // Get network state and signal.
5774 char buff[256] = {0};
5775 mbtk_signal_info_t signal;
5776 memset(&signal, 0xFF, sizeof(mbtk_signal_info_t));
5777 if(!req_net_signal_get(&signal, NULL)) {
5778 char tmp[50] = {0};
5779 struct timeval log_time;
5780 gettimeofday(&log_time, NULL);
5781 struct tm* tm_t = localtime(&(log_time.tv_sec));
5782 strftime(tmp, 50, "%F %T", tm_t);
5783 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,
5784 signal.rsrq, signal.rsrp);
5785 mbtk_signal_log(buff);
5786 }
5787 //
5788 break;
5789 }
5790 default:
5791 {
5792 LOGE("Unknown URC : %d", msg->msg);
5793 break;
5794 }
5795 }
5796}
5797
5798static void* pack_process_thread(void* arg)
5799{
5800 UNUSED(arg);
5801 info_queue_item_t* item = NULL;
5802 mbtk_queue_init(&info_queue);
5803 pthread_mutex_init(&info_mutex, NULL);
5804 pthread_cond_init(&info_cond, NULL);
5805
5806 memset(&band_support, 0xFF, sizeof(mbtk_band_info_t));
5807
5808 pthread_mutex_lock(&info_mutex);
5809 while(TRUE)
5810 {
5811 if(mbtk_queue_empty(&info_queue))
5812 {
5813 LOG("Packet process wait...");
5814 pthread_cond_wait(&info_cond, &info_mutex);
5815 LOG("Packet process continue...");
5816 }
5817 else
5818 {
5819 LOG("Packet process queue not empty,continue...");
5820 }
5821
5822 // Process all information request.
5823 mbtk_info_err_enum err;
5824 while((item = (info_queue_item_t*)mbtk_queue_get(&info_queue)) != NULL)
5825 {
5826 if(item->cli_info) { // REQ form client.
5827 mbtk_info_pack_t *pack = (mbtk_info_pack_t*)item->pack;
5828 LOG("Process REQ %s.", id2str(pack->info_id));
5829 at_process = true;
5830 err = pack_req_process(item->cli_info, pack);
5831 if(err != MBTK_INFO_ERR_SUCCESS)
5832 {
5833 if(item->cli_info->fd != DATA_CALL_BOOTCONN_FD)
5834 {
5835 pack_error_send(item->cli_info->fd, pack->info_id + 1, err);
5836 }
5837 else
5838 {
5839 free(pack->data);
5840 free(item->cli_info);
5841 }
5842 }
5843 at_process = false;
5844 mbtk_info_pack_free(&pack);
5845 free(item);
5846 } else { // REQ from myself.
5847 info_urc_msg_t *urc = (info_urc_msg_t*)item->pack;
5848 LOG("Process URC %d.", urc->msg);
5849 urc_msg_process(urc);
5850 if(!urc->data)
5851 free(urc->data);
5852 free(urc);
5853 }
5854 }
5855 }
5856 pthread_mutex_unlock(&info_mutex);
5857 return NULL;
5858}
5859
5860void apn_prop_get()
5861{
5862 char prop_name[20];
5863 char prop_data[300];
5864 // cid : 2 - 7
5865 int cid = MBTK_APN_CID_MIN;
5866 mbtk_apn_info_t apn;
5867 for(; cid <= MBTK_APN_CID_MAX; cid++) {
5868 memset(prop_name, 0, 20);
5869 memset(prop_data, 0, 300);
5870 memset(&apn, 0, sizeof(mbtk_apn_info_t));
5871 sprintf(prop_name, "%s_%d",MBTK_APN_PROP,cid);
5872 if(property_get(prop_name, prop_data, "") > 0 && !str_empty(prop_data)) {
5873 apn.cid = cid;
5874 char *ptr_1 = prop_data;
5875 apn.ip_type = (mbtk_ip_type_enum)atoi(ptr_1);
5876 ptr_1 = strstr(ptr_1, ",");
5877 if(!ptr_1) {
5878 continue;
5879 }
5880 ptr_1++; // Jump ',' to apn
5881
5882 char *ptr_2 = strstr(ptr_1, ",");
5883 if(!ptr_2) {
5884 continue;
5885 }
5886 memcpy(apn.apn, ptr_1, ptr_2 - ptr_1); // apn
5887
5888 ptr_2++; // Jump ',' to user
5889 ptr_1 = strstr(ptr_2, ",");
5890 if(!ptr_1) {
5891 continue;
5892 }
5893 if(memcmp(ptr_2, "NULL", 4)) { // Not "NULL"
5894 memcpy(apn.user, ptr_2, ptr_1 - ptr_2); // user
5895 }
5896
5897 ptr_1++; // Jump ',' to pass
5898 ptr_2 = strstr(ptr_1, ",");
5899 if(!ptr_2) {
5900 continue;
5901 }
5902 if(memcmp(ptr_1, "NULL", 4)) { // Not "NULL"
5903 memcpy(apn.pass, ptr_1, ptr_2 - ptr_1); // pass
5904 }
5905
5906 ptr_2++; // Jump ',' to auth (Is last item)
5907 if(memcmp(ptr_2, "NULL", 4)) { // Not "NULL"
5908 memcpy(apn.auth, ptr_2, strlen(ptr_2)); // auth
5909 }
5910
5911 req_apn_set(&apn, NULL);
5912 }
5913 }
5914}
5915
5916/*
5917AT*BAND=15,78,147,482,134742231
5918
5919OK
5920*/
5921static int lte_b28_set()
5922{
5923 int err = -1;
5924 ATResponse *p_response = NULL;
5925 char *line;
5926 char *ipv4 = NULL, *ipv6 = NULL;
5927 err = at_send_command("AT*BAND=15,78,147,482,134742231", &p_response);
5928 if ((err < 0) || (p_response == NULL) || (p_response->success == 0))
5929 {
5930 LOGE("*BAND exec error.");
5931 err = -1;
5932 goto error;
5933 }
5934
5935 LOGD("Set B28 Success.");
5936 err = 0;
5937
5938error:
5939 at_response_free(p_response);
5940 return err;
5941}
5942
5943static void* net_monitor_thread(void* arg)
5944{
5945 UNUSED(arg);
5946 // Start network monitor
5947 int cid;
5948 while(1) {
5949#if 0
5950 // Config IP
5951 list_node_t* apn_list = NULL;
5952 if(!apn_state_get(&apn_list) && apn_list != NULL) {
5953 info_apn_ip_t *apn = NULL;
5954 for(cid = MBTK_APN_CID_MIN; cid <= MBTK_APN_CID_MAX && cid_active[cid]; cid++) {
5955 bool ip_found = false;
5956 list_first(apn_list);
5957 while ((apn = (info_apn_ip_t*) list_next(apn_list))) {
5958 if(cid == apn->cid) {
5959 ip_found = true;
5960 break;
5961 }
5962 }
5963
5964 char dev[20] = {0};
5965 sprintf(dev, "ccinet%d", cid - 1);
5966 if(ip_found) { // Ip ok,set IP.
5967 if(apn->ipv4_valid) {
5968 if(mbtk_ifc_configure2(dev, (char*)apn->ipv4, 0, NULL, "255.255.255.0")) {
5969 LOGD("Config %s IPv4 %s fail.", dev, apn->ipv4);
5970 } else {
5971 LOGD("Config %s IPv4 %s success.", dev, apn->ipv4);
5972 }
5973 }
5974
5975 if(apn->ipv6_valid) {
5976 if(mbtk_ipv6_config(dev, (char*)apn->ipv6, 64)) {
5977 LOGD("Config %s IPv6 %s fail.", dev, apn->ipv6);
5978 } else {
5979 LOGD("Config %s IPv6 %s success.", dev, apn->ipv6);
5980 }
5981 }
5982 } else { // No ip
5983 if(mbtk_ifc_configure2(dev, NULL, 0, NULL, NULL)) {
5984 LOGD("Config %s IPv4 0 fail.", dev);
5985 } else {
5986 LOGD("Config %s IPv4 0 success.", dev);
5987 }
5988 }
5989 }
5990
5991 list_free(apn_list);
5992 }
5993#endif
5994
5995 if(net_info.radio_state == MBTK_RADIO_STATE_ON && net_info.sim_state == MBTK_SIM_READY) {
5996#if 0
5997 urc_msg_distribute(true, INFO_URC_MSG_NET_CS_REG_STATE, NULL, 0);
5998#else
5999 info_urc_msg_t *urc = (info_urc_msg_t*)malloc(sizeof(info_urc_msg_t));
6000 if(!urc)
6001 {
6002 LOG("malloc() fail[%d].", errno);
6003 } else {
6004 urc->msg = INFO_URC_MSG_NET_STATE_LOG;
6005 urc->data = NULL;
6006 urc->data_len = 0;
6007 send_pack_to_queue(NULL, urc);
6008 }
6009#endif
6010 }
6011
6012 sleep(15);
6013 }
6014
6015 LOGD("monitor_thread exit.");
6016 return NULL;
6017}
6018
6019static void* urc_process_thread(void* arg)
6020{
6021 UNUSED(arg);
6022 info_urc_msg_t* item = NULL;
6023 mbtk_queue_init(&urc_queue);
6024 pthread_mutex_init(&urc_mutex, NULL);
6025 pthread_cond_init(&urc_cond, NULL);
6026
6027 pthread_mutex_lock(&urc_mutex);
6028 while(TRUE)
6029 {
6030 if(mbtk_queue_empty(&urc_queue))
6031 {
6032 LOG("URC process wait...");
6033 pthread_cond_wait(&urc_cond, &urc_mutex);
6034 LOG("URC process continue...");
6035 }
6036 else
6037 {
6038 LOG("URC process queue not empty,continue...");
6039 }
6040
6041 // Process all information request.
6042 while((item = (info_urc_msg_t*)mbtk_queue_get(&urc_queue)) != NULL)
6043 {
6044 LOG("Process URC %d.", item->msg);
6045 uint8 *data = (uint8*)item->data;
6046 switch(item->msg) {
6047 case INFO_URC_MSG_RADIO_STATE:
6048 {
6049 radio_state_change(item->data, item->data_len);
6050 break;
6051 }
6052 case INFO_URC_MSG_CGEV:
6053 {
6054 bool act = data[0];
6055 int cid = data[1];
6056 if(cid > 0) {
6057 net_ifc_state_change(act, cid);
6058 }
6059 break;
6060 }
6061 default:
6062 {
6063 LOGE("Unknown URC : %d", item->msg);
6064 break;
6065 }
6066 }
6067 if(!item->data)
6068 free(item->data);
6069 free(item);
6070 }
6071 }
6072 pthread_mutex_unlock(&urc_mutex);
6073
6074 return NULL;
6075}
6076
6077static void ril_at_ready_process()
6078{
6079 net_info.radio_state = (isRadioOn() == 1) ? MBTK_RADIO_STATE_ON : MBTK_RADIO_STATE_OFF;
6080#if 1
6081 if (net_info.radio_state != MBTK_RADIO_STATE_ON)
6082 {
6083 setRadioPower(1);
6084 } else { // Radio has ON
6085 apn_prop_get();
6086 }
6087
6088 if(net_info.radio_state == MBTK_RADIO_STATE_ON)
6089 {
6090 at_send_command("AT+CEREG=2", NULL);
6091 }
6092
6093 int count = 0;
6094#endif
6095 net_info.sim_state = getSIMStatus();
6096#if 0
6097 while (net_info.sim_state != MBTK_SIM_READY && count < 30)
6098 {
6099 if(net_info.radio_state != MBTK_RADIO_STATE_ON)
6100 {
6101 setRadioPower(1);
6102 }
6103 LOGD("Waitting for SIM READY...");
6104 sleep(1);
6105 net_info.sim_state = getSIMStatus();
6106 count++;
6107 }
6108#endif
6109 if(net_info.sim_state == MBTK_SIM_READY)
6110 {
6111 LOGD("SIM READY!");
6112 }
6113 else
6114 {
6115 LOGE("SIM NOT READY!");
6116 }
6117
6118 // Set B28
6119 // AT*BAND=15,78,147,482,134742231
6120#if MBTK_LTE_B28_SUPPORT
6121 char b28_config[10];
6122 memset(b28_config, 0, 10);
6123 property_get("persist.mbtk.b28_config", b28_config, "0");
6124 if(atoi(b28_config) == 0) {
6125 if(!lte_b28_set()) { // Set B28 success.
6126 property_set("persist.mbtk.b28_config", "1");
6127 }
6128 }
6129#endif
6130}
6131
6132int mbtk_info_server_start()
6133{
6134 signal(SIGPIPE, SIG_IGN);
6135
6136 if(sock_listen_fd > 0)
6137 {
6138 LOG("Information Server Has Started.");
6139 return -1;
6140 }
6141
6142 struct sockaddr_un server_addr;
6143 sock_listen_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
6144 if(sock_listen_fd < 0)
6145 {
6146 LOG("socket() fail[%d].", errno);
6147 return -1;
6148 }
6149
6150 // Set O_NONBLOCK
6151 int flags = fcntl(sock_listen_fd, F_GETFL, 0);
6152 if (flags < 0)
6153 {
6154 LOG("Get flags error:%d", errno);
6155 goto error;
6156 }
6157 flags |= O_NONBLOCK;
6158 if (fcntl(sock_listen_fd, F_SETFL, flags) < 0)
6159 {
6160 LOG("Set flags error:%d", errno);
6161 goto error;
6162 }
6163
6164 unlink(SOCK_INFO_PATH);
6165 memset(&server_addr, 0, sizeof(struct sockaddr_un));
6166 server_addr.sun_family = AF_LOCAL;
6167 strcpy(server_addr.sun_path, SOCK_INFO_PATH);
6168 if(bind(sock_listen_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)))
6169 {
6170 LOG("bind() fail[%d].", errno);
6171 goto error;
6172 }
6173
6174 if(listen(sock_listen_fd, SOCK_CLIENT_MAX))
6175 {
6176 LOG("listen() fail[%d].", errno);
6177 goto error;
6178 }
6179
6180 sock_client_list = list_create(sock_cli_free_func);
6181 if(sock_client_list == NULL)
6182 {
6183 LOG("list_create() fail.");
6184 goto error;
6185 }
6186
6187 pthread_t info_pid, pack_pid, monitor_pid, urc_pid, bootconn_pid;
6188 pthread_attr_t thread_attr;
6189 pthread_attr_init(&thread_attr);
6190 if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
6191 {
6192 LOG("pthread_attr_setdetachstate() fail.");
6193 goto error;
6194 }
6195
6196 if(pthread_create(&info_pid, &thread_attr, info_main_pthread, NULL))
6197 {
6198 LOG("pthread_create() fail.");
6199 goto error;
6200 }
6201
6202 if(pthread_create(&pack_pid, &thread_attr, pack_process_thread, NULL))
6203 {
6204 LOG("pthread_create() fail.");
6205 goto error;
6206 }
6207
6208#if 0
6209 if(pthread_create(&urc_pid, &thread_attr, urc_process_thread, NULL))
6210 {
6211 LOG("pthread_create() fail.");
6212 goto error;
6213 }
6214#endif
6215
6216 ril_at_ready_process();
6217
6218 if(pthread_create(&monitor_pid, &thread_attr, net_monitor_thread, NULL))
6219 {
6220 LOG("pthread_create() fail.");
6221 }
6222
6223 //mbtk wyq for data_call_ex add start
6224 if(pthread_create(&bootconn_pid, &thread_attr, data_call_bootconn_pthread, NULL))
6225 {
6226 LOG("pthread_create() fail.");
6227 }
6228 //mbtk wyq for data_call_ex add end
6229
6230 pthread_attr_destroy(&thread_attr);
6231
6232 LOG("MBTK Information Server Start...");
6233
6234 return 0;
6235
6236error:
6237 close(sock_listen_fd);
6238 sock_listen_fd = -1;
6239 return -1;
6240}
6241
6242#if 0
6243int main(int argc, char *argv[])
6244{
6245 if(mbtk_info_server_start())
6246 {
6247 return -1;
6248 }
6249
6250 while(1)
6251 {
6252 sleep(24 * 60 * 60);
6253 }
6254
6255 return 0;
6256}
6257#endif
6258
6259