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