blob: 46758b4bc5885ae2b2416baa99f6eb4b26216ecf [file] [log] [blame]
b.liufd87baf2024-11-15 15:30:38 +08001/*
2* main.c
3*
4* mbtk_rild main file.
5*
6* The main functions are as follows:
7* 1. Execute boot script /etc/init.d/mbtk_boot_server_ready after AT is ready(Only execute one time).
8* 2. Execute boot script /etc/init.d/mbtk_boot_net_ready after network is ready(Only execute one time).
9* 3. Set the frequency band based on the relevant information in the "device_info" partition.
10* 4. Create socket server(The client implementation in API).
11* 5. Create 3 connections between atcmdsrv, related devices: /tmp/atcmd_at ,
12* /tmp/atcmd_at_1 , /tmp/atcmd_at_2
13*/
14/******************************************************************************
15
16 EDIT HISTORY FOR FILE
17
18 WHEN WHO WHAT,WHERE,WHY
19-------- -------- -------------------------------------------------------
202024/08/13 LiuBin Initial version
b.liu60c2f1f2024-12-31 12:54:45 +0800212024/12/31 LiuBin Add new sms urc process(+CMT)
b.liufd87baf2024-11-15 15:30:38 +080022
23******************************************************************************/
b.liu87afc4c2024-08-14 17:33:45 +080024#include <stdio.h>
25#include <stdlib.h>
26#include <unistd.h>
27#include <sys/socket.h>
28#include <errno.h>
29#include <fcntl.h>
30#include <string.h>
31#include <netinet/in.h>
32#include <arpa/inet.h>
33#include <linux/un.h>
34#include <linux/netlink.h>
35#include <cutils/properties.h>
36#include <time.h>
37#include <sys/time.h>
38#include <signal.h>
39#include <sys/epoll.h>
40#include <pthread.h>
41
42
43//#include "cploader.h"
44#include "mbtk_log.h"
45#include "mbtk_ifc.h"
46#include "mbtk_type.h"
47#include "atchannel.h"
48#include "at_tok.h"
49#include "mbtk_utils.h"
50#include "mbtk_task.h"
51#include "ril_info.h"
52#include "mbtk_ntp.h"
53#include "mbtk_net_control.h"
54#include "mbtk_ril.h"
55#include "mbtk_str.h"
56#include "mbtk_queue.h"
b.liufd87baf2024-11-15 15:30:38 +080057#include "mbtk_file.h"
b.liu87afc4c2024-08-14 17:33:45 +080058
b.liu62240ee2024-11-07 17:52:45 +080059#ifndef TEMP_FAILURE_RETRY
b.liu87afc4c2024-08-14 17:33:45 +080060#define TEMP_FAILURE_RETRY(exp) ({ \
61 typeof (exp) _rc; \
62 do { \
63 _rc = (exp); \
64 } while (_rc == -1 && errno == EINTR); \
65 _rc; })
b.liu62240ee2024-11-07 17:52:45 +080066#endif
b.liu87afc4c2024-08-14 17:33:45 +080067
68#define BUFFER_SIZE 2048
69#define UEVENT_USIM_DEV "/devices/virtual/usim_event/usim0"
70#define MBTK_BOOT_SERVER_READY "/etc/init.d/mbtk_boot_server_ready"
71#define MBTK_BOOT_NET_READY "/etc/init.d/mbtk_boot_net_ready"
72#define MBTK_RILD_PID_FILE "/var/run/mbtk_rild.pid"
73#define MBTK_RILD_FILE_NET_READY "/tmp/mbtk_rild.net_ready"
74#define MBTK_RILD_FILE_SER_READY "/tmp/mbtk_rild.ser_ready"
b.liu15f456b2024-10-31 20:16:06 +080075#define RIL_CALL_NUM_MAX 10
b.liu87afc4c2024-08-14 17:33:45 +080076
77static bool ril_net_ready = FALSE; // Only one time.
78static bool ril_server_ready = FALSE; // Only one time.
79ril_band_info_t band_info;
80ril_info_t ril_info;
b.liu15f456b2024-10-31 20:16:06 +080081static mbtk_ril_call_state_info_t call_list[RIL_CALL_NUM_MAX];
b.liuaced4f92024-12-31 11:14:51 +080082static bool cmt_found = FALSE;
83
b.liub4772072024-08-15 14:47:03 +080084extern mbtk_cell_pack_info_t cell_info;
b.liubcf86c92024-08-19 19:48:28 +080085extern ril_cgact_wait_t cgact_wait;
b.liufd87baf2024-11-15 15:30:38 +080086extern int ril_cid_start;
b.liu87afc4c2024-08-14 17:33:45 +080087
88// int urc_msg_distribute(bool async_process, info_urc_msg_id_enum msg, void *data, int data_len);
89// int mbtk_signal_log(char *data);
90int InProduction_Mode(void);
91mbtk_ril_err_enum dev_pack_req_process(sock_cli_info_t* cli_info, ril_msg_pack_info_t* pack);
92mbtk_ril_err_enum call_pack_req_process(sock_cli_info_t* cli_info, ril_msg_pack_info_t* pack);
93mbtk_ril_err_enum sim_pack_req_process(sock_cli_info_t* cli_info, ril_msg_pack_info_t* pack);
94mbtk_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 +080095mbtk_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 +080096mbtk_ril_err_enum pb_pack_req_process(sock_cli_info_t* cli_info, ril_msg_pack_info_t* pack);
97mbtk_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 +080098mbtk_ril_err_enum ecall_pack_req_process(sock_cli_info_t* cli_info, ril_msg_pack_info_t* pack);
99
b.liub171c9a2024-11-12 19:23:29 +0800100void data_call_retry(ATPortType_enum port, mbtk_ril_net_reg_state_info_t *reg_state);
b.liuafdf2c62024-11-12 11:10:44 +0800101
b.liu15f456b2024-10-31 20:16:06 +0800102void data_call_state_change_cb(int cid, bool action, bool auto_change, int reason);
103static int send_pack_to_queue(sock_cli_info_t* cli_info, void* pack);
b.liu472cfaf2024-12-19 19:08:19 +0800104int req_band_set(ATPortType_enum port, mbtk_band_info_t* band, int *cme_err);
b.liu87afc4c2024-08-14 17:33:45 +0800105
106/* Called on command thread */
107static void onATTimeout()
108{
109 LOGI("AT channel timeout; closing\n");
b.liub171c9a2024-11-12 19:23:29 +0800110 at_close(ATPORTTYPE_0);
b.liufd87baf2024-11-15 15:30:38 +0800111 at_close(ATPORTTYPE_1);
112 at_close(ATPORTTYPE_2);
b.liu87afc4c2024-08-14 17:33:45 +0800113}
114
115/* Called on command or reader thread */
116static void onATReaderClosed()
117{
118 LOGI("AT channel closed\n");
b.liub171c9a2024-11-12 19:23:29 +0800119 at_close(ATPORTTYPE_0);
b.liufd87baf2024-11-15 15:30:38 +0800120 at_close(ATPORTTYPE_1);
121 at_close(ATPORTTYPE_2);
b.liu87afc4c2024-08-14 17:33:45 +0800122}
123
b.liude989912025-02-12 14:40:42 +0800124static void sock_cli_free_func(void *data)
125{
126 if (data)
127 {
b.liu87afc4c2024-08-14 17:33:45 +0800128 sock_cli_info_t *info = (sock_cli_info_t*) data;
129 LOGD("Free Socket client[fd = %d].", info->fd);
b.liude989912025-02-12 14:40:42 +0800130 free(info);
131 }
b.liu87afc4c2024-08-14 17:33:45 +0800132}
133
b.liufd87baf2024-11-15 15:30:38 +0800134bool asr_auto_data_call_enable()
135{
136 /*
137 uci show wan_default.default.enable
138 wan_default.default.enable='1'
139
140 uci get wan_default.default.enable
141 1
142 */
143 char buff[128] = {0};
144 if(mbtk_cmd_line("uci get wan_default.default.enable", buff, sizeof(buff)) && strlen(buff) > 0) {
145 return buff[0] == '1' ? TRUE : FALSE;
146 } else {
147 return FALSE;
148 }
149}
150
b.liu87afc4c2024-08-14 17:33:45 +0800151/*
152* Will exec mbtk_boot_server_ready and mbtk_boot_net_ready if is_first_boot is true.
153*/
154static int net_ready_set()
155{
156 int ret = -1;
157 int fd = open(MBTK_RILD_FILE_NET_READY, O_CREAT | O_WRONLY | O_TRUNC, 0644);
158 if(fd > 0) {
159 if(write(fd, "1", 1) == 1) {
160 ret = 0;
161 ril_net_ready = TRUE;
162 }
163 close(fd);
164 } else {
165 LOGE("Open %s fail:%d", MBTK_RILD_FILE_NET_READY, errno);
166 }
167
168 return ret;
169}
170
171/*
172* Will exec mbtk_boot_server_ready and mbtk_boot_net_ready if is_first_boot is true.
173*/
174static int ser_ready_set()
175{
176 int ret = -1;
177 int fd = open(MBTK_RILD_FILE_SER_READY, O_CREAT | O_WRONLY | O_TRUNC, 0644);
178 if(fd > 0) {
179 if(write(fd, "1", 1) == 1) {
180 ret = 0;
181 ril_server_ready = TRUE;
182 }
183 close(fd);
184 } else {
185 LOGE("Open %s fail:%d", MBTK_RILD_FILE_SER_READY, errno);
186 }
187
188 return ret;
189}
190
191/*
192* Will exec mbtk_boot_server_ready and mbtk_boot_net_ready if is_first_boot is true.
193*/
194static void ready_state_update()
195{
196 int fd = open(MBTK_RILD_FILE_NET_READY, O_RDONLY, 0644);
197 char buff[10];
198 if(fd > 0) {
199 if(read(fd, buff, sizeof(buff)) > 0) {
200 ril_net_ready = TRUE;
201 } else {
202 ril_net_ready = FALSE;
203 }
204
205 close(fd);
206 } else {
207 ril_net_ready = FALSE;
208 LOGE("Open %s fail:%d", MBTK_RILD_FILE_NET_READY, errno);
209 }
210
211 fd = open(MBTK_RILD_FILE_SER_READY, O_RDONLY, 0644);
212 if(fd > 0) {
213 if(read(fd, buff, sizeof(buff)) > 0) {
214 ril_server_ready = TRUE;
215 } else {
216 ril_server_ready = FALSE;
217 }
218
219 close(fd);
220 } else {
221 ril_server_ready = FALSE;
222 LOGE("Open %s fail:%d", MBTK_RILD_FILE_SER_READY, errno);
223 }
224}
225
226static void mbtk_net_ready()
227{
228 // /etc/init.d/mbtk_boot_net_ready
229 if(!ril_net_ready) {
230 if(access(MBTK_BOOT_NET_READY , X_OK) == 0) {
231 LOGD("Exec : %s", MBTK_BOOT_NET_READY);
b.liu62240ee2024-11-07 17:52:45 +0800232 mbtk_system(MBTK_BOOT_NET_READY);
b.liu87afc4c2024-08-14 17:33:45 +0800233 } else {
234 LOGE("%s can not exec.", MBTK_BOOT_NET_READY);
235 }
236 net_ready_set();
237 } else {
238 LOGD("No exec : %s", MBTK_BOOT_NET_READY);
239 }
240}
241
242static void mbtk_ril_ready()
243{
244 // /etc/init.d/mbtk_boot_server_ready
245 if(!ril_server_ready) {
246 if(access(MBTK_BOOT_SERVER_READY , X_OK) == 0) {
247 LOGD("Exec : %s", MBTK_BOOT_SERVER_READY);
b.liu62240ee2024-11-07 17:52:45 +0800248 mbtk_system(MBTK_BOOT_SERVER_READY);
b.liu87afc4c2024-08-14 17:33:45 +0800249 } else {
250 LOGE("%s can not exec.", MBTK_BOOT_SERVER_READY);
251 }
252 ser_ready_set();
253 } else {
254 LOGD("No exec : %s", MBTK_BOOT_SERVER_READY);
255 }
256}
257
258static sock_cli_info_t* cli_find(int fd)
b.liude989912025-02-12 14:40:42 +0800259{
b.liu87afc4c2024-08-14 17:33:45 +0800260 sock_cli_info_t *result = NULL;
261 list_first(ril_info.sock_client_list);
262 while ((result = (sock_cli_info_t*) list_next(ril_info.sock_client_list)))
b.liude989912025-02-12 14:40:42 +0800263 {
264 if (result->fd == fd)
265 return result;
266 }
267
268 return NULL;
b.liu87afc4c2024-08-14 17:33:45 +0800269}
270
271static void cli_close(sock_cli_info_t* client)
b.liude989912025-02-12 14:40:42 +0800272{
273 struct epoll_event ev;
274 memset(&ev,0,sizeof(struct epoll_event));
275 ev.data.fd = client->fd;
276 ev.events = EPOLLIN | EPOLLERR | EPOLLET;
b.liu87afc4c2024-08-14 17:33:45 +0800277 epoll_ctl(ril_info.epoll_fd, EPOLL_CTL_DEL, client->fd, &ev);
b.liude989912025-02-12 14:40:42 +0800278
279 close(client->fd);
280
b.liu87afc4c2024-08-14 17:33:45 +0800281 if(list_remove(ril_info.sock_client_list, client))
b.liude989912025-02-12 14:40:42 +0800282 {
283 sock_cli_free_func(client);
284 }
285}
286
b.liub171c9a2024-11-12 19:23:29 +0800287static void ril_error_pack_send(ATPortType_enum port, int fd, int ril_id, int msg_index, int err)
b.liude989912025-02-12 14:40:42 +0800288{
b.liub171c9a2024-11-12 19:23:29 +0800289 ril_msg_pack_info_t* pack = ril_msg_pack_creat(port, RIL_MSG_TYPE_RSP, ril_id, msg_index, NULL, 0);
b.liude989912025-02-12 14:40:42 +0800290 if(pack)
b.liu87afc4c2024-08-14 17:33:45 +0800291 {
292 pack->err = (uint16)err;
293 ril_pack_send(fd, pack);
294 ril_msg_pack_free(pack);
b.liude989912025-02-12 14:40:42 +0800295 }
296 else
297 {
b.liu87afc4c2024-08-14 17:33:45 +0800298 LOGW("ril_msg_pack_creat() fail.");
b.liude989912025-02-12 14:40:42 +0800299 }
300}
301
b.liub171c9a2024-11-12 19:23:29 +0800302void 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 +0800303{
b.liub171c9a2024-11-12 19:23:29 +0800304 ril_msg_pack_info_t* pack = ril_msg_pack_creat(port, RIL_MSG_TYPE_RSP, ril_id, msg_index, data, data_len);
b.liude989912025-02-12 14:40:42 +0800305 if(pack)
306 {
b.liu87afc4c2024-08-14 17:33:45 +0800307 pack->err = (uint16)MBTK_RIL_ERR_SUCCESS;
308#if 0
b.liude989912025-02-12 14:40:42 +0800309 if(data != NULL && data_len > 0)
310 {
311 pack->data_len = (uint16)data_len;
b.liu87afc4c2024-08-14 17:33:45 +0800312 pack->data = (uint8*)mbtk_memcpy(data, data_len);
313 }
314#endif
315 ril_pack_send(fd, pack);
316 ril_msg_pack_free(pack);
b.liude989912025-02-12 14:40:42 +0800317 }
318 else
319 {
b.liu87afc4c2024-08-14 17:33:45 +0800320 LOGW("ril_msg_pack_creat() fail.");
b.liude989912025-02-12 14:40:42 +0800321 }
b.liu87afc4c2024-08-14 17:33:45 +0800322}
323
324void ril_ind_pack_send(int fd, int msg_id, const void* data, int data_len)
b.liude989912025-02-12 14:40:42 +0800325{
b.liub171c9a2024-11-12 19:23:29 +0800326 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.liude989912025-02-12 14:40:42 +0800327 if(pack)
328 {
b.liu87afc4c2024-08-14 17:33:45 +0800329 pack->err = (uint16)0;
330#if 0
b.liude989912025-02-12 14:40:42 +0800331 if(data != NULL && data_len > 0)
332 {
333 pack->data_len = (uint16)data_len;
b.liu87afc4c2024-08-14 17:33:45 +0800334 pack->data = (uint8*)mbtk_memcpy(data, data_len);
335 }
336#endif
337 ril_pack_send(fd, pack);
338 ril_msg_pack_free(pack);
b.liude989912025-02-12 14:40:42 +0800339 }
340 else
341 {
b.liu87afc4c2024-08-14 17:33:45 +0800342 LOGW("ril_msg_pack_creat() fail.");
b.liude989912025-02-12 14:40:42 +0800343 }
b.liu87afc4c2024-08-14 17:33:45 +0800344}
345
b.liufd87baf2024-11-15 15:30:38 +0800346void ril_state_change(ril_msg_id_enum msg_id, const void *data, int data_len)
b.liude989912025-02-12 14:40:42 +0800347{
b.liu15f456b2024-10-31 20:16:06 +0800348 sock_cli_info_t *cli = NULL;
349 list_first(ril_info.sock_client_list);
350 while ((cli = (sock_cli_info_t*) list_next(ril_info.sock_client_list)))
b.liude989912025-02-12 14:40:42 +0800351 {
352 if(cli->ind_num > 0) {
353 int i;
354 for(i = 0; i < IND_REGISTER_MAX; i++) {
b.liu15f456b2024-10-31 20:16:06 +0800355 if(cli->ind_register[i] == msg_id) {
356 ril_ind_pack_send(cli->fd, msg_id, data, data_len);
b.liude989912025-02-12 14:40:42 +0800357 break;
358 }
359 }
360 }
361 }
b.liu15f456b2024-10-31 20:16:06 +0800362}
363
b.liu9e8584b2024-11-06 19:21:28 +0800364static 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 +0800365{
366 // Send urc msg to client.
367 if(msg_id > RIL_MSG_ID_IND_BEGIN && msg_id < RIL_MSG_ID_IND_END) {
b.liufd87baf2024-11-15 15:30:38 +0800368 if(msg_id == RIL_MSG_ID_IND_PDP_STATE_CHANGE) {
369 mbtk_ril_pdp_state_info_t *info = (mbtk_ril_pdp_state_info_t*)data;
370 if(info->action && !info->ip_info_valid) {
371 LOGD("PDP action but no IP information, not send.");
372 } else {
373 ril_state_change(msg_id, data, data_len);
374 }
375 } else {
376 ril_state_change(msg_id, data, data_len);
377 }
b.liu15f456b2024-10-31 20:16:06 +0800378 }
379
380 // Async process urc msg.
b.liude989912025-02-12 14:40:42 +0800381 if(async_process) {
b.liu15f456b2024-10-31 20:16:06 +0800382 ril_urc_msg_info_t *msg = (ril_urc_msg_info_t*)malloc(sizeof(ril_urc_msg_info_t));
383 if(msg) {
384 msg->msg = msg_id;
385 msg->data = mbtk_memcpy(data, data_len);
386 msg->data_len = data_len;
387 if(msg->data == NULL) {
388 LOGE("mbtk_memcpy() fail.");
389 return -1;
390 }
391
392 return send_pack_to_queue(NULL, msg);
393 } else {
394 LOGE("malloc() fail.");
395 return -1;
396 }
397 }
398
399 return 0;
400}
401
402// *SIMDETEC:1,SIM
403// *EUICC:1
404// +CPIN: SIM PIN
405static void urc_sim_state_change_process(const char *s, const char *sms_pdu)
406{
407 mbtk_ril_sim_state_info_t state;
408 memset(&state, 0, sizeof(mbtk_ril_sim_state_info_t));
409 state.sim_type = MBTK_UNKNOWN;
410
b.liufd87baf2024-11-15 15:30:38 +0800411 char* tmp_s = memdup(s,strlen(s) + 1);
b.liu15f456b2024-10-31 20:16:06 +0800412 char *line = tmp_s;
413 int tmp_int;
414 char *tmp_str;
415
416 if(strStartsWith(s, "*SIMDETEC:")) {
417 if (at_tok_start(&line) < 0)
418 {
419 goto SIM_STATE_EXIT;
420 }
421 if (at_tok_nextint(&line, &tmp_int) < 0)
422 {
423 goto SIM_STATE_EXIT;
424 }
425 if (at_tok_nextstr(&line, &tmp_str) < 0)
426 {
427 goto SIM_STATE_EXIT;
428 }
429
430 if(tmp_str) {
431 if(strcmp(tmp_str, "NOS") == 0) {
432 state.sim_type = ril_info.sim_type;
433 state.sim_state = MBTK_SIM_STATE_ABSENT;
434 ril_info.sim_state = MBTK_SIM_STATE_ABSENT;
435 urc_msg_distribute(false, RIL_MSG_ID_IND_SIM_STATE_CHANGE, &state, sizeof(mbtk_ril_sim_state_info_t));
436 } else if(strcmp(tmp_str, "SIM") == 0) {
437 state.sim_type = ril_info.sim_type;
438 state.sim_state = MBTK_SIM_STATE_NOT_READY;
439 ril_info.sim_state = MBTK_SIM_STATE_NOT_READY;
440 urc_msg_distribute(false, RIL_MSG_ID_IND_SIM_STATE_CHANGE, &state, sizeof(mbtk_ril_sim_state_info_t));
441 }
442 }
443 } else if(strStartsWith(s, "+CPIN:")){
444 if(strStartsWith(s, "+CPIN: READY"))
445 {
446 state.sim_state = MBTK_SIM_STATE_READY;
447 }
448 else if(strStartsWith(s, "+CPIN: SIM PIN"))
449 {
450 state.sim_state = MBTK_SIM_STATE_SIM_PIN;
451 }
452 else if(strStartsWith(s, "+CPIN: SIM PUK"))
453 {
454 state.sim_state = MBTK_SIM_STATE_SIM_PUK;
455 }
456 else if(strStartsWith(s, "+CPIN: PH-SIMLOCK PIN"))
457 {
458 state.sim_state = MBTK_SIM_STATE_PH_SIMLOCK_PIN;
459 }
460 else if(strStartsWith(s, "+CPIN: PH-SIMLOCK PUK"))
461 {
462 state.sim_state = MBTK_SIM_STATE_PH_SIMLOCK_PUK;
463 }
464 else if(strStartsWith(s, "+CPIN: PH-FSIM PIN"))
465 {
466 state.sim_state = MBTK_SIM_STATE_PH_FSIM_PIN;
467 }
468 else if(strStartsWith(s, "+CPIN: PH-FSIM PUK"))
469 {
470 state.sim_state = MBTK_SIM_STATE_PH_FSIM_PUK;
471 }
472 else if(strStartsWith(s, "+CPIN: SIM PIN2"))
473 {
474 state.sim_state = MBTK_SIM_STATE_SIM_PIN2;
475 }
476 else if(strStartsWith(s, "+CPIN: SIM PUK2"))
477 {
478 state.sim_state = MBTK_SIM_STATE_SIM_PUK2;
479 }
480 else if(strStartsWith(s, "+CPIN: PH-NET PIN"))
481 {
482 state.sim_state = MBTK_SIM_STATE_PH_NET_PIN;
483 }
484 else if(strStartsWith(s, "+CPIN: PH-NET PUK"))
485 {
486 state.sim_state = MBTK_SIM_STATE_PH_NET_PUK;
487 }
488 else if(strStartsWith(s, "+CPIN: PH-NETSUB PIN"))
489 {
490 state.sim_state = MBTK_SIM_STATE_PH_NETSUB_PIN;
491 }
492 else if(strStartsWith(s, "+CPIN: PH-NETSUB PUK"))
493 {
494 state.sim_state = MBTK_SIM_STATE_PH_NETSUB_PUK;
495 }
496 else if(strStartsWith(s, "+CPIN: PH-SP PIN"))
497 {
498 state.sim_state = MBTK_SIM_STATE_PH_SP_PIN;
499 }
500 else if(strStartsWith(s, "+CPIN: PH-SP PUK"))
501 {
502 state.sim_state = MBTK_SIM_STATE_PH_SP_PUK;
503 }
504 else if(strStartsWith(s, "+CPIN: PH-CORP PIN"))
505 {
506 state.sim_state = MBTK_SIM_STATE_PH_CORP_PIN;
507 }
508 else if(strStartsWith(s, "+CPIN: PH-CORP PUK"))
509 {
510 state.sim_state = MBTK_SIM_STATE_PH_CORP_PUK;
511 }
512 else if(strStartsWith(s, "+CPIN: SIM REMOVED"))
513 {
514 state.sim_state = MBTK_SIM_STATE_ABSENT;
515 }
516
517 state.sim_type = ril_info.sim_type;
518 ril_info.sim_state = state.sim_state;
519
b.liufd87baf2024-11-15 15:30:38 +0800520 urc_msg_distribute(true, RIL_MSG_ID_IND_SIM_STATE_CHANGE, &state, sizeof(mbtk_ril_sim_state_info_t));
b.liu15f456b2024-10-31 20:16:06 +0800521 } else if(strStartsWith(s, "*EUICC:")){
522 if (at_tok_start(&line) < 0)
523 {
524 goto SIM_STATE_EXIT;
525 }
526 if (at_tok_nextint(&line, &tmp_int) < 0)
527 {
528 goto SIM_STATE_EXIT;
529 }
530 ril_info.sim_type = (mbtk_sim_card_type_enum)tmp_int;
531 } else {
532 LOGW("Unknown URC.");
533 }
534
535SIM_STATE_EXIT:
536 free(tmp_s);
537}
538
539// +CLCC: 1, 1, 6, 0, 0, "18981911691", 129, "",, 0
540// +CALLDISCONNECT: 1
541// +CPAS: 4
542static void urc_call_state_change_process(const char *s, const char *sms_pdu)
543{
b.liufd87baf2024-11-15 15:30:38 +0800544 char* tmp_s = memdup(s,strlen(s) + 1);
b.liu15f456b2024-10-31 20:16:06 +0800545 char *line = tmp_s;
546 int tmp_int;
547 char *tmp_str;
548
549 if(strStartsWith(s, "+CLCC:")) {
550 if (at_tok_start(&line) < 0)
551 {
552 goto CALL_STATE_EXIT;
553 }
554 if (at_tok_nextint(&line, &tmp_int) < 0) // call id
555 {
556 goto CALL_STATE_EXIT;
557 }
558
559 int i = 0;
560 while(i < RIL_CALL_NUM_MAX) {
561 if(call_list[i].call_id == tmp_int)
562 break;
563 i++;
564 }
565 if(i == RIL_CALL_NUM_MAX) { // No found this call id.
566 i = 0;
567 while(i < RIL_CALL_NUM_MAX) { // Find next empty call item.
568 if(call_list[i].call_id == 0)
569 break;
570 i++;
571 }
572 call_list[i].call_id = tmp_int;
573 }
574
575 LOGD("Found call id : %d", call_list[i].call_id);
576
577 if (at_tok_nextint(&line, &tmp_int) < 0) // dir
578 {
579 goto CALL_STATE_EXIT;
580 }
581 call_list[i].dir = (mbtk_ril_call_dir_enum)tmp_int;
582
583 if (at_tok_nextint(&line, &tmp_int) < 0) // state
584 {
585 goto CALL_STATE_EXIT;
586 }
587 call_list[i].state = (mbtk_ril_call_state_enum)tmp_int;
588
589 if (at_tok_nextint(&line, &tmp_int) < 0) // mode
590 {
591 goto CALL_STATE_EXIT;
592 }
593
594 if (at_tok_nextint(&line, &tmp_int) < 0) // mpty
595 {
596 goto CALL_STATE_EXIT;
597 }
598
599 if (at_tok_nextstr(&line, &tmp_str) < 0) // number
600 {
601 goto CALL_STATE_EXIT;
602 }
603 memset(call_list[i].call_number, 0, sizeof(call_list[i].call_number));
604 if(tmp_str && strlen(tmp_str) > 0) {
605 memcpy(call_list[i].call_number, tmp_str, strlen(tmp_str));
606 }
607
608 if (at_tok_nextint(&line, &tmp_int) < 0) // type
609 {
610 goto CALL_STATE_EXIT;
611 }
612 call_list[i].num_type = (mbtk_ril_call_num_type_enum)tmp_int;
613
614 urc_msg_distribute(false, RIL_MSG_ID_IND_CALL_STATE_CHANGE, &(call_list[i]), sizeof(mbtk_ril_call_state_info_t));
615 } else if(strStartsWith(s, "+CALLDISCONNECT:")){
616 if (at_tok_start(&line) < 0)
617 {
618 goto CALL_STATE_EXIT;
619 }
620 if (at_tok_nextint(&line, &tmp_int) < 0) // call id
621 {
622 goto CALL_STATE_EXIT;
623 }
624
625 int i = 0;
626 while(i < RIL_CALL_NUM_MAX) {
627 if(call_list[i].call_id == tmp_int)
628 break;
629 i++;
630 }
631
632 if(i == RIL_CALL_NUM_MAX) { // No found this call id.
633 LOGE("No found this call id : %d", tmp_int);
634 goto CALL_STATE_EXIT;
635 }
636
637 call_list[i].state = MBTK_RIL_CALL_STATE_DISCONNECT;
638
639 urc_msg_distribute(false, RIL_MSG_ID_IND_CALL_STATE_CHANGE, &(call_list[i]), sizeof(mbtk_ril_call_state_info_t));
640
641 // Reset after call disconnect.
642 memset(&(call_list[i]), 0, sizeof(mbtk_ril_call_state_info_t));
643 } else if(strStartsWith(s, "+CPAS:")){
644
645 } else {
646 LOGW("Unknown URC.");
647 }
648
649CALL_STATE_EXIT:
650 free(tmp_s);
651}
652
653// *ECALLDATA: <urc_id>[,<urc_data>]
654static void urc_ecall_state_change_process(const char *s, const char *sms_pdu)
655{
656 mbtk_ril_ecall_state_info_t ecall_state;
657 memset(&ecall_state, 0, sizeof(mbtk_ril_ecall_state_info_t));
658
b.liufd87baf2024-11-15 15:30:38 +0800659 char* tmp_s = memdup(s,strlen(s) + 1);
b.liu15f456b2024-10-31 20:16:06 +0800660 char *line = tmp_s;
661 int tmp_int;
662 char *tmp_str;
663 if (at_tok_start(&line) < 0)
664 {
665 goto ECALLDATA_EXIT;
666 }
667 if (at_tok_nextint(&line, &tmp_int) < 0)
668 {
669 goto ECALLDATA_EXIT;
670 }
671 ecall_state.urc_id = (uint8)tmp_int; // urc_id
672 if (at_tok_nextstr(&line, &tmp_str) < 0)
673 {
674 goto ECALLDATA_EXIT;
675 }
676
677 if(tmp_str && strlen(tmp_str) > 0) {
678 memcpy(ecall_state.urc_data, tmp_str, strlen(tmp_str));
679 }
680
681 urc_msg_distribute(false, RIL_MSG_ID_IND_ECALL_STATE_CHANGE, &ecall_state, sizeof(mbtk_ril_ecall_state_info_t));
682ECALLDATA_EXIT:
683 free(tmp_s);
684}
685
686// +CMT: ,23
687// 0891683108200855F6240D91688189911196F10000221130717445230331D90C
b.liu0bb1c8e2024-12-31 14:44:40 +0800688static void urc_sms_state_change_process(const char *s, bool is_pdu)
b.liu15f456b2024-10-31 20:16:06 +0800689{
b.liu0bb1c8e2024-12-31 14:44:40 +0800690 static mbtk_ril_sms_state_info_t sms_info;
691 if(!is_pdu) {
b.liu60c2f1f2024-12-31 12:54:45 +0800692 memset(&sms_info, 0, sizeof(mbtk_ril_sms_state_info_t));
693 char* tmp_s = memdup(s,strlen(s) + 1);
694 char *line = tmp_s;
695 char *tmp_str;
696 if (at_tok_start(&line) < 0)
697 {
698 goto CMT_EXIT;
699 }
700 if (at_tok_nextstr(&line, &tmp_str) < 0)
701 {
702 goto CMT_EXIT;
703 }
704 if (at_tok_nextstr(&line, &tmp_str) < 0)
705 {
706 goto CMT_EXIT;
707 }
708 sms_info.pdu_len = (uint16)atoi(tmp_str);
b.liu60c2f1f2024-12-31 12:54:45 +0800709 CMT_EXIT:
710 free(tmp_s);
b.liu0bb1c8e2024-12-31 14:44:40 +0800711 } else {
712 LOGD("PDU : %s", s);
713 memcpy(sms_info.pdu, s, strlen(s));
714 urc_msg_distribute(false, RIL_MSG_ID_IND_SMS_STATE_CHANGE, &sms_info, sizeof(mbtk_ril_sms_state_info_t));
b.liu60c2f1f2024-12-31 12:54:45 +0800715 }
b.liu15f456b2024-10-31 20:16:06 +0800716}
717
718// +CREG: 1, "8010", "000060a5", 0, 2, 0
719// +CREG: 1, "8330", "06447347", 7, 2, 0
720// +CEREG: 1, "8330", "06447347", 7
721// $CREG: 1, "8330", "06447347", 7,"0d4", 2, 0
722// $CREG: 1, "8010", "000060a7", 0,, 2, 0
723// +CGREG: 1
724static void urc_net_reg_state_change_process(const char *s, const char *sms_pdu)
725{
726 mbtk_ril_net_reg_state_info_t state;
727 memset(&state, 0, sizeof(mbtk_ril_net_reg_state_info_t));
728 state.tech = MBTK_RADIO_TECH_UNKNOWN;
729
730 if(strStartsWith(s, "+CREG:"))
731 {
732 state.type = MBTK_NET_REG_TYPE_CALL;
733 } else if(strStartsWith(s, "+CGREG:")) {
734 state.type = MBTK_NET_REG_TYPE_DATA_GSM_WCDMA;
b.liude989912025-02-12 14:40:42 +0800735 } else if(strStartsWith(s, "+C5GREG:")) {
736 state.type = MBTK_NET_REG_TYPE_DATA_NR;
b.liu15f456b2024-10-31 20:16:06 +0800737 } else {
738 state.type = MBTK_NET_REG_TYPE_DATA_LTE;
739 }
740
b.liufd87baf2024-11-15 15:30:38 +0800741 char* tmp_s = memdup(s,strlen(s) + 1);
b.liu15f456b2024-10-31 20:16:06 +0800742 char *line = tmp_s;
743 int tmp_int;
744 char *tmp_str;
745 if (at_tok_start(&line) < 0)
746 {
747 goto CGREG_EXIT;
748 }
749 if (at_tok_nextint(&line, &tmp_int) < 0)
750 {
751 goto CGREG_EXIT;
752 }
753 state.reg_state = (mbtk_net_reg_state_enum)tmp_int; // Reg State.
b.liude989912025-02-12 14:40:42 +0800754 if (state.reg_state && at_tok_hasmore(&line)) // Reg
b.liu15f456b2024-10-31 20:16:06 +0800755 {
b.liude989912025-02-12 14:40:42 +0800756 if (at_tok_nextstr(&line, &tmp_str) < 0) // tac
b.liu15f456b2024-10-31 20:16:06 +0800757 {
758 goto CGREG_EXIT;
759 }
b.liude989912025-02-12 14:40:42 +0800760 state.tac = (uint64)strtoull(tmp_str, NULL, 16);
b.liufd87baf2024-11-15 15:30:38 +0800761
b.liude989912025-02-12 14:40:42 +0800762 if (at_tok_nextstr(&line, &tmp_str) < 0) // ci
b.liu15f456b2024-10-31 20:16:06 +0800763 {
764 goto CGREG_EXIT;
765 }
b.liude989912025-02-12 14:40:42 +0800766 state.ci = (uint64)strtoull(tmp_str, NULL, 16);
b.liufd87baf2024-11-15 15:30:38 +0800767
b.liu15f456b2024-10-31 20:16:06 +0800768 if (at_tok_nextint(&line, &tmp_int) < 0)
769 {
770 goto CGREG_EXIT;
771 }
b.liufd87baf2024-11-15 15:30:38 +0800772
b.liu15f456b2024-10-31 20:16:06 +0800773 state.tech = (mbtk_radio_technology_enum)tmp_int; // AcT
774 }
775
b.liu62240ee2024-11-07 17:52:45 +0800776 if(state.reg_state == MBTK_NET_REG_STATE_HOME
777 || state.reg_state == MBTK_NET_REG_STATE_ROAMING) {
778 mbtk_net_ready();
779 }
780
b.liuafdf2c62024-11-12 11:10:44 +0800781 // Should restart data call if necessary.
782 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 +0800783CGREG_EXIT:
784 free(tmp_s);
785}
786
787static void urc_pdp_state_change_process(const char *s, const char *sms_pdu)
b.liubcf86c92024-08-19 19:48:28 +0800788{
789 // "CONNECT"
790 if(strStartsWith(s, "CONNECT"))
791 {
792#if 1
793 if(cgact_wait.waitting && cgact_wait.act) {
794 cgact_wait.waitting = false;
795 }
796#endif
797 }
798 // +CGEV:
799 // +CGEV: NW DEACT <cid>,<cid>
800 // +CGEV: ME DEACT <cid>,<cid>
801 // +CGEV: NW PDN DEACT <cid>
802 // +CGEV: ME PDN DEACT <cid>
803 // +CGEV: NW DETACH
804 // +CGEV: ME DETACH
805 //
806 // +CGEV: NW ACT <cid>,<cid>
807 // +CGEV: ME ACT <cid>,<cid>
808 // +CGEV: EPS PDN ACT <cid>
809 // +CGEV: ME PDN ACT <cid>,<reason>,<cid>
810 // +CGEV: ME PDN ACT <cid>,<reason>
811 // +CGEV: NW PDN ACT <cid>
812 // +CGEV: EPS ACT <cid>
813 // +CGEV: NW MODIFY <cid>,<reason>
814 // +CGEV: NW REATTACH
815
816 /*
817 +CGEV: NW DETACH
818 +CGEV: ME DETACH
819 +CGEV: NW CLASS <class>
820 +CGEV: ME CLASS <class>
821 +CGEV: NW PDN ACT <cid>
822 +CGEV: ME PDN ACT <cid>[,<reason>[,<cid_other>]]
823 +CGEV: NW ACT <p_cid>, <cid>, <event_type>
824 +CGEV: ME ACT <p_cid>, <cid>, <event_type>
825 +CGEV: NW DEACT <PDP_type>, <PDP_addr>, [<cid>]
826 +CGEV: ME DEACT <PDP_type>, <PDP_addr>, [<cid>]
827 +CGEV: NW PDN DEACT <cid>
828 +CGEV: NW DEACT <PDP_type>, <PDP_addr>, [<cid>]
829 +CGEV: ME PDN DEACT <cid>
830 +CGEV: ME DEACT <PDP_type>, <PDP_addr>, [<cid>]
831 +CGEV: NW DEACT <p_cid>, <cid>, <event_type>
832 +CGEV: NW DEACT <PDP_type>, <PDP_addr>, [<cid>]
833 +CGEV: ME DEACT <p_cid>, <cid>, <event_type>
834 +CGEV: ME DEACT <PDP_type>, <PDP_addr>, [<cid>]
835 +CGEV: NW MODIFY <cid>, <change_reason>, <event_type>
836 +CGEV: ME MODIFY <cid>, <change_reason>, <event_type>
837 +CGEV: REJECT <PDP_type>, <PDP_addr>
838 +CGEV: NW REACT <PDP_type>, <PDP_addr>, [<cid>]
839 */
840 else if(strStartsWith(s, "+CGEV:"))
841 {
842#if 1
843 // "+CGEV: ME PDN ACT ")) { // +CGEV: ME PDN ACT <cid>[,<reason>[,<cid_other>]]
844 // "+CGEV: NW MODIFY ")) { // +CGEV: NW MODIFY <cid>, <change_reason>, <event_type>
845 // "+CGEV: ME PDN DEACT ")) { // +CGEV: ME PDN DEACT 1
846 // "+CGEV: NW PDN DEACT ")) { // +CGEV: NW PDN DEACT <cid>
847 // "+CGEV: EPS PDN ACT ")) { // +CGEV: EPS PDN ACT <cid>
848 // "+CGEV: ME PDN DEACT ")) { // +CGEV: EPS PDN DEACT <cid>
849 // "+CGEV: ME PDN ACT ")) { // +CGEV: ME PDN ACT <cid>,1
b.liu15f456b2024-10-31 20:16:06 +0800850 mbtk_ril_pdp_state_info_t cgev_info;
851 memset(&cgev_info, 0, sizeof(mbtk_ril_pdp_state_info_t));
b.liu62240ee2024-11-07 17:52:45 +0800852 int cid, reason = 0;
853 memset(&cgev_info, 0, sizeof(mbtk_ril_pdp_state_info_t));
854 if (sscanf(s, "+CGEV: NW PDN DEACT %d", &cid) == 1) {
855 cgev_info.cid = (uint16)cid;
b.liu15f456b2024-10-31 20:16:06 +0800856 cgev_info.action = FALSE;
b.liu62240ee2024-11-07 17:52:45 +0800857 } else if (sscanf(s, "+CGEV: ME PDN DEACT %d", &cid) == 1) {
858 cgev_info.cid = (uint16)cid;
b.liu15f456b2024-10-31 20:16:06 +0800859 cgev_info.action = FALSE;
b.liu62240ee2024-11-07 17:52:45 +0800860 } else if(sscanf(s, "+CGEV: ME PDN ACT %d,%d", &cid, &reason) == 2
861 || sscanf(s, "+CGEV: ME PDN ACT %d", &cid) == 1) {
862 cgev_info.cid = (uint16)cid;
863 cgev_info.reason = (uint16)reason;
b.liu15f456b2024-10-31 20:16:06 +0800864 cgev_info.action = TRUE;
b.liubcf86c92024-08-19 19:48:28 +0800865 } else if (!strcmp(s, "+CGEV: ME DETACH")) {
866 if(cgact_wait.waitting) {
b.liu15f456b2024-10-31 20:16:06 +0800867 cgev_info.cid = cgact_wait.cid;
b.liubcf86c92024-08-19 19:48:28 +0800868 }
b.liu15f456b2024-10-31 20:16:06 +0800869 cgev_info.action = FALSE;
b.liu62240ee2024-11-07 17:52:45 +0800870 } else if (sscanf(s, "+CGEV: NW MODIFY %d,%d", &cid, &reason) == 2) {
871 cgev_info.cid = (uint16)cid;
872 cgev_info.reason = (uint16)reason;
b.liu15f456b2024-10-31 20:16:06 +0800873 cgev_info.action = TRUE;
b.liu62240ee2024-11-07 17:52:45 +0800874 } else if(sscanf(s, "+CGEV: EPS PDN ACT %d", &cid) == 1) {
875 cgev_info.cid = (uint16)cid;
b.liu15f456b2024-10-31 20:16:06 +0800876 cgev_info.action = TRUE;
b.liubcf86c92024-08-19 19:48:28 +0800877 } else {
878 LOGD(">>>>>>>>>No process +CGEV <<<<<<<<<");
879 return;
880 }
b.liu15f456b2024-10-31 20:16:06 +0800881 cgev_info.auto_change = !cgact_wait.waitting;
b.liubcf86c92024-08-19 19:48:28 +0800882
883 if(cgact_wait.act) {
b.liu15f456b2024-10-31 20:16:06 +0800884 if(cgact_wait.waitting && cgev_info.action && cgact_wait.cid == cgev_info.cid) {
b.liubcf86c92024-08-19 19:48:28 +0800885 cgact_wait.waitting = false;
886 }
887 } else {
b.liu15f456b2024-10-31 20:16:06 +0800888 if(cgact_wait.waitting && !cgev_info.action && cgact_wait.cid == cgev_info.cid) {
b.liubcf86c92024-08-19 19:48:28 +0800889 cgact_wait.waitting = false;
890 }
891 }
892
b.liu15f456b2024-10-31 20:16:06 +0800893 LOGD("+CGEV:cid - %d, act - %d, auto_change - %d, reason - %d", cgev_info.cid, cgev_info.action,
894 cgev_info.auto_change, cgev_info.reason);
b.liu15f456b2024-10-31 20:16:06 +0800895 if(cgev_info.cid >= MBTK_APN_CID_MIN && cgev_info.cid <= MBTK_APN_CID_MAX) {
b.liuafdf2c62024-11-12 11:10:44 +0800896 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 +0800897 urc_msg_distribute(false, RIL_MSG_ID_IND_PDP_STATE_CHANGE, &cgev_info, sizeof(mbtk_ril_pdp_state_info_t));
898 }
899
b.liubcf86c92024-08-19 19:48:28 +0800900#else
901 if(at_process) {
902 if(cgact_wait.act) {
903 if(strStartsWith(s, "+CGEV: ME PDN ACT ")) { // +CGEV: ME PDN ACT 15,4
904 if(cgact_wait.cid == atoi(s + 18)) {
905 cgact_wait.waitting = false;
906 }
907
908 uint8 data_pdp;
909 char* tmp_s = memdup(s + 18,strlen(s + 18));
910 char* free_ptr = tmp_s;
911 char *line = tmp_s;
912 int tmp_int;
913 if (at_tok_start(&line) < 0)
914 {
915 goto at_PDP_CREG_EXIT;
916 }
917 if (at_tok_nextint(&line, &tmp_int) < 0)
918 {
919 goto at_PDP_CREG_EXIT;
920 }
921 if (at_tok_nextint(&line, &tmp_int) < 0)
922 {
923 goto at_PDP_CREG_EXIT;
924 }
925 data_pdp = tmp_int;
926at_PDP_CREG_EXIT:
927 free(free_ptr);
928
929 //data_pdp = (uint8)atoi(s + 20); //reason
930 if(cgact_wait.cid >= 1 && cgact_wait.cid < 8)
931 {
932 if(data_pdp == 0)
933 {
934 data_pdp = 25;
935 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
936 //data_pdp = cgact_wait.cid + 200;
937 }
938 else if(data_pdp == 1)
939 {
940 data_pdp = 26;
941 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
942 }
943 else if(data_pdp == 2)
944 {
945 data_pdp = 27;
946 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
947 }
948 else if(data_pdp == 3)
949 {
950 data_pdp = 27;
951 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
952 }
953 else
954 {
955
956 }
957 if(cgact_wait.cid != 0)
958 {
959 data_pdp = cgact_wait.cid + 200;
960 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
961 }
962 }
963 } else if(strStartsWith(s, "+CGEV: NW MODIFY ")) { // +CGEV: NW MODIFY 1,4
964 if(cgact_wait.cid == atoi(s + 17)) {
965 cgact_wait.waitting = false;
966 }
967 }
968 } else {
969 if(strStartsWith(s, "+CGEV: ME PDN DEACT ")) { // +CGEV: ME PDN DEACT 1
970 if(cgact_wait.cid == atoi(s + 20)) {
971 cgact_wait.waitting = false;
972 }
973 uint8 data_pdp;
974 data_pdp = 0; //
975 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
976 if(cgact_wait.cid != 0)
977 {
978 data_pdp = cgact_wait.cid + 100;
979 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
980 }
981 }
982 }
983 } else {
984 // apn_state_set
985
986 // +CGEV: NW PDN DEACT <cid>
987
988 // +CGEV: EPS PDN ACT 1
989 // +CGEV: ME PDN ACT 8,1
990
991 // +CGEV: ME PDN ACT 2,4
992 uint8 data[2] = {0xFF};
993 if(strStartsWith(s, "+CGEV: NW PDN DEACT ")) { // +CGEV: NW PDN DEACT <cid>
994 //apn_state_set(atoi(s + 20), false);
995 data[0] = (uint8)0;
996 data[1] = (uint8)atoi(s + 20);
997
998 uint8 data_pdp;
999 data_pdp = 0; //
1000 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
1001 data_pdp = data[1] + 100;
1002 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
1003 } else if(strStartsWith(s, "+CGEV: EPS PDN ACT ")) { // +CGEV: EPS PDN ACT <cid>
1004 //apn_state_set(atoi(s + 19), true);
1005#if (defined(MBTK_AF_SUPPORT) || defined(MBTK_ALL_CID_SUPPORT))
1006 //data[0] = (uint8)1;
1007 //data[1] = (uint8)atoi(s + 19);
1008#else
1009 data[0] = (uint8)1;
1010 data[1] = (uint8)atoi(s + 19);
1011#endif
1012 } else if(strStartsWith(s, "+CGEV: ME PDN DEACT ")) { // +CGEV: EPS PDN DEACT <cid>
1013 //apn_state_set(atoi(s + 19), true);
1014 data[0] = (uint8)0;
1015 data[1] = (uint8)atoi(s + 20);
1016
1017 uint8 data_pdp;
1018 data_pdp = 0; //
1019 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
1020 data_pdp = data[1] + 100;
1021 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
1022 } else if(strStartsWith(s, "+CGEV: ME PDN ACT ")) { // +CGEV: ME PDN ACT <cid>,1
1023 //apn_state_set(atoi(s + 18), true);
1024 data[0] = (uint8)1;
1025 data[1] = (uint8)atoi(s + 18);
1026
1027 uint8 data_pdp;
1028 char* tmp_s = memdup(s + 18,strlen(s + 18));
1029 char* free_ptr = tmp_s;
1030 char *line = tmp_s;
1031 int tmp_int;
1032 if (at_tok_start(&line) < 0)
1033 {
1034 goto PDP_CREG_EXIT;
1035 }
1036 if (at_tok_nextint(&line, &tmp_int) < 0)
1037 {
1038 goto PDP_CREG_EXIT;
1039 }
1040 if (at_tok_nextint(&line, &tmp_int) < 0)
1041 {
1042 goto PDP_CREG_EXIT;
1043 }
1044 data_pdp = tmp_int;
1045PDP_CREG_EXIT:
1046 free(free_ptr);
1047 //data_pdp = (uint8)atoi(s + 20); //reason
1048 if(data[1] >= 1 && data[1] < 8)
1049 {
1050 if(data_pdp == 0)
1051 {
1052 data_pdp = 25;
1053 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
1054 }
1055 else if(data_pdp == 1)
1056 {
1057 data_pdp = 26;
1058 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
1059 }
1060 else if(data_pdp == 2)
1061 {
1062 data_pdp = 27;
1063 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
1064 }
1065 else if(data_pdp == 3)
1066 {
1067 data_pdp = 27;
1068 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
1069 }
1070 else
1071 {
1072
1073 }
1074
1075 data_pdp = data[1] + 200;
1076 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
1077 data[1] = 0;
1078 }
1079 } else {
1080 LOGI("No process : %s", s);
1081 }
1082
1083 urc_msg_distribute(true, INFO_URC_MSG_CGEV, data, sizeof(uint8) * 2);
1084 }
1085#endif
1086 } else {
1087 LOGW("Unknown PDP URC : %s", s);
1088 }
1089}
1090
1091
1092static void urc_cell_info_process(const char *s, const char *sms_pdu)
b.liub4772072024-08-15 14:47:03 +08001093{
b.liude989912025-02-12 14:40:42 +08001094 // +EEMNRSVC: <mcc>,<lenOfMnc>,<mnc>,<tac>,<phyCellId>,<dlNrArfcn>,<dlScs>,<ulNrArfcn>,<ulScs>,<sulNrArfcn>,<sulScs>,<band>,<dlBandwidth>,<cellId>,<IsRedCapCell>,<longDRXCyclePresent>,<shortDRXCyclePresent>,<longDRXCycle>,<shortDRXCycle>,<pagingCycle>,
1095 // <rsrp>,<rsrq>,<sinr>,<rssi>,<qRxLevMin>,<qQualMin>,<srxlev>,
1096 // <pathLoss>,
1097 // <dlBler>,<averDlPRB>,<averDlMcs>,<averDlPortNum>,<averCQI>,<averLi>,<averRi>,<dlThroughPut>,<dlPeakThroughPut>,
1098 // <ulBler>,<averUlPRB>,<averUlMcs>,<averUlPortNum>,<currPuschTxPower>,<currPucchTxPower>,<grantTotal>,<ulThroughPut>,<ulPeakThroughPut>,
1099 // <nrrcModeState>,<nmmState>,<serviceState>,<IsSingleNmmRejectCause>,<NMMRejectCause>,<amfRegionId>,<amfSetId>,<amfPointer>,<nrTmsi>,
1100 // <ulBandwidth>,<dlInitialBwpFreq>,<ulInitialBwpFreq>
1101 /*
1102 +EEMNRSVC: 1120, 2, 0, 2622342, 137, 158650, 0, 146678, 0, 0, 255, 28, 79, 620598788208, 0, 0, 0, 0, 0, 64,
1103 86, 65, 86, 51, 20, 0, 49,
1104 0,
1105 0, 0, 0, 0, 38, 0, 8, 0, 0,
1106 0, 256, 24, 16, 13, 20, 2, 2, 0,
1107 1, 10, 0, 1, 0, 9, 128, 5, 2358781729,
1108 79, 788390, 733390
1109 */
1110 if(strStartsWith(s, "+EEMNRSVC:")) {
1111 // mcc,lenOfMnc,mnc,tac,PCI,dlNrArfcn,ulNrArfcn,band,rsrp,rsrq,sinr,rssi
1112 // cellID
1113 if(cell_info.running) {
1114 int tmp_int;
1115 int i = 0;
1116 char* tmp_s = memdup(s,strlen(s) + 1);
1117 char* free_ptr = tmp_s;
1118 char *line = tmp_s;
1119 char *tmp_str;
1120 if (at_tok_start(&line) < 0)
1121 {
1122 goto EEMNRSVC_EXIT;
1123 }
1124
1125 if (at_tok_nextint(&line, &tmp_int) < 0) // mcc
1126 {
1127 goto EEMNRSVC_EXIT;
1128 }
1129 cell_info.cell_list.cell[cell_info.cell_list.num].value1 = (uint32)tmp_int;
1130
1131 if (at_tok_nextint(&line, &tmp_int) < 0) // lenOfMnc
1132 {
1133 goto EEMNRSVC_EXIT;
1134 }
1135 cell_info.cell_list.cell[cell_info.cell_list.num].value2 = (uint32)tmp_int;
1136 if (at_tok_nextint(&line, &tmp_int) < 0) // mnc
1137 {
1138 goto EEMNRSVC_EXIT;
1139 }
1140 cell_info.cell_list.cell[cell_info.cell_list.num].value3 = (uint32)tmp_int;
1141
1142 if (at_tok_nextint(&line, &tmp_int) < 0) // tac
1143 {
1144 goto EEMNRSVC_EXIT;
1145 }
1146 cell_info.cell_list.cell[cell_info.cell_list.num].value4 = (uint32)tmp_int;
1147
1148 if (at_tok_nextint(&line, &tmp_int) < 0) // pci
1149 {
1150 goto EEMNRSVC_EXIT;
1151 }
1152 cell_info.cell_list.cell[cell_info.cell_list.num].value5 = (uint32)tmp_int;
1153
1154 if (at_tok_nextint(&line, &tmp_int) < 0) // dlNrArfcn
1155 {
1156 goto EEMNRSVC_EXIT;
1157 }
1158 cell_info.cell_list.cell[cell_info.cell_list.num].value6 = (uint32)tmp_int;
1159
1160 if (at_tok_nextint(&line, &tmp_int) < 0) // dlScs
1161 {
1162 goto EEMNRSVC_EXIT;
1163 }
1164
1165 if (at_tok_nextint(&line, &tmp_int) < 0) // ulNrArfcn
1166 {
1167 goto EEMNRSVC_EXIT;
1168 }
1169 cell_info.cell_list.cell[cell_info.cell_list.num].value7 = (uint32)tmp_int;
1170
1171 // <ulScs>,<sulNrArfcn>,<sulScs>
1172 for(i =0; i < 3; i++)
1173 {
1174 if (at_tok_nextint(&line, &tmp_int) < 0)
1175 {
1176 goto EEMNRSVC_EXIT;
1177 }
1178 }
1179
1180 if (at_tok_nextint(&line, &tmp_int) < 0) // band
1181 {
1182 goto EEMNRSVC_EXIT;
1183 }
1184 cell_info.cell_list.cell[cell_info.cell_list.num].value8 = (uint32)tmp_int;
1185
1186
1187 if (at_tok_nextint(&line, &tmp_int) < 0) // dlBandwidth
1188 {
1189 goto EEMNRSVC_EXIT;
1190 }
1191
1192
1193 if (at_tok_nextstr(&line, &tmp_str) < 0) // cellId
1194 {
1195 goto EEMNRSVC_EXIT;
1196 }
1197 // LOGD("cellID-str : %s", tmp_str);
1198 cell_info.cell_list.cell[cell_info.cell_list.num].value64_1 = (uint64)strtoull(tmp_str, NULL, 10);
1199 // LOGD("cellID : %llu", cell_info.cell_list.cell[cell_info.cell_list.num].value64_1);
1200
1201 // <IsRedCapCell>,<longDRXCyclePresent>,<shortDRXCyclePresent>,<longDRXCycle>,<shortDRXCycle>,<pagingCycle>
1202 for(i =0; i < 6; i++)
1203 {
1204 if (at_tok_nextint(&line, &tmp_int) < 0)
1205 {
1206 goto EEMNRSVC_EXIT;
1207 }
1208 }
1209
1210 if (at_tok_nextint(&line, &tmp_int) < 0) // <rsrp>
1211 {
1212 goto EEMNRSVC_EXIT;
1213 }
1214 cell_info.cell_list.cell[cell_info.cell_list.num].value9 = (uint32)tmp_int;
1215
1216 if (at_tok_nextint(&line, &tmp_int) < 0) // <rsrq>
1217 {
1218 goto EEMNRSVC_EXIT;
1219 }
1220 cell_info.cell_list.cell[cell_info.cell_list.num].value10 = (uint32)tmp_int;
1221
1222 if (at_tok_nextint(&line, &tmp_int) < 0) // <sinr>
1223 {
1224 goto EEMNRSVC_EXIT;
1225 }
1226 cell_info.cell_list.cell[cell_info.cell_list.num].value11 = (uint32)tmp_int;
1227
1228 if (at_tok_nextint(&line, &tmp_int) < 0) // <rssi>
1229 {
1230 goto EEMNRSVC_EXIT;
1231 }
1232 cell_info.cell_list.cell[cell_info.cell_list.num].value12 = (uint32)tmp_int;
1233
1234 cell_info.cell_list.num++;
1235
1236EEMNRSVC_EXIT:
1237 free(free_ptr);
1238 }
1239 }
1240 // +EEMNRINTER: <index>,<phyCellId>,<nrArfcn>,<dlScs>,<rsrp>,<rsrq>,<sinr>,<srxlev>,<mcc>,<lenOfMnc>,<mnc>,<tac>,<cellId>
1241 /* +EEMNRINTER: 0, 508, 723360, 0, 39, 64, -8, 1, 0, 0, 0, 0, 4294967295 */
1242 /* +EEMNRINTER: 1, 254, 723360, 0, 45, 72, 1, 7, 0, 0, 0, 0, 4294967295 */
1243 /* +EEMNRINTER: 2, 595, 723360, 0, 40, 65, -5, 3, 0, 0, 0, 0, 4294967295 */
1244 else if(strStartsWith(s, "+EEMNRINTER:")) {
1245 // phyCellId,nrArfcn,rsrp,rsrq
1246 if(cell_info.running) {
1247 int tmp_int;
1248 char* tmp_s = memdup(s,strlen(s) + 1);
1249 char* free_ptr = tmp_s;
1250 char *line = tmp_s;
1251 if (at_tok_start(&line) < 0)
1252 {
1253 goto EEMNRINTER_EXIT;
1254 }
1255 if (at_tok_nextint(&line, &tmp_int) < 0) // index
1256 {
1257 goto EEMNRINTER_EXIT;
1258 }
1259 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int > 1007) // phyCellId (0 - 1007)
1260 {
1261 goto EEMNRINTER_EXIT;
1262 }
1263 cell_info.cell_list.cell[cell_info.cell_list.num].value1 = (uint32)tmp_int;
1264 if (at_tok_nextint(&line, &tmp_int) < 0) // nrArfcn
1265 {
1266 goto EEMNRINTER_EXIT;
1267 }
1268 cell_info.cell_list.cell[cell_info.cell_list.num].value2 = (uint32)tmp_int;
1269 if (at_tok_nextint(&line, &tmp_int) < 0) // dlScs
1270 {
1271 goto EEMNRINTER_EXIT;
1272 }
1273 if (at_tok_nextint(&line, &tmp_int) < 0) // rsrp
1274 {
1275 goto EEMNRINTER_EXIT;
1276 }
1277 cell_info.cell_list.cell[cell_info.cell_list.num].value3 = (uint32)tmp_int;
1278 if (at_tok_nextint(&line, &tmp_int) < 0) // rsrq
1279 {
1280 goto EEMNRINTER_EXIT;
1281 }
1282 cell_info.cell_list.cell[cell_info.cell_list.num].value4 = (uint32)tmp_int;
1283
1284 cell_info.cell_list.num++;
1285EEMNRINTER_EXIT:
1286 free(free_ptr);
1287 }
1288 }
1289 // +EEMNRINTERRAT:<net_type>,<numInterRATEutra>,
1290 // <earfcn>,<physCellId>,<rsrp>,<rsrq>,<sinr>,<mcc>,<lenOfMnc>,<mnc>,<tac>
1291 // ......
1292 // <earfcn>,<physCellId>,<rsrp>,<rsrq>,<sinr>,<mcc>,<lenOfMnc>,<mnc>,<tac>
1293 /*
1294 +EEMNRINTERRAT: 2, 6, // LTE
1295 1300, 423, 0, 0, 0, 0, 0, 0, 0,
1296 1300, 137, 0, 0, 0, 0, 0, 0, 0,
1297 1300, 149, 0, 0, 0, 0, 0, 0, 0,
1298 1300, 494, 0, 0, 0, 0, 0, 0, 0,
1299 1300, 337, 0, 0, 0, 0, 0, 0, 0,
1300 1300, 288, 0, 0, 0, 0, 0, 0, 0
1301 */
1302 /*
1303 +EEMNRINTERRAT: 1, 0 // UMTS
1304 */
1305 else if(strStartsWith(s, "+EEMNRINTERRAT:")) {
1306 // phyCellId,nrArfcn,rsrp,rsrq
1307 if(cell_info.running) {
1308 int tmp_int;
1309 char* tmp_s = memdup(s,strlen(s) + 1);
1310 char* free_ptr = tmp_s;
1311 char *line = tmp_s;
1312 if (at_tok_start(&line) < 0)
1313 {
1314 goto EEMNRINTERRAT_EXIT;
1315 }
1316 if (at_tok_nextint(&line, &tmp_int) < 0) // net_type
1317 {
1318 goto EEMNRINTERRAT_EXIT;
1319 }
1320
1321#define CI_DEV_EM_NETWORK_LTE 2
1322
1323 // Only support LTE.
1324 if(tmp_int == CI_DEV_EM_NETWORK_LTE) { // LTE
1325 int num;
1326 int i, j;
1327 if (at_tok_nextint(&line, &num) < 0) // numInterRATEutra
1328 {
1329 goto EEMNRINTERRAT_EXIT;
1330 }
1331 LOGD("LTE-RAT num : %d", num);
1332 for(i = 0; i < num; i++) {
1333 if (at_tok_nextint(&line, &tmp_int) < 0) // earfcn
1334 {
1335 goto EEMNRINTERRAT_EXIT;
1336 }
1337 cell_info.cell_list.cell[cell_info.cell_list.num].value2 = (uint32)tmp_int;
1338
1339 if (at_tok_nextint(&line, &tmp_int) < 0) // physCellId
1340 {
1341 goto EEMNRINTERRAT_EXIT;
1342 }
1343 cell_info.cell_list.cell[cell_info.cell_list.num].value1 = (uint32)tmp_int;
1344
1345 if (at_tok_nextint(&line, &tmp_int) < 0) // rsrp
1346 {
1347 goto EEMNRINTERRAT_EXIT;
1348 }
1349 cell_info.cell_list.cell[cell_info.cell_list.num].value3 = (uint32)tmp_int;
1350
1351 if (at_tok_nextint(&line, &tmp_int) < 0) // rsrq
1352 {
1353 goto EEMNRINTERRAT_EXIT;
1354 }
1355 cell_info.cell_list.cell[cell_info.cell_list.num].value4 = (uint32)tmp_int;
1356
1357 // Jump 5 items.
1358 j = 0;
1359 while(j < 5) {
1360 if (at_tok_nextint(&line, &tmp_int) < 0)
1361 {
1362 goto EEMNRINTERRAT_EXIT;
1363 }
1364 j++;
1365 }
1366
1367 cell_info.cell_list.num++;
1368 }
1369 }
1370EEMNRINTERRAT_EXIT:
1371 free(free_ptr);
1372 }
1373 }
b.liub4772072024-08-15 14:47:03 +08001374 /*
1375 // <mcc>, <length of mnc>, <mnc>, <tac>, <PCI>, <dlEuarfcn>, < ulEuarfcn >, <band>, <dlBandwidth>,
1376 // <rsrp>,<rsrq>, <sinr>,
1377 // errcModeState,emmState,serviceState,IsSingleEmmRejectCause,EMMRejectCause,mmeGroupId,mmeCode,mTmsi,
1378 // cellId,subFrameAssignType,specialSubframePatterns,transMode
1379 // mainRsrp,diversityRsrp,mainRsrq,diversityRsrq,rssi,cqi,pathLoss,tb0DlTpt,tb1DlTpt,tb0DlPeakTpt,tb1DlPeakTpt,tb0UlPeakTpt,
1380 // tb1UlPeakTpt,dlThroughPut,dlPeakThroughPut,averDlPRB,averCQITb0,averCQITb1,rankIndex,grantTotal,ulThroughPut,ulPeakThroughPut,currPuschTxPower,averUlPRB,
1381 // dlBer, ulBer,
1382 // diversitySinr, diversityRssi
1383 +EEMLTESVC: 1120, 2, 0, 33584, 430, 40936, 40936, 41, 20,
1384 0, 0, 0,
1385 1, 10, 0, 1, 0, 1059, 78, 3959566565,
1386 105149248, 2, 7, 7,
1387 0, 0, 0, 0, 0, 0, 0, 1190919, 0, 0, 0, 16779777,
1388 0, 5112867, 3959566565, 2, 0, 0, 0, 0, 0, 0, 0, 0,
1389 0, 0,
1390 7, 44
1391 */
b.liude989912025-02-12 14:40:42 +08001392 else if(strStartsWith(s, "+EEMLTESVC:")) // LTE Server Cell
b.liub4772072024-08-15 14:47:03 +08001393 {
1394 // tac, PCI, dlEuarfcn, ulEuarfcn, band
1395 if(cell_info.running) {
1396 int tmp_int;
1397 int i = 0;
b.liufd87baf2024-11-15 15:30:38 +08001398 char* tmp_s = memdup(s,strlen(s) + 1);
b.liub4772072024-08-15 14:47:03 +08001399 char* free_ptr = tmp_s;
1400 char *line = tmp_s;
1401 if (at_tok_start(&line) < 0)
1402 {
1403 goto EEMLTESVC_EXIT;
1404 }
1405
1406 if (at_tok_nextint(&line, &tmp_int) < 0)
1407 {
1408 goto EEMLTESVC_EXIT;
1409 }
1410 cell_info.cell_list.cell[cell_info.cell_list.num].value6 = (uint32)tmp_int; //mcc
1411 if (at_tok_nextint(&line, &tmp_int) < 0)
1412 {
1413 goto EEMLTESVC_EXIT;
1414 }
1415 if (at_tok_nextint(&line, &tmp_int) < 0)
1416 {
1417 goto EEMLTESVC_EXIT;
1418 }
1419 cell_info.cell_list.cell[cell_info.cell_list.num].value7 = (uint32)tmp_int; //mnc
1420 /*
1421 // Jump 2 integer.
1422 i = 0;
1423 while(i < 2) {
1424 if (at_tok_nextint(&line, &tmp_int) < 0)
1425 {
1426 goto EEMLTESVC_EXIT;
1427 }
1428 i++;
1429 }
1430 */
1431 if (at_tok_nextint(&line, &tmp_int) < 0)
1432 {
1433 goto EEMLTESVC_EXIT;
1434 }
1435 cell_info.cell_list.cell[cell_info.cell_list.num].value1 = (uint32)tmp_int; //tac
1436 if (at_tok_nextint(&line, &tmp_int) < 0)
1437 {
1438 goto EEMLTESVC_EXIT;
1439 }
1440 cell_info.cell_list.cell[cell_info.cell_list.num].value2 = (uint32)tmp_int; //pci
1441 if (at_tok_nextint(&line, &tmp_int) < 0)
1442 {
1443 goto EEMLTESVC_EXIT;
1444 }
1445 cell_info.cell_list.cell[cell_info.cell_list.num].value3 = (uint32)tmp_int; //dl arfcn
1446 if (at_tok_nextint(&line, &tmp_int) < 0)
1447 {
1448 goto EEMLTESVC_EXIT;
1449 }
1450 cell_info.cell_list.cell[cell_info.cell_list.num].value4 = (uint32)tmp_int; //ul arfcn
1451 if (at_tok_nextint(&line, &tmp_int) < 0)
1452 {
1453 goto EEMLTESVC_EXIT;
1454 }
1455 cell_info.cell_list.cell[cell_info.cell_list.num].value5 = (uint32)tmp_int; //band
1456 if (at_tok_nextint(&line, &tmp_int) < 0)
1457 {
1458 goto EEMLTESVC_EXIT;
1459 }
1460 if (at_tok_nextint(&line, &tmp_int) < 0)
1461 {
1462 goto EEMLTESVC_EXIT;
1463 }
1464 cell_info.cell_list.cell[cell_info.cell_list.num].value8 = (uint32)tmp_int; //cid
1465 if (at_tok_nextint(&line, &tmp_int) < 0)
1466 {
1467 goto EEMLTESVC_EXIT;
1468 }
1469 cell_info.cell_list.cell[cell_info.cell_list.num].value9 = (uint32)tmp_int; //rsrp
1470
1471 for(i =0; i < 10; i++)
1472 {
1473 if (at_tok_nextint(&line, &tmp_int) < 0)
1474 {
1475 goto EEMLTESVC_EXIT;
1476 }
1477 }
1478 cell_info.cell_list.cell[cell_info.cell_list.num].value10 = (uint32)tmp_int; //cell identiy
1479
1480 cell_info.cell_list.num++;
1481
1482EEMLTESVC_EXIT:
1483 free(free_ptr);
1484 }
1485 }
1486 /*
1487 // index,phyCellId,euArfcn,rsrp,rsrq
1488 +EEMLTEINTER: 0, 65535, 38950, 0, 0
1489 */
1490 else if(strStartsWith(s, "+EEMLTEINTER:") || strStartsWith(s, "+EEMLTEINTRA:")) // LTE ÒìÆµ/Í¬ÆµÐ¡Çø
1491 {
1492 // phyCellId,euArfcn,rsrp,rsrq
1493 if(cell_info.running) {
1494 int tmp_int;
b.liufd87baf2024-11-15 15:30:38 +08001495 char* tmp_s = memdup(s,strlen(s) + 1);
b.liub4772072024-08-15 14:47:03 +08001496 char* free_ptr = tmp_s;
1497 char *line = tmp_s;
1498 if (at_tok_start(&line) < 0)
1499 {
1500 goto EEMLTEINTER_EXIT;
1501 }
1502 if (at_tok_nextint(&line, &tmp_int) < 0)
1503 {
1504 goto EEMLTEINTER_EXIT;
1505 }
1506 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int <= 0 || tmp_int > 503)
1507 {
1508 goto EEMLTEINTER_EXIT;
1509 }
1510 cell_info.cell_list.cell[cell_info.cell_list.num].value1 = (uint32)tmp_int;
1511 if (at_tok_nextint(&line, &tmp_int) < 0)
1512 {
1513 goto EEMLTEINTER_EXIT;
1514 }
1515 cell_info.cell_list.cell[cell_info.cell_list.num].value2 = (uint32)tmp_int;
1516 if (at_tok_nextint(&line, &tmp_int) < 0)
1517 {
1518 goto EEMLTEINTER_EXIT;
1519 }
1520 cell_info.cell_list.cell[cell_info.cell_list.num].value3 = (uint32)tmp_int;
1521 LOG("cell line : %s", line);
1522 if (at_tok_nextint(&line, &tmp_int) < 0)
1523 {
1524 goto EEMLTEINTER_EXIT;
1525 }
1526 cell_info.cell_list.cell[cell_info.cell_list.num].value4 = (uint32)tmp_int;
1527 if (at_tok_nextint(&line, &tmp_int) < 0)
1528 {
1529 LOG("cell tmp_int : %d", tmp_int);
1530 goto EEMLTEINTER_EXIT;
1531 }
1532 cell_info.cell_list.cell[cell_info.cell_list.num].value5 = (uint32)tmp_int;
1533 LOG("cell value5 : %d", cell_info.cell_list.cell[cell_info.cell_list.num].value5);
1534 cell_info.cell_list.num++;
1535EEMLTEINTER_EXIT:
1536 free(free_ptr);
1537 }
1538 }
1539 // Do nothing
1540 else if(strStartsWith(s, "+EEMLTEINTERRAT:")) // LTE RATÐ¡ÇøÐÅÏ¢
1541 {
1542 if(cell_info.running) {
1543
1544 }
1545 }
1546 // WCDMA
1547 /*
1548 // Mode, sCMeasPresent, sCParamPresent, ueOpStatusPresent,
1549
1550 // if sCMeasPresent == 1
1551 // cpichRSCP, utraRssi, cpichEcN0, sQual, sRxLev, txPower,
1552 // endif
1553
1554 // if sCParamPresent == 1
1555 // rac, nom, mcc, mnc_len, mnc, lac, ci,
1556 // uraId, psc, arfcn, t3212, t3312, hcsUsed, attDetAllowed,
1557 // csDrxCycleLen, psDrxCycleLen, utranDrxCycleLen, HSDPASupport, HSUPASupport,
1558 // endif
1559
1560 // if ueOpStatusPresent == 1
1561 // rrcState, numLinks, srncId, sRnti,
1562 // algPresent, cipherAlg, cipherOn, algPresent, cipherAlg, cipherOn,
1563 // HSDPAActive, HSUPAActive, MccLastRegisteredNetwork, MncLastRegisteredNetwork, TMSI, PTMSI, IsSingleMmRejectCause, IsSingleGmmRejectCause,
1564 // MMRejectCause, GMMRejectCause, mmState, gmmState, gprsReadyState, readyTimerValueInSecs, NumActivePDPContext, ULThroughput, DLThroughput,
1565 // serviceStatus, pmmState, LAU_status, LAU_count, RAU_status, RAU_count
1566 // endif
1567 //
1568 +EEMUMTSSVC: 3, 1, 1, 1,
1569 -80, 27, -6, -18, -115, -32768,
1570 1, 1, 1120, 2, 1, 61697, 168432821,
1571 15, 24, 10763, 0, 0, 0, 0,
1572 128, 128, 65535, 0, 0,
1573 2, 255, 65535, 4294967295,
1574 0, 0, 0, 0, 0, 0,
1575 0, 0, 0, 0, 0, 0, 1, 1,
1576 28672, 28672, 0, 0, 0, 0, 0, 0, 0,
1577 0, 0, 0, 0, 0, 0
1578 */
1579 else if(strStartsWith(s, "+EEMUMTSSVC:")) // WCDMA Server Cell
1580 {
1581 // lac, ci, arfcn
1582 if(cell_info.running) {
1583 int tmp_int;
1584 int i = 0;
b.liufd87baf2024-11-15 15:30:38 +08001585 char* tmp_s = memdup(s,strlen(s) + 1);
b.liub4772072024-08-15 14:47:03 +08001586 char* free_ptr = tmp_s;
1587 char *line = tmp_s;
1588 if (at_tok_start(&line) < 0)
1589 {
1590 goto EEMUMTSSVC_EXIT;
1591 }
1592 // Jump 12 integer.
1593 i = 0;
1594 while(i < 12) {
1595 if (at_tok_nextint(&line, &tmp_int) < 0)
1596 {
1597 goto EEMUMTSSVC_EXIT;
1598 }
1599 i++;
1600 }
1601 // mcc
1602 if (at_tok_nextint(&line, &tmp_int) < 0)
1603 {
1604 goto EEMUMTSSVC_EXIT;
1605 }
1606 cell_info.cell_list.cell[cell_info.cell_list.num].value4= (uint32)tmp_int;
1607 // mnc
1608 if (at_tok_nextint(&line, &tmp_int) < 0)
1609 {
1610 goto EEMUMTSSVC_EXIT;
1611 }
1612 cell_info.cell_list.cell[cell_info.cell_list.num].value5= (uint32)tmp_int;
1613 // lac
1614 if (at_tok_nextint(&line, &tmp_int) < 0)
1615 {
1616 goto EEMUMTSSVC_EXIT;
1617 }
1618 cell_info.cell_list.cell[cell_info.cell_list.num].value1 = (uint32)tmp_int;
1619 // ci
1620 if (at_tok_nextint(&line, &tmp_int) < 0)
1621 {
1622 goto EEMUMTSSVC_EXIT;
1623 }
1624 cell_info.cell_list.cell[cell_info.cell_list.num].value2 = (uint32)tmp_int;
1625
1626 if (at_tok_nextint(&line, &tmp_int) < 0)
1627 {
1628 goto EEMUMTSSVC_EXIT;
1629 }
1630 // cpi
1631 if (at_tok_nextint(&line, &tmp_int) < 0)
1632 {
1633 goto EEMUMTSSVC_EXIT;
1634 }
1635 cell_info.cell_list.cell[cell_info.cell_list.num].value6= (uint32)tmp_int;
1636 /*
1637 // Jump 2 integer.
1638 i = 0;
1639 while(i < 2) {
1640 if (at_tok_nextint(&line, &tmp_int) < 0)
1641 {
1642 goto EEMUMTSSVC_EXIT;
1643 }
1644 i++;
1645 }
1646 */
1647 // arfcn
1648 if (at_tok_nextint(&line, &tmp_int) < 0)
1649 {
1650 goto EEMUMTSSVC_EXIT;
1651 }
1652 cell_info.cell_list.cell[cell_info.cell_list.num].value3 = (uint32)tmp_int;
1653
1654 cell_info.cell_list.num++;
1655EEMUMTSSVC_EXIT:
1656 free(free_ptr);
1657 }
1658 }
1659 /*
1660 // index, cpichRSCP, utraRssi, cpichEcN0, sQual, sRxLev ,mcc, mnc, lac, ci, arfcn, psc
1661 +EEMUMTSINTRA: 0, -32768, -1, -32768, -18, -115, 0, 0, 65534, 1, 10763, 32
1662 */
1663 else if(strStartsWith(s, "+EEMUMTSINTRA:")) // WCDMAÁÙ½üÐ¡Çø
1664 {
1665 // lac, ci, arfcn
1666 if(cell_info.running) {
1667 int tmp_int;
1668 int i = 0;
b.liufd87baf2024-11-15 15:30:38 +08001669 char* tmp_s = memdup(s,strlen(s) + 1);
b.liub4772072024-08-15 14:47:03 +08001670 char* free_ptr = tmp_s;
1671 char *line = tmp_s;
1672 if (at_tok_start(&line) < 0)
1673 {
1674 goto EEMUMTSINTRA_EXIT;
1675 }
1676 // Jump 8 integer.
1677 i = 0;
1678 while(i < 8) {
1679 if (at_tok_nextint(&line, &tmp_int) < 0)
1680 {
1681 goto EEMUMTSINTRA_EXIT;
1682 }
1683 i++;
1684 }
1685
1686 // lac
1687 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int <= 0 || tmp_int >= 65536)
1688 {
1689 goto EEMUMTSINTRA_EXIT;
1690 }
1691 cell_info.cell_list.cell[cell_info.cell_list.num].value1 = (uint32)tmp_int;
1692
1693 // ci
1694 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int <= 0 || tmp_int >= 65536)
1695 {
1696 goto EEMUMTSINTRA_EXIT;
1697 }
1698 cell_info.cell_list.cell[cell_info.cell_list.num].value2 = (uint32)tmp_int;
1699
1700 // arfcn
1701 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int <= 0 || tmp_int >= 65536)
1702 {
1703 goto EEMUMTSINTRA_EXIT;
1704 }
1705 cell_info.cell_list.cell[cell_info.cell_list.num].value3 = (uint32)tmp_int;
1706
1707 cell_info.cell_list.num++;
1708EEMUMTSINTRA_EXIT:
1709 free(free_ptr);
1710 }
1711 }
1712 /*
1713 // index,gsmRssi,rxLev,C1,C2,mcc,mnc,lac,ci,arfcn,bsic
1714 +EEMUMTSINTERRAT: 0, -32768, -107, -1, -1, 0, 0, 65534, 0, 117, 36
1715 */
1716 else if(strStartsWith(s, "+EEMUMTSINTERRAT:")) // WCDMA RATÐ¡ÇøÐÅÏ¢
1717 {
1718 // lac, ci, arfcn
1719 if(cell_info.running) {
1720 int tmp_int;
1721 int i = 0;
b.liufd87baf2024-11-15 15:30:38 +08001722 char* tmp_s = memdup(s,strlen(s) + 1);
b.liub4772072024-08-15 14:47:03 +08001723 char* free_ptr = tmp_s;
1724 char *line = tmp_s;
1725 if (at_tok_start(&line) < 0)
1726 {
1727 goto EEMUMTSINTERRAT_EXIT;
1728 }
1729 // Jump 7 integer.
1730 i = 0;
1731 while(i < 7) {
1732 if (at_tok_nextint(&line, &tmp_int) < 0)
1733 {
1734 goto EEMUMTSINTERRAT_EXIT;
1735 }
1736 i++;
1737 }
1738
1739 // lac
1740 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int <= 0 || tmp_int >= 65536)
1741 {
1742 goto EEMUMTSINTERRAT_EXIT;
1743 }
1744 cell_info.cell_list.cell[cell_info.cell_list.num].value1 = (uint32)tmp_int;
1745
1746 // ci
1747 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int <= 0 || tmp_int >= 65536)
1748 {
1749 goto EEMUMTSINTERRAT_EXIT;
1750 }
1751 cell_info.cell_list.cell[cell_info.cell_list.num].value2 = (uint32)tmp_int;
1752
1753 // arfcn
1754 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int <= 0 || tmp_int >= 65536)
1755 {
1756 goto EEMUMTSINTERRAT_EXIT;
1757 }
1758 cell_info.cell_list.cell[cell_info.cell_list.num].value3 = (uint32)tmp_int;
1759
1760 cell_info.cell_list.num++;
1761EEMUMTSINTERRAT_EXIT:
1762 free(free_ptr);
1763 }
1764 }
1765 // GSM
1766 // +EEMGINFOBASIC: 2
1767 // Do nothing.
1768 else if(strStartsWith(s, "+EEMGINFOBASIC:")) // Basic information in GSM
1769 // 0: ME in Idle mode 1: ME in Dedicated mode 2: ME in PS PTM mode
1770 {
1771 if(cell_info.running) {
1772
1773 }
1774 }
1775 /*
1776 // mcc, mnc_len, mnc, lac, ci, nom, nco,
1777 // bsic, C1, C2, TA, TxPwr,
1778 // RxSig, RxSigFull, RxSigSub, RxQualFull, RxQualSub,
1779 // ARFCB_tch, hopping_chnl, chnl_type, TS, PacketIdle, rac, arfcn,
1780 // bs_pa_mfrms, C31, C32, t3212, t3312, pbcch_support, EDGE_support,
1781 // ncc_permitted, rl_timeout, ho_count, ho_succ, chnl_access_count, chnl_access_succ_count,
1782 // gsmBand,channelMode
1783 +EEMGINFOSVC: 1120, 2, 0, 32784, 24741, 2, 0,
1784 63, 36, 146, 1, 7,
1785 46, 42, 42, 7, 0,
1786 53, 0, 8, 0, 1, 6, 53,
1787 2, 0, 146, 42, 54, 0, 1,
1788 1, 32, 0, 0, 0, 0,
1789 0, 0
1790 */
1791 else if(strStartsWith(s, "+EEMGINFOSVC:")) // GSM Server Cell
1792 {
1793 // lac, ci, arfcn, bsic
1794 LOG("+EEMGINFOSVC: 1= %d\n.",cell_info.running);
1795 if(cell_info.running) {
1796 int tmp_int;
1797 int i = 0;
b.liufd87baf2024-11-15 15:30:38 +08001798 char* tmp_s = memdup(s,strlen(s) + 1);
b.liub4772072024-08-15 14:47:03 +08001799 char* free_ptr = tmp_s;
1800 char *line = tmp_s;
1801 if (at_tok_start(&line) < 0)
1802 {
1803 goto EEMGINFOSVC_EXIT;
1804 }
1805
1806 // mcc
1807 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int <= 0 || tmp_int >= 65536)
1808 {
1809 goto EEMGINFOSVC_EXIT;
1810 }
1811 cell_info.cell_list.cell[cell_info.cell_list.num].value5 = (uint32)tmp_int;
1812
1813 //mnc_len
1814 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int <= 0 || tmp_int >= 65536)
1815 {
1816 goto EEMGINFOSVC_EXIT;
1817 }
1818 // mnc
1819 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int <= 0 || tmp_int >= 65536)
1820 {
1821 goto EEMGINFOSVC_EXIT;
1822 }
1823 cell_info.cell_list.cell[cell_info.cell_list.num].value6 = (uint32)tmp_int;
1824
1825 /*
1826 // Jump 3 integer.
1827 i = 0;
1828 while(i < 3) {
1829 if (at_tok_nextint(&line, &tmp_int) < 0)
1830 {
1831 goto EEMGINFOSVC_EXIT;
1832 }
1833 i++;
1834 }
1835 */
1836 // lac
1837 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1838 {
1839 goto EEMGINFOSVC_EXIT;
1840 }
1841 cell_info.cell_list.cell[cell_info.cell_list.num].value1 = (uint32)tmp_int;
1842
1843 // ci
1844 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1845 {
1846 goto EEMGINFOSVC_EXIT;
1847 }
1848 cell_info.cell_list.cell[cell_info.cell_list.num].value2 = (uint32)tmp_int;
1849
1850 // Jump 2 integer.
1851 i = 0;
1852 while(i < 2) {
1853 if (at_tok_nextint(&line, &tmp_int) < 0)
1854 {
1855 goto EEMGINFOSVC_EXIT;
1856 }
1857 i++;
1858 }
1859
1860 // bsic
1861 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1862 {
1863 goto EEMGINFOSVC_EXIT;
1864 }
1865 cell_info.cell_list.cell[cell_info.cell_list.num].value4 = (uint32)tmp_int;
1866
1867 // Jump 15 integer.
1868 i = 0;
1869 while(i < 15) {
1870 if (at_tok_nextint(&line, &tmp_int) < 0)
1871 {
1872 goto EEMGINFOSVC_EXIT;
1873 }
1874 i++;
1875 }
1876
1877 // arfcn
1878 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1879 {
1880 goto EEMGINFOSVC_EXIT;
1881 }
1882 cell_info.cell_list.cell[cell_info.cell_list.num].value3 = (uint32)tmp_int;
1883
1884 cell_info.cell_list.num++;
1885EEMGINFOSVC_EXIT:
1886 free(free_ptr);
1887 }
1888 }
1889 /*
1890 // PS_attached, attach_type, service_type, tx_power, c_value,
1891 // ul_ts, dl_ts, ul_cs, dl_cs, ul_modulation, dl_modulation,
1892 // gmsk_cv_bep, 8psk_cv_bep, gmsk_mean_bep, 8psk_mean_bep, EDGE_bep_period, single_gmm_rej_cause
1893 // pdp_active_num, mac_mode, network_control, network_mode, EDGE_slq_measurement_mode, edge_status
1894 +EEMGINFOPS: 1, 255, 0, 0, 0,
1895 0, 0, 268435501, 1, 0, 0,
1896 4, 0, 96, 0, 0, 0,
1897 0, 0, 0, 65535, 0, 13350
1898 */
1899 // Do nothing.
1900 else if(strStartsWith(s, "+EEMGINFOPS:")) // PSÐÅÏ¢
1901 {
1902 if(cell_info.running) {
1903
1904 }
1905 }
1906 else if(strStartsWith(s, "+EEMGINFONC:")) // cell
1907 {
1908 if(cell_info.running) {
1909 int tmp_int;
1910 int i = 0;
b.liufd87baf2024-11-15 15:30:38 +08001911 char* tmp_s = memdup(s,strlen(s) + 1);
b.liub4772072024-08-15 14:47:03 +08001912 char* free_ptr = tmp_s;
1913 char *line = tmp_s;
1914 if (at_tok_start(&line) < 0)
1915 {
1916 goto EEMGINFOPS_EXIT;
1917 }
1918
1919 // nc_num
1920 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1921 {
1922 LOG("cell_info.running 1= %d\n.",cell_info.running);
1923 goto EEMGINFOPS_EXIT;
1924 }
1925 // mcc
1926 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1927 {
1928 LOG("cell_info.running 2= %d\n.",cell_info.running);
1929 goto EEMGINFOPS_EXIT;
1930 }
1931 cell_info.cell_list.cell[cell_info.cell_list.num].value5 = (uint32)tmp_int;
1932
1933 // mnc
1934 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1935 {
1936 LOG("cell_info.running 3= %d\n.",cell_info.running);
1937 goto EEMGINFOPS_EXIT;
1938 }
1939 cell_info.cell_list.cell[cell_info.cell_list.num].value6 = (uint32)tmp_int;
1940
1941 // lac
1942 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1943 {
1944 LOG("cell_info.running 4= %d\n.",cell_info.running);
1945 goto EEMGINFOPS_EXIT;
1946 }
1947 cell_info.cell_list.cell[cell_info.cell_list.num].value1 = (uint32)tmp_int;
1948
1949 // rac
1950 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1951 {
1952 LOG("cell_info.running 5= %d\n.",cell_info.running);
1953 goto EEMGINFOPS_EXIT;
1954 }
1955
1956 // ci
1957 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1958 {
1959 LOG("cell_info.running 6= %d\n.",cell_info.running);
1960 goto EEMGINFOPS_EXIT;
1961 }
1962 cell_info.cell_list.cell[cell_info.cell_list.num].value2 = (uint32)tmp_int;
1963
1964 // rx_lv
1965 if (at_tok_nextint(&line, &tmp_int) < 0)
1966 {
1967 LOG("cell_info.running 7= %d\n.",cell_info.running);
1968 goto EEMGINFOPS_EXIT;
1969 }
1970
1971 // bsic
1972 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1973 {
1974 LOG("cell_info.running 8= %d\n.",cell_info.running);
1975 goto EEMGINFOPS_EXIT;
1976 }
1977 cell_info.cell_list.cell[cell_info.cell_list.num].value4 = (uint32)tmp_int;
1978
1979 // Jump 2 integer.
1980 i = 0;
1981 while(i < 2) {
1982 if (at_tok_nextint(&line, &tmp_int) < 0)
1983 {
1984 LOG("cell_info.running 9= %d\n.",cell_info.running);
1985 goto EEMGINFOPS_EXIT;
1986 }
1987 i++;
1988 }
1989
1990 // arfcn
1991 if (at_tok_nextint(&line, &tmp_int) < 0 || tmp_int < 0 || tmp_int >= 65536)
1992 {
1993 LOG("cell_info.running 10 = %d\n.",cell_info.running);
1994 goto EEMGINFOPS_EXIT;
1995 }
1996 cell_info.cell_list.cell[cell_info.cell_list.num].value3 = (uint32)tmp_int;
1997
1998 cell_info.cell_list.num++;
1999EEMGINFOPS_EXIT:
2000 free(free_ptr);
2001 }
2002 } else {
2003 LOGW("Unknown CELL URC : %s", s);
2004 }
2005}
2006
b.liu87afc4c2024-08-14 17:33:45 +08002007
2008static void onUnsolicited(const char *s, const char *sms_pdu)
2009{
b.liufd87baf2024-11-15 15:30:38 +08002010 LOGV("URC : %s", s);
b.liu87afc4c2024-08-14 17:33:45 +08002011 // MBTK_AT_READY
b.liuaced4f92024-12-31 11:14:51 +08002012 // +CMT: ,23
2013 // 0891683108200855F6240D91688189911196F10000221130717445230331D90C
2014 // Get PDU data.
2015 if(cmt_found) {
2016 cmt_found = FALSE;
b.liu0bb1c8e2024-12-31 14:44:40 +08002017 urc_sms_state_change_process(s, true);
b.liuaced4f92024-12-31 11:14:51 +08002018 } else if (strStartsWith(s, "MBTK_AT_READY")) { // AT ready.
b.liu87afc4c2024-08-14 17:33:45 +08002019
b.liubcf86c92024-08-19 19:48:28 +08002020 } else if(strStartsWith(s, "CONNECT") || strStartsWith(s, "+CGEV:")) {
b.liu15f456b2024-10-31 20:16:06 +08002021 urc_pdp_state_change_process(s, sms_pdu);
b.liude989912025-02-12 14:40:42 +08002022 } else if(strStartsWith(s, "+EEMNRSVC:") || strStartsWith(s, "+EEMNRINTER:")
2023 || strStartsWith(s, "+EEMNRINTERRAT:")
2024 || strStartsWith(s, "+EEMLTESVC:") || strStartsWith(s, "+EEMLTEINTER:")
b.liubcf86c92024-08-19 19:48:28 +08002025 || strStartsWith(s, "+EEMLTEINTRA:") || strStartsWith(s, "+EEMLTEINTERRAT:")
2026 || strStartsWith(s, "+EEMUMTSSVC:") || strStartsWith(s, "+EEMUMTSINTRA:")
2027 || strStartsWith(s, "+EEMUMTSINTERRAT:") || strStartsWith(s, "+EEMGINFOBASIC:")
2028 || strStartsWith(s, "+EEMGINFOSVC:") || strStartsWith(s, "+EEMGINFOPS:")
2029 || strStartsWith(s, "+EEMGINFONC:")) {
2030 urc_cell_info_process(s, sms_pdu);
b.liu87afc4c2024-08-14 17:33:45 +08002031 }
2032 else if(strStartsWith(s, "*RADIOPOWER:")) // "*RADIOPOWER: 1"
2033 {
2034 const char* ptr = s + strlen("*RADIOPOWER:");
2035 while(*ptr != '\0' && *ptr == ' ' )
2036 {
2037 ptr++;
2038 }
2039
b.liu15f456b2024-10-31 20:16:06 +08002040 mbtk_ril_radio_state_info_t state;
2041 memset(&state, 0, sizeof(mbtk_ril_radio_state_info_t));
b.liu87afc4c2024-08-14 17:33:45 +08002042 if(*ptr == '1') {
b.liu15f456b2024-10-31 20:16:06 +08002043 state.radio_state = MBTK_RADIO_STATE_FULL_FUNC;
b.liu87afc4c2024-08-14 17:33:45 +08002044 } else {
b.liu15f456b2024-10-31 20:16:06 +08002045 state.radio_state = MBTK_RADIO_STATE_MINI_FUNC;
b.liu87afc4c2024-08-14 17:33:45 +08002046 }
b.liu15f456b2024-10-31 20:16:06 +08002047 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 +08002048 }
2049 // +CREG: 1, "8010", "000060a5", 0, 2, 0
2050 // +CREG: 1, "8330", "06447347", 7, 2, 0
2051 // +CEREG: 1, "8330", "06447347", 7
2052 // $CREG: 1, "8330", "06447347", 7,"0d4", 2, 0
2053 // $CREG: 1, "8010", "000060a7", 0,, 2, 0
2054 // +CGREG: 1
b.liude989912025-02-12 14:40:42 +08002055 // +C5GREG: 1,"00280386","07e920010",11,1,"01"
b.liu87afc4c2024-08-14 17:33:45 +08002056 else if(strStartsWith(s, "+CGREG:") // GMS/WCDMA data registed.
b.liu15f456b2024-10-31 20:16:06 +08002057 || strStartsWith(s, "+CEREG:") // LTE data registed.
b.liude989912025-02-12 14:40:42 +08002058 || strStartsWith(s, "+CREG:") // GMS/WCDMA/LTE CS registed.
2059 || strStartsWith(s, "+C5GREG:")) // NR data registed.
b.liu87afc4c2024-08-14 17:33:45 +08002060 {
b.liu15f456b2024-10-31 20:16:06 +08002061 urc_net_reg_state_change_process(s, sms_pdu);
b.liu87afc4c2024-08-14 17:33:45 +08002062 }
b.liu15f456b2024-10-31 20:16:06 +08002063 // +CLCC: 1, 1, 6, 0, 0, "18981911691", 129, "",, 0
2064 else if(strStartsWith(s, "+CLCC:")
2065 || strStartsWith(s, "+CPAS:")
2066 || strStartsWith(s, "+CALLDISCONNECT:"))
b.liu87afc4c2024-08-14 17:33:45 +08002067 {
b.liu15f456b2024-10-31 20:16:06 +08002068 urc_call_state_change_process(s, sms_pdu);
b.liu87afc4c2024-08-14 17:33:45 +08002069 }
b.liu15f456b2024-10-31 20:16:06 +08002070 else if(strStartsWith(s, "*SIMDETEC:")
2071 || strStartsWith(s, "*EUICC:")
2072 || strStartsWith(s, "+CPIN:"))
2073 {
2074 urc_sim_state_change_process(s, sms_pdu);
2075 }
2076 else if(strStartsWith(s, "+CMT:"))
2077 {
b.liuaced4f92024-12-31 11:14:51 +08002078 cmt_found = TRUE;
b.liu0bb1c8e2024-12-31 14:44:40 +08002079 urc_sms_state_change_process(s, false);
b.liu15f456b2024-10-31 20:16:06 +08002080 }
2081 else if(strStartsWith(s, "*ECALLDATA:"))
2082 {
2083 urc_ecall_state_change_process(s, sms_pdu);
2084 }
2085#if 0
b.liu87afc4c2024-08-14 17:33:45 +08002086 // +CLCC: 1, 1, 6, 0, 0, "18981911691", 129, "",, 0
2087 else if(strStartsWith(s, "+CLCC:"))
2088 {
2089 mbtk_call_info_t reg;
2090 reg.call_wait = MBTK_CLCC;
2091 char* tmp_s = memdup(s,strlen(s));
2092 char* free_ptr = tmp_s;
2093 char *line = tmp_s;
2094 int tmp_int;
2095 char *tmp_str;
2096 int err;
2097
2098 err = at_tok_start(&line);
2099 if (err < 0)
2100 {
2101 goto CLCC_EXIT;
2102 }
2103 err = at_tok_nextint(&line, &tmp_int); // dir1
2104 if (err < 0)
2105 {
2106 goto CLCC_EXIT;
2107 }
2108 reg.dir1 = (uint8)tmp_int;
2109 err = at_tok_nextint(&line, &tmp_int);// dir
2110 if (err < 0)
2111 {
2112 goto CLCC_EXIT;
2113 }
2114 reg.dir = (uint8)tmp_int;
2115 err = at_tok_nextint(&line, &tmp_int);// state
2116 if (err < 0)
2117 {
2118 goto CLCC_EXIT;
2119 }
2120 reg.state = (uint8)tmp_int;
2121 err = at_tok_nextint(&line, &tmp_int);// mode
2122 if (err < 0)
2123 {
2124 goto CLCC_EXIT;
2125 }
2126 reg.mode = (uint8)tmp_int;
2127 err = at_tok_nextint(&line, &tmp_int);// mpty
2128 if (err < 0)
2129 {
2130 goto CLCC_EXIT;
2131 }
2132 reg.mpty = (uint8)tmp_int;
2133 err = at_tok_nextstr(&line, &tmp_str); // phone_number
2134 if (err < 0)
2135 {
2136 goto CLCC_EXIT;
2137 }
2138
2139 memset(reg.phone_number,0,sizeof(reg.phone_number));
2140 memcpy(reg.phone_number, tmp_str, strlen(tmp_str));
2141 err = at_tok_nextint(&line, &tmp_int);// tpye
2142 if (err < 0)
2143 {
2144 goto CLCC_EXIT;
2145 }
2146 reg.type = (uint8)tmp_int;
2147 urc_msg_distribute(false, INFO_URC_MSG_CALL_STATE, &reg, sizeof(mbtk_call_info_t));
2148CLCC_EXIT:
2149 free(free_ptr);
2150 }
2151 // +CPAS: 4
2152 else if(strStartsWith(s, "+CPAS:"))
2153 {
2154 mbtk_call_info_t reg;
2155 reg.call_wait = 0;
2156 char* tmp_s = memdup(s,strlen(s));
2157 char* free_ptr = tmp_s;
2158 char *line = tmp_s;
2159 int tmp_int;
2160 int err;
2161
2162 memset(&reg,0,sizeof(reg));
2163
2164 err = at_tok_start(&line);
2165 if (err < 0)
2166 {
2167 goto CPAS_EXIT;
2168 }
2169 err = at_tok_nextint(&line, &tmp_int);
2170 if (err < 0)
2171 {
2172 goto CPAS_EXIT;
2173 }
2174 reg.pas = (uint8)tmp_int;
2175 reg.call_wait = MBTK_CPAS;
2176 urc_msg_distribute(false, INFO_URC_MSG_CALL_STATE, &reg, sizeof(mbtk_call_info_t));
2177CPAS_EXIT:
2178 free(free_ptr);
2179 }
2180 // +CALLDISCONNECT: 1
2181 else if(strStartsWith(s, "+CALLDISCONNECT:"))
2182 {
2183 mbtk_call_info_t reg;
2184 reg.call_wait = 0;
2185 char* tmp_s = memdup(s,strlen(s));
2186 char* free_ptr = tmp_s;
2187 char *line = tmp_s;
2188 int tmp_int;
2189 int err;
2190
2191 memset(&reg,0,sizeof(reg));
2192
2193 err = at_tok_start(&line);
2194 if (err < 0)
2195 {
2196 goto CALLDISCONNECTED_EXIT;
2197 }
2198 err = at_tok_nextint(&line, &tmp_int);
2199 if (err < 0)
2200 {
2201 goto CALLDISCONNECTED_EXIT;
2202 }
2203 reg.disconnected_id = tmp_int;
2204 reg.call_wait = MBTK_DISCONNECTED;
2205
2206 if(reg.call_wait == MBTK_DISCONNECTED)
2207 {
2208 //mbtk_net_led_set(MBTK_NET_LED_CALL_DISCONNECT);
2209 }
2210
2211 urc_msg_distribute(false, INFO_URC_MSG_CALL_STATE, &reg, sizeof(mbtk_call_info_t));
2212
2213CALLDISCONNECTED_EXIT:
2214 free(free_ptr);
2215 }
2216 // *SIMDETEC:1,SIM
2217 else if(strStartsWith(s, "*SIMDETEC:"))
2218 {
2219 if(strStartsWith(s, "*SIMDETEC:1,NOS"))
2220 {
2221 net_info.sim_state = MBTK_SIM_ABSENT;
2222 }
2223
2224 sim_info_reg.sim = -1;
2225 if(strStartsWith(s, "*SIMDETEC:1,NOS"))
2226 sim_info_reg.sim = 0;
2227 else if(strStartsWith(s, "*SIMDETEC:1,SIM"))
2228 sim_info_reg.sim = 1;
2229 if(sim_info_reg.sim == 0)
2230 {
2231 uint8 data_pdp;
2232 data_pdp = 11; //
2233 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
2234 }
2235 urc_msg_distribute(false, INFO_URC_MSG_SIM_STATE, &sim_info_reg, sizeof(mbtk_sim_card_info));
2236 }
2237 // *EUICC:1
2238/*0: SIM
22391: USIM
22402: TEST SIM
22413: TEST USIM
22424: UNKNOWN
2243Note: *EUICC:
2244*/
2245 else if(strStartsWith(s, "*EUICC:"))
2246 {
2247 sim_info_reg.sim_card_type = -1;
2248 if(strStartsWith(s, "*EUICC: 0"))
2249 sim_info_reg.sim_card_type = 1;
2250 else if(strStartsWith(s, "*EUICC: 1"))
2251 sim_info_reg.sim_card_type = 2;
2252 else if(strStartsWith(s, "*EUICC: 2"))
2253 sim_info_reg.sim_card_type = 1;
2254 else if(strStartsWith(s, "*EUICC: 3"))
2255 sim_info_reg.sim_card_type = 2;
2256 else if(strStartsWith(s, "*EUICC: 4"))
2257 sim_info_reg.sim_card_type = 0;
2258 urc_msg_distribute(false, INFO_URC_MSG_SIM_STATE, &sim_info_reg, sizeof(mbtk_sim_card_info));
2259 }
2260 // +CPIN: SIM PIN
2261 else if(strStartsWith(s, "+CPIN:"))
2262 {
2263 sim_info_reg.sim = -1;
2264 if(strStartsWith(s, "+CPIN: READY"))
2265 {
2266 sim_info_reg.sim = 1;
2267 net_info.sim_state = MBTK_SIM_READY;
2268 }
2269 else if(strStartsWith(s, "+CPIN: SIM PIN"))
2270 {
2271 sim_info_reg.sim = 2;
2272 net_info.sim_state = MBTK_SIM_PIN;
2273 }
2274 else if(strStartsWith(s, "+CPIN: SIM PUK"))
2275 {
2276 sim_info_reg.sim = 3;
2277 net_info.sim_state = MBTK_SIM_PUK;
2278 }
2279 else if(strStartsWith(s, "+CPIN: PH-SIMLOCK PIN"))
2280 {
2281 sim_info_reg.sim = 4;
2282 net_info.sim_state = MBTK_SIM_ABSENT;
2283 }
2284 else if(strStartsWith(s, "+CPIN: PH-SIMLOCK PUK"))
2285 {
2286 sim_info_reg.sim = 5;
2287 net_info.sim_state = MBTK_SIM_ABSENT;
2288 }
2289 else if(strStartsWith(s, "+CPIN: PH-FSIM PIN"))
2290 {
2291 sim_info_reg.sim = 6;
2292 net_info.sim_state = MBTK_SIM_ABSENT;
2293 }
2294 else if(strStartsWith(s, "+CPIN: PH-FSIM PUK"))
2295 {
2296 sim_info_reg.sim = 7;
2297 net_info.sim_state = MBTK_SIM_ABSENT;
2298 }
2299 else if(strStartsWith(s, "+CPIN: SIM PIN2"))
2300 {
2301 sim_info_reg.sim = 8;
2302 net_info.sim_state = MBTK_SIM_ABSENT;
2303 }
2304 else if(strStartsWith(s, "+CPIN: SIM PUK2"))
2305 {
2306 sim_info_reg.sim = 9;
2307 net_info.sim_state = MBTK_SIM_ABSENT;
2308 }
2309 else if(strStartsWith(s, "+CPIN: PH-NET PIN"))
2310 {
2311 sim_info_reg.sim = 10;
2312 net_info.sim_state = MBTK_SIM_NETWORK_PERSONALIZATION;
2313 }
2314 else if(strStartsWith(s, "+CPIN: PH-NET PUK"))
2315 {
2316 sim_info_reg.sim = 11;
2317 net_info.sim_state = MBTK_SIM_ABSENT;
2318 }
2319 else if(strStartsWith(s, "+CPIN: PH-NETSUB PINMT"))
2320 {
2321 sim_info_reg.sim = 12;
2322 net_info.sim_state = MBTK_SIM_ABSENT;
2323 }
2324 else if(strStartsWith(s, "+CPIN: PH-NETSUB PUK"))
2325 {
2326 sim_info_reg.sim = 13;
2327 net_info.sim_state = MBTK_SIM_ABSENT;
2328 }
2329 else if(strStartsWith(s, "+CPIN: PH-SP PIN"))
2330 {
2331 sim_info_reg.sim = 14;
2332 net_info.sim_state = MBTK_SIM_ABSENT;
2333 }
2334 else if(strStartsWith(s, "+CPIN: PH-SP PUK"))
2335 {
2336 sim_info_reg.sim = 15;
2337 net_info.sim_state = MBTK_SIM_ABSENT;
2338 }
2339 else if(strStartsWith(s, "+CPIN: PH-CORP PIN"))
2340 {
2341 sim_info_reg.sim = 16;
2342 net_info.sim_state = MBTK_SIM_ABSENT;
2343 }
2344 else if(strStartsWith(s, "+CPIN: PH-CORP PUK"))
2345 {
2346 sim_info_reg.sim = 17;
2347 net_info.sim_state = MBTK_SIM_ABSENT;
2348 }
2349 else if(strStartsWith(s, "+CPIN: SIM REMOVED"))
2350 {
2351 sim_info_reg.sim = 18;
2352 net_info.sim_state = MBTK_SIM_ABSENT;
2353 }
2354 else
2355 sim_info_reg.sim = 20;
2356
2357 if(sim_info_reg.sim == 18)
2358 {
2359 uint8 data_pdp;
2360 data_pdp = 11; //
2361 urc_msg_distribute(false, INFO_URC_MSG_PDP_STATE, &data_pdp, sizeof(uint8));
2362 }
2363
2364 urc_msg_distribute(false, INFO_URC_MSG_SIM_STATE, &sim_info_reg, sizeof(mbtk_sim_card_info));
2365 }
2366 // +CMT: ,23
2367 // 0891683108200855F6240D91688189911196F10000221130717445230331D90C
2368 else if(strStartsWith(s, "+CMT:") || sms_cmt)
2369 {
2370 if(!sms_cmt){
2371 sms_cmt = true;
2372 }else{
2373 sms_cmt = false;
2374 }
2375 printf("+CMT() sms_cmt:%d, s:%s, len:%d\n",sms_cmt, s, strlen(s));
2376 urc_msg_distribute(false, INFO_URC_MSG_SMS_STATE, s, strlen(s));
2377 }
b.liub4772072024-08-15 14:47:03 +08002378#endif
b.liu87afc4c2024-08-14 17:33:45 +08002379 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"
2380 {
2381
2382 }
2383 else
2384 {
2385 LOGV("Unknown URC : %s", s);
2386 }
b.liu87afc4c2024-08-14 17:33:45 +08002387}
2388
2389static int openSocket(const char* sockname)
2390{
2391 int sock = socket(AF_UNIX, SOCK_STREAM, 0);
2392 if (sock < 0)
2393 {
2394 LOGE("Error create socket: %s\n", strerror(errno));
2395 return -1;
2396 }
2397 struct sockaddr_un addr;
2398 memset(&addr, 0, sizeof(addr));
2399 addr.sun_family = AF_UNIX;
2400 strncpy(addr.sun_path, sockname, sizeof(addr.sun_path));
2401 while (TEMP_FAILURE_RETRY(connect(sock,(const struct sockaddr*)&addr, sizeof(addr))) != 0)
2402 {
2403 LOGE("Error connect to socket %s: %s, try again", sockname, strerror(errno));
2404 sleep(1);
2405 }
2406
2407#if 0
2408 int sk_flags = fcntl(sock, F_GETFL, 0);
2409 fcntl(sock, F_SETFL, sk_flags | O_NONBLOCK);
2410#endif
2411
2412 return sock;
2413}
2414
b.liude989912025-02-12 14:40:42 +08002415static void ril_at_ready_process()
2416{
b.liub171c9a2024-11-12 19:23:29 +08002417 ril_info.radio_state = ril_radio_state_get(ATPORTTYPE_0);
b.liu87afc4c2024-08-14 17:33:45 +08002418 if (ril_info.radio_state != MBTK_RADIO_STATE_FULL_FUNC)
b.liude989912025-02-12 14:40:42 +08002419 {
b.liub171c9a2024-11-12 19:23:29 +08002420 ril_radio_state_set(ATPORTTYPE_0, MBTK_RADIO_STATE_FULL_FUNC, FALSE);
b.liu87afc4c2024-08-14 17:33:45 +08002421 }
b.liude989912025-02-12 14:40:42 +08002422
b.liu87afc4c2024-08-14 17:33:45 +08002423 if(ril_info.radio_state == MBTK_RADIO_STATE_FULL_FUNC)
2424 {
b.liub171c9a2024-11-12 19:23:29 +08002425 at_send_command(ATPORTTYPE_0, "AT+CEREG=2", NULL);
b.liude989912025-02-12 14:40:42 +08002426 }
2427
b.liub171c9a2024-11-12 19:23:29 +08002428 ril_info.sim_state = ril_sim_state_get(ATPORTTYPE_0);
b.liu87afc4c2024-08-14 17:33:45 +08002429 if(ril_info.sim_state == MBTK_SIM_STATE_READY)
b.liude989912025-02-12 14:40:42 +08002430 {
2431 LOGD("SIM READY!");
b.liub171c9a2024-11-12 19:23:29 +08002432 at_send_command(ATPORTTYPE_0, "AT+COPS=3", NULL);
b.liu87afc4c2024-08-14 17:33:45 +08002433
2434 // Set APN from prop.
b.liub171c9a2024-11-12 19:23:29 +08002435 apn_auto_conf_from_prop(ATPORTTYPE_0);
b.liude989912025-02-12 14:40:42 +08002436 }
2437 else
2438 {
2439 LOGE("SIM NOT READY!");
b.liu87afc4c2024-08-14 17:33:45 +08002440 }
2441}
2442
2443static void ind_regisger(sock_cli_info_t* cli_info, uint16 ind)
b.liude989912025-02-12 14:40:42 +08002444{
2445 uint32 i = 0;
2446 while(i < cli_info->ind_num)
2447 {
2448 if(cli_info->ind_register[i] == ind)
2449 break;
2450 i++;
2451 }
2452
2453 if(i == cli_info->ind_num) // No found IND
2454 {
2455 cli_info->ind_register[i] = ind;
2456 cli_info->ind_num++;
b.liu87afc4c2024-08-14 17:33:45 +08002457 LOGD("Register IND : %s", id2str(ind));
b.liude989912025-02-12 14:40:42 +08002458 }
2459 else
2460 {
b.liu87afc4c2024-08-14 17:33:45 +08002461 LOGW("IND had exist.");
b.liude989912025-02-12 14:40:42 +08002462 }
b.liu87afc4c2024-08-14 17:33:45 +08002463}
2464
b.liude989912025-02-12 14:40:42 +08002465// Process AT URC data
b.liu87afc4c2024-08-14 17:33:45 +08002466static int send_pack_to_queue(sock_cli_info_t* cli_info, void* pack)
b.liub171c9a2024-11-12 19:23:29 +08002467{
2468 if(cli_info) {
2469 if(ril_info.msg_queue[cli_info->port].count >= PACK_PROCESS_QUEUE_MAX)
2470 {
2471 LOGE("Packet process queue is full");
2472 return -1;
2473 }
2474 } else {
2475 if(ril_info.msg_queue[ATPORTTYPE_0].count >= PACK_PROCESS_QUEUE_MAX)
2476 {
2477 LOGE("Packet process queue is full");
2478 return -1;
2479 }
2480 }
b.liude989912025-02-12 14:40:42 +08002481
b.liu87afc4c2024-08-14 17:33:45 +08002482 ril_msg_queue_info_t *item = (ril_msg_queue_info_t*)malloc(sizeof(ril_msg_queue_info_t));
b.liude989912025-02-12 14:40:42 +08002483 if(!item)
2484 {
b.liu87afc4c2024-08-14 17:33:45 +08002485 LOGE("malloc() fail[%d].", errno);
b.liude989912025-02-12 14:40:42 +08002486 return -1;
2487 }
2488 item->cli_info = cli_info;
b.liub171c9a2024-11-12 19:23:29 +08002489 item->pack = pack;
2490
2491 if(cli_info) {
2492 mbtk_queue_put(&(ril_info.msg_queue[cli_info->port]), item);
2493
2494 // If thread is waitting,continue it.
2495 pthread_mutex_lock(&(ril_info.msg_mutex[cli_info->port]));
2496 pthread_cond_signal(&(ril_info.msg_cond[cli_info->port]));
2497 pthread_mutex_unlock(&(ril_info.msg_mutex[cli_info->port]));
2498 } else { // URC message, is null.
2499 mbtk_queue_put(&(ril_info.msg_queue[ATPORTTYPE_0]), item);
2500
2501 // If thread is waitting,continue it.
2502 pthread_mutex_lock(&(ril_info.msg_mutex[ATPORTTYPE_0]));
2503 pthread_cond_signal(&(ril_info.msg_cond[ATPORTTYPE_0]));
2504 pthread_mutex_unlock(&(ril_info.msg_mutex[ATPORTTYPE_0]));
2505 }
b.liude989912025-02-12 14:40:42 +08002506
2507 return 0;
b.liu87afc4c2024-08-14 17:33:45 +08002508}
2509
2510
2511static void pack_distribute(sock_cli_info_t* cli_info, ril_msg_pack_info_t* pack)
b.liude989912025-02-12 14:40:42 +08002512{
2513 // Register IND Message.
b.liu15f456b2024-10-31 20:16:06 +08002514 if(pack->msg_type == RIL_MSG_TYPE_REQ)
2515 {
2516 if(pack->msg_id > RIL_MSG_ID_IND_BEGIN
2517 && pack->msg_id < RIL_MSG_ID_IND_END) {
2518 mbtk_ril_err_enum err = MBTK_RIL_ERR_SUCCESS;
2519 if(cli_info->ind_num >= IND_REGISTER_MAX)
2520 {
2521 LOGE("IND if full.");
2522 err = MBTK_RIL_ERR_IND_FULL;
2523 }
2524 else
2525 {
2526 ind_regisger(cli_info, pack->msg_id);
2527 }
2528
b.liub171c9a2024-11-12 19:23:29 +08002529 ril_error_pack_send(cli_info->port, cli_info->fd, pack->msg_id, pack->msg_index, err);
b.liu15f456b2024-10-31 20:16:06 +08002530
2531 ril_msg_pack_free(pack);
2532 } else {
b.liub171c9a2024-11-12 19:23:29 +08002533 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 +08002534 if(0 && pack->data_len > 0)
2535 {
2536 log_hex("DATA", pack->data, pack->data_len);
2537 }
2538
2539 // Send to REQ_process_thread process.
2540 send_pack_to_queue(cli_info, pack);
2541
2542 // For test.
2543 // pack_error_send(cli_info->fd, pack->info_id + 1, MBTK_INFO_ERR_SUCCESS);
2544 }
2545 } else {
2546 LOGE("Pack type error : %d", pack->msg_type);
2547 }
b.liu87afc4c2024-08-14 17:33:45 +08002548}
2549
b.liude989912025-02-12 14:40:42 +08002550// Return MBTK_INFO_ERR_SUCCESS,will call pack_error_send() to send RSP.
2551// Otherwise, do not call pack_error_send().
b.liu87afc4c2024-08-14 17:33:45 +08002552static mbtk_ril_err_enum pack_req_process(sock_cli_info_t* cli_info, ril_msg_pack_info_t* pack)
b.liude989912025-02-12 14:40:42 +08002553{
b.liu87afc4c2024-08-14 17:33:45 +08002554 if(pack->msg_id > RIL_MSG_ID_DEV_BEGIN && pack->msg_id < RIL_MSG_ID_DEV_END) {
2555 return dev_pack_req_process(cli_info, pack);
2556 } else if(pack->msg_id > RIL_MSG_ID_SIM_BEGIN && pack->msg_id < RIL_MSG_ID_SIM_END) {
2557 return sim_pack_req_process(cli_info, pack);
2558 } else if(pack->msg_id > RIL_MSG_ID_NET_BEGIN && pack->msg_id < RIL_MSG_ID_NET_END) {
2559 return net_pack_req_process(cli_info, pack);
b.liu15f456b2024-10-31 20:16:06 +08002560 } else if(pack->msg_id > RIL_MSG_ID_DATA_CALL_BEGIN && pack->msg_id < RIL_MSG_ID_DATA_CALL_END) {
2561 return data_call_pack_req_process(cli_info, pack);
b.liu87afc4c2024-08-14 17:33:45 +08002562 } else if(pack->msg_id > RIL_MSG_ID_CALL_BEGIN && pack->msg_id < RIL_MSG_ID_CALL_END) {
2563 return call_pack_req_process(cli_info, pack);
2564 } else if(pack->msg_id > RIL_MSG_ID_SMS_BEGIN && pack->msg_id < RIL_MSG_ID_SMS_END) {
2565 return sms_pack_req_process(cli_info, pack);
2566 } else if(pack->msg_id > RIL_MSG_ID_PB_BEGIN && pack->msg_id < RIL_MSG_ID_PB_END) {
2567 return pb_pack_req_process(cli_info, pack);
b.liu15f456b2024-10-31 20:16:06 +08002568 } else if(pack->msg_id > RIL_MSG_ID_ECALL_BEGIN && pack->msg_id < RIL_MSG_ID_ECALL_END) {
2569 return ecall_pack_req_process(cli_info, pack);
b.liu87afc4c2024-08-14 17:33:45 +08002570 } else {
2571 LOGW("Unknown msg id : %d", pack->msg_id);
2572 return MBTK_RIL_ERR_FORMAT;
2573 }
2574}
2575
2576static void urc_msg_process(ril_urc_msg_info_t *msg)
b.liude989912025-02-12 14:40:42 +08002577{
b.liu472cfaf2024-12-19 19:08:19 +08002578 // data can be NULL (For RIL_URC_MSG_BAND_SET)
b.liu15f456b2024-10-31 20:16:06 +08002579 if(!msg->data || msg->data_len <= 0) {
b.liu472cfaf2024-12-19 19:08:19 +08002580 LOGW("URC data is NULL.");
2581 // return;
b.liu15f456b2024-10-31 20:16:06 +08002582 }
2583
b.liude989912025-02-12 14:40:42 +08002584 switch(msg->msg) {
b.liu15f456b2024-10-31 20:16:06 +08002585 case RIL_MSG_ID_IND_RADIO_STATE_CHANGE:
2586 {
2587 mbtk_ril_radio_state_info_t *state = (mbtk_ril_radio_state_info_t*)msg->data;
2588 LOGD("Radio state : %d", state->radio_state);
b.liude989912025-02-12 14:40:42 +08002589 break;
b.liu15f456b2024-10-31 20:16:06 +08002590 }
b.liufd87baf2024-11-15 15:30:38 +08002591 case RIL_MSG_ID_IND_SIM_STATE_CHANGE:
2592 {
2593 mbtk_ril_sim_state_info_t *state = (mbtk_ril_sim_state_info_t*)msg->data;
2594 if(state->sim_state == MBTK_SIM_STATE_READY) {
2595 LOGD("SIM READY!");
2596 at_send_command(ATPORTTYPE_0, "AT+COPS=3", NULL);
2597
2598 // Set APN from prop.
2599 apn_auto_conf_from_prop(ATPORTTYPE_0);
2600 }
2601 }
b.liuafdf2c62024-11-12 11:10:44 +08002602 case RIL_MSG_ID_IND_NET_REG_STATE_CHANGE:
2603 {
2604 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 +08002605 data_call_retry(ATPORTTYPE_0, reg_state);
b.liuafdf2c62024-11-12 11:10:44 +08002606 break;
2607 }
b.liu472cfaf2024-12-19 19:08:19 +08002608 case RIL_URC_MSG_BAND_SET:
2609 {
2610 int cme_err = MBTK_RIL_ERR_CME_NON;
2611 if(req_band_set(ATPORTTYPE_0, &band_info.band_support, &cme_err) || cme_err != MBTK_RIL_ERR_CME_NON)
2612 {
2613 LOGE("Set band fail.");
2614 }
2615 else // Set band success.
2616 {
2617 // log_hex("BAND-2", &band_set_info, sizeof(band_set_info_t));
2618 band_info.band_set_success = TRUE;
2619 if(band_info.band_area == MBTK_MODEM_BAND_AREA_CN) {
2620 property_set("persist.mbtk.band_config", "CN");
2621 } else if(band_info.band_area == MBTK_MODEM_BAND_AREA_EU) {
2622 property_set("persist.mbtk.band_config", "EU");
2623 } else if(band_info.band_area == MBTK_MODEM_BAND_AREA_SA) {
2624 property_set("persist.mbtk.band_config", "SA");
2625 } else {
2626 property_set("persist.mbtk.band_config", "ALL");
2627 }
2628 LOGD("Set band success.");
2629 }
2630 break;
2631 }
b.liude989912025-02-12 14:40:42 +08002632 default:
2633 {
2634 LOGE("Unknown URC : %d", msg->msg);
2635 break;
2636 }
2637 }
b.liu87afc4c2024-08-14 17:33:45 +08002638}
2639
2640// Read client conn/msg and push into ril_info.msg_queue.
2641static void* ril_read_pthread(void* arg)
b.liude989912025-02-12 14:40:42 +08002642{
2643 UNUSED(arg);
b.liu87afc4c2024-08-14 17:33:45 +08002644 ril_info.epoll_fd = epoll_create(SOCK_CLIENT_MAX + 1);
2645 if(ril_info.epoll_fd < 0)
b.liude989912025-02-12 14:40:42 +08002646 {
b.liu87afc4c2024-08-14 17:33:45 +08002647 LOGE("epoll_create() fail[%d].", errno);
b.liude989912025-02-12 14:40:42 +08002648 return NULL;
2649 }
2650
2651 uint32 event = EPOLLIN | EPOLLET;
2652 struct epoll_event ev;
b.liu87afc4c2024-08-14 17:33:45 +08002653 ev.data.fd = ril_info.sock_listen_fd;
b.liude989912025-02-12 14:40:42 +08002654 ev.events = event; //EPOLLIN | EPOLLERR | EPOLLET;
b.liu87afc4c2024-08-14 17:33:45 +08002655 epoll_ctl(ril_info.epoll_fd, EPOLL_CTL_ADD, ril_info.sock_listen_fd, &ev);
b.liude989912025-02-12 14:40:42 +08002656
2657 int nready = -1;
2658 struct epoll_event epoll_events[EPOLL_LISTEN_MAX];
2659 while(1)
2660 {
b.liu87afc4c2024-08-14 17:33:45 +08002661 nready = epoll_wait(ril_info.epoll_fd, epoll_events, EPOLL_LISTEN_MAX, -1);
b.liude989912025-02-12 14:40:42 +08002662 if(nready > 0)
2663 {
b.liu87afc4c2024-08-14 17:33:45 +08002664 sock_cli_info_t *cli_info = NULL;
b.liude989912025-02-12 14:40:42 +08002665 int i;
2666 for(i = 0; i < nready; i++)
2667 {
2668 LOG("fd[%d] event = %x",epoll_events[i].data.fd, epoll_events[i].events);
2669 if(epoll_events[i].events & EPOLLHUP) // Client Close.
2670 {
2671 if((cli_info = cli_find(epoll_events[i].data.fd)) != NULL)
2672 {
2673 cli_close(cli_info);
2674 }
2675 else
2676 {
2677 LOG("Unknown client[fd = %d].", epoll_events[i].data.fd);
2678 }
2679 }
2680 else if(epoll_events[i].events & EPOLLIN)
2681 {
b.liu87afc4c2024-08-14 17:33:45 +08002682 if(epoll_events[i].data.fd == ril_info.sock_listen_fd) // New clients connected.
b.liude989912025-02-12 14:40:42 +08002683 {
2684 int client_fd = -1;
2685 while(1)
2686 {
2687 struct sockaddr_in cliaddr;
2688 socklen_t clilen = sizeof(cliaddr);
2689 client_fd = accept(epoll_events[i].data.fd, (struct sockaddr *) &cliaddr, &clilen);
2690 if(client_fd < 0)
2691 {
2692 if(errno == EAGAIN)
2693 {
2694 LOG("All client connect get.");
2695 }
2696 else
2697 {
2698 LOG("accept() error[%d].", errno);
2699 }
2700 break;
2701 }
2702 // Set O_NONBLOCK
2703 int flags = fcntl(client_fd, F_GETFL, 0);
2704 if (flags > 0)
2705 {
2706 flags |= O_NONBLOCK;
2707 if (fcntl(client_fd, F_SETFL, flags) < 0)
2708 {
2709 LOG("Set flags error:%d", errno);
2710 }
2711 }
2712
2713 memset(&ev,0,sizeof(struct epoll_event));
2714 ev.data.fd = client_fd;
2715 ev.events = event;//EPOLLIN | EPOLLERR | EPOLLET;
b.liu87afc4c2024-08-14 17:33:45 +08002716 epoll_ctl(ril_info.epoll_fd, EPOLL_CTL_ADD, client_fd, &ev);
b.liude989912025-02-12 14:40:42 +08002717
b.liu87afc4c2024-08-14 17:33:45 +08002718 sock_cli_info_t *info = (sock_cli_info_t*)malloc(sizeof(sock_cli_info_t));
b.liude989912025-02-12 14:40:42 +08002719 if(info)
2720 {
b.liu87afc4c2024-08-14 17:33:45 +08002721 memset(info, 0, sizeof(sock_cli_info_t));
2722 info->fd = client_fd;
b.liub171c9a2024-11-12 19:23:29 +08002723
2724 // Default AT port.
2725 info->port = ATPORTTYPE_0;
2726
b.liu87afc4c2024-08-14 17:33:45 +08002727 list_add(ril_info.sock_client_list, info);
2728 LOG("Add New Client FD Into List.");
2729
b.liu15f456b2024-10-31 20:16:06 +08002730 // Send msg RIL_MSG_ID_IND_SER_STATE_CHANGE to client.
2731 mbtk_ril_ser_state_enum state = MBTK_RIL_SER_STATE_READY;
2732 ril_ind_pack_send(client_fd, RIL_MSG_ID_IND_SER_STATE_CHANGE, &state, 1);
b.liude989912025-02-12 14:40:42 +08002733 }
2734 else
2735 {
2736 LOG("malloc() fail.");
2737 }
2738 }
2739 }
2740 else if((cli_info = cli_find(epoll_events[i].data.fd)) != NULL) // Client data arrive.
2741 {
2742 // Read and process every message.
b.liu87afc4c2024-08-14 17:33:45 +08002743 mbtk_ril_err_enum err = MBTK_RIL_ERR_SUCCESS;
2744 ril_msg_pack_info_t** pack = ril_pack_recv(cli_info->fd, true, &err);
b.liub171c9a2024-11-12 19:23:29 +08002745
b.liude989912025-02-12 14:40:42 +08002746 // Parse packet error,send error response to client.
2747 if(pack == NULL)
2748 {
b.liub171c9a2024-11-12 19:23:29 +08002749 ril_error_pack_send(cli_info->port, cli_info->fd, RIL_MSG_ID_UNKNOWN, RIL_MSG_INDEX_INVALID, err);
b.liude989912025-02-12 14:40:42 +08002750 }
2751 else
2752 {
b.liu87afc4c2024-08-14 17:33:45 +08002753 ril_msg_pack_info_t** pack_ptr = pack;
b.liude989912025-02-12 14:40:42 +08002754 while(*pack_ptr)
b.liub171c9a2024-11-12 19:23:29 +08002755 {
2756 // Update AT port in the first.
2757 cli_info->port = (ATPortType_enum)((*pack_ptr)->at_port);
2758
b.liude989912025-02-12 14:40:42 +08002759 pack_distribute(cli_info, *pack_ptr);
2760 // Not free,will free in pack_process() or packet process thread.
2761 //mbtk_info_pack_free(pack_ptr);
2762 pack_ptr++;
2763 }
b.liu87afc4c2024-08-14 17:33:45 +08002764
b.liude989912025-02-12 14:40:42 +08002765 free(pack);
2766 }
2767 }
2768 else
2769 {
2770 LOG("Unknown socket : %d", epoll_events[i].data.fd);
2771 }
b.liu15f456b2024-10-31 20:16:06 +08002772 }
b.liude989912025-02-12 14:40:42 +08002773 else
2774 {
2775 LOG("Unknown event : %x", epoll_events[i].events);
2776 }
2777 }
2778 }
2779 else
2780 {
2781 LOG("epoll_wait() fail[%d].", errno);
2782 }
2783 }
b.liub171c9a2024-11-12 19:23:29 +08002784
b.liude989912025-02-12 14:40:42 +08002785 return NULL;
b.liu87afc4c2024-08-14 17:33:45 +08002786}
2787
b.liude989912025-02-12 14:40:42 +08002788static void band_support_init()
2789{
2790 mbtk_device_info_modem_t info_modem;
b.liu87afc4c2024-08-14 17:33:45 +08002791 memset(&band_info.band_support, 0, sizeof(mbtk_band_info_t));
b.liude989912025-02-12 14:40:42 +08002792 memset(&info_modem, 0, sizeof(mbtk_device_info_modem_t));
2793 if(mbtk_dev_info_read(MBTK_DEVICE_INFO_ITEM_MODEM, &(info_modem), sizeof(mbtk_device_info_modem_t))) {
b.liu61ad9172025-01-09 14:33:55 +08002794 LOGD("mbtk_dev_info_read(MODEM) fail, use default band.");
b.liu87afc4c2024-08-14 17:33:45 +08002795 band_info.band_area = MBTK_MODEM_BAND_AREA_ALL;
b.liu61ad9172025-01-09 14:33:55 +08002796#ifdef MBTK_5G_SUPPORT
2797 band_info.band_support.net_pref = MBTK_NET_PREF_LTE_NR_NR_PREF; // 19
2798 band_info.net_support = MBTK_NET_SUPPORT_4G | MBTK_NET_SUPPORT_5G;
b.liude989912025-02-12 14:40:42 +08002799
2800 band_info.band_support.gsm_band = 0;
2801 band_info.band_support.umts_band = 0;
2802 band_info.band_support.tdlte_band = MBTK_BAND_ALL_TDLTE_DEFAULT;
2803 band_info.band_support.fddlte_band = MBTK_BAND_ALL_FDDLTE_DEFAULT;
2804 band_info.band_support.lte_ext_band = MBTK_BAND_ALL_EXT_LTE_DEFAULT;
2805
2806 band_info.band_support.nr_3_band = MBTK_BAND_ALL_NR_3_DEFAULT;
2807 band_info.band_support.nr_2_band = MBTK_BAND_ALL_NR_2_DEFAULT;
2808 band_info.band_support.nr_1_band = MBTK_BAND_ALL_NR_1_DEFAULT;
2809 band_info.band_support.nr_0_band = MBTK_BAND_ALL_NR_0_DEFAULT;
b.liu61ad9172025-01-09 14:33:55 +08002810#else
b.liu472cfaf2024-12-19 19:08:19 +08002811 band_info.band_support.net_pref = MBTK_NET_PREF_GSM_UMTS_LTE_LTE_PREF; // 15
b.liu61ad9172025-01-09 14:33:55 +08002812 band_info.net_support = MBTK_NET_SUPPORT_2G | MBTK_NET_SUPPORT_3G | MBTK_NET_SUPPORT_4G;
b.liud363a772025-01-09 15:22:52 +08002813
b.liu87afc4c2024-08-14 17:33:45 +08002814 band_info.band_support.gsm_band = MBTK_BAND_ALL_GSM_DEFAULT;
2815 band_info.band_support.umts_band = MBTK_BAND_ALL_WCDMA_DEFAULT;
2816 band_info.band_support.tdlte_band = MBTK_BAND_ALL_TDLTE_DEFAULT;
2817 band_info.band_support.fddlte_band = MBTK_BAND_ALL_FDDLTE_DEFAULT;
2818 band_info.band_support.lte_ext_band = MBTK_BAND_ALL_EXT_LTE_DEFAULT;
b.liud363a772025-01-09 15:22:52 +08002819
b.liude989912025-02-12 14:40:42 +08002820 band_info.band_support.nr_3_band = 0;
2821 band_info.band_support.nr_2_band = 0;
2822 band_info.band_support.nr_1_band = 0;
2823 band_info.band_support.nr_0_band = 0;
b.liuf2ea8bf2025-01-09 15:07:34 +08002824#endif
b.liude989912025-02-12 14:40:42 +08002825 } else {
b.liu87afc4c2024-08-14 17:33:45 +08002826 band_info.band_area = info_modem.band_area;
b.liude989912025-02-12 14:40:42 +08002827
b.liu61ad9172025-01-09 14:33:55 +08002828 band_info.net_support = info_modem.net_support;
b.liu472cfaf2024-12-19 19:08:19 +08002829 if(info_modem.net_pref < MBTK_NET_PREF_MAX) {
2830 band_info.band_support.net_pref = info_modem.net_pref;
2831 } else {
b.liu61ad9172025-01-09 14:33:55 +08002832 if(band_info.net_support & MBTK_NET_SUPPORT_5G) {
2833 band_info.band_support.net_pref = MBTK_NET_PREF_LTE_NR_NR_PREF; // 19
2834 } else {
2835 band_info.band_support.net_pref = MBTK_NET_PREF_GSM_UMTS_LTE_LTE_PREF; // 15
2836 }
b.liu472cfaf2024-12-19 19:08:19 +08002837 }
b.liu87afc4c2024-08-14 17:33:45 +08002838 band_info.band_support.gsm_band = info_modem.band_gsm;
2839 band_info.band_support.umts_band = info_modem.band_wcdma;
2840 band_info.band_support.tdlte_band = info_modem.band_tdlte;
2841 band_info.band_support.fddlte_band = info_modem.band_fddlte;
2842 band_info.band_support.lte_ext_band = info_modem.band_lte_ext;
b.liude989912025-02-12 14:40:42 +08002843
b.liu61ad9172025-01-09 14:33:55 +08002844 band_info.band_support.nr_3_band = info_modem.band_nr_3;
2845 band_info.band_support.nr_2_band = info_modem.band_nr_2;
2846 band_info.band_support.nr_1_band = info_modem.band_nr_1;
2847 band_info.band_support.nr_0_band = info_modem.band_nr_0;
b.liude989912025-02-12 14:40:42 +08002848 }
2849}
2850
2851static void* ril_process_thread(void* arg)
2852{
2853 UNUSED(arg);
2854 ATPortType_enum *port = (ATPortType_enum*)arg;
2855 ril_msg_queue_info_t* item = NULL;
2856
2857 pthread_mutex_lock(&(ril_info.msg_mutex[*port]));
2858 while(TRUE)
2859 {
2860 if(mbtk_queue_empty(&(ril_info.msg_queue[*port])))
2861 {
2862 LOG("[Port-%d]Packet process wait...", *port);
2863 pthread_cond_wait(&(ril_info.msg_cond[*port]), &(ril_info.msg_mutex[*port]));
2864 LOG("[Port-%d]Packet process continue...", *port);
2865 }
2866 else
2867 {
2868 LOG("Packet process queue not empty,continue...");
2869 }
2870
2871 // Process all information request.
2872 mbtk_ril_err_enum err;
2873 while((item = (ril_msg_queue_info_t*)mbtk_queue_get(&(ril_info.msg_queue[*port]))) != NULL)
2874 {
2875 if(item->cli_info) { // REQ form client.
2876 ril_msg_pack_info_t *pack = (ril_msg_pack_info_t*)item->pack;
2877 LOGD("Process REQ %s.", id2str(pack->msg_id));
2878 ril_info.at_process[*port] = true;
2879 err = pack_req_process(item->cli_info, pack);
2880 if(err != MBTK_RIL_ERR_SUCCESS)
2881 {
2882 ril_error_pack_send(item->cli_info->port, item->cli_info->fd, pack->msg_id, pack->msg_index, err);
2883 }
2884 ril_info.at_process[*port] = false;
2885 ril_msg_pack_free(pack);
2886 free(item);
2887 } else { // REQ from myself.
2888 if(item->pack) {
2889 ril_urc_msg_info_t *urc = (ril_urc_msg_info_t*)item->pack;
2890 LOGD("Process URC %d.", urc->msg);
2891 urc_msg_process(urc);
2892 if(urc->data)
2893 free(urc->data);
2894 free(urc);
2895 }
2896 }
2897 }
2898 }
2899 pthread_mutex_unlock(&(ril_info.msg_mutex[*port]));
2900
2901 free(port);
2902
2903 return NULL;
2904}
2905
2906/*
2907AT*BAND=15,78,147,482,134742231
2908
2909OK
2910*/
2911static void* band_config_thread()
2912{
2913 band_info.band_set_success = FALSE;
b.liu62240ee2024-11-07 17:52:45 +08002914// bool is_first = TRUE;
b.liu87afc4c2024-08-14 17:33:45 +08002915 while(!band_info.band_set_success) {
2916 // Set band.
b.liu472cfaf2024-12-19 19:08:19 +08002917#if 1
2918 ril_urc_msg_info_t *msg = (ril_urc_msg_info_t*)malloc(sizeof(ril_urc_msg_info_t));
2919 if(msg) {
2920 msg->msg = RIL_URC_MSG_BAND_SET;
2921 msg->data = NULL;//mbtk_memcpy(&band_info, sizeof(ril_band_info_t));
2922 msg->data_len = 0; //sizeof(ril_band_info_t);
b.liu87afc4c2024-08-14 17:33:45 +08002923#if 0
b.liude989912025-02-12 14:40:42 +08002924 if(msg->data == NULL) {
2925 LOGE("mbtk_memcpy() fail.");
2926 break;
2927 }
b.liu472cfaf2024-12-19 19:08:19 +08002928#endif
2929 send_pack_to_queue(NULL, msg);
2930
b.liude989912025-02-12 14:40:42 +08002931 sleep(5);
b.liu472cfaf2024-12-19 19:08:19 +08002932 } else {
2933 LOG("malloc() fail[%d].", errno);
2934 break;
b.liu87afc4c2024-08-14 17:33:45 +08002935 }
2936#else
2937 sleep(5);
2938#endif
b.liude989912025-02-12 14:40:42 +08002939 }
2940
2941 LOGD("Set Band thread exit.");
2942 return NULL;
2943}
b.liu87afc4c2024-08-14 17:33:45 +08002944
2945
2946int ril_server_start()
b.liude989912025-02-12 14:40:42 +08002947{
b.liu87afc4c2024-08-14 17:33:45 +08002948 signal(SIGPIPE, SIG_IGN);
2949
2950 memset(&ril_info, 0, sizeof(ril_info_t));
2951 memset(&band_info, 0, sizeof(ril_band_info_t));
2952 memset(&band_info.band_support, 0xFF, sizeof(mbtk_band_info_t));
b.liude989912025-02-12 14:40:42 +08002953
2954 //check cfun and sim card status
2955 ril_at_ready_process();
2956
2957 // Init support band.
2958 band_support_init();
2959
2960 //any AT instruction that is not sent through pack_process_thread needs to precede the thread
2961 //thread create
b.liu87afc4c2024-08-14 17:33:45 +08002962 if(ril_info.sock_listen_fd > 0)
b.liude989912025-02-12 14:40:42 +08002963 {
b.liu87afc4c2024-08-14 17:33:45 +08002964 LOGE("Information Server Has Started.");
b.liude989912025-02-12 14:40:42 +08002965 return -1;
2966 }
2967
2968 struct sockaddr_un server_addr;
b.liu87afc4c2024-08-14 17:33:45 +08002969 ril_info.sock_listen_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
2970 if(ril_info.sock_listen_fd < 0)
b.liude989912025-02-12 14:40:42 +08002971 {
b.liu87afc4c2024-08-14 17:33:45 +08002972 LOGE("socket() fail[%d].", errno);
b.liude989912025-02-12 14:40:42 +08002973 return -1;
2974 }
2975
2976 // Set O_NONBLOCK
b.liu87afc4c2024-08-14 17:33:45 +08002977 int flags = fcntl(ril_info.sock_listen_fd, F_GETFL, 0);
b.liude989912025-02-12 14:40:42 +08002978 if (flags < 0)
2979 {
b.liu87afc4c2024-08-14 17:33:45 +08002980 LOGE("Get flags error:%d", errno);
b.liude989912025-02-12 14:40:42 +08002981 goto error;
2982 }
2983 flags |= O_NONBLOCK;
b.liu87afc4c2024-08-14 17:33:45 +08002984 if (fcntl(ril_info.sock_listen_fd, F_SETFL, flags) < 0)
b.liude989912025-02-12 14:40:42 +08002985 {
b.liu87afc4c2024-08-14 17:33:45 +08002986 LOGE("Set flags error:%d", errno);
b.liude989912025-02-12 14:40:42 +08002987 goto error;
2988 }
2989
b.liu87afc4c2024-08-14 17:33:45 +08002990 unlink(RIL_SOCK_NAME);
b.liude989912025-02-12 14:40:42 +08002991 memset(&server_addr, 0, sizeof(struct sockaddr_un));
2992 server_addr.sun_family = AF_LOCAL;
b.liu87afc4c2024-08-14 17:33:45 +08002993 strcpy(server_addr.sun_path, RIL_SOCK_NAME);
2994 if(bind(ril_info.sock_listen_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)))
b.liude989912025-02-12 14:40:42 +08002995 {
b.liu87afc4c2024-08-14 17:33:45 +08002996 LOGE("bind() fail[%d].", errno);
b.liude989912025-02-12 14:40:42 +08002997 goto error;
2998 }
2999
b.liu87afc4c2024-08-14 17:33:45 +08003000 if(listen(ril_info.sock_listen_fd, SOCK_CLIENT_MAX))
b.liude989912025-02-12 14:40:42 +08003001 {
b.liu87afc4c2024-08-14 17:33:45 +08003002 LOGE("listen() fail[%d].", errno);
b.liude989912025-02-12 14:40:42 +08003003 goto error;
3004 }
3005
b.liu87afc4c2024-08-14 17:33:45 +08003006 ril_info.sock_client_list = list_create(sock_cli_free_func);
3007 if(ril_info.sock_client_list == NULL)
b.liude989912025-02-12 14:40:42 +08003008 {
b.liu87afc4c2024-08-14 17:33:45 +08003009 LOGE("list_create() fail.");
b.liude989912025-02-12 14:40:42 +08003010 goto error;
b.liu87afc4c2024-08-14 17:33:45 +08003011 }
3012
b.liub171c9a2024-11-12 19:23:29 +08003013 mbtk_queue_init(&(ril_info.msg_queue[ATPORTTYPE_0]));
3014 pthread_mutex_init(&(ril_info.msg_mutex[ATPORTTYPE_0]), NULL);
3015 pthread_cond_init(&(ril_info.msg_cond[ATPORTTYPE_0]), NULL);
b.liude989912025-02-12 14:40:42 +08003016
b.liu62240ee2024-11-07 17:52:45 +08003017 pthread_t info_pid, pack_pid/*, monitor_pid, urc_pid, bootconn_pid*/;
b.liude989912025-02-12 14:40:42 +08003018 pthread_attr_t thread_attr;
3019 pthread_attr_init(&thread_attr);
3020 if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
3021 {
b.liu87afc4c2024-08-14 17:33:45 +08003022 LOGE("pthread_attr_setdetachstate() fail.");
b.liude989912025-02-12 14:40:42 +08003023 goto error;
3024 }
3025
b.liu87afc4c2024-08-14 17:33:45 +08003026 if(pthread_create(&info_pid, &thread_attr, ril_read_pthread, NULL))
b.liude989912025-02-12 14:40:42 +08003027 {
b.liu87afc4c2024-08-14 17:33:45 +08003028 LOGE("pthread_create() fail.");
b.liude989912025-02-12 14:40:42 +08003029 goto error;
3030 }
b.liub171c9a2024-11-12 19:23:29 +08003031
3032 ATPortType_enum *port_0 = (ATPortType_enum*)malloc(sizeof(ATPortType_enum));
3033 *port_0 = ATPORTTYPE_0;
3034 if(pthread_create(&pack_pid, &thread_attr, ril_process_thread, port_0))
b.liude989912025-02-12 14:40:42 +08003035 {
b.liu87afc4c2024-08-14 17:33:45 +08003036 LOGE("pthread_create() fail.");
b.liude989912025-02-12 14:40:42 +08003037 goto error;
b.liub171c9a2024-11-12 19:23:29 +08003038 }
3039
3040 ATPortType_enum *port_1 = (ATPortType_enum*)malloc(sizeof(ATPortType_enum));
3041 *port_1 = ATPORTTYPE_1;
3042 if(pthread_create(&pack_pid, &thread_attr, ril_process_thread, port_1))
b.liude989912025-02-12 14:40:42 +08003043 {
b.liub171c9a2024-11-12 19:23:29 +08003044 LOGE("pthread_create() fail.");
b.liude989912025-02-12 14:40:42 +08003045 goto error;
b.liub171c9a2024-11-12 19:23:29 +08003046 }
b.liufd87baf2024-11-15 15:30:38 +08003047
b.liu61eedc92024-11-13 16:07:00 +08003048 ATPortType_enum *port_2 = (ATPortType_enum*)malloc(sizeof(ATPortType_enum));
3049 *port_2 = ATPORTTYPE_2;
3050 if(pthread_create(&pack_pid, &thread_attr, ril_process_thread, port_2))
b.liude989912025-02-12 14:40:42 +08003051 {
b.liu61eedc92024-11-13 16:07:00 +08003052 LOGE("pthread_create() fail.");
b.liude989912025-02-12 14:40:42 +08003053 goto error;
b.liu61eedc92024-11-13 16:07:00 +08003054 }
b.liude989912025-02-12 14:40:42 +08003055
3056 // Set Band
3057 // AT*BAND=15,78,147,482,134742231
3058 char buff[10];
3059 memset(buff, 0, 10);
3060 property_get("persist.mbtk.band_config", buff, "");
3061 if(strlen(buff) == 0) {
3062 pthread_t band_pid;
3063 if(pthread_create(&band_pid, &thread_attr, band_config_thread, NULL))
3064 {
b.liu87afc4c2024-08-14 17:33:45 +08003065 LOGE("pthread_create() fail.");
b.liude989912025-02-12 14:40:42 +08003066 }
3067 }
3068
b.liufd87baf2024-11-15 15:30:38 +08003069 pthread_attr_destroy(&thread_attr);
3070
3071 if(asr_auto_data_call_enable()) {
3072 ril_cid_start = MBTK_RIL_CID_2;
3073
3074 char def_cid[10] = {0};
3075 char prop_data[100] = {0};
3076 int cid = -1;
3077 sprintf(def_cid, "%d", MBTK_RIL_CID_DEF); // Default route and DNS is CID 1.
3078 memset(prop_data, 0, sizeof(prop_data));
3079 if(property_get(MBTK_DEF_DNS_CID, prop_data, def_cid) > 0 && !str_empty(prop_data)) {
3080 cid = atoi(prop_data);
3081 }
3082
3083 if(cid == MBTK_RIL_CID_DEF) {
3084 if(file_link("/tmp/resolv.conf", "/etc/resolv.conf")) {
3085 LOGE("Create link file fail.");
3086 }
3087 }
3088 } else {
3089 ril_cid_start = MBTK_RIL_CID_DEF;
3090 }
b.liude989912025-02-12 14:40:42 +08003091
b.liufd87baf2024-11-15 15:30:38 +08003092 LOGD("MBTK Ril Server Start[CID start with : %d]...", ril_cid_start);
b.liude989912025-02-12 14:40:42 +08003093
3094 return 0;
b.liu87afc4c2024-08-14 17:33:45 +08003095error:
3096 if(ril_info.sock_client_list) {
3097 list_free(ril_info.sock_client_list);
3098 ril_info.sock_client_list = NULL;
3099 }
3100
3101 if(ril_info.sock_listen_fd > 0) {
3102 close(ril_info.sock_listen_fd);
3103 ril_info.sock_listen_fd = -1;
3104 }
b.liude989912025-02-12 14:40:42 +08003105 return -1;
b.liu87afc4c2024-08-14 17:33:45 +08003106}
3107
b.liu87afc4c2024-08-14 17:33:45 +08003108int main(int argc, char *argv[])
3109{
3110 mbtk_log_init("radio", "MBTK_RIL");
3111
b.liubcf86c92024-08-19 19:48:28 +08003112 MBTK_SOURCE_INFO_PRINT("mbtk_rild");
3113
b.liu87afc4c2024-08-14 17:33:45 +08003114#ifdef MBTK_DUMP_SUPPORT
3115 mbtk_debug_open(NULL, TRUE);
3116#endif
3117
3118// Using Killall,the file lock may be not release.
3119#if 0
3120 if(app_already_running(MBTK_RILD_PID_FILE)) {
3121 LOGW("daemon already running.");
3122 exit(1);
3123 }
3124#endif
3125
3126 LOGI("mbtk_rild start.");
3127
3128 if(InProduction_Mode()) {
3129 LOGI("Is Production Mode, will exit...");
3130 exit(0);
3131 }
3132
3133 ready_state_update();
3134
3135 int at_sock = openSocket("/tmp/atcmd_at");
3136 if(at_sock < 0)
3137 {
3138 LOGE("Open AT Socket Fail[%d].", errno);
3139 return -1;
3140 }
3141 int uart_sock = openSocket("/tmp/atcmd_urc");
3142 if(uart_sock < 0)
3143 {
3144 LOGE("Open Uart Socket Fail[%d].", errno);
3145 return -1;
3146 }
3147
b.liub171c9a2024-11-12 19:23:29 +08003148 int at_sock_1 = openSocket("/tmp/atcmd_at_1");
3149 if(at_sock_1 < 0)
b.liu87afc4c2024-08-14 17:33:45 +08003150 {
b.liub171c9a2024-11-12 19:23:29 +08003151 LOGE("Open AT Socket Fail[%d].", errno);
b.liu87afc4c2024-08-14 17:33:45 +08003152 return -1;
3153 }
3154
b.liu61eedc92024-11-13 16:07:00 +08003155 int at_sock_2 = openSocket("/tmp/atcmd_at_2");
3156 if(at_sock_2 < 0)
3157 {
3158 LOGE("Open AT Socket Fail[%d].", errno);
3159 return -1;
3160 }
3161
b.liub171c9a2024-11-12 19:23:29 +08003162 at_set_on_reader_closed(onATReaderClosed);
3163 at_set_on_timeout(onATTimeout);
3164
3165 if(at_open(ATPORTTYPE_0, at_sock, uart_sock, onUnsolicited))
3166 {
3167 LOGE("Start AT_0 thread fail.");
3168 return -1;
3169 }
3170
3171 if(at_open(ATPORTTYPE_1, at_sock_1, uart_sock, onUnsolicited))
3172 {
3173 LOGE("Start AT_1 thread fail.");
3174 return -1;
3175 }
3176
b.liu61eedc92024-11-13 16:07:00 +08003177 if(at_open(ATPORTTYPE_2, at_sock_2, uart_sock, onUnsolicited))
3178 {
3179 LOGE("Start AT_1 thread fail.");
3180 return -1;
3181 }
3182
b.liub171c9a2024-11-12 19:23:29 +08003183 if(at_handshake(ATPORTTYPE_0))
b.liu87afc4c2024-08-14 17:33:45 +08003184 {
3185 LOGE("AT handshake fail.");
3186 return -1;
3187 }
3188
3189 LOGD("AT OK.");
3190
3191 if(ril_server_start())
3192 {
3193 LOGE("ril_server_start() fail.");
3194 return -1;
3195 }
3196
3197 mbtk_ril_ready();
3198
3199 while(1)
3200 {
3201 sleep(24 * 60 * 60);
3202 }
3203
3204 LOGD("!!!mbtk_ril exit!!!");
3205 return 0;
3206}