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