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