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