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