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