blob: 6d653747371130cc82a1dd5d4be485ebd7748c10 [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;
liuyang67f4bac2024-06-26 17:06:13 +0800434 static bool net_led_gms_wcdma = FALSE;
435 static bool net_led_lte = FALSE;
liubin281ac462023-07-19 14:22:54 +0800436 while(*tmp_s && *tmp_s == ' ')
437 tmp_s++;
b.liufe320632024-01-17 20:38:08 +0800438 uint8 data[2];
439 data[0] = (uint8)atoi(tmp_s); // Reg State.
liubin281ac462023-07-19 14:22:54 +0800440
liuyang67f4bac2024-06-26 17:06:13 +0800441 if(strStartsWith(s, "+CGREG:"))
442 {
443 data[1] = 0; // GMS/WCDMA
444 if(data[0] == 1)
445 {
446 net_led_gms_wcdma = TRUE;
447 }
448 else
449 {
450 net_led_gms_wcdma = FALSE;
451 }
452
453 }
454 else
455 {
456 data[1] = 1; // LTE
457 if(data[0] == 1)
458 {
459 net_led_lte = TRUE;
460 }
461 else
462 {
463 net_led_lte = FALSE;
464 }
465 }
466
467 if(FALSE == net_led_gms_wcdma && FALSE == net_led_lte)
468 {
469 mbtk_net_led_set(MBTK_NET_LED_SEARCH_NETWORK);
470 }
471 else
472 {
473 mbtk_net_led_set(MBTK_NET_LED_NET_CONNECT);
474 mbtk_net_ready();
b.liufe320632024-01-17 20:38:08 +0800475 }
476
477 urc_msg_distribute(true, INFO_URC_MSG_NET_PS_REG_STATE, data, sizeof(data));
liuyang37623a02024-06-20 15:21:39 +0800478 urc_msg_distribute(true, INFO_URC_MSG_NET_STATE_LOG, NULL, 0);
liubin281ac462023-07-19 14:22:54 +0800479 }
480 // +CREG: 1, "8010", "000060a5", 0, 2, 0
481 // +CREG: 1, "8330", "06447347", 7, 2, 0
482 // +CREG: 0
483 else if(strStartsWith(s, "+CREG:")) // GMS/WCDMA/LTE CS registed.
484 {
485 uint8 data[3];
486 data[0] = (uint8)MBTK_NET_CS_STATE;
487 char* tmp_s = memdup(s,strlen(s));
488 char* free_ptr = tmp_s;
489 char *line = tmp_s;
490 int tmp_int;
491 char *tmp_str;
492 if (at_tok_start(&line) < 0)
493 {
494 goto CREG_EXIT;
495 }
496 if (at_tok_nextint(&line, &tmp_int) < 0)
497 {
498 goto CREG_EXIT;
499 }
500 data[1] = (uint8)tmp_int; // Reg State.
501 if (data[1])
502 {
503 if (at_tok_nextstr(&line, &tmp_str) < 0)
504 {
505 goto CREG_EXIT;
506 }
507 if (at_tok_nextstr(&line, &tmp_str) < 0)
508 {
509 goto CREG_EXIT;
510 }
511 if (at_tok_nextint(&line, &tmp_int) < 0)
512 {
513 goto CREG_EXIT;
514 }
515 data[2] = (uint8)tmp_int; // AcT
516 } else {
517 data[2] = (uint8)0xFF; // AcT
518 }
519 if(data[1] == 5)
520 {
521 uint8 data_pdp;
522 data_pdp = 5; //
523 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
524 }
525 urc_msg_distribute(false, INFO_URC_MSG_NET_CS_REG_STATE, data, sizeof(data));
liuyang37623a02024-06-20 15:21:39 +0800526 urc_msg_distribute(true, INFO_URC_MSG_NET_STATE_LOG, NULL, 0);
liubin281ac462023-07-19 14:22:54 +0800527CREG_EXIT:
528 free(free_ptr);
529 }
530 // +CLCC: 1, 1, 6, 0, 0, "18981911691", 129, "",, 0
531 else if(strStartsWith(s, "+CLCC:"))
532 {
533 mbtk_call_info_t reg;
534 reg.call_wait = MBTK_CLCC;
535 char* tmp_s = memdup(s,strlen(s));
536 char* free_ptr = tmp_s;
537 char *line = tmp_s;
538 int tmp_int;
539 char *tmp_str;
540 int err;
541
542 err = at_tok_start(&line);
543 if (err < 0)
544 {
545 goto CLCC_EXIT;
546 }
547 err = at_tok_nextint(&line, &tmp_int); // dir1
548 if (err < 0)
549 {
550 goto CLCC_EXIT;
551 }
552 reg.dir1 = (uint8)tmp_int;
553 err = at_tok_nextint(&line, &tmp_int);// dir
554 if (err < 0)
555 {
556 goto CLCC_EXIT;
557 }
558 reg.dir = (uint8)tmp_int;
559 err = at_tok_nextint(&line, &tmp_int);// state
560 if (err < 0)
561 {
562 goto CLCC_EXIT;
563 }
564 reg.state = (uint8)tmp_int;
565 err = at_tok_nextint(&line, &tmp_int);// mode
566 if (err < 0)
567 {
568 goto CLCC_EXIT;
569 }
570 reg.mode = (uint8)tmp_int;
571 err = at_tok_nextint(&line, &tmp_int);// mpty
572 if (err < 0)
573 {
574 goto CLCC_EXIT;
575 }
576 reg.mpty = (uint8)tmp_int;
577 err = at_tok_nextstr(&line, &tmp_str); // phone_number
578 if (err < 0)
579 {
580 goto CLCC_EXIT;
581 }
582
583 memset(reg.phone_number,0,sizeof(reg.phone_number));
584 memcpy(reg.phone_number, tmp_str, strlen(tmp_str));
585 err = at_tok_nextint(&line, &tmp_int);// tpye
586 if (err < 0)
587 {
588 goto CLCC_EXIT;
589 }
590 reg.type = (uint8)tmp_int;
591
r.xiaoe1404b32024-05-23 22:43:39 -0700592 if(reg.state == 2 || reg.state == 3 || reg.state == 0)
593 {
594 mbtk_net_led_set(MBTK_NET_LED_CALL_CONNECT);
595 }
596
r.xiao195e2522024-05-31 03:19:02 -0700597 if(reg.state == 4 && reg.dir == 1)
598 {
599 mbtk_net_led_set(MBTK_NET_LED_CALL_CONNECT);
600 }
601
602
liubin281ac462023-07-19 14:22:54 +0800603 urc_msg_distribute(false, INFO_URC_MSG_CALL_STATE, &reg, sizeof(mbtk_call_info_t));
604CLCC_EXIT:
605 free(free_ptr);
606 }
607 // +CPAS: 4
608 else if(strStartsWith(s, "+CPAS:"))
609 {
610 mbtk_call_info_t reg;
611 reg.call_wait = 0;
612 char* tmp_s = memdup(s,strlen(s));
613 char* free_ptr = tmp_s;
614 char *line = tmp_s;
615 int tmp_int;
616 int err;
617
618 memset(&reg,0,sizeof(reg));
619
620 err = at_tok_start(&line);
621 if (err < 0)
622 {
623 goto CPAS_EXIT;
624 }
625 err = at_tok_nextint(&line, &tmp_int);
626 if (err < 0)
627 {
628 goto CPAS_EXIT;
629 }
630 reg.pas = (uint8)tmp_int;
631 reg.call_wait = MBTK_CPAS;
632 urc_msg_distribute(false, INFO_URC_MSG_CALL_STATE, &reg, sizeof(mbtk_call_info_t));
633CPAS_EXIT:
634 free(free_ptr);
635 }
636 // +CALLDISCONNECT: 1
637 else if(strStartsWith(s, "+CALLDISCONNECT:"))
638 {
639 mbtk_call_info_t reg;
640 reg.call_wait = 0;
641 char* tmp_s = memdup(s,strlen(s));
642 char* free_ptr = tmp_s;
643 char *line = tmp_s;
644 int tmp_int;
645 int err;
646
647 memset(&reg,0,sizeof(reg));
648
649 err = at_tok_start(&line);
650 if (err < 0)
651 {
652 goto CALLDISCONNECTED_EXIT;
653 }
654 err = at_tok_nextint(&line, &tmp_int);
655 if (err < 0)
656 {
657 goto CALLDISCONNECTED_EXIT;
658 }
659 reg.disconnected_id = tmp_int;
660 reg.call_wait = MBTK_DISCONNECTED;
r.xiaoe1404b32024-05-23 22:43:39 -0700661
662 if(reg.call_wait == MBTK_DISCONNECTED)
663 {
664 mbtk_net_led_set(MBTK_NET_LED_CALL_DISCONNECT);
665 }
666
liubin281ac462023-07-19 14:22:54 +0800667 urc_msg_distribute(false, INFO_URC_MSG_CALL_STATE, &reg, sizeof(mbtk_call_info_t));
668
669CALLDISCONNECTED_EXIT:
670 free(free_ptr);
671 }
672 // *SIMDETEC:1,SIM
673 else if(strStartsWith(s, "*SIMDETEC:"))
674 {
liuyange22a25e2024-05-23 19:41:52 +0800675 if(strStartsWith(s, "*SIMDETEC:1,NOS"))
676 {
677 net_info.sim_state = MBTK_SIM_ABSENT;
678 }
b.liubb590492024-06-13 16:42:08 +0800679
liubin281ac462023-07-19 14:22:54 +0800680 sim_info_reg.sim = -1;
681 if(strStartsWith(s, "*SIMDETEC:1,NOS"))
682 sim_info_reg.sim = 0;
683 else if(strStartsWith(s, "*SIMDETEC:1,SIM"))
684 sim_info_reg.sim = 1;
685 if(sim_info_reg.sim == 0)
686 {
687 uint8 data_pdp;
688 data_pdp = 11; //
689 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
690 }
691 urc_msg_distribute(false, INFO_URC_MSG_SIM_STATE, &sim_info_reg, sizeof(mbtk_sim_card_info));
692 }
693 // *EUICC:1
694/*0: SIM
6951: USIM
6962: TEST SIM
6973: TEST USIM
6984: UNKNOWN
699Note: *EUICC:
700*/
701 else if(strStartsWith(s, "*EUICC:"))
702 {
703 sim_info_reg.sim_card_type = -1;
704 if(strStartsWith(s, "*EUICC: 0"))
705 sim_info_reg.sim_card_type = 1;
706 else if(strStartsWith(s, "*EUICC: 1"))
707 sim_info_reg.sim_card_type = 2;
708 else if(strStartsWith(s, "*EUICC: 2"))
709 sim_info_reg.sim_card_type = 1;
710 else if(strStartsWith(s, "*EUICC: 3"))
711 sim_info_reg.sim_card_type = 2;
712 else if(strStartsWith(s, "*EUICC: 4"))
713 sim_info_reg.sim_card_type = 0;
714 urc_msg_distribute(false, INFO_URC_MSG_SIM_STATE, &sim_info_reg, sizeof(mbtk_sim_card_info));
715 }
716 // +CPIN: SIM PIN
717 else if(strStartsWith(s, "+CPIN:"))
718 {
719 sim_info_reg.sim = -1;
720 if(strStartsWith(s, "+CPIN: READY"))
liuyange22a25e2024-05-23 19:41:52 +0800721 {
liubin281ac462023-07-19 14:22:54 +0800722 sim_info_reg.sim = 1;
liuyange22a25e2024-05-23 19:41:52 +0800723 net_info.sim_state = MBTK_SIM_READY;
724 }
liubin281ac462023-07-19 14:22:54 +0800725 else if(strStartsWith(s, "+CPIN: SIM PIN"))
liuyange22a25e2024-05-23 19:41:52 +0800726 {
liubin281ac462023-07-19 14:22:54 +0800727 sim_info_reg.sim = 2;
liuyange22a25e2024-05-23 19:41:52 +0800728 net_info.sim_state = MBTK_SIM_PIN;
729 }
liubin281ac462023-07-19 14:22:54 +0800730 else if(strStartsWith(s, "+CPIN: SIM PUK"))
liuyange22a25e2024-05-23 19:41:52 +0800731 {
liubin281ac462023-07-19 14:22:54 +0800732 sim_info_reg.sim = 3;
liuyange22a25e2024-05-23 19:41:52 +0800733 net_info.sim_state = MBTK_SIM_PUK;
734 }
liubin281ac462023-07-19 14:22:54 +0800735 else if(strStartsWith(s, "+CPIN: PH-SIMLOCK PIN"))
liuyange22a25e2024-05-23 19:41:52 +0800736 {
liubin281ac462023-07-19 14:22:54 +0800737 sim_info_reg.sim = 4;
liuyange22a25e2024-05-23 19:41:52 +0800738 net_info.sim_state = MBTK_SIM_ABSENT;
739 }
liubin281ac462023-07-19 14:22:54 +0800740 else if(strStartsWith(s, "+CPIN: PH-SIMLOCK PUK"))
liuyange22a25e2024-05-23 19:41:52 +0800741 {
liubin281ac462023-07-19 14:22:54 +0800742 sim_info_reg.sim = 5;
liuyange22a25e2024-05-23 19:41:52 +0800743 net_info.sim_state = MBTK_SIM_ABSENT;
744 }
liubin281ac462023-07-19 14:22:54 +0800745 else if(strStartsWith(s, "+CPIN: PH-FSIM PIN"))
liuyange22a25e2024-05-23 19:41:52 +0800746 {
liubin281ac462023-07-19 14:22:54 +0800747 sim_info_reg.sim = 6;
liuyange22a25e2024-05-23 19:41:52 +0800748 net_info.sim_state = MBTK_SIM_ABSENT;
749 }
liubin281ac462023-07-19 14:22:54 +0800750 else if(strStartsWith(s, "+CPIN: PH-FSIM PUK"))
liuyange22a25e2024-05-23 19:41:52 +0800751 {
liubin281ac462023-07-19 14:22:54 +0800752 sim_info_reg.sim = 7;
liuyange22a25e2024-05-23 19:41:52 +0800753 net_info.sim_state = MBTK_SIM_ABSENT;
754 }
liubin281ac462023-07-19 14:22:54 +0800755 else if(strStartsWith(s, "+CPIN: SIM PIN2"))
liuyange22a25e2024-05-23 19:41:52 +0800756 {
liubin281ac462023-07-19 14:22:54 +0800757 sim_info_reg.sim = 8;
liuyange22a25e2024-05-23 19:41:52 +0800758 net_info.sim_state = MBTK_SIM_ABSENT;
759 }
liubin281ac462023-07-19 14:22:54 +0800760 else if(strStartsWith(s, "+CPIN: SIM PUK2"))
liuyange22a25e2024-05-23 19:41:52 +0800761 {
liubin281ac462023-07-19 14:22:54 +0800762 sim_info_reg.sim = 9;
liuyange22a25e2024-05-23 19:41:52 +0800763 net_info.sim_state = MBTK_SIM_ABSENT;
764 }
liubin281ac462023-07-19 14:22:54 +0800765 else if(strStartsWith(s, "+CPIN: PH-NET PIN"))
liuyange22a25e2024-05-23 19:41:52 +0800766 {
liubin281ac462023-07-19 14:22:54 +0800767 sim_info_reg.sim = 10;
liuyange22a25e2024-05-23 19:41:52 +0800768 net_info.sim_state = MBTK_SIM_NETWORK_PERSONALIZATION;
769 }
liubin281ac462023-07-19 14:22:54 +0800770 else if(strStartsWith(s, "+CPIN: PH-NET PUK"))
liuyange22a25e2024-05-23 19:41:52 +0800771 {
liubin281ac462023-07-19 14:22:54 +0800772 sim_info_reg.sim = 11;
liuyange22a25e2024-05-23 19:41:52 +0800773 net_info.sim_state = MBTK_SIM_ABSENT;
774 }
liubin281ac462023-07-19 14:22:54 +0800775 else if(strStartsWith(s, "+CPIN: PH-NETSUB PINMT"))
liuyange22a25e2024-05-23 19:41:52 +0800776 {
liubin281ac462023-07-19 14:22:54 +0800777 sim_info_reg.sim = 12;
liuyange22a25e2024-05-23 19:41:52 +0800778 net_info.sim_state = MBTK_SIM_ABSENT;
779 }
liubin281ac462023-07-19 14:22:54 +0800780 else if(strStartsWith(s, "+CPIN: PH-NETSUB PUK"))
liuyange22a25e2024-05-23 19:41:52 +0800781 {
liubin281ac462023-07-19 14:22:54 +0800782 sim_info_reg.sim = 13;
liuyange22a25e2024-05-23 19:41:52 +0800783 net_info.sim_state = MBTK_SIM_ABSENT;
784 }
liubin281ac462023-07-19 14:22:54 +0800785 else if(strStartsWith(s, "+CPIN: PH-SP PIN"))
liuyange22a25e2024-05-23 19:41:52 +0800786 {
liubin281ac462023-07-19 14:22:54 +0800787 sim_info_reg.sim = 14;
liuyange22a25e2024-05-23 19:41:52 +0800788 net_info.sim_state = MBTK_SIM_ABSENT;
789 }
liubin281ac462023-07-19 14:22:54 +0800790 else if(strStartsWith(s, "+CPIN: PH-SP PUK"))
liuyange22a25e2024-05-23 19:41:52 +0800791 {
liubin281ac462023-07-19 14:22:54 +0800792 sim_info_reg.sim = 15;
liuyange22a25e2024-05-23 19:41:52 +0800793 net_info.sim_state = MBTK_SIM_ABSENT;
794 }
liubin281ac462023-07-19 14:22:54 +0800795 else if(strStartsWith(s, "+CPIN: PH-CORP PIN"))
liuyange22a25e2024-05-23 19:41:52 +0800796 {
liubin281ac462023-07-19 14:22:54 +0800797 sim_info_reg.sim = 16;
liuyange22a25e2024-05-23 19:41:52 +0800798 net_info.sim_state = MBTK_SIM_ABSENT;
799 }
liubin281ac462023-07-19 14:22:54 +0800800 else if(strStartsWith(s, "+CPIN: PH-CORP PUK"))
liuyange22a25e2024-05-23 19:41:52 +0800801 {
liubin281ac462023-07-19 14:22:54 +0800802 sim_info_reg.sim = 17;
liuyange22a25e2024-05-23 19:41:52 +0800803 net_info.sim_state = MBTK_SIM_ABSENT;
804 }
liubin281ac462023-07-19 14:22:54 +0800805 else if(strStartsWith(s, "+CPIN: SIM REMOVED"))
liuyange22a25e2024-05-23 19:41:52 +0800806 {
807 sim_info_reg.sim = 18;
808 net_info.sim_state = MBTK_SIM_ABSENT;
809 }
liubin281ac462023-07-19 14:22:54 +0800810 else
811 sim_info_reg.sim = 20;
812
813 if(sim_info_reg.sim == 18)
814 {
815 uint8 data_pdp;
816 data_pdp = 11; //
817 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
818 }
819
820 urc_msg_distribute(false, INFO_URC_MSG_SIM_STATE, &sim_info_reg, sizeof(mbtk_sim_card_info));
821 }
822 // +CMT: ,23
823 // 0891683108200855F6240D91688189911196F10000221130717445230331D90C
824 else if(strStartsWith(s, "+CMT:") || sms_cmt)
825 {
826 if(!sms_cmt){
827 sms_cmt = true;
828 }else{
829 sms_cmt = false;
830 }
831 printf("+CMT() sms_cmt:%d, s:%s, len:%d\n",sms_cmt, s, strlen(s));
832 urc_msg_distribute(false, INFO_URC_MSG_SMS_STATE, s, strlen(s));
833 }
834#if 0
835 // LTE data registed.
836 // +CEREG: 1, "8330", "06447347", 7
837 else if(strStartsWith(s, "+CEREG:"))
838 {
839 char* tmp_s = memdup(s,strlen(s));
840 char* free_ptr = tmp_s;
841 char *line = tmp_s;
842 int tmp_int;
843 char *tmp_str;
844 if (at_tok_start(&line) < 0)
845 {
846 goto CREG_EXIT;
847 }
848 if (at_tok_nextint(&line, &tmp_int) < 0)
849 {
850 goto CREG_EXIT;
851 }
852 uint8 data = (uint8)tmp_int; // Reg State.
853
854 urc_msg_distribute(INFO_URC_MSG_NET_REG_STATE, &data, sizeof(uint8));
855CREG_EXIT:
856 free(free_ptr);
857 }
858#endif
859 /*
860 // <mcc>, <length of mnc>, <mnc>, <tac>, <PCI>, <dlEuarfcn>, < ulEuarfcn >, <band>, <dlBandwidth>,
861 // <rsrp>,<rsrq>, <sinr>,
862 // errcModeState,emmState,serviceState,IsSingleEmmRejectCause,EMMRejectCause,mmeGroupId,mmeCode,mTmsi,
863 // cellId,subFrameAssignType,specialSubframePatterns,transMode
864 // mainRsrp,diversityRsrp,mainRsrq,diversityRsrq,rssi,cqi,pathLoss,tb0DlTpt,tb1DlTpt,tb0DlPeakTpt,tb1DlPeakTpt,tb0UlPeakTpt,
865 // tb1UlPeakTpt,dlThroughPut,dlPeakThroughPut,averDlPRB,averCQITb0,averCQITb1,rankIndex,grantTotal,ulThroughPut,ulPeakThroughPut,currPuschTxPower,averUlPRB,
866 // dlBer, ulBer,
867 // diversitySinr, diversityRssi
868 +EEMLTESVC: 1120, 2, 0, 33584, 430, 40936, 40936, 41, 20,
869 0, 0, 0,
870 1, 10, 0, 1, 0, 1059, 78, 3959566565,
871 105149248, 2, 7, 7,
872 0, 0, 0, 0, 0, 0, 0, 1190919, 0, 0, 0, 16779777,
873 0, 5112867, 3959566565, 2, 0, 0, 0, 0, 0, 0, 0, 0,
874 0, 0,
875 7, 44
876 */
877 else if(strStartsWith(s, "+EEMLTESVC:")) // LTE Server Cell
878 {
879 // tac, PCI, dlEuarfcn, ulEuarfcn, band
880 if(cell_info.running) {
881 int tmp_int;
882 int i = 0;
883 char* tmp_s = memdup(s,strlen(s));
884 char* free_ptr = tmp_s;
885 char *line = tmp_s;
886 if (at_tok_start(&line) < 0)
887 {
888 goto EEMLTESVC_EXIT;
889 }
890
891 if (at_tok_nextint(&line, &tmp_int) < 0)
892 {
893 goto EEMLTESVC_EXIT;
894 }
895 cell_info.cell[cell_info.cell_num].value6 = (uint32)tmp_int; //mcc
896 if (at_tok_nextint(&line, &tmp_int) < 0)
897 {
898 goto EEMLTESVC_EXIT;
899 }
900 if (at_tok_nextint(&line, &tmp_int) < 0)
901 {
902 goto EEMLTESVC_EXIT;
903 }
904 cell_info.cell[cell_info.cell_num].value7 = (uint32)tmp_int; //mnc
905 /*
906 // Jump 2 integer.
907 i = 0;
908 while(i < 2) {
909 if (at_tok_nextint(&line, &tmp_int) < 0)
910 {
911 goto EEMLTESVC_EXIT;
912 }
913 i++;
914 }
915 */
916 if (at_tok_nextint(&line, &tmp_int) < 0)
917 {
918 goto EEMLTESVC_EXIT;
919 }
920 cell_info.cell[cell_info.cell_num].value1 = (uint32)tmp_int; //tac
921 if (at_tok_nextint(&line, &tmp_int) < 0)
922 {
923 goto EEMLTESVC_EXIT;
924 }
925 cell_info.cell[cell_info.cell_num].value2 = (uint32)tmp_int; //pci
926 if (at_tok_nextint(&line, &tmp_int) < 0)
927 {
928 goto EEMLTESVC_EXIT;
929 }
930 cell_info.cell[cell_info.cell_num].value3 = (uint32)tmp_int; //dl arfcn
931 if (at_tok_nextint(&line, &tmp_int) < 0)
932 {
933 goto EEMLTESVC_EXIT;
934 }
935 cell_info.cell[cell_info.cell_num].value4 = (uint32)tmp_int; //ul arfcn
936 if (at_tok_nextint(&line, &tmp_int) < 0)
937 {
938 goto EEMLTESVC_EXIT;
939 }
940 cell_info.cell[cell_info.cell_num].value5 = (uint32)tmp_int; //band
941 if (at_tok_nextint(&line, &tmp_int) < 0)
942 {
943 goto EEMLTESVC_EXIT;
944 }
945 if (at_tok_nextint(&line, &tmp_int) < 0)
946 {
947 goto EEMLTESVC_EXIT;
948 }
949 cell_info.cell[cell_info.cell_num].value8 = (uint32)tmp_int; //cid
950 if (at_tok_nextint(&line, &tmp_int) < 0)
951 {
952 goto EEMLTESVC_EXIT;
953 }
954 cell_info.cell[cell_info.cell_num].value9 = (uint32)tmp_int; //rsrp
955
956 for(i =0; i < 10; i++)
957 {
958 if (at_tok_nextint(&line, &tmp_int) < 0)
959 {
960 goto EEMLTESVC_EXIT;
961 }
962 }
963 cell_info.cell[cell_info.cell_num].value10 = (uint32)tmp_int; //cell identiy
964
965 cell_info.cell_num++;
966
967EEMLTESVC_EXIT:
968 free(free_ptr);
969 }
970 }
971 /*
972 // index,phyCellId,euArfcn,rsrp,rsrq
973 +EEMLTEINTER: 0, 65535, 38950, 0, 0
974 */
975 else if(strStartsWith(s, "+EEMLTEINTER:") || strStartsWith(s, "+EEMLTEINTRA:")) // LTE ÒìÆµ/Í¬ÆµÐ¡Çø
976 {
977 // phyCellId,euArfcn,rsrp,rsrq
978 if(cell_info.running) {
979 int tmp_int;
980 char* tmp_s = memdup(s,strlen(s));
981 char* free_ptr = tmp_s;
982 char *line = tmp_s;
983 if (at_tok_start(&line) < 0)
984 {
985 goto EEMLTEINTER_EXIT;
986 }
987 if (at_tok_nextint(&line, &tmp_int) < 0)
988 {
989 goto EEMLTEINTER_EXIT;
990 }
991 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int <= 0 || tmp_int > 503)
992 {
993 goto EEMLTEINTER_EXIT;
994 }
995 cell_info.cell[cell_info.cell_num].value1 = (uint32)tmp_int;
996 if (at_tok_nextint(&line, &tmp_int) < 0)
997 {
998 goto EEMLTEINTER_EXIT;
999 }
1000 cell_info.cell[cell_info.cell_num].value2 = (uint32)tmp_int;
1001 if (at_tok_nextint(&line, &tmp_int) < 0)
1002 {
1003 goto EEMLTEINTER_EXIT;
1004 }
1005 cell_info.cell[cell_info.cell_num].value3 = (uint32)tmp_int;
1006 LOG("cell line : %s", line);
1007 if (at_tok_nextint(&line, &tmp_int) < 0)
1008 {
1009 goto EEMLTEINTER_EXIT;
1010 }
1011 cell_info.cell[cell_info.cell_num].value4 = (uint32)tmp_int;
1012 if (at_tok_nextint(&line, &tmp_int) < 0)
1013 {
1014 LOG("cell tmp_int : %d", tmp_int);
1015 goto EEMLTEINTER_EXIT;
1016 }
1017 cell_info.cell[cell_info.cell_num].value5 = (uint32)tmp_int;
1018 LOG("cell value5 : %d", cell_info.cell[cell_info.cell_num].value5);
1019 cell_info.cell_num++;
1020EEMLTEINTER_EXIT:
1021 free(free_ptr);
1022 }
1023 }
1024 // Do nothing
1025 else if(strStartsWith(s, "+EEMLTEINTERRAT:")) // LTE RATÐ¡ÇøÐÅÏ¢
1026 {
1027 if(cell_info.running) {
1028
1029 }
1030 }
1031 // WCDMA
1032 /*
1033 // Mode, sCMeasPresent, sCParamPresent, ueOpStatusPresent,
1034
1035 // if sCMeasPresent == 1
1036 // cpichRSCP, utraRssi, cpichEcN0, sQual, sRxLev, txPower,
1037 // endif
1038
1039 // if sCParamPresent == 1
1040 // rac, nom, mcc, mnc_len, mnc, lac, ci,
1041 // uraId, psc, arfcn, t3212, t3312, hcsUsed, attDetAllowed,
1042 // csDrxCycleLen, psDrxCycleLen, utranDrxCycleLen, HSDPASupport, HSUPASupport,
1043 // endif
1044
1045 // if ueOpStatusPresent == 1
1046 // rrcState, numLinks, srncId, sRnti,
1047 // algPresent, cipherAlg, cipherOn, algPresent, cipherAlg, cipherOn,
1048 // HSDPAActive, HSUPAActive, MccLastRegisteredNetwork, MncLastRegisteredNetwork, TMSI, PTMSI, IsSingleMmRejectCause, IsSingleGmmRejectCause,
1049 // MMRejectCause, GMMRejectCause, mmState, gmmState, gprsReadyState, readyTimerValueInSecs, NumActivePDPContext, ULThroughput, DLThroughput,
1050 // serviceStatus, pmmState, LAU_status, LAU_count, RAU_status, RAU_count
1051 // endif
1052 //
1053 +EEMUMTSSVC: 3, 1, 1, 1,
1054 -80, 27, -6, -18, -115, -32768,
1055 1, 1, 1120, 2, 1, 61697, 168432821,
1056 15, 24, 10763, 0, 0, 0, 0,
1057 128, 128, 65535, 0, 0,
1058 2, 255, 65535, 4294967295,
1059 0, 0, 0, 0, 0, 0,
1060 0, 0, 0, 0, 0, 0, 1, 1,
1061 28672, 28672, 0, 0, 0, 0, 0, 0, 0,
1062 0, 0, 0, 0, 0, 0
1063 */
1064 else if(strStartsWith(s, "+EEMUMTSSVC:")) // WCDMA Server Cell
1065 {
1066 // lac, ci, arfcn
1067 if(cell_info.running) {
1068 int tmp_int;
1069 int i = 0;
1070 char* tmp_s = memdup(s,strlen(s));
1071 char* free_ptr = tmp_s;
1072 char *line = tmp_s;
1073 if (at_tok_start(&line) < 0)
1074 {
1075 goto EEMUMTSSVC_EXIT;
1076 }
1077 // Jump 12 integer.
1078 i = 0;
1079 while(i < 12) {
1080 if (at_tok_nextint(&line, &tmp_int) < 0)
1081 {
1082 goto EEMUMTSSVC_EXIT;
1083 }
1084 i++;
1085 }
1086 // mcc
1087 if (at_tok_nextint(&line, &tmp_int) < 0)
1088 {
1089 goto EEMUMTSSVC_EXIT;
1090 }
1091 cell_info.cell[cell_info.cell_num].value4= (uint32)tmp_int;
1092 // mnc
1093 if (at_tok_nextint(&line, &tmp_int) < 0)
1094 {
1095 goto EEMUMTSSVC_EXIT;
1096 }
1097 cell_info.cell[cell_info.cell_num].value5= (uint32)tmp_int;
1098 // lac
1099 if (at_tok_nextint(&line, &tmp_int) < 0)
1100 {
1101 goto EEMUMTSSVC_EXIT;
1102 }
1103 cell_info.cell[cell_info.cell_num].value1 = (uint32)tmp_int;
1104 // ci
1105 if (at_tok_nextint(&line, &tmp_int) < 0)
1106 {
1107 goto EEMUMTSSVC_EXIT;
1108 }
1109 cell_info.cell[cell_info.cell_num].value2 = (uint32)tmp_int;
1110
1111 if (at_tok_nextint(&line, &tmp_int) < 0)
1112 {
1113 goto EEMUMTSSVC_EXIT;
1114 }
1115 // cpi
1116 if (at_tok_nextint(&line, &tmp_int) < 0)
1117 {
1118 goto EEMUMTSSVC_EXIT;
1119 }
1120 cell_info.cell[cell_info.cell_num].value6= (uint32)tmp_int;
1121 /*
1122 // Jump 2 integer.
1123 i = 0;
1124 while(i < 2) {
1125 if (at_tok_nextint(&line, &tmp_int) < 0)
1126 {
1127 goto EEMUMTSSVC_EXIT;
1128 }
1129 i++;
1130 }
1131 */
1132 // arfcn
1133 if (at_tok_nextint(&line, &tmp_int) < 0)
1134 {
1135 goto EEMUMTSSVC_EXIT;
1136 }
1137 cell_info.cell[cell_info.cell_num].value3 = (uint32)tmp_int;
1138
1139 cell_info.cell_num++;
1140EEMUMTSSVC_EXIT:
1141 free(free_ptr);
1142 }
1143 }
1144 /*
1145 // index, cpichRSCP, utraRssi, cpichEcN0, sQual, sRxLev ,mcc, mnc, lac, ci, arfcn, psc
1146 +EEMUMTSINTRA: 0, -32768, -1, -32768, -18, -115, 0, 0, 65534, 1, 10763, 32
1147 */
1148 else if(strStartsWith(s, "+EEMUMTSINTRA:")) // WCDMAÁÙ½üÐ¡Çø
1149 {
1150 // lac, ci, arfcn
1151 if(cell_info.running) {
1152 int tmp_int;
1153 int i = 0;
1154 char* tmp_s = memdup(s,strlen(s));
1155 char* free_ptr = tmp_s;
1156 char *line = tmp_s;
1157 if (at_tok_start(&line) < 0)
1158 {
1159 goto EEMUMTSINTRA_EXIT;
1160 }
1161 // Jump 8 integer.
1162 i = 0;
1163 while(i < 8) {
1164 if (at_tok_nextint(&line, &tmp_int) < 0)
1165 {
1166 goto EEMUMTSINTRA_EXIT;
1167 }
1168 i++;
1169 }
1170
1171 // lac
1172 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int <= 0 || tmp_int >= 65536)
1173 {
1174 goto EEMUMTSINTRA_EXIT;
1175 }
1176 cell_info.cell[cell_info.cell_num].value1 = (uint32)tmp_int;
1177
1178 // ci
1179 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int <= 0 || tmp_int >= 65536)
1180 {
1181 goto EEMUMTSINTRA_EXIT;
1182 }
1183 cell_info.cell[cell_info.cell_num].value2 = (uint32)tmp_int;
1184
1185 // arfcn
1186 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int <= 0 || tmp_int >= 65536)
1187 {
1188 goto EEMUMTSINTRA_EXIT;
1189 }
1190 cell_info.cell[cell_info.cell_num].value3 = (uint32)tmp_int;
1191
1192 cell_info.cell_num++;
1193EEMUMTSINTRA_EXIT:
1194 free(free_ptr);
1195 }
1196 }
1197 /*
1198 // index,gsmRssi,rxLev,C1,C2,mcc,mnc,lac,ci,arfcn,bsic
1199 +EEMUMTSINTERRAT: 0, -32768, -107, -1, -1, 0, 0, 65534, 0, 117, 36
1200 */
1201 else if(strStartsWith(s, "+EEMUMTSINTERRAT:")) // WCDMA RATÐ¡ÇøÐÅÏ¢
1202 {
1203 // lac, ci, arfcn
1204 if(cell_info.running) {
1205 int tmp_int;
1206 int i = 0;
1207 char* tmp_s = memdup(s,strlen(s));
1208 char* free_ptr = tmp_s;
1209 char *line = tmp_s;
1210 if (at_tok_start(&line) < 0)
1211 {
1212 goto EEMUMTSINTERRAT_EXIT;
1213 }
1214 // Jump 7 integer.
1215 i = 0;
1216 while(i < 7) {
1217 if (at_tok_nextint(&line, &tmp_int) < 0)
1218 {
1219 goto EEMUMTSINTERRAT_EXIT;
1220 }
1221 i++;
1222 }
1223
1224 // lac
1225 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int <= 0 || tmp_int >= 65536)
1226 {
1227 goto EEMUMTSINTERRAT_EXIT;
1228 }
1229 cell_info.cell[cell_info.cell_num].value1 = (uint32)tmp_int;
1230
1231 // ci
1232 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int <= 0 || tmp_int >= 65536)
1233 {
1234 goto EEMUMTSINTERRAT_EXIT;
1235 }
1236 cell_info.cell[cell_info.cell_num].value2 = (uint32)tmp_int;
1237
1238 // arfcn
1239 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int <= 0 || tmp_int >= 65536)
1240 {
1241 goto EEMUMTSINTERRAT_EXIT;
1242 }
1243 cell_info.cell[cell_info.cell_num].value3 = (uint32)tmp_int;
1244
1245 cell_info.cell_num++;
1246EEMUMTSINTERRAT_EXIT:
1247 free(free_ptr);
1248 }
1249 }
1250 // GSM
1251 // +EEMGINFOBASIC: 2
1252 // Do nothing.
1253 else if(strStartsWith(s, "+EEMGINFOBASIC:")) // Basic information in GSM
1254 // 0: ME in Idle mode 1: ME in Dedicated mode 2: ME in PS PTM mode
1255 {
1256 if(cell_info.running) {
1257
1258 }
1259 }
1260 /*
1261 // mcc, mnc_len, mnc, lac, ci, nom, nco,
1262 // bsic, C1, C2, TA, TxPwr,
1263 // RxSig, RxSigFull, RxSigSub, RxQualFull, RxQualSub,
1264 // ARFCB_tch, hopping_chnl, chnl_type, TS, PacketIdle, rac, arfcn,
1265 // bs_pa_mfrms, C31, C32, t3212, t3312, pbcch_support, EDGE_support,
1266 // ncc_permitted, rl_timeout, ho_count, ho_succ, chnl_access_count, chnl_access_succ_count,
1267 // gsmBand,channelMode
1268 +EEMGINFOSVC: 1120, 2, 0, 32784, 24741, 2, 0,
1269 63, 36, 146, 1, 7,
1270 46, 42, 42, 7, 0,
1271 53, 0, 8, 0, 1, 6, 53,
1272 2, 0, 146, 42, 54, 0, 1,
1273 1, 32, 0, 0, 0, 0,
1274 0, 0
1275 */
1276 else if(strStartsWith(s, "+EEMGINFOSVC:")) // GSM Server Cell
1277 {
1278 // lac, ci, arfcn, bsic
1279 LOG("+EEMGINFOSVC: 1= %d\n.",cell_info.running);
1280 if(cell_info.running) {
1281 int tmp_int;
1282 int i = 0;
1283 char* tmp_s = memdup(s,strlen(s));
1284 char* free_ptr = tmp_s;
1285 char *line = tmp_s;
1286 if (at_tok_start(&line) < 0)
1287 {
1288 goto EEMGINFOSVC_EXIT;
1289 }
1290
1291 // mcc
1292 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int <= 0 || tmp_int >= 65536)
1293 {
1294 goto EEMGINFOSVC_EXIT;
1295 }
1296 cell_info.cell[cell_info.cell_num].value5 = (uint32)tmp_int;
1297
1298 //mnc_len
1299 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int <= 0 || tmp_int >= 65536)
1300 {
1301 goto EEMGINFOSVC_EXIT;
1302 }
1303 // mnc
1304 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int <= 0 || tmp_int >= 65536)
1305 {
1306 goto EEMGINFOSVC_EXIT;
1307 }
1308 cell_info.cell[cell_info.cell_num].value6 = (uint32)tmp_int;
1309
1310 /*
1311 // Jump 3 integer.
1312 i = 0;
1313 while(i < 3) {
1314 if (at_tok_nextint(&line, &tmp_int) < 0)
1315 {
1316 goto EEMGINFOSVC_EXIT;
1317 }
1318 i++;
1319 }
1320 */
1321 // lac
1322 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1323 {
1324 goto EEMGINFOSVC_EXIT;
1325 }
1326 cell_info.cell[cell_info.cell_num].value1 = (uint32)tmp_int;
1327
1328 // ci
1329 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1330 {
1331 goto EEMGINFOSVC_EXIT;
1332 }
1333 cell_info.cell[cell_info.cell_num].value2 = (uint32)tmp_int;
1334
1335 // Jump 2 integer.
1336 i = 0;
1337 while(i < 2) {
1338 if (at_tok_nextint(&line, &tmp_int) < 0)
1339 {
1340 goto EEMGINFOSVC_EXIT;
1341 }
1342 i++;
1343 }
1344
1345 // bsic
1346 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1347 {
1348 goto EEMGINFOSVC_EXIT;
1349 }
1350 cell_info.cell[cell_info.cell_num].value4 = (uint32)tmp_int;
1351
1352 // Jump 15 integer.
1353 i = 0;
1354 while(i < 15) {
1355 if (at_tok_nextint(&line, &tmp_int) < 0)
1356 {
1357 goto EEMGINFOSVC_EXIT;
1358 }
1359 i++;
1360 }
1361
1362 // arfcn
1363 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1364 {
1365 goto EEMGINFOSVC_EXIT;
1366 }
1367 cell_info.cell[cell_info.cell_num].value3 = (uint32)tmp_int;
1368
1369 cell_info.cell_num++;
1370EEMGINFOSVC_EXIT:
1371 free(free_ptr);
1372 }
1373 }
1374 /*
1375 // PS_attached, attach_type, service_type, tx_power, c_value,
1376 // ul_ts, dl_ts, ul_cs, dl_cs, ul_modulation, dl_modulation,
1377 // gmsk_cv_bep, 8psk_cv_bep, gmsk_mean_bep, 8psk_mean_bep, EDGE_bep_period, single_gmm_rej_cause
1378 // pdp_active_num, mac_mode, network_control, network_mode, EDGE_slq_measurement_mode, edge_status
1379 +EEMGINFOPS: 1, 255, 0, 0, 0,
1380 0, 0, 268435501, 1, 0, 0,
1381 4, 0, 96, 0, 0, 0,
1382 0, 0, 0, 65535, 0, 13350
1383 */
1384 // Do nothing.
1385 else if(strStartsWith(s, "+EEMGINFOPS:")) // PSÐÅÏ¢
1386 {
1387 if(cell_info.running) {
1388
1389 }
1390 }
1391 else if(strStartsWith(s, "+EEMGINFONC:")) // cell
1392 {
1393 if(cell_info.running) {
1394 int tmp_int;
1395 int i = 0;
1396 char* tmp_s = memdup(s,strlen(s));
1397 char* free_ptr = tmp_s;
1398 char *line = tmp_s;
1399 if (at_tok_start(&line) < 0)
1400 {
1401 goto EEMGINFOPS_EXIT;
1402 }
1403
1404 // nc_num
1405 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1406 {
1407 LOG("cell_info.running 1= %d\n.",cell_info.running);
1408 goto EEMGINFOPS_EXIT;
1409 }
1410 // mcc
1411 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1412 {
1413 LOG("cell_info.running 2= %d\n.",cell_info.running);
1414 goto EEMGINFOPS_EXIT;
1415 }
1416 cell_info.cell[cell_info.cell_num].value5 = (uint32)tmp_int;
1417
1418 // mnc
1419 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1420 {
1421 LOG("cell_info.running 3= %d\n.",cell_info.running);
1422 goto EEMGINFOPS_EXIT;
1423 }
1424 cell_info.cell[cell_info.cell_num].value6 = (uint32)tmp_int;
1425
1426 // lac
1427 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1428 {
1429 LOG("cell_info.running 4= %d\n.",cell_info.running);
1430 goto EEMGINFOPS_EXIT;
1431 }
1432 cell_info.cell[cell_info.cell_num].value1 = (uint32)tmp_int;
1433
1434 // rac
1435 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1436 {
1437 LOG("cell_info.running 5= %d\n.",cell_info.running);
1438 goto EEMGINFOPS_EXIT;
1439 }
1440
1441 // ci
1442 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1443 {
1444 LOG("cell_info.running 6= %d\n.",cell_info.running);
1445 goto EEMGINFOPS_EXIT;
1446 }
1447 cell_info.cell[cell_info.cell_num].value2 = (uint32)tmp_int;
1448
1449 // rx_lv
1450 if (at_tok_nextint(&line, &tmp_int) < 0)
1451 {
1452 LOG("cell_info.running 7= %d\n.",cell_info.running);
1453 goto EEMGINFOPS_EXIT;
1454 }
1455
1456 // bsic
1457 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1458 {
1459 LOG("cell_info.running 8= %d\n.",cell_info.running);
1460 goto EEMGINFOPS_EXIT;
1461 }
1462 cell_info.cell[cell_info.cell_num].value4 = (uint32)tmp_int;
1463
1464 // Jump 2 integer.
1465 i = 0;
1466 while(i < 2) {
1467 if (at_tok_nextint(&line, &tmp_int) < 0)
1468 {
1469 LOG("cell_info.running 9= %d\n.",cell_info.running);
1470 goto EEMGINFOPS_EXIT;
1471 }
1472 i++;
1473 }
1474
1475 // arfcn
1476 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1477 {
1478 LOG("cell_info.running 10 = %d\n.",cell_info.running);
1479 goto EEMGINFOPS_EXIT;
1480 }
1481 cell_info.cell[cell_info.cell_num].value3 = (uint32)tmp_int;
1482
1483 cell_info.cell_num++;
1484EEMGINFOPS_EXIT:
1485 free(free_ptr);
1486 }
1487 }
1488 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"
1489 {
1490
1491 }
1492 else
1493 {
1494 LOGV("Unknown URC : %s", s);
1495 }
1496}
1497
1498static int openSocket(const char* sockname)
1499{
1500 int sock = socket(AF_UNIX, SOCK_STREAM, 0);
1501 if (sock < 0)
1502 {
1503 LOGE("Error create socket: %s\n", strerror(errno));
1504 return -1;
1505 }
1506 struct sockaddr_un addr;
1507 memset(&addr, 0, sizeof(addr));
1508 addr.sun_family = AF_UNIX;
1509 strncpy(addr.sun_path, sockname, sizeof(addr.sun_path));
1510 while (TEMP_FAILURE_RETRY(connect(sock,(const struct sockaddr*)&addr, sizeof(addr))) != 0)
1511 {
1512 LOGE("Error connect to socket %s: %s, try again", sockname, strerror(errno));
1513 sleep(1);
1514 }
1515
1516#if 0
1517 int sk_flags = fcntl(sock, F_GETFL, 0);
1518 fcntl(sock, F_SETFL, sk_flags | O_NONBLOCK);
1519#endif
1520
1521 return sock;
1522}
1523
1524static void ril_get_cgpaddr_ip_process()
1525{
1526 int err, skip;
1527 ATResponse *p_response = NULL;
1528 char *line;
1529 char *ipv4 = NULL, *ipv6 = NULL;
1530 err = at_send_command_singleline("AT+CGPADDR", "+CGPADDR:", &p_response);
1531 if ((err < 0) || (p_response == NULL) || (p_response->success == 0))
1532 {
1533 LOGE("+CGPADDR exec error.");
1534 goto error;
1535 }
1536
1537 // +CGPADDR: 1, "10.51.59.229", "254.128.0.0.0.0.0.0.0.1.0.0.111.176.63.99"
1538 // +CGPADDR: 1, "10.124.139.131"
1539 line = p_response->p_intermediates->line;
1540 err = at_tok_start(&line);
1541 if (err < 0)
1542 {
1543 goto error;
1544 }
1545
1546 err = at_tok_nextint(&line, &skip);
1547 if (err < 0)
1548 {
1549 goto error;
1550 }
1551
1552 if (!at_tok_hasmore(&line))
1553 {
1554 goto error;
1555 }
1556
1557 err = at_tok_nextstr(&line, &ipv4);
1558 if (err < 0)
1559 {
1560 LOGE("Get IPv4 fail.");
1561 goto error;
1562 }
1563
1564 if (at_tok_hasmore(&line))
1565 {
1566 err = at_tok_nextstr(&line, &ipv6);
1567 if (err < 0)
1568 {
1569 LOGE("Get IPv6 fail.");
1570 goto error;
1571 }
1572 }
1573 else
1574 {
1575 LOGD("No IPv6 Found.");
1576 }
1577
1578 if(ipv6)
1579 {
1580 LOGD("IPv6 : %s", ipv6);
1581 }
1582
1583 if(ipv4)
1584 {
1585 LOGD("IPv4 : %s", ipv4);
1586
1587// ril_net_dev_config("ccinet0", ipv4, NULL);
1588 }
1589error:
1590 at_response_free(p_response);
1591}
1592
1593static void sim_state_change(bool plug_in) {
1594 if(plug_in) {
1595 // If radio on,must off in the first.
1596 if(net_info.radio_state == MBTK_RADIO_STATE_ON) {
1597 setRadioPower(0);
1598 }
1599 setRadioPower(1);
1600 } else {
1601 setRadioPower(0);
1602 }
1603}
1604
1605static int open_uevent_socket()
1606{
1607 struct sockaddr_nl addr;
1608 int sz = 64*1024;
1609 int s = 0;
1610
1611 memset(&addr, 0, sizeof(addr));
1612 addr.nl_family = AF_NETLINK;
1613 addr.nl_pid = getpid();
1614 addr.nl_groups = 0xffffffff;
1615
1616 s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
1617 if (s < 0)
1618 {
1619 LOGE("socket() fail.[%d]", errno);
1620 return -1;
1621 }
1622
1623 setsockopt(s, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz));
1624
1625 if (bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0)
1626 {
1627 close(s);
1628 return -1;
1629 }
1630
1631 return s;
1632}
1633
1634static void parse_uevent(const char *msg, int msg_len, struct cooling_device *cdev)
1635{
1636 // change@/devices/virtual/usim_event/usim0\0
1637 // ACTION=change\0
1638 // DEVPATH=/devices/virtual/usim_event/usim0\0
1639 // SUBSYSTEM=usim_event\0
1640 // USIM_NAME=usim0\0
1641 // USIM_EVENT=plugout\0
1642 // SEQNUM=704
1643 int i = 0;
1644 while (i < msg_len)
1645 {
1646 if(*(msg + i) == '\0')
1647 {
1648 i++;
1649 continue;
1650 }
1651 if (!strncmp(msg + i, "USIM_NAME=", 10))
1652 {
1653 i += 10;
1654 cdev->name = msg + i;
1655 i += strlen(msg + i);
1656 }
1657 else if (!strncmp(msg + i, "ACTION=", 7))
1658 {
1659 i += 7;
1660 cdev->action = msg + i;
1661 i += strlen(msg + i);
1662 }
1663 else if (!strncmp(msg + i, "DEVPATH=", 8))
1664 {
1665 i += 8;
1666 cdev->path = msg + i;
1667 i += strlen(msg + i);
1668 }
1669 else if (!strncmp(msg + i, "USIM_EVENT=", 11))
1670 {
1671 i += 11;
1672 cdev->event = msg + i;
1673 i += strlen(msg + i);
1674 }
1675 else if (!strncmp(msg + i, "SUBSYSTEM=", 10))
1676 {
1677 i += 10;
1678 cdev->subsystem = msg + i;
1679 i += strlen(msg + i);
1680 }
1681 else
1682 {
1683 i++;
1684 }
1685 }
1686
1687 if(!strncmp(cdev->path, UEVENT_USIM_DEV, sizeof(UEVENT_USIM_DEV))
1688 && !strncmp(cdev->action, "change", 5))
1689 {
1690 LOGD("event { name=%s, action=%s, path=%s, subsystem=%s, event=%s}",
1691 cdev->name, cdev->action, cdev->path, cdev->subsystem, cdev->event);
1692 if(!strcmp(cdev->event, "plugout"))
1693 {
1694 sim_state_change(FALSE);
1695 }
1696 else if(!strcmp(cdev->event, "plugin"))
1697 {
1698 sim_state_change(TRUE);
1699 }
1700 else
1701 {
1702 LOGE("usim evnet error!");
1703 }
1704 }
1705}
1706
1707static void* uevnet_run(void *payload)
1708{
1709 int socket_fd = -1;
1710 char msg[BUFFER_SIZE+2];
1711 int n;
1712
1713 socket_fd = open_uevent_socket();
1714 if(socket_fd > 0)
1715 {
1716 while(1)
1717 {
1718 if((n = recv(socket_fd, msg, BUFFER_SIZE, 0)) > 0)
1719 {
1720 struct cooling_device cdev;
1721 memset(&cdev, 0x0, sizeof(cdev));
1722
1723 if(n == BUFFER_SIZE)
1724 continue;
1725 msg[n] = '\0';
1726 // 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
1727 log_hex("UEVENT", msg, n);
1728 parse_uevent(msg, n, &cdev);
1729 }
1730 else
1731 {
1732 LOGE("recv msg error.");
1733 }
1734 }
1735 }
1736
1737 LOGD("UEVENT Thread exit!!!");
1738 return NULL;
1739}
1740
1741int uevent_main()
1742{
1743 mbtk_task_info task;
1744 task.task_id = &uevnet_task_id;
1745 task.thread_run = uevnet_run;
1746 task.args = NULL;
1747 return mbtk_task_start(&task);
1748}
1749
1750/*
1751int ril_main()
1752{
1753 return mbtk_task_queue_start(&ril_task, ril_main_run);
1754}
1755*/
1756
1757int mbtk_info_server_start();
1758int InProduction_Mode(void);
1759void server_ready_set(void);
1760
1761
1762/*
1763 *Get mtdblock which name is ASR_FLAG
1764 *return path if found, else return NULL
1765 */
1766static int asrFlagPathGet(char *asr_flag_path)
1767{
1768 char buf[128];
1769 unsigned char find = 0;
1770 FILE *fd = fopen("/proc/mtd", "r");
1771 if (fd == NULL) {
1772 LOGE("Open MTD failed!");
1773 return -1;
1774 }
1775
1776 memset(buf, '\0', 128);
1777 while (fgets(buf, 128, fd) != NULL) {
1778 if(strstr(buf, "asr_flag")) {
1779 char *p = strstr(buf, "mtd");
1780 if(p)
1781 {
1782 int bln;
1783 sscanf(p, "mtd%d", &bln);
1784 sprintf(asr_flag_path, "/dev/mtdblock%d", bln);
1785 find = 1;
1786 break;
1787 }
1788 }
1789 memset(buf, '\0', 128);
1790 }
1791
1792 fclose(fd);
1793 return ((find == 1) ? 0 : -1);
1794}
1795
1796static int readFromMTD(const char *path, unsigned int offset, void *buf, int size)
1797{
1798 int ret, fd;
1799 if (!path)
1800 return -1;
1801
1802 fd = open(path, O_RDONLY);
1803 if (fd < 0) {
1804 LOGE("readFromMTD open error,%d", errno);
1805 return -1;
1806 }
1807
1808 ret = lseek(fd, offset, SEEK_SET);
1809 if (ret < 0) {
1810 close(fd);
1811 LOGE("readFromMTD lseek error,%d", errno);
1812 return -1;
1813 }
1814 ret = read(fd, buf, size);
1815 if (ret < 0) {
1816 close(fd);
1817 LOGE("readFromMTD read error,%d", errno);
1818 return -1;
1819 }
1820 close(fd);
1821 return 0;
1822}
1823
1824static int writeToMTD(const char *path, unsigned int offset, void *buf, int size)
1825{
1826 int ret, fd;
1827
1828 if (!path)
1829 return -1;
1830
1831 fd = open(path, O_RDWR | O_SYNC);
1832 if (fd < 0)
1833 return -1;
1834
1835 ret = lseek(fd, offset, SEEK_SET);
1836 if (ret < 0) {
1837 close(fd);
1838 return -1;
1839 }
1840 ret = write(fd, buf, size);
1841 if (ret < 0) {
1842 LOGE("writetomtd:write error:%d", errno);
1843 close(fd);
1844 return -1;
1845 }
1846
1847 close(fd);
1848 return 0;
1849}
1850
1851static void fota_result_check()
1852{
1853#if 0
1854 ASR_flag tag;
1855 char asr_flag_path[30] = {0};
1856 if(asrFlagPathGet(asr_flag_path)) {
1857 LOGE("getAsrFlagPath() fail.");
1858 return;
1859 }
1860
1861 if(readFromMTD(asr_flag_path, ASR_FLAG_OFFSET, &tag, sizeof(tag)) < 0)
1862 {
1863 LOGE("Get FOTA result fail.");
1864 }
1865 else
1866 {
1867 LOGD("FOTA result : %d, %d", tag.fota_result[0], tag.fota_result[1]);
1868 tag.fota_result[0] = 0;
1869 tag.fota_result[1] = 0;
1870 if(writeToMTD(asr_flag_path, ASR_FLAG_OFFSET, &tag, sizeof(tag)) < 0)
1871 {
1872 LOGE("FOTA result update fail.");
1873 } else {
1874 LOGD("FOTA result update success.");
1875 }
1876 }
1877#endif
1878}
1879
b.liubb590492024-06-13 16:42:08 +08001880static void mbtk_ril_ready()
liubin281ac462023-07-19 14:22:54 +08001881{
1882 // /etc/init.d/mbtk_boot_server_ready
b.liubb590492024-06-13 16:42:08 +08001883 if(is_first_boot) {
1884 if(access(MBTK_BOOT_SERVER_READY , X_OK) == 0) {
1885 LOGD("Exec : %s", MBTK_BOOT_SERVER_READY);
1886 system(MBTK_BOOT_SERVER_READY);
1887 } else {
1888 LOGE("%s can not exec.", MBTK_BOOT_SERVER_READY);
1889 }
liubin281ac462023-07-19 14:22:54 +08001890 } else {
b.liubb590492024-06-13 16:42:08 +08001891 LOGD("No exec : %s", MBTK_BOOT_SERVER_READY);
liubin281ac462023-07-19 14:22:54 +08001892 }
1893}
1894
1895#if 1
1896int main(int argc, char *argv[])
1897{
1898 mbtk_log_init("radio", "MBTK_RIL");
b.liubb590492024-06-13 16:42:08 +08001899
1900#ifdef MBTK_DUMP_SUPPORT
1901 mbtk_debug_open(NULL, TRUE);
1902#endif
1903
1904// Using Killall,the file lock may be not release.
1905#if 0
1906 if(app_already_running(MBTK_RILD_PID_FILE)) {
1907 LOGW("daemon already running.");
1908 exit(1);
1909 }
1910#endif
1911
liubin281ac462023-07-19 14:22:54 +08001912 LOGI("mbtk_ril start.");
1913
b.liubb590492024-06-13 16:42:08 +08001914 int fd = open(MBTK_RILD_TEMP_FILE, O_CREAT | O_RDWR, 0644);
1915 if(fd > 0) {
1916 char buff[10] = {0};
1917 int count = 0;
1918 if(read(fd, buff, sizeof(buff)) > 0) {
1919 count = atoi(buff);
1920 } else {
1921 count = 0;
1922 }
1923
1924 if(count <= 0) {
1925 is_first_boot = TRUE;
1926 count = 0;
1927 } else {
1928 is_first_boot = FALSE;
1929 }
1930
1931 count++;
1932 memset(buff, 0, sizeof(buff));
1933 snprintf(buff, sizeof(buff), "%d", count);
1934 write(fd, buff, strlen(buff));
1935 close(fd);
1936 } else {
1937 is_first_boot = FALSE;
1938 LOGE("Open %s fail:%d", MBTK_RILD_TEMP_FILE, errno);
1939 LOGW("Will not exec %s and %s.", MBTK_BOOT_SERVER_READY, MBTK_BOOT_NET_READY);
1940 }
1941
liubin281ac462023-07-19 14:22:54 +08001942 if(InProduction_Mode()) {
1943 LOGI("Is Production Mode, will exit...");
1944 exit(0);
1945 }
1946
1947 int at_sock = openSocket("/tmp/atcmd_at");
1948 if(at_sock < 0)
1949 {
1950 LOGE("Open AT Socket Fail[%d].", errno);
1951 return -1;
1952 }
1953 int uart_sock = openSocket("/tmp/atcmd_urc");
1954 if(uart_sock < 0)
1955 {
1956 LOGE("Open Uart Socket Fail[%d].", errno);
1957 return -1;
1958 }
1959
1960 at_set_on_reader_closed(onATReaderClosed);
1961 at_set_on_timeout(onATTimeout);
1962
1963 if(at_open(at_sock, uart_sock, onUnsolicited))
1964 {
1965 LOGE("Start AT thread fail.");
1966 return -1;
1967 }
1968
1969#if 1
1970 if(at_handshake())
1971 {
1972 LOGE("AT handshake fail.");
1973 return -1;
1974 }
1975#endif
1976
1977 LOGD("AT OK.");
1978
1979 if(mbtk_info_server_start())
1980 {
1981 LOGE("mbtk_info_server_start() fail.");
1982 return -1;
1983 }
b.liubb590492024-06-13 16:42:08 +08001984
wangyouqiang38e53362024-01-23 10:53:48 +08001985#ifdef MBTK_PROJECT_T108
1986 mbtk_led_init();
b.liubb590492024-06-13 16:42:08 +08001987#endif
liubin281ac462023-07-19 14:22:54 +08001988
1989#if 0
1990 if(uevent_main())
1991 {
1992 LOGE("Start uevent thread fail.");
1993 return -1;
1994 }
1995#endif
1996
1997 char time_type[10];
1998 memset(time_type, 0, 10);
1999 property_get("persist.mbtk.time_type", time_type, "0");
2000 if(atoi(time_type) == MBTK_TIME_TYPE_NTP) { // NTP time
2001 LOG("Start NTP thread.");
2002 ntp_thread_start();
2003 }
2004
2005 fota_result_check();
2006
b.liubb590492024-06-13 16:42:08 +08002007 mbtk_ril_ready();
liubin281ac462023-07-19 14:22:54 +08002008
2009 server_ready_set();//Set the server readiness state
2010 while(1)
2011 {
2012 sleep(24 * 60 * 60);
2013 }
2014
2015 LOGD("!!!mbtk_ril exit!!!");
2016 return 0;
2017}
2018
2019#else
2020int main()
2021{
2022 char buff[BUFFER_SIZE + 1] = {0};
2023 if(!mbtk_at("AT+CFUN=1", buff, BUFFER_SIZE))
2024 {
2025 LOGD("+CFUN RSP:%s", buff);
2026 while(1)
2027 {
2028 sleep(1);
2029 memset(buff, 0x0, BUFFER_SIZE + 1);
2030 if(!mbtk_at("AT+CGPADDR", buff, BUFFER_SIZE))
2031 {
2032 LOGD("+CGPADDR RSP:%s", buff);
2033 if(strstr(buff, "+CGPADDR:"))
2034 {
2035 // +CGPADDR: 1, "10.99.223.168", "254.128.0.0.0.0.0.0.0.1.0.0.117.9.250.59"
2036 //
2037 // OK
2038 char *ip_start = NULL;
2039 char *ip_end = NULL;
2040 char ipv4[50] = {0};
2041 ip_start = strstr(buff,"\"");
2042 if(ip_start)
2043 ip_end = strstr(ip_start + 1, "\"");
2044 if(ip_start && ip_end && ip_end - ip_start - 1 > 0)
2045 {
2046 memcpy(ipv4, ip_start + 1, ip_end - ip_start - 1);
2047 LOGD("IP : %s", ipv4);
2048 if(!mbtk_ifc_open())
2049 {
2050 in_addr_t addr;
2051 inet_aton(ipv4,(struct in_addr *)&addr);
2052 LOGD("IP : %s -> %x", ipv4, addr);
2053 if(!mbtk_ifc_set_addr("ccinet0", addr, 0))
2054 {
2055 mbtk_ifc_up("ccinet0");
2056 }
2057
2058 mbtk_ifc_close();
2059 }
2060
2061 system("route add default dev ccinet0");
2062
2063 LOGD("Set IP success.");
2064 }
2065 else
2066 {
2067 LOGD("Get IP fail.");
2068 }
2069
2070 break;
2071 }
2072 }
2073 }
2074 }
2075
2076 return 0;
2077}
2078#endif