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