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