blob: b3f3fdd841faebe4b6fe241e3a5793526e74f99d [file] [log] [blame]
b.liu87afc4c2024-08-14 17:33:45 +08001#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <errno.h>
5#include <sys/socket.h>
6#include <string.h>
7
8#include "mbtk_ril.h"
9#include "mbtk_list.h"
10#include "mbtk_utils.h"
11
12static uint16 ril_index = 1;
13
14static int sock_read(int fd, uint8 *msg, int data_len)
15{
16 memset(msg, 0, data_len);
17 int len = 0;
18 int read_len = 0;
19 while(1)
20 {
21 len = read(fd, msg + read_len, data_len - read_len);
22 if(len > 0)
23 {
24 read_len += len;
25 }
26 else if(len == 0)
27 {
28 LOG("read() end.");
29 break;
30 }
31 else
32 {
33 if(EAGAIN == errno)
34 {
35 LOG("Read end, lenght = %d", read_len);
36 }
37 else
38 {
39 LOG("read() error[%d].", errno);
40 }
41 break;
42 }
43 }
44
45 if(read_len > 0)
46 {
47 log_hex("DATA_RECV", msg, read_len);
48 return read_len;
49 }
50 else
51 {
52 return -1;
53 }
54}
55
56static int sock_write(int fd, uint8 *msg, int data_len)
57{
58 int len = 0;
59 int write_len = 0;
60 while(write_len < data_len)
61 {
62 len = write(fd, msg + write_len, data_len - write_len);
63 if(len > 0)
64 {
65 write_len += len;
66 }
67 else if(len == 0)
68 {
69 LOG("write() end.");
70 break;
71 }
72 else
73 {
74 LOG("write() error[%d].", errno);
75 break;
76 }
77 }
78
79 if(write_len > 0)
80 {
81 // log_hex("DATA_SEND", msg, write_len);
82 return write_len;
83 }
84 else
85 {
86 return -1;
87 }
88}
89
90static int pack_num_check(const void* data, int data_len)
91{
92 int count = 0;
93 int pack_len;
94 const uint8* ptr = (const uint8*)data;
95 while(ptr < (const uint8*)data + data_len)
96 {
97 if(RIL_SOCK_PACKET_TAG != byte_2_uint32(ptr, false))
98 {
99 LOG("pack_num_check() - TAG error.");
100 break;
101 }
102 ptr += sizeof(uint32);
103
104 pack_len = byte_2_uint16(ptr, false);
105 if(pack_len < RIL_SOCK_PACK_LEN_MIN - RIL_SOCK_PACK_EXTRA_LEN)
106 {
107 LOG("pack_num_check() - Packet length error.");
108 break;
109 }
110 ptr += sizeof(uint16);
111 ptr += pack_len;
112
113 count++;
114 }
115
116 return count;
117}
118
119char* type2str(ril_msg_type_enum type)
120{
121 switch(type)
122 {
123 case RIL_MSG_TYPE_REQ:
124 return "REQ";
125 case RIL_MSG_TYPE_RSP:
126 return "RSP";
127 case RIL_MSG_TYPE_IND:
128 return "IND";
129 default:
130 {
131 return "UNKNOWN";
132 }
133 }
134}
135
136char* apn2str(mbtk_ip_type_enum type)
137{
138 switch(type)
139 {
140 case MBTK_IP_TYPE_IP:
141 return "IP";
142 case MBTK_IP_TYPE_IPV6:
143 return "IPV6";
144 case MBTK_IP_TYPE_IPV4V6:
145 return "IPV4V6";
146 case MBTK_IP_TYPE_PPP:
147 return "PPP";
148 default:
149 {
150 return "UNKNOWN";
151 }
152 }
153}
154
155/*
156IPv6 : 254.128.0.0.0.0.0.0.0.1.0.2.144.5.212.239 -> uint128
157*/
158int str_2_ipv6(const void *ip_str, void *ipv6)
159{
b.liu62240ee2024-11-07 17:52:45 +0800160 const char *ptr = (const char*)ip_str;
b.liu87afc4c2024-08-14 17:33:45 +0800161 uint8 *ipv6_ptr = (uint8*)ipv6;
162 ipv6_ptr[0] = (uint8)atoi(ptr);
163 int i = 1;
164 while(i < 16) {
b.liu62240ee2024-11-07 17:52:45 +0800165 ptr = (const char*)strstr(ptr, ".");
b.liu87afc4c2024-08-14 17:33:45 +0800166 if(ptr == NULL)
167 return -1;
168 ptr++;
169 ipv6_ptr[i] = (uint8)atoi(ptr);
170 i++;
171 }
172
173 return 0;
174}
175
176/*
177IPv6 : uint128 -> fe80::215:1dff:fe81:484c
178*/
179int ipv6_2_str(const void *ipv6, void *ipv6_str)
180{
181 const uint8 *ptr = (const uint8*)ipv6;
182 uint8 *ipv6_ptr = (uint8*)ipv6_str;
183 int i = 0;
184 int index = 0;
185 while(i < 16) {
b.liu62240ee2024-11-07 17:52:45 +0800186 index += sprintf((char*)ipv6_ptr + index, "%02x%02x", ptr[i], ptr[i + 1]);
187 index += sprintf((char*)ipv6_ptr + index, ":");
b.liu87afc4c2024-08-14 17:33:45 +0800188 i += 2;
189 }
190
191 ipv6_ptr[index - 1] = '\0'; // Delete last ':'
192
193 return 0;
194}
195
196
197
198char* id2str(int id)
199{
200 switch(id)
201 {
202 // <string> IMEI
203 case RIL_MSG_ID_DEV_IMEI:
204 return "IMEI";
205 // <string> SN
206 case RIL_MSG_ID_DEV_SN:
207 return "SN";
208 // <string> MEID
209 case RIL_MSG_ID_DEV_MEID:
210 return "MEID";
211 // <string> VERSION
212 case RIL_MSG_ID_DEV_VERSION:
213 return "VERSION";
214 case RIL_MSG_ID_DEV_MODEL:
215 return "MODEL";
216 // <uint8> 0:Close 1:Open
217 case RIL_MSG_ID_DEV_VOLTE:
218 return "VOLTE";
219 // <string> Temperature
220 case RIL_MSG_ID_DEV_TEMP: // Temperature
221 return "TEMPERATURE";
222 case RIL_MSG_ID_DEV_CELL_TIME:
223 return "CELL_TIME";
224 case RIL_MSG_ID_DEV_MODEM:
225 return "MODEM";
b.liu9c19cc62024-11-28 09:43:02 +0800226 case RIL_MSG_ID_DEV_POWERIND:
227 return "POWERIND";
b.liu87afc4c2024-08-14 17:33:45 +0800228 // Sim Information
229
230 // <uint8> 0:NOT_EXIST 1:READY ...
231 case RIL_MSG_ID_SIM_STATE:
232 return "SIM_STATE";
233 case RIL_MSG_ID_SIM_TYPE:
234 return "SIM_TYPE";
235 // <string> IMSI
236 case RIL_MSG_ID_SIM_IMSI:
237 return "IMSI";
238 // <string> ICCID
239 case RIL_MSG_ID_SIM_ICCID:
240 return "ICCID";
241 // <string> Phone Number
242 case RIL_MSG_ID_SIM_PN:
243 return "PHONE_NUMBER";
244 case RIL_MSG_ID_SIM_LOCK:
245 return "SIM_LOCK";
246 case RIL_MSG_ID_SIM_PINPUK_TIMES:
247 return "SIM_PINPUK_TIMES";
248 case RIL_MSG_ID_SIM_PLMN:
249 return "SIM_PLMN";
250 case RIL_MSG_ID_NET_AVAILABLE:
251 return "NET_AVAILABLE";
252 case RIL_MSG_ID_NET_SEL_MODE:
253 return "NET_SEL_MODE";
254 case RIL_MSG_ID_NET_BAND:
255 return "NET_BNAD";
256 // <uint16>[4] rssi,rscp,rsrp,snr
257 case RIL_MSG_ID_NET_SIGNAL:
258 return "SIGNAL";
259 case RIL_MSG_ID_NET_REG:
260 return "NET_REG";
261 // <string> cmnet/ctnet/3gnet/...
b.liu15f456b2024-10-31 20:16:06 +0800262 case RIL_MSG_ID_DATA_CALL_APN:
b.liu87afc4c2024-08-14 17:33:45 +0800263 return "APN";
264 // Lock net/cell/frequency
265 case RIL_MSG_ID_NET_CELL:
266 return "NET_CELL";
b.liu557c81d2024-11-19 16:52:45 +0800267 case RIL_MSG_ID_NET_OOS:
268 return "NET_OOS";
b.liu15f456b2024-10-31 20:16:06 +0800269 case RIL_MSG_ID_DATA_CALL_OPT:
b.liu87afc4c2024-08-14 17:33:45 +0800270 return "DATA_CALL";
271 // Call Information
272 case RIL_MSG_ID_CALL_STATE:
273 return "CALL_STATE";
b.liufd87baf2024-11-15 15:30:38 +0800274 case RIL_MSG_ID_CALL_START:
275 return "CALL_START";
276 case RIL_MSG_ID_CALL_ANSWER:
277 return "CALL_ANSWER";
278 case RIL_MSG_ID_CALL_HANGUP:
279 return "CALL_HANGUP";
280 case RIL_MSG_ID_CALL_HANGUP_A:
281 return "CALL_HANGUP_A";
282 case RIL_MSG_ID_CALL_HANGUP_B:
283 return "CALL_HANGUP_B";
284 case RIL_MSG_ID_CALL_HANGUP_C:
285 return "CALL_HANGUP_C";
286 case RIL_MSG_ID_CALL_WAITIN:
287 return "CALL_WAITIN";
288 case RIL_MSG_ID_CALL_MUTE:
289 return "CALL_MUTE";
290 case RIL_MSG_ID_CALL_DTMF:
291 return "CALL_DTMF";
b.liu87afc4c2024-08-14 17:33:45 +0800292 // SMS Information
293 case RIL_MSG_ID_SMS_STATE:
294 return "SMS_STATE";
b.liufd87baf2024-11-15 15:30:38 +0800295 case RIL_MSG_ID_SMS_CMGF:
296 return "SMS_CMGF";
297 case RIL_MSG_ID_SMS_CPMS:
298 return "SMS_CPMS";
299 case RIL_MSG_ID_SMS_CMGS:
300 return "SMS_CMGS";
301 case RIL_MSG_ID_SMS_CMSS:
302 return "SMS_CMSS";
303 case RIL_MSG_ID_SMS_CMGR:
304 return "SMS_CMGR";
305 case RIL_MSG_ID_SMS_CMGW:
306 return "SMS_CMGW";
307 case RIL_MSG_ID_SMS_CMGD:
308 return "SMS_CMGD";
309 case RIL_MSG_ID_SMS_CMGL:
310 return "SMS_CMGL";
311 case RIL_MSG_ID_SMS_CSCA:
312 return "SMS_CSCA";
313 case RIL_MSG_ID_SMS_CSMP:
314 return "SMS_CSMP";
315 case RIL_MSG_ID_SMS_CSCB:
316 return "SMS_CSCB";
317 case RIL_MSG_ID_SMS_CNMI:
318 return "SMS_CNMI";
b.liu87afc4c2024-08-14 17:33:45 +0800319 // PhoneBook Information
320 case RIL_MSG_ID_PB_STATE:
321 return "PB_STATE";
b.liufd87baf2024-11-15 15:30:38 +0800322 case RIL_MSG_ID_ECALL_MSDCFG:
323 return "ECALL_MSDCFG";
324 case RIL_MSG_ID_ECALL_MSDGEN:
325 return "ECALL_MSDGEN";
326 case RIL_MSG_ID_ECALL_MSD:
327 return "ECALL_MSD";
328 case RIL_MSG_ID_ECALL_PUSH:
329 return "ECALL_PUSH";
330 case RIL_MSG_ID_ECALL_ONLY:
331 return "ECALL_ONLY";
332 case RIL_MSG_ID_ECALL_REG:
333 return "ECALL_REG";
334 case RIL_MSG_ID_ECALL_DIAL:
335 return "ECALL_DIAL";
336 case RIL_MSG_ID_ECALL_MODE:
337 return "ECALL_MODE";
338 case RIL_MSG_ID_ECALL_CFG:
339 return "ECALL_CFG";
340 case RIL_MSG_ID_ECALL_SMS_NUM:
341 return "ECALL_SMS_NUM";
342 case RIL_MSG_ID_ECALL_MUTESPK:
343 return "ECALL_MUTESPK";
344 case RIL_MSG_ID_ECALL_DSP_GAIN:
345 return "ECALL_DSP_GAIN";
b.liu87afc4c2024-08-14 17:33:45 +0800346 // IND Information
347 // <uint8> State
b.liueea595d2024-11-05 19:52:10 +0800348 case RIL_MSG_ID_IND_SER_STATE_CHANGE:
349 return "IND_SER_STATE";
b.liu87afc4c2024-08-14 17:33:45 +0800350 // <uint8> State
b.liu15f456b2024-10-31 20:16:06 +0800351 case RIL_MSG_ID_IND_NET_REG_STATE_CHANGE:
352 return "IND_NET_REG_STATE";
b.liu87afc4c2024-08-14 17:33:45 +0800353 // <uint8> State
354 case RIL_MSG_ID_IND_CALL_STATE_CHANGE:
355 return "IND_CALL_STATE";
356 // <uint8> State
357 case RIL_MSG_ID_IND_SMS_STATE_CHANGE:
358 return "IND_SMS_STATE";
359 // <uint8> State
360 case RIL_MSG_ID_IND_RADIO_STATE_CHANGE:
361 return "IND_RADIO_STATE";
362 // <uint8> State
363 case RIL_MSG_ID_IND_SIM_STATE_CHANGE:
364 return "IND_SIM_STATE";
365 // <uint8> State
366 case RIL_MSG_ID_IND_PDP_STATE_CHANGE:
367 return "IND_PDP_STATE";
368 // <uint8> State
369 case RIL_MSG_ID_IND_SIGNAL_STATE_CHANGE:
370 return "IND_SIGNAL_STATE";
b.liufd87baf2024-11-15 15:30:38 +0800371 case RIL_MSG_ID_IND_ECALL_STATE_CHANGE:
372 return "IND_ECALL_STATE_CHANGE";
b.liu87afc4c2024-08-14 17:33:45 +0800373 default:
374 {
375 return "UNKNOWN";
376 }
377 }
378}
379
380char* err2str(mbtk_ril_err_enum err)
381{
382 switch(err)
383 {
384 case MBTK_RIL_ERR_SUCCESS:
385 return "SUCCESS";
386 case MBTK_RIL_ERR_FORMAT:
387 return "ERR_FORMAT";
388 case MBTK_RIL_ERR_REQ_UNKNOWN:
389 return "ERR_REQ_UNKNOWN";
390 case MBTK_RIL_ERR_REQ_PARAMETER:
391 return "ERR_REQ_PARAMETER";
392 case MBTK_RIL_ERR_UNSUPPORTED:
393 return "ERR_UNSUPPORTED";
394 case MBTK_RIL_ERR_MEMORY:
395 return "ERR_MEMORY";
396 case MBTK_RIL_ERR_IND_FULL:
397 return "ERR_IND_FULL";
398 case MBTK_RIL_ERR_IND_UNKNOWN:
399 return "ERR_IND_UNKNOWN";
400 default:
401 {
402 if(err >= MBTK_RIL_ERR_CME) {
403 return "CME ERROR";
404 }
405
406 return "UNKNOWN";
407 }
408 }
409}
410
411void *mbtk_memcpy(const void *src, unsigned int n)
412{
413 void *dest = malloc(n);
414 if(dest) {
415 return memcpy(dest, src, n);
416 } else {
417 return NULL;
418 }
419}
420
421/*
4220 GSM
4231 GSM_COMPACT
4242 UTRAN
4253 GSM_EGPRS
4264 UTRAN_HSDPA
4275 UTRAN_HSUPA
4286 UTRAN_HSDPA_HSUPA
4297 EUTRAN
4308 ECGSM
431*/
432mbtk_net_type_enum mbtk_net_type_get(mbtk_radio_technology_enum radio_tech)
433{
434 switch(radio_tech)
435 {
436 case MBTK_RADIO_TECH_GSM:
437 case MBTK_RADIO_TECH_GSM_COMPACT:
438 case MBTK_RADIO_TECH_GSM_EGPRS:
439 case MBTK_RADIO_TECH_UTRAN_HSPA:
440 {
441 return MBTK_NET_TYPE_GSM;
442 }
443 case MBTK_RADIO_TECH_UTRAN:
444 case MBTK_RADIO_TECH_UTRAN_HSDPA:
445 case MBTK_RADIO_TECH_UTRAN_HSUPA:
446 case MBTK_RADIO_TECH_UTRAN_HSDPA_HSUPA:
447 {
448 return MBTK_NET_TYPE_UMTS;
449 }
450 case MBTK_RADIO_TECH_E_UTRAN:
451 {
452 return MBTK_NET_TYPE_LTE;
453 }
454 default:
455 {
456 return MBTK_NET_TYPE_UNKNOWN;
457 }
458 }
459}
460
461void ril_msg_pack_free(ril_msg_pack_info_t* pack)
462{
463 if(pack) {
464 LOGV("Free msg pack - %s", id2str(pack->msg_id));
465 if(pack->data) {
466 free(pack->data);
467 }
468 free(pack);
469 }
470}
471
b.liub171c9a2024-11-12 19:23:29 +0800472ril_msg_pack_info_t* ril_msg_pack_creat(ATPortType_enum port, int msg_type, int msg_id, int msg_index, const void *data, int data_len)
b.liu87afc4c2024-08-14 17:33:45 +0800473{
474 ril_msg_pack_info_t *pack = (ril_msg_pack_info_t *)malloc(sizeof(ril_msg_pack_info_t));
475 if(!pack)
476 {
477 LOGE("malloc() error[%d]", errno);
478 return NULL;
479 }
480
481 pack->tag = RIL_SOCK_PACKET_TAG;
b.liub171c9a2024-11-12 19:23:29 +0800482 pack->at_port = (uint8)port;
b.liu87afc4c2024-08-14 17:33:45 +0800483 if(msg_index < 0) {
484 pack->msg_index = ril_index++;
485 } else {
486 pack->msg_index = msg_index;
487 }
488 pack->msg_type = (uint16)msg_type;
489 pack->msg_id = (uint16)msg_id;
490 pack->err = (uint16)0;
491 if(data && data_len > 0) {
492 pack->msg_len = data_len + RIL_SOCK_PACK_LEN_MIN - RIL_SOCK_PACK_EXTRA_LEN;
493 pack->data_len = (uint16)data_len;
494 pack->data = (uint8*)malloc(data_len);
495 if(pack->data == NULL) {
496 LOGE("malloc(%d) fail.", data_len);
497 free(pack);
498 return NULL;
499 }
500 memcpy(pack->data, data, data_len);
501 } else {
502 pack->msg_len = RIL_SOCK_PACK_LEN_MIN - RIL_SOCK_PACK_EXTRA_LEN;
503 pack->data_len = (uint16)0;
504 pack->data = NULL;
505 }
506
507 return pack;
508}
509
510int ril_pack_send(int fd, ril_msg_pack_info_t *pack)
511{
512 if(!pack)
513 {
514 LOG("Packet is NULL.");
515 return -1;
516 }
517
518 uint8 buff[RIL_SOCK_MSG_LEN_MAX] = {0};
519 if(pack->data && pack->data_len > 0) {
520 memcpy(buff, pack, RIL_SOCK_PACK_LEN_MIN);
521 memcpy(buff + RIL_SOCK_PACK_LEN_MIN, pack->data, pack->data_len);
522 return sock_write(fd, buff, RIL_SOCK_PACK_LEN_MIN + pack->data_len);
523 } else {
524 memcpy(buff, pack, RIL_SOCK_PACK_LEN_MIN);
525 return sock_write(fd, buff, RIL_SOCK_PACK_LEN_MIN);
526 }
527}
528
529ril_msg_pack_info_t** ril_pack_recv(int fd, bool is_server, mbtk_ril_err_enum *err)
530{
531 uint8 msg[RIL_SOCK_MSG_LEN_MAX + 1];
532 *err = MBTK_RIL_ERR_SUCCESS;
533 int len = sock_read(fd, msg, RIL_SOCK_MSG_LEN_MAX + 1);
534 if(len < RIL_SOCK_PACK_LEN_MIN)
535 {
536 if(len > 0)
537 {
538 *err = MBTK_RIL_ERR_FORMAT;
539 LOGE("Insufficient packet data.");
540 }
541 return NULL;
542 }
543
544 int pack_count = pack_num_check(msg, len);
545 LOGD("Packet number : %d", pack_count);
546 if(pack_count < 1)
547 {
548 *err = MBTK_RIL_ERR_FORMAT;
549 LOGE("Packet not found.");
550 return NULL;
551 }
552 uint8 *ptr = msg;
553 ril_msg_pack_info_t** packs = (ril_msg_pack_info_t**)malloc(sizeof(ril_msg_pack_info_t*) * (pack_count + 1));
554 int i = 0;
555 while(i < pack_count)
556 {
557 packs[i] = (ril_msg_pack_info_t*)malloc(sizeof(ril_msg_pack_info_t));
558 if(packs[i] == NULL){
559 *err = MBTK_RIL_ERR_MEMORY;
560 goto error;
561 }
562 memcpy(packs[i], ptr, RIL_SOCK_PACK_LEN_MIN);
563
564 // TAG
565 if(RIL_SOCK_PACKET_TAG != packs[i]->tag)
566 {
567 *err = MBTK_RIL_ERR_FORMAT;
568 LOGE("Packet TAG error.");
569 goto error;
570 }
571
572 if(is_server)
573 {
574 // For server,"info_type" must by REQ or IND(Register IND).
575 if(packs[i]->msg_type != RIL_MSG_TYPE_REQ && packs[i]->msg_type != RIL_MSG_TYPE_IND)
576 {
577 *err = MBTK_RIL_ERR_FORMAT;
578 LOGE("Packet Type error : %d", packs[i]->msg_type);
579 goto error;
580 }
581 }
582 else
583 {
584 // For client,"info_type" must by RSP or IND.
585 if(packs[i]->msg_type != RIL_MSG_TYPE_RSP && packs[i]->msg_type != RIL_MSG_TYPE_IND)
586 {
587 *err = MBTK_RIL_ERR_FORMAT;
588 LOG("Packet Type error.");
589 goto error;
590 }
591 }
592
593 ptr += RIL_SOCK_PACK_LEN_MIN;
594 if(packs[i]->data_len > 0) {
595 packs[i]->data = (uint8*)malloc(packs[i]->data_len);
596 if(packs[i]->data == NULL) {
597 *err = MBTK_RIL_ERR_MEMORY;
598 goto error;
599 }
600 memcpy(packs[i]->data, ptr, packs[i]->data_len);
601 ptr += packs[i]->data_len;
602 } else {
603 packs[i]->data = NULL;
604 }
605
606 i++;
607 }
608 packs[i] = NULL;
609
610 return packs;
611
612error:
613 LOGD("mbtk_ril_pack_recv error, will free().");
614 if(packs)
615 {
616 ril_msg_pack_info_t** pack_ptr = packs;
617 while(*pack_ptr)
618 {
619 ril_msg_pack_free(*pack_ptr);
620 pack_ptr++;
621 }
622
623 free(packs);
624 }
625 return NULL;
626}
627