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