blob: 2b564eaa81410b92244b0ca31f70818f9e11c528 [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
4220mbtk_info_err_enum call_pack_req_process(sock_client_info_t* cli_info, mbtk_info_pack_t* pack);
4221mbtk_info_err_enum sms_pack_req_process(sock_client_info_t* cli_info, mbtk_info_pack_t* pack);
4222mbtk_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 -08004223
liubin281ac462023-07-19 14:22:54 +08004224//mbtk wyq for data_call_ex add start
4225void data_call_bootconn_save(int cid, int bootconn);
4226//mbtk wyq for data_call_ex add end
4227
4228//void net_list_free(void *data);
4229// Return MBTK_INFO_ERR_SUCCESS,will call pack_error_send() to send RSP.
4230// Otherwise, do not call pack_error_send().
4231static mbtk_info_err_enum pack_req_process(sock_client_info_t* cli_info, mbtk_info_pack_t* pack)
4232{
4233 if(pack->info_id > MBTK_INFO_ID_CALL_BEGIN && pack->info_id < MBTK_INFO_ID_CALL_END) {
4234 return call_pack_req_process(cli_info, pack);
4235 } else if(pack->info_id > MBTK_INFO_ID_SMS_BEGIN && pack->info_id < MBTK_INFO_ID_SMS_END) {
4236 return sms_pack_req_process(cli_info, pack);
4237 } else if(pack->info_id > MBTK_INFO_ID_PB_BEGIN && pack->info_id < MBTK_INFO_ID_PB_END) {
4238 return pb_pack_req_process(cli_info, pack);
b.liu06559f62024-11-01 18:48:22 +08004239 } else if(pack->info_id > RIL_MSG_ID_ECALL_BEGIN && pack->info_id < RIL_MSG_ID_ECALL_END) {
4240 return ecall_pack_req_process(cli_info, pack);
liubin281ac462023-07-19 14:22:54 +08004241 } else {
4242 mbtk_info_err_enum err = MBTK_INFO_ERR_SUCCESS;
4243 int cme_err = MBTK_INFO_ERR_CME_NON;
4244 switch(pack->info_id)
4245 {
4246 case MBTK_INFO_ID_DEV_IMEI_REQ: // <string> IMEI
4247 {
4248 if(pack->data_len == 0 || pack->data == NULL) // Get IMEI
4249 {
4250 char imei[20] = {0};
4251 if(req_imei_get(imei, &cme_err) || strlen(imei) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4252 {
4253 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4254 err = MBTK_INFO_ERR_CME + cme_err;
4255 } else {
4256 err = MBTK_INFO_ERR_UNKNOWN;
4257 }
4258 LOG("Get IMEI fail.");
4259 }
4260 else
4261 {
4262 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_IMEI_RSP, imei, strlen(imei));
4263 }
4264 }
4265 else // Set IMEI(Unsupport).
4266 {
4267 err = MBTK_INFO_ERR_UNSUPPORTED;
4268 LOG("Unsupport set IMEI.");
4269 }
4270 break;
4271 }
4272 case MBTK_INFO_ID_DEV_SN_REQ: // <string> SN
4273 {
4274 if(pack->data_len == 0 || pack->data == NULL) // Get SN
4275 {
4276 char sn[20] = {0};
4277 if(req_sn_get(sn, &cme_err) || strlen(sn) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4278 {
4279 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4280 err = MBTK_INFO_ERR_CME + cme_err;
4281 } else {
4282 err = MBTK_INFO_ERR_UNKNOWN;
4283 }
4284 LOG("Get SN fail.");
4285 }
4286 else
4287 {
4288 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_SN_RSP, sn, strlen(sn));
4289 }
4290 }
4291 else // Set SN(Unsupport).
4292 {
4293 err = MBTK_INFO_ERR_UNSUPPORTED;
4294 LOG("Unsupport set SN.");
4295 }
4296 break;
4297 }
4298 case MBTK_INFO_ID_DEV_MEID_REQ: // <string> MEID (Only for CDMA)
4299 {
4300 if(pack->data_len == 0 || pack->data == NULL) // Get MEID
4301 {
4302 err = MBTK_INFO_ERR_UNSUPPORTED;
4303 LOG("Support only for CDMA.");
4304 }
4305 else // Set MEID(Unsupport).
4306 {
4307 err = MBTK_INFO_ERR_UNSUPPORTED;
4308 LOG("Unsupport set MEID.");
4309 }
4310 break;
4311 }
4312 case MBTK_INFO_ID_DEV_VERSION_REQ: // <string> VERSION
4313 {
4314 if(pack->data_len == 0 || pack->data == NULL) // Get VERSION
4315 {
4316 char version[50] = {0};
4317 if(req_version_get(version, &cme_err) || strlen(version) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4318 {
4319 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4320 err = MBTK_INFO_ERR_CME + cme_err;
4321 } else {
4322 err = MBTK_INFO_ERR_UNKNOWN;
4323 }
4324 LOG("Get Version fail.");
4325 }
4326 else
4327 {
4328 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_VERSION_RSP, version, strlen(version));
4329 }
4330 }
4331 else // Set VERSION(Unsupport).
4332 {
4333 err = MBTK_INFO_ERR_UNSUPPORTED;
4334 LOG("Unsupport set VERSION.");
4335 }
4336 break;
4337 }
l.yang5b0ff422024-10-29 19:33:35 -07004338 case MBTK_INFO_ID_DEV_MD_VERSION_REQ:
4339 {
4340 if(pack->data_len == 0 || pack->data == NULL) // Get VERSION
4341 {
4342 char version[50] = {0};
4343 if(req_md_version_get(version, &cme_err) || strlen(version) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4344 {
4345 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4346 err = MBTK_INFO_ERR_CME + cme_err;
4347 } else {
4348 err = MBTK_INFO_ERR_UNKNOWN;
4349 }
4350 LOG("Get Version fail.");
4351 }
4352 else
4353 {
4354 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_MD_VERSION_RSP, version, strlen(version));
4355 }
4356 }
4357 else // Set MD_VERSION(Unsupport).
4358 {
4359 err = MBTK_INFO_ERR_UNSUPPORTED;
4360 LOG("Unsupport set MD_VERSION.");
4361 }
4362 break;
4363
4364 }
liubin281ac462023-07-19 14:22:54 +08004365 case MBTK_INFO_ID_DEV_MODEL_REQ: //MODEL
4366 {
4367 if(pack->data_len == 0 || pack->data == NULL) // Get MODEL
4368 {
4369 char model[50] = {0};
4370 if(req_model_get(model, &cme_err) || strlen(model) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4371 {
4372 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4373 err = MBTK_INFO_ERR_CME + cme_err;
4374 } else {
4375 err = MBTK_INFO_ERR_UNKNOWN;
4376 }
4377 LOG("Get model fail.");
4378 }
4379 else
4380 {
4381 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_VERSION_RSP, model, strlen(model));
4382 }
4383 }
4384 else // Set model(Unsupport).
4385 {
4386 err = MBTK_INFO_ERR_UNSUPPORTED;
4387 LOG("Unsupport set model.");
4388 }
4389 break;
4390 }
4391 case MBTK_INFO_ID_DEV_MODEM_REQ: //MODEM
4392 {
4393 if(pack->data_len == 0 || pack->data == NULL) // Get MODEM
4394 {
4395 int modem = -1;
4396 if(req_modem_get(&modem, &cme_err) || modem < 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4397 {
4398 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4399 err = MBTK_INFO_ERR_CME + cme_err;
4400 } else {
4401 err = MBTK_INFO_ERR_UNKNOWN;
4402 }
4403 LOG("Get modem fail.");
4404 }
4405 else
4406 {
4407 uint8 modem_type = (uint8)modem;
4408 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_MODEM_RSP, &modem_type, sizeof(uint8));
4409 }
4410 }
4411 else // Set modem
4412 {
4413 mbtk_modem_info_t *modem = (mbtk_modem_info_t *)pack->data;
4414 if(pack->data_len != sizeof(mbtk_modem_info_t))
4415 {
4416 err = MBTK_INFO_ERR_REQ_PARAMETER;
4417 LOG("Set modem error.");
4418 break;
4419 }
4420 if(req_modem_set(modem, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4421 {
4422 LOG("Set modem fail.");
4423 err = MBTK_INFO_ERR_FORMAT;
4424 } else {
4425 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_MODEM_RSP, NULL, 0);
4426 }
4427 }
4428 break;
4429 }
4430 case MBTK_INFO_ID_DEV_TIME_REQ: // <uint8><string> YYYY-MM-DD-HH:MM:SS
4431 {
4432 if(pack->data_len == 0 || pack->data == NULL) // Get Time
4433 {
4434 int type = -1;
4435 if(req_time_get(&type, &cme_err) || type < 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4436 {
4437 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4438 err = MBTK_INFO_ERR_CME + cme_err;
4439 } else {
4440 err = MBTK_INFO_ERR_UNKNOWN;
4441 }
4442 LOG("Get Time fail.");
4443 }
4444 else
4445 {
4446 uint8 time_type = (uint8)type;
4447 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_TIME_RSP, &time_type, sizeof(uint8));
4448 }
4449 }
4450 else // Set Time
4451 {
4452 if(pack->data_len == sizeof(uint8)) {
4453 if(req_time_set(*(pack->data), NULL, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4454 {
4455 LOG("Set Time fail.");
4456 err = MBTK_INFO_ERR_FORMAT;
4457 } else {
4458 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_TIME_RSP, NULL, 0);
4459 }
4460 } else {
4461 char time_ptr[100] = {0};
4462 memcpy(time_ptr, pack->data + sizeof(uint8), pack->data_len - sizeof(uint8));
4463 if(req_time_set(*(pack->data), time_ptr, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4464 {
4465 LOG("Set Time fail.");
4466 err = MBTK_INFO_ERR_FORMAT;
4467 } else {
4468 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_TIME_RSP, NULL, 0);
4469 }
4470 }
4471 }
4472 break;
4473 }
b.liubaa41e12024-07-19 15:07:24 +08004474 case MBTK_INFO_ID_DEV_CELL_TIME_REQ: // <string> YYYY-MM-DD-HH:MM:SS
liubin281ac462023-07-19 14:22:54 +08004475 {
4476 if(pack->data_len == 0 || pack->data == NULL) // Get Time
4477 {
4478 char time[100];
4479 if(req_net_time_get(time, &cme_err) || strlen(time) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4480 {
4481 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4482 err = MBTK_INFO_ERR_CME + cme_err;
4483 } else {
4484 err = MBTK_INFO_ERR_UNKNOWN;
4485 }
4486 LOG("Get Time fail.");
4487 }
4488 else
4489 {
4490 char time_ser[100]={0};
4491 memcpy(time_ser,time,strlen(time));
b.liubaa41e12024-07-19 15:07:24 +08004492 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_CELL_TIME_RSP, time_ser, strlen(time_ser));
liubin281ac462023-07-19 14:22:54 +08004493 }
4494 }
4495 else // Set Time
4496 {
4497 err = MBTK_INFO_ERR_UNSUPPORTED;
4498 LOG("Unsupport set TIME.");
4499 }
4500 break;
4501 }
4502 case MBTK_INFO_ID_DEV_VOLTE_REQ: // <uint8> 0:Close 1:Open
4503 {
4504 if(pack->data_len == 0 || pack->data == NULL) // Get VoLTE state.
4505 {
4506 int state;
4507 if(req_volte_get(&state, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4508 {
4509 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4510 err = MBTK_INFO_ERR_CME + cme_err;
4511 } else {
4512 err = MBTK_INFO_ERR_UNKNOWN;
4513 }
4514 LOG("Get VoLTE state fail.");
4515 }
4516 else
4517 {
4518 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_VOLTE_RSP, &state, sizeof(uint8));
4519 }
4520 }
4521 else // Set VoLTE state.
4522 {
4523 uint8 on = *(pack->data);
4524 if(pack->data_len != sizeof(uint8) || (on != 0 && on != 1))
4525 {
4526 err = MBTK_INFO_ERR_REQ_PARAMETER;
4527 LOG("Set VOLTE parameter error.");
4528 break;
4529 }
4530
4531 if(req_volte_set(on, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4532 {
4533 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4534 err = MBTK_INFO_ERR_CME + cme_err;
4535 } else {
4536 err = MBTK_INFO_ERR_UNKNOWN;
4537 }
4538 LOG("Set VoLTE state fail.");
4539 }
4540 else
4541 {
4542 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_VOLTE_RSP, NULL, 0);
4543
4544 // Restart is required to take effect.
4545 LOG("Will reboot system...");
4546 }
4547 }
4548 break;
4549 }
4550 case MBTK_INFO_ID_DEV_TEMP_REQ: // <string> Temperature
4551 {
4552 if(pack->data_len == sizeof(uint8) && pack->data) {
r.xiao2102d762024-06-07 03:10:38 -07004553 mbtk_thermal_info_t temp;
4554 memset(&temp, 0, sizeof(mbtk_thermal_info_t));
liubin281ac462023-07-19 14:22:54 +08004555 if(req_temp_get(*(pack->data), &temp, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4556 {
4557 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4558 err = MBTK_INFO_ERR_CME + cme_err;
4559 } else {
4560 err = MBTK_INFO_ERR_UNKNOWN;
4561 }
4562 LOG("Get temperature fail.");
4563 }
4564 else
4565 {
r.xiao2102d762024-06-07 03:10:38 -07004566 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_DEV_TEMP_RSP, &temp, sizeof(mbtk_thermal_info_t));
liubin281ac462023-07-19 14:22:54 +08004567 }
4568 } else {
4569 err = MBTK_INFO_ERR_FORMAT;
4570 LOG("Unsupport get Temperature.");
4571 }
4572 break;
4573 }
4574 case MBTK_INFO_ID_SIM_PLMN_REQ: // plmn
4575 {
4576 if(pack->data_len == 0 || pack->data == NULL) // plmn
4577 {
4578 mbtk_plmn_info plmn;
4579 if(req_plmn_get(&plmn, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4580 {
4581 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4582 err = MBTK_INFO_ERR_CME + cme_err;
4583 } else {
4584 err = MBTK_INFO_ERR_UNKNOWN;
4585 }
4586 LOG("Get PLMN fail.");
4587 }
4588 else
4589 {
4590 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_PLMN_RSP, &plmn, sizeof(mbtk_plmn_info));
4591 }
4592 }
4593 else // Set
4594 {
4595 err = MBTK_INFO_ERR_UNSUPPORTED;
4596 LOG("Set sim state fail.");
4597 }
4598 break;
4599 }
4600 case MBTK_INFO_ID_SIM_STATE_REQ: // mbtk_sim_state_enum
4601 {
4602 if(pack->data_len == 0 || pack->data == NULL) // Get sim state.
4603 {
4604 uint8 sim_state = (uint8)getSIMStatus();
4605 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_STATE_RSP, &sim_state, sizeof(uint8));
4606 }
4607 else // Set
4608 {
4609 err = MBTK_INFO_ERR_UNSUPPORTED;
4610 LOG("Set sim state fail.");
4611 }
4612 break;
4613 }
4614 case MBTK_INFO_ID_SIM_STYPE_REQ: // mbtk_sim_card_type_enum
4615 {
4616 if(pack->data_len == 0 || pack->data == NULL) // Get sim card type
4617 {
4618 uint8 sim_card_type;
4619 if(req_sim_card_type_get(&sim_card_type, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4620 {
4621 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4622 err = MBTK_INFO_ERR_CME + cme_err;
4623 } else {
4624 err = MBTK_INFO_ERR_UNKNOWN;
4625 }
4626 LOG("Get IMSI fail.");
4627 }
4628 else
4629 {
4630 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_STYPE_RSP, &sim_card_type, sizeof(uint8));
4631 }
4632 }
4633 else // Set
4634 {
4635 err = MBTK_INFO_ERR_UNSUPPORTED;
4636 LOG("Set sim state fail.");
4637 }
4638 break;
4639 }
4640 case MBTK_INFO_ID_SIM_PINPUK_TIMES_REQ: // mbtk_pin_puk_last_times
4641 {
4642 if(pack->data_len == 0 || pack->data == NULL) // Get sim card type
4643 {
4644 mbtk_pin_puk_last_times pin_puk_last_times;
4645 if(req_pin_puk_last_times_get(&pin_puk_last_times, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4646 {
4647 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4648 err = MBTK_INFO_ERR_CME + cme_err;
4649 } else {
4650 err = MBTK_INFO_ERR_UNKNOWN;
4651 }
4652 LOG("Get IMSI fail.");
4653 }
4654 else
4655 {
4656 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_PINPUK_TIMES_RSP, &pin_puk_last_times, sizeof(mbtk_pin_puk_last_times));
4657 }
4658 }
4659 else // Set
4660 {
4661 err = MBTK_INFO_ERR_UNSUPPORTED;
4662 LOG("Set sim state fail.");
4663 }
4664 break;
4665 }
4666 case MBTK_INFO_ID_SIM_ENABLE_PIN_REQ: // <string> PIN
4667 {
4668 if(pack->data_len == 0 || pack->data == NULL) // Enable PIN
4669 {
yq.wang586a0df2024-10-24 20:10:37 -07004670 mbtk_pin_state_enum pin_state = MBTK_PIN_DISABLE;
4671 if(req_get_pin_state(&pin_state, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4672 {
4673 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4674 err = MBTK_INFO_ERR_CME + cme_err;
4675 } else {
4676 err = MBTK_INFO_ERR_UNKNOWN;
4677 }
4678 LOGE("Get pin state fail.");
4679 }
4680 else
4681 {
4682 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_ENABLE_PIN_RSP, &pin_state, sizeof(mbtk_pin_state_enum));
4683 }
liubin281ac462023-07-19 14:22:54 +08004684 }
4685 else // Enable PIN
4686 {
4687 mbtk_enable_pin_info *pin = NULL;
4688 pin = (mbtk_enable_pin_info *)pack->data;
4689 if(req_pin_enable(pin, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4690 {
4691 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4692 err = MBTK_INFO_ERR_CME + cme_err;
4693 } else {
4694 err = MBTK_INFO_ERR_UNKNOWN;
4695 }
4696 LOG("Get IMSI fail.");
4697 }
4698 else
4699 {
4700 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_ENABLE_PIN_RSP, NULL, 0);
4701 }
4702 }
4703 break;
4704 }
4705 case MBTK_INFO_ID_SIM_PIN_REQ: // <string> PIN
4706 {
4707 if(pack->data_len == 0 || pack->data == NULL) // PIN
4708 {
4709 err = MBTK_INFO_ERR_UNSUPPORTED;
4710 LOG("Unsupport GET PIN.");
4711 }
4712 else // Set PIN
4713 {
4714 char pin[16] = {0};
4715 memcpy(pin, pack->data, pack->data_len);
4716 if(req_pin_verify(pin, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4717 {
4718 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4719 err = MBTK_INFO_ERR_CME + cme_err;
4720 } else {
4721 err = MBTK_INFO_ERR_UNKNOWN;
4722 }
4723 LOG("Set PIN fail.");
4724 }
4725 else
4726 {
4727 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_PIN_RSP, NULL, 0);
4728 }
4729 }
4730 break;
4731 }
4732 case MBTK_INFO_ID_SIM_PUK_REQ: // <string> PUK
4733 {
4734 if(pack->data_len == 0 || pack->data == NULL)
4735 {
4736 err = MBTK_INFO_ERR_UNSUPPORTED;
4737 LOG("Unsupport.");
4738 }
4739 else // change PIN
4740 {
4741 mbtk_unlock_pin_info *pin_info = NULL;
4742 pin_info = (mbtk_unlock_pin_info *)pack->data;
4743 if(req_puk_unlock_pin(pin_info, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4744 {
4745 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4746 err = MBTK_INFO_ERR_CME + cme_err;
4747 } else {
4748 err = MBTK_INFO_ERR_UNKNOWN;
4749 }
4750 LOG("Get IMSI fail.");
4751 }
4752 else
4753 {
4754 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_CHANGE_PIN_RSP, NULL, 0);
4755 }
4756 }
4757 break;
4758 }
4759 case MBTK_INFO_ID_SIM_CHANGE_PIN_REQ: // <string> <string> old_PIN new_PIN
4760 {
4761 if(pack->data_len == 0 || pack->data == NULL)
4762 {
4763 err = MBTK_INFO_ERR_UNSUPPORTED;
4764 LOG("Unsupport.");
4765 }
4766 else // change PIN
4767 {
4768 mbtk_change_pin_info *pin_info = NULL;
4769 pin_info = (mbtk_change_pin_info *)pack->data;
4770 if(req_pin_change(pin_info, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4771 {
4772 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4773 err = MBTK_INFO_ERR_CME + cme_err;
4774 } else {
4775 err = MBTK_INFO_ERR_UNKNOWN;
4776 }
4777 LOG("Get IMSI fail.");
4778 }
4779 else
4780 {
4781 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_CHANGE_PIN_RSP, NULL, 0);
4782 }
4783 }
4784 break;
4785 }
4786 case MBTK_INFO_ID_SIM_IMSI_REQ: // <string> IMSI
4787 {
4788 if(pack->data_len == 0 || pack->data == NULL) // Get IMSI
4789 {
4790 char imsi[20] = {0};
4791 if(req_imsi_get(imsi, &cme_err) || strlen(imsi) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4792 {
4793 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4794 err = MBTK_INFO_ERR_CME + cme_err;
4795 } else {
4796 err = MBTK_INFO_ERR_UNKNOWN;
4797 }
4798 LOG("Get IMSI fail.");
4799 }
4800 else
4801 {
4802 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_IMSI_RSP, imsi, strlen(imsi));
4803 }
4804 }
4805 else // Set IMSI(Unsupport).
4806 {
4807 err = MBTK_INFO_ERR_UNSUPPORTED;
4808 LOG("Unsupport set IMSI.");
4809 }
4810 break;
4811 }
4812 case MBTK_INFO_ID_SIM_ICCID_REQ: // <string> ICCID
4813 {
4814 if(pack->data_len == 0 || pack->data == NULL) // Get ICCID
4815 {
4816 //log_hex("pack", pack, sizeof(mbtk_info_pack_t));
4817 //sleep(1);
4818 char iccid[50] = {0};
4819 if(req_iccid_get(iccid, &cme_err) || strlen(iccid) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4820 {
4821 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4822 err = MBTK_INFO_ERR_CME + cme_err;
4823 } else {
4824 err = MBTK_INFO_ERR_UNKNOWN;
4825 }
4826 LOG("Get ICCID fail.");
4827 }
4828 else
4829 {
4830 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_ICCID_RSP, iccid, strlen(iccid));
4831 }
4832 }
4833 else // Set ICCID(Unsupport).
4834 {
4835 err = MBTK_INFO_ERR_UNSUPPORTED;
4836 LOG("Unsupport set ICCID.");
4837 }
4838 break;
4839 }
4840 case MBTK_INFO_ID_SIM_PN_REQ: // <string> Phone Number
4841 {
4842 if(pack->data_len == 0 || pack->data == NULL) // Get Phone Number
4843 {
4844 //log_hex("pack", pack, sizeof(mbtk_info_pack_t));
4845 //sleep(1);
4846 char phone_number[50] = {0};
4847 if(req_phone_number_get(phone_number, &cme_err) || strlen(phone_number) == 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4848 {
4849 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4850 err = MBTK_INFO_ERR_CME + cme_err;
4851 } else {
4852 err = MBTK_INFO_ERR_UNKNOWN;
4853 }
4854 LOG("Get Phone Number fail.");
4855 }
4856 else
4857 {
4858 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_SIM_PN_RSP, phone_number, strlen(phone_number));
4859 }
4860 }
4861 else // Set Phone Number(Unsupport).
4862 {
4863 err = MBTK_INFO_ERR_UNSUPPORTED;
4864 LOG("Unsupport set Phone Number.");
4865 }
4866 break;
4867 }
4868 case MBTK_INFO_ID_NET_SEL_MODE_REQ: // <mbtk_net_info_t> Operator
4869 {
4870 if(pack->data_len == 0 || pack->data == NULL) // Get
4871 {
4872 mbtk_net_info_t info;
4873 memset(&info, 0, sizeof(mbtk_net_info_t));
4874 if(req_net_sel_mode_get(&info, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4875 {
4876 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4877 err = MBTK_INFO_ERR_CME + cme_err;
4878 } else {
4879 err = MBTK_INFO_ERR_UNKNOWN;
4880 }
4881 LOG("Get Net select mode fail.");
4882 }
4883 else
4884 {
4885 LOG("NET : %d, %d, %d, %d", info.net_sel_mode, info.net_type, info.net_state, info.plmn);
4886 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_SEL_MODE_RSP, &info, sizeof(mbtk_net_info_t));
4887 }
4888 }
4889 else // Set
4890 {
4891 //LOG("1 pack-%p,data-%p,data_len-%d", pack, pack->data, pack->data_len);
4892 //log_hex("pack", pack, sizeof(mbtk_info_pack_t));
4893 //log_hex("data", pack->data, pack->data_len);
4894
4895 mbtk_net_info_t* info = (mbtk_net_info_t*)pack->data;//(mbtk_net_info_t*)mbtk_info_pack_data_get(pack, NULL);
4896 if(info == NULL) {
4897 err = MBTK_INFO_ERR_FORMAT;
4898 LOG("Get Net select mode fail.");
4899 } else {
4900 LOG("NET : %d, %d, %d", info->net_sel_mode, info->net_type, info->plmn);
4901 if(req_net_sel_mode_set(info, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON) {
4902 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4903 err = MBTK_INFO_ERR_CME + cme_err;
4904 } else {
4905 err = MBTK_INFO_ERR_UNKNOWN;
4906 }
4907 LOG("Get Net select mode fail.");
4908 } else {
4909 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_SEL_MODE_RSP, NULL, 0);
4910 }
4911 }
4912 }
4913 break;
4914 }
4915 case MBTK_INFO_ID_NET_AVAILABLE_REQ:// sel_mode(uint8)type(uint8)plmn(uint32)...sel_mode(uint8)type(uint8)plmn(uint32)
4916 {
4917 if(pack->data_len == 0 || pack->data == NULL) // Get Available Net.
4918 {
4919 int buffer_size;
4920 uint8 buffer[SOCK_MSG_LEN_MAX];
4921 memset(buffer, 0, SOCK_MSG_LEN_MAX);
4922 if((buffer_size = req_available_net_get(buffer, &cme_err)) <= 0 || cme_err != MBTK_INFO_ERR_CME_NON)
4923 {
4924 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4925 err = MBTK_INFO_ERR_CME + cme_err;
4926 } else {
4927 err = MBTK_INFO_ERR_UNKNOWN;
4928 }
4929 LOG("Get Available Net fail.");
4930 }
4931 else
4932 {
4933 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_AVAILABLE_RSP, buffer, buffer_size);
4934 }
4935 }
4936 else // Set Available Net(Unsupport).
4937 {
4938 err = MBTK_INFO_ERR_UNSUPPORTED;
4939 LOG("Unsupport set available net.");
4940 }
4941 break;
4942 }
4943 case MBTK_INFO_ID_NET_BAND_REQ:
4944 {
4945 if(pack->data_len == 0 || pack->data == NULL)
4946 {
4947 err = MBTK_INFO_ERR_REQ_PARAMETER;
4948 LOG("No data found.");
4949 }
4950 else // Get support/current bands.
4951 {
4952 if(pack->data_len == sizeof(uint8)) {
4953 if(*(pack->data)) { // Get current bands.
4954 mbtk_band_info_t band;
b.liu288093c2024-05-09 17:02:57 +08004955 memset(&band, 0x0, sizeof(mbtk_band_info_t));
liubin281ac462023-07-19 14:22:54 +08004956 if(req_band_get(&band, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4957 {
4958 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4959 err = MBTK_INFO_ERR_CME + cme_err;
4960 } else {
4961 err = MBTK_INFO_ERR_UNKNOWN;
4962 }
4963 LOG("Get net band fail.");
4964 }
4965 else
4966 {
4967 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_BAND_RSP, &band, sizeof(mbtk_band_info_t));
4968 }
4969 } else { // Get support bands.
4970 band_support_get();
4971 if(band_support.net_pref != 0)
4972 {
4973 err = MBTK_INFO_ERR_UNKNOWN;
4974 LOG("Get support bands fail.");
4975 }
4976 else
4977 {
4978 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_BAND_RSP, &band_support, sizeof(mbtk_band_info_t));
4979 }
4980 }
4981 } else { // Set current bands.
4982 mbtk_band_info_t* band = (mbtk_band_info_t*)pack->data;
4983 if(pack->data_len != sizeof(mbtk_band_info_t))
4984 {
4985 err = MBTK_INFO_ERR_REQ_PARAMETER;
4986 LOG("Set net band error.");
4987 break;
4988 }
4989
4990 if(req_band_set(band, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
4991 {
4992 if(cme_err != MBTK_INFO_ERR_CME_NON) {
4993 err = MBTK_INFO_ERR_CME + cme_err;
4994 } else {
4995 err = MBTK_INFO_ERR_UNKNOWN;
4996 }
4997 LOG("Set net band fail.");
4998 }
4999 else
5000 {
5001 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_BAND_RSP, NULL, 0);
5002 }
5003 }
5004 }
5005 break;
5006 }
5007 case MBTK_INFO_ID_NET_CELL_REQ: // mbtk_cell_info_t[]
5008 {
5009 if(pack->data_len == 0 || pack->data == NULL) // Get net cell.
5010 {
5011 if(req_cell_info_get(&cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5012 {
5013 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5014 err = MBTK_INFO_ERR_CME + cme_err;
5015 } else {
5016 err = MBTK_INFO_ERR_UNKNOWN;
5017 }
5018 LOG("Get net cell fail.");
5019 }
5020 else
5021 {
5022 LOG("req_cell_info_get() success,cell number: %d", cell_info.cell_num);
5023 //sleep(1);
5024 // mbtK_cell_pack_info_t
5025 if(cell_info.cell_num > 0 && cell_info.cell_num <= CELL_NUM_MAX && cell_info.type >= 0 && cell_info.type <= 2) {
5026 uint8 *data = (uint8*)malloc(sizeof(uint8) + sizeof(mbtk_cell_info_t) * cell_info.cell_num);
5027 if(data == NULL){
5028 err = MBTK_INFO_ERR_MEMORY;
5029 LOG("Get net cell fail.");
5030 } else {
5031 *data = cell_info.type; // Set network type.
5032 // Copy cell info item.
5033 #if 0
5034 int i = 0;
5035 while(i < cell_info.cell_num) {
5036 memcpy(data + sizeof(uint8) + sizeof(mbtk_cell_info_t) * i,
5037 &(cell_info.cell[i]),
5038 sizeof(mbtk_cell_info_t));
5039 i++;
5040 }
5041 #else
5042 memcpy(data + sizeof(uint8),
5043 &(cell_info.cell),
5044 sizeof(mbtk_cell_info_t) * cell_info.cell_num);
5045 #endif
5046 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_CELL_RSP, data, sizeof(uint8) + sizeof(mbtk_cell_info_t) * cell_info.cell_num);
5047 free(data);
5048 }
5049 } else {
5050 err = MBTK_INFO_ERR_UNKNOWN;
5051 LOG("Get net cell fail.");
5052 }
5053 }
5054 }
5055 else // Lock cell
5056 {
5057 char *mem = (char*)(pack->data);
5058 int len = pack->data_len;
5059 char reg[100] = {0};
5060 printf("mem:%s, len:%d", pack->data, pack->data_len);
5061
5062 if(req_cell_info_set(mem, reg, len, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5063 {
5064 // printf("cpms_set fail\n");
5065 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5066 err = MBTK_INFO_ERR_CME + cme_err;
5067 } else {
5068 err = MBTK_INFO_ERR_UNKNOWN;
5069 }
5070 // LOG("Set req_cell_info_set fail.");
5071 }
5072 else
5073 {
5074
5075 printf("req_cell_info_set success, reg:%s\n", reg);
5076 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_CELL_RSP, reg, strlen(reg));
5077
5078 // Restart is required to take effect.
5079 LOG("Will reboot system...");
5080 }
5081 }
5082 break;
5083 }
5084 case MBTK_INFO_ID_NET_RADIO_REQ: // <uint8> 0:OFF 1:ON
5085 {
5086 if(pack->data_len == 0 || pack->data == NULL) // Get
5087 {
5088 uint8 radio_on = (uint8)isRadioOn();
5089 if(radio_on < 0)
5090 {
5091 err = MBTK_INFO_ERR_UNKNOWN;
5092 LOG("Get radio state fail.");
5093 }
5094 else
5095 {
5096 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_RADIO_RSP, &radio_on, sizeof(uint8));
5097 }
5098 }
5099 else // Set
5100 {
yq.wangd58f71e2024-08-21 23:45:31 -07005101 at_cfun_command = true;
liubin281ac462023-07-19 14:22:54 +08005102 uint8 radio_on = *(pack->data);
5103 if(radio_on != 0 && radio_on != 1)
5104 {
5105 err = MBTK_INFO_ERR_REQ_PARAMETER;
5106 LOG("Set radio state fail.");
5107 }
5108 else
5109 {
5110 setRadioPower(radio_on);
5111 if((radio_on && net_info.radio_state == MBTK_RADIO_STATE_ON)
5112 || (!radio_on && net_info.radio_state == MBTK_RADIO_STATE_OFF)) {
5113 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_RADIO_RSP, NULL, 0);
5114 } else {
5115 err = MBTK_INFO_ERR_UNKNOWN;
5116 LOG("Set radio state fail.");
5117 }
5118 }
yq.wangd58f71e2024-08-21 23:45:31 -07005119 at_cfun_command = false;
liubin281ac462023-07-19 14:22:54 +08005120 }
5121 break;
5122 }
5123 case MBTK_INFO_ID_NET_SIGNAL_REQ: // mbtk_signal_info_t
5124 {
5125 if(pack->data_len == 0 || pack->data == NULL) // Get net signal.
5126 {
5127 mbtk_signal_info_t signal;
5128 memset(&signal, 0, sizeof(mbtk_signal_info_t));
5129 if(req_net_signal_get(&signal, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5130 {
5131 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5132 err = MBTK_INFO_ERR_CME + cme_err;
5133 } else {
5134 err = MBTK_INFO_ERR_UNKNOWN;
5135 }
5136 LOG("Get net signal fail.");
5137 }
5138 else
5139 {
5140 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_SIGNAL_RSP, &signal, sizeof(mbtk_signal_info_t));
5141 }
5142 }
5143 else // Set
5144 {
5145 err = MBTK_INFO_ERR_UNSUPPORTED;
5146 LOG("Set net signal fail.");
5147 }
5148 break;
5149 }
5150 case MBTK_INFO_ID_NET_REG_REQ: // mbtk_net_reg_info_t
5151 {
5152 if(pack->data_len == 0 || pack->data == NULL) // Get net reg.
5153 {
5154 mbtk_net_reg_info_t reg;
5155 memset(&reg, 0, sizeof(mbtk_net_reg_info_t));
5156 if(req_net_reg_get(&reg, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5157 {
5158 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5159 err = MBTK_INFO_ERR_CME + cme_err;
5160 } else {
5161 err = MBTK_INFO_ERR_UNKNOWN;
5162 }
5163 LOG("Get net reg fail.");
5164 }
5165 else
5166 {
5167 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_REG_RSP, &reg, sizeof(mbtk_net_reg_info_t));
5168 }
5169 }
5170 else // Set
5171 {
5172 err = MBTK_INFO_ERR_UNSUPPORTED;
5173 LOG("Set net reg fail.");
5174 }
5175 break;
5176 }
5177 case MBTK_INFO_ID_NET_APN_REQ: // mbtk_apn_info_t
5178 {
5179 if(pack->data_len == 0 || pack->data == NULL) // Get APN
5180 {
5181 uint8 buff[SOCK_MSG_LEN_MAX];
5182 memset(buff, 0, SOCK_MSG_LEN_MAX);
5183 int data_len = 0;
5184 if(req_apn_get(buff, &data_len, &cme_err) || data_len <= 0 || cme_err != MBTK_INFO_ERR_CME_NON)
5185 {
5186 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5187 err = MBTK_INFO_ERR_CME + cme_err;
5188 } else {
5189 err = MBTK_INFO_ERR_UNKNOWN;
5190 }
5191 LOG("Get APN fail.");
5192 }
5193 else
5194 {
5195 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_APN_RSP, buff, data_len);
5196 }
5197 }
5198 else // Set
5199 {
5200 // <cid[1]><ip_type[1]><apn_len[2]><apn><user_len[2]><user><pass_len[2]><pass><auth_len[2]><auth>
5201 const uint8* ptr = pack->data;
5202 mbtk_apn_info_t apn;
5203 int len;
5204 memset(&apn, 0, sizeof(mbtk_apn_info_t));
5205 // cid
5206 apn.cid = *ptr++;
5207
5208 // ip_type
5209 apn.ip_type = (mbtk_ip_type_enum)(*ptr++);
5210
5211 // apn
5212 len = byte_2_uint16(ptr, false);
5213 ptr += sizeof(uint16);
5214 if(len > 0) {
5215 memcpy(apn.apn, ptr, len);
5216 ptr += len;
5217 }
5218
5219 // user
5220 len = byte_2_uint16(ptr, false);
5221 ptr += sizeof(uint16);
5222 if(len > 0) {
5223 memcpy(apn.user, ptr, len);
5224 ptr += len;
5225 }
5226
5227 // pass
5228 len = byte_2_uint16(ptr, false);
5229 ptr += sizeof(uint16);
5230 if(len > 0) {
5231 memcpy(apn.pass, ptr, len);
5232 ptr += len;
5233 }
5234
5235 // auth
5236 len = byte_2_uint16(ptr, false);
5237 ptr += sizeof(uint16);
5238 if(len > 0) {
5239 memcpy(apn.auth, ptr, len);
5240 ptr += len;
5241 }
5242
b.liu9e8584b2024-11-06 19:21:28 +08005243 LOGD("APN : %d, %d, %s, %s, %s, %s", apn.cid, apn.ip_type, str_empty(apn.apn) ? "NULL" : (char*)apn.apn,
5244 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 +08005245 if(req_apn_set(&apn, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5246 {
5247 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5248 err = MBTK_INFO_ERR_CME + cme_err;
5249 } else {
5250 err = MBTK_INFO_ERR_UNKNOWN;
5251 }
5252 LOG("Set APN fail.");
5253 }
5254 else
5255 {
5256 // Save apn.
wangyouqianged88c722023-11-22 16:33:43 +08005257#ifdef MBTK_AF_SUPPORT
5258 if(apn.cid == 1)
5259 {
5260 default_iptype = apn.ip_type;
5261 }
5262#endif
liubin281ac462023-07-19 14:22:54 +08005263 apn_prop_set(&apn);
5264
5265 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_APN_RSP, NULL, 0);
5266 }
5267 }
5268 break;
5269 }
wangyouqiang80487e42024-05-24 15:06:20 +08005270 case MBTK_INFO_ID_NET_QSER_APN_REQ:
5271 {
5272 if(pack->data_len == 0 || pack->data == NULL)
5273 {
5274 uint8 buff[SOCK_MSG_LEN_MAX];
5275 memset(buff, 0, SOCK_MSG_LEN_MAX);
5276 int data_len = 0;
5277 if(mbtk_qser_req_apn_get(buff, &data_len, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5278 {
5279 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5280 err = MBTK_INFO_ERR_CME + cme_err;
5281 } else {
5282 err = MBTK_INFO_ERR_UNKNOWN;
5283 }
5284 LOGE("Get APN fail.");
5285 }
5286 else
5287 {
5288 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_APN_RSP, buff, data_len);
5289 }
5290 }
5291 else
5292 {
b.liu9e8584b2024-11-06 19:21:28 +08005293// const uint8* ptr = pack->data;
wangyouqiang80487e42024-05-24 15:06:20 +08005294 mbtk_apn_info_t apn;
5295 mbtk_apn_req_type_enum req_type = MBTK_APN_REQ_TYPE_SET;
5296 uint8 apn_type[MBTK_QSER_APN_NAME_SIZE] = {0};
5297 int ret = mbtk_strdata_to_apn(pack->data, &apn, apn_type, &req_type);
5298 if(ret < 0)
5299 {
5300 LOGE("mbtk_strdata_to_apn fail. ret = [%d]", ret);
5301 err = MBTK_INFO_ERR_REQ_PARAMETER;
5302 }
5303 else
5304 {
5305 if(req_apn_set(&apn, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5306 {
5307 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5308 err = MBTK_INFO_ERR_CME + cme_err;
5309 } else {
5310 err = MBTK_INFO_ERR_UNKNOWN;
5311 }
5312 LOGE("Set APN fail.");
5313 }
5314 else
5315 {
5316 // Save apn.
5317 apn_prop_set(&apn);
5318 mbtk_qser_apn_save(apn, apn_type, true);
5319 if(req_type == MBTK_APN_REQ_TYPE_ADD)
5320 {
5321 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_QSER_APN_RSP, (void *)&apn.cid, 1);
5322 }
5323 else
5324 {
5325 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_QSER_APN_RSP, NULL, 0);
5326 }
5327 }
5328 }
5329 }
5330
5331 break;
5332 }
b.liu288093c2024-05-09 17:02:57 +08005333 case MBTK_INFO_ID_NET_APN_DEL_REQ:
liuyang0e49d9a2024-04-23 21:04:54 +08005334 {
b.liu288093c2024-05-09 17:02:57 +08005335 if(pack->data_len == 0 || pack->data == NULL)
liuyang0e49d9a2024-04-23 21:04:54 +08005336 {
5337 err = MBTK_INFO_ERR_UNSUPPORTED;
yq.wang9823ddf2024-11-14 02:38:14 -08005338 LOGE("[%s] Unsupported delete apn.", __func__);
liuyang0e49d9a2024-04-23 21:04:54 +08005339 }
b.liu288093c2024-05-09 17:02:57 +08005340 else
liuyang0e49d9a2024-04-23 21:04:54 +08005341 {
liuyang1cefd852024-04-24 18:30:53 +08005342 int profile = pack->data[0];
wangyouqiang80487e42024-05-24 15:06:20 +08005343 if(cid_active[profile] == 1)
liuyang0e49d9a2024-04-23 21:04:54 +08005344 {
yq.wang9823ddf2024-11-14 02:38:14 -08005345 LOGE("[%s] cid[%d] pdp already open.", __func__, profile);
wangyouqiang80487e42024-05-24 15:06:20 +08005346 err = MBTK_INFO_ERR_CID_EXIST;
wangyouqiang13e98402024-05-24 16:07:43 +08005347 break;
wangyouqiang80487e42024-05-24 15:06:20 +08005348 }
5349 if(mbtk_qser_apn_del(profile) < 0)
5350 {
yq.wang9823ddf2024-11-14 02:38:14 -08005351 LOGE("[%s] mbtk_qser_apn_del fail.", __func__);
wangyouqiang80487e42024-05-24 15:06:20 +08005352 err = MBTK_INFO_ERR_REQ_PARAMETER;
liuyang0e49d9a2024-04-23 21:04:54 +08005353 }
5354 else
5355 {
wangyouqiang80487e42024-05-24 15:06:20 +08005356 if(req_apn_del(profile, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5357 {
5358 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5359 err = MBTK_INFO_ERR_CME + cme_err;
5360 } else {
5361 err = MBTK_INFO_ERR_UNKNOWN;
5362 }
yq.wang9823ddf2024-11-14 02:38:14 -08005363 LOGE("[%s] Delete apn fail. [%d]", __func__, err);
wangyouqiang80487e42024-05-24 15:06:20 +08005364 }
5365 else
5366 {
yq.wang9823ddf2024-11-14 02:38:14 -08005367 apn_prop_del(profile);
wangyouqiang80487e42024-05-24 15:06:20 +08005368 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_APN_DEL_RSP, NULL, 0);
5369 }
liuyang0e49d9a2024-04-23 21:04:54 +08005370 }
5371 }
5372 break;
5373 }
liubin281ac462023-07-19 14:22:54 +08005374 case MBTK_INFO_ID_NET_DATA_CALL_REQ:
5375 {
5376 if(pack->data_len == 0 || pack->data == NULL)
5377 {
5378 err = MBTK_INFO_ERR_UNSUPPORTED;
5379 }
5380 else
5381 {
5382 /* <call_type[1]><cid[1]><auto_conn_interval[1]><boot_conn[1]><timeout[1]>
5383 call_type : mbtk_data_call_type_enum
5384 cid : 2 - 7
5385 timeout : second
5386 */
5387 mbtk_data_call_type_enum call_type = (mbtk_data_call_type_enum)pack->data[0];
5388 int cid = pack->data[1];
5389 int reconn = 0;
5390
wangyouqiang80487e42024-05-24 15:06:20 +08005391#if 0
liubin281ac462023-07-19 14:22:54 +08005392 if(cid < MBTK_APN_CID_MIN || cid > MBTK_APN_CID_MAX) {
5393 err = MBTK_INFO_ERR_CID;
5394 break;
5395 }
wangyouqiang80487e42024-05-24 15:06:20 +08005396#endif
5397 if(mbtk_check_cid(cid) < 0)
5398 {
5399 err = MBTK_INFO_ERR_CID;
5400 break;
5401 }
liubin281ac462023-07-19 14:22:54 +08005402
5403 LOG("cid_active[%d] = %d", cid, cid_active[cid]);
5404 memset(&cgact_wait, 0, sizeof(info_cgact_wait_t));
5405 switch(call_type) {
5406 case MBTK_DATA_CALL_START: {
5407 //mbtk wyq for data_call_ex add start
5408 int auto_conn_interval = pack->data[2];
5409 int boot_conn = pack->data[3];
5410 int timeout = pack->data[4];
5411 data_call_bootconn_save(cid, boot_conn);
b.liufe320632024-01-17 20:38:08 +08005412
wangyouqiang13e98402024-05-24 16:07:43 +08005413 mbtk_signal_info_t signal;
5414 memset(&signal, 0xFF, sizeof(mbtk_signal_info_t));
5415 req_net_signal_get(&signal, NULL);
b.liufe320632024-01-17 20:38:08 +08005416
liubin281ac462023-07-19 14:22:54 +08005417 if(cid_active[cid] == 1)
5418 {
5419 err = MBTK_INFO_ERR_CID_EXIST;
5420 break;
5421 }
b.liufe320632024-01-17 20:38:08 +08005422
wangyouqiang80487e42024-05-24 15:06:20 +08005423 if(mbtk_check_default_pdp_state(cid))
5424 {
5425 err = MBTK_INFO_ERR_UNSUPPORTED;
5426 break;
5427 }
5428
liubin281ac462023-07-19 14:22:54 +08005429 data_call_reconn:
5430 //mbtk wyq for data_call_ex add end
5431 cgact_wait.waitting = true;
5432 cgact_wait.cid = cid;
5433 cgact_wait.act = true;
5434 if(req_data_call_start(cid, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5435 {
5436 //mbtk wyq for data_call_ex add start
5437 if(reconn < 5 && auto_conn_interval > 0)
5438 {
5439 sleep(auto_conn_interval);
5440 reconn++;
5441 cme_err = MBTK_INFO_ERR_CME_NON;
5442 LOG("data_call restart call.");
5443 goto data_call_reconn;
5444 }
5445 //mbtk wyq for data_call_ex add end
5446 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5447 err = MBTK_INFO_ERR_CME + cme_err;
5448 } else {
5449 err = MBTK_INFO_ERR_UNKNOWN;
5450 }
5451 LOG("%d active fail.", cid);
5452 }
5453 else
5454 {
5455 // Wait for "CONNECT" or "+CGEV:"
5456 if(wait_cgact_complete(timeout)) { // Timeout
5457 err = MBTK_INFO_ERR_TIMEOUT;
5458 break;
5459 }
5460
5461 // Get IP information.
5462 mbtk_ipv4_info_t ipv4;
5463 mbtk_ipv6_info_t ipv6;
5464 memset(&ipv4, 0, sizeof(mbtk_ipv4_info_t));
5465 memset(&ipv6, 0, sizeof(mbtk_ipv6_info_t));
5466 if(req_data_call_state_get(cid, &ipv4, &ipv6, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5467 {
5468 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5469 err = MBTK_INFO_ERR_CME + cme_err;
5470 } else {
5471 err = MBTK_INFO_ERR_UNKNOWN;
5472 }
5473 LOG("Get %d state fail.", cid);
5474 }
5475 else
5476 {
5477 // Config IPv4 address.
wangyouqianged88c722023-11-22 16:33:43 +08005478#ifdef MBTK_AF_SUPPORT
5479 if(cid == 1)
5480 {
5481 //uint8 pdp_data = cid;
5482 //pack_rsp_send(cli_info->fd , MBTK_INFO_ID_IND_PDP_STATE_CHANGE, &pdp_data, sizeof(uint8));
5483 ipv4.valid = false;
5484 ipv6.valid = false;
5485 if(default_iptype == MBTK_IP_TYPE_IP)
5486 {
5487 ipv4.valid = true;
5488 }
5489 else if(default_iptype == MBTK_IP_TYPE_IPV6)
5490 {
5491 ipv6.valid = true;
5492 }
5493 else
5494 {
5495 ipv4.valid = true;
5496 ipv6.valid = true;
5497 }
5498 }
5499#endif
liubin281ac462023-07-19 14:22:54 +08005500#if 1
5501 if(ipv4.valid) {
5502 char dev[20] = {0};
5503 sprintf(dev, "ccinet%d", cid - 1);
5504
5505 char ip[20] = {0};
5506 char gateway[20] = {0};
5507 char *gateway_ptr = NULL;
5508 char netmask[20] = {0};
5509 char *netmask_ptr = NULL;
5510 if(inet_ntop(AF_INET, &(ipv4.IPAddr), ip, 20) == NULL) {
5511 err = MBTK_INFO_ERR_UNKNOWN;
5512 LOGE("inet_ntop ipv4 ip fail.");
5513 log_hex("IPv4", &(ipv4.IPAddr), 4);
5514 break;
5515 }
5516
5517 if(ipv4.GateWay) {
5518 if(inet_ntop(AF_INET, &(ipv4.GateWay), gateway, 20) == NULL) {
5519 err = MBTK_INFO_ERR_UNKNOWN;
5520 LOGE("inet_ntop ipv4 gateway fail.");
5521 log_hex("IPv4", &(ipv4.IPAddr), 4);
5522 break;
5523 } else {
5524 gateway_ptr = gateway;
5525 }
5526 }
5527
5528 if(ipv4.NetMask) {
5529 if(inet_ntop(AF_INET, &(ipv4.NetMask), netmask, 20) == NULL) {
5530 err = MBTK_INFO_ERR_UNKNOWN;
5531 LOGE("inet_ntop ipv4 netmask fail.");
5532 log_hex("IPv4", &(ipv4.IPAddr), 4);
5533 break;
5534 } else {
5535 netmask_ptr = netmask;
5536 }
5537 }
5538
5539 if(netmask_ptr == NULL) {
5540 netmask_ptr = netmask;
5541 memcpy(netmask_ptr, "255.255.255.0", strlen("255.255.255.0"));
5542 }
5543
5544 if(mbtk_ifc_configure2(dev, ip, 0, gateway_ptr, netmask_ptr)) {
5545 LOGD("Config %s IPv4 %s fail.", dev, ip);
5546 } else {
5547 LOGD("Config %s IPv4 %s success.", dev, ip);
5548 }
5549
5550 }
5551#endif
5552 // Config IPv6 address.
5553 if(ipv6.valid) {
5554 char ip[50] = {0};
5555 char dev[20] = {0};
5556 sprintf(dev, "ccinet%d", cid - 1);
5557
5558 if(inet_ntop(AF_INET6, &(ipv6.IPV6Addr), ip, 50) == NULL) {
5559 err = MBTK_INFO_ERR_UNKNOWN;
5560 LOGE("inet_ntop ipv6 ip fail.");
5561 log_hex("IPv6", &(ipv6.IPV6Addr), 16);
5562 break;
5563 }
5564
5565 if(mbtk_ipv6_config(dev, ip, 64)) {
5566 LOGD("Config %s IPv6 %s fail.", dev, ip);
5567 } else {
5568 LOGD("Config %s IPv6 %s success.", dev, ip);
5569 }
5570 }
5571
5572 cid_active[cid] = 1;
wangyouqiang80487e42024-05-24 15:06:20 +08005573 mbtk_set_default_pdp_state(true, cid);
5574 mbtk_qser_route_config(cid, &ipv4, &ipv6);
liubin281ac462023-07-19 14:22:54 +08005575 if(cli_info->fd != DATA_CALL_BOOTCONN_FD)
5576 {
liuyange134d842024-06-27 17:34:02 +08005577 mbtk_net_led_set(MBTK_NET_LED_DATA_CONNECT);
liubin281ac462023-07-19 14:22:54 +08005578 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_DATA_CALL_RSP, NULL, 0);
5579 }
5580 else
5581 {
5582 free(pack->data);
5583 free(cli_info);
5584 LOG("data_call bootconn success.");
5585 }
5586 }
5587 }
5588 break;
5589 }
5590 case MBTK_DATA_CALL_STOP: {
5591 //mbtk wyq for data_call_ex add start
5592 if(cid_active[cid] == 0)
5593 {
5594 err = MBTK_INFO_ERR_CID_NO_EXIST;
5595 break;
5596 }
5597
5598 int timeout = pack->data[2];
5599 //mbtk wyq for data_call_ex add end
b.liuf37bd332024-03-18 13:51:24 +08005600#if (defined(MBTK_AF_SUPPORT) || defined(MBTK_ALL_CID_SUPPORT))
wangyouqianged88c722023-11-22 16:33:43 +08005601 if(cid == 1)
5602 {
5603 char dev[20] = {0};
5604 uint8 pdp_data = cid + 100;
5605 pack_rsp_send(cli_info->fd , MBTK_INFO_ID_IND_PDP_STATE_CHANGE, &pdp_data, sizeof(uint8));
b.liufe320632024-01-17 20:38:08 +08005606
wangyouqianged88c722023-11-22 16:33:43 +08005607 sprintf(dev, "ccinet%d", cid - 1);
5608
5609 // Config network.
5610 if(mbtk_ifc_configure2(dev, NULL, 0, NULL, NULL)) {
5611 LOGD("Config %s IPv4 0 fail.", dev);
5612 } else {
5613 LOGD("Config %s IPv4 0 success.", dev);
5614 }
5615 cid_active[cid] = 0;
wangyouqiang80487e42024-05-24 15:06:20 +08005616 mbtk_set_default_pdp_state(false, cid);
liuyange134d842024-06-27 17:34:02 +08005617 mbtk_net_led_set(MBTK_NET_LED_NET_CONNECT);
wangyouqianged88c722023-11-22 16:33:43 +08005618 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_DATA_CALL_RSP, NULL, 0);
5619 break;
5620 }
5621#endif
liubin281ac462023-07-19 14:22:54 +08005622 cgact_wait.waitting = true;
5623 cgact_wait.cid = cid;
5624 cgact_wait.act = false;
5625 if(req_data_call_stop(cid, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5626 {
5627 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5628 err = MBTK_INFO_ERR_CME + cme_err;
5629 } else {
5630 err = MBTK_INFO_ERR_UNKNOWN;
5631 }
5632 LOG("%d deactive fail.", cid);
5633 }
5634 else
5635 {
5636 // Wait for "CONNECT" or "+CGEV:"
5637 if(wait_cgact_complete(timeout)) { // Timeout
5638 err = MBTK_INFO_ERR_TIMEOUT;
5639 break;
5640 }
5641 char dev[20] = {0};
5642 sprintf(dev, "ccinet%d", cid - 1);
5643
5644 // Config network.
5645 if(mbtk_ifc_configure2(dev, NULL, 0, NULL, NULL)) {
5646 LOGD("Config %s IPv4 0 fail.", dev);
5647 } else {
5648 LOGD("Config %s IPv4 0 success.", dev);
5649 }
5650
5651#if 0
5652 if(mbtk_ipv6_config(dev, NULL, 64)) {
5653 LOGD("Config %s IPv6 0 fail.", dev);
5654 } else {
5655 LOGD("Config %s IPv6 0 success.", dev);
5656 }
5657#endif
5658 cid_active[cid] = 0;
wangyouqiang80487e42024-05-24 15:06:20 +08005659 mbtk_set_default_pdp_state(false, cid);
liuyange134d842024-06-27 17:34:02 +08005660 mbtk_net_led_set(MBTK_NET_LED_NET_CONNECT);
liubin281ac462023-07-19 14:22:54 +08005661 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_DATA_CALL_RSP, NULL, 0);
5662 }
5663 break;
5664 }
5665 case MBTK_DATA_CALL_STATE: {
wangyouqianged88c722023-11-22 16:33:43 +08005666 if(cid_active[cid] == 0)
5667 {
5668 err = MBTK_INFO_ERR_CID_NO_EXIST;
5669 break;
5670 }
liubin281ac462023-07-19 14:22:54 +08005671 mbtk_ipv4_info_t ipv4;
5672 mbtk_ipv6_info_t ipv6;
5673 memset(&ipv4, 0, sizeof(mbtk_ipv4_info_t));
5674 memset(&ipv6, 0, sizeof(mbtk_ipv6_info_t));
5675 if(req_data_call_state_get(cid, &ipv4, &ipv6, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5676 {
5677 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5678 err = MBTK_INFO_ERR_CME + cme_err;
5679 } else {
5680 err = MBTK_INFO_ERR_UNKNOWN;
5681 }
5682 LOG("Get %d state fail.", cid);
5683 }
5684 else
5685 {
5686 uint8 buff[SOCK_MSG_LEN_MAX] = {0};
5687 int buff_len = 0;
wangyouqianged88c722023-11-22 16:33:43 +08005688#ifdef MBTK_AF_SUPPORT
5689 if(cid == 1)
5690 {
5691 ipv4.valid = false;
5692 ipv6.valid = false;
5693 if(default_iptype == MBTK_IP_TYPE_IP)
5694 {
5695 ipv4.valid = true;
5696 }
5697 else if(default_iptype == MBTK_IP_TYPE_IPV6)
5698 {
5699 ipv6.valid = true;
5700 }
5701 else
5702 {
5703 ipv4.valid = true;
5704 ipv6.valid = true;
5705 }
5706 }
5707#endif
liubin281ac462023-07-19 14:22:54 +08005708 if(ipv4.valid && ipv6.valid) {
5709 buff[0] = (uint8)2;
5710 buff_len++;
5711
5712 memcpy(buff + buff_len, &ipv4, sizeof(mbtk_ipv4_info_t));
5713 buff_len += sizeof(mbtk_ipv4_info_t);
5714 memcpy(buff + buff_len, &ipv6, sizeof(mbtk_ipv6_info_t));
5715 buff_len += sizeof(mbtk_ipv6_info_t);
5716 } else if(ipv4.valid) {
5717 buff[0] = (uint8)0;
5718 buff_len++;
5719
5720 memcpy(buff + buff_len, &ipv4, sizeof(mbtk_ipv4_info_t));
5721 buff_len += sizeof(mbtk_ipv4_info_t);
5722 } else if(ipv6.valid) {
5723 buff[0] = (uint8)1;
5724 buff_len++;
5725
5726 memcpy(buff + buff_len, &ipv6, sizeof(mbtk_ipv6_info_t));
5727 buff_len += sizeof(mbtk_ipv6_info_t);
5728 } else {
5729 LOGE("Get IPv4/IPv6 fail.");
5730 err = MBTK_INFO_ERR_UNKNOWN;
5731 break;
5732 }
5733 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_DATA_CALL_RSP, buff, buff_len);
5734 }
5735 break;
5736 }
5737 default: {
5738 err = MBTK_INFO_ERR_FORMAT;
5739 break;
5740 }
5741 }
5742 }
5743 break;
5744 }
r.xiao06db9a12024-04-14 18:51:15 -07005745 case MBTK_INFO_ID_NET_IMS_REQ:
5746 {
5747 if(pack->data_len == 0 || pack->data == NULL) //Get
5748 {
5749 int reg = -1;
5750 if(net_ims_get(&reg, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5751 {
5752 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5753 err = MBTK_INFO_ERR_CME + cme_err;
5754 } else {
5755 err = MBTK_INFO_ERR_UNKNOWN;
5756 }
5757 LOG("Get net ims fail.");
5758 }
5759 else
5760 {
5761 uint8 reg_type = (uint8)reg;
5762 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_IMS_RSP, &reg_type, sizeof(uint8));
5763 }
5764 }
5765 else
5766 {
5767 uint8 ims = *(pack->data);
5768
5769 if(net_ims_set(ims, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5770 {
5771 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5772 err = MBTK_INFO_ERR_CME + cme_err;
5773 } else {
5774 err = MBTK_INFO_ERR_UNKNOWN;
5775 }
5776 LOG("Set net ims fail.");
5777 }
5778 else
5779 {
5780 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_IMS_RSP, NULL, 0);
5781 }
5782 }
5783 break;
5784 }
b.liufdf03172024-06-07 15:01:29 +08005785 case MBTK_INFO_ID_NET_IMS_REG_STATE_REQ:
5786 {
5787 if(pack->data_len == 0 || pack->data == NULL) //Get
5788 {
5789 int reg = -1;
5790 if(net_ims_reg_state_get(&reg, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5791 {
5792 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5793 err = MBTK_INFO_ERR_CME + cme_err;
5794 } else {
5795 err = MBTK_INFO_ERR_UNKNOWN;
5796 }
5797 LOG("Get net ims fail.");
5798 }
5799 else
5800 {
5801 uint8 reg_type = (uint8)reg;
5802 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_NET_IMS_REG_STATE_RSP, &reg_type, sizeof(uint8));
5803 }
5804 }
5805 else
5806 {
5807 err = MBTK_INFO_ERR_UNSUPPORTED;
5808 }
5809 break;
5810 }
r.xiaoec113d12024-01-12 02:13:28 -08005811 case MBTK_INFO_ID_WAKEUP_STA_REQ:
5812 {
5813 if(pack->data_len == 0 || pack->data == NULL)
5814 {
5815 err = MBTK_INFO_ERR_UNSUPPORTED;
5816 LOG("Get POWERIND state UNSUPPORTED.");
5817 }
5818 else // Set powerind state.
5819 {
5820 uint32 state = *(pack->data);
5821 if(req_powerind_set(state, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5822 {
5823 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5824 err = MBTK_INFO_ERR_CME + cme_err;
5825 } else {
5826 err = MBTK_INFO_ERR_UNKNOWN;
5827 }
5828 LOG("Set POWERIND state fail.");
5829 }
5830 else
5831 {
5832 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_WAKEUP_STA_RSP, NULL, 0);
5833 }
5834 }
5835 break;
5836 }
5837 case MBTK_INFO_ID_OOS_STA_REQ:
5838 {
5839 if(pack->data_len == 0 || pack->data == NULL)
5840 {
5841 mbtk_oos_info oos_t;
5842 if(req_oos_get(&oos_t, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5843 {
5844 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5845 err = MBTK_INFO_ERR_CME + cme_err;
5846 } else {
5847 err = MBTK_INFO_ERR_UNKNOWN;
5848 }
5849 LOG("Get SMS OOS fail.");
r.xiaoec113d12024-01-12 02:13:28 -08005850 }
5851 else
5852 {
r.xiaoec113d12024-01-12 02:13:28 -08005853 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_OOS_STA_RSP, &oos_t, sizeof(mbtk_oos_info));
5854 }
5855 }
b.liufe320632024-01-17 20:38:08 +08005856 else // Set OOS
r.xiaoec113d12024-01-12 02:13:28 -08005857 {
r.xiaocfd7c682024-01-22 03:59:46 -08005858 if(pack->data_len != sizeof(mbtk_oos_info))
5859 {
5860 err = MBTK_INFO_ERR_REQ_PARAMETER;
5861 LOG("Set oos error.");
5862 break;
5863 }
5864
b.liu9e8584b2024-11-06 19:21:28 +08005865 mbtk_oos_info *state = (mbtk_oos_info *)pack->data;
r.xiaoec113d12024-01-12 02:13:28 -08005866 if(req_oos_set(state, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
5867 {
5868 if(cme_err != MBTK_INFO_ERR_CME_NON) {
5869 err = MBTK_INFO_ERR_CME + cme_err;
5870 } else {
5871 err = MBTK_INFO_ERR_UNKNOWN;
5872 }
5873 LOG("Set OOS fail.");
5874 }
5875 else
5876 {
5877 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_OOS_STA_RSP, NULL, 0);
5878 }
5879 }
5880 break;
5881 }
wangyouqiang38e53362024-01-23 10:53:48 +08005882 case MBTK_INFO_ID_LED_REQ:
5883 {
5884 if(pack->data_len == 0 || pack->data == NULL)
5885 {
5886 err = MBTK_INFO_ERR_UNSUPPORTED;
5887 LOGE("led param is error.");
5888 }
5889 else
5890 {
5891 char type = pack->data[0];
5892 char status = pack->data[1];
5893 LOGE("[set_led] = [%d], [status_led] = [%d].", type, status);
r.xiaoec113d12024-01-12 02:13:28 -08005894
wangyouqiang38e53362024-01-23 10:53:48 +08005895 if(type == MBTK_LED_TYPE_NET)
5896 {
5897 if(status == MBTK_LED_STATUS_CLOSE)
5898 {
5899 mbtk_net_led_set(MBTK_NET_LED_CLOSE);
5900 }
5901 else
5902 {
5903 mbtk_net_led_set(MBTK_NET_LED_OPEN);
5904 }
5905 }
5906 else
5907 {
5908 if(status == MBTK_LED_STATUS_CLOSE)
5909 {
5910 status_led_set(MBTK_STATUS_LED_CLOSE);
5911 }
5912 else
5913 {
5914 status_led_set(MBTK_STATUS_LED_OPEN);
5915 }
5916 }
5917 pack_rsp_send(cli_info->fd, MBTK_INFO_ID_LED_RSP, NULL, 0);
5918 }
5919 break;
5920 }
liubin281ac462023-07-19 14:22:54 +08005921 default:
5922 {
5923 err = MBTK_INFO_ERR_REQ_UNKNOWN;
5924 LOG("Unknown request : %s", id2str(pack->info_id));
5925 break;
5926 }
5927 }
5928
5929 return err;
5930 }
5931}
5932
5933// Process AT URC data
5934static int send_pack_to_queue(sock_client_info_t* cli_info, void* pack)
5935{
5936 if(info_queue.count >= PACK_PROCESS_QUEUE_MAX)
5937 {
5938 LOG("Packet process queue is full");
5939 return -1;
5940 }
5941
5942 info_queue_item_t *item = (info_queue_item_t*)malloc(sizeof(info_queue_item_t));
5943 if(!item)
5944 {
5945 LOG("malloc() fail[%d].", errno);
5946 return -1;
5947 }
5948 item->cli_info = cli_info;
5949 item->pack = pack;
5950 mbtk_queue_put(&info_queue, item);
5951
5952 // If thread is waitting,continue it.
5953 if(1/*!is_running*/)
5954 {
5955 pthread_mutex_lock(&info_mutex);
5956 pthread_cond_signal(&info_cond);
5957 pthread_mutex_unlock(&info_mutex);
5958 }
5959 else
5960 {
5961 LOG("Packet process thread is process...");
5962 }
5963
5964 return 0;
5965}
5966
b.liu9e8584b2024-11-06 19:21:28 +08005967static void radio_state_change(const void *data, int data_len)
liubin281ac462023-07-19 14:22:54 +08005968{
5969 uint8 *data_ptr = (uint8*)data;
5970 if(data_ptr[0]) {
5971 net_info.radio_state = MBTK_RADIO_STATE_ON;
5972 } else {
5973 net_info.radio_state = MBTK_RADIO_STATE_OFF;
5974 }
5975
5976 sock_client_info_t *cli = NULL;
5977 list_first(sock_client_list);
5978 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
5979 {
5980 if(cli->ind_num > 0) {
5981 int i;
5982 for(i = 0; i < IND_REGISTER_MAX; i++) {
5983 // Registe MBTK_INFO_ID_IND_RADIO_STATE_CHANGE
5984 if(cli->ind_register[i] == MBTK_INFO_ID_IND_RADIO_STATE_CHANGE) {
5985 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_RADIO_STATE_CHANGE, data, data_len);
5986 break;
5987 }
5988 }
5989 }
5990 }
5991}
5992
b.liu9e8584b2024-11-06 19:21:28 +08005993static void pdp_state_change(const void *data, int data_len)
liubin281ac462023-07-19 14:22:54 +08005994{
5995 sock_client_info_t *cli = NULL;
5996 list_first(sock_client_list);
5997 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
5998 {
5999 if(cli->ind_num > 0) {
6000 int i;
6001 for(i = 0; i < IND_REGISTER_MAX; i++) {
6002 if(cli->ind_register[i] == MBTK_INFO_ID_IND_PDP_STATE_CHANGE) {
6003 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_PDP_STATE_CHANGE, data, data_len);
6004 break;
6005 }
6006 }
6007 }
6008 }
6009}
6010
b.liu9e8584b2024-11-06 19:21:28 +08006011static void net_state_change(const void *data, int data_len)
liubin281ac462023-07-19 14:22:54 +08006012{
6013 sock_client_info_t *cli = NULL;
6014 list_first(sock_client_list);
6015 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
6016 {
6017 if(cli->ind_num > 0) {
6018 int i;
6019 for(i = 0; i < IND_REGISTER_MAX; i++) {
6020 // Registe MBTK_INFO_ID_IND_NET_STATE_CHANGE
6021 if(cli->ind_register[i] == MBTK_INFO_ID_IND_NET_STATE_CHANGE) {
6022 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_NET_STATE_CHANGE, data, data_len);
6023 break;
6024 }
6025 }
6026 }
6027 }
6028}
6029
b.liu9e8584b2024-11-06 19:21:28 +08006030static void call_state_change(const void *data, int data_len)
liubin281ac462023-07-19 14:22:54 +08006031{
6032 sock_client_info_t *cli = NULL;
6033 list_first(sock_client_list);
6034 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
6035 {
6036 if(cli->ind_num > 0) {
6037 int i;
6038 for(i = 0; i < IND_REGISTER_MAX; i++) {
6039 // Registed MBTK_INFO_ID_IND_RADIO_STATE_CHANGE
6040 if(cli->ind_register[i] == MBTK_INFO_ID_IND_CALL_STATE_CHANGE) {
6041 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_CALL_STATE_CHANGE, data, data_len);
6042 break;
6043 }
6044 }
6045 }
6046 }
6047}
6048
b.liu9e8584b2024-11-06 19:21:28 +08006049static void sim_state_change(const void *data, int data_len)
liubin281ac462023-07-19 14:22:54 +08006050{
6051 sock_client_info_t *cli = NULL;
6052 list_first(sock_client_list);
6053 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
6054 {
6055 if(cli->ind_num > 0) {
6056 int i;
6057 for(i = 0; i < IND_REGISTER_MAX; i++) {
6058 // Registed MBTK_INFO_ID_IND_RADIO_STATE_CHANGE
6059 if(cli->ind_register[i] == MBTK_INFO_ID_IND_SIM_STATE_CHANGE) {
6060 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_SIM_STATE_CHANGE, data, data_len);
6061 break;
6062 }
6063 }
6064 }
6065 }
6066}
6067
b.liu9e8584b2024-11-06 19:21:28 +08006068static void sms_state_change(const void *data, int data_len)
liubin281ac462023-07-19 14:22:54 +08006069{
6070 sock_client_info_t *cli = NULL;
6071 list_first(sock_client_list);
6072 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
6073 {
6074 if(cli->ind_num > 0) {
6075 int i;
6076 for(i = 0; i < IND_REGISTER_MAX; i++) {
6077 // Registed MBTK_INFO_ID_IND_SMS_STATE_CHANGE
6078 if(cli->ind_register[i] == MBTK_INFO_ID_IND_SMS_STATE_CHANGE) {
6079 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_SMS_STATE_CHANGE, data, data_len);
6080 break;
6081 }
6082 }
6083 }
6084 }
6085}
6086
b.liu9e8584b2024-11-06 19:21:28 +08006087static void signal_state_change(const void *data, int data_len)
r.xiaofca7c472024-04-24 01:00:23 -07006088{
6089 sock_client_info_t *cli = NULL;
6090 list_first(sock_client_list);
6091 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
6092 {
6093 if(cli->ind_num > 0) {
6094 int i;
6095 for(i = 0; i < IND_REGISTER_MAX; i++) {
6096 // Registe MBTK_INFO_ID_IND_SIGNAL_STATE_CHANGE
6097 if(cli->ind_register[i] == MBTK_INFO_ID_IND_SIGNAL_STATE_CHANGE) {
6098 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_SIGNAL_STATE_CHANGE, data, data_len);
6099 break;
6100 }
6101 }
6102 }
6103 }
6104}
6105
6106
b.liu9e8584b2024-11-06 19:21:28 +08006107int urc_msg_distribute(bool async_process, info_urc_msg_id_enum msg, const void *data, int data_len)
liubin281ac462023-07-19 14:22:54 +08006108{
6109#if 0
6110 if(urc_queue.count >= PACK_PROCESS_QUEUE_MAX)
6111 {
6112 LOG("Packet process queue is full");
6113 return -1;
6114 }
6115
6116 info_urc_msg_t *urc = (info_urc_msg_t*)malloc(sizeof(info_urc_msg_t));
6117 if(!urc)
6118 {
6119 LOG("malloc() fail[%d].", errno);
6120 return -1;
6121 }
6122 urc->msg = msg;
6123 urc->data = memdup(data, data_len);
6124 urc->data_len = data_len;
6125
6126 mbtk_queue_put(&urc_queue, urc);
6127
6128 // If thread is waitting,continue it.
6129 if(1/*!is_running*/)
6130 {
6131 pthread_mutex_lock(&urc_mutex);
6132 pthread_cond_signal(&urc_cond);
6133 pthread_mutex_unlock(&urc_mutex);
6134 }
6135 else
6136 {
6137 LOG("Packet process thread is process...");
6138 }
6139
6140 return 0;
6141#else
b.liuf77b86c2024-11-09 13:24:10 +08006142
liubin281ac462023-07-19 14:22:54 +08006143 if(async_process) {
6144 info_urc_msg_t *urc = (info_urc_msg_t*)malloc(sizeof(info_urc_msg_t));
6145 if(!urc)
6146 {
6147 LOG("malloc() fail[%d].", errno);
6148 return -1;
6149 }
6150 urc->msg = msg;
6151 if(data && data_len > 0) {
6152 urc->data = memdup(data, data_len);
6153 urc->data_len = data_len;
6154 } else {
6155 urc->data = NULL;
6156 urc->data_len = 0;
6157 }
6158 return send_pack_to_queue(NULL, urc);
6159 } else {
6160 switch(msg) {
6161 case INFO_URC_MSG_NET_CS_REG_STATE:
6162 {
6163 net_state_change(data, data_len);
6164 break;
6165 }
6166 case INFO_URC_MSG_CALL_STATE:
6167 {
6168 call_state_change(data, data_len);
6169 break;
6170 }
6171 case INFO_URC_MSG_SMS_STATE:
6172 {
6173 sms_state_change(data, data_len);
6174 break;
6175 }
6176 case INFO_URC_MSG_SIM_STATE:
6177 {
6178 sim_state_change(data, data_len);
6179 break;
6180 }
6181 case INFO_URC_MSG_PDP_STATE:
6182 {
6183 pdp_state_change(data, data_len);
6184 break;
6185 }
6186 default: {
6187 LOGE("Unknown msg : %d", msg);
6188 break;
6189 }
6190 }
6191
6192 return 0;
6193 }
6194#endif
6195}
6196
6197
6198static void ind_regisger(sock_client_info_t* cli_info, uint16 ind)
6199{
6200 uint32 i = 0;
6201 while(i < cli_info->ind_num)
6202 {
6203 if(cli_info->ind_register[i] == ind)
6204 break;
6205 i++;
6206 }
6207
6208 if(i == cli_info->ind_num) // No found IND
6209 {
6210 cli_info->ind_register[i] = ind;
6211 cli_info->ind_num++;
6212 LOG("Register IND : %s", id2str(ind));
6213 }
6214 else
6215 {
6216 LOG("IND had exist.");
6217 }
6218}
6219
6220static void pack_distribute(sock_client_info_t* cli_info, mbtk_info_pack_t* pack)
6221{
6222 // Register IND Message.
6223 if(mbtk_info_type_get(pack->info_id) == MBTK_INFO_TYPE_IND)
6224 {
6225 mbtk_info_err_enum err = MBTK_INFO_ERR_SUCCESS;
6226 if(cli_info->ind_num >= IND_REGISTER_MAX)
6227 {
6228 LOG("IND if full.");
6229 err = MBTK_INFO_ERR_IND_FULL;
6230 }
6231 else
6232 {
6233 ind_regisger(cli_info, pack->info_id);
6234 }
6235
6236 pack_error_send(cli_info->fd, pack->info_id, err);
6237
6238 mbtk_info_pack_free(&pack);
6239 }
6240 else // Request Information.
6241 {
6242 LOG("Start process REQ(%s), Length : %d", id2str(pack->info_id), pack->data_len);
6243 if(0 && pack->data_len > 0)
6244 {
6245 log_hex("DATA", pack->data, pack->data_len);
6246 }
6247
6248 // Send to REQ_process_thread process.
6249 send_pack_to_queue(cli_info, pack);
6250
6251 // For test.
6252 // pack_error_send(cli_info->fd, pack->info_id + 1, MBTK_INFO_ERR_SUCCESS);
6253 }
6254}
6255
6256static sock_client_info_t* cli_find(int fd)
6257{
6258 sock_client_info_t *result = NULL;
6259 list_first(sock_client_list);
6260 while ((result = (sock_client_info_t*) list_next(sock_client_list)))
6261 {
6262 if (result->fd == fd)
6263 return result;
6264 }
6265
6266 return NULL;
6267}
6268
6269//mbtk wyq for server_ready_status add start
6270void server_ready_set(void)
6271{
6272 server_ready_status = 1;
6273}
6274
6275char server_ready_get(void)
6276{
6277 return server_ready_status;
6278}
6279
6280static void server_state_send(void)
6281{
6282 sock_client_info_t *cli = NULL;
6283 list_first(sock_client_list);
6284 while ((cli = (sock_client_info_t*) list_next(sock_client_list)))
6285 {
6286 if(cli->ind_num > 0) {
6287 if(cli->ind_register[0] == MBTK_INFO_ID_IND_SERVER_STATE_CHANGE) {
6288 cli->ind_num = 0;
6289 cli->ind_register[0] = 0;
6290 pack_rsp_send(cli->fd , MBTK_INFO_ID_IND_SERVER_STATE_CHANGE, "1", 1);
6291 break;
6292 }
6293 }
6294 else
6295 {
6296 break;
6297 }
6298 }
6299 LOG("handshake message send ok.");
6300}
6301
6302//mbtk wyq for server_ready_status add end
6303
6304//mbtk wyq for data_call_ex add start
6305//Save the cid that "DATA_CALL" needs to be automatically connected after startup
6306void data_call_bootconn_save(int cid, int bootconn)
6307{
6308 if(cid_bootconn[cid] == bootconn + '0')
6309 {
6310 return;
6311 }
6312 cid_bootconn[cid] = bootconn + '0';
6313
6314 LOG("data_call_bootconn_set cid_bootconn = %s", cid_bootconn);
6315 property_set("persist.mbtk.datacall.bootconn", cid_bootconn);
6316}
6317
6318static void* data_call_bootconn_pthread(void *arg)
6319{
6320 UNUSED(arg);
6321 LOG("data_call_bootconn_pthread enter.");
6322 int i = 0;
6323 int send_sum = 0;
6324 int bootconn = 0;
b.liufe320632024-01-17 20:38:08 +08006325
liubin281ac462023-07-19 14:22:54 +08006326 while(1)
6327 {
6328 if(server_ready_get() && send_sum == 0)
6329 {
6330 server_state_send();
6331 send_sum = 1;
6332 }
b.liufe320632024-01-17 20:38:08 +08006333
liubin281ac462023-07-19 14:22:54 +08006334 if(net_info.sim_state == MBTK_SIM_READY && net_info.net_type == MBTK_RADIO_TECH_E_UTRAN && bootconn == 0)
6335 {
6336 //data_call_bootconn_exec();
6337 property_get("persist.mbtk.datacall.bootconn", cid_bootconn, "00000000");
6338 LOG("data_call_bootconn_exec cid_bootconn = %s", cid_bootconn);
b.liufe320632024-01-17 20:38:08 +08006339
liubin281ac462023-07-19 14:22:54 +08006340 for(i = MBTK_APN_CID_MIN; i < MBTK_APN_CID_MAX + 1; i++)
6341 {
6342 if(cid_bootconn[i] == '1')
6343 {
6344 sock_client_info_t *info = (sock_client_info_t*)malloc(sizeof(sock_client_info_t));
6345 if(info == NULL)
6346 {
6347 LOG("clinent_info malloc() fail.");
6348 continue;
6349 }
6350 memset(info, 0, sizeof(sock_client_info_t));
6351 info->fd = DATA_CALL_BOOTCONN_FD;
b.liufe320632024-01-17 20:38:08 +08006352
liubin281ac462023-07-19 14:22:54 +08006353 mbtk_info_pack_t* pack = mbtk_info_pack_creat(MBTK_INFO_ID_NET_DATA_CALL_REQ);
6354 if(pack == NULL)
6355 {
6356 free(info);
6357 LOG("Packet malloc() fail.");
6358 continue;
6359 }
6360
6361 // "info_err"
6362 //pack->info_err = byte_2_uint16(ptr, false)
6363
6364 // "data_len"
6365 pack->data_len = 5;
6366
b.liu9e8584b2024-11-06 19:21:28 +08006367 uint8 *p = (uint8 *)malloc(5);
liubin281ac462023-07-19 14:22:54 +08006368 p[0] = MBTK_DATA_CALL_START;
6369 p[1] = i;
6370 p[2] = 0;
6371 p[3] = 1;
6372 p[4] = 10;
6373 pack->data = p;
6374 send_pack_to_queue(info, pack);
6375 }
6376 }
6377
6378 bootconn = 1;
6379 }
6380
6381 if(bootconn == 1 && send_sum == 1)
6382 {
6383 break;
6384 }
6385 else
6386 {
6387 sleep(1);
6388 }
6389 }
6390
6391 LOG("data_call_bootconn_pthread exit.");
6392 return NULL;
6393}
6394
6395//mbtk wyq for data_call_ex add end
6396
6397static void* info_main_pthread(void* arg)
6398{
6399 UNUSED(arg);
6400 epoll_fd = epoll_create(SOCK_CLIENT_MAX + 1);
6401 if(epoll_fd < 0)
6402 {
6403 LOG("epoll_create() fail[%d].", errno);
6404 return NULL;
6405 }
6406
6407 uint32 event = EPOLLIN | EPOLLET;
6408 struct epoll_event ev;
6409 ev.data.fd = sock_listen_fd;
6410 ev.events = event; //EPOLLIN | EPOLLERR | EPOLLET;
6411 epoll_ctl(epoll_fd,EPOLL_CTL_ADD,sock_listen_fd,&ev);
6412
6413 int nready = -1;
6414 struct epoll_event epoll_events[EPOLL_LISTEN_MAX];
6415 while(1)
6416 {
6417 nready = epoll_wait(epoll_fd, epoll_events, EPOLL_LISTEN_MAX, -1);
6418 if(nready > 0)
6419 {
6420 sock_client_info_t *cli_info = NULL;
6421 int i;
6422 for(i = 0; i < nready; i++)
6423 {
6424 LOG("fd[%d] event = %x",epoll_events[i].data.fd, epoll_events[i].events);
6425 if(epoll_events[i].events & EPOLLHUP) // Client Close.
6426 {
6427 if((cli_info = cli_find(epoll_events[i].data.fd)) != NULL)
6428 {
6429 cli_close(cli_info);
6430 }
6431 else
6432 {
6433 LOG("Unknown client[fd = %d].", epoll_events[i].data.fd);
6434 }
6435 }
6436 else if(epoll_events[i].events & EPOLLIN)
6437 {
6438 if(epoll_events[i].data.fd == sock_listen_fd) // New clients connected.
6439 {
6440 int client_fd = -1;
6441 while(1)
6442 {
6443 struct sockaddr_in cliaddr;
6444 socklen_t clilen = sizeof(cliaddr);
6445 client_fd = accept(epoll_events[i].data.fd, (struct sockaddr *) &cliaddr, &clilen);
6446 if(client_fd < 0)
6447 {
6448 if(errno == EAGAIN)
6449 {
6450 LOG("All client connect get.");
6451 }
6452 else
6453 {
6454 LOG("accept() error[%d].", errno);
6455 }
6456 break;
6457 }
6458 // Set O_NONBLOCK
6459 int flags = fcntl(client_fd, F_GETFL, 0);
6460 if (flags > 0)
6461 {
6462 flags |= O_NONBLOCK;
6463 if (fcntl(client_fd, F_SETFL, flags) < 0)
6464 {
6465 LOG("Set flags error:%d", errno);
6466 }
6467 }
6468
6469 memset(&ev,0,sizeof(struct epoll_event));
6470 ev.data.fd = client_fd;
6471 ev.events = event;//EPOLLIN | EPOLLERR | EPOLLET;
6472 epoll_ctl(epoll_fd, EPOLL_CTL_ADD, client_fd, &ev);
6473
6474 sock_client_info_t *info = (sock_client_info_t*)malloc(sizeof(sock_client_info_t));
6475 if(info)
6476 {
6477 memset(info, 0, sizeof(sock_client_info_t));
6478 info->fd = client_fd;
6479 if(server_ready_get() == 1)
6480 {
6481 info->ind_num = 0;
6482 pack_rsp_send(info->fd , MBTK_INFO_ID_IND_SERVER_STATE_CHANGE, "1", 1);
6483 LOG("server ready ok.");
6484 }
6485 else
6486 {
6487 info->ind_num = 1;
6488 info->ind_register[0] = MBTK_INFO_ID_IND_SERVER_STATE_CHANGE;
6489 LOG("server ready no.");
6490 }
6491 list_add(sock_client_list, info);
6492 LOG("Add New Client FD Into List.");
6493 }
6494 else
6495 {
6496 LOG("malloc() fail.");
6497 }
6498 }
6499 }
6500 else if((cli_info = cli_find(epoll_events[i].data.fd)) != NULL) // Client data arrive.
6501 {
6502 // Read and process every message.
6503 mbtk_info_err_enum err = MBTK_INFO_ERR_SUCCESS;
6504 mbtk_info_pack_t** pack = mbtk_info_pack_recv(cli_info->fd, true, &err);
6505
6506 // Parse packet error,send error response to client.
6507 if(pack == NULL)
6508 {
6509 if(err != MBTK_INFO_ERR_SUCCESS)
6510 {
6511 pack_error_send(cli_info->fd, MBTK_INFO_ID_REQ_UNKNOWN, err);
6512 }
6513 }
6514 else
6515 {
6516#if 0
6517 int i = 0;
6518 while(pack[i] != NULL)
6519 {
6520 pack_distribute(cli_info, pack[i]);
6521 // Not free,will free in pack_process() or packet process thread.
6522 //mbtk_info_pack_free(&(pack[i]));
6523 i++;
6524 }
6525 free(pack);
6526#else
6527 mbtk_info_pack_t** pack_ptr = pack;
6528 while(*pack_ptr)
6529 {
6530 pack_distribute(cli_info, *pack_ptr);
6531 // Not free,will free in pack_process() or packet process thread.
6532 //mbtk_info_pack_free(pack_ptr);
6533 pack_ptr++;
6534 }
6535
6536 free(pack);
6537#endif
6538 }
6539 }
6540 else
6541 {
6542 LOG("Unknown socket : %d", epoll_events[i].data.fd);
6543 }
6544 }
6545 else
6546 {
6547 LOG("Unknown event : %x", epoll_events[i].events);
6548 }
6549 }
6550 }
6551 else
6552 {
6553 LOG("epoll_wait() fail[%d].", errno);
6554 }
6555 }
6556
6557 return NULL;
6558}
6559
liubin281ac462023-07-19 14:22:54 +08006560/*
6561void mbtk_radio_ready_cb()
6562{
6563 pthread_t radio_pid;
6564 pthread_attr_t thread_attr;
6565 pthread_attr_init(&thread_attr);
6566 if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
6567 {
6568 LOG("pthread_attr_setdetachstate() fail.");
6569 return;
6570 }
6571
6572 if(pthread_create(&radio_pid, &thread_attr, radio_ready_thread, NULL))
6573 {
6574 LOG("pthread_create() fail.");
6575 }
6576
6577 pthread_attr_destroy(&thread_attr);
6578}
6579*/
6580
b.liuf77b86c2024-11-09 13:24:10 +08006581static void net_ifc_state_change(bool act, int cid, mbtk_pdp_act_info_t *act_info)
liubin281ac462023-07-19 14:22:54 +08006582{
6583 if(cid < MBTK_APN_CID_MIN || cid > MBTK_APN_CID_MAX) { // No nothing for cid 1 and 8
6584 return;
6585 }
6586
yq.wanga9efa822024-07-03 04:33:48 -07006587#if 0
wangyouqiang65884152023-10-25 19:54:15 +08006588 if(act)
6589 {
6590 cid_active[cid] = 1;
wangyouqiang80487e42024-05-24 15:06:20 +08006591 mbtk_set_default_pdp_state(true, cid);
wangyouqiang65884152023-10-25 19:54:15 +08006592 }
6593 else
6594 {
6595 cid_active[cid] = 0;
wangyouqiang80487e42024-05-24 15:06:20 +08006596 mbtk_set_default_pdp_state(false, cid);
wangyouqiang65884152023-10-25 19:54:15 +08006597 }
yq.wanga9efa822024-07-03 04:33:48 -07006598#endif
liubin281ac462023-07-19 14:22:54 +08006599 char dev[20] = {0};
6600 sprintf(dev, "ccinet%d", cid - 1);
6601 if(act) { // Config IP.
6602 // Get IP information.
6603 mbtk_ipv4_info_t ipv4;
6604 mbtk_ipv6_info_t ipv6;
6605 memset(&ipv4, 0, sizeof(mbtk_ipv4_info_t));
6606 memset(&ipv6, 0, sizeof(mbtk_ipv6_info_t));
6607 int cme_err = MBTK_INFO_ERR_CME_NON;
6608 if(!req_data_call_state_get(cid, &ipv4, &ipv6, &cme_err) && cme_err == MBTK_INFO_ERR_CME_NON)
6609 {
wangyouqianged88c722023-11-22 16:33:43 +08006610#ifdef MBTK_AF_SUPPORT
6611 if(cid == 1)
6612 {
6613 ipv4.valid = false;
6614 ipv6.valid = false;
6615 if(default_iptype == MBTK_IP_TYPE_IP)
6616 {
6617 ipv4.valid = true;
6618 }
6619 else if(default_iptype == MBTK_IP_TYPE_IPV6)
6620 {
6621 ipv6.valid = true;
6622 }
6623 else
6624 {
6625 ipv4.valid = true;
6626 ipv6.valid = true;
6627 }
6628 }
6629#endif
b.liuf77b86c2024-11-09 13:24:10 +08006630
6631 if(act_info) {
6632 memcpy(&(act_info->ipv4), &ipv4, sizeof(mbtk_ipv4_info_t));
6633 memcpy(&(act_info->ipv6), &ipv6, sizeof(mbtk_ipv6_info_t));
6634 }
6635
liubin281ac462023-07-19 14:22:54 +08006636 // Config IPv4 address.
6637 if(ipv4.valid) {
6638 char ip[20] = {0};
6639 if(inet_ntop(AF_INET, &(ipv4.IPAddr), ip, 20) == NULL) {
6640 LOGE("inet_ntop ipv4 ip fail.");
6641 return;
6642 }
6643
6644 if(mbtk_ifc_configure2(dev, ip, 0, NULL, "255.255.255.0")) {
6645 LOGD("Config %s IPv4 %s fail.", dev, ip);
6646 } else {
6647 LOGD("Config %s IPv4 %s success.", dev, ip);
6648 }
6649 }
6650
6651 // Config IPv6 address.
6652 if(ipv6.valid) {
6653 char ip[50] = {0};
6654
6655 if(inet_ntop(AF_INET6, &(ipv6.IPV6Addr), ip, 50) == NULL) {
6656 LOGE("inet_ntop ipv6 ip fail.");
6657 return;
6658 }
6659
6660 if(mbtk_ipv6_config(dev, ip, 64)) {
6661 LOGD("Config %s IPv6 %s fail.", dev, ip);
6662 } else {
6663 LOGD("Config %s IPv6 %s success.", dev, ip);
6664 }
6665 }
yq.wanga9efa822024-07-03 04:33:48 -07006666
6667 mbtk_qser_route_config(cid, &ipv4, &ipv6);
liubin281ac462023-07-19 14:22:54 +08006668 }
6669 } else { // Del IP
6670 if(mbtk_ifc_configure2(dev, NULL, 0, NULL, NULL)) {
6671 LOGD("Config %s IPv4 0 fail.", dev);
6672 } else {
6673 LOGD("Config %s IPv4 0 success.", dev);
6674 }
6675 }
6676}
6677
yq.wanga9efa822024-07-03 04:33:48 -07006678static void data_call_restart()
6679{
6680#if 0
6681 // Waitting for network ok.
6682 mbtk_net_info_t info;
6683 int cme_err;
6684 int i = 0;
6685 while(i < 15) { // 15s timeout
6686 cme_err = MBTK_INFO_ERR_CME_NON;
6687 memset(&info, 0, sizeof(mbtk_net_info_t));
6688 if(!req_net_sel_mode_get(&info, &cme_err) && cme_err == MBTK_INFO_ERR_CME_NON)
6689 {
6690 if(info.net_type >= 2) {
6691 break;
6692 }
6693 }
6694
6695 sleep(1);
6696 i++;
6697 }
6698#endif
6699 // +CGACT
6700 int cid;
wangyouqiang3947b302024-07-04 17:26:08 +08006701 uint8 data_pdp = 0;
yq.wanga9efa822024-07-03 04:33:48 -07006702 LOGD("Start active APN.");
6703 at_process = true;
6704 cgact_wait.act = true;
yq.wangd58f71e2024-08-21 23:45:31 -07006705 for(cid = MBTK_APN_CID_MIN; cid <= MBTK_APN_CID_MAX; cid++) {
6706 if(cid_active[cid])
6707 {
6708 LOG("Active cid : %d", cid);
6709 cgact_wait.waitting = true;
6710 cgact_wait.cid = cid;
b.liuf77b86c2024-11-09 13:24:10 +08006711 net_ifc_state_change(false, cid, NULL);
yq.wangd58f71e2024-08-21 23:45:31 -07006712 data_pdp = cid + 100;
6713 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
6714 req_data_call_start(cid, NULL);
6715 wait_cgact_complete(10);
b.liuf77b86c2024-11-09 13:24:10 +08006716
6717 mbtk_pdp_act_info_t act_info;
6718 memset(&act_info, 0, sizeof(mbtk_pdp_act_info_t));
yq.wangd58f71e2024-08-21 23:45:31 -07006719 data_pdp = cid + 220;
b.liuf77b86c2024-11-09 13:24:10 +08006720 act_info.state = data_pdp;
6721
6722 net_ifc_state_change(true, cid, &act_info);
6723
6724 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &act_info, sizeof(mbtk_pdp_act_info_t));
yq.wangd58f71e2024-08-21 23:45:31 -07006725 }
yq.wanga9efa822024-07-03 04:33:48 -07006726 }
6727 at_process = false;
6728}
6729
liubin281ac462023-07-19 14:22:54 +08006730static void urc_msg_process(info_urc_msg_t *msg)
6731{
6732 uint8 *data = NULL;
6733 if(msg->data) {
6734 data = (uint8*)msg->data;
6735 }
6736 switch(msg->msg) {
6737 case INFO_URC_MSG_RADIO_STATE:
6738 {
6739 radio_state_change(msg->data, msg->data_len);
6740 // Reconfig APN while radio on.
6741 if(data[0]) {
6742 apn_prop_get();
6743 }
6744 break;
6745 }
6746 case INFO_URC_MSG_CGEV:
6747 {
6748 bool act = data[0];
6749 int cid = data[1];
6750 if(cid > 0) {
b.liuf77b86c2024-11-09 13:24:10 +08006751 net_ifc_state_change(act, cid, NULL);
6752 }
6753 break;
6754 }
6755 case INFO_URC_MSG_PDP_STATE:
6756 {
6757 // pdp_state_change
6758 mbtk_pdp_act_info_t act_info;
6759 memset(&act_info, 0, sizeof(mbtk_pdp_act_info_t));
6760 int cme_err = MBTK_INFO_ERR_CME_NON;
6761 uint8 cid = *((uint8*)(msg->data));
6762 act_info.state = cid;
6763#if 0
6764 if(cid > 220) {
6765 cid -= 220;
6766 } else { // > 200
6767 cid -= 200;
6768 }
6769#endif
6770 if(!req_data_call_state_get(cid, &(act_info.ipv4), &(act_info.ipv6), &cme_err) && cme_err == MBTK_INFO_ERR_CME_NON)
6771 {
6772 pdp_state_change(&act_info, sizeof(mbtk_pdp_act_info_t));
liubin281ac462023-07-19 14:22:54 +08006773 }
6774 break;
6775 }
6776 case INFO_URC_MSG_NET_PS_REG_STATE:
6777 {
b.liufe320632024-01-17 20:38:08 +08006778 uint8 net_data[4];
liubin281ac462023-07-19 14:22:54 +08006779 net_data[0] = (uint8)MBTK_NET_PS_STATE;
b.liufe320632024-01-17 20:38:08 +08006780 net_data[1] = data[0]; // act
6781 net_data[3] = data[1]; // 0 - GSM/WCDMA; 1 - LTE
liubin281ac462023-07-19 14:22:54 +08006782 mbtk_net_reg_state_enum state = (mbtk_net_reg_state_enum)data[0];
6783 if(state == MBTK_NET_REG_STATE_HOME
6784 || state == MBTK_NET_REG_STATE_ROAMING) { // Registered, home network or roaming.
6785 mbtk_net_info_t info;
6786 int cme_err = MBTK_INFO_ERR_CME_NON;
6787 memset(&info, 0, sizeof(mbtk_net_info_t));
6788 if(!req_net_sel_mode_get(&info, &cme_err) && cme_err == MBTK_INFO_ERR_CME_NON)
6789 {
6790 net_data[2] = info.net_type;
6791 net_state_change(net_data, sizeof(net_data));
6792
6793 if(info.net_type >= MBTK_RADIO_TECH_UTRAN) {
6794 data_call_restart();
6795 }
6796 } else {
6797 net_data[2] = (uint8)0xFF;
6798 net_state_change(net_data, sizeof(net_data));
6799 }
6800 } else {
6801 net_data[2] = (uint8)0xFF;
6802 net_state_change(net_data, sizeof(net_data));
6803 }
6804 break;
6805 }
b.liuf37bd332024-03-18 13:51:24 +08006806 case INFO_URC_MSG_SET_BAND:
6807 {
6808 mbtk_band_info_t band;
6809 int cme_err = MBTK_INFO_ERR_CME_NON;
6810
6811 band.net_pref = MBTK_NET_PREF_GSM_UMTS_LTE_LTE_PREF; // 15
b.liu288093c2024-05-09 17:02:57 +08006812 band.gsm_band = (uint16)band_set_info.band_gsm;
6813 band.umts_band = (uint16)band_set_info.band_wcdma;
b.liuf37bd332024-03-18 13:51:24 +08006814 band.tdlte_band = band_set_info.band_tdlte;
6815 band.fddlte_band = band_set_info.band_fddlte;
b.liu288093c2024-05-09 17:02:57 +08006816 band.lte_ext_band = band_set_info.band_lte_ext;
6817
b.liuf37bd332024-03-18 13:51:24 +08006818 if(req_band_set(&band, &cme_err) || cme_err != MBTK_INFO_ERR_CME_NON)
6819 {
6820 LOG("Set band fail.");
6821 }
6822 else // Set band success.
6823 {
b.liu45bfb532024-05-11 15:37:22 +08006824 // log_hex("BAND-2", &band_set_info, sizeof(band_set_info_t));
b.liu288093c2024-05-09 17:02:57 +08006825 band_set_success = TRUE;
6826 if(band_area == MBTK_MODEM_BAND_AREA_CN) {
b.liuf37bd332024-03-18 13:51:24 +08006827 property_set("persist.mbtk.band_config", "CN");
b.liu288093c2024-05-09 17:02:57 +08006828 } else if(band_area == MBTK_MODEM_BAND_AREA_EU) {
b.liuf37bd332024-03-18 13:51:24 +08006829 property_set("persist.mbtk.band_config", "EU");
b.liu288093c2024-05-09 17:02:57 +08006830 } else if(band_area == MBTK_MODEM_BAND_AREA_SA) {
b.liuf678f992024-05-08 15:23:10 +08006831 property_set("persist.mbtk.band_config", "SA");
b.liuf37bd332024-03-18 13:51:24 +08006832 } else {
6833 property_set("persist.mbtk.band_config", "ALL");
6834 }
6835 LOG("Set band success.");
6836 }
6837 break;
6838 }
wangyouqiangce45a102024-04-18 18:08:29 +08006839 case INFO_URC_MSG_GET_SIM_STATE:
6840 {
6841 net_info.sim_state = getSIMStatus();
6842 if(net_info.sim_state == MBTK_SIM_READY)
6843 {
6844 LOG("SIM READY!");
6845 }
6846 else
6847 {
6848 LOG("SIM NOT READY!");
6849 }
6850 break;
6851 }
liubin281ac462023-07-19 14:22:54 +08006852 case INFO_URC_MSG_NET_STATE_LOG:
6853 {
6854 // Get network state and signal.
6855 char buff[256] = {0};
yq.wangf1fc31e2024-10-12 03:21:03 -07006856 uint8 data_signal[8];
6857 mbtk_signal_info_t signal;
liubin281ac462023-07-19 14:22:54 +08006858 memset(&signal, 0xFF, sizeof(mbtk_signal_info_t));
6859 if(!req_net_signal_get(&signal, NULL)) {
6860 char tmp[50] = {0};
6861 struct timeval log_time;
6862 gettimeofday(&log_time, NULL);
6863 struct tm* tm_t = localtime(&(log_time.tv_sec));
6864 strftime(tmp, 50, "%F %T", tm_t);
6865 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,
6866 signal.rsrq, signal.rsrp);
b.liu06559f62024-11-01 18:48:22 +08006867 mbtk_signal_log(buff);
yq.wangf1fc31e2024-10-12 03:21:03 -07006868 }
r.xiaofca7c472024-04-24 01:00:23 -07006869
yq.wangf1fc31e2024-10-12 03:21:03 -07006870 if(signal_globe.type != signal.type)
6871 {
6872 data_signal[0] = signal.type;
6873 data_signal[1] = signal.rssi;
6874 data_signal[2] = signal.rxlev;
6875 data_signal[3] = signal.ber;
6876 data_signal[4] = signal.rscp;
6877 data_signal[5] = signal.ecno;
6878 data_signal[6] = signal.rsrq;
6879 data_signal[7] = signal.rsrp;
6880 signal_globe.type = signal.type;
6881 signal_state_change(data_signal, sizeof(data_signal));
liubin281ac462023-07-19 14:22:54 +08006882 }
liubin281ac462023-07-19 14:22:54 +08006883 break;
6884 }
6885 default:
6886 {
6887 LOGE("Unknown URC : %d", msg->msg);
6888 break;
6889 }
6890 }
6891}
6892
6893static void* pack_process_thread(void* arg)
6894{
6895 UNUSED(arg);
6896 info_queue_item_t* item = NULL;
6897 mbtk_queue_init(&info_queue);
6898 pthread_mutex_init(&info_mutex, NULL);
6899 pthread_cond_init(&info_cond, NULL);
6900
6901 memset(&band_support, 0xFF, sizeof(mbtk_band_info_t));
6902
6903 pthread_mutex_lock(&info_mutex);
6904 while(TRUE)
6905 {
6906 if(mbtk_queue_empty(&info_queue))
6907 {
6908 LOG("Packet process wait...");
6909 pthread_cond_wait(&info_cond, &info_mutex);
6910 LOG("Packet process continue...");
6911 }
6912 else
6913 {
6914 LOG("Packet process queue not empty,continue...");
6915 }
6916
6917 // Process all information request.
6918 mbtk_info_err_enum err;
6919 while((item = (info_queue_item_t*)mbtk_queue_get(&info_queue)) != NULL)
6920 {
6921 if(item->cli_info) { // REQ form client.
6922 mbtk_info_pack_t *pack = (mbtk_info_pack_t*)item->pack;
6923 LOG("Process REQ %s.", id2str(pack->info_id));
6924 at_process = true;
6925 err = pack_req_process(item->cli_info, pack);
6926 if(err != MBTK_INFO_ERR_SUCCESS)
6927 {
6928 if(item->cli_info->fd != DATA_CALL_BOOTCONN_FD)
6929 {
6930 pack_error_send(item->cli_info->fd, pack->info_id + 1, err);
6931 }
6932 else
6933 {
wangyouqiang80487e42024-05-24 15:06:20 +08006934 if(pack->data != NULL)
6935 {
6936 free(pack->data);
6937 }
6938 if(item->cli_info)
6939 {
6940 free(item->cli_info);
6941 }
liubin281ac462023-07-19 14:22:54 +08006942 }
6943 }
6944 at_process = false;
6945 mbtk_info_pack_free(&pack);
6946 free(item);
6947 } else { // REQ from myself.
6948 info_urc_msg_t *urc = (info_urc_msg_t*)item->pack;
6949 LOG("Process URC %d.", urc->msg);
6950 urc_msg_process(urc);
6951 if(!urc->data)
6952 free(urc->data);
6953 free(urc);
6954 }
6955 }
6956 }
6957 pthread_mutex_unlock(&info_mutex);
6958 return NULL;
6959}
6960
6961void apn_prop_get()
6962{
6963 char prop_name[20];
6964 char prop_data[300];
6965 // cid : 2 - 7
6966 int cid = MBTK_APN_CID_MIN;
6967 mbtk_apn_info_t apn;
6968 for(; cid <= MBTK_APN_CID_MAX; cid++) {
6969 memset(prop_name, 0, 20);
6970 memset(prop_data, 0, 300);
6971 memset(&apn, 0, sizeof(mbtk_apn_info_t));
6972 sprintf(prop_name, "%s_%d",MBTK_APN_PROP,cid);
6973 if(property_get(prop_name, prop_data, "") > 0 && !str_empty(prop_data)) {
6974 apn.cid = cid;
6975 char *ptr_1 = prop_data;
6976 apn.ip_type = (mbtk_ip_type_enum)atoi(ptr_1);
6977 ptr_1 = strstr(ptr_1, ",");
6978 if(!ptr_1) {
6979 continue;
6980 }
6981 ptr_1++; // Jump ',' to apn
6982
6983 char *ptr_2 = strstr(ptr_1, ",");
6984 if(!ptr_2) {
6985 continue;
6986 }
6987 memcpy(apn.apn, ptr_1, ptr_2 - ptr_1); // apn
6988
6989 ptr_2++; // Jump ',' to user
6990 ptr_1 = strstr(ptr_2, ",");
6991 if(!ptr_1) {
6992 continue;
6993 }
6994 if(memcmp(ptr_2, "NULL", 4)) { // Not "NULL"
6995 memcpy(apn.user, ptr_2, ptr_1 - ptr_2); // user
6996 }
6997
6998 ptr_1++; // Jump ',' to pass
6999 ptr_2 = strstr(ptr_1, ",");
7000 if(!ptr_2) {
7001 continue;
7002 }
7003 if(memcmp(ptr_1, "NULL", 4)) { // Not "NULL"
7004 memcpy(apn.pass, ptr_1, ptr_2 - ptr_1); // pass
7005 }
7006
7007 ptr_2++; // Jump ',' to auth (Is last item)
7008 if(memcmp(ptr_2, "NULL", 4)) { // Not "NULL"
7009 memcpy(apn.auth, ptr_2, strlen(ptr_2)); // auth
7010 }
7011
7012 req_apn_set(&apn, NULL);
7013 }
7014 }
7015}
7016
b.liu9e8584b2024-11-06 19:21:28 +08007017#if 0
liubin281ac462023-07-19 14:22:54 +08007018/*
b.liue0ab2442024-02-06 18:53:28 +08007019root@OpenWrt:/usrdata# cat /proc/mtd
7020dev: size erasesize name
7021mtd0: 00040000 00020000 "bootloader"
7022mtd1: 00020000 00020000 "cp_reliabledata"
7023mtd2: 00020000 00020000 "ap_reliabledata"
7024mtd3: 00020000 00020000 "cp_reliabledata_backup"
7025mtd4: 00020000 00020000 "ap_reliabledata_backup"
7026mtd5: 00020000 00020000 "mep-ota"
7027mtd6: 00020000 00020000 "mep-ota_backup"
7028mtd7: 00040000 00020000 "dtim"
7029mtd8: 00f40000 00020000 "cpimage"
7030mtd9: 000c0000 00020000 "u-boot"
7031mtd10: 00500000 00020000 "kernel"
7032mtd11: 00100000 00020000 "asr_flag"
7033mtd12: 01400000 00020000 "rootfs"
7034mtd13: 01400000 00020000 "oem_data"
7035mtd14: 01e00000 00020000 "OTA"
7036mtd15: 01400000 00020000 "rootfs_data"
7037mtd16: 081a0000 00020000 "user_data"
7038mtd17: 00d20000 00020000 "MRVL_BBM"
7039*/
7040static int partition_name_2_dev(const char *name, char *dev) {
7041 if(name == NULL || dev == NULL) {
7042 LOGE("ARG error.");
7043 return -1;
7044 }
7045
b.liu9e8584b2024-11-06 19:21:28 +08007046 FILE* fp = fopen("/proc/mtd", "r");
b.liue0ab2442024-02-06 18:53:28 +08007047 if (fp == NULL) {
7048 LOGE("Open MTD failed!");
7049 return -1;
7050 }
7051
7052 char buf[1024];
7053 while (fgets(buf, 1024, fp) != NULL) {
7054 if(strstr(buf, name)) {
7055 int index = atoi(buf + 3);
7056 sprintf(dev, "/dev/mtdblock%d", index);
7057 LOGD("%s -> %s", name, dev);
7058 return 0;
7059 }
7060 }
7061
7062 return -1;
7063}
b.liu9e8584b2024-11-06 19:21:28 +08007064#endif
7065#if 0
b.liue0ab2442024-02-06 18:53:28 +08007066static int custom_partition_read(const char *name, mbtk_cust_info_t *cust_info)
7067{
b.liu9e8584b2024-11-06 19:21:28 +08007068 int fd = 0;
7069 unsigned int flaglen = 0;
b.liue0ab2442024-02-06 18:53:28 +08007070 flaglen = sizeof(mbtk_cust_info_t);
7071
7072 char mtd_path[50] = {0};
7073 if(partition_name_2_dev(name, mtd_path)) {
7074 LOGE("partition_name_2_dev() failed!");
7075 return -1;
7076 }
7077
7078 fd = open(mtd_path, O_RDONLY);
7079 if (fd < 0) {
7080 LOGE("Fatal error: can't open cust info %s\n", mtd_path);
7081 return -1;
7082 }
7083
7084 if (read(fd, cust_info, flaglen) < 0)
7085 goto error;
7086 if (cust_info->header != CUST_INFO_HEADER) {
7087 LOGE("Cust info partition error.");
7088 goto error;
7089 } else {
7090 if(cust_info->band_type == 1) { // CN
7091 LOGD("Band : CN");
7092 } else if(cust_info->band_type == 2) { // EU
7093 LOGD("Band : EU");
7094 } else {
7095 LOGE("Unknown band type:%d", cust_info->band_type);
7096 goto error;
7097 }
7098 }
7099 close(fd);
7100 return 0;
7101error:
7102 close(fd);
7103 return -1;
7104}
b.liu9e8584b2024-11-06 19:21:28 +08007105#endif
b.liue0ab2442024-02-06 18:53:28 +08007106
7107/*
liubin281ac462023-07-19 14:22:54 +08007108AT*BAND=15,78,147,482,134742231
7109
7110OK
7111*/
b.liubb5e7682024-02-28 20:13:04 +08007112static void* band_config_thread()
liubin281ac462023-07-19 14:22:54 +08007113{
b.liubb5e7682024-02-28 20:13:04 +08007114 mbtk_device_info_modem_t info_modem;
b.liuf37bd332024-03-18 13:51:24 +08007115 memset(&band_set_info, 0, sizeof(band_set_info_t));
b.liubb5e7682024-02-28 20:13:04 +08007116 memset(&info_modem, 0, sizeof(mbtk_device_info_modem_t));
b.liu288093c2024-05-09 17:02:57 +08007117 band_set_success = FALSE;
b.liuf37bd332024-03-18 13:51:24 +08007118 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 +08007119 LOGD("mbtk_dev_info_read(MODEM) fail, use default band.");
b.liu288093c2024-05-09 17:02:57 +08007120 band_area = MBTK_MODEM_BAND_AREA_ALL;
b.liuf37bd332024-03-18 13:51:24 +08007121 band_set_info.band_gsm = MBTK_BAND_ALL_GSM_DEFAULT;
7122 band_set_info.band_wcdma = MBTK_BAND_ALL_WCDMA_DEFAULT;
7123 band_set_info.band_tdlte = MBTK_BAND_ALL_TDLTE_DEFAULT;
7124 band_set_info.band_fddlte = MBTK_BAND_ALL_FDDLTE_DEFAULT;
b.liu288093c2024-05-09 17:02:57 +08007125 band_set_info.band_lte_ext = MBTK_BAND_ALL_EXT_LTE_DEFAULT;
b.liubb5e7682024-02-28 20:13:04 +08007126 } else {
b.liu288093c2024-05-09 17:02:57 +08007127 band_area = info_modem.band_area;
b.liuf37bd332024-03-18 13:51:24 +08007128 band_set_info.band_gsm = info_modem.band_gsm;
7129 band_set_info.band_wcdma = info_modem.band_wcdma;
7130 band_set_info.band_tdlte = info_modem.band_tdlte;
7131 band_set_info.band_fddlte = info_modem.band_fddlte;
b.liuf678f992024-05-08 15:23:10 +08007132 band_set_info.band_lte_ext = info_modem.band_lte_ext;
b.liuf37bd332024-03-18 13:51:24 +08007133 }
b.liubb5e7682024-02-28 20:13:04 +08007134
b.liuf37bd332024-03-18 13:51:24 +08007135 bool is_first = TRUE;
b.liu288093c2024-05-09 17:02:57 +08007136 while(!band_set_success) {
b.liuf37bd332024-03-18 13:51:24 +08007137 info_urc_msg_t *urc = (info_urc_msg_t*)malloc(sizeof(info_urc_msg_t));
7138 if(!urc)
7139 {
7140 LOG("malloc() fail[%d].", errno);
7141 break;
b.liubb5e7682024-02-28 20:13:04 +08007142 } else {
b.liuf37bd332024-03-18 13:51:24 +08007143 urc->msg = INFO_URC_MSG_SET_BAND;
7144 urc->data = NULL;
7145 urc->data_len = 0;
7146 send_pack_to_queue(NULL, urc);
7147
7148 if(is_first) {
7149 is_first = FALSE;
7150 } else {
b.liu288093c2024-05-09 17:02:57 +08007151 LOGE("*BAND exec error, will retry in 5s.");
b.liuf37bd332024-03-18 13:51:24 +08007152 }
b.liu288093c2024-05-09 17:02:57 +08007153 sleep(5);
b.liubb5e7682024-02-28 20:13:04 +08007154 }
7155 }
7156
b.liuf37bd332024-03-18 13:51:24 +08007157 LOGD("Set Band thread exit.");
b.liubb5e7682024-02-28 20:13:04 +08007158 return NULL;
liubin281ac462023-07-19 14:22:54 +08007159}
7160
b.liu9e8584b2024-11-06 19:21:28 +08007161#if 0
liubin281ac462023-07-19 14:22:54 +08007162static void* net_monitor_thread(void* arg)
7163{
7164 UNUSED(arg);
7165 // Start network monitor
b.liu9e8584b2024-11-06 19:21:28 +08007166// int cid;
liubin281ac462023-07-19 14:22:54 +08007167 while(1) {
7168#if 0
7169 // Config IP
7170 list_node_t* apn_list = NULL;
7171 if(!apn_state_get(&apn_list) && apn_list != NULL) {
7172 info_apn_ip_t *apn = NULL;
7173 for(cid = MBTK_APN_CID_MIN; cid <= MBTK_APN_CID_MAX && cid_active[cid]; cid++) {
7174 bool ip_found = false;
7175 list_first(apn_list);
7176 while ((apn = (info_apn_ip_t*) list_next(apn_list))) {
7177 if(cid == apn->cid) {
7178 ip_found = true;
7179 break;
7180 }
7181 }
7182
7183 char dev[20] = {0};
7184 sprintf(dev, "ccinet%d", cid - 1);
7185 if(ip_found) { // Ip ok,set IP.
7186 if(apn->ipv4_valid) {
7187 if(mbtk_ifc_configure2(dev, (char*)apn->ipv4, 0, NULL, "255.255.255.0")) {
7188 LOGD("Config %s IPv4 %s fail.", dev, apn->ipv4);
7189 } else {
7190 LOGD("Config %s IPv4 %s success.", dev, apn->ipv4);
7191 }
7192 }
7193
7194 if(apn->ipv6_valid) {
7195 if(mbtk_ipv6_config(dev, (char*)apn->ipv6, 64)) {
7196 LOGD("Config %s IPv6 %s fail.", dev, apn->ipv6);
7197 } else {
7198 LOGD("Config %s IPv6 %s success.", dev, apn->ipv6);
7199 }
7200 }
7201 } else { // No ip
7202 if(mbtk_ifc_configure2(dev, NULL, 0, NULL, NULL)) {
7203 LOGD("Config %s IPv4 0 fail.", dev);
7204 } else {
7205 LOGD("Config %s IPv4 0 success.", dev);
7206 }
7207 }
7208 }
7209
7210 list_free(apn_list);
7211 }
7212#endif
7213
7214 if(net_info.radio_state == MBTK_RADIO_STATE_ON && net_info.sim_state == MBTK_SIM_READY) {
7215#if 0
7216 urc_msg_distribute(true, INFO_URC_MSG_NET_CS_REG_STATE, NULL, 0);
7217#else
7218 info_urc_msg_t *urc = (info_urc_msg_t*)malloc(sizeof(info_urc_msg_t));
7219 if(!urc)
7220 {
7221 LOG("malloc() fail[%d].", errno);
7222 } else {
7223 urc->msg = INFO_URC_MSG_NET_STATE_LOG;
7224 urc->data = NULL;
7225 urc->data_len = 0;
7226 send_pack_to_queue(NULL, urc);
7227 }
7228#endif
wangyouqiangce45a102024-04-18 18:08:29 +08007229 sleep(15);
liubin281ac462023-07-19 14:22:54 +08007230 }
wangyouqiangce45a102024-04-18 18:08:29 +08007231 else
7232 {
7233 info_urc_msg_t *urc = (info_urc_msg_t*)malloc(sizeof(info_urc_msg_t));
7234 if(!urc)
7235 {
7236 LOG("malloc() fail[%d].", errno);
7237 }
7238 else
7239 {
7240 urc->msg = INFO_URC_MSG_GET_SIM_STATE;
7241 urc->data = NULL;
7242 urc->data_len = 0;
7243 send_pack_to_queue(NULL, urc);
7244 }
7245 sleep(2);
7246 }
liubin281ac462023-07-19 14:22:54 +08007247 }
7248
7249 LOGD("monitor_thread exit.");
7250 return NULL;
7251}
b.liu9e8584b2024-11-06 19:21:28 +08007252#endif
liubin281ac462023-07-19 14:22:54 +08007253
b.liu9e8584b2024-11-06 19:21:28 +08007254#if 0
liubin281ac462023-07-19 14:22:54 +08007255static void* urc_process_thread(void* arg)
7256{
7257 UNUSED(arg);
7258 info_urc_msg_t* item = NULL;
7259 mbtk_queue_init(&urc_queue);
7260 pthread_mutex_init(&urc_mutex, NULL);
7261 pthread_cond_init(&urc_cond, NULL);
7262
7263 pthread_mutex_lock(&urc_mutex);
7264 while(TRUE)
7265 {
7266 if(mbtk_queue_empty(&urc_queue))
7267 {
7268 LOG("URC process wait...");
7269 pthread_cond_wait(&urc_cond, &urc_mutex);
7270 LOG("URC process continue...");
7271 }
7272 else
7273 {
7274 LOG("URC process queue not empty,continue...");
7275 }
7276
7277 // Process all information request.
7278 while((item = (info_urc_msg_t*)mbtk_queue_get(&urc_queue)) != NULL)
7279 {
7280 LOG("Process URC %d.", item->msg);
7281 uint8 *data = (uint8*)item->data;
7282 switch(item->msg) {
7283 case INFO_URC_MSG_RADIO_STATE:
7284 {
7285 radio_state_change(item->data, item->data_len);
7286 break;
7287 }
7288 case INFO_URC_MSG_CGEV:
7289 {
7290 bool act = data[0];
7291 int cid = data[1];
7292 if(cid > 0) {
7293 net_ifc_state_change(act, cid);
7294 }
7295 break;
7296 }
7297 default:
7298 {
7299 LOGE("Unknown URC : %d", item->msg);
7300 break;
7301 }
7302 }
7303 if(!item->data)
7304 free(item->data);
7305 free(item);
7306 }
7307 }
7308 pthread_mutex_unlock(&urc_mutex);
7309
7310 return NULL;
7311}
b.liu9e8584b2024-11-06 19:21:28 +08007312#endif
liubin281ac462023-07-19 14:22:54 +08007313
7314static void ril_at_ready_process()
7315{
7316 net_info.radio_state = (isRadioOn() == 1) ? MBTK_RADIO_STATE_ON : MBTK_RADIO_STATE_OFF;
7317#if 1
7318 if (net_info.radio_state != MBTK_RADIO_STATE_ON)
7319 {
7320 setRadioPower(1);
7321 } else { // Radio has ON
7322 apn_prop_get();
7323 }
7324
7325 if(net_info.radio_state == MBTK_RADIO_STATE_ON)
7326 {
7327 at_send_command("AT+CEREG=2", NULL);
7328 }
7329
b.liu9e8584b2024-11-06 19:21:28 +08007330// int count = 0;
liubin281ac462023-07-19 14:22:54 +08007331#endif
7332 net_info.sim_state = getSIMStatus();
7333#if 0
7334 while (net_info.sim_state != MBTK_SIM_READY && count < 30)
7335 {
7336 if(net_info.radio_state != MBTK_RADIO_STATE_ON)
7337 {
7338 setRadioPower(1);
7339 }
7340 LOGD("Waitting for SIM READY...");
7341 sleep(1);
7342 net_info.sim_state = getSIMStatus();
7343 count++;
7344 }
7345#endif
7346 if(net_info.sim_state == MBTK_SIM_READY)
7347 {
7348 LOGD("SIM READY!");
r.xiao7c62bc62024-05-24 00:53:30 -07007349 at_send_command("AT+COPS=3", NULL);
liubin281ac462023-07-19 14:22:54 +08007350 }
7351 else
7352 {
7353 LOGE("SIM NOT READY!");
7354 }
liubin281ac462023-07-19 14:22:54 +08007355}
7356
wangyouqiang80487e42024-05-24 15:06:20 +08007357int mbtk_get_apn_send_pack(void)
7358{
7359 sock_client_info_t *info = (sock_client_info_t*)malloc(sizeof(sock_client_info_t));
7360 if(info == NULL)
7361 {
7362 LOG("clinent_info malloc() fail.");
7363 return -1;
7364 }
7365 memset(info, 0, sizeof(sock_client_info_t));
7366 info->fd = DATA_CALL_APN_GET_FD;
7367
7368 mbtk_info_pack_t* pack = mbtk_info_pack_creat(MBTK_INFO_ID_NET_QSER_APN_REQ);
7369 if(pack == NULL)
7370 {
7371 free(info);
7372 LOG("Packet malloc() fail.");
7373 return -1;
7374 }
7375
7376 send_pack_to_queue(info, pack);
7377 return 0;
7378}
7379
liubin281ac462023-07-19 14:22:54 +08007380int mbtk_info_server_start()
7381{
7382 signal(SIGPIPE, SIG_IGN);
7383
wangyouqiangce45a102024-04-18 18:08:29 +08007384 //check cfun and sim card status
7385 ril_at_ready_process();
7386
7387 //any AT instruction that is not sent through pack_process_thread needs to precede the thread
7388 //thread create
liubin281ac462023-07-19 14:22:54 +08007389 if(sock_listen_fd > 0)
7390 {
7391 LOG("Information Server Has Started.");
7392 return -1;
7393 }
7394
7395 struct sockaddr_un server_addr;
7396 sock_listen_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
7397 if(sock_listen_fd < 0)
7398 {
7399 LOG("socket() fail[%d].", errno);
7400 return -1;
7401 }
7402
7403 // Set O_NONBLOCK
7404 int flags = fcntl(sock_listen_fd, F_GETFL, 0);
7405 if (flags < 0)
7406 {
7407 LOG("Get flags error:%d", errno);
7408 goto error;
7409 }
7410 flags |= O_NONBLOCK;
7411 if (fcntl(sock_listen_fd, F_SETFL, flags) < 0)
7412 {
7413 LOG("Set flags error:%d", errno);
7414 goto error;
7415 }
7416
7417 unlink(SOCK_INFO_PATH);
7418 memset(&server_addr, 0, sizeof(struct sockaddr_un));
7419 server_addr.sun_family = AF_LOCAL;
7420 strcpy(server_addr.sun_path, SOCK_INFO_PATH);
7421 if(bind(sock_listen_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)))
7422 {
7423 LOG("bind() fail[%d].", errno);
7424 goto error;
7425 }
7426
7427 if(listen(sock_listen_fd, SOCK_CLIENT_MAX))
7428 {
7429 LOG("listen() fail[%d].", errno);
7430 goto error;
7431 }
7432
7433 sock_client_list = list_create(sock_cli_free_func);
7434 if(sock_client_list == NULL)
7435 {
7436 LOG("list_create() fail.");
7437 goto error;
7438 }
7439
b.liu9e8584b2024-11-06 19:21:28 +08007440 pthread_t info_pid, pack_pid/*, monitor_pid, urc_pid*/, bootconn_pid;
liubin281ac462023-07-19 14:22:54 +08007441 pthread_attr_t thread_attr;
7442 pthread_attr_init(&thread_attr);
7443 if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
7444 {
7445 LOG("pthread_attr_setdetachstate() fail.");
7446 goto error;
7447 }
7448
7449 if(pthread_create(&info_pid, &thread_attr, info_main_pthread, NULL))
7450 {
7451 LOG("pthread_create() fail.");
7452 goto error;
7453 }
7454
7455 if(pthread_create(&pack_pid, &thread_attr, pack_process_thread, NULL))
7456 {
7457 LOG("pthread_create() fail.");
7458 goto error;
7459 }
7460
7461#if 0
7462 if(pthread_create(&urc_pid, &thread_attr, urc_process_thread, NULL))
7463 {
7464 LOG("pthread_create() fail.");
7465 goto error;
7466 }
7467#endif
7468
b.liubb5e7682024-02-28 20:13:04 +08007469 // Set Band
7470 // AT*BAND=15,78,147,482,134742231
7471 char buff[10];
7472 memset(buff, 0, 10);
7473 property_get("persist.mbtk.band_config", buff, "");
7474 if(strlen(buff) == 0) {
7475 pthread_t band_pid;
7476 if(pthread_create(&band_pid, &thread_attr, band_config_thread, NULL))
7477 {
7478 LOG("pthread_create() fail.");
7479 }
7480 }
7481
b.liuf1ab8152024-05-23 13:16:07 +08007482#if 0
liubin281ac462023-07-19 14:22:54 +08007483 if(pthread_create(&monitor_pid, &thread_attr, net_monitor_thread, NULL))
7484 {
7485 LOG("pthread_create() fail.");
7486 }
b.liuf1ab8152024-05-23 13:16:07 +08007487#endif
liubin281ac462023-07-19 14:22:54 +08007488
7489 //mbtk wyq for data_call_ex add start
7490 if(pthread_create(&bootconn_pid, &thread_attr, data_call_bootconn_pthread, NULL))
7491 {
7492 LOG("pthread_create() fail.");
7493 }
7494 //mbtk wyq for data_call_ex add end
7495
7496 pthread_attr_destroy(&thread_attr);
7497
wangyouqiang80487e42024-05-24 15:06:20 +08007498 mbtk_qser_apn_init();
7499
liubin281ac462023-07-19 14:22:54 +08007500 LOG("MBTK Information Server Start...");
7501
7502 return 0;
7503
7504error:
7505 close(sock_listen_fd);
7506 sock_listen_fd = -1;
7507 return -1;
7508}
7509
7510#if 0
7511int main(int argc, char *argv[])
7512{
7513 if(mbtk_info_server_start())
7514 {
7515 return -1;
7516 }
7517
7518 while(1)
7519 {
7520 sleep(24 * 60 * 60);
7521 }
7522
7523 return 0;
7524}
7525#endif
7526
7527