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