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