blob: 8417c193c3e97dc5816a5295358b71f08e01741a [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
2984 sprintf(cmd, "AT+CIREG=%d", reg);
2985 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;
3002 int tmp_int, tmp_reg;
3003 int err;
3004 char *tmp_str = NULL;
3005
3006 err = at_send_command_multiline("AT+CIREG?", "+CIREG:", &response);
3007 if (err < 0 || response->success == 0 || !response->p_intermediates){
3008 tmp_reg = 0;
3009 err = 0;
3010 goto exit;
3011 }
3012 char *line = response->p_intermediates->line;
3013 err = at_tok_start(&line);
3014 if (err < 0)
3015 {
3016 goto exit;
3017 }
3018 err = at_tok_nextint(&line, &tmp_int); // n/stat
3019 if (err < 0)
3020 {
3021 goto exit;
3022 }
liuyangc4ca9592024-06-06 15:43:50 +08003023
3024 tmp_reg = tmp_int;
r.xiao06db9a12024-04-14 18:51:15 -07003025
3026 LOG("net_ims_get reg : %u", tmp_reg);
3027
3028exit:
3029 at_response_free(response);
3030 *reg = tmp_reg;
3031 return err;
3032}
3033
3034
liubin281ac462023-07-19 14:22:54 +08003035/*
3036AT+CGDCONT?
3037+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,,,,
3038
3039+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
3040
3041OK
3042
3043
3044*/
wangyouqianged88c722023-11-22 16:33:43 +08003045#ifdef MBTK_AF_SUPPORT
3046mbtk_ip_type_enum default_iptype = MBTK_IP_TYPE_IPV4V6;
3047#endif
3048
liubin281ac462023-07-19 14:22:54 +08003049static int req_apn_get(void *data, int *data_len, int *cme_err)
3050{
3051 ATResponse *response = NULL;
3052 int err = at_send_command_multiline("AT+CGDCONT?", "+CGDCONT:", &response);
3053
3054 if (err < 0 || response->success == 0 || !response->p_intermediates){
3055 *cme_err = at_get_cme_error(response);
3056 goto exit;
3057 }
3058
3059 ATLine* lines_ptr = response->p_intermediates;
3060 char *line = NULL;
3061 int tmp_int;
3062 char *tmp_str = NULL;
3063 /*
3064 <apn_num[1]><cid[1]><ip_type[1]><apn_len[2]><apn><user_len[2]><user><pass_len[2]><pass><auth_len[2]><auth>...
3065 <cid[1]><ip_type[1]><apn_len[2]><apn><user_len[2]><user><pass_len[2]><pass><auth_len[2]><auth>
3066 */
3067 uint8* apn_num = (uint8*)data;
3068 uint8* data_ptr = (uint8*)(data + sizeof(uint8)); // Jump apn_num[1]
3069 mbtk_apn_info_t apn;
3070 while(lines_ptr)
3071 {
3072 line = lines_ptr->line;
3073 err = at_tok_start(&line);
3074 if (err < 0)
3075 {
3076 goto exit;
3077 }
3078
3079 err = at_tok_nextint(&line, &tmp_int); // cid
3080 if (err < 0)
3081 {
3082 goto exit;
3083 }
3084 // Only get CID 1-7
3085 if(tmp_int >= MBTK_APN_CID_MIN && tmp_int <= MBTK_APN_CID_MAX) {
3086 memset(&apn, 0x0, sizeof(mbtk_apn_info_t));
3087 apn.cid = tmp_int;
3088 *data_ptr++ = (uint8)tmp_int; // cid
3089
3090 err = at_tok_nextstr(&line, &tmp_str);// ip type
3091 if (err < 0)
3092 {
3093 goto exit;
3094 }
3095 if(!strcasecmp(tmp_str, "IP")) {
3096 *data_ptr++ = (uint8)MBTK_IP_TYPE_IP;
3097 apn.ip_type = MBTK_IP_TYPE_IP;
3098 } else if(!strcasecmp(tmp_str, "IPV6")) {
3099 *data_ptr++ = (uint8)MBTK_IP_TYPE_IPV6;
3100 apn.ip_type = MBTK_IP_TYPE_IPV6;
3101 } else if(!strcasecmp(tmp_str, "IPV4V6")) {
3102 *data_ptr++ = (uint8)MBTK_IP_TYPE_IPV4V6;
3103 apn.ip_type = MBTK_IP_TYPE_IPV4V6;
3104 } else {
3105 *data_ptr++ = (uint8)MBTK_IP_TYPE_PPP;
3106 apn.ip_type = MBTK_IP_TYPE_PPP;
3107 }
3108
wangyouqianged88c722023-11-22 16:33:43 +08003109#ifdef MBTK_AF_SUPPORT
3110 if(apn.cid == 1)
3111 {
3112 default_iptype = apn.ip_type;
3113 }
3114#endif
liubin281ac462023-07-19 14:22:54 +08003115 err = at_tok_nextstr(&line, &tmp_str); // apn
3116 if (err < 0)
3117 {
3118 goto exit;
3119 }
3120 if(str_empty(tmp_str)) {
3121 uint16_2_byte((uint16)0, data_ptr, false);
3122 data_ptr += sizeof(uint16);
3123 } else {
3124 uint16_2_byte((uint16)strlen(tmp_str), data_ptr, false);
3125 data_ptr += sizeof(uint16);
3126 memcpy(data_ptr, tmp_str, strlen(tmp_str));
3127 data_ptr += strlen(tmp_str);
3128 memcpy(apn.apn, tmp_str, strlen(tmp_str));
3129 }
3130
3131 if(apn_user_pass_set_by_cid(apn.cid, &apn)) {
3132 // user
3133 uint16_2_byte((uint16)0, data_ptr, false);
3134 data_ptr += sizeof(uint16);
3135
3136 // pass
3137 uint16_2_byte((uint16)0, data_ptr, false);
3138 data_ptr += sizeof(uint16);
3139
3140 // auth
3141 uint16_2_byte((uint16)0, data_ptr, false);
3142 data_ptr += sizeof(uint16);
3143 } else {
3144 // user
3145 if(str_empty(apn.user)) {
3146 uint16_2_byte((uint16)0, data_ptr, false);
3147 data_ptr += sizeof(uint16);
3148 } else {
3149 uint16_2_byte((uint16)strlen(apn.user), data_ptr, false);
3150 data_ptr += sizeof(uint16);
3151 memcpy(data_ptr, apn.user, strlen(apn.user));
3152 data_ptr += strlen(apn.user);
3153 }
3154
3155 // pass
3156 if(str_empty(apn.pass)) {
3157 uint16_2_byte((uint16)0, data_ptr, false);
3158 data_ptr += sizeof(uint16);
3159 } else {
3160 uint16_2_byte((uint16)strlen(apn.pass), data_ptr, false);
3161 data_ptr += sizeof(uint16);
3162 memcpy(data_ptr, apn.pass, strlen(apn.pass));
3163 data_ptr += strlen(apn.pass);
3164 }
3165
3166 // auth
3167 if(str_empty(apn.auth)) {
3168 uint16_2_byte((uint16)0, data_ptr, false);
3169 data_ptr += sizeof(uint16);
3170 } else {
3171 uint16_2_byte((uint16)strlen(apn.auth), data_ptr, false);
3172 data_ptr += sizeof(uint16);
3173 memcpy(data_ptr, apn.auth, strlen(apn.auth));
3174 data_ptr += strlen(apn.auth);
3175 }
3176 }
3177
3178 (*apn_num)++;
3179 }
3180
3181 lines_ptr = lines_ptr->p_next;
3182 }
3183
3184 *data_len = data_ptr - (uint8*)data;
3185
3186 goto exit;
3187exit:
3188 at_response_free(response);
3189 return err;
3190}
3191
3192#if 0
3193/*
3194LTE APN
3195AT+CFUN=4
3196AT*CGDFLT=1,IP,"private.vpdn",1,0,1,0,0,0,0,0,0,0,1,0,0,1,0,0,0,1
3197AT*CGDFAUTH=1,2,"noc@njcc.vpdn.js","123456"
3198AT+CFUN=1
3199AT+CEREG?
3200AT+CGDCONT?
3201
32022/3G APN
3203AT+CGREG?
3204AT+CGDCONT=6,IP,"private.vpdn"
3205AT*AUTHREQ=6,2,"noc@njcc.vpdn.js","123456"
3206AT+CGDATA="",6
3207AT+CGDCONT?
3208*/
3209static int req_apn_set_username(mbtk_apn_info_t *apn, int *cme_err)
3210{
3211 ATResponse *response = NULL;
3212 char cmd[400] = {0};
3213 int index = 0;
3214 int err = 0;
3215
3216 err = at_send_command_singleline("AT+COPS?", "+COPS:", &response);
3217 if (err < 0 || response->success == 0 || !response->p_intermediates){
3218 if(cme_err != NULL)
3219 *cme_err = at_get_cme_error(response);
3220 err = -1;
3221 goto apn_set;
3222 }
3223
3224 int tmp_int = 0;
3225 int state=0;
3226 char cmd_buf[64];
3227 char *line = response->p_intermediates->line;
3228 err = at_tok_start(&line);
3229 if (err < 0)
3230 {
3231 goto apn_set;
3232 }
3233 err = at_tok_nextint(&line, &tmp_int);
3234 if (err < 0)
3235 {
3236 goto apn_set;
3237 }
3238 err = at_tok_nextint(&line, &tmp_int);
3239 if (err < 0)
3240 {
3241 goto apn_set;
3242 }
3243 err = at_tok_nextstr(&line, &cmd_buf);
3244 if (err < 0)
3245 {
3246 goto apn_set;
3247 }
3248 err = at_tok_nextint(&line, &tmp_int);
3249 if (err < 0)
3250 {
3251 goto apn_set;
3252 }
3253 else
3254 state = tmp_int;
3255
3256apn_set:
3257 at_response_free(response);
3258 *cme_err = MBTK_INFO_ERR_CME_NON;
3259 //if(state == 7 && apn->cid == 1) //LTE && cid = 1
3260 if(0) //LTE && cid = 1
3261 {
3262 err = at_send_command("AT+CFUN=4", &response);
3263 if (err < 0 || response->success == 0){
3264 *cme_err = at_get_cme_error(response);
3265 goto exit;
3266 }
3267 at_response_free(response);
3268
3269 memset(cmd, 0, 400);
3270 index = 0;
3271 index += sprintf(cmd, "AT*CGDFLT=%d,", 1);
3272 switch(apn->ip_type) {
3273 case MBTK_IP_TYPE_IP: {
3274 index += sprintf(cmd + index,"\"IP\",");
3275 break;
3276 }
3277 case MBTK_IP_TYPE_IPV6: {
3278 index += sprintf(cmd + index,"\"IPV6\",");
3279 break;
3280 }
3281 case MBTK_IP_TYPE_IPV4V6: {
3282 index += sprintf(cmd + index,"\"IPV4V6\",");
3283 break;
3284 }
3285 default: {
3286 index += sprintf(cmd + index,"\"PPP\",");
3287 break;
3288 }
3289 }
3290
3291 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);
3292 err = at_send_command(cmd, &response);
3293 if (err < 0 || response->success == 0){
3294 *cme_err = at_get_cme_error(response);
3295 goto exit;
3296 }
3297 at_response_free(response);
3298
3299 memset(cmd, 0, 400);
3300 int cmd_auth=0;
3301 if(strstr(apn->auth,"NONE"))
3302 cmd_auth = 0;
3303 else if(strstr(apn->auth,"PAP"))
3304 cmd_auth = 1;
3305 else if(strstr(apn->auth,"CHAP"))
3306 cmd_auth = 2;
3307 else if(strstr(apn->auth,"PAP AND CHAP"))
3308 cmd_auth = 3;
3309 else
3310 goto exit;
3311
3312 sprintf(cmd,"AT*CGDFAUTH=1,%d,%s,%s",cmd_auth,apn->user,apn->pass);
3313
3314 err = at_send_command(cmd, &response);
3315 if (err < 0 || response->success == 0){
3316 *cme_err = at_get_cme_error(response);
3317 goto exit;
3318 }
3319 }
3320 else //2/3G
3321 {
3322 memset(cmd,0,400);
3323 index = 0;
3324 index += sprintf(cmd, "AT+CGDCONT=%d,", apn->cid);
3325 switch(apn->ip_type) {
3326 case MBTK_IP_TYPE_IP: {
3327 index += sprintf(cmd + index,"\"IP\",");
3328 break;
3329 }
3330 case MBTK_IP_TYPE_IPV6: {
3331 index += sprintf(cmd + index,"\"IPV6\",");
3332 break;
3333 }
3334 case MBTK_IP_TYPE_IPV4V6: {
3335 index += sprintf(cmd + index,"\"IPV4V6\",");
3336 break;
3337 }
3338 default: {
3339 index += sprintf(cmd + index,"\"PPP\",");
3340 break;
3341 }
3342 }
3343 index += sprintf(cmd + index, "\"%s\"", apn->apn);
3344
3345 err = at_send_command(cmd, &response);
3346 if (err < 0 || response->success == 0){
3347 *cme_err = at_get_cme_error(response);
3348 goto exit;
3349 }
3350 at_response_free(response);
3351
3352 memset(cmd,0,400);
3353 int cmd_auth=0;
3354 if(strstr(apn->auth,"NONE"))
3355 cmd_auth = 0;
3356 else if(strstr(apn->auth,"PAP"))
3357 cmd_auth = 1;
3358 else if(strstr(apn->auth,"CHAP"))
3359 cmd_auth = 2;
3360 else if(strstr(apn->auth,"PAP AND CHAP"))
3361 cmd_auth = 3;
3362 else
3363 goto exit;
3364
3365 sprintf(cmd, "AT*AUTHREQ=%d,%d,%s,%s",apn->cid,cmd_auth,apn->user,apn->pass);
3366 err = at_send_command(cmd, &response);
3367 if (err < 0 || response->success == 0){
3368 *cme_err = at_get_cme_error(response);
3369 goto exit;
3370 }
3371 }
3372
3373exit:
3374 at_response_free(response);
3375 return err;
3376}
3377#endif
3378
3379/*
3380AT+CGDCONT=1,"IPV4V6","cmnet"
3381OK
3382
3383AT*CGDFLT=1,"IPv4v6","reliance.grevpdn.zj",,,,,,,,,,,,,,,,,,1
3384OK
3385
3386*/
3387static int req_apn_set(mbtk_apn_info_t *apn, int *cme_err)
3388{
3389 ATResponse *response = NULL;
3390 char cmd[400] = {0};
3391 int index = 0;
3392 int err = 0;
3393
3394 index += sprintf(cmd, "AT+CGDCONT=%d,", apn->cid);
3395 switch(apn->ip_type) {
3396 case MBTK_IP_TYPE_IP: {
3397 index += sprintf(cmd + index,"\"IP\",");
3398 break;
3399 }
3400 case MBTK_IP_TYPE_IPV6: {
3401 index += sprintf(cmd + index,"\"IPV6\",");
3402 break;
3403 }
3404 case MBTK_IP_TYPE_IPV4V6: {
3405 index += sprintf(cmd + index,"\"IPV4V6\",");
3406 break;
3407 }
3408 default: {
3409 index += sprintf(cmd + index,"\"PPP\",");
3410 break;
3411 }
3412 }
3413 if(strlen(apn->apn) > 0) {
3414 index += sprintf(cmd + index,"\"%s\"", apn->apn);
b.liudfec1e12024-06-06 16:38:59 +08003415 }
liubin281ac462023-07-19 14:22:54 +08003416
3417 err = at_send_command(cmd, &response);
3418 if (err < 0 || response->success == 0){
3419 if(cme_err) {
3420 *cme_err = at_get_cme_error(response);
3421 }
3422 goto exit;
3423 }
3424
3425 if(!str_empty(apn->user) || !str_empty(apn->pass)) {
3426 at_response_free(response);
3427
3428 memset(cmd,0,400);
3429 int cmd_auth=0;
3430 if(strstr(apn->auth,"NONE"))
3431 cmd_auth = 0;
3432 else if(strstr(apn->auth,"PAP"))
3433 cmd_auth = 1;
3434 else if(strstr(apn->auth,"CHAP"))
3435 cmd_auth = 2;
3436#if 0
3437 else if(strstr(apn->auth,"PAP AND CHAP"))
3438 cmd_auth = 3;
3439#endif
3440 else
3441 goto exit;
3442
3443 sprintf(cmd, "AT*AUTHREQ=%d,%d,%s,%s",apn->cid,cmd_auth,apn->user,apn->pass);
3444 err = at_send_command(cmd, &response);
3445 if (err < 0 || response->success == 0){
3446 *cme_err = at_get_cme_error(response);
3447 goto exit;
3448 }
3449 }
3450
3451exit:
3452 at_response_free(response);
3453 return err;
3454}
3455
liuyang1cefd852024-04-24 18:30:53 +08003456static int req_apn_del(int data, int *cme_err)
liuyang0e49d9a2024-04-23 21:04:54 +08003457{
3458 ATResponse *response = NULL;
3459 char cmd[64]={0};
liuyang1cefd852024-04-24 18:30:53 +08003460 sprintf(cmd, "AT+CGDCONT=%d", data);
liuyang0e49d9a2024-04-23 21:04:54 +08003461 int err = at_send_command(cmd, &response);
3462 if (err < 0 || response->success == 0){
3463 if(cme_err) {
3464 *cme_err = at_get_cme_error(response);
3465 }
3466 goto exit;
3467 }
3468
3469exit:
3470 at_response_free(response);
3471 return err;
3472}
3473
3474
liubin281ac462023-07-19 14:22:54 +08003475int wait_cgact_complete(int timeout)
3476{
3477 int count = timeout * 10; // timeout * 1000 / 100
3478 int i = 0;
3479
3480 while(cgact_wait.waitting && i < count) {
3481 i++;
3482 usleep(100000); // 100ms
3483 }
3484
3485 if(i == count) { // Timeout
3486 return -1;
3487 } else {
3488 return 0;
3489 }
3490}
3491
3492/*
3493AT+CGDATA="",6
3494CONNECT
3495
3496OK
3497
3498AT+CFUN=1
3499
3500OK
3501
3502*/
3503static int req_data_call_user_start(int cid, int *cme_err)
3504{
3505 ATResponse *response = NULL;
3506 char cmd[400] = {0};
3507 int index = 0;
3508 int err = 0;
3509
3510 err = at_send_command_singleline("AT+COPS?", "+COPS:", &response);
3511 if (err < 0 || response->success == 0 || !response->p_intermediates){
3512 if(cme_err != NULL)
3513 *cme_err = at_get_cme_error(response);
3514 err = -1;
3515 goto exit;
3516 }
3517
3518 int tmp_int;
3519 char cmd_buf[64];
3520 char *line = response->p_intermediates->line;
3521 err = at_tok_start(&line);
3522 if (err < 0)
3523 {
3524 goto exit;
3525 }
3526 err = at_tok_nextint(&line, &tmp_int);
3527 if (err < 0)
3528 {
3529 goto exit;
3530 }
3531 err = at_tok_nextint(&line, &tmp_int);
3532 if (err < 0)
3533 {
3534 goto exit;
3535 }
3536 err = at_tok_nextstr(&line, &cmd_buf);
3537 if (err < 0)
3538 {
3539 goto exit;
3540 }
3541 err = at_tok_nextint(&line, &tmp_int);
3542 if (err < 0)
3543 {
3544 goto exit;
3545 }
3546 at_response_free(response);
3547
3548 if(tmp_int == 7 && cid == 1) //LTE && cid = 1
3549 {
3550 ATResponse *response = NULL;
3551 char cmd[400] = {0};
3552 int err = 0;
3553
3554 err = at_send_command("AT+CFUN=1", &response);
3555 if (err < 0 || response->success == 0){
3556 if(cme_err) {
3557 *cme_err = at_get_cme_error(response);
3558 }
3559 goto exit;
3560 }
3561 }
3562 else
3563 {
3564 ATResponse *response = NULL;
3565 char cmd[400] = {0};
3566 int err = 0;
3567 sprintf(cmd, "AT+CGDATA=\"\",%d", cid);
3568 err = at_send_command(cmd, &response);
3569 if (err < 0 || response->success == 0){
3570 if(cme_err) {
3571 *cme_err = at_get_cme_error(response);
3572 }
3573 goto exit;
3574 }
3575 }
3576
3577exit:
3578 at_response_free(response);
3579 return err;
3580}
3581
3582/*
3583AT+CGACT?
3584+CGACT: 1,1
3585+CGACT: 8,1
3586OK
3587
3588AT+CGACT=1,<cid>
3589OK
3590
3591*/
3592static int req_data_call_start(int cid, int *cme_err)
3593{
3594 ATResponse *response = NULL;
3595 char cmd[400] = {0};
3596 int err = 0;
3597#if 0
3598 err = at_send_command_multiline("AT+CGACT?", "+CGACT:", &response);
3599 if (err < 0 || response->success == 0 || !response->p_intermediates){
3600 *cme_err = at_get_cme_error(response);
3601 goto exit;
3602 }
3603 ATLine* lines_ptr = response->p_intermediates;
3604 char *line = NULL;
3605 int tmp_int;
3606 while(lines_ptr)
3607 {
3608 line = lines_ptr->line;
3609 err = at_tok_start(&line);
3610 if (err < 0)
3611 {
3612 goto exit;
3613 }
3614
3615 err = at_tok_nextint(&line, &tmp_int); // cid
3616 if (err < 0)
3617 {
3618 goto exit;
3619 }
3620 if(tmp_int == cid) { // Found cid
3621 err = at_tok_nextint(&line, &tmp_int); // cid
3622 if (err < 0)
3623 {
3624 goto exit;
3625 }
3626 if(tmp_int == 1) { // This cid has active.
3627 goto net_config;
3628 } else {
3629 goto cid_active;
3630 }
3631 break;
3632 }
3633
3634 lines_ptr = lines_ptr->p_next;
3635 }
3636
3637 if(lines_ptr == NULL) { // No found this cid.
3638 LOGE("No found cid : %d", cid);
3639 goto exit;
3640 }
3641 at_response_free(response);
3642
3643 // Start active cid.
3644cid_active:
3645#endif
3646
3647 sprintf(cmd, "AT+CGACT=1,%d", cid);
3648 err = at_send_command(cmd, &response);
3649 if (err < 0 || response->success == 0){
3650 if(cme_err) {
3651 *cme_err = at_get_cme_error(response);
3652 }
3653 goto exit;
3654 }
3655
3656exit:
3657 at_response_free(response);
3658 return err;
3659}
3660
3661/*
3662AT+CGACT=0,<cid>
3663OK
3664
3665*/
3666static int req_data_call_stop(int cid, int *cme_err)
3667{
3668 ATResponse *response = NULL;
3669 char cmd[400] = {0};
3670 int err = 0;
3671#if 0
3672 err = at_send_command_multiline("AT+CGACT?", "+CGACT:", &response);
3673 if (err < 0 || response->success == 0 || !response->p_intermediates){
3674 *cme_err = at_get_cme_error(response);
3675 goto exit;
3676 }
3677 ATLine* lines_ptr = response->p_intermediates;
3678 char *line = NULL;
3679 int tmp_int;
3680 while(lines_ptr)
3681 {
3682 line = lines_ptr->line;
3683 err = at_tok_start(&line);
3684 if (err < 0)
3685 {
3686 goto exit;
3687 }
3688
3689 err = at_tok_nextint(&line, &tmp_int); // cid
3690 if (err < 0)
3691 {
3692 goto exit;
3693 }
3694 if(tmp_int == cid) { // Found cid
3695 err = at_tok_nextint(&line, &tmp_int); // cid
3696 if (err < 0)
3697 {
3698 goto exit;
3699 }
3700 if(tmp_int == 1) { // This cid has active.
3701 goto net_config;
3702 } else {
3703 goto cid_active;
3704 }
3705 break;
3706 }
3707
3708 lines_ptr = lines_ptr->p_next;
3709 }
3710
3711 if(lines_ptr == NULL) { // No found this cid.
3712 LOGE("No found cid : %d", cid);
3713 goto exit;
3714 }
3715 at_response_free(response);
3716
3717 // Start active cid.
3718cid_active:
3719#endif
3720
3721 sprintf(cmd, "AT+CGACT=0,%d", cid);
3722 err = at_send_command(cmd, &response);
3723 if (err < 0 || response->success == 0){
3724 *cme_err = at_get_cme_error(response);
3725 goto exit;
3726 }
3727
3728exit:
3729 at_response_free(response);
3730 return err;
3731}
3732
3733/*
3734IPv4 : 10.255.74.26
3735IPv6 : 254.128.0.0.0.0.0.0.0.1.0.2.144.5.212.239
3736*/
3737static bool is_ipv4(const char *ip)
3738{
3739 const char *ptr = ip;
3740 int count = 0;
3741 while(*ptr) {
3742 if(*ptr == '.')
3743 count++;
3744 ptr++;
3745 }
3746
3747 if(count == 3) {
3748 return true;
3749 } else {
3750 return false;
3751 }
3752}
3753
3754/*
3755AT+CGCONTRDP=1
3756+CGCONTRDP: 1,7,"cmnet-2.MNC000.MCC460.GPRS","10.255.74.26","","223.87.253.100","223.87.253.253","","",0,0
3757+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
3758
3759OK
3760
3761*/
3762static int req_data_call_state_get(int cid, mbtk_ipv4_info_t *ipv4, mbtk_ipv6_info_t *ipv6, int *cme_err)
3763{
3764 ATResponse *response = NULL;
3765 char cmd[50] = {0};
3766 int err = 0;
3767
3768 sprintf(cmd, "AT+CGCONTRDP=%d", cid);
3769
3770 err = at_send_command_multiline(cmd, "+CGCONTRDP:", &response);
3771 if (err < 0 || response->success == 0 || !response->p_intermediates){
3772 *cme_err = at_get_cme_error(response);
3773 goto exit;
3774 }
3775 ATLine* lines_ptr = response->p_intermediates;
3776 char *line = NULL;
3777 int tmp_int;
3778 char *tmp_ptr = NULL;
3779 while(lines_ptr)
3780 {
3781 line = lines_ptr->line;
3782 err = at_tok_start(&line);
3783 if (err < 0)
3784 {
3785 goto exit;
3786 }
3787
3788 err = at_tok_nextint(&line, &tmp_int); // cid
3789 if (err < 0)
3790 {
3791 goto exit;
3792 }
3793 err = at_tok_nextint(&line, &tmp_int); // bearer_id
3794 if (err < 0)
3795 {
3796 goto exit;
3797 }
3798 err = at_tok_nextstr(&line, &tmp_ptr); // APN
3799 if (err < 0)
3800 {
3801 goto exit;
3802 }
3803
3804 err = at_tok_nextstr(&line, &tmp_ptr); // IP
3805 if (err < 0 || str_empty(tmp_ptr))
3806 {
3807 goto exit;
3808 }
3809 if(is_ipv4(tmp_ptr)) {
3810 if(inet_pton(AF_INET, tmp_ptr, &(ipv4->IPAddr)) < 0) {
3811 LOGE("inet_pton() fail.");
3812 err = -1;
3813 goto exit;
3814 }
3815
3816 ipv4->valid = true;
3817 //log_hex("IPv4", &(ipv4->IPAddr), sizeof(struct in_addr));
3818 } else {
3819 if(str_2_ipv6(tmp_ptr, &(ipv6->IPV6Addr))) {
3820 LOGE("str_2_ipv6() fail.");
3821 err = -1;
3822 goto exit;
3823 }
3824
3825 ipv6->valid = true;
3826 //log_hex("IPv6", &(ipv6->IPV6Addr), 16);
3827 }
3828
3829 err = at_tok_nextstr(&line, &tmp_ptr); // Gateway
3830 if (err < 0)
3831 {
3832 goto exit;
3833 }
3834 if(!str_empty(tmp_ptr)) { // No found gateway
3835 if(is_ipv4(tmp_ptr)) {
3836 if(inet_pton(AF_INET, tmp_ptr, &(ipv4->GateWay)) < 0) {
3837 LOGE("inet_pton() fail.");
3838 err = -1;
3839 goto exit;
3840 }
3841
3842 //log_hex("IPv4", &(ipv4->GateWay), sizeof(struct in_addr));
3843 } else {
3844 if(str_2_ipv6(tmp_ptr, &(ipv6->GateWay))) {
3845 LOGE("str_2_ipv6() fail.");
3846 err = -1;
3847 goto exit;
3848 }
3849
3850 //log_hex("IPv6", &(ipv6->GateWay), 16);
3851 }
3852 }
3853
3854 err = at_tok_nextstr(&line, &tmp_ptr); // prim_DNS
3855 if (err < 0)
3856 {
3857 goto exit;
3858 }
3859 if(!str_empty(tmp_ptr)) { // No found Primary DNS
3860 if(is_ipv4(tmp_ptr)) {
3861 if(inet_pton(AF_INET, tmp_ptr, &(ipv4->PrimaryDNS)) < 0) {
3862 LOGE("inet_pton() fail.");
3863 err = -1;
3864 goto exit;
3865 }
3866
3867 //log_hex("IPv4", &(ipv4->PrimaryDNS), sizeof(struct in_addr));
3868 } else {
3869 if(str_2_ipv6(tmp_ptr, &(ipv6->PrimaryDNS))) {
3870 LOGE("str_2_ipv6() fail.");
3871 err = -1;
3872 goto exit;
3873 }
3874
3875 //log_hex("IPv6", &(ipv6->PrimaryDNS), 16);
3876 }
3877 }
3878
3879 err = at_tok_nextstr(&line, &tmp_ptr); // sec_DNS
3880 if (err < 0)
3881 {
3882 goto exit;
3883 }
3884 if(!str_empty(tmp_ptr)) { // No found Secondary DNS
3885 if(is_ipv4(tmp_ptr)) {
3886 if(inet_pton(AF_INET, tmp_ptr, &(ipv4->SecondaryDNS)) < 0) {
3887 LOGE("inet_pton() fail.");
3888 err = -1;
3889 goto exit;
3890 }
3891
3892 //log_hex("IPv4", &(ipv4->SecondaryDNS), sizeof(struct in_addr));
3893 } else {
3894 if(str_2_ipv6(tmp_ptr, &(ipv6->SecondaryDNS))) {
3895 LOGE("str_2_ipv6() fail.");
3896 err = -1;
3897 goto exit;
3898 }
3899
3900 //log_hex("IPv6", &(ipv6->SecondaryDNS), 16);
3901 }
3902 }
3903
3904 lines_ptr = lines_ptr->p_next;
3905 }
3906
3907exit:
3908 at_response_free(response);
3909 return err;
3910}
3911
3912/*
3913AT+CGCONTRDP
3914+CGCONTRDP: 1,5,"cmnet.MNC000.MCC460.GPRS","10.156.238.86","","223.87.253.100","223.87.253.253","","",0,0
3915+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
3916+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
3917OK
3918
3919*/
3920static int apn_state_get(list_node_t **apn_list)
3921{
3922 ATResponse *response = NULL;
3923 int err = 0;
3924 *apn_list = list_create(NULL);
3925 if(*apn_list == NULL)
3926 {
3927 LOG("list_create() fail.");
3928 return -1;
3929 }
3930
3931 err = at_send_command_multiline("AT+CGCONTRDP", "+CGCONTRDP:", &response);
3932 if (err < 0 || response->success == 0 || !response->p_intermediates){
3933 goto exit;
3934 }
3935 ATLine* lines_ptr = response->p_intermediates;
3936 char *line = NULL;
3937 int tmp_int;
3938 char *tmp_ptr = NULL;
3939 int cid_current = 0;
3940 info_apn_ip_t *apn = NULL;
3941 while(lines_ptr)
3942 {
3943 line = lines_ptr->line;
3944 err = at_tok_start(&line);
3945 if (err < 0)
3946 {
3947 goto exit;
3948 }
3949
3950 err = at_tok_nextint(&line, &tmp_int); // cid
3951 if (err < 0)
3952 {
3953 goto exit;
3954 }
3955
3956 if(tmp_int != 1 && tmp_int != 8) { // Not process cid 1 and 8.
3957 if(cid_current != tmp_int) { // New cid.
3958 apn = (info_apn_ip_t*)malloc(sizeof(info_apn_ip_t));
3959 if(apn == NULL) {
3960 goto exit;
3961 }
3962 memset(apn, 0, sizeof(info_apn_ip_t));
3963 apn->cid = tmp_int;
3964 cid_current = tmp_int;
3965
3966 list_add(*apn_list, apn);
3967 }
3968 err = at_tok_nextint(&line, &tmp_int); // bearer_id
3969 if (err < 0)
3970 {
3971 goto exit;
3972 }
3973 err = at_tok_nextstr(&line, &tmp_ptr); // APN
3974 if (err < 0)
3975 {
3976 goto exit;
3977 }
3978
3979 err = at_tok_nextstr(&line, &tmp_ptr); // IP
3980 if (err < 0 || str_empty(tmp_ptr))
3981 {
3982 goto exit;
3983 }
3984 if(is_ipv4(tmp_ptr)) {
3985 apn->ipv4_valid = true;
3986 memcpy(apn->ipv4, tmp_ptr, strlen(tmp_ptr));
3987 } else {
3988 apn->ipv6_valid = true;
3989 uint8 tmp_ipv6[16];
3990 if(str_2_ipv6(tmp_ptr, tmp_ipv6)) {
3991 LOGE("str_2_ipv6() fail.");
3992 err = -1;
3993 goto exit;
3994 }
3995
3996 if(inet_ntop(AF_INET6, tmp_ipv6, apn->ipv6, 50) == NULL) {
3997 err = -1;
3998 LOGE("inet_ntop ipv6 ip fail.");
3999 goto exit;
4000 }
4001 }
4002 }
4003
4004 lines_ptr = lines_ptr->p_next;
4005 }
4006
4007exit:
4008 at_response_free(response);
4009 return err;
4010}
4011
4012mbtk_info_err_enum call_pack_req_process(sock_client_info_t* cli_info, mbtk_info_pack_t* pack);
4013mbtk_info_err_enum sms_pack_req_process(sock_client_info_t* cli_info, mbtk_info_pack_t* pack);
4014mbtk_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 -08004015
liubin281ac462023-07-19 14:22:54 +08004016//mbtk wyq for data_call_ex add start
4017void data_call_bootconn_save(int cid, int bootconn);
4018//mbtk wyq for data_call_ex add end
4019
4020//void net_list_free(void *data);
4021// Return MBTK_INFO_ERR_SUCCESS,will call pack_error_send() to send RSP.
4022// Otherwise, do not call pack_error_send().
4023static mbtk_info_err_enum pack_req_process(sock_client_info_t* cli_info, mbtk_info_pack_t* pack)
4024{
4025 if(pack->info_id > MBTK_INFO_ID_CALL_BEGIN && pack->info_id < MBTK_INFO_ID_CALL_END) {
4026 return call_pack_req_process(cli_info, pack);
4027 } else if(pack->info_id > MBTK_INFO_ID_SMS_BEGIN && pack->info_id < MBTK_INFO_ID_SMS_END) {
4028 return sms_pack_req_process(cli_info, pack);
4029 } else if(pack->info_id > MBTK_INFO_ID_PB_BEGIN && pack->info_id < MBTK_INFO_ID_PB_END) {
4030 return pb_pack_req_process(cli_info, pack);
4031 } else {
4032 mbtk_info_err_enum err = MBTK_INFO_ERR_SUCCESS;
4033 int cme_err = MBTK_INFO_ERR_CME_NON;
4034 switch(pack->info_id)
4035 {
4036 case MBTK_INFO_ID_DEV_IMEI_REQ: // <string> IMEI
4037 {
4038 if(pack->data_len == 0 || pack->data == NULL) // Get IMEI
4039 {
4040 char imei[20] = {0};
4041 if(req_imei_get(imei, &cme_err) || strlen(imei) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4042 {
4043 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4044 err = MBTK_INFO_ERR_CME + cme_err;
4045 } else {
4046 err = MBTK_INFO_ERR_UNKNOWN;
4047 }
4048 LOG("Get IMEI fail.");
4049 }
4050 else
4051 {
4052 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_IMEI_RSP, imei, strlen(imei));
4053 }
4054 }
4055 else // Set IMEI(Unsupport).
4056 {
4057 err = MBTK_INFO_ERR_UNSUPPORTED;
4058 LOG("Unsupport set IMEI.");
4059 }
4060 break;
4061 }
4062 case MBTK_INFO_ID_DEV_SN_REQ: // <string> SN
4063 {
4064 if(pack->data_len == 0 || pack->data == NULL) // Get SN
4065 {
4066 char sn[20] = {0};
4067 if(req_sn_get(sn, &cme_err) || strlen(sn) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4068 {
4069 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4070 err = MBTK_INFO_ERR_CME + cme_err;
4071 } else {
4072 err = MBTK_INFO_ERR_UNKNOWN;
4073 }
4074 LOG("Get SN fail.");
4075 }
4076 else
4077 {
4078 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_SN_RSP, sn, strlen(sn));
4079 }
4080 }
4081 else // Set SN(Unsupport).
4082 {
4083 err = MBTK_INFO_ERR_UNSUPPORTED;
4084 LOG("Unsupport set SN.");
4085 }
4086 break;
4087 }
4088 case MBTK_INFO_ID_DEV_MEID_REQ: // <string> MEID (Only for CDMA)
4089 {
4090 if(pack->data_len == 0 || pack->data == NULL) // Get MEID
4091 {
4092 err = MBTK_INFO_ERR_UNSUPPORTED;
4093 LOG("Support only for CDMA.");
4094 }
4095 else // Set MEID(Unsupport).
4096 {
4097 err = MBTK_INFO_ERR_UNSUPPORTED;
4098 LOG("Unsupport set MEID.");
4099 }
4100 break;
4101 }
4102 case MBTK_INFO_ID_DEV_VERSION_REQ: // <string> VERSION
4103 {
4104 if(pack->data_len == 0 || pack->data == NULL) // Get VERSION
4105 {
4106 char version[50] = {0};
4107 if(req_version_get(version, &cme_err) || strlen(version) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4108 {
4109 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4110 err = MBTK_INFO_ERR_CME + cme_err;
4111 } else {
4112 err = MBTK_INFO_ERR_UNKNOWN;
4113 }
4114 LOG("Get Version fail.");
4115 }
4116 else
4117 {
4118 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_VERSION_RSP, version, strlen(version));
4119 }
4120 }
4121 else // Set VERSION(Unsupport).
4122 {
4123 err = MBTK_INFO_ERR_UNSUPPORTED;
4124 LOG("Unsupport set VERSION.");
4125 }
4126 break;
4127 }
4128 case MBTK_INFO_ID_DEV_MODEL_REQ: //MODEL
4129 {
4130 if(pack->data_len == 0 || pack->data == NULL) // Get MODEL
4131 {
4132 char model[50] = {0};
4133 if(req_model_get(model, &cme_err) || strlen(model) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4134 {
4135 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4136 err = MBTK_INFO_ERR_CME + cme_err;
4137 } else {
4138 err = MBTK_INFO_ERR_UNKNOWN;
4139 }
4140 LOG("Get model fail.");
4141 }
4142 else
4143 {
4144 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_VERSION_RSP, model, strlen(model));
4145 }
4146 }
4147 else // Set model(Unsupport).
4148 {
4149 err = MBTK_INFO_ERR_UNSUPPORTED;
4150 LOG("Unsupport set model.");
4151 }
4152 break;
4153 }
4154 case MBTK_INFO_ID_DEV_MODEM_REQ: //MODEM
4155 {
4156 if(pack->data_len == 0 || pack->data == NULL) // Get MODEM
4157 {
4158 int modem = -1;
4159 if(req_modem_get(&modem, &cme_err) || modem < 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4160 {
4161 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4162 err = MBTK_INFO_ERR_CME + cme_err;
4163 } else {
4164 err = MBTK_INFO_ERR_UNKNOWN;
4165 }
4166 LOG("Get modem fail.");
4167 }
4168 else
4169 {
4170 uint8 modem_type = (uint8)modem;
4171 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_MODEM_RSP, &modem_type, sizeof(uint8));
4172 }
4173 }
4174 else // Set modem
4175 {
4176 mbtk_modem_info_t *modem = (mbtk_modem_info_t *)pack->data;
4177 if(pack->data_len != sizeof(mbtk_modem_info_t))
4178 {
4179 err = MBTK_INFO_ERR_REQ_PARAMETER;
4180 LOG("Set modem error.");
4181 break;
4182 }
4183 if(req_modem_set(modem, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4184 {
4185 LOG("Set modem fail.");
4186 err = MBTK_INFO_ERR_FORMAT;
4187 } else {
4188 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_MODEM_RSP, NULL, 0);
4189 }
4190 }
4191 break;
4192 }
4193 case MBTK_INFO_ID_DEV_TIME_REQ: // <uint8><string> YYYY-MM-DD-HH:MM:SS
4194 {
4195 if(pack->data_len == 0 || pack->data == NULL) // Get Time
4196 {
4197 int type = -1;
4198 if(req_time_get(&type, &cme_err) || type < 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4199 {
4200 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4201 err = MBTK_INFO_ERR_CME + cme_err;
4202 } else {
4203 err = MBTK_INFO_ERR_UNKNOWN;
4204 }
4205 LOG("Get Time fail.");
4206 }
4207 else
4208 {
4209 uint8 time_type = (uint8)type;
4210 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_TIME_RSP, &time_type, sizeof(uint8));
4211 }
4212 }
4213 else // Set Time
4214 {
4215 if(pack->data_len == sizeof(uint8)) {
4216 if(req_time_set(*(pack->data), NULL, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4217 {
4218 LOG("Set Time fail.");
4219 err = MBTK_INFO_ERR_FORMAT;
4220 } else {
4221 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_TIME_RSP, NULL, 0);
4222 }
4223 } else {
4224 char time_ptr[100] = {0};
4225 memcpy(time_ptr, pack->data + sizeof(uint8), pack->data_len - sizeof(uint8));
4226 if(req_time_set(*(pack->data), time_ptr, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4227 {
4228 LOG("Set Time fail.");
4229 err = MBTK_INFO_ERR_FORMAT;
4230 } else {
4231 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_TIME_RSP, NULL, 0);
4232 }
4233 }
4234 }
4235 break;
4236 }
4237 case MBTK_INFO_ID_NET_TIME_REQ: // <string> YYYY-MM-DD-HH:MM:SS
4238 {
4239 if(pack->data_len == 0 || pack->data == NULL) // Get Time
4240 {
4241 char time[100];
4242 if(req_net_time_get(time, &cme_err) || strlen(time) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4243 {
4244 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4245 err = MBTK_INFO_ERR_CME + cme_err;
4246 } else {
4247 err = MBTK_INFO_ERR_UNKNOWN;
4248 }
4249 LOG("Get Time fail.");
4250 }
4251 else
4252 {
4253 char time_ser[100]={0};
4254 memcpy(time_ser,time,strlen(time));
4255 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_TIME_RSP, time_ser, strlen(time_ser));
4256 }
4257 }
4258 else // Set Time
4259 {
4260 err = MBTK_INFO_ERR_UNSUPPORTED;
4261 LOG("Unsupport set TIME.");
4262 }
4263 break;
4264 }
4265 case MBTK_INFO_ID_DEV_VOLTE_REQ: // <uint8> 0:Close 1:Open
4266 {
4267 if(pack->data_len == 0 || pack->data == NULL) // Get VoLTE state.
4268 {
4269 int state;
4270 if(req_volte_get(&state, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4271 {
4272 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4273 err = MBTK_INFO_ERR_CME + cme_err;
4274 } else {
4275 err = MBTK_INFO_ERR_UNKNOWN;
4276 }
4277 LOG("Get VoLTE state fail.");
4278 }
4279 else
4280 {
4281 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_VOLTE_RSP, &state, sizeof(uint8));
4282 }
4283 }
4284 else // Set VoLTE state.
4285 {
4286 uint8 on = *(pack->data);
4287 if(pack->data_len != sizeof(uint8) || (on != 0 && on != 1))
4288 {
4289 err = MBTK_INFO_ERR_REQ_PARAMETER;
4290 LOG("Set VOLTE parameter error.");
4291 break;
4292 }
4293
4294 if(req_volte_set(on, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4295 {
4296 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4297 err = MBTK_INFO_ERR_CME + cme_err;
4298 } else {
4299 err = MBTK_INFO_ERR_UNKNOWN;
4300 }
4301 LOG("Set VoLTE state fail.");
4302 }
4303 else
4304 {
4305 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_VOLTE_RSP, NULL, 0);
4306
4307 // Restart is required to take effect.
4308 LOG("Will reboot system...");
4309 }
4310 }
4311 break;
4312 }
4313 case MBTK_INFO_ID_DEV_TEMP_REQ: // <string> Temperature
4314 {
4315 if(pack->data_len == sizeof(uint8) && pack->data) {
4316 int temp;
4317 if(req_temp_get(*(pack->data), &temp, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4318 {
4319 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4320 err = MBTK_INFO_ERR_CME + cme_err;
4321 } else {
4322 err = MBTK_INFO_ERR_UNKNOWN;
4323 }
4324 LOG("Get temperature fail.");
4325 }
4326 else
4327 {
4328 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_TEMP_RSP, &temp, sizeof(uint8));
4329 }
4330 } else {
4331 err = MBTK_INFO_ERR_FORMAT;
4332 LOG("Unsupport get Temperature.");
4333 }
4334 break;
4335 }
4336 case MBTK_INFO_ID_SIM_PLMN_REQ: // plmn
4337 {
4338 if(pack->data_len == 0 || pack->data == NULL) // plmn
4339 {
4340 mbtk_plmn_info plmn;
4341 if(req_plmn_get(&plmn, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4342 {
4343 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4344 err = MBTK_INFO_ERR_CME + cme_err;
4345 } else {
4346 err = MBTK_INFO_ERR_UNKNOWN;
4347 }
4348 LOG("Get PLMN fail.");
4349 }
4350 else
4351 {
4352 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_PLMN_RSP, &plmn, sizeof(mbtk_plmn_info));
4353 }
4354 }
4355 else // Set
4356 {
4357 err = MBTK_INFO_ERR_UNSUPPORTED;
4358 LOG("Set sim state fail.");
4359 }
4360 break;
4361 }
4362 case MBTK_INFO_ID_SIM_STATE_REQ: // mbtk_sim_state_enum
4363 {
4364 if(pack->data_len == 0 || pack->data == NULL) // Get sim state.
4365 {
4366 uint8 sim_state = (uint8)getSIMStatus();
4367 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_STATE_RSP, &sim_state, sizeof(uint8));
4368 }
4369 else // Set
4370 {
4371 err = MBTK_INFO_ERR_UNSUPPORTED;
4372 LOG("Set sim state fail.");
4373 }
4374 break;
4375 }
4376 case MBTK_INFO_ID_SIM_STYPE_REQ: // mbtk_sim_card_type_enum
4377 {
4378 if(pack->data_len == 0 || pack->data == NULL) // Get sim card type
4379 {
4380 uint8 sim_card_type;
4381 if(req_sim_card_type_get(&sim_card_type, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4382 {
4383 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4384 err = MBTK_INFO_ERR_CME + cme_err;
4385 } else {
4386 err = MBTK_INFO_ERR_UNKNOWN;
4387 }
4388 LOG("Get IMSI fail.");
4389 }
4390 else
4391 {
4392 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_STYPE_RSP, &sim_card_type, sizeof(uint8));
4393 }
4394 }
4395 else // Set
4396 {
4397 err = MBTK_INFO_ERR_UNSUPPORTED;
4398 LOG("Set sim state fail.");
4399 }
4400 break;
4401 }
4402 case MBTK_INFO_ID_SIM_PINPUK_TIMES_REQ: // mbtk_pin_puk_last_times
4403 {
4404 if(pack->data_len == 0 || pack->data == NULL) // Get sim card type
4405 {
4406 mbtk_pin_puk_last_times pin_puk_last_times;
4407 if(req_pin_puk_last_times_get(&pin_puk_last_times, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4408 {
4409 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4410 err = MBTK_INFO_ERR_CME + cme_err;
4411 } else {
4412 err = MBTK_INFO_ERR_UNKNOWN;
4413 }
4414 LOG("Get IMSI fail.");
4415 }
4416 else
4417 {
4418 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_PINPUK_TIMES_RSP, &pin_puk_last_times, sizeof(mbtk_pin_puk_last_times));
4419 }
4420 }
4421 else // Set
4422 {
4423 err = MBTK_INFO_ERR_UNSUPPORTED;
4424 LOG("Set sim state fail.");
4425 }
4426 break;
4427 }
4428 case MBTK_INFO_ID_SIM_ENABLE_PIN_REQ: // <string> PIN
4429 {
4430 if(pack->data_len == 0 || pack->data == NULL) // Enable PIN
4431 {
4432 err = MBTK_INFO_ERR_UNSUPPORTED;
4433 LOG("Unsupport GET PIN.");
4434 }
4435 else // Enable PIN
4436 {
4437 mbtk_enable_pin_info *pin = NULL;
4438 pin = (mbtk_enable_pin_info *)pack->data;
4439 if(req_pin_enable(pin, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4440 {
4441 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4442 err = MBTK_INFO_ERR_CME + cme_err;
4443 } else {
4444 err = MBTK_INFO_ERR_UNKNOWN;
4445 }
4446 LOG("Get IMSI fail.");
4447 }
4448 else
4449 {
4450 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_ENABLE_PIN_RSP, NULL, 0);
4451 }
4452 }
4453 break;
4454 }
4455 case MBTK_INFO_ID_SIM_PIN_REQ: // <string> PIN
4456 {
4457 if(pack->data_len == 0 || pack->data == NULL) // PIN
4458 {
4459 err = MBTK_INFO_ERR_UNSUPPORTED;
4460 LOG("Unsupport GET PIN.");
4461 }
4462 else // Set PIN
4463 {
4464 char pin[16] = {0};
4465 memcpy(pin, pack->data, pack->data_len);
4466 if(req_pin_verify(pin, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4467 {
4468 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4469 err = MBTK_INFO_ERR_CME + cme_err;
4470 } else {
4471 err = MBTK_INFO_ERR_UNKNOWN;
4472 }
4473 LOG("Set PIN fail.");
4474 }
4475 else
4476 {
4477 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_PIN_RSP, NULL, 0);
4478 }
4479 }
4480 break;
4481 }
4482 case MBTK_INFO_ID_SIM_PUK_REQ: // <string> PUK
4483 {
4484 if(pack->data_len == 0 || pack->data == NULL)
4485 {
4486 err = MBTK_INFO_ERR_UNSUPPORTED;
4487 LOG("Unsupport.");
4488 }
4489 else // change PIN
4490 {
4491 mbtk_unlock_pin_info *pin_info = NULL;
4492 pin_info = (mbtk_unlock_pin_info *)pack->data;
4493 if(req_puk_unlock_pin(pin_info, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4494 {
4495 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4496 err = MBTK_INFO_ERR_CME + cme_err;
4497 } else {
4498 err = MBTK_INFO_ERR_UNKNOWN;
4499 }
4500 LOG("Get IMSI fail.");
4501 }
4502 else
4503 {
4504 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_CHANGE_PIN_RSP, NULL, 0);
4505 }
4506 }
4507 break;
4508 }
4509 case MBTK_INFO_ID_SIM_CHANGE_PIN_REQ: // <string> <string> old_PIN new_PIN
4510 {
4511 if(pack->data_len == 0 || pack->data == NULL)
4512 {
4513 err = MBTK_INFO_ERR_UNSUPPORTED;
4514 LOG("Unsupport.");
4515 }
4516 else // change PIN
4517 {
4518 mbtk_change_pin_info *pin_info = NULL;
4519 pin_info = (mbtk_change_pin_info *)pack->data;
4520 if(req_pin_change(pin_info, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4521 {
4522 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4523 err = MBTK_INFO_ERR_CME + cme_err;
4524 } else {
4525 err = MBTK_INFO_ERR_UNKNOWN;
4526 }
4527 LOG("Get IMSI fail.");
4528 }
4529 else
4530 {
4531 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_CHANGE_PIN_RSP, NULL, 0);
4532 }
4533 }
4534 break;
4535 }
4536 case MBTK_INFO_ID_SIM_IMSI_REQ: // <string> IMSI
4537 {
4538 if(pack->data_len == 0 || pack->data == NULL) // Get IMSI
4539 {
4540 char imsi[20] = {0};
4541 if(req_imsi_get(imsi, &cme_err) || strlen(imsi) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4542 {
4543 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4544 err = MBTK_INFO_ERR_CME + cme_err;
4545 } else {
4546 err = MBTK_INFO_ERR_UNKNOWN;
4547 }
4548 LOG("Get IMSI fail.");
4549 }
4550 else
4551 {
4552 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_IMSI_RSP, imsi, strlen(imsi));
4553 }
4554 }
4555 else // Set IMSI(Unsupport).
4556 {
4557 err = MBTK_INFO_ERR_UNSUPPORTED;
4558 LOG("Unsupport set IMSI.");
4559 }
4560 break;
4561 }
4562 case MBTK_INFO_ID_SIM_ICCID_REQ: // <string> ICCID
4563 {
4564 if(pack->data_len == 0 || pack->data == NULL) // Get ICCID
4565 {
4566 //log_hex("pack", pack, sizeof(mbtk_info_pack_t));
4567 //sleep(1);
4568 char iccid[50] = {0};
4569 if(req_iccid_get(iccid, &cme_err) || strlen(iccid) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4570 {
4571 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4572 err = MBTK_INFO_ERR_CME + cme_err;
4573 } else {
4574 err = MBTK_INFO_ERR_UNKNOWN;
4575 }
4576 LOG("Get ICCID fail.");
4577 }
4578 else
4579 {
4580 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_ICCID_RSP, iccid, strlen(iccid));
4581 }
4582 }
4583 else // Set ICCID(Unsupport).
4584 {
4585 err = MBTK_INFO_ERR_UNSUPPORTED;
4586 LOG("Unsupport set ICCID.");
4587 }
4588 break;
4589 }
4590 case MBTK_INFO_ID_SIM_PN_REQ: // <string> Phone Number
4591 {
4592 if(pack->data_len == 0 || pack->data == NULL) // Get Phone Number
4593 {
4594 //log_hex("pack", pack, sizeof(mbtk_info_pack_t));
4595 //sleep(1);
4596 char phone_number[50] = {0};
4597 if(req_phone_number_get(phone_number, &cme_err) || strlen(phone_number) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4598 {
4599 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4600 err = MBTK_INFO_ERR_CME + cme_err;
4601 } else {
4602 err = MBTK_INFO_ERR_UNKNOWN;
4603 }
4604 LOG("Get Phone Number fail.");
4605 }
4606 else
4607 {
4608 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_PN_RSP, phone_number, strlen(phone_number));
4609 }
4610 }
4611 else // Set Phone Number(Unsupport).
4612 {
4613 err = MBTK_INFO_ERR_UNSUPPORTED;
4614 LOG("Unsupport set Phone Number.");
4615 }
4616 break;
4617 }
4618 case MBTK_INFO_ID_NET_SEL_MODE_REQ: // <mbtk_net_info_t> Operator
4619 {
4620 if(pack->data_len == 0 || pack->data == NULL) // Get
4621 {
4622 mbtk_net_info_t info;
4623 memset(&info, 0, sizeof(mbtk_net_info_t));
4624 if(req_net_sel_mode_get(&info, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4625 {
4626 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4627 err = MBTK_INFO_ERR_CME + cme_err;
4628 } else {
4629 err = MBTK_INFO_ERR_UNKNOWN;
4630 }
4631 LOG("Get Net select mode fail.");
4632 }
4633 else
4634 {
4635 LOG("NET : %d, %d, %d, %d", info.net_sel_mode, info.net_type, info.net_state, info.plmn);
4636 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_SEL_MODE_RSP, &info, sizeof(mbtk_net_info_t));
4637 }
4638 }
4639 else // Set
4640 {
4641 //LOG("1 pack-%p,data-%p,data_len-%d", pack, pack->data, pack->data_len);
4642 //log_hex("pack", pack, sizeof(mbtk_info_pack_t));
4643 //log_hex("data", pack->data, pack->data_len);
4644
4645 mbtk_net_info_t* info = (mbtk_net_info_t*)pack->data;//(mbtk_net_info_t*)mbtk_info_pack_data_get(pack, NULL);
4646 if(info == NULL) {
4647 err = MBTK_INFO_ERR_FORMAT;
4648 LOG("Get Net select mode fail.");
4649 } else {
4650 LOG("NET : %d, %d, %d", info->net_sel_mode, info->net_type, info->plmn);
4651 if(req_net_sel_mode_set(info, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON) {
4652 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4653 err = MBTK_INFO_ERR_CME + cme_err;
4654 } else {
4655 err = MBTK_INFO_ERR_UNKNOWN;
4656 }
4657 LOG("Get Net select mode fail.");
4658 } else {
4659 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_SEL_MODE_RSP, NULL, 0);
4660 }
4661 }
4662 }
4663 break;
4664 }
4665 case MBTK_INFO_ID_NET_AVAILABLE_REQ:// sel_mode(uint8)type(uint8)plmn(uint32)...sel_mode(uint8)type(uint8)plmn(uint32)
4666 {
4667 if(pack->data_len == 0 || pack->data == NULL) // Get Available Net.
4668 {
4669 int buffer_size;
4670 uint8 buffer[SOCK_MSG_LEN_MAX];
4671 memset(buffer, 0, SOCK_MSG_LEN_MAX);
4672 if((buffer_size = req_available_net_get(buffer, &cme_err)) <= 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4673 {
4674 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4675 err = MBTK_INFO_ERR_CME + cme_err;
4676 } else {
4677 err = MBTK_INFO_ERR_UNKNOWN;
4678 }
4679 LOG("Get Available Net fail.");
4680 }
4681 else
4682 {
4683 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_AVAILABLE_RSP, buffer, buffer_size);
4684 }
4685 }
4686 else // Set Available Net(Unsupport).
4687 {
4688 err = MBTK_INFO_ERR_UNSUPPORTED;
4689 LOG("Unsupport set available net.");
4690 }
4691 break;
4692 }
4693 case MBTK_INFO_ID_NET_BAND_REQ:
4694 {
4695 if(pack->data_len == 0 || pack->data == NULL)
4696 {
4697 err = MBTK_INFO_ERR_REQ_PARAMETER;
4698 LOG("No data found.");
4699 }
4700 else // Get support/current bands.
4701 {
4702 if(pack->data_len == sizeof(uint8)) {
4703 if(*(pack->data)) { // Get current bands.
4704 mbtk_band_info_t band;
b.liu288093c2024-05-09 17:02:57 +08004705 memset(&band, 0x0, sizeof(mbtk_band_info_t));
liubin281ac462023-07-19 14:22:54 +08004706 if(req_band_get(&band, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4707 {
4708 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4709 err = MBTK_INFO_ERR_CME + cme_err;
4710 } else {
4711 err = MBTK_INFO_ERR_UNKNOWN;
4712 }
4713 LOG("Get net band fail.");
4714 }
4715 else
4716 {
4717 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_BAND_RSP, &band, sizeof(mbtk_band_info_t));
4718 }
4719 } else { // Get support bands.
4720 band_support_get();
4721 if(band_support.net_pref != 0)
4722 {
4723 err = MBTK_INFO_ERR_UNKNOWN;
4724 LOG("Get support bands fail.");
4725 }
4726 else
4727 {
4728 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_BAND_RSP, &band_support, sizeof(mbtk_band_info_t));
4729 }
4730 }
4731 } else { // Set current bands.
4732 mbtk_band_info_t* band = (mbtk_band_info_t*)pack->data;
4733 if(pack->data_len != sizeof(mbtk_band_info_t))
4734 {
4735 err = MBTK_INFO_ERR_REQ_PARAMETER;
4736 LOG("Set net band error.");
4737 break;
4738 }
4739
4740 if(req_band_set(band, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4741 {
4742 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4743 err = MBTK_INFO_ERR_CME + cme_err;
4744 } else {
4745 err = MBTK_INFO_ERR_UNKNOWN;
4746 }
4747 LOG("Set net band fail.");
4748 }
4749 else
4750 {
4751 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_BAND_RSP, NULL, 0);
4752 }
4753 }
4754 }
4755 break;
4756 }
4757 case MBTK_INFO_ID_NET_CELL_REQ: // mbtk_cell_info_t[]
4758 {
4759 if(pack->data_len == 0 || pack->data == NULL) // Get net cell.
4760 {
4761 if(req_cell_info_get(&cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4762 {
4763 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4764 err = MBTK_INFO_ERR_CME + cme_err;
4765 } else {
4766 err = MBTK_INFO_ERR_UNKNOWN;
4767 }
4768 LOG("Get net cell fail.");
4769 }
4770 else
4771 {
4772 LOG("req_cell_info_get() success,cell number: %d", cell_info.cell_num);
4773 //sleep(1);
4774 // mbtK_cell_pack_info_t
4775 if(cell_info.cell_num > 0 && cell_info.cell_num <= CELL_NUM_MAX && cell_info.type >= 0 && cell_info.type <= 2) {
4776 uint8 *data = (uint8*)malloc(sizeof(uint8) + sizeof(mbtk_cell_info_t) * cell_info.cell_num);
4777 if(data == NULL){
4778 err = MBTK_INFO_ERR_MEMORY;
4779 LOG("Get net cell fail.");
4780 } else {
4781 *data = cell_info.type; // Set network type.
4782 // Copy cell info item.
4783 #if 0
4784 int i = 0;
4785 while(i < cell_info.cell_num) {
4786 memcpy(data + sizeof(uint8) + sizeof(mbtk_cell_info_t) * i,
4787 &(cell_info.cell[i]),
4788 sizeof(mbtk_cell_info_t));
4789 i++;
4790 }
4791 #else
4792 memcpy(data + sizeof(uint8),
4793 &(cell_info.cell),
4794 sizeof(mbtk_cell_info_t) * cell_info.cell_num);
4795 #endif
4796 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_CELL_RSP, data, sizeof(uint8) + sizeof(mbtk_cell_info_t) * cell_info.cell_num);
4797 free(data);
4798 }
4799 } else {
4800 err = MBTK_INFO_ERR_UNKNOWN;
4801 LOG("Get net cell fail.");
4802 }
4803 }
4804 }
4805 else // Lock cell
4806 {
4807 char *mem = (char*)(pack->data);
4808 int len = pack->data_len;
4809 char reg[100] = {0};
4810 printf("mem:%s, len:%d", pack->data, pack->data_len);
4811
4812 if(req_cell_info_set(mem, reg, len, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4813 {
4814 // printf("cpms_set fail\n");
4815 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4816 err = MBTK_INFO_ERR_CME + cme_err;
4817 } else {
4818 err = MBTK_INFO_ERR_UNKNOWN;
4819 }
4820 // LOG("Set req_cell_info_set fail.");
4821 }
4822 else
4823 {
4824
4825 printf("req_cell_info_set success, reg:%s\n", reg);
4826 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_CELL_RSP, reg, strlen(reg));
4827
4828 // Restart is required to take effect.
4829 LOG("Will reboot system...");
4830 }
4831 }
4832 break;
4833 }
4834 case MBTK_INFO_ID_NET_RADIO_REQ: // <uint8> 0:OFF 1:ON
4835 {
4836 if(pack->data_len == 0 || pack->data == NULL) // Get
4837 {
4838 uint8 radio_on = (uint8)isRadioOn();
4839 if(radio_on < 0)
4840 {
4841 err = MBTK_INFO_ERR_UNKNOWN;
4842 LOG("Get radio state fail.");
4843 }
4844 else
4845 {
4846 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_RADIO_RSP, &radio_on, sizeof(uint8));
4847 }
4848 }
4849 else // Set
4850 {
4851 uint8 radio_on = *(pack->data);
4852 if(radio_on != 0 && radio_on != 1)
4853 {
4854 err = MBTK_INFO_ERR_REQ_PARAMETER;
4855 LOG("Set radio state fail.");
4856 }
4857 else
4858 {
4859 setRadioPower(radio_on);
4860 if((radio_on && net_info.radio_state == MBTK_RADIO_STATE_ON)
4861 || (!radio_on && net_info.radio_state == MBTK_RADIO_STATE_OFF)) {
4862 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_RADIO_RSP, NULL, 0);
4863 } else {
4864 err = MBTK_INFO_ERR_UNKNOWN;
4865 LOG("Set radio state fail.");
4866 }
4867 }
4868 }
4869 break;
4870 }
4871 case MBTK_INFO_ID_NET_SIGNAL_REQ: // mbtk_signal_info_t
4872 {
4873 if(pack->data_len == 0 || pack->data == NULL) // Get net signal.
4874 {
4875 mbtk_signal_info_t signal;
4876 memset(&signal, 0, sizeof(mbtk_signal_info_t));
4877 if(req_net_signal_get(&signal, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4878 {
4879 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4880 err = MBTK_INFO_ERR_CME + cme_err;
4881 } else {
4882 err = MBTK_INFO_ERR_UNKNOWN;
4883 }
4884 LOG("Get net signal fail.");
4885 }
4886 else
4887 {
4888 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_SIGNAL_RSP, &signal, sizeof(mbtk_signal_info_t));
4889 }
4890 }
4891 else // Set
4892 {
4893 err = MBTK_INFO_ERR_UNSUPPORTED;
4894 LOG("Set net signal fail.");
4895 }
4896 break;
4897 }
4898 case MBTK_INFO_ID_NET_REG_REQ: // mbtk_net_reg_info_t
4899 {
4900 if(pack->data_len == 0 || pack->data == NULL) // Get net reg.
4901 {
4902 mbtk_net_reg_info_t reg;
4903 memset(&reg, 0, sizeof(mbtk_net_reg_info_t));
4904 if(req_net_reg_get(&reg, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4905 {
4906 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4907 err = MBTK_INFO_ERR_CME + cme_err;
4908 } else {
4909 err = MBTK_INFO_ERR_UNKNOWN;
4910 }
4911 LOG("Get net reg fail.");
4912 }
4913 else
4914 {
4915 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_REG_RSP, &reg, sizeof(mbtk_net_reg_info_t));
4916 }
4917 }
4918 else // Set
4919 {
4920 err = MBTK_INFO_ERR_UNSUPPORTED;
4921 LOG("Set net reg fail.");
4922 }
4923 break;
4924 }
4925 case MBTK_INFO_ID_NET_APN_REQ: // mbtk_apn_info_t
4926 {
4927 if(pack->data_len == 0 || pack->data == NULL) // Get APN
4928 {
4929 uint8 buff[SOCK_MSG_LEN_MAX];
4930 memset(buff, 0, SOCK_MSG_LEN_MAX);
4931 int data_len = 0;
4932 if(req_apn_get(buff, &data_len, &cme_err) || data_len <= 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4933 {
4934 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4935 err = MBTK_INFO_ERR_CME + cme_err;
4936 } else {
4937 err = MBTK_INFO_ERR_UNKNOWN;
4938 }
4939 LOG("Get APN fail.");
4940 }
4941 else
4942 {
4943 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_APN_RSP, buff, data_len);
4944 }
4945 }
4946 else // Set
4947 {
4948 // <cid[1]><ip_type[1]><apn_len[2]><apn><user_len[2]><user><pass_len[2]><pass><auth_len[2]><auth>
4949 const uint8* ptr = pack->data;
4950 mbtk_apn_info_t apn;
4951 int len;
4952 memset(&apn, 0, sizeof(mbtk_apn_info_t));
4953 // cid
4954 apn.cid = *ptr++;
4955
4956 // ip_type
4957 apn.ip_type = (mbtk_ip_type_enum)(*ptr++);
4958
4959 // apn
4960 len = byte_2_uint16(ptr, false);
4961 ptr += sizeof(uint16);
4962 if(len > 0) {
4963 memcpy(apn.apn, ptr, len);
4964 ptr += len;
4965 }
4966
4967 // user
4968 len = byte_2_uint16(ptr, false);
4969 ptr += sizeof(uint16);
4970 if(len > 0) {
4971 memcpy(apn.user, ptr, len);
4972 ptr += len;
4973 }
4974
4975 // pass
4976 len = byte_2_uint16(ptr, false);
4977 ptr += sizeof(uint16);
4978 if(len > 0) {
4979 memcpy(apn.pass, ptr, len);
4980 ptr += len;
4981 }
4982
4983 // auth
4984 len = byte_2_uint16(ptr, false);
4985 ptr += sizeof(uint16);
4986 if(len > 0) {
4987 memcpy(apn.auth, ptr, len);
4988 ptr += len;
4989 }
4990
4991 LOGD("APN : %d, %d, %s, %s, %s, %s", apn.cid, apn.ip_type, str_empty(apn.apn) ? "NULL" : apn.apn,
4992 str_empty(apn.user) ? "NULL" : apn.user, str_empty(apn.pass) ? "NULL" : apn.pass, str_empty(apn.auth) ? "NULL" : apn.auth);
4993 if(req_apn_set(&apn, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4994 {
4995 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4996 err = MBTK_INFO_ERR_CME + cme_err;
4997 } else {
4998 err = MBTK_INFO_ERR_UNKNOWN;
4999 }
5000 LOG("Set APN fail.");
5001 }
5002 else
5003 {
5004 // Save apn.
wangyouqianged88c722023-11-22 16:33:43 +08005005#ifdef MBTK_AF_SUPPORT
5006 if(apn.cid == 1)
5007 {
5008 default_iptype = apn.ip_type;
5009 }
5010#endif
liubin281ac462023-07-19 14:22:54 +08005011 apn_prop_set(&apn);
5012
5013 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_APN_RSP, NULL, 0);
5014 }
5015 }
5016 break;
5017 }
wangyouqiang80487e42024-05-24 15:06:20 +08005018 case MBTK_INFO_ID_NET_QSER_APN_REQ:
5019 {
5020 if(pack->data_len == 0 || pack->data == NULL)
5021 {
5022 uint8 buff[SOCK_MSG_LEN_MAX];
5023 memset(buff, 0, SOCK_MSG_LEN_MAX);
5024 int data_len = 0;
5025 if(mbtk_qser_req_apn_get(buff, &data_len, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5026 {
5027 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5028 err = MBTK_INFO_ERR_CME + cme_err;
5029 } else {
5030 err = MBTK_INFO_ERR_UNKNOWN;
5031 }
5032 LOGE("Get APN fail.");
5033 }
5034 else
5035 {
5036 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_APN_RSP, buff, data_len);
5037 }
5038 }
5039 else
5040 {
5041 const uint8* ptr = pack->data;
5042 mbtk_apn_info_t apn;
5043 mbtk_apn_req_type_enum req_type = MBTK_APN_REQ_TYPE_SET;
5044 uint8 apn_type[MBTK_QSER_APN_NAME_SIZE] = {0};
5045 int ret = mbtk_strdata_to_apn(pack->data, &apn, apn_type, &req_type);
5046 if(ret < 0)
5047 {
5048 LOGE("mbtk_strdata_to_apn fail. ret = [%d]", ret);
5049 err = MBTK_INFO_ERR_REQ_PARAMETER;
5050 }
5051 else
5052 {
5053 if(req_apn_set(&apn, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5054 {
5055 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5056 err = MBTK_INFO_ERR_CME + cme_err;
5057 } else {
5058 err = MBTK_INFO_ERR_UNKNOWN;
5059 }
5060 LOGE("Set APN fail.");
5061 }
5062 else
5063 {
5064 // Save apn.
5065 apn_prop_set(&apn);
5066 mbtk_qser_apn_save(apn, apn_type, true);
5067 if(req_type == MBTK_APN_REQ_TYPE_ADD)
5068 {
5069 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_QSER_APN_RSP, (void *)&apn.cid, 1);
5070 }
5071 else
5072 {
5073 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_QSER_APN_RSP, NULL, 0);
5074 }
5075 }
5076 }
5077 }
5078
5079 break;
5080 }
b.liu288093c2024-05-09 17:02:57 +08005081 case MBTK_INFO_ID_NET_APN_DEL_REQ:
liuyang0e49d9a2024-04-23 21:04:54 +08005082 {
b.liu288093c2024-05-09 17:02:57 +08005083 if(pack->data_len == 0 || pack->data == NULL)
liuyang0e49d9a2024-04-23 21:04:54 +08005084 {
5085 err = MBTK_INFO_ERR_UNSUPPORTED;
5086 LOG("Unsupported delete apn .");
5087 }
b.liu288093c2024-05-09 17:02:57 +08005088 else
liuyang0e49d9a2024-04-23 21:04:54 +08005089 {
liuyang1cefd852024-04-24 18:30:53 +08005090 int profile = pack->data[0];
wangyouqiang80487e42024-05-24 15:06:20 +08005091 if(cid_active[profile] == 1)
liuyang0e49d9a2024-04-23 21:04:54 +08005092 {
wangyouqiang80487e42024-05-24 15:06:20 +08005093 LOGD("cid pdp already open.");
5094 err = MBTK_INFO_ERR_CID_EXIST;
wangyouqiang13e98402024-05-24 16:07:43 +08005095 break;
wangyouqiang80487e42024-05-24 15:06:20 +08005096 }
5097 if(mbtk_qser_apn_del(profile) < 0)
5098 {
5099 LOGD("del error.");
5100 err = MBTK_INFO_ERR_REQ_PARAMETER;
liuyang0e49d9a2024-04-23 21:04:54 +08005101 }
5102 else
5103 {
wangyouqiang80487e42024-05-24 15:06:20 +08005104 if(req_apn_del(profile, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5105 {
5106 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5107 err = MBTK_INFO_ERR_CME + cme_err;
5108 } else {
5109 err = MBTK_INFO_ERR_UNKNOWN;
5110 }
5111 LOG("Delete apn fail.");
5112 }
5113 else
5114 {
5115 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_APN_DEL_RSP, NULL, 0);
5116 }
liuyang0e49d9a2024-04-23 21:04:54 +08005117 }
5118 }
5119 break;
5120 }
liubin281ac462023-07-19 14:22:54 +08005121 case MBTK_INFO_ID_NET_DATA_CALL_REQ:
5122 {
5123 if(pack->data_len == 0 || pack->data == NULL)
5124 {
5125 err = MBTK_INFO_ERR_UNSUPPORTED;
5126 }
5127 else
5128 {
5129 /* <call_type[1]><cid[1]><auto_conn_interval[1]><boot_conn[1]><timeout[1]>
5130 call_type : mbtk_data_call_type_enum
5131 cid : 2 - 7
5132 timeout : second
5133 */
5134 mbtk_data_call_type_enum call_type = (mbtk_data_call_type_enum)pack->data[0];
5135 int cid = pack->data[1];
5136 int reconn = 0;
5137
wangyouqiang80487e42024-05-24 15:06:20 +08005138#if 0
liubin281ac462023-07-19 14:22:54 +08005139 if(cid < MBTK_APN_CID_MIN || cid > MBTK_APN_CID_MAX) {
5140 err = MBTK_INFO_ERR_CID;
5141 break;
5142 }
wangyouqiang80487e42024-05-24 15:06:20 +08005143#endif
5144 if(mbtk_check_cid(cid) < 0)
5145 {
5146 err = MBTK_INFO_ERR_CID;
5147 break;
5148 }
liubin281ac462023-07-19 14:22:54 +08005149
5150 LOG("cid_active[%d] = %d", cid, cid_active[cid]);
5151 memset(&cgact_wait, 0, sizeof(info_cgact_wait_t));
5152 switch(call_type) {
5153 case MBTK_DATA_CALL_START: {
5154 //mbtk wyq for data_call_ex add start
5155 int auto_conn_interval = pack->data[2];
5156 int boot_conn = pack->data[3];
5157 int timeout = pack->data[4];
5158 data_call_bootconn_save(cid, boot_conn);
b.liufe320632024-01-17 20:38:08 +08005159
wangyouqiang13e98402024-05-24 16:07:43 +08005160 mbtk_signal_info_t signal;
5161 memset(&signal, 0xFF, sizeof(mbtk_signal_info_t));
5162 req_net_signal_get(&signal, NULL);
liubin281ac462023-07-19 14:22:54 +08005163 if(net_info.net_type != MBTK_RADIO_TECH_E_UTRAN)
5164 {
5165 err = MBTK_INFO_ERR_NET_NO_INIT;
5166 break;
5167 }
b.liufe320632024-01-17 20:38:08 +08005168
liubin281ac462023-07-19 14:22:54 +08005169 if(cid_active[cid] == 1)
5170 {
5171 err = MBTK_INFO_ERR_CID_EXIST;
5172 break;
5173 }
b.liufe320632024-01-17 20:38:08 +08005174
wangyouqiang80487e42024-05-24 15:06:20 +08005175 if(mbtk_check_default_pdp_state(cid))
5176 {
5177 err = MBTK_INFO_ERR_UNSUPPORTED;
5178 break;
5179 }
5180
liubin281ac462023-07-19 14:22:54 +08005181 data_call_reconn:
5182 //mbtk wyq for data_call_ex add end
5183 cgact_wait.waitting = true;
5184 cgact_wait.cid = cid;
5185 cgact_wait.act = true;
5186 if(req_data_call_start(cid, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5187 {
5188 //mbtk wyq for data_call_ex add start
5189 if(reconn < 5 && auto_conn_interval > 0)
5190 {
5191 sleep(auto_conn_interval);
5192 reconn++;
5193 cme_err = MBTK_INFO_ERR_CME_NON;
5194 LOG("data_call restart call.");
5195 goto data_call_reconn;
5196 }
5197 //mbtk wyq for data_call_ex add end
5198 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5199 err = MBTK_INFO_ERR_CME + cme_err;
5200 } else {
5201 err = MBTK_INFO_ERR_UNKNOWN;
5202 }
5203 LOG("%d active fail.", cid);
5204 }
5205 else
5206 {
5207 // Wait for "CONNECT" or "+CGEV:"
5208 if(wait_cgact_complete(timeout)) { // Timeout
5209 err = MBTK_INFO_ERR_TIMEOUT;
5210 break;
5211 }
5212
5213 // Get IP information.
5214 mbtk_ipv4_info_t ipv4;
5215 mbtk_ipv6_info_t ipv6;
5216 memset(&ipv4, 0, sizeof(mbtk_ipv4_info_t));
5217 memset(&ipv6, 0, sizeof(mbtk_ipv6_info_t));
5218 if(req_data_call_state_get(cid, &ipv4, &ipv6, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5219 {
5220 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5221 err = MBTK_INFO_ERR_CME + cme_err;
5222 } else {
5223 err = MBTK_INFO_ERR_UNKNOWN;
5224 }
5225 LOG("Get %d state fail.", cid);
5226 }
5227 else
5228 {
5229 // Config IPv4 address.
wangyouqianged88c722023-11-22 16:33:43 +08005230#ifdef MBTK_AF_SUPPORT
5231 if(cid == 1)
5232 {
5233 //uint8 pdp_data = cid;
5234 //pack_rsp_send(cli_info->fd , MBTK_INFO_ID_IND_PDP_STATE_CHANGE, &pdp_data, sizeof(uint8));
5235 ipv4.valid = false;
5236 ipv6.valid = false;
5237 if(default_iptype == MBTK_IP_TYPE_IP)
5238 {
5239 ipv4.valid = true;
5240 }
5241 else if(default_iptype == MBTK_IP_TYPE_IPV6)
5242 {
5243 ipv6.valid = true;
5244 }
5245 else
5246 {
5247 ipv4.valid = true;
5248 ipv6.valid = true;
5249 }
5250 }
5251#endif
liubin281ac462023-07-19 14:22:54 +08005252#if 1
5253 if(ipv4.valid) {
5254 char dev[20] = {0};
5255 sprintf(dev, "ccinet%d", cid - 1);
5256
5257 char ip[20] = {0};
5258 char gateway[20] = {0};
5259 char *gateway_ptr = NULL;
5260 char netmask[20] = {0};
5261 char *netmask_ptr = NULL;
5262 if(inet_ntop(AF_INET, &(ipv4.IPAddr), ip, 20) == NULL) {
5263 err = MBTK_INFO_ERR_UNKNOWN;
5264 LOGE("inet_ntop ipv4 ip fail.");
5265 log_hex("IPv4", &(ipv4.IPAddr), 4);
5266 break;
5267 }
5268
5269 if(ipv4.GateWay) {
5270 if(inet_ntop(AF_INET, &(ipv4.GateWay), gateway, 20) == NULL) {
5271 err = MBTK_INFO_ERR_UNKNOWN;
5272 LOGE("inet_ntop ipv4 gateway fail.");
5273 log_hex("IPv4", &(ipv4.IPAddr), 4);
5274 break;
5275 } else {
5276 gateway_ptr = gateway;
5277 }
5278 }
5279
5280 if(ipv4.NetMask) {
5281 if(inet_ntop(AF_INET, &(ipv4.NetMask), netmask, 20) == NULL) {
5282 err = MBTK_INFO_ERR_UNKNOWN;
5283 LOGE("inet_ntop ipv4 netmask fail.");
5284 log_hex("IPv4", &(ipv4.IPAddr), 4);
5285 break;
5286 } else {
5287 netmask_ptr = netmask;
5288 }
5289 }
5290
5291 if(netmask_ptr == NULL) {
5292 netmask_ptr = netmask;
5293 memcpy(netmask_ptr, "255.255.255.0", strlen("255.255.255.0"));
5294 }
5295
5296 if(mbtk_ifc_configure2(dev, ip, 0, gateway_ptr, netmask_ptr)) {
5297 LOGD("Config %s IPv4 %s fail.", dev, ip);
5298 } else {
5299 LOGD("Config %s IPv4 %s success.", dev, ip);
5300 }
5301
5302 }
5303#endif
5304 // Config IPv6 address.
5305 if(ipv6.valid) {
5306 char ip[50] = {0};
5307 char dev[20] = {0};
5308 sprintf(dev, "ccinet%d", cid - 1);
5309
5310 if(inet_ntop(AF_INET6, &(ipv6.IPV6Addr), ip, 50) == NULL) {
5311 err = MBTK_INFO_ERR_UNKNOWN;
5312 LOGE("inet_ntop ipv6 ip fail.");
5313 log_hex("IPv6", &(ipv6.IPV6Addr), 16);
5314 break;
5315 }
5316
5317 if(mbtk_ipv6_config(dev, ip, 64)) {
5318 LOGD("Config %s IPv6 %s fail.", dev, ip);
5319 } else {
5320 LOGD("Config %s IPv6 %s success.", dev, ip);
5321 }
5322 }
5323
5324 cid_active[cid] = 1;
wangyouqiang80487e42024-05-24 15:06:20 +08005325 mbtk_set_default_pdp_state(true, cid);
5326 mbtk_qser_route_config(cid, &ipv4, &ipv6);
liubin281ac462023-07-19 14:22:54 +08005327 if(cli_info->fd != DATA_CALL_BOOTCONN_FD)
5328 {
5329 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_DATA_CALL_RSP, NULL, 0);
5330 }
5331 else
5332 {
5333 free(pack->data);
5334 free(cli_info);
5335 LOG("data_call bootconn success.");
5336 }
5337 }
5338 }
5339 break;
5340 }
5341 case MBTK_DATA_CALL_STOP: {
5342 //mbtk wyq for data_call_ex add start
5343 if(cid_active[cid] == 0)
5344 {
5345 err = MBTK_INFO_ERR_CID_NO_EXIST;
5346 break;
5347 }
5348
5349 int timeout = pack->data[2];
5350 //mbtk wyq for data_call_ex add end
b.liuf37bd332024-03-18 13:51:24 +08005351#if (defined(MBTK_AF_SUPPORT) || defined(MBTK_ALL_CID_SUPPORT))
wangyouqianged88c722023-11-22 16:33:43 +08005352 if(cid == 1)
5353 {
5354 char dev[20] = {0};
5355 uint8 pdp_data = cid + 100;
5356 pack_rsp_send(cli_info->fd , MBTK_INFO_ID_IND_PDP_STATE_CHANGE, &pdp_data, sizeof(uint8));
b.liufe320632024-01-17 20:38:08 +08005357
wangyouqianged88c722023-11-22 16:33:43 +08005358 sprintf(dev, "ccinet%d", cid - 1);
5359
5360 // Config network.
5361 if(mbtk_ifc_configure2(dev, NULL, 0, NULL, NULL)) {
5362 LOGD("Config %s IPv4 0 fail.", dev);
5363 } else {
5364 LOGD("Config %s IPv4 0 success.", dev);
5365 }
5366 cid_active[cid] = 0;
wangyouqiang80487e42024-05-24 15:06:20 +08005367 mbtk_set_default_pdp_state(false, cid);
wangyouqianged88c722023-11-22 16:33:43 +08005368 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_DATA_CALL_RSP, NULL, 0);
5369 break;
5370 }
5371#endif
liubin281ac462023-07-19 14:22:54 +08005372 cgact_wait.waitting = true;
5373 cgact_wait.cid = cid;
5374 cgact_wait.act = false;
5375 if(req_data_call_stop(cid, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5376 {
5377 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5378 err = MBTK_INFO_ERR_CME + cme_err;
5379 } else {
5380 err = MBTK_INFO_ERR_UNKNOWN;
5381 }
5382 LOG("%d deactive fail.", cid);
5383 }
5384 else
5385 {
5386 // Wait for "CONNECT" or "+CGEV:"
5387 if(wait_cgact_complete(timeout)) { // Timeout
5388 err = MBTK_INFO_ERR_TIMEOUT;
5389 break;
5390 }
5391 char dev[20] = {0};
5392 sprintf(dev, "ccinet%d", cid - 1);
5393
5394 // Config network.
5395 if(mbtk_ifc_configure2(dev, NULL, 0, NULL, NULL)) {
5396 LOGD("Config %s IPv4 0 fail.", dev);
5397 } else {
5398 LOGD("Config %s IPv4 0 success.", dev);
5399 }
5400
5401#if 0
5402 if(mbtk_ipv6_config(dev, NULL, 64)) {
5403 LOGD("Config %s IPv6 0 fail.", dev);
5404 } else {
5405 LOGD("Config %s IPv6 0 success.", dev);
5406 }
5407#endif
5408 cid_active[cid] = 0;
wangyouqiang80487e42024-05-24 15:06:20 +08005409 mbtk_set_default_pdp_state(false, cid);
liubin281ac462023-07-19 14:22:54 +08005410 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_DATA_CALL_RSP, NULL, 0);
5411 }
5412 break;
5413 }
5414 case MBTK_DATA_CALL_STATE: {
wangyouqianged88c722023-11-22 16:33:43 +08005415 if(cid_active[cid] == 0)
5416 {
5417 err = MBTK_INFO_ERR_CID_NO_EXIST;
5418 break;
5419 }
liubin281ac462023-07-19 14:22:54 +08005420 mbtk_ipv4_info_t ipv4;
5421 mbtk_ipv6_info_t ipv6;
5422 memset(&ipv4, 0, sizeof(mbtk_ipv4_info_t));
5423 memset(&ipv6, 0, sizeof(mbtk_ipv6_info_t));
5424 if(req_data_call_state_get(cid, &ipv4, &ipv6, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5425 {
5426 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5427 err = MBTK_INFO_ERR_CME + cme_err;
5428 } else {
5429 err = MBTK_INFO_ERR_UNKNOWN;
5430 }
5431 LOG("Get %d state fail.", cid);
5432 }
5433 else
5434 {
5435 uint8 buff[SOCK_MSG_LEN_MAX] = {0};
5436 int buff_len = 0;
wangyouqianged88c722023-11-22 16:33:43 +08005437#ifdef MBTK_AF_SUPPORT
5438 if(cid == 1)
5439 {
5440 ipv4.valid = false;
5441 ipv6.valid = false;
5442 if(default_iptype == MBTK_IP_TYPE_IP)
5443 {
5444 ipv4.valid = true;
5445 }
5446 else if(default_iptype == MBTK_IP_TYPE_IPV6)
5447 {
5448 ipv6.valid = true;
5449 }
5450 else
5451 {
5452 ipv4.valid = true;
5453 ipv6.valid = true;
5454 }
5455 }
5456#endif
liubin281ac462023-07-19 14:22:54 +08005457 if(ipv4.valid && ipv6.valid) {
5458 buff[0] = (uint8)2;
5459 buff_len++;
5460
5461 memcpy(buff + buff_len, &ipv4, sizeof(mbtk_ipv4_info_t));
5462 buff_len += sizeof(mbtk_ipv4_info_t);
5463 memcpy(buff + buff_len, &ipv6, sizeof(mbtk_ipv6_info_t));
5464 buff_len += sizeof(mbtk_ipv6_info_t);
5465 } else if(ipv4.valid) {
5466 buff[0] = (uint8)0;
5467 buff_len++;
5468
5469 memcpy(buff + buff_len, &ipv4, sizeof(mbtk_ipv4_info_t));
5470 buff_len += sizeof(mbtk_ipv4_info_t);
5471 } else if(ipv6.valid) {
5472 buff[0] = (uint8)1;
5473 buff_len++;
5474
5475 memcpy(buff + buff_len, &ipv6, sizeof(mbtk_ipv6_info_t));
5476 buff_len += sizeof(mbtk_ipv6_info_t);
5477 } else {
5478 LOGE("Get IPv4/IPv6 fail.");
5479 err = MBTK_INFO_ERR_UNKNOWN;
5480 break;
5481 }
5482 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_DATA_CALL_RSP, buff, buff_len);
5483 }
5484 break;
5485 }
5486 default: {
5487 err = MBTK_INFO_ERR_FORMAT;
5488 break;
5489 }
5490 }
5491 }
5492 break;
5493 }
r.xiao06db9a12024-04-14 18:51:15 -07005494 case MBTK_INFO_ID_NET_IMS_REQ:
5495 {
5496 if(pack->data_len == 0 || pack->data == NULL) //Get
5497 {
5498 int reg = -1;
5499 if(net_ims_get(&reg, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5500 {
5501 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5502 err = MBTK_INFO_ERR_CME + cme_err;
5503 } else {
5504 err = MBTK_INFO_ERR_UNKNOWN;
5505 }
5506 LOG("Get net ims fail.");
5507 }
5508 else
5509 {
5510 uint8 reg_type = (uint8)reg;
5511 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_IMS_RSP, &reg_type, sizeof(uint8));
5512 }
5513 }
5514 else
5515 {
5516 uint8 ims = *(pack->data);
5517
5518 if(net_ims_set(ims, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5519 {
5520 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5521 err = MBTK_INFO_ERR_CME + cme_err;
5522 } else {
5523 err = MBTK_INFO_ERR_UNKNOWN;
5524 }
5525 LOG("Set net ims fail.");
5526 }
5527 else
5528 {
5529 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_IMS_RSP, NULL, 0);
5530 }
5531 }
5532 break;
5533 }
r.xiaoec113d12024-01-12 02:13:28 -08005534 case MBTK_INFO_ID_WAKEUP_STA_REQ:
5535 {
5536 if(pack->data_len == 0 || pack->data == NULL)
5537 {
5538 err = MBTK_INFO_ERR_UNSUPPORTED;
5539 LOG("Get POWERIND state UNSUPPORTED.");
5540 }
5541 else // Set powerind state.
5542 {
5543 uint32 state = *(pack->data);
5544 if(req_powerind_set(state, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5545 {
5546 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5547 err = MBTK_INFO_ERR_CME + cme_err;
5548 } else {
5549 err = MBTK_INFO_ERR_UNKNOWN;
5550 }
5551 LOG("Set POWERIND state fail.");
5552 }
5553 else
5554 {
5555 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_WAKEUP_STA_RSP, NULL, 0);
5556 }
5557 }
5558 break;
5559 }
5560 case MBTK_INFO_ID_OOS_STA_REQ:
5561 {
5562 if(pack->data_len == 0 || pack->data == NULL)
5563 {
5564 mbtk_oos_info oos_t;
5565 if(req_oos_get(&oos_t, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5566 {
5567 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5568 err = MBTK_INFO_ERR_CME + cme_err;
5569 } else {
5570 err = MBTK_INFO_ERR_UNKNOWN;
5571 }
5572 LOG("Get SMS OOS fail.");
r.xiaoec113d12024-01-12 02:13:28 -08005573 }
5574 else
5575 {
r.xiaoec113d12024-01-12 02:13:28 -08005576 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_OOS_STA_RSP, &oos_t, sizeof(mbtk_oos_info));
5577 }
5578 }
b.liufe320632024-01-17 20:38:08 +08005579 else // Set OOS
r.xiaoec113d12024-01-12 02:13:28 -08005580 {
r.xiaocfd7c682024-01-22 03:59:46 -08005581 mbtk_oos_info *state = (mbtk_oos_info *)pack->data;
5582 if(pack->data_len != sizeof(mbtk_oos_info))
5583 {
5584 err = MBTK_INFO_ERR_REQ_PARAMETER;
5585 LOG("Set oos error.");
5586 break;
5587 }
5588
r.xiaoec113d12024-01-12 02:13:28 -08005589 if(req_oos_set(state, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5590 {
5591 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5592 err = MBTK_INFO_ERR_CME + cme_err;
5593 } else {
5594 err = MBTK_INFO_ERR_UNKNOWN;
5595 }
5596 LOG("Set OOS fail.");
5597 }
5598 else
5599 {
5600 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_OOS_STA_RSP, NULL, 0);
5601 }
5602 }
5603 break;
5604 }
wangyouqiang38e53362024-01-23 10:53:48 +08005605 case MBTK_INFO_ID_LED_REQ:
5606 {
5607 if(pack->data_len == 0 || pack->data == NULL)
5608 {
5609 err = MBTK_INFO_ERR_UNSUPPORTED;
5610 LOGE("led param is error.");
5611 }
5612 else
5613 {
5614 char type = pack->data[0];
5615 char status = pack->data[1];
5616 LOGE("[set_led] = [%d], [status_led] = [%d].", type, status);
r.xiaoec113d12024-01-12 02:13:28 -08005617
wangyouqiang38e53362024-01-23 10:53:48 +08005618 if(type == MBTK_LED_TYPE_NET)
5619 {
5620 if(status == MBTK_LED_STATUS_CLOSE)
5621 {
5622 mbtk_net_led_set(MBTK_NET_LED_CLOSE);
5623 }
5624 else
5625 {
5626 mbtk_net_led_set(MBTK_NET_LED_OPEN);
5627 }
5628 }
5629 else
5630 {
5631 if(status == MBTK_LED_STATUS_CLOSE)
5632 {
5633 status_led_set(MBTK_STATUS_LED_CLOSE);
5634 }
5635 else
5636 {
5637 status_led_set(MBTK_STATUS_LED_OPEN);
5638 }
5639 }
5640 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_LED_RSP, NULL, 0);
5641 }
5642 break;
5643 }
liubin281ac462023-07-19 14:22:54 +08005644 default:
5645 {
5646 err = MBTK_INFO_ERR_REQ_UNKNOWN;
5647 LOG("Unknown request : %s", id2str(pack->info_id));
5648 break;
5649 }
5650 }
5651
5652 return err;
5653 }
5654}
5655
5656// Process AT URC data
5657static int send_pack_to_queue(sock_client_info_t* cli_info, void* pack)
5658{
5659 if(info_queue.count >= PACK_PROCESS_QUEUE_MAX)
5660 {
5661 LOG("Packet process queue is full");
5662 return -1;
5663 }
5664
5665 info_queue_item_t *item = (info_queue_item_t*)malloc(sizeof(info_queue_item_t));
5666 if(!item)
5667 {
5668 LOG("malloc() fail[%d].", errno);
5669 return -1;
5670 }
5671 item->cli_info = cli_info;
5672 item->pack = pack;
5673 mbtk_queue_put(&info_queue, item);
5674
5675 // If thread is waitting,continue it.
5676 if(1/*!is_running*/)
5677 {
5678 pthread_mutex_lock(&info_mutex);
5679 pthread_cond_signal(&info_cond);
5680 pthread_mutex_unlock(&info_mutex);
5681 }
5682 else
5683 {
5684 LOG("Packet process thread is process...");
5685 }
5686
5687 return 0;
5688}
5689
5690static void radio_state_change(void *data, int data_len)
5691{
5692 uint8 *data_ptr = (uint8*)data;
5693 if(data_ptr[0]) {
5694 net_info.radio_state = MBTK_RADIO_STATE_ON;
5695 } else {
5696 net_info.radio_state = MBTK_RADIO_STATE_OFF;
5697 }
5698
5699 sock_client_info_t *cli = NULL;
5700 list_first(sock_client_list);
5701 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
5702 {
5703 if(cli->ind_num > 0) {
5704 int i;
5705 for(i = 0; i < IND_REGISTER_MAX; i++) {
5706 // Registe MBTK_INFO_ID_IND_RADIO_STATE_CHANGE
5707 if(cli->ind_register[i] == MBTK_INFO_ID_IND_RADIO_STATE_CHANGE) {
5708 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_RADIO_STATE_CHANGE, data, data_len);
5709 break;
5710 }
5711 }
5712 }
5713 }
5714}
5715
5716static void pdp_state_change(void *data, int data_len)
5717{
5718 sock_client_info_t *cli = NULL;
5719 list_first(sock_client_list);
5720 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
5721 {
5722 if(cli->ind_num > 0) {
5723 int i;
5724 for(i = 0; i < IND_REGISTER_MAX; i++) {
5725 if(cli->ind_register[i] == MBTK_INFO_ID_IND_PDP_STATE_CHANGE) {
5726 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_PDP_STATE_CHANGE, data, data_len);
5727 break;
5728 }
5729 }
5730 }
5731 }
5732}
5733
5734static void net_state_change(void *data, int data_len)
5735{
5736 sock_client_info_t *cli = NULL;
5737 list_first(sock_client_list);
5738 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
5739 {
5740 if(cli->ind_num > 0) {
5741 int i;
5742 for(i = 0; i < IND_REGISTER_MAX; i++) {
5743 // Registe MBTK_INFO_ID_IND_NET_STATE_CHANGE
5744 if(cli->ind_register[i] == MBTK_INFO_ID_IND_NET_STATE_CHANGE) {
5745 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_NET_STATE_CHANGE, data, data_len);
5746 break;
5747 }
5748 }
5749 }
5750 }
5751}
5752
5753static void call_state_change(void *data, int data_len)
5754{
5755 sock_client_info_t *cli = NULL;
5756 list_first(sock_client_list);
5757 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
5758 {
5759 if(cli->ind_num > 0) {
5760 int i;
5761 for(i = 0; i < IND_REGISTER_MAX; i++) {
5762 // Registed MBTK_INFO_ID_IND_RADIO_STATE_CHANGE
5763 if(cli->ind_register[i] == MBTK_INFO_ID_IND_CALL_STATE_CHANGE) {
5764 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_CALL_STATE_CHANGE, data, data_len);
5765 break;
5766 }
5767 }
5768 }
5769 }
5770}
5771
5772static void sim_state_change(void *data, int data_len)
5773{
5774 sock_client_info_t *cli = NULL;
5775 list_first(sock_client_list);
5776 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
5777 {
5778 if(cli->ind_num > 0) {
5779 int i;
5780 for(i = 0; i < IND_REGISTER_MAX; i++) {
5781 // Registed MBTK_INFO_ID_IND_RADIO_STATE_CHANGE
5782 if(cli->ind_register[i] == MBTK_INFO_ID_IND_SIM_STATE_CHANGE) {
5783 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_SIM_STATE_CHANGE, data, data_len);
5784 break;
5785 }
5786 }
5787 }
5788 }
5789}
5790
5791static void sms_state_change(void *data, int data_len)
5792{
5793 sock_client_info_t *cli = NULL;
5794 list_first(sock_client_list);
5795 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
5796 {
5797 if(cli->ind_num > 0) {
5798 int i;
5799 for(i = 0; i < IND_REGISTER_MAX; i++) {
5800 // Registed MBTK_INFO_ID_IND_SMS_STATE_CHANGE
5801 if(cli->ind_register[i] == MBTK_INFO_ID_IND_SMS_STATE_CHANGE) {
5802 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_SMS_STATE_CHANGE, data, data_len);
5803 break;
5804 }
5805 }
5806 }
5807 }
5808}
5809
r.xiaofca7c472024-04-24 01:00:23 -07005810static void signal_state_change(void *data, int data_len)
5811{
5812 sock_client_info_t *cli = NULL;
5813 list_first(sock_client_list);
5814 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
5815 {
5816 if(cli->ind_num > 0) {
5817 int i;
5818 for(i = 0; i < IND_REGISTER_MAX; i++) {
5819 // Registe MBTK_INFO_ID_IND_SIGNAL_STATE_CHANGE
5820 if(cli->ind_register[i] == MBTK_INFO_ID_IND_SIGNAL_STATE_CHANGE) {
5821 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_SIGNAL_STATE_CHANGE, data, data_len);
5822 break;
5823 }
5824 }
5825 }
5826 }
5827}
5828
5829
liubin281ac462023-07-19 14:22:54 +08005830int urc_msg_distribute(bool async_process, info_urc_msg_id_enum msg, void *data, int data_len)
5831{
5832#if 0
5833 if(urc_queue.count >= PACK_PROCESS_QUEUE_MAX)
5834 {
5835 LOG("Packet process queue is full");
5836 return -1;
5837 }
5838
5839 info_urc_msg_t *urc = (info_urc_msg_t*)malloc(sizeof(info_urc_msg_t));
5840 if(!urc)
5841 {
5842 LOG("malloc() fail[%d].", errno);
5843 return -1;
5844 }
5845 urc->msg = msg;
5846 urc->data = memdup(data, data_len);
5847 urc->data_len = data_len;
5848
5849 mbtk_queue_put(&urc_queue, urc);
5850
5851 // If thread is waitting,continue it.
5852 if(1/*!is_running*/)
5853 {
5854 pthread_mutex_lock(&urc_mutex);
5855 pthread_cond_signal(&urc_cond);
5856 pthread_mutex_unlock(&urc_mutex);
5857 }
5858 else
5859 {
5860 LOG("Packet process thread is process...");
5861 }
5862
5863 return 0;
5864#else
5865 if(async_process) {
5866 info_urc_msg_t *urc = (info_urc_msg_t*)malloc(sizeof(info_urc_msg_t));
5867 if(!urc)
5868 {
5869 LOG("malloc() fail[%d].", errno);
5870 return -1;
5871 }
5872 urc->msg = msg;
5873 if(data && data_len > 0) {
5874 urc->data = memdup(data, data_len);
5875 urc->data_len = data_len;
5876 } else {
5877 urc->data = NULL;
5878 urc->data_len = 0;
5879 }
5880 return send_pack_to_queue(NULL, urc);
5881 } else {
5882 switch(msg) {
5883 case INFO_URC_MSG_NET_CS_REG_STATE:
5884 {
5885 net_state_change(data, data_len);
5886 break;
5887 }
5888 case INFO_URC_MSG_CALL_STATE:
5889 {
5890 call_state_change(data, data_len);
5891 break;
5892 }
5893 case INFO_URC_MSG_SMS_STATE:
5894 {
5895 sms_state_change(data, data_len);
5896 break;
5897 }
5898 case INFO_URC_MSG_SIM_STATE:
5899 {
5900 sim_state_change(data, data_len);
5901 break;
5902 }
5903 case INFO_URC_MSG_PDP_STATE:
5904 {
5905 pdp_state_change(data, data_len);
5906 break;
5907 }
5908 default: {
5909 LOGE("Unknown msg : %d", msg);
5910 break;
5911 }
5912 }
5913
5914 return 0;
5915 }
5916#endif
5917}
5918
5919
5920static void ind_regisger(sock_client_info_t* cli_info, uint16 ind)
5921{
5922 uint32 i = 0;
5923 while(i < cli_info->ind_num)
5924 {
5925 if(cli_info->ind_register[i] == ind)
5926 break;
5927 i++;
5928 }
5929
5930 if(i == cli_info->ind_num) // No found IND
5931 {
5932 cli_info->ind_register[i] = ind;
5933 cli_info->ind_num++;
5934 LOG("Register IND : %s", id2str(ind));
5935 }
5936 else
5937 {
5938 LOG("IND had exist.");
5939 }
5940}
5941
5942static void pack_distribute(sock_client_info_t* cli_info, mbtk_info_pack_t* pack)
5943{
5944 // Register IND Message.
5945 if(mbtk_info_type_get(pack->info_id) == MBTK_INFO_TYPE_IND)
5946 {
5947 mbtk_info_err_enum err = MBTK_INFO_ERR_SUCCESS;
5948 if(cli_info->ind_num >= IND_REGISTER_MAX)
5949 {
5950 LOG("IND if full.");
5951 err = MBTK_INFO_ERR_IND_FULL;
5952 }
5953 else
5954 {
5955 ind_regisger(cli_info, pack->info_id);
5956 }
5957
5958 pack_error_send(cli_info->fd, pack->info_id, err);
5959
5960 mbtk_info_pack_free(&pack);
5961 }
5962 else // Request Information.
5963 {
5964 LOG("Start process REQ(%s), Length : %d", id2str(pack->info_id), pack->data_len);
5965 if(0 && pack->data_len > 0)
5966 {
5967 log_hex("DATA", pack->data, pack->data_len);
5968 }
5969
5970 // Send to REQ_process_thread process.
5971 send_pack_to_queue(cli_info, pack);
5972
5973 // For test.
5974 // pack_error_send(cli_info->fd, pack->info_id + 1, MBTK_INFO_ERR_SUCCESS);
5975 }
5976}
5977
5978static sock_client_info_t* cli_find(int fd)
5979{
5980 sock_client_info_t *result = NULL;
5981 list_first(sock_client_list);
5982 while ((result = (sock_client_info_t*) list_next(sock_client_list)))
5983 {
5984 if (result->fd == fd)
5985 return result;
5986 }
5987
5988 return NULL;
5989}
5990
5991//mbtk wyq for server_ready_status add start
5992void server_ready_set(void)
5993{
5994 server_ready_status = 1;
5995}
5996
5997char server_ready_get(void)
5998{
5999 return server_ready_status;
6000}
6001
6002static void server_state_send(void)
6003{
6004 sock_client_info_t *cli = NULL;
6005 list_first(sock_client_list);
6006 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
6007 {
6008 if(cli->ind_num > 0) {
6009 if(cli->ind_register[0] == MBTK_INFO_ID_IND_SERVER_STATE_CHANGE) {
6010 cli->ind_num = 0;
6011 cli->ind_register[0] = 0;
6012 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_SERVER_STATE_CHANGE, "1", 1);
6013 break;
6014 }
6015 }
6016 else
6017 {
6018 break;
6019 }
6020 }
6021 LOG("handshake message send ok.");
6022}
6023
6024//mbtk wyq for server_ready_status add end
6025
6026//mbtk wyq for data_call_ex add start
6027//Save the cid that "DATA_CALL" needs to be automatically connected after startup
6028void data_call_bootconn_save(int cid, int bootconn)
6029{
6030 if(cid_bootconn[cid] == bootconn + '0')
6031 {
6032 return;
6033 }
6034 cid_bootconn[cid] = bootconn + '0';
6035
6036 LOG("data_call_bootconn_set cid_bootconn = %s", cid_bootconn);
6037 property_set("persist.mbtk.datacall.bootconn", cid_bootconn);
6038}
6039
6040static void* data_call_bootconn_pthread(void *arg)
6041{
6042 UNUSED(arg);
6043 LOG("data_call_bootconn_pthread enter.");
6044 int i = 0;
6045 int send_sum = 0;
6046 int bootconn = 0;
b.liufe320632024-01-17 20:38:08 +08006047
liubin281ac462023-07-19 14:22:54 +08006048 while(1)
6049 {
6050 if(server_ready_get() && send_sum == 0)
6051 {
6052 server_state_send();
6053 send_sum = 1;
6054 }
b.liufe320632024-01-17 20:38:08 +08006055
liubin281ac462023-07-19 14:22:54 +08006056 if(net_info.sim_state == MBTK_SIM_READY && net_info.net_type == MBTK_RADIO_TECH_E_UTRAN && bootconn == 0)
6057 {
6058 //data_call_bootconn_exec();
6059 property_get("persist.mbtk.datacall.bootconn", cid_bootconn, "00000000");
6060 LOG("data_call_bootconn_exec cid_bootconn = %s", cid_bootconn);
b.liufe320632024-01-17 20:38:08 +08006061
liubin281ac462023-07-19 14:22:54 +08006062 for(i = MBTK_APN_CID_MIN; i < MBTK_APN_CID_MAX + 1; i++)
6063 {
6064 if(cid_bootconn[i] == '1')
6065 {
6066 sock_client_info_t *info = (sock_client_info_t*)malloc(sizeof(sock_client_info_t));
6067 if(info == NULL)
6068 {
6069 LOG("clinent_info malloc() fail.");
6070 continue;
6071 }
6072 memset(info, 0, sizeof(sock_client_info_t));
6073 info->fd = DATA_CALL_BOOTCONN_FD;
b.liufe320632024-01-17 20:38:08 +08006074
liubin281ac462023-07-19 14:22:54 +08006075 mbtk_info_pack_t* pack = mbtk_info_pack_creat(MBTK_INFO_ID_NET_DATA_CALL_REQ);
6076 if(pack == NULL)
6077 {
6078 free(info);
6079 LOG("Packet malloc() fail.");
6080 continue;
6081 }
6082
6083 // "info_err"
6084 //pack->info_err = byte_2_uint16(ptr, false)
6085
6086 // "data_len"
6087 pack->data_len = 5;
6088
6089 char *p = (char *)malloc(5);
6090 p[0] = MBTK_DATA_CALL_START;
6091 p[1] = i;
6092 p[2] = 0;
6093 p[3] = 1;
6094 p[4] = 10;
6095 pack->data = p;
6096 send_pack_to_queue(info, pack);
6097 }
6098 }
6099
6100 bootconn = 1;
6101 }
6102
6103 if(bootconn == 1 && send_sum == 1)
6104 {
6105 break;
6106 }
6107 else
6108 {
6109 sleep(1);
6110 }
6111 }
6112
6113 LOG("data_call_bootconn_pthread exit.");
6114 return NULL;
6115}
6116
6117//mbtk wyq for data_call_ex add end
6118
6119static void* info_main_pthread(void* arg)
6120{
6121 UNUSED(arg);
6122 epoll_fd = epoll_create(SOCK_CLIENT_MAX + 1);
6123 if(epoll_fd < 0)
6124 {
6125 LOG("epoll_create() fail[%d].", errno);
6126 return NULL;
6127 }
6128
6129 uint32 event = EPOLLIN | EPOLLET;
6130 struct epoll_event ev;
6131 ev.data.fd = sock_listen_fd;
6132 ev.events = event; //EPOLLIN | EPOLLERR | EPOLLET;
6133 epoll_ctl(epoll_fd,EPOLL_CTL_ADD,sock_listen_fd,&ev);
6134
6135 int nready = -1;
6136 struct epoll_event epoll_events[EPOLL_LISTEN_MAX];
6137 while(1)
6138 {
6139 nready = epoll_wait(epoll_fd, epoll_events, EPOLL_LISTEN_MAX, -1);
6140 if(nready > 0)
6141 {
6142 sock_client_info_t *cli_info = NULL;
6143 int i;
6144 for(i = 0; i < nready; i++)
6145 {
6146 LOG("fd[%d] event = %x",epoll_events[i].data.fd, epoll_events[i].events);
6147 if(epoll_events[i].events & EPOLLHUP) // Client Close.
6148 {
6149 if((cli_info = cli_find(epoll_events[i].data.fd)) != NULL)
6150 {
6151 cli_close(cli_info);
6152 }
6153 else
6154 {
6155 LOG("Unknown client[fd = %d].", epoll_events[i].data.fd);
6156 }
6157 }
6158 else if(epoll_events[i].events & EPOLLIN)
6159 {
6160 if(epoll_events[i].data.fd == sock_listen_fd) // New clients connected.
6161 {
6162 int client_fd = -1;
6163 while(1)
6164 {
6165 struct sockaddr_in cliaddr;
6166 socklen_t clilen = sizeof(cliaddr);
6167 client_fd = accept(epoll_events[i].data.fd, (struct sockaddr *) &cliaddr, &clilen);
6168 if(client_fd < 0)
6169 {
6170 if(errno == EAGAIN)
6171 {
6172 LOG("All client connect get.");
6173 }
6174 else
6175 {
6176 LOG("accept() error[%d].", errno);
6177 }
6178 break;
6179 }
6180 // Set O_NONBLOCK
6181 int flags = fcntl(client_fd, F_GETFL, 0);
6182 if (flags > 0)
6183 {
6184 flags |= O_NONBLOCK;
6185 if (fcntl(client_fd, F_SETFL, flags) < 0)
6186 {
6187 LOG("Set flags error:%d", errno);
6188 }
6189 }
6190
6191 memset(&ev,0,sizeof(struct epoll_event));
6192 ev.data.fd = client_fd;
6193 ev.events = event;//EPOLLIN | EPOLLERR | EPOLLET;
6194 epoll_ctl(epoll_fd, EPOLL_CTL_ADD, client_fd, &ev);
6195
6196 sock_client_info_t *info = (sock_client_info_t*)malloc(sizeof(sock_client_info_t));
6197 if(info)
6198 {
6199 memset(info, 0, sizeof(sock_client_info_t));
6200 info->fd = client_fd;
6201 if(server_ready_get() == 1)
6202 {
6203 info->ind_num = 0;
6204 pack_rsp_send(info->fd , MBTK_INFO_ID_IND_SERVER_STATE_CHANGE, "1", 1);
6205 LOG("server ready ok.");
6206 }
6207 else
6208 {
6209 info->ind_num = 1;
6210 info->ind_register[0] = MBTK_INFO_ID_IND_SERVER_STATE_CHANGE;
6211 LOG("server ready no.");
6212 }
6213 list_add(sock_client_list, info);
6214 LOG("Add New Client FD Into List.");
6215 }
6216 else
6217 {
6218 LOG("malloc() fail.");
6219 }
6220 }
6221 }
6222 else if((cli_info = cli_find(epoll_events[i].data.fd)) != NULL) // Client data arrive.
6223 {
6224 // Read and process every message.
6225 mbtk_info_err_enum err = MBTK_INFO_ERR_SUCCESS;
6226 mbtk_info_pack_t** pack = mbtk_info_pack_recv(cli_info->fd, true, &err);
6227
6228 // Parse packet error,send error response to client.
6229 if(pack == NULL)
6230 {
6231 if(err != MBTK_INFO_ERR_SUCCESS)
6232 {
6233 pack_error_send(cli_info->fd, MBTK_INFO_ID_REQ_UNKNOWN, err);
6234 }
6235 }
6236 else
6237 {
6238#if 0
6239 int i = 0;
6240 while(pack[i] != NULL)
6241 {
6242 pack_distribute(cli_info, pack[i]);
6243 // Not free,will free in pack_process() or packet process thread.
6244 //mbtk_info_pack_free(&(pack[i]));
6245 i++;
6246 }
6247 free(pack);
6248#else
6249 mbtk_info_pack_t** pack_ptr = pack;
6250 while(*pack_ptr)
6251 {
6252 pack_distribute(cli_info, *pack_ptr);
6253 // Not free,will free in pack_process() or packet process thread.
6254 //mbtk_info_pack_free(pack_ptr);
6255 pack_ptr++;
6256 }
6257
6258 free(pack);
6259#endif
6260 }
6261 }
6262 else
6263 {
6264 LOG("Unknown socket : %d", epoll_events[i].data.fd);
6265 }
6266 }
6267 else
6268 {
6269 LOG("Unknown event : %x", epoll_events[i].events);
6270 }
6271 }
6272 }
6273 else
6274 {
6275 LOG("epoll_wait() fail[%d].", errno);
6276 }
6277 }
6278
6279 return NULL;
6280}
6281
6282static void data_call_restart()
6283{
6284#if 0
6285 // Waitting for network ok.
6286 mbtk_net_info_t info;
6287 int cme_err;
6288 int i = 0;
6289 while(i < 15) { // 15s timeout
6290 cme_err = MBTK_INFO_ERR_CME_NON;
6291 memset(&info, 0, sizeof(mbtk_net_info_t));
6292 if(!req_net_sel_mode_get(&info, &cme_err) && cme_err == MBTK_INFO_ERR_CME_NON)
6293 {
6294 if(info.net_type >= 2) {
6295 break;
6296 }
6297 }
6298
6299 sleep(1);
6300 i++;
6301 }
6302#endif
6303 // +CGACT
6304 int cid;
6305 LOGD("Start active APN.");
6306 for(cid = MBTK_APN_CID_MIN; cid <= MBTK_APN_CID_MAX && cid_active[cid]; cid++) {
6307 LOG("Active cid : %d", cid);
6308 req_data_call_start(cid, NULL);
6309 }
6310}
6311
6312/*
6313void mbtk_radio_ready_cb()
6314{
6315 pthread_t radio_pid;
6316 pthread_attr_t thread_attr;
6317 pthread_attr_init(&thread_attr);
6318 if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
6319 {
6320 LOG("pthread_attr_setdetachstate() fail.");
6321 return;
6322 }
6323
6324 if(pthread_create(&radio_pid, &thread_attr, radio_ready_thread, NULL))
6325 {
6326 LOG("pthread_create() fail.");
6327 }
6328
6329 pthread_attr_destroy(&thread_attr);
6330}
6331*/
6332
6333static void net_ifc_state_change(bool act, int cid)
6334{
6335 if(cid < MBTK_APN_CID_MIN || cid > MBTK_APN_CID_MAX) { // No nothing for cid 1 and 8
6336 return;
6337 }
6338
wangyouqiang65884152023-10-25 19:54:15 +08006339 if(act)
6340 {
6341 cid_active[cid] = 1;
wangyouqiang80487e42024-05-24 15:06:20 +08006342 mbtk_set_default_pdp_state(true, cid);
wangyouqiang65884152023-10-25 19:54:15 +08006343 }
6344 else
6345 {
6346 cid_active[cid] = 0;
wangyouqiang80487e42024-05-24 15:06:20 +08006347 mbtk_set_default_pdp_state(false, cid);
wangyouqiang65884152023-10-25 19:54:15 +08006348 }
liubin281ac462023-07-19 14:22:54 +08006349 char dev[20] = {0};
6350 sprintf(dev, "ccinet%d", cid - 1);
6351 if(act) { // Config IP.
6352 // Get IP information.
6353 mbtk_ipv4_info_t ipv4;
6354 mbtk_ipv6_info_t ipv6;
6355 memset(&ipv4, 0, sizeof(mbtk_ipv4_info_t));
6356 memset(&ipv6, 0, sizeof(mbtk_ipv6_info_t));
6357 int cme_err = MBTK_INFO_ERR_CME_NON;
6358 if(!req_data_call_state_get(cid, &ipv4, &ipv6, &cme_err) && cme_err == MBTK_INFO_ERR_CME_NON)
6359 {
wangyouqianged88c722023-11-22 16:33:43 +08006360#ifdef MBTK_AF_SUPPORT
6361 if(cid == 1)
6362 {
6363 ipv4.valid = false;
6364 ipv6.valid = false;
6365 if(default_iptype == MBTK_IP_TYPE_IP)
6366 {
6367 ipv4.valid = true;
6368 }
6369 else if(default_iptype == MBTK_IP_TYPE_IPV6)
6370 {
6371 ipv6.valid = true;
6372 }
6373 else
6374 {
6375 ipv4.valid = true;
6376 ipv6.valid = true;
6377 }
6378 }
6379#endif
liubin281ac462023-07-19 14:22:54 +08006380 // Config IPv4 address.
6381 if(ipv4.valid) {
6382 char ip[20] = {0};
6383 if(inet_ntop(AF_INET, &(ipv4.IPAddr), ip, 20) == NULL) {
6384 LOGE("inet_ntop ipv4 ip fail.");
6385 return;
6386 }
6387
6388 if(mbtk_ifc_configure2(dev, ip, 0, NULL, "255.255.255.0")) {
6389 LOGD("Config %s IPv4 %s fail.", dev, ip);
6390 } else {
6391 LOGD("Config %s IPv4 %s success.", dev, ip);
6392 }
6393 }
6394
6395 // Config IPv6 address.
6396 if(ipv6.valid) {
6397 char ip[50] = {0};
6398
6399 if(inet_ntop(AF_INET6, &(ipv6.IPV6Addr), ip, 50) == NULL) {
6400 LOGE("inet_ntop ipv6 ip fail.");
6401 return;
6402 }
6403
6404 if(mbtk_ipv6_config(dev, ip, 64)) {
6405 LOGD("Config %s IPv6 %s fail.", dev, ip);
6406 } else {
6407 LOGD("Config %s IPv6 %s success.", dev, ip);
6408 }
6409 }
6410 }
6411 } else { // Del IP
6412 if(mbtk_ifc_configure2(dev, NULL, 0, NULL, NULL)) {
6413 LOGD("Config %s IPv4 0 fail.", dev);
6414 } else {
6415 LOGD("Config %s IPv4 0 success.", dev);
6416 }
6417 }
6418}
6419
6420static void urc_msg_process(info_urc_msg_t *msg)
6421{
6422 uint8 *data = NULL;
6423 if(msg->data) {
6424 data = (uint8*)msg->data;
6425 }
6426 switch(msg->msg) {
6427 case INFO_URC_MSG_RADIO_STATE:
6428 {
6429 radio_state_change(msg->data, msg->data_len);
6430 // Reconfig APN while radio on.
6431 if(data[0]) {
6432 apn_prop_get();
6433 }
6434 break;
6435 }
6436 case INFO_URC_MSG_CGEV:
6437 {
6438 bool act = data[0];
6439 int cid = data[1];
6440 if(cid > 0) {
6441 net_ifc_state_change(act, cid);
6442 }
6443 break;
6444 }
6445 case INFO_URC_MSG_NET_PS_REG_STATE:
6446 {
b.liufe320632024-01-17 20:38:08 +08006447 uint8 net_data[4];
liubin281ac462023-07-19 14:22:54 +08006448 net_data[0] = (uint8)MBTK_NET_PS_STATE;
b.liufe320632024-01-17 20:38:08 +08006449 net_data[1] = data[0]; // act
6450 net_data[3] = data[1]; // 0 - GSM/WCDMA; 1 - LTE
liubin281ac462023-07-19 14:22:54 +08006451 mbtk_net_reg_state_enum state = (mbtk_net_reg_state_enum)data[0];
6452 if(state == MBTK_NET_REG_STATE_HOME
6453 || state == MBTK_NET_REG_STATE_ROAMING) { // Registered, home network or roaming.
6454 mbtk_net_info_t info;
6455 int cme_err = MBTK_INFO_ERR_CME_NON;
6456 memset(&info, 0, sizeof(mbtk_net_info_t));
6457 if(!req_net_sel_mode_get(&info, &cme_err) && cme_err == MBTK_INFO_ERR_CME_NON)
6458 {
6459 net_data[2] = info.net_type;
6460 net_state_change(net_data, sizeof(net_data));
6461
6462 if(info.net_type >= MBTK_RADIO_TECH_UTRAN) {
6463 data_call_restart();
6464 }
6465 } else {
6466 net_data[2] = (uint8)0xFF;
6467 net_state_change(net_data, sizeof(net_data));
6468 }
6469 } else {
6470 net_data[2] = (uint8)0xFF;
6471 net_state_change(net_data, sizeof(net_data));
6472 }
6473 break;
6474 }
b.liuf37bd332024-03-18 13:51:24 +08006475 case INFO_URC_MSG_SET_BAND:
6476 {
6477 mbtk_band_info_t band;
6478 int cme_err = MBTK_INFO_ERR_CME_NON;
6479
6480 band.net_pref = MBTK_NET_PREF_GSM_UMTS_LTE_LTE_PREF; // 15
b.liu288093c2024-05-09 17:02:57 +08006481 band.gsm_band = (uint16)band_set_info.band_gsm;
6482 band.umts_band = (uint16)band_set_info.band_wcdma;
b.liuf37bd332024-03-18 13:51:24 +08006483 band.tdlte_band = band_set_info.band_tdlte;
6484 band.fddlte_band = band_set_info.band_fddlte;
b.liu288093c2024-05-09 17:02:57 +08006485 band.lte_ext_band = band_set_info.band_lte_ext;
6486
b.liuf37bd332024-03-18 13:51:24 +08006487 if(req_band_set(&band, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
6488 {
6489 LOG("Set band fail.");
6490 }
6491 else // Set band success.
6492 {
b.liu45bfb532024-05-11 15:37:22 +08006493 // log_hex("BAND-2", &band_set_info, sizeof(band_set_info_t));
b.liu288093c2024-05-09 17:02:57 +08006494 band_set_success = TRUE;
6495 if(band_area == MBTK_MODEM_BAND_AREA_CN) {
b.liuf37bd332024-03-18 13:51:24 +08006496 property_set("persist.mbtk.band_config", "CN");
b.liu288093c2024-05-09 17:02:57 +08006497 } else if(band_area == MBTK_MODEM_BAND_AREA_EU) {
b.liuf37bd332024-03-18 13:51:24 +08006498 property_set("persist.mbtk.band_config", "EU");
b.liu288093c2024-05-09 17:02:57 +08006499 } else if(band_area == MBTK_MODEM_BAND_AREA_SA) {
b.liuf678f992024-05-08 15:23:10 +08006500 property_set("persist.mbtk.band_config", "SA");
b.liuf37bd332024-03-18 13:51:24 +08006501 } else {
6502 property_set("persist.mbtk.band_config", "ALL");
6503 }
6504 LOG("Set band success.");
6505 }
6506 break;
6507 }
wangyouqiangce45a102024-04-18 18:08:29 +08006508 case INFO_URC_MSG_GET_SIM_STATE:
6509 {
6510 net_info.sim_state = getSIMStatus();
6511 if(net_info.sim_state == MBTK_SIM_READY)
6512 {
6513 LOG("SIM READY!");
6514 }
6515 else
6516 {
6517 LOG("SIM NOT READY!");
6518 }
6519 break;
6520 }
liubin281ac462023-07-19 14:22:54 +08006521 case INFO_URC_MSG_NET_STATE_LOG:
6522 {
6523 // Get network state and signal.
6524 char buff[256] = {0};
r.xiaofca7c472024-04-24 01:00:23 -07006525 uint8 data_signal[7];
r.xiao9ad82632024-04-24 02:18:13 -07006526 mbtk_signal_info_t signal, tmp_signal;
liubin281ac462023-07-19 14:22:54 +08006527 memset(&signal, 0xFF, sizeof(mbtk_signal_info_t));
6528 if(!req_net_signal_get(&signal, NULL)) {
6529 char tmp[50] = {0};
6530 struct timeval log_time;
6531 gettimeofday(&log_time, NULL);
6532 struct tm* tm_t = localtime(&(log_time.tv_sec));
6533 strftime(tmp, 50, "%F %T", tm_t);
6534 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,
6535 signal.rsrq, signal.rsrp);
6536 mbtk_signal_log(buff);
r.xiao9ad82632024-04-24 02:18:13 -07006537 memset(&tmp_signal, 0xFF, sizeof(mbtk_signal_info_t));
6538 tmp_signal.type = signal.type;
6539 tmp_signal.rssi = signal.rssi;
6540 tmp_signal.rxlev = signal.rxlev;
6541 tmp_signal.ber = signal.ber;
6542 tmp_signal.rscp = signal.rscp;
6543 tmp_signal.ecno = signal.ecno;
6544 tmp_signal.rsrq = signal.rsrq;
6545 tmp_signal.rsrp = signal.rsrp;
r.xiaofca7c472024-04-24 01:00:23 -07006546
r.xiao9ad82632024-04-24 02:18:13 -07006547 if( (signal_globe.type != tmp_signal.type) || (signal_globe.rssi != tmp_signal.rssi) || (signal_globe.rxlev != tmp_signal.rxlev) ||
6548 (signal_globe.ber != tmp_signal.ber) || (signal_globe.rscp != tmp_signal.rscp) || (signal_globe.ecno != tmp_signal.ecno) ||
6549 (signal_globe.rsrq != tmp_signal.rsrq) || (signal_globe.rsrp != tmp_signal.rsrp) )
b.liu288093c2024-05-09 17:02:57 +08006550
r.xiaofca7c472024-04-24 01:00:23 -07006551 {
r.xiao9ad82632024-04-24 02:18:13 -07006552 signal_globe.type = signal.type;
6553 signal_globe.rssi = signal.rssi;
6554 signal_globe.rxlev = signal.rxlev;
6555 signal_globe.ber = signal.ber;
6556 signal_globe.rscp = signal.rscp;
6557 signal_globe.ecno = signal.ecno;
6558 signal_globe.rsrq = signal.rsrq;
6559 signal_globe.rsrp = signal.rsrp;
r.xiaofca7c472024-04-24 01:00:23 -07006560 //ADD SIGANLE CB
6561 data_signal[0] = signal.type;
6562 data_signal[1] = signal.rssi;
6563 data_signal[2] = signal.rxlev;
6564 data_signal[3] = signal.ber;
6565 data_signal[4] = signal.rscp;
6566 data_signal[5] = signal.ecno;
6567 data_signal[6] = signal.rsrq;
6568 data_signal[7] = signal.rsrp;
6569 /*
6570 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",
6571 data_signal[0],data_signal[1],data_signal[2],data_signal[3],data_signal[4],data_signal[5],data_signal[6],data_signal[7]);
6572 */
6573 signal_state_change(data_signal, sizeof(data_signal));
6574 }
liubin281ac462023-07-19 14:22:54 +08006575 }
liubin281ac462023-07-19 14:22:54 +08006576 break;
6577 }
6578 default:
6579 {
6580 LOGE("Unknown URC : %d", msg->msg);
6581 break;
6582 }
6583 }
6584}
6585
6586static void* pack_process_thread(void* arg)
6587{
6588 UNUSED(arg);
6589 info_queue_item_t* item = NULL;
6590 mbtk_queue_init(&info_queue);
6591 pthread_mutex_init(&info_mutex, NULL);
6592 pthread_cond_init(&info_cond, NULL);
6593
6594 memset(&band_support, 0xFF, sizeof(mbtk_band_info_t));
6595
6596 pthread_mutex_lock(&info_mutex);
6597 while(TRUE)
6598 {
6599 if(mbtk_queue_empty(&info_queue))
6600 {
6601 LOG("Packet process wait...");
6602 pthread_cond_wait(&info_cond, &info_mutex);
6603 LOG("Packet process continue...");
6604 }
6605 else
6606 {
6607 LOG("Packet process queue not empty,continue...");
6608 }
6609
6610 // Process all information request.
6611 mbtk_info_err_enum err;
6612 while((item = (info_queue_item_t*)mbtk_queue_get(&info_queue)) != NULL)
6613 {
6614 if(item->cli_info) { // REQ form client.
6615 mbtk_info_pack_t *pack = (mbtk_info_pack_t*)item->pack;
6616 LOG("Process REQ %s.", id2str(pack->info_id));
6617 at_process = true;
6618 err = pack_req_process(item->cli_info, pack);
6619 if(err != MBTK_INFO_ERR_SUCCESS)
6620 {
6621 if(item->cli_info->fd != DATA_CALL_BOOTCONN_FD)
6622 {
6623 pack_error_send(item->cli_info->fd, pack->info_id + 1, err);
6624 }
6625 else
6626 {
wangyouqiang80487e42024-05-24 15:06:20 +08006627 if(pack->data != NULL)
6628 {
6629 free(pack->data);
6630 }
6631 if(item->cli_info)
6632 {
6633 free(item->cli_info);
6634 }
liubin281ac462023-07-19 14:22:54 +08006635 }
6636 }
6637 at_process = false;
6638 mbtk_info_pack_free(&pack);
6639 free(item);
6640 } else { // REQ from myself.
6641 info_urc_msg_t *urc = (info_urc_msg_t*)item->pack;
6642 LOG("Process URC %d.", urc->msg);
6643 urc_msg_process(urc);
6644 if(!urc->data)
6645 free(urc->data);
6646 free(urc);
6647 }
6648 }
6649 }
6650 pthread_mutex_unlock(&info_mutex);
6651 return NULL;
6652}
6653
6654void apn_prop_get()
6655{
6656 char prop_name[20];
6657 char prop_data[300];
6658 // cid : 2 - 7
6659 int cid = MBTK_APN_CID_MIN;
6660 mbtk_apn_info_t apn;
6661 for(; cid <= MBTK_APN_CID_MAX; cid++) {
6662 memset(prop_name, 0, 20);
6663 memset(prop_data, 0, 300);
6664 memset(&apn, 0, sizeof(mbtk_apn_info_t));
6665 sprintf(prop_name, "%s_%d",MBTK_APN_PROP,cid);
6666 if(property_get(prop_name, prop_data, "") > 0 && !str_empty(prop_data)) {
6667 apn.cid = cid;
6668 char *ptr_1 = prop_data;
6669 apn.ip_type = (mbtk_ip_type_enum)atoi(ptr_1);
6670 ptr_1 = strstr(ptr_1, ",");
6671 if(!ptr_1) {
6672 continue;
6673 }
6674 ptr_1++; // Jump ',' to apn
6675
6676 char *ptr_2 = strstr(ptr_1, ",");
6677 if(!ptr_2) {
6678 continue;
6679 }
6680 memcpy(apn.apn, ptr_1, ptr_2 - ptr_1); // apn
6681
6682 ptr_2++; // Jump ',' to user
6683 ptr_1 = strstr(ptr_2, ",");
6684 if(!ptr_1) {
6685 continue;
6686 }
6687 if(memcmp(ptr_2, "NULL", 4)) { // Not "NULL"
6688 memcpy(apn.user, ptr_2, ptr_1 - ptr_2); // user
6689 }
6690
6691 ptr_1++; // Jump ',' to pass
6692 ptr_2 = strstr(ptr_1, ",");
6693 if(!ptr_2) {
6694 continue;
6695 }
6696 if(memcmp(ptr_1, "NULL", 4)) { // Not "NULL"
6697 memcpy(apn.pass, ptr_1, ptr_2 - ptr_1); // pass
6698 }
6699
6700 ptr_2++; // Jump ',' to auth (Is last item)
6701 if(memcmp(ptr_2, "NULL", 4)) { // Not "NULL"
6702 memcpy(apn.auth, ptr_2, strlen(ptr_2)); // auth
6703 }
6704
6705 req_apn_set(&apn, NULL);
6706 }
6707 }
6708}
6709
6710/*
b.liue0ab2442024-02-06 18:53:28 +08006711root@OpenWrt:/usrdata# cat /proc/mtd
6712dev: size erasesize name
6713mtd0: 00040000 00020000 "bootloader"
6714mtd1: 00020000 00020000 "cp_reliabledata"
6715mtd2: 00020000 00020000 "ap_reliabledata"
6716mtd3: 00020000 00020000 "cp_reliabledata_backup"
6717mtd4: 00020000 00020000 "ap_reliabledata_backup"
6718mtd5: 00020000 00020000 "mep-ota"
6719mtd6: 00020000 00020000 "mep-ota_backup"
6720mtd7: 00040000 00020000 "dtim"
6721mtd8: 00f40000 00020000 "cpimage"
6722mtd9: 000c0000 00020000 "u-boot"
6723mtd10: 00500000 00020000 "kernel"
6724mtd11: 00100000 00020000 "asr_flag"
6725mtd12: 01400000 00020000 "rootfs"
6726mtd13: 01400000 00020000 "oem_data"
6727mtd14: 01e00000 00020000 "OTA"
6728mtd15: 01400000 00020000 "rootfs_data"
6729mtd16: 081a0000 00020000 "user_data"
6730mtd17: 00d20000 00020000 "MRVL_BBM"
6731*/
6732static int partition_name_2_dev(const char *name, char *dev) {
6733 if(name == NULL || dev == NULL) {
6734 LOGE("ARG error.");
6735 return -1;
6736 }
6737
6738 int fp = fopen("/proc/mtd", "r");
6739 if (fp == NULL) {
6740 LOGE("Open MTD failed!");
6741 return -1;
6742 }
6743
6744 char buf[1024];
6745 while (fgets(buf, 1024, fp) != NULL) {
6746 if(strstr(buf, name)) {
6747 int index = atoi(buf + 3);
6748 sprintf(dev, "/dev/mtdblock%d", index);
6749 LOGD("%s -> %s", name, dev);
6750 return 0;
6751 }
6752 }
6753
6754 return -1;
6755}
6756
6757static int custom_partition_read(const char *name, mbtk_cust_info_t *cust_info)
6758{
6759 int fd = 0, ret = 0;
6760 unsigned int step = 0, offset = 0, flaglen = 0;
6761 flaglen = sizeof(mbtk_cust_info_t);
6762
6763 char mtd_path[50] = {0};
6764 if(partition_name_2_dev(name, mtd_path)) {
6765 LOGE("partition_name_2_dev() failed!");
6766 return -1;
6767 }
6768
6769 fd = open(mtd_path, O_RDONLY);
6770 if (fd < 0) {
6771 LOGE("Fatal error: can't open cust info %s\n", mtd_path);
6772 return -1;
6773 }
6774
6775 if (read(fd, cust_info, flaglen) < 0)
6776 goto error;
6777 if (cust_info->header != CUST_INFO_HEADER) {
6778 LOGE("Cust info partition error.");
6779 goto error;
6780 } else {
6781 if(cust_info->band_type == 1) { // CN
6782 LOGD("Band : CN");
6783 } else if(cust_info->band_type == 2) { // EU
6784 LOGD("Band : EU");
6785 } else {
6786 LOGE("Unknown band type:%d", cust_info->band_type);
6787 goto error;
6788 }
6789 }
6790 close(fd);
6791 return 0;
6792error:
6793 close(fd);
6794 return -1;
6795}
6796
6797/*
liubin281ac462023-07-19 14:22:54 +08006798AT*BAND=15,78,147,482,134742231
6799
6800OK
6801*/
b.liubb5e7682024-02-28 20:13:04 +08006802static void* band_config_thread()
liubin281ac462023-07-19 14:22:54 +08006803{
b.liubb5e7682024-02-28 20:13:04 +08006804 mbtk_device_info_modem_t info_modem;
b.liuf37bd332024-03-18 13:51:24 +08006805 memset(&band_set_info, 0, sizeof(band_set_info_t));
b.liubb5e7682024-02-28 20:13:04 +08006806 memset(&info_modem, 0, sizeof(mbtk_device_info_modem_t));
b.liu288093c2024-05-09 17:02:57 +08006807 band_set_success = FALSE;
b.liuf37bd332024-03-18 13:51:24 +08006808 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 +08006809 LOGD("mbtk_dev_info_read(MODEM) fail, use default band.");
b.liu288093c2024-05-09 17:02:57 +08006810 band_area = MBTK_MODEM_BAND_AREA_ALL;
b.liuf37bd332024-03-18 13:51:24 +08006811 band_set_info.band_gsm = MBTK_BAND_ALL_GSM_DEFAULT;
6812 band_set_info.band_wcdma = MBTK_BAND_ALL_WCDMA_DEFAULT;
6813 band_set_info.band_tdlte = MBTK_BAND_ALL_TDLTE_DEFAULT;
6814 band_set_info.band_fddlte = MBTK_BAND_ALL_FDDLTE_DEFAULT;
b.liu288093c2024-05-09 17:02:57 +08006815 band_set_info.band_lte_ext = MBTK_BAND_ALL_EXT_LTE_DEFAULT;
b.liubb5e7682024-02-28 20:13:04 +08006816 } else {
b.liu288093c2024-05-09 17:02:57 +08006817 band_area = info_modem.band_area;
b.liuf37bd332024-03-18 13:51:24 +08006818 band_set_info.band_gsm = info_modem.band_gsm;
6819 band_set_info.band_wcdma = info_modem.band_wcdma;
6820 band_set_info.band_tdlte = info_modem.band_tdlte;
6821 band_set_info.band_fddlte = info_modem.band_fddlte;
b.liuf678f992024-05-08 15:23:10 +08006822 band_set_info.band_lte_ext = info_modem.band_lte_ext;
b.liuf37bd332024-03-18 13:51:24 +08006823 }
b.liubb5e7682024-02-28 20:13:04 +08006824
b.liuf37bd332024-03-18 13:51:24 +08006825 bool is_first = TRUE;
b.liu288093c2024-05-09 17:02:57 +08006826 while(!band_set_success) {
b.liuf37bd332024-03-18 13:51:24 +08006827 info_urc_msg_t *urc = (info_urc_msg_t*)malloc(sizeof(info_urc_msg_t));
6828 if(!urc)
6829 {
6830 LOG("malloc() fail[%d].", errno);
6831 break;
b.liubb5e7682024-02-28 20:13:04 +08006832 } else {
b.liuf37bd332024-03-18 13:51:24 +08006833 urc->msg = INFO_URC_MSG_SET_BAND;
6834 urc->data = NULL;
6835 urc->data_len = 0;
6836 send_pack_to_queue(NULL, urc);
6837
6838 if(is_first) {
6839 is_first = FALSE;
6840 } else {
b.liu288093c2024-05-09 17:02:57 +08006841 LOGE("*BAND exec error, will retry in 5s.");
b.liuf37bd332024-03-18 13:51:24 +08006842 }
b.liu288093c2024-05-09 17:02:57 +08006843 sleep(5);
b.liubb5e7682024-02-28 20:13:04 +08006844 }
6845 }
6846
b.liuf37bd332024-03-18 13:51:24 +08006847 LOGD("Set Band thread exit.");
b.liubb5e7682024-02-28 20:13:04 +08006848 return NULL;
liubin281ac462023-07-19 14:22:54 +08006849}
6850
6851static void* net_monitor_thread(void* arg)
6852{
6853 UNUSED(arg);
6854 // Start network monitor
6855 int cid;
6856 while(1) {
6857#if 0
6858 // Config IP
6859 list_node_t* apn_list = NULL;
6860 if(!apn_state_get(&apn_list) && apn_list != NULL) {
6861 info_apn_ip_t *apn = NULL;
6862 for(cid = MBTK_APN_CID_MIN; cid <= MBTK_APN_CID_MAX && cid_active[cid]; cid++) {
6863 bool ip_found = false;
6864 list_first(apn_list);
6865 while ((apn = (info_apn_ip_t*) list_next(apn_list))) {
6866 if(cid == apn->cid) {
6867 ip_found = true;
6868 break;
6869 }
6870 }
6871
6872 char dev[20] = {0};
6873 sprintf(dev, "ccinet%d", cid - 1);
6874 if(ip_found) { // Ip ok,set IP.
6875 if(apn->ipv4_valid) {
6876 if(mbtk_ifc_configure2(dev, (char*)apn->ipv4, 0, NULL, "255.255.255.0")) {
6877 LOGD("Config %s IPv4 %s fail.", dev, apn->ipv4);
6878 } else {
6879 LOGD("Config %s IPv4 %s success.", dev, apn->ipv4);
6880 }
6881 }
6882
6883 if(apn->ipv6_valid) {
6884 if(mbtk_ipv6_config(dev, (char*)apn->ipv6, 64)) {
6885 LOGD("Config %s IPv6 %s fail.", dev, apn->ipv6);
6886 } else {
6887 LOGD("Config %s IPv6 %s success.", dev, apn->ipv6);
6888 }
6889 }
6890 } else { // No ip
6891 if(mbtk_ifc_configure2(dev, NULL, 0, NULL, NULL)) {
6892 LOGD("Config %s IPv4 0 fail.", dev);
6893 } else {
6894 LOGD("Config %s IPv4 0 success.", dev);
6895 }
6896 }
6897 }
6898
6899 list_free(apn_list);
6900 }
6901#endif
6902
6903 if(net_info.radio_state == MBTK_RADIO_STATE_ON && net_info.sim_state == MBTK_SIM_READY) {
6904#if 0
6905 urc_msg_distribute(true, INFO_URC_MSG_NET_CS_REG_STATE, NULL, 0);
6906#else
6907 info_urc_msg_t *urc = (info_urc_msg_t*)malloc(sizeof(info_urc_msg_t));
6908 if(!urc)
6909 {
6910 LOG("malloc() fail[%d].", errno);
6911 } else {
6912 urc->msg = INFO_URC_MSG_NET_STATE_LOG;
6913 urc->data = NULL;
6914 urc->data_len = 0;
6915 send_pack_to_queue(NULL, urc);
6916 }
6917#endif
wangyouqiangce45a102024-04-18 18:08:29 +08006918 sleep(15);
liubin281ac462023-07-19 14:22:54 +08006919 }
wangyouqiangce45a102024-04-18 18:08:29 +08006920 else
6921 {
6922 info_urc_msg_t *urc = (info_urc_msg_t*)malloc(sizeof(info_urc_msg_t));
6923 if(!urc)
6924 {
6925 LOG("malloc() fail[%d].", errno);
6926 }
6927 else
6928 {
6929 urc->msg = INFO_URC_MSG_GET_SIM_STATE;
6930 urc->data = NULL;
6931 urc->data_len = 0;
6932 send_pack_to_queue(NULL, urc);
6933 }
6934 sleep(2);
6935 }
liubin281ac462023-07-19 14:22:54 +08006936 }
6937
6938 LOGD("monitor_thread exit.");
6939 return NULL;
6940}
6941
6942static void* urc_process_thread(void* arg)
6943{
6944 UNUSED(arg);
6945 info_urc_msg_t* item = NULL;
6946 mbtk_queue_init(&urc_queue);
6947 pthread_mutex_init(&urc_mutex, NULL);
6948 pthread_cond_init(&urc_cond, NULL);
6949
6950 pthread_mutex_lock(&urc_mutex);
6951 while(TRUE)
6952 {
6953 if(mbtk_queue_empty(&urc_queue))
6954 {
6955 LOG("URC process wait...");
6956 pthread_cond_wait(&urc_cond, &urc_mutex);
6957 LOG("URC process continue...");
6958 }
6959 else
6960 {
6961 LOG("URC process queue not empty,continue...");
6962 }
6963
6964 // Process all information request.
6965 while((item = (info_urc_msg_t*)mbtk_queue_get(&urc_queue)) != NULL)
6966 {
6967 LOG("Process URC %d.", item->msg);
6968 uint8 *data = (uint8*)item->data;
6969 switch(item->msg) {
6970 case INFO_URC_MSG_RADIO_STATE:
6971 {
6972 radio_state_change(item->data, item->data_len);
6973 break;
6974 }
6975 case INFO_URC_MSG_CGEV:
6976 {
6977 bool act = data[0];
6978 int cid = data[1];
6979 if(cid > 0) {
6980 net_ifc_state_change(act, cid);
6981 }
6982 break;
6983 }
6984 default:
6985 {
6986 LOGE("Unknown URC : %d", item->msg);
6987 break;
6988 }
6989 }
6990 if(!item->data)
6991 free(item->data);
6992 free(item);
6993 }
6994 }
6995 pthread_mutex_unlock(&urc_mutex);
6996
6997 return NULL;
6998}
6999
7000static void ril_at_ready_process()
7001{
7002 net_info.radio_state = (isRadioOn() == 1) ? MBTK_RADIO_STATE_ON : MBTK_RADIO_STATE_OFF;
7003#if 1
7004 if (net_info.radio_state != MBTK_RADIO_STATE_ON)
7005 {
7006 setRadioPower(1);
7007 } else { // Radio has ON
7008 apn_prop_get();
7009 }
7010
7011 if(net_info.radio_state == MBTK_RADIO_STATE_ON)
7012 {
7013 at_send_command("AT+CEREG=2", NULL);
7014 }
7015
7016 int count = 0;
7017#endif
7018 net_info.sim_state = getSIMStatus();
7019#if 0
7020 while (net_info.sim_state != MBTK_SIM_READY && count < 30)
7021 {
7022 if(net_info.radio_state != MBTK_RADIO_STATE_ON)
7023 {
7024 setRadioPower(1);
7025 }
7026 LOGD("Waitting for SIM READY...");
7027 sleep(1);
7028 net_info.sim_state = getSIMStatus();
7029 count++;
7030 }
7031#endif
7032 if(net_info.sim_state == MBTK_SIM_READY)
7033 {
7034 LOGD("SIM READY!");
r.xiao7c62bc62024-05-24 00:53:30 -07007035 at_send_command("AT+COPS=3", NULL);
liubin281ac462023-07-19 14:22:54 +08007036 }
7037 else
7038 {
7039 LOGE("SIM NOT READY!");
7040 }
liubin281ac462023-07-19 14:22:54 +08007041}
7042
wangyouqiang80487e42024-05-24 15:06:20 +08007043int mbtk_get_apn_send_pack(void)
7044{
7045 sock_client_info_t *info = (sock_client_info_t*)malloc(sizeof(sock_client_info_t));
7046 if(info == NULL)
7047 {
7048 LOG("clinent_info malloc() fail.");
7049 return -1;
7050 }
7051 memset(info, 0, sizeof(sock_client_info_t));
7052 info->fd = DATA_CALL_APN_GET_FD;
7053
7054 mbtk_info_pack_t* pack = mbtk_info_pack_creat(MBTK_INFO_ID_NET_QSER_APN_REQ);
7055 if(pack == NULL)
7056 {
7057 free(info);
7058 LOG("Packet malloc() fail.");
7059 return -1;
7060 }
7061
7062 send_pack_to_queue(info, pack);
7063 return 0;
7064}
7065
liubin281ac462023-07-19 14:22:54 +08007066int mbtk_info_server_start()
7067{
7068 signal(SIGPIPE, SIG_IGN);
7069
wangyouqiangce45a102024-04-18 18:08:29 +08007070 //check cfun and sim card status
7071 ril_at_ready_process();
7072
7073 //any AT instruction that is not sent through pack_process_thread needs to precede the thread
7074 //thread create
liubin281ac462023-07-19 14:22:54 +08007075 if(sock_listen_fd > 0)
7076 {
7077 LOG("Information Server Has Started.");
7078 return -1;
7079 }
7080
7081 struct sockaddr_un server_addr;
7082 sock_listen_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
7083 if(sock_listen_fd < 0)
7084 {
7085 LOG("socket() fail[%d].", errno);
7086 return -1;
7087 }
7088
7089 // Set O_NONBLOCK
7090 int flags = fcntl(sock_listen_fd, F_GETFL, 0);
7091 if (flags < 0)
7092 {
7093 LOG("Get flags error:%d", errno);
7094 goto error;
7095 }
7096 flags |= O_NONBLOCK;
7097 if (fcntl(sock_listen_fd, F_SETFL, flags) < 0)
7098 {
7099 LOG("Set flags error:%d", errno);
7100 goto error;
7101 }
7102
7103 unlink(SOCK_INFO_PATH);
7104 memset(&server_addr, 0, sizeof(struct sockaddr_un));
7105 server_addr.sun_family = AF_LOCAL;
7106 strcpy(server_addr.sun_path, SOCK_INFO_PATH);
7107 if(bind(sock_listen_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)))
7108 {
7109 LOG("bind() fail[%d].", errno);
7110 goto error;
7111 }
7112
7113 if(listen(sock_listen_fd, SOCK_CLIENT_MAX))
7114 {
7115 LOG("listen() fail[%d].", errno);
7116 goto error;
7117 }
7118
7119 sock_client_list = list_create(sock_cli_free_func);
7120 if(sock_client_list == NULL)
7121 {
7122 LOG("list_create() fail.");
7123 goto error;
7124 }
7125
7126 pthread_t info_pid, pack_pid, monitor_pid, urc_pid, bootconn_pid;
7127 pthread_attr_t thread_attr;
7128 pthread_attr_init(&thread_attr);
7129 if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
7130 {
7131 LOG("pthread_attr_setdetachstate() fail.");
7132 goto error;
7133 }
7134
7135 if(pthread_create(&info_pid, &thread_attr, info_main_pthread, NULL))
7136 {
7137 LOG("pthread_create() fail.");
7138 goto error;
7139 }
7140
7141 if(pthread_create(&pack_pid, &thread_attr, pack_process_thread, NULL))
7142 {
7143 LOG("pthread_create() fail.");
7144 goto error;
7145 }
7146
7147#if 0
7148 if(pthread_create(&urc_pid, &thread_attr, urc_process_thread, NULL))
7149 {
7150 LOG("pthread_create() fail.");
7151 goto error;
7152 }
7153#endif
7154
b.liubb5e7682024-02-28 20:13:04 +08007155 // Set Band
7156 // AT*BAND=15,78,147,482,134742231
7157 char buff[10];
7158 memset(buff, 0, 10);
7159 property_get("persist.mbtk.band_config", buff, "");
7160 if(strlen(buff) == 0) {
7161 pthread_t band_pid;
7162 if(pthread_create(&band_pid, &thread_attr, band_config_thread, NULL))
7163 {
7164 LOG("pthread_create() fail.");
7165 }
7166 }
7167
b.liuf1ab8152024-05-23 13:16:07 +08007168#if 0
liubin281ac462023-07-19 14:22:54 +08007169 if(pthread_create(&monitor_pid, &thread_attr, net_monitor_thread, NULL))
7170 {
7171 LOG("pthread_create() fail.");
7172 }
b.liuf1ab8152024-05-23 13:16:07 +08007173#endif
liubin281ac462023-07-19 14:22:54 +08007174
7175 //mbtk wyq for data_call_ex add start
7176 if(pthread_create(&bootconn_pid, &thread_attr, data_call_bootconn_pthread, NULL))
7177 {
7178 LOG("pthread_create() fail.");
7179 }
7180 //mbtk wyq for data_call_ex add end
7181
7182 pthread_attr_destroy(&thread_attr);
7183
wangyouqiang80487e42024-05-24 15:06:20 +08007184 mbtk_qser_apn_init();
7185
liubin281ac462023-07-19 14:22:54 +08007186 LOG("MBTK Information Server Start...");
7187
7188 return 0;
7189
7190error:
7191 close(sock_listen_fd);
7192 sock_listen_fd = -1;
7193 return -1;
7194}
7195
7196#if 0
7197int main(int argc, char *argv[])
7198{
7199 if(mbtk_info_server_start())
7200 {
7201 return -1;
7202 }
7203
7204 while(1)
7205 {
7206 sleep(24 * 60 * 60);
7207 }
7208
7209 return 0;
7210}
7211#endif
7212
7213