blob: 346b2cfb63c514ba62258429329a4100b965bbc9 [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.
lichengzhangefd58e52025-08-29 19:30:24 +08002747 // 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 int err = 0;
liubin281ac462023-07-19 14:22:54 +08002753
2754 // Reset buffer in the first.
2755 memset(&cell_info, 0xFF, sizeof(mbtK_cell_pack_info_t));
2756 cell_info.running = true;
2757 cell_info.cell_num = 0;
2758
2759 err = at_send_command_singleline("AT+EEMGINFO?", "+EEMGINFO:", &response);
2760 if (err < 0 || response->success == 0 || !response->p_intermediates){
2761 *cme_err = at_get_cme_error(response);
2762 goto exit;
2763 }
2764
2765 // Now, cell infomation has get from URC message.
2766
2767 char *line = response->p_intermediates->line;
2768 err = at_tok_start(&line);
2769 if (err < 0)
2770 {
2771 goto exit;
2772 }
2773 err = at_tok_nextint(&line, &tmp_int);
2774 if (err < 0)
2775 {
2776 goto exit;
2777 }
2778 err = at_tok_nextint(&line, &tmp_int);
2779 if (err < 0)
2780 {
2781 goto exit;
2782 }
2783
2784 cell_info.type = (uint8)tmp_int;
2785 cell_info.running = false;
2786
2787#if 0
2788 while(lines_ptr)
2789 {
2790 // LTE
2791 if(strStartsWith(line, "+EEMLTESVC:")) // LTE Server Cell
2792 {
2793
2794 }
b.liue0ab2442024-02-06 18:53:28 +08002795 else if(strStartsWith(line, "+EEMLTEINTER:")) // LTE
liubin281ac462023-07-19 14:22:54 +08002796 {
2797
2798 }
b.liue0ab2442024-02-06 18:53:28 +08002799 else if(strStartsWith(line, "+EEMLTEINTRA:")) // LTE
liubin281ac462023-07-19 14:22:54 +08002800 {
2801
2802 }
b.liue0ab2442024-02-06 18:53:28 +08002803 else if(strStartsWith(line, "+EEMLTEINTERRAT:")) // LTE
liubin281ac462023-07-19 14:22:54 +08002804 {
2805
2806 }
2807 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
2808 // <nw_type>: 0: GSM 1: UMTS 2: LTE
2809 {
2810
2811 }
2812 // WCDMA
2813 else if(strStartsWith(line, "+EEMUMTSSVC:")) // WCDMA Server Cell
2814 {
2815
2816 }
b.liue0ab2442024-02-06 18:53:28 +08002817 else if(strStartsWith(line, "+EEMUMTSINTRA:")) // WCDMA
liubin281ac462023-07-19 14:22:54 +08002818 {
2819
2820 }
b.liue0ab2442024-02-06 18:53:28 +08002821 else if(strStartsWith(line, "+EEMUMTSINTERRAT:")) // WCDMA
liubin281ac462023-07-19 14:22:54 +08002822 {
2823
2824 }
2825 // GSM
2826 else if(strStartsWith(line, "+EEMGINFOBASIC:")) // Basic information in GSM
2827 // 0: ME in Idle mode 1: ME in Dedicated mode 2: ME in PS PTM mode
2828 {
2829
2830 }
2831 else if(strStartsWith(line, "+EEMGINFOSVC:")) // GSM Server Cell
2832 {
2833
2834 }
b.liue0ab2442024-02-06 18:53:28 +08002835 else if(strStartsWith(line, "+EEMGINFOPS:")) // PS
liubin281ac462023-07-19 14:22:54 +08002836 {
2837
2838 }
2839
2840
2841 lines_ptr = lines_ptr->p_next;
2842 }
2843#endif
2844
2845exit:
2846 at_response_free(response);
2847 return buff_size;
2848}
2849
2850static int req_cell_info_set(const char *cmgl, char *reg, int len, int *cme_err)
2851{
2852 printf("req_cmgl_set(2)-----------------start\n");
2853 printf("cmgl:%s\n", cmgl);
2854 ATResponse *response = NULL;
b.liu9e8584b2024-11-06 19:21:28 +08002855 char cmd[500] = {0};
q.huang8a993672025-06-24 16:19:46 +08002856 char data[218] = {0};
liubin281ac462023-07-19 14:22:54 +08002857 int err = 0;
2858
q.huang8a993672025-06-24 16:19:46 +08002859 memcpy(data, cmgl, len);
liubin281ac462023-07-19 14:22:54 +08002860
q.huang8a993672025-06-24 16:19:46 +08002861 sprintf(cmd, "at*cell=%s", data);
2862 printf("cmd:%s\n", cmd);
liubin281ac462023-07-19 14:22:54 +08002863
q.huang8a993672025-06-24 16:19:46 +08002864 if(strlen(cmd) > 0)
2865 {
liubin281ac462023-07-19 14:22:54 +08002866 err = at_send_command_multiline(cmd, "", &response);
q.huang8a993672025-06-24 16:19:46 +08002867 if (err < 0 || response->success == 0 || !response->p_intermediates){
2868 *cme_err = at_get_cme_error(response);
liubin281ac462023-07-19 14:22:54 +08002869 // printf("at_send_command_multiline() is err-----------------\n");
q.huang8a993672025-06-24 16:19:46 +08002870 goto exit;
2871 }
liubin281ac462023-07-19 14:22:54 +08002872
2873 ATLine* lines_ptr = response->p_intermediates;
2874 char *line = NULL;
b.liu9e8584b2024-11-06 19:21:28 +08002875// int reg_len = 0;
q.huang8a993672025-06-24 16:19:46 +08002876// bool flag = false;
liubin281ac462023-07-19 14:22:54 +08002877 while(lines_ptr)
2878 {
2879 line = lines_ptr->line;
2880 if(line ==NULL)
2881 {
2882 printf("line is null----------------------\n");
2883 }
2884 printf("-----line:%s\n", line);
2885
2886 lines_ptr = lines_ptr->p_next;
2887 }
q.huang8a993672025-06-24 16:19:46 +08002888 }
liubin281ac462023-07-19 14:22:54 +08002889 err = 0;
q.huang8a993672025-06-24 16:19:46 +08002890 memcpy(reg, "req_cell_info_set succss", strlen("req_cell_info_set succss"));
liubin281ac462023-07-19 14:22:54 +08002891exit:
2892 at_response_free(response);
2893 printf("req_cell_info_set()-----------------end\n");
2894 return err;
2895}
2896
2897
2898
2899/*
2900AT+CSQ
2901+CSQ: 31,99
2902
2903OK
2904
2905AT+CESQ
2906+CESQ: 60,99,255,255,20,61
2907
2908OK
2909
2910AT+COPS?
2911+COPS: 0,2,"46001",7
2912
2913OK
2914
2915*/
2916static int req_net_signal_get(mbtk_signal_info_t *signal, int *cme_err)
2917{
2918 ATResponse *response = NULL;
2919 int tmp_int;
2920 char *tmp_ptr = NULL;
2921 // AT+EEMOPT=1 in the first.
q.huang8a993672025-06-24 16:19:46 +08002922#if 0
liubin281ac462023-07-19 14:22:54 +08002923 int err = at_send_command_singleline("AT+CSQ", "+CSQ:", &response);
2924 if (err < 0 || response->success == 0 || !response->p_intermediates){
2925 if(cme_err != NULL)
2926 *cme_err = at_get_cme_error(response);
2927 err = -1;
2928 goto exit;
2929 }
2930
2931 char *line = response->p_intermediates->line;
2932 err = at_tok_start(&line);
2933 if (err < 0)
2934 {
2935 goto exit;
2936 }
2937 err = at_tok_nextint(&line, &tmp_int);
2938 if (err < 0)
2939 {
2940 goto exit;
2941 }
2942 signal->rssi = (uint8)tmp_int;
2943 at_response_free(response);
q.huang8a993672025-06-24 16:19:46 +08002944#endif
2945 int err = at_send_command_singleline("AT*CESQ", "*CESQ:", &response);
liubin281ac462023-07-19 14:22:54 +08002946 if (err < 0 || response->success == 0 || !response->p_intermediates){
2947 if(cme_err != NULL)
2948 *cme_err = at_get_cme_error(response);
2949 err = -1;
2950 goto exit;
2951 }
2952
q.huang8a993672025-06-24 16:19:46 +08002953 char* line = response->p_intermediates->line;
liubin281ac462023-07-19 14:22:54 +08002954 err = at_tok_start(&line);
2955 if (err < 0)
2956 {
2957 goto exit;
2958 }
2959 err = at_tok_nextint(&line, &tmp_int);
2960 if (err < 0)
2961 {
2962 goto exit;
2963 }
2964 signal->rxlev = (uint8)tmp_int;
2965
2966 err = at_tok_nextint(&line, &tmp_int);
2967 if (err < 0)
2968 {
2969 goto exit;
2970 }
2971 signal->ber = (uint8)tmp_int;
2972
2973 err = at_tok_nextint(&line, &tmp_int);
2974 if (err < 0)
2975 {
2976 goto exit;
2977 }
2978 signal->rscp = (uint8)tmp_int;
2979
2980 err = at_tok_nextint(&line, &tmp_int);
2981 if (err < 0)
2982 {
2983 goto exit;
2984 }
2985 signal->ecno = (uint8)tmp_int;
2986
2987 err = at_tok_nextint(&line, &tmp_int);
2988 if (err < 0)
2989 {
2990 goto exit;
2991 }
2992 signal->rsrq = (uint8)tmp_int;
2993
2994 err = at_tok_nextint(&line, &tmp_int);
2995 if (err < 0)
2996 {
2997 goto exit;
2998 }
2999 signal->rsrp = (uint8)tmp_int;
3000
q.huang8a993672025-06-24 16:19:46 +08003001
3002 err = at_tok_nextint(&line, &tmp_int);
3003 if (err < 0)
3004 {
3005 goto exit;
3006 }
3007 signal->sinr = (int8)tmp_int;
3008
liubin281ac462023-07-19 14:22:54 +08003009 at_response_free(response);
3010 err = at_send_command_singleline("AT+COPS?", "+COPS:", &response);
3011 if (err < 0 || response->success == 0 || !response->p_intermediates){
3012 if(cme_err != NULL)
3013 *cme_err = at_get_cme_error(response);
3014 err = -1;
3015 goto exit;
3016 }
3017 line = response->p_intermediates->line;
3018 err = at_tok_start(&line);
3019 if (err < 0)
3020 {
3021 goto exit;
3022 }
3023 err = at_tok_nextint(&line, &tmp_int);
3024 if (err < 0)
3025 {
3026 goto exit;
3027 }
3028 if(!at_tok_hasmore(&line)) {
3029 goto exit;
3030 }
3031 err = at_tok_nextint(&line, &tmp_int);
3032 if (err < 0)
3033 {
3034 goto exit;
3035 }
3036 err = at_tok_nextstr(&line, &tmp_ptr);
3037 if (err < 0)
3038 {
3039 goto exit;
3040 }
3041 err = at_tok_nextint(&line, &tmp_int);
3042 if (err < 0)
3043 {
3044 goto exit;
3045 }
3046 signal->type = (uint8)tmp_int;
3047 net_info.net_type = signal->type;
3048
3049exit:
3050 at_response_free(response);
3051 return err;
3052}
3053
3054/*
3055AT+CREG=3
3056OK
3057
3058AT+CREG?
3059+CREG: 3,1,"8330","06447340",7
3060
3061OK
3062
3063AT+CREG?
3064+CREG: 3,0
3065
3066OK
3067
3068AT+CEREG?
3069+CEREG: 3,1,"8330","06447340",7
3070
3071OK
3072
3073
3074AT+CIREG?
3075+CIREG: 2,1,15
3076
3077OK
3078
3079AT+CIREG?
3080+CIREG: 0
3081
3082OK
3083
3084
3085*/
3086static int req_net_reg_get(mbtk_net_reg_info_t *reg, int *cme_err)
3087{
3088 ATResponse *response = NULL;
3089 int tmp_int;
3090 char *tmp_str = NULL;
3091 int err = at_send_command("AT+CREG=3", &response);
3092 if (err < 0 || response->success == 0){
3093 *cme_err = at_get_cme_error(response);
3094 goto exit;
3095 }
3096 at_response_free(response);
3097
3098 err = at_send_command_multiline("AT+CREG?", "+CREG:", &response);
3099 if (err < 0 || response->success == 0 || !response->p_intermediates){
3100 *cme_err = at_get_cme_error(response);
3101 goto exit;
3102 }
3103
3104 char *line = response->p_intermediates->line;
3105 err = at_tok_start(&line);
3106 if (err < 0)
3107 {
3108 goto exit;
3109 }
3110 err = at_tok_nextint(&line, &tmp_int); // n
3111 if (err < 0)
3112 {
3113 goto exit;
3114 }
3115 err = at_tok_nextint(&line, &tmp_int);// stat
3116 if (err < 0)
3117 {
3118 goto exit;
3119 }
3120 reg->call_state = (uint8)tmp_int;
3121
3122 if(at_tok_hasmore(&line)) {
3123 err = at_tok_nextstr(&line, &tmp_str); // lac
3124 if (err < 0)
3125 {
yq.wang31f71842024-12-27 14:07:01 +08003126 LOGE("[%s] at_tok_nextstr get lac fail.[%d]", __func__, err);
liubin281ac462023-07-19 14:22:54 +08003127 goto exit;
3128 }
3129 reg->lac = strtol(tmp_str, NULL, 16);
3130
3131 err = at_tok_nextstr(&line, &tmp_str); // ci
3132 if (err < 0)
3133 {
yq.wang31f71842024-12-27 14:07:01 +08003134 LOGE("[%s] at_tok_nextstr get ci fail.[%d]", __func__, err);
liubin281ac462023-07-19 14:22:54 +08003135 goto exit;
3136 }
3137 reg->ci = strtol(tmp_str, NULL, 16);
3138
3139 err = at_tok_nextint(&line, &tmp_int);// AcT
3140 if (err < 0)
3141 {
yq.wang31f71842024-12-27 14:07:01 +08003142 LOGE("[%s] at_tok_nextstr get act fail.[%d]", __func__, err);
3143 //goto exit;
liubin281ac462023-07-19 14:22:54 +08003144 }
yq.wang31f71842024-12-27 14:07:01 +08003145 else
3146 {
3147 reg->type = (uint8)tmp_int;
3148 }
liubin281ac462023-07-19 14:22:54 +08003149 }
3150 at_response_free(response);
3151
yq.wang31f71842024-12-27 14:07:01 +08003152 if(reg->type == MBTK_RADIO_TECH_E_UTRAN)
3153 {
3154 err = at_send_command_multiline("AT+CEREG?", "+CEREG:", &response);
3155 if (err < 0 || response->success == 0 || !response->p_intermediates){
3156 *cme_err = at_get_cme_error(response);
3157 goto exit;
3158 }
liubin281ac462023-07-19 14:22:54 +08003159
yq.wang31f71842024-12-27 14:07:01 +08003160 line = response->p_intermediates->line;
3161 err = at_tok_start(&line);
liubin281ac462023-07-19 14:22:54 +08003162 if (err < 0)
3163 {
3164 goto exit;
3165 }
yq.wang31f71842024-12-27 14:07:01 +08003166 err = at_tok_nextint(&line, &tmp_int); // n
liubin281ac462023-07-19 14:22:54 +08003167 if (err < 0)
3168 {
3169 goto exit;
3170 }
yq.wang31f71842024-12-27 14:07:01 +08003171 err = at_tok_nextint(&line, &tmp_int);// stat
liubin281ac462023-07-19 14:22:54 +08003172 if (err < 0)
3173 {
3174 goto exit;
3175 }
yq.wang31f71842024-12-27 14:07:01 +08003176 reg->data_state = (uint8)tmp_int;
3177
3178 if(reg->lac == 0 && at_tok_hasmore(&line)) {
3179 err = at_tok_nextstr(&line, &tmp_str); // lac
3180 if (err < 0)
3181 {
3182 goto exit;
3183 }
3184 reg->lac = strtol(tmp_str, NULL, 16);
3185
3186 err = at_tok_nextstr(&line, &tmp_str); // ci
3187 if (err < 0)
3188 {
3189 goto exit;
3190 }
3191 reg->ci = strtol(tmp_str, NULL, 16);
3192
3193 err = at_tok_nextint(&line, &tmp_int);// AcT
3194 if (err < 0)
3195 {
3196 goto exit;
3197 }
3198 reg->type = (uint8)tmp_int;
3199 }
3200 at_response_free(response);
liubin281ac462023-07-19 14:22:54 +08003201 }
yq.wang31f71842024-12-27 14:07:01 +08003202 else
3203 {
3204 err = at_send_command_multiline("AT+CGREG?", "+CGREG:", &response);
3205 if (err < 0 || response->success == 0 || !response->p_intermediates){
3206 *cme_err = at_get_cme_error(response);
3207 goto exit;
3208 }
3209
3210 line = response->p_intermediates->line;
3211 err = at_tok_start(&line);
3212 if (err < 0)
3213 {
3214 goto exit;
3215 }
3216 err = at_tok_nextint(&line, &tmp_int); // n
3217 if (err < 0)
3218 {
3219 goto exit;
3220 }
3221 err = at_tok_nextint(&line, &tmp_int);// stat
3222 if (err < 0)
3223 {
3224 goto exit;
3225 }
3226 reg->data_state = (uint8)tmp_int;
3227
3228 if(reg->lac == 0 && at_tok_hasmore(&line)) {
3229 err = at_tok_nextstr(&line, &tmp_str); // lac
3230 if (err < 0)
3231 {
3232 goto exit;
3233 }
3234 reg->lac = strtol(tmp_str, NULL, 16);
3235
3236 err = at_tok_nextstr(&line, &tmp_str); // ci
3237 if (err < 0)
3238 {
3239 goto exit;
3240 }
3241 reg->ci = strtol(tmp_str, NULL, 16);
3242
3243 err = at_tok_nextint(&line, &tmp_int);// AcT
3244 if (err < 0)
3245 {
3246 goto exit;
3247 }
3248 reg->type = (uint8)tmp_int;
3249 }
3250 at_response_free(response);
3251 }
liubin281ac462023-07-19 14:22:54 +08003252
3253 err = at_send_command_multiline("AT+CIREG?", "+CIREG:", &response);
3254 if (err < 0 || response->success == 0 || !response->p_intermediates){
3255 reg->ims_state = (uint8)0;
3256 err = 0;
3257 goto exit;
3258 }
3259 line = response->p_intermediates->line;
3260 err = at_tok_start(&line);
3261 if (err < 0)
3262 {
3263 goto exit;
3264 }
3265 err = at_tok_nextint(&line, &tmp_int); // n/stat
3266 if (err < 0)
3267 {
3268 goto exit;
3269 }
3270 if(at_tok_hasmore(&line)) {
3271 err = at_tok_nextint(&line, &tmp_int);// stat
3272 if (err < 0)
3273 {
3274 goto exit;
3275 }
3276 reg->ims_state = (uint8)tmp_int;
3277 } else {
3278 reg->ims_state = (uint8)tmp_int;
3279 }
3280
3281exit:
3282 at_response_free(response);
3283 return err;
3284}
3285
r.xiao06db9a12024-04-14 18:51:15 -07003286
3287static int net_ims_set(uint8 reg, int *cme_err)
3288{
3289 ATResponse *response = NULL;
3290 char cmd[30] = {0};
3291 int err = -1;
3292
b.liufdf03172024-06-07 15:01:29 +08003293 sprintf(cmd, "AT+ACONFIG=\"IMSD=%d\"", reg);
r.xiao06db9a12024-04-14 18:51:15 -07003294 err = at_send_command(cmd, &response);
3295 LOG("cmd : %s", cmd);
3296
3297 if (err < 0 || response->success == 0){
3298 *cme_err = at_get_cme_error(response);
3299 goto exit;
3300 }
3301
3302exit:
3303 at_response_free(response);
3304 return err;
3305}
3306
3307
3308static int net_ims_get(int *reg, int *cme_err)
3309{
3310 ATResponse *response = NULL;
b.liu9e8584b2024-11-06 19:21:28 +08003311 int tmp_reg = 0;
b.liufdf03172024-06-07 15:01:29 +08003312 int err;
b.liu9e8584b2024-11-06 19:21:28 +08003313// char *tmp_str = NULL;
b.liufdf03172024-06-07 15:01:29 +08003314
3315 err = at_send_command_singleline("AT+ACONFIG?", "", &response);
3316 if (err < 0 || response->success == 0 || !response->p_intermediates){
3317 tmp_reg = 0;
3318 err = 0;
3319 goto exit;
3320 }
3321 if(response->p_intermediates->line) {
3322 char *ptr = strstr(response->p_intermediates->line, "IMSD=");
3323 if(ptr) {
3324 tmp_reg = atoi(ptr + strlen("IMSD="));
3325 }
3326 }
3327
3328 LOG("net_ims_get reg : %u", tmp_reg);
3329
3330exit:
3331 at_response_free(response);
3332 *reg = tmp_reg;
3333 return err;
3334}
3335
3336static int net_ims_reg_state_get(int *reg, int *cme_err)
3337{
3338 ATResponse *response = NULL;
3339 int tmp_int, tmp_reg = -1;
r.xiao06db9a12024-04-14 18:51:15 -07003340 int err;
b.liu9e8584b2024-11-06 19:21:28 +08003341// char *tmp_str = NULL;
r.xiao06db9a12024-04-14 18:51:15 -07003342
3343 err = at_send_command_multiline("AT+CIREG?", "+CIREG:", &response);
3344 if (err < 0 || response->success == 0 || !response->p_intermediates){
3345 tmp_reg = 0;
3346 err = 0;
3347 goto exit;
3348 }
3349 char *line = response->p_intermediates->line;
3350 err = at_tok_start(&line);
3351 if (err < 0)
3352 {
3353 goto exit;
3354 }
3355 err = at_tok_nextint(&line, &tmp_int); // n/stat
3356 if (err < 0)
3357 {
3358 goto exit;
3359 }
liuyangc4ca9592024-06-06 15:43:50 +08003360
b.liufdf03172024-06-07 15:01:29 +08003361 if(at_tok_hasmore(&line)) {
3362 err = at_tok_nextint(&line, &tmp_int);// stat
3363 if (err < 0)
3364 {
3365 goto exit;
3366 }
3367 tmp_reg = tmp_int;
3368 } else {
3369 tmp_reg = tmp_int;
3370 }
r.xiao06db9a12024-04-14 18:51:15 -07003371
3372 LOG("net_ims_get reg : %u", tmp_reg);
3373
3374exit:
3375 at_response_free(response);
3376 *reg = tmp_reg;
3377 return err;
3378}
3379
3380
b.liufdf03172024-06-07 15:01:29 +08003381
liubin281ac462023-07-19 14:22:54 +08003382/*
3383AT+CGDCONT?
3384+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,,,,
3385
3386+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
3387
3388OK
3389
3390
3391*/
wangyouqianged88c722023-11-22 16:33:43 +08003392#ifdef MBTK_AF_SUPPORT
3393mbtk_ip_type_enum default_iptype = MBTK_IP_TYPE_IPV4V6;
3394#endif
3395
liubin281ac462023-07-19 14:22:54 +08003396static int req_apn_get(void *data, int *data_len, int *cme_err)
3397{
3398 ATResponse *response = NULL;
3399 int err = at_send_command_multiline("AT+CGDCONT?", "+CGDCONT:", &response);
3400
3401 if (err < 0 || response->success == 0 || !response->p_intermediates){
3402 *cme_err = at_get_cme_error(response);
3403 goto exit;
3404 }
3405
3406 ATLine* lines_ptr = response->p_intermediates;
3407 char *line = NULL;
3408 int tmp_int;
3409 char *tmp_str = NULL;
3410 /*
3411 <apn_num[1]><cid[1]><ip_type[1]><apn_len[2]><apn><user_len[2]><user><pass_len[2]><pass><auth_len[2]><auth>...
3412 <cid[1]><ip_type[1]><apn_len[2]><apn><user_len[2]><user><pass_len[2]><pass><auth_len[2]><auth>
3413 */
3414 uint8* apn_num = (uint8*)data;
3415 uint8* data_ptr = (uint8*)(data + sizeof(uint8)); // Jump apn_num[1]
3416 mbtk_apn_info_t apn;
3417 while(lines_ptr)
3418 {
3419 line = lines_ptr->line;
3420 err = at_tok_start(&line);
3421 if (err < 0)
3422 {
3423 goto exit;
3424 }
3425
3426 err = at_tok_nextint(&line, &tmp_int); // cid
3427 if (err < 0)
3428 {
3429 goto exit;
3430 }
3431 // Only get CID 1-7
3432 if(tmp_int >= MBTK_APN_CID_MIN && tmp_int <= MBTK_APN_CID_MAX) {
3433 memset(&apn, 0x0, sizeof(mbtk_apn_info_t));
3434 apn.cid = tmp_int;
3435 *data_ptr++ = (uint8)tmp_int; // cid
3436
3437 err = at_tok_nextstr(&line, &tmp_str);// ip type
3438 if (err < 0)
3439 {
3440 goto exit;
3441 }
3442 if(!strcasecmp(tmp_str, "IP")) {
3443 *data_ptr++ = (uint8)MBTK_IP_TYPE_IP;
3444 apn.ip_type = MBTK_IP_TYPE_IP;
3445 } else if(!strcasecmp(tmp_str, "IPV6")) {
3446 *data_ptr++ = (uint8)MBTK_IP_TYPE_IPV6;
3447 apn.ip_type = MBTK_IP_TYPE_IPV6;
3448 } else if(!strcasecmp(tmp_str, "IPV4V6")) {
3449 *data_ptr++ = (uint8)MBTK_IP_TYPE_IPV4V6;
3450 apn.ip_type = MBTK_IP_TYPE_IPV4V6;
3451 } else {
3452 *data_ptr++ = (uint8)MBTK_IP_TYPE_PPP;
3453 apn.ip_type = MBTK_IP_TYPE_PPP;
3454 }
3455
wangyouqianged88c722023-11-22 16:33:43 +08003456#ifdef MBTK_AF_SUPPORT
3457 if(apn.cid == 1)
3458 {
3459 default_iptype = apn.ip_type;
3460 }
3461#endif
liubin281ac462023-07-19 14:22:54 +08003462 err = at_tok_nextstr(&line, &tmp_str); // apn
3463 if (err < 0)
3464 {
3465 goto exit;
3466 }
3467 if(str_empty(tmp_str)) {
3468 uint16_2_byte((uint16)0, data_ptr, false);
3469 data_ptr += sizeof(uint16);
3470 } else {
3471 uint16_2_byte((uint16)strlen(tmp_str), data_ptr, false);
3472 data_ptr += sizeof(uint16);
3473 memcpy(data_ptr, tmp_str, strlen(tmp_str));
3474 data_ptr += strlen(tmp_str);
3475 memcpy(apn.apn, tmp_str, strlen(tmp_str));
3476 }
3477
3478 if(apn_user_pass_set_by_cid(apn.cid, &apn)) {
3479 // user
3480 uint16_2_byte((uint16)0, data_ptr, false);
3481 data_ptr += sizeof(uint16);
3482
3483 // pass
3484 uint16_2_byte((uint16)0, data_ptr, false);
3485 data_ptr += sizeof(uint16);
3486
3487 // auth
3488 uint16_2_byte((uint16)0, data_ptr, false);
3489 data_ptr += sizeof(uint16);
3490 } else {
3491 // user
3492 if(str_empty(apn.user)) {
3493 uint16_2_byte((uint16)0, data_ptr, false);
3494 data_ptr += sizeof(uint16);
3495 } else {
b.liu9e8584b2024-11-06 19:21:28 +08003496 uint16_2_byte((uint16)strlen((char*)apn.user), data_ptr, false);
liubin281ac462023-07-19 14:22:54 +08003497 data_ptr += sizeof(uint16);
b.liu9e8584b2024-11-06 19:21:28 +08003498 memcpy(data_ptr, apn.user, strlen((char*)apn.user));
3499 data_ptr += strlen((char*)apn.user);
liubin281ac462023-07-19 14:22:54 +08003500 }
3501
3502 // pass
3503 if(str_empty(apn.pass)) {
3504 uint16_2_byte((uint16)0, data_ptr, false);
3505 data_ptr += sizeof(uint16);
3506 } else {
b.liu9e8584b2024-11-06 19:21:28 +08003507 uint16_2_byte((uint16)strlen((char*)apn.pass), data_ptr, false);
liubin281ac462023-07-19 14:22:54 +08003508 data_ptr += sizeof(uint16);
b.liu9e8584b2024-11-06 19:21:28 +08003509 memcpy(data_ptr, apn.pass, strlen((char*)apn.pass));
3510 data_ptr += strlen((char*)apn.pass);
liubin281ac462023-07-19 14:22:54 +08003511 }
3512
3513 // auth
3514 if(str_empty(apn.auth)) {
3515 uint16_2_byte((uint16)0, data_ptr, false);
3516 data_ptr += sizeof(uint16);
3517 } else {
b.liu9e8584b2024-11-06 19:21:28 +08003518 uint16_2_byte((uint16)strlen((char*)apn.auth), data_ptr, false);
liubin281ac462023-07-19 14:22:54 +08003519 data_ptr += sizeof(uint16);
b.liu9e8584b2024-11-06 19:21:28 +08003520 memcpy(data_ptr, apn.auth, strlen((char*)apn.auth));
3521 data_ptr += strlen((char*)apn.auth);
liubin281ac462023-07-19 14:22:54 +08003522 }
3523 }
3524
3525 (*apn_num)++;
3526 }
3527
3528 lines_ptr = lines_ptr->p_next;
3529 }
3530
3531 *data_len = data_ptr - (uint8*)data;
3532
3533 goto exit;
3534exit:
3535 at_response_free(response);
3536 return err;
3537}
3538
3539#if 0
3540/*
3541LTE APN
3542AT+CFUN=4
3543AT*CGDFLT=1,IP,"private.vpdn",1,0,1,0,0,0,0,0,0,0,1,0,0,1,0,0,0,1
3544AT*CGDFAUTH=1,2,"noc@njcc.vpdn.js","123456"
3545AT+CFUN=1
3546AT+CEREG?
3547AT+CGDCONT?
3548
35492/3G APN
3550AT+CGREG?
3551AT+CGDCONT=6,IP,"private.vpdn"
3552AT*AUTHREQ=6,2,"noc@njcc.vpdn.js","123456"
3553AT+CGDATA="",6
3554AT+CGDCONT?
3555*/
3556static int req_apn_set_username(mbtk_apn_info_t *apn, int *cme_err)
3557{
3558 ATResponse *response = NULL;
3559 char cmd[400] = {0};
3560 int index = 0;
3561 int err = 0;
3562
3563 err = at_send_command_singleline("AT+COPS?", "+COPS:", &response);
3564 if (err < 0 || response->success == 0 || !response->p_intermediates){
3565 if(cme_err != NULL)
3566 *cme_err = at_get_cme_error(response);
3567 err = -1;
3568 goto apn_set;
3569 }
3570
3571 int tmp_int = 0;
3572 int state=0;
3573 char cmd_buf[64];
3574 char *line = response->p_intermediates->line;
3575 err = at_tok_start(&line);
3576 if (err < 0)
3577 {
3578 goto apn_set;
3579 }
3580 err = at_tok_nextint(&line, &tmp_int);
3581 if (err < 0)
3582 {
3583 goto apn_set;
3584 }
3585 err = at_tok_nextint(&line, &tmp_int);
3586 if (err < 0)
3587 {
3588 goto apn_set;
3589 }
3590 err = at_tok_nextstr(&line, &cmd_buf);
3591 if (err < 0)
3592 {
3593 goto apn_set;
3594 }
3595 err = at_tok_nextint(&line, &tmp_int);
3596 if (err < 0)
3597 {
3598 goto apn_set;
3599 }
3600 else
3601 state = tmp_int;
3602
3603apn_set:
3604 at_response_free(response);
3605 *cme_err = MBTK_INFO_ERR_CME_NON;
3606 //if(state == 7 && apn->cid == 1) //LTE && cid = 1
3607 if(0) //LTE && cid = 1
3608 {
3609 err = at_send_command("AT+CFUN=4", &response);
3610 if (err < 0 || response->success == 0){
3611 *cme_err = at_get_cme_error(response);
3612 goto exit;
3613 }
3614 at_response_free(response);
3615
3616 memset(cmd, 0, 400);
3617 index = 0;
3618 index += sprintf(cmd, "AT*CGDFLT=%d,", 1);
3619 switch(apn->ip_type) {
3620 case MBTK_IP_TYPE_IP: {
3621 index += sprintf(cmd + index,"\"IP\",");
3622 break;
3623 }
3624 case MBTK_IP_TYPE_IPV6: {
3625 index += sprintf(cmd + index,"\"IPV6\",");
3626 break;
3627 }
3628 case MBTK_IP_TYPE_IPV4V6: {
3629 index += sprintf(cmd + index,"\"IPV4V6\",");
3630 break;
3631 }
3632 default: {
3633 index += sprintf(cmd + index,"\"PPP\",");
3634 break;
3635 }
3636 }
3637
3638 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);
3639 err = at_send_command(cmd, &response);
3640 if (err < 0 || response->success == 0){
3641 *cme_err = at_get_cme_error(response);
3642 goto exit;
3643 }
3644 at_response_free(response);
3645
3646 memset(cmd, 0, 400);
3647 int cmd_auth=0;
3648 if(strstr(apn->auth,"NONE"))
3649 cmd_auth = 0;
3650 else if(strstr(apn->auth,"PAP"))
3651 cmd_auth = 1;
3652 else if(strstr(apn->auth,"CHAP"))
3653 cmd_auth = 2;
3654 else if(strstr(apn->auth,"PAP AND CHAP"))
3655 cmd_auth = 3;
3656 else
3657 goto exit;
3658
3659 sprintf(cmd,"AT*CGDFAUTH=1,%d,%s,%s",cmd_auth,apn->user,apn->pass);
3660
3661 err = at_send_command(cmd, &response);
3662 if (err < 0 || response->success == 0){
3663 *cme_err = at_get_cme_error(response);
3664 goto exit;
3665 }
3666 }
3667 else //2/3G
3668 {
3669 memset(cmd,0,400);
3670 index = 0;
3671 index += sprintf(cmd, "AT+CGDCONT=%d,", apn->cid);
3672 switch(apn->ip_type) {
3673 case MBTK_IP_TYPE_IP: {
3674 index += sprintf(cmd + index,"\"IP\",");
3675 break;
3676 }
3677 case MBTK_IP_TYPE_IPV6: {
3678 index += sprintf(cmd + index,"\"IPV6\",");
3679 break;
3680 }
3681 case MBTK_IP_TYPE_IPV4V6: {
3682 index += sprintf(cmd + index,"\"IPV4V6\",");
3683 break;
3684 }
3685 default: {
3686 index += sprintf(cmd + index,"\"PPP\",");
3687 break;
3688 }
3689 }
3690 index += sprintf(cmd + index, "\"%s\"", apn->apn);
3691
3692 err = at_send_command(cmd, &response);
3693 if (err < 0 || response->success == 0){
3694 *cme_err = at_get_cme_error(response);
3695 goto exit;
3696 }
3697 at_response_free(response);
3698
3699 memset(cmd,0,400);
3700 int cmd_auth=0;
3701 if(strstr(apn->auth,"NONE"))
3702 cmd_auth = 0;
3703 else if(strstr(apn->auth,"PAP"))
3704 cmd_auth = 1;
3705 else if(strstr(apn->auth,"CHAP"))
3706 cmd_auth = 2;
3707 else if(strstr(apn->auth,"PAP AND CHAP"))
3708 cmd_auth = 3;
3709 else
3710 goto exit;
3711
3712 sprintf(cmd, "AT*AUTHREQ=%d,%d,%s,%s",apn->cid,cmd_auth,apn->user,apn->pass);
3713 err = at_send_command(cmd, &response);
3714 if (err < 0 || response->success == 0){
3715 *cme_err = at_get_cme_error(response);
3716 goto exit;
3717 }
3718 }
3719
3720exit:
3721 at_response_free(response);
3722 return err;
3723}
3724#endif
3725
q.huangac0e8a02025-08-29 17:11:41 +08003726
3727int req_apn_init_attach_apn_inner( mbtk_apn_info_t *apn, int *cme_err)
3728{
3729 ATResponse *response = NULL;
3730 char cmd[400] = {0};
3731 int index = 0;
3732 int err = 0;
3733
3734 if(cme_err==NULL)
3735 {
3736 LOGE("cme_err is NULL");
3737 return AT_ERROR_GENERIC;
3738 }
3739
3740 index += sprintf(cmd, "AT*CGDFLT=1,");//save to nvm
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 }
3759 if(strlen((char*)apn->apn) > 0) {
3760 index += sprintf(cmd + index,"\"%s\",,,,,,,,,,,,,,,,,,1", apn->apn);
3761 }
3762 else
3763 {
3764 index += sprintf(cmd + index,"\"%s\",,,,,,,,,,,,,,,,,,0", "");
3765 }
3766
3767 err = at_send_command(cmd, &response);
3768 if (err < 0 || response->success == 0){
3769 *cme_err = at_get_cme_error(response);
3770 goto exit;
3771 }
3772
3773 if(!str_empty(apn->user) || !str_empty(apn->pass)) {
3774 at_response_free(response);
3775 response = NULL;
3776 memset(cmd,0,400);
3777 int cmd_auth=0;
3778 if(strstr((char*)apn->auth,"NONE"))
3779 cmd_auth = 0;
3780 else if(strstr((char*)apn->auth,"PAP"))
3781 cmd_auth = 1;
3782 else if(strstr((char*)apn->auth,"CHAP"))
3783 cmd_auth = 2;
3784#if 0
3785 else if(strstr(apn->auth,"PAP AND CHAP"))
3786 cmd_auth = 3;
3787#endif
3788 else
3789 goto exit;
3790
3791 sprintf(cmd, "AT*CGDFAUTH=%d,%d,%s,%s",1,cmd_auth,apn->user,apn->pass);
3792 err = at_send_command(cmd, &response);
3793 if (err < 0 || response->success == 0){
3794 *cme_err = at_get_cme_error(response);
3795 goto exit;
3796 }
3797 }
3798exit:
3799 at_response_free(response);
3800 return err;
3801}
3802
3803
liubin281ac462023-07-19 14:22:54 +08003804/*
3805AT+CGDCONT=1,"IPV4V6","cmnet"
3806OK
3807
3808AT*CGDFLT=1,"IPv4v6","reliance.grevpdn.zj",,,,,,,,,,,,,,,,,,1
3809OK
3810
3811*/
3812static int req_apn_set(mbtk_apn_info_t *apn, int *cme_err)
3813{
3814 ATResponse *response = NULL;
3815 char cmd[400] = {0};
3816 int index = 0;
3817 int err = 0;
3818
q.huangac0e8a02025-08-29 17:11:41 +08003819 if(cme_err==NULL)
3820 {
3821 LOGE("cme_err is NULL");
3822 return AT_ERROR_GENERIC;
3823 }
3824
3825 if(apn->cid==1)
3826 {
3827 err=req_apn_init_attach_apn_inner(apn, cme_err);
3828 if(err || *cme_err != MBTK_INFO_ERR_CME_NON)
3829 {
3830 LOGE("Set initial attach APN fail, err is %d, cme_err is %d",err,*cme_err);
3831 goto exit;
3832 }
3833 }
3834
liubin281ac462023-07-19 14:22:54 +08003835 index += sprintf(cmd, "AT+CGDCONT=%d,", apn->cid);
3836 switch(apn->ip_type) {
3837 case MBTK_IP_TYPE_IP: {
3838 index += sprintf(cmd + index,"\"IP\",");
3839 break;
3840 }
3841 case MBTK_IP_TYPE_IPV6: {
3842 index += sprintf(cmd + index,"\"IPV6\",");
3843 break;
3844 }
3845 case MBTK_IP_TYPE_IPV4V6: {
3846 index += sprintf(cmd + index,"\"IPV4V6\",");
3847 break;
3848 }
3849 default: {
3850 index += sprintf(cmd + index,"\"PPP\",");
3851 break;
3852 }
3853 }
b.liu9e8584b2024-11-06 19:21:28 +08003854 if(strlen((char*)apn->apn) > 0) {
liubin281ac462023-07-19 14:22:54 +08003855 index += sprintf(cmd + index,"\"%s\"", apn->apn);
b.liudfec1e12024-06-06 16:38:59 +08003856 }
liubin281ac462023-07-19 14:22:54 +08003857
3858 err = at_send_command(cmd, &response);
3859 if (err < 0 || response->success == 0){
q.huangac0e8a02025-08-29 17:11:41 +08003860 *cme_err = at_get_cme_error(response);
liubin281ac462023-07-19 14:22:54 +08003861 goto exit;
3862 }
3863
3864 if(!str_empty(apn->user) || !str_empty(apn->pass)) {
3865 at_response_free(response);
xy.he9f275d62024-12-17 21:50:40 -08003866 response = NULL;
liubin281ac462023-07-19 14:22:54 +08003867 memset(cmd,0,400);
3868 int cmd_auth=0;
b.liu9e8584b2024-11-06 19:21:28 +08003869 if(strstr((char*)apn->auth,"NONE"))
liubin281ac462023-07-19 14:22:54 +08003870 cmd_auth = 0;
b.liu9e8584b2024-11-06 19:21:28 +08003871 else if(strstr((char*)apn->auth,"PAP"))
liubin281ac462023-07-19 14:22:54 +08003872 cmd_auth = 1;
b.liu9e8584b2024-11-06 19:21:28 +08003873 else if(strstr((char*)apn->auth,"CHAP"))
liubin281ac462023-07-19 14:22:54 +08003874 cmd_auth = 2;
3875#if 0
3876 else if(strstr(apn->auth,"PAP AND CHAP"))
3877 cmd_auth = 3;
3878#endif
3879 else
3880 goto exit;
3881
3882 sprintf(cmd, "AT*AUTHREQ=%d,%d,%s,%s",apn->cid,cmd_auth,apn->user,apn->pass);
3883 err = at_send_command(cmd, &response);
3884 if (err < 0 || response->success == 0){
3885 *cme_err = at_get_cme_error(response);
3886 goto exit;
3887 }
3888 }
3889
3890exit:
3891 at_response_free(response);
3892 return err;
3893}
3894
liuyang1cefd852024-04-24 18:30:53 +08003895static int req_apn_del(int data, int *cme_err)
liuyang0e49d9a2024-04-23 21:04:54 +08003896{
3897 ATResponse *response = NULL;
3898 char cmd[64]={0};
liuyang1cefd852024-04-24 18:30:53 +08003899 sprintf(cmd, "AT+CGDCONT=%d", data);
liuyang0e49d9a2024-04-23 21:04:54 +08003900 int err = at_send_command(cmd, &response);
3901 if (err < 0 || response->success == 0){
3902 if(cme_err) {
3903 *cme_err = at_get_cme_error(response);
3904 }
3905 goto exit;
3906 }
3907
3908exit:
3909 at_response_free(response);
3910 return err;
3911}
3912
3913
liubin281ac462023-07-19 14:22:54 +08003914int wait_cgact_complete(int timeout)
3915{
3916 int count = timeout * 10; // timeout * 1000 / 100
3917 int i = 0;
3918
3919 while(cgact_wait.waitting && i < count) {
3920 i++;
3921 usleep(100000); // 100ms
3922 }
3923
3924 if(i == count) { // Timeout
3925 return -1;
3926 } else {
3927 return 0;
3928 }
3929}
3930
b.liu9e8584b2024-11-06 19:21:28 +08003931#if 0
liubin281ac462023-07-19 14:22:54 +08003932/*
3933AT+CGDATA="",6
3934CONNECT
3935
3936OK
3937
3938AT+CFUN=1
3939
3940OK
3941
3942*/
3943static int req_data_call_user_start(int cid, int *cme_err)
3944{
3945 ATResponse *response = NULL;
3946 char cmd[400] = {0};
b.liu9e8584b2024-11-06 19:21:28 +08003947// int index = 0;
liubin281ac462023-07-19 14:22:54 +08003948 int err = 0;
3949
3950 err = at_send_command_singleline("AT+COPS?", "+COPS:", &response);
3951 if (err < 0 || response->success == 0 || !response->p_intermediates){
3952 if(cme_err != NULL)
3953 *cme_err = at_get_cme_error(response);
3954 err = -1;
3955 goto exit;
3956 }
3957
3958 int tmp_int;
b.liu9e8584b2024-11-06 19:21:28 +08003959 char *cmd_buf = NULL;
liubin281ac462023-07-19 14:22:54 +08003960 char *line = response->p_intermediates->line;
3961 err = at_tok_start(&line);
3962 if (err < 0)
3963 {
3964 goto exit;
3965 }
3966 err = at_tok_nextint(&line, &tmp_int);
3967 if (err < 0)
3968 {
3969 goto exit;
3970 }
3971 err = at_tok_nextint(&line, &tmp_int);
3972 if (err < 0)
3973 {
3974 goto exit;
3975 }
3976 err = at_tok_nextstr(&line, &cmd_buf);
3977 if (err < 0)
3978 {
3979 goto exit;
3980 }
3981 err = at_tok_nextint(&line, &tmp_int);
3982 if (err < 0)
3983 {
3984 goto exit;
3985 }
3986 at_response_free(response);
3987
3988 if(tmp_int == 7 && cid == 1) //LTE && cid = 1
3989 {
3990 ATResponse *response = NULL;
b.liu9e8584b2024-11-06 19:21:28 +08003991// char cmd[400] = {0};
liubin281ac462023-07-19 14:22:54 +08003992 int err = 0;
3993
3994 err = at_send_command("AT+CFUN=1", &response);
3995 if (err < 0 || response->success == 0){
3996 if(cme_err) {
3997 *cme_err = at_get_cme_error(response);
3998 }
3999 goto exit;
4000 }
4001 }
4002 else
4003 {
4004 ATResponse *response = NULL;
b.liu9e8584b2024-11-06 19:21:28 +08004005// char cmd[400] = {0};
4006 memset(cmd, 0, sizeof(cmd));
liubin281ac462023-07-19 14:22:54 +08004007 int err = 0;
4008 sprintf(cmd, "AT+CGDATA=\"\",%d", cid);
4009 err = at_send_command(cmd, &response);
4010 if (err < 0 || response->success == 0){
4011 if(cme_err) {
4012 *cme_err = at_get_cme_error(response);
4013 }
4014 goto exit;
4015 }
4016 }
4017
4018exit:
4019 at_response_free(response);
4020 return err;
4021}
b.liu9e8584b2024-11-06 19:21:28 +08004022#endif
liubin281ac462023-07-19 14:22:54 +08004023
4024/*
4025AT+CGACT?
4026+CGACT: 1,1
4027+CGACT: 8,1
4028OK
4029
4030AT+CGACT=1,<cid>
4031OK
4032
4033*/
4034static int req_data_call_start(int cid, int *cme_err)
4035{
4036 ATResponse *response = NULL;
4037 char cmd[400] = {0};
4038 int err = 0;
4039#if 0
4040 err = at_send_command_multiline("AT+CGACT?", "+CGACT:", &response);
4041 if (err < 0 || response->success == 0 || !response->p_intermediates){
4042 *cme_err = at_get_cme_error(response);
4043 goto exit;
4044 }
4045 ATLine* lines_ptr = response->p_intermediates;
4046 char *line = NULL;
4047 int tmp_int;
4048 while(lines_ptr)
4049 {
4050 line = lines_ptr->line;
4051 err = at_tok_start(&line);
4052 if (err < 0)
4053 {
4054 goto exit;
4055 }
4056
4057 err = at_tok_nextint(&line, &tmp_int); // cid
4058 if (err < 0)
4059 {
4060 goto exit;
4061 }
4062 if(tmp_int == cid) { // Found cid
4063 err = at_tok_nextint(&line, &tmp_int); // cid
4064 if (err < 0)
4065 {
4066 goto exit;
4067 }
4068 if(tmp_int == 1) { // This cid has active.
4069 goto net_config;
4070 } else {
4071 goto cid_active;
4072 }
4073 break;
4074 }
4075
4076 lines_ptr = lines_ptr->p_next;
4077 }
4078
4079 if(lines_ptr == NULL) { // No found this cid.
4080 LOGE("No found cid : %d", cid);
4081 goto exit;
4082 }
4083 at_response_free(response);
4084
4085 // Start active cid.
4086cid_active:
4087#endif
4088
4089 sprintf(cmd, "AT+CGACT=1,%d", cid);
4090 err = at_send_command(cmd, &response);
4091 if (err < 0 || response->success == 0){
4092 if(cme_err) {
4093 *cme_err = at_get_cme_error(response);
4094 }
4095 goto exit;
4096 }
4097
4098exit:
4099 at_response_free(response);
4100 return err;
4101}
4102
4103/*
4104AT+CGACT=0,<cid>
4105OK
4106
4107*/
4108static int req_data_call_stop(int cid, int *cme_err)
4109{
4110 ATResponse *response = NULL;
4111 char cmd[400] = {0};
4112 int err = 0;
4113#if 0
4114 err = at_send_command_multiline("AT+CGACT?", "+CGACT:", &response);
4115 if (err < 0 || response->success == 0 || !response->p_intermediates){
4116 *cme_err = at_get_cme_error(response);
4117 goto exit;
4118 }
4119 ATLine* lines_ptr = response->p_intermediates;
4120 char *line = NULL;
4121 int tmp_int;
4122 while(lines_ptr)
4123 {
4124 line = lines_ptr->line;
4125 err = at_tok_start(&line);
4126 if (err < 0)
4127 {
4128 goto exit;
4129 }
4130
4131 err = at_tok_nextint(&line, &tmp_int); // cid
4132 if (err < 0)
4133 {
4134 goto exit;
4135 }
4136 if(tmp_int == cid) { // Found cid
4137 err = at_tok_nextint(&line, &tmp_int); // cid
4138 if (err < 0)
4139 {
4140 goto exit;
4141 }
4142 if(tmp_int == 1) { // This cid has active.
4143 goto net_config;
4144 } else {
4145 goto cid_active;
4146 }
4147 break;
4148 }
4149
4150 lines_ptr = lines_ptr->p_next;
4151 }
4152
4153 if(lines_ptr == NULL) { // No found this cid.
4154 LOGE("No found cid : %d", cid);
4155 goto exit;
4156 }
4157 at_response_free(response);
4158
4159 // Start active cid.
4160cid_active:
4161#endif
4162
4163 sprintf(cmd, "AT+CGACT=0,%d", cid);
4164 err = at_send_command(cmd, &response);
4165 if (err < 0 || response->success == 0){
4166 *cme_err = at_get_cme_error(response);
4167 goto exit;
4168 }
4169
4170exit:
4171 at_response_free(response);
4172 return err;
4173}
4174
4175/*
4176IPv4 : 10.255.74.26
4177IPv6 : 254.128.0.0.0.0.0.0.0.1.0.2.144.5.212.239
4178*/
4179static bool is_ipv4(const char *ip)
4180{
4181 const char *ptr = ip;
4182 int count = 0;
4183 while(*ptr) {
4184 if(*ptr == '.')
4185 count++;
4186 ptr++;
4187 }
4188
4189 if(count == 3) {
4190 return true;
4191 } else {
4192 return false;
4193 }
4194}
4195
4196/*
4197AT+CGCONTRDP=1
4198+CGCONTRDP: 1,7,"cmnet-2.MNC000.MCC460.GPRS","10.255.74.26","","223.87.253.100","223.87.253.253","","",0,0
4199+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
4200
4201OK
4202
4203*/
4204static int req_data_call_state_get(int cid, mbtk_ipv4_info_t *ipv4, mbtk_ipv6_info_t *ipv6, int *cme_err)
4205{
4206 ATResponse *response = NULL;
4207 char cmd[50] = {0};
4208 int err = 0;
4209
4210 sprintf(cmd, "AT+CGCONTRDP=%d", cid);
4211
4212 err = at_send_command_multiline(cmd, "+CGCONTRDP:", &response);
4213 if (err < 0 || response->success == 0 || !response->p_intermediates){
4214 *cme_err = at_get_cme_error(response);
4215 goto exit;
4216 }
4217 ATLine* lines_ptr = response->p_intermediates;
4218 char *line = NULL;
4219 int tmp_int;
4220 char *tmp_ptr = NULL;
4221 while(lines_ptr)
4222 {
4223 line = lines_ptr->line;
4224 err = at_tok_start(&line);
4225 if (err < 0)
4226 {
4227 goto exit;
4228 }
4229
4230 err = at_tok_nextint(&line, &tmp_int); // cid
4231 if (err < 0)
4232 {
4233 goto exit;
4234 }
4235 err = at_tok_nextint(&line, &tmp_int); // bearer_id
4236 if (err < 0)
4237 {
4238 goto exit;
4239 }
4240 err = at_tok_nextstr(&line, &tmp_ptr); // APN
4241 if (err < 0)
4242 {
4243 goto exit;
4244 }
4245
4246 err = at_tok_nextstr(&line, &tmp_ptr); // IP
4247 if (err < 0 || str_empty(tmp_ptr))
4248 {
4249 goto exit;
4250 }
4251 if(is_ipv4(tmp_ptr)) {
4252 if(inet_pton(AF_INET, tmp_ptr, &(ipv4->IPAddr)) < 0) {
4253 LOGE("inet_pton() fail.");
4254 err = -1;
4255 goto exit;
4256 }
4257
4258 ipv4->valid = true;
4259 //log_hex("IPv4", &(ipv4->IPAddr), sizeof(struct in_addr));
4260 } else {
4261 if(str_2_ipv6(tmp_ptr, &(ipv6->IPV6Addr))) {
4262 LOGE("str_2_ipv6() fail.");
4263 err = -1;
4264 goto exit;
4265 }
4266
4267 ipv6->valid = true;
4268 //log_hex("IPv6", &(ipv6->IPV6Addr), 16);
4269 }
4270
4271 err = at_tok_nextstr(&line, &tmp_ptr); // Gateway
4272 if (err < 0)
4273 {
4274 goto exit;
4275 }
4276 if(!str_empty(tmp_ptr)) { // No found gateway
4277 if(is_ipv4(tmp_ptr)) {
4278 if(inet_pton(AF_INET, tmp_ptr, &(ipv4->GateWay)) < 0) {
4279 LOGE("inet_pton() fail.");
4280 err = -1;
4281 goto exit;
4282 }
4283
4284 //log_hex("IPv4", &(ipv4->GateWay), sizeof(struct in_addr));
4285 } else {
4286 if(str_2_ipv6(tmp_ptr, &(ipv6->GateWay))) {
4287 LOGE("str_2_ipv6() fail.");
4288 err = -1;
4289 goto exit;
4290 }
4291
4292 //log_hex("IPv6", &(ipv6->GateWay), 16);
4293 }
4294 }
4295
4296 err = at_tok_nextstr(&line, &tmp_ptr); // prim_DNS
4297 if (err < 0)
4298 {
4299 goto exit;
4300 }
4301 if(!str_empty(tmp_ptr)) { // No found Primary DNS
4302 if(is_ipv4(tmp_ptr)) {
4303 if(inet_pton(AF_INET, tmp_ptr, &(ipv4->PrimaryDNS)) < 0) {
4304 LOGE("inet_pton() fail.");
4305 err = -1;
4306 goto exit;
4307 }
4308
4309 //log_hex("IPv4", &(ipv4->PrimaryDNS), sizeof(struct in_addr));
4310 } else {
4311 if(str_2_ipv6(tmp_ptr, &(ipv6->PrimaryDNS))) {
4312 LOGE("str_2_ipv6() fail.");
4313 err = -1;
4314 goto exit;
4315 }
4316
4317 //log_hex("IPv6", &(ipv6->PrimaryDNS), 16);
4318 }
4319 }
4320
4321 err = at_tok_nextstr(&line, &tmp_ptr); // sec_DNS
4322 if (err < 0)
4323 {
4324 goto exit;
4325 }
4326 if(!str_empty(tmp_ptr)) { // No found Secondary DNS
4327 if(is_ipv4(tmp_ptr)) {
4328 if(inet_pton(AF_INET, tmp_ptr, &(ipv4->SecondaryDNS)) < 0) {
4329 LOGE("inet_pton() fail.");
4330 err = -1;
4331 goto exit;
4332 }
4333
4334 //log_hex("IPv4", &(ipv4->SecondaryDNS), sizeof(struct in_addr));
4335 } else {
4336 if(str_2_ipv6(tmp_ptr, &(ipv6->SecondaryDNS))) {
4337 LOGE("str_2_ipv6() fail.");
4338 err = -1;
4339 goto exit;
4340 }
4341
4342 //log_hex("IPv6", &(ipv6->SecondaryDNS), 16);
4343 }
4344 }
4345
4346 lines_ptr = lines_ptr->p_next;
4347 }
4348
4349exit:
4350 at_response_free(response);
4351 return err;
4352}
4353
b.liu9e8584b2024-11-06 19:21:28 +08004354#if 0
liubin281ac462023-07-19 14:22:54 +08004355/*
4356AT+CGCONTRDP
4357+CGCONTRDP: 1,5,"cmnet.MNC000.MCC460.GPRS","10.156.238.86","","223.87.253.100","223.87.253.253","","",0,0
4358+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
4359+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
4360OK
4361
4362*/
4363static int apn_state_get(list_node_t **apn_list)
4364{
4365 ATResponse *response = NULL;
4366 int err = 0;
4367 *apn_list = list_create(NULL);
4368 if(*apn_list == NULL)
4369 {
4370 LOG("list_create() fail.");
4371 return -1;
4372 }
4373
4374 err = at_send_command_multiline("AT+CGCONTRDP", "+CGCONTRDP:", &response);
4375 if (err < 0 || response->success == 0 || !response->p_intermediates){
4376 goto exit;
4377 }
4378 ATLine* lines_ptr = response->p_intermediates;
4379 char *line = NULL;
4380 int tmp_int;
4381 char *tmp_ptr = NULL;
4382 int cid_current = 0;
4383 info_apn_ip_t *apn = NULL;
4384 while(lines_ptr)
4385 {
4386 line = lines_ptr->line;
4387 err = at_tok_start(&line);
4388 if (err < 0)
4389 {
4390 goto exit;
4391 }
4392
4393 err = at_tok_nextint(&line, &tmp_int); // cid
4394 if (err < 0)
4395 {
4396 goto exit;
4397 }
4398
4399 if(tmp_int != 1 && tmp_int != 8) { // Not process cid 1 and 8.
4400 if(cid_current != tmp_int) { // New cid.
4401 apn = (info_apn_ip_t*)malloc(sizeof(info_apn_ip_t));
4402 if(apn == NULL) {
4403 goto exit;
4404 }
4405 memset(apn, 0, sizeof(info_apn_ip_t));
4406 apn->cid = tmp_int;
4407 cid_current = tmp_int;
4408
4409 list_add(*apn_list, apn);
4410 }
4411 err = at_tok_nextint(&line, &tmp_int); // bearer_id
4412 if (err < 0)
4413 {
4414 goto exit;
4415 }
4416 err = at_tok_nextstr(&line, &tmp_ptr); // APN
4417 if (err < 0)
4418 {
4419 goto exit;
4420 }
4421
4422 err = at_tok_nextstr(&line, &tmp_ptr); // IP
4423 if (err < 0 || str_empty(tmp_ptr))
4424 {
4425 goto exit;
4426 }
4427 if(is_ipv4(tmp_ptr)) {
4428 apn->ipv4_valid = true;
4429 memcpy(apn->ipv4, tmp_ptr, strlen(tmp_ptr));
4430 } else {
4431 apn->ipv6_valid = true;
4432 uint8 tmp_ipv6[16];
4433 if(str_2_ipv6(tmp_ptr, tmp_ipv6)) {
4434 LOGE("str_2_ipv6() fail.");
4435 err = -1;
4436 goto exit;
4437 }
4438
b.liu9e8584b2024-11-06 19:21:28 +08004439 if(inet_ntop(AF_INET6, tmp_ipv6, (char*)apn->ipv6, 50) == NULL) {
liubin281ac462023-07-19 14:22:54 +08004440 err = -1;
4441 LOGE("inet_ntop ipv6 ip fail.");
4442 goto exit;
4443 }
4444 }
4445 }
4446
4447 lines_ptr = lines_ptr->p_next;
4448 }
4449
4450exit:
4451 at_response_free(response);
4452 return err;
4453}
b.liu9e8584b2024-11-06 19:21:28 +08004454#endif
liubin281ac462023-07-19 14:22:54 +08004455
liuyang4d7ac4b2024-11-21 16:25:22 +08004456int req_ceer_call(char *reg, int *cme_err);
liubin281ac462023-07-19 14:22:54 +08004457mbtk_info_err_enum call_pack_req_process(sock_client_info_t* cli_info, mbtk_info_pack_t* pack);
4458mbtk_info_err_enum sms_pack_req_process(sock_client_info_t* cli_info, mbtk_info_pack_t* pack);
4459mbtk_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 -08004460
liubin281ac462023-07-19 14:22:54 +08004461//mbtk wyq for data_call_ex add start
4462void data_call_bootconn_save(int cid, int bootconn);
4463//mbtk wyq for data_call_ex add end
4464
4465//void net_list_free(void *data);
4466// Return MBTK_INFO_ERR_SUCCESS,will call pack_error_send() to send RSP.
4467// Otherwise, do not call pack_error_send().
4468static mbtk_info_err_enum pack_req_process(sock_client_info_t* cli_info, mbtk_info_pack_t* pack)
4469{
4470 if(pack->info_id > MBTK_INFO_ID_CALL_BEGIN && pack->info_id < MBTK_INFO_ID_CALL_END) {
4471 return call_pack_req_process(cli_info, pack);
4472 } else if(pack->info_id > MBTK_INFO_ID_SMS_BEGIN && pack->info_id < MBTK_INFO_ID_SMS_END) {
4473 return sms_pack_req_process(cli_info, pack);
4474 } else if(pack->info_id > MBTK_INFO_ID_PB_BEGIN && pack->info_id < MBTK_INFO_ID_PB_END) {
4475 return pb_pack_req_process(cli_info, pack);
b.liu06559f62024-11-01 18:48:22 +08004476 } else if(pack->info_id > RIL_MSG_ID_ECALL_BEGIN && pack->info_id < RIL_MSG_ID_ECALL_END) {
4477 return ecall_pack_req_process(cli_info, pack);
liubin281ac462023-07-19 14:22:54 +08004478 } else {
4479 mbtk_info_err_enum err = MBTK_INFO_ERR_SUCCESS;
4480 int cme_err = MBTK_INFO_ERR_CME_NON;
4481 switch(pack->info_id)
4482 {
4483 case MBTK_INFO_ID_DEV_IMEI_REQ: // <string> IMEI
4484 {
4485 if(pack->data_len == 0 || pack->data == NULL) // Get IMEI
4486 {
4487 char imei[20] = {0};
4488 if(req_imei_get(imei, &cme_err) || strlen(imei) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4489 {
4490 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4491 err = MBTK_INFO_ERR_CME + cme_err;
4492 } else {
4493 err = MBTK_INFO_ERR_UNKNOWN;
4494 }
4495 LOG("Get IMEI fail.");
4496 }
4497 else
4498 {
4499 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_IMEI_RSP, imei, strlen(imei));
4500 }
4501 }
4502 else // Set IMEI(Unsupport).
4503 {
4504 err = MBTK_INFO_ERR_UNSUPPORTED;
4505 LOG("Unsupport set IMEI.");
4506 }
4507 break;
4508 }
4509 case MBTK_INFO_ID_DEV_SN_REQ: // <string> SN
4510 {
4511 if(pack->data_len == 0 || pack->data == NULL) // Get SN
4512 {
4513 char sn[20] = {0};
4514 if(req_sn_get(sn, &cme_err) || strlen(sn) == 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 SN fail.");
4522 }
4523 else
4524 {
4525 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_SN_RSP, sn, strlen(sn));
4526 }
4527 }
4528 else // Set SN(Unsupport).
4529 {
4530 err = MBTK_INFO_ERR_UNSUPPORTED;
4531 LOG("Unsupport set SN.");
4532 }
4533 break;
4534 }
4535 case MBTK_INFO_ID_DEV_MEID_REQ: // <string> MEID (Only for CDMA)
4536 {
4537 if(pack->data_len == 0 || pack->data == NULL) // Get MEID
4538 {
4539 err = MBTK_INFO_ERR_UNSUPPORTED;
4540 LOG("Support only for CDMA.");
4541 }
4542 else // Set MEID(Unsupport).
4543 {
4544 err = MBTK_INFO_ERR_UNSUPPORTED;
4545 LOG("Unsupport set MEID.");
4546 }
4547 break;
4548 }
4549 case MBTK_INFO_ID_DEV_VERSION_REQ: // <string> VERSION
4550 {
4551 if(pack->data_len == 0 || pack->data == NULL) // Get VERSION
4552 {
4553 char version[50] = {0};
4554 if(req_version_get(version, &cme_err) || strlen(version) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4555 {
4556 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4557 err = MBTK_INFO_ERR_CME + cme_err;
4558 } else {
4559 err = MBTK_INFO_ERR_UNKNOWN;
4560 }
4561 LOG("Get Version fail.");
4562 }
4563 else
4564 {
4565 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_VERSION_RSP, version, strlen(version));
4566 }
4567 }
4568 else // Set VERSION(Unsupport).
4569 {
4570 err = MBTK_INFO_ERR_UNSUPPORTED;
4571 LOG("Unsupport set VERSION.");
4572 }
4573 break;
4574 }
l.yang5b0ff422024-10-29 19:33:35 -07004575 case MBTK_INFO_ID_DEV_MD_VERSION_REQ:
4576 {
4577 if(pack->data_len == 0 || pack->data == NULL) // Get VERSION
4578 {
4579 char version[50] = {0};
4580 if(req_md_version_get(version, &cme_err) || strlen(version) == 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 Version fail.");
4588 }
4589 else
4590 {
4591 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_MD_VERSION_RSP, version, strlen(version));
4592 }
4593 }
4594 else // Set MD_VERSION(Unsupport).
4595 {
4596 err = MBTK_INFO_ERR_UNSUPPORTED;
4597 LOG("Unsupport set MD_VERSION.");
4598 }
4599 break;
4600
4601 }
liubin281ac462023-07-19 14:22:54 +08004602 case MBTK_INFO_ID_DEV_MODEL_REQ: //MODEL
4603 {
4604 if(pack->data_len == 0 || pack->data == NULL) // Get MODEL
4605 {
4606 char model[50] = {0};
4607 if(req_model_get(model, &cme_err) || strlen(model) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4608 {
4609 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4610 err = MBTK_INFO_ERR_CME + cme_err;
4611 } else {
4612 err = MBTK_INFO_ERR_UNKNOWN;
4613 }
4614 LOG("Get model fail.");
4615 }
4616 else
4617 {
4618 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_VERSION_RSP, model, strlen(model));
4619 }
4620 }
4621 else // Set model(Unsupport).
4622 {
4623 err = MBTK_INFO_ERR_UNSUPPORTED;
4624 LOG("Unsupport set model.");
4625 }
4626 break;
4627 }
4628 case MBTK_INFO_ID_DEV_MODEM_REQ: //MODEM
4629 {
4630 if(pack->data_len == 0 || pack->data == NULL) // Get MODEM
4631 {
4632 int modem = -1;
4633 if(req_modem_get(&modem, &cme_err) || modem < 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4634 {
4635 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4636 err = MBTK_INFO_ERR_CME + cme_err;
4637 } else {
4638 err = MBTK_INFO_ERR_UNKNOWN;
4639 }
4640 LOG("Get modem fail.");
4641 }
4642 else
4643 {
4644 uint8 modem_type = (uint8)modem;
4645 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_MODEM_RSP, &modem_type, sizeof(uint8));
4646 }
4647 }
4648 else // Set modem
4649 {
4650 mbtk_modem_info_t *modem = (mbtk_modem_info_t *)pack->data;
4651 if(pack->data_len != sizeof(mbtk_modem_info_t))
4652 {
4653 err = MBTK_INFO_ERR_REQ_PARAMETER;
4654 LOG("Set modem error.");
4655 break;
4656 }
q.huanga0261872025-08-01 19:44:59 +08004657 at_cfun_command = true; //LYNQ_ASR_BUG2001_Q.HUANG_20250731_ADD
liubin281ac462023-07-19 14:22:54 +08004658 if(req_modem_set(modem, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4659 {
4660 LOG("Set modem fail.");
4661 err = MBTK_INFO_ERR_FORMAT;
4662 } else {
4663 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_MODEM_RSP, NULL, 0);
4664 }
4665 }
4666 break;
4667 }
4668 case MBTK_INFO_ID_DEV_TIME_REQ: // <uint8><string> YYYY-MM-DD-HH:MM:SS
4669 {
4670 if(pack->data_len == 0 || pack->data == NULL) // Get Time
4671 {
4672 int type = -1;
4673 if(req_time_get(&type, &cme_err) || type < 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4674 {
4675 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4676 err = MBTK_INFO_ERR_CME + cme_err;
4677 } else {
4678 err = MBTK_INFO_ERR_UNKNOWN;
4679 }
4680 LOG("Get Time fail.");
4681 }
4682 else
4683 {
4684 uint8 time_type = (uint8)type;
4685 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_TIME_RSP, &time_type, sizeof(uint8));
4686 }
4687 }
4688 else // Set Time
4689 {
4690 if(pack->data_len == sizeof(uint8)) {
4691 if(req_time_set(*(pack->data), NULL, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4692 {
4693 LOG("Set Time fail.");
4694 err = MBTK_INFO_ERR_FORMAT;
4695 } else {
4696 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_TIME_RSP, NULL, 0);
4697 }
4698 } else {
4699 char time_ptr[100] = {0};
4700 memcpy(time_ptr, pack->data + sizeof(uint8), pack->data_len - sizeof(uint8));
4701 if(req_time_set(*(pack->data), time_ptr, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4702 {
4703 LOG("Set Time fail.");
4704 err = MBTK_INFO_ERR_FORMAT;
4705 } else {
4706 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_TIME_RSP, NULL, 0);
4707 }
4708 }
4709 }
4710 break;
4711 }
b.liubaa41e12024-07-19 15:07:24 +08004712 case MBTK_INFO_ID_DEV_CELL_TIME_REQ: // <string> YYYY-MM-DD-HH:MM:SS
liubin281ac462023-07-19 14:22:54 +08004713 {
4714 if(pack->data_len == 0 || pack->data == NULL) // Get Time
4715 {
4716 char time[100];
4717 if(req_net_time_get(time, &cme_err) || strlen(time) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4718 {
4719 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4720 err = MBTK_INFO_ERR_CME + cme_err;
4721 } else {
4722 err = MBTK_INFO_ERR_UNKNOWN;
4723 }
4724 LOG("Get Time fail.");
4725 }
4726 else
4727 {
4728 char time_ser[100]={0};
4729 memcpy(time_ser,time,strlen(time));
b.liubaa41e12024-07-19 15:07:24 +08004730 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_CELL_TIME_RSP, time_ser, strlen(time_ser));
liubin281ac462023-07-19 14:22:54 +08004731 }
4732 }
4733 else // Set Time
4734 {
4735 err = MBTK_INFO_ERR_UNSUPPORTED;
4736 LOG("Unsupport set TIME.");
4737 }
4738 break;
4739 }
4740 case MBTK_INFO_ID_DEV_VOLTE_REQ: // <uint8> 0:Close 1:Open
4741 {
4742 if(pack->data_len == 0 || pack->data == NULL) // Get VoLTE state.
4743 {
4744 int state;
4745 if(req_volte_get(&state, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4746 {
4747 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4748 err = MBTK_INFO_ERR_CME + cme_err;
4749 } else {
4750 err = MBTK_INFO_ERR_UNKNOWN;
4751 }
4752 LOG("Get VoLTE state fail.");
4753 }
4754 else
4755 {
4756 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_VOLTE_RSP, &state, sizeof(uint8));
4757 }
4758 }
4759 else // Set VoLTE state.
4760 {
4761 uint8 on = *(pack->data);
4762 if(pack->data_len != sizeof(uint8) || (on != 0 && on != 1))
4763 {
4764 err = MBTK_INFO_ERR_REQ_PARAMETER;
4765 LOG("Set VOLTE parameter error.");
4766 break;
4767 }
4768
4769 if(req_volte_set(on, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4770 {
4771 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4772 err = MBTK_INFO_ERR_CME + cme_err;
4773 } else {
4774 err = MBTK_INFO_ERR_UNKNOWN;
4775 }
4776 LOG("Set VoLTE state fail.");
4777 }
4778 else
4779 {
4780 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_VOLTE_RSP, NULL, 0);
4781
4782 // Restart is required to take effect.
4783 LOG("Will reboot system...");
4784 }
4785 }
4786 break;
4787 }
4788 case MBTK_INFO_ID_DEV_TEMP_REQ: // <string> Temperature
4789 {
4790 if(pack->data_len == sizeof(uint8) && pack->data) {
r.xiao2102d762024-06-07 03:10:38 -07004791 mbtk_thermal_info_t temp;
4792 memset(&temp, 0, sizeof(mbtk_thermal_info_t));
liubin281ac462023-07-19 14:22:54 +08004793 if(req_temp_get(*(pack->data), &temp, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4794 {
4795 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4796 err = MBTK_INFO_ERR_CME + cme_err;
4797 } else {
4798 err = MBTK_INFO_ERR_UNKNOWN;
4799 }
4800 LOG("Get temperature fail.");
4801 }
4802 else
4803 {
r.xiao2102d762024-06-07 03:10:38 -07004804 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_TEMP_RSP, &temp, sizeof(mbtk_thermal_info_t));
liubin281ac462023-07-19 14:22:54 +08004805 }
4806 } else {
4807 err = MBTK_INFO_ERR_FORMAT;
4808 LOG("Unsupport get Temperature.");
4809 }
4810 break;
4811 }
4812 case MBTK_INFO_ID_SIM_PLMN_REQ: // plmn
4813 {
4814 if(pack->data_len == 0 || pack->data == NULL) // plmn
4815 {
4816 mbtk_plmn_info plmn;
4817 if(req_plmn_get(&plmn, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4818 {
4819 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4820 err = MBTK_INFO_ERR_CME + cme_err;
4821 } else {
4822 err = MBTK_INFO_ERR_UNKNOWN;
4823 }
4824 LOG("Get PLMN fail.");
4825 }
4826 else
4827 {
4828 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_PLMN_RSP, &plmn, sizeof(mbtk_plmn_info));
4829 }
4830 }
4831 else // Set
4832 {
4833 err = MBTK_INFO_ERR_UNSUPPORTED;
4834 LOG("Set sim state fail.");
4835 }
4836 break;
4837 }
4838 case MBTK_INFO_ID_SIM_STATE_REQ: // mbtk_sim_state_enum
4839 {
4840 if(pack->data_len == 0 || pack->data == NULL) // Get sim state.
4841 {
4842 uint8 sim_state = (uint8)getSIMStatus();
4843 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_STATE_RSP, &sim_state, sizeof(uint8));
4844 }
4845 else // Set
4846 {
4847 err = MBTK_INFO_ERR_UNSUPPORTED;
4848 LOG("Set sim state fail.");
4849 }
4850 break;
4851 }
4852 case MBTK_INFO_ID_SIM_STYPE_REQ: // mbtk_sim_card_type_enum
4853 {
4854 if(pack->data_len == 0 || pack->data == NULL) // Get sim card type
4855 {
4856 uint8 sim_card_type;
4857 if(req_sim_card_type_get(&sim_card_type, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4858 {
4859 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4860 err = MBTK_INFO_ERR_CME + cme_err;
4861 } else {
4862 err = MBTK_INFO_ERR_UNKNOWN;
4863 }
4864 LOG("Get IMSI fail.");
4865 }
4866 else
4867 {
4868 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_STYPE_RSP, &sim_card_type, sizeof(uint8));
4869 }
4870 }
4871 else // Set
4872 {
4873 err = MBTK_INFO_ERR_UNSUPPORTED;
4874 LOG("Set sim state fail.");
4875 }
4876 break;
4877 }
4878 case MBTK_INFO_ID_SIM_PINPUK_TIMES_REQ: // mbtk_pin_puk_last_times
4879 {
4880 if(pack->data_len == 0 || pack->data == NULL) // Get sim card type
4881 {
4882 mbtk_pin_puk_last_times pin_puk_last_times;
4883 if(req_pin_puk_last_times_get(&pin_puk_last_times, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4884 {
4885 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4886 err = MBTK_INFO_ERR_CME + cme_err;
4887 } else {
4888 err = MBTK_INFO_ERR_UNKNOWN;
4889 }
4890 LOG("Get IMSI fail.");
4891 }
4892 else
4893 {
4894 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_PINPUK_TIMES_RSP, &pin_puk_last_times, sizeof(mbtk_pin_puk_last_times));
4895 }
4896 }
4897 else // Set
4898 {
4899 err = MBTK_INFO_ERR_UNSUPPORTED;
4900 LOG("Set sim state fail.");
4901 }
4902 break;
4903 }
4904 case MBTK_INFO_ID_SIM_ENABLE_PIN_REQ: // <string> PIN
4905 {
4906 if(pack->data_len == 0 || pack->data == NULL) // Enable PIN
4907 {
yq.wang586a0df2024-10-24 20:10:37 -07004908 mbtk_pin_state_enum pin_state = MBTK_PIN_DISABLE;
4909 if(req_get_pin_state(&pin_state, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4910 {
4911 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4912 err = MBTK_INFO_ERR_CME + cme_err;
4913 } else {
4914 err = MBTK_INFO_ERR_UNKNOWN;
4915 }
4916 LOGE("Get pin state fail.");
4917 }
4918 else
4919 {
4920 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_ENABLE_PIN_RSP, &pin_state, sizeof(mbtk_pin_state_enum));
4921 }
liubin281ac462023-07-19 14:22:54 +08004922 }
4923 else // Enable PIN
4924 {
4925 mbtk_enable_pin_info *pin = NULL;
4926 pin = (mbtk_enable_pin_info *)pack->data;
4927 if(req_pin_enable(pin, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4928 {
4929 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4930 err = MBTK_INFO_ERR_CME + cme_err;
4931 } else {
4932 err = MBTK_INFO_ERR_UNKNOWN;
4933 }
4934 LOG("Get IMSI fail.");
4935 }
4936 else
4937 {
4938 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_ENABLE_PIN_RSP, NULL, 0);
4939 }
4940 }
4941 break;
4942 }
4943 case MBTK_INFO_ID_SIM_PIN_REQ: // <string> PIN
4944 {
4945 if(pack->data_len == 0 || pack->data == NULL) // PIN
4946 {
4947 err = MBTK_INFO_ERR_UNSUPPORTED;
4948 LOG("Unsupport GET PIN.");
4949 }
4950 else // Set PIN
4951 {
4952 char pin[16] = {0};
4953 memcpy(pin, pack->data, pack->data_len);
4954 if(req_pin_verify(pin, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4955 {
4956 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4957 err = MBTK_INFO_ERR_CME + cme_err;
4958 } else {
4959 err = MBTK_INFO_ERR_UNKNOWN;
4960 }
4961 LOG("Set PIN fail.");
4962 }
4963 else
4964 {
4965 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_PIN_RSP, NULL, 0);
4966 }
4967 }
4968 break;
4969 }
4970 case MBTK_INFO_ID_SIM_PUK_REQ: // <string> PUK
4971 {
4972 if(pack->data_len == 0 || pack->data == NULL)
4973 {
4974 err = MBTK_INFO_ERR_UNSUPPORTED;
4975 LOG("Unsupport.");
4976 }
4977 else // change PIN
4978 {
4979 mbtk_unlock_pin_info *pin_info = NULL;
4980 pin_info = (mbtk_unlock_pin_info *)pack->data;
4981 if(req_puk_unlock_pin(pin_info, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4982 {
4983 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4984 err = MBTK_INFO_ERR_CME + cme_err;
4985 } else {
4986 err = MBTK_INFO_ERR_UNKNOWN;
4987 }
4988 LOG("Get IMSI fail.");
4989 }
4990 else
4991 {
4992 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_CHANGE_PIN_RSP, NULL, 0);
4993 }
4994 }
4995 break;
4996 }
4997 case MBTK_INFO_ID_SIM_CHANGE_PIN_REQ: // <string> <string> old_PIN new_PIN
4998 {
4999 if(pack->data_len == 0 || pack->data == NULL)
5000 {
5001 err = MBTK_INFO_ERR_UNSUPPORTED;
5002 LOG("Unsupport.");
5003 }
5004 else // change PIN
5005 {
5006 mbtk_change_pin_info *pin_info = NULL;
5007 pin_info = (mbtk_change_pin_info *)pack->data;
5008 if(req_pin_change(pin_info, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5009 {
5010 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5011 err = MBTK_INFO_ERR_CME + cme_err;
5012 } else {
5013 err = MBTK_INFO_ERR_UNKNOWN;
5014 }
5015 LOG("Get IMSI fail.");
5016 }
5017 else
5018 {
5019 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_CHANGE_PIN_RSP, NULL, 0);
5020 }
5021 }
5022 break;
5023 }
5024 case MBTK_INFO_ID_SIM_IMSI_REQ: // <string> IMSI
5025 {
5026 if(pack->data_len == 0 || pack->data == NULL) // Get IMSI
5027 {
5028 char imsi[20] = {0};
5029 if(req_imsi_get(imsi, &cme_err) || strlen(imsi) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
5030 {
5031 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5032 err = MBTK_INFO_ERR_CME + cme_err;
5033 } else {
5034 err = MBTK_INFO_ERR_UNKNOWN;
5035 }
5036 LOG("Get IMSI fail.");
5037 }
5038 else
5039 {
5040 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_IMSI_RSP, imsi, strlen(imsi));
5041 }
5042 }
5043 else // Set IMSI(Unsupport).
5044 {
5045 err = MBTK_INFO_ERR_UNSUPPORTED;
5046 LOG("Unsupport set IMSI.");
5047 }
5048 break;
5049 }
5050 case MBTK_INFO_ID_SIM_ICCID_REQ: // <string> ICCID
5051 {
5052 if(pack->data_len == 0 || pack->data == NULL) // Get ICCID
5053 {
5054 //log_hex("pack", pack, sizeof(mbtk_info_pack_t));
5055 //sleep(1);
5056 char iccid[50] = {0};
5057 if(req_iccid_get(iccid, &cme_err) || strlen(iccid) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
5058 {
5059 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5060 err = MBTK_INFO_ERR_CME + cme_err;
5061 } else {
5062 err = MBTK_INFO_ERR_UNKNOWN;
5063 }
5064 LOG("Get ICCID fail.");
5065 }
5066 else
5067 {
5068 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_ICCID_RSP, iccid, strlen(iccid));
5069 }
5070 }
5071 else // Set ICCID(Unsupport).
5072 {
5073 err = MBTK_INFO_ERR_UNSUPPORTED;
5074 LOG("Unsupport set ICCID.");
5075 }
5076 break;
5077 }
5078 case MBTK_INFO_ID_SIM_PN_REQ: // <string> Phone Number
5079 {
5080 if(pack->data_len == 0 || pack->data == NULL) // Get Phone Number
5081 {
5082 //log_hex("pack", pack, sizeof(mbtk_info_pack_t));
5083 //sleep(1);
5084 char phone_number[50] = {0};
5085 if(req_phone_number_get(phone_number, &cme_err) || strlen(phone_number) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
5086 {
5087 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5088 err = MBTK_INFO_ERR_CME + cme_err;
5089 } else {
5090 err = MBTK_INFO_ERR_UNKNOWN;
5091 }
5092 LOG("Get Phone Number fail.");
5093 }
5094 else
5095 {
5096 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_PN_RSP, phone_number, strlen(phone_number));
5097 }
5098 }
5099 else // Set Phone Number(Unsupport).
5100 {
5101 err = MBTK_INFO_ERR_UNSUPPORTED;
5102 LOG("Unsupport set Phone Number.");
5103 }
5104 break;
5105 }
5106 case MBTK_INFO_ID_NET_SEL_MODE_REQ: // <mbtk_net_info_t> Operator
5107 {
5108 if(pack->data_len == 0 || pack->data == NULL) // Get
5109 {
5110 mbtk_net_info_t info;
5111 memset(&info, 0, sizeof(mbtk_net_info_t));
5112 if(req_net_sel_mode_get(&info, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5113 {
5114 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5115 err = MBTK_INFO_ERR_CME + cme_err;
5116 } else {
5117 err = MBTK_INFO_ERR_UNKNOWN;
5118 }
5119 LOG("Get Net select mode fail.");
5120 }
5121 else
5122 {
5123 LOG("NET : %d, %d, %d, %d", info.net_sel_mode, info.net_type, info.net_state, info.plmn);
5124 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_SEL_MODE_RSP, &info, sizeof(mbtk_net_info_t));
5125 }
5126 }
5127 else // Set
5128 {
5129 //LOG("1 pack-%p,data-%p,data_len-%d", pack, pack->data, pack->data_len);
5130 //log_hex("pack", pack, sizeof(mbtk_info_pack_t));
5131 //log_hex("data", pack->data, pack->data_len);
5132
5133 mbtk_net_info_t* info = (mbtk_net_info_t*)pack->data;//(mbtk_net_info_t*)mbtk_info_pack_data_get(pack, NULL);
5134 if(info == NULL) {
5135 err = MBTK_INFO_ERR_FORMAT;
5136 LOG("Get Net select mode fail.");
5137 } else {
5138 LOG("NET : %d, %d, %d", info->net_sel_mode, info->net_type, info->plmn);
5139 if(req_net_sel_mode_set(info, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON) {
5140 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5141 err = MBTK_INFO_ERR_CME + cme_err;
5142 } else {
5143 err = MBTK_INFO_ERR_UNKNOWN;
5144 }
5145 LOG("Get Net select mode fail.");
5146 } else {
5147 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_SEL_MODE_RSP, NULL, 0);
5148 }
5149 }
5150 }
5151 break;
5152 }
5153 case MBTK_INFO_ID_NET_AVAILABLE_REQ:// sel_mode(uint8)type(uint8)plmn(uint32)...sel_mode(uint8)type(uint8)plmn(uint32)
5154 {
5155 if(pack->data_len == 0 || pack->data == NULL) // Get Available Net.
5156 {
5157 int buffer_size;
5158 uint8 buffer[SOCK_MSG_LEN_MAX];
5159 memset(buffer, 0, SOCK_MSG_LEN_MAX);
5160 if((buffer_size = req_available_net_get(buffer, &cme_err)) <= 0 || cme_err != MBTK_INFO_ERR_CME_NON)
5161 {
5162 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5163 err = MBTK_INFO_ERR_CME + cme_err;
5164 } else {
5165 err = MBTK_INFO_ERR_UNKNOWN;
5166 }
5167 LOG("Get Available Net fail.");
5168 }
5169 else
5170 {
5171 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_AVAILABLE_RSP, buffer, buffer_size);
5172 }
5173 }
5174 else // Set Available Net(Unsupport).
5175 {
5176 err = MBTK_INFO_ERR_UNSUPPORTED;
5177 LOG("Unsupport set available net.");
5178 }
5179 break;
5180 }
5181 case MBTK_INFO_ID_NET_BAND_REQ:
5182 {
5183 if(pack->data_len == 0 || pack->data == NULL)
5184 {
5185 err = MBTK_INFO_ERR_REQ_PARAMETER;
5186 LOG("No data found.");
5187 }
5188 else // Get support/current bands.
5189 {
5190 if(pack->data_len == sizeof(uint8)) {
5191 if(*(pack->data)) { // Get current bands.
5192 mbtk_band_info_t band;
b.liu288093c2024-05-09 17:02:57 +08005193 memset(&band, 0x0, sizeof(mbtk_band_info_t));
liubin281ac462023-07-19 14:22:54 +08005194 if(req_band_get(&band, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5195 {
5196 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5197 err = MBTK_INFO_ERR_CME + cme_err;
5198 } else {
5199 err = MBTK_INFO_ERR_UNKNOWN;
5200 }
5201 LOG("Get net band fail.");
5202 }
5203 else
5204 {
5205 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_BAND_RSP, &band, sizeof(mbtk_band_info_t));
5206 }
5207 } else { // Get support bands.
5208 band_support_get();
5209 if(band_support.net_pref != 0)
5210 {
5211 err = MBTK_INFO_ERR_UNKNOWN;
5212 LOG("Get support bands fail.");
5213 }
5214 else
5215 {
5216 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_BAND_RSP, &band_support, sizeof(mbtk_band_info_t));
5217 }
5218 }
5219 } else { // Set current bands.
5220 mbtk_band_info_t* band = (mbtk_band_info_t*)pack->data;
5221 if(pack->data_len != sizeof(mbtk_band_info_t))
5222 {
5223 err = MBTK_INFO_ERR_REQ_PARAMETER;
5224 LOG("Set net band error.");
5225 break;
5226 }
5227
5228 if(req_band_set(band, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5229 {
5230 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5231 err = MBTK_INFO_ERR_CME + cme_err;
5232 } else {
5233 err = MBTK_INFO_ERR_UNKNOWN;
5234 }
5235 LOG("Set net band fail.");
5236 }
5237 else
5238 {
5239 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_BAND_RSP, NULL, 0);
5240 }
5241 }
5242 }
5243 break;
5244 }
5245 case MBTK_INFO_ID_NET_CELL_REQ: // mbtk_cell_info_t[]
5246 {
5247 if(pack->data_len == 0 || pack->data == NULL) // Get net cell.
5248 {
5249 if(req_cell_info_get(&cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5250 {
5251 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5252 err = MBTK_INFO_ERR_CME + cme_err;
5253 } else {
5254 err = MBTK_INFO_ERR_UNKNOWN;
5255 }
5256 LOG("Get net cell fail.");
5257 }
5258 else
5259 {
5260 LOG("req_cell_info_get() success,cell number: %d", cell_info.cell_num);
5261 //sleep(1);
5262 // mbtK_cell_pack_info_t
5263 if(cell_info.cell_num > 0 && cell_info.cell_num <= CELL_NUM_MAX && cell_info.type >= 0 && cell_info.type <= 2) {
5264 uint8 *data = (uint8*)malloc(sizeof(uint8) + sizeof(mbtk_cell_info_t) * cell_info.cell_num);
5265 if(data == NULL){
5266 err = MBTK_INFO_ERR_MEMORY;
5267 LOG("Get net cell fail.");
5268 } else {
5269 *data = cell_info.type; // Set network type.
5270 // Copy cell info item.
5271 #if 0
5272 int i = 0;
5273 while(i < cell_info.cell_num) {
5274 memcpy(data + sizeof(uint8) + sizeof(mbtk_cell_info_t) * i,
5275 &(cell_info.cell[i]),
5276 sizeof(mbtk_cell_info_t));
5277 i++;
5278 }
5279 #else
5280 memcpy(data + sizeof(uint8),
5281 &(cell_info.cell),
5282 sizeof(mbtk_cell_info_t) * cell_info.cell_num);
5283 #endif
5284 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_CELL_RSP, data, sizeof(uint8) + sizeof(mbtk_cell_info_t) * cell_info.cell_num);
5285 free(data);
5286 }
yq.wang31f71842024-12-27 14:07:01 +08005287 }else if(cell_info.cell_num == 0) {
5288 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_CELL_RSP, &cell_info.type, sizeof(uint8));
5289 }else {
liubin281ac462023-07-19 14:22:54 +08005290 err = MBTK_INFO_ERR_UNKNOWN;
5291 LOG("Get net cell fail.");
5292 }
5293 }
5294 }
5295 else // Lock cell
5296 {
q.huang8a993672025-06-24 16:19:46 +08005297 char *mem = (char*)(pack->data);
5298 int len = pack->data_len;
5299 char reg[100] = {0};
5300 printf("mem:%s, len:%d", pack->data, pack->data_len);
liubin281ac462023-07-19 14:22:54 +08005301
q.huang8a993672025-06-24 16:19:46 +08005302 if(req_cell_info_set(mem, reg, len, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5303 {
5304 // printf("cpms_set fail\n");
5305 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5306 err = MBTK_INFO_ERR_CME + cme_err;
5307 } else {
5308 err = MBTK_INFO_ERR_UNKNOWN;
5309 }
5310 // LOG("Set req_cell_info_set fail.");
5311 }
5312 else
5313 {
liubin281ac462023-07-19 14:22:54 +08005314
q.huang8a993672025-06-24 16:19:46 +08005315 printf("req_cell_info_set success, reg:%s\n", reg);
5316 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_CELL_RSP, reg, strlen(reg));
liubin281ac462023-07-19 14:22:54 +08005317
q.huang8a993672025-06-24 16:19:46 +08005318 // Restart is required to take effect.
5319 LOG("Will reboot system...");
5320 }
liubin281ac462023-07-19 14:22:54 +08005321 }
5322 break;
5323 }
5324 case MBTK_INFO_ID_NET_RADIO_REQ: // <uint8> 0:OFF 1:ON
5325 {
5326 if(pack->data_len == 0 || pack->data == NULL) // Get
5327 {
5328 uint8 radio_on = (uint8)isRadioOn();
5329 if(radio_on < 0)
5330 {
5331 err = MBTK_INFO_ERR_UNKNOWN;
5332 LOG("Get radio state fail.");
5333 }
5334 else
5335 {
5336 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_RADIO_RSP, &radio_on, sizeof(uint8));
5337 }
5338 }
5339 else // Set
5340 {
yq.wangd58f71e2024-08-21 23:45:31 -07005341 at_cfun_command = true;
liubin281ac462023-07-19 14:22:54 +08005342 uint8 radio_on = *(pack->data);
5343 if(radio_on != 0 && radio_on != 1)
5344 {
5345 err = MBTK_INFO_ERR_REQ_PARAMETER;
5346 LOG("Set radio state fail.");
5347 }
5348 else
5349 {
5350 setRadioPower(radio_on);
5351 if((radio_on && net_info.radio_state == MBTK_RADIO_STATE_ON)
5352 || (!radio_on && net_info.radio_state == MBTK_RADIO_STATE_OFF)) {
5353 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_RADIO_RSP, NULL, 0);
5354 } else {
5355 err = MBTK_INFO_ERR_UNKNOWN;
5356 LOG("Set radio state fail.");
5357 }
5358 }
5359 }
5360 break;
5361 }
5362 case MBTK_INFO_ID_NET_SIGNAL_REQ: // mbtk_signal_info_t
5363 {
5364 if(pack->data_len == 0 || pack->data == NULL) // Get net signal.
5365 {
5366 mbtk_signal_info_t signal;
5367 memset(&signal, 0, sizeof(mbtk_signal_info_t));
5368 if(req_net_signal_get(&signal, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5369 {
5370 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5371 err = MBTK_INFO_ERR_CME + cme_err;
5372 } else {
5373 err = MBTK_INFO_ERR_UNKNOWN;
5374 }
5375 LOG("Get net signal fail.");
5376 }
5377 else
5378 {
5379 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_SIGNAL_RSP, &signal, sizeof(mbtk_signal_info_t));
5380 }
5381 }
5382 else // Set
5383 {
5384 err = MBTK_INFO_ERR_UNSUPPORTED;
5385 LOG("Set net signal fail.");
5386 }
5387 break;
5388 }
5389 case MBTK_INFO_ID_NET_REG_REQ: // mbtk_net_reg_info_t
5390 {
5391 if(pack->data_len == 0 || pack->data == NULL) // Get net reg.
5392 {
5393 mbtk_net_reg_info_t reg;
5394 memset(&reg, 0, sizeof(mbtk_net_reg_info_t));
5395 if(req_net_reg_get(&reg, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5396 {
5397 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5398 err = MBTK_INFO_ERR_CME + cme_err;
5399 } else {
5400 err = MBTK_INFO_ERR_UNKNOWN;
5401 }
5402 LOG("Get net reg fail.");
5403 }
5404 else
5405 {
5406 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_REG_RSP, &reg, sizeof(mbtk_net_reg_info_t));
5407 }
5408 }
5409 else // Set
5410 {
5411 err = MBTK_INFO_ERR_UNSUPPORTED;
5412 LOG("Set net reg fail.");
5413 }
5414 break;
5415 }
5416 case MBTK_INFO_ID_NET_APN_REQ: // mbtk_apn_info_t
5417 {
5418 if(pack->data_len == 0 || pack->data == NULL) // Get APN
5419 {
5420 uint8 buff[SOCK_MSG_LEN_MAX];
5421 memset(buff, 0, SOCK_MSG_LEN_MAX);
5422 int data_len = 0;
5423 if(req_apn_get(buff, &data_len, &cme_err) || data_len <= 0 || 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 LOG("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 // Set
5438 {
5439 // <cid[1]><ip_type[1]><apn_len[2]><apn><user_len[2]><user><pass_len[2]><pass><auth_len[2]><auth>
5440 const uint8* ptr = pack->data;
5441 mbtk_apn_info_t apn;
5442 int len;
5443 memset(&apn, 0, sizeof(mbtk_apn_info_t));
5444 // cid
5445 apn.cid = *ptr++;
5446
5447 // ip_type
5448 apn.ip_type = (mbtk_ip_type_enum)(*ptr++);
5449
5450 // apn
5451 len = byte_2_uint16(ptr, false);
5452 ptr += sizeof(uint16);
5453 if(len > 0) {
5454 memcpy(apn.apn, ptr, len);
5455 ptr += len;
5456 }
5457
5458 // user
5459 len = byte_2_uint16(ptr, false);
5460 ptr += sizeof(uint16);
5461 if(len > 0) {
5462 memcpy(apn.user, ptr, len);
5463 ptr += len;
5464 }
5465
5466 // pass
5467 len = byte_2_uint16(ptr, false);
5468 ptr += sizeof(uint16);
5469 if(len > 0) {
5470 memcpy(apn.pass, ptr, len);
5471 ptr += len;
5472 }
5473
5474 // auth
5475 len = byte_2_uint16(ptr, false);
5476 ptr += sizeof(uint16);
5477 if(len > 0) {
5478 memcpy(apn.auth, ptr, len);
5479 ptr += len;
5480 }
5481
b.liu9e8584b2024-11-06 19:21:28 +08005482 LOGD("APN : %d, %d, %s, %s, %s, %s", apn.cid, apn.ip_type, str_empty(apn.apn) ? "NULL" : (char*)apn.apn,
5483 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 +08005484 if(req_apn_set(&apn, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5485 {
5486 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5487 err = MBTK_INFO_ERR_CME + cme_err;
5488 } else {
5489 err = MBTK_INFO_ERR_UNKNOWN;
5490 }
5491 LOG("Set APN fail.");
5492 }
5493 else
5494 {
5495 // Save apn.
wangyouqianged88c722023-11-22 16:33:43 +08005496#ifdef MBTK_AF_SUPPORT
5497 if(apn.cid == 1)
5498 {
5499 default_iptype = apn.ip_type;
5500 }
5501#endif
liubin281ac462023-07-19 14:22:54 +08005502 apn_prop_set(&apn);
5503
5504 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_APN_RSP, NULL, 0);
5505 }
5506 }
5507 break;
5508 }
wangyouqiang80487e42024-05-24 15:06:20 +08005509 case MBTK_INFO_ID_NET_QSER_APN_REQ:
5510 {
5511 if(pack->data_len == 0 || pack->data == NULL)
5512 {
5513 uint8 buff[SOCK_MSG_LEN_MAX];
5514 memset(buff, 0, SOCK_MSG_LEN_MAX);
5515 int data_len = 0;
5516 if(mbtk_qser_req_apn_get(buff, &data_len, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5517 {
5518 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5519 err = MBTK_INFO_ERR_CME + cme_err;
5520 } else {
5521 err = MBTK_INFO_ERR_UNKNOWN;
5522 }
5523 LOGE("Get APN fail.");
5524 }
5525 else
5526 {
5527 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_APN_RSP, buff, data_len);
5528 }
5529 }
5530 else
5531 {
b.liu9e8584b2024-11-06 19:21:28 +08005532// const uint8* ptr = pack->data;
wangyouqiang80487e42024-05-24 15:06:20 +08005533 mbtk_apn_info_t apn;
5534 mbtk_apn_req_type_enum req_type = MBTK_APN_REQ_TYPE_SET;
5535 uint8 apn_type[MBTK_QSER_APN_NAME_SIZE] = {0};
5536 int ret = mbtk_strdata_to_apn(pack->data, &apn, apn_type, &req_type);
5537 if(ret < 0)
5538 {
5539 LOGE("mbtk_strdata_to_apn fail. ret = [%d]", ret);
5540 err = MBTK_INFO_ERR_REQ_PARAMETER;
5541 }
5542 else
5543 {
5544 if(req_apn_set(&apn, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5545 {
5546 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5547 err = MBTK_INFO_ERR_CME + cme_err;
5548 } else {
5549 err = MBTK_INFO_ERR_UNKNOWN;
5550 }
5551 LOGE("Set APN fail.");
5552 }
5553 else
5554 {
5555 // Save apn.
5556 apn_prop_set(&apn);
5557 mbtk_qser_apn_save(apn, apn_type, true);
5558 if(req_type == MBTK_APN_REQ_TYPE_ADD)
5559 {
5560 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_QSER_APN_RSP, (void *)&apn.cid, 1);
5561 }
5562 else
5563 {
5564 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_QSER_APN_RSP, NULL, 0);
5565 }
5566 }
5567 }
5568 }
5569
5570 break;
5571 }
b.liu288093c2024-05-09 17:02:57 +08005572 case MBTK_INFO_ID_NET_APN_DEL_REQ:
liuyang0e49d9a2024-04-23 21:04:54 +08005573 {
b.liu288093c2024-05-09 17:02:57 +08005574 if(pack->data_len == 0 || pack->data == NULL)
liuyang0e49d9a2024-04-23 21:04:54 +08005575 {
5576 err = MBTK_INFO_ERR_UNSUPPORTED;
yq.wang9823ddf2024-11-14 02:38:14 -08005577 LOGE("[%s] Unsupported delete apn.", __func__);
liuyang0e49d9a2024-04-23 21:04:54 +08005578 }
b.liu288093c2024-05-09 17:02:57 +08005579 else
liuyang0e49d9a2024-04-23 21:04:54 +08005580 {
liuyang1cefd852024-04-24 18:30:53 +08005581 int profile = pack->data[0];
wangyouqiang80487e42024-05-24 15:06:20 +08005582 if(cid_active[profile] == 1)
liuyang0e49d9a2024-04-23 21:04:54 +08005583 {
yq.wang9823ddf2024-11-14 02:38:14 -08005584 LOGE("[%s] cid[%d] pdp already open.", __func__, profile);
wangyouqiang80487e42024-05-24 15:06:20 +08005585 err = MBTK_INFO_ERR_CID_EXIST;
wangyouqiang13e98402024-05-24 16:07:43 +08005586 break;
wangyouqiang80487e42024-05-24 15:06:20 +08005587 }
5588 if(mbtk_qser_apn_del(profile) < 0)
5589 {
yq.wang9823ddf2024-11-14 02:38:14 -08005590 LOGE("[%s] mbtk_qser_apn_del fail.", __func__);
wangyouqiang80487e42024-05-24 15:06:20 +08005591 err = MBTK_INFO_ERR_REQ_PARAMETER;
liuyang0e49d9a2024-04-23 21:04:54 +08005592 }
5593 else
5594 {
wangyouqiang80487e42024-05-24 15:06:20 +08005595 if(req_apn_del(profile, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5596 {
5597 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5598 err = MBTK_INFO_ERR_CME + cme_err;
5599 } else {
5600 err = MBTK_INFO_ERR_UNKNOWN;
5601 }
yq.wang9823ddf2024-11-14 02:38:14 -08005602 LOGE("[%s] Delete apn fail. [%d]", __func__, err);
wangyouqiang80487e42024-05-24 15:06:20 +08005603 }
5604 else
5605 {
yq.wang9823ddf2024-11-14 02:38:14 -08005606 apn_prop_del(profile);
wangyouqiang80487e42024-05-24 15:06:20 +08005607 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_APN_DEL_RSP, NULL, 0);
5608 }
liuyang0e49d9a2024-04-23 21:04:54 +08005609 }
5610 }
5611 break;
5612 }
liubin281ac462023-07-19 14:22:54 +08005613 case MBTK_INFO_ID_NET_DATA_CALL_REQ:
5614 {
5615 if(pack->data_len == 0 || pack->data == NULL)
5616 {
5617 err = MBTK_INFO_ERR_UNSUPPORTED;
5618 }
5619 else
5620 {
5621 /* <call_type[1]><cid[1]><auto_conn_interval[1]><boot_conn[1]><timeout[1]>
5622 call_type : mbtk_data_call_type_enum
5623 cid : 2 - 7
5624 timeout : second
5625 */
5626 mbtk_data_call_type_enum call_type = (mbtk_data_call_type_enum)pack->data[0];
5627 int cid = pack->data[1];
5628 int reconn = 0;
5629
wangyouqiang80487e42024-05-24 15:06:20 +08005630#if 0
liubin281ac462023-07-19 14:22:54 +08005631 if(cid < MBTK_APN_CID_MIN || cid > MBTK_APN_CID_MAX) {
5632 err = MBTK_INFO_ERR_CID;
5633 break;
5634 }
wangyouqiang80487e42024-05-24 15:06:20 +08005635#endif
5636 if(mbtk_check_cid(cid) < 0)
5637 {
5638 err = MBTK_INFO_ERR_CID;
5639 break;
5640 }
liubin281ac462023-07-19 14:22:54 +08005641
5642 LOG("cid_active[%d] = %d", cid, cid_active[cid]);
5643 memset(&cgact_wait, 0, sizeof(info_cgact_wait_t));
5644 switch(call_type) {
5645 case MBTK_DATA_CALL_START: {
5646 //mbtk wyq for data_call_ex add start
5647 int auto_conn_interval = pack->data[2];
5648 int boot_conn = pack->data[3];
5649 int timeout = pack->data[4];
5650 data_call_bootconn_save(cid, boot_conn);
b.liufe320632024-01-17 20:38:08 +08005651
wangyouqiang13e98402024-05-24 16:07:43 +08005652 mbtk_signal_info_t signal;
5653 memset(&signal, 0xFF, sizeof(mbtk_signal_info_t));
5654 req_net_signal_get(&signal, NULL);
b.liufe320632024-01-17 20:38:08 +08005655
liubin281ac462023-07-19 14:22:54 +08005656 if(cid_active[cid] == 1)
5657 {
5658 err = MBTK_INFO_ERR_CID_EXIST;
5659 break;
5660 }
yq.wang69a51872025-06-17 09:45:12 +08005661#if 0
wangyouqiang80487e42024-05-24 15:06:20 +08005662 if(mbtk_check_default_pdp_state(cid))
5663 {
5664 err = MBTK_INFO_ERR_UNSUPPORTED;
5665 break;
5666 }
yq.wang69a51872025-06-17 09:45:12 +08005667#endif
liubin281ac462023-07-19 14:22:54 +08005668 data_call_reconn:
5669 //mbtk wyq for data_call_ex add end
5670 cgact_wait.waitting = true;
5671 cgact_wait.cid = cid;
5672 cgact_wait.act = true;
5673 if(req_data_call_start(cid, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5674 {
5675 //mbtk wyq for data_call_ex add start
5676 if(reconn < 5 && auto_conn_interval > 0)
5677 {
5678 sleep(auto_conn_interval);
5679 reconn++;
5680 cme_err = MBTK_INFO_ERR_CME_NON;
5681 LOG("data_call restart call.");
5682 goto data_call_reconn;
5683 }
5684 //mbtk wyq for data_call_ex add end
5685 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5686 err = MBTK_INFO_ERR_CME + cme_err;
5687 } else {
5688 err = MBTK_INFO_ERR_UNKNOWN;
5689 }
5690 LOG("%d active fail.", cid);
5691 }
5692 else
5693 {
5694 // Wait for "CONNECT" or "+CGEV:"
5695 if(wait_cgact_complete(timeout)) { // Timeout
5696 err = MBTK_INFO_ERR_TIMEOUT;
5697 break;
5698 }
5699
5700 // Get IP information.
5701 mbtk_ipv4_info_t ipv4;
5702 mbtk_ipv6_info_t ipv6;
5703 memset(&ipv4, 0, sizeof(mbtk_ipv4_info_t));
5704 memset(&ipv6, 0, sizeof(mbtk_ipv6_info_t));
5705 if(req_data_call_state_get(cid, &ipv4, &ipv6, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5706 {
5707 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5708 err = MBTK_INFO_ERR_CME + cme_err;
5709 } else {
5710 err = MBTK_INFO_ERR_UNKNOWN;
5711 }
5712 LOG("Get %d state fail.", cid);
5713 }
5714 else
5715 {
5716 // Config IPv4 address.
wangyouqianged88c722023-11-22 16:33:43 +08005717#ifdef MBTK_AF_SUPPORT
5718 if(cid == 1)
5719 {
5720 //uint8 pdp_data = cid;
5721 //pack_rsp_send(cli_info->fd , MBTK_INFO_ID_IND_PDP_STATE_CHANGE, &pdp_data, sizeof(uint8));
5722 ipv4.valid = false;
5723 ipv6.valid = false;
5724 if(default_iptype == MBTK_IP_TYPE_IP)
5725 {
5726 ipv4.valid = true;
5727 }
5728 else if(default_iptype == MBTK_IP_TYPE_IPV6)
5729 {
5730 ipv6.valid = true;
5731 }
5732 else
5733 {
5734 ipv4.valid = true;
5735 ipv6.valid = true;
5736 }
5737 }
5738#endif
liubin281ac462023-07-19 14:22:54 +08005739#if 1
5740 if(ipv4.valid) {
5741 char dev[20] = {0};
5742 sprintf(dev, "ccinet%d", cid - 1);
5743
5744 char ip[20] = {0};
5745 char gateway[20] = {0};
5746 char *gateway_ptr = NULL;
5747 char netmask[20] = {0};
5748 char *netmask_ptr = NULL;
5749 if(inet_ntop(AF_INET, &(ipv4.IPAddr), ip, 20) == NULL) {
5750 err = MBTK_INFO_ERR_UNKNOWN;
5751 LOGE("inet_ntop ipv4 ip fail.");
5752 log_hex("IPv4", &(ipv4.IPAddr), 4);
5753 break;
5754 }
5755
5756 if(ipv4.GateWay) {
5757 if(inet_ntop(AF_INET, &(ipv4.GateWay), gateway, 20) == NULL) {
5758 err = MBTK_INFO_ERR_UNKNOWN;
5759 LOGE("inet_ntop ipv4 gateway fail.");
5760 log_hex("IPv4", &(ipv4.IPAddr), 4);
5761 break;
5762 } else {
5763 gateway_ptr = gateway;
5764 }
5765 }
5766
5767 if(ipv4.NetMask) {
5768 if(inet_ntop(AF_INET, &(ipv4.NetMask), netmask, 20) == NULL) {
5769 err = MBTK_INFO_ERR_UNKNOWN;
5770 LOGE("inet_ntop ipv4 netmask fail.");
5771 log_hex("IPv4", &(ipv4.IPAddr), 4);
5772 break;
5773 } else {
5774 netmask_ptr = netmask;
5775 }
5776 }
5777
5778 if(netmask_ptr == NULL) {
5779 netmask_ptr = netmask;
5780 memcpy(netmask_ptr, "255.255.255.0", strlen("255.255.255.0"));
5781 }
5782
5783 if(mbtk_ifc_configure2(dev, ip, 0, gateway_ptr, netmask_ptr)) {
5784 LOGD("Config %s IPv4 %s fail.", dev, ip);
5785 } else {
5786 LOGD("Config %s IPv4 %s success.", dev, ip);
5787 }
5788
5789 }
5790#endif
5791 // Config IPv6 address.
5792 if(ipv6.valid) {
5793 char ip[50] = {0};
5794 char dev[20] = {0};
5795 sprintf(dev, "ccinet%d", cid - 1);
5796
5797 if(inet_ntop(AF_INET6, &(ipv6.IPV6Addr), ip, 50) == NULL) {
5798 err = MBTK_INFO_ERR_UNKNOWN;
5799 LOGE("inet_ntop ipv6 ip fail.");
5800 log_hex("IPv6", &(ipv6.IPV6Addr), 16);
5801 break;
5802 }
5803
5804 if(mbtk_ipv6_config(dev, ip, 64)) {
5805 LOGD("Config %s IPv6 %s fail.", dev, ip);
5806 } else {
5807 LOGD("Config %s IPv6 %s success.", dev, ip);
5808 }
5809 }
5810
5811 cid_active[cid] = 1;
yq.wang69a51872025-06-17 09:45:12 +08005812 //mbtk_set_default_pdp_state(true, cid);
yq.wangc98d1342025-06-13 17:32:06 +08005813 //mbtk_qser_route_config(cid, &ipv4, &ipv6);
liubin281ac462023-07-19 14:22:54 +08005814 if(cli_info->fd != DATA_CALL_BOOTCONN_FD)
5815 {
liuyange134d842024-06-27 17:34:02 +08005816 mbtk_net_led_set(MBTK_NET_LED_DATA_CONNECT);
liubin281ac462023-07-19 14:22:54 +08005817 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_DATA_CALL_RSP, NULL, 0);
yq.wangc98d1342025-06-13 17:32:06 +08005818#ifdef MBTK_ALL_CID_SUPPORT
q.huang8a993672025-06-24 16:19:46 +08005819 if(cid == 1) //cid 1 as a special treatment
5820 {
5821 mbtk_pdp_cb_info_s pdp_cb_info;
5822 memset(&pdp_cb_info, 0x00, sizeof(mbtk_pdp_cb_info_s));
5823 pdp_cb_info.cid = cid;
5824 pdp_cb_info.auto_urc = false;
5825 pdp_cb_info.connect_state = true;
5826 memcpy(&(pdp_cb_info.ipv4), &ipv4, sizeof(mbtk_ipv4_info_t));
5827 memcpy(&(pdp_cb_info.ipv6), &ipv6, sizeof(mbtk_ipv6_info_t));
5828 usleep(1000);
5829 pack_rsp_send(cli_info->fd , MBTK_INFO_ID_IND_PDP_STATE_CHANGE, &pdp_cb_info, sizeof(mbtk_pdp_cb_info_s));
5830 }
yq.wangc98d1342025-06-13 17:32:06 +08005831#endif
liubin281ac462023-07-19 14:22:54 +08005832 }
5833 else
5834 {
5835 free(pack->data);
5836 free(cli_info);
5837 LOG("data_call bootconn success.");
5838 }
5839 }
5840 }
5841 break;
5842 }
5843 case MBTK_DATA_CALL_STOP: {
5844 //mbtk wyq for data_call_ex add start
5845 if(cid_active[cid] == 0)
5846 {
5847 err = MBTK_INFO_ERR_CID_NO_EXIST;
5848 break;
5849 }
5850
5851 int timeout = pack->data[2];
5852 //mbtk wyq for data_call_ex add end
b.liuf37bd332024-03-18 13:51:24 +08005853#if (defined(MBTK_AF_SUPPORT) || defined(MBTK_ALL_CID_SUPPORT))
wangyouqianged88c722023-11-22 16:33:43 +08005854 if(cid == 1)
5855 {
5856 char dev[20] = {0};
yq.wangf98110c2025-06-11 14:50:04 +08005857 mbtk_pdp_cb_info_s pdp_cb_info;
5858 memset(&pdp_cb_info, 0x00, sizeof(mbtk_pdp_cb_info_s));
b.liufe320632024-01-17 20:38:08 +08005859
wangyouqianged88c722023-11-22 16:33:43 +08005860 sprintf(dev, "ccinet%d", cid - 1);
5861
5862 // Config network.
5863 if(mbtk_ifc_configure2(dev, NULL, 0, NULL, NULL)) {
5864 LOGD("Config %s IPv4 0 fail.", dev);
5865 } else {
5866 LOGD("Config %s IPv4 0 success.", dev);
5867 }
yq.wangf98110c2025-06-11 14:50:04 +08005868 pdp_cb_info.cid = cid;
5869 pdp_cb_info.connect_state = false;
5870 pdp_cb_info.auto_urc = false;
5871 pack_rsp_send(cli_info->fd , MBTK_INFO_ID_IND_PDP_STATE_CHANGE, &pdp_cb_info, sizeof(mbtk_pdp_cb_info_s));
5872
wangyouqianged88c722023-11-22 16:33:43 +08005873 cid_active[cid] = 0;
yq.wang69a51872025-06-17 09:45:12 +08005874 //mbtk_set_default_pdp_state(false, cid);
liuyange134d842024-06-27 17:34:02 +08005875 mbtk_net_led_set(MBTK_NET_LED_NET_CONNECT);
wangyouqianged88c722023-11-22 16:33:43 +08005876 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_DATA_CALL_RSP, NULL, 0);
5877 break;
5878 }
5879#endif
liubin281ac462023-07-19 14:22:54 +08005880 cgact_wait.waitting = true;
5881 cgact_wait.cid = cid;
5882 cgact_wait.act = false;
5883 if(req_data_call_stop(cid, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5884 {
5885 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5886 err = MBTK_INFO_ERR_CME + cme_err;
5887 } else {
5888 err = MBTK_INFO_ERR_UNKNOWN;
5889 }
5890 LOG("%d deactive fail.", cid);
5891 }
5892 else
5893 {
5894 // Wait for "CONNECT" or "+CGEV:"
5895 if(wait_cgact_complete(timeout)) { // Timeout
q.huangd36e6422025-07-25 21:16:03 +08005896 /* err = MBTK_INFO_ERR_TIMEOUT;
5897 break;*/ //LYNQ_T108_BUG_1358_CR_138966_Q.HUANG_20250725_COMMENT
5898 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 +08005899 }
5900 char dev[20] = {0};
5901 sprintf(dev, "ccinet%d", cid - 1);
5902
5903 // Config network.
5904 if(mbtk_ifc_configure2(dev, NULL, 0, NULL, NULL)) {
5905 LOGD("Config %s IPv4 0 fail.", dev);
5906 } else {
5907 LOGD("Config %s IPv4 0 success.", dev);
5908 }
5909
5910#if 0
5911 if(mbtk_ipv6_config(dev, NULL, 64)) {
5912 LOGD("Config %s IPv6 0 fail.", dev);
5913 } else {
5914 LOGD("Config %s IPv6 0 success.", dev);
5915 }
5916#endif
5917 cid_active[cid] = 0;
yq.wang69a51872025-06-17 09:45:12 +08005918 //mbtk_set_default_pdp_state(false, cid);
liuyange134d842024-06-27 17:34:02 +08005919 mbtk_net_led_set(MBTK_NET_LED_NET_CONNECT);
liubin281ac462023-07-19 14:22:54 +08005920 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_DATA_CALL_RSP, NULL, 0);
5921 }
5922 break;
5923 }
5924 case MBTK_DATA_CALL_STATE: {
wangyouqianged88c722023-11-22 16:33:43 +08005925 if(cid_active[cid] == 0)
5926 {
5927 err = MBTK_INFO_ERR_CID_NO_EXIST;
5928 break;
5929 }
liubin281ac462023-07-19 14:22:54 +08005930 mbtk_ipv4_info_t ipv4;
5931 mbtk_ipv6_info_t ipv6;
5932 memset(&ipv4, 0, sizeof(mbtk_ipv4_info_t));
5933 memset(&ipv6, 0, sizeof(mbtk_ipv6_info_t));
5934 if(req_data_call_state_get(cid, &ipv4, &ipv6, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5935 {
5936 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5937 err = MBTK_INFO_ERR_CME + cme_err;
5938 } else {
5939 err = MBTK_INFO_ERR_UNKNOWN;
5940 }
5941 LOG("Get %d state fail.", cid);
5942 }
5943 else
5944 {
5945 uint8 buff[SOCK_MSG_LEN_MAX] = {0};
5946 int buff_len = 0;
wangyouqianged88c722023-11-22 16:33:43 +08005947#ifdef MBTK_AF_SUPPORT
5948 if(cid == 1)
5949 {
5950 ipv4.valid = false;
5951 ipv6.valid = false;
5952 if(default_iptype == MBTK_IP_TYPE_IP)
5953 {
5954 ipv4.valid = true;
5955 }
5956 else if(default_iptype == MBTK_IP_TYPE_IPV6)
5957 {
5958 ipv6.valid = true;
5959 }
5960 else
5961 {
5962 ipv4.valid = true;
5963 ipv6.valid = true;
5964 }
5965 }
5966#endif
liubin281ac462023-07-19 14:22:54 +08005967 if(ipv4.valid && ipv6.valid) {
5968 buff[0] = (uint8)2;
5969 buff_len++;
5970
5971 memcpy(buff + buff_len, &ipv4, sizeof(mbtk_ipv4_info_t));
5972 buff_len += sizeof(mbtk_ipv4_info_t);
5973 memcpy(buff + buff_len, &ipv6, sizeof(mbtk_ipv6_info_t));
5974 buff_len += sizeof(mbtk_ipv6_info_t);
5975 } else if(ipv4.valid) {
5976 buff[0] = (uint8)0;
5977 buff_len++;
5978
5979 memcpy(buff + buff_len, &ipv4, sizeof(mbtk_ipv4_info_t));
5980 buff_len += sizeof(mbtk_ipv4_info_t);
5981 } else if(ipv6.valid) {
5982 buff[0] = (uint8)1;
5983 buff_len++;
5984
5985 memcpy(buff + buff_len, &ipv6, sizeof(mbtk_ipv6_info_t));
5986 buff_len += sizeof(mbtk_ipv6_info_t);
5987 } else {
5988 LOGE("Get IPv4/IPv6 fail.");
5989 err = MBTK_INFO_ERR_UNKNOWN;
5990 break;
5991 }
5992 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_DATA_CALL_RSP, buff, buff_len);
5993 }
5994 break;
5995 }
5996 default: {
5997 err = MBTK_INFO_ERR_FORMAT;
5998 break;
5999 }
6000 }
6001 }
6002 break;
6003 }
r.xiao06db9a12024-04-14 18:51:15 -07006004 case MBTK_INFO_ID_NET_IMS_REQ:
6005 {
6006 if(pack->data_len == 0 || pack->data == NULL) //Get
6007 {
6008 int reg = -1;
6009 if(net_ims_get(&reg, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
6010 {
6011 if(cme_err != MBTK_INFO_ERR_CME_NON) {
6012 err = MBTK_INFO_ERR_CME + cme_err;
6013 } else {
6014 err = MBTK_INFO_ERR_UNKNOWN;
6015 }
6016 LOG("Get net ims fail.");
6017 }
6018 else
6019 {
6020 uint8 reg_type = (uint8)reg;
6021 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_IMS_RSP, &reg_type, sizeof(uint8));
6022 }
6023 }
6024 else
6025 {
6026 uint8 ims = *(pack->data);
6027
6028 if(net_ims_set(ims, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
6029 {
6030 if(cme_err != MBTK_INFO_ERR_CME_NON) {
6031 err = MBTK_INFO_ERR_CME + cme_err;
6032 } else {
6033 err = MBTK_INFO_ERR_UNKNOWN;
6034 }
6035 LOG("Set net ims fail.");
6036 }
6037 else
6038 {
6039 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_IMS_RSP, NULL, 0);
6040 }
6041 }
6042 break;
6043 }
b.liufdf03172024-06-07 15:01:29 +08006044 case MBTK_INFO_ID_NET_IMS_REG_STATE_REQ:
6045 {
6046 if(pack->data_len == 0 || pack->data == NULL) //Get
6047 {
6048 int reg = -1;
6049 if(net_ims_reg_state_get(&reg, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
6050 {
6051 if(cme_err != MBTK_INFO_ERR_CME_NON) {
6052 err = MBTK_INFO_ERR_CME + cme_err;
6053 } else {
6054 err = MBTK_INFO_ERR_UNKNOWN;
6055 }
6056 LOG("Get net ims fail.");
6057 }
6058 else
6059 {
6060 uint8 reg_type = (uint8)reg;
6061 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_IMS_REG_STATE_RSP, &reg_type, sizeof(uint8));
6062 }
6063 }
6064 else
6065 {
6066 err = MBTK_INFO_ERR_UNSUPPORTED;
6067 }
6068 break;
6069 }
xy.he22063af2024-12-06 01:28:08 -08006070 case MBTK_INFO_ID_NET_FPLMN_REQ:
6071 {
6072 LOG("MBTK_INFO_ID_NET_FPLMN_REQ in\n");
6073 if(pack->data_len == 0 || pack->data == NULL) //Get
6074 {
6075 char fplmn[256] = {0};
6076 LOG("fplmn_test");
6077 if(req_fplmn_get(fplmn, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
6078 {
6079 if(cme_err != MBTK_INFO_ERR_CME_NON) {
6080 err = MBTK_INFO_ERR_CME + cme_err;
6081 }else {
6082 err = MBTK_INFO_ERR_UNKNOWN;
6083 }
6084 LOGE("Get FPLMN fail.");
6085 printf("get fplmn fail\n");
6086 }
6087 else
6088 {
6089 printf("get fplmn success\n");
6090 LOGE("Get FPLMN success.");
6091 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_FPLMN_RSP, fplmn, strlen(fplmn));
6092 }
6093 }
6094 else //set
6095 {
6096 char *fplmn_temp = (char *)pack->data;
6097 char fplmn[128] = {0};
6098 int data_len = pack->data_len;
6099 char reg[50] = {0};
6100 memcpy(fplmn, fplmn_temp, data_len);
6101 LOG("fplmn_test_set ,fplmn_temp = %s,len = %d\n",fplmn_temp,data_len);
6102 LOG("fplmn_test_set ,fplmn = %s, len = %d\n",fplmn,strlen(fplmn));
6103 if(req_fplmn_set(fplmn,reg,&cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
6104 {
6105 if(cme_err != MBTK_INFO_ERR_CME_NON) {
6106 err = MBTK_INFO_ERR_CME + cme_err;
6107 } else {
6108 err = MBTK_INFO_ERR_UNKNOWN;
6109 }
6110 LOG("req_fplmn_set fail.");
6111 }
6112 else
6113 {
6114 LOG("req_fplmn_set success.,reg:%s",reg);
6115 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_FPLMN_RSP, NULL, 0);
6116 }
6117 }
6118 break;
6119 }
r.xiaoec113d12024-01-12 02:13:28 -08006120 case MBTK_INFO_ID_WAKEUP_STA_REQ:
6121 {
6122 if(pack->data_len == 0 || pack->data == NULL)
6123 {
6124 err = MBTK_INFO_ERR_UNSUPPORTED;
6125 LOG("Get POWERIND state UNSUPPORTED.");
6126 }
6127 else // Set powerind state.
6128 {
6129 uint32 state = *(pack->data);
6130 if(req_powerind_set(state, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
6131 {
6132 if(cme_err != MBTK_INFO_ERR_CME_NON) {
6133 err = MBTK_INFO_ERR_CME + cme_err;
6134 } else {
6135 err = MBTK_INFO_ERR_UNKNOWN;
6136 }
6137 LOG("Set POWERIND state fail.");
6138 }
6139 else
6140 {
6141 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_WAKEUP_STA_RSP, NULL, 0);
6142 }
6143 }
6144 break;
6145 }
6146 case MBTK_INFO_ID_OOS_STA_REQ:
6147 {
6148 if(pack->data_len == 0 || pack->data == NULL)
6149 {
6150 mbtk_oos_info oos_t;
6151 if(req_oos_get(&oos_t, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
6152 {
6153 if(cme_err != MBTK_INFO_ERR_CME_NON) {
6154 err = MBTK_INFO_ERR_CME + cme_err;
6155 } else {
6156 err = MBTK_INFO_ERR_UNKNOWN;
6157 }
6158 LOG("Get SMS OOS fail.");
r.xiaoec113d12024-01-12 02:13:28 -08006159 }
6160 else
6161 {
r.xiaoec113d12024-01-12 02:13:28 -08006162 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_OOS_STA_RSP, &oos_t, sizeof(mbtk_oos_info));
6163 }
6164 }
b.liufe320632024-01-17 20:38:08 +08006165 else // Set OOS
r.xiaoec113d12024-01-12 02:13:28 -08006166 {
r.xiaocfd7c682024-01-22 03:59:46 -08006167 if(pack->data_len != sizeof(mbtk_oos_info))
6168 {
6169 err = MBTK_INFO_ERR_REQ_PARAMETER;
6170 LOG("Set oos error.");
6171 break;
6172 }
6173
b.liu9e8584b2024-11-06 19:21:28 +08006174 mbtk_oos_info *state = (mbtk_oos_info *)pack->data;
r.xiaoec113d12024-01-12 02:13:28 -08006175 if(req_oos_set(state, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
6176 {
6177 if(cme_err != MBTK_INFO_ERR_CME_NON) {
6178 err = MBTK_INFO_ERR_CME + cme_err;
6179 } else {
6180 err = MBTK_INFO_ERR_UNKNOWN;
6181 }
6182 LOG("Set OOS fail.");
6183 }
6184 else
6185 {
6186 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_OOS_STA_RSP, NULL, 0);
6187 }
6188 }
6189 break;
6190 }
q.huang8a993672025-06-24 16:19:46 +08006191 case MBTK_INFO_ID_LED_REQ:
wangyouqiang38e53362024-01-23 10:53:48 +08006192 {
6193 if(pack->data_len == 0 || pack->data == NULL)
6194 {
6195 err = MBTK_INFO_ERR_UNSUPPORTED;
6196 LOGE("led param is error.");
6197 }
6198 else
6199 {
6200 char type = pack->data[0];
6201 char status = pack->data[1];
6202 LOGE("[set_led] = [%d], [status_led] = [%d].", type, status);
r.xiaoec113d12024-01-12 02:13:28 -08006203
wangyouqiang38e53362024-01-23 10:53:48 +08006204 if(type == MBTK_LED_TYPE_NET)
6205 {
6206 if(status == MBTK_LED_STATUS_CLOSE)
6207 {
6208 mbtk_net_led_set(MBTK_NET_LED_CLOSE);
6209 }
6210 else
6211 {
6212 mbtk_net_led_set(MBTK_NET_LED_OPEN);
6213 }
6214 }
6215 else
6216 {
6217 if(status == MBTK_LED_STATUS_CLOSE)
6218 {
6219 status_led_set(MBTK_STATUS_LED_CLOSE);
6220 }
6221 else
6222 {
6223 status_led_set(MBTK_STATUS_LED_OPEN);
6224 }
6225 }
6226 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_LED_RSP, NULL, 0);
6227 }
6228 break;
6229 }
liubin281ac462023-07-19 14:22:54 +08006230 default:
6231 {
6232 err = MBTK_INFO_ERR_REQ_UNKNOWN;
6233 LOG("Unknown request : %s", id2str(pack->info_id));
6234 break;
6235 }
6236 }
6237
6238 return err;
6239 }
6240}
6241
6242// Process AT URC data
6243static int send_pack_to_queue(sock_client_info_t* cli_info, void* pack)
6244{
6245 if(info_queue.count >= PACK_PROCESS_QUEUE_MAX)
6246 {
6247 LOG("Packet process queue is full");
6248 return -1;
6249 }
6250
6251 info_queue_item_t *item = (info_queue_item_t*)malloc(sizeof(info_queue_item_t));
6252 if(!item)
6253 {
6254 LOG("malloc() fail[%d].", errno);
6255 return -1;
6256 }
6257 item->cli_info = cli_info;
6258 item->pack = pack;
6259 mbtk_queue_put(&info_queue, item);
6260
6261 // If thread is waitting,continue it.
6262 if(1/*!is_running*/)
6263 {
6264 pthread_mutex_lock(&info_mutex);
6265 pthread_cond_signal(&info_cond);
6266 pthread_mutex_unlock(&info_mutex);
6267 }
6268 else
6269 {
6270 LOG("Packet process thread is process...");
6271 }
6272
6273 return 0;
6274}
6275
b.liu9e8584b2024-11-06 19:21:28 +08006276static void radio_state_change(const void *data, int data_len)
liubin281ac462023-07-19 14:22:54 +08006277{
6278 uint8 *data_ptr = (uint8*)data;
6279 if(data_ptr[0]) {
6280 net_info.radio_state = MBTK_RADIO_STATE_ON;
6281 } else {
6282 net_info.radio_state = MBTK_RADIO_STATE_OFF;
6283 }
6284
6285 sock_client_info_t *cli = NULL;
6286 list_first(sock_client_list);
6287 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
6288 {
6289 if(cli->ind_num > 0) {
6290 int i;
6291 for(i = 0; i < IND_REGISTER_MAX; i++) {
6292 // Registe MBTK_INFO_ID_IND_RADIO_STATE_CHANGE
6293 if(cli->ind_register[i] == MBTK_INFO_ID_IND_RADIO_STATE_CHANGE) {
6294 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_RADIO_STATE_CHANGE, data, data_len);
6295 break;
6296 }
6297 }
6298 }
6299 }
6300}
6301
b.liu9e8584b2024-11-06 19:21:28 +08006302static void pdp_state_change(const void *data, int data_len)
liubin281ac462023-07-19 14:22:54 +08006303{
6304 sock_client_info_t *cli = NULL;
6305 list_first(sock_client_list);
6306 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
6307 {
6308 if(cli->ind_num > 0) {
6309 int i;
6310 for(i = 0; i < IND_REGISTER_MAX; i++) {
6311 if(cli->ind_register[i] == MBTK_INFO_ID_IND_PDP_STATE_CHANGE) {
6312 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_PDP_STATE_CHANGE, data, data_len);
6313 break;
6314 }
6315 }
6316 }
6317 }
6318}
6319
b.liu9e8584b2024-11-06 19:21:28 +08006320static void net_state_change(const void *data, int data_len)
liubin281ac462023-07-19 14:22:54 +08006321{
6322 sock_client_info_t *cli = NULL;
6323 list_first(sock_client_list);
6324 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
6325 {
6326 if(cli->ind_num > 0) {
6327 int i;
6328 for(i = 0; i < IND_REGISTER_MAX; i++) {
6329 // Registe MBTK_INFO_ID_IND_NET_STATE_CHANGE
6330 if(cli->ind_register[i] == MBTK_INFO_ID_IND_NET_STATE_CHANGE) {
6331 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_NET_STATE_CHANGE, data, data_len);
6332 break;
6333 }
6334 }
6335 }
6336 }
6337}
6338
b.liu9e8584b2024-11-06 19:21:28 +08006339static void call_state_change(const void *data, int data_len)
liubin281ac462023-07-19 14:22:54 +08006340{
6341 sock_client_info_t *cli = NULL;
6342 list_first(sock_client_list);
6343 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
6344 {
6345 if(cli->ind_num > 0) {
6346 int i;
6347 for(i = 0; i < IND_REGISTER_MAX; i++) {
6348 // Registed MBTK_INFO_ID_IND_RADIO_STATE_CHANGE
6349 if(cli->ind_register[i] == MBTK_INFO_ID_IND_CALL_STATE_CHANGE) {
6350 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_CALL_STATE_CHANGE, data, data_len);
6351 break;
6352 }
6353 }
6354 }
6355 }
6356}
6357
b.liu9e8584b2024-11-06 19:21:28 +08006358static void sim_state_change(const void *data, int data_len)
liubin281ac462023-07-19 14:22:54 +08006359{
6360 sock_client_info_t *cli = NULL;
6361 list_first(sock_client_list);
6362 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
6363 {
6364 if(cli->ind_num > 0) {
6365 int i;
6366 for(i = 0; i < IND_REGISTER_MAX; i++) {
6367 // Registed MBTK_INFO_ID_IND_RADIO_STATE_CHANGE
6368 if(cli->ind_register[i] == MBTK_INFO_ID_IND_SIM_STATE_CHANGE) {
6369 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_SIM_STATE_CHANGE, data, data_len);
6370 break;
6371 }
6372 }
6373 }
6374 }
6375}
6376
b.liu9e8584b2024-11-06 19:21:28 +08006377static void sms_state_change(const void *data, int data_len)
liubin281ac462023-07-19 14:22:54 +08006378{
6379 sock_client_info_t *cli = NULL;
6380 list_first(sock_client_list);
6381 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
6382 {
6383 if(cli->ind_num > 0) {
6384 int i;
6385 for(i = 0; i < IND_REGISTER_MAX; i++) {
6386 // Registed MBTK_INFO_ID_IND_SMS_STATE_CHANGE
6387 if(cli->ind_register[i] == MBTK_INFO_ID_IND_SMS_STATE_CHANGE) {
6388 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_SMS_STATE_CHANGE, data, data_len);
6389 break;
6390 }
6391 }
6392 }
6393 }
6394}
6395
b.liu9e8584b2024-11-06 19:21:28 +08006396static void signal_state_change(const void *data, int data_len)
r.xiaofca7c472024-04-24 01:00:23 -07006397{
6398 sock_client_info_t *cli = NULL;
6399 list_first(sock_client_list);
6400 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
6401 {
6402 if(cli->ind_num > 0) {
6403 int i;
6404 for(i = 0; i < IND_REGISTER_MAX; i++) {
6405 // Registe MBTK_INFO_ID_IND_SIGNAL_STATE_CHANGE
6406 if(cli->ind_register[i] == MBTK_INFO_ID_IND_SIGNAL_STATE_CHANGE) {
6407 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_SIGNAL_STATE_CHANGE, data, data_len);
6408 break;
6409 }
6410 }
6411 }
6412 }
6413}
6414
6415
b.liu9e8584b2024-11-06 19:21:28 +08006416int urc_msg_distribute(bool async_process, info_urc_msg_id_enum msg, const void *data, int data_len)
liubin281ac462023-07-19 14:22:54 +08006417{
6418#if 0
6419 if(urc_queue.count >= PACK_PROCESS_QUEUE_MAX)
6420 {
6421 LOG("Packet process queue is full");
6422 return -1;
6423 }
6424
6425 info_urc_msg_t *urc = (info_urc_msg_t*)malloc(sizeof(info_urc_msg_t));
6426 if(!urc)
6427 {
6428 LOG("malloc() fail[%d].", errno);
6429 return -1;
6430 }
6431 urc->msg = msg;
6432 urc->data = memdup(data, data_len);
6433 urc->data_len = data_len;
6434
6435 mbtk_queue_put(&urc_queue, urc);
6436
6437 // If thread is waitting,continue it.
6438 if(1/*!is_running*/)
6439 {
6440 pthread_mutex_lock(&urc_mutex);
6441 pthread_cond_signal(&urc_cond);
6442 pthread_mutex_unlock(&urc_mutex);
6443 }
6444 else
6445 {
6446 LOG("Packet process thread is process...");
6447 }
6448
6449 return 0;
6450#else
b.liuf77b86c2024-11-09 13:24:10 +08006451
liubin281ac462023-07-19 14:22:54 +08006452 if(async_process) {
6453 info_urc_msg_t *urc = (info_urc_msg_t*)malloc(sizeof(info_urc_msg_t));
6454 if(!urc)
6455 {
6456 LOG("malloc() fail[%d].", errno);
6457 return -1;
6458 }
6459 urc->msg = msg;
6460 if(data && data_len > 0) {
6461 urc->data = memdup(data, data_len);
6462 urc->data_len = data_len;
6463 } else {
6464 urc->data = NULL;
6465 urc->data_len = 0;
6466 }
6467 return send_pack_to_queue(NULL, urc);
6468 } else {
6469 switch(msg) {
6470 case INFO_URC_MSG_NET_CS_REG_STATE:
6471 {
6472 net_state_change(data, data_len);
6473 break;
6474 }
6475 case INFO_URC_MSG_CALL_STATE:
6476 {
6477 call_state_change(data, data_len);
6478 break;
6479 }
6480 case INFO_URC_MSG_SMS_STATE:
6481 {
6482 sms_state_change(data, data_len);
6483 break;
6484 }
6485 case INFO_URC_MSG_SIM_STATE:
6486 {
6487 sim_state_change(data, data_len);
6488 break;
6489 }
6490 case INFO_URC_MSG_PDP_STATE:
6491 {
6492 pdp_state_change(data, data_len);
6493 break;
6494 }
6495 default: {
6496 LOGE("Unknown msg : %d", msg);
6497 break;
6498 }
6499 }
6500
6501 return 0;
6502 }
6503#endif
6504}
6505
6506
6507static void ind_regisger(sock_client_info_t* cli_info, uint16 ind)
6508{
6509 uint32 i = 0;
6510 while(i < cli_info->ind_num)
6511 {
6512 if(cli_info->ind_register[i] == ind)
6513 break;
6514 i++;
6515 }
6516
6517 if(i == cli_info->ind_num) // No found IND
6518 {
6519 cli_info->ind_register[i] = ind;
6520 cli_info->ind_num++;
6521 LOG("Register IND : %s", id2str(ind));
6522 }
6523 else
6524 {
6525 LOG("IND had exist.");
6526 }
6527}
6528
6529static void pack_distribute(sock_client_info_t* cli_info, mbtk_info_pack_t* pack)
6530{
6531 // Register IND Message.
6532 if(mbtk_info_type_get(pack->info_id) == MBTK_INFO_TYPE_IND)
6533 {
6534 mbtk_info_err_enum err = MBTK_INFO_ERR_SUCCESS;
6535 if(cli_info->ind_num >= IND_REGISTER_MAX)
6536 {
6537 LOG("IND if full.");
6538 err = MBTK_INFO_ERR_IND_FULL;
6539 }
6540 else
6541 {
6542 ind_regisger(cli_info, pack->info_id);
6543 }
6544
6545 pack_error_send(cli_info->fd, pack->info_id, err);
6546
6547 mbtk_info_pack_free(&pack);
6548 }
6549 else // Request Information.
6550 {
6551 LOG("Start process REQ(%s), Length : %d", id2str(pack->info_id), pack->data_len);
6552 if(0 && pack->data_len > 0)
6553 {
6554 log_hex("DATA", pack->data, pack->data_len);
6555 }
6556
6557 // Send to REQ_process_thread process.
6558 send_pack_to_queue(cli_info, pack);
6559
6560 // For test.
6561 // pack_error_send(cli_info->fd, pack->info_id + 1, MBTK_INFO_ERR_SUCCESS);
6562 }
6563}
6564
6565static sock_client_info_t* cli_find(int fd)
6566{
6567 sock_client_info_t *result = NULL;
6568 list_first(sock_client_list);
6569 while ((result = (sock_client_info_t*) list_next(sock_client_list)))
6570 {
6571 if (result->fd == fd)
6572 return result;
6573 }
6574
6575 return NULL;
6576}
6577
6578//mbtk wyq for server_ready_status add start
6579void server_ready_set(void)
6580{
6581 server_ready_status = 1;
6582}
6583
6584char server_ready_get(void)
6585{
6586 return server_ready_status;
6587}
6588
6589static void server_state_send(void)
6590{
6591 sock_client_info_t *cli = NULL;
6592 list_first(sock_client_list);
6593 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
6594 {
6595 if(cli->ind_num > 0) {
6596 if(cli->ind_register[0] == MBTK_INFO_ID_IND_SERVER_STATE_CHANGE) {
6597 cli->ind_num = 0;
6598 cli->ind_register[0] = 0;
6599 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_SERVER_STATE_CHANGE, "1", 1);
6600 break;
6601 }
6602 }
6603 else
6604 {
6605 break;
6606 }
6607 }
6608 LOG("handshake message send ok.");
6609}
6610
6611//mbtk wyq for server_ready_status add end
6612
6613//mbtk wyq for data_call_ex add start
6614//Save the cid that "DATA_CALL" needs to be automatically connected after startup
6615void data_call_bootconn_save(int cid, int bootconn)
6616{
6617 if(cid_bootconn[cid] == bootconn + '0')
6618 {
6619 return;
6620 }
6621 cid_bootconn[cid] = bootconn + '0';
6622
6623 LOG("data_call_bootconn_set cid_bootconn = %s", cid_bootconn);
6624 property_set("persist.mbtk.datacall.bootconn", cid_bootconn);
6625}
6626
6627static void* data_call_bootconn_pthread(void *arg)
6628{
6629 UNUSED(arg);
6630 LOG("data_call_bootconn_pthread enter.");
6631 int i = 0;
6632 int send_sum = 0;
6633 int bootconn = 0;
b.liufe320632024-01-17 20:38:08 +08006634
liubin281ac462023-07-19 14:22:54 +08006635 while(1)
6636 {
6637 if(server_ready_get() && send_sum == 0)
6638 {
6639 server_state_send();
6640 send_sum = 1;
6641 }
b.liufe320632024-01-17 20:38:08 +08006642
liubin281ac462023-07-19 14:22:54 +08006643 if(net_info.sim_state == MBTK_SIM_READY && net_info.net_type == MBTK_RADIO_TECH_E_UTRAN && bootconn == 0)
6644 {
6645 //data_call_bootconn_exec();
6646 property_get("persist.mbtk.datacall.bootconn", cid_bootconn, "00000000");
6647 LOG("data_call_bootconn_exec cid_bootconn = %s", cid_bootconn);
b.liufe320632024-01-17 20:38:08 +08006648
liubin281ac462023-07-19 14:22:54 +08006649 for(i = MBTK_APN_CID_MIN; i < MBTK_APN_CID_MAX + 1; i++)
6650 {
6651 if(cid_bootconn[i] == '1')
6652 {
6653 sock_client_info_t *info = (sock_client_info_t*)malloc(sizeof(sock_client_info_t));
6654 if(info == NULL)
6655 {
6656 LOG("clinent_info malloc() fail.");
6657 continue;
6658 }
6659 memset(info, 0, sizeof(sock_client_info_t));
6660 info->fd = DATA_CALL_BOOTCONN_FD;
b.liufe320632024-01-17 20:38:08 +08006661
liubin281ac462023-07-19 14:22:54 +08006662 mbtk_info_pack_t* pack = mbtk_info_pack_creat(MBTK_INFO_ID_NET_DATA_CALL_REQ);
6663 if(pack == NULL)
6664 {
6665 free(info);
6666 LOG("Packet malloc() fail.");
6667 continue;
6668 }
6669
6670 // "info_err"
6671 //pack->info_err = byte_2_uint16(ptr, false)
6672
6673 // "data_len"
6674 pack->data_len = 5;
6675
b.liu9e8584b2024-11-06 19:21:28 +08006676 uint8 *p = (uint8 *)malloc(5);
liubin281ac462023-07-19 14:22:54 +08006677 p[0] = MBTK_DATA_CALL_START;
6678 p[1] = i;
6679 p[2] = 0;
6680 p[3] = 1;
6681 p[4] = 10;
6682 pack->data = p;
6683 send_pack_to_queue(info, pack);
6684 }
6685 }
6686
6687 bootconn = 1;
6688 }
6689
6690 if(bootconn == 1 && send_sum == 1)
6691 {
6692 break;
6693 }
6694 else
6695 {
6696 sleep(1);
6697 }
6698 }
6699
6700 LOG("data_call_bootconn_pthread exit.");
6701 return NULL;
6702}
6703
6704//mbtk wyq for data_call_ex add end
6705
6706static void* info_main_pthread(void* arg)
6707{
6708 UNUSED(arg);
6709 epoll_fd = epoll_create(SOCK_CLIENT_MAX + 1);
6710 if(epoll_fd < 0)
6711 {
6712 LOG("epoll_create() fail[%d].", errno);
6713 return NULL;
6714 }
6715
6716 uint32 event = EPOLLIN | EPOLLET;
6717 struct epoll_event ev;
6718 ev.data.fd = sock_listen_fd;
6719 ev.events = event; //EPOLLIN | EPOLLERR | EPOLLET;
6720 epoll_ctl(epoll_fd,EPOLL_CTL_ADD,sock_listen_fd,&ev);
6721
6722 int nready = -1;
6723 struct epoll_event epoll_events[EPOLL_LISTEN_MAX];
6724 while(1)
6725 {
6726 nready = epoll_wait(epoll_fd, epoll_events, EPOLL_LISTEN_MAX, -1);
6727 if(nready > 0)
6728 {
6729 sock_client_info_t *cli_info = NULL;
6730 int i;
6731 for(i = 0; i < nready; i++)
6732 {
6733 LOG("fd[%d] event = %x",epoll_events[i].data.fd, epoll_events[i].events);
6734 if(epoll_events[i].events & EPOLLHUP) // Client Close.
6735 {
6736 if((cli_info = cli_find(epoll_events[i].data.fd)) != NULL)
6737 {
6738 cli_close(cli_info);
6739 }
6740 else
6741 {
6742 LOG("Unknown client[fd = %d].", epoll_events[i].data.fd);
6743 }
6744 }
6745 else if(epoll_events[i].events & EPOLLIN)
6746 {
6747 if(epoll_events[i].data.fd == sock_listen_fd) // New clients connected.
6748 {
6749 int client_fd = -1;
6750 while(1)
6751 {
6752 struct sockaddr_in cliaddr;
6753 socklen_t clilen = sizeof(cliaddr);
6754 client_fd = accept(epoll_events[i].data.fd, (struct sockaddr *) &cliaddr, &clilen);
6755 if(client_fd < 0)
6756 {
6757 if(errno == EAGAIN)
6758 {
6759 LOG("All client connect get.");
6760 }
6761 else
6762 {
6763 LOG("accept() error[%d].", errno);
6764 }
6765 break;
6766 }
6767 // Set O_NONBLOCK
6768 int flags = fcntl(client_fd, F_GETFL, 0);
6769 if (flags > 0)
6770 {
6771 flags |= O_NONBLOCK;
6772 if (fcntl(client_fd, F_SETFL, flags) < 0)
6773 {
6774 LOG("Set flags error:%d", errno);
6775 }
6776 }
6777
6778 memset(&ev,0,sizeof(struct epoll_event));
6779 ev.data.fd = client_fd;
6780 ev.events = event;//EPOLLIN | EPOLLERR | EPOLLET;
6781 epoll_ctl(epoll_fd, EPOLL_CTL_ADD, client_fd, &ev);
6782
6783 sock_client_info_t *info = (sock_client_info_t*)malloc(sizeof(sock_client_info_t));
6784 if(info)
6785 {
6786 memset(info, 0, sizeof(sock_client_info_t));
6787 info->fd = client_fd;
6788 if(server_ready_get() == 1)
6789 {
6790 info->ind_num = 0;
6791 pack_rsp_send(info->fd , MBTK_INFO_ID_IND_SERVER_STATE_CHANGE, "1", 1);
6792 LOG("server ready ok.");
6793 }
6794 else
6795 {
6796 info->ind_num = 1;
6797 info->ind_register[0] = MBTK_INFO_ID_IND_SERVER_STATE_CHANGE;
6798 LOG("server ready no.");
6799 }
6800 list_add(sock_client_list, info);
6801 LOG("Add New Client FD Into List.");
6802 }
6803 else
6804 {
6805 LOG("malloc() fail.");
6806 }
6807 }
6808 }
6809 else if((cli_info = cli_find(epoll_events[i].data.fd)) != NULL) // Client data arrive.
6810 {
6811 // Read and process every message.
6812 mbtk_info_err_enum err = MBTK_INFO_ERR_SUCCESS;
6813 mbtk_info_pack_t** pack = mbtk_info_pack_recv(cli_info->fd, true, &err);
6814
6815 // Parse packet error,send error response to client.
6816 if(pack == NULL)
6817 {
6818 if(err != MBTK_INFO_ERR_SUCCESS)
6819 {
6820 pack_error_send(cli_info->fd, MBTK_INFO_ID_REQ_UNKNOWN, err);
6821 }
6822 }
6823 else
6824 {
6825#if 0
6826 int i = 0;
6827 while(pack[i] != NULL)
6828 {
6829 pack_distribute(cli_info, pack[i]);
6830 // Not free,will free in pack_process() or packet process thread.
6831 //mbtk_info_pack_free(&(pack[i]));
6832 i++;
6833 }
6834 free(pack);
6835#else
6836 mbtk_info_pack_t** pack_ptr = pack;
6837 while(*pack_ptr)
6838 {
6839 pack_distribute(cli_info, *pack_ptr);
6840 // Not free,will free in pack_process() or packet process thread.
6841 //mbtk_info_pack_free(pack_ptr);
6842 pack_ptr++;
6843 }
6844
6845 free(pack);
6846#endif
6847 }
6848 }
6849 else
6850 {
6851 LOG("Unknown socket : %d", epoll_events[i].data.fd);
6852 }
6853 }
6854 else
6855 {
6856 LOG("Unknown event : %x", epoll_events[i].events);
6857 }
6858 }
6859 }
6860 else
6861 {
6862 LOG("epoll_wait() fail[%d].", errno);
6863 }
6864 }
6865
6866 return NULL;
6867}
6868
liubin281ac462023-07-19 14:22:54 +08006869/*
6870void mbtk_radio_ready_cb()
6871{
6872 pthread_t radio_pid;
6873 pthread_attr_t thread_attr;
6874 pthread_attr_init(&thread_attr);
6875 if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
6876 {
6877 LOG("pthread_attr_setdetachstate() fail.");
6878 return;
6879 }
6880
6881 if(pthread_create(&radio_pid, &thread_attr, radio_ready_thread, NULL))
6882 {
6883 LOG("pthread_create() fail.");
6884 }
6885
6886 pthread_attr_destroy(&thread_attr);
6887}
6888*/
6889
yq.wangf98110c2025-06-11 14:50:04 +08006890static void net_ifc_state_change(bool act, int cid, mbtk_pdp_cb_info_s *pdp_cb_info)
liubin281ac462023-07-19 14:22:54 +08006891{
6892 if(cid < MBTK_APN_CID_MIN || cid > MBTK_APN_CID_MAX) { // No nothing for cid 1 and 8
6893 return;
6894 }
6895
yq.wanga9efa822024-07-03 04:33:48 -07006896#if 0
wangyouqiang65884152023-10-25 19:54:15 +08006897 if(act)
6898 {
6899 cid_active[cid] = 1;
wangyouqiang80487e42024-05-24 15:06:20 +08006900 mbtk_set_default_pdp_state(true, cid);
wangyouqiang65884152023-10-25 19:54:15 +08006901 }
6902 else
6903 {
6904 cid_active[cid] = 0;
wangyouqiang80487e42024-05-24 15:06:20 +08006905 mbtk_set_default_pdp_state(false, cid);
wangyouqiang65884152023-10-25 19:54:15 +08006906 }
yq.wanga9efa822024-07-03 04:33:48 -07006907#endif
liubin281ac462023-07-19 14:22:54 +08006908 char dev[20] = {0};
6909 sprintf(dev, "ccinet%d", cid - 1);
6910 if(act) { // Config IP.
6911 // Get IP information.
6912 mbtk_ipv4_info_t ipv4;
6913 mbtk_ipv6_info_t ipv6;
6914 memset(&ipv4, 0, sizeof(mbtk_ipv4_info_t));
6915 memset(&ipv6, 0, sizeof(mbtk_ipv6_info_t));
6916 int cme_err = MBTK_INFO_ERR_CME_NON;
6917 if(!req_data_call_state_get(cid, &ipv4, &ipv6, &cme_err) && cme_err == MBTK_INFO_ERR_CME_NON)
6918 {
wangyouqianged88c722023-11-22 16:33:43 +08006919#ifdef MBTK_AF_SUPPORT
6920 if(cid == 1)
6921 {
6922 ipv4.valid = false;
6923 ipv6.valid = false;
6924 if(default_iptype == MBTK_IP_TYPE_IP)
6925 {
6926 ipv4.valid = true;
6927 }
6928 else if(default_iptype == MBTK_IP_TYPE_IPV6)
6929 {
6930 ipv6.valid = true;
6931 }
6932 else
6933 {
6934 ipv4.valid = true;
6935 ipv6.valid = true;
6936 }
6937 }
6938#endif
b.liuf77b86c2024-11-09 13:24:10 +08006939
yq.wangf98110c2025-06-11 14:50:04 +08006940 if(pdp_cb_info) {
6941 memcpy(&(pdp_cb_info->ipv4), &ipv4, sizeof(mbtk_ipv4_info_t));
6942 memcpy(&(pdp_cb_info->ipv6), &ipv6, sizeof(mbtk_ipv6_info_t));
b.liuf77b86c2024-11-09 13:24:10 +08006943 }
6944
liubin281ac462023-07-19 14:22:54 +08006945 // Config IPv4 address.
6946 if(ipv4.valid) {
6947 char ip[20] = {0};
6948 if(inet_ntop(AF_INET, &(ipv4.IPAddr), ip, 20) == NULL) {
6949 LOGE("inet_ntop ipv4 ip fail.");
6950 return;
6951 }
6952
6953 if(mbtk_ifc_configure2(dev, ip, 0, NULL, "255.255.255.0")) {
6954 LOGD("Config %s IPv4 %s fail.", dev, ip);
6955 } else {
6956 LOGD("Config %s IPv4 %s success.", dev, ip);
6957 }
6958 }
6959
6960 // Config IPv6 address.
6961 if(ipv6.valid) {
6962 char ip[50] = {0};
6963
6964 if(inet_ntop(AF_INET6, &(ipv6.IPV6Addr), ip, 50) == NULL) {
6965 LOGE("inet_ntop ipv6 ip fail.");
6966 return;
6967 }
6968
6969 if(mbtk_ipv6_config(dev, ip, 64)) {
6970 LOGD("Config %s IPv6 %s fail.", dev, ip);
6971 } else {
6972 LOGD("Config %s IPv6 %s success.", dev, ip);
6973 }
6974 }
yq.wanga9efa822024-07-03 04:33:48 -07006975
yq.wangc98d1342025-06-13 17:32:06 +08006976 //mbtk_qser_route_config(cid, &ipv4, &ipv6);
liubin281ac462023-07-19 14:22:54 +08006977 }
6978 } else { // Del IP
6979 if(mbtk_ifc_configure2(dev, NULL, 0, NULL, NULL)) {
6980 LOGD("Config %s IPv4 0 fail.", dev);
6981 } else {
6982 LOGD("Config %s IPv4 0 success.", dev);
6983 }
6984 }
6985}
6986
yq.wang69a51872025-06-17 09:45:12 +08006987#if 0
yq.wanga9efa822024-07-03 04:33:48 -07006988static void data_call_restart()
6989{
6990#if 0
6991 // Waitting for network ok.
6992 mbtk_net_info_t info;
6993 int cme_err;
6994 int i = 0;
6995 while(i < 15) { // 15s timeout
6996 cme_err = MBTK_INFO_ERR_CME_NON;
6997 memset(&info, 0, sizeof(mbtk_net_info_t));
6998 if(!req_net_sel_mode_get(&info, &cme_err) && cme_err == MBTK_INFO_ERR_CME_NON)
6999 {
7000 if(info.net_type >= 2) {
7001 break;
7002 }
7003 }
7004
7005 sleep(1);
7006 i++;
7007 }
7008#endif
7009 // +CGACT
7010 int cid;
yq.wangf98110c2025-06-11 14:50:04 +08007011 mbtk_pdp_cb_info_s pdp_cb_info;
yq.wanga9efa822024-07-03 04:33:48 -07007012 LOGD("Start active APN.");
yq.wangf98110c2025-06-11 14:50:04 +08007013 //at_process = true;
yq.wanga9efa822024-07-03 04:33:48 -07007014 cgact_wait.act = true;
yq.wangf98110c2025-06-11 14:50:04 +08007015 for(cid = MBTK_APN_CID_MIN; cid <= MBTK_APN_CID_MAX; cid++)
7016 {
yq.wangd58f71e2024-08-21 23:45:31 -07007017 if(cid_active[cid])
7018 {
7019 LOG("Active cid : %d", cid);
yq.wangf98110c2025-06-11 14:50:04 +08007020 memset(&pdp_cb_info, 0x00, sizeof(mbtk_pdp_cb_info_s));
yq.wangd58f71e2024-08-21 23:45:31 -07007021 cgact_wait.waitting = true;
7022 cgact_wait.cid = cid;
yq.wangf98110c2025-06-11 14:50:04 +08007023
b.liuf77b86c2024-11-09 13:24:10 +08007024 net_ifc_state_change(false, cid, NULL);
yq.wangf98110c2025-06-11 14:50:04 +08007025 pdp_cb_info.cid = cid;
7026 pdp_cb_info.connect_state = false;
7027 pdp_cb_info.auto_urc = true;
7028 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &pdp_cb_info, sizeof(mbtk_pdp_cb_info_s));
7029
yq.wangd58f71e2024-08-21 23:45:31 -07007030 req_data_call_start(cid, NULL);
7031 wait_cgact_complete(10);
yq.wangf98110c2025-06-11 14:50:04 +08007032
7033 net_ifc_state_change(true, cid, &pdp_cb_info);
7034 pdp_cb_info.connect_state = true;
7035 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 -07007036 }
yq.wanga9efa822024-07-03 04:33:48 -07007037 }
yq.wangf98110c2025-06-11 14:50:04 +08007038 //at_process = false;
yq.wanga9efa822024-07-03 04:33:48 -07007039}
yq.wang69a51872025-06-17 09:45:12 +08007040#endif
yq.wanga9efa822024-07-03 04:33:48 -07007041
liubin281ac462023-07-19 14:22:54 +08007042static void urc_msg_process(info_urc_msg_t *msg)
7043{
7044 uint8 *data = NULL;
7045 if(msg->data) {
7046 data = (uint8*)msg->data;
7047 }
7048 switch(msg->msg) {
7049 case INFO_URC_MSG_RADIO_STATE:
7050 {
7051 radio_state_change(msg->data, msg->data_len);
7052 // Reconfig APN while radio on.
7053 if(data[0]) {
7054 apn_prop_get();
7055 }
7056 break;
7057 }
7058 case INFO_URC_MSG_CGEV:
7059 {
yq.wangf98110c2025-06-11 14:50:04 +08007060 mbtk_pdp_cb_info_s *pdp_cb_info = (mbtk_pdp_cb_info_s *)(msg->data);
yq.wang69a51872025-06-17 09:45:12 +08007061 if(pdp_cb_info->cid >= MBTK_APN_CID_MIN && pdp_cb_info->cid <= MBTK_APN_CID_MAX)
7062 {
7063 net_ifc_state_change(pdp_cb_info->connect_state, pdp_cb_info->cid, NULL);
q.huange5f91fb2025-08-08 20:01:20 +08007064 if(cid_active[pdp_cb_info->cid]!=0) //LYNQ_ASR_BUG1946_Q.HUANG_20250808_ADD
7065 {
7066 cid_active[pdp_cb_info->cid] = 0;
7067 pdp_state_change(pdp_cb_info, sizeof(mbtk_pdp_cb_info_s));
7068 }
7069 else
7070 {
7071 LOGE("[%s] report cid disconnect, active flag has been 0", __func__, pdp_cb_info->cid); //LYNQ_ASR_BUG1946_Q.HUANG_20250808_ADD
7072 }
yq.wang69a51872025-06-17 09:45:12 +08007073 }
7074 else
7075 {
7076 LOGD("[%s] pdp_cb_info->cid unknown.[%d]", __func__, pdp_cb_info->cid);
7077 }
b.liuf77b86c2024-11-09 13:24:10 +08007078 break;
7079 }
7080 case INFO_URC_MSG_PDP_STATE:
7081 {
yq.wangf98110c2025-06-11 14:50:04 +08007082 //data call connect success
b.liuf77b86c2024-11-09 13:24:10 +08007083 int cme_err = MBTK_INFO_ERR_CME_NON;
yq.wangf98110c2025-06-11 14:50:04 +08007084 mbtk_pdp_cb_info_s *pdp_cb_info = (mbtk_pdp_cb_info_s *)(msg->data);
7085 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 +08007086 {
yq.wangf98110c2025-06-11 14:50:04 +08007087 LOGE("[%s] req_data_call_state_get() fail.[%d]", __func__, cme_err);
liubin281ac462023-07-19 14:22:54 +08007088 }
yq.wangf98110c2025-06-11 14:50:04 +08007089 pdp_state_change(pdp_cb_info, sizeof(mbtk_pdp_cb_info_s));
liubin281ac462023-07-19 14:22:54 +08007090 break;
7091 }
7092 case INFO_URC_MSG_NET_PS_REG_STATE:
7093 {
b.liufe320632024-01-17 20:38:08 +08007094 uint8 net_data[4];
liubin281ac462023-07-19 14:22:54 +08007095 net_data[0] = (uint8)MBTK_NET_PS_STATE;
b.liufe320632024-01-17 20:38:08 +08007096 net_data[1] = data[0]; // act
7097 net_data[3] = data[1]; // 0 - GSM/WCDMA; 1 - LTE
liubin281ac462023-07-19 14:22:54 +08007098 mbtk_net_reg_state_enum state = (mbtk_net_reg_state_enum)data[0];
7099 if(state == MBTK_NET_REG_STATE_HOME
7100 || state == MBTK_NET_REG_STATE_ROAMING) { // Registered, home network or roaming.
7101 mbtk_net_info_t info;
7102 int cme_err = MBTK_INFO_ERR_CME_NON;
7103 memset(&info, 0, sizeof(mbtk_net_info_t));
7104 if(!req_net_sel_mode_get(&info, &cme_err) && cme_err == MBTK_INFO_ERR_CME_NON)
7105 {
7106 net_data[2] = info.net_type;
7107 net_state_change(net_data, sizeof(net_data));
yq.wang69a51872025-06-17 09:45:12 +08007108#if 0
liubin281ac462023-07-19 14:22:54 +08007109 if(info.net_type >= MBTK_RADIO_TECH_UTRAN) {
7110 data_call_restart();
7111 }
yq.wang69a51872025-06-17 09:45:12 +08007112#endif
liubin281ac462023-07-19 14:22:54 +08007113 } else {
7114 net_data[2] = (uint8)0xFF;
7115 net_state_change(net_data, sizeof(net_data));
7116 }
7117 } else {
7118 net_data[2] = (uint8)0xFF;
7119 net_state_change(net_data, sizeof(net_data));
7120 }
7121 break;
7122 }
b.liuf37bd332024-03-18 13:51:24 +08007123 case INFO_URC_MSG_SET_BAND:
7124 {
7125 mbtk_band_info_t band;
7126 int cme_err = MBTK_INFO_ERR_CME_NON;
7127
7128 band.net_pref = MBTK_NET_PREF_GSM_UMTS_LTE_LTE_PREF; // 15
b.liu288093c2024-05-09 17:02:57 +08007129 band.gsm_band = (uint16)band_set_info.band_gsm;
7130 band.umts_band = (uint16)band_set_info.band_wcdma;
b.liuf37bd332024-03-18 13:51:24 +08007131 band.tdlte_band = band_set_info.band_tdlte;
7132 band.fddlte_band = band_set_info.band_fddlte;
b.liu288093c2024-05-09 17:02:57 +08007133 band.lte_ext_band = band_set_info.band_lte_ext;
7134
b.liuf37bd332024-03-18 13:51:24 +08007135 if(req_band_set(&band, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
7136 {
7137 LOG("Set band fail.");
7138 }
7139 else // Set band success.
7140 {
b.liu45bfb532024-05-11 15:37:22 +08007141 // log_hex("BAND-2", &band_set_info, sizeof(band_set_info_t));
b.liu288093c2024-05-09 17:02:57 +08007142 band_set_success = TRUE;
7143 if(band_area == MBTK_MODEM_BAND_AREA_CN) {
b.liuf37bd332024-03-18 13:51:24 +08007144 property_set("persist.mbtk.band_config", "CN");
b.liu288093c2024-05-09 17:02:57 +08007145 } else if(band_area == MBTK_MODEM_BAND_AREA_EU) {
b.liuf37bd332024-03-18 13:51:24 +08007146 property_set("persist.mbtk.band_config", "EU");
b.liu288093c2024-05-09 17:02:57 +08007147 } else if(band_area == MBTK_MODEM_BAND_AREA_SA) {
b.liuf678f992024-05-08 15:23:10 +08007148 property_set("persist.mbtk.band_config", "SA");
b.liuf37bd332024-03-18 13:51:24 +08007149 } else {
7150 property_set("persist.mbtk.band_config", "ALL");
7151 }
7152 LOG("Set band success.");
7153 }
7154 break;
7155 }
wangyouqiangce45a102024-04-18 18:08:29 +08007156 case INFO_URC_MSG_GET_SIM_STATE:
7157 {
7158 net_info.sim_state = getSIMStatus();
7159 if(net_info.sim_state == MBTK_SIM_READY)
7160 {
7161 LOG("SIM READY!");
7162 }
7163 else
7164 {
7165 LOG("SIM NOT READY!");
7166 }
7167 break;
7168 }
liubin281ac462023-07-19 14:22:54 +08007169 case INFO_URC_MSG_NET_STATE_LOG:
7170 {
7171 // Get network state and signal.
7172 char buff[256] = {0};
q.huang8a993672025-06-24 16:19:46 +08007173 uint8 data_signal[9];
yq.wangf1fc31e2024-10-12 03:21:03 -07007174 mbtk_signal_info_t signal;
liubin281ac462023-07-19 14:22:54 +08007175 memset(&signal, 0xFF, sizeof(mbtk_signal_info_t));
7176 if(!req_net_signal_get(&signal, NULL)) {
7177 char tmp[50] = {0};
7178 struct timeval log_time;
7179 gettimeofday(&log_time, NULL);
7180 struct tm* tm_t = localtime(&(log_time.tv_sec));
7181 strftime(tmp, 50, "%F %T", tm_t);
q.huang8a993672025-06-24 16:19:46 +08007182 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,
7183 signal.rsrq, signal.rsrp,signal.sinr);
b.liu06559f62024-11-01 18:48:22 +08007184 mbtk_signal_log(buff);
q.huang8a993672025-06-24 16:19:46 +08007185 if(signal_globe.type != signal.type)
7186 {
7187 data_signal[0] = signal.type;
7188 data_signal[1] = signal.rssi;
7189 data_signal[2] = signal.rxlev;
7190 data_signal[3] = signal.ber;
7191 data_signal[4] = signal.rscp;
7192 data_signal[5] = signal.ecno;
7193 data_signal[6] = signal.rsrq;
7194 data_signal[7] = signal.rsrp;
7195 data_signal[8] = signal.sinr;
7196 signal_globe.type = signal.type;
7197 signal_state_change(data_signal, sizeof(data_signal));
7198 }
7199 }
liubin281ac462023-07-19 14:22:54 +08007200 break;
7201 }
liuyang4d7ac4b2024-11-21 16:25:22 +08007202 case INFO_URC_MSG_CALL_STATE:
7203 {
7204 mbtk_call_info_t* reg = (mbtk_call_info_t*)data;
7205 char ceer[128] = {0};
7206 memset(&ceer, 0, 128);
7207 int cme_err = MBTK_INFO_ERR_CME_NON;
7208 if(req_ceer_call(ceer, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
7209 {
7210 LOG("get ceer call fail.");
7211 }
7212 else
7213 {
7214 memcpy(reg->end_reason, ceer, strlen(ceer));
7215 LOG(" reg->end_reason:%s", reg->end_reason);
7216 call_state_change(reg, sizeof(mbtk_call_info_t));
7217 }
7218
7219
7220 break;
7221 }
liubin281ac462023-07-19 14:22:54 +08007222 default:
7223 {
7224 LOGE("Unknown URC : %d", msg->msg);
7225 break;
7226 }
7227 }
7228}
7229
7230static void* pack_process_thread(void* arg)
7231{
7232 UNUSED(arg);
7233 info_queue_item_t* item = NULL;
7234 mbtk_queue_init(&info_queue);
7235 pthread_mutex_init(&info_mutex, NULL);
7236 pthread_cond_init(&info_cond, NULL);
7237
7238 memset(&band_support, 0xFF, sizeof(mbtk_band_info_t));
7239
7240 pthread_mutex_lock(&info_mutex);
7241 while(TRUE)
7242 {
7243 if(mbtk_queue_empty(&info_queue))
7244 {
7245 LOG("Packet process wait...");
7246 pthread_cond_wait(&info_cond, &info_mutex);
7247 LOG("Packet process continue...");
7248 }
7249 else
7250 {
7251 LOG("Packet process queue not empty,continue...");
7252 }
7253
7254 // Process all information request.
7255 mbtk_info_err_enum err;
7256 while((item = (info_queue_item_t*)mbtk_queue_get(&info_queue)) != NULL)
7257 {
7258 if(item->cli_info) { // REQ form client.
7259 mbtk_info_pack_t *pack = (mbtk_info_pack_t*)item->pack;
7260 LOG("Process REQ %s.", id2str(pack->info_id));
7261 at_process = true;
7262 err = pack_req_process(item->cli_info, pack);
7263 if(err != MBTK_INFO_ERR_SUCCESS)
7264 {
7265 if(item->cli_info->fd != DATA_CALL_BOOTCONN_FD)
7266 {
7267 pack_error_send(item->cli_info->fd, pack->info_id + 1, err);
7268 }
7269 else
7270 {
wangyouqiang80487e42024-05-24 15:06:20 +08007271 if(pack->data != NULL)
7272 {
7273 free(pack->data);
7274 }
7275 if(item->cli_info)
7276 {
7277 free(item->cli_info);
7278 }
liubin281ac462023-07-19 14:22:54 +08007279 }
7280 }
7281 at_process = false;
q.huanga0261872025-08-01 19:44:59 +08007282 at_cfun_command = false; //LYNQ_ASR_BUG2001_Q.HUANG_20250731_ADD
liubin281ac462023-07-19 14:22:54 +08007283 mbtk_info_pack_free(&pack);
7284 free(item);
7285 } else { // REQ from myself.
7286 info_urc_msg_t *urc = (info_urc_msg_t*)item->pack;
7287 LOG("Process URC %d.", urc->msg);
7288 urc_msg_process(urc);
7289 if(!urc->data)
7290 free(urc->data);
7291 free(urc);
7292 }
7293 }
7294 }
7295 pthread_mutex_unlock(&info_mutex);
7296 return NULL;
7297}
7298
7299void apn_prop_get()
7300{
7301 char prop_name[20];
7302 char prop_data[300];
7303 // cid : 2 - 7
7304 int cid = MBTK_APN_CID_MIN;
7305 mbtk_apn_info_t apn;
q.huangac0e8a02025-08-29 17:11:41 +08007306 int cme_err=MBTK_INFO_ERR_CME_NON;
liubin281ac462023-07-19 14:22:54 +08007307 for(; cid <= MBTK_APN_CID_MAX; cid++) {
7308 memset(prop_name, 0, 20);
7309 memset(prop_data, 0, 300);
7310 memset(&apn, 0, sizeof(mbtk_apn_info_t));
7311 sprintf(prop_name, "%s_%d",MBTK_APN_PROP,cid);
7312 if(property_get(prop_name, prop_data, "") > 0 && !str_empty(prop_data)) {
7313 apn.cid = cid;
7314 char *ptr_1 = prop_data;
7315 apn.ip_type = (mbtk_ip_type_enum)atoi(ptr_1);
7316 ptr_1 = strstr(ptr_1, ",");
7317 if(!ptr_1) {
7318 continue;
7319 }
7320 ptr_1++; // Jump ',' to apn
7321
7322 char *ptr_2 = strstr(ptr_1, ",");
7323 if(!ptr_2) {
7324 continue;
7325 }
7326 memcpy(apn.apn, ptr_1, ptr_2 - ptr_1); // apn
7327
7328 ptr_2++; // Jump ',' to user
7329 ptr_1 = strstr(ptr_2, ",");
7330 if(!ptr_1) {
7331 continue;
7332 }
7333 if(memcmp(ptr_2, "NULL", 4)) { // Not "NULL"
7334 memcpy(apn.user, ptr_2, ptr_1 - ptr_2); // user
7335 }
7336
7337 ptr_1++; // Jump ',' to pass
7338 ptr_2 = strstr(ptr_1, ",");
7339 if(!ptr_2) {
7340 continue;
7341 }
7342 if(memcmp(ptr_1, "NULL", 4)) { // Not "NULL"
7343 memcpy(apn.pass, ptr_1, ptr_2 - ptr_1); // pass
7344 }
7345
7346 ptr_2++; // Jump ',' to auth (Is last item)
7347 if(memcmp(ptr_2, "NULL", 4)) { // Not "NULL"
7348 memcpy(apn.auth, ptr_2, strlen(ptr_2)); // auth
7349 }
q.huangac0e8a02025-08-29 17:11:41 +08007350
7351 req_apn_set(&apn, &cme_err);
liubin281ac462023-07-19 14:22:54 +08007352 }
7353 }
7354}
7355
b.liu9e8584b2024-11-06 19:21:28 +08007356#if 0
liubin281ac462023-07-19 14:22:54 +08007357/*
b.liue0ab2442024-02-06 18:53:28 +08007358root@OpenWrt:/usrdata# cat /proc/mtd
7359dev: size erasesize name
7360mtd0: 00040000 00020000 "bootloader"
7361mtd1: 00020000 00020000 "cp_reliabledata"
7362mtd2: 00020000 00020000 "ap_reliabledata"
7363mtd3: 00020000 00020000 "cp_reliabledata_backup"
7364mtd4: 00020000 00020000 "ap_reliabledata_backup"
7365mtd5: 00020000 00020000 "mep-ota"
7366mtd6: 00020000 00020000 "mep-ota_backup"
7367mtd7: 00040000 00020000 "dtim"
7368mtd8: 00f40000 00020000 "cpimage"
7369mtd9: 000c0000 00020000 "u-boot"
7370mtd10: 00500000 00020000 "kernel"
7371mtd11: 00100000 00020000 "asr_flag"
7372mtd12: 01400000 00020000 "rootfs"
7373mtd13: 01400000 00020000 "oem_data"
7374mtd14: 01e00000 00020000 "OTA"
7375mtd15: 01400000 00020000 "rootfs_data"
7376mtd16: 081a0000 00020000 "user_data"
7377mtd17: 00d20000 00020000 "MRVL_BBM"
7378*/
7379static int partition_name_2_dev(const char *name, char *dev) {
7380 if(name == NULL || dev == NULL) {
7381 LOGE("ARG error.");
q.huang8a993672025-06-24 16:19:46 +08007382 return -1;
b.liue0ab2442024-02-06 18:53:28 +08007383 }
7384
b.liu9e8584b2024-11-06 19:21:28 +08007385 FILE* fp = fopen("/proc/mtd", "r");
q.huang8a993672025-06-24 16:19:46 +08007386 if (fp == NULL) {
7387 LOGE("Open MTD failed!");
7388 return -1;
7389 }
b.liue0ab2442024-02-06 18:53:28 +08007390
7391 char buf[1024];
q.huang8a993672025-06-24 16:19:46 +08007392 while (fgets(buf, 1024, fp) != NULL) {
7393 if(strstr(buf, name)) {
b.liue0ab2442024-02-06 18:53:28 +08007394 int index = atoi(buf + 3);
7395 sprintf(dev, "/dev/mtdblock%d", index);
7396 LOGD("%s -> %s", name, dev);
7397 return 0;
q.huang8a993672025-06-24 16:19:46 +08007398 }
7399 }
b.liue0ab2442024-02-06 18:53:28 +08007400
7401 return -1;
7402}
b.liu9e8584b2024-11-06 19:21:28 +08007403#endif
7404#if 0
b.liue0ab2442024-02-06 18:53:28 +08007405static int custom_partition_read(const char *name, mbtk_cust_info_t *cust_info)
7406{
b.liu9e8584b2024-11-06 19:21:28 +08007407 int fd = 0;
7408 unsigned int flaglen = 0;
b.liue0ab2442024-02-06 18:53:28 +08007409 flaglen = sizeof(mbtk_cust_info_t);
7410
7411 char mtd_path[50] = {0};
7412 if(partition_name_2_dev(name, mtd_path)) {
7413 LOGE("partition_name_2_dev() failed!");
7414 return -1;
7415 }
7416
7417 fd = open(mtd_path, O_RDONLY);
7418 if (fd < 0) {
7419 LOGE("Fatal error: can't open cust info %s\n", mtd_path);
7420 return -1;
7421 }
7422
7423 if (read(fd, cust_info, flaglen) < 0)
7424 goto error;
7425 if (cust_info->header != CUST_INFO_HEADER) {
7426 LOGE("Cust info partition error.");
7427 goto error;
7428 } else {
7429 if(cust_info->band_type == 1) { // CN
7430 LOGD("Band : CN");
7431 } else if(cust_info->band_type == 2) { // EU
7432 LOGD("Band : EU");
7433 } else {
7434 LOGE("Unknown band type:%d", cust_info->band_type);
7435 goto error;
7436 }
7437 }
7438 close(fd);
7439 return 0;
7440error:
7441 close(fd);
7442 return -1;
7443}
b.liu9e8584b2024-11-06 19:21:28 +08007444#endif
b.liue0ab2442024-02-06 18:53:28 +08007445
7446/*
liubin281ac462023-07-19 14:22:54 +08007447AT*BAND=15,78,147,482,134742231
7448
7449OK
7450*/
b.liubb5e7682024-02-28 20:13:04 +08007451static void* band_config_thread()
liubin281ac462023-07-19 14:22:54 +08007452{
b.liubb5e7682024-02-28 20:13:04 +08007453 mbtk_device_info_modem_t info_modem;
b.liuf37bd332024-03-18 13:51:24 +08007454 memset(&band_set_info, 0, sizeof(band_set_info_t));
b.liubb5e7682024-02-28 20:13:04 +08007455 memset(&info_modem, 0, sizeof(mbtk_device_info_modem_t));
b.liu288093c2024-05-09 17:02:57 +08007456 band_set_success = FALSE;
b.liuf37bd332024-03-18 13:51:24 +08007457 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 +08007458 LOGD("mbtk_dev_info_read(MODEM) fail, use default band.");
b.liu288093c2024-05-09 17:02:57 +08007459 band_area = MBTK_MODEM_BAND_AREA_ALL;
b.liuf37bd332024-03-18 13:51:24 +08007460 band_set_info.band_gsm = MBTK_BAND_ALL_GSM_DEFAULT;
7461 band_set_info.band_wcdma = MBTK_BAND_ALL_WCDMA_DEFAULT;
7462 band_set_info.band_tdlte = MBTK_BAND_ALL_TDLTE_DEFAULT;
7463 band_set_info.band_fddlte = MBTK_BAND_ALL_FDDLTE_DEFAULT;
b.liu288093c2024-05-09 17:02:57 +08007464 band_set_info.band_lte_ext = MBTK_BAND_ALL_EXT_LTE_DEFAULT;
b.liubb5e7682024-02-28 20:13:04 +08007465 } else {
b.liub7530d22025-06-16 19:49:05 +08007466 if(info_modem.version == DEV_INFO_VERSION_V1) {
7467 band_area = info_modem.modem.v1.band_area;
7468 band_set_info.band_gsm = info_modem.modem.v1.band_gsm;
7469 band_set_info.band_wcdma = info_modem.modem.v1.band_wcdma;
7470 band_set_info.band_tdlte = info_modem.modem.v1.band_tdlte;
7471 band_set_info.band_fddlte = info_modem.modem.v1.band_fddlte;
7472 band_set_info.band_lte_ext = info_modem.modem.v1.band_lte_ext;
7473 } else {
7474 band_area = info_modem.modem.v2.band_area;
7475 band_set_info.band_gsm = info_modem.modem.v2.band_gsm;
7476 band_set_info.band_wcdma = info_modem.modem.v2.band_wcdma;
7477 band_set_info.band_tdlte = info_modem.modem.v2.band_tdlte;
7478 band_set_info.band_fddlte = info_modem.modem.v2.band_fddlte;
7479 band_set_info.band_lte_ext = info_modem.modem.v2.band_lte_ext;
7480 }
b.liuf37bd332024-03-18 13:51:24 +08007481 }
b.liubb5e7682024-02-28 20:13:04 +08007482
b.liuf37bd332024-03-18 13:51:24 +08007483 bool is_first = TRUE;
b.liu288093c2024-05-09 17:02:57 +08007484 while(!band_set_success) {
b.liuf37bd332024-03-18 13:51:24 +08007485 info_urc_msg_t *urc = (info_urc_msg_t*)malloc(sizeof(info_urc_msg_t));
7486 if(!urc)
7487 {
7488 LOG("malloc() fail[%d].", errno);
7489 break;
b.liubb5e7682024-02-28 20:13:04 +08007490 } else {
b.liuf37bd332024-03-18 13:51:24 +08007491 urc->msg = INFO_URC_MSG_SET_BAND;
7492 urc->data = NULL;
7493 urc->data_len = 0;
7494 send_pack_to_queue(NULL, urc);
7495
7496 if(is_first) {
7497 is_first = FALSE;
7498 } else {
b.liu288093c2024-05-09 17:02:57 +08007499 LOGE("*BAND exec error, will retry in 5s.");
b.liuf37bd332024-03-18 13:51:24 +08007500 }
b.liu288093c2024-05-09 17:02:57 +08007501 sleep(5);
b.liubb5e7682024-02-28 20:13:04 +08007502 }
7503 }
7504
b.liuf37bd332024-03-18 13:51:24 +08007505 LOGD("Set Band thread exit.");
b.liubb5e7682024-02-28 20:13:04 +08007506 return NULL;
liubin281ac462023-07-19 14:22:54 +08007507}
7508
yq.wang8333e172024-12-09 23:53:57 -08007509static void* reboot_reason_thread(void* arg)
7510{
7511 UNUSED(arg);
7512 int ret = 0;
7513 char buff[32] = {0};
7514 mbtk_device_info_reboot_flag_enum reboot_reason = MBTK_REBOOT_FLAG_NORMAL;
7515 mbtk_device_info_basic_t info_basic = {0};
7516 memset(&info_basic, 0, sizeof(mbtk_device_info_basic_t));
7517
7518
7519 ret = mbtk_dev_info_read(MBTK_DEVICE_INFO_ITEM_BASIC, &(info_basic), sizeof(mbtk_device_info_basic_t));
7520 if(ret != 0)
7521 {
7522 LOGE("[%s] mbtk_dev_info_read(BASIC) fail.", __func__);
7523 reboot_reason = MBTK_REBOOT_FLAG_UNKNOWN;
7524 }
7525 else
7526 {
b.liub7530d22025-06-16 19:49:05 +08007527 reboot_reason = info_basic.version == DEV_INFO_VERSION_V1 ? info_basic.basic.v1.reboot_flag :
7528 info_basic.basic.v2.reboot_flag;
7529 if(reboot_reason != MBTK_REBOOT_FLAG_NORMAL)
yq.wang8333e172024-12-09 23:53:57 -08007530 {
b.liub7530d22025-06-16 19:49:05 +08007531 if(info_basic.version == DEV_INFO_VERSION_V1) {
7532 info_basic.basic.v1.reboot_flag = MBTK_REBOOT_FLAG_NORMAL;
7533 } else {
7534 info_basic.basic.v2.reboot_flag = MBTK_REBOOT_FLAG_NORMAL;
7535 }
yq.wang8333e172024-12-09 23:53:57 -08007536 ret = mbtk_dev_info_write(MBTK_DEVICE_INFO_ITEM_BASIC, &info_basic, sizeof(mbtk_device_info_basic_t));
7537 if(ret != 0) {
7538 LOGE("[%s] mbtk_dev_info_write(BASIC) fail.", __func__);
7539 }
7540 }
7541 }
7542
rx.xie24956e92025-08-27 23:51:31 -07007543 LOGE("[%s] reboot reason [%d].", __func__, reboot_reason);
yq.wang8333e172024-12-09 23:53:57 -08007544 memset(buff, 0x0, 32);
7545 sprintf(buff, "%d", reboot_reason);
7546 property_set("persist.mbtk.reboot_reason", buff);
7547
7548 LOGD("[%s] reboot reason thread exit.", __func__);
7549 return NULL;
7550}
7551
7552
b.liu9e8584b2024-11-06 19:21:28 +08007553#if 0
liubin281ac462023-07-19 14:22:54 +08007554static void* net_monitor_thread(void* arg)
7555{
7556 UNUSED(arg);
7557 // Start network monitor
b.liu9e8584b2024-11-06 19:21:28 +08007558// int cid;
liubin281ac462023-07-19 14:22:54 +08007559 while(1) {
7560#if 0
7561 // Config IP
7562 list_node_t* apn_list = NULL;
7563 if(!apn_state_get(&apn_list) && apn_list != NULL) {
7564 info_apn_ip_t *apn = NULL;
7565 for(cid = MBTK_APN_CID_MIN; cid <= MBTK_APN_CID_MAX && cid_active[cid]; cid++) {
7566 bool ip_found = false;
7567 list_first(apn_list);
7568 while ((apn = (info_apn_ip_t*) list_next(apn_list))) {
7569 if(cid == apn->cid) {
7570 ip_found = true;
7571 break;
7572 }
7573 }
7574
7575 char dev[20] = {0};
7576 sprintf(dev, "ccinet%d", cid - 1);
7577 if(ip_found) { // Ip ok,set IP.
7578 if(apn->ipv4_valid) {
7579 if(mbtk_ifc_configure2(dev, (char*)apn->ipv4, 0, NULL, "255.255.255.0")) {
7580 LOGD("Config %s IPv4 %s fail.", dev, apn->ipv4);
7581 } else {
7582 LOGD("Config %s IPv4 %s success.", dev, apn->ipv4);
7583 }
7584 }
7585
7586 if(apn->ipv6_valid) {
7587 if(mbtk_ipv6_config(dev, (char*)apn->ipv6, 64)) {
7588 LOGD("Config %s IPv6 %s fail.", dev, apn->ipv6);
7589 } else {
7590 LOGD("Config %s IPv6 %s success.", dev, apn->ipv6);
7591 }
7592 }
7593 } else { // No ip
7594 if(mbtk_ifc_configure2(dev, NULL, 0, NULL, NULL)) {
7595 LOGD("Config %s IPv4 0 fail.", dev);
7596 } else {
7597 LOGD("Config %s IPv4 0 success.", dev);
7598 }
7599 }
7600 }
7601
7602 list_free(apn_list);
7603 }
7604#endif
7605
7606 if(net_info.radio_state == MBTK_RADIO_STATE_ON && net_info.sim_state == MBTK_SIM_READY) {
7607#if 0
7608 urc_msg_distribute(true, INFO_URC_MSG_NET_CS_REG_STATE, NULL, 0);
7609#else
7610 info_urc_msg_t *urc = (info_urc_msg_t*)malloc(sizeof(info_urc_msg_t));
7611 if(!urc)
7612 {
7613 LOG("malloc() fail[%d].", errno);
7614 } else {
7615 urc->msg = INFO_URC_MSG_NET_STATE_LOG;
7616 urc->data = NULL;
7617 urc->data_len = 0;
7618 send_pack_to_queue(NULL, urc);
7619 }
7620#endif
wangyouqiangce45a102024-04-18 18:08:29 +08007621 sleep(15);
liubin281ac462023-07-19 14:22:54 +08007622 }
wangyouqiangce45a102024-04-18 18:08:29 +08007623 else
7624 {
7625 info_urc_msg_t *urc = (info_urc_msg_t*)malloc(sizeof(info_urc_msg_t));
7626 if(!urc)
7627 {
7628 LOG("malloc() fail[%d].", errno);
7629 }
7630 else
7631 {
7632 urc->msg = INFO_URC_MSG_GET_SIM_STATE;
7633 urc->data = NULL;
7634 urc->data_len = 0;
7635 send_pack_to_queue(NULL, urc);
7636 }
7637 sleep(2);
7638 }
liubin281ac462023-07-19 14:22:54 +08007639 }
7640
7641 LOGD("monitor_thread exit.");
7642 return NULL;
7643}
b.liu9e8584b2024-11-06 19:21:28 +08007644#endif
liubin281ac462023-07-19 14:22:54 +08007645
b.liu9e8584b2024-11-06 19:21:28 +08007646#if 0
liubin281ac462023-07-19 14:22:54 +08007647static void* urc_process_thread(void* arg)
7648{
7649 UNUSED(arg);
7650 info_urc_msg_t* item = NULL;
7651 mbtk_queue_init(&urc_queue);
7652 pthread_mutex_init(&urc_mutex, NULL);
7653 pthread_cond_init(&urc_cond, NULL);
7654
7655 pthread_mutex_lock(&urc_mutex);
7656 while(TRUE)
7657 {
7658 if(mbtk_queue_empty(&urc_queue))
7659 {
7660 LOG("URC process wait...");
7661 pthread_cond_wait(&urc_cond, &urc_mutex);
7662 LOG("URC process continue...");
7663 }
7664 else
7665 {
7666 LOG("URC process queue not empty,continue...");
7667 }
7668
7669 // Process all information request.
7670 while((item = (info_urc_msg_t*)mbtk_queue_get(&urc_queue)) != NULL)
7671 {
7672 LOG("Process URC %d.", item->msg);
7673 uint8 *data = (uint8*)item->data;
7674 switch(item->msg) {
7675 case INFO_URC_MSG_RADIO_STATE:
7676 {
7677 radio_state_change(item->data, item->data_len);
7678 break;
7679 }
7680 case INFO_URC_MSG_CGEV:
7681 {
7682 bool act = data[0];
7683 int cid = data[1];
7684 if(cid > 0) {
7685 net_ifc_state_change(act, cid);
7686 }
7687 break;
7688 }
7689 default:
7690 {
7691 LOGE("Unknown URC : %d", item->msg);
7692 break;
7693 }
7694 }
7695 if(!item->data)
7696 free(item->data);
7697 free(item);
7698 }
7699 }
7700 pthread_mutex_unlock(&urc_mutex);
7701
7702 return NULL;
7703}
b.liu9e8584b2024-11-06 19:21:28 +08007704#endif
liubin281ac462023-07-19 14:22:54 +08007705
7706static void ril_at_ready_process()
7707{
7708 net_info.radio_state = (isRadioOn() == 1) ? MBTK_RADIO_STATE_ON : MBTK_RADIO_STATE_OFF;
7709#if 1
7710 if (net_info.radio_state != MBTK_RADIO_STATE_ON)
7711 {
7712 setRadioPower(1);
7713 } else { // Radio has ON
7714 apn_prop_get();
7715 }
7716
7717 if(net_info.radio_state == MBTK_RADIO_STATE_ON)
7718 {
7719 at_send_command("AT+CEREG=2", NULL);
7720 }
7721
b.liu9e8584b2024-11-06 19:21:28 +08007722// int count = 0;
liubin281ac462023-07-19 14:22:54 +08007723#endif
7724 net_info.sim_state = getSIMStatus();
7725#if 0
7726 while (net_info.sim_state != MBTK_SIM_READY && count < 30)
7727 {
7728 if(net_info.radio_state != MBTK_RADIO_STATE_ON)
7729 {
7730 setRadioPower(1);
7731 }
7732 LOGD("Waitting for SIM READY...");
7733 sleep(1);
7734 net_info.sim_state = getSIMStatus();
7735 count++;
7736 }
7737#endif
7738 if(net_info.sim_state == MBTK_SIM_READY)
7739 {
7740 LOGD("SIM READY!");
r.xiao7c62bc62024-05-24 00:53:30 -07007741 at_send_command("AT+COPS=3", NULL);
liubin281ac462023-07-19 14:22:54 +08007742 }
7743 else
7744 {
7745 LOGE("SIM NOT READY!");
7746 }
liubin281ac462023-07-19 14:22:54 +08007747}
7748
wangyouqiang80487e42024-05-24 15:06:20 +08007749int mbtk_get_apn_send_pack(void)
7750{
7751 sock_client_info_t *info = (sock_client_info_t*)malloc(sizeof(sock_client_info_t));
7752 if(info == NULL)
7753 {
7754 LOG("clinent_info malloc() fail.");
7755 return -1;
7756 }
7757 memset(info, 0, sizeof(sock_client_info_t));
7758 info->fd = DATA_CALL_APN_GET_FD;
7759
7760 mbtk_info_pack_t* pack = mbtk_info_pack_creat(MBTK_INFO_ID_NET_QSER_APN_REQ);
7761 if(pack == NULL)
7762 {
7763 free(info);
7764 LOG("Packet malloc() fail.");
7765 return -1;
7766 }
7767
7768 send_pack_to_queue(info, pack);
7769 return 0;
7770}
7771
liubin281ac462023-07-19 14:22:54 +08007772int mbtk_info_server_start()
7773{
7774 signal(SIGPIPE, SIG_IGN);
7775
wangyouqiangce45a102024-04-18 18:08:29 +08007776 //check cfun and sim card status
7777 ril_at_ready_process();
7778
7779 //any AT instruction that is not sent through pack_process_thread needs to precede the thread
7780 //thread create
liubin281ac462023-07-19 14:22:54 +08007781 if(sock_listen_fd > 0)
7782 {
7783 LOG("Information Server Has Started.");
7784 return -1;
7785 }
7786
7787 struct sockaddr_un server_addr;
7788 sock_listen_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
7789 if(sock_listen_fd < 0)
7790 {
7791 LOG("socket() fail[%d].", errno);
7792 return -1;
7793 }
7794
7795 // Set O_NONBLOCK
7796 int flags = fcntl(sock_listen_fd, F_GETFL, 0);
7797 if (flags < 0)
7798 {
7799 LOG("Get flags error:%d", errno);
7800 goto error;
7801 }
7802 flags |= O_NONBLOCK;
7803 if (fcntl(sock_listen_fd, F_SETFL, flags) < 0)
7804 {
7805 LOG("Set flags error:%d", errno);
7806 goto error;
7807 }
7808
7809 unlink(SOCK_INFO_PATH);
7810 memset(&server_addr, 0, sizeof(struct sockaddr_un));
7811 server_addr.sun_family = AF_LOCAL;
7812 strcpy(server_addr.sun_path, SOCK_INFO_PATH);
7813 if(bind(sock_listen_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)))
7814 {
7815 LOG("bind() fail[%d].", errno);
7816 goto error;
7817 }
7818
7819 if(listen(sock_listen_fd, SOCK_CLIENT_MAX))
7820 {
7821 LOG("listen() fail[%d].", errno);
7822 goto error;
7823 }
7824
7825 sock_client_list = list_create(sock_cli_free_func);
7826 if(sock_client_list == NULL)
7827 {
7828 LOG("list_create() fail.");
7829 goto error;
7830 }
7831
yq.wang8333e172024-12-09 23:53:57 -08007832 pthread_t info_pid, pack_pid/*, monitor_pid, urc_pid*/, bootconn_pid, reboot_reason_pid;
liubin281ac462023-07-19 14:22:54 +08007833 pthread_attr_t thread_attr;
7834 pthread_attr_init(&thread_attr);
7835 if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
7836 {
7837 LOG("pthread_attr_setdetachstate() fail.");
7838 goto error;
7839 }
7840
7841 if(pthread_create(&info_pid, &thread_attr, info_main_pthread, NULL))
7842 {
7843 LOG("pthread_create() fail.");
7844 goto error;
7845 }
7846
7847 if(pthread_create(&pack_pid, &thread_attr, pack_process_thread, NULL))
7848 {
7849 LOG("pthread_create() fail.");
7850 goto error;
7851 }
7852
7853#if 0
7854 if(pthread_create(&urc_pid, &thread_attr, urc_process_thread, NULL))
7855 {
7856 LOG("pthread_create() fail.");
7857 goto error;
7858 }
7859#endif
7860
b.liubb5e7682024-02-28 20:13:04 +08007861 // Set Band
7862 // AT*BAND=15,78,147,482,134742231
7863 char buff[10];
7864 memset(buff, 0, 10);
7865 property_get("persist.mbtk.band_config", buff, "");
7866 if(strlen(buff) == 0) {
7867 pthread_t band_pid;
7868 if(pthread_create(&band_pid, &thread_attr, band_config_thread, NULL))
7869 {
7870 LOG("pthread_create() fail.");
7871 }
7872 }
7873
yq.wang8333e172024-12-09 23:53:57 -08007874 if(pthread_create(&reboot_reason_pid, &thread_attr, reboot_reason_thread, NULL))
7875 {
7876 LOGE("[%s] reboot_reason pthread_create() fail.", __func__);
7877 goto error;
7878 }
b.liuf1ab8152024-05-23 13:16:07 +08007879#if 0
liubin281ac462023-07-19 14:22:54 +08007880 if(pthread_create(&monitor_pid, &thread_attr, net_monitor_thread, NULL))
7881 {
7882 LOG("pthread_create() fail.");
7883 }
b.liuf1ab8152024-05-23 13:16:07 +08007884#endif
liubin281ac462023-07-19 14:22:54 +08007885
7886 //mbtk wyq for data_call_ex add start
7887 if(pthread_create(&bootconn_pid, &thread_attr, data_call_bootconn_pthread, NULL))
7888 {
7889 LOG("pthread_create() fail.");
7890 }
7891 //mbtk wyq for data_call_ex add end
7892
7893 pthread_attr_destroy(&thread_attr);
7894
wangyouqiang80487e42024-05-24 15:06:20 +08007895 mbtk_qser_apn_init();
7896
liubin281ac462023-07-19 14:22:54 +08007897 LOG("MBTK Information Server Start...");
7898
7899 return 0;
7900
7901error:
7902 close(sock_listen_fd);
7903 sock_listen_fd = -1;
7904 return -1;
7905}
7906
7907#if 0
7908int main(int argc, char *argv[])
7909{
7910 if(mbtk_info_server_start())
7911 {
7912 return -1;
7913 }
7914
7915 while(1)
7916 {
7917 sleep(24 * 60 * 60);
7918 }
7919
7920 return 0;
7921}
7922#endif
7923
7924