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