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