blob: 45a30f656017e1c180eca814197487eaf239b551 [file] [log] [blame]
b.liu87afc4c2024-08-14 17:33:45 +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#include <signal.h>
16#include <sys/epoll.h>
17#include <pthread.h>
18
19
20//#include "cploader.h"
21#include "mbtk_log.h"
22#include "mbtk_ifc.h"
23#include "mbtk_type.h"
24#include "atchannel.h"
25#include "at_tok.h"
26#include "mbtk_utils.h"
27#include "mbtk_task.h"
28#include "ril_info.h"
29#include "mbtk_ntp.h"
30#include "mbtk_net_control.h"
31#include "mbtk_ril.h"
32#include "mbtk_str.h"
33#include "mbtk_queue.h"
34
35#define TEMP_FAILURE_RETRY(exp) ({ \
36 typeof (exp) _rc; \
37 do { \
38 _rc = (exp); \
39 } while (_rc == -1 && errno == EINTR); \
40 _rc; })
41
42#define BUFFER_SIZE 2048
43#define UEVENT_USIM_DEV "/devices/virtual/usim_event/usim0"
44#define MBTK_BOOT_SERVER_READY "/etc/init.d/mbtk_boot_server_ready"
45#define MBTK_BOOT_NET_READY "/etc/init.d/mbtk_boot_net_ready"
46#define MBTK_RILD_PID_FILE "/var/run/mbtk_rild.pid"
47#define MBTK_RILD_FILE_NET_READY "/tmp/mbtk_rild.net_ready"
48#define MBTK_RILD_FILE_SER_READY "/tmp/mbtk_rild.ser_ready"
49
50static bool ril_net_ready = FALSE; // Only one time.
51static bool ril_server_ready = FALSE; // Only one time.
52ril_band_info_t band_info;
53ril_info_t ril_info;
54
55// int urc_msg_distribute(bool async_process, info_urc_msg_id_enum msg, void *data, int data_len);
56// int mbtk_signal_log(char *data);
57int InProduction_Mode(void);
58mbtk_ril_err_enum dev_pack_req_process(sock_cli_info_t* cli_info, ril_msg_pack_info_t* pack);
59mbtk_ril_err_enum call_pack_req_process(sock_cli_info_t* cli_info, ril_msg_pack_info_t* pack);
60mbtk_ril_err_enum sim_pack_req_process(sock_cli_info_t* cli_info, ril_msg_pack_info_t* pack);
61mbtk_ril_err_enum net_pack_req_process(sock_cli_info_t* cli_info, ril_msg_pack_info_t* pack);
62mbtk_ril_err_enum pb_pack_req_process(sock_cli_info_t* cli_info, ril_msg_pack_info_t* pack);
63mbtk_ril_err_enum sms_pack_req_process(sock_cli_info_t* cli_info, ril_msg_pack_info_t* pack);
64
65/* Called on command thread */
66static void onATTimeout()
67{
68 LOGI("AT channel timeout; closing\n");
69 at_close();
70}
71
72/* Called on command or reader thread */
73static void onATReaderClosed()
74{
75 LOGI("AT channel closed\n");
76 at_close();
77}
78
79static void sock_cli_free_func(void *data)
80{
81 if (data)
82 {
83 sock_cli_info_t *info = (sock_cli_info_t*) data;
84 LOGD("Free Socket client[fd = %d].", info->fd);
85 free(info);
86 }
87}
88
89/*
90* Will exec mbtk_boot_server_ready and mbtk_boot_net_ready if is_first_boot is true.
91*/
92static int net_ready_set()
93{
94 int ret = -1;
95 int fd = open(MBTK_RILD_FILE_NET_READY, O_CREAT | O_WRONLY | O_TRUNC, 0644);
96 if(fd > 0) {
97 if(write(fd, "1", 1) == 1) {
98 ret = 0;
99 ril_net_ready = TRUE;
100 }
101 close(fd);
102 } else {
103 LOGE("Open %s fail:%d", MBTK_RILD_FILE_NET_READY, errno);
104 }
105
106 return ret;
107}
108
109/*
110* Will exec mbtk_boot_server_ready and mbtk_boot_net_ready if is_first_boot is true.
111*/
112static int ser_ready_set()
113{
114 int ret = -1;
115 int fd = open(MBTK_RILD_FILE_SER_READY, O_CREAT | O_WRONLY | O_TRUNC, 0644);
116 if(fd > 0) {
117 if(write(fd, "1", 1) == 1) {
118 ret = 0;
119 ril_server_ready = TRUE;
120 }
121 close(fd);
122 } else {
123 LOGE("Open %s fail:%d", MBTK_RILD_FILE_SER_READY, errno);
124 }
125
126 return ret;
127}
128
129/*
130* Will exec mbtk_boot_server_ready and mbtk_boot_net_ready if is_first_boot is true.
131*/
132static void ready_state_update()
133{
134 int fd = open(MBTK_RILD_FILE_NET_READY, O_RDONLY, 0644);
135 char buff[10];
136 if(fd > 0) {
137 if(read(fd, buff, sizeof(buff)) > 0) {
138 ril_net_ready = TRUE;
139 } else {
140 ril_net_ready = FALSE;
141 }
142
143 close(fd);
144 } else {
145 ril_net_ready = FALSE;
146 LOGE("Open %s fail:%d", MBTK_RILD_FILE_NET_READY, errno);
147 }
148
149 fd = open(MBTK_RILD_FILE_SER_READY, O_RDONLY, 0644);
150 if(fd > 0) {
151 if(read(fd, buff, sizeof(buff)) > 0) {
152 ril_server_ready = TRUE;
153 } else {
154 ril_server_ready = FALSE;
155 }
156
157 close(fd);
158 } else {
159 ril_server_ready = FALSE;
160 LOGE("Open %s fail:%d", MBTK_RILD_FILE_SER_READY, errno);
161 }
162}
163
164static void mbtk_net_ready()
165{
166 // /etc/init.d/mbtk_boot_net_ready
167 if(!ril_net_ready) {
168 if(access(MBTK_BOOT_NET_READY , X_OK) == 0) {
169 LOGD("Exec : %s", MBTK_BOOT_NET_READY);
170 system(MBTK_BOOT_NET_READY);
171 } else {
172 LOGE("%s can not exec.", MBTK_BOOT_NET_READY);
173 }
174 net_ready_set();
175 } else {
176 LOGD("No exec : %s", MBTK_BOOT_NET_READY);
177 }
178}
179
180static void mbtk_ril_ready()
181{
182 // /etc/init.d/mbtk_boot_server_ready
183 if(!ril_server_ready) {
184 if(access(MBTK_BOOT_SERVER_READY , X_OK) == 0) {
185 LOGD("Exec : %s", MBTK_BOOT_SERVER_READY);
186 system(MBTK_BOOT_SERVER_READY);
187 } else {
188 LOGE("%s can not exec.", MBTK_BOOT_SERVER_READY);
189 }
190 ser_ready_set();
191 } else {
192 LOGD("No exec : %s", MBTK_BOOT_SERVER_READY);
193 }
194}
195
196static sock_cli_info_t* cli_find(int fd)
197{
198 sock_cli_info_t *result = NULL;
199 list_first(ril_info.sock_client_list);
200 while ((result = (sock_cli_info_t*) list_next(ril_info.sock_client_list)))
201 {
202 if (result->fd == fd)
203 return result;
204 }
205
206 return NULL;
207}
208
209static void cli_close(sock_cli_info_t* client)
210{
211 struct epoll_event ev;
212 memset(&ev,0,sizeof(struct epoll_event));
213 ev.data.fd = client->fd;
214 ev.events = EPOLLIN | EPOLLERR | EPOLLET;
215 epoll_ctl(ril_info.epoll_fd, EPOLL_CTL_DEL, client->fd, &ev);
216
217 close(client->fd);
218
219 if(list_remove(ril_info.sock_client_list, client))
220 {
221 sock_cli_free_func(client);
222 }
223}
224
225static void ril_error_pack_send(int fd, int ril_id, int msg_index, int err)
226{
227 ril_msg_pack_info_t* pack = ril_msg_pack_creat(RIL_MSG_TYPE_RSP, ril_id, msg_index, NULL, 0);
228 if(pack)
229 {
230 pack->err = (uint16)err;
231 ril_pack_send(fd, pack);
232 ril_msg_pack_free(pack);
233 }
234 else
235 {
236 LOGW("ril_msg_pack_creat() fail.");
237 }
238}
239
240void ril_rsp_pack_send(int fd, int ril_id, int msg_index, const void* data, int data_len)
241{
242 ril_msg_pack_info_t* pack = ril_msg_pack_creat(RIL_MSG_TYPE_RSP, ril_id, msg_index, data, data_len);
243 if(pack)
244 {
245 pack->err = (uint16)MBTK_RIL_ERR_SUCCESS;
246#if 0
247 if(data != NULL && data_len > 0)
248 {
249 pack->data_len = (uint16)data_len;
250 pack->data = (uint8*)mbtk_memcpy(data, data_len);
251 }
252#endif
253 ril_pack_send(fd, pack);
254 ril_msg_pack_free(pack);
255 }
256 else
257 {
258 LOGW("ril_msg_pack_creat() fail.");
259 }
260}
261
262void ril_ind_pack_send(int fd, int msg_id, const void* data, int data_len)
263{
264 ril_msg_pack_info_t* pack = ril_msg_pack_creat(RIL_MSG_TYPE_IND, msg_id, RIL_MSG_INDEX_INVALID, data, data_len);
265 if(pack)
266 {
267 pack->err = (uint16)0;
268#if 0
269 if(data != NULL && data_len > 0)
270 {
271 pack->data_len = (uint16)data_len;
272 pack->data = (uint8*)mbtk_memcpy(data, data_len);
273 }
274#endif
275 ril_pack_send(fd, pack);
276 ril_msg_pack_free(pack);
277 }
278 else
279 {
280 LOGW("ril_msg_pack_creat() fail.");
281 }
282}
283
284
285static void onUnsolicited(const char *s, const char *sms_pdu)
286{
287 LOGD("URC : %s", s);
288#if 0
289 // MBTK_AT_READY
290 if (strStartsWith(s, "MBTK_AT_READY")) // AT ready.
291 {
292
293 }
294 else if(strStartsWith(s, "*RADIOPOWER:")) // "*RADIOPOWER: 1"
295 {
296 const char* ptr = s + strlen("*RADIOPOWER:");
297 while(*ptr != '\0' && *ptr == ' ' )
298 {
299 ptr++;
300 }
301
302 uint8 state;
303 if(*ptr == '1') {
304 //net_info.radio_state = MBTK_RADIO_STATE_ON;
305 // mbtk_radio_ready_cb();
306 state = (uint8)1;
307 } else {
308 //net_info.radio_state = MBTK_RADIO_STATE_OFF;
309 state = (uint8)0;
310 }
311 urc_msg_distribute(true, INFO_URC_MSG_RADIO_STATE, &state, sizeof(uint8));
312 }
313 // "CONNECT"
314 else if(strStartsWith(s, "CONNECT"))
315 {
316 if(cgact_wait.waitting && cgact_wait.act) {
317 cgact_wait.waitting = false;
318 }
319
320 uint8 data_pdp;
321 data_pdp = 1; //
322 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
323 }
324 // +CGEV:
325 // +CGEV: NW DEACT <cid>,<cid>
326 // +CGEV: ME DEACT <cid>,<cid>
327 // +CGEV: NW PDN DEACT <cid>
328 // +CGEV: ME PDN DEACT <cid>
329 // +CGEV: NW DETACH
330 // +CGEV: ME DETACH
331 //
332 // +CGEV: NW ACT <cid>,<cid>
333 // +CGEV: ME ACT <cid>,<cid>
334 // +CGEV: EPS PDN ACT <cid>
335 // +CGEV: ME PDN ACT <cid>,<reason>,<cid>
336 // +CGEV: ME PDN ACT <cid>,<reason>
337 // +CGEV: NW PDN ACT <cid>
338 // +CGEV: EPS ACT <cid>
339 // +CGEV: NW MODIFY <cid>,<reason>
340 // +CGEV: NW REATTACH
341 else if(strStartsWith(s, "+CGEV:"))
342 {
343 if(at_process) {
344 if(cgact_wait.act) {
345 if(strStartsWith(s, "+CGEV: ME PDN ACT ")) { // +CGEV: ME PDN ACT 15,4
346 if(cgact_wait.cid == atoi(s + 18)) {
347 cgact_wait.waitting = false;
348 }
349
350 uint8 data_pdp;
351 char* tmp_s = memdup(s + 18,strlen(s + 18));
352 char* free_ptr = tmp_s;
353 char *line = tmp_s;
354 int tmp_int;
355 if (at_tok_start(&line) < 0)
356 {
357 goto at_PDP_CREG_EXIT;
358 }
359 if (at_tok_nextint(&line, &tmp_int) < 0)
360 {
361 goto at_PDP_CREG_EXIT;
362 }
363 if (at_tok_nextint(&line, &tmp_int) < 0)
364 {
365 goto at_PDP_CREG_EXIT;
366 }
367 data_pdp = tmp_int;
368at_PDP_CREG_EXIT:
369 free(free_ptr);
370
371 //data_pdp = (uint8)atoi(s + 20); //reason
372 if(cgact_wait.cid >= 1 && cgact_wait.cid < 8)
373 {
374 if(data_pdp == 0)
375 {
376 data_pdp = 25;
377 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
378 //data_pdp = cgact_wait.cid + 200;
379 }
380 else if(data_pdp == 1)
381 {
382 data_pdp = 26;
383 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
384 }
385 else if(data_pdp == 2)
386 {
387 data_pdp = 27;
388 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
389 }
390 else if(data_pdp == 3)
391 {
392 data_pdp = 27;
393 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
394 }
395 else
396 {
397
398 }
399 if(cgact_wait.cid != 0)
400 {
401 data_pdp = cgact_wait.cid + 200;
402 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
403 }
404 }
405 } else if(strStartsWith(s, "+CGEV: NW MODIFY ")) { // +CGEV: NW MODIFY 1,4
406 if(cgact_wait.cid == atoi(s + 17)) {
407 cgact_wait.waitting = false;
408 }
409 }
410 } else {
411 if(strStartsWith(s, "+CGEV: ME PDN DEACT ")) { // +CGEV: ME PDN DEACT 1
412 if(cgact_wait.cid == atoi(s + 20)) {
413 cgact_wait.waitting = false;
414 }
415 uint8 data_pdp;
416 data_pdp = 0; //
417 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
418 if(cgact_wait.cid != 0)
419 {
420 data_pdp = cgact_wait.cid + 100;
421 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
422 }
423 }
424 }
425 } else {
426 // apn_state_set
427
428 // +CGEV: NW PDN DEACT <cid>
429
430 // +CGEV: EPS PDN ACT 1
431 // +CGEV: ME PDN ACT 8,1
432
433 // +CGEV: ME PDN ACT 2,4
434 uint8 data[2] = {0xFF};
435 if(strStartsWith(s, "+CGEV: NW PDN DEACT ")) { // +CGEV: NW PDN DEACT <cid>
436 //apn_state_set(atoi(s + 20), false);
437 data[0] = (uint8)0;
438 data[1] = (uint8)atoi(s + 20);
439
440 uint8 data_pdp;
441 data_pdp = 0; //
442 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
443 data_pdp = data[1] + 100;
444 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
445 } else if(strStartsWith(s, "+CGEV: EPS PDN ACT ")) { // +CGEV: EPS PDN ACT <cid>
446 //apn_state_set(atoi(s + 19), true);
447 data[0] = (uint8)1;
448 data[1] = (uint8)atoi(s + 19);
449 } else if(strStartsWith(s, "+CGEV: ME PDN DEACT ")) { // +CGEV: EPS PDN DEACT <cid>
450 //apn_state_set(atoi(s + 19), true);
451 data[0] = (uint8)0;
452 data[1] = (uint8)atoi(s + 20);
453
454 uint8 data_pdp;
455 data_pdp = 0; //
456 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
457 data_pdp = data[1] + 100;
458 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
459 } else if(strStartsWith(s, "+CGEV: ME PDN ACT ")) { // +CGEV: ME PDN ACT <cid>,1
460 //apn_state_set(atoi(s + 18), true);
461 data[0] = (uint8)1;
462 data[1] = (uint8)atoi(s + 18);
463
464 uint8 data_pdp;
465 char* tmp_s = memdup(s + 18,strlen(s + 18));
466 char* free_ptr = tmp_s;
467 char *line = tmp_s;
468 int tmp_int;
469 if (at_tok_start(&line) < 0)
470 {
471 goto PDP_CREG_EXIT;
472 }
473 if (at_tok_nextint(&line, &tmp_int) < 0)
474 {
475 goto PDP_CREG_EXIT;
476 }
477 if (at_tok_nextint(&line, &tmp_int) < 0)
478 {
479 goto PDP_CREG_EXIT;
480 }
481 data_pdp = tmp_int;
482PDP_CREG_EXIT:
483 free(free_ptr);
484 //data_pdp = (uint8)atoi(s + 20); //reason
485 if(data[1] >= 1 && data[1] < 8)
486 {
487 if(data_pdp == 0)
488 {
489 data_pdp = 25;
490 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
491 }
492 else if(data_pdp == 1)
493 {
494 data_pdp = 26;
495 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
496 }
497 else if(data_pdp == 2)
498 {
499 data_pdp = 27;
500 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
501 }
502 else if(data_pdp == 3)
503 {
504 data_pdp = 27;
505 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
506 }
507 else
508 {
509
510 }
511
512 data_pdp = data[1] + 200;
513 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
514 data[1] = 0;
515 }
516 } else {
517 LOGI("No process : %s", s);
518 }
519
520 urc_msg_distribute(true, INFO_URC_MSG_CGEV, data, sizeof(uint8) * 2);
521 }
522 }
523 // +CREG: 1, "8010", "000060a5", 0, 2, 0
524 // +CREG: 1, "8330", "06447347", 7, 2, 0
525 // +CEREG: 1, "8330", "06447347", 7
526 // $CREG: 1, "8330", "06447347", 7,"0d4", 2, 0
527 // $CREG: 1, "8010", "000060a7", 0,, 2, 0
528 // +CGREG: 1
529 else if(strStartsWith(s, "+CGREG:") // GMS/WCDMA data registed.
530 || strStartsWith(s, "+CEREG:")) // LTE data registed.
531 {
532 char* tmp_s = s + 7;
533 static bool net_led_gms_wcdma = FALSE;
534 static bool net_led_lte = FALSE;
535 while(*tmp_s && *tmp_s == ' ')
536 tmp_s++;
537 uint8 data[2];
538 data[0] = (uint8)atoi(tmp_s); // Reg State.
539
540 if(strStartsWith(s, "+CGREG:"))
541 {
542 data[1] = 0; // GMS/WCDMA
543 if(data[0] == 1)
544 {
545 net_led_gms_wcdma = TRUE;
546 }
547 else
548 {
549 net_led_gms_wcdma = FALSE;
550 }
551
552 }
553 else
554 {
555 data[1] = 1; // LTE
556 if(data[0] == 1)
557 {
558 net_led_lte = TRUE;
559 }
560 else
561 {
562 net_led_lte = FALSE;
563 }
564 }
565
566 if(FALSE == net_led_gms_wcdma && FALSE == net_led_lte)
567 {
568 //mbtk_net_led_set(MBTK_NET_LED_SEARCH_NETWORK);
569 }
570 else
571 {
572 //mbtk_net_led_set(MBTK_NET_LED_NET_CONNECT);
573 mbtk_net_ready();
574 }
575
576 urc_msg_distribute(true, INFO_URC_MSG_NET_PS_REG_STATE, data, sizeof(data));
577 urc_msg_distribute(true, INFO_URC_MSG_NET_STATE_LOG, NULL, 0);
578 }
579 // +CREG: 1, "8010", "000060a5", 0, 2, 0
580 // +CREG: 1, "8330", "06447347", 7, 2, 0
581 // +CREG: 0
582 else if(strStartsWith(s, "+CREG:")) // GMS/WCDMA/LTE CS registed.
583 {
584 uint8 data[3];
585 data[0] = (uint8)MBTK_NET_CS_STATE;
586 char* tmp_s = memdup(s,strlen(s));
587 char* free_ptr = tmp_s;
588 char *line = tmp_s;
589 int tmp_int;
590 char *tmp_str;
591 if (at_tok_start(&line) < 0)
592 {
593 goto CREG_EXIT;
594 }
595 if (at_tok_nextint(&line, &tmp_int) < 0)
596 {
597 goto CREG_EXIT;
598 }
599 data[1] = (uint8)tmp_int; // Reg State.
600 if (data[1])
601 {
602 if (at_tok_nextstr(&line, &tmp_str) < 0)
603 {
604 goto CREG_EXIT;
605 }
606 if (at_tok_nextstr(&line, &tmp_str) < 0)
607 {
608 goto CREG_EXIT;
609 }
610 if (at_tok_nextint(&line, &tmp_int) < 0)
611 {
612 goto CREG_EXIT;
613 }
614 data[2] = (uint8)tmp_int; // AcT
615 } else {
616 data[2] = (uint8)0xFF; // AcT
617 }
618 if(data[1] == 5)
619 {
620 uint8 data_pdp;
621 data_pdp = 5; //
622 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
623 }
624 urc_msg_distribute(false, INFO_URC_MSG_NET_CS_REG_STATE, data, sizeof(data));
625 urc_msg_distribute(true, INFO_URC_MSG_NET_STATE_LOG, NULL, 0);
626CREG_EXIT:
627 free(free_ptr);
628 }
629 // +CLCC: 1, 1, 6, 0, 0, "18981911691", 129, "",, 0
630 else if(strStartsWith(s, "+CLCC:"))
631 {
632 mbtk_call_info_t reg;
633 reg.call_wait = MBTK_CLCC;
634 char* tmp_s = memdup(s,strlen(s));
635 char* free_ptr = tmp_s;
636 char *line = tmp_s;
637 int tmp_int;
638 char *tmp_str;
639 int err;
640
641 err = at_tok_start(&line);
642 if (err < 0)
643 {
644 goto CLCC_EXIT;
645 }
646 err = at_tok_nextint(&line, &tmp_int); // dir1
647 if (err < 0)
648 {
649 goto CLCC_EXIT;
650 }
651 reg.dir1 = (uint8)tmp_int;
652 err = at_tok_nextint(&line, &tmp_int);// dir
653 if (err < 0)
654 {
655 goto CLCC_EXIT;
656 }
657 reg.dir = (uint8)tmp_int;
658 err = at_tok_nextint(&line, &tmp_int);// state
659 if (err < 0)
660 {
661 goto CLCC_EXIT;
662 }
663 reg.state = (uint8)tmp_int;
664 err = at_tok_nextint(&line, &tmp_int);// mode
665 if (err < 0)
666 {
667 goto CLCC_EXIT;
668 }
669 reg.mode = (uint8)tmp_int;
670 err = at_tok_nextint(&line, &tmp_int);// mpty
671 if (err < 0)
672 {
673 goto CLCC_EXIT;
674 }
675 reg.mpty = (uint8)tmp_int;
676 err = at_tok_nextstr(&line, &tmp_str); // phone_number
677 if (err < 0)
678 {
679 goto CLCC_EXIT;
680 }
681
682 memset(reg.phone_number,0,sizeof(reg.phone_number));
683 memcpy(reg.phone_number, tmp_str, strlen(tmp_str));
684 err = at_tok_nextint(&line, &tmp_int);// tpye
685 if (err < 0)
686 {
687 goto CLCC_EXIT;
688 }
689 reg.type = (uint8)tmp_int;
690 urc_msg_distribute(false, INFO_URC_MSG_CALL_STATE, &reg, sizeof(mbtk_call_info_t));
691CLCC_EXIT:
692 free(free_ptr);
693 }
694 // +CPAS: 4
695 else if(strStartsWith(s, "+CPAS:"))
696 {
697 mbtk_call_info_t reg;
698 reg.call_wait = 0;
699 char* tmp_s = memdup(s,strlen(s));
700 char* free_ptr = tmp_s;
701 char *line = tmp_s;
702 int tmp_int;
703 int err;
704
705 memset(&reg,0,sizeof(reg));
706
707 err = at_tok_start(&line);
708 if (err < 0)
709 {
710 goto CPAS_EXIT;
711 }
712 err = at_tok_nextint(&line, &tmp_int);
713 if (err < 0)
714 {
715 goto CPAS_EXIT;
716 }
717 reg.pas = (uint8)tmp_int;
718 reg.call_wait = MBTK_CPAS;
719 urc_msg_distribute(false, INFO_URC_MSG_CALL_STATE, &reg, sizeof(mbtk_call_info_t));
720CPAS_EXIT:
721 free(free_ptr);
722 }
723 // +CALLDISCONNECT: 1
724 else if(strStartsWith(s, "+CALLDISCONNECT:"))
725 {
726 mbtk_call_info_t reg;
727 reg.call_wait = 0;
728 char* tmp_s = memdup(s,strlen(s));
729 char* free_ptr = tmp_s;
730 char *line = tmp_s;
731 int tmp_int;
732 int err;
733
734 memset(&reg,0,sizeof(reg));
735
736 err = at_tok_start(&line);
737 if (err < 0)
738 {
739 goto CALLDISCONNECTED_EXIT;
740 }
741 err = at_tok_nextint(&line, &tmp_int);
742 if (err < 0)
743 {
744 goto CALLDISCONNECTED_EXIT;
745 }
746 reg.disconnected_id = tmp_int;
747 reg.call_wait = MBTK_DISCONNECTED;
748
749 if(reg.call_wait == MBTK_DISCONNECTED)
750 {
751 //mbtk_net_led_set(MBTK_NET_LED_CALL_DISCONNECT);
752 }
753
754 urc_msg_distribute(false, INFO_URC_MSG_CALL_STATE, &reg, sizeof(mbtk_call_info_t));
755
756CALLDISCONNECTED_EXIT:
757 free(free_ptr);
758 }
759 // *SIMDETEC:1,SIM
760 else if(strStartsWith(s, "*SIMDETEC:"))
761 {
762 if(strStartsWith(s, "*SIMDETEC:1,NOS"))
763 {
764 net_info.sim_state = MBTK_SIM_ABSENT;
765 }
766
767 sim_info_reg.sim = -1;
768 if(strStartsWith(s, "*SIMDETEC:1,NOS"))
769 sim_info_reg.sim = 0;
770 else if(strStartsWith(s, "*SIMDETEC:1,SIM"))
771 sim_info_reg.sim = 1;
772 if(sim_info_reg.sim == 0)
773 {
774 uint8 data_pdp;
775 data_pdp = 11; //
776 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
777 }
778 urc_msg_distribute(false, INFO_URC_MSG_SIM_STATE, &sim_info_reg, sizeof(mbtk_sim_card_info));
779 }
780 // *EUICC:1
781/*0: SIM
7821: USIM
7832: TEST SIM
7843: TEST USIM
7854: UNKNOWN
786Note: *EUICC:
787*/
788 else if(strStartsWith(s, "*EUICC:"))
789 {
790 sim_info_reg.sim_card_type = -1;
791 if(strStartsWith(s, "*EUICC: 0"))
792 sim_info_reg.sim_card_type = 1;
793 else if(strStartsWith(s, "*EUICC: 1"))
794 sim_info_reg.sim_card_type = 2;
795 else if(strStartsWith(s, "*EUICC: 2"))
796 sim_info_reg.sim_card_type = 1;
797 else if(strStartsWith(s, "*EUICC: 3"))
798 sim_info_reg.sim_card_type = 2;
799 else if(strStartsWith(s, "*EUICC: 4"))
800 sim_info_reg.sim_card_type = 0;
801 urc_msg_distribute(false, INFO_URC_MSG_SIM_STATE, &sim_info_reg, sizeof(mbtk_sim_card_info));
802 }
803 // +CPIN: SIM PIN
804 else if(strStartsWith(s, "+CPIN:"))
805 {
806 sim_info_reg.sim = -1;
807 if(strStartsWith(s, "+CPIN: READY"))
808 {
809 sim_info_reg.sim = 1;
810 net_info.sim_state = MBTK_SIM_READY;
811 }
812 else if(strStartsWith(s, "+CPIN: SIM PIN"))
813 {
814 sim_info_reg.sim = 2;
815 net_info.sim_state = MBTK_SIM_PIN;
816 }
817 else if(strStartsWith(s, "+CPIN: SIM PUK"))
818 {
819 sim_info_reg.sim = 3;
820 net_info.sim_state = MBTK_SIM_PUK;
821 }
822 else if(strStartsWith(s, "+CPIN: PH-SIMLOCK PIN"))
823 {
824 sim_info_reg.sim = 4;
825 net_info.sim_state = MBTK_SIM_ABSENT;
826 }
827 else if(strStartsWith(s, "+CPIN: PH-SIMLOCK PUK"))
828 {
829 sim_info_reg.sim = 5;
830 net_info.sim_state = MBTK_SIM_ABSENT;
831 }
832 else if(strStartsWith(s, "+CPIN: PH-FSIM PIN"))
833 {
834 sim_info_reg.sim = 6;
835 net_info.sim_state = MBTK_SIM_ABSENT;
836 }
837 else if(strStartsWith(s, "+CPIN: PH-FSIM PUK"))
838 {
839 sim_info_reg.sim = 7;
840 net_info.sim_state = MBTK_SIM_ABSENT;
841 }
842 else if(strStartsWith(s, "+CPIN: SIM PIN2"))
843 {
844 sim_info_reg.sim = 8;
845 net_info.sim_state = MBTK_SIM_ABSENT;
846 }
847 else if(strStartsWith(s, "+CPIN: SIM PUK2"))
848 {
849 sim_info_reg.sim = 9;
850 net_info.sim_state = MBTK_SIM_ABSENT;
851 }
852 else if(strStartsWith(s, "+CPIN: PH-NET PIN"))
853 {
854 sim_info_reg.sim = 10;
855 net_info.sim_state = MBTK_SIM_NETWORK_PERSONALIZATION;
856 }
857 else if(strStartsWith(s, "+CPIN: PH-NET PUK"))
858 {
859 sim_info_reg.sim = 11;
860 net_info.sim_state = MBTK_SIM_ABSENT;
861 }
862 else if(strStartsWith(s, "+CPIN: PH-NETSUB PINMT"))
863 {
864 sim_info_reg.sim = 12;
865 net_info.sim_state = MBTK_SIM_ABSENT;
866 }
867 else if(strStartsWith(s, "+CPIN: PH-NETSUB PUK"))
868 {
869 sim_info_reg.sim = 13;
870 net_info.sim_state = MBTK_SIM_ABSENT;
871 }
872 else if(strStartsWith(s, "+CPIN: PH-SP PIN"))
873 {
874 sim_info_reg.sim = 14;
875 net_info.sim_state = MBTK_SIM_ABSENT;
876 }
877 else if(strStartsWith(s, "+CPIN: PH-SP PUK"))
878 {
879 sim_info_reg.sim = 15;
880 net_info.sim_state = MBTK_SIM_ABSENT;
881 }
882 else if(strStartsWith(s, "+CPIN: PH-CORP PIN"))
883 {
884 sim_info_reg.sim = 16;
885 net_info.sim_state = MBTK_SIM_ABSENT;
886 }
887 else if(strStartsWith(s, "+CPIN: PH-CORP PUK"))
888 {
889 sim_info_reg.sim = 17;
890 net_info.sim_state = MBTK_SIM_ABSENT;
891 }
892 else if(strStartsWith(s, "+CPIN: SIM REMOVED"))
893 {
894 sim_info_reg.sim = 18;
895 net_info.sim_state = MBTK_SIM_ABSENT;
896 }
897 else
898 sim_info_reg.sim = 20;
899
900 if(sim_info_reg.sim == 18)
901 {
902 uint8 data_pdp;
903 data_pdp = 11; //
904 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
905 }
906
907 urc_msg_distribute(false, INFO_URC_MSG_SIM_STATE, &sim_info_reg, sizeof(mbtk_sim_card_info));
908 }
909 // +CMT: ,23
910 // 0891683108200855F6240D91688189911196F10000221130717445230331D90C
911 else if(strStartsWith(s, "+CMT:") || sms_cmt)
912 {
913 if(!sms_cmt){
914 sms_cmt = true;
915 }else{
916 sms_cmt = false;
917 }
918 printf("+CMT() sms_cmt:%d, s:%s, len:%d\n",sms_cmt, s, strlen(s));
919 urc_msg_distribute(false, INFO_URC_MSG_SMS_STATE, s, strlen(s));
920 }
921#if 0
922 // LTE data registed.
923 // +CEREG: 1, "8330", "06447347", 7
924 else if(strStartsWith(s, "+CEREG:"))
925 {
926 char* tmp_s = memdup(s,strlen(s));
927 char* free_ptr = tmp_s;
928 char *line = tmp_s;
929 int tmp_int;
930 char *tmp_str;
931 if (at_tok_start(&line) < 0)
932 {
933 goto CREG_EXIT;
934 }
935 if (at_tok_nextint(&line, &tmp_int) < 0)
936 {
937 goto CREG_EXIT;
938 }
939 uint8 data = (uint8)tmp_int; // Reg State.
940
941 urc_msg_distribute(INFO_URC_MSG_NET_REG_STATE, &data, sizeof(uint8));
942CREG_EXIT:
943 free(free_ptr);
944 }
945#endif
946 /*
947 // <mcc>, <length of mnc>, <mnc>, <tac>, <PCI>, <dlEuarfcn>, < ulEuarfcn >, <band>, <dlBandwidth>,
948 // <rsrp>,<rsrq>, <sinr>,
949 // errcModeState,emmState,serviceState,IsSingleEmmRejectCause,EMMRejectCause,mmeGroupId,mmeCode,mTmsi,
950 // cellId,subFrameAssignType,specialSubframePatterns,transMode
951 // mainRsrp,diversityRsrp,mainRsrq,diversityRsrq,rssi,cqi,pathLoss,tb0DlTpt,tb1DlTpt,tb0DlPeakTpt,tb1DlPeakTpt,tb0UlPeakTpt,
952 // tb1UlPeakTpt,dlThroughPut,dlPeakThroughPut,averDlPRB,averCQITb0,averCQITb1,rankIndex,grantTotal,ulThroughPut,ulPeakThroughPut,currPuschTxPower,averUlPRB,
953 // dlBer, ulBer,
954 // diversitySinr, diversityRssi
955 +EEMLTESVC: 1120, 2, 0, 33584, 430, 40936, 40936, 41, 20,
956 0, 0, 0,
957 1, 10, 0, 1, 0, 1059, 78, 3959566565,
958 105149248, 2, 7, 7,
959 0, 0, 0, 0, 0, 0, 0, 1190919, 0, 0, 0, 16779777,
960 0, 5112867, 3959566565, 2, 0, 0, 0, 0, 0, 0, 0, 0,
961 0, 0,
962 7, 44
963 */
964 else if(strStartsWith(s, "+EEMLTESVC:")) // LTE Server Cell
965 {
966 // tac, PCI, dlEuarfcn, ulEuarfcn, band
967 if(cell_info.running) {
968 int tmp_int;
969 int i = 0;
970 char* tmp_s = memdup(s,strlen(s));
971 char* free_ptr = tmp_s;
972 char *line = tmp_s;
973 if (at_tok_start(&line) < 0)
974 {
975 goto EEMLTESVC_EXIT;
976 }
977
978 if (at_tok_nextint(&line, &tmp_int) < 0)
979 {
980 goto EEMLTESVC_EXIT;
981 }
982 cell_info.cell[cell_info.cell_num].value6 = (uint32)tmp_int; //mcc
983 if (at_tok_nextint(&line, &tmp_int) < 0)
984 {
985 goto EEMLTESVC_EXIT;
986 }
987 if (at_tok_nextint(&line, &tmp_int) < 0)
988 {
989 goto EEMLTESVC_EXIT;
990 }
991 cell_info.cell[cell_info.cell_num].value7 = (uint32)tmp_int; //mnc
992 /*
993 // Jump 2 integer.
994 i = 0;
995 while(i < 2) {
996 if (at_tok_nextint(&line, &tmp_int) < 0)
997 {
998 goto EEMLTESVC_EXIT;
999 }
1000 i++;
1001 }
1002 */
1003 if (at_tok_nextint(&line, &tmp_int) < 0)
1004 {
1005 goto EEMLTESVC_EXIT;
1006 }
1007 cell_info.cell[cell_info.cell_num].value1 = (uint32)tmp_int; //tac
1008 if (at_tok_nextint(&line, &tmp_int) < 0)
1009 {
1010 goto EEMLTESVC_EXIT;
1011 }
1012 cell_info.cell[cell_info.cell_num].value2 = (uint32)tmp_int; //pci
1013 if (at_tok_nextint(&line, &tmp_int) < 0)
1014 {
1015 goto EEMLTESVC_EXIT;
1016 }
1017 cell_info.cell[cell_info.cell_num].value3 = (uint32)tmp_int; //dl arfcn
1018 if (at_tok_nextint(&line, &tmp_int) < 0)
1019 {
1020 goto EEMLTESVC_EXIT;
1021 }
1022 cell_info.cell[cell_info.cell_num].value4 = (uint32)tmp_int; //ul arfcn
1023 if (at_tok_nextint(&line, &tmp_int) < 0)
1024 {
1025 goto EEMLTESVC_EXIT;
1026 }
1027 cell_info.cell[cell_info.cell_num].value5 = (uint32)tmp_int; //band
1028 if (at_tok_nextint(&line, &tmp_int) < 0)
1029 {
1030 goto EEMLTESVC_EXIT;
1031 }
1032 if (at_tok_nextint(&line, &tmp_int) < 0)
1033 {
1034 goto EEMLTESVC_EXIT;
1035 }
1036 cell_info.cell[cell_info.cell_num].value8 = (uint32)tmp_int; //cid
1037 if (at_tok_nextint(&line, &tmp_int) < 0)
1038 {
1039 goto EEMLTESVC_EXIT;
1040 }
1041 cell_info.cell[cell_info.cell_num].value9 = (uint32)tmp_int; //rsrp
1042
1043 for(i =0; i < 10; i++)
1044 {
1045 if (at_tok_nextint(&line, &tmp_int) < 0)
1046 {
1047 goto EEMLTESVC_EXIT;
1048 }
1049 }
1050 cell_info.cell[cell_info.cell_num].value10 = (uint32)tmp_int; //cell identiy
1051
1052 cell_info.cell_num++;
1053
1054EEMLTESVC_EXIT:
1055 free(free_ptr);
1056 }
1057 }
1058 /*
1059 // index,phyCellId,euArfcn,rsrp,rsrq
1060 +EEMLTEINTER: 0, 65535, 38950, 0, 0
1061 */
1062 else if(strStartsWith(s, "+EEMLTEINTER:") || strStartsWith(s, "+EEMLTEINTRA:")) // LTE ÒìÆµ/Í¬ÆµÐ¡Çø
1063 {
1064 // phyCellId,euArfcn,rsrp,rsrq
1065 if(cell_info.running) {
1066 int tmp_int;
1067 char* tmp_s = memdup(s,strlen(s));
1068 char* free_ptr = tmp_s;
1069 char *line = tmp_s;
1070 if (at_tok_start(&line) < 0)
1071 {
1072 goto EEMLTEINTER_EXIT;
1073 }
1074 if (at_tok_nextint(&line, &tmp_int) < 0)
1075 {
1076 goto EEMLTEINTER_EXIT;
1077 }
1078 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int <= 0 || tmp_int > 503)
1079 {
1080 goto EEMLTEINTER_EXIT;
1081 }
1082 cell_info.cell[cell_info.cell_num].value1 = (uint32)tmp_int;
1083 if (at_tok_nextint(&line, &tmp_int) < 0)
1084 {
1085 goto EEMLTEINTER_EXIT;
1086 }
1087 cell_info.cell[cell_info.cell_num].value2 = (uint32)tmp_int;
1088 if (at_tok_nextint(&line, &tmp_int) < 0)
1089 {
1090 goto EEMLTEINTER_EXIT;
1091 }
1092 cell_info.cell[cell_info.cell_num].value3 = (uint32)tmp_int;
1093 LOG("cell line : %s", line);
1094 if (at_tok_nextint(&line, &tmp_int) < 0)
1095 {
1096 goto EEMLTEINTER_EXIT;
1097 }
1098 cell_info.cell[cell_info.cell_num].value4 = (uint32)tmp_int;
1099 if (at_tok_nextint(&line, &tmp_int) < 0)
1100 {
1101 LOG("cell tmp_int : %d", tmp_int);
1102 goto EEMLTEINTER_EXIT;
1103 }
1104 cell_info.cell[cell_info.cell_num].value5 = (uint32)tmp_int;
1105 LOG("cell value5 : %d", cell_info.cell[cell_info.cell_num].value5);
1106 cell_info.cell_num++;
1107EEMLTEINTER_EXIT:
1108 free(free_ptr);
1109 }
1110 }
1111 // Do nothing
1112 else if(strStartsWith(s, "+EEMLTEINTERRAT:")) // LTE RATÐ¡ÇøÐÅÏ¢
1113 {
1114 if(cell_info.running) {
1115
1116 }
1117 }
1118 // WCDMA
1119 /*
1120 // Mode, sCMeasPresent, sCParamPresent, ueOpStatusPresent,
1121
1122 // if sCMeasPresent == 1
1123 // cpichRSCP, utraRssi, cpichEcN0, sQual, sRxLev, txPower,
1124 // endif
1125
1126 // if sCParamPresent == 1
1127 // rac, nom, mcc, mnc_len, mnc, lac, ci,
1128 // uraId, psc, arfcn, t3212, t3312, hcsUsed, attDetAllowed,
1129 // csDrxCycleLen, psDrxCycleLen, utranDrxCycleLen, HSDPASupport, HSUPASupport,
1130 // endif
1131
1132 // if ueOpStatusPresent == 1
1133 // rrcState, numLinks, srncId, sRnti,
1134 // algPresent, cipherAlg, cipherOn, algPresent, cipherAlg, cipherOn,
1135 // HSDPAActive, HSUPAActive, MccLastRegisteredNetwork, MncLastRegisteredNetwork, TMSI, PTMSI, IsSingleMmRejectCause, IsSingleGmmRejectCause,
1136 // MMRejectCause, GMMRejectCause, mmState, gmmState, gprsReadyState, readyTimerValueInSecs, NumActivePDPContext, ULThroughput, DLThroughput,
1137 // serviceStatus, pmmState, LAU_status, LAU_count, RAU_status, RAU_count
1138 // endif
1139 //
1140 +EEMUMTSSVC: 3, 1, 1, 1,
1141 -80, 27, -6, -18, -115, -32768,
1142 1, 1, 1120, 2, 1, 61697, 168432821,
1143 15, 24, 10763, 0, 0, 0, 0,
1144 128, 128, 65535, 0, 0,
1145 2, 255, 65535, 4294967295,
1146 0, 0, 0, 0, 0, 0,
1147 0, 0, 0, 0, 0, 0, 1, 1,
1148 28672, 28672, 0, 0, 0, 0, 0, 0, 0,
1149 0, 0, 0, 0, 0, 0
1150 */
1151 else if(strStartsWith(s, "+EEMUMTSSVC:")) // WCDMA Server Cell
1152 {
1153 // lac, ci, arfcn
1154 if(cell_info.running) {
1155 int tmp_int;
1156 int i = 0;
1157 char* tmp_s = memdup(s,strlen(s));
1158 char* free_ptr = tmp_s;
1159 char *line = tmp_s;
1160 if (at_tok_start(&line) < 0)
1161 {
1162 goto EEMUMTSSVC_EXIT;
1163 }
1164 // Jump 12 integer.
1165 i = 0;
1166 while(i < 12) {
1167 if (at_tok_nextint(&line, &tmp_int) < 0)
1168 {
1169 goto EEMUMTSSVC_EXIT;
1170 }
1171 i++;
1172 }
1173 // mcc
1174 if (at_tok_nextint(&line, &tmp_int) < 0)
1175 {
1176 goto EEMUMTSSVC_EXIT;
1177 }
1178 cell_info.cell[cell_info.cell_num].value4= (uint32)tmp_int;
1179 // mnc
1180 if (at_tok_nextint(&line, &tmp_int) < 0)
1181 {
1182 goto EEMUMTSSVC_EXIT;
1183 }
1184 cell_info.cell[cell_info.cell_num].value5= (uint32)tmp_int;
1185 // lac
1186 if (at_tok_nextint(&line, &tmp_int) < 0)
1187 {
1188 goto EEMUMTSSVC_EXIT;
1189 }
1190 cell_info.cell[cell_info.cell_num].value1 = (uint32)tmp_int;
1191 // ci
1192 if (at_tok_nextint(&line, &tmp_int) < 0)
1193 {
1194 goto EEMUMTSSVC_EXIT;
1195 }
1196 cell_info.cell[cell_info.cell_num].value2 = (uint32)tmp_int;
1197
1198 if (at_tok_nextint(&line, &tmp_int) < 0)
1199 {
1200 goto EEMUMTSSVC_EXIT;
1201 }
1202 // cpi
1203 if (at_tok_nextint(&line, &tmp_int) < 0)
1204 {
1205 goto EEMUMTSSVC_EXIT;
1206 }
1207 cell_info.cell[cell_info.cell_num].value6= (uint32)tmp_int;
1208 /*
1209 // Jump 2 integer.
1210 i = 0;
1211 while(i < 2) {
1212 if (at_tok_nextint(&line, &tmp_int) < 0)
1213 {
1214 goto EEMUMTSSVC_EXIT;
1215 }
1216 i++;
1217 }
1218 */
1219 // arfcn
1220 if (at_tok_nextint(&line, &tmp_int) < 0)
1221 {
1222 goto EEMUMTSSVC_EXIT;
1223 }
1224 cell_info.cell[cell_info.cell_num].value3 = (uint32)tmp_int;
1225
1226 cell_info.cell_num++;
1227EEMUMTSSVC_EXIT:
1228 free(free_ptr);
1229 }
1230 }
1231 /*
1232 // index, cpichRSCP, utraRssi, cpichEcN0, sQual, sRxLev ,mcc, mnc, lac, ci, arfcn, psc
1233 +EEMUMTSINTRA: 0, -32768, -1, -32768, -18, -115, 0, 0, 65534, 1, 10763, 32
1234 */
1235 else if(strStartsWith(s, "+EEMUMTSINTRA:")) // WCDMAÁÙ½üÐ¡Çø
1236 {
1237 // lac, ci, arfcn
1238 if(cell_info.running) {
1239 int tmp_int;
1240 int i = 0;
1241 char* tmp_s = memdup(s,strlen(s));
1242 char* free_ptr = tmp_s;
1243 char *line = tmp_s;
1244 if (at_tok_start(&line) < 0)
1245 {
1246 goto EEMUMTSINTRA_EXIT;
1247 }
1248 // Jump 8 integer.
1249 i = 0;
1250 while(i < 8) {
1251 if (at_tok_nextint(&line, &tmp_int) < 0)
1252 {
1253 goto EEMUMTSINTRA_EXIT;
1254 }
1255 i++;
1256 }
1257
1258 // lac
1259 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int <= 0 || tmp_int >= 65536)
1260 {
1261 goto EEMUMTSINTRA_EXIT;
1262 }
1263 cell_info.cell[cell_info.cell_num].value1 = (uint32)tmp_int;
1264
1265 // ci
1266 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int <= 0 || tmp_int >= 65536)
1267 {
1268 goto EEMUMTSINTRA_EXIT;
1269 }
1270 cell_info.cell[cell_info.cell_num].value2 = (uint32)tmp_int;
1271
1272 // arfcn
1273 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int <= 0 || tmp_int >= 65536)
1274 {
1275 goto EEMUMTSINTRA_EXIT;
1276 }
1277 cell_info.cell[cell_info.cell_num].value3 = (uint32)tmp_int;
1278
1279 cell_info.cell_num++;
1280EEMUMTSINTRA_EXIT:
1281 free(free_ptr);
1282 }
1283 }
1284 /*
1285 // index,gsmRssi,rxLev,C1,C2,mcc,mnc,lac,ci,arfcn,bsic
1286 +EEMUMTSINTERRAT: 0, -32768, -107, -1, -1, 0, 0, 65534, 0, 117, 36
1287 */
1288 else if(strStartsWith(s, "+EEMUMTSINTERRAT:")) // WCDMA RATÐ¡ÇøÐÅÏ¢
1289 {
1290 // lac, ci, arfcn
1291 if(cell_info.running) {
1292 int tmp_int;
1293 int i = 0;
1294 char* tmp_s = memdup(s,strlen(s));
1295 char* free_ptr = tmp_s;
1296 char *line = tmp_s;
1297 if (at_tok_start(&line) < 0)
1298 {
1299 goto EEMUMTSINTERRAT_EXIT;
1300 }
1301 // Jump 7 integer.
1302 i = 0;
1303 while(i < 7) {
1304 if (at_tok_nextint(&line, &tmp_int) < 0)
1305 {
1306 goto EEMUMTSINTERRAT_EXIT;
1307 }
1308 i++;
1309 }
1310
1311 // lac
1312 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int <= 0 || tmp_int >= 65536)
1313 {
1314 goto EEMUMTSINTERRAT_EXIT;
1315 }
1316 cell_info.cell[cell_info.cell_num].value1 = (uint32)tmp_int;
1317
1318 // ci
1319 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int <= 0 || tmp_int >= 65536)
1320 {
1321 goto EEMUMTSINTERRAT_EXIT;
1322 }
1323 cell_info.cell[cell_info.cell_num].value2 = (uint32)tmp_int;
1324
1325 // arfcn
1326 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int <= 0 || tmp_int >= 65536)
1327 {
1328 goto EEMUMTSINTERRAT_EXIT;
1329 }
1330 cell_info.cell[cell_info.cell_num].value3 = (uint32)tmp_int;
1331
1332 cell_info.cell_num++;
1333EEMUMTSINTERRAT_EXIT:
1334 free(free_ptr);
1335 }
1336 }
1337 // GSM
1338 // +EEMGINFOBASIC: 2
1339 // Do nothing.
1340 else if(strStartsWith(s, "+EEMGINFOBASIC:")) // Basic information in GSM
1341 // 0: ME in Idle mode 1: ME in Dedicated mode 2: ME in PS PTM mode
1342 {
1343 if(cell_info.running) {
1344
1345 }
1346 }
1347 /*
1348 // mcc, mnc_len, mnc, lac, ci, nom, nco,
1349 // bsic, C1, C2, TA, TxPwr,
1350 // RxSig, RxSigFull, RxSigSub, RxQualFull, RxQualSub,
1351 // ARFCB_tch, hopping_chnl, chnl_type, TS, PacketIdle, rac, arfcn,
1352 // bs_pa_mfrms, C31, C32, t3212, t3312, pbcch_support, EDGE_support,
1353 // ncc_permitted, rl_timeout, ho_count, ho_succ, chnl_access_count, chnl_access_succ_count,
1354 // gsmBand,channelMode
1355 +EEMGINFOSVC: 1120, 2, 0, 32784, 24741, 2, 0,
1356 63, 36, 146, 1, 7,
1357 46, 42, 42, 7, 0,
1358 53, 0, 8, 0, 1, 6, 53,
1359 2, 0, 146, 42, 54, 0, 1,
1360 1, 32, 0, 0, 0, 0,
1361 0, 0
1362 */
1363 else if(strStartsWith(s, "+EEMGINFOSVC:")) // GSM Server Cell
1364 {
1365 // lac, ci, arfcn, bsic
1366 LOG("+EEMGINFOSVC: 1= %d\n.",cell_info.running);
1367 if(cell_info.running) {
1368 int tmp_int;
1369 int i = 0;
1370 char* tmp_s = memdup(s,strlen(s));
1371 char* free_ptr = tmp_s;
1372 char *line = tmp_s;
1373 if (at_tok_start(&line) < 0)
1374 {
1375 goto EEMGINFOSVC_EXIT;
1376 }
1377
1378 // mcc
1379 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int <= 0 || tmp_int >= 65536)
1380 {
1381 goto EEMGINFOSVC_EXIT;
1382 }
1383 cell_info.cell[cell_info.cell_num].value5 = (uint32)tmp_int;
1384
1385 //mnc_len
1386 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int <= 0 || tmp_int >= 65536)
1387 {
1388 goto EEMGINFOSVC_EXIT;
1389 }
1390 // mnc
1391 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int <= 0 || tmp_int >= 65536)
1392 {
1393 goto EEMGINFOSVC_EXIT;
1394 }
1395 cell_info.cell[cell_info.cell_num].value6 = (uint32)tmp_int;
1396
1397 /*
1398 // Jump 3 integer.
1399 i = 0;
1400 while(i < 3) {
1401 if (at_tok_nextint(&line, &tmp_int) < 0)
1402 {
1403 goto EEMGINFOSVC_EXIT;
1404 }
1405 i++;
1406 }
1407 */
1408 // lac
1409 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1410 {
1411 goto EEMGINFOSVC_EXIT;
1412 }
1413 cell_info.cell[cell_info.cell_num].value1 = (uint32)tmp_int;
1414
1415 // ci
1416 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1417 {
1418 goto EEMGINFOSVC_EXIT;
1419 }
1420 cell_info.cell[cell_info.cell_num].value2 = (uint32)tmp_int;
1421
1422 // Jump 2 integer.
1423 i = 0;
1424 while(i < 2) {
1425 if (at_tok_nextint(&line, &tmp_int) < 0)
1426 {
1427 goto EEMGINFOSVC_EXIT;
1428 }
1429 i++;
1430 }
1431
1432 // bsic
1433 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1434 {
1435 goto EEMGINFOSVC_EXIT;
1436 }
1437 cell_info.cell[cell_info.cell_num].value4 = (uint32)tmp_int;
1438
1439 // Jump 15 integer.
1440 i = 0;
1441 while(i < 15) {
1442 if (at_tok_nextint(&line, &tmp_int) < 0)
1443 {
1444 goto EEMGINFOSVC_EXIT;
1445 }
1446 i++;
1447 }
1448
1449 // arfcn
1450 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1451 {
1452 goto EEMGINFOSVC_EXIT;
1453 }
1454 cell_info.cell[cell_info.cell_num].value3 = (uint32)tmp_int;
1455
1456 cell_info.cell_num++;
1457EEMGINFOSVC_EXIT:
1458 free(free_ptr);
1459 }
1460 }
1461 /*
1462 // PS_attached, attach_type, service_type, tx_power, c_value,
1463 // ul_ts, dl_ts, ul_cs, dl_cs, ul_modulation, dl_modulation,
1464 // gmsk_cv_bep, 8psk_cv_bep, gmsk_mean_bep, 8psk_mean_bep, EDGE_bep_period, single_gmm_rej_cause
1465 // pdp_active_num, mac_mode, network_control, network_mode, EDGE_slq_measurement_mode, edge_status
1466 +EEMGINFOPS: 1, 255, 0, 0, 0,
1467 0, 0, 268435501, 1, 0, 0,
1468 4, 0, 96, 0, 0, 0,
1469 0, 0, 0, 65535, 0, 13350
1470 */
1471 // Do nothing.
1472 else if(strStartsWith(s, "+EEMGINFOPS:")) // PSÐÅÏ¢
1473 {
1474 if(cell_info.running) {
1475
1476 }
1477 }
1478 else if(strStartsWith(s, "+EEMGINFONC:")) // cell
1479 {
1480 if(cell_info.running) {
1481 int tmp_int;
1482 int i = 0;
1483 char* tmp_s = memdup(s,strlen(s));
1484 char* free_ptr = tmp_s;
1485 char *line = tmp_s;
1486 if (at_tok_start(&line) < 0)
1487 {
1488 goto EEMGINFOPS_EXIT;
1489 }
1490
1491 // nc_num
1492 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1493 {
1494 LOG("cell_info.running 1= %d\n.",cell_info.running);
1495 goto EEMGINFOPS_EXIT;
1496 }
1497 // mcc
1498 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1499 {
1500 LOG("cell_info.running 2= %d\n.",cell_info.running);
1501 goto EEMGINFOPS_EXIT;
1502 }
1503 cell_info.cell[cell_info.cell_num].value5 = (uint32)tmp_int;
1504
1505 // mnc
1506 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1507 {
1508 LOG("cell_info.running 3= %d\n.",cell_info.running);
1509 goto EEMGINFOPS_EXIT;
1510 }
1511 cell_info.cell[cell_info.cell_num].value6 = (uint32)tmp_int;
1512
1513 // lac
1514 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1515 {
1516 LOG("cell_info.running 4= %d\n.",cell_info.running);
1517 goto EEMGINFOPS_EXIT;
1518 }
1519 cell_info.cell[cell_info.cell_num].value1 = (uint32)tmp_int;
1520
1521 // rac
1522 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1523 {
1524 LOG("cell_info.running 5= %d\n.",cell_info.running);
1525 goto EEMGINFOPS_EXIT;
1526 }
1527
1528 // ci
1529 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1530 {
1531 LOG("cell_info.running 6= %d\n.",cell_info.running);
1532 goto EEMGINFOPS_EXIT;
1533 }
1534 cell_info.cell[cell_info.cell_num].value2 = (uint32)tmp_int;
1535
1536 // rx_lv
1537 if (at_tok_nextint(&line, &tmp_int) < 0)
1538 {
1539 LOG("cell_info.running 7= %d\n.",cell_info.running);
1540 goto EEMGINFOPS_EXIT;
1541 }
1542
1543 // bsic
1544 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1545 {
1546 LOG("cell_info.running 8= %d\n.",cell_info.running);
1547 goto EEMGINFOPS_EXIT;
1548 }
1549 cell_info.cell[cell_info.cell_num].value4 = (uint32)tmp_int;
1550
1551 // Jump 2 integer.
1552 i = 0;
1553 while(i < 2) {
1554 if (at_tok_nextint(&line, &tmp_int) < 0)
1555 {
1556 LOG("cell_info.running 9= %d\n.",cell_info.running);
1557 goto EEMGINFOPS_EXIT;
1558 }
1559 i++;
1560 }
1561
1562 // arfcn
1563 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1564 {
1565 LOG("cell_info.running 10 = %d\n.",cell_info.running);
1566 goto EEMGINFOPS_EXIT;
1567 }
1568 cell_info.cell[cell_info.cell_num].value3 = (uint32)tmp_int;
1569
1570 cell_info.cell_num++;
1571EEMGINFOPS_EXIT:
1572 free(free_ptr);
1573 }
1574 }
1575 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"
1576 {
1577
1578 }
1579 else
1580 {
1581 LOGV("Unknown URC : %s", s);
1582 }
1583#endif
1584}
1585
1586static int openSocket(const char* sockname)
1587{
1588 int sock = socket(AF_UNIX, SOCK_STREAM, 0);
1589 if (sock < 0)
1590 {
1591 LOGE("Error create socket: %s\n", strerror(errno));
1592 return -1;
1593 }
1594 struct sockaddr_un addr;
1595 memset(&addr, 0, sizeof(addr));
1596 addr.sun_family = AF_UNIX;
1597 strncpy(addr.sun_path, sockname, sizeof(addr.sun_path));
1598 while (TEMP_FAILURE_RETRY(connect(sock,(const struct sockaddr*)&addr, sizeof(addr))) != 0)
1599 {
1600 LOGE("Error connect to socket %s: %s, try again", sockname, strerror(errno));
1601 sleep(1);
1602 }
1603
1604#if 0
1605 int sk_flags = fcntl(sock, F_GETFL, 0);
1606 fcntl(sock, F_SETFL, sk_flags | O_NONBLOCK);
1607#endif
1608
1609 return sock;
1610}
1611
1612static void ril_at_ready_process()
1613{
1614 ril_info.radio_state = ril_radio_state_get();
1615 if (ril_info.radio_state != MBTK_RADIO_STATE_FULL_FUNC)
1616 {
1617 ril_radio_state_set(MBTK_RADIO_STATE_FULL_FUNC, FALSE);
1618 }
1619
1620 if(ril_info.radio_state == MBTK_RADIO_STATE_FULL_FUNC)
1621 {
1622 at_send_command("AT+CEREG=2", NULL);
1623 }
1624
1625 ril_info.sim_state = ril_sim_state_get();
1626 if(ril_info.sim_state == MBTK_SIM_STATE_READY)
1627 {
1628 LOGD("SIM READY!");
1629 at_send_command("AT+COPS=3", NULL);
1630
1631 // Set APN from prop.
1632 apn_auto_conf_from_prop();
1633 }
1634 else
1635 {
1636 LOGE("SIM NOT READY!");
1637 }
1638}
1639
1640static void ind_regisger(sock_cli_info_t* cli_info, uint16 ind)
1641{
1642 uint32 i = 0;
1643 while(i < cli_info->ind_num)
1644 {
1645 if(cli_info->ind_register[i] == ind)
1646 break;
1647 i++;
1648 }
1649
1650 if(i == cli_info->ind_num) // No found IND
1651 {
1652 cli_info->ind_register[i] = ind;
1653 cli_info->ind_num++;
1654 LOGD("Register IND : %s", id2str(ind));
1655 }
1656 else
1657 {
1658 LOGW("IND had exist.");
1659 }
1660}
1661
1662// Process AT URC data
1663static int send_pack_to_queue(sock_cli_info_t* cli_info, void* pack)
1664{
1665 if(ril_info.msg_queue.count >= PACK_PROCESS_QUEUE_MAX)
1666 {
1667 LOGE("Packet process queue is full");
1668 return -1;
1669 }
1670
1671 ril_msg_queue_info_t *item = (ril_msg_queue_info_t*)malloc(sizeof(ril_msg_queue_info_t));
1672 if(!item)
1673 {
1674 LOGE("malloc() fail[%d].", errno);
1675 return -1;
1676 }
1677 item->cli_info = cli_info;
1678 item->pack = pack;
1679 mbtk_queue_put(&ril_info.msg_queue, item);
1680
1681 // If thread is waitting,continue it.
1682 pthread_mutex_lock(&ril_info.msg_mutex);
1683 pthread_cond_signal(&ril_info.msg_cond);
1684 pthread_mutex_unlock(&ril_info.msg_mutex);
1685
1686 return 0;
1687}
1688
1689
1690static void pack_distribute(sock_cli_info_t* cli_info, ril_msg_pack_info_t* pack)
1691{
1692 // Register IND Message.
1693 if(pack->msg_type == RIL_MSG_TYPE_IND)
1694 {
1695 mbtk_ril_err_enum err = MBTK_RIL_ERR_SUCCESS;
1696 if(cli_info->ind_num >= IND_REGISTER_MAX)
1697 {
1698 LOGE("IND if full.");
1699 err = MBTK_RIL_ERR_IND_FULL;
1700 }
1701 else
1702 {
1703 ind_regisger(cli_info, pack->msg_id);
1704 }
1705
1706 ril_error_pack_send(cli_info->fd, pack->msg_id, pack->msg_index, err);
1707
1708 ril_msg_pack_free(pack);
1709 }
1710 else // Request Information.
1711 {
1712 LOGD("Start process REQ(%s), Length : %d", id2str(pack->msg_id), pack->data_len);
1713 if(0 && pack->data_len > 0)
1714 {
1715 log_hex("DATA", pack->data, pack->data_len);
1716 }
1717
1718 // Send to REQ_process_thread process.
1719 send_pack_to_queue(cli_info, pack);
1720
1721 // For test.
1722 // pack_error_send(cli_info->fd, pack->info_id + 1, MBTK_INFO_ERR_SUCCESS);
1723 }
1724}
1725
1726// Return MBTK_INFO_ERR_SUCCESS,will call pack_error_send() to send RSP.
1727// Otherwise, do not call pack_error_send().
1728static mbtk_ril_err_enum pack_req_process(sock_cli_info_t* cli_info, ril_msg_pack_info_t* pack)
1729{
1730 if(pack->msg_id > RIL_MSG_ID_DEV_BEGIN && pack->msg_id < RIL_MSG_ID_DEV_END) {
1731 return dev_pack_req_process(cli_info, pack);
1732 } else if(pack->msg_id > RIL_MSG_ID_SIM_BEGIN && pack->msg_id < RIL_MSG_ID_SIM_END) {
1733 return sim_pack_req_process(cli_info, pack);
1734 } else if(pack->msg_id > RIL_MSG_ID_NET_BEGIN && pack->msg_id < RIL_MSG_ID_NET_END) {
1735 return net_pack_req_process(cli_info, pack);
1736 } else if(pack->msg_id > RIL_MSG_ID_CALL_BEGIN && pack->msg_id < RIL_MSG_ID_CALL_END) {
1737 return call_pack_req_process(cli_info, pack);
1738 } else if(pack->msg_id > RIL_MSG_ID_SMS_BEGIN && pack->msg_id < RIL_MSG_ID_SMS_END) {
1739 return sms_pack_req_process(cli_info, pack);
1740 } else if(pack->msg_id > RIL_MSG_ID_PB_BEGIN && pack->msg_id < RIL_MSG_ID_PB_END) {
1741 return pb_pack_req_process(cli_info, pack);
1742 } else {
1743 LOGW("Unknown msg id : %d", pack->msg_id);
1744 return MBTK_RIL_ERR_FORMAT;
1745 }
1746}
1747
1748static void urc_msg_process(ril_urc_msg_info_t *msg)
1749{
1750 uint8 *data = NULL;
1751 if(msg->data) {
1752 data = (uint8*)msg->data;
1753 }
1754 switch(msg->msg) {
1755 case RIL_URC_MSG_RADIO_STATE:
1756 {
1757 break;
1758 }
1759 default:
1760 {
1761 LOGE("Unknown URC : %d", msg->msg);
1762 break;
1763 }
1764 }
1765}
1766
1767// Read client conn/msg and push into ril_info.msg_queue.
1768static void* ril_read_pthread(void* arg)
1769{
1770 UNUSED(arg);
1771 ril_info.epoll_fd = epoll_create(SOCK_CLIENT_MAX + 1);
1772 if(ril_info.epoll_fd < 0)
1773 {
1774 LOGE("epoll_create() fail[%d].", errno);
1775 return NULL;
1776 }
1777
1778 uint32 event = EPOLLIN | EPOLLET;
1779 struct epoll_event ev;
1780 ev.data.fd = ril_info.sock_listen_fd;
1781 ev.events = event; //EPOLLIN | EPOLLERR | EPOLLET;
1782 epoll_ctl(ril_info.epoll_fd, EPOLL_CTL_ADD, ril_info.sock_listen_fd, &ev);
1783
1784 int nready = -1;
1785 struct epoll_event epoll_events[EPOLL_LISTEN_MAX];
1786 while(1)
1787 {
1788 nready = epoll_wait(ril_info.epoll_fd, epoll_events, EPOLL_LISTEN_MAX, -1);
1789 if(nready > 0)
1790 {
1791 sock_cli_info_t *cli_info = NULL;
1792 int i;
1793 for(i = 0; i < nready; i++)
1794 {
1795 LOG("fd[%d] event = %x",epoll_events[i].data.fd, epoll_events[i].events);
1796 if(epoll_events[i].events & EPOLLHUP) // Client Close.
1797 {
1798 if((cli_info = cli_find(epoll_events[i].data.fd)) != NULL)
1799 {
1800 cli_close(cli_info);
1801 }
1802 else
1803 {
1804 LOG("Unknown client[fd = %d].", epoll_events[i].data.fd);
1805 }
1806 }
1807 else if(epoll_events[i].events & EPOLLIN)
1808 {
1809 if(epoll_events[i].data.fd == ril_info.sock_listen_fd) // New clients connected.
1810 {
1811 int client_fd = -1;
1812 while(1)
1813 {
1814 struct sockaddr_in cliaddr;
1815 socklen_t clilen = sizeof(cliaddr);
1816 client_fd = accept(epoll_events[i].data.fd, (struct sockaddr *) &cliaddr, &clilen);
1817 if(client_fd < 0)
1818 {
1819 if(errno == EAGAIN)
1820 {
1821 LOG("All client connect get.");
1822 }
1823 else
1824 {
1825 LOG("accept() error[%d].", errno);
1826 }
1827 break;
1828 }
1829 // Set O_NONBLOCK
1830 int flags = fcntl(client_fd, F_GETFL, 0);
1831 if (flags > 0)
1832 {
1833 flags |= O_NONBLOCK;
1834 if (fcntl(client_fd, F_SETFL, flags) < 0)
1835 {
1836 LOG("Set flags error:%d", errno);
1837 }
1838 }
1839
1840 memset(&ev,0,sizeof(struct epoll_event));
1841 ev.data.fd = client_fd;
1842 ev.events = event;//EPOLLIN | EPOLLERR | EPOLLET;
1843 epoll_ctl(ril_info.epoll_fd, EPOLL_CTL_ADD, client_fd, &ev);
1844
1845 sock_cli_info_t *info = (sock_cli_info_t*)malloc(sizeof(sock_cli_info_t));
1846 if(info)
1847 {
1848 memset(info, 0, sizeof(sock_cli_info_t));
1849 info->fd = client_fd;
1850 list_add(ril_info.sock_client_list, info);
1851 LOG("Add New Client FD Into List.");
1852
1853 // Send msg RIL_MSG_ID_IND_SER_READY to client.
1854 ril_ind_pack_send(client_fd, RIL_MSG_ID_IND_SER_READY, NULL, 0);
1855 }
1856 else
1857 {
1858 LOG("malloc() fail.");
1859 }
1860 }
1861 }
1862 else if((cli_info = cli_find(epoll_events[i].data.fd)) != NULL) // Client data arrive.
1863 {
1864 // Read and process every message.
1865 mbtk_ril_err_enum err = MBTK_RIL_ERR_SUCCESS;
1866 ril_msg_pack_info_t** pack = ril_pack_recv(cli_info->fd, true, &err);
1867
1868 // Parse packet error,send error response to client.
1869 if(pack == NULL)
1870 {
1871 ril_error_pack_send(cli_info->fd, RIL_MSG_ID_UNKNOWN, RIL_MSG_INDEX_INVALID, err);
1872 }
1873 else
1874 {
1875 ril_msg_pack_info_t** pack_ptr = pack;
1876 while(*pack_ptr)
1877 {
1878 pack_distribute(cli_info, *pack_ptr);
1879 // Not free,will free in pack_process() or packet process thread.
1880 //mbtk_info_pack_free(pack_ptr);
1881 pack_ptr++;
1882 }
1883
1884 free(pack);
1885 }
1886 }
1887 else
1888 {
1889 LOG("Unknown socket : %d", epoll_events[i].data.fd);
1890 }
1891 }
1892 else
1893 {
1894 LOG("Unknown event : %x", epoll_events[i].events);
1895 }
1896 }
1897 }
1898 else
1899 {
1900 LOG("epoll_wait() fail[%d].", errno);
1901 }
1902 }
1903
1904 return NULL;
1905}
1906
1907static void* ril_process_thread(void* arg)
1908{
1909 UNUSED(arg);
1910 ril_msg_queue_info_t* item = NULL;
1911
1912 pthread_mutex_lock(&ril_info.msg_mutex);
1913 while(TRUE)
1914 {
1915 if(mbtk_queue_empty(&ril_info.msg_queue))
1916 {
1917 LOG("Packet process wait...");
1918 pthread_cond_wait(&ril_info.msg_cond, &ril_info.msg_mutex);
1919 LOG("Packet process continue...");
1920 }
1921 else
1922 {
1923 LOG("Packet process queue not empty,continue...");
1924 }
1925
1926 // Process all information request.
1927 mbtk_ril_err_enum err;
1928 while((item = (ril_msg_queue_info_t*)mbtk_queue_get(&ril_info.msg_queue)) != NULL)
1929 {
1930 if(item->cli_info) { // REQ form client.
1931 ril_msg_pack_info_t *pack = (ril_msg_pack_info_t*)item->pack;
1932 LOGD("Process REQ %s.", id2str(pack->msg_id));
1933 ril_info.at_process = true;
1934 err = pack_req_process(item->cli_info, pack);
1935 if(err != MBTK_RIL_ERR_SUCCESS)
1936 {
1937 ril_error_pack_send(item->cli_info->fd, pack->msg_id, pack->msg_index, err);
1938 }
1939 ril_info.at_process = false;
1940 ril_msg_pack_free(pack);
1941 free(item);
1942 } else { // REQ from myself.
1943 ril_urc_msg_info_t *urc = (ril_urc_msg_info_t*)item->pack;
1944 LOGD("Process URC %d.", urc->msg);
1945 urc_msg_process(urc);
1946 if(urc->data)
1947 free(urc->data);
1948 free(urc);
1949 }
1950 }
1951 }
1952 pthread_mutex_unlock(&ril_info.msg_mutex);
1953 return NULL;
1954}
1955
1956/*
1957AT*BAND=15,78,147,482,134742231
1958
1959OK
1960*/
1961static void* band_config_thread()
1962{
1963 mbtk_device_info_modem_t info_modem;
1964 memset(&band_info.band_support, 0, sizeof(mbtk_band_info_t));
1965 memset(&info_modem, 0, sizeof(mbtk_device_info_modem_t));
1966 band_info.band_set_success = FALSE;
1967 if(mbtk_dev_info_read(MBTK_DEVICE_INFO_ITEM_MODEM, &(info_modem), sizeof(mbtk_device_info_modem_t))) {
1968 LOGD("mbtk_dev_info_read(MODEM) fail, use default band.");
1969 band_info.band_area = MBTK_MODEM_BAND_AREA_ALL;
1970 band_info.band_support.net_pref = MBTK_NET_PREF_UNUSE;
1971 band_info.band_support.gsm_band = MBTK_BAND_ALL_GSM_DEFAULT;
1972 band_info.band_support.umts_band = MBTK_BAND_ALL_WCDMA_DEFAULT;
1973 band_info.band_support.tdlte_band = MBTK_BAND_ALL_TDLTE_DEFAULT;
1974 band_info.band_support.fddlte_band = MBTK_BAND_ALL_FDDLTE_DEFAULT;
1975 band_info.band_support.lte_ext_band = MBTK_BAND_ALL_EXT_LTE_DEFAULT;
1976 } else {
1977 band_info.band_area = info_modem.band_area;
1978 band_info.band_support.net_pref = MBTK_NET_PREF_UNUSE;
1979 band_info.band_support.gsm_band = info_modem.band_gsm;
1980 band_info.band_support.umts_band = info_modem.band_wcdma;
1981 band_info.band_support.tdlte_band = info_modem.band_tdlte;
1982 band_info.band_support.fddlte_band = info_modem.band_fddlte;
1983 band_info.band_support.lte_ext_band = info_modem.band_lte_ext;
1984 }
1985
1986 bool is_first = TRUE;
1987 while(!band_info.band_set_success) {
1988 // Set band.
1989#if 0
1990 info_urc_msg_t *urc = (info_urc_msg_t*)malloc(sizeof(info_urc_msg_t));
1991 if(!urc)
1992 {
1993 LOG("malloc() fail[%d].", errno);
1994 break;
1995 } else {
1996 urc->msg = INFO_URC_MSG_SET_BAND;
1997 urc->data = NULL;
1998 urc->data_len = 0;
1999 send_pack_to_queue(NULL, urc);
2000
2001 if(is_first) {
2002 is_first = FALSE;
2003 } else {
2004 LOGE("*BAND exec error, will retry in 5s.");
2005 }
2006 sleep(5);
2007 }
2008#else
2009 sleep(5);
2010#endif
2011 }
2012
2013 LOGD("Set Band thread exit.");
2014 return NULL;
2015}
2016
2017
2018int ril_server_start()
2019{
2020 signal(SIGPIPE, SIG_IGN);
2021
2022 memset(&ril_info, 0, sizeof(ril_info_t));
2023 memset(&band_info, 0, sizeof(ril_band_info_t));
2024 memset(&band_info.band_support, 0xFF, sizeof(mbtk_band_info_t));
2025
2026 //check cfun and sim card status
2027 ril_at_ready_process();
2028
2029 //any AT instruction that is not sent through pack_process_thread needs to precede the thread
2030 //thread create
2031 if(ril_info.sock_listen_fd > 0)
2032 {
2033 LOGE("Information Server Has Started.");
2034 return -1;
2035 }
2036
2037 struct sockaddr_un server_addr;
2038 ril_info.sock_listen_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
2039 if(ril_info.sock_listen_fd < 0)
2040 {
2041 LOGE("socket() fail[%d].", errno);
2042 return -1;
2043 }
2044
2045 // Set O_NONBLOCK
2046 int flags = fcntl(ril_info.sock_listen_fd, F_GETFL, 0);
2047 if (flags < 0)
2048 {
2049 LOGE("Get flags error:%d", errno);
2050 goto error;
2051 }
2052 flags |= O_NONBLOCK;
2053 if (fcntl(ril_info.sock_listen_fd, F_SETFL, flags) < 0)
2054 {
2055 LOGE("Set flags error:%d", errno);
2056 goto error;
2057 }
2058
2059 unlink(RIL_SOCK_NAME);
2060 memset(&server_addr, 0, sizeof(struct sockaddr_un));
2061 server_addr.sun_family = AF_LOCAL;
2062 strcpy(server_addr.sun_path, RIL_SOCK_NAME);
2063 if(bind(ril_info.sock_listen_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)))
2064 {
2065 LOGE("bind() fail[%d].", errno);
2066 goto error;
2067 }
2068
2069 if(listen(ril_info.sock_listen_fd, SOCK_CLIENT_MAX))
2070 {
2071 LOGE("listen() fail[%d].", errno);
2072 goto error;
2073 }
2074
2075 ril_info.sock_client_list = list_create(sock_cli_free_func);
2076 if(ril_info.sock_client_list == NULL)
2077 {
2078 LOGE("list_create() fail.");
2079 goto error;
2080 }
2081
2082 mbtk_queue_init(&ril_info.msg_queue);
2083 pthread_mutex_init(&ril_info.msg_mutex, NULL);
2084 pthread_cond_init(&ril_info.msg_cond, NULL);
2085
2086 pthread_t info_pid, pack_pid, monitor_pid, urc_pid, bootconn_pid;
2087 pthread_attr_t thread_attr;
2088 pthread_attr_init(&thread_attr);
2089 if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
2090 {
2091 LOGE("pthread_attr_setdetachstate() fail.");
2092 goto error;
2093 }
2094
2095 if(pthread_create(&info_pid, &thread_attr, ril_read_pthread, NULL))
2096 {
2097 LOGE("pthread_create() fail.");
2098 goto error;
2099 }
2100
2101 if(pthread_create(&pack_pid, &thread_attr, ril_process_thread, NULL))
2102 {
2103 LOGE("pthread_create() fail.");
2104 goto error;
2105 }
2106
2107 // Set Band
2108 // AT*BAND=15,78,147,482,134742231
2109 char buff[10];
2110 memset(buff, 0, 10);
2111 property_get("persist.mbtk.band_config", buff, "");
2112 if(strlen(buff) == 0) {
2113 pthread_t band_pid;
2114 if(pthread_create(&band_pid, &thread_attr, band_config_thread, NULL))
2115 {
2116 LOGE("pthread_create() fail.");
2117 }
2118 }
2119
2120 pthread_attr_destroy(&thread_attr);
2121
2122 LOGD("MBTK Ril Server Start...");
2123
2124 return 0;
2125error:
2126 if(ril_info.sock_client_list) {
2127 list_free(ril_info.sock_client_list);
2128 ril_info.sock_client_list = NULL;
2129 }
2130
2131 if(ril_info.sock_listen_fd > 0) {
2132 close(ril_info.sock_listen_fd);
2133 ril_info.sock_listen_fd = -1;
2134 }
2135 return -1;
2136}
2137
2138
2139int main(int argc, char *argv[])
2140{
2141 mbtk_log_init("radio", "MBTK_RIL");
2142
2143#ifdef MBTK_DUMP_SUPPORT
2144 mbtk_debug_open(NULL, TRUE);
2145#endif
2146
2147// Using Killall,the file lock may be not release.
2148#if 0
2149 if(app_already_running(MBTK_RILD_PID_FILE)) {
2150 LOGW("daemon already running.");
2151 exit(1);
2152 }
2153#endif
2154
2155 LOGI("mbtk_rild start.");
2156
2157 if(InProduction_Mode()) {
2158 LOGI("Is Production Mode, will exit...");
2159 exit(0);
2160 }
2161
2162 ready_state_update();
2163
2164 int at_sock = openSocket("/tmp/atcmd_at");
2165 if(at_sock < 0)
2166 {
2167 LOGE("Open AT Socket Fail[%d].", errno);
2168 return -1;
2169 }
2170 int uart_sock = openSocket("/tmp/atcmd_urc");
2171 if(uart_sock < 0)
2172 {
2173 LOGE("Open Uart Socket Fail[%d].", errno);
2174 return -1;
2175 }
2176
2177 at_set_on_reader_closed(onATReaderClosed);
2178 at_set_on_timeout(onATTimeout);
2179
2180 if(at_open(at_sock, uart_sock, onUnsolicited))
2181 {
2182 LOGE("Start AT thread fail.");
2183 return -1;
2184 }
2185
2186 if(at_handshake())
2187 {
2188 LOGE("AT handshake fail.");
2189 return -1;
2190 }
2191
2192 LOGD("AT OK.");
2193
2194 if(ril_server_start())
2195 {
2196 LOGE("ril_server_start() fail.");
2197 return -1;
2198 }
2199
2200 mbtk_ril_ready();
2201
2202 while(1)
2203 {
2204 sleep(24 * 60 * 60);
2205 }
2206
2207 LOGD("!!!mbtk_ril exit!!!");
2208 return 0;
2209}