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