blob: 04d20caebf59706792ae9ecb62b8cac3121e0173 [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>
b.liu9e8584b2024-11-06 19:21:28 +080013#include <sys/time.h>
liubin281ac462023-07-19 14:22:54 +080014
15#include "mbtk_type.h"
16#include "mbtk_info.h"
17#include "mbtk_queue.h"
18#include "atchannel.h"
19#include "at_tok.h"
20#include "mbtk_utils.h"
21#include "mbtk_ifc.h"
22#include "info_data.h"
wangyouqiang38e53362024-01-23 10:53:48 +080023#include "mbtk_led.h"
b.liue0ab2442024-02-06 18:53:28 +080024#include "cust_info.h"
b.liubb5e7682024-02-28 20:13:04 +080025#include "mbtk_device.h"
wangyouqiang80487e42024-05-24 15:06:20 +080026#include "mbtk_data_call.h"
liubin281ac462023-07-19 14:22:54 +080027
b.liuf37bd332024-03-18 13:51:24 +080028typedef struct {
b.liuf37bd332024-03-18 13:51:24 +080029 uint32 band_gsm;
30 uint32 band_wcdma;
31 uint32 band_tdlte;
32 uint32 band_fddlte;
b.liuf678f992024-05-08 15:23:10 +080033 uint32 band_lte_ext;
b.liuf37bd332024-03-18 13:51:24 +080034} band_set_info_t;
35
liubin281ac462023-07-19 14:22:54 +080036static int sock_listen_fd = -1;
37static int epoll_fd = -1;
38static list_node_t *sock_client_list = NULL;
39static mbtk_queue_node_t info_queue;
40static pthread_cond_t info_cond;
41static pthread_mutex_t info_mutex;
b.liu9e8584b2024-11-06 19:21:28 +080042//static mbtk_queue_node_t urc_queue;
43//static pthread_cond_t urc_cond;
44//static pthread_mutex_t urc_mutex;
b.liuf37bd332024-03-18 13:51:24 +080045static band_set_info_t band_set_info;
b.liu288093c2024-05-09 17:02:57 +080046static bool band_set_success = FALSE;
47static mbtk_modem_band_area_enum band_area;
liubin281ac462023-07-19 14:22:54 +080048
49static mbtk_band_info_t band_support;
50net_info_t net_info;
51mbtK_cell_pack_info_t cell_info;
52info_cgact_wait_t cgact_wait;
53static int cid_active[MBTK_APN_CID_MAX + 1] = {0};
54bool at_process = false;
yq.wangd58f71e2024-08-21 23:45:31 -070055bool at_cfun_command = false;
liubin281ac462023-07-19 14:22:54 +080056//mbtk wyq for data_call_ex add start
57// |2----7|
58//"00000000"+'\0'
59static char cid_bootconn[MBTK_APN_CID_MAX + 2] = {0};
60#define DATA_CALL_BOOTCONN_FD 0x5f6f7f8f
61//mbtk wyq for data_call_ex add end
62
63//mbtk wyq for server_ready_status add start
64static char server_ready_status = 0;
65//mbtk wyq for server_ready_status add end
66
r.xiaofca7c472024-04-24 01:00:23 -070067static mbtk_signal_info_t signal_globe;
68
b.liu06559f62024-11-01 18:48:22 +080069mbtk_info_err_enum ecall_pack_req_process(sock_client_info_t* cli_info, mbtk_info_pack_t* pack);
b.liu9e8584b2024-11-06 19:21:28 +080070void apn_prop_get();
71int mbtk_signal_log(char *data);
72
r.xiaoec113d12024-01-12 02:13:28 -080073
74/*
75AT*POWERIND=0"
76or
77AT*POWERIND=1~31"
78
79OK
80
81AT*POWERIND=31,就相当于设置NETWORK、SIM、SMS、CS CALL、PS DATA变化时都不主动上报,
82其中PS DATA目前暂时不支持,只是保留了这个标志位,0 means resume all.
83
84AP power state: 1~31 means suspend,
85bitmap:
86bit0 - NETWORK;
87bit1 - SIM;
88bit2 - SMS;
89bit3 - CS CALL
90bit4 - PS DATA
91
92*/
93static int req_powerind_set(uint32 state, int *cme_err)
94{
95 ATResponse *response = NULL;
96 char cmd[100] = {0};
97
98 if (state >= 0 && state < 32)
99 {
100 sprintf(cmd, "AT*POWERIND=%d", state);
r.xiaocfd7c682024-01-22 03:59:46 -0800101 LOG("Set the powerind command is = [%s]\n", cmd);
r.xiaoec113d12024-01-12 02:13:28 -0800102 }
103 int err = at_send_command(cmd, &response);
104 if (err < 0 || response->success == 0){
105 *cme_err = at_get_cme_error(response);
106 goto exit;
107 }
108
109exit:
110 at_response_free(response);
111 return err;
112}
113
114/*
r.xiaocfd7c682024-01-22 03:59:46 -0800115AT+OOSPP=1
116or
117AT+OOSPP=0
118or
r.xiaoec113d12024-01-12 02:13:28 -0800119AT+OOSPP=1,20,30,40 //AtOospp()
120 param1:mode
121 param2:oosPhasePeriod[0] //5 times, 5s by default;
r.xiaocfd7c682024-01-22 03:59:46 -0800122 param3:oosPhasePeriod[1] //5 times, 10s by default;
123 param4:oosPhasePeriod[2] //unlimited, 20s by default;
r.xiaoec113d12024-01-12 02:13:28 -0800124
125
126BTW
1271, 如果只输入mode=1,其余参数不设置,相当于这个功能打开,时间间隔是这个功能的默认值。
1282, 如果当mode=1加上其余设置参数后,功能打开,时间间隔是本次设置的值;
r.xiaocfd7c682024-01-22 03:59:46 -08001293,如果再设置mode=0,相当于这个功能关闭,是走平台自己另一套的搜网设置。
130平台本身是有一套间隔搜网,也有历史频点优先处理的逻辑(不需要我们进行处理),
131提供给我们的AT+OOSPP指令是让我们可以自定义搜网间隔
r.xiaoec113d12024-01-12 02:13:28 -0800132*/
r.xiaocfd7c682024-01-22 03:59:46 -0800133static int req_oos_set(mbtk_oos_info* state, int *cme_err)
r.xiaoec113d12024-01-12 02:13:28 -0800134{
135 ATResponse *response = NULL;
136 char cmd[100] = {0};
b.liu9e8584b2024-11-06 19:21:28 +0800137 int err = 0;
r.xiaoec113d12024-01-12 02:13:28 -0800138
r.xiaocfd7c682024-01-22 03:59:46 -0800139 if ((state->mode == 1 && state->oosPhase[0] == 0 && state->oosPhase[1] == 0 && state->oosPhase[2] == 0) \
140 || state->mode == 0)
r.xiaoec113d12024-01-12 02:13:28 -0800141 {
r.xiaocfd7c682024-01-22 03:59:46 -0800142 sprintf(cmd, "AT+OOSPP=%d", state->mode);//只有一个值0/1
r.xiaoec113d12024-01-12 02:13:28 -0800143 }
144 else
145 {
r.xiaocfd7c682024-01-22 03:59:46 -0800146 if ((state->oosPhase[0] != 0) && (state->oosPhase[1] != 0) && (state->oosPhase[2] != 0))
147 {
148 sprintf(cmd, "AT+OOSPP=%d,%d,%d,%d", state->mode, state->oosPhase[0], state->oosPhase[1], state->oosPhase[2]);
149 }
150 else if ((state->oosPhase[0] != 0) && (state->oosPhase[1] != 0) && (state->oosPhase[2] == 0))
151 {
152 sprintf(cmd, "AT+OOSPP=%d,%d,%d", state->mode, state->oosPhase[0], state->oosPhase[1]);
153 }
154 else if ((state->oosPhase[0] != 0) && (state->oosPhase[1] == 0) && (state->oosPhase[2] == 0))
155 {
156 sprintf(cmd, "AT+OOSPP=%d,%d", state->mode, state->oosPhase[0]);
157 }
158 else
159 {
160 LOG("AT+OOSPP SET ERR");
161 goto exit;
162 }
r.xiaoec113d12024-01-12 02:13:28 -0800163 }
164
r.xiaocfd7c682024-01-22 03:59:46 -0800165 LOG("Set the oos command is = [%s]\n", cmd);
b.liu9e8584b2024-11-06 19:21:28 +0800166 err = at_send_command(cmd, &response);
r.xiaoec113d12024-01-12 02:13:28 -0800167 if (err < 0 || response->success == 0){
168 *cme_err = at_get_cme_error(response);
169 goto exit;
170 }
171
172exit:
173 at_response_free(response);
174 return err;
175}
176
177
178/*
179AT+OOSPP?
r.xiaocfd7c682024-01-22 03:59:46 -0800180开(默认值):
181+OOSPP:5,10,20
r.xiaoec113d12024-01-12 02:13:28 -0800182关:
183+OOSPP:0
184*/
185static int req_oos_get(mbtk_oos_info *req, int *cme_err)
186{
187 ATResponse *response = NULL;
188
r.xiaocfd7c682024-01-22 03:59:46 -0800189 int err = at_send_command_singleline("AT+OOSPP?", "+OOSPP:", &response);
r.xiaoec113d12024-01-12 02:13:28 -0800190
191 if (err < 0 || response->success == 0 || !response->p_intermediates){
192 *cme_err = at_get_cme_error(response);
193 goto exit;
194 }
195
196 char *line = response->p_intermediates->line;
197
198 char *tmp_str = NULL;
r.xiaocfd7c682024-01-22 03:59:46 -0800199 err = at_tok_start(&line);//+OOSPP:10,15,20,过滤+OOSPP:
200 if (err < 0)
201 {
202 goto exit;
203 }
204
205 //LOG("req_oos_get =[%s]",line);
206
r.xiaoec113d12024-01-12 02:13:28 -0800207 err = at_tok_nextstr(&line, &tmp_str);
208 if (err < 0)
209 {
210 goto exit;
211 }
212
r.xiaoec113d12024-01-12 02:13:28 -0800213 int mode = atoi(tmp_str);
214 if (mode == 0)//关闭状态
215 {
r.xiaocfd7c682024-01-22 03:59:46 -0800216 req->mode = mode;
r.xiaoec113d12024-01-12 02:13:28 -0800217 }
218 else//开状态
219 {
220 req->mode = 1;
r.xiaocfd7c682024-01-22 03:59:46 -0800221 //LOG("tmp_str =[%s]",tmp_str);
222 req->oosPhase[0] = atoi(tmp_str);
b.liufe320632024-01-17 20:38:08 +0800223
r.xiaoec113d12024-01-12 02:13:28 -0800224 err = at_tok_nextstr(&line, &tmp_str);
225 if (err < 0)
226 {
227 goto exit;
228 }
r.xiaocfd7c682024-01-22 03:59:46 -0800229 //LOG("tmp_str =[%s]",tmp_str);
230 req->oosPhase[1] = atoi(tmp_str);
r.xiaoec113d12024-01-12 02:13:28 -0800231
232 err = at_tok_nextstr(&line, &tmp_str);
233 if (err < 0)
234 {
235 goto exit;
236 }
r.xiaocfd7c682024-01-22 03:59:46 -0800237 //LOG("tmp_str =[%s]",tmp_str);
238 req->oosPhase[2] = atoi(tmp_str);
r.xiaoec113d12024-01-12 02:13:28 -0800239 }
240
r.xiaoec113d12024-01-12 02:13:28 -0800241exit:
242 at_response_free(response);
243 return err;
244}
245
liubin281ac462023-07-19 14:22:54 +0800246static void sock_cli_free_func(void *data)
247{
248 if (data)
249 {
250 sock_client_info_t *info = (sock_client_info_t*) data;
251 LOG("Free Socket client[fd = %d].", info->fd);
252 free(info);
253 }
254}
255
256static void cli_close(sock_client_info_t* client)
257{
258 struct epoll_event ev;
259 memset(&ev,0,sizeof(struct epoll_event));
260 ev.data.fd = client->fd;
261 ev.events = EPOLLIN | EPOLLERR | EPOLLET;
262 epoll_ctl(epoll_fd, EPOLL_CTL_DEL, client->fd, &ev);
263
264 close(client->fd);
265
266 if(list_remove(sock_client_list, client))
267 {
268 sock_cli_free_func(client);
269 }
270}
271
272static void pack_error_send(int fd, int info_id, int err)
273{
274 mbtk_info_pack_t* pack = mbtk_info_pack_creat(info_id);
275 if(pack)
276 {
277 pack->info_err = (uint16)err;
278 mbtk_info_pack_send(fd, pack);
279 mbtk_info_pack_free(&pack);
280 }
281 else
282 {
283 LOG("mbtk_info_pack_creat() fail.");
284 }
285}
286
287void pack_rsp_send(int fd, int info_id, const void* data, int data_len)
288{
289 mbtk_info_pack_t* pack = mbtk_info_pack_creat(info_id);
290 if(pack)
291 {
292 pack->info_err = (uint16)MBTK_INFO_ERR_SUCCESS;
293 if(data != NULL && data_len > 0)
294 {
295 //mbtk_info_pack_data_set(pack, data, data_len);
296 pack->data_len = (uint16)data_len;
b.liu9e8584b2024-11-06 19:21:28 +0800297 pack->data = (uint8*)data;
liubin281ac462023-07-19 14:22:54 +0800298 }
299 mbtk_info_pack_send(fd, pack);
300 mbtk_info_pack_free(&pack);
301 }
302 else
303 {
304 LOG("mbtk_info_pack_creat() fail.");
305 }
306}
307
308static int apn_prop_set(mbtk_apn_info_t *apn)
309{
310 char prop_name[20] = {0};
b.liu9e8584b2024-11-06 19:21:28 +0800311 char prop_data[1024] = {0};
liubin281ac462023-07-19 14:22:54 +0800312 sprintf(prop_name, "%s_%d",MBTK_APN_PROP,apn->cid);
b.liu9e8584b2024-11-06 19:21:28 +0800313 snprintf(prop_data, sizeof(prop_data), "%d,%s,%s,%s,%s", apn->ip_type, apn->apn,
314 str_empty(apn->user) ? "NULL" : (char*)apn->user,
315 str_empty(apn->pass) ? "NULL" : (char*)apn->pass,
316 str_empty(apn->auth) ? "NULL" : (char*)apn->auth);
liubin281ac462023-07-19 14:22:54 +0800317
318 return property_set(prop_name, prop_data);
319}
320
yq.wang9823ddf2024-11-14 02:38:14 -0800321static int apn_prop_del(int cid)
322{
323 char prop_name[32] = {0};
324 snprintf(prop_name, sizeof(prop_name), "%s_%d", MBTK_APN_PROP, cid);
325
326 return property_set(prop_name, "");
327}
328
liubin281ac462023-07-19 14:22:54 +0800329/*
330AT*BAND=?
331*BAND:(0-18),79,147,482,524503
332
333
334OK
335
336AT*BAND=15,78,147,482,134742231
337
338
339*/
340static void band_support_get()
341{
342 // Support band has get.
343 if(band_support.net_pref != 0xFF) {
344 return;
345 }
346
347#if 1
348 // 79,147,482,524503
b.liu288093c2024-05-09 17:02:57 +0800349 band_support.gsm_band = (uint16)79; // GSM : B2/B3/B5/B8(GSM 850/PGSM 900/EGSM 900/DCS GSM 1800/PCS GSM 1900)
350 band_support.umts_band = (uint16)155; // WCDMA : B1/B2/B4/B5/B8
351 band_support.tdlte_band = (uint32)482; // TDD-LTE : B34/B38/B39/B40/B41
352 band_support.fddlte_band = (uint32)134742239; // FDD-LTE : B1/B2/B3/B4/B5/B7/B8/B20/B28
353 band_support.lte_ext_band = (uint32)2; // B66
liubin281ac462023-07-19 14:22:54 +0800354 band_support.net_pref = (uint8)0;
355#else
356 ATResponse *response = NULL;
357 int tmp_int;
358 char *tmp_str;
359 int err = at_send_command_singleline("AT*BAND=?", "*BAND:", &response);
360
361 if (err < 0 || response->success == 0 || !response->p_intermediates)
362 goto exit;
363
364 char *line = response->p_intermediates->line;
365 err = at_tok_start(&line);
366 if (err < 0)
367 {
368 goto exit;
369 }
370
371 err = at_tok_nextstr(&line, &tmp_str);
372 if (err < 0)
373 {
374 goto exit;
375 }
376
377 err = at_tok_nextint(&line, &tmp_int);
378 if (err < 0)
379 {
380 goto exit;
381 }
382 band_support.gsm_band = (uint16)tmp_int;
383
384 err = at_tok_nextint(&line, &tmp_int);
385 if (err < 0)
386 {
387 goto exit;
388 }
389 band_support.umts_band = (uint16)tmp_int;
390
391 err = at_tok_nextint(&line, &tmp_int);
392 if (err < 0)
393 {
394 goto exit;
395 }
396 band_support.tdlte_band = (uint32)tmp_int;
397
398 err = at_tok_nextint(&line, &tmp_int);
399 if (err < 0)
400 {
401 goto exit;
402 }
403 band_support.fddlte_band = (uint32)tmp_int;
404 band_support.net_pref = (uint8)0;
405exit:
406 at_response_free(response);
407#endif
408}
409
b.liu9e8584b2024-11-06 19:21:28 +0800410#if 0
liubin281ac462023-07-19 14:22:54 +0800411static int parseRegistrationState(char *str, int *items, int **response)
412{
413 int err;
414 char *line = str, *p;
415 int *resp = NULL;
416 int skip;
417 int commas;
418
419 LOGD("parseRegistrationState. Parsing: %s",str);
420 err = at_tok_start(&line);
421 if (err < 0) goto error;
422
423 /* Ok you have to be careful here
424 * The solicited version of the CREG response is
425 * +CREG: n, stat, [lac, cid]
426 * and the unsolicited version is
427 * +CREG: stat, [lac, cid]
428 * The <n> parameter is basically "is unsolicited creg on?"
429 * which it should always be
430 *
431 * Now we should normally get the solicited version here,
432 * but the unsolicited version could have snuck in
433 * so we have to handle both
434 *
435 * Also since the LAC and CID are only reported when registered,
436 * we can have 1, 2, 3, or 4 arguments here
437 *
438 * finally, a +CGREG: answer may have a fifth value that corresponds
439 * to the network type, as in;
440 *
441 * +CGREG: n, stat [,lac, cid [,networkType]]
442 */
443
444 /* count number of commas */
445 commas = 0;
446 for (p = line ; *p != '\0' ; p++)
447 {
448 if (*p == ',') commas++;
449 }
450
451 resp = (int *)calloc(commas + 1, sizeof(int));
452 if (!resp) goto error;
453 switch (commas)
454 {
455 case 0: /* +CREG: <stat> */
456 err = at_tok_nextint(&line, &resp[0]);
457 if (err < 0) goto error;
458 //resp[1] = -1;
459 //resp[2] = -1;
460 break;
461
462 case 1: /* +CREG: <n>, <stat> */
463 err = at_tok_nextint(&line, &skip);
464 if (err < 0) goto error;
465 err = at_tok_nextint(&line, &resp[0]);
466 if (err < 0) goto error;
467 resp[1] = -1;
468 //resp[2] = -1;
469 if (err < 0) goto error;
470 break;
471
472 case 2: /* +CREG: <stat>, <lac>, <cid> */
473 err = at_tok_nextint(&line, &resp[0]);
474 if (err < 0) goto error;
475 err = at_tok_nexthexint(&line, &resp[1]);
476 if (err < 0) goto error;
477 err = at_tok_nexthexint(&line, &resp[2]);
478 if (err < 0) goto error;
479 break;
480 case 3: /* +CREG: <n>, <stat>, <lac>, <cid> */
481 /* +CEREG: 1,"8330","06447340",7 */
482#if 0
483 err = at_tok_nextint(&line, &skip);
484 if (err < 0) goto error;
485 err = at_tok_nextint(&line, &resp[0]);
486 if (err < 0) goto error;
487 err = at_tok_nexthexint(&line, &resp[1]);
488 if (err < 0) goto error;
489 err = at_tok_nexthexint(&line, &resp[2]);
490 if (err < 0) goto error;
491#else
492 err = at_tok_nextint(&line, &resp[0]);
493 if (err < 0) goto error;
494 err = at_tok_nextint(&line, &resp[1]);
495 if (err < 0) goto error;
496 err = at_tok_nexthexint(&line, &resp[2]);
497 if (err < 0) goto error;
498 err = at_tok_nextint(&line, &resp[3]);
499 if (err < 0) goto error;
500#endif
501 break;
502 /* special case for CGREG, there is a fourth parameter
503 * that is the network type (unknown/gprs/edge/umts)
504 */
505 case 4: /* +CGREG: <n>, <stat>, <lac>, <cid>, <networkType> */
506 /* +CEREG: 2,1,"8330","06447340",7 */
507 err = at_tok_nextint(&line, &skip);
508 if (err < 0) goto error;
509 err = at_tok_nextint(&line, &resp[0]);
510 if (err < 0) goto error;
511 err = at_tok_nexthexint(&line, &resp[1]);
512 if (err < 0) goto error;
513 err = at_tok_nexthexint(&line, &resp[2]);
514 if (err < 0) goto error;
515 err = at_tok_nexthexint(&line, &resp[3]);
516 if (err < 0) goto error;
517 break;
518 default:
519 goto error;
520 }
521 /*
522 if(commas > 1) {
523 s_lac = resp[1];
524 s_cid = resp[2];
525 }*/
526 if (response)
527 *response = resp;
528 if (items)
529 *items = commas + 1;
530 return 0;
531error:
532 free(resp);
533 return -1;
534}
b.liu9e8584b2024-11-06 19:21:28 +0800535#endif
liubin281ac462023-07-19 14:22:54 +0800536/*
5370: minimum functionality
5381: full functionality
5393: disable phone receive RF circuits.
5404: disable phone both transmit and receive RF circuits
5415: disable SIM
5426: turn off full secondary receive.
543-1: fail
544*/
545static int isRadioOn()
546{
547 ATResponse *p_response = NULL;
548 int err;
549 char *line;
550 int ret;
551
552 err = at_send_command_singleline("AT+CFUN?", "+CFUN:", &p_response);
553
554 if (err < 0 || p_response->success == 0 || !p_response->p_intermediates)
555 {
556 // assume radio is off
557 goto error;
558 }
559
560 line = p_response->p_intermediates->line;
561
562 err = at_tok_start(&line);
563 if (err < 0) goto error;
564
565 err = at_tok_nextint(&line, &ret);
566 if (err < 0) goto error;
567
568 at_response_free(p_response);
569
570 if(ret == 1) {
571 net_info.radio_state = MBTK_RADIO_STATE_ON;
572 } else {
573 net_info.radio_state = MBTK_RADIO_STATE_OFF;
574 }
575
576 return ret;
577
578error:
579
580 at_response_free(p_response);
581 return -1;
582}
583
584/** Returns SIM_NOT_READY on error */
585static mbtk_sim_state_enum getSIMStatus()
586{
587 ATResponse *p_response = NULL;
588 int err;
589 mbtk_sim_state_enum ret;
590 char *cpinLine;
591 char *cpinResult;
592
593 err = at_send_command_singleline("AT+CPIN?", "+CPIN:", &p_response);
594
595 if (err < 0 || p_response->success == 0 || !p_response->p_intermediates)
596 {
yq.wang586a0df2024-10-24 20:10:37 -0700597 switch (at_get_cme_error(p_response))
598 {
599 case CME_SUCCESS:
600 break;
601
602 case CME_SIM_NOT_INSERTED:
b.liu9e8584b2024-11-06 19:21:28 +0800603 case CME_SIM_ABSENT: //sim fail,as "sim absent"
yq.wang586a0df2024-10-24 20:10:37 -0700604 ret = MBTK_SIM_ABSENT;
605 goto done;
606
607 default:
608 ret = MBTK_SIM_NOT_READY;
609 goto done;
610 }
liubin281ac462023-07-19 14:22:54 +0800611 ret = MBTK_SIM_NOT_READY;
612 goto done;
613 }
b.liu06559f62024-11-01 18:48:22 +0800614
liubin281ac462023-07-19 14:22:54 +0800615 /* CPIN? has succeeded, now look at the result */
616
617 cpinLine = p_response->p_intermediates->line;
618 err = at_tok_start (&cpinLine);
619
620 if (err < 0)
621 {
622 ret = MBTK_SIM_NOT_READY;
623 goto done;
624 }
625
626 err = at_tok_nextstr(&cpinLine, &cpinResult);
627
628 if (err < 0)
629 {
630 ret = MBTK_SIM_NOT_READY;
631 goto done;
632 }
633
634 if (0 == strcmp (cpinResult, "SIM PIN"))
635 {
636 ret = MBTK_SIM_PIN;
637 goto done;
638 }
639 else if (0 == strcmp (cpinResult, "SIM PUK"))
640 {
641 ret = MBTK_SIM_PUK;
642 goto done;
643 }
644 else if (0 == strcmp (cpinResult, "PH-NET PIN"))
645 {
646 return MBTK_SIM_NETWORK_PERSONALIZATION;
647 }
648 else if (0 != strcmp (cpinResult, "READY"))
649 {
650 /* we're treating unsupported lock types as "sim absent" */
651 ret = MBTK_SIM_ABSENT;
652 goto done;
653 }
654
655 at_response_free(p_response);
656 p_response = NULL;
657 cpinResult = NULL;
658
659 // ret = net_info.radio_state == MBTK_RADIO_STATE_ON ? MBTK_SIM_READY : MBTK_SIM_NOT_READY;
660 net_info.sim_state = MBTK_SIM_READY;
661 return MBTK_SIM_READY;
662done:
663 at_response_free(p_response);
664 net_info.sim_state = ret;
665 return ret;
666}
667
668void setRadioPower(int isOn)
669{
670 int err;
671 ATResponse *p_response = NULL;
672
673 LOGI("RadioPower - %s", isOn == 0 ? "OFF" : "ON");
674
675 if(isOn == 0 && net_info.radio_state == MBTK_RADIO_STATE_ON)
676 {
677 err = at_send_command("AT+CFUN=0", &p_response);
678 if (err || !p_response->success)
679 goto error;
680
681 net_info.radio_state = MBTK_RADIO_STATE_OFF;
682 }
683 else if(isOn && net_info.radio_state != MBTK_RADIO_STATE_ON)
684 {
685 err = at_send_command("AT+CFUN=1", &p_response);
686 if (err || !p_response->success)
687 {
688 if(isRadioOn() == 1)
689 {
690 net_info.radio_state = MBTK_RADIO_STATE_ON;
691 }
692 goto error;
693 }
694
695 net_info.radio_state = MBTK_RADIO_STATE_ON;
696 }
697
698 at_response_free(p_response);
699 return;
700error:
701 at_response_free(p_response);
702}
703
704static int apn_user_pass_set_by_cid(int cid, mbtk_apn_info_t *apn)
705{
706 char prop_name[20] = {0};
707 char prop_data[300] = {0};
708 sprintf(prop_name, "%s_%d",MBTK_APN_PROP,cid);
709 if(property_get(prop_name, prop_data, "") > 0 && !str_empty(prop_data)) {
710 char apn_name[128] = {0};
711 char *ptr_1 = prop_data;
712 mbtk_ip_type_enum ip_type = (mbtk_ip_type_enum)atoi(ptr_1);
713 ptr_1 = strstr(ptr_1, ",");
714 if(!ptr_1) {
715 return -1;
716 }
717 ptr_1++; // Jump ',' to apn
718
719 char *ptr_2 = strstr(ptr_1, ",");
720 if(!ptr_2) {
721 return -1;
722 }
723 memcpy(apn_name, ptr_1, ptr_2 - ptr_1); // apn
724
725 // Check ip_type and apn_name
b.liu9e8584b2024-11-06 19:21:28 +0800726 if(ip_type != apn->ip_type || strcmp(apn_name, (char*)apn->apn)) {
liubin281ac462023-07-19 14:22:54 +0800727 LOGD("APN Changed, not get user/pass/auth.");
728 return -1;
729 }
730
731 ptr_2++; // Jump ',' to user
732 ptr_1 = strstr(ptr_2, ",");
733 if(!ptr_1) {
734 return -1;
735 }
736 if(memcmp(ptr_2, "NULL", 4)) { // Not "NULL"
737 memcpy(apn->user, ptr_2, ptr_1 - ptr_2); // user
738 }
739
740 ptr_1++; // Jump ',' to pass
741 ptr_2 = strstr(ptr_1, ",");
742 if(!ptr_2) {
743 return -1;
744 }
745 if(memcmp(ptr_1, "NULL", 4)) { // Not "NULL"
746 memcpy(apn->pass, ptr_1, ptr_2 - ptr_1); // pass
747 }
748
749 ptr_2++; // Jump ',' to auth (Is last item)
750 if(memcmp(ptr_2, "NULL", 4)) { // Not "NULL"
751 memcpy(apn->auth, ptr_2, strlen(ptr_2)); // auth
752 }
753
754 return 0;
755 }
756 return -1;
757}
758
759
760/*
761AT+CPOL?
762*EUICC: 1
763
764OK
765*/
766static int req_plmn_get(mbtk_plmn_info *type, int *cme_err)
767{
768 ATResponse *response = NULL;
b.liu9e8584b2024-11-06 19:21:28 +0800769// char *tmp_ptr = NULL;
liubin281ac462023-07-19 14:22:54 +0800770 int err = at_send_command_multiline("AT+CPOL?", "+CPOL:", &response);
771
772 if (err < 0 || response->success == 0 || !response->p_intermediates){
773 *cme_err = at_get_cme_error(response);
774 goto exit;
775 }
776
777 int mccmnc_type = -1;
778 int count = -1;
779 char *mccmnc_name = NULL;
780 ATLine* lines_ptr = response->p_intermediates;
781 char *line = NULL;
782 while(lines_ptr)
783 {
784 line = lines_ptr->line;
785 //if(strStartsWith(line, "+CPOL:"))
786 {
787 err = at_tok_start(&line);
788 if (err < 0)
789 {
790 goto exit;
791 }
792 err = at_tok_nextint(&line, &count);
793 if (err < 0)
794 {
795 goto exit;
796 }
797 type->count = count;
798
799 err = at_tok_nextint(&line, &mccmnc_type);
800 if (err < 0)
801 {
802 goto exit;
803 }
804 type->mbtk_plmn_name[count-1].format = mccmnc_type;
805
806 err = at_tok_nextstr(&line, &mccmnc_name);
807 if (err < 0)
808 {
809 goto exit;
810 }
811 memcpy(type->mbtk_plmn_name[count-1].plmn_name, mccmnc_name, strlen(mccmnc_name));
812 mccmnc_name = NULL;
813 }
814 lines_ptr = lines_ptr->p_next;
815 }
816
817exit:
818 at_response_free(response);
819 return err;
820}
821
822
823/*
824AT*EUICC?
825*EUICC: 1
826
827OK
828*/
829static int req_sim_card_type_get(uint8 *type, int *cme_err)
830{
831 ATResponse *response = NULL;
b.liu9e8584b2024-11-06 19:21:28 +0800832// char *tmp_ptr = NULL;
liubin281ac462023-07-19 14:22:54 +0800833 int err = at_send_command_singleline("AT*EUICC?", "*EUICC:", &response);
834
835 if (err < 0 || response->success == 0 || !response->p_intermediates){
836 *cme_err = at_get_cme_error(response);
837 goto exit;
838 }
839
840 char *line = response->p_intermediates->line;
841 err = at_tok_start(&line);
842 if (err < 0)
843 {
844 goto exit;
845 }
846 int sim_card_type = -1;
847 err = at_tok_nextint(&line, &sim_card_type);
848 if (err < 0)
849 {
850 goto exit;
851 }
852 if(sim_card_type != -1)
853 *type = sim_card_type;
854 goto exit;
855exit:
856 at_response_free(response);
857 return err;
858}
859
860/*
861AT+EPIN?
862+EPIN: 3,0,10,0
863
864OK
865*/
866static int req_pin_puk_last_times_get(mbtk_pin_puk_last_times *times, int *cme_err)
867{
868 ATResponse *response = NULL;
b.liu9e8584b2024-11-06 19:21:28 +0800869// char *tmp_ptr = NULL;
870 int tmp_int;
liubin281ac462023-07-19 14:22:54 +0800871 int err = at_send_command_singleline("AT+EPIN?", "+EPIN:", &response);
872
873 if (err < 0 || response->success == 0 || !response->p_intermediates){
874 *cme_err = at_get_cme_error(response);
875 goto exit;
876 }
877
878 char *line = response->p_intermediates->line;
879 err = at_tok_start(&line);
880 if (err < 0)
881 {
882 goto exit;
883 }
884 mbtk_pin_puk_last_times last_times={0};
b.liu9e8584b2024-11-06 19:21:28 +0800885 err = at_tok_nextint(&line, &tmp_int);
liubin281ac462023-07-19 14:22:54 +0800886 if (err < 0)
887 {
888 goto exit;
889 }
b.liu9e8584b2024-11-06 19:21:28 +0800890 last_times.p1_retry = (uint8_t)tmp_int;
liubin281ac462023-07-19 14:22:54 +0800891 times->p1_retry = last_times.p1_retry;
b.liu9e8584b2024-11-06 19:21:28 +0800892 err = at_tok_nextint(&line, &tmp_int);
liubin281ac462023-07-19 14:22:54 +0800893 if (err < 0)
894 {
895 goto exit;
896 }
b.liu9e8584b2024-11-06 19:21:28 +0800897 last_times.p2_retry = (uint8_t)tmp_int;
liubin281ac462023-07-19 14:22:54 +0800898 times->p2_retry = last_times.p2_retry;
b.liu9e8584b2024-11-06 19:21:28 +0800899 err = at_tok_nextint(&line, &tmp_int);
liubin281ac462023-07-19 14:22:54 +0800900 if (err < 0)
901 {
902 goto exit;
903 }
b.liu9e8584b2024-11-06 19:21:28 +0800904 last_times.puk1_retry = (uint8_t)tmp_int;
liubin281ac462023-07-19 14:22:54 +0800905 times->puk1_retry = last_times.puk1_retry;
b.liu9e8584b2024-11-06 19:21:28 +0800906 err = at_tok_nextint(&line, &tmp_int);
liubin281ac462023-07-19 14:22:54 +0800907 if (err < 0)
908 {
909 goto exit;
910 }
b.liu9e8584b2024-11-06 19:21:28 +0800911 last_times.puk2_retry = (uint8_t)tmp_int;
liubin281ac462023-07-19 14:22:54 +0800912 times->puk2_retry = last_times.puk2_retry;
913
914exit:
915 at_response_free(response);
916 return err;
917}
918
919
920/*
921AT+CGSN
922864788050901201
923
924OK
925*/
926static int req_imei_get(void *data, int *cme_err)
927{
928 ATResponse *response = NULL;
929 int err = at_send_command_numeric("AT+CGSN", &response);
930
931 if (err < 0 || response->success == 0 || !response->p_intermediates) {
932 *cme_err = at_get_cme_error(response);
933 goto exit;
934 }
935
936 memcpy(data, response->p_intermediates->line, strlen(response->p_intermediates->line));
937exit:
938 at_response_free(response);
939 return err;
940}
941
942/*
943AT+MRD_SN=R
944+MRD_SN:0101,Thu Nov 12 00:00:00 2020,G4M32301020006
945
946OK
947
948*/
949static int req_sn_get(void *data, int *cme_err)
950{
951 ATResponse *response = NULL;
952 char *tmp_ptr = NULL;
953 int err = at_send_command_singleline("AT+MRD_SN=R", "+MRD_SN:", &response);
954
955 if (err < 0 || response->success == 0 || !response->p_intermediates){
956 *cme_err = at_get_cme_error(response);
957 goto exit;
958 }
959
960 char *line = response->p_intermediates->line;
961 err = at_tok_start(&line);
962 if (err < 0)
963 {
964 goto exit;
965 }
966
967 err = at_tok_nextstr(&line, &tmp_ptr);
968 if (err < 0)
969 {
970 goto exit;
971 }
972
973 err = at_tok_nextstr(&line, &tmp_ptr);
974 if (err < 0)
975 {
976 goto exit;
977 }
978
979 err = at_tok_nextstr(&line, &tmp_ptr);
980 if (err < 0)
981 {
982 goto exit;
983 }
984
985 memcpy(data, tmp_ptr, strlen(tmp_ptr));
986
987 goto exit;
988exit:
989 at_response_free(response);
990 return err;
991}
992
993/*
994AT+SYSTIME?
995+SYSTIME: 1
996
997OK
998
999*/
1000static int req_time_get(int *data, int *cme_err)
1001{
1002 ATResponse *response = NULL;
1003 int tmp_int;
1004 int err = at_send_command_singleline("AT+SYSTIME?", "+SYSTIME:", &response);
1005
1006 if (err < 0 || response->success == 0 || !response->p_intermediates){
1007 *cme_err = at_get_cme_error(response);
1008 goto exit;
1009 }
1010
1011 char *line = response->p_intermediates->line;
1012 err = at_tok_start(&line);
1013 if (err < 0)
1014 {
1015 goto exit;
1016 }
1017 err = at_tok_nextint(&line, &tmp_int);
1018 if (err < 0)
1019 {
1020 goto exit;
1021 }
1022 *data = tmp_int;
1023
1024exit:
1025 at_response_free(response);
1026 return err;
1027}
1028
1029/*
1030AT+CCLK?
1031+CCLK: "23/03/20,01:58:00+32"
1032
1033OK
1034
1035*/
1036static int req_net_time_get(char *data, int *cme_err)
1037{
1038 ATResponse *response = NULL;
1039 char *tmp_ptr = NULL;
1040 int err = at_send_command_singleline("AT+CCLK?", "+CCLK:", &response);
1041
1042 if (err < 0 || response->success == 0 || !response->p_intermediates){
1043 *cme_err = at_get_cme_error(response);
1044 goto exit;
1045 }
1046
1047 char *line = response->p_intermediates->line;
1048 err = at_tok_start(&line);
1049 if (err < 0)
1050 {
1051 goto exit;
1052 }
1053 err = at_tok_nextstr(&line, &tmp_ptr);
1054 if (err < 0)
1055 {
1056 goto exit;
1057 }
1058 memcpy(data, tmp_ptr, strlen(tmp_ptr));
1059
1060exit:
1061 at_response_free(response);
1062 return err;
1063}
1064
1065/*
1066AT+CFUN?
1067+CFUN: 1
1068OK
1069*/
1070static int req_modem_get(int *data, int *cme_err)
1071{
1072 ATResponse *response = NULL;
1073 int modem;
1074 int err = at_send_command_singleline("AT+CFUN?", "+CFUN:", &response);
1075
1076 if (err < 0 || response->success == 0 || !response->p_intermediates){
1077 *cme_err = at_get_cme_error(response);
1078 goto exit;
1079 }
1080
1081 char *line = response->p_intermediates->line;
1082 err = at_tok_start(&line);
1083 if (err < 0)
1084 {
1085 goto exit;
1086 }
1087 err = at_tok_nextint(&line, &modem);
1088 if (err < 0)
1089 {
1090 goto exit;
1091 }
1092 *data = modem;
1093
1094exit:
1095 at_response_free(response);
1096 return err;
1097}
1098
1099/*
1100AT+CFUN=<fun>[,<rst>]
1101OK
1102*/
1103static int req_modem_set(mbtk_modem_info_t* modem, int *cme_err)
1104{
1105 ATResponse *response = NULL;
1106 char cmd[30] = {0};
1107 int err = -1;
1108
1109 sprintf(cmd, "AT+CFUN=%d,%d", modem->fun, modem->rst);
1110 err = at_send_command(cmd, &response);
1111
1112 if (err < 0 || response->success == 0){
1113 *cme_err = at_get_cme_error(response);
1114 goto exit;
1115 }
1116/*
1117 ATResponse *response = NULL;
1118 int err = at_send_command_multiline(cmd, "", &response);
1119
1120 if (err < 0 || response->success == 0 || !response->p_intermediates){
1121 *cme_err = at_get_cme_error(response);
1122 goto exit;
1123 }
1124
1125 ATLine* lines_ptr = response->p_intermediates;
1126 char *line = NULL;
1127 while(lines_ptr)
1128 {
1129 line = lines_ptr->line;
1130 if(strStartsWith(line, "Revision"))
1131 {
1132 err = at_tok_start(&line);
1133 if (err < 0)
1134 {
1135 goto exit;
1136 }
1137 memcpy(data, line, strlen(line));
1138 break;
1139 }
1140 lines_ptr = lines_ptr->p_next;
1141 }
1142
1143 goto exit;
1144*/
1145exit:
1146 at_response_free(response);
1147 return err;
1148}
1149
1150
1151/*
1152AT+SYSTIME=0,"2022-01-25-11:15:30"
1153OK
1154
1155AT+SYSTIME=1
1156OK
1157*/
1158static int req_time_set(int type, char *time, int *cme_err)
1159{
1160 ATResponse *response = NULL;
b.liu9e8584b2024-11-06 19:21:28 +08001161// int tmp_int;
liubin281ac462023-07-19 14:22:54 +08001162 char cmd[200] = {0};
1163 if(str_empty(time)){
1164 sprintf(cmd, "AT+SYSTIME=%d", type);
1165 } else {
1166 sprintf(cmd, "AT+SYSTIME=%d,\"%s\"", type, time);
1167 }
1168 int err = at_send_command(cmd, &response);
1169
1170 if (err < 0 || response->success == 0){
1171 *cme_err = at_get_cme_error(response);
1172 goto exit;
1173 }
1174
1175exit:
1176 at_response_free(response);
1177 return err;
1178}
1179
1180
1181/*
1182ATI
1183Manufacturer:"LYNQ"
1184Model:"LYNQ_L508TLC"
1185Revision:L508TLCv02.01b01.00
1186IMEI:864788050901201
1187
1188OK
1189
1190*/
1191static int req_version_get(void *data, int *cme_err)
1192{
1193 ATResponse *response = NULL;
1194 int err = at_send_command_multiline("ATI", "", &response);
1195
1196 if (err < 0 || response->success == 0 || !response->p_intermediates){
1197 *cme_err = at_get_cme_error(response);
1198 goto exit;
1199 }
1200
1201 ATLine* lines_ptr = response->p_intermediates;
1202 char *line = NULL;
1203 while(lines_ptr)
1204 {
1205 line = lines_ptr->line;
1206 if(strStartsWith(line, "Revision"))
1207 {
1208 err = at_tok_start(&line);
1209 if (err < 0)
1210 {
1211 goto exit;
1212 }
1213 memcpy(data, line, strlen(line));
1214 break;
1215 }
1216 lines_ptr = lines_ptr->p_next;
1217 }
1218
1219 goto exit;
1220exit:
1221 at_response_free(response);
1222 return err;
1223}
1224
l.yang5b0ff422024-10-29 19:33:35 -07001225
1226static int req_md_version_get(void *data, int *cme_err)
1227{
1228 ATResponse *response = NULL;
1229 int err = at_send_command_multiline("AT*CGMR", "", &response);
1230
1231 if (err < 0 || response->success == 0 || !response->p_intermediates)
1232 {
1233 *cme_err = at_get_cme_error(response);
1234 goto exit;
1235 }
1236
1237 ATLine* lines_ptr = response->p_intermediates;
1238 char *line = NULL;
1239 while(lines_ptr)
1240 {
1241 line = lines_ptr->line;
1242 char *start = strstr(line, "FALCON_CP_SDK");
b.liu06559f62024-11-01 18:48:22 +08001243 if(start)
l.yang5b0ff422024-10-29 19:33:35 -07001244 {
1245 char *end = strstr(start, "_Linux");
b.liu06559f62024-11-01 18:48:22 +08001246 if(end)
l.yang5b0ff422024-10-29 19:33:35 -07001247 {
b.liu06559f62024-11-01 18:48:22 +08001248 end += strlen("_Linux");
l.yang5b0ff422024-10-29 19:33:35 -07001249 int length = end - start;
1250 if (length)
1251 {
1252 strncpy(data, start, length);
1253 ((char*)data)[length] = '\0';
1254 break;
b.liu06559f62024-11-01 18:48:22 +08001255 }
l.yang5b0ff422024-10-29 19:33:35 -07001256 else
1257 {
1258 err = -1;
1259 goto exit;
b.liu06559f62024-11-01 18:48:22 +08001260
l.yang5b0ff422024-10-29 19:33:35 -07001261 }
b.liu06559f62024-11-01 18:48:22 +08001262 }
1263 else
l.yang5b0ff422024-10-29 19:33:35 -07001264 {
1265 err = -1;
1266 goto exit;
1267 }
1268 }
1269 lines_ptr = lines_ptr->p_next;
1270 }
b.liu06559f62024-11-01 18:48:22 +08001271
l.yang5b0ff422024-10-29 19:33:35 -07001272 goto exit;
1273exit:
1274 at_response_free(response);
1275 return err;
1276}
1277
1278
1279
liubin281ac462023-07-19 14:22:54 +08001280/*
1281ATI
1282Manufacturer:"LYNQ"
1283Model:"LYNQ_L508TLC"
1284Revision:L508TLCv02.01b01.00
1285IMEI:864788050901201
1286
1287OK
1288
1289*/
1290static int req_model_get(void *data, int *cme_err)
1291{
1292 ATResponse *response = NULL;
1293 int err = at_send_command_multiline("ATI", "", &response);
1294
1295 if (err < 0 || response->success == 0 || !response->p_intermediates){
1296 *cme_err = at_get_cme_error(response);
1297 goto exit;
1298 }
1299
1300 ATLine* lines_ptr = response->p_intermediates;
1301 char *line = NULL;
1302 while(lines_ptr)
1303 {
1304 line = lines_ptr->line;
1305 if(strStartsWith(line, "Model"))
1306 {
1307 err = at_tok_start(&line);
1308 if (err < 0)
1309 {
1310 goto exit;
1311 }
1312 memcpy(data, line, strlen(line));
1313 break;
1314 }
1315 lines_ptr = lines_ptr->p_next;
1316 }
1317
1318 goto exit;
1319exit:
1320 at_response_free(response);
1321 return err;
1322}
1323
1324/*
xy.he22063af2024-12-06 01:28:08 -08001325AT+CRSM=176,28539,0,0,0
1326+CRSM: 144,0,"64F00064F01064F02064F06064F07064F00264F051FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
1327*/
1328static int req_fplmn_get(char *req, int *cme_err)
1329{
1330 ATResponse *response = NULL;
1331
1332 int err = at_send_command_singleline("AT+CRSM=176,28539,0,0,0", "", &response);
1333
1334 if (err < 0 || response->success == 0 || !response->p_intermediates){
1335 *cme_err = at_get_cme_error(response);
1336 goto exit;
1337 }
1338
1339 char *line = response->p_intermediates->line;
1340 LOG("req_fplmn_get() ---line = %s\n", line);
1341 char *ptr = strstr(line, "+CRSM: 144,0,");
1342 LOG("req_fplmn_get() ---ptr = %s\n", ptr+strlen("+CRSM: 144,0,"));
1343
1344 if(ptr)
1345 {
1346 char* start = strchr(ptr, '"') + 1;
1347 char* end = strrchr(ptr, '"') - 1;
1348 LOG("start = %s\n end = %s",start,end);
1349 size_t length = end - start + 1;
1350
1351 LOG("length = %d",length);
1352 strncpy(req, start, length);
1353 req[length] = '\0';
1354
1355 LOG("err:%d, req:%s\n", err, req);
1356 err = 0;
1357 }
1358 else
1359 {
1360 err = -1;
1361 }
1362
1363exit:
1364 at_response_free(response);
1365 return err;
1366}
1367
1368
1369/*
1370AT+CRSM=214,28539,0,0,0,64F00064F01064F02064F06064F07064F00264F051FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
1371OK
1372*/
1373
1374static int req_fplmn_set(char *req, char *reg, int *cme_err)
1375{
1376 ATResponse *response = NULL;
1377 char cmd[256] = {0};
1378 LOG("req_fplmn_set() ---cmd = %s\n", req);
1379 sprintf(cmd, "AT+CRSM=214,28539,0,0,0,%s", req);
1380 LOG("req_fplmn_set() ---cmd = %s\n", cmd);
1381
1382 int err = at_send_command_singleline(cmd, "+CRSM: 144,0",&response);
1383 LOG("err:%d, response:%d", err, response->success);
1384
1385 if (err < 0 || response->success == 0){
1386 *cme_err = at_get_cme_error(response);
1387 goto exit;
1388 }
1389
1390exit:
1391 at_response_free(response);
1392 return err;
1393}
1394
1395
1396
1397
1398
1399/*
liubin281ac462023-07-19 14:22:54 +08001400AT+ACONFIG="IMSD=1"
1401or
1402AT+ACONFIG="IMSD=0"
1403
1404OK
1405*/
1406static int req_volte_set(int state, int *cme_err)
1407{
1408 ATResponse *response = NULL;
1409 char cmd[30] = {0};
1410 if(state)
1411 {
1412 strcpy(cmd, "AT+ACONFIG=\"IMSD=1\"");
1413 }
1414 else
1415 {
1416 strcpy(cmd, "AT+ACONFIG=\"IMSD=0\"");
1417 }
1418 int err = at_send_command(cmd, &response);
1419
1420 if (err < 0 || response->success == 0) {
1421 *cme_err = at_get_cme_error(response);
1422 goto exit;
1423 }
1424
1425 err = 0;
1426exit:
1427 at_response_free(response);
1428 return err;
1429}
1430
1431/*
1432AT+ACONFIG?
1433PID=0,VID=0,IMSD=1,PIPE=0,FAST=0,RDUP=1,NOCP=0,GEFL=-1237040617
1434
1435OK
1436*/
1437static int req_volte_get(int *state, int *cme_err)
1438{
1439 ATResponse *response = NULL;
b.liu9e8584b2024-11-06 19:21:28 +08001440// char *tmp_ptr = NULL;
liubin281ac462023-07-19 14:22:54 +08001441 int err = at_send_command_singleline("AT+ACONFIG?", "", &response);
1442
1443 if (err < 0 || response->success == 0 || !response->p_intermediates){
1444 *cme_err = at_get_cme_error(response);
1445 goto exit;
1446 }
1447
1448 char *line = response->p_intermediates->line;
1449 char* ptr = strstr(line, "IMSD=");
1450 if(ptr)
1451 {
1452 *state = atoi(ptr + strlen("IMSD="));
1453 }
1454 else
1455 {
1456 err = -1;
1457 }
1458exit:
1459 at_response_free(response);
1460 return err;
1461}
1462
1463
1464/*
1465* Get system temperature.
1466*
1467* type[IN]:
1468* 0: Soc temperature.
1469* 1: RF temperature.
1470* temp[OUT]:
1471* temperature in celsius.
1472*
1473
1474AT*RFTEMP
1475*RFTEMP:0,28
1476OK
1477
1478AT*SOCTEMP
1479*SOCTEMP:24000
1480OK
1481
1482*/
r.xiao2102d762024-06-07 03:10:38 -07001483static int req_temp_get(int type, mbtk_thermal_info_t *temp, int *cme_err)
liubin281ac462023-07-19 14:22:54 +08001484{
1485 ATResponse *response = NULL;
1486 int err = -1;
1487 int tmp_int;
1488 if(type == 0) { // Soc
1489 err = at_send_command_singleline("AT*SOCTEMP", "*SOCTEMP:", &response);
1490 } else { // RF
1491 err = at_send_command_singleline("AT*RFTEMP", "*RFTEMP:", &response);
1492 }
1493
1494 if (err < 0 || response->success == 0 || !response->p_intermediates){
1495 *cme_err = at_get_cme_error(response);
1496 goto exit;
1497 }
1498
1499 char *line = response->p_intermediates->line;
1500 err = at_tok_start(&line);
1501 if (err < 0)
1502 {
1503 goto exit;
1504 }
1505 err = at_tok_nextint(&line, &tmp_int);
1506 if (err < 0)
1507 {
1508 goto exit;
1509 }
1510
1511 if(type == 1) { // RF
1512 err = at_tok_nextint(&line, &tmp_int);
1513 if (err < 0)
1514 {
1515 goto exit;
1516 }
r.xiao2102d762024-06-07 03:10:38 -07001517 temp->ther = tmp_int;
liubin281ac462023-07-19 14:22:54 +08001518 } else {
r.xiao2102d762024-06-07 03:10:38 -07001519 tmp_int = tmp_int / 1000;
1520 temp->ther = tmp_int;
1521 //LOG(" >>>temp =%d",temp->ther);
liubin281ac462023-07-19 14:22:54 +08001522 }
1523
1524exit:
1525 at_response_free(response);
1526 return err;
1527}
1528
1529/*
1530AT*BAND=15
1531OK
1532
1533*/
1534static int req_band_set(mbtk_band_info_t* band, int *cme_err)
1535{
1536 ATResponse *response = NULL;
b.liudfec1e12024-06-06 16:38:59 +08001537 char cmd[100] = {0};
liubin281ac462023-07-19 14:22:54 +08001538 int err = -1;
1539
1540 if(band->gsm_band == 0 && band->umts_band == 0
1541 && band->tdlte_band == 0 && band->fddlte_band == 0) {
1542 sprintf(cmd, "AT*BAND=%d", band->net_pref);
1543 } else {
1544 band_support_get();
1545
1546 log_hex("BAND_SUPPORT", &band_support, sizeof(mbtk_band_info_t));
1547 log_hex("BAND", band, sizeof(mbtk_band_info_t));
1548
1549 if(band->gsm_band == 0) {
1550 band->gsm_band = band_support.gsm_band;
1551 }
1552 if(band->umts_band == 0) {
1553 band->umts_band = band_support.umts_band;
1554 }
1555 if(band->tdlte_band == 0) {
1556 band->tdlte_band = band_support.tdlte_band;
1557 }
1558 if(band->fddlte_band == 0) {
1559 band->fddlte_band = band_support.fddlte_band;
1560 }
1561
1562 if((band->gsm_band & band_support.gsm_band) != band->gsm_band) {
1563 LOG("GSM band error.");
1564 goto exit;
1565 }
1566
1567 if((band->umts_band & band_support.umts_band) != band->umts_band) {
1568 LOG("UMTS band error.");
1569 goto exit;
1570 }
1571
1572 if((band->tdlte_band & band_support.tdlte_band) != band->tdlte_band) {
1573 LOG("TDLTE band error.");
1574 goto exit;
1575 }
1576
1577 if((band->fddlte_band & band_support.fddlte_band) != band->fddlte_band) {
1578 LOG("FDDLTE band error.");
1579 goto exit;
1580 }
1581
b.liu288093c2024-05-09 17:02:57 +08001582 if((band->lte_ext_band & band_support.lte_ext_band) != band->lte_ext_band) {
1583 LOG("EXT_LTE band error.");
1584 goto exit;
1585 }
1586
liubin281ac462023-07-19 14:22:54 +08001587 if(band->net_pref == 0xFF) { // No change net_pref.
1588 int tmp_int;
1589 err = at_send_command_singleline("AT*BAND?", "*BAND:", &response);
1590 if (err < 0 || response->success == 0 || !response->p_intermediates){
1591 *cme_err = at_get_cme_error(response);
1592 goto exit;
1593 }
1594
1595 char *line = response->p_intermediates->line;
1596 err = at_tok_start(&line);
1597 if (err < 0)
1598 {
1599 goto exit;
1600 }
1601
1602 err = at_tok_nextint(&line, &tmp_int);
1603 if (err < 0)
1604 {
1605 goto exit;
1606 }
1607 band->net_pref = (uint8)tmp_int; // Set to current net_pref.
1608
1609 at_response_free(response);
1610 }
1611
b.liu288093c2024-05-09 17:02:57 +08001612 if(band->lte_ext_band > 0) {
1613 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);
1614 } else {
1615 sprintf(cmd, "AT*BAND=%d,%d,%d,%d,%d", band->net_pref, band->gsm_band, band->umts_band, band->tdlte_band, band->fddlte_band);
1616 }
liubin281ac462023-07-19 14:22:54 +08001617 }
1618 err = at_send_command(cmd, &response);
1619
1620 if (err < 0 || response->success == 0){
1621 *cme_err = at_get_cme_error(response);
1622 goto exit;
1623 }
1624
1625 err = 0;
1626exit:
1627 at_response_free(response);
1628 return err;
1629}
1630
1631/*
1632// ???????
1633AT*BAND=?
1634*BAND:(0-18),79,147,482,524503
1635
1636OK
1637
1638// ???????????
1639AT*BAND?
1640*BAND: 15, 78, 147, 482, 524503, 0, 2, 2, 0, 0
1641
1642OK
1643
1644// ?????????
1645AT*BAND=5,79,147,128,1
1646OK
1647
1648net_prefferred??
1649 0 : GSM only
1650 1 : UMTS only
1651 2 : GSM/UMTS(auto)
1652 3 : GSM/UMTS(GSM preferred)
1653 4 : GSM/UMTS(UMTS preferred)
1654 5 : LTE only
1655 6 : GSM/LTE(auto)
1656 7 : GSM/LTE(GSM preferred)
1657 8 : GSM/LTE(LTE preferred)
1658 9 : UMTS/LTE(auto)
1659 10 : UMTS/LTE(UMTS preferred)
1660 11 : UMTS/LTE(LTE preferred)
1661 12 : GSM/UMTS/LTE(auto)
1662 13 : GSM/UMTS/LTE(GSM preferred)
1663 14 : GSM/UMTS/LTE(UMTS preferred)
1664 15 : GSM/UMTS/LTE(LTE preferred)
1665GSM band??
1666 1 ?C PGSM 900 (standard or primary)
1667 2 ?C DCS GSM 1800
1668 4 ?C PCS GSM 1900
1669 8 ?C EGSM 900 (extended)
1670 16 ?C GSM 450
1671 32 ?C GSM 480
1672 64 ?C GSM 850
1673 512 - BAND_LOCK_BIT // used for GSM band setting
1674UMTS band??
1675 1 ?C UMTS_BAND_1
1676 2 ?C UMTS_BAND_2
1677 4 ?C UMTS_BAND_3
1678 8 ?C UMTS_BAND_4
1679 16 ?C UMTS_BAND_5
1680 32 ?C UMTS_BAND_6
1681 64 ?C UMTS_BAND_7
1682 128 ?C UMTS_BAND_8
1683 256 ?C UMTS_BAND_9
1684LTEbandH(TDD-LTE band)
1685 32 ?C TDLTE_BAND_38
1686 64 ?C TDLTE_BAND_39
1687 128 ?C TDLTE_BAND_40
1688 256 ?C TDLTE_BAND_41
1689LTEbandL(FDD-LTE band)
1690 1 ?C FDDLTE_BAND_1
1691 4 ?C FDDLTE _BAND_3
1692 8 ?C FDDLTE _BAND_4
1693 64 ?C FDDLTE _BAND_7
1694 65536 ?C FDDLTE _BAND_17
1695 524288 ?C FDDLTE _BAND_20
1696*/
1697static int req_band_get(mbtk_band_info_t *band, int *cme_err)
1698{
1699 ATResponse *response = NULL;
1700 int tmp_int;
1701
1702 band_support_get();
1703
1704 log_hex("BAND_SUPPORT", &band_support, sizeof(mbtk_band_info_t));
1705 int err = at_send_command_singleline("AT*BAND?", "*BAND:", &response);
1706 if (err < 0 || response->success == 0 || !response->p_intermediates){
1707 *cme_err = at_get_cme_error(response);
1708 goto exit;
1709 }
1710
1711 char *line = response->p_intermediates->line;
1712 err = at_tok_start(&line);
1713 if (err < 0)
1714 {
1715 goto exit;
1716 }
1717
1718 err = at_tok_nextint(&line, &tmp_int);
1719 if (err < 0)
1720 {
1721 goto exit;
1722 }
1723 band->net_pref = (uint8)tmp_int;
1724
1725 err = at_tok_nextint(&line, &tmp_int);
1726 if (err < 0)
1727 {
1728 goto exit;
1729 }
1730 band->gsm_band = (uint16)tmp_int;
1731
1732 err = at_tok_nextint(&line, &tmp_int);
1733 if (err < 0)
1734 {
1735 goto exit;
1736 }
1737 band->umts_band = (uint16)tmp_int;
1738
1739 err = at_tok_nextint(&line, &tmp_int);
1740 if (err < 0)
1741 {
1742 goto exit;
1743 }
1744 band->tdlte_band = (uint32)tmp_int;
1745
1746 err = at_tok_nextint(&line, &tmp_int);
1747 if (err < 0)
1748 {
1749 goto exit;
1750 }
1751 band->fddlte_band = (uint32)tmp_int;
1752
b.liu288093c2024-05-09 17:02:57 +08001753 // roamingConfig
1754 err = at_tok_nextint(&line, &tmp_int);
1755 if (err < 0)
1756 {
1757 goto exit;
1758 }
1759
1760 // srvDomain
1761 err = at_tok_nextint(&line, &tmp_int);
1762 if (err < 0)
1763 {
1764 goto exit;
1765 }
1766
1767 // bandPriorityFlag
1768 err = at_tok_nextint(&line, &tmp_int);
1769 if (err < 0)
1770 {
1771 goto exit;
1772 }
1773
1774 //
1775 err = at_tok_nextint(&line, &tmp_int);
1776 if (err < 0)
1777 {
1778 goto exit;
1779 }
1780
1781 // ltebandExt
1782 err = at_tok_nextint(&line, &tmp_int);
1783 if (err < 0)
1784 {
1785 goto exit;
1786 }
1787 band->lte_ext_band = (uint32)tmp_int;
1788
liubin281ac462023-07-19 14:22:54 +08001789 log_hex("BAND", band, sizeof(mbtk_band_info_t));
1790
1791exit:
1792 at_response_free(response);
1793 return err;
1794}
1795
1796/*
1797AT+ICCID
1798+ICCID: 89860621330065648041
1799
1800OK
1801*/
1802static int req_iccid_get(void *data, int *cme_err)
1803{
1804 ATResponse *response = NULL;
1805 char *tmp_ptr = NULL;
1806 int err = at_send_command_singleline("AT+ICCID", "+ICCID:", &response);
1807
1808 if (err < 0 || response->success == 0 || !response->p_intermediates){
1809 *cme_err = at_get_cme_error(response);
1810 goto exit;
1811 }
1812
1813 char *line = response->p_intermediates->line;
1814 err = at_tok_start(&line);
1815 if (err < 0)
1816 {
1817 goto exit;
1818 }
1819
1820 err = at_tok_nextstr(&line, &tmp_ptr);
1821 if (err < 0)
1822 {
1823 goto exit;
1824 }
1825
1826 memcpy(data, tmp_ptr, strlen(tmp_ptr));
1827exit:
1828 at_response_free(response);
1829 return err;
1830}
1831
1832/*
1833AT+CNUM?
1834+CNUM: "","13980414101",129
1835
1836OK
1837
1838*/
1839static int req_phone_number_get(void *data, int *cme_err)
1840{
1841 ATResponse *response = NULL;
1842 char *tmp_ptr = NULL;
1843 int err = at_send_command_singleline("AT+CNUM?", "+CNUM:", &response);
1844 if (err < 0 || response == NULL || response->success == 0 || !response->p_intermediates){
1845 if(response) {
1846 *cme_err = at_get_cme_error(response);
1847 }
1848 LOGD("AT+CNUM? fail.");
1849 goto exit;
1850 }
1851
1852 char *line = response->p_intermediates->line;
1853 if(line == NULL) {
1854 LOGD("line is NULL");
1855 goto exit;
1856 }
1857 err = at_tok_start(&line);
1858 if (err < 0)
1859 {
1860 goto exit;
1861 }
1862
1863 err = at_tok_nextstr(&line, &tmp_ptr);
1864 if (err < 0)
1865 {
1866 goto exit;
1867 }
1868
1869 err = at_tok_nextstr(&line, &tmp_ptr);
1870 if (err < 0)
1871 {
1872 goto exit;
1873 }
1874
1875 memcpy(data, tmp_ptr, strlen(tmp_ptr));
1876exit:
1877 at_response_free(response);
1878 return err;
1879}
1880
1881
1882/*
1883AT+CIMI
1884460068103383304
1885
1886OK
1887
1888*/
1889static int req_imsi_get(void *data, int *cme_err)
1890{
1891 ATResponse *response = NULL;
1892 int err = at_send_command_numeric("AT+CIMI", &response);
1893
1894 if (err < 0 || response->success == 0 || !response->p_intermediates){
1895 *cme_err = at_get_cme_error(response);
1896 goto exit;
1897 }
1898
1899 memcpy(data, response->p_intermediates->line, strlen(response->p_intermediates->line));
1900exit:
1901 at_response_free(response);
1902 return err;
1903}
1904
1905
1906/*
1907AT+CLCK=SC,1/0,1234
1908+CLCK:1/0
1909
1910OK
1911
1912*/
1913static int req_pin_enable(mbtk_enable_pin_info *data, int *cme_err)
1914{
1915 ATResponse *response = NULL;
1916 char cmd[64]={0};
1917 sprintf(cmd, "AT+CLCK=SC,%d,%s", data->enable, data->pin_value);
1918
1919 int err = at_send_command_singleline(cmd, "+CLCK:", &response);
1920 if (err < 0 || response == NULL || response->success == 0 || !response->p_intermediates){
1921 if(response) {
1922 *cme_err = at_get_cme_error(response);
1923 }
1924 LOGD("AT+CLCK? fail.");
1925 goto exit;
1926 }
1927
1928 char *line = response->p_intermediates->line;
1929 if(line == NULL) {
1930 LOGD("line is NULL");
1931 goto exit;
1932 }
1933 err = at_tok_start(&line);
1934 if (err < 0)
1935 {
1936 goto exit;
1937 }
1938 int clck;
1939 err = at_tok_nextint(&line, &clck);
1940 if (err < 0)
1941 {
1942 goto exit;
1943 }
1944
1945exit:
1946 at_response_free(response);
1947 return err;
1948}
1949
1950/*
yq.wang586a0df2024-10-24 20:10:37 -07001951AT+CLCK=SC,2
1952+CLCK: 1
1953
1954OK
1955*/
b.liu9e8584b2024-11-06 19:21:28 +08001956static int req_get_pin_state(mbtk_pin_state_enum *state, int *cme_err)
yq.wang586a0df2024-10-24 20:10:37 -07001957{
1958 ATResponse *response = NULL;
b.liu9e8584b2024-11-06 19:21:28 +08001959// char cmd[64]={0};
b.liu06559f62024-11-01 18:48:22 +08001960
yq.wang586a0df2024-10-24 20:10:37 -07001961 int err = at_send_command_singleline("AT+CLCK=SC,2", "+CLCK:", &response);
1962 if (err < 0 || response == NULL || response->success == 0 || !response->p_intermediates)
1963 {
1964 if(response)
1965 {
1966 *cme_err = at_get_cme_error(response);
1967 }
1968 LOGE("[req_get_pin_state] AT+CLCK fail.");
1969 goto exit;
1970 }
1971
1972 char *line = response->p_intermediates->line;
1973 if(line == NULL)
1974 {
1975 LOGE("[req_get_pin_state] line is NULL");
1976 goto exit;
1977 }
1978 err = at_tok_start(&line);
1979 if (err < 0)
1980 {
1981 LOGE("[req_get_pin_state] at_tok_start fail.[%d]", err);
1982 goto exit;
1983 }
b.liu06559f62024-11-01 18:48:22 +08001984
yq.wang586a0df2024-10-24 20:10:37 -07001985 int clck;
1986 err = at_tok_nextint(&line, &clck);
1987 if (err < 0)
1988 {
1989 LOGE("[req_get_pin_state] at_tok_nextint fail.[%d]", err);
1990 goto exit;
1991 }
1992
1993 if(clck == 1)
1994 {
b.liu9e8584b2024-11-06 19:21:28 +08001995 *state = MBTK_PIN_ENABLE;
yq.wang586a0df2024-10-24 20:10:37 -07001996 }
1997 else
1998 {
b.liu9e8584b2024-11-06 19:21:28 +08001999 *state = MBTK_PIN_DISABLE;
yq.wang586a0df2024-10-24 20:10:37 -07002000 }
2001exit:
2002 at_response_free(response);
2003 response = NULL;
2004 return err;
2005}
2006
2007
2008/*
liubin281ac462023-07-19 14:22:54 +08002009AT+CPIN=1234
2010
2011OK
2012
2013*/
2014static int req_pin_verify(char *data, int *cme_err)
2015{
2016 ATResponse *response = NULL;
2017 char cmd[64]={0};
2018 sprintf(cmd, "AT+CPIN=%s", data);
2019 int err = at_send_command(cmd, &response);
2020 if (err < 0 || response->success == 0){
2021 if(cme_err) {
2022 *cme_err = at_get_cme_error(response);
2023 }
2024 goto exit;
2025 }
2026
2027exit:
2028 at_response_free(response);
2029 return err;
2030}
2031
2032/*
2033AT+CLCK=SC,2
2034+CLCK: 1
2035
2036OK
2037
2038AT+CLCK="SC",1,"1234"
2039+CLCK:1
2040
2041OK
2042
2043AT+CPWD="SC","1234","4321"
2044
2045OK
2046
2047*/
2048static int req_pin_change(mbtk_change_pin_info *data, int *cme_err)
2049{
2050 ATResponse *response = NULL;
2051 char cmd[64]={0};
2052
2053 int err = at_send_command_singleline("AT+CLCK=SC,2", "+CLCK:", &response);
2054 if (err < 0 || response == NULL || response->success == 0 || !response->p_intermediates){
2055 if(response) {
2056 *cme_err = at_get_cme_error(response);
2057 }
2058 LOGD("AT+CLCK fail.");
2059 goto exit;
2060 }
2061
2062 char *line = response->p_intermediates->line;
2063 if(line == NULL) {
2064 LOGD("line is NULL");
2065 goto exit;
2066 }
2067 err = at_tok_start(&line);
2068 if (err < 0)
2069 {
2070 goto exit;
2071 }
2072 int clck;
2073 err = at_tok_nextint(&line, &clck);
2074 if (err < 0)
2075 {
2076 goto exit;
2077 }
2078 at_response_free(response);
2079
2080 if(clck==0)
2081 {
2082 sprintf(cmd, "AT+CLCK=SC,1,%s", data->old_pin_value);
2083 err = at_send_command_singleline(cmd, "+CLCK:", &response);
2084 if (err < 0 || response == NULL || response->success == 0 || !response->p_intermediates){
2085 if(response) {
2086 *cme_err = at_get_cme_error(response);
2087 }
2088 LOGD("AT+CLCK fail.");
2089 goto exit;
2090 }
2091 line = response->p_intermediates->line;
2092 if(line == NULL) {
2093 LOGD("line is NULL");
2094 goto exit;
2095 }
2096 err = at_tok_start(&line);
2097 if (err < 0)
2098 {
2099 goto exit;
2100 }
2101 clck = -1;
2102 err = at_tok_nextint(&line, &clck);
2103 if (err < 0)
2104 {
2105 goto exit;
2106 }
2107 at_response_free(response);
2108 if(clck != 1)
2109 return err;
2110 }
2111 memset(cmd, 0, 64);
2112 sprintf(cmd, "AT+CPWD=SC,%s,%s", data->old_pin_value,data->new_pin_value);
2113 err = at_send_command(cmd, &response);
2114 if (err < 0 || response->success == 0){
2115 if(cme_err) {
2116 *cme_err = at_get_cme_error(response);
2117 }
2118 goto exit;
2119 }
2120
2121exit:
2122 at_response_free(response);
2123 return err;
2124}
2125
2126/*
2127AT+CPIN?
2128+CPIN:SIM PUK
2129
2130OK
2131
2132AT+CPIN="PUK","PIN"
2133+CPIN: READY
2134
2135OK
2136*/
2137static int req_puk_unlock_pin(mbtk_unlock_pin_info *data, int *cme_err)
2138{
2139 ATResponse *response = NULL;
2140 char cmd[64]={0};
2141#if 0
2142 int err = at_send_command_singleline("AT+CPIN?", "+CPIN:", &response);
2143 if (err < 0 || response == NULL || response->success == 0 || !response->p_intermediates){
2144 if(response) {
2145 *cme_err = at_get_cme_error(response);
2146 }
2147 LOGD("AT+CNUM? fail.");
2148 goto exit;
2149 }
2150
2151 char *line = response->p_intermediates->line;
2152 if(line == NULL) {
2153 LOGD("line is NULL");
2154 goto exit;
2155 }
2156 err = at_tok_start(&line);
2157 if (err < 0)
2158 {
2159 goto exit;
2160 }
2161 char *tmp_ptr = NULL;
2162 err = at_tok_nextstr(&line, &tmp_ptr);
2163 if (err < 0)
2164 {
2165 goto exit;
2166 }
2167 at_response_free(response);
2168
2169 if(!strstr(tmp_ptr,"SIM PUK"))
2170 {
2171 sprintf(cmd, "AT+CPIN=%s,%s", data->puk_value, data->pin_value);
2172 err = at_send_command_singleline(cmd, "+CPIN:", &response);
2173 if (err < 0 || response == NULL || response->success == 0 || !response->p_intermediates){
2174 if(response) {
2175 *cme_err = at_get_cme_error(response);
2176 }
2177 LOGD("AT+CNUM? fail.");
2178 goto exit;
2179 }
2180 line = response->p_intermediates->line;
2181 if(line == NULL) {
2182 LOGD("line is NULL");
2183 goto exit;
2184 }
2185 err = at_tok_start(&line);
2186 if (err < 0)
2187 {
2188 goto exit;
2189 }
2190 memset(tmp_ptr, 0, strlen(tmp_ptr));
2191 err = at_tok_nextstr(&line, &tmp_ptr);
2192 if (err < 0)
2193 {
2194 goto exit;
2195 }
2196 at_response_free(response);
2197 if(strstr(tmp_ptr, "READY"))
2198 return err;
2199 }
2200 else
2201 return err;
2202#else
2203 sprintf(cmd, "AT+CPIN=%s,%s", data->puk_value, data->pin_value);
r.xiao2ad43d12024-03-23 00:03:29 -07002204 int err = at_send_command(cmd, &response);
2205 if (err < 0 || response->success == 0){
2206 if(cme_err) {
liubin281ac462023-07-19 14:22:54 +08002207 *cme_err = at_get_cme_error(response);
2208 }
liubin281ac462023-07-19 14:22:54 +08002209 goto exit;
2210 }
r.xiao2ad43d12024-03-23 00:03:29 -07002211
liubin281ac462023-07-19 14:22:54 +08002212#endif
2213exit:
2214 at_response_free(response);
2215 return err;
2216}
2217
2218/*
2219AT+COPS=?
2220
2221+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)
2222
2223OK
2224
2225// Return [sel_mode(uint8)type(uint8)plmn(uint32)...sel_mode(uint8)type(uint8)plmn(uint32)]
2226*/
2227#if 0
2228static int req_available_net_get(mbtk_net_array_info_t *data_ptr)
2229{
2230 ATResponse *response = NULL;
2231 char *tmp_ptr = NULL;
2232 int tmp_int;
2233 int err = at_send_command_singleline("AT+COPS=?", "+COPS:", &response);
2234
2235 if (err < 0 || response->success == 0 || !response->p_intermediates)
2236 goto exit;
2237#if 1
2238 char *line_ptr = response->p_intermediates->line;
2239 if(line_ptr == NULL) {
2240 LOG("line is NULL");
2241 goto exit;
2242 }
2243 //LOG("Line:%s",line_ptr);
2244 line_ptr = strstr(line_ptr, "(");
2245 while(line_ptr) {
2246 line_ptr++;
2247 // Only for available/current net.
2248 if(*line_ptr == '1' || *line_ptr == '2') {
2249 //LOG("Temp:%s",line_ptr);
2250 //sleep(1);
2251 line_ptr = strstr(line_ptr, ",");
2252 if(line_ptr == NULL)
2253 goto exit;
2254 line_ptr++;
2255
2256 line_ptr = strstr(line_ptr, ",");
2257 if(line_ptr == NULL)
2258 goto exit;
2259 line_ptr++;
2260
2261 line_ptr = strstr(line_ptr, ",");
2262 if(line_ptr == NULL)
2263 goto exit;
2264
2265 while(*line_ptr != '\0' && (*line_ptr == ',' || *line_ptr == ' ' || *line_ptr == '"'))
2266 line_ptr++;
2267
2268 mbtk_net_info_t *net = (mbtk_net_info_t*)malloc(sizeof(mbtk_net_info_t));
2269 if(net == NULL) {
2270 LOG("malloc() fail.");
2271 goto exit;
2272 }
2273 memset(net, 0, sizeof(mbtk_net_info_t));
2274
2275 // Point to "46000"
2276 //LOG("PLMN:%s",line_ptr);
2277 //sleep(1);
2278 net->plmn = (uint32)atoi(line_ptr);
2279
2280 line_ptr = strstr(line_ptr, ",");
2281 if(line_ptr == NULL)
2282 goto exit;
2283
2284 while(*line_ptr != '\0' && (*line_ptr == ',' || *line_ptr == ' '))
2285 line_ptr++;
2286
2287 // Point to "7"
2288 if(*line_ptr == '\0') {
2289 free(net);
2290 goto exit;
2291 }
2292 //LOG("Type:%s",line_ptr);
2293 //sleep(1);
2294 net->net_type = (uint8)atoi(line_ptr);
2295 list_add(data_ptr->net_list, net);
2296 data_ptr->count++;
2297 }
2298
2299 line_ptr = strstr(line_ptr, "(");
2300 }
2301#endif
2302exit:
2303 at_response_free(response);
2304 return err;
2305}
2306#else
2307static int req_available_net_get(void* buff, int *cme_err)
2308{
2309 ATResponse *response = NULL;
b.liu9e8584b2024-11-06 19:21:28 +08002310// char *tmp_ptr = NULL;
2311// int tmp_int;
liubin281ac462023-07-19 14:22:54 +08002312 int buff_size = 0;
2313 int err = at_send_command_singleline("AT+COPS=?", "+COPS:", &response);
2314
2315 if (err < 0 || response->success == 0 || !response->p_intermediates){
2316 *cme_err = at_get_cme_error(response);
2317 goto exit;
2318 }
2319 char *line_ptr = response->p_intermediates->line;
2320 if(line_ptr == NULL) {
2321 LOG("line is NULL");
2322 goto exit;
2323 }
2324 uint8* buff_ptr = (uint8*)buff;
2325 //LOG("Line:%s",line_ptr);
2326 line_ptr = strstr(line_ptr, "(");
2327 while(line_ptr) {
2328 line_ptr++;
2329 // Only for available/current net.
2330 if(*line_ptr == '1' || *line_ptr == '2' || *line_ptr == '3') {
2331 *(buff_ptr + 2) = (uint8)atoi(line_ptr); // net_state
2332
2333 line_ptr = strstr(line_ptr, ",");
2334 if(line_ptr == NULL)
2335 goto exit;
2336 line_ptr++;
2337
2338 line_ptr = strstr(line_ptr, ",");
2339 if(line_ptr == NULL)
2340 goto exit;
2341 line_ptr++;
2342
2343 line_ptr = strstr(line_ptr, ",");
2344 if(line_ptr == NULL)
2345 goto exit;
2346
2347 while(*line_ptr != '\0' && (*line_ptr == ',' || *line_ptr == ' ' || *line_ptr == '"'))
2348 line_ptr++;
2349
2350 // set sel_mode to 0
2351 *buff_ptr = (uint8)0;
2352 // Point to "46000"
2353 //LOG("PLMN:%s",line_ptr);
2354 //sleep(1);
2355 uint32_2_byte((uint32)atoi(line_ptr), buff_ptr + 3, false); // plmn
2356
2357 line_ptr = strstr(line_ptr, ",");
2358 if(line_ptr == NULL)
2359 goto exit;
2360
2361 while(*line_ptr != '\0' && (*line_ptr == ',' || *line_ptr == ' '))
2362 line_ptr++;
2363
2364 // Point to "7"
2365 if(*line_ptr == '\0') {
2366 goto exit;
2367 }
2368 //LOG("Type:%s",line_ptr);
2369 //sleep(1);
2370 *(buff_ptr + 1) = (uint8)atoi(line_ptr); // net_type
2371
2372 buff_size += sizeof(mbtk_net_info_t);
2373 buff_ptr += sizeof(mbtk_net_info_t);
2374 }
2375
2376 line_ptr = strstr(line_ptr, "(");
2377 }
2378exit:
2379 at_response_free(response);
2380 return buff_size;
2381}
2382#endif
2383
2384/*
2385AT+COPS?
2386+COPS: 1
2387
2388OK
2389
2390or
2391
2392AT+COPS?
2393+COPS: 0,2,"46001",7
2394
2395OK
2396
2397*/
2398static int req_net_sel_mode_get(mbtk_net_info_t *net, int *cme_err)
2399{
liubin281ac462023-07-19 14:22:54 +08002400 ATResponse *response = NULL;
2401 int tmp_int;
yq.wang68df93c2024-11-13 02:33:45 -08002402 mbtk_net_opera_format_enum format;
liubin281ac462023-07-19 14:22:54 +08002403 char *tmp_ptr = NULL;
yq.wang68df93c2024-11-13 02:33:45 -08002404
2405 int err = at_send_command("AT+COPS=3,2", &response);
2406 if (err < 0 || response->success == 0)
2407 {
liubin281ac462023-07-19 14:22:54 +08002408 if(cme_err != NULL)
yq.wang68df93c2024-11-13 02:33:45 -08002409 {
liubin281ac462023-07-19 14:22:54 +08002410 *cme_err = at_get_cme_error(response);
yq.wang68df93c2024-11-13 02:33:45 -08002411 }
2412 LOGE("[%s] at_send_command err[%d], cme_err[%d], response->success[%d].", __func__, err, *cme_err, response->success);
liubin281ac462023-07-19 14:22:54 +08002413 goto exit;
2414 }
yq.wang68df93c2024-11-13 02:33:45 -08002415 else
2416 {
2417 if(response)
2418 {
2419 at_response_free(response);
2420 response = NULL;
2421 }
2422 }
2423
2424 err = at_send_command_singleline("AT+COPS?", "+COPS:", &response);
2425 if (err < 0 || response->success == 0 || !response->p_intermediates)
2426 {
2427 if(cme_err != NULL)
2428 {
2429 *cme_err = at_get_cme_error(response);
2430 }
2431 LOGE("[%s] at_send_command_singleline err[%d], cme_err[%d], response->success[%d].", __func__, err, *cme_err, response->success);
2432 goto exit;
2433 }
2434
liubin281ac462023-07-19 14:22:54 +08002435 char *line = response->p_intermediates->line;
yq.wang68df93c2024-11-13 02:33:45 -08002436 if(line == NULL)
2437 {
2438 LOGE("[%s] line is NULL.", __func__);
liubin281ac462023-07-19 14:22:54 +08002439 goto exit;
2440 }
yq.wang68df93c2024-11-13 02:33:45 -08002441
liubin281ac462023-07-19 14:22:54 +08002442 err = at_tok_start(&line);
2443 if (err < 0)
2444 {
yq.wang68df93c2024-11-13 02:33:45 -08002445 LOGE("[%s] at_tok_start fail.[%d]", __func__, err);
liubin281ac462023-07-19 14:22:54 +08002446 goto exit;
2447 }
yq.wang68df93c2024-11-13 02:33:45 -08002448
liubin281ac462023-07-19 14:22:54 +08002449 err = at_tok_nextint(&line, &tmp_int);
2450 if (err < 0)
2451 {
yq.wang68df93c2024-11-13 02:33:45 -08002452 LOGE("[%s] net_sel_mode fail.[%d]", __func__, err);
liubin281ac462023-07-19 14:22:54 +08002453 goto exit;
2454 }
2455 net->net_sel_mode = (uint8)tmp_int;
yq.wang68df93c2024-11-13 02:33:45 -08002456
2457 if(!at_tok_hasmore(&line))
2458 {
2459 LOGE("[%s] no more data.[%d]", __func__);
liubin281ac462023-07-19 14:22:54 +08002460 goto exit;
2461 }
yq.wang68df93c2024-11-13 02:33:45 -08002462
liubin281ac462023-07-19 14:22:54 +08002463 err = at_tok_nextint(&line, &tmp_int);
2464 if (err < 0)
2465 {
yq.wang68df93c2024-11-13 02:33:45 -08002466 LOGE("[%s] format fail.[%d]", __func__, err);
liubin281ac462023-07-19 14:22:54 +08002467 goto exit;
2468 }
yq.wang68df93c2024-11-13 02:33:45 -08002469 format = (mbtk_net_opera_format_enum)tmp_int;
2470
liubin281ac462023-07-19 14:22:54 +08002471 err = at_tok_nextstr(&line, &tmp_ptr);
2472 if (err < 0)
2473 {
yq.wang68df93c2024-11-13 02:33:45 -08002474 LOGE("[%s] plmn fail.[%d]", __func__, err);
liubin281ac462023-07-19 14:22:54 +08002475 goto exit;
2476 }
yq.wang68df93c2024-11-13 02:33:45 -08002477 if(format == MBTK_NET_OPERA_FORMAT_NUMBER)
2478 {
2479 net->plmn = (uint32)atoi(tmp_ptr);
2480 }
2481 else
2482 {
2483 LOGE("[%s] plmn format error.", __func__);
2484 goto exit;
2485 }
2486
liubin281ac462023-07-19 14:22:54 +08002487 err = at_tok_nextint(&line, &tmp_int);
2488 if (err < 0)
2489 {
yq.wang68df93c2024-11-13 02:33:45 -08002490 LOGE("[%s] net reg type fail.[%d]", __func__, err);
liubin281ac462023-07-19 14:22:54 +08002491 goto exit;
2492 }
2493 net->net_type = (uint8)tmp_int;
2494
2495 net->net_state = (uint8)MBTK_NET_AVIL_STATE_CURRENT;
liubin281ac462023-07-19 14:22:54 +08002496exit:
yq.wang68df93c2024-11-13 02:33:45 -08002497 if(response)
2498 {
2499 at_response_free(response);
2500 response = NULL;
2501 }
liubin281ac462023-07-19 14:22:54 +08002502 return err;
2503}
2504
2505/*
2506AT+COPS=0
2507or
2508AT+COPS=1,2,"46000",7
2509
2510OK
2511
2512*/
2513static int req_net_sel_mode_set(mbtk_net_info_t* net, int *cme_err)
2514{
2515 ATResponse *response = NULL;
2516 char cmd[50] = {0};
2517 char* cmp_ptr = cmd;
2518 if(net == NULL) {
2519 cmp_ptr += sprintf(cmp_ptr, "AT+COPS=0");
2520 } else {
2521 if(net->net_sel_mode == 0) {
2522 cmp_ptr += sprintf(cmp_ptr, "AT+COPS=0");
2523 } else if(net->net_type == 0xFF) {
2524 cmp_ptr += sprintf(cmp_ptr, "AT+COPS=1,2,\"%d\"",net->plmn);
2525 } else {
2526 cmp_ptr += sprintf(cmp_ptr, "AT+COPS=1,2,\"%d\",%d",net->plmn, net->net_type);
2527 }
2528 }
2529
2530 int err = at_send_command(cmd, &response);
2531
2532 if (err < 0 || response->success == 0) {
2533 *cme_err = at_get_cme_error(response);
2534 goto exit;
2535 }
2536
2537exit:
2538 at_response_free(response);
2539 return err;
2540}
2541
2542/*
2543AT+EEMOPT=1
2544OK
2545
2546// LTE
2547AT+EEMGINFO?
2548// <mcc>, <length of mnc>, <mnc>, <tac>, <PCI>, <dlEuarfcn>, < ulEuarfcn >, <band>, <dlBandwidth>,
2549// <rsrp>,<rsrq>, <sinr>,
2550// errcModeState,emmState,serviceState,IsSingleEmmRejectCause,EMMRejectCause,mmeGroupId,mmeCode,mTmsi,
2551// cellId,subFrameAssignType,specialSubframePatterns,transMode
2552// mainRsrp,diversityRsrp,mainRsrq,diversityRsrq,rssi,cqi,pathLoss,tb0DlTpt,tb1DlTpt,tb0DlPeakTpt,tb1DlPeakTpt,tb0UlPeakTpt,
2553// tb1UlPeakTpt,dlThroughPut,dlPeakThroughPut,averDlPRB,averCQITb0,averCQITb1,rankIndex,grantTotal,ulThroughPut,ulPeakThroughPut,currPuschTxPower,averUlPRB,
2554// dlBer, ulBer,
2555// diversitySinr, diversityRssi
2556+EEMLTESVC: 1120, 2, 0, 33584, 430, 40936, 40936, 41, 20,
25570, 0, 0,
25581, 10, 0, 1, 0, 1059, 78, 3959566565,
2559105149248, 2, 7, 7,
25600, 0, 0, 0, 0, 0, 0, 1190919, 0, 0, 0, 16779777,
25610, 5112867, 3959566565, 2, 0, 0, 0, 0, 0, 0, 0, 0,
25620, 0,
25637, 44
2564
2565// index,phyCellId,euArfcn,rsrp,rsrq
2566+EEMLTEINTER: 0, 65535, 38950, 0, 0
2567
2568+EEMLTEINTER: 1, 0, 0, 0, 0
2569
2570+EEMLTEINTER: 2, 0, 4294967295, 255, 255
2571
2572+EEMLTEINTER: 3, 65535, 1300, 0, 0
2573
2574+EEMLTEINTER: 4, 0, 0, 0, 0
2575
2576+EEMLTEINTER: 5, 0, 4294967295, 247, 0
2577
2578+EEMLTEINTER: 6, 197, 41332, 24, 9
2579
2580+EEMLTEINTER: 7, 0, 0, 0, 0
2581
2582+EEMLTEINTER: 8, 0, 0, 0, 0
2583
2584+EEMLTEINTRA: 0, 429, 40936, 56, 12
2585
2586+EEMLTEINTERRAT: 0,0
2587
2588+EEMLTEINTERRAT: 1,0
2589
2590+EEMGINFO: 3, 2 // <state>:
2591 // 0: ME in Idle mode
2592 // 1: ME in Dedicated mode
2593 // 2: ME in PS PTM mode
2594 // 3: invalid state
2595 // <nw_type>:
2596 // 0: GSM 1: UMTS 2: LTE
2597
2598OK
2599
2600// WCDMA
2601AT+EEMGINFO?
2602// Mode, sCMeasPresent, sCParamPresent, ueOpStatusPresent,
2603
2604// if sCMeasPresent == 1
2605// cpichRSCP, utraRssi, cpichEcN0, sQual, sRxLev, txPower,
2606// endif
2607
2608// if sCParamPresent == 1
2609// rac, nom, mcc, mnc_len, mnc, lac, ci,
2610// uraId, psc, arfcn, t3212, t3312, hcsUsed, attDetAllowed,
2611// csDrxCycleLen, psDrxCycleLen, utranDrxCycleLen, HSDPASupport, HSUPASupport,
2612// endif
2613
2614// if ueOpStatusPresent == 1
2615// rrcState, numLinks, srncId, sRnti,
2616// algPresent, cipherAlg, cipherOn, algPresent, cipherAlg, cipherOn,
2617// HSDPAActive, HSUPAActive, MccLastRegisteredNetwork, MncLastRegisteredNetwork, TMSI, PTMSI, IsSingleMmRejectCause, IsSingleGmmRejectCause,
2618// MMRejectCause, GMMRejectCause, mmState, gmmState, gprsReadyState, readyTimerValueInSecs, NumActivePDPContext, ULThroughput, DLThroughput,
2619// serviceStatus, pmmState, LAU_status, LAU_count, RAU_status, RAU_count
2620// endif
2621//
2622+EEMUMTSSVC: 3, 1, 1, 1,
2623-80, 27, -6, -18, -115, -32768,
26241, 1, 1120, 2, 1, 61697, 168432821,
262515, 24, 10763, 0, 0, 0, 0,
2626128, 128, 65535, 0, 0,
26272, 255, 65535, 4294967295,
26280, 0, 0, 0, 0, 0,
26290, 0, 0, 0, 0, 0, 1, 1,
263028672, 28672, 0, 0, 0, 0, 0, 0, 0,
26310, 0, 0, 0, 0, 0
2632
2633// index, cpichRSCP, utraRssi, cpichEcN0, sQual, sRxLev ,mcc, mnc, lac, ci, arfcn, psc
2634+EEMUMTSINTRA: 0, -32768, -1, -32768, -18, -115, 0, 0, 65534, 1, 10763, 32
2635
2636+EEMUMTSINTRA: 1, -1, -32768, -18, -115, 0, 0, 65534, 2, 10763, 40, 32768
2637
2638+EEMUMTSINTRA: 2, -32768, -18, -115, 0, 0, 65534, 3, 10763, 278, 32768, 65535
2639
2640+EEMUMTSINTRA: 3, -18, -115, 0, 0, -2, 4, 10763, 28, 32768, 65535, 32768
2641
2642+EEMUMTSINTRA: 4, -115, 0, 0, -2, 5, 10763, 270, 32768, 65535, 32768, 65518
2643
2644+EEMUMTSINTRA: 5, 0, 0, -2, 6, 10763, 286, 32768, 65535, 32768, 65518, 65421
2645
2646+EEMUMTSINTRA: 6, 0, -2, 7, 10763, 80, 32768, 65535, 32768, 65518, 65421, 0
2647
2648+EEMUMTSINTRA: 7, -2, 8, 10763, 206, -32768, 65535, 32768, 65518, 65421, 0, 0
2649
2650+EEMUMTSINTRA: 8, 9, 10763, 11, -32768, -1, 32768, 65518, 65421, 0, 0, 65534
2651
2652+EEMUMTSINTRA: 9, 10763, 19, -32768, -1, -32768, 65518, 65421, 0, 0, 65534, 11
2653
2654+EEMUMTSINTRA: 10, 232, -32768, -1, -32768, -18, 65421, 0, 0, 65534, 12, 10763
2655
2656+EEMUMTSINTRA: 11, -32768, -1, -32768, -18, -115, 0, 0, 65534, 13, 10763, 66
2657
2658+EEMUMTSINTRA: 12, -1, -32768, -18, -115, 0, 0, 65534, 14, 10763, 216, 32768
2659
2660+EEMUMTSINTRA: 13, -32768, -18, -115, 0, 0, 65534, 15, 10763, 183, 32768, 65535
2661
2662+EEMUMTSINTRA: 14, -18, -115, 0, 0, -2, 16, 10763, 165, 32768, 65535, 32768
2663
2664+EEMUMTSINTRA: 15, -115, 0, 0, -2, 17, 10763, 151, 32768, 65535, 32768, 65518
2665
2666+EEMUMTSINTRA: 16, 0, 0, -2, 18, 10763, 43, 32768, 65535, 32768, 65518, 65421
2667
2668+EEMUMTSINTRA: 17, 0, -2, 19, 10763, 72, 32768, 65535, 32768, 65518, 65421, 0
2669
2670+EEMUMTSINTRA: 18, -2, 20, 10763, 157, -32768, 65535, 32768, 65518, 65421, 0, 0
2671
2672+EEMUMTSINTRA: 19, 21, 10763, 165, -32768, -1, 32768, 65518, 65421, 0, 0, 65534
2673
2674+EEMUMTSINTRA: 20, 10763, 301, -32768, -1, -32768, 65518, 65421, 0, 0, 65534, 23
2675
2676+EEMUMTSINTRA: 21, 23, -32768, -1, -32768, -18, 65421, 0, 0, 65534, 24, 10763
2677
2678+EEMUMTSINTRA: 22, -32768, -1, -32768, -18, -115, 0, 0, 65534, 25, 10763, 0
2679
2680+EEMUMTSINTRA: 23, -1, -32768, -18, -115, 0, 0, 65534, 26, 10763, 167, 32768
2681
2682+EEMUMTSINTRA: 24, -32768, -18, -115, 0, 0, 65534, 27, 10763, 34, 32768, 65535
2683
2684+EEMUMTSINTRA: 25, -18, -115, 0, 0, -2, 28, 10763, 313, 32768, 65535, 32768
2685
2686+EEMUMTSINTRA: 26, -115, 0, 0, -2, 29, 10763, 152, 32768, 65535, 32768, 65518
2687
2688+EEMUMTSINTRA: 27, 0, 0, -2, 30, 10763, 239, 0, 0, 0, 0, 0
2689
2690+EEMUMTSINTRA: 28, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2691
2692+EEMUMTSINTRA: 29, 0, 0, 0, 0, -115, 0, 0, 65534, 30, 10763, 239
2693
2694// index,gsmRssi,rxLev,C1,C2,mcc,mnc,lac,ci,arfcn,bsic
2695+EEMUMTSINTERRAT: 0, -32768, -107, -1, -1, 0, 0, 65534, 0, 117, 36
2696
2697+EEMUMTSINTERRAT: 1, -107, -1, -1, 0, 0, 65534, 1, 72, 49, 0
2698
2699+EEMUMTSINTERRAT: 2, -1, -1, 0, 0, 65534, 2, 119, 15, 32768, 149
2700
2701+EEMUMTSINTERRAT: 3, -1, 0, 0, -2, 3, 121, 23, 0, 0, 0
2702
2703+EEMGINFO: 3, 1
2704
2705OK
2706
2707
2708// GSM
2709AT+EEMGINFO?
2710+EEMGINFOBASIC: 2
2711
2712// mcc, mnc_len, mnc, lac, ci, nom, nco,
2713// bsic, C1, C2, TA, TxPwr,
2714// RxSig, RxSigFull, RxSigSub, RxQualFull, RxQualSub,
2715// ARFCB_tch, hopping_chnl, chnl_type, TS, PacketIdle, rac, arfcn,
2716// bs_pa_mfrms, C31, C32, t3212, t3312, pbcch_support, EDGE_support,
2717// ncc_permitted, rl_timeout, ho_count, ho_succ, chnl_access_count, chnl_access_succ_count,
2718// gsmBand,channelMode
2719+EEMGINFOSVC: 1120, 2, 0, 32784, 24741, 2, 0,
272063, 36, 146, 1, 7,
272146, 42, 42, 7, 0,
272253, 0, 8, 0, 1, 6, 53,
27232, 0, 146, 42, 54, 0, 1,
27241, 32, 0, 0, 0, 0,
27250, 0
2726
2727// PS_attached, attach_type, service_type, tx_power, c_value,
2728// ul_ts, dl_ts, ul_cs, dl_cs, ul_modulation, dl_modulation,
2729// gmsk_cv_bep, 8psk_cv_bep, gmsk_mean_bep, 8psk_mean_bep, EDGE_bep_period, single_gmm_rej_cause
2730// pdp_active_num, mac_mode, network_control, network_mode, EDGE_slq_measurement_mode, edge_status
2731+EEMGINFOPS: 1, 255, 0, 0, 0,
27320, 0, 268435501, 1, 0, 0,
27334, 0, 96, 0, 0, 0,
27340, 0, 0, 65535, 0, 13350
2735
2736+EEMGINFO: 0, 0
2737
2738OK
2739
2740*/
2741static int req_cell_info_get(int *cme_err)
2742{
2743 ATResponse *response = NULL;
2744 int tmp_int;
2745 int buff_size = 0;
2746 // AT+EEMOPT=1 in the first.
2747 int err = at_send_command("AT+EEMOPT=1", &response);
2748 if (err < 0 || response->success == 0){
2749 *cme_err = at_get_cme_error(response);
2750 goto exit;
2751 }
2752
2753 // Reset buffer in the first.
2754 memset(&cell_info, 0xFF, sizeof(mbtK_cell_pack_info_t));
2755 cell_info.running = true;
2756 cell_info.cell_num = 0;
2757
2758 err = at_send_command_singleline("AT+EEMGINFO?", "+EEMGINFO:", &response);
2759 if (err < 0 || response->success == 0 || !response->p_intermediates){
2760 *cme_err = at_get_cme_error(response);
2761 goto exit;
2762 }
2763
2764 // Now, cell infomation has get from URC message.
2765
2766 char *line = response->p_intermediates->line;
2767 err = at_tok_start(&line);
2768 if (err < 0)
2769 {
2770 goto exit;
2771 }
2772 err = at_tok_nextint(&line, &tmp_int);
2773 if (err < 0)
2774 {
2775 goto exit;
2776 }
2777 err = at_tok_nextint(&line, &tmp_int);
2778 if (err < 0)
2779 {
2780 goto exit;
2781 }
2782
2783 cell_info.type = (uint8)tmp_int;
2784 cell_info.running = false;
2785
2786#if 0
2787 while(lines_ptr)
2788 {
2789 // LTE
2790 if(strStartsWith(line, "+EEMLTESVC:")) // LTE Server Cell
2791 {
2792
2793 }
b.liue0ab2442024-02-06 18:53:28 +08002794 else if(strStartsWith(line, "+EEMLTEINTER:")) // LTE
liubin281ac462023-07-19 14:22:54 +08002795 {
2796
2797 }
b.liue0ab2442024-02-06 18:53:28 +08002798 else if(strStartsWith(line, "+EEMLTEINTRA:")) // LTE
liubin281ac462023-07-19 14:22:54 +08002799 {
2800
2801 }
b.liue0ab2442024-02-06 18:53:28 +08002802 else if(strStartsWith(line, "+EEMLTEINTERRAT:")) // LTE
liubin281ac462023-07-19 14:22:54 +08002803 {
2804
2805 }
2806 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
2807 // <nw_type>: 0: GSM 1: UMTS 2: LTE
2808 {
2809
2810 }
2811 // WCDMA
2812 else if(strStartsWith(line, "+EEMUMTSSVC:")) // WCDMA Server Cell
2813 {
2814
2815 }
b.liue0ab2442024-02-06 18:53:28 +08002816 else if(strStartsWith(line, "+EEMUMTSINTRA:")) // WCDMA
liubin281ac462023-07-19 14:22:54 +08002817 {
2818
2819 }
b.liue0ab2442024-02-06 18:53:28 +08002820 else if(strStartsWith(line, "+EEMUMTSINTERRAT:")) // WCDMA
liubin281ac462023-07-19 14:22:54 +08002821 {
2822
2823 }
2824 // GSM
2825 else if(strStartsWith(line, "+EEMGINFOBASIC:")) // Basic information in GSM
2826 // 0: ME in Idle mode 1: ME in Dedicated mode 2: ME in PS PTM mode
2827 {
2828
2829 }
2830 else if(strStartsWith(line, "+EEMGINFOSVC:")) // GSM Server Cell
2831 {
2832
2833 }
b.liue0ab2442024-02-06 18:53:28 +08002834 else if(strStartsWith(line, "+EEMGINFOPS:")) // PS
liubin281ac462023-07-19 14:22:54 +08002835 {
2836
2837 }
2838
2839
2840 lines_ptr = lines_ptr->p_next;
2841 }
2842#endif
2843
2844exit:
2845 at_response_free(response);
2846 return buff_size;
2847}
2848
2849static int req_cell_info_set(const char *cmgl, char *reg, int len, int *cme_err)
2850{
2851 printf("req_cmgl_set(2)-----------------start\n");
2852 printf("cmgl:%s\n", cmgl);
2853 ATResponse *response = NULL;
b.liu9e8584b2024-11-06 19:21:28 +08002854 char cmd[500] = {0};
q.huang8a993672025-06-24 16:19:46 +08002855 char data[218] = {0};
liubin281ac462023-07-19 14:22:54 +08002856 int err = 0;
2857
q.huang8a993672025-06-24 16:19:46 +08002858 memcpy(data, cmgl, len);
liubin281ac462023-07-19 14:22:54 +08002859
q.huang8a993672025-06-24 16:19:46 +08002860 sprintf(cmd, "at*cell=%s", data);
2861 printf("cmd:%s\n", cmd);
liubin281ac462023-07-19 14:22:54 +08002862
q.huang8a993672025-06-24 16:19:46 +08002863 if(strlen(cmd) > 0)
2864 {
liubin281ac462023-07-19 14:22:54 +08002865 err = at_send_command_multiline(cmd, "", &response);
q.huang8a993672025-06-24 16:19:46 +08002866 if (err < 0 || response->success == 0 || !response->p_intermediates){
2867 *cme_err = at_get_cme_error(response);
liubin281ac462023-07-19 14:22:54 +08002868 // printf("at_send_command_multiline() is err-----------------\n");
q.huang8a993672025-06-24 16:19:46 +08002869 goto exit;
2870 }
liubin281ac462023-07-19 14:22:54 +08002871
2872 ATLine* lines_ptr = response->p_intermediates;
2873 char *line = NULL;
b.liu9e8584b2024-11-06 19:21:28 +08002874// int reg_len = 0;
q.huang8a993672025-06-24 16:19:46 +08002875// bool flag = false;
liubin281ac462023-07-19 14:22:54 +08002876 while(lines_ptr)
2877 {
2878 line = lines_ptr->line;
2879 if(line ==NULL)
2880 {
2881 printf("line is null----------------------\n");
2882 }
2883 printf("-----line:%s\n", line);
2884
2885 lines_ptr = lines_ptr->p_next;
2886 }
q.huang8a993672025-06-24 16:19:46 +08002887 }
liubin281ac462023-07-19 14:22:54 +08002888 err = 0;
q.huang8a993672025-06-24 16:19:46 +08002889 memcpy(reg, "req_cell_info_set succss", strlen("req_cell_info_set succss"));
liubin281ac462023-07-19 14:22:54 +08002890exit:
2891 at_response_free(response);
2892 printf("req_cell_info_set()-----------------end\n");
2893 return err;
2894}
2895
2896
2897
2898/*
2899AT+CSQ
2900+CSQ: 31,99
2901
2902OK
2903
2904AT+CESQ
2905+CESQ: 60,99,255,255,20,61
2906
2907OK
2908
2909AT+COPS?
2910+COPS: 0,2,"46001",7
2911
2912OK
2913
2914*/
2915static int req_net_signal_get(mbtk_signal_info_t *signal, int *cme_err)
2916{
2917 ATResponse *response = NULL;
2918 int tmp_int;
2919 char *tmp_ptr = NULL;
2920 // AT+EEMOPT=1 in the first.
q.huang8a993672025-06-24 16:19:46 +08002921#if 0
liubin281ac462023-07-19 14:22:54 +08002922 int err = at_send_command_singleline("AT+CSQ", "+CSQ:", &response);
2923 if (err < 0 || response->success == 0 || !response->p_intermediates){
2924 if(cme_err != NULL)
2925 *cme_err = at_get_cme_error(response);
2926 err = -1;
2927 goto exit;
2928 }
2929
2930 char *line = response->p_intermediates->line;
2931 err = at_tok_start(&line);
2932 if (err < 0)
2933 {
2934 goto exit;
2935 }
2936 err = at_tok_nextint(&line, &tmp_int);
2937 if (err < 0)
2938 {
2939 goto exit;
2940 }
2941 signal->rssi = (uint8)tmp_int;
2942 at_response_free(response);
q.huang8a993672025-06-24 16:19:46 +08002943#endif
2944 int err = at_send_command_singleline("AT*CESQ", "*CESQ:", &response);
liubin281ac462023-07-19 14:22:54 +08002945 if (err < 0 || response->success == 0 || !response->p_intermediates){
2946 if(cme_err != NULL)
2947 *cme_err = at_get_cme_error(response);
2948 err = -1;
2949 goto exit;
2950 }
2951
q.huang8a993672025-06-24 16:19:46 +08002952 char* line = response->p_intermediates->line;
liubin281ac462023-07-19 14:22:54 +08002953 err = at_tok_start(&line);
2954 if (err < 0)
2955 {
2956 goto exit;
2957 }
2958 err = at_tok_nextint(&line, &tmp_int);
2959 if (err < 0)
2960 {
2961 goto exit;
2962 }
2963 signal->rxlev = (uint8)tmp_int;
2964
2965 err = at_tok_nextint(&line, &tmp_int);
2966 if (err < 0)
2967 {
2968 goto exit;
2969 }
2970 signal->ber = (uint8)tmp_int;
2971
2972 err = at_tok_nextint(&line, &tmp_int);
2973 if (err < 0)
2974 {
2975 goto exit;
2976 }
2977 signal->rscp = (uint8)tmp_int;
2978
2979 err = at_tok_nextint(&line, &tmp_int);
2980 if (err < 0)
2981 {
2982 goto exit;
2983 }
2984 signal->ecno = (uint8)tmp_int;
2985
2986 err = at_tok_nextint(&line, &tmp_int);
2987 if (err < 0)
2988 {
2989 goto exit;
2990 }
2991 signal->rsrq = (uint8)tmp_int;
2992
2993 err = at_tok_nextint(&line, &tmp_int);
2994 if (err < 0)
2995 {
2996 goto exit;
2997 }
2998 signal->rsrp = (uint8)tmp_int;
2999
q.huang8a993672025-06-24 16:19:46 +08003000
3001 err = at_tok_nextint(&line, &tmp_int);
3002 if (err < 0)
3003 {
3004 goto exit;
3005 }
3006 signal->sinr = (int8)tmp_int;
3007
liubin281ac462023-07-19 14:22:54 +08003008 at_response_free(response);
3009 err = at_send_command_singleline("AT+COPS?", "+COPS:", &response);
3010 if (err < 0 || response->success == 0 || !response->p_intermediates){
3011 if(cme_err != NULL)
3012 *cme_err = at_get_cme_error(response);
3013 err = -1;
3014 goto exit;
3015 }
3016 line = response->p_intermediates->line;
3017 err = at_tok_start(&line);
3018 if (err < 0)
3019 {
3020 goto exit;
3021 }
3022 err = at_tok_nextint(&line, &tmp_int);
3023 if (err < 0)
3024 {
3025 goto exit;
3026 }
3027 if(!at_tok_hasmore(&line)) {
3028 goto exit;
3029 }
3030 err = at_tok_nextint(&line, &tmp_int);
3031 if (err < 0)
3032 {
3033 goto exit;
3034 }
3035 err = at_tok_nextstr(&line, &tmp_ptr);
3036 if (err < 0)
3037 {
3038 goto exit;
3039 }
3040 err = at_tok_nextint(&line, &tmp_int);
3041 if (err < 0)
3042 {
3043 goto exit;
3044 }
3045 signal->type = (uint8)tmp_int;
3046 net_info.net_type = signal->type;
3047
3048exit:
3049 at_response_free(response);
3050 return err;
3051}
3052
3053/*
3054AT+CREG=3
3055OK
3056
3057AT+CREG?
3058+CREG: 3,1,"8330","06447340",7
3059
3060OK
3061
3062AT+CREG?
3063+CREG: 3,0
3064
3065OK
3066
3067AT+CEREG?
3068+CEREG: 3,1,"8330","06447340",7
3069
3070OK
3071
3072
3073AT+CIREG?
3074+CIREG: 2,1,15
3075
3076OK
3077
3078AT+CIREG?
3079+CIREG: 0
3080
3081OK
3082
3083
3084*/
3085static int req_net_reg_get(mbtk_net_reg_info_t *reg, int *cme_err)
3086{
3087 ATResponse *response = NULL;
3088 int tmp_int;
3089 char *tmp_str = NULL;
3090 int err = at_send_command("AT+CREG=3", &response);
3091 if (err < 0 || response->success == 0){
3092 *cme_err = at_get_cme_error(response);
3093 goto exit;
3094 }
3095 at_response_free(response);
3096
3097 err = at_send_command_multiline("AT+CREG?", "+CREG:", &response);
3098 if (err < 0 || response->success == 0 || !response->p_intermediates){
3099 *cme_err = at_get_cme_error(response);
3100 goto exit;
3101 }
3102
3103 char *line = response->p_intermediates->line;
3104 err = at_tok_start(&line);
3105 if (err < 0)
3106 {
3107 goto exit;
3108 }
3109 err = at_tok_nextint(&line, &tmp_int); // n
3110 if (err < 0)
3111 {
3112 goto exit;
3113 }
3114 err = at_tok_nextint(&line, &tmp_int);// stat
3115 if (err < 0)
3116 {
3117 goto exit;
3118 }
3119 reg->call_state = (uint8)tmp_int;
3120
3121 if(at_tok_hasmore(&line)) {
3122 err = at_tok_nextstr(&line, &tmp_str); // lac
3123 if (err < 0)
3124 {
yq.wang31f71842024-12-27 14:07:01 +08003125 LOGE("[%s] at_tok_nextstr get lac fail.[%d]", __func__, err);
liubin281ac462023-07-19 14:22:54 +08003126 goto exit;
3127 }
3128 reg->lac = strtol(tmp_str, NULL, 16);
3129
3130 err = at_tok_nextstr(&line, &tmp_str); // ci
3131 if (err < 0)
3132 {
yq.wang31f71842024-12-27 14:07:01 +08003133 LOGE("[%s] at_tok_nextstr get ci fail.[%d]", __func__, err);
liubin281ac462023-07-19 14:22:54 +08003134 goto exit;
3135 }
3136 reg->ci = strtol(tmp_str, NULL, 16);
3137
3138 err = at_tok_nextint(&line, &tmp_int);// AcT
3139 if (err < 0)
3140 {
yq.wang31f71842024-12-27 14:07:01 +08003141 LOGE("[%s] at_tok_nextstr get act fail.[%d]", __func__, err);
3142 //goto exit;
liubin281ac462023-07-19 14:22:54 +08003143 }
yq.wang31f71842024-12-27 14:07:01 +08003144 else
3145 {
3146 reg->type = (uint8)tmp_int;
3147 }
liubin281ac462023-07-19 14:22:54 +08003148 }
3149 at_response_free(response);
3150
yq.wang31f71842024-12-27 14:07:01 +08003151 if(reg->type == MBTK_RADIO_TECH_E_UTRAN)
3152 {
3153 err = at_send_command_multiline("AT+CEREG?", "+CEREG:", &response);
3154 if (err < 0 || response->success == 0 || !response->p_intermediates){
3155 *cme_err = at_get_cme_error(response);
3156 goto exit;
3157 }
liubin281ac462023-07-19 14:22:54 +08003158
yq.wang31f71842024-12-27 14:07:01 +08003159 line = response->p_intermediates->line;
3160 err = at_tok_start(&line);
liubin281ac462023-07-19 14:22:54 +08003161 if (err < 0)
3162 {
3163 goto exit;
3164 }
yq.wang31f71842024-12-27 14:07:01 +08003165 err = at_tok_nextint(&line, &tmp_int); // n
liubin281ac462023-07-19 14:22:54 +08003166 if (err < 0)
3167 {
3168 goto exit;
3169 }
yq.wang31f71842024-12-27 14:07:01 +08003170 err = at_tok_nextint(&line, &tmp_int);// stat
liubin281ac462023-07-19 14:22:54 +08003171 if (err < 0)
3172 {
3173 goto exit;
3174 }
yq.wang31f71842024-12-27 14:07:01 +08003175 reg->data_state = (uint8)tmp_int;
3176
3177 if(reg->lac == 0 && at_tok_hasmore(&line)) {
3178 err = at_tok_nextstr(&line, &tmp_str); // lac
3179 if (err < 0)
3180 {
3181 goto exit;
3182 }
3183 reg->lac = strtol(tmp_str, NULL, 16);
3184
3185 err = at_tok_nextstr(&line, &tmp_str); // ci
3186 if (err < 0)
3187 {
3188 goto exit;
3189 }
3190 reg->ci = strtol(tmp_str, NULL, 16);
3191
3192 err = at_tok_nextint(&line, &tmp_int);// AcT
3193 if (err < 0)
3194 {
3195 goto exit;
3196 }
3197 reg->type = (uint8)tmp_int;
3198 }
3199 at_response_free(response);
liubin281ac462023-07-19 14:22:54 +08003200 }
yq.wang31f71842024-12-27 14:07:01 +08003201 else
3202 {
3203 err = at_send_command_multiline("AT+CGREG?", "+CGREG:", &response);
3204 if (err < 0 || response->success == 0 || !response->p_intermediates){
3205 *cme_err = at_get_cme_error(response);
3206 goto exit;
3207 }
3208
3209 line = response->p_intermediates->line;
3210 err = at_tok_start(&line);
3211 if (err < 0)
3212 {
3213 goto exit;
3214 }
3215 err = at_tok_nextint(&line, &tmp_int); // n
3216 if (err < 0)
3217 {
3218 goto exit;
3219 }
3220 err = at_tok_nextint(&line, &tmp_int);// stat
3221 if (err < 0)
3222 {
3223 goto exit;
3224 }
3225 reg->data_state = (uint8)tmp_int;
3226
3227 if(reg->lac == 0 && at_tok_hasmore(&line)) {
3228 err = at_tok_nextstr(&line, &tmp_str); // lac
3229 if (err < 0)
3230 {
3231 goto exit;
3232 }
3233 reg->lac = strtol(tmp_str, NULL, 16);
3234
3235 err = at_tok_nextstr(&line, &tmp_str); // ci
3236 if (err < 0)
3237 {
3238 goto exit;
3239 }
3240 reg->ci = strtol(tmp_str, NULL, 16);
3241
3242 err = at_tok_nextint(&line, &tmp_int);// AcT
3243 if (err < 0)
3244 {
3245 goto exit;
3246 }
3247 reg->type = (uint8)tmp_int;
3248 }
3249 at_response_free(response);
3250 }
liubin281ac462023-07-19 14:22:54 +08003251
3252 err = at_send_command_multiline("AT+CIREG?", "+CIREG:", &response);
3253 if (err < 0 || response->success == 0 || !response->p_intermediates){
3254 reg->ims_state = (uint8)0;
3255 err = 0;
3256 goto exit;
3257 }
3258 line = response->p_intermediates->line;
3259 err = at_tok_start(&line);
3260 if (err < 0)
3261 {
3262 goto exit;
3263 }
3264 err = at_tok_nextint(&line, &tmp_int); // n/stat
3265 if (err < 0)
3266 {
3267 goto exit;
3268 }
3269 if(at_tok_hasmore(&line)) {
3270 err = at_tok_nextint(&line, &tmp_int);// stat
3271 if (err < 0)
3272 {
3273 goto exit;
3274 }
3275 reg->ims_state = (uint8)tmp_int;
3276 } else {
3277 reg->ims_state = (uint8)tmp_int;
3278 }
3279
3280exit:
3281 at_response_free(response);
3282 return err;
3283}
3284
r.xiao06db9a12024-04-14 18:51:15 -07003285
3286static int net_ims_set(uint8 reg, int *cme_err)
3287{
3288 ATResponse *response = NULL;
3289 char cmd[30] = {0};
3290 int err = -1;
3291
b.liufdf03172024-06-07 15:01:29 +08003292 sprintf(cmd, "AT+ACONFIG=\"IMSD=%d\"", reg);
r.xiao06db9a12024-04-14 18:51:15 -07003293 err = at_send_command(cmd, &response);
3294 LOG("cmd : %s", cmd);
3295
3296 if (err < 0 || response->success == 0){
3297 *cme_err = at_get_cme_error(response);
3298 goto exit;
3299 }
3300
3301exit:
3302 at_response_free(response);
3303 return err;
3304}
3305
3306
3307static int net_ims_get(int *reg, int *cme_err)
3308{
3309 ATResponse *response = NULL;
b.liu9e8584b2024-11-06 19:21:28 +08003310 int tmp_reg = 0;
b.liufdf03172024-06-07 15:01:29 +08003311 int err;
b.liu9e8584b2024-11-06 19:21:28 +08003312// char *tmp_str = NULL;
b.liufdf03172024-06-07 15:01:29 +08003313
3314 err = at_send_command_singleline("AT+ACONFIG?", "", &response);
3315 if (err < 0 || response->success == 0 || !response->p_intermediates){
3316 tmp_reg = 0;
3317 err = 0;
3318 goto exit;
3319 }
3320 if(response->p_intermediates->line) {
3321 char *ptr = strstr(response->p_intermediates->line, "IMSD=");
3322 if(ptr) {
3323 tmp_reg = atoi(ptr + strlen("IMSD="));
3324 }
3325 }
3326
3327 LOG("net_ims_get reg : %u", tmp_reg);
3328
3329exit:
3330 at_response_free(response);
3331 *reg = tmp_reg;
3332 return err;
3333}
3334
3335static int net_ims_reg_state_get(int *reg, int *cme_err)
3336{
3337 ATResponse *response = NULL;
3338 int tmp_int, tmp_reg = -1;
r.xiao06db9a12024-04-14 18:51:15 -07003339 int err;
b.liu9e8584b2024-11-06 19:21:28 +08003340// char *tmp_str = NULL;
r.xiao06db9a12024-04-14 18:51:15 -07003341
3342 err = at_send_command_multiline("AT+CIREG?", "+CIREG:", &response);
3343 if (err < 0 || response->success == 0 || !response->p_intermediates){
3344 tmp_reg = 0;
3345 err = 0;
3346 goto exit;
3347 }
3348 char *line = response->p_intermediates->line;
3349 err = at_tok_start(&line);
3350 if (err < 0)
3351 {
3352 goto exit;
3353 }
3354 err = at_tok_nextint(&line, &tmp_int); // n/stat
3355 if (err < 0)
3356 {
3357 goto exit;
3358 }
liuyangc4ca9592024-06-06 15:43:50 +08003359
b.liufdf03172024-06-07 15:01:29 +08003360 if(at_tok_hasmore(&line)) {
3361 err = at_tok_nextint(&line, &tmp_int);// stat
3362 if (err < 0)
3363 {
3364 goto exit;
3365 }
3366 tmp_reg = tmp_int;
3367 } else {
3368 tmp_reg = tmp_int;
3369 }
r.xiao06db9a12024-04-14 18:51:15 -07003370
3371 LOG("net_ims_get reg : %u", tmp_reg);
3372
3373exit:
3374 at_response_free(response);
3375 *reg = tmp_reg;
3376 return err;
3377}
3378
3379
b.liufdf03172024-06-07 15:01:29 +08003380
liubin281ac462023-07-19 14:22:54 +08003381/*
3382AT+CGDCONT?
3383+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,,,,
3384
3385+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
3386
3387OK
3388
3389
3390*/
wangyouqianged88c722023-11-22 16:33:43 +08003391#ifdef MBTK_AF_SUPPORT
3392mbtk_ip_type_enum default_iptype = MBTK_IP_TYPE_IPV4V6;
3393#endif
3394
liubin281ac462023-07-19 14:22:54 +08003395static int req_apn_get(void *data, int *data_len, int *cme_err)
3396{
3397 ATResponse *response = NULL;
3398 int err = at_send_command_multiline("AT+CGDCONT?", "+CGDCONT:", &response);
3399
3400 if (err < 0 || response->success == 0 || !response->p_intermediates){
3401 *cme_err = at_get_cme_error(response);
3402 goto exit;
3403 }
3404
3405 ATLine* lines_ptr = response->p_intermediates;
3406 char *line = NULL;
3407 int tmp_int;
3408 char *tmp_str = NULL;
3409 /*
3410 <apn_num[1]><cid[1]><ip_type[1]><apn_len[2]><apn><user_len[2]><user><pass_len[2]><pass><auth_len[2]><auth>...
3411 <cid[1]><ip_type[1]><apn_len[2]><apn><user_len[2]><user><pass_len[2]><pass><auth_len[2]><auth>
3412 */
3413 uint8* apn_num = (uint8*)data;
3414 uint8* data_ptr = (uint8*)(data + sizeof(uint8)); // Jump apn_num[1]
3415 mbtk_apn_info_t apn;
3416 while(lines_ptr)
3417 {
3418 line = lines_ptr->line;
3419 err = at_tok_start(&line);
3420 if (err < 0)
3421 {
3422 goto exit;
3423 }
3424
3425 err = at_tok_nextint(&line, &tmp_int); // cid
3426 if (err < 0)
3427 {
3428 goto exit;
3429 }
3430 // Only get CID 1-7
3431 if(tmp_int >= MBTK_APN_CID_MIN && tmp_int <= MBTK_APN_CID_MAX) {
3432 memset(&apn, 0x0, sizeof(mbtk_apn_info_t));
3433 apn.cid = tmp_int;
3434 *data_ptr++ = (uint8)tmp_int; // cid
3435
3436 err = at_tok_nextstr(&line, &tmp_str);// ip type
3437 if (err < 0)
3438 {
3439 goto exit;
3440 }
3441 if(!strcasecmp(tmp_str, "IP")) {
3442 *data_ptr++ = (uint8)MBTK_IP_TYPE_IP;
3443 apn.ip_type = MBTK_IP_TYPE_IP;
3444 } else if(!strcasecmp(tmp_str, "IPV6")) {
3445 *data_ptr++ = (uint8)MBTK_IP_TYPE_IPV6;
3446 apn.ip_type = MBTK_IP_TYPE_IPV6;
3447 } else if(!strcasecmp(tmp_str, "IPV4V6")) {
3448 *data_ptr++ = (uint8)MBTK_IP_TYPE_IPV4V6;
3449 apn.ip_type = MBTK_IP_TYPE_IPV4V6;
3450 } else {
3451 *data_ptr++ = (uint8)MBTK_IP_TYPE_PPP;
3452 apn.ip_type = MBTK_IP_TYPE_PPP;
3453 }
3454
wangyouqianged88c722023-11-22 16:33:43 +08003455#ifdef MBTK_AF_SUPPORT
3456 if(apn.cid == 1)
3457 {
3458 default_iptype = apn.ip_type;
3459 }
3460#endif
liubin281ac462023-07-19 14:22:54 +08003461 err = at_tok_nextstr(&line, &tmp_str); // apn
3462 if (err < 0)
3463 {
3464 goto exit;
3465 }
3466 if(str_empty(tmp_str)) {
3467 uint16_2_byte((uint16)0, data_ptr, false);
3468 data_ptr += sizeof(uint16);
3469 } else {
3470 uint16_2_byte((uint16)strlen(tmp_str), data_ptr, false);
3471 data_ptr += sizeof(uint16);
3472 memcpy(data_ptr, tmp_str, strlen(tmp_str));
3473 data_ptr += strlen(tmp_str);
3474 memcpy(apn.apn, tmp_str, strlen(tmp_str));
3475 }
3476
3477 if(apn_user_pass_set_by_cid(apn.cid, &apn)) {
3478 // user
3479 uint16_2_byte((uint16)0, data_ptr, false);
3480 data_ptr += sizeof(uint16);
3481
3482 // pass
3483 uint16_2_byte((uint16)0, data_ptr, false);
3484 data_ptr += sizeof(uint16);
3485
3486 // auth
3487 uint16_2_byte((uint16)0, data_ptr, false);
3488 data_ptr += sizeof(uint16);
3489 } else {
3490 // user
3491 if(str_empty(apn.user)) {
3492 uint16_2_byte((uint16)0, data_ptr, false);
3493 data_ptr += sizeof(uint16);
3494 } else {
b.liu9e8584b2024-11-06 19:21:28 +08003495 uint16_2_byte((uint16)strlen((char*)apn.user), data_ptr, false);
liubin281ac462023-07-19 14:22:54 +08003496 data_ptr += sizeof(uint16);
b.liu9e8584b2024-11-06 19:21:28 +08003497 memcpy(data_ptr, apn.user, strlen((char*)apn.user));
3498 data_ptr += strlen((char*)apn.user);
liubin281ac462023-07-19 14:22:54 +08003499 }
3500
3501 // pass
3502 if(str_empty(apn.pass)) {
3503 uint16_2_byte((uint16)0, data_ptr, false);
3504 data_ptr += sizeof(uint16);
3505 } else {
b.liu9e8584b2024-11-06 19:21:28 +08003506 uint16_2_byte((uint16)strlen((char*)apn.pass), data_ptr, false);
liubin281ac462023-07-19 14:22:54 +08003507 data_ptr += sizeof(uint16);
b.liu9e8584b2024-11-06 19:21:28 +08003508 memcpy(data_ptr, apn.pass, strlen((char*)apn.pass));
3509 data_ptr += strlen((char*)apn.pass);
liubin281ac462023-07-19 14:22:54 +08003510 }
3511
3512 // auth
3513 if(str_empty(apn.auth)) {
3514 uint16_2_byte((uint16)0, data_ptr, false);
3515 data_ptr += sizeof(uint16);
3516 } else {
b.liu9e8584b2024-11-06 19:21:28 +08003517 uint16_2_byte((uint16)strlen((char*)apn.auth), data_ptr, false);
liubin281ac462023-07-19 14:22:54 +08003518 data_ptr += sizeof(uint16);
b.liu9e8584b2024-11-06 19:21:28 +08003519 memcpy(data_ptr, apn.auth, strlen((char*)apn.auth));
3520 data_ptr += strlen((char*)apn.auth);
liubin281ac462023-07-19 14:22:54 +08003521 }
3522 }
3523
3524 (*apn_num)++;
3525 }
3526
3527 lines_ptr = lines_ptr->p_next;
3528 }
3529
3530 *data_len = data_ptr - (uint8*)data;
3531
3532 goto exit;
3533exit:
3534 at_response_free(response);
3535 return err;
3536}
3537
3538#if 0
3539/*
3540LTE APN
3541AT+CFUN=4
3542AT*CGDFLT=1,IP,"private.vpdn",1,0,1,0,0,0,0,0,0,0,1,0,0,1,0,0,0,1
3543AT*CGDFAUTH=1,2,"noc@njcc.vpdn.js","123456"
3544AT+CFUN=1
3545AT+CEREG?
3546AT+CGDCONT?
3547
35482/3G APN
3549AT+CGREG?
3550AT+CGDCONT=6,IP,"private.vpdn"
3551AT*AUTHREQ=6,2,"noc@njcc.vpdn.js","123456"
3552AT+CGDATA="",6
3553AT+CGDCONT?
3554*/
3555static int req_apn_set_username(mbtk_apn_info_t *apn, int *cme_err)
3556{
3557 ATResponse *response = NULL;
3558 char cmd[400] = {0};
3559 int index = 0;
3560 int err = 0;
3561
3562 err = at_send_command_singleline("AT+COPS?", "+COPS:", &response);
3563 if (err < 0 || response->success == 0 || !response->p_intermediates){
3564 if(cme_err != NULL)
3565 *cme_err = at_get_cme_error(response);
3566 err = -1;
3567 goto apn_set;
3568 }
3569
3570 int tmp_int = 0;
3571 int state=0;
3572 char cmd_buf[64];
3573 char *line = response->p_intermediates->line;
3574 err = at_tok_start(&line);
3575 if (err < 0)
3576 {
3577 goto apn_set;
3578 }
3579 err = at_tok_nextint(&line, &tmp_int);
3580 if (err < 0)
3581 {
3582 goto apn_set;
3583 }
3584 err = at_tok_nextint(&line, &tmp_int);
3585 if (err < 0)
3586 {
3587 goto apn_set;
3588 }
3589 err = at_tok_nextstr(&line, &cmd_buf);
3590 if (err < 0)
3591 {
3592 goto apn_set;
3593 }
3594 err = at_tok_nextint(&line, &tmp_int);
3595 if (err < 0)
3596 {
3597 goto apn_set;
3598 }
3599 else
3600 state = tmp_int;
3601
3602apn_set:
3603 at_response_free(response);
3604 *cme_err = MBTK_INFO_ERR_CME_NON;
3605 //if(state == 7 && apn->cid == 1) //LTE && cid = 1
3606 if(0) //LTE && cid = 1
3607 {
3608 err = at_send_command("AT+CFUN=4", &response);
3609 if (err < 0 || response->success == 0){
3610 *cme_err = at_get_cme_error(response);
3611 goto exit;
3612 }
3613 at_response_free(response);
3614
3615 memset(cmd, 0, 400);
3616 index = 0;
3617 index += sprintf(cmd, "AT*CGDFLT=%d,", 1);
3618 switch(apn->ip_type) {
3619 case MBTK_IP_TYPE_IP: {
3620 index += sprintf(cmd + index,"\"IP\",");
3621 break;
3622 }
3623 case MBTK_IP_TYPE_IPV6: {
3624 index += sprintf(cmd + index,"\"IPV6\",");
3625 break;
3626 }
3627 case MBTK_IP_TYPE_IPV4V6: {
3628 index += sprintf(cmd + index,"\"IPV4V6\",");
3629 break;
3630 }
3631 default: {
3632 index += sprintf(cmd + index,"\"PPP\",");
3633 break;
3634 }
3635 }
3636
3637 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);
3638 err = at_send_command(cmd, &response);
3639 if (err < 0 || response->success == 0){
3640 *cme_err = at_get_cme_error(response);
3641 goto exit;
3642 }
3643 at_response_free(response);
3644
3645 memset(cmd, 0, 400);
3646 int cmd_auth=0;
3647 if(strstr(apn->auth,"NONE"))
3648 cmd_auth = 0;
3649 else if(strstr(apn->auth,"PAP"))
3650 cmd_auth = 1;
3651 else if(strstr(apn->auth,"CHAP"))
3652 cmd_auth = 2;
3653 else if(strstr(apn->auth,"PAP AND CHAP"))
3654 cmd_auth = 3;
3655 else
3656 goto exit;
3657
3658 sprintf(cmd,"AT*CGDFAUTH=1,%d,%s,%s",cmd_auth,apn->user,apn->pass);
3659
3660 err = at_send_command(cmd, &response);
3661 if (err < 0 || response->success == 0){
3662 *cme_err = at_get_cme_error(response);
3663 goto exit;
3664 }
3665 }
3666 else //2/3G
3667 {
3668 memset(cmd,0,400);
3669 index = 0;
3670 index += sprintf(cmd, "AT+CGDCONT=%d,", apn->cid);
3671 switch(apn->ip_type) {
3672 case MBTK_IP_TYPE_IP: {
3673 index += sprintf(cmd + index,"\"IP\",");
3674 break;
3675 }
3676 case MBTK_IP_TYPE_IPV6: {
3677 index += sprintf(cmd + index,"\"IPV6\",");
3678 break;
3679 }
3680 case MBTK_IP_TYPE_IPV4V6: {
3681 index += sprintf(cmd + index,"\"IPV4V6\",");
3682 break;
3683 }
3684 default: {
3685 index += sprintf(cmd + index,"\"PPP\",");
3686 break;
3687 }
3688 }
3689 index += sprintf(cmd + index, "\"%s\"", apn->apn);
3690
3691 err = at_send_command(cmd, &response);
3692 if (err < 0 || response->success == 0){
3693 *cme_err = at_get_cme_error(response);
3694 goto exit;
3695 }
3696 at_response_free(response);
3697
3698 memset(cmd,0,400);
3699 int cmd_auth=0;
3700 if(strstr(apn->auth,"NONE"))
3701 cmd_auth = 0;
3702 else if(strstr(apn->auth,"PAP"))
3703 cmd_auth = 1;
3704 else if(strstr(apn->auth,"CHAP"))
3705 cmd_auth = 2;
3706 else if(strstr(apn->auth,"PAP AND CHAP"))
3707 cmd_auth = 3;
3708 else
3709 goto exit;
3710
3711 sprintf(cmd, "AT*AUTHREQ=%d,%d,%s,%s",apn->cid,cmd_auth,apn->user,apn->pass);
3712 err = at_send_command(cmd, &response);
3713 if (err < 0 || response->success == 0){
3714 *cme_err = at_get_cme_error(response);
3715 goto exit;
3716 }
3717 }
3718
3719exit:
3720 at_response_free(response);
3721 return err;
3722}
3723#endif
3724
3725/*
3726AT+CGDCONT=1,"IPV4V6","cmnet"
3727OK
3728
3729AT*CGDFLT=1,"IPv4v6","reliance.grevpdn.zj",,,,,,,,,,,,,,,,,,1
3730OK
3731
3732*/
3733static int req_apn_set(mbtk_apn_info_t *apn, int *cme_err)
3734{
3735 ATResponse *response = NULL;
3736 char cmd[400] = {0};
3737 int index = 0;
3738 int err = 0;
3739
3740 index += sprintf(cmd, "AT+CGDCONT=%d,", apn->cid);
3741 switch(apn->ip_type) {
3742 case MBTK_IP_TYPE_IP: {
3743 index += sprintf(cmd + index,"\"IP\",");
3744 break;
3745 }
3746 case MBTK_IP_TYPE_IPV6: {
3747 index += sprintf(cmd + index,"\"IPV6\",");
3748 break;
3749 }
3750 case MBTK_IP_TYPE_IPV4V6: {
3751 index += sprintf(cmd + index,"\"IPV4V6\",");
3752 break;
3753 }
3754 default: {
3755 index += sprintf(cmd + index,"\"PPP\",");
3756 break;
3757 }
3758 }
b.liu9e8584b2024-11-06 19:21:28 +08003759 if(strlen((char*)apn->apn) > 0) {
liubin281ac462023-07-19 14:22:54 +08003760 index += sprintf(cmd + index,"\"%s\"", apn->apn);
b.liudfec1e12024-06-06 16:38:59 +08003761 }
liubin281ac462023-07-19 14:22:54 +08003762
3763 err = at_send_command(cmd, &response);
3764 if (err < 0 || response->success == 0){
3765 if(cme_err) {
3766 *cme_err = at_get_cme_error(response);
3767 }
3768 goto exit;
3769 }
3770
3771 if(!str_empty(apn->user) || !str_empty(apn->pass)) {
3772 at_response_free(response);
xy.he9f275d62024-12-17 21:50:40 -08003773 response = NULL;
liubin281ac462023-07-19 14:22:54 +08003774 memset(cmd,0,400);
3775 int cmd_auth=0;
b.liu9e8584b2024-11-06 19:21:28 +08003776 if(strstr((char*)apn->auth,"NONE"))
liubin281ac462023-07-19 14:22:54 +08003777 cmd_auth = 0;
b.liu9e8584b2024-11-06 19:21:28 +08003778 else if(strstr((char*)apn->auth,"PAP"))
liubin281ac462023-07-19 14:22:54 +08003779 cmd_auth = 1;
b.liu9e8584b2024-11-06 19:21:28 +08003780 else if(strstr((char*)apn->auth,"CHAP"))
liubin281ac462023-07-19 14:22:54 +08003781 cmd_auth = 2;
3782#if 0
3783 else if(strstr(apn->auth,"PAP AND CHAP"))
3784 cmd_auth = 3;
3785#endif
3786 else
3787 goto exit;
3788
3789 sprintf(cmd, "AT*AUTHREQ=%d,%d,%s,%s",apn->cid,cmd_auth,apn->user,apn->pass);
3790 err = at_send_command(cmd, &response);
3791 if (err < 0 || response->success == 0){
3792 *cme_err = at_get_cme_error(response);
3793 goto exit;
3794 }
3795 }
3796
3797exit:
3798 at_response_free(response);
3799 return err;
3800}
3801
liuyang1cefd852024-04-24 18:30:53 +08003802static int req_apn_del(int data, int *cme_err)
liuyang0e49d9a2024-04-23 21:04:54 +08003803{
3804 ATResponse *response = NULL;
3805 char cmd[64]={0};
liuyang1cefd852024-04-24 18:30:53 +08003806 sprintf(cmd, "AT+CGDCONT=%d", data);
liuyang0e49d9a2024-04-23 21:04:54 +08003807 int err = at_send_command(cmd, &response);
3808 if (err < 0 || response->success == 0){
3809 if(cme_err) {
3810 *cme_err = at_get_cme_error(response);
3811 }
3812 goto exit;
3813 }
3814
3815exit:
3816 at_response_free(response);
3817 return err;
3818}
3819
3820
liubin281ac462023-07-19 14:22:54 +08003821int wait_cgact_complete(int timeout)
3822{
3823 int count = timeout * 10; // timeout * 1000 / 100
3824 int i = 0;
3825
3826 while(cgact_wait.waitting && i < count) {
3827 i++;
3828 usleep(100000); // 100ms
3829 }
3830
3831 if(i == count) { // Timeout
3832 return -1;
3833 } else {
3834 return 0;
3835 }
3836}
3837
b.liu9e8584b2024-11-06 19:21:28 +08003838#if 0
liubin281ac462023-07-19 14:22:54 +08003839/*
3840AT+CGDATA="",6
3841CONNECT
3842
3843OK
3844
3845AT+CFUN=1
3846
3847OK
3848
3849*/
3850static int req_data_call_user_start(int cid, int *cme_err)
3851{
3852 ATResponse *response = NULL;
3853 char cmd[400] = {0};
b.liu9e8584b2024-11-06 19:21:28 +08003854// int index = 0;
liubin281ac462023-07-19 14:22:54 +08003855 int err = 0;
3856
3857 err = at_send_command_singleline("AT+COPS?", "+COPS:", &response);
3858 if (err < 0 || response->success == 0 || !response->p_intermediates){
3859 if(cme_err != NULL)
3860 *cme_err = at_get_cme_error(response);
3861 err = -1;
3862 goto exit;
3863 }
3864
3865 int tmp_int;
b.liu9e8584b2024-11-06 19:21:28 +08003866 char *cmd_buf = NULL;
liubin281ac462023-07-19 14:22:54 +08003867 char *line = response->p_intermediates->line;
3868 err = at_tok_start(&line);
3869 if (err < 0)
3870 {
3871 goto exit;
3872 }
3873 err = at_tok_nextint(&line, &tmp_int);
3874 if (err < 0)
3875 {
3876 goto exit;
3877 }
3878 err = at_tok_nextint(&line, &tmp_int);
3879 if (err < 0)
3880 {
3881 goto exit;
3882 }
3883 err = at_tok_nextstr(&line, &cmd_buf);
3884 if (err < 0)
3885 {
3886 goto exit;
3887 }
3888 err = at_tok_nextint(&line, &tmp_int);
3889 if (err < 0)
3890 {
3891 goto exit;
3892 }
3893 at_response_free(response);
3894
3895 if(tmp_int == 7 && cid == 1) //LTE && cid = 1
3896 {
3897 ATResponse *response = NULL;
b.liu9e8584b2024-11-06 19:21:28 +08003898// char cmd[400] = {0};
liubin281ac462023-07-19 14:22:54 +08003899 int err = 0;
3900
3901 err = at_send_command("AT+CFUN=1", &response);
3902 if (err < 0 || response->success == 0){
3903 if(cme_err) {
3904 *cme_err = at_get_cme_error(response);
3905 }
3906 goto exit;
3907 }
3908 }
3909 else
3910 {
3911 ATResponse *response = NULL;
b.liu9e8584b2024-11-06 19:21:28 +08003912// char cmd[400] = {0};
3913 memset(cmd, 0, sizeof(cmd));
liubin281ac462023-07-19 14:22:54 +08003914 int err = 0;
3915 sprintf(cmd, "AT+CGDATA=\"\",%d", cid);
3916 err = at_send_command(cmd, &response);
3917 if (err < 0 || response->success == 0){
3918 if(cme_err) {
3919 *cme_err = at_get_cme_error(response);
3920 }
3921 goto exit;
3922 }
3923 }
3924
3925exit:
3926 at_response_free(response);
3927 return err;
3928}
b.liu9e8584b2024-11-06 19:21:28 +08003929#endif
liubin281ac462023-07-19 14:22:54 +08003930
3931/*
3932AT+CGACT?
3933+CGACT: 1,1
3934+CGACT: 8,1
3935OK
3936
3937AT+CGACT=1,<cid>
3938OK
3939
3940*/
3941static int req_data_call_start(int cid, int *cme_err)
3942{
3943 ATResponse *response = NULL;
3944 char cmd[400] = {0};
3945 int err = 0;
3946#if 0
3947 err = at_send_command_multiline("AT+CGACT?", "+CGACT:", &response);
3948 if (err < 0 || response->success == 0 || !response->p_intermediates){
3949 *cme_err = at_get_cme_error(response);
3950 goto exit;
3951 }
3952 ATLine* lines_ptr = response->p_intermediates;
3953 char *line = NULL;
3954 int tmp_int;
3955 while(lines_ptr)
3956 {
3957 line = lines_ptr->line;
3958 err = at_tok_start(&line);
3959 if (err < 0)
3960 {
3961 goto exit;
3962 }
3963
3964 err = at_tok_nextint(&line, &tmp_int); // cid
3965 if (err < 0)
3966 {
3967 goto exit;
3968 }
3969 if(tmp_int == cid) { // Found cid
3970 err = at_tok_nextint(&line, &tmp_int); // cid
3971 if (err < 0)
3972 {
3973 goto exit;
3974 }
3975 if(tmp_int == 1) { // This cid has active.
3976 goto net_config;
3977 } else {
3978 goto cid_active;
3979 }
3980 break;
3981 }
3982
3983 lines_ptr = lines_ptr->p_next;
3984 }
3985
3986 if(lines_ptr == NULL) { // No found this cid.
3987 LOGE("No found cid : %d", cid);
3988 goto exit;
3989 }
3990 at_response_free(response);
3991
3992 // Start active cid.
3993cid_active:
3994#endif
3995
3996 sprintf(cmd, "AT+CGACT=1,%d", cid);
3997 err = at_send_command(cmd, &response);
3998 if (err < 0 || response->success == 0){
3999 if(cme_err) {
4000 *cme_err = at_get_cme_error(response);
4001 }
4002 goto exit;
4003 }
4004
4005exit:
4006 at_response_free(response);
4007 return err;
4008}
4009
4010/*
4011AT+CGACT=0,<cid>
4012OK
4013
4014*/
4015static int req_data_call_stop(int cid, int *cme_err)
4016{
4017 ATResponse *response = NULL;
4018 char cmd[400] = {0};
4019 int err = 0;
4020#if 0
4021 err = at_send_command_multiline("AT+CGACT?", "+CGACT:", &response);
4022 if (err < 0 || response->success == 0 || !response->p_intermediates){
4023 *cme_err = at_get_cme_error(response);
4024 goto exit;
4025 }
4026 ATLine* lines_ptr = response->p_intermediates;
4027 char *line = NULL;
4028 int tmp_int;
4029 while(lines_ptr)
4030 {
4031 line = lines_ptr->line;
4032 err = at_tok_start(&line);
4033 if (err < 0)
4034 {
4035 goto exit;
4036 }
4037
4038 err = at_tok_nextint(&line, &tmp_int); // cid
4039 if (err < 0)
4040 {
4041 goto exit;
4042 }
4043 if(tmp_int == cid) { // Found cid
4044 err = at_tok_nextint(&line, &tmp_int); // cid
4045 if (err < 0)
4046 {
4047 goto exit;
4048 }
4049 if(tmp_int == 1) { // This cid has active.
4050 goto net_config;
4051 } else {
4052 goto cid_active;
4053 }
4054 break;
4055 }
4056
4057 lines_ptr = lines_ptr->p_next;
4058 }
4059
4060 if(lines_ptr == NULL) { // No found this cid.
4061 LOGE("No found cid : %d", cid);
4062 goto exit;
4063 }
4064 at_response_free(response);
4065
4066 // Start active cid.
4067cid_active:
4068#endif
4069
4070 sprintf(cmd, "AT+CGACT=0,%d", cid);
4071 err = at_send_command(cmd, &response);
4072 if (err < 0 || response->success == 0){
4073 *cme_err = at_get_cme_error(response);
4074 goto exit;
4075 }
4076
4077exit:
4078 at_response_free(response);
4079 return err;
4080}
4081
4082/*
4083IPv4 : 10.255.74.26
4084IPv6 : 254.128.0.0.0.0.0.0.0.1.0.2.144.5.212.239
4085*/
4086static bool is_ipv4(const char *ip)
4087{
4088 const char *ptr = ip;
4089 int count = 0;
4090 while(*ptr) {
4091 if(*ptr == '.')
4092 count++;
4093 ptr++;
4094 }
4095
4096 if(count == 3) {
4097 return true;
4098 } else {
4099 return false;
4100 }
4101}
4102
4103/*
4104AT+CGCONTRDP=1
4105+CGCONTRDP: 1,7,"cmnet-2.MNC000.MCC460.GPRS","10.255.74.26","","223.87.253.100","223.87.253.253","","",0,0
4106+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
4107
4108OK
4109
4110*/
4111static int req_data_call_state_get(int cid, mbtk_ipv4_info_t *ipv4, mbtk_ipv6_info_t *ipv6, int *cme_err)
4112{
4113 ATResponse *response = NULL;
4114 char cmd[50] = {0};
4115 int err = 0;
4116
4117 sprintf(cmd, "AT+CGCONTRDP=%d", cid);
4118
4119 err = at_send_command_multiline(cmd, "+CGCONTRDP:", &response);
4120 if (err < 0 || response->success == 0 || !response->p_intermediates){
4121 *cme_err = at_get_cme_error(response);
4122 goto exit;
4123 }
4124 ATLine* lines_ptr = response->p_intermediates;
4125 char *line = NULL;
4126 int tmp_int;
4127 char *tmp_ptr = NULL;
4128 while(lines_ptr)
4129 {
4130 line = lines_ptr->line;
4131 err = at_tok_start(&line);
4132 if (err < 0)
4133 {
4134 goto exit;
4135 }
4136
4137 err = at_tok_nextint(&line, &tmp_int); // cid
4138 if (err < 0)
4139 {
4140 goto exit;
4141 }
4142 err = at_tok_nextint(&line, &tmp_int); // bearer_id
4143 if (err < 0)
4144 {
4145 goto exit;
4146 }
4147 err = at_tok_nextstr(&line, &tmp_ptr); // APN
4148 if (err < 0)
4149 {
4150 goto exit;
4151 }
4152
4153 err = at_tok_nextstr(&line, &tmp_ptr); // IP
4154 if (err < 0 || str_empty(tmp_ptr))
4155 {
4156 goto exit;
4157 }
4158 if(is_ipv4(tmp_ptr)) {
4159 if(inet_pton(AF_INET, tmp_ptr, &(ipv4->IPAddr)) < 0) {
4160 LOGE("inet_pton() fail.");
4161 err = -1;
4162 goto exit;
4163 }
4164
4165 ipv4->valid = true;
4166 //log_hex("IPv4", &(ipv4->IPAddr), sizeof(struct in_addr));
4167 } else {
4168 if(str_2_ipv6(tmp_ptr, &(ipv6->IPV6Addr))) {
4169 LOGE("str_2_ipv6() fail.");
4170 err = -1;
4171 goto exit;
4172 }
4173
4174 ipv6->valid = true;
4175 //log_hex("IPv6", &(ipv6->IPV6Addr), 16);
4176 }
4177
4178 err = at_tok_nextstr(&line, &tmp_ptr); // Gateway
4179 if (err < 0)
4180 {
4181 goto exit;
4182 }
4183 if(!str_empty(tmp_ptr)) { // No found gateway
4184 if(is_ipv4(tmp_ptr)) {
4185 if(inet_pton(AF_INET, tmp_ptr, &(ipv4->GateWay)) < 0) {
4186 LOGE("inet_pton() fail.");
4187 err = -1;
4188 goto exit;
4189 }
4190
4191 //log_hex("IPv4", &(ipv4->GateWay), sizeof(struct in_addr));
4192 } else {
4193 if(str_2_ipv6(tmp_ptr, &(ipv6->GateWay))) {
4194 LOGE("str_2_ipv6() fail.");
4195 err = -1;
4196 goto exit;
4197 }
4198
4199 //log_hex("IPv6", &(ipv6->GateWay), 16);
4200 }
4201 }
4202
4203 err = at_tok_nextstr(&line, &tmp_ptr); // prim_DNS
4204 if (err < 0)
4205 {
4206 goto exit;
4207 }
4208 if(!str_empty(tmp_ptr)) { // No found Primary DNS
4209 if(is_ipv4(tmp_ptr)) {
4210 if(inet_pton(AF_INET, tmp_ptr, &(ipv4->PrimaryDNS)) < 0) {
4211 LOGE("inet_pton() fail.");
4212 err = -1;
4213 goto exit;
4214 }
4215
4216 //log_hex("IPv4", &(ipv4->PrimaryDNS), sizeof(struct in_addr));
4217 } else {
4218 if(str_2_ipv6(tmp_ptr, &(ipv6->PrimaryDNS))) {
4219 LOGE("str_2_ipv6() fail.");
4220 err = -1;
4221 goto exit;
4222 }
4223
4224 //log_hex("IPv6", &(ipv6->PrimaryDNS), 16);
4225 }
4226 }
4227
4228 err = at_tok_nextstr(&line, &tmp_ptr); // sec_DNS
4229 if (err < 0)
4230 {
4231 goto exit;
4232 }
4233 if(!str_empty(tmp_ptr)) { // No found Secondary DNS
4234 if(is_ipv4(tmp_ptr)) {
4235 if(inet_pton(AF_INET, tmp_ptr, &(ipv4->SecondaryDNS)) < 0) {
4236 LOGE("inet_pton() fail.");
4237 err = -1;
4238 goto exit;
4239 }
4240
4241 //log_hex("IPv4", &(ipv4->SecondaryDNS), sizeof(struct in_addr));
4242 } else {
4243 if(str_2_ipv6(tmp_ptr, &(ipv6->SecondaryDNS))) {
4244 LOGE("str_2_ipv6() fail.");
4245 err = -1;
4246 goto exit;
4247 }
4248
4249 //log_hex("IPv6", &(ipv6->SecondaryDNS), 16);
4250 }
4251 }
4252
4253 lines_ptr = lines_ptr->p_next;
4254 }
4255
4256exit:
4257 at_response_free(response);
4258 return err;
4259}
4260
b.liu9e8584b2024-11-06 19:21:28 +08004261#if 0
liubin281ac462023-07-19 14:22:54 +08004262/*
4263AT+CGCONTRDP
4264+CGCONTRDP: 1,5,"cmnet.MNC000.MCC460.GPRS","10.156.238.86","","223.87.253.100","223.87.253.253","","",0,0
4265+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
4266+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
4267OK
4268
4269*/
4270static int apn_state_get(list_node_t **apn_list)
4271{
4272 ATResponse *response = NULL;
4273 int err = 0;
4274 *apn_list = list_create(NULL);
4275 if(*apn_list == NULL)
4276 {
4277 LOG("list_create() fail.");
4278 return -1;
4279 }
4280
4281 err = at_send_command_multiline("AT+CGCONTRDP", "+CGCONTRDP:", &response);
4282 if (err < 0 || response->success == 0 || !response->p_intermediates){
4283 goto exit;
4284 }
4285 ATLine* lines_ptr = response->p_intermediates;
4286 char *line = NULL;
4287 int tmp_int;
4288 char *tmp_ptr = NULL;
4289 int cid_current = 0;
4290 info_apn_ip_t *apn = NULL;
4291 while(lines_ptr)
4292 {
4293 line = lines_ptr->line;
4294 err = at_tok_start(&line);
4295 if (err < 0)
4296 {
4297 goto exit;
4298 }
4299
4300 err = at_tok_nextint(&line, &tmp_int); // cid
4301 if (err < 0)
4302 {
4303 goto exit;
4304 }
4305
4306 if(tmp_int != 1 && tmp_int != 8) { // Not process cid 1 and 8.
4307 if(cid_current != tmp_int) { // New cid.
4308 apn = (info_apn_ip_t*)malloc(sizeof(info_apn_ip_t));
4309 if(apn == NULL) {
4310 goto exit;
4311 }
4312 memset(apn, 0, sizeof(info_apn_ip_t));
4313 apn->cid = tmp_int;
4314 cid_current = tmp_int;
4315
4316 list_add(*apn_list, apn);
4317 }
4318 err = at_tok_nextint(&line, &tmp_int); // bearer_id
4319 if (err < 0)
4320 {
4321 goto exit;
4322 }
4323 err = at_tok_nextstr(&line, &tmp_ptr); // APN
4324 if (err < 0)
4325 {
4326 goto exit;
4327 }
4328
4329 err = at_tok_nextstr(&line, &tmp_ptr); // IP
4330 if (err < 0 || str_empty(tmp_ptr))
4331 {
4332 goto exit;
4333 }
4334 if(is_ipv4(tmp_ptr)) {
4335 apn->ipv4_valid = true;
4336 memcpy(apn->ipv4, tmp_ptr, strlen(tmp_ptr));
4337 } else {
4338 apn->ipv6_valid = true;
4339 uint8 tmp_ipv6[16];
4340 if(str_2_ipv6(tmp_ptr, tmp_ipv6)) {
4341 LOGE("str_2_ipv6() fail.");
4342 err = -1;
4343 goto exit;
4344 }
4345
b.liu9e8584b2024-11-06 19:21:28 +08004346 if(inet_ntop(AF_INET6, tmp_ipv6, (char*)apn->ipv6, 50) == NULL) {
liubin281ac462023-07-19 14:22:54 +08004347 err = -1;
4348 LOGE("inet_ntop ipv6 ip fail.");
4349 goto exit;
4350 }
4351 }
4352 }
4353
4354 lines_ptr = lines_ptr->p_next;
4355 }
4356
4357exit:
4358 at_response_free(response);
4359 return err;
4360}
b.liu9e8584b2024-11-06 19:21:28 +08004361#endif
liubin281ac462023-07-19 14:22:54 +08004362
liuyang4d7ac4b2024-11-21 16:25:22 +08004363int req_ceer_call(char *reg, int *cme_err);
liubin281ac462023-07-19 14:22:54 +08004364mbtk_info_err_enum call_pack_req_process(sock_client_info_t* cli_info, mbtk_info_pack_t* pack);
4365mbtk_info_err_enum sms_pack_req_process(sock_client_info_t* cli_info, mbtk_info_pack_t* pack);
4366mbtk_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 -08004367
liubin281ac462023-07-19 14:22:54 +08004368//mbtk wyq for data_call_ex add start
4369void data_call_bootconn_save(int cid, int bootconn);
4370//mbtk wyq for data_call_ex add end
4371
4372//void net_list_free(void *data);
4373// Return MBTK_INFO_ERR_SUCCESS,will call pack_error_send() to send RSP.
4374// Otherwise, do not call pack_error_send().
4375static mbtk_info_err_enum pack_req_process(sock_client_info_t* cli_info, mbtk_info_pack_t* pack)
4376{
4377 if(pack->info_id > MBTK_INFO_ID_CALL_BEGIN && pack->info_id < MBTK_INFO_ID_CALL_END) {
4378 return call_pack_req_process(cli_info, pack);
4379 } else if(pack->info_id > MBTK_INFO_ID_SMS_BEGIN && pack->info_id < MBTK_INFO_ID_SMS_END) {
4380 return sms_pack_req_process(cli_info, pack);
4381 } else if(pack->info_id > MBTK_INFO_ID_PB_BEGIN && pack->info_id < MBTK_INFO_ID_PB_END) {
4382 return pb_pack_req_process(cli_info, pack);
b.liu06559f62024-11-01 18:48:22 +08004383 } else if(pack->info_id > RIL_MSG_ID_ECALL_BEGIN && pack->info_id < RIL_MSG_ID_ECALL_END) {
4384 return ecall_pack_req_process(cli_info, pack);
liubin281ac462023-07-19 14:22:54 +08004385 } else {
4386 mbtk_info_err_enum err = MBTK_INFO_ERR_SUCCESS;
4387 int cme_err = MBTK_INFO_ERR_CME_NON;
4388 switch(pack->info_id)
4389 {
4390 case MBTK_INFO_ID_DEV_IMEI_REQ: // <string> IMEI
4391 {
4392 if(pack->data_len == 0 || pack->data == NULL) // Get IMEI
4393 {
4394 char imei[20] = {0};
4395 if(req_imei_get(imei, &cme_err) || strlen(imei) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4396 {
4397 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4398 err = MBTK_INFO_ERR_CME + cme_err;
4399 } else {
4400 err = MBTK_INFO_ERR_UNKNOWN;
4401 }
4402 LOG("Get IMEI fail.");
4403 }
4404 else
4405 {
4406 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_IMEI_RSP, imei, strlen(imei));
4407 }
4408 }
4409 else // Set IMEI(Unsupport).
4410 {
4411 err = MBTK_INFO_ERR_UNSUPPORTED;
4412 LOG("Unsupport set IMEI.");
4413 }
4414 break;
4415 }
4416 case MBTK_INFO_ID_DEV_SN_REQ: // <string> SN
4417 {
4418 if(pack->data_len == 0 || pack->data == NULL) // Get SN
4419 {
4420 char sn[20] = {0};
4421 if(req_sn_get(sn, &cme_err) || strlen(sn) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4422 {
4423 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4424 err = MBTK_INFO_ERR_CME + cme_err;
4425 } else {
4426 err = MBTK_INFO_ERR_UNKNOWN;
4427 }
4428 LOG("Get SN fail.");
4429 }
4430 else
4431 {
4432 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_SN_RSP, sn, strlen(sn));
4433 }
4434 }
4435 else // Set SN(Unsupport).
4436 {
4437 err = MBTK_INFO_ERR_UNSUPPORTED;
4438 LOG("Unsupport set SN.");
4439 }
4440 break;
4441 }
4442 case MBTK_INFO_ID_DEV_MEID_REQ: // <string> MEID (Only for CDMA)
4443 {
4444 if(pack->data_len == 0 || pack->data == NULL) // Get MEID
4445 {
4446 err = MBTK_INFO_ERR_UNSUPPORTED;
4447 LOG("Support only for CDMA.");
4448 }
4449 else // Set MEID(Unsupport).
4450 {
4451 err = MBTK_INFO_ERR_UNSUPPORTED;
4452 LOG("Unsupport set MEID.");
4453 }
4454 break;
4455 }
4456 case MBTK_INFO_ID_DEV_VERSION_REQ: // <string> VERSION
4457 {
4458 if(pack->data_len == 0 || pack->data == NULL) // Get VERSION
4459 {
4460 char version[50] = {0};
4461 if(req_version_get(version, &cme_err) || strlen(version) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4462 {
4463 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4464 err = MBTK_INFO_ERR_CME + cme_err;
4465 } else {
4466 err = MBTK_INFO_ERR_UNKNOWN;
4467 }
4468 LOG("Get Version fail.");
4469 }
4470 else
4471 {
4472 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_VERSION_RSP, version, strlen(version));
4473 }
4474 }
4475 else // Set VERSION(Unsupport).
4476 {
4477 err = MBTK_INFO_ERR_UNSUPPORTED;
4478 LOG("Unsupport set VERSION.");
4479 }
4480 break;
4481 }
l.yang5b0ff422024-10-29 19:33:35 -07004482 case MBTK_INFO_ID_DEV_MD_VERSION_REQ:
4483 {
4484 if(pack->data_len == 0 || pack->data == NULL) // Get VERSION
4485 {
4486 char version[50] = {0};
4487 if(req_md_version_get(version, &cme_err) || strlen(version) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4488 {
4489 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4490 err = MBTK_INFO_ERR_CME + cme_err;
4491 } else {
4492 err = MBTK_INFO_ERR_UNKNOWN;
4493 }
4494 LOG("Get Version fail.");
4495 }
4496 else
4497 {
4498 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_MD_VERSION_RSP, version, strlen(version));
4499 }
4500 }
4501 else // Set MD_VERSION(Unsupport).
4502 {
4503 err = MBTK_INFO_ERR_UNSUPPORTED;
4504 LOG("Unsupport set MD_VERSION.");
4505 }
4506 break;
4507
4508 }
liubin281ac462023-07-19 14:22:54 +08004509 case MBTK_INFO_ID_DEV_MODEL_REQ: //MODEL
4510 {
4511 if(pack->data_len == 0 || pack->data == NULL) // Get MODEL
4512 {
4513 char model[50] = {0};
4514 if(req_model_get(model, &cme_err) || strlen(model) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4515 {
4516 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4517 err = MBTK_INFO_ERR_CME + cme_err;
4518 } else {
4519 err = MBTK_INFO_ERR_UNKNOWN;
4520 }
4521 LOG("Get model fail.");
4522 }
4523 else
4524 {
4525 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_VERSION_RSP, model, strlen(model));
4526 }
4527 }
4528 else // Set model(Unsupport).
4529 {
4530 err = MBTK_INFO_ERR_UNSUPPORTED;
4531 LOG("Unsupport set model.");
4532 }
4533 break;
4534 }
4535 case MBTK_INFO_ID_DEV_MODEM_REQ: //MODEM
4536 {
4537 if(pack->data_len == 0 || pack->data == NULL) // Get MODEM
4538 {
4539 int modem = -1;
4540 if(req_modem_get(&modem, &cme_err) || modem < 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4541 {
4542 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4543 err = MBTK_INFO_ERR_CME + cme_err;
4544 } else {
4545 err = MBTK_INFO_ERR_UNKNOWN;
4546 }
4547 LOG("Get modem fail.");
4548 }
4549 else
4550 {
4551 uint8 modem_type = (uint8)modem;
4552 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_MODEM_RSP, &modem_type, sizeof(uint8));
4553 }
4554 }
4555 else // Set modem
4556 {
4557 mbtk_modem_info_t *modem = (mbtk_modem_info_t *)pack->data;
4558 if(pack->data_len != sizeof(mbtk_modem_info_t))
4559 {
4560 err = MBTK_INFO_ERR_REQ_PARAMETER;
4561 LOG("Set modem error.");
4562 break;
4563 }
q.huanga0261872025-08-01 19:44:59 +08004564 at_cfun_command = true; //LYNQ_ASR_BUG2001_Q.HUANG_20250731_ADD
liubin281ac462023-07-19 14:22:54 +08004565 if(req_modem_set(modem, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4566 {
4567 LOG("Set modem fail.");
4568 err = MBTK_INFO_ERR_FORMAT;
4569 } else {
4570 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_MODEM_RSP, NULL, 0);
4571 }
4572 }
4573 break;
4574 }
4575 case MBTK_INFO_ID_DEV_TIME_REQ: // <uint8><string> YYYY-MM-DD-HH:MM:SS
4576 {
4577 if(pack->data_len == 0 || pack->data == NULL) // Get Time
4578 {
4579 int type = -1;
4580 if(req_time_get(&type, &cme_err) || type < 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4581 {
4582 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4583 err = MBTK_INFO_ERR_CME + cme_err;
4584 } else {
4585 err = MBTK_INFO_ERR_UNKNOWN;
4586 }
4587 LOG("Get Time fail.");
4588 }
4589 else
4590 {
4591 uint8 time_type = (uint8)type;
4592 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_TIME_RSP, &time_type, sizeof(uint8));
4593 }
4594 }
4595 else // Set Time
4596 {
4597 if(pack->data_len == sizeof(uint8)) {
4598 if(req_time_set(*(pack->data), NULL, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4599 {
4600 LOG("Set Time fail.");
4601 err = MBTK_INFO_ERR_FORMAT;
4602 } else {
4603 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_TIME_RSP, NULL, 0);
4604 }
4605 } else {
4606 char time_ptr[100] = {0};
4607 memcpy(time_ptr, pack->data + sizeof(uint8), pack->data_len - sizeof(uint8));
4608 if(req_time_set(*(pack->data), time_ptr, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4609 {
4610 LOG("Set Time fail.");
4611 err = MBTK_INFO_ERR_FORMAT;
4612 } else {
4613 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_TIME_RSP, NULL, 0);
4614 }
4615 }
4616 }
4617 break;
4618 }
b.liubaa41e12024-07-19 15:07:24 +08004619 case MBTK_INFO_ID_DEV_CELL_TIME_REQ: // <string> YYYY-MM-DD-HH:MM:SS
liubin281ac462023-07-19 14:22:54 +08004620 {
4621 if(pack->data_len == 0 || pack->data == NULL) // Get Time
4622 {
4623 char time[100];
4624 if(req_net_time_get(time, &cme_err) || strlen(time) == 0 || 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 Time fail.");
4632 }
4633 else
4634 {
4635 char time_ser[100]={0};
4636 memcpy(time_ser,time,strlen(time));
b.liubaa41e12024-07-19 15:07:24 +08004637 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_CELL_TIME_RSP, time_ser, strlen(time_ser));
liubin281ac462023-07-19 14:22:54 +08004638 }
4639 }
4640 else // Set Time
4641 {
4642 err = MBTK_INFO_ERR_UNSUPPORTED;
4643 LOG("Unsupport set TIME.");
4644 }
4645 break;
4646 }
4647 case MBTK_INFO_ID_DEV_VOLTE_REQ: // <uint8> 0:Close 1:Open
4648 {
4649 if(pack->data_len == 0 || pack->data == NULL) // Get VoLTE state.
4650 {
4651 int state;
4652 if(req_volte_get(&state, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4653 {
4654 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4655 err = MBTK_INFO_ERR_CME + cme_err;
4656 } else {
4657 err = MBTK_INFO_ERR_UNKNOWN;
4658 }
4659 LOG("Get VoLTE state fail.");
4660 }
4661 else
4662 {
4663 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_VOLTE_RSP, &state, sizeof(uint8));
4664 }
4665 }
4666 else // Set VoLTE state.
4667 {
4668 uint8 on = *(pack->data);
4669 if(pack->data_len != sizeof(uint8) || (on != 0 && on != 1))
4670 {
4671 err = MBTK_INFO_ERR_REQ_PARAMETER;
4672 LOG("Set VOLTE parameter error.");
4673 break;
4674 }
4675
4676 if(req_volte_set(on, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4677 {
4678 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4679 err = MBTK_INFO_ERR_CME + cme_err;
4680 } else {
4681 err = MBTK_INFO_ERR_UNKNOWN;
4682 }
4683 LOG("Set VoLTE state fail.");
4684 }
4685 else
4686 {
4687 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_VOLTE_RSP, NULL, 0);
4688
4689 // Restart is required to take effect.
4690 LOG("Will reboot system...");
4691 }
4692 }
4693 break;
4694 }
4695 case MBTK_INFO_ID_DEV_TEMP_REQ: // <string> Temperature
4696 {
4697 if(pack->data_len == sizeof(uint8) && pack->data) {
r.xiao2102d762024-06-07 03:10:38 -07004698 mbtk_thermal_info_t temp;
4699 memset(&temp, 0, sizeof(mbtk_thermal_info_t));
liubin281ac462023-07-19 14:22:54 +08004700 if(req_temp_get(*(pack->data), &temp, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4701 {
4702 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4703 err = MBTK_INFO_ERR_CME + cme_err;
4704 } else {
4705 err = MBTK_INFO_ERR_UNKNOWN;
4706 }
4707 LOG("Get temperature fail.");
4708 }
4709 else
4710 {
r.xiao2102d762024-06-07 03:10:38 -07004711 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_TEMP_RSP, &temp, sizeof(mbtk_thermal_info_t));
liubin281ac462023-07-19 14:22:54 +08004712 }
4713 } else {
4714 err = MBTK_INFO_ERR_FORMAT;
4715 LOG("Unsupport get Temperature.");
4716 }
4717 break;
4718 }
4719 case MBTK_INFO_ID_SIM_PLMN_REQ: // plmn
4720 {
4721 if(pack->data_len == 0 || pack->data == NULL) // plmn
4722 {
4723 mbtk_plmn_info plmn;
4724 if(req_plmn_get(&plmn, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4725 {
4726 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4727 err = MBTK_INFO_ERR_CME + cme_err;
4728 } else {
4729 err = MBTK_INFO_ERR_UNKNOWN;
4730 }
4731 LOG("Get PLMN fail.");
4732 }
4733 else
4734 {
4735 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_PLMN_RSP, &plmn, sizeof(mbtk_plmn_info));
4736 }
4737 }
4738 else // Set
4739 {
4740 err = MBTK_INFO_ERR_UNSUPPORTED;
4741 LOG("Set sim state fail.");
4742 }
4743 break;
4744 }
4745 case MBTK_INFO_ID_SIM_STATE_REQ: // mbtk_sim_state_enum
4746 {
4747 if(pack->data_len == 0 || pack->data == NULL) // Get sim state.
4748 {
4749 uint8 sim_state = (uint8)getSIMStatus();
4750 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_STATE_RSP, &sim_state, sizeof(uint8));
4751 }
4752 else // Set
4753 {
4754 err = MBTK_INFO_ERR_UNSUPPORTED;
4755 LOG("Set sim state fail.");
4756 }
4757 break;
4758 }
4759 case MBTK_INFO_ID_SIM_STYPE_REQ: // mbtk_sim_card_type_enum
4760 {
4761 if(pack->data_len == 0 || pack->data == NULL) // Get sim card type
4762 {
4763 uint8 sim_card_type;
4764 if(req_sim_card_type_get(&sim_card_type, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4765 {
4766 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4767 err = MBTK_INFO_ERR_CME + cme_err;
4768 } else {
4769 err = MBTK_INFO_ERR_UNKNOWN;
4770 }
4771 LOG("Get IMSI fail.");
4772 }
4773 else
4774 {
4775 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_STYPE_RSP, &sim_card_type, sizeof(uint8));
4776 }
4777 }
4778 else // Set
4779 {
4780 err = MBTK_INFO_ERR_UNSUPPORTED;
4781 LOG("Set sim state fail.");
4782 }
4783 break;
4784 }
4785 case MBTK_INFO_ID_SIM_PINPUK_TIMES_REQ: // mbtk_pin_puk_last_times
4786 {
4787 if(pack->data_len == 0 || pack->data == NULL) // Get sim card type
4788 {
4789 mbtk_pin_puk_last_times pin_puk_last_times;
4790 if(req_pin_puk_last_times_get(&pin_puk_last_times, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4791 {
4792 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4793 err = MBTK_INFO_ERR_CME + cme_err;
4794 } else {
4795 err = MBTK_INFO_ERR_UNKNOWN;
4796 }
4797 LOG("Get IMSI fail.");
4798 }
4799 else
4800 {
4801 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_PINPUK_TIMES_RSP, &pin_puk_last_times, sizeof(mbtk_pin_puk_last_times));
4802 }
4803 }
4804 else // Set
4805 {
4806 err = MBTK_INFO_ERR_UNSUPPORTED;
4807 LOG("Set sim state fail.");
4808 }
4809 break;
4810 }
4811 case MBTK_INFO_ID_SIM_ENABLE_PIN_REQ: // <string> PIN
4812 {
4813 if(pack->data_len == 0 || pack->data == NULL) // Enable PIN
4814 {
yq.wang586a0df2024-10-24 20:10:37 -07004815 mbtk_pin_state_enum pin_state = MBTK_PIN_DISABLE;
4816 if(req_get_pin_state(&pin_state, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4817 {
4818 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4819 err = MBTK_INFO_ERR_CME + cme_err;
4820 } else {
4821 err = MBTK_INFO_ERR_UNKNOWN;
4822 }
4823 LOGE("Get pin state fail.");
4824 }
4825 else
4826 {
4827 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_ENABLE_PIN_RSP, &pin_state, sizeof(mbtk_pin_state_enum));
4828 }
liubin281ac462023-07-19 14:22:54 +08004829 }
4830 else // Enable PIN
4831 {
4832 mbtk_enable_pin_info *pin = NULL;
4833 pin = (mbtk_enable_pin_info *)pack->data;
4834 if(req_pin_enable(pin, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4835 {
4836 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4837 err = MBTK_INFO_ERR_CME + cme_err;
4838 } else {
4839 err = MBTK_INFO_ERR_UNKNOWN;
4840 }
4841 LOG("Get IMSI fail.");
4842 }
4843 else
4844 {
4845 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_ENABLE_PIN_RSP, NULL, 0);
4846 }
4847 }
4848 break;
4849 }
4850 case MBTK_INFO_ID_SIM_PIN_REQ: // <string> PIN
4851 {
4852 if(pack->data_len == 0 || pack->data == NULL) // PIN
4853 {
4854 err = MBTK_INFO_ERR_UNSUPPORTED;
4855 LOG("Unsupport GET PIN.");
4856 }
4857 else // Set PIN
4858 {
4859 char pin[16] = {0};
4860 memcpy(pin, pack->data, pack->data_len);
4861 if(req_pin_verify(pin, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4862 {
4863 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4864 err = MBTK_INFO_ERR_CME + cme_err;
4865 } else {
4866 err = MBTK_INFO_ERR_UNKNOWN;
4867 }
4868 LOG("Set PIN fail.");
4869 }
4870 else
4871 {
4872 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_PIN_RSP, NULL, 0);
4873 }
4874 }
4875 break;
4876 }
4877 case MBTK_INFO_ID_SIM_PUK_REQ: // <string> PUK
4878 {
4879 if(pack->data_len == 0 || pack->data == NULL)
4880 {
4881 err = MBTK_INFO_ERR_UNSUPPORTED;
4882 LOG("Unsupport.");
4883 }
4884 else // change PIN
4885 {
4886 mbtk_unlock_pin_info *pin_info = NULL;
4887 pin_info = (mbtk_unlock_pin_info *)pack->data;
4888 if(req_puk_unlock_pin(pin_info, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4889 {
4890 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4891 err = MBTK_INFO_ERR_CME + cme_err;
4892 } else {
4893 err = MBTK_INFO_ERR_UNKNOWN;
4894 }
4895 LOG("Get IMSI fail.");
4896 }
4897 else
4898 {
4899 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_CHANGE_PIN_RSP, NULL, 0);
4900 }
4901 }
4902 break;
4903 }
4904 case MBTK_INFO_ID_SIM_CHANGE_PIN_REQ: // <string> <string> old_PIN new_PIN
4905 {
4906 if(pack->data_len == 0 || pack->data == NULL)
4907 {
4908 err = MBTK_INFO_ERR_UNSUPPORTED;
4909 LOG("Unsupport.");
4910 }
4911 else // change PIN
4912 {
4913 mbtk_change_pin_info *pin_info = NULL;
4914 pin_info = (mbtk_change_pin_info *)pack->data;
4915 if(req_pin_change(pin_info, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4916 {
4917 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4918 err = MBTK_INFO_ERR_CME + cme_err;
4919 } else {
4920 err = MBTK_INFO_ERR_UNKNOWN;
4921 }
4922 LOG("Get IMSI fail.");
4923 }
4924 else
4925 {
4926 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_CHANGE_PIN_RSP, NULL, 0);
4927 }
4928 }
4929 break;
4930 }
4931 case MBTK_INFO_ID_SIM_IMSI_REQ: // <string> IMSI
4932 {
4933 if(pack->data_len == 0 || pack->data == NULL) // Get IMSI
4934 {
4935 char imsi[20] = {0};
4936 if(req_imsi_get(imsi, &cme_err) || strlen(imsi) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4937 {
4938 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4939 err = MBTK_INFO_ERR_CME + cme_err;
4940 } else {
4941 err = MBTK_INFO_ERR_UNKNOWN;
4942 }
4943 LOG("Get IMSI fail.");
4944 }
4945 else
4946 {
4947 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_IMSI_RSP, imsi, strlen(imsi));
4948 }
4949 }
4950 else // Set IMSI(Unsupport).
4951 {
4952 err = MBTK_INFO_ERR_UNSUPPORTED;
4953 LOG("Unsupport set IMSI.");
4954 }
4955 break;
4956 }
4957 case MBTK_INFO_ID_SIM_ICCID_REQ: // <string> ICCID
4958 {
4959 if(pack->data_len == 0 || pack->data == NULL) // Get ICCID
4960 {
4961 //log_hex("pack", pack, sizeof(mbtk_info_pack_t));
4962 //sleep(1);
4963 char iccid[50] = {0};
4964 if(req_iccid_get(iccid, &cme_err) || strlen(iccid) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4965 {
4966 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4967 err = MBTK_INFO_ERR_CME + cme_err;
4968 } else {
4969 err = MBTK_INFO_ERR_UNKNOWN;
4970 }
4971 LOG("Get ICCID fail.");
4972 }
4973 else
4974 {
4975 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_ICCID_RSP, iccid, strlen(iccid));
4976 }
4977 }
4978 else // Set ICCID(Unsupport).
4979 {
4980 err = MBTK_INFO_ERR_UNSUPPORTED;
4981 LOG("Unsupport set ICCID.");
4982 }
4983 break;
4984 }
4985 case MBTK_INFO_ID_SIM_PN_REQ: // <string> Phone Number
4986 {
4987 if(pack->data_len == 0 || pack->data == NULL) // Get Phone Number
4988 {
4989 //log_hex("pack", pack, sizeof(mbtk_info_pack_t));
4990 //sleep(1);
4991 char phone_number[50] = {0};
4992 if(req_phone_number_get(phone_number, &cme_err) || strlen(phone_number) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4993 {
4994 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4995 err = MBTK_INFO_ERR_CME + cme_err;
4996 } else {
4997 err = MBTK_INFO_ERR_UNKNOWN;
4998 }
4999 LOG("Get Phone Number fail.");
5000 }
5001 else
5002 {
5003 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_PN_RSP, phone_number, strlen(phone_number));
5004 }
5005 }
5006 else // Set Phone Number(Unsupport).
5007 {
5008 err = MBTK_INFO_ERR_UNSUPPORTED;
5009 LOG("Unsupport set Phone Number.");
5010 }
5011 break;
5012 }
5013 case MBTK_INFO_ID_NET_SEL_MODE_REQ: // <mbtk_net_info_t> Operator
5014 {
5015 if(pack->data_len == 0 || pack->data == NULL) // Get
5016 {
5017 mbtk_net_info_t info;
5018 memset(&info, 0, sizeof(mbtk_net_info_t));
5019 if(req_net_sel_mode_get(&info, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5020 {
5021 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5022 err = MBTK_INFO_ERR_CME + cme_err;
5023 } else {
5024 err = MBTK_INFO_ERR_UNKNOWN;
5025 }
5026 LOG("Get Net select mode fail.");
5027 }
5028 else
5029 {
5030 LOG("NET : %d, %d, %d, %d", info.net_sel_mode, info.net_type, info.net_state, info.plmn);
5031 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_SEL_MODE_RSP, &info, sizeof(mbtk_net_info_t));
5032 }
5033 }
5034 else // Set
5035 {
5036 //LOG("1 pack-%p,data-%p,data_len-%d", pack, pack->data, pack->data_len);
5037 //log_hex("pack", pack, sizeof(mbtk_info_pack_t));
5038 //log_hex("data", pack->data, pack->data_len);
5039
5040 mbtk_net_info_t* info = (mbtk_net_info_t*)pack->data;//(mbtk_net_info_t*)mbtk_info_pack_data_get(pack, NULL);
5041 if(info == NULL) {
5042 err = MBTK_INFO_ERR_FORMAT;
5043 LOG("Get Net select mode fail.");
5044 } else {
5045 LOG("NET : %d, %d, %d", info->net_sel_mode, info->net_type, info->plmn);
5046 if(req_net_sel_mode_set(info, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON) {
5047 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5048 err = MBTK_INFO_ERR_CME + cme_err;
5049 } else {
5050 err = MBTK_INFO_ERR_UNKNOWN;
5051 }
5052 LOG("Get Net select mode fail.");
5053 } else {
5054 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_SEL_MODE_RSP, NULL, 0);
5055 }
5056 }
5057 }
5058 break;
5059 }
5060 case MBTK_INFO_ID_NET_AVAILABLE_REQ:// sel_mode(uint8)type(uint8)plmn(uint32)...sel_mode(uint8)type(uint8)plmn(uint32)
5061 {
5062 if(pack->data_len == 0 || pack->data == NULL) // Get Available Net.
5063 {
5064 int buffer_size;
5065 uint8 buffer[SOCK_MSG_LEN_MAX];
5066 memset(buffer, 0, SOCK_MSG_LEN_MAX);
5067 if((buffer_size = req_available_net_get(buffer, &cme_err)) <= 0 || cme_err != MBTK_INFO_ERR_CME_NON)
5068 {
5069 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5070 err = MBTK_INFO_ERR_CME + cme_err;
5071 } else {
5072 err = MBTK_INFO_ERR_UNKNOWN;
5073 }
5074 LOG("Get Available Net fail.");
5075 }
5076 else
5077 {
5078 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_AVAILABLE_RSP, buffer, buffer_size);
5079 }
5080 }
5081 else // Set Available Net(Unsupport).
5082 {
5083 err = MBTK_INFO_ERR_UNSUPPORTED;
5084 LOG("Unsupport set available net.");
5085 }
5086 break;
5087 }
5088 case MBTK_INFO_ID_NET_BAND_REQ:
5089 {
5090 if(pack->data_len == 0 || pack->data == NULL)
5091 {
5092 err = MBTK_INFO_ERR_REQ_PARAMETER;
5093 LOG("No data found.");
5094 }
5095 else // Get support/current bands.
5096 {
5097 if(pack->data_len == sizeof(uint8)) {
5098 if(*(pack->data)) { // Get current bands.
5099 mbtk_band_info_t band;
b.liu288093c2024-05-09 17:02:57 +08005100 memset(&band, 0x0, sizeof(mbtk_band_info_t));
liubin281ac462023-07-19 14:22:54 +08005101 if(req_band_get(&band, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5102 {
5103 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5104 err = MBTK_INFO_ERR_CME + cme_err;
5105 } else {
5106 err = MBTK_INFO_ERR_UNKNOWN;
5107 }
5108 LOG("Get net band fail.");
5109 }
5110 else
5111 {
5112 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_BAND_RSP, &band, sizeof(mbtk_band_info_t));
5113 }
5114 } else { // Get support bands.
5115 band_support_get();
5116 if(band_support.net_pref != 0)
5117 {
5118 err = MBTK_INFO_ERR_UNKNOWN;
5119 LOG("Get support bands fail.");
5120 }
5121 else
5122 {
5123 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_BAND_RSP, &band_support, sizeof(mbtk_band_info_t));
5124 }
5125 }
5126 } else { // Set current bands.
5127 mbtk_band_info_t* band = (mbtk_band_info_t*)pack->data;
5128 if(pack->data_len != sizeof(mbtk_band_info_t))
5129 {
5130 err = MBTK_INFO_ERR_REQ_PARAMETER;
5131 LOG("Set net band error.");
5132 break;
5133 }
5134
5135 if(req_band_set(band, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5136 {
5137 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5138 err = MBTK_INFO_ERR_CME + cme_err;
5139 } else {
5140 err = MBTK_INFO_ERR_UNKNOWN;
5141 }
5142 LOG("Set net band fail.");
5143 }
5144 else
5145 {
5146 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_BAND_RSP, NULL, 0);
5147 }
5148 }
5149 }
5150 break;
5151 }
5152 case MBTK_INFO_ID_NET_CELL_REQ: // mbtk_cell_info_t[]
5153 {
5154 if(pack->data_len == 0 || pack->data == NULL) // Get net cell.
5155 {
5156 if(req_cell_info_get(&cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5157 {
5158 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5159 err = MBTK_INFO_ERR_CME + cme_err;
5160 } else {
5161 err = MBTK_INFO_ERR_UNKNOWN;
5162 }
5163 LOG("Get net cell fail.");
5164 }
5165 else
5166 {
5167 LOG("req_cell_info_get() success,cell number: %d", cell_info.cell_num);
5168 //sleep(1);
5169 // mbtK_cell_pack_info_t
5170 if(cell_info.cell_num > 0 && cell_info.cell_num <= CELL_NUM_MAX && cell_info.type >= 0 && cell_info.type <= 2) {
5171 uint8 *data = (uint8*)malloc(sizeof(uint8) + sizeof(mbtk_cell_info_t) * cell_info.cell_num);
5172 if(data == NULL){
5173 err = MBTK_INFO_ERR_MEMORY;
5174 LOG("Get net cell fail.");
5175 } else {
5176 *data = cell_info.type; // Set network type.
5177 // Copy cell info item.
5178 #if 0
5179 int i = 0;
5180 while(i < cell_info.cell_num) {
5181 memcpy(data + sizeof(uint8) + sizeof(mbtk_cell_info_t) * i,
5182 &(cell_info.cell[i]),
5183 sizeof(mbtk_cell_info_t));
5184 i++;
5185 }
5186 #else
5187 memcpy(data + sizeof(uint8),
5188 &(cell_info.cell),
5189 sizeof(mbtk_cell_info_t) * cell_info.cell_num);
5190 #endif
5191 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_CELL_RSP, data, sizeof(uint8) + sizeof(mbtk_cell_info_t) * cell_info.cell_num);
5192 free(data);
5193 }
yq.wang31f71842024-12-27 14:07:01 +08005194 }else if(cell_info.cell_num == 0) {
5195 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_CELL_RSP, &cell_info.type, sizeof(uint8));
5196 }else {
liubin281ac462023-07-19 14:22:54 +08005197 err = MBTK_INFO_ERR_UNKNOWN;
5198 LOG("Get net cell fail.");
5199 }
5200 }
5201 }
5202 else // Lock cell
5203 {
q.huang8a993672025-06-24 16:19:46 +08005204 char *mem = (char*)(pack->data);
5205 int len = pack->data_len;
5206 char reg[100] = {0};
5207 printf("mem:%s, len:%d", pack->data, pack->data_len);
liubin281ac462023-07-19 14:22:54 +08005208
q.huang8a993672025-06-24 16:19:46 +08005209 if(req_cell_info_set(mem, reg, len, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5210 {
5211 // printf("cpms_set fail\n");
5212 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5213 err = MBTK_INFO_ERR_CME + cme_err;
5214 } else {
5215 err = MBTK_INFO_ERR_UNKNOWN;
5216 }
5217 // LOG("Set req_cell_info_set fail.");
5218 }
5219 else
5220 {
liubin281ac462023-07-19 14:22:54 +08005221
q.huang8a993672025-06-24 16:19:46 +08005222 printf("req_cell_info_set success, reg:%s\n", reg);
5223 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_CELL_RSP, reg, strlen(reg));
liubin281ac462023-07-19 14:22:54 +08005224
q.huang8a993672025-06-24 16:19:46 +08005225 // Restart is required to take effect.
5226 LOG("Will reboot system...");
5227 }
liubin281ac462023-07-19 14:22:54 +08005228 }
5229 break;
5230 }
5231 case MBTK_INFO_ID_NET_RADIO_REQ: // <uint8> 0:OFF 1:ON
5232 {
5233 if(pack->data_len == 0 || pack->data == NULL) // Get
5234 {
5235 uint8 radio_on = (uint8)isRadioOn();
5236 if(radio_on < 0)
5237 {
5238 err = MBTK_INFO_ERR_UNKNOWN;
5239 LOG("Get radio state fail.");
5240 }
5241 else
5242 {
5243 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_RADIO_RSP, &radio_on, sizeof(uint8));
5244 }
5245 }
5246 else // Set
5247 {
yq.wangd58f71e2024-08-21 23:45:31 -07005248 at_cfun_command = true;
liubin281ac462023-07-19 14:22:54 +08005249 uint8 radio_on = *(pack->data);
5250 if(radio_on != 0 && radio_on != 1)
5251 {
5252 err = MBTK_INFO_ERR_REQ_PARAMETER;
5253 LOG("Set radio state fail.");
5254 }
5255 else
5256 {
5257 setRadioPower(radio_on);
5258 if((radio_on && net_info.radio_state == MBTK_RADIO_STATE_ON)
5259 || (!radio_on && net_info.radio_state == MBTK_RADIO_STATE_OFF)) {
5260 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_RADIO_RSP, NULL, 0);
5261 } else {
5262 err = MBTK_INFO_ERR_UNKNOWN;
5263 LOG("Set radio state fail.");
5264 }
5265 }
5266 }
5267 break;
5268 }
5269 case MBTK_INFO_ID_NET_SIGNAL_REQ: // mbtk_signal_info_t
5270 {
5271 if(pack->data_len == 0 || pack->data == NULL) // Get net signal.
5272 {
5273 mbtk_signal_info_t signal;
5274 memset(&signal, 0, sizeof(mbtk_signal_info_t));
5275 if(req_net_signal_get(&signal, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5276 {
5277 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5278 err = MBTK_INFO_ERR_CME + cme_err;
5279 } else {
5280 err = MBTK_INFO_ERR_UNKNOWN;
5281 }
5282 LOG("Get net signal fail.");
5283 }
5284 else
5285 {
5286 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_SIGNAL_RSP, &signal, sizeof(mbtk_signal_info_t));
5287 }
5288 }
5289 else // Set
5290 {
5291 err = MBTK_INFO_ERR_UNSUPPORTED;
5292 LOG("Set net signal fail.");
5293 }
5294 break;
5295 }
5296 case MBTK_INFO_ID_NET_REG_REQ: // mbtk_net_reg_info_t
5297 {
5298 if(pack->data_len == 0 || pack->data == NULL) // Get net reg.
5299 {
5300 mbtk_net_reg_info_t reg;
5301 memset(&reg, 0, sizeof(mbtk_net_reg_info_t));
5302 if(req_net_reg_get(&reg, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5303 {
5304 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5305 err = MBTK_INFO_ERR_CME + cme_err;
5306 } else {
5307 err = MBTK_INFO_ERR_UNKNOWN;
5308 }
5309 LOG("Get net reg fail.");
5310 }
5311 else
5312 {
5313 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_REG_RSP, &reg, sizeof(mbtk_net_reg_info_t));
5314 }
5315 }
5316 else // Set
5317 {
5318 err = MBTK_INFO_ERR_UNSUPPORTED;
5319 LOG("Set net reg fail.");
5320 }
5321 break;
5322 }
5323 case MBTK_INFO_ID_NET_APN_REQ: // mbtk_apn_info_t
5324 {
5325 if(pack->data_len == 0 || pack->data == NULL) // Get APN
5326 {
5327 uint8 buff[SOCK_MSG_LEN_MAX];
5328 memset(buff, 0, SOCK_MSG_LEN_MAX);
5329 int data_len = 0;
5330 if(req_apn_get(buff, &data_len, &cme_err) || data_len <= 0 || cme_err != MBTK_INFO_ERR_CME_NON)
5331 {
5332 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5333 err = MBTK_INFO_ERR_CME + cme_err;
5334 } else {
5335 err = MBTK_INFO_ERR_UNKNOWN;
5336 }
5337 LOG("Get APN fail.");
5338 }
5339 else
5340 {
5341 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_APN_RSP, buff, data_len);
5342 }
5343 }
5344 else // Set
5345 {
5346 // <cid[1]><ip_type[1]><apn_len[2]><apn><user_len[2]><user><pass_len[2]><pass><auth_len[2]><auth>
5347 const uint8* ptr = pack->data;
5348 mbtk_apn_info_t apn;
5349 int len;
5350 memset(&apn, 0, sizeof(mbtk_apn_info_t));
5351 // cid
5352 apn.cid = *ptr++;
5353
5354 // ip_type
5355 apn.ip_type = (mbtk_ip_type_enum)(*ptr++);
5356
5357 // apn
5358 len = byte_2_uint16(ptr, false);
5359 ptr += sizeof(uint16);
5360 if(len > 0) {
5361 memcpy(apn.apn, ptr, len);
5362 ptr += len;
5363 }
5364
5365 // user
5366 len = byte_2_uint16(ptr, false);
5367 ptr += sizeof(uint16);
5368 if(len > 0) {
5369 memcpy(apn.user, ptr, len);
5370 ptr += len;
5371 }
5372
5373 // pass
5374 len = byte_2_uint16(ptr, false);
5375 ptr += sizeof(uint16);
5376 if(len > 0) {
5377 memcpy(apn.pass, ptr, len);
5378 ptr += len;
5379 }
5380
5381 // auth
5382 len = byte_2_uint16(ptr, false);
5383 ptr += sizeof(uint16);
5384 if(len > 0) {
5385 memcpy(apn.auth, ptr, len);
5386 ptr += len;
5387 }
5388
b.liu9e8584b2024-11-06 19:21:28 +08005389 LOGD("APN : %d, %d, %s, %s, %s, %s", apn.cid, apn.ip_type, str_empty(apn.apn) ? "NULL" : (char*)apn.apn,
5390 str_empty(apn.user) ? "NULL" : (char*)apn.user, str_empty(apn.pass) ? "NULL" : (char*)apn.pass, str_empty(apn.auth) ? "NULL" : (char*)apn.auth);
liubin281ac462023-07-19 14:22:54 +08005391 if(req_apn_set(&apn, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5392 {
5393 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5394 err = MBTK_INFO_ERR_CME + cme_err;
5395 } else {
5396 err = MBTK_INFO_ERR_UNKNOWN;
5397 }
5398 LOG("Set APN fail.");
5399 }
5400 else
5401 {
5402 // Save apn.
wangyouqianged88c722023-11-22 16:33:43 +08005403#ifdef MBTK_AF_SUPPORT
5404 if(apn.cid == 1)
5405 {
5406 default_iptype = apn.ip_type;
5407 }
5408#endif
liubin281ac462023-07-19 14:22:54 +08005409 apn_prop_set(&apn);
5410
5411 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_APN_RSP, NULL, 0);
5412 }
5413 }
5414 break;
5415 }
wangyouqiang80487e42024-05-24 15:06:20 +08005416 case MBTK_INFO_ID_NET_QSER_APN_REQ:
5417 {
5418 if(pack->data_len == 0 || pack->data == NULL)
5419 {
5420 uint8 buff[SOCK_MSG_LEN_MAX];
5421 memset(buff, 0, SOCK_MSG_LEN_MAX);
5422 int data_len = 0;
5423 if(mbtk_qser_req_apn_get(buff, &data_len, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5424 {
5425 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5426 err = MBTK_INFO_ERR_CME + cme_err;
5427 } else {
5428 err = MBTK_INFO_ERR_UNKNOWN;
5429 }
5430 LOGE("Get APN fail.");
5431 }
5432 else
5433 {
5434 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_APN_RSP, buff, data_len);
5435 }
5436 }
5437 else
5438 {
b.liu9e8584b2024-11-06 19:21:28 +08005439// const uint8* ptr = pack->data;
wangyouqiang80487e42024-05-24 15:06:20 +08005440 mbtk_apn_info_t apn;
5441 mbtk_apn_req_type_enum req_type = MBTK_APN_REQ_TYPE_SET;
5442 uint8 apn_type[MBTK_QSER_APN_NAME_SIZE] = {0};
5443 int ret = mbtk_strdata_to_apn(pack->data, &apn, apn_type, &req_type);
5444 if(ret < 0)
5445 {
5446 LOGE("mbtk_strdata_to_apn fail. ret = [%d]", ret);
5447 err = MBTK_INFO_ERR_REQ_PARAMETER;
5448 }
5449 else
5450 {
5451 if(req_apn_set(&apn, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5452 {
5453 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5454 err = MBTK_INFO_ERR_CME + cme_err;
5455 } else {
5456 err = MBTK_INFO_ERR_UNKNOWN;
5457 }
5458 LOGE("Set APN fail.");
5459 }
5460 else
5461 {
5462 // Save apn.
5463 apn_prop_set(&apn);
5464 mbtk_qser_apn_save(apn, apn_type, true);
5465 if(req_type == MBTK_APN_REQ_TYPE_ADD)
5466 {
5467 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_QSER_APN_RSP, (void *)&apn.cid, 1);
5468 }
5469 else
5470 {
5471 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_QSER_APN_RSP, NULL, 0);
5472 }
5473 }
5474 }
5475 }
5476
5477 break;
5478 }
b.liu288093c2024-05-09 17:02:57 +08005479 case MBTK_INFO_ID_NET_APN_DEL_REQ:
liuyang0e49d9a2024-04-23 21:04:54 +08005480 {
b.liu288093c2024-05-09 17:02:57 +08005481 if(pack->data_len == 0 || pack->data == NULL)
liuyang0e49d9a2024-04-23 21:04:54 +08005482 {
5483 err = MBTK_INFO_ERR_UNSUPPORTED;
yq.wang9823ddf2024-11-14 02:38:14 -08005484 LOGE("[%s] Unsupported delete apn.", __func__);
liuyang0e49d9a2024-04-23 21:04:54 +08005485 }
b.liu288093c2024-05-09 17:02:57 +08005486 else
liuyang0e49d9a2024-04-23 21:04:54 +08005487 {
liuyang1cefd852024-04-24 18:30:53 +08005488 int profile = pack->data[0];
wangyouqiang80487e42024-05-24 15:06:20 +08005489 if(cid_active[profile] == 1)
liuyang0e49d9a2024-04-23 21:04:54 +08005490 {
yq.wang9823ddf2024-11-14 02:38:14 -08005491 LOGE("[%s] cid[%d] pdp already open.", __func__, profile);
wangyouqiang80487e42024-05-24 15:06:20 +08005492 err = MBTK_INFO_ERR_CID_EXIST;
wangyouqiang13e98402024-05-24 16:07:43 +08005493 break;
wangyouqiang80487e42024-05-24 15:06:20 +08005494 }
5495 if(mbtk_qser_apn_del(profile) < 0)
5496 {
yq.wang9823ddf2024-11-14 02:38:14 -08005497 LOGE("[%s] mbtk_qser_apn_del fail.", __func__);
wangyouqiang80487e42024-05-24 15:06:20 +08005498 err = MBTK_INFO_ERR_REQ_PARAMETER;
liuyang0e49d9a2024-04-23 21:04:54 +08005499 }
5500 else
5501 {
wangyouqiang80487e42024-05-24 15:06:20 +08005502 if(req_apn_del(profile, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5503 {
5504 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5505 err = MBTK_INFO_ERR_CME + cme_err;
5506 } else {
5507 err = MBTK_INFO_ERR_UNKNOWN;
5508 }
yq.wang9823ddf2024-11-14 02:38:14 -08005509 LOGE("[%s] Delete apn fail. [%d]", __func__, err);
wangyouqiang80487e42024-05-24 15:06:20 +08005510 }
5511 else
5512 {
yq.wang9823ddf2024-11-14 02:38:14 -08005513 apn_prop_del(profile);
wangyouqiang80487e42024-05-24 15:06:20 +08005514 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_APN_DEL_RSP, NULL, 0);
5515 }
liuyang0e49d9a2024-04-23 21:04:54 +08005516 }
5517 }
5518 break;
5519 }
liubin281ac462023-07-19 14:22:54 +08005520 case MBTK_INFO_ID_NET_DATA_CALL_REQ:
5521 {
5522 if(pack->data_len == 0 || pack->data == NULL)
5523 {
5524 err = MBTK_INFO_ERR_UNSUPPORTED;
5525 }
5526 else
5527 {
5528 /* <call_type[1]><cid[1]><auto_conn_interval[1]><boot_conn[1]><timeout[1]>
5529 call_type : mbtk_data_call_type_enum
5530 cid : 2 - 7
5531 timeout : second
5532 */
5533 mbtk_data_call_type_enum call_type = (mbtk_data_call_type_enum)pack->data[0];
5534 int cid = pack->data[1];
5535 int reconn = 0;
5536
wangyouqiang80487e42024-05-24 15:06:20 +08005537#if 0
liubin281ac462023-07-19 14:22:54 +08005538 if(cid < MBTK_APN_CID_MIN || cid > MBTK_APN_CID_MAX) {
5539 err = MBTK_INFO_ERR_CID;
5540 break;
5541 }
wangyouqiang80487e42024-05-24 15:06:20 +08005542#endif
5543 if(mbtk_check_cid(cid) < 0)
5544 {
5545 err = MBTK_INFO_ERR_CID;
5546 break;
5547 }
liubin281ac462023-07-19 14:22:54 +08005548
5549 LOG("cid_active[%d] = %d", cid, cid_active[cid]);
5550 memset(&cgact_wait, 0, sizeof(info_cgact_wait_t));
5551 switch(call_type) {
5552 case MBTK_DATA_CALL_START: {
5553 //mbtk wyq for data_call_ex add start
5554 int auto_conn_interval = pack->data[2];
5555 int boot_conn = pack->data[3];
5556 int timeout = pack->data[4];
5557 data_call_bootconn_save(cid, boot_conn);
b.liufe320632024-01-17 20:38:08 +08005558
wangyouqiang13e98402024-05-24 16:07:43 +08005559 mbtk_signal_info_t signal;
5560 memset(&signal, 0xFF, sizeof(mbtk_signal_info_t));
5561 req_net_signal_get(&signal, NULL);
b.liufe320632024-01-17 20:38:08 +08005562
liubin281ac462023-07-19 14:22:54 +08005563 if(cid_active[cid] == 1)
5564 {
5565 err = MBTK_INFO_ERR_CID_EXIST;
5566 break;
5567 }
yq.wang69a51872025-06-17 09:45:12 +08005568#if 0
wangyouqiang80487e42024-05-24 15:06:20 +08005569 if(mbtk_check_default_pdp_state(cid))
5570 {
5571 err = MBTK_INFO_ERR_UNSUPPORTED;
5572 break;
5573 }
yq.wang69a51872025-06-17 09:45:12 +08005574#endif
liubin281ac462023-07-19 14:22:54 +08005575 data_call_reconn:
5576 //mbtk wyq for data_call_ex add end
5577 cgact_wait.waitting = true;
5578 cgact_wait.cid = cid;
5579 cgact_wait.act = true;
5580 if(req_data_call_start(cid, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5581 {
5582 //mbtk wyq for data_call_ex add start
5583 if(reconn < 5 && auto_conn_interval > 0)
5584 {
5585 sleep(auto_conn_interval);
5586 reconn++;
5587 cme_err = MBTK_INFO_ERR_CME_NON;
5588 LOG("data_call restart call.");
5589 goto data_call_reconn;
5590 }
5591 //mbtk wyq for data_call_ex add end
5592 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5593 err = MBTK_INFO_ERR_CME + cme_err;
5594 } else {
5595 err = MBTK_INFO_ERR_UNKNOWN;
5596 }
5597 LOG("%d active fail.", cid);
5598 }
5599 else
5600 {
5601 // Wait for "CONNECT" or "+CGEV:"
5602 if(wait_cgact_complete(timeout)) { // Timeout
5603 err = MBTK_INFO_ERR_TIMEOUT;
5604 break;
5605 }
5606
5607 // Get IP information.
5608 mbtk_ipv4_info_t ipv4;
5609 mbtk_ipv6_info_t ipv6;
5610 memset(&ipv4, 0, sizeof(mbtk_ipv4_info_t));
5611 memset(&ipv6, 0, sizeof(mbtk_ipv6_info_t));
5612 if(req_data_call_state_get(cid, &ipv4, &ipv6, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5613 {
5614 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5615 err = MBTK_INFO_ERR_CME + cme_err;
5616 } else {
5617 err = MBTK_INFO_ERR_UNKNOWN;
5618 }
5619 LOG("Get %d state fail.", cid);
5620 }
5621 else
5622 {
5623 // Config IPv4 address.
wangyouqianged88c722023-11-22 16:33:43 +08005624#ifdef MBTK_AF_SUPPORT
5625 if(cid == 1)
5626 {
5627 //uint8 pdp_data = cid;
5628 //pack_rsp_send(cli_info->fd , MBTK_INFO_ID_IND_PDP_STATE_CHANGE, &pdp_data, sizeof(uint8));
5629 ipv4.valid = false;
5630 ipv6.valid = false;
5631 if(default_iptype == MBTK_IP_TYPE_IP)
5632 {
5633 ipv4.valid = true;
5634 }
5635 else if(default_iptype == MBTK_IP_TYPE_IPV6)
5636 {
5637 ipv6.valid = true;
5638 }
5639 else
5640 {
5641 ipv4.valid = true;
5642 ipv6.valid = true;
5643 }
5644 }
5645#endif
liubin281ac462023-07-19 14:22:54 +08005646#if 1
5647 if(ipv4.valid) {
5648 char dev[20] = {0};
5649 sprintf(dev, "ccinet%d", cid - 1);
5650
5651 char ip[20] = {0};
5652 char gateway[20] = {0};
5653 char *gateway_ptr = NULL;
5654 char netmask[20] = {0};
5655 char *netmask_ptr = NULL;
5656 if(inet_ntop(AF_INET, &(ipv4.IPAddr), ip, 20) == NULL) {
5657 err = MBTK_INFO_ERR_UNKNOWN;
5658 LOGE("inet_ntop ipv4 ip fail.");
5659 log_hex("IPv4", &(ipv4.IPAddr), 4);
5660 break;
5661 }
5662
5663 if(ipv4.GateWay) {
5664 if(inet_ntop(AF_INET, &(ipv4.GateWay), gateway, 20) == NULL) {
5665 err = MBTK_INFO_ERR_UNKNOWN;
5666 LOGE("inet_ntop ipv4 gateway fail.");
5667 log_hex("IPv4", &(ipv4.IPAddr), 4);
5668 break;
5669 } else {
5670 gateway_ptr = gateway;
5671 }
5672 }
5673
5674 if(ipv4.NetMask) {
5675 if(inet_ntop(AF_INET, &(ipv4.NetMask), netmask, 20) == NULL) {
5676 err = MBTK_INFO_ERR_UNKNOWN;
5677 LOGE("inet_ntop ipv4 netmask fail.");
5678 log_hex("IPv4", &(ipv4.IPAddr), 4);
5679 break;
5680 } else {
5681 netmask_ptr = netmask;
5682 }
5683 }
5684
5685 if(netmask_ptr == NULL) {
5686 netmask_ptr = netmask;
5687 memcpy(netmask_ptr, "255.255.255.0", strlen("255.255.255.0"));
5688 }
5689
5690 if(mbtk_ifc_configure2(dev, ip, 0, gateway_ptr, netmask_ptr)) {
5691 LOGD("Config %s IPv4 %s fail.", dev, ip);
5692 } else {
5693 LOGD("Config %s IPv4 %s success.", dev, ip);
5694 }
5695
5696 }
5697#endif
5698 // Config IPv6 address.
5699 if(ipv6.valid) {
5700 char ip[50] = {0};
5701 char dev[20] = {0};
5702 sprintf(dev, "ccinet%d", cid - 1);
5703
5704 if(inet_ntop(AF_INET6, &(ipv6.IPV6Addr), ip, 50) == NULL) {
5705 err = MBTK_INFO_ERR_UNKNOWN;
5706 LOGE("inet_ntop ipv6 ip fail.");
5707 log_hex("IPv6", &(ipv6.IPV6Addr), 16);
5708 break;
5709 }
5710
5711 if(mbtk_ipv6_config(dev, ip, 64)) {
5712 LOGD("Config %s IPv6 %s fail.", dev, ip);
5713 } else {
5714 LOGD("Config %s IPv6 %s success.", dev, ip);
5715 }
5716 }
5717
5718 cid_active[cid] = 1;
yq.wang69a51872025-06-17 09:45:12 +08005719 //mbtk_set_default_pdp_state(true, cid);
yq.wangc98d1342025-06-13 17:32:06 +08005720 //mbtk_qser_route_config(cid, &ipv4, &ipv6);
liubin281ac462023-07-19 14:22:54 +08005721 if(cli_info->fd != DATA_CALL_BOOTCONN_FD)
5722 {
liuyange134d842024-06-27 17:34:02 +08005723 mbtk_net_led_set(MBTK_NET_LED_DATA_CONNECT);
liubin281ac462023-07-19 14:22:54 +08005724 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_DATA_CALL_RSP, NULL, 0);
yq.wangc98d1342025-06-13 17:32:06 +08005725#ifdef MBTK_ALL_CID_SUPPORT
q.huang8a993672025-06-24 16:19:46 +08005726 if(cid == 1) //cid 1 as a special treatment
5727 {
5728 mbtk_pdp_cb_info_s pdp_cb_info;
5729 memset(&pdp_cb_info, 0x00, sizeof(mbtk_pdp_cb_info_s));
5730 pdp_cb_info.cid = cid;
5731 pdp_cb_info.auto_urc = false;
5732 pdp_cb_info.connect_state = true;
5733 memcpy(&(pdp_cb_info.ipv4), &ipv4, sizeof(mbtk_ipv4_info_t));
5734 memcpy(&(pdp_cb_info.ipv6), &ipv6, sizeof(mbtk_ipv6_info_t));
5735 usleep(1000);
5736 pack_rsp_send(cli_info->fd , MBTK_INFO_ID_IND_PDP_STATE_CHANGE, &pdp_cb_info, sizeof(mbtk_pdp_cb_info_s));
5737 }
yq.wangc98d1342025-06-13 17:32:06 +08005738#endif
liubin281ac462023-07-19 14:22:54 +08005739 }
5740 else
5741 {
5742 free(pack->data);
5743 free(cli_info);
5744 LOG("data_call bootconn success.");
5745 }
5746 }
5747 }
5748 break;
5749 }
5750 case MBTK_DATA_CALL_STOP: {
5751 //mbtk wyq for data_call_ex add start
5752 if(cid_active[cid] == 0)
5753 {
5754 err = MBTK_INFO_ERR_CID_NO_EXIST;
5755 break;
5756 }
5757
5758 int timeout = pack->data[2];
5759 //mbtk wyq for data_call_ex add end
b.liuf37bd332024-03-18 13:51:24 +08005760#if (defined(MBTK_AF_SUPPORT) || defined(MBTK_ALL_CID_SUPPORT))
wangyouqianged88c722023-11-22 16:33:43 +08005761 if(cid == 1)
5762 {
5763 char dev[20] = {0};
yq.wangf98110c2025-06-11 14:50:04 +08005764 mbtk_pdp_cb_info_s pdp_cb_info;
5765 memset(&pdp_cb_info, 0x00, sizeof(mbtk_pdp_cb_info_s));
b.liufe320632024-01-17 20:38:08 +08005766
wangyouqianged88c722023-11-22 16:33:43 +08005767 sprintf(dev, "ccinet%d", cid - 1);
5768
5769 // Config network.
5770 if(mbtk_ifc_configure2(dev, NULL, 0, NULL, NULL)) {
5771 LOGD("Config %s IPv4 0 fail.", dev);
5772 } else {
5773 LOGD("Config %s IPv4 0 success.", dev);
5774 }
yq.wangf98110c2025-06-11 14:50:04 +08005775 pdp_cb_info.cid = cid;
5776 pdp_cb_info.connect_state = false;
5777 pdp_cb_info.auto_urc = false;
5778 pack_rsp_send(cli_info->fd , MBTK_INFO_ID_IND_PDP_STATE_CHANGE, &pdp_cb_info, sizeof(mbtk_pdp_cb_info_s));
5779
wangyouqianged88c722023-11-22 16:33:43 +08005780 cid_active[cid] = 0;
yq.wang69a51872025-06-17 09:45:12 +08005781 //mbtk_set_default_pdp_state(false, cid);
liuyange134d842024-06-27 17:34:02 +08005782 mbtk_net_led_set(MBTK_NET_LED_NET_CONNECT);
wangyouqianged88c722023-11-22 16:33:43 +08005783 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_DATA_CALL_RSP, NULL, 0);
5784 break;
5785 }
5786#endif
liubin281ac462023-07-19 14:22:54 +08005787 cgact_wait.waitting = true;
5788 cgact_wait.cid = cid;
5789 cgact_wait.act = false;
5790 if(req_data_call_stop(cid, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5791 {
5792 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5793 err = MBTK_INFO_ERR_CME + cme_err;
5794 } else {
5795 err = MBTK_INFO_ERR_UNKNOWN;
5796 }
5797 LOG("%d deactive fail.", cid);
5798 }
5799 else
5800 {
5801 // Wait for "CONNECT" or "+CGEV:"
5802 if(wait_cgact_complete(timeout)) { // Timeout
q.huangd36e6422025-07-25 21:16:03 +08005803 /* err = MBTK_INFO_ERR_TIMEOUT;
5804 break;*/ //LYNQ_T108_BUG_1358_CR_138966_Q.HUANG_20250725_COMMENT
5805 LOGW("Stop Data Call cid %d time out,still regard stop success", cid); //LYNQ_T108_BUG_1358_CR_138966_Q.HUANG_20250725_ADD
liubin281ac462023-07-19 14:22:54 +08005806 }
5807 char dev[20] = {0};
5808 sprintf(dev, "ccinet%d", cid - 1);
5809
5810 // Config network.
5811 if(mbtk_ifc_configure2(dev, NULL, 0, NULL, NULL)) {
5812 LOGD("Config %s IPv4 0 fail.", dev);
5813 } else {
5814 LOGD("Config %s IPv4 0 success.", dev);
5815 }
5816
5817#if 0
5818 if(mbtk_ipv6_config(dev, NULL, 64)) {
5819 LOGD("Config %s IPv6 0 fail.", dev);
5820 } else {
5821 LOGD("Config %s IPv6 0 success.", dev);
5822 }
5823#endif
5824 cid_active[cid] = 0;
yq.wang69a51872025-06-17 09:45:12 +08005825 //mbtk_set_default_pdp_state(false, cid);
liuyange134d842024-06-27 17:34:02 +08005826 mbtk_net_led_set(MBTK_NET_LED_NET_CONNECT);
liubin281ac462023-07-19 14:22:54 +08005827 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_DATA_CALL_RSP, NULL, 0);
5828 }
5829 break;
5830 }
5831 case MBTK_DATA_CALL_STATE: {
wangyouqianged88c722023-11-22 16:33:43 +08005832 if(cid_active[cid] == 0)
5833 {
5834 err = MBTK_INFO_ERR_CID_NO_EXIST;
5835 break;
5836 }
liubin281ac462023-07-19 14:22:54 +08005837 mbtk_ipv4_info_t ipv4;
5838 mbtk_ipv6_info_t ipv6;
5839 memset(&ipv4, 0, sizeof(mbtk_ipv4_info_t));
5840 memset(&ipv6, 0, sizeof(mbtk_ipv6_info_t));
5841 if(req_data_call_state_get(cid, &ipv4, &ipv6, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5842 {
5843 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5844 err = MBTK_INFO_ERR_CME + cme_err;
5845 } else {
5846 err = MBTK_INFO_ERR_UNKNOWN;
5847 }
5848 LOG("Get %d state fail.", cid);
5849 }
5850 else
5851 {
5852 uint8 buff[SOCK_MSG_LEN_MAX] = {0};
5853 int buff_len = 0;
wangyouqianged88c722023-11-22 16:33:43 +08005854#ifdef MBTK_AF_SUPPORT
5855 if(cid == 1)
5856 {
5857 ipv4.valid = false;
5858 ipv6.valid = false;
5859 if(default_iptype == MBTK_IP_TYPE_IP)
5860 {
5861 ipv4.valid = true;
5862 }
5863 else if(default_iptype == MBTK_IP_TYPE_IPV6)
5864 {
5865 ipv6.valid = true;
5866 }
5867 else
5868 {
5869 ipv4.valid = true;
5870 ipv6.valid = true;
5871 }
5872 }
5873#endif
liubin281ac462023-07-19 14:22:54 +08005874 if(ipv4.valid && ipv6.valid) {
5875 buff[0] = (uint8)2;
5876 buff_len++;
5877
5878 memcpy(buff + buff_len, &ipv4, sizeof(mbtk_ipv4_info_t));
5879 buff_len += sizeof(mbtk_ipv4_info_t);
5880 memcpy(buff + buff_len, &ipv6, sizeof(mbtk_ipv6_info_t));
5881 buff_len += sizeof(mbtk_ipv6_info_t);
5882 } else if(ipv4.valid) {
5883 buff[0] = (uint8)0;
5884 buff_len++;
5885
5886 memcpy(buff + buff_len, &ipv4, sizeof(mbtk_ipv4_info_t));
5887 buff_len += sizeof(mbtk_ipv4_info_t);
5888 } else if(ipv6.valid) {
5889 buff[0] = (uint8)1;
5890 buff_len++;
5891
5892 memcpy(buff + buff_len, &ipv6, sizeof(mbtk_ipv6_info_t));
5893 buff_len += sizeof(mbtk_ipv6_info_t);
5894 } else {
5895 LOGE("Get IPv4/IPv6 fail.");
5896 err = MBTK_INFO_ERR_UNKNOWN;
5897 break;
5898 }
5899 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_DATA_CALL_RSP, buff, buff_len);
5900 }
5901 break;
5902 }
5903 default: {
5904 err = MBTK_INFO_ERR_FORMAT;
5905 break;
5906 }
5907 }
5908 }
5909 break;
5910 }
r.xiao06db9a12024-04-14 18:51:15 -07005911 case MBTK_INFO_ID_NET_IMS_REQ:
5912 {
5913 if(pack->data_len == 0 || pack->data == NULL) //Get
5914 {
5915 int reg = -1;
5916 if(net_ims_get(&reg, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5917 {
5918 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5919 err = MBTK_INFO_ERR_CME + cme_err;
5920 } else {
5921 err = MBTK_INFO_ERR_UNKNOWN;
5922 }
5923 LOG("Get net ims fail.");
5924 }
5925 else
5926 {
5927 uint8 reg_type = (uint8)reg;
5928 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_IMS_RSP, &reg_type, sizeof(uint8));
5929 }
5930 }
5931 else
5932 {
5933 uint8 ims = *(pack->data);
5934
5935 if(net_ims_set(ims, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5936 {
5937 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5938 err = MBTK_INFO_ERR_CME + cme_err;
5939 } else {
5940 err = MBTK_INFO_ERR_UNKNOWN;
5941 }
5942 LOG("Set net ims fail.");
5943 }
5944 else
5945 {
5946 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_IMS_RSP, NULL, 0);
5947 }
5948 }
5949 break;
5950 }
b.liufdf03172024-06-07 15:01:29 +08005951 case MBTK_INFO_ID_NET_IMS_REG_STATE_REQ:
5952 {
5953 if(pack->data_len == 0 || pack->data == NULL) //Get
5954 {
5955 int reg = -1;
5956 if(net_ims_reg_state_get(&reg, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5957 {
5958 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5959 err = MBTK_INFO_ERR_CME + cme_err;
5960 } else {
5961 err = MBTK_INFO_ERR_UNKNOWN;
5962 }
5963 LOG("Get net ims fail.");
5964 }
5965 else
5966 {
5967 uint8 reg_type = (uint8)reg;
5968 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_IMS_REG_STATE_RSP, &reg_type, sizeof(uint8));
5969 }
5970 }
5971 else
5972 {
5973 err = MBTK_INFO_ERR_UNSUPPORTED;
5974 }
5975 break;
5976 }
xy.he22063af2024-12-06 01:28:08 -08005977 case MBTK_INFO_ID_NET_FPLMN_REQ:
5978 {
5979 LOG("MBTK_INFO_ID_NET_FPLMN_REQ in\n");
5980 if(pack->data_len == 0 || pack->data == NULL) //Get
5981 {
5982 char fplmn[256] = {0};
5983 LOG("fplmn_test");
5984 if(req_fplmn_get(fplmn, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5985 {
5986 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5987 err = MBTK_INFO_ERR_CME + cme_err;
5988 }else {
5989 err = MBTK_INFO_ERR_UNKNOWN;
5990 }
5991 LOGE("Get FPLMN fail.");
5992 printf("get fplmn fail\n");
5993 }
5994 else
5995 {
5996 printf("get fplmn success\n");
5997 LOGE("Get FPLMN success.");
5998 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_FPLMN_RSP, fplmn, strlen(fplmn));
5999 }
6000 }
6001 else //set
6002 {
6003 char *fplmn_temp = (char *)pack->data;
6004 char fplmn[128] = {0};
6005 int data_len = pack->data_len;
6006 char reg[50] = {0};
6007 memcpy(fplmn, fplmn_temp, data_len);
6008 LOG("fplmn_test_set ,fplmn_temp = %s,len = %d\n",fplmn_temp,data_len);
6009 LOG("fplmn_test_set ,fplmn = %s, len = %d\n",fplmn,strlen(fplmn));
6010 if(req_fplmn_set(fplmn,reg,&cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
6011 {
6012 if(cme_err != MBTK_INFO_ERR_CME_NON) {
6013 err = MBTK_INFO_ERR_CME + cme_err;
6014 } else {
6015 err = MBTK_INFO_ERR_UNKNOWN;
6016 }
6017 LOG("req_fplmn_set fail.");
6018 }
6019 else
6020 {
6021 LOG("req_fplmn_set success.,reg:%s",reg);
6022 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_FPLMN_RSP, NULL, 0);
6023 }
6024 }
6025 break;
6026 }
r.xiaoec113d12024-01-12 02:13:28 -08006027 case MBTK_INFO_ID_WAKEUP_STA_REQ:
6028 {
6029 if(pack->data_len == 0 || pack->data == NULL)
6030 {
6031 err = MBTK_INFO_ERR_UNSUPPORTED;
6032 LOG("Get POWERIND state UNSUPPORTED.");
6033 }
6034 else // Set powerind state.
6035 {
6036 uint32 state = *(pack->data);
6037 if(req_powerind_set(state, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
6038 {
6039 if(cme_err != MBTK_INFO_ERR_CME_NON) {
6040 err = MBTK_INFO_ERR_CME + cme_err;
6041 } else {
6042 err = MBTK_INFO_ERR_UNKNOWN;
6043 }
6044 LOG("Set POWERIND state fail.");
6045 }
6046 else
6047 {
6048 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_WAKEUP_STA_RSP, NULL, 0);
6049 }
6050 }
6051 break;
6052 }
6053 case MBTK_INFO_ID_OOS_STA_REQ:
6054 {
6055 if(pack->data_len == 0 || pack->data == NULL)
6056 {
6057 mbtk_oos_info oos_t;
6058 if(req_oos_get(&oos_t, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
6059 {
6060 if(cme_err != MBTK_INFO_ERR_CME_NON) {
6061 err = MBTK_INFO_ERR_CME + cme_err;
6062 } else {
6063 err = MBTK_INFO_ERR_UNKNOWN;
6064 }
6065 LOG("Get SMS OOS fail.");
r.xiaoec113d12024-01-12 02:13:28 -08006066 }
6067 else
6068 {
r.xiaoec113d12024-01-12 02:13:28 -08006069 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_OOS_STA_RSP, &oos_t, sizeof(mbtk_oos_info));
6070 }
6071 }
b.liufe320632024-01-17 20:38:08 +08006072 else // Set OOS
r.xiaoec113d12024-01-12 02:13:28 -08006073 {
r.xiaocfd7c682024-01-22 03:59:46 -08006074 if(pack->data_len != sizeof(mbtk_oos_info))
6075 {
6076 err = MBTK_INFO_ERR_REQ_PARAMETER;
6077 LOG("Set oos error.");
6078 break;
6079 }
6080
b.liu9e8584b2024-11-06 19:21:28 +08006081 mbtk_oos_info *state = (mbtk_oos_info *)pack->data;
r.xiaoec113d12024-01-12 02:13:28 -08006082 if(req_oos_set(state, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
6083 {
6084 if(cme_err != MBTK_INFO_ERR_CME_NON) {
6085 err = MBTK_INFO_ERR_CME + cme_err;
6086 } else {
6087 err = MBTK_INFO_ERR_UNKNOWN;
6088 }
6089 LOG("Set OOS fail.");
6090 }
6091 else
6092 {
6093 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_OOS_STA_RSP, NULL, 0);
6094 }
6095 }
6096 break;
6097 }
q.huang8a993672025-06-24 16:19:46 +08006098 case MBTK_INFO_ID_LED_REQ:
wangyouqiang38e53362024-01-23 10:53:48 +08006099 {
6100 if(pack->data_len == 0 || pack->data == NULL)
6101 {
6102 err = MBTK_INFO_ERR_UNSUPPORTED;
6103 LOGE("led param is error.");
6104 }
6105 else
6106 {
6107 char type = pack->data[0];
6108 char status = pack->data[1];
6109 LOGE("[set_led] = [%d], [status_led] = [%d].", type, status);
r.xiaoec113d12024-01-12 02:13:28 -08006110
wangyouqiang38e53362024-01-23 10:53:48 +08006111 if(type == MBTK_LED_TYPE_NET)
6112 {
6113 if(status == MBTK_LED_STATUS_CLOSE)
6114 {
6115 mbtk_net_led_set(MBTK_NET_LED_CLOSE);
6116 }
6117 else
6118 {
6119 mbtk_net_led_set(MBTK_NET_LED_OPEN);
6120 }
6121 }
6122 else
6123 {
6124 if(status == MBTK_LED_STATUS_CLOSE)
6125 {
6126 status_led_set(MBTK_STATUS_LED_CLOSE);
6127 }
6128 else
6129 {
6130 status_led_set(MBTK_STATUS_LED_OPEN);
6131 }
6132 }
6133 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_LED_RSP, NULL, 0);
6134 }
6135 break;
6136 }
liubin281ac462023-07-19 14:22:54 +08006137 default:
6138 {
6139 err = MBTK_INFO_ERR_REQ_UNKNOWN;
6140 LOG("Unknown request : %s", id2str(pack->info_id));
6141 break;
6142 }
6143 }
6144
6145 return err;
6146 }
6147}
6148
6149// Process AT URC data
6150static int send_pack_to_queue(sock_client_info_t* cli_info, void* pack)
6151{
6152 if(info_queue.count >= PACK_PROCESS_QUEUE_MAX)
6153 {
6154 LOG("Packet process queue is full");
6155 return -1;
6156 }
6157
6158 info_queue_item_t *item = (info_queue_item_t*)malloc(sizeof(info_queue_item_t));
6159 if(!item)
6160 {
6161 LOG("malloc() fail[%d].", errno);
6162 return -1;
6163 }
6164 item->cli_info = cli_info;
6165 item->pack = pack;
6166 mbtk_queue_put(&info_queue, item);
6167
6168 // If thread is waitting,continue it.
6169 if(1/*!is_running*/)
6170 {
6171 pthread_mutex_lock(&info_mutex);
6172 pthread_cond_signal(&info_cond);
6173 pthread_mutex_unlock(&info_mutex);
6174 }
6175 else
6176 {
6177 LOG("Packet process thread is process...");
6178 }
6179
6180 return 0;
6181}
6182
b.liu9e8584b2024-11-06 19:21:28 +08006183static void radio_state_change(const void *data, int data_len)
liubin281ac462023-07-19 14:22:54 +08006184{
6185 uint8 *data_ptr = (uint8*)data;
6186 if(data_ptr[0]) {
6187 net_info.radio_state = MBTK_RADIO_STATE_ON;
6188 } else {
6189 net_info.radio_state = MBTK_RADIO_STATE_OFF;
6190 }
6191
6192 sock_client_info_t *cli = NULL;
6193 list_first(sock_client_list);
6194 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
6195 {
6196 if(cli->ind_num > 0) {
6197 int i;
6198 for(i = 0; i < IND_REGISTER_MAX; i++) {
6199 // Registe MBTK_INFO_ID_IND_RADIO_STATE_CHANGE
6200 if(cli->ind_register[i] == MBTK_INFO_ID_IND_RADIO_STATE_CHANGE) {
6201 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_RADIO_STATE_CHANGE, data, data_len);
6202 break;
6203 }
6204 }
6205 }
6206 }
6207}
6208
b.liu9e8584b2024-11-06 19:21:28 +08006209static void pdp_state_change(const void *data, int data_len)
liubin281ac462023-07-19 14:22:54 +08006210{
6211 sock_client_info_t *cli = NULL;
6212 list_first(sock_client_list);
6213 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
6214 {
6215 if(cli->ind_num > 0) {
6216 int i;
6217 for(i = 0; i < IND_REGISTER_MAX; i++) {
6218 if(cli->ind_register[i] == MBTK_INFO_ID_IND_PDP_STATE_CHANGE) {
6219 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_PDP_STATE_CHANGE, data, data_len);
6220 break;
6221 }
6222 }
6223 }
6224 }
6225}
6226
b.liu9e8584b2024-11-06 19:21:28 +08006227static void net_state_change(const void *data, int data_len)
liubin281ac462023-07-19 14:22:54 +08006228{
6229 sock_client_info_t *cli = NULL;
6230 list_first(sock_client_list);
6231 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
6232 {
6233 if(cli->ind_num > 0) {
6234 int i;
6235 for(i = 0; i < IND_REGISTER_MAX; i++) {
6236 // Registe MBTK_INFO_ID_IND_NET_STATE_CHANGE
6237 if(cli->ind_register[i] == MBTK_INFO_ID_IND_NET_STATE_CHANGE) {
6238 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_NET_STATE_CHANGE, data, data_len);
6239 break;
6240 }
6241 }
6242 }
6243 }
6244}
6245
b.liu9e8584b2024-11-06 19:21:28 +08006246static void call_state_change(const void *data, int data_len)
liubin281ac462023-07-19 14:22:54 +08006247{
6248 sock_client_info_t *cli = NULL;
6249 list_first(sock_client_list);
6250 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
6251 {
6252 if(cli->ind_num > 0) {
6253 int i;
6254 for(i = 0; i < IND_REGISTER_MAX; i++) {
6255 // Registed MBTK_INFO_ID_IND_RADIO_STATE_CHANGE
6256 if(cli->ind_register[i] == MBTK_INFO_ID_IND_CALL_STATE_CHANGE) {
6257 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_CALL_STATE_CHANGE, data, data_len);
6258 break;
6259 }
6260 }
6261 }
6262 }
6263}
6264
b.liu9e8584b2024-11-06 19:21:28 +08006265static void sim_state_change(const void *data, int data_len)
liubin281ac462023-07-19 14:22:54 +08006266{
6267 sock_client_info_t *cli = NULL;
6268 list_first(sock_client_list);
6269 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
6270 {
6271 if(cli->ind_num > 0) {
6272 int i;
6273 for(i = 0; i < IND_REGISTER_MAX; i++) {
6274 // Registed MBTK_INFO_ID_IND_RADIO_STATE_CHANGE
6275 if(cli->ind_register[i] == MBTK_INFO_ID_IND_SIM_STATE_CHANGE) {
6276 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_SIM_STATE_CHANGE, data, data_len);
6277 break;
6278 }
6279 }
6280 }
6281 }
6282}
6283
b.liu9e8584b2024-11-06 19:21:28 +08006284static void sms_state_change(const void *data, int data_len)
liubin281ac462023-07-19 14:22:54 +08006285{
6286 sock_client_info_t *cli = NULL;
6287 list_first(sock_client_list);
6288 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
6289 {
6290 if(cli->ind_num > 0) {
6291 int i;
6292 for(i = 0; i < IND_REGISTER_MAX; i++) {
6293 // Registed MBTK_INFO_ID_IND_SMS_STATE_CHANGE
6294 if(cli->ind_register[i] == MBTK_INFO_ID_IND_SMS_STATE_CHANGE) {
6295 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_SMS_STATE_CHANGE, data, data_len);
6296 break;
6297 }
6298 }
6299 }
6300 }
6301}
6302
b.liu9e8584b2024-11-06 19:21:28 +08006303static void signal_state_change(const void *data, int data_len)
r.xiaofca7c472024-04-24 01:00:23 -07006304{
6305 sock_client_info_t *cli = NULL;
6306 list_first(sock_client_list);
6307 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
6308 {
6309 if(cli->ind_num > 0) {
6310 int i;
6311 for(i = 0; i < IND_REGISTER_MAX; i++) {
6312 // Registe MBTK_INFO_ID_IND_SIGNAL_STATE_CHANGE
6313 if(cli->ind_register[i] == MBTK_INFO_ID_IND_SIGNAL_STATE_CHANGE) {
6314 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_SIGNAL_STATE_CHANGE, data, data_len);
6315 break;
6316 }
6317 }
6318 }
6319 }
6320}
6321
6322
b.liu9e8584b2024-11-06 19:21:28 +08006323int urc_msg_distribute(bool async_process, info_urc_msg_id_enum msg, const void *data, int data_len)
liubin281ac462023-07-19 14:22:54 +08006324{
6325#if 0
6326 if(urc_queue.count >= PACK_PROCESS_QUEUE_MAX)
6327 {
6328 LOG("Packet process queue is full");
6329 return -1;
6330 }
6331
6332 info_urc_msg_t *urc = (info_urc_msg_t*)malloc(sizeof(info_urc_msg_t));
6333 if(!urc)
6334 {
6335 LOG("malloc() fail[%d].", errno);
6336 return -1;
6337 }
6338 urc->msg = msg;
6339 urc->data = memdup(data, data_len);
6340 urc->data_len = data_len;
6341
6342 mbtk_queue_put(&urc_queue, urc);
6343
6344 // If thread is waitting,continue it.
6345 if(1/*!is_running*/)
6346 {
6347 pthread_mutex_lock(&urc_mutex);
6348 pthread_cond_signal(&urc_cond);
6349 pthread_mutex_unlock(&urc_mutex);
6350 }
6351 else
6352 {
6353 LOG("Packet process thread is process...");
6354 }
6355
6356 return 0;
6357#else
b.liuf77b86c2024-11-09 13:24:10 +08006358
liubin281ac462023-07-19 14:22:54 +08006359 if(async_process) {
6360 info_urc_msg_t *urc = (info_urc_msg_t*)malloc(sizeof(info_urc_msg_t));
6361 if(!urc)
6362 {
6363 LOG("malloc() fail[%d].", errno);
6364 return -1;
6365 }
6366 urc->msg = msg;
6367 if(data && data_len > 0) {
6368 urc->data = memdup(data, data_len);
6369 urc->data_len = data_len;
6370 } else {
6371 urc->data = NULL;
6372 urc->data_len = 0;
6373 }
6374 return send_pack_to_queue(NULL, urc);
6375 } else {
6376 switch(msg) {
6377 case INFO_URC_MSG_NET_CS_REG_STATE:
6378 {
6379 net_state_change(data, data_len);
6380 break;
6381 }
6382 case INFO_URC_MSG_CALL_STATE:
6383 {
6384 call_state_change(data, data_len);
6385 break;
6386 }
6387 case INFO_URC_MSG_SMS_STATE:
6388 {
6389 sms_state_change(data, data_len);
6390 break;
6391 }
6392 case INFO_URC_MSG_SIM_STATE:
6393 {
6394 sim_state_change(data, data_len);
6395 break;
6396 }
6397 case INFO_URC_MSG_PDP_STATE:
6398 {
6399 pdp_state_change(data, data_len);
6400 break;
6401 }
6402 default: {
6403 LOGE("Unknown msg : %d", msg);
6404 break;
6405 }
6406 }
6407
6408 return 0;
6409 }
6410#endif
6411}
6412
6413
6414static void ind_regisger(sock_client_info_t* cli_info, uint16 ind)
6415{
6416 uint32 i = 0;
6417 while(i < cli_info->ind_num)
6418 {
6419 if(cli_info->ind_register[i] == ind)
6420 break;
6421 i++;
6422 }
6423
6424 if(i == cli_info->ind_num) // No found IND
6425 {
6426 cli_info->ind_register[i] = ind;
6427 cli_info->ind_num++;
6428 LOG("Register IND : %s", id2str(ind));
6429 }
6430 else
6431 {
6432 LOG("IND had exist.");
6433 }
6434}
6435
6436static void pack_distribute(sock_client_info_t* cli_info, mbtk_info_pack_t* pack)
6437{
6438 // Register IND Message.
6439 if(mbtk_info_type_get(pack->info_id) == MBTK_INFO_TYPE_IND)
6440 {
6441 mbtk_info_err_enum err = MBTK_INFO_ERR_SUCCESS;
6442 if(cli_info->ind_num >= IND_REGISTER_MAX)
6443 {
6444 LOG("IND if full.");
6445 err = MBTK_INFO_ERR_IND_FULL;
6446 }
6447 else
6448 {
6449 ind_regisger(cli_info, pack->info_id);
6450 }
6451
6452 pack_error_send(cli_info->fd, pack->info_id, err);
6453
6454 mbtk_info_pack_free(&pack);
6455 }
6456 else // Request Information.
6457 {
6458 LOG("Start process REQ(%s), Length : %d", id2str(pack->info_id), pack->data_len);
6459 if(0 && pack->data_len > 0)
6460 {
6461 log_hex("DATA", pack->data, pack->data_len);
6462 }
6463
6464 // Send to REQ_process_thread process.
6465 send_pack_to_queue(cli_info, pack);
6466
6467 // For test.
6468 // pack_error_send(cli_info->fd, pack->info_id + 1, MBTK_INFO_ERR_SUCCESS);
6469 }
6470}
6471
6472static sock_client_info_t* cli_find(int fd)
6473{
6474 sock_client_info_t *result = NULL;
6475 list_first(sock_client_list);
6476 while ((result = (sock_client_info_t*) list_next(sock_client_list)))
6477 {
6478 if (result->fd == fd)
6479 return result;
6480 }
6481
6482 return NULL;
6483}
6484
6485//mbtk wyq for server_ready_status add start
6486void server_ready_set(void)
6487{
6488 server_ready_status = 1;
6489}
6490
6491char server_ready_get(void)
6492{
6493 return server_ready_status;
6494}
6495
6496static void server_state_send(void)
6497{
6498 sock_client_info_t *cli = NULL;
6499 list_first(sock_client_list);
6500 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
6501 {
6502 if(cli->ind_num > 0) {
6503 if(cli->ind_register[0] == MBTK_INFO_ID_IND_SERVER_STATE_CHANGE) {
6504 cli->ind_num = 0;
6505 cli->ind_register[0] = 0;
6506 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_SERVER_STATE_CHANGE, "1", 1);
6507 break;
6508 }
6509 }
6510 else
6511 {
6512 break;
6513 }
6514 }
6515 LOG("handshake message send ok.");
6516}
6517
6518//mbtk wyq for server_ready_status add end
6519
6520//mbtk wyq for data_call_ex add start
6521//Save the cid that "DATA_CALL" needs to be automatically connected after startup
6522void data_call_bootconn_save(int cid, int bootconn)
6523{
6524 if(cid_bootconn[cid] == bootconn + '0')
6525 {
6526 return;
6527 }
6528 cid_bootconn[cid] = bootconn + '0';
6529
6530 LOG("data_call_bootconn_set cid_bootconn = %s", cid_bootconn);
6531 property_set("persist.mbtk.datacall.bootconn", cid_bootconn);
6532}
6533
6534static void* data_call_bootconn_pthread(void *arg)
6535{
6536 UNUSED(arg);
6537 LOG("data_call_bootconn_pthread enter.");
6538 int i = 0;
6539 int send_sum = 0;
6540 int bootconn = 0;
b.liufe320632024-01-17 20:38:08 +08006541
liubin281ac462023-07-19 14:22:54 +08006542 while(1)
6543 {
6544 if(server_ready_get() && send_sum == 0)
6545 {
6546 server_state_send();
6547 send_sum = 1;
6548 }
b.liufe320632024-01-17 20:38:08 +08006549
liubin281ac462023-07-19 14:22:54 +08006550 if(net_info.sim_state == MBTK_SIM_READY && net_info.net_type == MBTK_RADIO_TECH_E_UTRAN && bootconn == 0)
6551 {
6552 //data_call_bootconn_exec();
6553 property_get("persist.mbtk.datacall.bootconn", cid_bootconn, "00000000");
6554 LOG("data_call_bootconn_exec cid_bootconn = %s", cid_bootconn);
b.liufe320632024-01-17 20:38:08 +08006555
liubin281ac462023-07-19 14:22:54 +08006556 for(i = MBTK_APN_CID_MIN; i < MBTK_APN_CID_MAX + 1; i++)
6557 {
6558 if(cid_bootconn[i] == '1')
6559 {
6560 sock_client_info_t *info = (sock_client_info_t*)malloc(sizeof(sock_client_info_t));
6561 if(info == NULL)
6562 {
6563 LOG("clinent_info malloc() fail.");
6564 continue;
6565 }
6566 memset(info, 0, sizeof(sock_client_info_t));
6567 info->fd = DATA_CALL_BOOTCONN_FD;
b.liufe320632024-01-17 20:38:08 +08006568
liubin281ac462023-07-19 14:22:54 +08006569 mbtk_info_pack_t* pack = mbtk_info_pack_creat(MBTK_INFO_ID_NET_DATA_CALL_REQ);
6570 if(pack == NULL)
6571 {
6572 free(info);
6573 LOG("Packet malloc() fail.");
6574 continue;
6575 }
6576
6577 // "info_err"
6578 //pack->info_err = byte_2_uint16(ptr, false)
6579
6580 // "data_len"
6581 pack->data_len = 5;
6582
b.liu9e8584b2024-11-06 19:21:28 +08006583 uint8 *p = (uint8 *)malloc(5);
liubin281ac462023-07-19 14:22:54 +08006584 p[0] = MBTK_DATA_CALL_START;
6585 p[1] = i;
6586 p[2] = 0;
6587 p[3] = 1;
6588 p[4] = 10;
6589 pack->data = p;
6590 send_pack_to_queue(info, pack);
6591 }
6592 }
6593
6594 bootconn = 1;
6595 }
6596
6597 if(bootconn == 1 && send_sum == 1)
6598 {
6599 break;
6600 }
6601 else
6602 {
6603 sleep(1);
6604 }
6605 }
6606
6607 LOG("data_call_bootconn_pthread exit.");
6608 return NULL;
6609}
6610
6611//mbtk wyq for data_call_ex add end
6612
6613static void* info_main_pthread(void* arg)
6614{
6615 UNUSED(arg);
6616 epoll_fd = epoll_create(SOCK_CLIENT_MAX + 1);
6617 if(epoll_fd < 0)
6618 {
6619 LOG("epoll_create() fail[%d].", errno);
6620 return NULL;
6621 }
6622
6623 uint32 event = EPOLLIN | EPOLLET;
6624 struct epoll_event ev;
6625 ev.data.fd = sock_listen_fd;
6626 ev.events = event; //EPOLLIN | EPOLLERR | EPOLLET;
6627 epoll_ctl(epoll_fd,EPOLL_CTL_ADD,sock_listen_fd,&ev);
6628
6629 int nready = -1;
6630 struct epoll_event epoll_events[EPOLL_LISTEN_MAX];
6631 while(1)
6632 {
6633 nready = epoll_wait(epoll_fd, epoll_events, EPOLL_LISTEN_MAX, -1);
6634 if(nready > 0)
6635 {
6636 sock_client_info_t *cli_info = NULL;
6637 int i;
6638 for(i = 0; i < nready; i++)
6639 {
6640 LOG("fd[%d] event = %x",epoll_events[i].data.fd, epoll_events[i].events);
6641 if(epoll_events[i].events & EPOLLHUP) // Client Close.
6642 {
6643 if((cli_info = cli_find(epoll_events[i].data.fd)) != NULL)
6644 {
6645 cli_close(cli_info);
6646 }
6647 else
6648 {
6649 LOG("Unknown client[fd = %d].", epoll_events[i].data.fd);
6650 }
6651 }
6652 else if(epoll_events[i].events & EPOLLIN)
6653 {
6654 if(epoll_events[i].data.fd == sock_listen_fd) // New clients connected.
6655 {
6656 int client_fd = -1;
6657 while(1)
6658 {
6659 struct sockaddr_in cliaddr;
6660 socklen_t clilen = sizeof(cliaddr);
6661 client_fd = accept(epoll_events[i].data.fd, (struct sockaddr *) &cliaddr, &clilen);
6662 if(client_fd < 0)
6663 {
6664 if(errno == EAGAIN)
6665 {
6666 LOG("All client connect get.");
6667 }
6668 else
6669 {
6670 LOG("accept() error[%d].", errno);
6671 }
6672 break;
6673 }
6674 // Set O_NONBLOCK
6675 int flags = fcntl(client_fd, F_GETFL, 0);
6676 if (flags > 0)
6677 {
6678 flags |= O_NONBLOCK;
6679 if (fcntl(client_fd, F_SETFL, flags) < 0)
6680 {
6681 LOG("Set flags error:%d", errno);
6682 }
6683 }
6684
6685 memset(&ev,0,sizeof(struct epoll_event));
6686 ev.data.fd = client_fd;
6687 ev.events = event;//EPOLLIN | EPOLLERR | EPOLLET;
6688 epoll_ctl(epoll_fd, EPOLL_CTL_ADD, client_fd, &ev);
6689
6690 sock_client_info_t *info = (sock_client_info_t*)malloc(sizeof(sock_client_info_t));
6691 if(info)
6692 {
6693 memset(info, 0, sizeof(sock_client_info_t));
6694 info->fd = client_fd;
6695 if(server_ready_get() == 1)
6696 {
6697 info->ind_num = 0;
6698 pack_rsp_send(info->fd , MBTK_INFO_ID_IND_SERVER_STATE_CHANGE, "1", 1);
6699 LOG("server ready ok.");
6700 }
6701 else
6702 {
6703 info->ind_num = 1;
6704 info->ind_register[0] = MBTK_INFO_ID_IND_SERVER_STATE_CHANGE;
6705 LOG("server ready no.");
6706 }
6707 list_add(sock_client_list, info);
6708 LOG("Add New Client FD Into List.");
6709 }
6710 else
6711 {
6712 LOG("malloc() fail.");
6713 }
6714 }
6715 }
6716 else if((cli_info = cli_find(epoll_events[i].data.fd)) != NULL) // Client data arrive.
6717 {
6718 // Read and process every message.
6719 mbtk_info_err_enum err = MBTK_INFO_ERR_SUCCESS;
6720 mbtk_info_pack_t** pack = mbtk_info_pack_recv(cli_info->fd, true, &err);
6721
6722 // Parse packet error,send error response to client.
6723 if(pack == NULL)
6724 {
6725 if(err != MBTK_INFO_ERR_SUCCESS)
6726 {
6727 pack_error_send(cli_info->fd, MBTK_INFO_ID_REQ_UNKNOWN, err);
6728 }
6729 }
6730 else
6731 {
6732#if 0
6733 int i = 0;
6734 while(pack[i] != NULL)
6735 {
6736 pack_distribute(cli_info, pack[i]);
6737 // Not free,will free in pack_process() or packet process thread.
6738 //mbtk_info_pack_free(&(pack[i]));
6739 i++;
6740 }
6741 free(pack);
6742#else
6743 mbtk_info_pack_t** pack_ptr = pack;
6744 while(*pack_ptr)
6745 {
6746 pack_distribute(cli_info, *pack_ptr);
6747 // Not free,will free in pack_process() or packet process thread.
6748 //mbtk_info_pack_free(pack_ptr);
6749 pack_ptr++;
6750 }
6751
6752 free(pack);
6753#endif
6754 }
6755 }
6756 else
6757 {
6758 LOG("Unknown socket : %d", epoll_events[i].data.fd);
6759 }
6760 }
6761 else
6762 {
6763 LOG("Unknown event : %x", epoll_events[i].events);
6764 }
6765 }
6766 }
6767 else
6768 {
6769 LOG("epoll_wait() fail[%d].", errno);
6770 }
6771 }
6772
6773 return NULL;
6774}
6775
liubin281ac462023-07-19 14:22:54 +08006776/*
6777void mbtk_radio_ready_cb()
6778{
6779 pthread_t radio_pid;
6780 pthread_attr_t thread_attr;
6781 pthread_attr_init(&thread_attr);
6782 if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
6783 {
6784 LOG("pthread_attr_setdetachstate() fail.");
6785 return;
6786 }
6787
6788 if(pthread_create(&radio_pid, &thread_attr, radio_ready_thread, NULL))
6789 {
6790 LOG("pthread_create() fail.");
6791 }
6792
6793 pthread_attr_destroy(&thread_attr);
6794}
6795*/
6796
yq.wangf98110c2025-06-11 14:50:04 +08006797static void net_ifc_state_change(bool act, int cid, mbtk_pdp_cb_info_s *pdp_cb_info)
liubin281ac462023-07-19 14:22:54 +08006798{
6799 if(cid < MBTK_APN_CID_MIN || cid > MBTK_APN_CID_MAX) { // No nothing for cid 1 and 8
6800 return;
6801 }
6802
yq.wanga9efa822024-07-03 04:33:48 -07006803#if 0
wangyouqiang65884152023-10-25 19:54:15 +08006804 if(act)
6805 {
6806 cid_active[cid] = 1;
wangyouqiang80487e42024-05-24 15:06:20 +08006807 mbtk_set_default_pdp_state(true, cid);
wangyouqiang65884152023-10-25 19:54:15 +08006808 }
6809 else
6810 {
6811 cid_active[cid] = 0;
wangyouqiang80487e42024-05-24 15:06:20 +08006812 mbtk_set_default_pdp_state(false, cid);
wangyouqiang65884152023-10-25 19:54:15 +08006813 }
yq.wanga9efa822024-07-03 04:33:48 -07006814#endif
liubin281ac462023-07-19 14:22:54 +08006815 char dev[20] = {0};
6816 sprintf(dev, "ccinet%d", cid - 1);
6817 if(act) { // Config IP.
6818 // Get IP information.
6819 mbtk_ipv4_info_t ipv4;
6820 mbtk_ipv6_info_t ipv6;
6821 memset(&ipv4, 0, sizeof(mbtk_ipv4_info_t));
6822 memset(&ipv6, 0, sizeof(mbtk_ipv6_info_t));
6823 int cme_err = MBTK_INFO_ERR_CME_NON;
6824 if(!req_data_call_state_get(cid, &ipv4, &ipv6, &cme_err) && cme_err == MBTK_INFO_ERR_CME_NON)
6825 {
wangyouqianged88c722023-11-22 16:33:43 +08006826#ifdef MBTK_AF_SUPPORT
6827 if(cid == 1)
6828 {
6829 ipv4.valid = false;
6830 ipv6.valid = false;
6831 if(default_iptype == MBTK_IP_TYPE_IP)
6832 {
6833 ipv4.valid = true;
6834 }
6835 else if(default_iptype == MBTK_IP_TYPE_IPV6)
6836 {
6837 ipv6.valid = true;
6838 }
6839 else
6840 {
6841 ipv4.valid = true;
6842 ipv6.valid = true;
6843 }
6844 }
6845#endif
b.liuf77b86c2024-11-09 13:24:10 +08006846
yq.wangf98110c2025-06-11 14:50:04 +08006847 if(pdp_cb_info) {
6848 memcpy(&(pdp_cb_info->ipv4), &ipv4, sizeof(mbtk_ipv4_info_t));
6849 memcpy(&(pdp_cb_info->ipv6), &ipv6, sizeof(mbtk_ipv6_info_t));
b.liuf77b86c2024-11-09 13:24:10 +08006850 }
6851
liubin281ac462023-07-19 14:22:54 +08006852 // Config IPv4 address.
6853 if(ipv4.valid) {
6854 char ip[20] = {0};
6855 if(inet_ntop(AF_INET, &(ipv4.IPAddr), ip, 20) == NULL) {
6856 LOGE("inet_ntop ipv4 ip fail.");
6857 return;
6858 }
6859
6860 if(mbtk_ifc_configure2(dev, ip, 0, NULL, "255.255.255.0")) {
6861 LOGD("Config %s IPv4 %s fail.", dev, ip);
6862 } else {
6863 LOGD("Config %s IPv4 %s success.", dev, ip);
6864 }
6865 }
6866
6867 // Config IPv6 address.
6868 if(ipv6.valid) {
6869 char ip[50] = {0};
6870
6871 if(inet_ntop(AF_INET6, &(ipv6.IPV6Addr), ip, 50) == NULL) {
6872 LOGE("inet_ntop ipv6 ip fail.");
6873 return;
6874 }
6875
6876 if(mbtk_ipv6_config(dev, ip, 64)) {
6877 LOGD("Config %s IPv6 %s fail.", dev, ip);
6878 } else {
6879 LOGD("Config %s IPv6 %s success.", dev, ip);
6880 }
6881 }
yq.wanga9efa822024-07-03 04:33:48 -07006882
yq.wangc98d1342025-06-13 17:32:06 +08006883 //mbtk_qser_route_config(cid, &ipv4, &ipv6);
liubin281ac462023-07-19 14:22:54 +08006884 }
6885 } else { // Del IP
6886 if(mbtk_ifc_configure2(dev, NULL, 0, NULL, NULL)) {
6887 LOGD("Config %s IPv4 0 fail.", dev);
6888 } else {
6889 LOGD("Config %s IPv4 0 success.", dev);
6890 }
6891 }
6892}
6893
yq.wang69a51872025-06-17 09:45:12 +08006894#if 0
yq.wanga9efa822024-07-03 04:33:48 -07006895static void data_call_restart()
6896{
6897#if 0
6898 // Waitting for network ok.
6899 mbtk_net_info_t info;
6900 int cme_err;
6901 int i = 0;
6902 while(i < 15) { // 15s timeout
6903 cme_err = MBTK_INFO_ERR_CME_NON;
6904 memset(&info, 0, sizeof(mbtk_net_info_t));
6905 if(!req_net_sel_mode_get(&info, &cme_err) && cme_err == MBTK_INFO_ERR_CME_NON)
6906 {
6907 if(info.net_type >= 2) {
6908 break;
6909 }
6910 }
6911
6912 sleep(1);
6913 i++;
6914 }
6915#endif
6916 // +CGACT
6917 int cid;
yq.wangf98110c2025-06-11 14:50:04 +08006918 mbtk_pdp_cb_info_s pdp_cb_info;
yq.wanga9efa822024-07-03 04:33:48 -07006919 LOGD("Start active APN.");
yq.wangf98110c2025-06-11 14:50:04 +08006920 //at_process = true;
yq.wanga9efa822024-07-03 04:33:48 -07006921 cgact_wait.act = true;
yq.wangf98110c2025-06-11 14:50:04 +08006922 for(cid = MBTK_APN_CID_MIN; cid <= MBTK_APN_CID_MAX; cid++)
6923 {
yq.wangd58f71e2024-08-21 23:45:31 -07006924 if(cid_active[cid])
6925 {
6926 LOG("Active cid : %d", cid);
yq.wangf98110c2025-06-11 14:50:04 +08006927 memset(&pdp_cb_info, 0x00, sizeof(mbtk_pdp_cb_info_s));
yq.wangd58f71e2024-08-21 23:45:31 -07006928 cgact_wait.waitting = true;
6929 cgact_wait.cid = cid;
yq.wangf98110c2025-06-11 14:50:04 +08006930
b.liuf77b86c2024-11-09 13:24:10 +08006931 net_ifc_state_change(false, cid, NULL);
yq.wangf98110c2025-06-11 14:50:04 +08006932 pdp_cb_info.cid = cid;
6933 pdp_cb_info.connect_state = false;
6934 pdp_cb_info.auto_urc = true;
6935 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &pdp_cb_info, sizeof(mbtk_pdp_cb_info_s));
6936
yq.wangd58f71e2024-08-21 23:45:31 -07006937 req_data_call_start(cid, NULL);
6938 wait_cgact_complete(10);
yq.wangf98110c2025-06-11 14:50:04 +08006939
6940 net_ifc_state_change(true, cid, &pdp_cb_info);
6941 pdp_cb_info.connect_state = true;
6942 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &pdp_cb_info, sizeof(mbtk_pdp_cb_info_s));
yq.wangd58f71e2024-08-21 23:45:31 -07006943 }
yq.wanga9efa822024-07-03 04:33:48 -07006944 }
yq.wangf98110c2025-06-11 14:50:04 +08006945 //at_process = false;
yq.wanga9efa822024-07-03 04:33:48 -07006946}
yq.wang69a51872025-06-17 09:45:12 +08006947#endif
yq.wanga9efa822024-07-03 04:33:48 -07006948
liubin281ac462023-07-19 14:22:54 +08006949static void urc_msg_process(info_urc_msg_t *msg)
6950{
6951 uint8 *data = NULL;
6952 if(msg->data) {
6953 data = (uint8*)msg->data;
6954 }
6955 switch(msg->msg) {
6956 case INFO_URC_MSG_RADIO_STATE:
6957 {
6958 radio_state_change(msg->data, msg->data_len);
6959 // Reconfig APN while radio on.
6960 if(data[0]) {
6961 apn_prop_get();
6962 }
6963 break;
6964 }
6965 case INFO_URC_MSG_CGEV:
6966 {
yq.wangf98110c2025-06-11 14:50:04 +08006967 mbtk_pdp_cb_info_s *pdp_cb_info = (mbtk_pdp_cb_info_s *)(msg->data);
yq.wang69a51872025-06-17 09:45:12 +08006968 if(pdp_cb_info->cid >= MBTK_APN_CID_MIN && pdp_cb_info->cid <= MBTK_APN_CID_MAX)
6969 {
6970 net_ifc_state_change(pdp_cb_info->connect_state, pdp_cb_info->cid, NULL);
q.huange5f91fb2025-08-08 20:01:20 +08006971 if(cid_active[pdp_cb_info->cid]!=0) //LYNQ_ASR_BUG1946_Q.HUANG_20250808_ADD
6972 {
6973 cid_active[pdp_cb_info->cid] = 0;
6974 pdp_state_change(pdp_cb_info, sizeof(mbtk_pdp_cb_info_s));
6975 }
6976 else
6977 {
6978 LOGE("[%s] report cid disconnect, active flag has been 0", __func__, pdp_cb_info->cid); //LYNQ_ASR_BUG1946_Q.HUANG_20250808_ADD
6979 }
yq.wang69a51872025-06-17 09:45:12 +08006980 }
6981 else
6982 {
6983 LOGD("[%s] pdp_cb_info->cid unknown.[%d]", __func__, pdp_cb_info->cid);
6984 }
b.liuf77b86c2024-11-09 13:24:10 +08006985 break;
6986 }
6987 case INFO_URC_MSG_PDP_STATE:
6988 {
yq.wangf98110c2025-06-11 14:50:04 +08006989 //data call connect success
b.liuf77b86c2024-11-09 13:24:10 +08006990 int cme_err = MBTK_INFO_ERR_CME_NON;
yq.wangf98110c2025-06-11 14:50:04 +08006991 mbtk_pdp_cb_info_s *pdp_cb_info = (mbtk_pdp_cb_info_s *)(msg->data);
6992 if(req_data_call_state_get(pdp_cb_info->cid, &(pdp_cb_info->ipv4), &(pdp_cb_info->ipv6), &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
b.liuf77b86c2024-11-09 13:24:10 +08006993 {
yq.wangf98110c2025-06-11 14:50:04 +08006994 LOGE("[%s] req_data_call_state_get() fail.[%d]", __func__, cme_err);
liubin281ac462023-07-19 14:22:54 +08006995 }
yq.wangf98110c2025-06-11 14:50:04 +08006996 pdp_state_change(pdp_cb_info, sizeof(mbtk_pdp_cb_info_s));
liubin281ac462023-07-19 14:22:54 +08006997 break;
6998 }
6999 case INFO_URC_MSG_NET_PS_REG_STATE:
7000 {
b.liufe320632024-01-17 20:38:08 +08007001 uint8 net_data[4];
liubin281ac462023-07-19 14:22:54 +08007002 net_data[0] = (uint8)MBTK_NET_PS_STATE;
b.liufe320632024-01-17 20:38:08 +08007003 net_data[1] = data[0]; // act
7004 net_data[3] = data[1]; // 0 - GSM/WCDMA; 1 - LTE
liubin281ac462023-07-19 14:22:54 +08007005 mbtk_net_reg_state_enum state = (mbtk_net_reg_state_enum)data[0];
7006 if(state == MBTK_NET_REG_STATE_HOME
7007 || state == MBTK_NET_REG_STATE_ROAMING) { // Registered, home network or roaming.
7008 mbtk_net_info_t info;
7009 int cme_err = MBTK_INFO_ERR_CME_NON;
7010 memset(&info, 0, sizeof(mbtk_net_info_t));
7011 if(!req_net_sel_mode_get(&info, &cme_err) && cme_err == MBTK_INFO_ERR_CME_NON)
7012 {
7013 net_data[2] = info.net_type;
7014 net_state_change(net_data, sizeof(net_data));
yq.wang69a51872025-06-17 09:45:12 +08007015#if 0
liubin281ac462023-07-19 14:22:54 +08007016 if(info.net_type >= MBTK_RADIO_TECH_UTRAN) {
7017 data_call_restart();
7018 }
yq.wang69a51872025-06-17 09:45:12 +08007019#endif
liubin281ac462023-07-19 14:22:54 +08007020 } else {
7021 net_data[2] = (uint8)0xFF;
7022 net_state_change(net_data, sizeof(net_data));
7023 }
7024 } else {
7025 net_data[2] = (uint8)0xFF;
7026 net_state_change(net_data, sizeof(net_data));
7027 }
7028 break;
7029 }
b.liuf37bd332024-03-18 13:51:24 +08007030 case INFO_URC_MSG_SET_BAND:
7031 {
7032 mbtk_band_info_t band;
7033 int cme_err = MBTK_INFO_ERR_CME_NON;
7034
7035 band.net_pref = MBTK_NET_PREF_GSM_UMTS_LTE_LTE_PREF; // 15
b.liu288093c2024-05-09 17:02:57 +08007036 band.gsm_band = (uint16)band_set_info.band_gsm;
7037 band.umts_band = (uint16)band_set_info.band_wcdma;
b.liuf37bd332024-03-18 13:51:24 +08007038 band.tdlte_band = band_set_info.band_tdlte;
7039 band.fddlte_band = band_set_info.band_fddlte;
b.liu288093c2024-05-09 17:02:57 +08007040 band.lte_ext_band = band_set_info.band_lte_ext;
7041
b.liuf37bd332024-03-18 13:51:24 +08007042 if(req_band_set(&band, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
7043 {
7044 LOG("Set band fail.");
7045 }
7046 else // Set band success.
7047 {
b.liu45bfb532024-05-11 15:37:22 +08007048 // log_hex("BAND-2", &band_set_info, sizeof(band_set_info_t));
b.liu288093c2024-05-09 17:02:57 +08007049 band_set_success = TRUE;
7050 if(band_area == MBTK_MODEM_BAND_AREA_CN) {
b.liuf37bd332024-03-18 13:51:24 +08007051 property_set("persist.mbtk.band_config", "CN");
b.liu288093c2024-05-09 17:02:57 +08007052 } else if(band_area == MBTK_MODEM_BAND_AREA_EU) {
b.liuf37bd332024-03-18 13:51:24 +08007053 property_set("persist.mbtk.band_config", "EU");
b.liu288093c2024-05-09 17:02:57 +08007054 } else if(band_area == MBTK_MODEM_BAND_AREA_SA) {
b.liuf678f992024-05-08 15:23:10 +08007055 property_set("persist.mbtk.band_config", "SA");
b.liuf37bd332024-03-18 13:51:24 +08007056 } else {
7057 property_set("persist.mbtk.band_config", "ALL");
7058 }
7059 LOG("Set band success.");
7060 }
7061 break;
7062 }
wangyouqiangce45a102024-04-18 18:08:29 +08007063 case INFO_URC_MSG_GET_SIM_STATE:
7064 {
7065 net_info.sim_state = getSIMStatus();
7066 if(net_info.sim_state == MBTK_SIM_READY)
7067 {
7068 LOG("SIM READY!");
7069 }
7070 else
7071 {
7072 LOG("SIM NOT READY!");
7073 }
7074 break;
7075 }
liubin281ac462023-07-19 14:22:54 +08007076 case INFO_URC_MSG_NET_STATE_LOG:
7077 {
7078 // Get network state and signal.
7079 char buff[256] = {0};
q.huang8a993672025-06-24 16:19:46 +08007080 uint8 data_signal[9];
yq.wangf1fc31e2024-10-12 03:21:03 -07007081 mbtk_signal_info_t signal;
liubin281ac462023-07-19 14:22:54 +08007082 memset(&signal, 0xFF, sizeof(mbtk_signal_info_t));
7083 if(!req_net_signal_get(&signal, NULL)) {
7084 char tmp[50] = {0};
7085 struct timeval log_time;
7086 gettimeofday(&log_time, NULL);
7087 struct tm* tm_t = localtime(&(log_time.tv_sec));
7088 strftime(tmp, 50, "%F %T", tm_t);
q.huang8a993672025-06-24 16:19:46 +08007089 snprintf(buff, sizeof(buff), "%s:%d,%d,%d,%d,%d,%d,%d,%d,%d", tmp, signal.type, signal.rssi, signal.rxlev, signal.ber, signal.rscp, signal.ecno,
7090 signal.rsrq, signal.rsrp,signal.sinr);
b.liu06559f62024-11-01 18:48:22 +08007091 mbtk_signal_log(buff);
q.huang8a993672025-06-24 16:19:46 +08007092 if(signal_globe.type != signal.type)
7093 {
7094 data_signal[0] = signal.type;
7095 data_signal[1] = signal.rssi;
7096 data_signal[2] = signal.rxlev;
7097 data_signal[3] = signal.ber;
7098 data_signal[4] = signal.rscp;
7099 data_signal[5] = signal.ecno;
7100 data_signal[6] = signal.rsrq;
7101 data_signal[7] = signal.rsrp;
7102 data_signal[8] = signal.sinr;
7103 signal_globe.type = signal.type;
7104 signal_state_change(data_signal, sizeof(data_signal));
7105 }
7106 }
liubin281ac462023-07-19 14:22:54 +08007107 break;
7108 }
liuyang4d7ac4b2024-11-21 16:25:22 +08007109 case INFO_URC_MSG_CALL_STATE:
7110 {
7111 mbtk_call_info_t* reg = (mbtk_call_info_t*)data;
7112 char ceer[128] = {0};
7113 memset(&ceer, 0, 128);
7114 int cme_err = MBTK_INFO_ERR_CME_NON;
7115 if(req_ceer_call(ceer, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
7116 {
7117 LOG("get ceer call fail.");
7118 }
7119 else
7120 {
7121 memcpy(reg->end_reason, ceer, strlen(ceer));
7122 LOG(" reg->end_reason:%s", reg->end_reason);
7123 call_state_change(reg, sizeof(mbtk_call_info_t));
7124 }
7125
7126
7127 break;
7128 }
liubin281ac462023-07-19 14:22:54 +08007129 default:
7130 {
7131 LOGE("Unknown URC : %d", msg->msg);
7132 break;
7133 }
7134 }
7135}
7136
7137static void* pack_process_thread(void* arg)
7138{
7139 UNUSED(arg);
7140 info_queue_item_t* item = NULL;
7141 mbtk_queue_init(&info_queue);
7142 pthread_mutex_init(&info_mutex, NULL);
7143 pthread_cond_init(&info_cond, NULL);
7144
7145 memset(&band_support, 0xFF, sizeof(mbtk_band_info_t));
7146
7147 pthread_mutex_lock(&info_mutex);
7148 while(TRUE)
7149 {
7150 if(mbtk_queue_empty(&info_queue))
7151 {
7152 LOG("Packet process wait...");
7153 pthread_cond_wait(&info_cond, &info_mutex);
7154 LOG("Packet process continue...");
7155 }
7156 else
7157 {
7158 LOG("Packet process queue not empty,continue...");
7159 }
7160
7161 // Process all information request.
7162 mbtk_info_err_enum err;
7163 while((item = (info_queue_item_t*)mbtk_queue_get(&info_queue)) != NULL)
7164 {
7165 if(item->cli_info) { // REQ form client.
7166 mbtk_info_pack_t *pack = (mbtk_info_pack_t*)item->pack;
7167 LOG("Process REQ %s.", id2str(pack->info_id));
7168 at_process = true;
7169 err = pack_req_process(item->cli_info, pack);
7170 if(err != MBTK_INFO_ERR_SUCCESS)
7171 {
7172 if(item->cli_info->fd != DATA_CALL_BOOTCONN_FD)
7173 {
7174 pack_error_send(item->cli_info->fd, pack->info_id + 1, err);
7175 }
7176 else
7177 {
wangyouqiang80487e42024-05-24 15:06:20 +08007178 if(pack->data != NULL)
7179 {
7180 free(pack->data);
7181 }
7182 if(item->cli_info)
7183 {
7184 free(item->cli_info);
7185 }
liubin281ac462023-07-19 14:22:54 +08007186 }
7187 }
7188 at_process = false;
q.huanga0261872025-08-01 19:44:59 +08007189 at_cfun_command = false; //LYNQ_ASR_BUG2001_Q.HUANG_20250731_ADD
liubin281ac462023-07-19 14:22:54 +08007190 mbtk_info_pack_free(&pack);
7191 free(item);
7192 } else { // REQ from myself.
7193 info_urc_msg_t *urc = (info_urc_msg_t*)item->pack;
7194 LOG("Process URC %d.", urc->msg);
7195 urc_msg_process(urc);
7196 if(!urc->data)
7197 free(urc->data);
7198 free(urc);
7199 }
7200 }
7201 }
7202 pthread_mutex_unlock(&info_mutex);
7203 return NULL;
7204}
7205
7206void apn_prop_get()
7207{
7208 char prop_name[20];
7209 char prop_data[300];
7210 // cid : 2 - 7
7211 int cid = MBTK_APN_CID_MIN;
7212 mbtk_apn_info_t apn;
7213 for(; cid <= MBTK_APN_CID_MAX; cid++) {
7214 memset(prop_name, 0, 20);
7215 memset(prop_data, 0, 300);
7216 memset(&apn, 0, sizeof(mbtk_apn_info_t));
7217 sprintf(prop_name, "%s_%d",MBTK_APN_PROP,cid);
7218 if(property_get(prop_name, prop_data, "") > 0 && !str_empty(prop_data)) {
7219 apn.cid = cid;
7220 char *ptr_1 = prop_data;
7221 apn.ip_type = (mbtk_ip_type_enum)atoi(ptr_1);
7222 ptr_1 = strstr(ptr_1, ",");
7223 if(!ptr_1) {
7224 continue;
7225 }
7226 ptr_1++; // Jump ',' to apn
7227
7228 char *ptr_2 = strstr(ptr_1, ",");
7229 if(!ptr_2) {
7230 continue;
7231 }
7232 memcpy(apn.apn, ptr_1, ptr_2 - ptr_1); // apn
7233
7234 ptr_2++; // Jump ',' to user
7235 ptr_1 = strstr(ptr_2, ",");
7236 if(!ptr_1) {
7237 continue;
7238 }
7239 if(memcmp(ptr_2, "NULL", 4)) { // Not "NULL"
7240 memcpy(apn.user, ptr_2, ptr_1 - ptr_2); // user
7241 }
7242
7243 ptr_1++; // Jump ',' to pass
7244 ptr_2 = strstr(ptr_1, ",");
7245 if(!ptr_2) {
7246 continue;
7247 }
7248 if(memcmp(ptr_1, "NULL", 4)) { // Not "NULL"
7249 memcpy(apn.pass, ptr_1, ptr_2 - ptr_1); // pass
7250 }
7251
7252 ptr_2++; // Jump ',' to auth (Is last item)
7253 if(memcmp(ptr_2, "NULL", 4)) { // Not "NULL"
7254 memcpy(apn.auth, ptr_2, strlen(ptr_2)); // auth
7255 }
7256
7257 req_apn_set(&apn, NULL);
7258 }
7259 }
7260}
7261
b.liu9e8584b2024-11-06 19:21:28 +08007262#if 0
liubin281ac462023-07-19 14:22:54 +08007263/*
b.liue0ab2442024-02-06 18:53:28 +08007264root@OpenWrt:/usrdata# cat /proc/mtd
7265dev: size erasesize name
7266mtd0: 00040000 00020000 "bootloader"
7267mtd1: 00020000 00020000 "cp_reliabledata"
7268mtd2: 00020000 00020000 "ap_reliabledata"
7269mtd3: 00020000 00020000 "cp_reliabledata_backup"
7270mtd4: 00020000 00020000 "ap_reliabledata_backup"
7271mtd5: 00020000 00020000 "mep-ota"
7272mtd6: 00020000 00020000 "mep-ota_backup"
7273mtd7: 00040000 00020000 "dtim"
7274mtd8: 00f40000 00020000 "cpimage"
7275mtd9: 000c0000 00020000 "u-boot"
7276mtd10: 00500000 00020000 "kernel"
7277mtd11: 00100000 00020000 "asr_flag"
7278mtd12: 01400000 00020000 "rootfs"
7279mtd13: 01400000 00020000 "oem_data"
7280mtd14: 01e00000 00020000 "OTA"
7281mtd15: 01400000 00020000 "rootfs_data"
7282mtd16: 081a0000 00020000 "user_data"
7283mtd17: 00d20000 00020000 "MRVL_BBM"
7284*/
7285static int partition_name_2_dev(const char *name, char *dev) {
7286 if(name == NULL || dev == NULL) {
7287 LOGE("ARG error.");
q.huang8a993672025-06-24 16:19:46 +08007288 return -1;
b.liue0ab2442024-02-06 18:53:28 +08007289 }
7290
b.liu9e8584b2024-11-06 19:21:28 +08007291 FILE* fp = fopen("/proc/mtd", "r");
q.huang8a993672025-06-24 16:19:46 +08007292 if (fp == NULL) {
7293 LOGE("Open MTD failed!");
7294 return -1;
7295 }
b.liue0ab2442024-02-06 18:53:28 +08007296
7297 char buf[1024];
q.huang8a993672025-06-24 16:19:46 +08007298 while (fgets(buf, 1024, fp) != NULL) {
7299 if(strstr(buf, name)) {
b.liue0ab2442024-02-06 18:53:28 +08007300 int index = atoi(buf + 3);
7301 sprintf(dev, "/dev/mtdblock%d", index);
7302 LOGD("%s -> %s", name, dev);
7303 return 0;
q.huang8a993672025-06-24 16:19:46 +08007304 }
7305 }
b.liue0ab2442024-02-06 18:53:28 +08007306
7307 return -1;
7308}
b.liu9e8584b2024-11-06 19:21:28 +08007309#endif
7310#if 0
b.liue0ab2442024-02-06 18:53:28 +08007311static int custom_partition_read(const char *name, mbtk_cust_info_t *cust_info)
7312{
b.liu9e8584b2024-11-06 19:21:28 +08007313 int fd = 0;
7314 unsigned int flaglen = 0;
b.liue0ab2442024-02-06 18:53:28 +08007315 flaglen = sizeof(mbtk_cust_info_t);
7316
7317 char mtd_path[50] = {0};
7318 if(partition_name_2_dev(name, mtd_path)) {
7319 LOGE("partition_name_2_dev() failed!");
7320 return -1;
7321 }
7322
7323 fd = open(mtd_path, O_RDONLY);
7324 if (fd < 0) {
7325 LOGE("Fatal error: can't open cust info %s\n", mtd_path);
7326 return -1;
7327 }
7328
7329 if (read(fd, cust_info, flaglen) < 0)
7330 goto error;
7331 if (cust_info->header != CUST_INFO_HEADER) {
7332 LOGE("Cust info partition error.");
7333 goto error;
7334 } else {
7335 if(cust_info->band_type == 1) { // CN
7336 LOGD("Band : CN");
7337 } else if(cust_info->band_type == 2) { // EU
7338 LOGD("Band : EU");
7339 } else {
7340 LOGE("Unknown band type:%d", cust_info->band_type);
7341 goto error;
7342 }
7343 }
7344 close(fd);
7345 return 0;
7346error:
7347 close(fd);
7348 return -1;
7349}
b.liu9e8584b2024-11-06 19:21:28 +08007350#endif
b.liue0ab2442024-02-06 18:53:28 +08007351
7352/*
liubin281ac462023-07-19 14:22:54 +08007353AT*BAND=15,78,147,482,134742231
7354
7355OK
7356*/
b.liubb5e7682024-02-28 20:13:04 +08007357static void* band_config_thread()
liubin281ac462023-07-19 14:22:54 +08007358{
b.liubb5e7682024-02-28 20:13:04 +08007359 mbtk_device_info_modem_t info_modem;
b.liuf37bd332024-03-18 13:51:24 +08007360 memset(&band_set_info, 0, sizeof(band_set_info_t));
b.liubb5e7682024-02-28 20:13:04 +08007361 memset(&info_modem, 0, sizeof(mbtk_device_info_modem_t));
b.liu288093c2024-05-09 17:02:57 +08007362 band_set_success = FALSE;
b.liuf37bd332024-03-18 13:51:24 +08007363 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 +08007364 LOGD("mbtk_dev_info_read(MODEM) fail, use default band.");
b.liu288093c2024-05-09 17:02:57 +08007365 band_area = MBTK_MODEM_BAND_AREA_ALL;
b.liuf37bd332024-03-18 13:51:24 +08007366 band_set_info.band_gsm = MBTK_BAND_ALL_GSM_DEFAULT;
7367 band_set_info.band_wcdma = MBTK_BAND_ALL_WCDMA_DEFAULT;
7368 band_set_info.band_tdlte = MBTK_BAND_ALL_TDLTE_DEFAULT;
7369 band_set_info.band_fddlte = MBTK_BAND_ALL_FDDLTE_DEFAULT;
b.liu288093c2024-05-09 17:02:57 +08007370 band_set_info.band_lte_ext = MBTK_BAND_ALL_EXT_LTE_DEFAULT;
b.liubb5e7682024-02-28 20:13:04 +08007371 } else {
b.liub7530d22025-06-16 19:49:05 +08007372 if(info_modem.version == DEV_INFO_VERSION_V1) {
7373 band_area = info_modem.modem.v1.band_area;
7374 band_set_info.band_gsm = info_modem.modem.v1.band_gsm;
7375 band_set_info.band_wcdma = info_modem.modem.v1.band_wcdma;
7376 band_set_info.band_tdlte = info_modem.modem.v1.band_tdlte;
7377 band_set_info.band_fddlte = info_modem.modem.v1.band_fddlte;
7378 band_set_info.band_lte_ext = info_modem.modem.v1.band_lte_ext;
7379 } else {
7380 band_area = info_modem.modem.v2.band_area;
7381 band_set_info.band_gsm = info_modem.modem.v2.band_gsm;
7382 band_set_info.band_wcdma = info_modem.modem.v2.band_wcdma;
7383 band_set_info.band_tdlte = info_modem.modem.v2.band_tdlte;
7384 band_set_info.band_fddlte = info_modem.modem.v2.band_fddlte;
7385 band_set_info.band_lte_ext = info_modem.modem.v2.band_lte_ext;
7386 }
b.liuf37bd332024-03-18 13:51:24 +08007387 }
b.liubb5e7682024-02-28 20:13:04 +08007388
b.liuf37bd332024-03-18 13:51:24 +08007389 bool is_first = TRUE;
b.liu288093c2024-05-09 17:02:57 +08007390 while(!band_set_success) {
b.liuf37bd332024-03-18 13:51:24 +08007391 info_urc_msg_t *urc = (info_urc_msg_t*)malloc(sizeof(info_urc_msg_t));
7392 if(!urc)
7393 {
7394 LOG("malloc() fail[%d].", errno);
7395 break;
b.liubb5e7682024-02-28 20:13:04 +08007396 } else {
b.liuf37bd332024-03-18 13:51:24 +08007397 urc->msg = INFO_URC_MSG_SET_BAND;
7398 urc->data = NULL;
7399 urc->data_len = 0;
7400 send_pack_to_queue(NULL, urc);
7401
7402 if(is_first) {
7403 is_first = FALSE;
7404 } else {
b.liu288093c2024-05-09 17:02:57 +08007405 LOGE("*BAND exec error, will retry in 5s.");
b.liuf37bd332024-03-18 13:51:24 +08007406 }
b.liu288093c2024-05-09 17:02:57 +08007407 sleep(5);
b.liubb5e7682024-02-28 20:13:04 +08007408 }
7409 }
7410
b.liuf37bd332024-03-18 13:51:24 +08007411 LOGD("Set Band thread exit.");
b.liubb5e7682024-02-28 20:13:04 +08007412 return NULL;
liubin281ac462023-07-19 14:22:54 +08007413}
7414
yq.wang8333e172024-12-09 23:53:57 -08007415static void* reboot_reason_thread(void* arg)
7416{
7417 UNUSED(arg);
7418 int ret = 0;
7419 char buff[32] = {0};
7420 mbtk_device_info_reboot_flag_enum reboot_reason = MBTK_REBOOT_FLAG_NORMAL;
7421 mbtk_device_info_basic_t info_basic = {0};
7422 memset(&info_basic, 0, sizeof(mbtk_device_info_basic_t));
7423
7424
7425 ret = mbtk_dev_info_read(MBTK_DEVICE_INFO_ITEM_BASIC, &(info_basic), sizeof(mbtk_device_info_basic_t));
7426 if(ret != 0)
7427 {
7428 LOGE("[%s] mbtk_dev_info_read(BASIC) fail.", __func__);
7429 reboot_reason = MBTK_REBOOT_FLAG_UNKNOWN;
7430 }
7431 else
7432 {
b.liub7530d22025-06-16 19:49:05 +08007433 reboot_reason = info_basic.version == DEV_INFO_VERSION_V1 ? info_basic.basic.v1.reboot_flag :
7434 info_basic.basic.v2.reboot_flag;
7435 if(reboot_reason != MBTK_REBOOT_FLAG_NORMAL)
yq.wang8333e172024-12-09 23:53:57 -08007436 {
b.liub7530d22025-06-16 19:49:05 +08007437 if(info_basic.version == DEV_INFO_VERSION_V1) {
7438 info_basic.basic.v1.reboot_flag = MBTK_REBOOT_FLAG_NORMAL;
7439 } else {
7440 info_basic.basic.v2.reboot_flag = MBTK_REBOOT_FLAG_NORMAL;
7441 }
yq.wang8333e172024-12-09 23:53:57 -08007442 ret = mbtk_dev_info_write(MBTK_DEVICE_INFO_ITEM_BASIC, &info_basic, sizeof(mbtk_device_info_basic_t));
7443 if(ret != 0) {
7444 LOGE("[%s] mbtk_dev_info_write(BASIC) fail.", __func__);
7445 }
7446 }
7447 }
7448
7449 LOGD("[%s] reboot reason [%d].", __func__, reboot_reason);
7450 memset(buff, 0x0, 32);
7451 sprintf(buff, "%d", reboot_reason);
7452 property_set("persist.mbtk.reboot_reason", buff);
7453
7454 LOGD("[%s] reboot reason thread exit.", __func__);
7455 return NULL;
7456}
7457
7458
b.liu9e8584b2024-11-06 19:21:28 +08007459#if 0
liubin281ac462023-07-19 14:22:54 +08007460static void* net_monitor_thread(void* arg)
7461{
7462 UNUSED(arg);
7463 // Start network monitor
b.liu9e8584b2024-11-06 19:21:28 +08007464// int cid;
liubin281ac462023-07-19 14:22:54 +08007465 while(1) {
7466#if 0
7467 // Config IP
7468 list_node_t* apn_list = NULL;
7469 if(!apn_state_get(&apn_list) && apn_list != NULL) {
7470 info_apn_ip_t *apn = NULL;
7471 for(cid = MBTK_APN_CID_MIN; cid <= MBTK_APN_CID_MAX && cid_active[cid]; cid++) {
7472 bool ip_found = false;
7473 list_first(apn_list);
7474 while ((apn = (info_apn_ip_t*) list_next(apn_list))) {
7475 if(cid == apn->cid) {
7476 ip_found = true;
7477 break;
7478 }
7479 }
7480
7481 char dev[20] = {0};
7482 sprintf(dev, "ccinet%d", cid - 1);
7483 if(ip_found) { // Ip ok,set IP.
7484 if(apn->ipv4_valid) {
7485 if(mbtk_ifc_configure2(dev, (char*)apn->ipv4, 0, NULL, "255.255.255.0")) {
7486 LOGD("Config %s IPv4 %s fail.", dev, apn->ipv4);
7487 } else {
7488 LOGD("Config %s IPv4 %s success.", dev, apn->ipv4);
7489 }
7490 }
7491
7492 if(apn->ipv6_valid) {
7493 if(mbtk_ipv6_config(dev, (char*)apn->ipv6, 64)) {
7494 LOGD("Config %s IPv6 %s fail.", dev, apn->ipv6);
7495 } else {
7496 LOGD("Config %s IPv6 %s success.", dev, apn->ipv6);
7497 }
7498 }
7499 } else { // No ip
7500 if(mbtk_ifc_configure2(dev, NULL, 0, NULL, NULL)) {
7501 LOGD("Config %s IPv4 0 fail.", dev);
7502 } else {
7503 LOGD("Config %s IPv4 0 success.", dev);
7504 }
7505 }
7506 }
7507
7508 list_free(apn_list);
7509 }
7510#endif
7511
7512 if(net_info.radio_state == MBTK_RADIO_STATE_ON && net_info.sim_state == MBTK_SIM_READY) {
7513#if 0
7514 urc_msg_distribute(true, INFO_URC_MSG_NET_CS_REG_STATE, NULL, 0);
7515#else
7516 info_urc_msg_t *urc = (info_urc_msg_t*)malloc(sizeof(info_urc_msg_t));
7517 if(!urc)
7518 {
7519 LOG("malloc() fail[%d].", errno);
7520 } else {
7521 urc->msg = INFO_URC_MSG_NET_STATE_LOG;
7522 urc->data = NULL;
7523 urc->data_len = 0;
7524 send_pack_to_queue(NULL, urc);
7525 }
7526#endif
wangyouqiangce45a102024-04-18 18:08:29 +08007527 sleep(15);
liubin281ac462023-07-19 14:22:54 +08007528 }
wangyouqiangce45a102024-04-18 18:08:29 +08007529 else
7530 {
7531 info_urc_msg_t *urc = (info_urc_msg_t*)malloc(sizeof(info_urc_msg_t));
7532 if(!urc)
7533 {
7534 LOG("malloc() fail[%d].", errno);
7535 }
7536 else
7537 {
7538 urc->msg = INFO_URC_MSG_GET_SIM_STATE;
7539 urc->data = NULL;
7540 urc->data_len = 0;
7541 send_pack_to_queue(NULL, urc);
7542 }
7543 sleep(2);
7544 }
liubin281ac462023-07-19 14:22:54 +08007545 }
7546
7547 LOGD("monitor_thread exit.");
7548 return NULL;
7549}
b.liu9e8584b2024-11-06 19:21:28 +08007550#endif
liubin281ac462023-07-19 14:22:54 +08007551
b.liu9e8584b2024-11-06 19:21:28 +08007552#if 0
liubin281ac462023-07-19 14:22:54 +08007553static void* urc_process_thread(void* arg)
7554{
7555 UNUSED(arg);
7556 info_urc_msg_t* item = NULL;
7557 mbtk_queue_init(&urc_queue);
7558 pthread_mutex_init(&urc_mutex, NULL);
7559 pthread_cond_init(&urc_cond, NULL);
7560
7561 pthread_mutex_lock(&urc_mutex);
7562 while(TRUE)
7563 {
7564 if(mbtk_queue_empty(&urc_queue))
7565 {
7566 LOG("URC process wait...");
7567 pthread_cond_wait(&urc_cond, &urc_mutex);
7568 LOG("URC process continue...");
7569 }
7570 else
7571 {
7572 LOG("URC process queue not empty,continue...");
7573 }
7574
7575 // Process all information request.
7576 while((item = (info_urc_msg_t*)mbtk_queue_get(&urc_queue)) != NULL)
7577 {
7578 LOG("Process URC %d.", item->msg);
7579 uint8 *data = (uint8*)item->data;
7580 switch(item->msg) {
7581 case INFO_URC_MSG_RADIO_STATE:
7582 {
7583 radio_state_change(item->data, item->data_len);
7584 break;
7585 }
7586 case INFO_URC_MSG_CGEV:
7587 {
7588 bool act = data[0];
7589 int cid = data[1];
7590 if(cid > 0) {
7591 net_ifc_state_change(act, cid);
7592 }
7593 break;
7594 }
7595 default:
7596 {
7597 LOGE("Unknown URC : %d", item->msg);
7598 break;
7599 }
7600 }
7601 if(!item->data)
7602 free(item->data);
7603 free(item);
7604 }
7605 }
7606 pthread_mutex_unlock(&urc_mutex);
7607
7608 return NULL;
7609}
b.liu9e8584b2024-11-06 19:21:28 +08007610#endif
liubin281ac462023-07-19 14:22:54 +08007611
7612static void ril_at_ready_process()
7613{
7614 net_info.radio_state = (isRadioOn() == 1) ? MBTK_RADIO_STATE_ON : MBTK_RADIO_STATE_OFF;
7615#if 1
7616 if (net_info.radio_state != MBTK_RADIO_STATE_ON)
7617 {
7618 setRadioPower(1);
7619 } else { // Radio has ON
7620 apn_prop_get();
7621 }
7622
7623 if(net_info.radio_state == MBTK_RADIO_STATE_ON)
7624 {
7625 at_send_command("AT+CEREG=2", NULL);
7626 }
7627
b.liu9e8584b2024-11-06 19:21:28 +08007628// int count = 0;
liubin281ac462023-07-19 14:22:54 +08007629#endif
7630 net_info.sim_state = getSIMStatus();
7631#if 0
7632 while (net_info.sim_state != MBTK_SIM_READY && count < 30)
7633 {
7634 if(net_info.radio_state != MBTK_RADIO_STATE_ON)
7635 {
7636 setRadioPower(1);
7637 }
7638 LOGD("Waitting for SIM READY...");
7639 sleep(1);
7640 net_info.sim_state = getSIMStatus();
7641 count++;
7642 }
7643#endif
7644 if(net_info.sim_state == MBTK_SIM_READY)
7645 {
7646 LOGD("SIM READY!");
r.xiao7c62bc62024-05-24 00:53:30 -07007647 at_send_command("AT+COPS=3", NULL);
liubin281ac462023-07-19 14:22:54 +08007648 }
7649 else
7650 {
7651 LOGE("SIM NOT READY!");
7652 }
liubin281ac462023-07-19 14:22:54 +08007653}
7654
wangyouqiang80487e42024-05-24 15:06:20 +08007655int mbtk_get_apn_send_pack(void)
7656{
7657 sock_client_info_t *info = (sock_client_info_t*)malloc(sizeof(sock_client_info_t));
7658 if(info == NULL)
7659 {
7660 LOG("clinent_info malloc() fail.");
7661 return -1;
7662 }
7663 memset(info, 0, sizeof(sock_client_info_t));
7664 info->fd = DATA_CALL_APN_GET_FD;
7665
7666 mbtk_info_pack_t* pack = mbtk_info_pack_creat(MBTK_INFO_ID_NET_QSER_APN_REQ);
7667 if(pack == NULL)
7668 {
7669 free(info);
7670 LOG("Packet malloc() fail.");
7671 return -1;
7672 }
7673
7674 send_pack_to_queue(info, pack);
7675 return 0;
7676}
7677
liubin281ac462023-07-19 14:22:54 +08007678int mbtk_info_server_start()
7679{
7680 signal(SIGPIPE, SIG_IGN);
7681
wangyouqiangce45a102024-04-18 18:08:29 +08007682 //check cfun and sim card status
7683 ril_at_ready_process();
7684
7685 //any AT instruction that is not sent through pack_process_thread needs to precede the thread
7686 //thread create
liubin281ac462023-07-19 14:22:54 +08007687 if(sock_listen_fd > 0)
7688 {
7689 LOG("Information Server Has Started.");
7690 return -1;
7691 }
7692
7693 struct sockaddr_un server_addr;
7694 sock_listen_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
7695 if(sock_listen_fd < 0)
7696 {
7697 LOG("socket() fail[%d].", errno);
7698 return -1;
7699 }
7700
7701 // Set O_NONBLOCK
7702 int flags = fcntl(sock_listen_fd, F_GETFL, 0);
7703 if (flags < 0)
7704 {
7705 LOG("Get flags error:%d", errno);
7706 goto error;
7707 }
7708 flags |= O_NONBLOCK;
7709 if (fcntl(sock_listen_fd, F_SETFL, flags) < 0)
7710 {
7711 LOG("Set flags error:%d", errno);
7712 goto error;
7713 }
7714
7715 unlink(SOCK_INFO_PATH);
7716 memset(&server_addr, 0, sizeof(struct sockaddr_un));
7717 server_addr.sun_family = AF_LOCAL;
7718 strcpy(server_addr.sun_path, SOCK_INFO_PATH);
7719 if(bind(sock_listen_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)))
7720 {
7721 LOG("bind() fail[%d].", errno);
7722 goto error;
7723 }
7724
7725 if(listen(sock_listen_fd, SOCK_CLIENT_MAX))
7726 {
7727 LOG("listen() fail[%d].", errno);
7728 goto error;
7729 }
7730
7731 sock_client_list = list_create(sock_cli_free_func);
7732 if(sock_client_list == NULL)
7733 {
7734 LOG("list_create() fail.");
7735 goto error;
7736 }
7737
yq.wang8333e172024-12-09 23:53:57 -08007738 pthread_t info_pid, pack_pid/*, monitor_pid, urc_pid*/, bootconn_pid, reboot_reason_pid;
liubin281ac462023-07-19 14:22:54 +08007739 pthread_attr_t thread_attr;
7740 pthread_attr_init(&thread_attr);
7741 if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
7742 {
7743 LOG("pthread_attr_setdetachstate() fail.");
7744 goto error;
7745 }
7746
7747 if(pthread_create(&info_pid, &thread_attr, info_main_pthread, NULL))
7748 {
7749 LOG("pthread_create() fail.");
7750 goto error;
7751 }
7752
7753 if(pthread_create(&pack_pid, &thread_attr, pack_process_thread, NULL))
7754 {
7755 LOG("pthread_create() fail.");
7756 goto error;
7757 }
7758
7759#if 0
7760 if(pthread_create(&urc_pid, &thread_attr, urc_process_thread, NULL))
7761 {
7762 LOG("pthread_create() fail.");
7763 goto error;
7764 }
7765#endif
7766
b.liubb5e7682024-02-28 20:13:04 +08007767 // Set Band
7768 // AT*BAND=15,78,147,482,134742231
7769 char buff[10];
7770 memset(buff, 0, 10);
7771 property_get("persist.mbtk.band_config", buff, "");
7772 if(strlen(buff) == 0) {
7773 pthread_t band_pid;
7774 if(pthread_create(&band_pid, &thread_attr, band_config_thread, NULL))
7775 {
7776 LOG("pthread_create() fail.");
7777 }
7778 }
7779
yq.wang8333e172024-12-09 23:53:57 -08007780 if(pthread_create(&reboot_reason_pid, &thread_attr, reboot_reason_thread, NULL))
7781 {
7782 LOGE("[%s] reboot_reason pthread_create() fail.", __func__);
7783 goto error;
7784 }
b.liuf1ab8152024-05-23 13:16:07 +08007785#if 0
liubin281ac462023-07-19 14:22:54 +08007786 if(pthread_create(&monitor_pid, &thread_attr, net_monitor_thread, NULL))
7787 {
7788 LOG("pthread_create() fail.");
7789 }
b.liuf1ab8152024-05-23 13:16:07 +08007790#endif
liubin281ac462023-07-19 14:22:54 +08007791
7792 //mbtk wyq for data_call_ex add start
7793 if(pthread_create(&bootconn_pid, &thread_attr, data_call_bootconn_pthread, NULL))
7794 {
7795 LOG("pthread_create() fail.");
7796 }
7797 //mbtk wyq for data_call_ex add end
7798
7799 pthread_attr_destroy(&thread_attr);
7800
wangyouqiang80487e42024-05-24 15:06:20 +08007801 mbtk_qser_apn_init();
7802
liubin281ac462023-07-19 14:22:54 +08007803 LOG("MBTK Information Server Start...");
7804
7805 return 0;
7806
7807error:
7808 close(sock_listen_fd);
7809 sock_listen_fd = -1;
7810 return -1;
7811}
7812
7813#if 0
7814int main(int argc, char *argv[])
7815{
7816 if(mbtk_info_server_start())
7817 {
7818 return -1;
7819 }
7820
7821 while(1)
7822 {
7823 sleep(24 * 60 * 60);
7824 }
7825
7826 return 0;
7827}
7828#endif
7829
7830