blob: f935a65751d9cd3057ec27db7bc2797d217fba85 [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
321/*
322AT*BAND=?
323*BAND:(0-18),79,147,482,524503
324
325
326OK
327
328AT*BAND=15,78,147,482,134742231
329
330
331*/
332static void band_support_get()
333{
334 // Support band has get.
335 if(band_support.net_pref != 0xFF) {
336 return;
337 }
338
339#if 1
340 // 79,147,482,524503
b.liu288093c2024-05-09 17:02:57 +0800341 band_support.gsm_band = (uint16)79; // GSM : B2/B3/B5/B8(GSM 850/PGSM 900/EGSM 900/DCS GSM 1800/PCS GSM 1900)
342 band_support.umts_band = (uint16)155; // WCDMA : B1/B2/B4/B5/B8
343 band_support.tdlte_band = (uint32)482; // TDD-LTE : B34/B38/B39/B40/B41
344 band_support.fddlte_band = (uint32)134742239; // FDD-LTE : B1/B2/B3/B4/B5/B7/B8/B20/B28
345 band_support.lte_ext_band = (uint32)2; // B66
liubin281ac462023-07-19 14:22:54 +0800346 band_support.net_pref = (uint8)0;
347#else
348 ATResponse *response = NULL;
349 int tmp_int;
350 char *tmp_str;
351 int err = at_send_command_singleline("AT*BAND=?", "*BAND:", &response);
352
353 if (err < 0 || response->success == 0 || !response->p_intermediates)
354 goto exit;
355
356 char *line = response->p_intermediates->line;
357 err = at_tok_start(&line);
358 if (err < 0)
359 {
360 goto exit;
361 }
362
363 err = at_tok_nextstr(&line, &tmp_str);
364 if (err < 0)
365 {
366 goto exit;
367 }
368
369 err = at_tok_nextint(&line, &tmp_int);
370 if (err < 0)
371 {
372 goto exit;
373 }
374 band_support.gsm_band = (uint16)tmp_int;
375
376 err = at_tok_nextint(&line, &tmp_int);
377 if (err < 0)
378 {
379 goto exit;
380 }
381 band_support.umts_band = (uint16)tmp_int;
382
383 err = at_tok_nextint(&line, &tmp_int);
384 if (err < 0)
385 {
386 goto exit;
387 }
388 band_support.tdlte_band = (uint32)tmp_int;
389
390 err = at_tok_nextint(&line, &tmp_int);
391 if (err < 0)
392 {
393 goto exit;
394 }
395 band_support.fddlte_band = (uint32)tmp_int;
396 band_support.net_pref = (uint8)0;
397exit:
398 at_response_free(response);
399#endif
400}
401
b.liu9e8584b2024-11-06 19:21:28 +0800402#if 0
liubin281ac462023-07-19 14:22:54 +0800403static int parseRegistrationState(char *str, int *items, int **response)
404{
405 int err;
406 char *line = str, *p;
407 int *resp = NULL;
408 int skip;
409 int commas;
410
411 LOGD("parseRegistrationState. Parsing: %s",str);
412 err = at_tok_start(&line);
413 if (err < 0) goto error;
414
415 /* Ok you have to be careful here
416 * The solicited version of the CREG response is
417 * +CREG: n, stat, [lac, cid]
418 * and the unsolicited version is
419 * +CREG: stat, [lac, cid]
420 * The <n> parameter is basically "is unsolicited creg on?"
421 * which it should always be
422 *
423 * Now we should normally get the solicited version here,
424 * but the unsolicited version could have snuck in
425 * so we have to handle both
426 *
427 * Also since the LAC and CID are only reported when registered,
428 * we can have 1, 2, 3, or 4 arguments here
429 *
430 * finally, a +CGREG: answer may have a fifth value that corresponds
431 * to the network type, as in;
432 *
433 * +CGREG: n, stat [,lac, cid [,networkType]]
434 */
435
436 /* count number of commas */
437 commas = 0;
438 for (p = line ; *p != '\0' ; p++)
439 {
440 if (*p == ',') commas++;
441 }
442
443 resp = (int *)calloc(commas + 1, sizeof(int));
444 if (!resp) goto error;
445 switch (commas)
446 {
447 case 0: /* +CREG: <stat> */
448 err = at_tok_nextint(&line, &resp[0]);
449 if (err < 0) goto error;
450 //resp[1] = -1;
451 //resp[2] = -1;
452 break;
453
454 case 1: /* +CREG: <n>, <stat> */
455 err = at_tok_nextint(&line, &skip);
456 if (err < 0) goto error;
457 err = at_tok_nextint(&line, &resp[0]);
458 if (err < 0) goto error;
459 resp[1] = -1;
460 //resp[2] = -1;
461 if (err < 0) goto error;
462 break;
463
464 case 2: /* +CREG: <stat>, <lac>, <cid> */
465 err = at_tok_nextint(&line, &resp[0]);
466 if (err < 0) goto error;
467 err = at_tok_nexthexint(&line, &resp[1]);
468 if (err < 0) goto error;
469 err = at_tok_nexthexint(&line, &resp[2]);
470 if (err < 0) goto error;
471 break;
472 case 3: /* +CREG: <n>, <stat>, <lac>, <cid> */
473 /* +CEREG: 1,"8330","06447340",7 */
474#if 0
475 err = at_tok_nextint(&line, &skip);
476 if (err < 0) goto error;
477 err = at_tok_nextint(&line, &resp[0]);
478 if (err < 0) goto error;
479 err = at_tok_nexthexint(&line, &resp[1]);
480 if (err < 0) goto error;
481 err = at_tok_nexthexint(&line, &resp[2]);
482 if (err < 0) goto error;
483#else
484 err = at_tok_nextint(&line, &resp[0]);
485 if (err < 0) goto error;
486 err = at_tok_nextint(&line, &resp[1]);
487 if (err < 0) goto error;
488 err = at_tok_nexthexint(&line, &resp[2]);
489 if (err < 0) goto error;
490 err = at_tok_nextint(&line, &resp[3]);
491 if (err < 0) goto error;
492#endif
493 break;
494 /* special case for CGREG, there is a fourth parameter
495 * that is the network type (unknown/gprs/edge/umts)
496 */
497 case 4: /* +CGREG: <n>, <stat>, <lac>, <cid>, <networkType> */
498 /* +CEREG: 2,1,"8330","06447340",7 */
499 err = at_tok_nextint(&line, &skip);
500 if (err < 0) goto error;
501 err = at_tok_nextint(&line, &resp[0]);
502 if (err < 0) goto error;
503 err = at_tok_nexthexint(&line, &resp[1]);
504 if (err < 0) goto error;
505 err = at_tok_nexthexint(&line, &resp[2]);
506 if (err < 0) goto error;
507 err = at_tok_nexthexint(&line, &resp[3]);
508 if (err < 0) goto error;
509 break;
510 default:
511 goto error;
512 }
513 /*
514 if(commas > 1) {
515 s_lac = resp[1];
516 s_cid = resp[2];
517 }*/
518 if (response)
519 *response = resp;
520 if (items)
521 *items = commas + 1;
522 return 0;
523error:
524 free(resp);
525 return -1;
526}
b.liu9e8584b2024-11-06 19:21:28 +0800527#endif
liubin281ac462023-07-19 14:22:54 +0800528/*
5290: minimum functionality
5301: full functionality
5313: disable phone receive RF circuits.
5324: disable phone both transmit and receive RF circuits
5335: disable SIM
5346: turn off full secondary receive.
535-1: fail
536*/
537static int isRadioOn()
538{
539 ATResponse *p_response = NULL;
540 int err;
541 char *line;
542 int ret;
543
544 err = at_send_command_singleline("AT+CFUN?", "+CFUN:", &p_response);
545
546 if (err < 0 || p_response->success == 0 || !p_response->p_intermediates)
547 {
548 // assume radio is off
549 goto error;
550 }
551
552 line = p_response->p_intermediates->line;
553
554 err = at_tok_start(&line);
555 if (err < 0) goto error;
556
557 err = at_tok_nextint(&line, &ret);
558 if (err < 0) goto error;
559
560 at_response_free(p_response);
561
562 if(ret == 1) {
563 net_info.radio_state = MBTK_RADIO_STATE_ON;
564 } else {
565 net_info.radio_state = MBTK_RADIO_STATE_OFF;
566 }
567
568 return ret;
569
570error:
571
572 at_response_free(p_response);
573 return -1;
574}
575
576/** Returns SIM_NOT_READY on error */
577static mbtk_sim_state_enum getSIMStatus()
578{
579 ATResponse *p_response = NULL;
580 int err;
581 mbtk_sim_state_enum ret;
582 char *cpinLine;
583 char *cpinResult;
584
585 err = at_send_command_singleline("AT+CPIN?", "+CPIN:", &p_response);
586
587 if (err < 0 || p_response->success == 0 || !p_response->p_intermediates)
588 {
yq.wang586a0df2024-10-24 20:10:37 -0700589 switch (at_get_cme_error(p_response))
590 {
591 case CME_SUCCESS:
592 break;
593
594 case CME_SIM_NOT_INSERTED:
b.liu9e8584b2024-11-06 19:21:28 +0800595 case CME_SIM_ABSENT: //sim fail,as "sim absent"
yq.wang586a0df2024-10-24 20:10:37 -0700596 ret = MBTK_SIM_ABSENT;
597 goto done;
598
599 default:
600 ret = MBTK_SIM_NOT_READY;
601 goto done;
602 }
liubin281ac462023-07-19 14:22:54 +0800603 ret = MBTK_SIM_NOT_READY;
604 goto done;
605 }
b.liu06559f62024-11-01 18:48:22 +0800606
liubin281ac462023-07-19 14:22:54 +0800607 /* CPIN? has succeeded, now look at the result */
608
609 cpinLine = p_response->p_intermediates->line;
610 err = at_tok_start (&cpinLine);
611
612 if (err < 0)
613 {
614 ret = MBTK_SIM_NOT_READY;
615 goto done;
616 }
617
618 err = at_tok_nextstr(&cpinLine, &cpinResult);
619
620 if (err < 0)
621 {
622 ret = MBTK_SIM_NOT_READY;
623 goto done;
624 }
625
626 if (0 == strcmp (cpinResult, "SIM PIN"))
627 {
628 ret = MBTK_SIM_PIN;
629 goto done;
630 }
631 else if (0 == strcmp (cpinResult, "SIM PUK"))
632 {
633 ret = MBTK_SIM_PUK;
634 goto done;
635 }
636 else if (0 == strcmp (cpinResult, "PH-NET PIN"))
637 {
638 return MBTK_SIM_NETWORK_PERSONALIZATION;
639 }
640 else if (0 != strcmp (cpinResult, "READY"))
641 {
642 /* we're treating unsupported lock types as "sim absent" */
643 ret = MBTK_SIM_ABSENT;
644 goto done;
645 }
646
647 at_response_free(p_response);
648 p_response = NULL;
649 cpinResult = NULL;
650
651 // ret = net_info.radio_state == MBTK_RADIO_STATE_ON ? MBTK_SIM_READY : MBTK_SIM_NOT_READY;
652 net_info.sim_state = MBTK_SIM_READY;
653 return MBTK_SIM_READY;
654done:
655 at_response_free(p_response);
656 net_info.sim_state = ret;
657 return ret;
658}
659
660void setRadioPower(int isOn)
661{
662 int err;
663 ATResponse *p_response = NULL;
664
665 LOGI("RadioPower - %s", isOn == 0 ? "OFF" : "ON");
666
667 if(isOn == 0 && net_info.radio_state == MBTK_RADIO_STATE_ON)
668 {
669 err = at_send_command("AT+CFUN=0", &p_response);
670 if (err || !p_response->success)
671 goto error;
672
673 net_info.radio_state = MBTK_RADIO_STATE_OFF;
674 }
675 else if(isOn && net_info.radio_state != MBTK_RADIO_STATE_ON)
676 {
677 err = at_send_command("AT+CFUN=1", &p_response);
678 if (err || !p_response->success)
679 {
680 if(isRadioOn() == 1)
681 {
682 net_info.radio_state = MBTK_RADIO_STATE_ON;
683 }
684 goto error;
685 }
686
687 net_info.radio_state = MBTK_RADIO_STATE_ON;
688 }
689
690 at_response_free(p_response);
691 return;
692error:
693 at_response_free(p_response);
694}
695
696static int apn_user_pass_set_by_cid(int cid, mbtk_apn_info_t *apn)
697{
698 char prop_name[20] = {0};
699 char prop_data[300] = {0};
700 sprintf(prop_name, "%s_%d",MBTK_APN_PROP,cid);
701 if(property_get(prop_name, prop_data, "") > 0 && !str_empty(prop_data)) {
702 char apn_name[128] = {0};
703 char *ptr_1 = prop_data;
704 mbtk_ip_type_enum ip_type = (mbtk_ip_type_enum)atoi(ptr_1);
705 ptr_1 = strstr(ptr_1, ",");
706 if(!ptr_1) {
707 return -1;
708 }
709 ptr_1++; // Jump ',' to apn
710
711 char *ptr_2 = strstr(ptr_1, ",");
712 if(!ptr_2) {
713 return -1;
714 }
715 memcpy(apn_name, ptr_1, ptr_2 - ptr_1); // apn
716
717 // Check ip_type and apn_name
b.liu9e8584b2024-11-06 19:21:28 +0800718 if(ip_type != apn->ip_type || strcmp(apn_name, (char*)apn->apn)) {
liubin281ac462023-07-19 14:22:54 +0800719 LOGD("APN Changed, not get user/pass/auth.");
720 return -1;
721 }
722
723 ptr_2++; // Jump ',' to user
724 ptr_1 = strstr(ptr_2, ",");
725 if(!ptr_1) {
726 return -1;
727 }
728 if(memcmp(ptr_2, "NULL", 4)) { // Not "NULL"
729 memcpy(apn->user, ptr_2, ptr_1 - ptr_2); // user
730 }
731
732 ptr_1++; // Jump ',' to pass
733 ptr_2 = strstr(ptr_1, ",");
734 if(!ptr_2) {
735 return -1;
736 }
737 if(memcmp(ptr_1, "NULL", 4)) { // Not "NULL"
738 memcpy(apn->pass, ptr_1, ptr_2 - ptr_1); // pass
739 }
740
741 ptr_2++; // Jump ',' to auth (Is last item)
742 if(memcmp(ptr_2, "NULL", 4)) { // Not "NULL"
743 memcpy(apn->auth, ptr_2, strlen(ptr_2)); // auth
744 }
745
746 return 0;
747 }
748 return -1;
749}
750
751
752/*
753AT+CPOL?
754*EUICC: 1
755
756OK
757*/
758static int req_plmn_get(mbtk_plmn_info *type, int *cme_err)
759{
760 ATResponse *response = NULL;
b.liu9e8584b2024-11-06 19:21:28 +0800761// char *tmp_ptr = NULL;
liubin281ac462023-07-19 14:22:54 +0800762 int err = at_send_command_multiline("AT+CPOL?", "+CPOL:", &response);
763
764 if (err < 0 || response->success == 0 || !response->p_intermediates){
765 *cme_err = at_get_cme_error(response);
766 goto exit;
767 }
768
769 int mccmnc_type = -1;
770 int count = -1;
771 char *mccmnc_name = NULL;
772 ATLine* lines_ptr = response->p_intermediates;
773 char *line = NULL;
774 while(lines_ptr)
775 {
776 line = lines_ptr->line;
777 //if(strStartsWith(line, "+CPOL:"))
778 {
779 err = at_tok_start(&line);
780 if (err < 0)
781 {
782 goto exit;
783 }
784 err = at_tok_nextint(&line, &count);
785 if (err < 0)
786 {
787 goto exit;
788 }
789 type->count = count;
790
791 err = at_tok_nextint(&line, &mccmnc_type);
792 if (err < 0)
793 {
794 goto exit;
795 }
796 type->mbtk_plmn_name[count-1].format = mccmnc_type;
797
798 err = at_tok_nextstr(&line, &mccmnc_name);
799 if (err < 0)
800 {
801 goto exit;
802 }
803 memcpy(type->mbtk_plmn_name[count-1].plmn_name, mccmnc_name, strlen(mccmnc_name));
804 mccmnc_name = NULL;
805 }
806 lines_ptr = lines_ptr->p_next;
807 }
808
809exit:
810 at_response_free(response);
811 return err;
812}
813
814
815/*
816AT*EUICC?
817*EUICC: 1
818
819OK
820*/
821static int req_sim_card_type_get(uint8 *type, int *cme_err)
822{
823 ATResponse *response = NULL;
b.liu9e8584b2024-11-06 19:21:28 +0800824// char *tmp_ptr = NULL;
liubin281ac462023-07-19 14:22:54 +0800825 int err = at_send_command_singleline("AT*EUICC?", "*EUICC:", &response);
826
827 if (err < 0 || response->success == 0 || !response->p_intermediates){
828 *cme_err = at_get_cme_error(response);
829 goto exit;
830 }
831
832 char *line = response->p_intermediates->line;
833 err = at_tok_start(&line);
834 if (err < 0)
835 {
836 goto exit;
837 }
838 int sim_card_type = -1;
839 err = at_tok_nextint(&line, &sim_card_type);
840 if (err < 0)
841 {
842 goto exit;
843 }
844 if(sim_card_type != -1)
845 *type = sim_card_type;
846 goto exit;
847exit:
848 at_response_free(response);
849 return err;
850}
851
852/*
853AT+EPIN?
854+EPIN: 3,0,10,0
855
856OK
857*/
858static int req_pin_puk_last_times_get(mbtk_pin_puk_last_times *times, int *cme_err)
859{
860 ATResponse *response = NULL;
b.liu9e8584b2024-11-06 19:21:28 +0800861// char *tmp_ptr = NULL;
862 int tmp_int;
liubin281ac462023-07-19 14:22:54 +0800863 int err = at_send_command_singleline("AT+EPIN?", "+EPIN:", &response);
864
865 if (err < 0 || response->success == 0 || !response->p_intermediates){
866 *cme_err = at_get_cme_error(response);
867 goto exit;
868 }
869
870 char *line = response->p_intermediates->line;
871 err = at_tok_start(&line);
872 if (err < 0)
873 {
874 goto exit;
875 }
876 mbtk_pin_puk_last_times last_times={0};
b.liu9e8584b2024-11-06 19:21:28 +0800877 err = at_tok_nextint(&line, &tmp_int);
liubin281ac462023-07-19 14:22:54 +0800878 if (err < 0)
879 {
880 goto exit;
881 }
b.liu9e8584b2024-11-06 19:21:28 +0800882 last_times.p1_retry = (uint8_t)tmp_int;
liubin281ac462023-07-19 14:22:54 +0800883 times->p1_retry = last_times.p1_retry;
b.liu9e8584b2024-11-06 19:21:28 +0800884 err = at_tok_nextint(&line, &tmp_int);
liubin281ac462023-07-19 14:22:54 +0800885 if (err < 0)
886 {
887 goto exit;
888 }
b.liu9e8584b2024-11-06 19:21:28 +0800889 last_times.p2_retry = (uint8_t)tmp_int;
liubin281ac462023-07-19 14:22:54 +0800890 times->p2_retry = last_times.p2_retry;
b.liu9e8584b2024-11-06 19:21:28 +0800891 err = at_tok_nextint(&line, &tmp_int);
liubin281ac462023-07-19 14:22:54 +0800892 if (err < 0)
893 {
894 goto exit;
895 }
b.liu9e8584b2024-11-06 19:21:28 +0800896 last_times.puk1_retry = (uint8_t)tmp_int;
liubin281ac462023-07-19 14:22:54 +0800897 times->puk1_retry = last_times.puk1_retry;
b.liu9e8584b2024-11-06 19:21:28 +0800898 err = at_tok_nextint(&line, &tmp_int);
liubin281ac462023-07-19 14:22:54 +0800899 if (err < 0)
900 {
901 goto exit;
902 }
b.liu9e8584b2024-11-06 19:21:28 +0800903 last_times.puk2_retry = (uint8_t)tmp_int;
liubin281ac462023-07-19 14:22:54 +0800904 times->puk2_retry = last_times.puk2_retry;
905
906exit:
907 at_response_free(response);
908 return err;
909}
910
911
912/*
913AT+CGSN
914864788050901201
915
916OK
917*/
918static int req_imei_get(void *data, int *cme_err)
919{
920 ATResponse *response = NULL;
921 int err = at_send_command_numeric("AT+CGSN", &response);
922
923 if (err < 0 || response->success == 0 || !response->p_intermediates) {
924 *cme_err = at_get_cme_error(response);
925 goto exit;
926 }
927
928 memcpy(data, response->p_intermediates->line, strlen(response->p_intermediates->line));
929exit:
930 at_response_free(response);
931 return err;
932}
933
934/*
935AT+MRD_SN=R
936+MRD_SN:0101,Thu Nov 12 00:00:00 2020,G4M32301020006
937
938OK
939
940*/
941static int req_sn_get(void *data, int *cme_err)
942{
943 ATResponse *response = NULL;
944 char *tmp_ptr = NULL;
945 int err = at_send_command_singleline("AT+MRD_SN=R", "+MRD_SN:", &response);
946
947 if (err < 0 || response->success == 0 || !response->p_intermediates){
948 *cme_err = at_get_cme_error(response);
949 goto exit;
950 }
951
952 char *line = response->p_intermediates->line;
953 err = at_tok_start(&line);
954 if (err < 0)
955 {
956 goto exit;
957 }
958
959 err = at_tok_nextstr(&line, &tmp_ptr);
960 if (err < 0)
961 {
962 goto exit;
963 }
964
965 err = at_tok_nextstr(&line, &tmp_ptr);
966 if (err < 0)
967 {
968 goto exit;
969 }
970
971 err = at_tok_nextstr(&line, &tmp_ptr);
972 if (err < 0)
973 {
974 goto exit;
975 }
976
977 memcpy(data, tmp_ptr, strlen(tmp_ptr));
978
979 goto exit;
980exit:
981 at_response_free(response);
982 return err;
983}
984
985/*
986AT+SYSTIME?
987+SYSTIME: 1
988
989OK
990
991*/
992static int req_time_get(int *data, int *cme_err)
993{
994 ATResponse *response = NULL;
995 int tmp_int;
996 int err = at_send_command_singleline("AT+SYSTIME?", "+SYSTIME:", &response);
997
998 if (err < 0 || response->success == 0 || !response->p_intermediates){
999 *cme_err = at_get_cme_error(response);
1000 goto exit;
1001 }
1002
1003 char *line = response->p_intermediates->line;
1004 err = at_tok_start(&line);
1005 if (err < 0)
1006 {
1007 goto exit;
1008 }
1009 err = at_tok_nextint(&line, &tmp_int);
1010 if (err < 0)
1011 {
1012 goto exit;
1013 }
1014 *data = tmp_int;
1015
1016exit:
1017 at_response_free(response);
1018 return err;
1019}
1020
1021/*
1022AT+CCLK?
1023+CCLK: "23/03/20,01:58:00+32"
1024
1025OK
1026
1027*/
1028static int req_net_time_get(char *data, int *cme_err)
1029{
1030 ATResponse *response = NULL;
1031 char *tmp_ptr = NULL;
1032 int err = at_send_command_singleline("AT+CCLK?", "+CCLK:", &response);
1033
1034 if (err < 0 || response->success == 0 || !response->p_intermediates){
1035 *cme_err = at_get_cme_error(response);
1036 goto exit;
1037 }
1038
1039 char *line = response->p_intermediates->line;
1040 err = at_tok_start(&line);
1041 if (err < 0)
1042 {
1043 goto exit;
1044 }
1045 err = at_tok_nextstr(&line, &tmp_ptr);
1046 if (err < 0)
1047 {
1048 goto exit;
1049 }
1050 memcpy(data, tmp_ptr, strlen(tmp_ptr));
1051
1052exit:
1053 at_response_free(response);
1054 return err;
1055}
1056
1057/*
1058AT+CFUN?
1059+CFUN: 1
1060OK
1061*/
1062static int req_modem_get(int *data, int *cme_err)
1063{
1064 ATResponse *response = NULL;
1065 int modem;
1066 int err = at_send_command_singleline("AT+CFUN?", "+CFUN:", &response);
1067
1068 if (err < 0 || response->success == 0 || !response->p_intermediates){
1069 *cme_err = at_get_cme_error(response);
1070 goto exit;
1071 }
1072
1073 char *line = response->p_intermediates->line;
1074 err = at_tok_start(&line);
1075 if (err < 0)
1076 {
1077 goto exit;
1078 }
1079 err = at_tok_nextint(&line, &modem);
1080 if (err < 0)
1081 {
1082 goto exit;
1083 }
1084 *data = modem;
1085
1086exit:
1087 at_response_free(response);
1088 return err;
1089}
1090
1091/*
1092AT+CFUN=<fun>[,<rst>]
1093OK
1094*/
1095static int req_modem_set(mbtk_modem_info_t* modem, int *cme_err)
1096{
1097 ATResponse *response = NULL;
1098 char cmd[30] = {0};
1099 int err = -1;
1100
1101 sprintf(cmd, "AT+CFUN=%d,%d", modem->fun, modem->rst);
1102 err = at_send_command(cmd, &response);
1103
1104 if (err < 0 || response->success == 0){
1105 *cme_err = at_get_cme_error(response);
1106 goto exit;
1107 }
1108/*
1109 ATResponse *response = NULL;
1110 int err = at_send_command_multiline(cmd, "", &response);
1111
1112 if (err < 0 || response->success == 0 || !response->p_intermediates){
1113 *cme_err = at_get_cme_error(response);
1114 goto exit;
1115 }
1116
1117 ATLine* lines_ptr = response->p_intermediates;
1118 char *line = NULL;
1119 while(lines_ptr)
1120 {
1121 line = lines_ptr->line;
1122 if(strStartsWith(line, "Revision"))
1123 {
1124 err = at_tok_start(&line);
1125 if (err < 0)
1126 {
1127 goto exit;
1128 }
1129 memcpy(data, line, strlen(line));
1130 break;
1131 }
1132 lines_ptr = lines_ptr->p_next;
1133 }
1134
1135 goto exit;
1136*/
1137exit:
1138 at_response_free(response);
1139 return err;
1140}
1141
1142
1143/*
1144AT+SYSTIME=0,"2022-01-25-11:15:30"
1145OK
1146
1147AT+SYSTIME=1
1148OK
1149*/
1150static int req_time_set(int type, char *time, int *cme_err)
1151{
1152 ATResponse *response = NULL;
b.liu9e8584b2024-11-06 19:21:28 +08001153// int tmp_int;
liubin281ac462023-07-19 14:22:54 +08001154 char cmd[200] = {0};
1155 if(str_empty(time)){
1156 sprintf(cmd, "AT+SYSTIME=%d", type);
1157 } else {
1158 sprintf(cmd, "AT+SYSTIME=%d,\"%s\"", type, time);
1159 }
1160 int err = at_send_command(cmd, &response);
1161
1162 if (err < 0 || response->success == 0){
1163 *cme_err = at_get_cme_error(response);
1164 goto exit;
1165 }
1166
1167exit:
1168 at_response_free(response);
1169 return err;
1170}
1171
1172
1173/*
1174ATI
1175Manufacturer:"LYNQ"
1176Model:"LYNQ_L508TLC"
1177Revision:L508TLCv02.01b01.00
1178IMEI:864788050901201
1179
1180OK
1181
1182*/
1183static int req_version_get(void *data, int *cme_err)
1184{
1185 ATResponse *response = NULL;
1186 int err = at_send_command_multiline("ATI", "", &response);
1187
1188 if (err < 0 || response->success == 0 || !response->p_intermediates){
1189 *cme_err = at_get_cme_error(response);
1190 goto exit;
1191 }
1192
1193 ATLine* lines_ptr = response->p_intermediates;
1194 char *line = NULL;
1195 while(lines_ptr)
1196 {
1197 line = lines_ptr->line;
1198 if(strStartsWith(line, "Revision"))
1199 {
1200 err = at_tok_start(&line);
1201 if (err < 0)
1202 {
1203 goto exit;
1204 }
1205 memcpy(data, line, strlen(line));
1206 break;
1207 }
1208 lines_ptr = lines_ptr->p_next;
1209 }
1210
1211 goto exit;
1212exit:
1213 at_response_free(response);
1214 return err;
1215}
1216
l.yang5b0ff422024-10-29 19:33:35 -07001217
1218static int req_md_version_get(void *data, int *cme_err)
1219{
1220 ATResponse *response = NULL;
1221 int err = at_send_command_multiline("AT*CGMR", "", &response);
1222
1223 if (err < 0 || response->success == 0 || !response->p_intermediates)
1224 {
1225 *cme_err = at_get_cme_error(response);
1226 goto exit;
1227 }
1228
1229 ATLine* lines_ptr = response->p_intermediates;
1230 char *line = NULL;
1231 while(lines_ptr)
1232 {
1233 line = lines_ptr->line;
1234 char *start = strstr(line, "FALCON_CP_SDK");
b.liu06559f62024-11-01 18:48:22 +08001235 if(start)
l.yang5b0ff422024-10-29 19:33:35 -07001236 {
1237 char *end = strstr(start, "_Linux");
b.liu06559f62024-11-01 18:48:22 +08001238 if(end)
l.yang5b0ff422024-10-29 19:33:35 -07001239 {
b.liu06559f62024-11-01 18:48:22 +08001240 end += strlen("_Linux");
l.yang5b0ff422024-10-29 19:33:35 -07001241 int length = end - start;
1242 if (length)
1243 {
1244 strncpy(data, start, length);
1245 ((char*)data)[length] = '\0';
1246 break;
b.liu06559f62024-11-01 18:48:22 +08001247 }
l.yang5b0ff422024-10-29 19:33:35 -07001248 else
1249 {
1250 err = -1;
1251 goto exit;
b.liu06559f62024-11-01 18:48:22 +08001252
l.yang5b0ff422024-10-29 19:33:35 -07001253 }
b.liu06559f62024-11-01 18:48:22 +08001254 }
1255 else
l.yang5b0ff422024-10-29 19:33:35 -07001256 {
1257 err = -1;
1258 goto exit;
1259 }
1260 }
1261 lines_ptr = lines_ptr->p_next;
1262 }
b.liu06559f62024-11-01 18:48:22 +08001263
l.yang5b0ff422024-10-29 19:33:35 -07001264 goto exit;
1265exit:
1266 at_response_free(response);
1267 return err;
1268}
1269
1270
1271
liubin281ac462023-07-19 14:22:54 +08001272/*
1273ATI
1274Manufacturer:"LYNQ"
1275Model:"LYNQ_L508TLC"
1276Revision:L508TLCv02.01b01.00
1277IMEI:864788050901201
1278
1279OK
1280
1281*/
1282static int req_model_get(void *data, int *cme_err)
1283{
1284 ATResponse *response = NULL;
1285 int err = at_send_command_multiline("ATI", "", &response);
1286
1287 if (err < 0 || response->success == 0 || !response->p_intermediates){
1288 *cme_err = at_get_cme_error(response);
1289 goto exit;
1290 }
1291
1292 ATLine* lines_ptr = response->p_intermediates;
1293 char *line = NULL;
1294 while(lines_ptr)
1295 {
1296 line = lines_ptr->line;
1297 if(strStartsWith(line, "Model"))
1298 {
1299 err = at_tok_start(&line);
1300 if (err < 0)
1301 {
1302 goto exit;
1303 }
1304 memcpy(data, line, strlen(line));
1305 break;
1306 }
1307 lines_ptr = lines_ptr->p_next;
1308 }
1309
1310 goto exit;
1311exit:
1312 at_response_free(response);
1313 return err;
1314}
1315
1316/*
1317AT+ACONFIG="IMSD=1"
1318or
1319AT+ACONFIG="IMSD=0"
1320
1321OK
1322*/
1323static int req_volte_set(int state, int *cme_err)
1324{
1325 ATResponse *response = NULL;
1326 char cmd[30] = {0};
1327 if(state)
1328 {
1329 strcpy(cmd, "AT+ACONFIG=\"IMSD=1\"");
1330 }
1331 else
1332 {
1333 strcpy(cmd, "AT+ACONFIG=\"IMSD=0\"");
1334 }
1335 int err = at_send_command(cmd, &response);
1336
1337 if (err < 0 || response->success == 0) {
1338 *cme_err = at_get_cme_error(response);
1339 goto exit;
1340 }
1341
1342 err = 0;
1343exit:
1344 at_response_free(response);
1345 return err;
1346}
1347
1348/*
1349AT+ACONFIG?
1350PID=0,VID=0,IMSD=1,PIPE=0,FAST=0,RDUP=1,NOCP=0,GEFL=-1237040617
1351
1352OK
1353*/
1354static int req_volte_get(int *state, int *cme_err)
1355{
1356 ATResponse *response = NULL;
b.liu9e8584b2024-11-06 19:21:28 +08001357// char *tmp_ptr = NULL;
liubin281ac462023-07-19 14:22:54 +08001358 int err = at_send_command_singleline("AT+ACONFIG?", "", &response);
1359
1360 if (err < 0 || response->success == 0 || !response->p_intermediates){
1361 *cme_err = at_get_cme_error(response);
1362 goto exit;
1363 }
1364
1365 char *line = response->p_intermediates->line;
1366 char* ptr = strstr(line, "IMSD=");
1367 if(ptr)
1368 {
1369 *state = atoi(ptr + strlen("IMSD="));
1370 }
1371 else
1372 {
1373 err = -1;
1374 }
1375exit:
1376 at_response_free(response);
1377 return err;
1378}
1379
1380
1381/*
1382* Get system temperature.
1383*
1384* type[IN]:
1385* 0: Soc temperature.
1386* 1: RF temperature.
1387* temp[OUT]:
1388* temperature in celsius.
1389*
1390
1391AT*RFTEMP
1392*RFTEMP:0,28
1393OK
1394
1395AT*SOCTEMP
1396*SOCTEMP:24000
1397OK
1398
1399*/
r.xiao2102d762024-06-07 03:10:38 -07001400static int req_temp_get(int type, mbtk_thermal_info_t *temp, int *cme_err)
liubin281ac462023-07-19 14:22:54 +08001401{
1402 ATResponse *response = NULL;
1403 int err = -1;
1404 int tmp_int;
1405 if(type == 0) { // Soc
1406 err = at_send_command_singleline("AT*SOCTEMP", "*SOCTEMP:", &response);
1407 } else { // RF
1408 err = at_send_command_singleline("AT*RFTEMP", "*RFTEMP:", &response);
1409 }
1410
1411 if (err < 0 || response->success == 0 || !response->p_intermediates){
1412 *cme_err = at_get_cme_error(response);
1413 goto exit;
1414 }
1415
1416 char *line = response->p_intermediates->line;
1417 err = at_tok_start(&line);
1418 if (err < 0)
1419 {
1420 goto exit;
1421 }
1422 err = at_tok_nextint(&line, &tmp_int);
1423 if (err < 0)
1424 {
1425 goto exit;
1426 }
1427
1428 if(type == 1) { // RF
1429 err = at_tok_nextint(&line, &tmp_int);
1430 if (err < 0)
1431 {
1432 goto exit;
1433 }
r.xiao2102d762024-06-07 03:10:38 -07001434 temp->ther = tmp_int;
liubin281ac462023-07-19 14:22:54 +08001435 } else {
r.xiao2102d762024-06-07 03:10:38 -07001436 tmp_int = tmp_int / 1000;
1437 temp->ther = tmp_int;
1438 //LOG(" >>>temp =%d",temp->ther);
liubin281ac462023-07-19 14:22:54 +08001439 }
1440
1441exit:
1442 at_response_free(response);
1443 return err;
1444}
1445
1446/*
1447AT*BAND=15
1448OK
1449
1450*/
1451static int req_band_set(mbtk_band_info_t* band, int *cme_err)
1452{
1453 ATResponse *response = NULL;
b.liudfec1e12024-06-06 16:38:59 +08001454 char cmd[100] = {0};
liubin281ac462023-07-19 14:22:54 +08001455 int err = -1;
1456
1457 if(band->gsm_band == 0 && band->umts_band == 0
1458 && band->tdlte_band == 0 && band->fddlte_band == 0) {
1459 sprintf(cmd, "AT*BAND=%d", band->net_pref);
1460 } else {
1461 band_support_get();
1462
1463 log_hex("BAND_SUPPORT", &band_support, sizeof(mbtk_band_info_t));
1464 log_hex("BAND", band, sizeof(mbtk_band_info_t));
1465
1466 if(band->gsm_band == 0) {
1467 band->gsm_band = band_support.gsm_band;
1468 }
1469 if(band->umts_band == 0) {
1470 band->umts_band = band_support.umts_band;
1471 }
1472 if(band->tdlte_band == 0) {
1473 band->tdlte_band = band_support.tdlte_band;
1474 }
1475 if(band->fddlte_band == 0) {
1476 band->fddlte_band = band_support.fddlte_band;
1477 }
1478
1479 if((band->gsm_band & band_support.gsm_band) != band->gsm_band) {
1480 LOG("GSM band error.");
1481 goto exit;
1482 }
1483
1484 if((band->umts_band & band_support.umts_band) != band->umts_band) {
1485 LOG("UMTS band error.");
1486 goto exit;
1487 }
1488
1489 if((band->tdlte_band & band_support.tdlte_band) != band->tdlte_band) {
1490 LOG("TDLTE band error.");
1491 goto exit;
1492 }
1493
1494 if((band->fddlte_band & band_support.fddlte_band) != band->fddlte_band) {
1495 LOG("FDDLTE band error.");
1496 goto exit;
1497 }
1498
b.liu288093c2024-05-09 17:02:57 +08001499 if((band->lte_ext_band & band_support.lte_ext_band) != band->lte_ext_band) {
1500 LOG("EXT_LTE band error.");
1501 goto exit;
1502 }
1503
liubin281ac462023-07-19 14:22:54 +08001504 if(band->net_pref == 0xFF) { // No change net_pref.
1505 int tmp_int;
1506 err = at_send_command_singleline("AT*BAND?", "*BAND:", &response);
1507 if (err < 0 || response->success == 0 || !response->p_intermediates){
1508 *cme_err = at_get_cme_error(response);
1509 goto exit;
1510 }
1511
1512 char *line = response->p_intermediates->line;
1513 err = at_tok_start(&line);
1514 if (err < 0)
1515 {
1516 goto exit;
1517 }
1518
1519 err = at_tok_nextint(&line, &tmp_int);
1520 if (err < 0)
1521 {
1522 goto exit;
1523 }
1524 band->net_pref = (uint8)tmp_int; // Set to current net_pref.
1525
1526 at_response_free(response);
1527 }
1528
b.liu288093c2024-05-09 17:02:57 +08001529 if(band->lte_ext_band > 0) {
1530 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);
1531 } else {
1532 sprintf(cmd, "AT*BAND=%d,%d,%d,%d,%d", band->net_pref, band->gsm_band, band->umts_band, band->tdlte_band, band->fddlte_band);
1533 }
liubin281ac462023-07-19 14:22:54 +08001534 }
1535 err = at_send_command(cmd, &response);
1536
1537 if (err < 0 || response->success == 0){
1538 *cme_err = at_get_cme_error(response);
1539 goto exit;
1540 }
1541
1542 err = 0;
1543exit:
1544 at_response_free(response);
1545 return err;
1546}
1547
1548/*
1549// ???????
1550AT*BAND=?
1551*BAND:(0-18),79,147,482,524503
1552
1553OK
1554
1555// ???????????
1556AT*BAND?
1557*BAND: 15, 78, 147, 482, 524503, 0, 2, 2, 0, 0
1558
1559OK
1560
1561// ?????????
1562AT*BAND=5,79,147,128,1
1563OK
1564
1565net_prefferred??
1566 0 : GSM only
1567 1 : UMTS only
1568 2 : GSM/UMTS(auto)
1569 3 : GSM/UMTS(GSM preferred)
1570 4 : GSM/UMTS(UMTS preferred)
1571 5 : LTE only
1572 6 : GSM/LTE(auto)
1573 7 : GSM/LTE(GSM preferred)
1574 8 : GSM/LTE(LTE preferred)
1575 9 : UMTS/LTE(auto)
1576 10 : UMTS/LTE(UMTS preferred)
1577 11 : UMTS/LTE(LTE preferred)
1578 12 : GSM/UMTS/LTE(auto)
1579 13 : GSM/UMTS/LTE(GSM preferred)
1580 14 : GSM/UMTS/LTE(UMTS preferred)
1581 15 : GSM/UMTS/LTE(LTE preferred)
1582GSM band??
1583 1 ?C PGSM 900 (standard or primary)
1584 2 ?C DCS GSM 1800
1585 4 ?C PCS GSM 1900
1586 8 ?C EGSM 900 (extended)
1587 16 ?C GSM 450
1588 32 ?C GSM 480
1589 64 ?C GSM 850
1590 512 - BAND_LOCK_BIT // used for GSM band setting
1591UMTS band??
1592 1 ?C UMTS_BAND_1
1593 2 ?C UMTS_BAND_2
1594 4 ?C UMTS_BAND_3
1595 8 ?C UMTS_BAND_4
1596 16 ?C UMTS_BAND_5
1597 32 ?C UMTS_BAND_6
1598 64 ?C UMTS_BAND_7
1599 128 ?C UMTS_BAND_8
1600 256 ?C UMTS_BAND_9
1601LTEbandH(TDD-LTE band)
1602 32 ?C TDLTE_BAND_38
1603 64 ?C TDLTE_BAND_39
1604 128 ?C TDLTE_BAND_40
1605 256 ?C TDLTE_BAND_41
1606LTEbandL(FDD-LTE band)
1607 1 ?C FDDLTE_BAND_1
1608 4 ?C FDDLTE _BAND_3
1609 8 ?C FDDLTE _BAND_4
1610 64 ?C FDDLTE _BAND_7
1611 65536 ?C FDDLTE _BAND_17
1612 524288 ?C FDDLTE _BAND_20
1613*/
1614static int req_band_get(mbtk_band_info_t *band, int *cme_err)
1615{
1616 ATResponse *response = NULL;
1617 int tmp_int;
1618
1619 band_support_get();
1620
1621 log_hex("BAND_SUPPORT", &band_support, sizeof(mbtk_band_info_t));
1622 int err = at_send_command_singleline("AT*BAND?", "*BAND:", &response);
1623 if (err < 0 || response->success == 0 || !response->p_intermediates){
1624 *cme_err = at_get_cme_error(response);
1625 goto exit;
1626 }
1627
1628 char *line = response->p_intermediates->line;
1629 err = at_tok_start(&line);
1630 if (err < 0)
1631 {
1632 goto exit;
1633 }
1634
1635 err = at_tok_nextint(&line, &tmp_int);
1636 if (err < 0)
1637 {
1638 goto exit;
1639 }
1640 band->net_pref = (uint8)tmp_int;
1641
1642 err = at_tok_nextint(&line, &tmp_int);
1643 if (err < 0)
1644 {
1645 goto exit;
1646 }
1647 band->gsm_band = (uint16)tmp_int;
1648
1649 err = at_tok_nextint(&line, &tmp_int);
1650 if (err < 0)
1651 {
1652 goto exit;
1653 }
1654 band->umts_band = (uint16)tmp_int;
1655
1656 err = at_tok_nextint(&line, &tmp_int);
1657 if (err < 0)
1658 {
1659 goto exit;
1660 }
1661 band->tdlte_band = (uint32)tmp_int;
1662
1663 err = at_tok_nextint(&line, &tmp_int);
1664 if (err < 0)
1665 {
1666 goto exit;
1667 }
1668 band->fddlte_band = (uint32)tmp_int;
1669
b.liu288093c2024-05-09 17:02:57 +08001670 // roamingConfig
1671 err = at_tok_nextint(&line, &tmp_int);
1672 if (err < 0)
1673 {
1674 goto exit;
1675 }
1676
1677 // srvDomain
1678 err = at_tok_nextint(&line, &tmp_int);
1679 if (err < 0)
1680 {
1681 goto exit;
1682 }
1683
1684 // bandPriorityFlag
1685 err = at_tok_nextint(&line, &tmp_int);
1686 if (err < 0)
1687 {
1688 goto exit;
1689 }
1690
1691 //
1692 err = at_tok_nextint(&line, &tmp_int);
1693 if (err < 0)
1694 {
1695 goto exit;
1696 }
1697
1698 // ltebandExt
1699 err = at_tok_nextint(&line, &tmp_int);
1700 if (err < 0)
1701 {
1702 goto exit;
1703 }
1704 band->lte_ext_band = (uint32)tmp_int;
1705
liubin281ac462023-07-19 14:22:54 +08001706 log_hex("BAND", band, sizeof(mbtk_band_info_t));
1707
1708exit:
1709 at_response_free(response);
1710 return err;
1711}
1712
1713/*
1714AT+ICCID
1715+ICCID: 89860621330065648041
1716
1717OK
1718*/
1719static int req_iccid_get(void *data, int *cme_err)
1720{
1721 ATResponse *response = NULL;
1722 char *tmp_ptr = NULL;
1723 int err = at_send_command_singleline("AT+ICCID", "+ICCID:", &response);
1724
1725 if (err < 0 || response->success == 0 || !response->p_intermediates){
1726 *cme_err = at_get_cme_error(response);
1727 goto exit;
1728 }
1729
1730 char *line = response->p_intermediates->line;
1731 err = at_tok_start(&line);
1732 if (err < 0)
1733 {
1734 goto exit;
1735 }
1736
1737 err = at_tok_nextstr(&line, &tmp_ptr);
1738 if (err < 0)
1739 {
1740 goto exit;
1741 }
1742
1743 memcpy(data, tmp_ptr, strlen(tmp_ptr));
1744exit:
1745 at_response_free(response);
1746 return err;
1747}
1748
1749/*
1750AT+CNUM?
1751+CNUM: "","13980414101",129
1752
1753OK
1754
1755*/
1756static int req_phone_number_get(void *data, int *cme_err)
1757{
1758 ATResponse *response = NULL;
1759 char *tmp_ptr = NULL;
1760 int err = at_send_command_singleline("AT+CNUM?", "+CNUM:", &response);
1761 if (err < 0 || response == NULL || response->success == 0 || !response->p_intermediates){
1762 if(response) {
1763 *cme_err = at_get_cme_error(response);
1764 }
1765 LOGD("AT+CNUM? fail.");
1766 goto exit;
1767 }
1768
1769 char *line = response->p_intermediates->line;
1770 if(line == NULL) {
1771 LOGD("line is NULL");
1772 goto exit;
1773 }
1774 err = at_tok_start(&line);
1775 if (err < 0)
1776 {
1777 goto exit;
1778 }
1779
1780 err = at_tok_nextstr(&line, &tmp_ptr);
1781 if (err < 0)
1782 {
1783 goto exit;
1784 }
1785
1786 err = at_tok_nextstr(&line, &tmp_ptr);
1787 if (err < 0)
1788 {
1789 goto exit;
1790 }
1791
1792 memcpy(data, tmp_ptr, strlen(tmp_ptr));
1793exit:
1794 at_response_free(response);
1795 return err;
1796}
1797
1798
1799/*
1800AT+CIMI
1801460068103383304
1802
1803OK
1804
1805*/
1806static int req_imsi_get(void *data, int *cme_err)
1807{
1808 ATResponse *response = NULL;
1809 int err = at_send_command_numeric("AT+CIMI", &response);
1810
1811 if (err < 0 || response->success == 0 || !response->p_intermediates){
1812 *cme_err = at_get_cme_error(response);
1813 goto exit;
1814 }
1815
1816 memcpy(data, response->p_intermediates->line, strlen(response->p_intermediates->line));
1817exit:
1818 at_response_free(response);
1819 return err;
1820}
1821
1822
1823/*
1824AT+CLCK=SC,1/0,1234
1825+CLCK:1/0
1826
1827OK
1828
1829*/
1830static int req_pin_enable(mbtk_enable_pin_info *data, int *cme_err)
1831{
1832 ATResponse *response = NULL;
1833 char cmd[64]={0};
1834 sprintf(cmd, "AT+CLCK=SC,%d,%s", data->enable, data->pin_value);
1835
1836 int err = at_send_command_singleline(cmd, "+CLCK:", &response);
1837 if (err < 0 || response == NULL || response->success == 0 || !response->p_intermediates){
1838 if(response) {
1839 *cme_err = at_get_cme_error(response);
1840 }
1841 LOGD("AT+CLCK? fail.");
1842 goto exit;
1843 }
1844
1845 char *line = response->p_intermediates->line;
1846 if(line == NULL) {
1847 LOGD("line is NULL");
1848 goto exit;
1849 }
1850 err = at_tok_start(&line);
1851 if (err < 0)
1852 {
1853 goto exit;
1854 }
1855 int clck;
1856 err = at_tok_nextint(&line, &clck);
1857 if (err < 0)
1858 {
1859 goto exit;
1860 }
1861
1862exit:
1863 at_response_free(response);
1864 return err;
1865}
1866
1867/*
yq.wang586a0df2024-10-24 20:10:37 -07001868AT+CLCK=SC,2
1869+CLCK: 1
1870
1871OK
1872*/
b.liu9e8584b2024-11-06 19:21:28 +08001873static int req_get_pin_state(mbtk_pin_state_enum *state, int *cme_err)
yq.wang586a0df2024-10-24 20:10:37 -07001874{
1875 ATResponse *response = NULL;
b.liu9e8584b2024-11-06 19:21:28 +08001876// char cmd[64]={0};
b.liu06559f62024-11-01 18:48:22 +08001877
yq.wang586a0df2024-10-24 20:10:37 -07001878 int err = at_send_command_singleline("AT+CLCK=SC,2", "+CLCK:", &response);
1879 if (err < 0 || response == NULL || response->success == 0 || !response->p_intermediates)
1880 {
1881 if(response)
1882 {
1883 *cme_err = at_get_cme_error(response);
1884 }
1885 LOGE("[req_get_pin_state] AT+CLCK fail.");
1886 goto exit;
1887 }
1888
1889 char *line = response->p_intermediates->line;
1890 if(line == NULL)
1891 {
1892 LOGE("[req_get_pin_state] line is NULL");
1893 goto exit;
1894 }
1895 err = at_tok_start(&line);
1896 if (err < 0)
1897 {
1898 LOGE("[req_get_pin_state] at_tok_start fail.[%d]", err);
1899 goto exit;
1900 }
b.liu06559f62024-11-01 18:48:22 +08001901
yq.wang586a0df2024-10-24 20:10:37 -07001902 int clck;
1903 err = at_tok_nextint(&line, &clck);
1904 if (err < 0)
1905 {
1906 LOGE("[req_get_pin_state] at_tok_nextint fail.[%d]", err);
1907 goto exit;
1908 }
1909
1910 if(clck == 1)
1911 {
b.liu9e8584b2024-11-06 19:21:28 +08001912 *state = MBTK_PIN_ENABLE;
yq.wang586a0df2024-10-24 20:10:37 -07001913 }
1914 else
1915 {
b.liu9e8584b2024-11-06 19:21:28 +08001916 *state = MBTK_PIN_DISABLE;
yq.wang586a0df2024-10-24 20:10:37 -07001917 }
1918exit:
1919 at_response_free(response);
1920 response = NULL;
1921 return err;
1922}
1923
1924
1925/*
liubin281ac462023-07-19 14:22:54 +08001926AT+CPIN=1234
1927
1928OK
1929
1930*/
1931static int req_pin_verify(char *data, int *cme_err)
1932{
1933 ATResponse *response = NULL;
1934 char cmd[64]={0};
1935 sprintf(cmd, "AT+CPIN=%s", data);
1936 int err = at_send_command(cmd, &response);
1937 if (err < 0 || response->success == 0){
1938 if(cme_err) {
1939 *cme_err = at_get_cme_error(response);
1940 }
1941 goto exit;
1942 }
1943
1944exit:
1945 at_response_free(response);
1946 return err;
1947}
1948
1949/*
1950AT+CLCK=SC,2
1951+CLCK: 1
1952
1953OK
1954
1955AT+CLCK="SC",1,"1234"
1956+CLCK:1
1957
1958OK
1959
1960AT+CPWD="SC","1234","4321"
1961
1962OK
1963
1964*/
1965static int req_pin_change(mbtk_change_pin_info *data, int *cme_err)
1966{
1967 ATResponse *response = NULL;
1968 char cmd[64]={0};
1969
1970 int err = at_send_command_singleline("AT+CLCK=SC,2", "+CLCK:", &response);
1971 if (err < 0 || response == NULL || response->success == 0 || !response->p_intermediates){
1972 if(response) {
1973 *cme_err = at_get_cme_error(response);
1974 }
1975 LOGD("AT+CLCK fail.");
1976 goto exit;
1977 }
1978
1979 char *line = response->p_intermediates->line;
1980 if(line == NULL) {
1981 LOGD("line is NULL");
1982 goto exit;
1983 }
1984 err = at_tok_start(&line);
1985 if (err < 0)
1986 {
1987 goto exit;
1988 }
1989 int clck;
1990 err = at_tok_nextint(&line, &clck);
1991 if (err < 0)
1992 {
1993 goto exit;
1994 }
1995 at_response_free(response);
1996
1997 if(clck==0)
1998 {
1999 sprintf(cmd, "AT+CLCK=SC,1,%s", data->old_pin_value);
2000 err = at_send_command_singleline(cmd, "+CLCK:", &response);
2001 if (err < 0 || response == NULL || response->success == 0 || !response->p_intermediates){
2002 if(response) {
2003 *cme_err = at_get_cme_error(response);
2004 }
2005 LOGD("AT+CLCK fail.");
2006 goto exit;
2007 }
2008 line = response->p_intermediates->line;
2009 if(line == NULL) {
2010 LOGD("line is NULL");
2011 goto exit;
2012 }
2013 err = at_tok_start(&line);
2014 if (err < 0)
2015 {
2016 goto exit;
2017 }
2018 clck = -1;
2019 err = at_tok_nextint(&line, &clck);
2020 if (err < 0)
2021 {
2022 goto exit;
2023 }
2024 at_response_free(response);
2025 if(clck != 1)
2026 return err;
2027 }
2028 memset(cmd, 0, 64);
2029 sprintf(cmd, "AT+CPWD=SC,%s,%s", data->old_pin_value,data->new_pin_value);
2030 err = at_send_command(cmd, &response);
2031 if (err < 0 || response->success == 0){
2032 if(cme_err) {
2033 *cme_err = at_get_cme_error(response);
2034 }
2035 goto exit;
2036 }
2037
2038exit:
2039 at_response_free(response);
2040 return err;
2041}
2042
2043/*
2044AT+CPIN?
2045+CPIN:SIM PUK
2046
2047OK
2048
2049AT+CPIN="PUK","PIN"
2050+CPIN: READY
2051
2052OK
2053*/
2054static int req_puk_unlock_pin(mbtk_unlock_pin_info *data, int *cme_err)
2055{
2056 ATResponse *response = NULL;
2057 char cmd[64]={0};
2058#if 0
2059 int err = at_send_command_singleline("AT+CPIN?", "+CPIN:", &response);
2060 if (err < 0 || response == NULL || response->success == 0 || !response->p_intermediates){
2061 if(response) {
2062 *cme_err = at_get_cme_error(response);
2063 }
2064 LOGD("AT+CNUM? fail.");
2065 goto exit;
2066 }
2067
2068 char *line = response->p_intermediates->line;
2069 if(line == NULL) {
2070 LOGD("line is NULL");
2071 goto exit;
2072 }
2073 err = at_tok_start(&line);
2074 if (err < 0)
2075 {
2076 goto exit;
2077 }
2078 char *tmp_ptr = NULL;
2079 err = at_tok_nextstr(&line, &tmp_ptr);
2080 if (err < 0)
2081 {
2082 goto exit;
2083 }
2084 at_response_free(response);
2085
2086 if(!strstr(tmp_ptr,"SIM PUK"))
2087 {
2088 sprintf(cmd, "AT+CPIN=%s,%s", data->puk_value, data->pin_value);
2089 err = at_send_command_singleline(cmd, "+CPIN:", &response);
2090 if (err < 0 || response == NULL || response->success == 0 || !response->p_intermediates){
2091 if(response) {
2092 *cme_err = at_get_cme_error(response);
2093 }
2094 LOGD("AT+CNUM? fail.");
2095 goto exit;
2096 }
2097 line = response->p_intermediates->line;
2098 if(line == NULL) {
2099 LOGD("line is NULL");
2100 goto exit;
2101 }
2102 err = at_tok_start(&line);
2103 if (err < 0)
2104 {
2105 goto exit;
2106 }
2107 memset(tmp_ptr, 0, strlen(tmp_ptr));
2108 err = at_tok_nextstr(&line, &tmp_ptr);
2109 if (err < 0)
2110 {
2111 goto exit;
2112 }
2113 at_response_free(response);
2114 if(strstr(tmp_ptr, "READY"))
2115 return err;
2116 }
2117 else
2118 return err;
2119#else
2120 sprintf(cmd, "AT+CPIN=%s,%s", data->puk_value, data->pin_value);
r.xiao2ad43d12024-03-23 00:03:29 -07002121 int err = at_send_command(cmd, &response);
2122 if (err < 0 || response->success == 0){
2123 if(cme_err) {
liubin281ac462023-07-19 14:22:54 +08002124 *cme_err = at_get_cme_error(response);
2125 }
liubin281ac462023-07-19 14:22:54 +08002126 goto exit;
2127 }
r.xiao2ad43d12024-03-23 00:03:29 -07002128
liubin281ac462023-07-19 14:22:54 +08002129#endif
2130exit:
2131 at_response_free(response);
2132 return err;
2133}
2134
2135/*
2136AT+COPS=?
2137
2138+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)
2139
2140OK
2141
2142// Return [sel_mode(uint8)type(uint8)plmn(uint32)...sel_mode(uint8)type(uint8)plmn(uint32)]
2143*/
2144#if 0
2145static int req_available_net_get(mbtk_net_array_info_t *data_ptr)
2146{
2147 ATResponse *response = NULL;
2148 char *tmp_ptr = NULL;
2149 int tmp_int;
2150 int err = at_send_command_singleline("AT+COPS=?", "+COPS:", &response);
2151
2152 if (err < 0 || response->success == 0 || !response->p_intermediates)
2153 goto exit;
2154#if 1
2155 char *line_ptr = response->p_intermediates->line;
2156 if(line_ptr == NULL) {
2157 LOG("line is NULL");
2158 goto exit;
2159 }
2160 //LOG("Line:%s",line_ptr);
2161 line_ptr = strstr(line_ptr, "(");
2162 while(line_ptr) {
2163 line_ptr++;
2164 // Only for available/current net.
2165 if(*line_ptr == '1' || *line_ptr == '2') {
2166 //LOG("Temp:%s",line_ptr);
2167 //sleep(1);
2168 line_ptr = strstr(line_ptr, ",");
2169 if(line_ptr == NULL)
2170 goto exit;
2171 line_ptr++;
2172
2173 line_ptr = strstr(line_ptr, ",");
2174 if(line_ptr == NULL)
2175 goto exit;
2176 line_ptr++;
2177
2178 line_ptr = strstr(line_ptr, ",");
2179 if(line_ptr == NULL)
2180 goto exit;
2181
2182 while(*line_ptr != '\0' && (*line_ptr == ',' || *line_ptr == ' ' || *line_ptr == '"'))
2183 line_ptr++;
2184
2185 mbtk_net_info_t *net = (mbtk_net_info_t*)malloc(sizeof(mbtk_net_info_t));
2186 if(net == NULL) {
2187 LOG("malloc() fail.");
2188 goto exit;
2189 }
2190 memset(net, 0, sizeof(mbtk_net_info_t));
2191
2192 // Point to "46000"
2193 //LOG("PLMN:%s",line_ptr);
2194 //sleep(1);
2195 net->plmn = (uint32)atoi(line_ptr);
2196
2197 line_ptr = strstr(line_ptr, ",");
2198 if(line_ptr == NULL)
2199 goto exit;
2200
2201 while(*line_ptr != '\0' && (*line_ptr == ',' || *line_ptr == ' '))
2202 line_ptr++;
2203
2204 // Point to "7"
2205 if(*line_ptr == '\0') {
2206 free(net);
2207 goto exit;
2208 }
2209 //LOG("Type:%s",line_ptr);
2210 //sleep(1);
2211 net->net_type = (uint8)atoi(line_ptr);
2212 list_add(data_ptr->net_list, net);
2213 data_ptr->count++;
2214 }
2215
2216 line_ptr = strstr(line_ptr, "(");
2217 }
2218#endif
2219exit:
2220 at_response_free(response);
2221 return err;
2222}
2223#else
2224static int req_available_net_get(void* buff, int *cme_err)
2225{
2226 ATResponse *response = NULL;
b.liu9e8584b2024-11-06 19:21:28 +08002227// char *tmp_ptr = NULL;
2228// int tmp_int;
liubin281ac462023-07-19 14:22:54 +08002229 int buff_size = 0;
2230 int err = at_send_command_singleline("AT+COPS=?", "+COPS:", &response);
2231
2232 if (err < 0 || response->success == 0 || !response->p_intermediates){
2233 *cme_err = at_get_cme_error(response);
2234 goto exit;
2235 }
2236 char *line_ptr = response->p_intermediates->line;
2237 if(line_ptr == NULL) {
2238 LOG("line is NULL");
2239 goto exit;
2240 }
2241 uint8* buff_ptr = (uint8*)buff;
2242 //LOG("Line:%s",line_ptr);
2243 line_ptr = strstr(line_ptr, "(");
2244 while(line_ptr) {
2245 line_ptr++;
2246 // Only for available/current net.
2247 if(*line_ptr == '1' || *line_ptr == '2' || *line_ptr == '3') {
2248 *(buff_ptr + 2) = (uint8)atoi(line_ptr); // net_state
2249
2250 line_ptr = strstr(line_ptr, ",");
2251 if(line_ptr == NULL)
2252 goto exit;
2253 line_ptr++;
2254
2255 line_ptr = strstr(line_ptr, ",");
2256 if(line_ptr == NULL)
2257 goto exit;
2258 line_ptr++;
2259
2260 line_ptr = strstr(line_ptr, ",");
2261 if(line_ptr == NULL)
2262 goto exit;
2263
2264 while(*line_ptr != '\0' && (*line_ptr == ',' || *line_ptr == ' ' || *line_ptr == '"'))
2265 line_ptr++;
2266
2267 // set sel_mode to 0
2268 *buff_ptr = (uint8)0;
2269 // Point to "46000"
2270 //LOG("PLMN:%s",line_ptr);
2271 //sleep(1);
2272 uint32_2_byte((uint32)atoi(line_ptr), buff_ptr + 3, false); // plmn
2273
2274 line_ptr = strstr(line_ptr, ",");
2275 if(line_ptr == NULL)
2276 goto exit;
2277
2278 while(*line_ptr != '\0' && (*line_ptr == ',' || *line_ptr == ' '))
2279 line_ptr++;
2280
2281 // Point to "7"
2282 if(*line_ptr == '\0') {
2283 goto exit;
2284 }
2285 //LOG("Type:%s",line_ptr);
2286 //sleep(1);
2287 *(buff_ptr + 1) = (uint8)atoi(line_ptr); // net_type
2288
2289 buff_size += sizeof(mbtk_net_info_t);
2290 buff_ptr += sizeof(mbtk_net_info_t);
2291 }
2292
2293 line_ptr = strstr(line_ptr, "(");
2294 }
2295exit:
2296 at_response_free(response);
2297 return buff_size;
2298}
2299#endif
2300
2301/*
2302AT+COPS?
2303+COPS: 1
2304
2305OK
2306
2307or
2308
2309AT+COPS?
2310+COPS: 0,2,"46001",7
2311
2312OK
2313
2314*/
2315static int req_net_sel_mode_get(mbtk_net_info_t *net, int *cme_err)
2316{
2317 //LOG("req_net_sel_mode_get() 0");
2318 //sleep(1);
2319 ATResponse *response = NULL;
2320 int tmp_int;
2321 char *tmp_ptr = NULL;
2322 int err = at_send_command_singleline("AT+COPS?", "+COPS:", &response);
2323 //LOG("req_net_sel_mode_get() 00");
2324 //sleep(1);
2325 if (err < 0 || response->success == 0 || !response->p_intermediates){
2326 if(cme_err != NULL)
2327 *cme_err = at_get_cme_error(response);
2328 err = -1;
2329 goto exit;
2330 }
2331 //LOG("req_net_sel_mode_get() 1");
2332 //sleep(1);
2333 char *line = response->p_intermediates->line;
2334 if(line == NULL) {
2335 LOG("line is NULL");
2336 goto exit;
2337 }
2338 //LOG("req_net_sel_mode_get() 2");
2339 //sleep(1);
2340 err = at_tok_start(&line);
2341 if (err < 0)
2342 {
2343 goto exit;
2344 }
2345 //LOG("req_net_sel_mode_get() 3");
2346 //sleep(1);
2347 err = at_tok_nextint(&line, &tmp_int);
2348 if (err < 0)
2349 {
2350 goto exit;
2351 }
2352 net->net_sel_mode = (uint8)tmp_int;
2353 //LOG("req_net_sel_mode_get() 4");
2354 //sleep(1);
2355 // +COPS: 1
2356 if(!at_tok_hasmore(&line)) {
2357 goto exit;
2358 }
2359 //LOG("req_net_sel_mode_get() 5");
2360 //sleep(1);
2361 err = at_tok_nextint(&line, &tmp_int);
2362 if (err < 0)
2363 {
2364 goto exit;
2365 }
2366 //LOG("req_net_sel_mode_get() 6");
2367 //sleep(1);
2368 err = at_tok_nextstr(&line, &tmp_ptr);
2369 if (err < 0)
2370 {
2371 goto exit;
2372 }
2373 // memcpy(net->plmn, tmp_ptr, strlen(tmp_ptr));
2374 net->plmn = (uint32)atoi(tmp_ptr);
2375 //LOG("req_net_sel_mode_get() 7");
2376 //sleep(1);
2377 err = at_tok_nextint(&line, &tmp_int);
2378 if (err < 0)
2379 {
2380 goto exit;
2381 }
2382 net->net_type = (uint8)tmp_int;
2383
2384 net->net_state = (uint8)MBTK_NET_AVIL_STATE_CURRENT;
2385
2386exit:
2387 //LOG("req_net_sel_mode_get() 8");
2388 //sleep(1);
2389 at_response_free(response);
2390 return err;
2391}
2392
2393/*
2394AT+COPS=0
2395or
2396AT+COPS=1,2,"46000",7
2397
2398OK
2399
2400*/
2401static int req_net_sel_mode_set(mbtk_net_info_t* net, int *cme_err)
2402{
2403 ATResponse *response = NULL;
2404 char cmd[50] = {0};
2405 char* cmp_ptr = cmd;
2406 if(net == NULL) {
2407 cmp_ptr += sprintf(cmp_ptr, "AT+COPS=0");
2408 } else {
2409 if(net->net_sel_mode == 0) {
2410 cmp_ptr += sprintf(cmp_ptr, "AT+COPS=0");
2411 } else if(net->net_type == 0xFF) {
2412 cmp_ptr += sprintf(cmp_ptr, "AT+COPS=1,2,\"%d\"",net->plmn);
2413 } else {
2414 cmp_ptr += sprintf(cmp_ptr, "AT+COPS=1,2,\"%d\",%d",net->plmn, net->net_type);
2415 }
2416 }
2417
2418 int err = at_send_command(cmd, &response);
2419
2420 if (err < 0 || response->success == 0) {
2421 *cme_err = at_get_cme_error(response);
2422 goto exit;
2423 }
2424
2425exit:
2426 at_response_free(response);
2427 return err;
2428}
2429
2430/*
2431AT+EEMOPT=1
2432OK
2433
2434// LTE
2435AT+EEMGINFO?
2436// <mcc>, <length of mnc>, <mnc>, <tac>, <PCI>, <dlEuarfcn>, < ulEuarfcn >, <band>, <dlBandwidth>,
2437// <rsrp>,<rsrq>, <sinr>,
2438// errcModeState,emmState,serviceState,IsSingleEmmRejectCause,EMMRejectCause,mmeGroupId,mmeCode,mTmsi,
2439// cellId,subFrameAssignType,specialSubframePatterns,transMode
2440// mainRsrp,diversityRsrp,mainRsrq,diversityRsrq,rssi,cqi,pathLoss,tb0DlTpt,tb1DlTpt,tb0DlPeakTpt,tb1DlPeakTpt,tb0UlPeakTpt,
2441// tb1UlPeakTpt,dlThroughPut,dlPeakThroughPut,averDlPRB,averCQITb0,averCQITb1,rankIndex,grantTotal,ulThroughPut,ulPeakThroughPut,currPuschTxPower,averUlPRB,
2442// dlBer, ulBer,
2443// diversitySinr, diversityRssi
2444+EEMLTESVC: 1120, 2, 0, 33584, 430, 40936, 40936, 41, 20,
24450, 0, 0,
24461, 10, 0, 1, 0, 1059, 78, 3959566565,
2447105149248, 2, 7, 7,
24480, 0, 0, 0, 0, 0, 0, 1190919, 0, 0, 0, 16779777,
24490, 5112867, 3959566565, 2, 0, 0, 0, 0, 0, 0, 0, 0,
24500, 0,
24517, 44
2452
2453// index,phyCellId,euArfcn,rsrp,rsrq
2454+EEMLTEINTER: 0, 65535, 38950, 0, 0
2455
2456+EEMLTEINTER: 1, 0, 0, 0, 0
2457
2458+EEMLTEINTER: 2, 0, 4294967295, 255, 255
2459
2460+EEMLTEINTER: 3, 65535, 1300, 0, 0
2461
2462+EEMLTEINTER: 4, 0, 0, 0, 0
2463
2464+EEMLTEINTER: 5, 0, 4294967295, 247, 0
2465
2466+EEMLTEINTER: 6, 197, 41332, 24, 9
2467
2468+EEMLTEINTER: 7, 0, 0, 0, 0
2469
2470+EEMLTEINTER: 8, 0, 0, 0, 0
2471
2472+EEMLTEINTRA: 0, 429, 40936, 56, 12
2473
2474+EEMLTEINTERRAT: 0,0
2475
2476+EEMLTEINTERRAT: 1,0
2477
2478+EEMGINFO: 3, 2 // <state>:
2479 // 0: ME in Idle mode
2480 // 1: ME in Dedicated mode
2481 // 2: ME in PS PTM mode
2482 // 3: invalid state
2483 // <nw_type>:
2484 // 0: GSM 1: UMTS 2: LTE
2485
2486OK
2487
2488// WCDMA
2489AT+EEMGINFO?
2490// Mode, sCMeasPresent, sCParamPresent, ueOpStatusPresent,
2491
2492// if sCMeasPresent == 1
2493// cpichRSCP, utraRssi, cpichEcN0, sQual, sRxLev, txPower,
2494// endif
2495
2496// if sCParamPresent == 1
2497// rac, nom, mcc, mnc_len, mnc, lac, ci,
2498// uraId, psc, arfcn, t3212, t3312, hcsUsed, attDetAllowed,
2499// csDrxCycleLen, psDrxCycleLen, utranDrxCycleLen, HSDPASupport, HSUPASupport,
2500// endif
2501
2502// if ueOpStatusPresent == 1
2503// rrcState, numLinks, srncId, sRnti,
2504// algPresent, cipherAlg, cipherOn, algPresent, cipherAlg, cipherOn,
2505// HSDPAActive, HSUPAActive, MccLastRegisteredNetwork, MncLastRegisteredNetwork, TMSI, PTMSI, IsSingleMmRejectCause, IsSingleGmmRejectCause,
2506// MMRejectCause, GMMRejectCause, mmState, gmmState, gprsReadyState, readyTimerValueInSecs, NumActivePDPContext, ULThroughput, DLThroughput,
2507// serviceStatus, pmmState, LAU_status, LAU_count, RAU_status, RAU_count
2508// endif
2509//
2510+EEMUMTSSVC: 3, 1, 1, 1,
2511-80, 27, -6, -18, -115, -32768,
25121, 1, 1120, 2, 1, 61697, 168432821,
251315, 24, 10763, 0, 0, 0, 0,
2514128, 128, 65535, 0, 0,
25152, 255, 65535, 4294967295,
25160, 0, 0, 0, 0, 0,
25170, 0, 0, 0, 0, 0, 1, 1,
251828672, 28672, 0, 0, 0, 0, 0, 0, 0,
25190, 0, 0, 0, 0, 0
2520
2521// index, cpichRSCP, utraRssi, cpichEcN0, sQual, sRxLev ,mcc, mnc, lac, ci, arfcn, psc
2522+EEMUMTSINTRA: 0, -32768, -1, -32768, -18, -115, 0, 0, 65534, 1, 10763, 32
2523
2524+EEMUMTSINTRA: 1, -1, -32768, -18, -115, 0, 0, 65534, 2, 10763, 40, 32768
2525
2526+EEMUMTSINTRA: 2, -32768, -18, -115, 0, 0, 65534, 3, 10763, 278, 32768, 65535
2527
2528+EEMUMTSINTRA: 3, -18, -115, 0, 0, -2, 4, 10763, 28, 32768, 65535, 32768
2529
2530+EEMUMTSINTRA: 4, -115, 0, 0, -2, 5, 10763, 270, 32768, 65535, 32768, 65518
2531
2532+EEMUMTSINTRA: 5, 0, 0, -2, 6, 10763, 286, 32768, 65535, 32768, 65518, 65421
2533
2534+EEMUMTSINTRA: 6, 0, -2, 7, 10763, 80, 32768, 65535, 32768, 65518, 65421, 0
2535
2536+EEMUMTSINTRA: 7, -2, 8, 10763, 206, -32768, 65535, 32768, 65518, 65421, 0, 0
2537
2538+EEMUMTSINTRA: 8, 9, 10763, 11, -32768, -1, 32768, 65518, 65421, 0, 0, 65534
2539
2540+EEMUMTSINTRA: 9, 10763, 19, -32768, -1, -32768, 65518, 65421, 0, 0, 65534, 11
2541
2542+EEMUMTSINTRA: 10, 232, -32768, -1, -32768, -18, 65421, 0, 0, 65534, 12, 10763
2543
2544+EEMUMTSINTRA: 11, -32768, -1, -32768, -18, -115, 0, 0, 65534, 13, 10763, 66
2545
2546+EEMUMTSINTRA: 12, -1, -32768, -18, -115, 0, 0, 65534, 14, 10763, 216, 32768
2547
2548+EEMUMTSINTRA: 13, -32768, -18, -115, 0, 0, 65534, 15, 10763, 183, 32768, 65535
2549
2550+EEMUMTSINTRA: 14, -18, -115, 0, 0, -2, 16, 10763, 165, 32768, 65535, 32768
2551
2552+EEMUMTSINTRA: 15, -115, 0, 0, -2, 17, 10763, 151, 32768, 65535, 32768, 65518
2553
2554+EEMUMTSINTRA: 16, 0, 0, -2, 18, 10763, 43, 32768, 65535, 32768, 65518, 65421
2555
2556+EEMUMTSINTRA: 17, 0, -2, 19, 10763, 72, 32768, 65535, 32768, 65518, 65421, 0
2557
2558+EEMUMTSINTRA: 18, -2, 20, 10763, 157, -32768, 65535, 32768, 65518, 65421, 0, 0
2559
2560+EEMUMTSINTRA: 19, 21, 10763, 165, -32768, -1, 32768, 65518, 65421, 0, 0, 65534
2561
2562+EEMUMTSINTRA: 20, 10763, 301, -32768, -1, -32768, 65518, 65421, 0, 0, 65534, 23
2563
2564+EEMUMTSINTRA: 21, 23, -32768, -1, -32768, -18, 65421, 0, 0, 65534, 24, 10763
2565
2566+EEMUMTSINTRA: 22, -32768, -1, -32768, -18, -115, 0, 0, 65534, 25, 10763, 0
2567
2568+EEMUMTSINTRA: 23, -1, -32768, -18, -115, 0, 0, 65534, 26, 10763, 167, 32768
2569
2570+EEMUMTSINTRA: 24, -32768, -18, -115, 0, 0, 65534, 27, 10763, 34, 32768, 65535
2571
2572+EEMUMTSINTRA: 25, -18, -115, 0, 0, -2, 28, 10763, 313, 32768, 65535, 32768
2573
2574+EEMUMTSINTRA: 26, -115, 0, 0, -2, 29, 10763, 152, 32768, 65535, 32768, 65518
2575
2576+EEMUMTSINTRA: 27, 0, 0, -2, 30, 10763, 239, 0, 0, 0, 0, 0
2577
2578+EEMUMTSINTRA: 28, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
2579
2580+EEMUMTSINTRA: 29, 0, 0, 0, 0, -115, 0, 0, 65534, 30, 10763, 239
2581
2582// index,gsmRssi,rxLev,C1,C2,mcc,mnc,lac,ci,arfcn,bsic
2583+EEMUMTSINTERRAT: 0, -32768, -107, -1, -1, 0, 0, 65534, 0, 117, 36
2584
2585+EEMUMTSINTERRAT: 1, -107, -1, -1, 0, 0, 65534, 1, 72, 49, 0
2586
2587+EEMUMTSINTERRAT: 2, -1, -1, 0, 0, 65534, 2, 119, 15, 32768, 149
2588
2589+EEMUMTSINTERRAT: 3, -1, 0, 0, -2, 3, 121, 23, 0, 0, 0
2590
2591+EEMGINFO: 3, 1
2592
2593OK
2594
2595
2596// GSM
2597AT+EEMGINFO?
2598+EEMGINFOBASIC: 2
2599
2600// mcc, mnc_len, mnc, lac, ci, nom, nco,
2601// bsic, C1, C2, TA, TxPwr,
2602// RxSig, RxSigFull, RxSigSub, RxQualFull, RxQualSub,
2603// ARFCB_tch, hopping_chnl, chnl_type, TS, PacketIdle, rac, arfcn,
2604// bs_pa_mfrms, C31, C32, t3212, t3312, pbcch_support, EDGE_support,
2605// ncc_permitted, rl_timeout, ho_count, ho_succ, chnl_access_count, chnl_access_succ_count,
2606// gsmBand,channelMode
2607+EEMGINFOSVC: 1120, 2, 0, 32784, 24741, 2, 0,
260863, 36, 146, 1, 7,
260946, 42, 42, 7, 0,
261053, 0, 8, 0, 1, 6, 53,
26112, 0, 146, 42, 54, 0, 1,
26121, 32, 0, 0, 0, 0,
26130, 0
2614
2615// PS_attached, attach_type, service_type, tx_power, c_value,
2616// ul_ts, dl_ts, ul_cs, dl_cs, ul_modulation, dl_modulation,
2617// gmsk_cv_bep, 8psk_cv_bep, gmsk_mean_bep, 8psk_mean_bep, EDGE_bep_period, single_gmm_rej_cause
2618// pdp_active_num, mac_mode, network_control, network_mode, EDGE_slq_measurement_mode, edge_status
2619+EEMGINFOPS: 1, 255, 0, 0, 0,
26200, 0, 268435501, 1, 0, 0,
26214, 0, 96, 0, 0, 0,
26220, 0, 0, 65535, 0, 13350
2623
2624+EEMGINFO: 0, 0
2625
2626OK
2627
2628*/
2629static int req_cell_info_get(int *cme_err)
2630{
2631 ATResponse *response = NULL;
2632 int tmp_int;
2633 int buff_size = 0;
2634 // AT+EEMOPT=1 in the first.
2635 int err = at_send_command("AT+EEMOPT=1", &response);
2636 if (err < 0 || response->success == 0){
2637 *cme_err = at_get_cme_error(response);
2638 goto exit;
2639 }
2640
2641 // Reset buffer in the first.
2642 memset(&cell_info, 0xFF, sizeof(mbtK_cell_pack_info_t));
2643 cell_info.running = true;
2644 cell_info.cell_num = 0;
2645
2646 err = at_send_command_singleline("AT+EEMGINFO?", "+EEMGINFO:", &response);
2647 if (err < 0 || response->success == 0 || !response->p_intermediates){
2648 *cme_err = at_get_cme_error(response);
2649 goto exit;
2650 }
2651
2652 // Now, cell infomation has get from URC message.
2653
2654 char *line = response->p_intermediates->line;
2655 err = at_tok_start(&line);
2656 if (err < 0)
2657 {
2658 goto exit;
2659 }
2660 err = at_tok_nextint(&line, &tmp_int);
2661 if (err < 0)
2662 {
2663 goto exit;
2664 }
2665 err = at_tok_nextint(&line, &tmp_int);
2666 if (err < 0)
2667 {
2668 goto exit;
2669 }
2670
2671 cell_info.type = (uint8)tmp_int;
2672 cell_info.running = false;
2673
2674#if 0
2675 while(lines_ptr)
2676 {
2677 // LTE
2678 if(strStartsWith(line, "+EEMLTESVC:")) // LTE Server Cell
2679 {
2680
2681 }
b.liue0ab2442024-02-06 18:53:28 +08002682 else if(strStartsWith(line, "+EEMLTEINTER:")) // LTE
liubin281ac462023-07-19 14:22:54 +08002683 {
2684
2685 }
b.liue0ab2442024-02-06 18:53:28 +08002686 else if(strStartsWith(line, "+EEMLTEINTRA:")) // LTE
liubin281ac462023-07-19 14:22:54 +08002687 {
2688
2689 }
b.liue0ab2442024-02-06 18:53:28 +08002690 else if(strStartsWith(line, "+EEMLTEINTERRAT:")) // LTE
liubin281ac462023-07-19 14:22:54 +08002691 {
2692
2693 }
2694 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
2695 // <nw_type>: 0: GSM 1: UMTS 2: LTE
2696 {
2697
2698 }
2699 // WCDMA
2700 else if(strStartsWith(line, "+EEMUMTSSVC:")) // WCDMA Server Cell
2701 {
2702
2703 }
b.liue0ab2442024-02-06 18:53:28 +08002704 else if(strStartsWith(line, "+EEMUMTSINTRA:")) // WCDMA
liubin281ac462023-07-19 14:22:54 +08002705 {
2706
2707 }
b.liue0ab2442024-02-06 18:53:28 +08002708 else if(strStartsWith(line, "+EEMUMTSINTERRAT:")) // WCDMA
liubin281ac462023-07-19 14:22:54 +08002709 {
2710
2711 }
2712 // GSM
2713 else if(strStartsWith(line, "+EEMGINFOBASIC:")) // Basic information in GSM
2714 // 0: ME in Idle mode 1: ME in Dedicated mode 2: ME in PS PTM mode
2715 {
2716
2717 }
2718 else if(strStartsWith(line, "+EEMGINFOSVC:")) // GSM Server Cell
2719 {
2720
2721 }
b.liue0ab2442024-02-06 18:53:28 +08002722 else if(strStartsWith(line, "+EEMGINFOPS:")) // PS
liubin281ac462023-07-19 14:22:54 +08002723 {
2724
2725 }
2726
2727
2728 lines_ptr = lines_ptr->p_next;
2729 }
2730#endif
2731
2732exit:
2733 at_response_free(response);
2734 return buff_size;
2735}
2736
2737static int req_cell_info_set(const char *cmgl, char *reg, int len, int *cme_err)
2738{
2739 printf("req_cmgl_set(2)-----------------start\n");
2740 printf("cmgl:%s\n", cmgl);
2741 ATResponse *response = NULL;
b.liu9e8584b2024-11-06 19:21:28 +08002742 char cmd[500] = {0};
liubin281ac462023-07-19 14:22:54 +08002743 char data[218] = {0};
2744 int err = 0;
2745
2746 memcpy(data, cmgl, len);
2747
2748 sprintf(cmd, "at*cell=%s", data);
2749 printf("cmd:%s\n", cmd);
2750
2751 if(strlen(cmd) > 0)
2752 {
2753 err = at_send_command_multiline(cmd, "", &response);
2754 if (err < 0 || response->success == 0 || !response->p_intermediates){
2755 *cme_err = at_get_cme_error(response);
2756 // printf("at_send_command_multiline() is err-----------------\n");
2757 goto exit;
2758 }
2759
2760 ATLine* lines_ptr = response->p_intermediates;
2761 char *line = NULL;
b.liu9e8584b2024-11-06 19:21:28 +08002762// int reg_len = 0;
2763// bool flag = false;
liubin281ac462023-07-19 14:22:54 +08002764 while(lines_ptr)
2765 {
2766 line = lines_ptr->line;
2767 if(line ==NULL)
2768 {
2769 printf("line is null----------------------\n");
2770 }
2771 printf("-----line:%s\n", line);
2772
2773 lines_ptr = lines_ptr->p_next;
2774 }
2775 }
2776 err = 0;
2777 memcpy(reg, "req_cell_info_set succss", strlen("req_cell_info_set succss"));
2778exit:
2779 at_response_free(response);
2780 printf("req_cell_info_set()-----------------end\n");
2781 return err;
2782}
2783
2784
2785
2786/*
2787AT+CSQ
2788+CSQ: 31,99
2789
2790OK
2791
2792AT+CESQ
2793+CESQ: 60,99,255,255,20,61
2794
2795OK
2796
2797AT+COPS?
2798+COPS: 0,2,"46001",7
2799
2800OK
2801
2802*/
2803static int req_net_signal_get(mbtk_signal_info_t *signal, int *cme_err)
2804{
2805 ATResponse *response = NULL;
2806 int tmp_int;
2807 char *tmp_ptr = NULL;
2808 // AT+EEMOPT=1 in the first.
2809 int err = at_send_command_singleline("AT+CSQ", "+CSQ:", &response);
2810 if (err < 0 || response->success == 0 || !response->p_intermediates){
2811 if(cme_err != NULL)
2812 *cme_err = at_get_cme_error(response);
2813 err = -1;
2814 goto exit;
2815 }
2816
2817 char *line = response->p_intermediates->line;
2818 err = at_tok_start(&line);
2819 if (err < 0)
2820 {
2821 goto exit;
2822 }
2823 err = at_tok_nextint(&line, &tmp_int);
2824 if (err < 0)
2825 {
2826 goto exit;
2827 }
2828 signal->rssi = (uint8)tmp_int;
2829 at_response_free(response);
2830
2831 err = at_send_command_singleline("AT+CESQ", "+CESQ:", &response);
2832 if (err < 0 || response->success == 0 || !response->p_intermediates){
2833 if(cme_err != NULL)
2834 *cme_err = at_get_cme_error(response);
2835 err = -1;
2836 goto exit;
2837 }
2838
2839 line = response->p_intermediates->line;
2840 err = at_tok_start(&line);
2841 if (err < 0)
2842 {
2843 goto exit;
2844 }
2845 err = at_tok_nextint(&line, &tmp_int);
2846 if (err < 0)
2847 {
2848 goto exit;
2849 }
2850 signal->rxlev = (uint8)tmp_int;
2851
2852 err = at_tok_nextint(&line, &tmp_int);
2853 if (err < 0)
2854 {
2855 goto exit;
2856 }
2857 signal->ber = (uint8)tmp_int;
2858
2859 err = at_tok_nextint(&line, &tmp_int);
2860 if (err < 0)
2861 {
2862 goto exit;
2863 }
2864 signal->rscp = (uint8)tmp_int;
2865
2866 err = at_tok_nextint(&line, &tmp_int);
2867 if (err < 0)
2868 {
2869 goto exit;
2870 }
2871 signal->ecno = (uint8)tmp_int;
2872
2873 err = at_tok_nextint(&line, &tmp_int);
2874 if (err < 0)
2875 {
2876 goto exit;
2877 }
2878 signal->rsrq = (uint8)tmp_int;
2879
2880 err = at_tok_nextint(&line, &tmp_int);
2881 if (err < 0)
2882 {
2883 goto exit;
2884 }
2885 signal->rsrp = (uint8)tmp_int;
2886
2887 at_response_free(response);
2888 err = at_send_command_singleline("AT+COPS?", "+COPS:", &response);
2889 if (err < 0 || response->success == 0 || !response->p_intermediates){
2890 if(cme_err != NULL)
2891 *cme_err = at_get_cme_error(response);
2892 err = -1;
2893 goto exit;
2894 }
2895 line = response->p_intermediates->line;
2896 err = at_tok_start(&line);
2897 if (err < 0)
2898 {
2899 goto exit;
2900 }
2901 err = at_tok_nextint(&line, &tmp_int);
2902 if (err < 0)
2903 {
2904 goto exit;
2905 }
2906 if(!at_tok_hasmore(&line)) {
2907 goto exit;
2908 }
2909 err = at_tok_nextint(&line, &tmp_int);
2910 if (err < 0)
2911 {
2912 goto exit;
2913 }
2914 err = at_tok_nextstr(&line, &tmp_ptr);
2915 if (err < 0)
2916 {
2917 goto exit;
2918 }
2919 err = at_tok_nextint(&line, &tmp_int);
2920 if (err < 0)
2921 {
2922 goto exit;
2923 }
2924 signal->type = (uint8)tmp_int;
2925 net_info.net_type = signal->type;
2926
2927exit:
2928 at_response_free(response);
2929 return err;
2930}
2931
2932/*
2933AT+CREG=3
2934OK
2935
2936AT+CREG?
2937+CREG: 3,1,"8330","06447340",7
2938
2939OK
2940
2941AT+CREG?
2942+CREG: 3,0
2943
2944OK
2945
2946AT+CEREG?
2947+CEREG: 3,1,"8330","06447340",7
2948
2949OK
2950
2951
2952AT+CIREG?
2953+CIREG: 2,1,15
2954
2955OK
2956
2957AT+CIREG?
2958+CIREG: 0
2959
2960OK
2961
2962
2963*/
2964static int req_net_reg_get(mbtk_net_reg_info_t *reg, int *cme_err)
2965{
2966 ATResponse *response = NULL;
2967 int tmp_int;
2968 char *tmp_str = NULL;
2969 int err = at_send_command("AT+CREG=3", &response);
2970 if (err < 0 || response->success == 0){
2971 *cme_err = at_get_cme_error(response);
2972 goto exit;
2973 }
2974 at_response_free(response);
2975
2976 err = at_send_command_multiline("AT+CREG?", "+CREG:", &response);
2977 if (err < 0 || response->success == 0 || !response->p_intermediates){
2978 *cme_err = at_get_cme_error(response);
2979 goto exit;
2980 }
2981
2982 char *line = response->p_intermediates->line;
2983 err = at_tok_start(&line);
2984 if (err < 0)
2985 {
2986 goto exit;
2987 }
2988 err = at_tok_nextint(&line, &tmp_int); // n
2989 if (err < 0)
2990 {
2991 goto exit;
2992 }
2993 err = at_tok_nextint(&line, &tmp_int);// stat
2994 if (err < 0)
2995 {
2996 goto exit;
2997 }
2998 reg->call_state = (uint8)tmp_int;
2999
3000 if(at_tok_hasmore(&line)) {
3001 err = at_tok_nextstr(&line, &tmp_str); // lac
3002 if (err < 0)
3003 {
3004 goto exit;
3005 }
3006 reg->lac = strtol(tmp_str, NULL, 16);
3007
3008 err = at_tok_nextstr(&line, &tmp_str); // ci
3009 if (err < 0)
3010 {
3011 goto exit;
3012 }
3013 reg->ci = strtol(tmp_str, NULL, 16);
3014
3015 err = at_tok_nextint(&line, &tmp_int);// AcT
3016 if (err < 0)
3017 {
3018 goto exit;
3019 }
3020 reg->type = (uint8)tmp_int;
3021 }
3022 at_response_free(response);
3023
3024 err = at_send_command_multiline("AT+CEREG?", "+CEREG:", &response);
3025 if (err < 0 || response->success == 0 || !response->p_intermediates){
3026 *cme_err = at_get_cme_error(response);
3027 goto exit;
3028 }
3029
3030 line = response->p_intermediates->line;
3031 err = at_tok_start(&line);
3032 if (err < 0)
3033 {
3034 goto exit;
3035 }
3036 err = at_tok_nextint(&line, &tmp_int); // n
3037 if (err < 0)
3038 {
3039 goto exit;
3040 }
3041 err = at_tok_nextint(&line, &tmp_int);// stat
3042 if (err < 0)
3043 {
3044 goto exit;
3045 }
3046 reg->data_state = (uint8)tmp_int;
3047
3048 if(reg->lac == 0 && at_tok_hasmore(&line)) {
3049 err = at_tok_nextstr(&line, &tmp_str); // lac
3050 if (err < 0)
3051 {
3052 goto exit;
3053 }
3054 reg->lac = strtol(tmp_str, NULL, 16);
3055
3056 err = at_tok_nextstr(&line, &tmp_str); // ci
3057 if (err < 0)
3058 {
3059 goto exit;
3060 }
3061 reg->ci = strtol(tmp_str, NULL, 16);
3062
3063 err = at_tok_nextint(&line, &tmp_int);// AcT
3064 if (err < 0)
3065 {
3066 goto exit;
3067 }
3068 reg->type = (uint8)tmp_int;
3069 }
3070 at_response_free(response);
3071
3072 err = at_send_command_multiline("AT+CIREG?", "+CIREG:", &response);
3073 if (err < 0 || response->success == 0 || !response->p_intermediates){
3074 reg->ims_state = (uint8)0;
3075 err = 0;
3076 goto exit;
3077 }
3078 line = response->p_intermediates->line;
3079 err = at_tok_start(&line);
3080 if (err < 0)
3081 {
3082 goto exit;
3083 }
3084 err = at_tok_nextint(&line, &tmp_int); // n/stat
3085 if (err < 0)
3086 {
3087 goto exit;
3088 }
3089 if(at_tok_hasmore(&line)) {
3090 err = at_tok_nextint(&line, &tmp_int);// stat
3091 if (err < 0)
3092 {
3093 goto exit;
3094 }
3095 reg->ims_state = (uint8)tmp_int;
3096 } else {
3097 reg->ims_state = (uint8)tmp_int;
3098 }
3099
3100exit:
3101 at_response_free(response);
3102 return err;
3103}
3104
r.xiao06db9a12024-04-14 18:51:15 -07003105
3106static int net_ims_set(uint8 reg, int *cme_err)
3107{
3108 ATResponse *response = NULL;
3109 char cmd[30] = {0};
3110 int err = -1;
3111
b.liufdf03172024-06-07 15:01:29 +08003112 sprintf(cmd, "AT+ACONFIG=\"IMSD=%d\"", reg);
r.xiao06db9a12024-04-14 18:51:15 -07003113 err = at_send_command(cmd, &response);
3114 LOG("cmd : %s", cmd);
3115
3116 if (err < 0 || response->success == 0){
3117 *cme_err = at_get_cme_error(response);
3118 goto exit;
3119 }
3120
3121exit:
3122 at_response_free(response);
3123 return err;
3124}
3125
3126
3127static int net_ims_get(int *reg, int *cme_err)
3128{
3129 ATResponse *response = NULL;
b.liu9e8584b2024-11-06 19:21:28 +08003130 int tmp_reg = 0;
b.liufdf03172024-06-07 15:01:29 +08003131 int err;
b.liu9e8584b2024-11-06 19:21:28 +08003132// char *tmp_str = NULL;
b.liufdf03172024-06-07 15:01:29 +08003133
3134 err = at_send_command_singleline("AT+ACONFIG?", "", &response);
3135 if (err < 0 || response->success == 0 || !response->p_intermediates){
3136 tmp_reg = 0;
3137 err = 0;
3138 goto exit;
3139 }
3140 if(response->p_intermediates->line) {
3141 char *ptr = strstr(response->p_intermediates->line, "IMSD=");
3142 if(ptr) {
3143 tmp_reg = atoi(ptr + strlen("IMSD="));
3144 }
3145 }
3146
3147 LOG("net_ims_get reg : %u", tmp_reg);
3148
3149exit:
3150 at_response_free(response);
3151 *reg = tmp_reg;
3152 return err;
3153}
3154
3155static int net_ims_reg_state_get(int *reg, int *cme_err)
3156{
3157 ATResponse *response = NULL;
3158 int tmp_int, tmp_reg = -1;
r.xiao06db9a12024-04-14 18:51:15 -07003159 int err;
b.liu9e8584b2024-11-06 19:21:28 +08003160// char *tmp_str = NULL;
r.xiao06db9a12024-04-14 18:51:15 -07003161
3162 err = at_send_command_multiline("AT+CIREG?", "+CIREG:", &response);
3163 if (err < 0 || response->success == 0 || !response->p_intermediates){
3164 tmp_reg = 0;
3165 err = 0;
3166 goto exit;
3167 }
3168 char *line = response->p_intermediates->line;
3169 err = at_tok_start(&line);
3170 if (err < 0)
3171 {
3172 goto exit;
3173 }
3174 err = at_tok_nextint(&line, &tmp_int); // n/stat
3175 if (err < 0)
3176 {
3177 goto exit;
3178 }
liuyangc4ca9592024-06-06 15:43:50 +08003179
b.liufdf03172024-06-07 15:01:29 +08003180 if(at_tok_hasmore(&line)) {
3181 err = at_tok_nextint(&line, &tmp_int);// stat
3182 if (err < 0)
3183 {
3184 goto exit;
3185 }
3186 tmp_reg = tmp_int;
3187 } else {
3188 tmp_reg = tmp_int;
3189 }
r.xiao06db9a12024-04-14 18:51:15 -07003190
3191 LOG("net_ims_get reg : %u", tmp_reg);
3192
3193exit:
3194 at_response_free(response);
3195 *reg = tmp_reg;
3196 return err;
3197}
3198
3199
b.liufdf03172024-06-07 15:01:29 +08003200
liubin281ac462023-07-19 14:22:54 +08003201/*
3202AT+CGDCONT?
3203+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,,,,
3204
3205+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
3206
3207OK
3208
3209
3210*/
wangyouqianged88c722023-11-22 16:33:43 +08003211#ifdef MBTK_AF_SUPPORT
3212mbtk_ip_type_enum default_iptype = MBTK_IP_TYPE_IPV4V6;
3213#endif
3214
liubin281ac462023-07-19 14:22:54 +08003215static int req_apn_get(void *data, int *data_len, int *cme_err)
3216{
3217 ATResponse *response = NULL;
3218 int err = at_send_command_multiline("AT+CGDCONT?", "+CGDCONT:", &response);
3219
3220 if (err < 0 || response->success == 0 || !response->p_intermediates){
3221 *cme_err = at_get_cme_error(response);
3222 goto exit;
3223 }
3224
3225 ATLine* lines_ptr = response->p_intermediates;
3226 char *line = NULL;
3227 int tmp_int;
3228 char *tmp_str = NULL;
3229 /*
3230 <apn_num[1]><cid[1]><ip_type[1]><apn_len[2]><apn><user_len[2]><user><pass_len[2]><pass><auth_len[2]><auth>...
3231 <cid[1]><ip_type[1]><apn_len[2]><apn><user_len[2]><user><pass_len[2]><pass><auth_len[2]><auth>
3232 */
3233 uint8* apn_num = (uint8*)data;
3234 uint8* data_ptr = (uint8*)(data + sizeof(uint8)); // Jump apn_num[1]
3235 mbtk_apn_info_t apn;
3236 while(lines_ptr)
3237 {
3238 line = lines_ptr->line;
3239 err = at_tok_start(&line);
3240 if (err < 0)
3241 {
3242 goto exit;
3243 }
3244
3245 err = at_tok_nextint(&line, &tmp_int); // cid
3246 if (err < 0)
3247 {
3248 goto exit;
3249 }
3250 // Only get CID 1-7
3251 if(tmp_int >= MBTK_APN_CID_MIN && tmp_int <= MBTK_APN_CID_MAX) {
3252 memset(&apn, 0x0, sizeof(mbtk_apn_info_t));
3253 apn.cid = tmp_int;
3254 *data_ptr++ = (uint8)tmp_int; // cid
3255
3256 err = at_tok_nextstr(&line, &tmp_str);// ip type
3257 if (err < 0)
3258 {
3259 goto exit;
3260 }
3261 if(!strcasecmp(tmp_str, "IP")) {
3262 *data_ptr++ = (uint8)MBTK_IP_TYPE_IP;
3263 apn.ip_type = MBTK_IP_TYPE_IP;
3264 } else if(!strcasecmp(tmp_str, "IPV6")) {
3265 *data_ptr++ = (uint8)MBTK_IP_TYPE_IPV6;
3266 apn.ip_type = MBTK_IP_TYPE_IPV6;
3267 } else if(!strcasecmp(tmp_str, "IPV4V6")) {
3268 *data_ptr++ = (uint8)MBTK_IP_TYPE_IPV4V6;
3269 apn.ip_type = MBTK_IP_TYPE_IPV4V6;
3270 } else {
3271 *data_ptr++ = (uint8)MBTK_IP_TYPE_PPP;
3272 apn.ip_type = MBTK_IP_TYPE_PPP;
3273 }
3274
wangyouqianged88c722023-11-22 16:33:43 +08003275#ifdef MBTK_AF_SUPPORT
3276 if(apn.cid == 1)
3277 {
3278 default_iptype = apn.ip_type;
3279 }
3280#endif
liubin281ac462023-07-19 14:22:54 +08003281 err = at_tok_nextstr(&line, &tmp_str); // apn
3282 if (err < 0)
3283 {
3284 goto exit;
3285 }
3286 if(str_empty(tmp_str)) {
3287 uint16_2_byte((uint16)0, data_ptr, false);
3288 data_ptr += sizeof(uint16);
3289 } else {
3290 uint16_2_byte((uint16)strlen(tmp_str), data_ptr, false);
3291 data_ptr += sizeof(uint16);
3292 memcpy(data_ptr, tmp_str, strlen(tmp_str));
3293 data_ptr += strlen(tmp_str);
3294 memcpy(apn.apn, tmp_str, strlen(tmp_str));
3295 }
3296
3297 if(apn_user_pass_set_by_cid(apn.cid, &apn)) {
3298 // user
3299 uint16_2_byte((uint16)0, data_ptr, false);
3300 data_ptr += sizeof(uint16);
3301
3302 // pass
3303 uint16_2_byte((uint16)0, data_ptr, false);
3304 data_ptr += sizeof(uint16);
3305
3306 // auth
3307 uint16_2_byte((uint16)0, data_ptr, false);
3308 data_ptr += sizeof(uint16);
3309 } else {
3310 // user
3311 if(str_empty(apn.user)) {
3312 uint16_2_byte((uint16)0, data_ptr, false);
3313 data_ptr += sizeof(uint16);
3314 } else {
b.liu9e8584b2024-11-06 19:21:28 +08003315 uint16_2_byte((uint16)strlen((char*)apn.user), data_ptr, false);
liubin281ac462023-07-19 14:22:54 +08003316 data_ptr += sizeof(uint16);
b.liu9e8584b2024-11-06 19:21:28 +08003317 memcpy(data_ptr, apn.user, strlen((char*)apn.user));
3318 data_ptr += strlen((char*)apn.user);
liubin281ac462023-07-19 14:22:54 +08003319 }
3320
3321 // pass
3322 if(str_empty(apn.pass)) {
3323 uint16_2_byte((uint16)0, data_ptr, false);
3324 data_ptr += sizeof(uint16);
3325 } else {
b.liu9e8584b2024-11-06 19:21:28 +08003326 uint16_2_byte((uint16)strlen((char*)apn.pass), data_ptr, false);
liubin281ac462023-07-19 14:22:54 +08003327 data_ptr += sizeof(uint16);
b.liu9e8584b2024-11-06 19:21:28 +08003328 memcpy(data_ptr, apn.pass, strlen((char*)apn.pass));
3329 data_ptr += strlen((char*)apn.pass);
liubin281ac462023-07-19 14:22:54 +08003330 }
3331
3332 // auth
3333 if(str_empty(apn.auth)) {
3334 uint16_2_byte((uint16)0, data_ptr, false);
3335 data_ptr += sizeof(uint16);
3336 } else {
b.liu9e8584b2024-11-06 19:21:28 +08003337 uint16_2_byte((uint16)strlen((char*)apn.auth), data_ptr, false);
liubin281ac462023-07-19 14:22:54 +08003338 data_ptr += sizeof(uint16);
b.liu9e8584b2024-11-06 19:21:28 +08003339 memcpy(data_ptr, apn.auth, strlen((char*)apn.auth));
3340 data_ptr += strlen((char*)apn.auth);
liubin281ac462023-07-19 14:22:54 +08003341 }
3342 }
3343
3344 (*apn_num)++;
3345 }
3346
3347 lines_ptr = lines_ptr->p_next;
3348 }
3349
3350 *data_len = data_ptr - (uint8*)data;
3351
3352 goto exit;
3353exit:
3354 at_response_free(response);
3355 return err;
3356}
3357
3358#if 0
3359/*
3360LTE APN
3361AT+CFUN=4
3362AT*CGDFLT=1,IP,"private.vpdn",1,0,1,0,0,0,0,0,0,0,1,0,0,1,0,0,0,1
3363AT*CGDFAUTH=1,2,"noc@njcc.vpdn.js","123456"
3364AT+CFUN=1
3365AT+CEREG?
3366AT+CGDCONT?
3367
33682/3G APN
3369AT+CGREG?
3370AT+CGDCONT=6,IP,"private.vpdn"
3371AT*AUTHREQ=6,2,"noc@njcc.vpdn.js","123456"
3372AT+CGDATA="",6
3373AT+CGDCONT?
3374*/
3375static int req_apn_set_username(mbtk_apn_info_t *apn, int *cme_err)
3376{
3377 ATResponse *response = NULL;
3378 char cmd[400] = {0};
3379 int index = 0;
3380 int err = 0;
3381
3382 err = at_send_command_singleline("AT+COPS?", "+COPS:", &response);
3383 if (err < 0 || response->success == 0 || !response->p_intermediates){
3384 if(cme_err != NULL)
3385 *cme_err = at_get_cme_error(response);
3386 err = -1;
3387 goto apn_set;
3388 }
3389
3390 int tmp_int = 0;
3391 int state=0;
3392 char cmd_buf[64];
3393 char *line = response->p_intermediates->line;
3394 err = at_tok_start(&line);
3395 if (err < 0)
3396 {
3397 goto apn_set;
3398 }
3399 err = at_tok_nextint(&line, &tmp_int);
3400 if (err < 0)
3401 {
3402 goto apn_set;
3403 }
3404 err = at_tok_nextint(&line, &tmp_int);
3405 if (err < 0)
3406 {
3407 goto apn_set;
3408 }
3409 err = at_tok_nextstr(&line, &cmd_buf);
3410 if (err < 0)
3411 {
3412 goto apn_set;
3413 }
3414 err = at_tok_nextint(&line, &tmp_int);
3415 if (err < 0)
3416 {
3417 goto apn_set;
3418 }
3419 else
3420 state = tmp_int;
3421
3422apn_set:
3423 at_response_free(response);
3424 *cme_err = MBTK_INFO_ERR_CME_NON;
3425 //if(state == 7 && apn->cid == 1) //LTE && cid = 1
3426 if(0) //LTE && cid = 1
3427 {
3428 err = at_send_command("AT+CFUN=4", &response);
3429 if (err < 0 || response->success == 0){
3430 *cme_err = at_get_cme_error(response);
3431 goto exit;
3432 }
3433 at_response_free(response);
3434
3435 memset(cmd, 0, 400);
3436 index = 0;
3437 index += sprintf(cmd, "AT*CGDFLT=%d,", 1);
3438 switch(apn->ip_type) {
3439 case MBTK_IP_TYPE_IP: {
3440 index += sprintf(cmd + index,"\"IP\",");
3441 break;
3442 }
3443 case MBTK_IP_TYPE_IPV6: {
3444 index += sprintf(cmd + index,"\"IPV6\",");
3445 break;
3446 }
3447 case MBTK_IP_TYPE_IPV4V6: {
3448 index += sprintf(cmd + index,"\"IPV4V6\",");
3449 break;
3450 }
3451 default: {
3452 index += sprintf(cmd + index,"\"PPP\",");
3453 break;
3454 }
3455 }
3456
3457 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);
3458 err = at_send_command(cmd, &response);
3459 if (err < 0 || response->success == 0){
3460 *cme_err = at_get_cme_error(response);
3461 goto exit;
3462 }
3463 at_response_free(response);
3464
3465 memset(cmd, 0, 400);
3466 int cmd_auth=0;
3467 if(strstr(apn->auth,"NONE"))
3468 cmd_auth = 0;
3469 else if(strstr(apn->auth,"PAP"))
3470 cmd_auth = 1;
3471 else if(strstr(apn->auth,"CHAP"))
3472 cmd_auth = 2;
3473 else if(strstr(apn->auth,"PAP AND CHAP"))
3474 cmd_auth = 3;
3475 else
3476 goto exit;
3477
3478 sprintf(cmd,"AT*CGDFAUTH=1,%d,%s,%s",cmd_auth,apn->user,apn->pass);
3479
3480 err = at_send_command(cmd, &response);
3481 if (err < 0 || response->success == 0){
3482 *cme_err = at_get_cme_error(response);
3483 goto exit;
3484 }
3485 }
3486 else //2/3G
3487 {
3488 memset(cmd,0,400);
3489 index = 0;
3490 index += sprintf(cmd, "AT+CGDCONT=%d,", apn->cid);
3491 switch(apn->ip_type) {
3492 case MBTK_IP_TYPE_IP: {
3493 index += sprintf(cmd + index,"\"IP\",");
3494 break;
3495 }
3496 case MBTK_IP_TYPE_IPV6: {
3497 index += sprintf(cmd + index,"\"IPV6\",");
3498 break;
3499 }
3500 case MBTK_IP_TYPE_IPV4V6: {
3501 index += sprintf(cmd + index,"\"IPV4V6\",");
3502 break;
3503 }
3504 default: {
3505 index += sprintf(cmd + index,"\"PPP\",");
3506 break;
3507 }
3508 }
3509 index += sprintf(cmd + index, "\"%s\"", apn->apn);
3510
3511 err = at_send_command(cmd, &response);
3512 if (err < 0 || response->success == 0){
3513 *cme_err = at_get_cme_error(response);
3514 goto exit;
3515 }
3516 at_response_free(response);
3517
3518 memset(cmd,0,400);
3519 int cmd_auth=0;
3520 if(strstr(apn->auth,"NONE"))
3521 cmd_auth = 0;
3522 else if(strstr(apn->auth,"PAP"))
3523 cmd_auth = 1;
3524 else if(strstr(apn->auth,"CHAP"))
3525 cmd_auth = 2;
3526 else if(strstr(apn->auth,"PAP AND CHAP"))
3527 cmd_auth = 3;
3528 else
3529 goto exit;
3530
3531 sprintf(cmd, "AT*AUTHREQ=%d,%d,%s,%s",apn->cid,cmd_auth,apn->user,apn->pass);
3532 err = at_send_command(cmd, &response);
3533 if (err < 0 || response->success == 0){
3534 *cme_err = at_get_cme_error(response);
3535 goto exit;
3536 }
3537 }
3538
3539exit:
3540 at_response_free(response);
3541 return err;
3542}
3543#endif
3544
3545/*
3546AT+CGDCONT=1,"IPV4V6","cmnet"
3547OK
3548
3549AT*CGDFLT=1,"IPv4v6","reliance.grevpdn.zj",,,,,,,,,,,,,,,,,,1
3550OK
3551
3552*/
3553static int req_apn_set(mbtk_apn_info_t *apn, int *cme_err)
3554{
3555 ATResponse *response = NULL;
3556 char cmd[400] = {0};
3557 int index = 0;
3558 int err = 0;
3559
3560 index += sprintf(cmd, "AT+CGDCONT=%d,", apn->cid);
3561 switch(apn->ip_type) {
3562 case MBTK_IP_TYPE_IP: {
3563 index += sprintf(cmd + index,"\"IP\",");
3564 break;
3565 }
3566 case MBTK_IP_TYPE_IPV6: {
3567 index += sprintf(cmd + index,"\"IPV6\",");
3568 break;
3569 }
3570 case MBTK_IP_TYPE_IPV4V6: {
3571 index += sprintf(cmd + index,"\"IPV4V6\",");
3572 break;
3573 }
3574 default: {
3575 index += sprintf(cmd + index,"\"PPP\",");
3576 break;
3577 }
3578 }
b.liu9e8584b2024-11-06 19:21:28 +08003579 if(strlen((char*)apn->apn) > 0) {
liubin281ac462023-07-19 14:22:54 +08003580 index += sprintf(cmd + index,"\"%s\"", apn->apn);
b.liudfec1e12024-06-06 16:38:59 +08003581 }
liubin281ac462023-07-19 14:22:54 +08003582
3583 err = at_send_command(cmd, &response);
3584 if (err < 0 || response->success == 0){
3585 if(cme_err) {
3586 *cme_err = at_get_cme_error(response);
3587 }
3588 goto exit;
3589 }
3590
3591 if(!str_empty(apn->user) || !str_empty(apn->pass)) {
3592 at_response_free(response);
3593
3594 memset(cmd,0,400);
3595 int cmd_auth=0;
b.liu9e8584b2024-11-06 19:21:28 +08003596 if(strstr((char*)apn->auth,"NONE"))
liubin281ac462023-07-19 14:22:54 +08003597 cmd_auth = 0;
b.liu9e8584b2024-11-06 19:21:28 +08003598 else if(strstr((char*)apn->auth,"PAP"))
liubin281ac462023-07-19 14:22:54 +08003599 cmd_auth = 1;
b.liu9e8584b2024-11-06 19:21:28 +08003600 else if(strstr((char*)apn->auth,"CHAP"))
liubin281ac462023-07-19 14:22:54 +08003601 cmd_auth = 2;
3602#if 0
3603 else if(strstr(apn->auth,"PAP AND CHAP"))
3604 cmd_auth = 3;
3605#endif
3606 else
3607 goto exit;
3608
3609 sprintf(cmd, "AT*AUTHREQ=%d,%d,%s,%s",apn->cid,cmd_auth,apn->user,apn->pass);
3610 err = at_send_command(cmd, &response);
3611 if (err < 0 || response->success == 0){
3612 *cme_err = at_get_cme_error(response);
3613 goto exit;
3614 }
3615 }
3616
3617exit:
3618 at_response_free(response);
3619 return err;
3620}
3621
liuyang1cefd852024-04-24 18:30:53 +08003622static int req_apn_del(int data, int *cme_err)
liuyang0e49d9a2024-04-23 21:04:54 +08003623{
3624 ATResponse *response = NULL;
3625 char cmd[64]={0};
liuyang1cefd852024-04-24 18:30:53 +08003626 sprintf(cmd, "AT+CGDCONT=%d", data);
liuyang0e49d9a2024-04-23 21:04:54 +08003627 int err = at_send_command(cmd, &response);
3628 if (err < 0 || response->success == 0){
3629 if(cme_err) {
3630 *cme_err = at_get_cme_error(response);
3631 }
3632 goto exit;
3633 }
3634
3635exit:
3636 at_response_free(response);
3637 return err;
3638}
3639
3640
liubin281ac462023-07-19 14:22:54 +08003641int wait_cgact_complete(int timeout)
3642{
3643 int count = timeout * 10; // timeout * 1000 / 100
3644 int i = 0;
3645
3646 while(cgact_wait.waitting && i < count) {
3647 i++;
3648 usleep(100000); // 100ms
3649 }
3650
3651 if(i == count) { // Timeout
3652 return -1;
3653 } else {
3654 return 0;
3655 }
3656}
3657
b.liu9e8584b2024-11-06 19:21:28 +08003658#if 0
liubin281ac462023-07-19 14:22:54 +08003659/*
3660AT+CGDATA="",6
3661CONNECT
3662
3663OK
3664
3665AT+CFUN=1
3666
3667OK
3668
3669*/
3670static int req_data_call_user_start(int cid, int *cme_err)
3671{
3672 ATResponse *response = NULL;
3673 char cmd[400] = {0};
b.liu9e8584b2024-11-06 19:21:28 +08003674// int index = 0;
liubin281ac462023-07-19 14:22:54 +08003675 int err = 0;
3676
3677 err = at_send_command_singleline("AT+COPS?", "+COPS:", &response);
3678 if (err < 0 || response->success == 0 || !response->p_intermediates){
3679 if(cme_err != NULL)
3680 *cme_err = at_get_cme_error(response);
3681 err = -1;
3682 goto exit;
3683 }
3684
3685 int tmp_int;
b.liu9e8584b2024-11-06 19:21:28 +08003686 char *cmd_buf = NULL;
liubin281ac462023-07-19 14:22:54 +08003687 char *line = response->p_intermediates->line;
3688 err = at_tok_start(&line);
3689 if (err < 0)
3690 {
3691 goto exit;
3692 }
3693 err = at_tok_nextint(&line, &tmp_int);
3694 if (err < 0)
3695 {
3696 goto exit;
3697 }
3698 err = at_tok_nextint(&line, &tmp_int);
3699 if (err < 0)
3700 {
3701 goto exit;
3702 }
3703 err = at_tok_nextstr(&line, &cmd_buf);
3704 if (err < 0)
3705 {
3706 goto exit;
3707 }
3708 err = at_tok_nextint(&line, &tmp_int);
3709 if (err < 0)
3710 {
3711 goto exit;
3712 }
3713 at_response_free(response);
3714
3715 if(tmp_int == 7 && cid == 1) //LTE && cid = 1
3716 {
3717 ATResponse *response = NULL;
b.liu9e8584b2024-11-06 19:21:28 +08003718// char cmd[400] = {0};
liubin281ac462023-07-19 14:22:54 +08003719 int err = 0;
3720
3721 err = at_send_command("AT+CFUN=1", &response);
3722 if (err < 0 || response->success == 0){
3723 if(cme_err) {
3724 *cme_err = at_get_cme_error(response);
3725 }
3726 goto exit;
3727 }
3728 }
3729 else
3730 {
3731 ATResponse *response = NULL;
b.liu9e8584b2024-11-06 19:21:28 +08003732// char cmd[400] = {0};
3733 memset(cmd, 0, sizeof(cmd));
liubin281ac462023-07-19 14:22:54 +08003734 int err = 0;
3735 sprintf(cmd, "AT+CGDATA=\"\",%d", cid);
3736 err = at_send_command(cmd, &response);
3737 if (err < 0 || response->success == 0){
3738 if(cme_err) {
3739 *cme_err = at_get_cme_error(response);
3740 }
3741 goto exit;
3742 }
3743 }
3744
3745exit:
3746 at_response_free(response);
3747 return err;
3748}
b.liu9e8584b2024-11-06 19:21:28 +08003749#endif
liubin281ac462023-07-19 14:22:54 +08003750
3751/*
3752AT+CGACT?
3753+CGACT: 1,1
3754+CGACT: 8,1
3755OK
3756
3757AT+CGACT=1,<cid>
3758OK
3759
3760*/
3761static int req_data_call_start(int cid, int *cme_err)
3762{
3763 ATResponse *response = NULL;
3764 char cmd[400] = {0};
3765 int err = 0;
3766#if 0
3767 err = at_send_command_multiline("AT+CGACT?", "+CGACT:", &response);
3768 if (err < 0 || response->success == 0 || !response->p_intermediates){
3769 *cme_err = at_get_cme_error(response);
3770 goto exit;
3771 }
3772 ATLine* lines_ptr = response->p_intermediates;
3773 char *line = NULL;
3774 int tmp_int;
3775 while(lines_ptr)
3776 {
3777 line = lines_ptr->line;
3778 err = at_tok_start(&line);
3779 if (err < 0)
3780 {
3781 goto exit;
3782 }
3783
3784 err = at_tok_nextint(&line, &tmp_int); // cid
3785 if (err < 0)
3786 {
3787 goto exit;
3788 }
3789 if(tmp_int == cid) { // Found cid
3790 err = at_tok_nextint(&line, &tmp_int); // cid
3791 if (err < 0)
3792 {
3793 goto exit;
3794 }
3795 if(tmp_int == 1) { // This cid has active.
3796 goto net_config;
3797 } else {
3798 goto cid_active;
3799 }
3800 break;
3801 }
3802
3803 lines_ptr = lines_ptr->p_next;
3804 }
3805
3806 if(lines_ptr == NULL) { // No found this cid.
3807 LOGE("No found cid : %d", cid);
3808 goto exit;
3809 }
3810 at_response_free(response);
3811
3812 // Start active cid.
3813cid_active:
3814#endif
3815
3816 sprintf(cmd, "AT+CGACT=1,%d", cid);
3817 err = at_send_command(cmd, &response);
3818 if (err < 0 || response->success == 0){
3819 if(cme_err) {
3820 *cme_err = at_get_cme_error(response);
3821 }
3822 goto exit;
3823 }
3824
3825exit:
3826 at_response_free(response);
3827 return err;
3828}
3829
3830/*
3831AT+CGACT=0,<cid>
3832OK
3833
3834*/
3835static int req_data_call_stop(int cid, int *cme_err)
3836{
3837 ATResponse *response = NULL;
3838 char cmd[400] = {0};
3839 int err = 0;
3840#if 0
3841 err = at_send_command_multiline("AT+CGACT?", "+CGACT:", &response);
3842 if (err < 0 || response->success == 0 || !response->p_intermediates){
3843 *cme_err = at_get_cme_error(response);
3844 goto exit;
3845 }
3846 ATLine* lines_ptr = response->p_intermediates;
3847 char *line = NULL;
3848 int tmp_int;
3849 while(lines_ptr)
3850 {
3851 line = lines_ptr->line;
3852 err = at_tok_start(&line);
3853 if (err < 0)
3854 {
3855 goto exit;
3856 }
3857
3858 err = at_tok_nextint(&line, &tmp_int); // cid
3859 if (err < 0)
3860 {
3861 goto exit;
3862 }
3863 if(tmp_int == cid) { // Found cid
3864 err = at_tok_nextint(&line, &tmp_int); // cid
3865 if (err < 0)
3866 {
3867 goto exit;
3868 }
3869 if(tmp_int == 1) { // This cid has active.
3870 goto net_config;
3871 } else {
3872 goto cid_active;
3873 }
3874 break;
3875 }
3876
3877 lines_ptr = lines_ptr->p_next;
3878 }
3879
3880 if(lines_ptr == NULL) { // No found this cid.
3881 LOGE("No found cid : %d", cid);
3882 goto exit;
3883 }
3884 at_response_free(response);
3885
3886 // Start active cid.
3887cid_active:
3888#endif
3889
3890 sprintf(cmd, "AT+CGACT=0,%d", cid);
3891 err = at_send_command(cmd, &response);
3892 if (err < 0 || response->success == 0){
3893 *cme_err = at_get_cme_error(response);
3894 goto exit;
3895 }
3896
3897exit:
3898 at_response_free(response);
3899 return err;
3900}
3901
3902/*
3903IPv4 : 10.255.74.26
3904IPv6 : 254.128.0.0.0.0.0.0.0.1.0.2.144.5.212.239
3905*/
3906static bool is_ipv4(const char *ip)
3907{
3908 const char *ptr = ip;
3909 int count = 0;
3910 while(*ptr) {
3911 if(*ptr == '.')
3912 count++;
3913 ptr++;
3914 }
3915
3916 if(count == 3) {
3917 return true;
3918 } else {
3919 return false;
3920 }
3921}
3922
3923/*
3924AT+CGCONTRDP=1
3925+CGCONTRDP: 1,7,"cmnet-2.MNC000.MCC460.GPRS","10.255.74.26","","223.87.253.100","223.87.253.253","","",0,0
3926+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
3927
3928OK
3929
3930*/
3931static int req_data_call_state_get(int cid, mbtk_ipv4_info_t *ipv4, mbtk_ipv6_info_t *ipv6, int *cme_err)
3932{
3933 ATResponse *response = NULL;
3934 char cmd[50] = {0};
3935 int err = 0;
3936
3937 sprintf(cmd, "AT+CGCONTRDP=%d", cid);
3938
3939 err = at_send_command_multiline(cmd, "+CGCONTRDP:", &response);
3940 if (err < 0 || response->success == 0 || !response->p_intermediates){
3941 *cme_err = at_get_cme_error(response);
3942 goto exit;
3943 }
3944 ATLine* lines_ptr = response->p_intermediates;
3945 char *line = NULL;
3946 int tmp_int;
3947 char *tmp_ptr = NULL;
3948 while(lines_ptr)
3949 {
3950 line = lines_ptr->line;
3951 err = at_tok_start(&line);
3952 if (err < 0)
3953 {
3954 goto exit;
3955 }
3956
3957 err = at_tok_nextint(&line, &tmp_int); // cid
3958 if (err < 0)
3959 {
3960 goto exit;
3961 }
3962 err = at_tok_nextint(&line, &tmp_int); // bearer_id
3963 if (err < 0)
3964 {
3965 goto exit;
3966 }
3967 err = at_tok_nextstr(&line, &tmp_ptr); // APN
3968 if (err < 0)
3969 {
3970 goto exit;
3971 }
3972
3973 err = at_tok_nextstr(&line, &tmp_ptr); // IP
3974 if (err < 0 || str_empty(tmp_ptr))
3975 {
3976 goto exit;
3977 }
3978 if(is_ipv4(tmp_ptr)) {
3979 if(inet_pton(AF_INET, tmp_ptr, &(ipv4->IPAddr)) < 0) {
3980 LOGE("inet_pton() fail.");
3981 err = -1;
3982 goto exit;
3983 }
3984
3985 ipv4->valid = true;
3986 //log_hex("IPv4", &(ipv4->IPAddr), sizeof(struct in_addr));
3987 } else {
3988 if(str_2_ipv6(tmp_ptr, &(ipv6->IPV6Addr))) {
3989 LOGE("str_2_ipv6() fail.");
3990 err = -1;
3991 goto exit;
3992 }
3993
3994 ipv6->valid = true;
3995 //log_hex("IPv6", &(ipv6->IPV6Addr), 16);
3996 }
3997
3998 err = at_tok_nextstr(&line, &tmp_ptr); // Gateway
3999 if (err < 0)
4000 {
4001 goto exit;
4002 }
4003 if(!str_empty(tmp_ptr)) { // No found gateway
4004 if(is_ipv4(tmp_ptr)) {
4005 if(inet_pton(AF_INET, tmp_ptr, &(ipv4->GateWay)) < 0) {
4006 LOGE("inet_pton() fail.");
4007 err = -1;
4008 goto exit;
4009 }
4010
4011 //log_hex("IPv4", &(ipv4->GateWay), sizeof(struct in_addr));
4012 } else {
4013 if(str_2_ipv6(tmp_ptr, &(ipv6->GateWay))) {
4014 LOGE("str_2_ipv6() fail.");
4015 err = -1;
4016 goto exit;
4017 }
4018
4019 //log_hex("IPv6", &(ipv6->GateWay), 16);
4020 }
4021 }
4022
4023 err = at_tok_nextstr(&line, &tmp_ptr); // prim_DNS
4024 if (err < 0)
4025 {
4026 goto exit;
4027 }
4028 if(!str_empty(tmp_ptr)) { // No found Primary DNS
4029 if(is_ipv4(tmp_ptr)) {
4030 if(inet_pton(AF_INET, tmp_ptr, &(ipv4->PrimaryDNS)) < 0) {
4031 LOGE("inet_pton() fail.");
4032 err = -1;
4033 goto exit;
4034 }
4035
4036 //log_hex("IPv4", &(ipv4->PrimaryDNS), sizeof(struct in_addr));
4037 } else {
4038 if(str_2_ipv6(tmp_ptr, &(ipv6->PrimaryDNS))) {
4039 LOGE("str_2_ipv6() fail.");
4040 err = -1;
4041 goto exit;
4042 }
4043
4044 //log_hex("IPv6", &(ipv6->PrimaryDNS), 16);
4045 }
4046 }
4047
4048 err = at_tok_nextstr(&line, &tmp_ptr); // sec_DNS
4049 if (err < 0)
4050 {
4051 goto exit;
4052 }
4053 if(!str_empty(tmp_ptr)) { // No found Secondary DNS
4054 if(is_ipv4(tmp_ptr)) {
4055 if(inet_pton(AF_INET, tmp_ptr, &(ipv4->SecondaryDNS)) < 0) {
4056 LOGE("inet_pton() fail.");
4057 err = -1;
4058 goto exit;
4059 }
4060
4061 //log_hex("IPv4", &(ipv4->SecondaryDNS), sizeof(struct in_addr));
4062 } else {
4063 if(str_2_ipv6(tmp_ptr, &(ipv6->SecondaryDNS))) {
4064 LOGE("str_2_ipv6() fail.");
4065 err = -1;
4066 goto exit;
4067 }
4068
4069 //log_hex("IPv6", &(ipv6->SecondaryDNS), 16);
4070 }
4071 }
4072
4073 lines_ptr = lines_ptr->p_next;
4074 }
4075
4076exit:
4077 at_response_free(response);
4078 return err;
4079}
4080
b.liu9e8584b2024-11-06 19:21:28 +08004081#if 0
liubin281ac462023-07-19 14:22:54 +08004082/*
4083AT+CGCONTRDP
4084+CGCONTRDP: 1,5,"cmnet.MNC000.MCC460.GPRS","10.156.238.86","","223.87.253.100","223.87.253.253","","",0,0
4085+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
4086+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
4087OK
4088
4089*/
4090static int apn_state_get(list_node_t **apn_list)
4091{
4092 ATResponse *response = NULL;
4093 int err = 0;
4094 *apn_list = list_create(NULL);
4095 if(*apn_list == NULL)
4096 {
4097 LOG("list_create() fail.");
4098 return -1;
4099 }
4100
4101 err = at_send_command_multiline("AT+CGCONTRDP", "+CGCONTRDP:", &response);
4102 if (err < 0 || response->success == 0 || !response->p_intermediates){
4103 goto exit;
4104 }
4105 ATLine* lines_ptr = response->p_intermediates;
4106 char *line = NULL;
4107 int tmp_int;
4108 char *tmp_ptr = NULL;
4109 int cid_current = 0;
4110 info_apn_ip_t *apn = NULL;
4111 while(lines_ptr)
4112 {
4113 line = lines_ptr->line;
4114 err = at_tok_start(&line);
4115 if (err < 0)
4116 {
4117 goto exit;
4118 }
4119
4120 err = at_tok_nextint(&line, &tmp_int); // cid
4121 if (err < 0)
4122 {
4123 goto exit;
4124 }
4125
4126 if(tmp_int != 1 && tmp_int != 8) { // Not process cid 1 and 8.
4127 if(cid_current != tmp_int) { // New cid.
4128 apn = (info_apn_ip_t*)malloc(sizeof(info_apn_ip_t));
4129 if(apn == NULL) {
4130 goto exit;
4131 }
4132 memset(apn, 0, sizeof(info_apn_ip_t));
4133 apn->cid = tmp_int;
4134 cid_current = tmp_int;
4135
4136 list_add(*apn_list, apn);
4137 }
4138 err = at_tok_nextint(&line, &tmp_int); // bearer_id
4139 if (err < 0)
4140 {
4141 goto exit;
4142 }
4143 err = at_tok_nextstr(&line, &tmp_ptr); // APN
4144 if (err < 0)
4145 {
4146 goto exit;
4147 }
4148
4149 err = at_tok_nextstr(&line, &tmp_ptr); // IP
4150 if (err < 0 || str_empty(tmp_ptr))
4151 {
4152 goto exit;
4153 }
4154 if(is_ipv4(tmp_ptr)) {
4155 apn->ipv4_valid = true;
4156 memcpy(apn->ipv4, tmp_ptr, strlen(tmp_ptr));
4157 } else {
4158 apn->ipv6_valid = true;
4159 uint8 tmp_ipv6[16];
4160 if(str_2_ipv6(tmp_ptr, tmp_ipv6)) {
4161 LOGE("str_2_ipv6() fail.");
4162 err = -1;
4163 goto exit;
4164 }
4165
b.liu9e8584b2024-11-06 19:21:28 +08004166 if(inet_ntop(AF_INET6, tmp_ipv6, (char*)apn->ipv6, 50) == NULL) {
liubin281ac462023-07-19 14:22:54 +08004167 err = -1;
4168 LOGE("inet_ntop ipv6 ip fail.");
4169 goto exit;
4170 }
4171 }
4172 }
4173
4174 lines_ptr = lines_ptr->p_next;
4175 }
4176
4177exit:
4178 at_response_free(response);
4179 return err;
4180}
b.liu9e8584b2024-11-06 19:21:28 +08004181#endif
liubin281ac462023-07-19 14:22:54 +08004182
4183mbtk_info_err_enum call_pack_req_process(sock_client_info_t* cli_info, mbtk_info_pack_t* pack);
4184mbtk_info_err_enum sms_pack_req_process(sock_client_info_t* cli_info, mbtk_info_pack_t* pack);
4185mbtk_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 -08004186
liubin281ac462023-07-19 14:22:54 +08004187//mbtk wyq for data_call_ex add start
4188void data_call_bootconn_save(int cid, int bootconn);
4189//mbtk wyq for data_call_ex add end
4190
4191//void net_list_free(void *data);
4192// Return MBTK_INFO_ERR_SUCCESS,will call pack_error_send() to send RSP.
4193// Otherwise, do not call pack_error_send().
4194static mbtk_info_err_enum pack_req_process(sock_client_info_t* cli_info, mbtk_info_pack_t* pack)
4195{
4196 if(pack->info_id > MBTK_INFO_ID_CALL_BEGIN && pack->info_id < MBTK_INFO_ID_CALL_END) {
4197 return call_pack_req_process(cli_info, pack);
4198 } else if(pack->info_id > MBTK_INFO_ID_SMS_BEGIN && pack->info_id < MBTK_INFO_ID_SMS_END) {
4199 return sms_pack_req_process(cli_info, pack);
4200 } else if(pack->info_id > MBTK_INFO_ID_PB_BEGIN && pack->info_id < MBTK_INFO_ID_PB_END) {
4201 return pb_pack_req_process(cli_info, pack);
b.liu06559f62024-11-01 18:48:22 +08004202 } else if(pack->info_id > RIL_MSG_ID_ECALL_BEGIN && pack->info_id < RIL_MSG_ID_ECALL_END) {
4203 return ecall_pack_req_process(cli_info, pack);
liubin281ac462023-07-19 14:22:54 +08004204 } else {
4205 mbtk_info_err_enum err = MBTK_INFO_ERR_SUCCESS;
4206 int cme_err = MBTK_INFO_ERR_CME_NON;
4207 switch(pack->info_id)
4208 {
4209 case MBTK_INFO_ID_DEV_IMEI_REQ: // <string> IMEI
4210 {
4211 if(pack->data_len == 0 || pack->data == NULL) // Get IMEI
4212 {
4213 char imei[20] = {0};
4214 if(req_imei_get(imei, &cme_err) || strlen(imei) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4215 {
4216 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4217 err = MBTK_INFO_ERR_CME + cme_err;
4218 } else {
4219 err = MBTK_INFO_ERR_UNKNOWN;
4220 }
4221 LOG("Get IMEI fail.");
4222 }
4223 else
4224 {
4225 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_IMEI_RSP, imei, strlen(imei));
4226 }
4227 }
4228 else // Set IMEI(Unsupport).
4229 {
4230 err = MBTK_INFO_ERR_UNSUPPORTED;
4231 LOG("Unsupport set IMEI.");
4232 }
4233 break;
4234 }
4235 case MBTK_INFO_ID_DEV_SN_REQ: // <string> SN
4236 {
4237 if(pack->data_len == 0 || pack->data == NULL) // Get SN
4238 {
4239 char sn[20] = {0};
4240 if(req_sn_get(sn, &cme_err) || strlen(sn) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4241 {
4242 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4243 err = MBTK_INFO_ERR_CME + cme_err;
4244 } else {
4245 err = MBTK_INFO_ERR_UNKNOWN;
4246 }
4247 LOG("Get SN fail.");
4248 }
4249 else
4250 {
4251 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_SN_RSP, sn, strlen(sn));
4252 }
4253 }
4254 else // Set SN(Unsupport).
4255 {
4256 err = MBTK_INFO_ERR_UNSUPPORTED;
4257 LOG("Unsupport set SN.");
4258 }
4259 break;
4260 }
4261 case MBTK_INFO_ID_DEV_MEID_REQ: // <string> MEID (Only for CDMA)
4262 {
4263 if(pack->data_len == 0 || pack->data == NULL) // Get MEID
4264 {
4265 err = MBTK_INFO_ERR_UNSUPPORTED;
4266 LOG("Support only for CDMA.");
4267 }
4268 else // Set MEID(Unsupport).
4269 {
4270 err = MBTK_INFO_ERR_UNSUPPORTED;
4271 LOG("Unsupport set MEID.");
4272 }
4273 break;
4274 }
4275 case MBTK_INFO_ID_DEV_VERSION_REQ: // <string> VERSION
4276 {
4277 if(pack->data_len == 0 || pack->data == NULL) // Get VERSION
4278 {
4279 char version[50] = {0};
4280 if(req_version_get(version, &cme_err) || strlen(version) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4281 {
4282 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4283 err = MBTK_INFO_ERR_CME + cme_err;
4284 } else {
4285 err = MBTK_INFO_ERR_UNKNOWN;
4286 }
4287 LOG("Get Version fail.");
4288 }
4289 else
4290 {
4291 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_VERSION_RSP, version, strlen(version));
4292 }
4293 }
4294 else // Set VERSION(Unsupport).
4295 {
4296 err = MBTK_INFO_ERR_UNSUPPORTED;
4297 LOG("Unsupport set VERSION.");
4298 }
4299 break;
4300 }
l.yang5b0ff422024-10-29 19:33:35 -07004301 case MBTK_INFO_ID_DEV_MD_VERSION_REQ:
4302 {
4303 if(pack->data_len == 0 || pack->data == NULL) // Get VERSION
4304 {
4305 char version[50] = {0};
4306 if(req_md_version_get(version, &cme_err) || strlen(version) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4307 {
4308 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4309 err = MBTK_INFO_ERR_CME + cme_err;
4310 } else {
4311 err = MBTK_INFO_ERR_UNKNOWN;
4312 }
4313 LOG("Get Version fail.");
4314 }
4315 else
4316 {
4317 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_MD_VERSION_RSP, version, strlen(version));
4318 }
4319 }
4320 else // Set MD_VERSION(Unsupport).
4321 {
4322 err = MBTK_INFO_ERR_UNSUPPORTED;
4323 LOG("Unsupport set MD_VERSION.");
4324 }
4325 break;
4326
4327 }
liubin281ac462023-07-19 14:22:54 +08004328 case MBTK_INFO_ID_DEV_MODEL_REQ: //MODEL
4329 {
4330 if(pack->data_len == 0 || pack->data == NULL) // Get MODEL
4331 {
4332 char model[50] = {0};
4333 if(req_model_get(model, &cme_err) || strlen(model) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4334 {
4335 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4336 err = MBTK_INFO_ERR_CME + cme_err;
4337 } else {
4338 err = MBTK_INFO_ERR_UNKNOWN;
4339 }
4340 LOG("Get model fail.");
4341 }
4342 else
4343 {
4344 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_VERSION_RSP, model, strlen(model));
4345 }
4346 }
4347 else // Set model(Unsupport).
4348 {
4349 err = MBTK_INFO_ERR_UNSUPPORTED;
4350 LOG("Unsupport set model.");
4351 }
4352 break;
4353 }
4354 case MBTK_INFO_ID_DEV_MODEM_REQ: //MODEM
4355 {
4356 if(pack->data_len == 0 || pack->data == NULL) // Get MODEM
4357 {
4358 int modem = -1;
4359 if(req_modem_get(&modem, &cme_err) || modem < 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4360 {
4361 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4362 err = MBTK_INFO_ERR_CME + cme_err;
4363 } else {
4364 err = MBTK_INFO_ERR_UNKNOWN;
4365 }
4366 LOG("Get modem fail.");
4367 }
4368 else
4369 {
4370 uint8 modem_type = (uint8)modem;
4371 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_MODEM_RSP, &modem_type, sizeof(uint8));
4372 }
4373 }
4374 else // Set modem
4375 {
4376 mbtk_modem_info_t *modem = (mbtk_modem_info_t *)pack->data;
4377 if(pack->data_len != sizeof(mbtk_modem_info_t))
4378 {
4379 err = MBTK_INFO_ERR_REQ_PARAMETER;
4380 LOG("Set modem error.");
4381 break;
4382 }
4383 if(req_modem_set(modem, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4384 {
4385 LOG("Set modem fail.");
4386 err = MBTK_INFO_ERR_FORMAT;
4387 } else {
4388 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_MODEM_RSP, NULL, 0);
4389 }
4390 }
4391 break;
4392 }
4393 case MBTK_INFO_ID_DEV_TIME_REQ: // <uint8><string> YYYY-MM-DD-HH:MM:SS
4394 {
4395 if(pack->data_len == 0 || pack->data == NULL) // Get Time
4396 {
4397 int type = -1;
4398 if(req_time_get(&type, &cme_err) || type < 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4399 {
4400 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4401 err = MBTK_INFO_ERR_CME + cme_err;
4402 } else {
4403 err = MBTK_INFO_ERR_UNKNOWN;
4404 }
4405 LOG("Get Time fail.");
4406 }
4407 else
4408 {
4409 uint8 time_type = (uint8)type;
4410 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_TIME_RSP, &time_type, sizeof(uint8));
4411 }
4412 }
4413 else // Set Time
4414 {
4415 if(pack->data_len == sizeof(uint8)) {
4416 if(req_time_set(*(pack->data), NULL, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4417 {
4418 LOG("Set Time fail.");
4419 err = MBTK_INFO_ERR_FORMAT;
4420 } else {
4421 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_TIME_RSP, NULL, 0);
4422 }
4423 } else {
4424 char time_ptr[100] = {0};
4425 memcpy(time_ptr, pack->data + sizeof(uint8), pack->data_len - sizeof(uint8));
4426 if(req_time_set(*(pack->data), time_ptr, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4427 {
4428 LOG("Set Time fail.");
4429 err = MBTK_INFO_ERR_FORMAT;
4430 } else {
4431 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_TIME_RSP, NULL, 0);
4432 }
4433 }
4434 }
4435 break;
4436 }
b.liubaa41e12024-07-19 15:07:24 +08004437 case MBTK_INFO_ID_DEV_CELL_TIME_REQ: // <string> YYYY-MM-DD-HH:MM:SS
liubin281ac462023-07-19 14:22:54 +08004438 {
4439 if(pack->data_len == 0 || pack->data == NULL) // Get Time
4440 {
4441 char time[100];
4442 if(req_net_time_get(time, &cme_err) || strlen(time) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4443 {
4444 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4445 err = MBTK_INFO_ERR_CME + cme_err;
4446 } else {
4447 err = MBTK_INFO_ERR_UNKNOWN;
4448 }
4449 LOG("Get Time fail.");
4450 }
4451 else
4452 {
4453 char time_ser[100]={0};
4454 memcpy(time_ser,time,strlen(time));
b.liubaa41e12024-07-19 15:07:24 +08004455 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_CELL_TIME_RSP, time_ser, strlen(time_ser));
liubin281ac462023-07-19 14:22:54 +08004456 }
4457 }
4458 else // Set Time
4459 {
4460 err = MBTK_INFO_ERR_UNSUPPORTED;
4461 LOG("Unsupport set TIME.");
4462 }
4463 break;
4464 }
4465 case MBTK_INFO_ID_DEV_VOLTE_REQ: // <uint8> 0:Close 1:Open
4466 {
4467 if(pack->data_len == 0 || pack->data == NULL) // Get VoLTE state.
4468 {
4469 int state;
4470 if(req_volte_get(&state, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4471 {
4472 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4473 err = MBTK_INFO_ERR_CME + cme_err;
4474 } else {
4475 err = MBTK_INFO_ERR_UNKNOWN;
4476 }
4477 LOG("Get VoLTE state fail.");
4478 }
4479 else
4480 {
4481 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_VOLTE_RSP, &state, sizeof(uint8));
4482 }
4483 }
4484 else // Set VoLTE state.
4485 {
4486 uint8 on = *(pack->data);
4487 if(pack->data_len != sizeof(uint8) || (on != 0 && on != 1))
4488 {
4489 err = MBTK_INFO_ERR_REQ_PARAMETER;
4490 LOG("Set VOLTE parameter error.");
4491 break;
4492 }
4493
4494 if(req_volte_set(on, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4495 {
4496 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4497 err = MBTK_INFO_ERR_CME + cme_err;
4498 } else {
4499 err = MBTK_INFO_ERR_UNKNOWN;
4500 }
4501 LOG("Set VoLTE state fail.");
4502 }
4503 else
4504 {
4505 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_VOLTE_RSP, NULL, 0);
4506
4507 // Restart is required to take effect.
4508 LOG("Will reboot system...");
4509 }
4510 }
4511 break;
4512 }
4513 case MBTK_INFO_ID_DEV_TEMP_REQ: // <string> Temperature
4514 {
4515 if(pack->data_len == sizeof(uint8) && pack->data) {
r.xiao2102d762024-06-07 03:10:38 -07004516 mbtk_thermal_info_t temp;
4517 memset(&temp, 0, sizeof(mbtk_thermal_info_t));
liubin281ac462023-07-19 14:22:54 +08004518 if(req_temp_get(*(pack->data), &temp, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4519 {
4520 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4521 err = MBTK_INFO_ERR_CME + cme_err;
4522 } else {
4523 err = MBTK_INFO_ERR_UNKNOWN;
4524 }
4525 LOG("Get temperature fail.");
4526 }
4527 else
4528 {
r.xiao2102d762024-06-07 03:10:38 -07004529 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_TEMP_RSP, &temp, sizeof(mbtk_thermal_info_t));
liubin281ac462023-07-19 14:22:54 +08004530 }
4531 } else {
4532 err = MBTK_INFO_ERR_FORMAT;
4533 LOG("Unsupport get Temperature.");
4534 }
4535 break;
4536 }
4537 case MBTK_INFO_ID_SIM_PLMN_REQ: // plmn
4538 {
4539 if(pack->data_len == 0 || pack->data == NULL) // plmn
4540 {
4541 mbtk_plmn_info plmn;
4542 if(req_plmn_get(&plmn, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4543 {
4544 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4545 err = MBTK_INFO_ERR_CME + cme_err;
4546 } else {
4547 err = MBTK_INFO_ERR_UNKNOWN;
4548 }
4549 LOG("Get PLMN fail.");
4550 }
4551 else
4552 {
4553 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_PLMN_RSP, &plmn, sizeof(mbtk_plmn_info));
4554 }
4555 }
4556 else // Set
4557 {
4558 err = MBTK_INFO_ERR_UNSUPPORTED;
4559 LOG("Set sim state fail.");
4560 }
4561 break;
4562 }
4563 case MBTK_INFO_ID_SIM_STATE_REQ: // mbtk_sim_state_enum
4564 {
4565 if(pack->data_len == 0 || pack->data == NULL) // Get sim state.
4566 {
4567 uint8 sim_state = (uint8)getSIMStatus();
4568 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_STATE_RSP, &sim_state, sizeof(uint8));
4569 }
4570 else // Set
4571 {
4572 err = MBTK_INFO_ERR_UNSUPPORTED;
4573 LOG("Set sim state fail.");
4574 }
4575 break;
4576 }
4577 case MBTK_INFO_ID_SIM_STYPE_REQ: // mbtk_sim_card_type_enum
4578 {
4579 if(pack->data_len == 0 || pack->data == NULL) // Get sim card type
4580 {
4581 uint8 sim_card_type;
4582 if(req_sim_card_type_get(&sim_card_type, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4583 {
4584 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4585 err = MBTK_INFO_ERR_CME + cme_err;
4586 } else {
4587 err = MBTK_INFO_ERR_UNKNOWN;
4588 }
4589 LOG("Get IMSI fail.");
4590 }
4591 else
4592 {
4593 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_STYPE_RSP, &sim_card_type, sizeof(uint8));
4594 }
4595 }
4596 else // Set
4597 {
4598 err = MBTK_INFO_ERR_UNSUPPORTED;
4599 LOG("Set sim state fail.");
4600 }
4601 break;
4602 }
4603 case MBTK_INFO_ID_SIM_PINPUK_TIMES_REQ: // mbtk_pin_puk_last_times
4604 {
4605 if(pack->data_len == 0 || pack->data == NULL) // Get sim card type
4606 {
4607 mbtk_pin_puk_last_times pin_puk_last_times;
4608 if(req_pin_puk_last_times_get(&pin_puk_last_times, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4609 {
4610 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4611 err = MBTK_INFO_ERR_CME + cme_err;
4612 } else {
4613 err = MBTK_INFO_ERR_UNKNOWN;
4614 }
4615 LOG("Get IMSI fail.");
4616 }
4617 else
4618 {
4619 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_PINPUK_TIMES_RSP, &pin_puk_last_times, sizeof(mbtk_pin_puk_last_times));
4620 }
4621 }
4622 else // Set
4623 {
4624 err = MBTK_INFO_ERR_UNSUPPORTED;
4625 LOG("Set sim state fail.");
4626 }
4627 break;
4628 }
4629 case MBTK_INFO_ID_SIM_ENABLE_PIN_REQ: // <string> PIN
4630 {
4631 if(pack->data_len == 0 || pack->data == NULL) // Enable PIN
4632 {
yq.wang586a0df2024-10-24 20:10:37 -07004633 mbtk_pin_state_enum pin_state = MBTK_PIN_DISABLE;
4634 if(req_get_pin_state(&pin_state, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4635 {
4636 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4637 err = MBTK_INFO_ERR_CME + cme_err;
4638 } else {
4639 err = MBTK_INFO_ERR_UNKNOWN;
4640 }
4641 LOGE("Get pin state fail.");
4642 }
4643 else
4644 {
4645 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_ENABLE_PIN_RSP, &pin_state, sizeof(mbtk_pin_state_enum));
4646 }
liubin281ac462023-07-19 14:22:54 +08004647 }
4648 else // Enable PIN
4649 {
4650 mbtk_enable_pin_info *pin = NULL;
4651 pin = (mbtk_enable_pin_info *)pack->data;
4652 if(req_pin_enable(pin, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4653 {
4654 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4655 err = MBTK_INFO_ERR_CME + cme_err;
4656 } else {
4657 err = MBTK_INFO_ERR_UNKNOWN;
4658 }
4659 LOG("Get IMSI fail.");
4660 }
4661 else
4662 {
4663 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_ENABLE_PIN_RSP, NULL, 0);
4664 }
4665 }
4666 break;
4667 }
4668 case MBTK_INFO_ID_SIM_PIN_REQ: // <string> PIN
4669 {
4670 if(pack->data_len == 0 || pack->data == NULL) // PIN
4671 {
4672 err = MBTK_INFO_ERR_UNSUPPORTED;
4673 LOG("Unsupport GET PIN.");
4674 }
4675 else // Set PIN
4676 {
4677 char pin[16] = {0};
4678 memcpy(pin, pack->data, pack->data_len);
4679 if(req_pin_verify(pin, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4680 {
4681 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4682 err = MBTK_INFO_ERR_CME + cme_err;
4683 } else {
4684 err = MBTK_INFO_ERR_UNKNOWN;
4685 }
4686 LOG("Set PIN fail.");
4687 }
4688 else
4689 {
4690 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_PIN_RSP, NULL, 0);
4691 }
4692 }
4693 break;
4694 }
4695 case MBTK_INFO_ID_SIM_PUK_REQ: // <string> PUK
4696 {
4697 if(pack->data_len == 0 || pack->data == NULL)
4698 {
4699 err = MBTK_INFO_ERR_UNSUPPORTED;
4700 LOG("Unsupport.");
4701 }
4702 else // change PIN
4703 {
4704 mbtk_unlock_pin_info *pin_info = NULL;
4705 pin_info = (mbtk_unlock_pin_info *)pack->data;
4706 if(req_puk_unlock_pin(pin_info, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4707 {
4708 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4709 err = MBTK_INFO_ERR_CME + cme_err;
4710 } else {
4711 err = MBTK_INFO_ERR_UNKNOWN;
4712 }
4713 LOG("Get IMSI fail.");
4714 }
4715 else
4716 {
4717 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_CHANGE_PIN_RSP, NULL, 0);
4718 }
4719 }
4720 break;
4721 }
4722 case MBTK_INFO_ID_SIM_CHANGE_PIN_REQ: // <string> <string> old_PIN new_PIN
4723 {
4724 if(pack->data_len == 0 || pack->data == NULL)
4725 {
4726 err = MBTK_INFO_ERR_UNSUPPORTED;
4727 LOG("Unsupport.");
4728 }
4729 else // change PIN
4730 {
4731 mbtk_change_pin_info *pin_info = NULL;
4732 pin_info = (mbtk_change_pin_info *)pack->data;
4733 if(req_pin_change(pin_info, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4734 {
4735 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4736 err = MBTK_INFO_ERR_CME + cme_err;
4737 } else {
4738 err = MBTK_INFO_ERR_UNKNOWN;
4739 }
4740 LOG("Get IMSI fail.");
4741 }
4742 else
4743 {
4744 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_CHANGE_PIN_RSP, NULL, 0);
4745 }
4746 }
4747 break;
4748 }
4749 case MBTK_INFO_ID_SIM_IMSI_REQ: // <string> IMSI
4750 {
4751 if(pack->data_len == 0 || pack->data == NULL) // Get IMSI
4752 {
4753 char imsi[20] = {0};
4754 if(req_imsi_get(imsi, &cme_err) || strlen(imsi) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4755 {
4756 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4757 err = MBTK_INFO_ERR_CME + cme_err;
4758 } else {
4759 err = MBTK_INFO_ERR_UNKNOWN;
4760 }
4761 LOG("Get IMSI fail.");
4762 }
4763 else
4764 {
4765 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_IMSI_RSP, imsi, strlen(imsi));
4766 }
4767 }
4768 else // Set IMSI(Unsupport).
4769 {
4770 err = MBTK_INFO_ERR_UNSUPPORTED;
4771 LOG("Unsupport set IMSI.");
4772 }
4773 break;
4774 }
4775 case MBTK_INFO_ID_SIM_ICCID_REQ: // <string> ICCID
4776 {
4777 if(pack->data_len == 0 || pack->data == NULL) // Get ICCID
4778 {
4779 //log_hex("pack", pack, sizeof(mbtk_info_pack_t));
4780 //sleep(1);
4781 char iccid[50] = {0};
4782 if(req_iccid_get(iccid, &cme_err) || strlen(iccid) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4783 {
4784 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4785 err = MBTK_INFO_ERR_CME + cme_err;
4786 } else {
4787 err = MBTK_INFO_ERR_UNKNOWN;
4788 }
4789 LOG("Get ICCID fail.");
4790 }
4791 else
4792 {
4793 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_ICCID_RSP, iccid, strlen(iccid));
4794 }
4795 }
4796 else // Set ICCID(Unsupport).
4797 {
4798 err = MBTK_INFO_ERR_UNSUPPORTED;
4799 LOG("Unsupport set ICCID.");
4800 }
4801 break;
4802 }
4803 case MBTK_INFO_ID_SIM_PN_REQ: // <string> Phone Number
4804 {
4805 if(pack->data_len == 0 || pack->data == NULL) // Get Phone Number
4806 {
4807 //log_hex("pack", pack, sizeof(mbtk_info_pack_t));
4808 //sleep(1);
4809 char phone_number[50] = {0};
4810 if(req_phone_number_get(phone_number, &cme_err) || strlen(phone_number) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4811 {
4812 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4813 err = MBTK_INFO_ERR_CME + cme_err;
4814 } else {
4815 err = MBTK_INFO_ERR_UNKNOWN;
4816 }
4817 LOG("Get Phone Number fail.");
4818 }
4819 else
4820 {
4821 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_PN_RSP, phone_number, strlen(phone_number));
4822 }
4823 }
4824 else // Set Phone Number(Unsupport).
4825 {
4826 err = MBTK_INFO_ERR_UNSUPPORTED;
4827 LOG("Unsupport set Phone Number.");
4828 }
4829 break;
4830 }
4831 case MBTK_INFO_ID_NET_SEL_MODE_REQ: // <mbtk_net_info_t> Operator
4832 {
4833 if(pack->data_len == 0 || pack->data == NULL) // Get
4834 {
4835 mbtk_net_info_t info;
4836 memset(&info, 0, sizeof(mbtk_net_info_t));
4837 if(req_net_sel_mode_get(&info, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4838 {
4839 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4840 err = MBTK_INFO_ERR_CME + cme_err;
4841 } else {
4842 err = MBTK_INFO_ERR_UNKNOWN;
4843 }
4844 LOG("Get Net select mode fail.");
4845 }
4846 else
4847 {
4848 LOG("NET : %d, %d, %d, %d", info.net_sel_mode, info.net_type, info.net_state, info.plmn);
4849 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_SEL_MODE_RSP, &info, sizeof(mbtk_net_info_t));
4850 }
4851 }
4852 else // Set
4853 {
4854 //LOG("1 pack-%p,data-%p,data_len-%d", pack, pack->data, pack->data_len);
4855 //log_hex("pack", pack, sizeof(mbtk_info_pack_t));
4856 //log_hex("data", pack->data, pack->data_len);
4857
4858 mbtk_net_info_t* info = (mbtk_net_info_t*)pack->data;//(mbtk_net_info_t*)mbtk_info_pack_data_get(pack, NULL);
4859 if(info == NULL) {
4860 err = MBTK_INFO_ERR_FORMAT;
4861 LOG("Get Net select mode fail.");
4862 } else {
4863 LOG("NET : %d, %d, %d", info->net_sel_mode, info->net_type, info->plmn);
4864 if(req_net_sel_mode_set(info, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON) {
4865 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4866 err = MBTK_INFO_ERR_CME + cme_err;
4867 } else {
4868 err = MBTK_INFO_ERR_UNKNOWN;
4869 }
4870 LOG("Get Net select mode fail.");
4871 } else {
4872 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_SEL_MODE_RSP, NULL, 0);
4873 }
4874 }
4875 }
4876 break;
4877 }
4878 case MBTK_INFO_ID_NET_AVAILABLE_REQ:// sel_mode(uint8)type(uint8)plmn(uint32)...sel_mode(uint8)type(uint8)plmn(uint32)
4879 {
4880 if(pack->data_len == 0 || pack->data == NULL) // Get Available Net.
4881 {
4882 int buffer_size;
4883 uint8 buffer[SOCK_MSG_LEN_MAX];
4884 memset(buffer, 0, SOCK_MSG_LEN_MAX);
4885 if((buffer_size = req_available_net_get(buffer, &cme_err)) <= 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4886 {
4887 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4888 err = MBTK_INFO_ERR_CME + cme_err;
4889 } else {
4890 err = MBTK_INFO_ERR_UNKNOWN;
4891 }
4892 LOG("Get Available Net fail.");
4893 }
4894 else
4895 {
4896 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_AVAILABLE_RSP, buffer, buffer_size);
4897 }
4898 }
4899 else // Set Available Net(Unsupport).
4900 {
4901 err = MBTK_INFO_ERR_UNSUPPORTED;
4902 LOG("Unsupport set available net.");
4903 }
4904 break;
4905 }
4906 case MBTK_INFO_ID_NET_BAND_REQ:
4907 {
4908 if(pack->data_len == 0 || pack->data == NULL)
4909 {
4910 err = MBTK_INFO_ERR_REQ_PARAMETER;
4911 LOG("No data found.");
4912 }
4913 else // Get support/current bands.
4914 {
4915 if(pack->data_len == sizeof(uint8)) {
4916 if(*(pack->data)) { // Get current bands.
4917 mbtk_band_info_t band;
b.liu288093c2024-05-09 17:02:57 +08004918 memset(&band, 0x0, sizeof(mbtk_band_info_t));
liubin281ac462023-07-19 14:22:54 +08004919 if(req_band_get(&band, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4920 {
4921 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4922 err = MBTK_INFO_ERR_CME + cme_err;
4923 } else {
4924 err = MBTK_INFO_ERR_UNKNOWN;
4925 }
4926 LOG("Get net band fail.");
4927 }
4928 else
4929 {
4930 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_BAND_RSP, &band, sizeof(mbtk_band_info_t));
4931 }
4932 } else { // Get support bands.
4933 band_support_get();
4934 if(band_support.net_pref != 0)
4935 {
4936 err = MBTK_INFO_ERR_UNKNOWN;
4937 LOG("Get support bands fail.");
4938 }
4939 else
4940 {
4941 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_BAND_RSP, &band_support, sizeof(mbtk_band_info_t));
4942 }
4943 }
4944 } else { // Set current bands.
4945 mbtk_band_info_t* band = (mbtk_band_info_t*)pack->data;
4946 if(pack->data_len != sizeof(mbtk_band_info_t))
4947 {
4948 err = MBTK_INFO_ERR_REQ_PARAMETER;
4949 LOG("Set net band error.");
4950 break;
4951 }
4952
4953 if(req_band_set(band, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4954 {
4955 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4956 err = MBTK_INFO_ERR_CME + cme_err;
4957 } else {
4958 err = MBTK_INFO_ERR_UNKNOWN;
4959 }
4960 LOG("Set net band fail.");
4961 }
4962 else
4963 {
4964 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_BAND_RSP, NULL, 0);
4965 }
4966 }
4967 }
4968 break;
4969 }
4970 case MBTK_INFO_ID_NET_CELL_REQ: // mbtk_cell_info_t[]
4971 {
4972 if(pack->data_len == 0 || pack->data == NULL) // Get net cell.
4973 {
4974 if(req_cell_info_get(&cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4975 {
4976 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4977 err = MBTK_INFO_ERR_CME + cme_err;
4978 } else {
4979 err = MBTK_INFO_ERR_UNKNOWN;
4980 }
4981 LOG("Get net cell fail.");
4982 }
4983 else
4984 {
4985 LOG("req_cell_info_get() success,cell number: %d", cell_info.cell_num);
4986 //sleep(1);
4987 // mbtK_cell_pack_info_t
4988 if(cell_info.cell_num > 0 && cell_info.cell_num <= CELL_NUM_MAX && cell_info.type >= 0 && cell_info.type <= 2) {
4989 uint8 *data = (uint8*)malloc(sizeof(uint8) + sizeof(mbtk_cell_info_t) * cell_info.cell_num);
4990 if(data == NULL){
4991 err = MBTK_INFO_ERR_MEMORY;
4992 LOG("Get net cell fail.");
4993 } else {
4994 *data = cell_info.type; // Set network type.
4995 // Copy cell info item.
4996 #if 0
4997 int i = 0;
4998 while(i < cell_info.cell_num) {
4999 memcpy(data + sizeof(uint8) + sizeof(mbtk_cell_info_t) * i,
5000 &(cell_info.cell[i]),
5001 sizeof(mbtk_cell_info_t));
5002 i++;
5003 }
5004 #else
5005 memcpy(data + sizeof(uint8),
5006 &(cell_info.cell),
5007 sizeof(mbtk_cell_info_t) * cell_info.cell_num);
5008 #endif
5009 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_CELL_RSP, data, sizeof(uint8) + sizeof(mbtk_cell_info_t) * cell_info.cell_num);
5010 free(data);
5011 }
5012 } else {
5013 err = MBTK_INFO_ERR_UNKNOWN;
5014 LOG("Get net cell fail.");
5015 }
5016 }
5017 }
5018 else // Lock cell
5019 {
5020 char *mem = (char*)(pack->data);
5021 int len = pack->data_len;
5022 char reg[100] = {0};
5023 printf("mem:%s, len:%d", pack->data, pack->data_len);
5024
5025 if(req_cell_info_set(mem, reg, len, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5026 {
5027 // printf("cpms_set fail\n");
5028 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5029 err = MBTK_INFO_ERR_CME + cme_err;
5030 } else {
5031 err = MBTK_INFO_ERR_UNKNOWN;
5032 }
5033 // LOG("Set req_cell_info_set fail.");
5034 }
5035 else
5036 {
5037
5038 printf("req_cell_info_set success, reg:%s\n", reg);
5039 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_CELL_RSP, reg, strlen(reg));
5040
5041 // Restart is required to take effect.
5042 LOG("Will reboot system...");
5043 }
5044 }
5045 break;
5046 }
5047 case MBTK_INFO_ID_NET_RADIO_REQ: // <uint8> 0:OFF 1:ON
5048 {
5049 if(pack->data_len == 0 || pack->data == NULL) // Get
5050 {
5051 uint8 radio_on = (uint8)isRadioOn();
5052 if(radio_on < 0)
5053 {
5054 err = MBTK_INFO_ERR_UNKNOWN;
5055 LOG("Get radio state fail.");
5056 }
5057 else
5058 {
5059 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_RADIO_RSP, &radio_on, sizeof(uint8));
5060 }
5061 }
5062 else // Set
5063 {
yq.wangd58f71e2024-08-21 23:45:31 -07005064 at_cfun_command = true;
liubin281ac462023-07-19 14:22:54 +08005065 uint8 radio_on = *(pack->data);
5066 if(radio_on != 0 && radio_on != 1)
5067 {
5068 err = MBTK_INFO_ERR_REQ_PARAMETER;
5069 LOG("Set radio state fail.");
5070 }
5071 else
5072 {
5073 setRadioPower(radio_on);
5074 if((radio_on && net_info.radio_state == MBTK_RADIO_STATE_ON)
5075 || (!radio_on && net_info.radio_state == MBTK_RADIO_STATE_OFF)) {
5076 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_RADIO_RSP, NULL, 0);
5077 } else {
5078 err = MBTK_INFO_ERR_UNKNOWN;
5079 LOG("Set radio state fail.");
5080 }
5081 }
yq.wangd58f71e2024-08-21 23:45:31 -07005082 at_cfun_command = false;
liubin281ac462023-07-19 14:22:54 +08005083 }
5084 break;
5085 }
5086 case MBTK_INFO_ID_NET_SIGNAL_REQ: // mbtk_signal_info_t
5087 {
5088 if(pack->data_len == 0 || pack->data == NULL) // Get net signal.
5089 {
5090 mbtk_signal_info_t signal;
5091 memset(&signal, 0, sizeof(mbtk_signal_info_t));
5092 if(req_net_signal_get(&signal, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5093 {
5094 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5095 err = MBTK_INFO_ERR_CME + cme_err;
5096 } else {
5097 err = MBTK_INFO_ERR_UNKNOWN;
5098 }
5099 LOG("Get net signal fail.");
5100 }
5101 else
5102 {
5103 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_SIGNAL_RSP, &signal, sizeof(mbtk_signal_info_t));
5104 }
5105 }
5106 else // Set
5107 {
5108 err = MBTK_INFO_ERR_UNSUPPORTED;
5109 LOG("Set net signal fail.");
5110 }
5111 break;
5112 }
5113 case MBTK_INFO_ID_NET_REG_REQ: // mbtk_net_reg_info_t
5114 {
5115 if(pack->data_len == 0 || pack->data == NULL) // Get net reg.
5116 {
5117 mbtk_net_reg_info_t reg;
5118 memset(&reg, 0, sizeof(mbtk_net_reg_info_t));
5119 if(req_net_reg_get(&reg, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5120 {
5121 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5122 err = MBTK_INFO_ERR_CME + cme_err;
5123 } else {
5124 err = MBTK_INFO_ERR_UNKNOWN;
5125 }
5126 LOG("Get net reg fail.");
5127 }
5128 else
5129 {
5130 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_REG_RSP, &reg, sizeof(mbtk_net_reg_info_t));
5131 }
5132 }
5133 else // Set
5134 {
5135 err = MBTK_INFO_ERR_UNSUPPORTED;
5136 LOG("Set net reg fail.");
5137 }
5138 break;
5139 }
5140 case MBTK_INFO_ID_NET_APN_REQ: // mbtk_apn_info_t
5141 {
5142 if(pack->data_len == 0 || pack->data == NULL) // Get APN
5143 {
5144 uint8 buff[SOCK_MSG_LEN_MAX];
5145 memset(buff, 0, SOCK_MSG_LEN_MAX);
5146 int data_len = 0;
5147 if(req_apn_get(buff, &data_len, &cme_err) || data_len <= 0 || cme_err != MBTK_INFO_ERR_CME_NON)
5148 {
5149 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5150 err = MBTK_INFO_ERR_CME + cme_err;
5151 } else {
5152 err = MBTK_INFO_ERR_UNKNOWN;
5153 }
5154 LOG("Get APN fail.");
5155 }
5156 else
5157 {
5158 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_APN_RSP, buff, data_len);
5159 }
5160 }
5161 else // Set
5162 {
5163 // <cid[1]><ip_type[1]><apn_len[2]><apn><user_len[2]><user><pass_len[2]><pass><auth_len[2]><auth>
5164 const uint8* ptr = pack->data;
5165 mbtk_apn_info_t apn;
5166 int len;
5167 memset(&apn, 0, sizeof(mbtk_apn_info_t));
5168 // cid
5169 apn.cid = *ptr++;
5170
5171 // ip_type
5172 apn.ip_type = (mbtk_ip_type_enum)(*ptr++);
5173
5174 // apn
5175 len = byte_2_uint16(ptr, false);
5176 ptr += sizeof(uint16);
5177 if(len > 0) {
5178 memcpy(apn.apn, ptr, len);
5179 ptr += len;
5180 }
5181
5182 // user
5183 len = byte_2_uint16(ptr, false);
5184 ptr += sizeof(uint16);
5185 if(len > 0) {
5186 memcpy(apn.user, ptr, len);
5187 ptr += len;
5188 }
5189
5190 // pass
5191 len = byte_2_uint16(ptr, false);
5192 ptr += sizeof(uint16);
5193 if(len > 0) {
5194 memcpy(apn.pass, ptr, len);
5195 ptr += len;
5196 }
5197
5198 // auth
5199 len = byte_2_uint16(ptr, false);
5200 ptr += sizeof(uint16);
5201 if(len > 0) {
5202 memcpy(apn.auth, ptr, len);
5203 ptr += len;
5204 }
5205
b.liu9e8584b2024-11-06 19:21:28 +08005206 LOGD("APN : %d, %d, %s, %s, %s, %s", apn.cid, apn.ip_type, str_empty(apn.apn) ? "NULL" : (char*)apn.apn,
5207 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 +08005208 if(req_apn_set(&apn, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5209 {
5210 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5211 err = MBTK_INFO_ERR_CME + cme_err;
5212 } else {
5213 err = MBTK_INFO_ERR_UNKNOWN;
5214 }
5215 LOG("Set APN fail.");
5216 }
5217 else
5218 {
5219 // Save apn.
wangyouqianged88c722023-11-22 16:33:43 +08005220#ifdef MBTK_AF_SUPPORT
5221 if(apn.cid == 1)
5222 {
5223 default_iptype = apn.ip_type;
5224 }
5225#endif
liubin281ac462023-07-19 14:22:54 +08005226 apn_prop_set(&apn);
5227
5228 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_APN_RSP, NULL, 0);
5229 }
5230 }
5231 break;
5232 }
wangyouqiang80487e42024-05-24 15:06:20 +08005233 case MBTK_INFO_ID_NET_QSER_APN_REQ:
5234 {
5235 if(pack->data_len == 0 || pack->data == NULL)
5236 {
5237 uint8 buff[SOCK_MSG_LEN_MAX];
5238 memset(buff, 0, SOCK_MSG_LEN_MAX);
5239 int data_len = 0;
5240 if(mbtk_qser_req_apn_get(buff, &data_len, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5241 {
5242 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5243 err = MBTK_INFO_ERR_CME + cme_err;
5244 } else {
5245 err = MBTK_INFO_ERR_UNKNOWN;
5246 }
5247 LOGE("Get APN fail.");
5248 }
5249 else
5250 {
5251 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_APN_RSP, buff, data_len);
5252 }
5253 }
5254 else
5255 {
b.liu9e8584b2024-11-06 19:21:28 +08005256// const uint8* ptr = pack->data;
wangyouqiang80487e42024-05-24 15:06:20 +08005257 mbtk_apn_info_t apn;
5258 mbtk_apn_req_type_enum req_type = MBTK_APN_REQ_TYPE_SET;
5259 uint8 apn_type[MBTK_QSER_APN_NAME_SIZE] = {0};
5260 int ret = mbtk_strdata_to_apn(pack->data, &apn, apn_type, &req_type);
5261 if(ret < 0)
5262 {
5263 LOGE("mbtk_strdata_to_apn fail. ret = [%d]", ret);
5264 err = MBTK_INFO_ERR_REQ_PARAMETER;
5265 }
5266 else
5267 {
5268 if(req_apn_set(&apn, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5269 {
5270 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5271 err = MBTK_INFO_ERR_CME + cme_err;
5272 } else {
5273 err = MBTK_INFO_ERR_UNKNOWN;
5274 }
5275 LOGE("Set APN fail.");
5276 }
5277 else
5278 {
5279 // Save apn.
5280 apn_prop_set(&apn);
5281 mbtk_qser_apn_save(apn, apn_type, true);
5282 if(req_type == MBTK_APN_REQ_TYPE_ADD)
5283 {
5284 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_QSER_APN_RSP, (void *)&apn.cid, 1);
5285 }
5286 else
5287 {
5288 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_QSER_APN_RSP, NULL, 0);
5289 }
5290 }
5291 }
5292 }
5293
5294 break;
5295 }
b.liu288093c2024-05-09 17:02:57 +08005296 case MBTK_INFO_ID_NET_APN_DEL_REQ:
liuyang0e49d9a2024-04-23 21:04:54 +08005297 {
b.liu288093c2024-05-09 17:02:57 +08005298 if(pack->data_len == 0 || pack->data == NULL)
liuyang0e49d9a2024-04-23 21:04:54 +08005299 {
5300 err = MBTK_INFO_ERR_UNSUPPORTED;
5301 LOG("Unsupported delete apn .");
5302 }
b.liu288093c2024-05-09 17:02:57 +08005303 else
liuyang0e49d9a2024-04-23 21:04:54 +08005304 {
liuyang1cefd852024-04-24 18:30:53 +08005305 int profile = pack->data[0];
wangyouqiang80487e42024-05-24 15:06:20 +08005306 if(cid_active[profile] == 1)
liuyang0e49d9a2024-04-23 21:04:54 +08005307 {
wangyouqiang80487e42024-05-24 15:06:20 +08005308 LOGD("cid pdp already open.");
5309 err = MBTK_INFO_ERR_CID_EXIST;
wangyouqiang13e98402024-05-24 16:07:43 +08005310 break;
wangyouqiang80487e42024-05-24 15:06:20 +08005311 }
5312 if(mbtk_qser_apn_del(profile) < 0)
5313 {
5314 LOGD("del error.");
5315 err = MBTK_INFO_ERR_REQ_PARAMETER;
liuyang0e49d9a2024-04-23 21:04:54 +08005316 }
5317 else
5318 {
wangyouqiang80487e42024-05-24 15:06:20 +08005319 if(req_apn_del(profile, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5320 {
5321 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5322 err = MBTK_INFO_ERR_CME + cme_err;
5323 } else {
5324 err = MBTK_INFO_ERR_UNKNOWN;
5325 }
5326 LOG("Delete apn fail.");
5327 }
5328 else
5329 {
5330 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_APN_DEL_RSP, NULL, 0);
5331 }
liuyang0e49d9a2024-04-23 21:04:54 +08005332 }
5333 }
5334 break;
5335 }
liubin281ac462023-07-19 14:22:54 +08005336 case MBTK_INFO_ID_NET_DATA_CALL_REQ:
5337 {
5338 if(pack->data_len == 0 || pack->data == NULL)
5339 {
5340 err = MBTK_INFO_ERR_UNSUPPORTED;
5341 }
5342 else
5343 {
5344 /* <call_type[1]><cid[1]><auto_conn_interval[1]><boot_conn[1]><timeout[1]>
5345 call_type : mbtk_data_call_type_enum
5346 cid : 2 - 7
5347 timeout : second
5348 */
5349 mbtk_data_call_type_enum call_type = (mbtk_data_call_type_enum)pack->data[0];
5350 int cid = pack->data[1];
5351 int reconn = 0;
5352
wangyouqiang80487e42024-05-24 15:06:20 +08005353#if 0
liubin281ac462023-07-19 14:22:54 +08005354 if(cid < MBTK_APN_CID_MIN || cid > MBTK_APN_CID_MAX) {
5355 err = MBTK_INFO_ERR_CID;
5356 break;
5357 }
wangyouqiang80487e42024-05-24 15:06:20 +08005358#endif
5359 if(mbtk_check_cid(cid) < 0)
5360 {
5361 err = MBTK_INFO_ERR_CID;
5362 break;
5363 }
liubin281ac462023-07-19 14:22:54 +08005364
5365 LOG("cid_active[%d] = %d", cid, cid_active[cid]);
5366 memset(&cgact_wait, 0, sizeof(info_cgact_wait_t));
5367 switch(call_type) {
5368 case MBTK_DATA_CALL_START: {
5369 //mbtk wyq for data_call_ex add start
5370 int auto_conn_interval = pack->data[2];
5371 int boot_conn = pack->data[3];
5372 int timeout = pack->data[4];
5373 data_call_bootconn_save(cid, boot_conn);
b.liufe320632024-01-17 20:38:08 +08005374
wangyouqiang13e98402024-05-24 16:07:43 +08005375 mbtk_signal_info_t signal;
5376 memset(&signal, 0xFF, sizeof(mbtk_signal_info_t));
5377 req_net_signal_get(&signal, NULL);
b.liufe320632024-01-17 20:38:08 +08005378
liubin281ac462023-07-19 14:22:54 +08005379 if(cid_active[cid] == 1)
5380 {
5381 err = MBTK_INFO_ERR_CID_EXIST;
5382 break;
5383 }
b.liufe320632024-01-17 20:38:08 +08005384
wangyouqiang80487e42024-05-24 15:06:20 +08005385 if(mbtk_check_default_pdp_state(cid))
5386 {
5387 err = MBTK_INFO_ERR_UNSUPPORTED;
5388 break;
5389 }
5390
liubin281ac462023-07-19 14:22:54 +08005391 data_call_reconn:
5392 //mbtk wyq for data_call_ex add end
5393 cgact_wait.waitting = true;
5394 cgact_wait.cid = cid;
5395 cgact_wait.act = true;
5396 if(req_data_call_start(cid, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5397 {
5398 //mbtk wyq for data_call_ex add start
5399 if(reconn < 5 && auto_conn_interval > 0)
5400 {
5401 sleep(auto_conn_interval);
5402 reconn++;
5403 cme_err = MBTK_INFO_ERR_CME_NON;
5404 LOG("data_call restart call.");
5405 goto data_call_reconn;
5406 }
5407 //mbtk wyq for data_call_ex add end
5408 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5409 err = MBTK_INFO_ERR_CME + cme_err;
5410 } else {
5411 err = MBTK_INFO_ERR_UNKNOWN;
5412 }
5413 LOG("%d active fail.", cid);
5414 }
5415 else
5416 {
5417 // Wait for "CONNECT" or "+CGEV:"
5418 if(wait_cgact_complete(timeout)) { // Timeout
5419 err = MBTK_INFO_ERR_TIMEOUT;
5420 break;
5421 }
5422
5423 // Get IP information.
5424 mbtk_ipv4_info_t ipv4;
5425 mbtk_ipv6_info_t ipv6;
5426 memset(&ipv4, 0, sizeof(mbtk_ipv4_info_t));
5427 memset(&ipv6, 0, sizeof(mbtk_ipv6_info_t));
5428 if(req_data_call_state_get(cid, &ipv4, &ipv6, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5429 {
5430 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5431 err = MBTK_INFO_ERR_CME + cme_err;
5432 } else {
5433 err = MBTK_INFO_ERR_UNKNOWN;
5434 }
5435 LOG("Get %d state fail.", cid);
5436 }
5437 else
5438 {
5439 // Config IPv4 address.
wangyouqianged88c722023-11-22 16:33:43 +08005440#ifdef MBTK_AF_SUPPORT
5441 if(cid == 1)
5442 {
5443 //uint8 pdp_data = cid;
5444 //pack_rsp_send(cli_info->fd , MBTK_INFO_ID_IND_PDP_STATE_CHANGE, &pdp_data, sizeof(uint8));
5445 ipv4.valid = false;
5446 ipv6.valid = false;
5447 if(default_iptype == MBTK_IP_TYPE_IP)
5448 {
5449 ipv4.valid = true;
5450 }
5451 else if(default_iptype == MBTK_IP_TYPE_IPV6)
5452 {
5453 ipv6.valid = true;
5454 }
5455 else
5456 {
5457 ipv4.valid = true;
5458 ipv6.valid = true;
5459 }
5460 }
5461#endif
liubin281ac462023-07-19 14:22:54 +08005462#if 1
5463 if(ipv4.valid) {
5464 char dev[20] = {0};
5465 sprintf(dev, "ccinet%d", cid - 1);
5466
5467 char ip[20] = {0};
5468 char gateway[20] = {0};
5469 char *gateway_ptr = NULL;
5470 char netmask[20] = {0};
5471 char *netmask_ptr = NULL;
5472 if(inet_ntop(AF_INET, &(ipv4.IPAddr), ip, 20) == NULL) {
5473 err = MBTK_INFO_ERR_UNKNOWN;
5474 LOGE("inet_ntop ipv4 ip fail.");
5475 log_hex("IPv4", &(ipv4.IPAddr), 4);
5476 break;
5477 }
5478
5479 if(ipv4.GateWay) {
5480 if(inet_ntop(AF_INET, &(ipv4.GateWay), gateway, 20) == NULL) {
5481 err = MBTK_INFO_ERR_UNKNOWN;
5482 LOGE("inet_ntop ipv4 gateway fail.");
5483 log_hex("IPv4", &(ipv4.IPAddr), 4);
5484 break;
5485 } else {
5486 gateway_ptr = gateway;
5487 }
5488 }
5489
5490 if(ipv4.NetMask) {
5491 if(inet_ntop(AF_INET, &(ipv4.NetMask), netmask, 20) == NULL) {
5492 err = MBTK_INFO_ERR_UNKNOWN;
5493 LOGE("inet_ntop ipv4 netmask fail.");
5494 log_hex("IPv4", &(ipv4.IPAddr), 4);
5495 break;
5496 } else {
5497 netmask_ptr = netmask;
5498 }
5499 }
5500
5501 if(netmask_ptr == NULL) {
5502 netmask_ptr = netmask;
5503 memcpy(netmask_ptr, "255.255.255.0", strlen("255.255.255.0"));
5504 }
5505
5506 if(mbtk_ifc_configure2(dev, ip, 0, gateway_ptr, netmask_ptr)) {
5507 LOGD("Config %s IPv4 %s fail.", dev, ip);
5508 } else {
5509 LOGD("Config %s IPv4 %s success.", dev, ip);
5510 }
5511
5512 }
5513#endif
5514 // Config IPv6 address.
5515 if(ipv6.valid) {
5516 char ip[50] = {0};
5517 char dev[20] = {0};
5518 sprintf(dev, "ccinet%d", cid - 1);
5519
5520 if(inet_ntop(AF_INET6, &(ipv6.IPV6Addr), ip, 50) == NULL) {
5521 err = MBTK_INFO_ERR_UNKNOWN;
5522 LOGE("inet_ntop ipv6 ip fail.");
5523 log_hex("IPv6", &(ipv6.IPV6Addr), 16);
5524 break;
5525 }
5526
5527 if(mbtk_ipv6_config(dev, ip, 64)) {
5528 LOGD("Config %s IPv6 %s fail.", dev, ip);
5529 } else {
5530 LOGD("Config %s IPv6 %s success.", dev, ip);
5531 }
5532 }
5533
5534 cid_active[cid] = 1;
wangyouqiang80487e42024-05-24 15:06:20 +08005535 mbtk_set_default_pdp_state(true, cid);
5536 mbtk_qser_route_config(cid, &ipv4, &ipv6);
liubin281ac462023-07-19 14:22:54 +08005537 if(cli_info->fd != DATA_CALL_BOOTCONN_FD)
5538 {
liuyange134d842024-06-27 17:34:02 +08005539 mbtk_net_led_set(MBTK_NET_LED_DATA_CONNECT);
liubin281ac462023-07-19 14:22:54 +08005540 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_DATA_CALL_RSP, NULL, 0);
5541 }
5542 else
5543 {
5544 free(pack->data);
5545 free(cli_info);
5546 LOG("data_call bootconn success.");
5547 }
5548 }
5549 }
5550 break;
5551 }
5552 case MBTK_DATA_CALL_STOP: {
5553 //mbtk wyq for data_call_ex add start
5554 if(cid_active[cid] == 0)
5555 {
5556 err = MBTK_INFO_ERR_CID_NO_EXIST;
5557 break;
5558 }
5559
5560 int timeout = pack->data[2];
5561 //mbtk wyq for data_call_ex add end
b.liuf37bd332024-03-18 13:51:24 +08005562#if (defined(MBTK_AF_SUPPORT) || defined(MBTK_ALL_CID_SUPPORT))
wangyouqianged88c722023-11-22 16:33:43 +08005563 if(cid == 1)
5564 {
5565 char dev[20] = {0};
5566 uint8 pdp_data = cid + 100;
5567 pack_rsp_send(cli_info->fd , MBTK_INFO_ID_IND_PDP_STATE_CHANGE, &pdp_data, sizeof(uint8));
b.liufe320632024-01-17 20:38:08 +08005568
wangyouqianged88c722023-11-22 16:33:43 +08005569 sprintf(dev, "ccinet%d", cid - 1);
5570
5571 // Config network.
5572 if(mbtk_ifc_configure2(dev, NULL, 0, NULL, NULL)) {
5573 LOGD("Config %s IPv4 0 fail.", dev);
5574 } else {
5575 LOGD("Config %s IPv4 0 success.", dev);
5576 }
5577 cid_active[cid] = 0;
wangyouqiang80487e42024-05-24 15:06:20 +08005578 mbtk_set_default_pdp_state(false, cid);
liuyange134d842024-06-27 17:34:02 +08005579 mbtk_net_led_set(MBTK_NET_LED_NET_CONNECT);
wangyouqianged88c722023-11-22 16:33:43 +08005580 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_DATA_CALL_RSP, NULL, 0);
5581 break;
5582 }
5583#endif
liubin281ac462023-07-19 14:22:54 +08005584 cgact_wait.waitting = true;
5585 cgact_wait.cid = cid;
5586 cgact_wait.act = false;
5587 if(req_data_call_stop(cid, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5588 {
5589 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5590 err = MBTK_INFO_ERR_CME + cme_err;
5591 } else {
5592 err = MBTK_INFO_ERR_UNKNOWN;
5593 }
5594 LOG("%d deactive fail.", cid);
5595 }
5596 else
5597 {
5598 // Wait for "CONNECT" or "+CGEV:"
5599 if(wait_cgact_complete(timeout)) { // Timeout
5600 err = MBTK_INFO_ERR_TIMEOUT;
5601 break;
5602 }
5603 char dev[20] = {0};
5604 sprintf(dev, "ccinet%d", cid - 1);
5605
5606 // Config network.
5607 if(mbtk_ifc_configure2(dev, NULL, 0, NULL, NULL)) {
5608 LOGD("Config %s IPv4 0 fail.", dev);
5609 } else {
5610 LOGD("Config %s IPv4 0 success.", dev);
5611 }
5612
5613#if 0
5614 if(mbtk_ipv6_config(dev, NULL, 64)) {
5615 LOGD("Config %s IPv6 0 fail.", dev);
5616 } else {
5617 LOGD("Config %s IPv6 0 success.", dev);
5618 }
5619#endif
5620 cid_active[cid] = 0;
wangyouqiang80487e42024-05-24 15:06:20 +08005621 mbtk_set_default_pdp_state(false, cid);
liuyange134d842024-06-27 17:34:02 +08005622 mbtk_net_led_set(MBTK_NET_LED_NET_CONNECT);
liubin281ac462023-07-19 14:22:54 +08005623 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_DATA_CALL_RSP, NULL, 0);
5624 }
5625 break;
5626 }
5627 case MBTK_DATA_CALL_STATE: {
wangyouqianged88c722023-11-22 16:33:43 +08005628 if(cid_active[cid] == 0)
5629 {
5630 err = MBTK_INFO_ERR_CID_NO_EXIST;
5631 break;
5632 }
liubin281ac462023-07-19 14:22:54 +08005633 mbtk_ipv4_info_t ipv4;
5634 mbtk_ipv6_info_t ipv6;
5635 memset(&ipv4, 0, sizeof(mbtk_ipv4_info_t));
5636 memset(&ipv6, 0, sizeof(mbtk_ipv6_info_t));
5637 if(req_data_call_state_get(cid, &ipv4, &ipv6, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5638 {
5639 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5640 err = MBTK_INFO_ERR_CME + cme_err;
5641 } else {
5642 err = MBTK_INFO_ERR_UNKNOWN;
5643 }
5644 LOG("Get %d state fail.", cid);
5645 }
5646 else
5647 {
5648 uint8 buff[SOCK_MSG_LEN_MAX] = {0};
5649 int buff_len = 0;
wangyouqianged88c722023-11-22 16:33:43 +08005650#ifdef MBTK_AF_SUPPORT
5651 if(cid == 1)
5652 {
5653 ipv4.valid = false;
5654 ipv6.valid = false;
5655 if(default_iptype == MBTK_IP_TYPE_IP)
5656 {
5657 ipv4.valid = true;
5658 }
5659 else if(default_iptype == MBTK_IP_TYPE_IPV6)
5660 {
5661 ipv6.valid = true;
5662 }
5663 else
5664 {
5665 ipv4.valid = true;
5666 ipv6.valid = true;
5667 }
5668 }
5669#endif
liubin281ac462023-07-19 14:22:54 +08005670 if(ipv4.valid && ipv6.valid) {
5671 buff[0] = (uint8)2;
5672 buff_len++;
5673
5674 memcpy(buff + buff_len, &ipv4, sizeof(mbtk_ipv4_info_t));
5675 buff_len += sizeof(mbtk_ipv4_info_t);
5676 memcpy(buff + buff_len, &ipv6, sizeof(mbtk_ipv6_info_t));
5677 buff_len += sizeof(mbtk_ipv6_info_t);
5678 } else if(ipv4.valid) {
5679 buff[0] = (uint8)0;
5680 buff_len++;
5681
5682 memcpy(buff + buff_len, &ipv4, sizeof(mbtk_ipv4_info_t));
5683 buff_len += sizeof(mbtk_ipv4_info_t);
5684 } else if(ipv6.valid) {
5685 buff[0] = (uint8)1;
5686 buff_len++;
5687
5688 memcpy(buff + buff_len, &ipv6, sizeof(mbtk_ipv6_info_t));
5689 buff_len += sizeof(mbtk_ipv6_info_t);
5690 } else {
5691 LOGE("Get IPv4/IPv6 fail.");
5692 err = MBTK_INFO_ERR_UNKNOWN;
5693 break;
5694 }
5695 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_DATA_CALL_RSP, buff, buff_len);
5696 }
5697 break;
5698 }
5699 default: {
5700 err = MBTK_INFO_ERR_FORMAT;
5701 break;
5702 }
5703 }
5704 }
5705 break;
5706 }
r.xiao06db9a12024-04-14 18:51:15 -07005707 case MBTK_INFO_ID_NET_IMS_REQ:
5708 {
5709 if(pack->data_len == 0 || pack->data == NULL) //Get
5710 {
5711 int reg = -1;
5712 if(net_ims_get(&reg, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5713 {
5714 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5715 err = MBTK_INFO_ERR_CME + cme_err;
5716 } else {
5717 err = MBTK_INFO_ERR_UNKNOWN;
5718 }
5719 LOG("Get net ims fail.");
5720 }
5721 else
5722 {
5723 uint8 reg_type = (uint8)reg;
5724 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_IMS_RSP, &reg_type, sizeof(uint8));
5725 }
5726 }
5727 else
5728 {
5729 uint8 ims = *(pack->data);
5730
5731 if(net_ims_set(ims, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5732 {
5733 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5734 err = MBTK_INFO_ERR_CME + cme_err;
5735 } else {
5736 err = MBTK_INFO_ERR_UNKNOWN;
5737 }
5738 LOG("Set net ims fail.");
5739 }
5740 else
5741 {
5742 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_IMS_RSP, NULL, 0);
5743 }
5744 }
5745 break;
5746 }
b.liufdf03172024-06-07 15:01:29 +08005747 case MBTK_INFO_ID_NET_IMS_REG_STATE_REQ:
5748 {
5749 if(pack->data_len == 0 || pack->data == NULL) //Get
5750 {
5751 int reg = -1;
5752 if(net_ims_reg_state_get(&reg, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5753 {
5754 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5755 err = MBTK_INFO_ERR_CME + cme_err;
5756 } else {
5757 err = MBTK_INFO_ERR_UNKNOWN;
5758 }
5759 LOG("Get net ims fail.");
5760 }
5761 else
5762 {
5763 uint8 reg_type = (uint8)reg;
5764 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_IMS_REG_STATE_RSP, &reg_type, sizeof(uint8));
5765 }
5766 }
5767 else
5768 {
5769 err = MBTK_INFO_ERR_UNSUPPORTED;
5770 }
5771 break;
5772 }
r.xiaoec113d12024-01-12 02:13:28 -08005773 case MBTK_INFO_ID_WAKEUP_STA_REQ:
5774 {
5775 if(pack->data_len == 0 || pack->data == NULL)
5776 {
5777 err = MBTK_INFO_ERR_UNSUPPORTED;
5778 LOG("Get POWERIND state UNSUPPORTED.");
5779 }
5780 else // Set powerind state.
5781 {
5782 uint32 state = *(pack->data);
5783 if(req_powerind_set(state, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5784 {
5785 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5786 err = MBTK_INFO_ERR_CME + cme_err;
5787 } else {
5788 err = MBTK_INFO_ERR_UNKNOWN;
5789 }
5790 LOG("Set POWERIND state fail.");
5791 }
5792 else
5793 {
5794 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_WAKEUP_STA_RSP, NULL, 0);
5795 }
5796 }
5797 break;
5798 }
5799 case MBTK_INFO_ID_OOS_STA_REQ:
5800 {
5801 if(pack->data_len == 0 || pack->data == NULL)
5802 {
5803 mbtk_oos_info oos_t;
5804 if(req_oos_get(&oos_t, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5805 {
5806 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5807 err = MBTK_INFO_ERR_CME + cme_err;
5808 } else {
5809 err = MBTK_INFO_ERR_UNKNOWN;
5810 }
5811 LOG("Get SMS OOS fail.");
r.xiaoec113d12024-01-12 02:13:28 -08005812 }
5813 else
5814 {
r.xiaoec113d12024-01-12 02:13:28 -08005815 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_OOS_STA_RSP, &oos_t, sizeof(mbtk_oos_info));
5816 }
5817 }
b.liufe320632024-01-17 20:38:08 +08005818 else // Set OOS
r.xiaoec113d12024-01-12 02:13:28 -08005819 {
r.xiaocfd7c682024-01-22 03:59:46 -08005820 if(pack->data_len != sizeof(mbtk_oos_info))
5821 {
5822 err = MBTK_INFO_ERR_REQ_PARAMETER;
5823 LOG("Set oos error.");
5824 break;
5825 }
5826
b.liu9e8584b2024-11-06 19:21:28 +08005827 mbtk_oos_info *state = (mbtk_oos_info *)pack->data;
r.xiaoec113d12024-01-12 02:13:28 -08005828 if(req_oos_set(state, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5829 {
5830 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5831 err = MBTK_INFO_ERR_CME + cme_err;
5832 } else {
5833 err = MBTK_INFO_ERR_UNKNOWN;
5834 }
5835 LOG("Set OOS fail.");
5836 }
5837 else
5838 {
5839 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_OOS_STA_RSP, NULL, 0);
5840 }
5841 }
5842 break;
5843 }
wangyouqiang38e53362024-01-23 10:53:48 +08005844 case MBTK_INFO_ID_LED_REQ:
5845 {
5846 if(pack->data_len == 0 || pack->data == NULL)
5847 {
5848 err = MBTK_INFO_ERR_UNSUPPORTED;
5849 LOGE("led param is error.");
5850 }
5851 else
5852 {
5853 char type = pack->data[0];
5854 char status = pack->data[1];
5855 LOGE("[set_led] = [%d], [status_led] = [%d].", type, status);
r.xiaoec113d12024-01-12 02:13:28 -08005856
wangyouqiang38e53362024-01-23 10:53:48 +08005857 if(type == MBTK_LED_TYPE_NET)
5858 {
5859 if(status == MBTK_LED_STATUS_CLOSE)
5860 {
5861 mbtk_net_led_set(MBTK_NET_LED_CLOSE);
5862 }
5863 else
5864 {
5865 mbtk_net_led_set(MBTK_NET_LED_OPEN);
5866 }
5867 }
5868 else
5869 {
5870 if(status == MBTK_LED_STATUS_CLOSE)
5871 {
5872 status_led_set(MBTK_STATUS_LED_CLOSE);
5873 }
5874 else
5875 {
5876 status_led_set(MBTK_STATUS_LED_OPEN);
5877 }
5878 }
5879 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_LED_RSP, NULL, 0);
5880 }
5881 break;
5882 }
liubin281ac462023-07-19 14:22:54 +08005883 default:
5884 {
5885 err = MBTK_INFO_ERR_REQ_UNKNOWN;
5886 LOG("Unknown request : %s", id2str(pack->info_id));
5887 break;
5888 }
5889 }
5890
5891 return err;
5892 }
5893}
5894
5895// Process AT URC data
5896static int send_pack_to_queue(sock_client_info_t* cli_info, void* pack)
5897{
5898 if(info_queue.count >= PACK_PROCESS_QUEUE_MAX)
5899 {
5900 LOG("Packet process queue is full");
5901 return -1;
5902 }
5903
5904 info_queue_item_t *item = (info_queue_item_t*)malloc(sizeof(info_queue_item_t));
5905 if(!item)
5906 {
5907 LOG("malloc() fail[%d].", errno);
5908 return -1;
5909 }
5910 item->cli_info = cli_info;
5911 item->pack = pack;
5912 mbtk_queue_put(&info_queue, item);
5913
5914 // If thread is waitting,continue it.
5915 if(1/*!is_running*/)
5916 {
5917 pthread_mutex_lock(&info_mutex);
5918 pthread_cond_signal(&info_cond);
5919 pthread_mutex_unlock(&info_mutex);
5920 }
5921 else
5922 {
5923 LOG("Packet process thread is process...");
5924 }
5925
5926 return 0;
5927}
5928
b.liu9e8584b2024-11-06 19:21:28 +08005929static void radio_state_change(const void *data, int data_len)
liubin281ac462023-07-19 14:22:54 +08005930{
5931 uint8 *data_ptr = (uint8*)data;
5932 if(data_ptr[0]) {
5933 net_info.radio_state = MBTK_RADIO_STATE_ON;
5934 } else {
5935 net_info.radio_state = MBTK_RADIO_STATE_OFF;
5936 }
5937
5938 sock_client_info_t *cli = NULL;
5939 list_first(sock_client_list);
5940 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
5941 {
5942 if(cli->ind_num > 0) {
5943 int i;
5944 for(i = 0; i < IND_REGISTER_MAX; i++) {
5945 // Registe MBTK_INFO_ID_IND_RADIO_STATE_CHANGE
5946 if(cli->ind_register[i] == MBTK_INFO_ID_IND_RADIO_STATE_CHANGE) {
5947 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_RADIO_STATE_CHANGE, data, data_len);
5948 break;
5949 }
5950 }
5951 }
5952 }
5953}
5954
b.liu9e8584b2024-11-06 19:21:28 +08005955static void pdp_state_change(const void *data, int data_len)
liubin281ac462023-07-19 14:22:54 +08005956{
5957 sock_client_info_t *cli = NULL;
5958 list_first(sock_client_list);
5959 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
5960 {
5961 if(cli->ind_num > 0) {
5962 int i;
5963 for(i = 0; i < IND_REGISTER_MAX; i++) {
5964 if(cli->ind_register[i] == MBTK_INFO_ID_IND_PDP_STATE_CHANGE) {
5965 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_PDP_STATE_CHANGE, data, data_len);
5966 break;
5967 }
5968 }
5969 }
5970 }
5971}
5972
b.liu9e8584b2024-11-06 19:21:28 +08005973static void net_state_change(const void *data, int data_len)
liubin281ac462023-07-19 14:22:54 +08005974{
5975 sock_client_info_t *cli = NULL;
5976 list_first(sock_client_list);
5977 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
5978 {
5979 if(cli->ind_num > 0) {
5980 int i;
5981 for(i = 0; i < IND_REGISTER_MAX; i++) {
5982 // Registe MBTK_INFO_ID_IND_NET_STATE_CHANGE
5983 if(cli->ind_register[i] == MBTK_INFO_ID_IND_NET_STATE_CHANGE) {
5984 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_NET_STATE_CHANGE, data, data_len);
5985 break;
5986 }
5987 }
5988 }
5989 }
5990}
5991
b.liu9e8584b2024-11-06 19:21:28 +08005992static void call_state_change(const void *data, int data_len)
liubin281ac462023-07-19 14:22:54 +08005993{
5994 sock_client_info_t *cli = NULL;
5995 list_first(sock_client_list);
5996 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
5997 {
5998 if(cli->ind_num > 0) {
5999 int i;
6000 for(i = 0; i < IND_REGISTER_MAX; i++) {
6001 // Registed MBTK_INFO_ID_IND_RADIO_STATE_CHANGE
6002 if(cli->ind_register[i] == MBTK_INFO_ID_IND_CALL_STATE_CHANGE) {
6003 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_CALL_STATE_CHANGE, data, data_len);
6004 break;
6005 }
6006 }
6007 }
6008 }
6009}
6010
b.liu9e8584b2024-11-06 19:21:28 +08006011static void sim_state_change(const void *data, int data_len)
liubin281ac462023-07-19 14:22:54 +08006012{
6013 sock_client_info_t *cli = NULL;
6014 list_first(sock_client_list);
6015 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
6016 {
6017 if(cli->ind_num > 0) {
6018 int i;
6019 for(i = 0; i < IND_REGISTER_MAX; i++) {
6020 // Registed MBTK_INFO_ID_IND_RADIO_STATE_CHANGE
6021 if(cli->ind_register[i] == MBTK_INFO_ID_IND_SIM_STATE_CHANGE) {
6022 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_SIM_STATE_CHANGE, data, data_len);
6023 break;
6024 }
6025 }
6026 }
6027 }
6028}
6029
b.liu9e8584b2024-11-06 19:21:28 +08006030static void sms_state_change(const void *data, int data_len)
liubin281ac462023-07-19 14:22:54 +08006031{
6032 sock_client_info_t *cli = NULL;
6033 list_first(sock_client_list);
6034 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
6035 {
6036 if(cli->ind_num > 0) {
6037 int i;
6038 for(i = 0; i < IND_REGISTER_MAX; i++) {
6039 // Registed MBTK_INFO_ID_IND_SMS_STATE_CHANGE
6040 if(cli->ind_register[i] == MBTK_INFO_ID_IND_SMS_STATE_CHANGE) {
6041 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_SMS_STATE_CHANGE, data, data_len);
6042 break;
6043 }
6044 }
6045 }
6046 }
6047}
6048
b.liu9e8584b2024-11-06 19:21:28 +08006049static void signal_state_change(const void *data, int data_len)
r.xiaofca7c472024-04-24 01:00:23 -07006050{
6051 sock_client_info_t *cli = NULL;
6052 list_first(sock_client_list);
6053 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
6054 {
6055 if(cli->ind_num > 0) {
6056 int i;
6057 for(i = 0; i < IND_REGISTER_MAX; i++) {
6058 // Registe MBTK_INFO_ID_IND_SIGNAL_STATE_CHANGE
6059 if(cli->ind_register[i] == MBTK_INFO_ID_IND_SIGNAL_STATE_CHANGE) {
6060 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_SIGNAL_STATE_CHANGE, data, data_len);
6061 break;
6062 }
6063 }
6064 }
6065 }
6066}
6067
6068
b.liu9e8584b2024-11-06 19:21:28 +08006069int urc_msg_distribute(bool async_process, info_urc_msg_id_enum msg, const void *data, int data_len)
liubin281ac462023-07-19 14:22:54 +08006070{
6071#if 0
6072 if(urc_queue.count >= PACK_PROCESS_QUEUE_MAX)
6073 {
6074 LOG("Packet process queue is full");
6075 return -1;
6076 }
6077
6078 info_urc_msg_t *urc = (info_urc_msg_t*)malloc(sizeof(info_urc_msg_t));
6079 if(!urc)
6080 {
6081 LOG("malloc() fail[%d].", errno);
6082 return -1;
6083 }
6084 urc->msg = msg;
6085 urc->data = memdup(data, data_len);
6086 urc->data_len = data_len;
6087
6088 mbtk_queue_put(&urc_queue, urc);
6089
6090 // If thread is waitting,continue it.
6091 if(1/*!is_running*/)
6092 {
6093 pthread_mutex_lock(&urc_mutex);
6094 pthread_cond_signal(&urc_cond);
6095 pthread_mutex_unlock(&urc_mutex);
6096 }
6097 else
6098 {
6099 LOG("Packet process thread is process...");
6100 }
6101
6102 return 0;
6103#else
6104 if(async_process) {
6105 info_urc_msg_t *urc = (info_urc_msg_t*)malloc(sizeof(info_urc_msg_t));
6106 if(!urc)
6107 {
6108 LOG("malloc() fail[%d].", errno);
6109 return -1;
6110 }
6111 urc->msg = msg;
6112 if(data && data_len > 0) {
6113 urc->data = memdup(data, data_len);
6114 urc->data_len = data_len;
6115 } else {
6116 urc->data = NULL;
6117 urc->data_len = 0;
6118 }
6119 return send_pack_to_queue(NULL, urc);
6120 } else {
6121 switch(msg) {
6122 case INFO_URC_MSG_NET_CS_REG_STATE:
6123 {
6124 net_state_change(data, data_len);
6125 break;
6126 }
6127 case INFO_URC_MSG_CALL_STATE:
6128 {
6129 call_state_change(data, data_len);
6130 break;
6131 }
6132 case INFO_URC_MSG_SMS_STATE:
6133 {
6134 sms_state_change(data, data_len);
6135 break;
6136 }
6137 case INFO_URC_MSG_SIM_STATE:
6138 {
6139 sim_state_change(data, data_len);
6140 break;
6141 }
6142 case INFO_URC_MSG_PDP_STATE:
6143 {
6144 pdp_state_change(data, data_len);
6145 break;
6146 }
6147 default: {
6148 LOGE("Unknown msg : %d", msg);
6149 break;
6150 }
6151 }
6152
6153 return 0;
6154 }
6155#endif
6156}
6157
6158
6159static void ind_regisger(sock_client_info_t* cli_info, uint16 ind)
6160{
6161 uint32 i = 0;
6162 while(i < cli_info->ind_num)
6163 {
6164 if(cli_info->ind_register[i] == ind)
6165 break;
6166 i++;
6167 }
6168
6169 if(i == cli_info->ind_num) // No found IND
6170 {
6171 cli_info->ind_register[i] = ind;
6172 cli_info->ind_num++;
6173 LOG("Register IND : %s", id2str(ind));
6174 }
6175 else
6176 {
6177 LOG("IND had exist.");
6178 }
6179}
6180
6181static void pack_distribute(sock_client_info_t* cli_info, mbtk_info_pack_t* pack)
6182{
6183 // Register IND Message.
6184 if(mbtk_info_type_get(pack->info_id) == MBTK_INFO_TYPE_IND)
6185 {
6186 mbtk_info_err_enum err = MBTK_INFO_ERR_SUCCESS;
6187 if(cli_info->ind_num >= IND_REGISTER_MAX)
6188 {
6189 LOG("IND if full.");
6190 err = MBTK_INFO_ERR_IND_FULL;
6191 }
6192 else
6193 {
6194 ind_regisger(cli_info, pack->info_id);
6195 }
6196
6197 pack_error_send(cli_info->fd, pack->info_id, err);
6198
6199 mbtk_info_pack_free(&pack);
6200 }
6201 else // Request Information.
6202 {
6203 LOG("Start process REQ(%s), Length : %d", id2str(pack->info_id), pack->data_len);
6204 if(0 && pack->data_len > 0)
6205 {
6206 log_hex("DATA", pack->data, pack->data_len);
6207 }
6208
6209 // Send to REQ_process_thread process.
6210 send_pack_to_queue(cli_info, pack);
6211
6212 // For test.
6213 // pack_error_send(cli_info->fd, pack->info_id + 1, MBTK_INFO_ERR_SUCCESS);
6214 }
6215}
6216
6217static sock_client_info_t* cli_find(int fd)
6218{
6219 sock_client_info_t *result = NULL;
6220 list_first(sock_client_list);
6221 while ((result = (sock_client_info_t*) list_next(sock_client_list)))
6222 {
6223 if (result->fd == fd)
6224 return result;
6225 }
6226
6227 return NULL;
6228}
6229
6230//mbtk wyq for server_ready_status add start
6231void server_ready_set(void)
6232{
6233 server_ready_status = 1;
6234}
6235
6236char server_ready_get(void)
6237{
6238 return server_ready_status;
6239}
6240
6241static void server_state_send(void)
6242{
6243 sock_client_info_t *cli = NULL;
6244 list_first(sock_client_list);
6245 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
6246 {
6247 if(cli->ind_num > 0) {
6248 if(cli->ind_register[0] == MBTK_INFO_ID_IND_SERVER_STATE_CHANGE) {
6249 cli->ind_num = 0;
6250 cli->ind_register[0] = 0;
6251 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_SERVER_STATE_CHANGE, "1", 1);
6252 break;
6253 }
6254 }
6255 else
6256 {
6257 break;
6258 }
6259 }
6260 LOG("handshake message send ok.");
6261}
6262
6263//mbtk wyq for server_ready_status add end
6264
6265//mbtk wyq for data_call_ex add start
6266//Save the cid that "DATA_CALL" needs to be automatically connected after startup
6267void data_call_bootconn_save(int cid, int bootconn)
6268{
6269 if(cid_bootconn[cid] == bootconn + '0')
6270 {
6271 return;
6272 }
6273 cid_bootconn[cid] = bootconn + '0';
6274
6275 LOG("data_call_bootconn_set cid_bootconn = %s", cid_bootconn);
6276 property_set("persist.mbtk.datacall.bootconn", cid_bootconn);
6277}
6278
6279static void* data_call_bootconn_pthread(void *arg)
6280{
6281 UNUSED(arg);
6282 LOG("data_call_bootconn_pthread enter.");
6283 int i = 0;
6284 int send_sum = 0;
6285 int bootconn = 0;
b.liufe320632024-01-17 20:38:08 +08006286
liubin281ac462023-07-19 14:22:54 +08006287 while(1)
6288 {
6289 if(server_ready_get() && send_sum == 0)
6290 {
6291 server_state_send();
6292 send_sum = 1;
6293 }
b.liufe320632024-01-17 20:38:08 +08006294
liubin281ac462023-07-19 14:22:54 +08006295 if(net_info.sim_state == MBTK_SIM_READY && net_info.net_type == MBTK_RADIO_TECH_E_UTRAN && bootconn == 0)
6296 {
6297 //data_call_bootconn_exec();
6298 property_get("persist.mbtk.datacall.bootconn", cid_bootconn, "00000000");
6299 LOG("data_call_bootconn_exec cid_bootconn = %s", cid_bootconn);
b.liufe320632024-01-17 20:38:08 +08006300
liubin281ac462023-07-19 14:22:54 +08006301 for(i = MBTK_APN_CID_MIN; i < MBTK_APN_CID_MAX + 1; i++)
6302 {
6303 if(cid_bootconn[i] == '1')
6304 {
6305 sock_client_info_t *info = (sock_client_info_t*)malloc(sizeof(sock_client_info_t));
6306 if(info == NULL)
6307 {
6308 LOG("clinent_info malloc() fail.");
6309 continue;
6310 }
6311 memset(info, 0, sizeof(sock_client_info_t));
6312 info->fd = DATA_CALL_BOOTCONN_FD;
b.liufe320632024-01-17 20:38:08 +08006313
liubin281ac462023-07-19 14:22:54 +08006314 mbtk_info_pack_t* pack = mbtk_info_pack_creat(MBTK_INFO_ID_NET_DATA_CALL_REQ);
6315 if(pack == NULL)
6316 {
6317 free(info);
6318 LOG("Packet malloc() fail.");
6319 continue;
6320 }
6321
6322 // "info_err"
6323 //pack->info_err = byte_2_uint16(ptr, false)
6324
6325 // "data_len"
6326 pack->data_len = 5;
6327
b.liu9e8584b2024-11-06 19:21:28 +08006328 uint8 *p = (uint8 *)malloc(5);
liubin281ac462023-07-19 14:22:54 +08006329 p[0] = MBTK_DATA_CALL_START;
6330 p[1] = i;
6331 p[2] = 0;
6332 p[3] = 1;
6333 p[4] = 10;
6334 pack->data = p;
6335 send_pack_to_queue(info, pack);
6336 }
6337 }
6338
6339 bootconn = 1;
6340 }
6341
6342 if(bootconn == 1 && send_sum == 1)
6343 {
6344 break;
6345 }
6346 else
6347 {
6348 sleep(1);
6349 }
6350 }
6351
6352 LOG("data_call_bootconn_pthread exit.");
6353 return NULL;
6354}
6355
6356//mbtk wyq for data_call_ex add end
6357
6358static void* info_main_pthread(void* arg)
6359{
6360 UNUSED(arg);
6361 epoll_fd = epoll_create(SOCK_CLIENT_MAX + 1);
6362 if(epoll_fd < 0)
6363 {
6364 LOG("epoll_create() fail[%d].", errno);
6365 return NULL;
6366 }
6367
6368 uint32 event = EPOLLIN | EPOLLET;
6369 struct epoll_event ev;
6370 ev.data.fd = sock_listen_fd;
6371 ev.events = event; //EPOLLIN | EPOLLERR | EPOLLET;
6372 epoll_ctl(epoll_fd,EPOLL_CTL_ADD,sock_listen_fd,&ev);
6373
6374 int nready = -1;
6375 struct epoll_event epoll_events[EPOLL_LISTEN_MAX];
6376 while(1)
6377 {
6378 nready = epoll_wait(epoll_fd, epoll_events, EPOLL_LISTEN_MAX, -1);
6379 if(nready > 0)
6380 {
6381 sock_client_info_t *cli_info = NULL;
6382 int i;
6383 for(i = 0; i < nready; i++)
6384 {
6385 LOG("fd[%d] event = %x",epoll_events[i].data.fd, epoll_events[i].events);
6386 if(epoll_events[i].events & EPOLLHUP) // Client Close.
6387 {
6388 if((cli_info = cli_find(epoll_events[i].data.fd)) != NULL)
6389 {
6390 cli_close(cli_info);
6391 }
6392 else
6393 {
6394 LOG("Unknown client[fd = %d].", epoll_events[i].data.fd);
6395 }
6396 }
6397 else if(epoll_events[i].events & EPOLLIN)
6398 {
6399 if(epoll_events[i].data.fd == sock_listen_fd) // New clients connected.
6400 {
6401 int client_fd = -1;
6402 while(1)
6403 {
6404 struct sockaddr_in cliaddr;
6405 socklen_t clilen = sizeof(cliaddr);
6406 client_fd = accept(epoll_events[i].data.fd, (struct sockaddr *) &cliaddr, &clilen);
6407 if(client_fd < 0)
6408 {
6409 if(errno == EAGAIN)
6410 {
6411 LOG("All client connect get.");
6412 }
6413 else
6414 {
6415 LOG("accept() error[%d].", errno);
6416 }
6417 break;
6418 }
6419 // Set O_NONBLOCK
6420 int flags = fcntl(client_fd, F_GETFL, 0);
6421 if (flags > 0)
6422 {
6423 flags |= O_NONBLOCK;
6424 if (fcntl(client_fd, F_SETFL, flags) < 0)
6425 {
6426 LOG("Set flags error:%d", errno);
6427 }
6428 }
6429
6430 memset(&ev,0,sizeof(struct epoll_event));
6431 ev.data.fd = client_fd;
6432 ev.events = event;//EPOLLIN | EPOLLERR | EPOLLET;
6433 epoll_ctl(epoll_fd, EPOLL_CTL_ADD, client_fd, &ev);
6434
6435 sock_client_info_t *info = (sock_client_info_t*)malloc(sizeof(sock_client_info_t));
6436 if(info)
6437 {
6438 memset(info, 0, sizeof(sock_client_info_t));
6439 info->fd = client_fd;
6440 if(server_ready_get() == 1)
6441 {
6442 info->ind_num = 0;
6443 pack_rsp_send(info->fd , MBTK_INFO_ID_IND_SERVER_STATE_CHANGE, "1", 1);
6444 LOG("server ready ok.");
6445 }
6446 else
6447 {
6448 info->ind_num = 1;
6449 info->ind_register[0] = MBTK_INFO_ID_IND_SERVER_STATE_CHANGE;
6450 LOG("server ready no.");
6451 }
6452 list_add(sock_client_list, info);
6453 LOG("Add New Client FD Into List.");
6454 }
6455 else
6456 {
6457 LOG("malloc() fail.");
6458 }
6459 }
6460 }
6461 else if((cli_info = cli_find(epoll_events[i].data.fd)) != NULL) // Client data arrive.
6462 {
6463 // Read and process every message.
6464 mbtk_info_err_enum err = MBTK_INFO_ERR_SUCCESS;
6465 mbtk_info_pack_t** pack = mbtk_info_pack_recv(cli_info->fd, true, &err);
6466
6467 // Parse packet error,send error response to client.
6468 if(pack == NULL)
6469 {
6470 if(err != MBTK_INFO_ERR_SUCCESS)
6471 {
6472 pack_error_send(cli_info->fd, MBTK_INFO_ID_REQ_UNKNOWN, err);
6473 }
6474 }
6475 else
6476 {
6477#if 0
6478 int i = 0;
6479 while(pack[i] != NULL)
6480 {
6481 pack_distribute(cli_info, pack[i]);
6482 // Not free,will free in pack_process() or packet process thread.
6483 //mbtk_info_pack_free(&(pack[i]));
6484 i++;
6485 }
6486 free(pack);
6487#else
6488 mbtk_info_pack_t** pack_ptr = pack;
6489 while(*pack_ptr)
6490 {
6491 pack_distribute(cli_info, *pack_ptr);
6492 // Not free,will free in pack_process() or packet process thread.
6493 //mbtk_info_pack_free(pack_ptr);
6494 pack_ptr++;
6495 }
6496
6497 free(pack);
6498#endif
6499 }
6500 }
6501 else
6502 {
6503 LOG("Unknown socket : %d", epoll_events[i].data.fd);
6504 }
6505 }
6506 else
6507 {
6508 LOG("Unknown event : %x", epoll_events[i].events);
6509 }
6510 }
6511 }
6512 else
6513 {
6514 LOG("epoll_wait() fail[%d].", errno);
6515 }
6516 }
6517
6518 return NULL;
6519}
6520
liubin281ac462023-07-19 14:22:54 +08006521/*
6522void mbtk_radio_ready_cb()
6523{
6524 pthread_t radio_pid;
6525 pthread_attr_t thread_attr;
6526 pthread_attr_init(&thread_attr);
6527 if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
6528 {
6529 LOG("pthread_attr_setdetachstate() fail.");
6530 return;
6531 }
6532
6533 if(pthread_create(&radio_pid, &thread_attr, radio_ready_thread, NULL))
6534 {
6535 LOG("pthread_create() fail.");
6536 }
6537
6538 pthread_attr_destroy(&thread_attr);
6539}
6540*/
6541
6542static void net_ifc_state_change(bool act, int cid)
6543{
6544 if(cid < MBTK_APN_CID_MIN || cid > MBTK_APN_CID_MAX) { // No nothing for cid 1 and 8
6545 return;
6546 }
6547
yq.wanga9efa822024-07-03 04:33:48 -07006548#if 0
wangyouqiang65884152023-10-25 19:54:15 +08006549 if(act)
6550 {
6551 cid_active[cid] = 1;
wangyouqiang80487e42024-05-24 15:06:20 +08006552 mbtk_set_default_pdp_state(true, cid);
wangyouqiang65884152023-10-25 19:54:15 +08006553 }
6554 else
6555 {
6556 cid_active[cid] = 0;
wangyouqiang80487e42024-05-24 15:06:20 +08006557 mbtk_set_default_pdp_state(false, cid);
wangyouqiang65884152023-10-25 19:54:15 +08006558 }
yq.wanga9efa822024-07-03 04:33:48 -07006559#endif
liubin281ac462023-07-19 14:22:54 +08006560 char dev[20] = {0};
6561 sprintf(dev, "ccinet%d", cid - 1);
6562 if(act) { // Config IP.
6563 // Get IP information.
6564 mbtk_ipv4_info_t ipv4;
6565 mbtk_ipv6_info_t ipv6;
6566 memset(&ipv4, 0, sizeof(mbtk_ipv4_info_t));
6567 memset(&ipv6, 0, sizeof(mbtk_ipv6_info_t));
6568 int cme_err = MBTK_INFO_ERR_CME_NON;
6569 if(!req_data_call_state_get(cid, &ipv4, &ipv6, &cme_err) && cme_err == MBTK_INFO_ERR_CME_NON)
6570 {
wangyouqianged88c722023-11-22 16:33:43 +08006571#ifdef MBTK_AF_SUPPORT
6572 if(cid == 1)
6573 {
6574 ipv4.valid = false;
6575 ipv6.valid = false;
6576 if(default_iptype == MBTK_IP_TYPE_IP)
6577 {
6578 ipv4.valid = true;
6579 }
6580 else if(default_iptype == MBTK_IP_TYPE_IPV6)
6581 {
6582 ipv6.valid = true;
6583 }
6584 else
6585 {
6586 ipv4.valid = true;
6587 ipv6.valid = true;
6588 }
6589 }
6590#endif
liubin281ac462023-07-19 14:22:54 +08006591 // Config IPv4 address.
6592 if(ipv4.valid) {
6593 char ip[20] = {0};
6594 if(inet_ntop(AF_INET, &(ipv4.IPAddr), ip, 20) == NULL) {
6595 LOGE("inet_ntop ipv4 ip fail.");
6596 return;
6597 }
6598
6599 if(mbtk_ifc_configure2(dev, ip, 0, NULL, "255.255.255.0")) {
6600 LOGD("Config %s IPv4 %s fail.", dev, ip);
6601 } else {
6602 LOGD("Config %s IPv4 %s success.", dev, ip);
6603 }
6604 }
6605
6606 // Config IPv6 address.
6607 if(ipv6.valid) {
6608 char ip[50] = {0};
6609
6610 if(inet_ntop(AF_INET6, &(ipv6.IPV6Addr), ip, 50) == NULL) {
6611 LOGE("inet_ntop ipv6 ip fail.");
6612 return;
6613 }
6614
6615 if(mbtk_ipv6_config(dev, ip, 64)) {
6616 LOGD("Config %s IPv6 %s fail.", dev, ip);
6617 } else {
6618 LOGD("Config %s IPv6 %s success.", dev, ip);
6619 }
6620 }
yq.wanga9efa822024-07-03 04:33:48 -07006621
6622 mbtk_qser_route_config(cid, &ipv4, &ipv6);
liubin281ac462023-07-19 14:22:54 +08006623 }
6624 } else { // Del IP
6625 if(mbtk_ifc_configure2(dev, NULL, 0, NULL, NULL)) {
6626 LOGD("Config %s IPv4 0 fail.", dev);
6627 } else {
6628 LOGD("Config %s IPv4 0 success.", dev);
6629 }
6630 }
6631}
6632
yq.wanga9efa822024-07-03 04:33:48 -07006633static void data_call_restart()
6634{
6635#if 0
6636 // Waitting for network ok.
6637 mbtk_net_info_t info;
6638 int cme_err;
6639 int i = 0;
6640 while(i < 15) { // 15s timeout
6641 cme_err = MBTK_INFO_ERR_CME_NON;
6642 memset(&info, 0, sizeof(mbtk_net_info_t));
6643 if(!req_net_sel_mode_get(&info, &cme_err) && cme_err == MBTK_INFO_ERR_CME_NON)
6644 {
6645 if(info.net_type >= 2) {
6646 break;
6647 }
6648 }
6649
6650 sleep(1);
6651 i++;
6652 }
6653#endif
6654 // +CGACT
6655 int cid;
wangyouqiang3947b302024-07-04 17:26:08 +08006656 uint8 data_pdp = 0;
yq.wanga9efa822024-07-03 04:33:48 -07006657 LOGD("Start active APN.");
6658 at_process = true;
6659 cgact_wait.act = true;
yq.wangd58f71e2024-08-21 23:45:31 -07006660 for(cid = MBTK_APN_CID_MIN; cid <= MBTK_APN_CID_MAX; cid++) {
6661 if(cid_active[cid])
6662 {
6663 LOG("Active cid : %d", cid);
6664 cgact_wait.waitting = true;
6665 cgact_wait.cid = cid;
6666 net_ifc_state_change(false, cid);
6667 data_pdp = cid + 100;
6668 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
6669 req_data_call_start(cid, NULL);
6670 wait_cgact_complete(10);
6671 net_ifc_state_change(true, cid);
6672 data_pdp = cid + 220;
6673 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
6674 }
yq.wanga9efa822024-07-03 04:33:48 -07006675 }
6676 at_process = false;
6677}
6678
liubin281ac462023-07-19 14:22:54 +08006679static void urc_msg_process(info_urc_msg_t *msg)
6680{
6681 uint8 *data = NULL;
6682 if(msg->data) {
6683 data = (uint8*)msg->data;
6684 }
6685 switch(msg->msg) {
6686 case INFO_URC_MSG_RADIO_STATE:
6687 {
6688 radio_state_change(msg->data, msg->data_len);
6689 // Reconfig APN while radio on.
6690 if(data[0]) {
6691 apn_prop_get();
6692 }
6693 break;
6694 }
6695 case INFO_URC_MSG_CGEV:
6696 {
6697 bool act = data[0];
6698 int cid = data[1];
6699 if(cid > 0) {
6700 net_ifc_state_change(act, cid);
6701 }
6702 break;
6703 }
6704 case INFO_URC_MSG_NET_PS_REG_STATE:
6705 {
b.liufe320632024-01-17 20:38:08 +08006706 uint8 net_data[4];
liubin281ac462023-07-19 14:22:54 +08006707 net_data[0] = (uint8)MBTK_NET_PS_STATE;
b.liufe320632024-01-17 20:38:08 +08006708 net_data[1] = data[0]; // act
6709 net_data[3] = data[1]; // 0 - GSM/WCDMA; 1 - LTE
liubin281ac462023-07-19 14:22:54 +08006710 mbtk_net_reg_state_enum state = (mbtk_net_reg_state_enum)data[0];
6711 if(state == MBTK_NET_REG_STATE_HOME
6712 || state == MBTK_NET_REG_STATE_ROAMING) { // Registered, home network or roaming.
6713 mbtk_net_info_t info;
6714 int cme_err = MBTK_INFO_ERR_CME_NON;
6715 memset(&info, 0, sizeof(mbtk_net_info_t));
6716 if(!req_net_sel_mode_get(&info, &cme_err) && cme_err == MBTK_INFO_ERR_CME_NON)
6717 {
6718 net_data[2] = info.net_type;
6719 net_state_change(net_data, sizeof(net_data));
6720
6721 if(info.net_type >= MBTK_RADIO_TECH_UTRAN) {
6722 data_call_restart();
6723 }
6724 } else {
6725 net_data[2] = (uint8)0xFF;
6726 net_state_change(net_data, sizeof(net_data));
6727 }
6728 } else {
6729 net_data[2] = (uint8)0xFF;
6730 net_state_change(net_data, sizeof(net_data));
6731 }
6732 break;
6733 }
b.liuf37bd332024-03-18 13:51:24 +08006734 case INFO_URC_MSG_SET_BAND:
6735 {
6736 mbtk_band_info_t band;
6737 int cme_err = MBTK_INFO_ERR_CME_NON;
6738
6739 band.net_pref = MBTK_NET_PREF_GSM_UMTS_LTE_LTE_PREF; // 15
b.liu288093c2024-05-09 17:02:57 +08006740 band.gsm_band = (uint16)band_set_info.band_gsm;
6741 band.umts_band = (uint16)band_set_info.band_wcdma;
b.liuf37bd332024-03-18 13:51:24 +08006742 band.tdlte_band = band_set_info.band_tdlte;
6743 band.fddlte_band = band_set_info.band_fddlte;
b.liu288093c2024-05-09 17:02:57 +08006744 band.lte_ext_band = band_set_info.band_lte_ext;
6745
b.liuf37bd332024-03-18 13:51:24 +08006746 if(req_band_set(&band, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
6747 {
6748 LOG("Set band fail.");
6749 }
6750 else // Set band success.
6751 {
b.liu45bfb532024-05-11 15:37:22 +08006752 // log_hex("BAND-2", &band_set_info, sizeof(band_set_info_t));
b.liu288093c2024-05-09 17:02:57 +08006753 band_set_success = TRUE;
6754 if(band_area == MBTK_MODEM_BAND_AREA_CN) {
b.liuf37bd332024-03-18 13:51:24 +08006755 property_set("persist.mbtk.band_config", "CN");
b.liu288093c2024-05-09 17:02:57 +08006756 } else if(band_area == MBTK_MODEM_BAND_AREA_EU) {
b.liuf37bd332024-03-18 13:51:24 +08006757 property_set("persist.mbtk.band_config", "EU");
b.liu288093c2024-05-09 17:02:57 +08006758 } else if(band_area == MBTK_MODEM_BAND_AREA_SA) {
b.liuf678f992024-05-08 15:23:10 +08006759 property_set("persist.mbtk.band_config", "SA");
b.liuf37bd332024-03-18 13:51:24 +08006760 } else {
6761 property_set("persist.mbtk.band_config", "ALL");
6762 }
6763 LOG("Set band success.");
6764 }
6765 break;
6766 }
wangyouqiangce45a102024-04-18 18:08:29 +08006767 case INFO_URC_MSG_GET_SIM_STATE:
6768 {
6769 net_info.sim_state = getSIMStatus();
6770 if(net_info.sim_state == MBTK_SIM_READY)
6771 {
6772 LOG("SIM READY!");
6773 }
6774 else
6775 {
6776 LOG("SIM NOT READY!");
6777 }
6778 break;
6779 }
liubin281ac462023-07-19 14:22:54 +08006780 case INFO_URC_MSG_NET_STATE_LOG:
6781 {
6782 // Get network state and signal.
6783 char buff[256] = {0};
yq.wangf1fc31e2024-10-12 03:21:03 -07006784 uint8 data_signal[8];
6785 mbtk_signal_info_t signal;
liubin281ac462023-07-19 14:22:54 +08006786 memset(&signal, 0xFF, sizeof(mbtk_signal_info_t));
6787 if(!req_net_signal_get(&signal, NULL)) {
6788 char tmp[50] = {0};
6789 struct timeval log_time;
6790 gettimeofday(&log_time, NULL);
6791 struct tm* tm_t = localtime(&(log_time.tv_sec));
6792 strftime(tmp, 50, "%F %T", tm_t);
6793 snprintf(buff, sizeof(buff), "%s:%d,%d,%d,%d,%d,%d,%d,%d", tmp, signal.type, signal.rssi, signal.rxlev, signal.ber, signal.rscp, signal.ecno,
6794 signal.rsrq, signal.rsrp);
b.liu06559f62024-11-01 18:48:22 +08006795 mbtk_signal_log(buff);
yq.wangf1fc31e2024-10-12 03:21:03 -07006796 }
r.xiaofca7c472024-04-24 01:00:23 -07006797
yq.wangf1fc31e2024-10-12 03:21:03 -07006798 if(signal_globe.type != signal.type)
6799 {
6800 data_signal[0] = signal.type;
6801 data_signal[1] = signal.rssi;
6802 data_signal[2] = signal.rxlev;
6803 data_signal[3] = signal.ber;
6804 data_signal[4] = signal.rscp;
6805 data_signal[5] = signal.ecno;
6806 data_signal[6] = signal.rsrq;
6807 data_signal[7] = signal.rsrp;
6808 signal_globe.type = signal.type;
6809 signal_state_change(data_signal, sizeof(data_signal));
liubin281ac462023-07-19 14:22:54 +08006810 }
liubin281ac462023-07-19 14:22:54 +08006811 break;
6812 }
6813 default:
6814 {
6815 LOGE("Unknown URC : %d", msg->msg);
6816 break;
6817 }
6818 }
6819}
6820
6821static void* pack_process_thread(void* arg)
6822{
6823 UNUSED(arg);
6824 info_queue_item_t* item = NULL;
6825 mbtk_queue_init(&info_queue);
6826 pthread_mutex_init(&info_mutex, NULL);
6827 pthread_cond_init(&info_cond, NULL);
6828
6829 memset(&band_support, 0xFF, sizeof(mbtk_band_info_t));
6830
6831 pthread_mutex_lock(&info_mutex);
6832 while(TRUE)
6833 {
6834 if(mbtk_queue_empty(&info_queue))
6835 {
6836 LOG("Packet process wait...");
6837 pthread_cond_wait(&info_cond, &info_mutex);
6838 LOG("Packet process continue...");
6839 }
6840 else
6841 {
6842 LOG("Packet process queue not empty,continue...");
6843 }
6844
6845 // Process all information request.
6846 mbtk_info_err_enum err;
6847 while((item = (info_queue_item_t*)mbtk_queue_get(&info_queue)) != NULL)
6848 {
6849 if(item->cli_info) { // REQ form client.
6850 mbtk_info_pack_t *pack = (mbtk_info_pack_t*)item->pack;
6851 LOG("Process REQ %s.", id2str(pack->info_id));
6852 at_process = true;
6853 err = pack_req_process(item->cli_info, pack);
6854 if(err != MBTK_INFO_ERR_SUCCESS)
6855 {
6856 if(item->cli_info->fd != DATA_CALL_BOOTCONN_FD)
6857 {
6858 pack_error_send(item->cli_info->fd, pack->info_id + 1, err);
6859 }
6860 else
6861 {
wangyouqiang80487e42024-05-24 15:06:20 +08006862 if(pack->data != NULL)
6863 {
6864 free(pack->data);
6865 }
6866 if(item->cli_info)
6867 {
6868 free(item->cli_info);
6869 }
liubin281ac462023-07-19 14:22:54 +08006870 }
6871 }
6872 at_process = false;
6873 mbtk_info_pack_free(&pack);
6874 free(item);
6875 } else { // REQ from myself.
6876 info_urc_msg_t *urc = (info_urc_msg_t*)item->pack;
6877 LOG("Process URC %d.", urc->msg);
6878 urc_msg_process(urc);
6879 if(!urc->data)
6880 free(urc->data);
6881 free(urc);
6882 }
6883 }
6884 }
6885 pthread_mutex_unlock(&info_mutex);
6886 return NULL;
6887}
6888
6889void apn_prop_get()
6890{
6891 char prop_name[20];
6892 char prop_data[300];
6893 // cid : 2 - 7
6894 int cid = MBTK_APN_CID_MIN;
6895 mbtk_apn_info_t apn;
6896 for(; cid <= MBTK_APN_CID_MAX; cid++) {
6897 memset(prop_name, 0, 20);
6898 memset(prop_data, 0, 300);
6899 memset(&apn, 0, sizeof(mbtk_apn_info_t));
6900 sprintf(prop_name, "%s_%d",MBTK_APN_PROP,cid);
6901 if(property_get(prop_name, prop_data, "") > 0 && !str_empty(prop_data)) {
6902 apn.cid = cid;
6903 char *ptr_1 = prop_data;
6904 apn.ip_type = (mbtk_ip_type_enum)atoi(ptr_1);
6905 ptr_1 = strstr(ptr_1, ",");
6906 if(!ptr_1) {
6907 continue;
6908 }
6909 ptr_1++; // Jump ',' to apn
6910
6911 char *ptr_2 = strstr(ptr_1, ",");
6912 if(!ptr_2) {
6913 continue;
6914 }
6915 memcpy(apn.apn, ptr_1, ptr_2 - ptr_1); // apn
6916
6917 ptr_2++; // Jump ',' to user
6918 ptr_1 = strstr(ptr_2, ",");
6919 if(!ptr_1) {
6920 continue;
6921 }
6922 if(memcmp(ptr_2, "NULL", 4)) { // Not "NULL"
6923 memcpy(apn.user, ptr_2, ptr_1 - ptr_2); // user
6924 }
6925
6926 ptr_1++; // Jump ',' to pass
6927 ptr_2 = strstr(ptr_1, ",");
6928 if(!ptr_2) {
6929 continue;
6930 }
6931 if(memcmp(ptr_1, "NULL", 4)) { // Not "NULL"
6932 memcpy(apn.pass, ptr_1, ptr_2 - ptr_1); // pass
6933 }
6934
6935 ptr_2++; // Jump ',' to auth (Is last item)
6936 if(memcmp(ptr_2, "NULL", 4)) { // Not "NULL"
6937 memcpy(apn.auth, ptr_2, strlen(ptr_2)); // auth
6938 }
6939
6940 req_apn_set(&apn, NULL);
6941 }
6942 }
6943}
6944
b.liu9e8584b2024-11-06 19:21:28 +08006945#if 0
liubin281ac462023-07-19 14:22:54 +08006946/*
b.liue0ab2442024-02-06 18:53:28 +08006947root@OpenWrt:/usrdata# cat /proc/mtd
6948dev: size erasesize name
6949mtd0: 00040000 00020000 "bootloader"
6950mtd1: 00020000 00020000 "cp_reliabledata"
6951mtd2: 00020000 00020000 "ap_reliabledata"
6952mtd3: 00020000 00020000 "cp_reliabledata_backup"
6953mtd4: 00020000 00020000 "ap_reliabledata_backup"
6954mtd5: 00020000 00020000 "mep-ota"
6955mtd6: 00020000 00020000 "mep-ota_backup"
6956mtd7: 00040000 00020000 "dtim"
6957mtd8: 00f40000 00020000 "cpimage"
6958mtd9: 000c0000 00020000 "u-boot"
6959mtd10: 00500000 00020000 "kernel"
6960mtd11: 00100000 00020000 "asr_flag"
6961mtd12: 01400000 00020000 "rootfs"
6962mtd13: 01400000 00020000 "oem_data"
6963mtd14: 01e00000 00020000 "OTA"
6964mtd15: 01400000 00020000 "rootfs_data"
6965mtd16: 081a0000 00020000 "user_data"
6966mtd17: 00d20000 00020000 "MRVL_BBM"
6967*/
6968static int partition_name_2_dev(const char *name, char *dev) {
6969 if(name == NULL || dev == NULL) {
6970 LOGE("ARG error.");
6971 return -1;
6972 }
6973
b.liu9e8584b2024-11-06 19:21:28 +08006974 FILE* fp = fopen("/proc/mtd", "r");
b.liue0ab2442024-02-06 18:53:28 +08006975 if (fp == NULL) {
6976 LOGE("Open MTD failed!");
6977 return -1;
6978 }
6979
6980 char buf[1024];
6981 while (fgets(buf, 1024, fp) != NULL) {
6982 if(strstr(buf, name)) {
6983 int index = atoi(buf + 3);
6984 sprintf(dev, "/dev/mtdblock%d", index);
6985 LOGD("%s -> %s", name, dev);
6986 return 0;
6987 }
6988 }
6989
6990 return -1;
6991}
b.liu9e8584b2024-11-06 19:21:28 +08006992#endif
6993#if 0
b.liue0ab2442024-02-06 18:53:28 +08006994static int custom_partition_read(const char *name, mbtk_cust_info_t *cust_info)
6995{
b.liu9e8584b2024-11-06 19:21:28 +08006996 int fd = 0;
6997 unsigned int flaglen = 0;
b.liue0ab2442024-02-06 18:53:28 +08006998 flaglen = sizeof(mbtk_cust_info_t);
6999
7000 char mtd_path[50] = {0};
7001 if(partition_name_2_dev(name, mtd_path)) {
7002 LOGE("partition_name_2_dev() failed!");
7003 return -1;
7004 }
7005
7006 fd = open(mtd_path, O_RDONLY);
7007 if (fd < 0) {
7008 LOGE("Fatal error: can't open cust info %s\n", mtd_path);
7009 return -1;
7010 }
7011
7012 if (read(fd, cust_info, flaglen) < 0)
7013 goto error;
7014 if (cust_info->header != CUST_INFO_HEADER) {
7015 LOGE("Cust info partition error.");
7016 goto error;
7017 } else {
7018 if(cust_info->band_type == 1) { // CN
7019 LOGD("Band : CN");
7020 } else if(cust_info->band_type == 2) { // EU
7021 LOGD("Band : EU");
7022 } else {
7023 LOGE("Unknown band type:%d", cust_info->band_type);
7024 goto error;
7025 }
7026 }
7027 close(fd);
7028 return 0;
7029error:
7030 close(fd);
7031 return -1;
7032}
b.liu9e8584b2024-11-06 19:21:28 +08007033#endif
b.liue0ab2442024-02-06 18:53:28 +08007034
7035/*
liubin281ac462023-07-19 14:22:54 +08007036AT*BAND=15,78,147,482,134742231
7037
7038OK
7039*/
b.liubb5e7682024-02-28 20:13:04 +08007040static void* band_config_thread()
liubin281ac462023-07-19 14:22:54 +08007041{
b.liubb5e7682024-02-28 20:13:04 +08007042 mbtk_device_info_modem_t info_modem;
b.liuf37bd332024-03-18 13:51:24 +08007043 memset(&band_set_info, 0, sizeof(band_set_info_t));
b.liubb5e7682024-02-28 20:13:04 +08007044 memset(&info_modem, 0, sizeof(mbtk_device_info_modem_t));
b.liu288093c2024-05-09 17:02:57 +08007045 band_set_success = FALSE;
b.liuf37bd332024-03-18 13:51:24 +08007046 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 +08007047 LOGD("mbtk_dev_info_read(MODEM) fail, use default band.");
b.liu288093c2024-05-09 17:02:57 +08007048 band_area = MBTK_MODEM_BAND_AREA_ALL;
b.liuf37bd332024-03-18 13:51:24 +08007049 band_set_info.band_gsm = MBTK_BAND_ALL_GSM_DEFAULT;
7050 band_set_info.band_wcdma = MBTK_BAND_ALL_WCDMA_DEFAULT;
7051 band_set_info.band_tdlte = MBTK_BAND_ALL_TDLTE_DEFAULT;
7052 band_set_info.band_fddlte = MBTK_BAND_ALL_FDDLTE_DEFAULT;
b.liu288093c2024-05-09 17:02:57 +08007053 band_set_info.band_lte_ext = MBTK_BAND_ALL_EXT_LTE_DEFAULT;
b.liubb5e7682024-02-28 20:13:04 +08007054 } else {
b.liu288093c2024-05-09 17:02:57 +08007055 band_area = info_modem.band_area;
b.liuf37bd332024-03-18 13:51:24 +08007056 band_set_info.band_gsm = info_modem.band_gsm;
7057 band_set_info.band_wcdma = info_modem.band_wcdma;
7058 band_set_info.band_tdlte = info_modem.band_tdlte;
7059 band_set_info.band_fddlte = info_modem.band_fddlte;
b.liuf678f992024-05-08 15:23:10 +08007060 band_set_info.band_lte_ext = info_modem.band_lte_ext;
b.liuf37bd332024-03-18 13:51:24 +08007061 }
b.liubb5e7682024-02-28 20:13:04 +08007062
b.liuf37bd332024-03-18 13:51:24 +08007063 bool is_first = TRUE;
b.liu288093c2024-05-09 17:02:57 +08007064 while(!band_set_success) {
b.liuf37bd332024-03-18 13:51:24 +08007065 info_urc_msg_t *urc = (info_urc_msg_t*)malloc(sizeof(info_urc_msg_t));
7066 if(!urc)
7067 {
7068 LOG("malloc() fail[%d].", errno);
7069 break;
b.liubb5e7682024-02-28 20:13:04 +08007070 } else {
b.liuf37bd332024-03-18 13:51:24 +08007071 urc->msg = INFO_URC_MSG_SET_BAND;
7072 urc->data = NULL;
7073 urc->data_len = 0;
7074 send_pack_to_queue(NULL, urc);
7075
7076 if(is_first) {
7077 is_first = FALSE;
7078 } else {
b.liu288093c2024-05-09 17:02:57 +08007079 LOGE("*BAND exec error, will retry in 5s.");
b.liuf37bd332024-03-18 13:51:24 +08007080 }
b.liu288093c2024-05-09 17:02:57 +08007081 sleep(5);
b.liubb5e7682024-02-28 20:13:04 +08007082 }
7083 }
7084
b.liuf37bd332024-03-18 13:51:24 +08007085 LOGD("Set Band thread exit.");
b.liubb5e7682024-02-28 20:13:04 +08007086 return NULL;
liubin281ac462023-07-19 14:22:54 +08007087}
7088
b.liu9e8584b2024-11-06 19:21:28 +08007089#if 0
liubin281ac462023-07-19 14:22:54 +08007090static void* net_monitor_thread(void* arg)
7091{
7092 UNUSED(arg);
7093 // Start network monitor
b.liu9e8584b2024-11-06 19:21:28 +08007094// int cid;
liubin281ac462023-07-19 14:22:54 +08007095 while(1) {
7096#if 0
7097 // Config IP
7098 list_node_t* apn_list = NULL;
7099 if(!apn_state_get(&apn_list) && apn_list != NULL) {
7100 info_apn_ip_t *apn = NULL;
7101 for(cid = MBTK_APN_CID_MIN; cid <= MBTK_APN_CID_MAX && cid_active[cid]; cid++) {
7102 bool ip_found = false;
7103 list_first(apn_list);
7104 while ((apn = (info_apn_ip_t*) list_next(apn_list))) {
7105 if(cid == apn->cid) {
7106 ip_found = true;
7107 break;
7108 }
7109 }
7110
7111 char dev[20] = {0};
7112 sprintf(dev, "ccinet%d", cid - 1);
7113 if(ip_found) { // Ip ok,set IP.
7114 if(apn->ipv4_valid) {
7115 if(mbtk_ifc_configure2(dev, (char*)apn->ipv4, 0, NULL, "255.255.255.0")) {
7116 LOGD("Config %s IPv4 %s fail.", dev, apn->ipv4);
7117 } else {
7118 LOGD("Config %s IPv4 %s success.", dev, apn->ipv4);
7119 }
7120 }
7121
7122 if(apn->ipv6_valid) {
7123 if(mbtk_ipv6_config(dev, (char*)apn->ipv6, 64)) {
7124 LOGD("Config %s IPv6 %s fail.", dev, apn->ipv6);
7125 } else {
7126 LOGD("Config %s IPv6 %s success.", dev, apn->ipv6);
7127 }
7128 }
7129 } else { // No ip
7130 if(mbtk_ifc_configure2(dev, NULL, 0, NULL, NULL)) {
7131 LOGD("Config %s IPv4 0 fail.", dev);
7132 } else {
7133 LOGD("Config %s IPv4 0 success.", dev);
7134 }
7135 }
7136 }
7137
7138 list_free(apn_list);
7139 }
7140#endif
7141
7142 if(net_info.radio_state == MBTK_RADIO_STATE_ON && net_info.sim_state == MBTK_SIM_READY) {
7143#if 0
7144 urc_msg_distribute(true, INFO_URC_MSG_NET_CS_REG_STATE, NULL, 0);
7145#else
7146 info_urc_msg_t *urc = (info_urc_msg_t*)malloc(sizeof(info_urc_msg_t));
7147 if(!urc)
7148 {
7149 LOG("malloc() fail[%d].", errno);
7150 } else {
7151 urc->msg = INFO_URC_MSG_NET_STATE_LOG;
7152 urc->data = NULL;
7153 urc->data_len = 0;
7154 send_pack_to_queue(NULL, urc);
7155 }
7156#endif
wangyouqiangce45a102024-04-18 18:08:29 +08007157 sleep(15);
liubin281ac462023-07-19 14:22:54 +08007158 }
wangyouqiangce45a102024-04-18 18:08:29 +08007159 else
7160 {
7161 info_urc_msg_t *urc = (info_urc_msg_t*)malloc(sizeof(info_urc_msg_t));
7162 if(!urc)
7163 {
7164 LOG("malloc() fail[%d].", errno);
7165 }
7166 else
7167 {
7168 urc->msg = INFO_URC_MSG_GET_SIM_STATE;
7169 urc->data = NULL;
7170 urc->data_len = 0;
7171 send_pack_to_queue(NULL, urc);
7172 }
7173 sleep(2);
7174 }
liubin281ac462023-07-19 14:22:54 +08007175 }
7176
7177 LOGD("monitor_thread exit.");
7178 return NULL;
7179}
b.liu9e8584b2024-11-06 19:21:28 +08007180#endif
liubin281ac462023-07-19 14:22:54 +08007181
b.liu9e8584b2024-11-06 19:21:28 +08007182#if 0
liubin281ac462023-07-19 14:22:54 +08007183static void* urc_process_thread(void* arg)
7184{
7185 UNUSED(arg);
7186 info_urc_msg_t* item = NULL;
7187 mbtk_queue_init(&urc_queue);
7188 pthread_mutex_init(&urc_mutex, NULL);
7189 pthread_cond_init(&urc_cond, NULL);
7190
7191 pthread_mutex_lock(&urc_mutex);
7192 while(TRUE)
7193 {
7194 if(mbtk_queue_empty(&urc_queue))
7195 {
7196 LOG("URC process wait...");
7197 pthread_cond_wait(&urc_cond, &urc_mutex);
7198 LOG("URC process continue...");
7199 }
7200 else
7201 {
7202 LOG("URC process queue not empty,continue...");
7203 }
7204
7205 // Process all information request.
7206 while((item = (info_urc_msg_t*)mbtk_queue_get(&urc_queue)) != NULL)
7207 {
7208 LOG("Process URC %d.", item->msg);
7209 uint8 *data = (uint8*)item->data;
7210 switch(item->msg) {
7211 case INFO_URC_MSG_RADIO_STATE:
7212 {
7213 radio_state_change(item->data, item->data_len);
7214 break;
7215 }
7216 case INFO_URC_MSG_CGEV:
7217 {
7218 bool act = data[0];
7219 int cid = data[1];
7220 if(cid > 0) {
7221 net_ifc_state_change(act, cid);
7222 }
7223 break;
7224 }
7225 default:
7226 {
7227 LOGE("Unknown URC : %d", item->msg);
7228 break;
7229 }
7230 }
7231 if(!item->data)
7232 free(item->data);
7233 free(item);
7234 }
7235 }
7236 pthread_mutex_unlock(&urc_mutex);
7237
7238 return NULL;
7239}
b.liu9e8584b2024-11-06 19:21:28 +08007240#endif
liubin281ac462023-07-19 14:22:54 +08007241
7242static void ril_at_ready_process()
7243{
7244 net_info.radio_state = (isRadioOn() == 1) ? MBTK_RADIO_STATE_ON : MBTK_RADIO_STATE_OFF;
7245#if 1
7246 if (net_info.radio_state != MBTK_RADIO_STATE_ON)
7247 {
7248 setRadioPower(1);
7249 } else { // Radio has ON
7250 apn_prop_get();
7251 }
7252
7253 if(net_info.radio_state == MBTK_RADIO_STATE_ON)
7254 {
7255 at_send_command("AT+CEREG=2", NULL);
7256 }
7257
b.liu9e8584b2024-11-06 19:21:28 +08007258// int count = 0;
liubin281ac462023-07-19 14:22:54 +08007259#endif
7260 net_info.sim_state = getSIMStatus();
7261#if 0
7262 while (net_info.sim_state != MBTK_SIM_READY && count < 30)
7263 {
7264 if(net_info.radio_state != MBTK_RADIO_STATE_ON)
7265 {
7266 setRadioPower(1);
7267 }
7268 LOGD("Waitting for SIM READY...");
7269 sleep(1);
7270 net_info.sim_state = getSIMStatus();
7271 count++;
7272 }
7273#endif
7274 if(net_info.sim_state == MBTK_SIM_READY)
7275 {
7276 LOGD("SIM READY!");
r.xiao7c62bc62024-05-24 00:53:30 -07007277 at_send_command("AT+COPS=3", NULL);
liubin281ac462023-07-19 14:22:54 +08007278 }
7279 else
7280 {
7281 LOGE("SIM NOT READY!");
7282 }
liubin281ac462023-07-19 14:22:54 +08007283}
7284
wangyouqiang80487e42024-05-24 15:06:20 +08007285int mbtk_get_apn_send_pack(void)
7286{
7287 sock_client_info_t *info = (sock_client_info_t*)malloc(sizeof(sock_client_info_t));
7288 if(info == NULL)
7289 {
7290 LOG("clinent_info malloc() fail.");
7291 return -1;
7292 }
7293 memset(info, 0, sizeof(sock_client_info_t));
7294 info->fd = DATA_CALL_APN_GET_FD;
7295
7296 mbtk_info_pack_t* pack = mbtk_info_pack_creat(MBTK_INFO_ID_NET_QSER_APN_REQ);
7297 if(pack == NULL)
7298 {
7299 free(info);
7300 LOG("Packet malloc() fail.");
7301 return -1;
7302 }
7303
7304 send_pack_to_queue(info, pack);
7305 return 0;
7306}
7307
liubin281ac462023-07-19 14:22:54 +08007308int mbtk_info_server_start()
7309{
7310 signal(SIGPIPE, SIG_IGN);
7311
wangyouqiangce45a102024-04-18 18:08:29 +08007312 //check cfun and sim card status
7313 ril_at_ready_process();
7314
7315 //any AT instruction that is not sent through pack_process_thread needs to precede the thread
7316 //thread create
liubin281ac462023-07-19 14:22:54 +08007317 if(sock_listen_fd > 0)
7318 {
7319 LOG("Information Server Has Started.");
7320 return -1;
7321 }
7322
7323 struct sockaddr_un server_addr;
7324 sock_listen_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
7325 if(sock_listen_fd < 0)
7326 {
7327 LOG("socket() fail[%d].", errno);
7328 return -1;
7329 }
7330
7331 // Set O_NONBLOCK
7332 int flags = fcntl(sock_listen_fd, F_GETFL, 0);
7333 if (flags < 0)
7334 {
7335 LOG("Get flags error:%d", errno);
7336 goto error;
7337 }
7338 flags |= O_NONBLOCK;
7339 if (fcntl(sock_listen_fd, F_SETFL, flags) < 0)
7340 {
7341 LOG("Set flags error:%d", errno);
7342 goto error;
7343 }
7344
7345 unlink(SOCK_INFO_PATH);
7346 memset(&server_addr, 0, sizeof(struct sockaddr_un));
7347 server_addr.sun_family = AF_LOCAL;
7348 strcpy(server_addr.sun_path, SOCK_INFO_PATH);
7349 if(bind(sock_listen_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)))
7350 {
7351 LOG("bind() fail[%d].", errno);
7352 goto error;
7353 }
7354
7355 if(listen(sock_listen_fd, SOCK_CLIENT_MAX))
7356 {
7357 LOG("listen() fail[%d].", errno);
7358 goto error;
7359 }
7360
7361 sock_client_list = list_create(sock_cli_free_func);
7362 if(sock_client_list == NULL)
7363 {
7364 LOG("list_create() fail.");
7365 goto error;
7366 }
7367
b.liu9e8584b2024-11-06 19:21:28 +08007368 pthread_t info_pid, pack_pid/*, monitor_pid, urc_pid*/, bootconn_pid;
liubin281ac462023-07-19 14:22:54 +08007369 pthread_attr_t thread_attr;
7370 pthread_attr_init(&thread_attr);
7371 if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
7372 {
7373 LOG("pthread_attr_setdetachstate() fail.");
7374 goto error;
7375 }
7376
7377 if(pthread_create(&info_pid, &thread_attr, info_main_pthread, NULL))
7378 {
7379 LOG("pthread_create() fail.");
7380 goto error;
7381 }
7382
7383 if(pthread_create(&pack_pid, &thread_attr, pack_process_thread, NULL))
7384 {
7385 LOG("pthread_create() fail.");
7386 goto error;
7387 }
7388
7389#if 0
7390 if(pthread_create(&urc_pid, &thread_attr, urc_process_thread, NULL))
7391 {
7392 LOG("pthread_create() fail.");
7393 goto error;
7394 }
7395#endif
7396
b.liubb5e7682024-02-28 20:13:04 +08007397 // Set Band
7398 // AT*BAND=15,78,147,482,134742231
7399 char buff[10];
7400 memset(buff, 0, 10);
7401 property_get("persist.mbtk.band_config", buff, "");
7402 if(strlen(buff) == 0) {
7403 pthread_t band_pid;
7404 if(pthread_create(&band_pid, &thread_attr, band_config_thread, NULL))
7405 {
7406 LOG("pthread_create() fail.");
7407 }
7408 }
7409
b.liuf1ab8152024-05-23 13:16:07 +08007410#if 0
liubin281ac462023-07-19 14:22:54 +08007411 if(pthread_create(&monitor_pid, &thread_attr, net_monitor_thread, NULL))
7412 {
7413 LOG("pthread_create() fail.");
7414 }
b.liuf1ab8152024-05-23 13:16:07 +08007415#endif
liubin281ac462023-07-19 14:22:54 +08007416
7417 //mbtk wyq for data_call_ex add start
7418 if(pthread_create(&bootconn_pid, &thread_attr, data_call_bootconn_pthread, NULL))
7419 {
7420 LOG("pthread_create() fail.");
7421 }
7422 //mbtk wyq for data_call_ex add end
7423
7424 pthread_attr_destroy(&thread_attr);
7425
wangyouqiang80487e42024-05-24 15:06:20 +08007426 mbtk_qser_apn_init();
7427
liubin281ac462023-07-19 14:22:54 +08007428 LOG("MBTK Information Server Start...");
7429
7430 return 0;
7431
7432error:
7433 close(sock_listen_fd);
7434 sock_listen_fd = -1;
7435 return -1;
7436}
7437
7438#if 0
7439int main(int argc, char *argv[])
7440{
7441 if(mbtk_info_server_start())
7442 {
7443 return -1;
7444 }
7445
7446 while(1)
7447 {
7448 sleep(24 * 60 * 60);
7449 }
7450
7451 return 0;
7452}
7453#endif
7454
7455