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