blob: 5e81f2c0a3b124cccf56dd21b36a4d17d0b7bd64 [file] [log] [blame]
liubin281ac462023-07-19 14:22:54 +08001#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <sys/socket.h>
5#include <errno.h>
6#include <fcntl.h>
7#include <string.h>
8#include <netinet/in.h>
9#include <arpa/inet.h>
10#include <linux/un.h>
11#include <linux/netlink.h>
12#include <cutils/properties.h>
13#include <time.h>
14#include <sys/time.h>
15
16//#include "cploader.h"
17#include "mbtk_log.h"
18#include "mbtk_ifc.h"
19#include "mbtk_type.h"
20#include "atchannel.h"
21#include "at_tok.h"
22#include "mbtk_utils.h"
23#include "mbtk_task.h"
24#include "mbtk_info.h"
25#include "mbtk_ntp.h"
26#include "mbtk_net_control.h"
27#include "info_data.h"
wangyouqiang38e53362024-01-23 10:53:48 +080028#include "mbtk_led.h"
liubin281ac462023-07-19 14:22:54 +080029
30#define TEMP_FAILURE_RETRY(exp) ({ \
31 typeof (exp) _rc; \
32 do { \
33 _rc = (exp); \
34 } while (_rc == -1 && errno == EINTR); \
35 _rc; })
36
37#define BUFFER_SIZE 2048
38#define UEVENT_USIM_DEV "/devices/virtual/usim_event/usim0"
39#define MBTK_BOOT_SERVER_READY "/etc/init.d/mbtk_boot_server_ready"
b.liubb590492024-06-13 16:42:08 +080040#define MBTK_BOOT_NET_READY "/etc/init.d/mbtk_boot_net_ready"
41#define MBTK_RILD_PID_FILE "/var/run/mbtk_rild.pid"
42#define MBTK_RILD_TEMP_FILE "/tmp/mbtk_rild.count"
liubin281ac462023-07-19 14:22:54 +080043
44struct cooling_device
45{
46 const char *name;
47 const char *action;
48 const char *path;
49 const char *event;
50 const char *subsystem;
51};
52static pthread_t uevnet_task_id;
53extern net_info_t net_info;
54extern mbtK_cell_pack_info_t cell_info;
55extern info_cgact_wait_t cgact_wait;
56extern bool at_process;
b.liubb590492024-06-13 16:42:08 +080057static bool is_first_boot = FALSE;
liubin281ac462023-07-19 14:22:54 +080058
59void setRadioPower(int isOn);
60int urc_msg_distribute(bool async_process, info_urc_msg_id_enum msg, void *data, int data_len);
61int mbtk_signal_log(char *data);
62
63/* Called on command thread */
64static void onATTimeout()
65{
66 LOGI("AT channel timeout; closing\n");
67 at_close();
68}
69
70/* Called on command or reader thread */
71static void onATReaderClosed()
72{
73 LOGI("AT channel closed\n");
74 at_close();
75}
76
77//int req_time_set(int type, char *time, int *cme_err);
78static int metis_strptime(char *str_time)
79{
80 struct tm stm;
81 char dateTime[30];
82 struct timeval tv;
83 if(strptime(str_time, "%Y-%m-%d %H:%M:%S",&stm) != NULL)
84 {
85 time_t _t = mktime(&stm);
86 tv.tv_sec = _t;
87 if(settimeofday(&tv, NULL)) {
88 LOG("Set time fail:%d", errno);
89 return -1;
90 } else {
91 LOG("Set time to %s.", str_time);
92 return 0;
93 }
94 } else {
95 LOG("Set time fail.");
96 return -1;
97 }
98}
99
100static void* ntp_pthread_run(void* arg)
101{
102 // Waitting for network connected.
103 while(mbtk_net_state_get() == MBTK_NET_STATE_OFF) {
104 sleep(1);
105 }
106 LOG("Network is connected.");
107
108 char time_type[10];
109 while(1){
110 memset(time_type, 0, 10);
111 property_get("persist.mbtk.time_type", time_type, "0");
112 if(atoi(time_type) == MBTK_TIME_TYPE_NTP) // NTP time
113 {
114 char time_str[100] = {0};
115 time_t time = 0;
116 while((time = (time_t)mbtk_at_systime()) == 0) {
117 usleep(100000);
118 }
119 struct tm *tm_t;
120 tm_t = localtime(&time);
121 strftime(time_str,128,"%F %T",tm_t);
122
123 // NTP time
124 metis_strptime(time_str);
125 } else {
126 break;
127 }
128
129 sleep(64); // Sleep 64s.
130 }
131 return NULL;
132}
133
134static void ntp_thread_start()
135{
136 pthread_t ntp_pid;
137 pthread_attr_t thread_attr;
138 pthread_attr_init(&thread_attr);
139 if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
140 {
141 LOG("pthread_attr_setdetachstate() fail.");
142 return;
143 }
144
145 if(pthread_create(&ntp_pid, &thread_attr, ntp_pthread_run, NULL))
146 {
147 LOG("pthread_create() fail.");
148 }
149}
150
b.liubb590492024-06-13 16:42:08 +0800151static void mbtk_net_ready()
152{
153 // /etc/init.d/mbtk_boot_net_ready
154 if(is_first_boot) {
155 if(access(MBTK_BOOT_NET_READY , X_OK) == 0) {
156 LOGD("Exec : %s", MBTK_BOOT_NET_READY);
157 system(MBTK_BOOT_NET_READY);
158 } else {
159 LOGE("%s can not exec.", MBTK_BOOT_NET_READY);
160 }
161 is_first_boot = FALSE; // Only one time.
162 } else {
163 LOGD("No exec : %s", MBTK_BOOT_NET_READY);
164 }
165}
166
liubin281ac462023-07-19 14:22:54 +0800167bool sms_cmt = false;
168mbtk_sim_card_info sim_info_reg={0};
169static void onUnsolicited(const char *s, const char *sms_pdu)
170{
171 LOGV("URC : %s", s);
172 // MBTK_AT_READY
173 if (strStartsWith(s, "MBTK_AT_READY")) // AT ready.
174 {
175
176 }
177#if 0
178 else if(strStartsWith(s, "*SIMDETEC:")) // *SIMDETEC:1,SIM
179 {
180 const char* ptr = strstr(s, ",");
181 if(ptr)
182 {
183 ptr++; // Jump ','
184 if(memcmp(ptr, "SIM", 3) == 0)
185 net_info.sim_state = MBTK_SIM_STATE_READY;
186 else
187 net_info.sim_state = MBTK_SIM_STATE_ABSENT;
188 }
189 }
190#endif
191 else if(strStartsWith(s, "*RADIOPOWER:")) // "*RADIOPOWER: 1"
192 {
193 const char* ptr = s + strlen("*RADIOPOWER:");
194 while(*ptr != '\0' && *ptr == ' ' )
195 {
196 ptr++;
197 }
198
199 uint8 state;
200 if(*ptr == '1') {
201 //net_info.radio_state = MBTK_RADIO_STATE_ON;
202 // mbtk_radio_ready_cb();
203 state = (uint8)1;
204 } else {
205 //net_info.radio_state = MBTK_RADIO_STATE_OFF;
206 state = (uint8)0;
207 }
208 urc_msg_distribute(true, INFO_URC_MSG_RADIO_STATE, &state, sizeof(uint8));
209 }
210 // "CONNECT"
211 else if(strStartsWith(s, "CONNECT"))
212 {
213 if(cgact_wait.waitting && cgact_wait.act) {
214 cgact_wait.waitting = false;
215 }
216
217 uint8 data_pdp;
218 data_pdp = 1; //
219 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
220 }
221 // +CGEV:
222 // +CGEV: NW DEACT <cid>,<cid>
223 // +CGEV: ME DEACT <cid>,<cid>
224 // +CGEV: NW PDN DEACT <cid>
225 // +CGEV: ME PDN DEACT <cid>
226 // +CGEV: NW DETACH
227 // +CGEV: ME DETACH
228 //
229 // +CGEV: NW ACT <cid>,<cid>
230 // +CGEV: ME ACT <cid>,<cid>
231 // +CGEV: EPS PDN ACT <cid>
232 // +CGEV: ME PDN ACT <cid>,<reason>,<cid>
233 // +CGEV: ME PDN ACT <cid>,<reason>
234 // +CGEV: NW PDN ACT <cid>
235 // +CGEV: EPS ACT <cid>
236 // +CGEV: NW MODIFY <cid>,<reason>
237 // +CGEV: NW REATTACH
238 else if(strStartsWith(s, "+CGEV:"))
239 {
240 if(at_process) {
241 if(cgact_wait.act) {
242 if(strStartsWith(s, "+CGEV: ME PDN ACT ")) { // +CGEV: ME PDN ACT 15,4
243 if(cgact_wait.cid == atoi(s + 18)) {
244 cgact_wait.waitting = false;
245 }
246
247 uint8 data_pdp;
248 char* tmp_s = memdup(s + 18,strlen(s + 18));
249 char* free_ptr = tmp_s;
250 char *line = tmp_s;
251 int tmp_int;
252 if (at_tok_start(&line) < 0)
253 {
254 goto at_PDP_CREG_EXIT;
255 }
256 if (at_tok_nextint(&line, &tmp_int) < 0)
257 {
258 goto at_PDP_CREG_EXIT;
259 }
260 if (at_tok_nextint(&line, &tmp_int) < 0)
261 {
262 goto at_PDP_CREG_EXIT;
263 }
264 data_pdp = tmp_int;
265at_PDP_CREG_EXIT:
266 free(free_ptr);
267
268 //data_pdp = (uint8)atoi(s + 20); //reason
269 if(cgact_wait.cid >= 1 && cgact_wait.cid < 8)
270 {
271 if(data_pdp == 0)
272 {
273 data_pdp = 25;
274 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
275 //data_pdp = cgact_wait.cid + 200;
276 }
277 else if(data_pdp == 1)
278 {
279 data_pdp = 26;
280 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
281 }
282 else if(data_pdp == 2)
283 {
284 data_pdp = 27;
285 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
286 }
287 else if(data_pdp == 3)
288 {
289 data_pdp = 27;
290 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
291 }
292 else
293 {
294
295 }
296 if(cgact_wait.cid != 0)
297 {
298 data_pdp = cgact_wait.cid + 200;
299 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
300 }
301 }
302 } else if(strStartsWith(s, "+CGEV: NW MODIFY ")) { // +CGEV: NW MODIFY 1,4
303 if(cgact_wait.cid == atoi(s + 17)) {
304 cgact_wait.waitting = false;
305 }
306 }
307 } else {
308 if(strStartsWith(s, "+CGEV: ME PDN DEACT ")) { // +CGEV: ME PDN DEACT 1
309 if(cgact_wait.cid == atoi(s + 20)) {
310 cgact_wait.waitting = false;
311 }
312 uint8 data_pdp;
313 data_pdp = 0; //
314 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
315 if(cgact_wait.cid != 0)
316 {
317 data_pdp = cgact_wait.cid + 100;
318 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
319 }
320 }
321 }
322 } else {
323 // apn_state_set
324
325 // +CGEV: NW PDN DEACT <cid>
326
327 // +CGEV: EPS PDN ACT 1
328 // +CGEV: ME PDN ACT 8,1
329
330 // +CGEV: ME PDN ACT 2,4
331 uint8 data[2] = {0xFF};
332 if(strStartsWith(s, "+CGEV: NW PDN DEACT ")) { // +CGEV: NW PDN DEACT <cid>
333 //apn_state_set(atoi(s + 20), false);
334 data[0] = (uint8)0;
335 data[1] = (uint8)atoi(s + 20);
336
337 uint8 data_pdp;
338 data_pdp = 0; //
339 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
wangyouqiang65884152023-10-25 19:54:15 +0800340 data_pdp = data[1] + 100;
liubin281ac462023-07-19 14:22:54 +0800341 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
342 } else if(strStartsWith(s, "+CGEV: EPS PDN ACT ")) { // +CGEV: EPS PDN ACT <cid>
343 //apn_state_set(atoi(s + 19), true);
b.liuf37bd332024-03-18 13:51:24 +0800344#if (defined(MBTK_AF_SUPPORT) || defined(MBTK_ALL_CID_SUPPORT))
wangyouqianged88c722023-11-22 16:33:43 +0800345 //data[0] = (uint8)1;
346 //data[1] = (uint8)atoi(s + 19);
347#else
liubin281ac462023-07-19 14:22:54 +0800348 data[0] = (uint8)1;
349 data[1] = (uint8)atoi(s + 19);
wangyouqianged88c722023-11-22 16:33:43 +0800350#endif
wangyouqiang65884152023-10-25 19:54:15 +0800351 } else if(strStartsWith(s, "+CGEV: ME PDN DEACT ")) { // +CGEV: EPS PDN DEACT <cid>
352 //apn_state_set(atoi(s + 19), true);
353 data[0] = (uint8)0;
354 data[1] = (uint8)atoi(s + 20);
355
356 uint8 data_pdp;
357 data_pdp = 0; //
358 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
359 data_pdp = data[1] + 100;
360 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
liubin281ac462023-07-19 14:22:54 +0800361 } else if(strStartsWith(s, "+CGEV: ME PDN ACT ")) { // +CGEV: ME PDN ACT <cid>,1
362 //apn_state_set(atoi(s + 18), true);
363 data[0] = (uint8)1;
364 data[1] = (uint8)atoi(s + 18);
365
366 uint8 data_pdp;
367 char* tmp_s = memdup(s + 18,strlen(s + 18));
368 char* free_ptr = tmp_s;
369 char *line = tmp_s;
370 int tmp_int;
371 if (at_tok_start(&line) < 0)
372 {
373 goto PDP_CREG_EXIT;
374 }
375 if (at_tok_nextint(&line, &tmp_int) < 0)
376 {
377 goto PDP_CREG_EXIT;
378 }
379 if (at_tok_nextint(&line, &tmp_int) < 0)
380 {
381 goto PDP_CREG_EXIT;
382 }
383 data_pdp = tmp_int;
384PDP_CREG_EXIT:
385 free(free_ptr);
386 //data_pdp = (uint8)atoi(s + 20); //reason
387 if(data[1] >= 1 && data[1] < 8)
388 {
389 if(data_pdp == 0)
390 {
391 data_pdp = 25;
392 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
393 }
394 else if(data_pdp == 1)
395 {
396 data_pdp = 26;
397 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
398 }
399 else if(data_pdp == 2)
400 {
401 data_pdp = 27;
402 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
403 }
404 else if(data_pdp == 3)
405 {
406 data_pdp = 27;
407 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
408 }
409 else
410 {
411
412 }
b.liufe320632024-01-17 20:38:08 +0800413
wangyouqiang65884152023-10-25 19:54:15 +0800414 data_pdp = data[1] + 200;
415 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
liubin281ac462023-07-19 14:22:54 +0800416 }
417 } else {
418 LOGI("No process : %s", s);
419 }
420
421 urc_msg_distribute(true, INFO_URC_MSG_CGEV, data, sizeof(uint8) * 2);
422 }
423 }
424 // +CREG: 1, "8010", "000060a5", 0, 2, 0
425 // +CREG: 1, "8330", "06447347", 7, 2, 0
426 // +CEREG: 1, "8330", "06447347", 7
427 // $CREG: 1, "8330", "06447347", 7,"0d4", 2, 0
428 // $CREG: 1, "8010", "000060a7", 0,, 2, 0
429 // +CGREG: 1
430 else if(strStartsWith(s, "+CGREG:") // GMS/WCDMA data registed.
431 || strStartsWith(s, "+CEREG:")) // LTE data registed.
432 {
433 char* tmp_s = s + 7;
434 while(*tmp_s && *tmp_s == ' ')
435 tmp_s++;
b.liufe320632024-01-17 20:38:08 +0800436 uint8 data[2];
437 data[0] = (uint8)atoi(tmp_s); // Reg State.
liubin281ac462023-07-19 14:22:54 +0800438
b.liufe320632024-01-17 20:38:08 +0800439 if(strStartsWith(s, "+CGREG:")) {
440 data[1] = 0; // GMS/WCDMA
441 } else {
442 data[1] = 1; // LTE
wangyouqiang38e53362024-01-23 10:53:48 +0800443 if(data[0] == 1)
444 {
445 mbtk_net_led_set(MBTK_NET_LED_NET_CONNECT);
b.liubb590492024-06-13 16:42:08 +0800446
447 mbtk_net_ready();
wangyouqiang38e53362024-01-23 10:53:48 +0800448 }
449 else
450 {
451 mbtk_net_led_set(MBTK_NET_LED_SEARCH_NETWORK);
452 }
b.liufe320632024-01-17 20:38:08 +0800453 }
454
455 urc_msg_distribute(true, INFO_URC_MSG_NET_PS_REG_STATE, data, sizeof(data));
liuyang37623a02024-06-20 15:21:39 +0800456 urc_msg_distribute(true, INFO_URC_MSG_NET_STATE_LOG, NULL, 0);
liubin281ac462023-07-19 14:22:54 +0800457 }
458 // +CREG: 1, "8010", "000060a5", 0, 2, 0
459 // +CREG: 1, "8330", "06447347", 7, 2, 0
460 // +CREG: 0
461 else if(strStartsWith(s, "+CREG:")) // GMS/WCDMA/LTE CS registed.
462 {
463 uint8 data[3];
464 data[0] = (uint8)MBTK_NET_CS_STATE;
465 char* tmp_s = memdup(s,strlen(s));
466 char* free_ptr = tmp_s;
467 char *line = tmp_s;
468 int tmp_int;
469 char *tmp_str;
470 if (at_tok_start(&line) < 0)
471 {
472 goto CREG_EXIT;
473 }
474 if (at_tok_nextint(&line, &tmp_int) < 0)
475 {
476 goto CREG_EXIT;
477 }
478 data[1] = (uint8)tmp_int; // Reg State.
479 if (data[1])
480 {
481 if (at_tok_nextstr(&line, &tmp_str) < 0)
482 {
483 goto CREG_EXIT;
484 }
485 if (at_tok_nextstr(&line, &tmp_str) < 0)
486 {
487 goto CREG_EXIT;
488 }
489 if (at_tok_nextint(&line, &tmp_int) < 0)
490 {
491 goto CREG_EXIT;
492 }
493 data[2] = (uint8)tmp_int; // AcT
494 } else {
495 data[2] = (uint8)0xFF; // AcT
496 }
497 if(data[1] == 5)
498 {
499 uint8 data_pdp;
500 data_pdp = 5; //
501 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
502 }
503 urc_msg_distribute(false, INFO_URC_MSG_NET_CS_REG_STATE, data, sizeof(data));
liuyang37623a02024-06-20 15:21:39 +0800504 urc_msg_distribute(true, INFO_URC_MSG_NET_STATE_LOG, NULL, 0);
liubin281ac462023-07-19 14:22:54 +0800505CREG_EXIT:
506 free(free_ptr);
507 }
508 // +CLCC: 1, 1, 6, 0, 0, "18981911691", 129, "",, 0
509 else if(strStartsWith(s, "+CLCC:"))
510 {
511 mbtk_call_info_t reg;
512 reg.call_wait = MBTK_CLCC;
513 char* tmp_s = memdup(s,strlen(s));
514 char* free_ptr = tmp_s;
515 char *line = tmp_s;
516 int tmp_int;
517 char *tmp_str;
518 int err;
519
520 err = at_tok_start(&line);
521 if (err < 0)
522 {
523 goto CLCC_EXIT;
524 }
525 err = at_tok_nextint(&line, &tmp_int); // dir1
526 if (err < 0)
527 {
528 goto CLCC_EXIT;
529 }
530 reg.dir1 = (uint8)tmp_int;
531 err = at_tok_nextint(&line, &tmp_int);// dir
532 if (err < 0)
533 {
534 goto CLCC_EXIT;
535 }
536 reg.dir = (uint8)tmp_int;
537 err = at_tok_nextint(&line, &tmp_int);// state
538 if (err < 0)
539 {
540 goto CLCC_EXIT;
541 }
542 reg.state = (uint8)tmp_int;
543 err = at_tok_nextint(&line, &tmp_int);// mode
544 if (err < 0)
545 {
546 goto CLCC_EXIT;
547 }
548 reg.mode = (uint8)tmp_int;
549 err = at_tok_nextint(&line, &tmp_int);// mpty
550 if (err < 0)
551 {
552 goto CLCC_EXIT;
553 }
554 reg.mpty = (uint8)tmp_int;
555 err = at_tok_nextstr(&line, &tmp_str); // phone_number
556 if (err < 0)
557 {
558 goto CLCC_EXIT;
559 }
560
561 memset(reg.phone_number,0,sizeof(reg.phone_number));
562 memcpy(reg.phone_number, tmp_str, strlen(tmp_str));
563 err = at_tok_nextint(&line, &tmp_int);// tpye
564 if (err < 0)
565 {
566 goto CLCC_EXIT;
567 }
568 reg.type = (uint8)tmp_int;
569
r.xiaoe1404b32024-05-23 22:43:39 -0700570 if(reg.state == 2 || reg.state == 3 || reg.state == 0)
571 {
572 mbtk_net_led_set(MBTK_NET_LED_CALL_CONNECT);
573 }
574
r.xiao195e2522024-05-31 03:19:02 -0700575 if(reg.state == 4 && reg.dir == 1)
576 {
577 mbtk_net_led_set(MBTK_NET_LED_CALL_CONNECT);
578 }
579
580
liubin281ac462023-07-19 14:22:54 +0800581 urc_msg_distribute(false, INFO_URC_MSG_CALL_STATE, &reg, sizeof(mbtk_call_info_t));
582CLCC_EXIT:
583 free(free_ptr);
584 }
585 // +CPAS: 4
586 else if(strStartsWith(s, "+CPAS:"))
587 {
588 mbtk_call_info_t reg;
589 reg.call_wait = 0;
590 char* tmp_s = memdup(s,strlen(s));
591 char* free_ptr = tmp_s;
592 char *line = tmp_s;
593 int tmp_int;
594 int err;
595
596 memset(&reg,0,sizeof(reg));
597
598 err = at_tok_start(&line);
599 if (err < 0)
600 {
601 goto CPAS_EXIT;
602 }
603 err = at_tok_nextint(&line, &tmp_int);
604 if (err < 0)
605 {
606 goto CPAS_EXIT;
607 }
608 reg.pas = (uint8)tmp_int;
609 reg.call_wait = MBTK_CPAS;
610 urc_msg_distribute(false, INFO_URC_MSG_CALL_STATE, &reg, sizeof(mbtk_call_info_t));
611CPAS_EXIT:
612 free(free_ptr);
613 }
614 // +CALLDISCONNECT: 1
615 else if(strStartsWith(s, "+CALLDISCONNECT:"))
616 {
617 mbtk_call_info_t reg;
618 reg.call_wait = 0;
619 char* tmp_s = memdup(s,strlen(s));
620 char* free_ptr = tmp_s;
621 char *line = tmp_s;
622 int tmp_int;
623 int err;
624
625 memset(&reg,0,sizeof(reg));
626
627 err = at_tok_start(&line);
628 if (err < 0)
629 {
630 goto CALLDISCONNECTED_EXIT;
631 }
632 err = at_tok_nextint(&line, &tmp_int);
633 if (err < 0)
634 {
635 goto CALLDISCONNECTED_EXIT;
636 }
637 reg.disconnected_id = tmp_int;
638 reg.call_wait = MBTK_DISCONNECTED;
r.xiaoe1404b32024-05-23 22:43:39 -0700639
640 if(reg.call_wait == MBTK_DISCONNECTED)
641 {
642 mbtk_net_led_set(MBTK_NET_LED_CALL_DISCONNECT);
643 }
644
liubin281ac462023-07-19 14:22:54 +0800645 urc_msg_distribute(false, INFO_URC_MSG_CALL_STATE, &reg, sizeof(mbtk_call_info_t));
646
647CALLDISCONNECTED_EXIT:
648 free(free_ptr);
649 }
650 // *SIMDETEC:1,SIM
651 else if(strStartsWith(s, "*SIMDETEC:"))
652 {
liuyange22a25e2024-05-23 19:41:52 +0800653 if(strStartsWith(s, "*SIMDETEC:1,NOS"))
654 {
655 net_info.sim_state = MBTK_SIM_ABSENT;
656 }
b.liubb590492024-06-13 16:42:08 +0800657
liubin281ac462023-07-19 14:22:54 +0800658 sim_info_reg.sim = -1;
659 if(strStartsWith(s, "*SIMDETEC:1,NOS"))
660 sim_info_reg.sim = 0;
661 else if(strStartsWith(s, "*SIMDETEC:1,SIM"))
662 sim_info_reg.sim = 1;
663 if(sim_info_reg.sim == 0)
664 {
665 uint8 data_pdp;
666 data_pdp = 11; //
667 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
668 }
669 urc_msg_distribute(false, INFO_URC_MSG_SIM_STATE, &sim_info_reg, sizeof(mbtk_sim_card_info));
670 }
671 // *EUICC:1
672/*0: SIM
6731: USIM
6742: TEST SIM
6753: TEST USIM
6764: UNKNOWN
677Note: *EUICC:
678*/
679 else if(strStartsWith(s, "*EUICC:"))
680 {
681 sim_info_reg.sim_card_type = -1;
682 if(strStartsWith(s, "*EUICC: 0"))
683 sim_info_reg.sim_card_type = 1;
684 else if(strStartsWith(s, "*EUICC: 1"))
685 sim_info_reg.sim_card_type = 2;
686 else if(strStartsWith(s, "*EUICC: 2"))
687 sim_info_reg.sim_card_type = 1;
688 else if(strStartsWith(s, "*EUICC: 3"))
689 sim_info_reg.sim_card_type = 2;
690 else if(strStartsWith(s, "*EUICC: 4"))
691 sim_info_reg.sim_card_type = 0;
692 urc_msg_distribute(false, INFO_URC_MSG_SIM_STATE, &sim_info_reg, sizeof(mbtk_sim_card_info));
693 }
694 // +CPIN: SIM PIN
695 else if(strStartsWith(s, "+CPIN:"))
696 {
697 sim_info_reg.sim = -1;
698 if(strStartsWith(s, "+CPIN: READY"))
liuyange22a25e2024-05-23 19:41:52 +0800699 {
liubin281ac462023-07-19 14:22:54 +0800700 sim_info_reg.sim = 1;
liuyange22a25e2024-05-23 19:41:52 +0800701 net_info.sim_state = MBTK_SIM_READY;
702 }
liubin281ac462023-07-19 14:22:54 +0800703 else if(strStartsWith(s, "+CPIN: SIM PIN"))
liuyange22a25e2024-05-23 19:41:52 +0800704 {
liubin281ac462023-07-19 14:22:54 +0800705 sim_info_reg.sim = 2;
liuyange22a25e2024-05-23 19:41:52 +0800706 net_info.sim_state = MBTK_SIM_PIN;
707 }
liubin281ac462023-07-19 14:22:54 +0800708 else if(strStartsWith(s, "+CPIN: SIM PUK"))
liuyange22a25e2024-05-23 19:41:52 +0800709 {
liubin281ac462023-07-19 14:22:54 +0800710 sim_info_reg.sim = 3;
liuyange22a25e2024-05-23 19:41:52 +0800711 net_info.sim_state = MBTK_SIM_PUK;
712 }
liubin281ac462023-07-19 14:22:54 +0800713 else if(strStartsWith(s, "+CPIN: PH-SIMLOCK PIN"))
liuyange22a25e2024-05-23 19:41:52 +0800714 {
liubin281ac462023-07-19 14:22:54 +0800715 sim_info_reg.sim = 4;
liuyange22a25e2024-05-23 19:41:52 +0800716 net_info.sim_state = MBTK_SIM_ABSENT;
717 }
liubin281ac462023-07-19 14:22:54 +0800718 else if(strStartsWith(s, "+CPIN: PH-SIMLOCK PUK"))
liuyange22a25e2024-05-23 19:41:52 +0800719 {
liubin281ac462023-07-19 14:22:54 +0800720 sim_info_reg.sim = 5;
liuyange22a25e2024-05-23 19:41:52 +0800721 net_info.sim_state = MBTK_SIM_ABSENT;
722 }
liubin281ac462023-07-19 14:22:54 +0800723 else if(strStartsWith(s, "+CPIN: PH-FSIM PIN"))
liuyange22a25e2024-05-23 19:41:52 +0800724 {
liubin281ac462023-07-19 14:22:54 +0800725 sim_info_reg.sim = 6;
liuyange22a25e2024-05-23 19:41:52 +0800726 net_info.sim_state = MBTK_SIM_ABSENT;
727 }
liubin281ac462023-07-19 14:22:54 +0800728 else if(strStartsWith(s, "+CPIN: PH-FSIM PUK"))
liuyange22a25e2024-05-23 19:41:52 +0800729 {
liubin281ac462023-07-19 14:22:54 +0800730 sim_info_reg.sim = 7;
liuyange22a25e2024-05-23 19:41:52 +0800731 net_info.sim_state = MBTK_SIM_ABSENT;
732 }
liubin281ac462023-07-19 14:22:54 +0800733 else if(strStartsWith(s, "+CPIN: SIM PIN2"))
liuyange22a25e2024-05-23 19:41:52 +0800734 {
liubin281ac462023-07-19 14:22:54 +0800735 sim_info_reg.sim = 8;
liuyange22a25e2024-05-23 19:41:52 +0800736 net_info.sim_state = MBTK_SIM_ABSENT;
737 }
liubin281ac462023-07-19 14:22:54 +0800738 else if(strStartsWith(s, "+CPIN: SIM PUK2"))
liuyange22a25e2024-05-23 19:41:52 +0800739 {
liubin281ac462023-07-19 14:22:54 +0800740 sim_info_reg.sim = 9;
liuyange22a25e2024-05-23 19:41:52 +0800741 net_info.sim_state = MBTK_SIM_ABSENT;
742 }
liubin281ac462023-07-19 14:22:54 +0800743 else if(strStartsWith(s, "+CPIN: PH-NET PIN"))
liuyange22a25e2024-05-23 19:41:52 +0800744 {
liubin281ac462023-07-19 14:22:54 +0800745 sim_info_reg.sim = 10;
liuyange22a25e2024-05-23 19:41:52 +0800746 net_info.sim_state = MBTK_SIM_NETWORK_PERSONALIZATION;
747 }
liubin281ac462023-07-19 14:22:54 +0800748 else if(strStartsWith(s, "+CPIN: PH-NET PUK"))
liuyange22a25e2024-05-23 19:41:52 +0800749 {
liubin281ac462023-07-19 14:22:54 +0800750 sim_info_reg.sim = 11;
liuyange22a25e2024-05-23 19:41:52 +0800751 net_info.sim_state = MBTK_SIM_ABSENT;
752 }
liubin281ac462023-07-19 14:22:54 +0800753 else if(strStartsWith(s, "+CPIN: PH-NETSUB PINMT"))
liuyange22a25e2024-05-23 19:41:52 +0800754 {
liubin281ac462023-07-19 14:22:54 +0800755 sim_info_reg.sim = 12;
liuyange22a25e2024-05-23 19:41:52 +0800756 net_info.sim_state = MBTK_SIM_ABSENT;
757 }
liubin281ac462023-07-19 14:22:54 +0800758 else if(strStartsWith(s, "+CPIN: PH-NETSUB PUK"))
liuyange22a25e2024-05-23 19:41:52 +0800759 {
liubin281ac462023-07-19 14:22:54 +0800760 sim_info_reg.sim = 13;
liuyange22a25e2024-05-23 19:41:52 +0800761 net_info.sim_state = MBTK_SIM_ABSENT;
762 }
liubin281ac462023-07-19 14:22:54 +0800763 else if(strStartsWith(s, "+CPIN: PH-SP PIN"))
liuyange22a25e2024-05-23 19:41:52 +0800764 {
liubin281ac462023-07-19 14:22:54 +0800765 sim_info_reg.sim = 14;
liuyange22a25e2024-05-23 19:41:52 +0800766 net_info.sim_state = MBTK_SIM_ABSENT;
767 }
liubin281ac462023-07-19 14:22:54 +0800768 else if(strStartsWith(s, "+CPIN: PH-SP PUK"))
liuyange22a25e2024-05-23 19:41:52 +0800769 {
liubin281ac462023-07-19 14:22:54 +0800770 sim_info_reg.sim = 15;
liuyange22a25e2024-05-23 19:41:52 +0800771 net_info.sim_state = MBTK_SIM_ABSENT;
772 }
liubin281ac462023-07-19 14:22:54 +0800773 else if(strStartsWith(s, "+CPIN: PH-CORP PIN"))
liuyange22a25e2024-05-23 19:41:52 +0800774 {
liubin281ac462023-07-19 14:22:54 +0800775 sim_info_reg.sim = 16;
liuyange22a25e2024-05-23 19:41:52 +0800776 net_info.sim_state = MBTK_SIM_ABSENT;
777 }
liubin281ac462023-07-19 14:22:54 +0800778 else if(strStartsWith(s, "+CPIN: PH-CORP PUK"))
liuyange22a25e2024-05-23 19:41:52 +0800779 {
liubin281ac462023-07-19 14:22:54 +0800780 sim_info_reg.sim = 17;
liuyange22a25e2024-05-23 19:41:52 +0800781 net_info.sim_state = MBTK_SIM_ABSENT;
782 }
liubin281ac462023-07-19 14:22:54 +0800783 else if(strStartsWith(s, "+CPIN: SIM REMOVED"))
liuyange22a25e2024-05-23 19:41:52 +0800784 {
785 sim_info_reg.sim = 18;
786 net_info.sim_state = MBTK_SIM_ABSENT;
787 }
liubin281ac462023-07-19 14:22:54 +0800788 else
789 sim_info_reg.sim = 20;
790
791 if(sim_info_reg.sim == 18)
792 {
793 uint8 data_pdp;
794 data_pdp = 11; //
795 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
796 }
797
798 urc_msg_distribute(false, INFO_URC_MSG_SIM_STATE, &sim_info_reg, sizeof(mbtk_sim_card_info));
799 }
800 // +CMT: ,23
801 // 0891683108200855F6240D91688189911196F10000221130717445230331D90C
802 else if(strStartsWith(s, "+CMT:") || sms_cmt)
803 {
804 if(!sms_cmt){
805 sms_cmt = true;
806 }else{
807 sms_cmt = false;
808 }
809 printf("+CMT() sms_cmt:%d, s:%s, len:%d\n",sms_cmt, s, strlen(s));
810 urc_msg_distribute(false, INFO_URC_MSG_SMS_STATE, s, strlen(s));
811 }
812#if 0
813 // LTE data registed.
814 // +CEREG: 1, "8330", "06447347", 7
815 else if(strStartsWith(s, "+CEREG:"))
816 {
817 char* tmp_s = memdup(s,strlen(s));
818 char* free_ptr = tmp_s;
819 char *line = tmp_s;
820 int tmp_int;
821 char *tmp_str;
822 if (at_tok_start(&line) < 0)
823 {
824 goto CREG_EXIT;
825 }
826 if (at_tok_nextint(&line, &tmp_int) < 0)
827 {
828 goto CREG_EXIT;
829 }
830 uint8 data = (uint8)tmp_int; // Reg State.
831
832 urc_msg_distribute(INFO_URC_MSG_NET_REG_STATE, &data, sizeof(uint8));
833CREG_EXIT:
834 free(free_ptr);
835 }
836#endif
837 /*
838 // <mcc>, <length of mnc>, <mnc>, <tac>, <PCI>, <dlEuarfcn>, < ulEuarfcn >, <band>, <dlBandwidth>,
839 // <rsrp>,<rsrq>, <sinr>,
840 // errcModeState,emmState,serviceState,IsSingleEmmRejectCause,EMMRejectCause,mmeGroupId,mmeCode,mTmsi,
841 // cellId,subFrameAssignType,specialSubframePatterns,transMode
842 // mainRsrp,diversityRsrp,mainRsrq,diversityRsrq,rssi,cqi,pathLoss,tb0DlTpt,tb1DlTpt,tb0DlPeakTpt,tb1DlPeakTpt,tb0UlPeakTpt,
843 // tb1UlPeakTpt,dlThroughPut,dlPeakThroughPut,averDlPRB,averCQITb0,averCQITb1,rankIndex,grantTotal,ulThroughPut,ulPeakThroughPut,currPuschTxPower,averUlPRB,
844 // dlBer, ulBer,
845 // diversitySinr, diversityRssi
846 +EEMLTESVC: 1120, 2, 0, 33584, 430, 40936, 40936, 41, 20,
847 0, 0, 0,
848 1, 10, 0, 1, 0, 1059, 78, 3959566565,
849 105149248, 2, 7, 7,
850 0, 0, 0, 0, 0, 0, 0, 1190919, 0, 0, 0, 16779777,
851 0, 5112867, 3959566565, 2, 0, 0, 0, 0, 0, 0, 0, 0,
852 0, 0,
853 7, 44
854 */
855 else if(strStartsWith(s, "+EEMLTESVC:")) // LTE Server Cell
856 {
857 // tac, PCI, dlEuarfcn, ulEuarfcn, band
858 if(cell_info.running) {
859 int tmp_int;
860 int i = 0;
861 char* tmp_s = memdup(s,strlen(s));
862 char* free_ptr = tmp_s;
863 char *line = tmp_s;
864 if (at_tok_start(&line) < 0)
865 {
866 goto EEMLTESVC_EXIT;
867 }
868
869 if (at_tok_nextint(&line, &tmp_int) < 0)
870 {
871 goto EEMLTESVC_EXIT;
872 }
873 cell_info.cell[cell_info.cell_num].value6 = (uint32)tmp_int; //mcc
874 if (at_tok_nextint(&line, &tmp_int) < 0)
875 {
876 goto EEMLTESVC_EXIT;
877 }
878 if (at_tok_nextint(&line, &tmp_int) < 0)
879 {
880 goto EEMLTESVC_EXIT;
881 }
882 cell_info.cell[cell_info.cell_num].value7 = (uint32)tmp_int; //mnc
883 /*
884 // Jump 2 integer.
885 i = 0;
886 while(i < 2) {
887 if (at_tok_nextint(&line, &tmp_int) < 0)
888 {
889 goto EEMLTESVC_EXIT;
890 }
891 i++;
892 }
893 */
894 if (at_tok_nextint(&line, &tmp_int) < 0)
895 {
896 goto EEMLTESVC_EXIT;
897 }
898 cell_info.cell[cell_info.cell_num].value1 = (uint32)tmp_int; //tac
899 if (at_tok_nextint(&line, &tmp_int) < 0)
900 {
901 goto EEMLTESVC_EXIT;
902 }
903 cell_info.cell[cell_info.cell_num].value2 = (uint32)tmp_int; //pci
904 if (at_tok_nextint(&line, &tmp_int) < 0)
905 {
906 goto EEMLTESVC_EXIT;
907 }
908 cell_info.cell[cell_info.cell_num].value3 = (uint32)tmp_int; //dl arfcn
909 if (at_tok_nextint(&line, &tmp_int) < 0)
910 {
911 goto EEMLTESVC_EXIT;
912 }
913 cell_info.cell[cell_info.cell_num].value4 = (uint32)tmp_int; //ul arfcn
914 if (at_tok_nextint(&line, &tmp_int) < 0)
915 {
916 goto EEMLTESVC_EXIT;
917 }
918 cell_info.cell[cell_info.cell_num].value5 = (uint32)tmp_int; //band
919 if (at_tok_nextint(&line, &tmp_int) < 0)
920 {
921 goto EEMLTESVC_EXIT;
922 }
923 if (at_tok_nextint(&line, &tmp_int) < 0)
924 {
925 goto EEMLTESVC_EXIT;
926 }
927 cell_info.cell[cell_info.cell_num].value8 = (uint32)tmp_int; //cid
928 if (at_tok_nextint(&line, &tmp_int) < 0)
929 {
930 goto EEMLTESVC_EXIT;
931 }
932 cell_info.cell[cell_info.cell_num].value9 = (uint32)tmp_int; //rsrp
933
934 for(i =0; i < 10; i++)
935 {
936 if (at_tok_nextint(&line, &tmp_int) < 0)
937 {
938 goto EEMLTESVC_EXIT;
939 }
940 }
941 cell_info.cell[cell_info.cell_num].value10 = (uint32)tmp_int; //cell identiy
942
943 cell_info.cell_num++;
944
945EEMLTESVC_EXIT:
946 free(free_ptr);
947 }
948 }
949 /*
950 // index,phyCellId,euArfcn,rsrp,rsrq
951 +EEMLTEINTER: 0, 65535, 38950, 0, 0
952 */
953 else if(strStartsWith(s, "+EEMLTEINTER:") || strStartsWith(s, "+EEMLTEINTRA:")) // LTE ÒìÆµ/Í¬ÆµÐ¡Çø
954 {
955 // phyCellId,euArfcn,rsrp,rsrq
956 if(cell_info.running) {
957 int tmp_int;
958 char* tmp_s = memdup(s,strlen(s));
959 char* free_ptr = tmp_s;
960 char *line = tmp_s;
961 if (at_tok_start(&line) < 0)
962 {
963 goto EEMLTEINTER_EXIT;
964 }
965 if (at_tok_nextint(&line, &tmp_int) < 0)
966 {
967 goto EEMLTEINTER_EXIT;
968 }
969 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int <= 0 || tmp_int > 503)
970 {
971 goto EEMLTEINTER_EXIT;
972 }
973 cell_info.cell[cell_info.cell_num].value1 = (uint32)tmp_int;
974 if (at_tok_nextint(&line, &tmp_int) < 0)
975 {
976 goto EEMLTEINTER_EXIT;
977 }
978 cell_info.cell[cell_info.cell_num].value2 = (uint32)tmp_int;
979 if (at_tok_nextint(&line, &tmp_int) < 0)
980 {
981 goto EEMLTEINTER_EXIT;
982 }
983 cell_info.cell[cell_info.cell_num].value3 = (uint32)tmp_int;
984 LOG("cell line : %s", line);
985 if (at_tok_nextint(&line, &tmp_int) < 0)
986 {
987 goto EEMLTEINTER_EXIT;
988 }
989 cell_info.cell[cell_info.cell_num].value4 = (uint32)tmp_int;
990 if (at_tok_nextint(&line, &tmp_int) < 0)
991 {
992 LOG("cell tmp_int : %d", tmp_int);
993 goto EEMLTEINTER_EXIT;
994 }
995 cell_info.cell[cell_info.cell_num].value5 = (uint32)tmp_int;
996 LOG("cell value5 : %d", cell_info.cell[cell_info.cell_num].value5);
997 cell_info.cell_num++;
998EEMLTEINTER_EXIT:
999 free(free_ptr);
1000 }
1001 }
1002 // Do nothing
1003 else if(strStartsWith(s, "+EEMLTEINTERRAT:")) // LTE RATÐ¡ÇøÐÅÏ¢
1004 {
1005 if(cell_info.running) {
1006
1007 }
1008 }
1009 // WCDMA
1010 /*
1011 // Mode, sCMeasPresent, sCParamPresent, ueOpStatusPresent,
1012
1013 // if sCMeasPresent == 1
1014 // cpichRSCP, utraRssi, cpichEcN0, sQual, sRxLev, txPower,
1015 // endif
1016
1017 // if sCParamPresent == 1
1018 // rac, nom, mcc, mnc_len, mnc, lac, ci,
1019 // uraId, psc, arfcn, t3212, t3312, hcsUsed, attDetAllowed,
1020 // csDrxCycleLen, psDrxCycleLen, utranDrxCycleLen, HSDPASupport, HSUPASupport,
1021 // endif
1022
1023 // if ueOpStatusPresent == 1
1024 // rrcState, numLinks, srncId, sRnti,
1025 // algPresent, cipherAlg, cipherOn, algPresent, cipherAlg, cipherOn,
1026 // HSDPAActive, HSUPAActive, MccLastRegisteredNetwork, MncLastRegisteredNetwork, TMSI, PTMSI, IsSingleMmRejectCause, IsSingleGmmRejectCause,
1027 // MMRejectCause, GMMRejectCause, mmState, gmmState, gprsReadyState, readyTimerValueInSecs, NumActivePDPContext, ULThroughput, DLThroughput,
1028 // serviceStatus, pmmState, LAU_status, LAU_count, RAU_status, RAU_count
1029 // endif
1030 //
1031 +EEMUMTSSVC: 3, 1, 1, 1,
1032 -80, 27, -6, -18, -115, -32768,
1033 1, 1, 1120, 2, 1, 61697, 168432821,
1034 15, 24, 10763, 0, 0, 0, 0,
1035 128, 128, 65535, 0, 0,
1036 2, 255, 65535, 4294967295,
1037 0, 0, 0, 0, 0, 0,
1038 0, 0, 0, 0, 0, 0, 1, 1,
1039 28672, 28672, 0, 0, 0, 0, 0, 0, 0,
1040 0, 0, 0, 0, 0, 0
1041 */
1042 else if(strStartsWith(s, "+EEMUMTSSVC:")) // WCDMA Server Cell
1043 {
1044 // lac, ci, arfcn
1045 if(cell_info.running) {
1046 int tmp_int;
1047 int i = 0;
1048 char* tmp_s = memdup(s,strlen(s));
1049 char* free_ptr = tmp_s;
1050 char *line = tmp_s;
1051 if (at_tok_start(&line) < 0)
1052 {
1053 goto EEMUMTSSVC_EXIT;
1054 }
1055 // Jump 12 integer.
1056 i = 0;
1057 while(i < 12) {
1058 if (at_tok_nextint(&line, &tmp_int) < 0)
1059 {
1060 goto EEMUMTSSVC_EXIT;
1061 }
1062 i++;
1063 }
1064 // mcc
1065 if (at_tok_nextint(&line, &tmp_int) < 0)
1066 {
1067 goto EEMUMTSSVC_EXIT;
1068 }
1069 cell_info.cell[cell_info.cell_num].value4= (uint32)tmp_int;
1070 // mnc
1071 if (at_tok_nextint(&line, &tmp_int) < 0)
1072 {
1073 goto EEMUMTSSVC_EXIT;
1074 }
1075 cell_info.cell[cell_info.cell_num].value5= (uint32)tmp_int;
1076 // lac
1077 if (at_tok_nextint(&line, &tmp_int) < 0)
1078 {
1079 goto EEMUMTSSVC_EXIT;
1080 }
1081 cell_info.cell[cell_info.cell_num].value1 = (uint32)tmp_int;
1082 // ci
1083 if (at_tok_nextint(&line, &tmp_int) < 0)
1084 {
1085 goto EEMUMTSSVC_EXIT;
1086 }
1087 cell_info.cell[cell_info.cell_num].value2 = (uint32)tmp_int;
1088
1089 if (at_tok_nextint(&line, &tmp_int) < 0)
1090 {
1091 goto EEMUMTSSVC_EXIT;
1092 }
1093 // cpi
1094 if (at_tok_nextint(&line, &tmp_int) < 0)
1095 {
1096 goto EEMUMTSSVC_EXIT;
1097 }
1098 cell_info.cell[cell_info.cell_num].value6= (uint32)tmp_int;
1099 /*
1100 // Jump 2 integer.
1101 i = 0;
1102 while(i < 2) {
1103 if (at_tok_nextint(&line, &tmp_int) < 0)
1104 {
1105 goto EEMUMTSSVC_EXIT;
1106 }
1107 i++;
1108 }
1109 */
1110 // arfcn
1111 if (at_tok_nextint(&line, &tmp_int) < 0)
1112 {
1113 goto EEMUMTSSVC_EXIT;
1114 }
1115 cell_info.cell[cell_info.cell_num].value3 = (uint32)tmp_int;
1116
1117 cell_info.cell_num++;
1118EEMUMTSSVC_EXIT:
1119 free(free_ptr);
1120 }
1121 }
1122 /*
1123 // index, cpichRSCP, utraRssi, cpichEcN0, sQual, sRxLev ,mcc, mnc, lac, ci, arfcn, psc
1124 +EEMUMTSINTRA: 0, -32768, -1, -32768, -18, -115, 0, 0, 65534, 1, 10763, 32
1125 */
1126 else if(strStartsWith(s, "+EEMUMTSINTRA:")) // WCDMAÁÙ½üÐ¡Çø
1127 {
1128 // lac, ci, arfcn
1129 if(cell_info.running) {
1130 int tmp_int;
1131 int i = 0;
1132 char* tmp_s = memdup(s,strlen(s));
1133 char* free_ptr = tmp_s;
1134 char *line = tmp_s;
1135 if (at_tok_start(&line) < 0)
1136 {
1137 goto EEMUMTSINTRA_EXIT;
1138 }
1139 // Jump 8 integer.
1140 i = 0;
1141 while(i < 8) {
1142 if (at_tok_nextint(&line, &tmp_int) < 0)
1143 {
1144 goto EEMUMTSINTRA_EXIT;
1145 }
1146 i++;
1147 }
1148
1149 // lac
1150 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int <= 0 || tmp_int >= 65536)
1151 {
1152 goto EEMUMTSINTRA_EXIT;
1153 }
1154 cell_info.cell[cell_info.cell_num].value1 = (uint32)tmp_int;
1155
1156 // ci
1157 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int <= 0 || tmp_int >= 65536)
1158 {
1159 goto EEMUMTSINTRA_EXIT;
1160 }
1161 cell_info.cell[cell_info.cell_num].value2 = (uint32)tmp_int;
1162
1163 // arfcn
1164 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int <= 0 || tmp_int >= 65536)
1165 {
1166 goto EEMUMTSINTRA_EXIT;
1167 }
1168 cell_info.cell[cell_info.cell_num].value3 = (uint32)tmp_int;
1169
1170 cell_info.cell_num++;
1171EEMUMTSINTRA_EXIT:
1172 free(free_ptr);
1173 }
1174 }
1175 /*
1176 // index,gsmRssi,rxLev,C1,C2,mcc,mnc,lac,ci,arfcn,bsic
1177 +EEMUMTSINTERRAT: 0, -32768, -107, -1, -1, 0, 0, 65534, 0, 117, 36
1178 */
1179 else if(strStartsWith(s, "+EEMUMTSINTERRAT:")) // WCDMA RATÐ¡ÇøÐÅÏ¢
1180 {
1181 // lac, ci, arfcn
1182 if(cell_info.running) {
1183 int tmp_int;
1184 int i = 0;
1185 char* tmp_s = memdup(s,strlen(s));
1186 char* free_ptr = tmp_s;
1187 char *line = tmp_s;
1188 if (at_tok_start(&line) < 0)
1189 {
1190 goto EEMUMTSINTERRAT_EXIT;
1191 }
1192 // Jump 7 integer.
1193 i = 0;
1194 while(i < 7) {
1195 if (at_tok_nextint(&line, &tmp_int) < 0)
1196 {
1197 goto EEMUMTSINTERRAT_EXIT;
1198 }
1199 i++;
1200 }
1201
1202 // lac
1203 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int <= 0 || tmp_int >= 65536)
1204 {
1205 goto EEMUMTSINTERRAT_EXIT;
1206 }
1207 cell_info.cell[cell_info.cell_num].value1 = (uint32)tmp_int;
1208
1209 // ci
1210 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int <= 0 || tmp_int >= 65536)
1211 {
1212 goto EEMUMTSINTERRAT_EXIT;
1213 }
1214 cell_info.cell[cell_info.cell_num].value2 = (uint32)tmp_int;
1215
1216 // arfcn
1217 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int <= 0 || tmp_int >= 65536)
1218 {
1219 goto EEMUMTSINTERRAT_EXIT;
1220 }
1221 cell_info.cell[cell_info.cell_num].value3 = (uint32)tmp_int;
1222
1223 cell_info.cell_num++;
1224EEMUMTSINTERRAT_EXIT:
1225 free(free_ptr);
1226 }
1227 }
1228 // GSM
1229 // +EEMGINFOBASIC: 2
1230 // Do nothing.
1231 else if(strStartsWith(s, "+EEMGINFOBASIC:")) // Basic information in GSM
1232 // 0: ME in Idle mode 1: ME in Dedicated mode 2: ME in PS PTM mode
1233 {
1234 if(cell_info.running) {
1235
1236 }
1237 }
1238 /*
1239 // mcc, mnc_len, mnc, lac, ci, nom, nco,
1240 // bsic, C1, C2, TA, TxPwr,
1241 // RxSig, RxSigFull, RxSigSub, RxQualFull, RxQualSub,
1242 // ARFCB_tch, hopping_chnl, chnl_type, TS, PacketIdle, rac, arfcn,
1243 // bs_pa_mfrms, C31, C32, t3212, t3312, pbcch_support, EDGE_support,
1244 // ncc_permitted, rl_timeout, ho_count, ho_succ, chnl_access_count, chnl_access_succ_count,
1245 // gsmBand,channelMode
1246 +EEMGINFOSVC: 1120, 2, 0, 32784, 24741, 2, 0,
1247 63, 36, 146, 1, 7,
1248 46, 42, 42, 7, 0,
1249 53, 0, 8, 0, 1, 6, 53,
1250 2, 0, 146, 42, 54, 0, 1,
1251 1, 32, 0, 0, 0, 0,
1252 0, 0
1253 */
1254 else if(strStartsWith(s, "+EEMGINFOSVC:")) // GSM Server Cell
1255 {
1256 // lac, ci, arfcn, bsic
1257 LOG("+EEMGINFOSVC: 1= %d\n.",cell_info.running);
1258 if(cell_info.running) {
1259 int tmp_int;
1260 int i = 0;
1261 char* tmp_s = memdup(s,strlen(s));
1262 char* free_ptr = tmp_s;
1263 char *line = tmp_s;
1264 if (at_tok_start(&line) < 0)
1265 {
1266 goto EEMGINFOSVC_EXIT;
1267 }
1268
1269 // mcc
1270 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int <= 0 || tmp_int >= 65536)
1271 {
1272 goto EEMGINFOSVC_EXIT;
1273 }
1274 cell_info.cell[cell_info.cell_num].value5 = (uint32)tmp_int;
1275
1276 //mnc_len
1277 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int <= 0 || tmp_int >= 65536)
1278 {
1279 goto EEMGINFOSVC_EXIT;
1280 }
1281 // mnc
1282 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int <= 0 || tmp_int >= 65536)
1283 {
1284 goto EEMGINFOSVC_EXIT;
1285 }
1286 cell_info.cell[cell_info.cell_num].value6 = (uint32)tmp_int;
1287
1288 /*
1289 // Jump 3 integer.
1290 i = 0;
1291 while(i < 3) {
1292 if (at_tok_nextint(&line, &tmp_int) < 0)
1293 {
1294 goto EEMGINFOSVC_EXIT;
1295 }
1296 i++;
1297 }
1298 */
1299 // lac
1300 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1301 {
1302 goto EEMGINFOSVC_EXIT;
1303 }
1304 cell_info.cell[cell_info.cell_num].value1 = (uint32)tmp_int;
1305
1306 // ci
1307 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1308 {
1309 goto EEMGINFOSVC_EXIT;
1310 }
1311 cell_info.cell[cell_info.cell_num].value2 = (uint32)tmp_int;
1312
1313 // Jump 2 integer.
1314 i = 0;
1315 while(i < 2) {
1316 if (at_tok_nextint(&line, &tmp_int) < 0)
1317 {
1318 goto EEMGINFOSVC_EXIT;
1319 }
1320 i++;
1321 }
1322
1323 // bsic
1324 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1325 {
1326 goto EEMGINFOSVC_EXIT;
1327 }
1328 cell_info.cell[cell_info.cell_num].value4 = (uint32)tmp_int;
1329
1330 // Jump 15 integer.
1331 i = 0;
1332 while(i < 15) {
1333 if (at_tok_nextint(&line, &tmp_int) < 0)
1334 {
1335 goto EEMGINFOSVC_EXIT;
1336 }
1337 i++;
1338 }
1339
1340 // arfcn
1341 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1342 {
1343 goto EEMGINFOSVC_EXIT;
1344 }
1345 cell_info.cell[cell_info.cell_num].value3 = (uint32)tmp_int;
1346
1347 cell_info.cell_num++;
1348EEMGINFOSVC_EXIT:
1349 free(free_ptr);
1350 }
1351 }
1352 /*
1353 // PS_attached, attach_type, service_type, tx_power, c_value,
1354 // ul_ts, dl_ts, ul_cs, dl_cs, ul_modulation, dl_modulation,
1355 // gmsk_cv_bep, 8psk_cv_bep, gmsk_mean_bep, 8psk_mean_bep, EDGE_bep_period, single_gmm_rej_cause
1356 // pdp_active_num, mac_mode, network_control, network_mode, EDGE_slq_measurement_mode, edge_status
1357 +EEMGINFOPS: 1, 255, 0, 0, 0,
1358 0, 0, 268435501, 1, 0, 0,
1359 4, 0, 96, 0, 0, 0,
1360 0, 0, 0, 65535, 0, 13350
1361 */
1362 // Do nothing.
1363 else if(strStartsWith(s, "+EEMGINFOPS:")) // PSÐÅÏ¢
1364 {
1365 if(cell_info.running) {
1366
1367 }
1368 }
1369 else if(strStartsWith(s, "+EEMGINFONC:")) // cell
1370 {
1371 if(cell_info.running) {
1372 int tmp_int;
1373 int i = 0;
1374 char* tmp_s = memdup(s,strlen(s));
1375 char* free_ptr = tmp_s;
1376 char *line = tmp_s;
1377 if (at_tok_start(&line) < 0)
1378 {
1379 goto EEMGINFOPS_EXIT;
1380 }
1381
1382 // nc_num
1383 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1384 {
1385 LOG("cell_info.running 1= %d\n.",cell_info.running);
1386 goto EEMGINFOPS_EXIT;
1387 }
1388 // mcc
1389 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1390 {
1391 LOG("cell_info.running 2= %d\n.",cell_info.running);
1392 goto EEMGINFOPS_EXIT;
1393 }
1394 cell_info.cell[cell_info.cell_num].value5 = (uint32)tmp_int;
1395
1396 // mnc
1397 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1398 {
1399 LOG("cell_info.running 3= %d\n.",cell_info.running);
1400 goto EEMGINFOPS_EXIT;
1401 }
1402 cell_info.cell[cell_info.cell_num].value6 = (uint32)tmp_int;
1403
1404 // lac
1405 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1406 {
1407 LOG("cell_info.running 4= %d\n.",cell_info.running);
1408 goto EEMGINFOPS_EXIT;
1409 }
1410 cell_info.cell[cell_info.cell_num].value1 = (uint32)tmp_int;
1411
1412 // rac
1413 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1414 {
1415 LOG("cell_info.running 5= %d\n.",cell_info.running);
1416 goto EEMGINFOPS_EXIT;
1417 }
1418
1419 // ci
1420 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1421 {
1422 LOG("cell_info.running 6= %d\n.",cell_info.running);
1423 goto EEMGINFOPS_EXIT;
1424 }
1425 cell_info.cell[cell_info.cell_num].value2 = (uint32)tmp_int;
1426
1427 // rx_lv
1428 if (at_tok_nextint(&line, &tmp_int) < 0)
1429 {
1430 LOG("cell_info.running 7= %d\n.",cell_info.running);
1431 goto EEMGINFOPS_EXIT;
1432 }
1433
1434 // bsic
1435 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1436 {
1437 LOG("cell_info.running 8= %d\n.",cell_info.running);
1438 goto EEMGINFOPS_EXIT;
1439 }
1440 cell_info.cell[cell_info.cell_num].value4 = (uint32)tmp_int;
1441
1442 // Jump 2 integer.
1443 i = 0;
1444 while(i < 2) {
1445 if (at_tok_nextint(&line, &tmp_int) < 0)
1446 {
1447 LOG("cell_info.running 9= %d\n.",cell_info.running);
1448 goto EEMGINFOPS_EXIT;
1449 }
1450 i++;
1451 }
1452
1453 // arfcn
1454 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1455 {
1456 LOG("cell_info.running 10 = %d\n.",cell_info.running);
1457 goto EEMGINFOPS_EXIT;
1458 }
1459 cell_info.cell[cell_info.cell_num].value3 = (uint32)tmp_int;
1460
1461 cell_info.cell_num++;
1462EEMGINFOPS_EXIT:
1463 free(free_ptr);
1464 }
1465 }
1466 else if(strStartsWith(s, "+ZGIPDNS:")) // +ZGIPDNS: 1,"IPV4V6","10.156.239.245","10.156.239.246","223.87.253.100","223.87.253.253","fe80:0000:0000:0000:0001:0001:9b8c:7c0c","fe80::1:1:9b8c:7c0d","2409:8062:2000:2::1","2409:8062:2000:2::2"
1467 {
1468
1469 }
1470 else
1471 {
1472 LOGV("Unknown URC : %s", s);
1473 }
1474}
1475
1476static int openSocket(const char* sockname)
1477{
1478 int sock = socket(AF_UNIX, SOCK_STREAM, 0);
1479 if (sock < 0)
1480 {
1481 LOGE("Error create socket: %s\n", strerror(errno));
1482 return -1;
1483 }
1484 struct sockaddr_un addr;
1485 memset(&addr, 0, sizeof(addr));
1486 addr.sun_family = AF_UNIX;
1487 strncpy(addr.sun_path, sockname, sizeof(addr.sun_path));
1488 while (TEMP_FAILURE_RETRY(connect(sock,(const struct sockaddr*)&addr, sizeof(addr))) != 0)
1489 {
1490 LOGE("Error connect to socket %s: %s, try again", sockname, strerror(errno));
1491 sleep(1);
1492 }
1493
1494#if 0
1495 int sk_flags = fcntl(sock, F_GETFL, 0);
1496 fcntl(sock, F_SETFL, sk_flags | O_NONBLOCK);
1497#endif
1498
1499 return sock;
1500}
1501
1502static void ril_get_cgpaddr_ip_process()
1503{
1504 int err, skip;
1505 ATResponse *p_response = NULL;
1506 char *line;
1507 char *ipv4 = NULL, *ipv6 = NULL;
1508 err = at_send_command_singleline("AT+CGPADDR", "+CGPADDR:", &p_response);
1509 if ((err < 0) || (p_response == NULL) || (p_response->success == 0))
1510 {
1511 LOGE("+CGPADDR exec error.");
1512 goto error;
1513 }
1514
1515 // +CGPADDR: 1, "10.51.59.229", "254.128.0.0.0.0.0.0.0.1.0.0.111.176.63.99"
1516 // +CGPADDR: 1, "10.124.139.131"
1517 line = p_response->p_intermediates->line;
1518 err = at_tok_start(&line);
1519 if (err < 0)
1520 {
1521 goto error;
1522 }
1523
1524 err = at_tok_nextint(&line, &skip);
1525 if (err < 0)
1526 {
1527 goto error;
1528 }
1529
1530 if (!at_tok_hasmore(&line))
1531 {
1532 goto error;
1533 }
1534
1535 err = at_tok_nextstr(&line, &ipv4);
1536 if (err < 0)
1537 {
1538 LOGE("Get IPv4 fail.");
1539 goto error;
1540 }
1541
1542 if (at_tok_hasmore(&line))
1543 {
1544 err = at_tok_nextstr(&line, &ipv6);
1545 if (err < 0)
1546 {
1547 LOGE("Get IPv6 fail.");
1548 goto error;
1549 }
1550 }
1551 else
1552 {
1553 LOGD("No IPv6 Found.");
1554 }
1555
1556 if(ipv6)
1557 {
1558 LOGD("IPv6 : %s", ipv6);
1559 }
1560
1561 if(ipv4)
1562 {
1563 LOGD("IPv4 : %s", ipv4);
1564
1565// ril_net_dev_config("ccinet0", ipv4, NULL);
1566 }
1567error:
1568 at_response_free(p_response);
1569}
1570
1571static void sim_state_change(bool plug_in) {
1572 if(plug_in) {
1573 // If radio on,must off in the first.
1574 if(net_info.radio_state == MBTK_RADIO_STATE_ON) {
1575 setRadioPower(0);
1576 }
1577 setRadioPower(1);
1578 } else {
1579 setRadioPower(0);
1580 }
1581}
1582
1583static int open_uevent_socket()
1584{
1585 struct sockaddr_nl addr;
1586 int sz = 64*1024;
1587 int s = 0;
1588
1589 memset(&addr, 0, sizeof(addr));
1590 addr.nl_family = AF_NETLINK;
1591 addr.nl_pid = getpid();
1592 addr.nl_groups = 0xffffffff;
1593
1594 s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
1595 if (s < 0)
1596 {
1597 LOGE("socket() fail.[%d]", errno);
1598 return -1;
1599 }
1600
1601 setsockopt(s, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz));
1602
1603 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0)
1604 {
1605 close(s);
1606 return -1;
1607 }
1608
1609 return s;
1610}
1611
1612static void parse_uevent(const char *msg, int msg_len, struct cooling_device *cdev)
1613{
1614 // change@/devices/virtual/usim_event/usim0\0
1615 // ACTION=change\0
1616 // DEVPATH=/devices/virtual/usim_event/usim0\0
1617 // SUBSYSTEM=usim_event\0
1618 // USIM_NAME=usim0\0
1619 // USIM_EVENT=plugout\0
1620 // SEQNUM=704
1621 int i = 0;
1622 while (i < msg_len)
1623 {
1624 if(*(msg + i) == '\0')
1625 {
1626 i++;
1627 continue;
1628 }
1629 if (!strncmp(msg + i, "USIM_NAME=", 10))
1630 {
1631 i += 10;
1632 cdev->name = msg + i;
1633 i += strlen(msg + i);
1634 }
1635 else if (!strncmp(msg + i, "ACTION=", 7))
1636 {
1637 i += 7;
1638 cdev->action = msg + i;
1639 i += strlen(msg + i);
1640 }
1641 else if (!strncmp(msg + i, "DEVPATH=", 8))
1642 {
1643 i += 8;
1644 cdev->path = msg + i;
1645 i += strlen(msg + i);
1646 }
1647 else if (!strncmp(msg + i, "USIM_EVENT=", 11))
1648 {
1649 i += 11;
1650 cdev->event = msg + i;
1651 i += strlen(msg + i);
1652 }
1653 else if (!strncmp(msg + i, "SUBSYSTEM=", 10))
1654 {
1655 i += 10;
1656 cdev->subsystem = msg + i;
1657 i += strlen(msg + i);
1658 }
1659 else
1660 {
1661 i++;
1662 }
1663 }
1664
1665 if(!strncmp(cdev->path, UEVENT_USIM_DEV, sizeof(UEVENT_USIM_DEV))
1666 && !strncmp(cdev->action, "change", 5))
1667 {
1668 LOGD("event { name=%s, action=%s, path=%s, subsystem=%s, event=%s}",
1669 cdev->name, cdev->action, cdev->path, cdev->subsystem, cdev->event);
1670 if(!strcmp(cdev->event, "plugout"))
1671 {
1672 sim_state_change(FALSE);
1673 }
1674 else if(!strcmp(cdev->event, "plugin"))
1675 {
1676 sim_state_change(TRUE);
1677 }
1678 else
1679 {
1680 LOGE("usim evnet error!");
1681 }
1682 }
1683}
1684
1685static void* uevnet_run(void *payload)
1686{
1687 int socket_fd = -1;
1688 char msg[BUFFER_SIZE+2];
1689 int n;
1690
1691 socket_fd = open_uevent_socket();
1692 if(socket_fd > 0)
1693 {
1694 while(1)
1695 {
1696 if((n = recv(socket_fd, msg, BUFFER_SIZE, 0)) > 0)
1697 {
1698 struct cooling_device cdev;
1699 memset(&cdev, 0x0, sizeof(cdev));
1700
1701 if(n == BUFFER_SIZE)
1702 continue;
1703 msg[n] = '\0';
1704 // change@/devices/virtual/usim_event/usim0\0ACTION=change\0DEVPATH=/devices/virtual/usim_event/usim0\0SUBSYSTEM=usim_event\0USIM_NAME=usim0\0USIM_EVENT=plugout\0SEQNUM=704
1705 log_hex("UEVENT", msg, n);
1706 parse_uevent(msg, n, &cdev);
1707 }
1708 else
1709 {
1710 LOGE("recv msg error.");
1711 }
1712 }
1713 }
1714
1715 LOGD("UEVENT Thread exit!!!");
1716 return NULL;
1717}
1718
1719int uevent_main()
1720{
1721 mbtk_task_info task;
1722 task.task_id = &uevnet_task_id;
1723 task.thread_run = uevnet_run;
1724 task.args = NULL;
1725 return mbtk_task_start(&task);
1726}
1727
1728/*
1729int ril_main()
1730{
1731 return mbtk_task_queue_start(&ril_task, ril_main_run);
1732}
1733*/
1734
1735int mbtk_info_server_start();
1736int InProduction_Mode(void);
1737void server_ready_set(void);
1738
1739
1740/*
1741 *Get mtdblock which name is ASR_FLAG
1742 *return path if found, else return NULL
1743 */
1744static int asrFlagPathGet(char *asr_flag_path)
1745{
1746 char buf[128];
1747 unsigned char find = 0;
1748 FILE *fd = fopen("/proc/mtd", "r");
1749 if (fd == NULL) {
1750 LOGE("Open MTD failed!");
1751 return -1;
1752 }
1753
1754 memset(buf, '\0', 128);
1755 while (fgets(buf, 128, fd) != NULL) {
1756 if(strstr(buf, "asr_flag")) {
1757 char *p = strstr(buf, "mtd");
1758 if(p)
1759 {
1760 int bln;
1761 sscanf(p, "mtd%d", &bln);
1762 sprintf(asr_flag_path, "/dev/mtdblock%d", bln);
1763 find = 1;
1764 break;
1765 }
1766 }
1767 memset(buf, '\0', 128);
1768 }
1769
1770 fclose(fd);
1771 return ((find == 1) ? 0 : -1);
1772}
1773
1774static int readFromMTD(const char *path, unsigned int offset, void *buf, int size)
1775{
1776 int ret, fd;
1777 if (!path)
1778 return -1;
1779
1780 fd = open(path, O_RDONLY);
1781 if (fd < 0) {
1782 LOGE("readFromMTD open error,%d", errno);
1783 return -1;
1784 }
1785
1786 ret = lseek(fd, offset, SEEK_SET);
1787 if (ret < 0) {
1788 close(fd);
1789 LOGE("readFromMTD lseek error,%d", errno);
1790 return -1;
1791 }
1792 ret = read(fd, buf, size);
1793 if (ret < 0) {
1794 close(fd);
1795 LOGE("readFromMTD read error,%d", errno);
1796 return -1;
1797 }
1798 close(fd);
1799 return 0;
1800}
1801
1802static int writeToMTD(const char *path, unsigned int offset, void *buf, int size)
1803{
1804 int ret, fd;
1805
1806 if (!path)
1807 return -1;
1808
1809 fd = open(path, O_RDWR | O_SYNC);
1810 if (fd < 0)
1811 return -1;
1812
1813 ret = lseek(fd, offset, SEEK_SET);
1814 if (ret < 0) {
1815 close(fd);
1816 return -1;
1817 }
1818 ret = write(fd, buf, size);
1819 if (ret < 0) {
1820 LOGE("writetomtd:write error:%d", errno);
1821 close(fd);
1822 return -1;
1823 }
1824
1825 close(fd);
1826 return 0;
1827}
1828
1829static void fota_result_check()
1830{
1831#if 0
1832 ASR_flag tag;
1833 char asr_flag_path[30] = {0};
1834 if(asrFlagPathGet(asr_flag_path)) {
1835 LOGE("getAsrFlagPath() fail.");
1836 return;
1837 }
1838
1839 if(readFromMTD(asr_flag_path, ASR_FLAG_OFFSET, &tag, sizeof(tag)) < 0)
1840 {
1841 LOGE("Get FOTA result fail.");
1842 }
1843 else
1844 {
1845 LOGD("FOTA result : %d, %d", tag.fota_result[0], tag.fota_result[1]);
1846 tag.fota_result[0] = 0;
1847 tag.fota_result[1] = 0;
1848 if(writeToMTD(asr_flag_path, ASR_FLAG_OFFSET, &tag, sizeof(tag)) < 0)
1849 {
1850 LOGE("FOTA result update fail.");
1851 } else {
1852 LOGD("FOTA result update success.");
1853 }
1854 }
1855#endif
1856}
1857
b.liubb590492024-06-13 16:42:08 +08001858static void mbtk_ril_ready()
liubin281ac462023-07-19 14:22:54 +08001859{
1860 // /etc/init.d/mbtk_boot_server_ready
b.liubb590492024-06-13 16:42:08 +08001861 if(is_first_boot) {
1862 if(access(MBTK_BOOT_SERVER_READY , X_OK) == 0) {
1863 LOGD("Exec : %s", MBTK_BOOT_SERVER_READY);
1864 system(MBTK_BOOT_SERVER_READY);
1865 } else {
1866 LOGE("%s can not exec.", MBTK_BOOT_SERVER_READY);
1867 }
liubin281ac462023-07-19 14:22:54 +08001868 } else {
b.liubb590492024-06-13 16:42:08 +08001869 LOGD("No exec : %s", MBTK_BOOT_SERVER_READY);
liubin281ac462023-07-19 14:22:54 +08001870 }
1871}
1872
1873#if 1
1874int main(int argc, char *argv[])
1875{
1876 mbtk_log_init("radio", "MBTK_RIL");
b.liubb590492024-06-13 16:42:08 +08001877
1878#ifdef MBTK_DUMP_SUPPORT
1879 mbtk_debug_open(NULL, TRUE);
1880#endif
1881
1882// Using Killall,the file lock may be not release.
1883#if 0
1884 if(app_already_running(MBTK_RILD_PID_FILE)) {
1885 LOGW("daemon already running.");
1886 exit(1);
1887 }
1888#endif
1889
liubin281ac462023-07-19 14:22:54 +08001890 LOGI("mbtk_ril start.");
1891
b.liubb590492024-06-13 16:42:08 +08001892 int fd = open(MBTK_RILD_TEMP_FILE, O_CREAT | O_RDWR, 0644);
1893 if(fd > 0) {
1894 char buff[10] = {0};
1895 int count = 0;
1896 if(read(fd, buff, sizeof(buff)) > 0) {
1897 count = atoi(buff);
1898 } else {
1899 count = 0;
1900 }
1901
1902 if(count <= 0) {
1903 is_first_boot = TRUE;
1904 count = 0;
1905 } else {
1906 is_first_boot = FALSE;
1907 }
1908
1909 count++;
1910 memset(buff, 0, sizeof(buff));
1911 snprintf(buff, sizeof(buff), "%d", count);
1912 write(fd, buff, strlen(buff));
1913 close(fd);
1914 } else {
1915 is_first_boot = FALSE;
1916 LOGE("Open %s fail:%d", MBTK_RILD_TEMP_FILE, errno);
1917 LOGW("Will not exec %s and %s.", MBTK_BOOT_SERVER_READY, MBTK_BOOT_NET_READY);
1918 }
1919
liubin281ac462023-07-19 14:22:54 +08001920 if(InProduction_Mode()) {
1921 LOGI("Is Production Mode, will exit...");
1922 exit(0);
1923 }
1924
1925 int at_sock = openSocket("/tmp/atcmd_at");
1926 if(at_sock < 0)
1927 {
1928 LOGE("Open AT Socket Fail[%d].", errno);
1929 return -1;
1930 }
1931 int uart_sock = openSocket("/tmp/atcmd_urc");
1932 if(uart_sock < 0)
1933 {
1934 LOGE("Open Uart Socket Fail[%d].", errno);
1935 return -1;
1936 }
1937
1938 at_set_on_reader_closed(onATReaderClosed);
1939 at_set_on_timeout(onATTimeout);
1940
1941 if(at_open(at_sock, uart_sock, onUnsolicited))
1942 {
1943 LOGE("Start AT thread fail.");
1944 return -1;
1945 }
1946
1947#if 1
1948 if(at_handshake())
1949 {
1950 LOGE("AT handshake fail.");
1951 return -1;
1952 }
1953#endif
1954
1955 LOGD("AT OK.");
1956
1957 if(mbtk_info_server_start())
1958 {
1959 LOGE("mbtk_info_server_start() fail.");
1960 return -1;
1961 }
b.liubb590492024-06-13 16:42:08 +08001962
wangyouqiang38e53362024-01-23 10:53:48 +08001963#ifdef MBTK_PROJECT_T108
1964 mbtk_led_init();
b.liubb590492024-06-13 16:42:08 +08001965#endif
liubin281ac462023-07-19 14:22:54 +08001966
1967#if 0
1968 if(uevent_main())
1969 {
1970 LOGE("Start uevent thread fail.");
1971 return -1;
1972 }
1973#endif
1974
1975 char time_type[10];
1976 memset(time_type, 0, 10);
1977 property_get("persist.mbtk.time_type", time_type, "0");
1978 if(atoi(time_type) == MBTK_TIME_TYPE_NTP) { // NTP time
1979 LOG("Start NTP thread.");
1980 ntp_thread_start();
1981 }
1982
1983 fota_result_check();
1984
b.liubb590492024-06-13 16:42:08 +08001985 mbtk_ril_ready();
liubin281ac462023-07-19 14:22:54 +08001986
1987 server_ready_set();//Set the server readiness state
1988 while(1)
1989 {
1990 sleep(24 * 60 * 60);
1991 }
1992
1993 LOGD("!!!mbtk_ril exit!!!");
1994 return 0;
1995}
1996
1997#else
1998int main()
1999{
2000 char buff[BUFFER_SIZE + 1] = {0};
2001 if(!mbtk_at("AT+CFUN=1", buff, BUFFER_SIZE))
2002 {
2003 LOGD("+CFUN RSP:%s", buff);
2004 while(1)
2005 {
2006 sleep(1);
2007 memset(buff, 0x0, BUFFER_SIZE + 1);
2008 if(!mbtk_at("AT+CGPADDR", buff, BUFFER_SIZE))
2009 {
2010 LOGD("+CGPADDR RSP:%s", buff);
2011 if(strstr(buff, "+CGPADDR:"))
2012 {
2013 // +CGPADDR: 1, "10.99.223.168", "254.128.0.0.0.0.0.0.0.1.0.0.117.9.250.59"
2014 //
2015 // OK
2016 char *ip_start = NULL;
2017 char *ip_end = NULL;
2018 char ipv4[50] = {0};
2019 ip_start = strstr(buff,"\"");
2020 if(ip_start)
2021 ip_end = strstr(ip_start + 1, "\"");
2022 if(ip_start && ip_end && ip_end - ip_start - 1 > 0)
2023 {
2024 memcpy(ipv4, ip_start + 1, ip_end - ip_start - 1);
2025 LOGD("IP : %s", ipv4);
2026 if(!mbtk_ifc_open())
2027 {
2028 in_addr_t addr;
2029 inet_aton(ipv4,(struct in_addr *)&addr);
2030 LOGD("IP : %s -> %x", ipv4, addr);
2031 if(!mbtk_ifc_set_addr("ccinet0", addr, 0))
2032 {
2033 mbtk_ifc_up("ccinet0");
2034 }
2035
2036 mbtk_ifc_close();
2037 }
2038
2039 system("route add default dev ccinet0");
2040
2041 LOGD("Set IP success.");
2042 }
2043 else
2044 {
2045 LOGD("Get IP fail.");
2046 }
2047
2048 break;
2049 }
2050 }
2051 }
2052 }
2053
2054 return 0;
2055}
2056#endif