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