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