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