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