blob: b77046f9e0f5534c971ec7c0417bc2cb32886a2d [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";
226
227 // Sim Information
228
229 // <uint8> 0:NOT_EXIST 1:READY ...
230 case RIL_MSG_ID_SIM_STATE:
231 return "SIM_STATE";
232 case RIL_MSG_ID_SIM_TYPE:
233 return "SIM_TYPE";
234 // <string> IMSI
235 case RIL_MSG_ID_SIM_IMSI:
236 return "IMSI";
237 // <string> ICCID
238 case RIL_MSG_ID_SIM_ICCID:
239 return "ICCID";
240 // <string> Phone Number
241 case RIL_MSG_ID_SIM_PN:
242 return "PHONE_NUMBER";
243 case RIL_MSG_ID_SIM_LOCK:
244 return "SIM_LOCK";
245 case RIL_MSG_ID_SIM_PINPUK_TIMES:
246 return "SIM_PINPUK_TIMES";
247 case RIL_MSG_ID_SIM_PLMN:
248 return "SIM_PLMN";
249 case RIL_MSG_ID_NET_AVAILABLE:
250 return "NET_AVAILABLE";
251 case RIL_MSG_ID_NET_SEL_MODE:
252 return "NET_SEL_MODE";
253 case RIL_MSG_ID_NET_BAND:
254 return "NET_BNAD";
255 // <uint16>[4] rssi,rscp,rsrp,snr
256 case RIL_MSG_ID_NET_SIGNAL:
257 return "SIGNAL";
258 case RIL_MSG_ID_NET_REG:
259 return "NET_REG";
260 // <string> cmnet/ctnet/3gnet/...
b.liu15f456b2024-10-31 20:16:06 +0800261 case RIL_MSG_ID_DATA_CALL_APN:
b.liu87afc4c2024-08-14 17:33:45 +0800262 return "APN";
263 // Lock net/cell/frequency
264 case RIL_MSG_ID_NET_CELL:
265 return "NET_CELL";
b.liu15f456b2024-10-31 20:16:06 +0800266 case RIL_MSG_ID_DATA_CALL_OPT:
b.liu87afc4c2024-08-14 17:33:45 +0800267 return "DATA_CALL";
268 // Call Information
269 case RIL_MSG_ID_CALL_STATE:
270 return "CALL_STATE";
b.liufd87baf2024-11-15 15:30:38 +0800271 case RIL_MSG_ID_CALL_START:
272 return "CALL_START";
273 case RIL_MSG_ID_CALL_ANSWER:
274 return "CALL_ANSWER";
275 case RIL_MSG_ID_CALL_HANGUP:
276 return "CALL_HANGUP";
277 case RIL_MSG_ID_CALL_HANGUP_A:
278 return "CALL_HANGUP_A";
279 case RIL_MSG_ID_CALL_HANGUP_B:
280 return "CALL_HANGUP_B";
281 case RIL_MSG_ID_CALL_HANGUP_C:
282 return "CALL_HANGUP_C";
283 case RIL_MSG_ID_CALL_WAITIN:
284 return "CALL_WAITIN";
285 case RIL_MSG_ID_CALL_MUTE:
286 return "CALL_MUTE";
287 case RIL_MSG_ID_CALL_DTMF:
288 return "CALL_DTMF";
b.liu87afc4c2024-08-14 17:33:45 +0800289 // SMS Information
290 case RIL_MSG_ID_SMS_STATE:
291 return "SMS_STATE";
b.liufd87baf2024-11-15 15:30:38 +0800292 case RIL_MSG_ID_SMS_CMGF:
293 return "SMS_CMGF";
294 case RIL_MSG_ID_SMS_CPMS:
295 return "SMS_CPMS";
296 case RIL_MSG_ID_SMS_CMGS:
297 return "SMS_CMGS";
298 case RIL_MSG_ID_SMS_CMSS:
299 return "SMS_CMSS";
300 case RIL_MSG_ID_SMS_CMGR:
301 return "SMS_CMGR";
302 case RIL_MSG_ID_SMS_CMGW:
303 return "SMS_CMGW";
304 case RIL_MSG_ID_SMS_CMGD:
305 return "SMS_CMGD";
306 case RIL_MSG_ID_SMS_CMGL:
307 return "SMS_CMGL";
308 case RIL_MSG_ID_SMS_CSCA:
309 return "SMS_CSCA";
310 case RIL_MSG_ID_SMS_CSMP:
311 return "SMS_CSMP";
312 case RIL_MSG_ID_SMS_CSCB:
313 return "SMS_CSCB";
314 case RIL_MSG_ID_SMS_CNMI:
315 return "SMS_CNMI";
b.liu87afc4c2024-08-14 17:33:45 +0800316 // PhoneBook Information
317 case RIL_MSG_ID_PB_STATE:
318 return "PB_STATE";
b.liufd87baf2024-11-15 15:30:38 +0800319 case RIL_MSG_ID_ECALL_MSDCFG:
320 return "ECALL_MSDCFG";
321 case RIL_MSG_ID_ECALL_MSDGEN:
322 return "ECALL_MSDGEN";
323 case RIL_MSG_ID_ECALL_MSD:
324 return "ECALL_MSD";
325 case RIL_MSG_ID_ECALL_PUSH:
326 return "ECALL_PUSH";
327 case RIL_MSG_ID_ECALL_ONLY:
328 return "ECALL_ONLY";
329 case RIL_MSG_ID_ECALL_REG:
330 return "ECALL_REG";
331 case RIL_MSG_ID_ECALL_DIAL:
332 return "ECALL_DIAL";
333 case RIL_MSG_ID_ECALL_MODE:
334 return "ECALL_MODE";
335 case RIL_MSG_ID_ECALL_CFG:
336 return "ECALL_CFG";
337 case RIL_MSG_ID_ECALL_SMS_NUM:
338 return "ECALL_SMS_NUM";
339 case RIL_MSG_ID_ECALL_MUTESPK:
340 return "ECALL_MUTESPK";
341 case RIL_MSG_ID_ECALL_DSP_GAIN:
342 return "ECALL_DSP_GAIN";
b.liu87afc4c2024-08-14 17:33:45 +0800343 // IND Information
344 // <uint8> State
b.liueea595d2024-11-05 19:52:10 +0800345 case RIL_MSG_ID_IND_SER_STATE_CHANGE:
346 return "IND_SER_STATE";
b.liu87afc4c2024-08-14 17:33:45 +0800347 // <uint8> State
b.liu15f456b2024-10-31 20:16:06 +0800348 case RIL_MSG_ID_IND_NET_REG_STATE_CHANGE:
349 return "IND_NET_REG_STATE";
b.liu87afc4c2024-08-14 17:33:45 +0800350 // <uint8> State
351 case RIL_MSG_ID_IND_CALL_STATE_CHANGE:
352 return "IND_CALL_STATE";
353 // <uint8> State
354 case RIL_MSG_ID_IND_SMS_STATE_CHANGE:
355 return "IND_SMS_STATE";
356 // <uint8> State
357 case RIL_MSG_ID_IND_RADIO_STATE_CHANGE:
358 return "IND_RADIO_STATE";
359 // <uint8> State
360 case RIL_MSG_ID_IND_SIM_STATE_CHANGE:
361 return "IND_SIM_STATE";
362 // <uint8> State
363 case RIL_MSG_ID_IND_PDP_STATE_CHANGE:
364 return "IND_PDP_STATE";
365 // <uint8> State
366 case RIL_MSG_ID_IND_SIGNAL_STATE_CHANGE:
367 return "IND_SIGNAL_STATE";
b.liufd87baf2024-11-15 15:30:38 +0800368 case RIL_MSG_ID_IND_ECALL_STATE_CHANGE:
369 return "IND_ECALL_STATE_CHANGE";
b.liu87afc4c2024-08-14 17:33:45 +0800370 default:
371 {
372 return "UNKNOWN";
373 }
374 }
375}
376
377char* err2str(mbtk_ril_err_enum err)
378{
379 switch(err)
380 {
381 case MBTK_RIL_ERR_SUCCESS:
382 return "SUCCESS";
383 case MBTK_RIL_ERR_FORMAT:
384 return "ERR_FORMAT";
385 case MBTK_RIL_ERR_REQ_UNKNOWN:
386 return "ERR_REQ_UNKNOWN";
387 case MBTK_RIL_ERR_REQ_PARAMETER:
388 return "ERR_REQ_PARAMETER";
389 case MBTK_RIL_ERR_UNSUPPORTED:
390 return "ERR_UNSUPPORTED";
391 case MBTK_RIL_ERR_MEMORY:
392 return "ERR_MEMORY";
393 case MBTK_RIL_ERR_IND_FULL:
394 return "ERR_IND_FULL";
395 case MBTK_RIL_ERR_IND_UNKNOWN:
396 return "ERR_IND_UNKNOWN";
397 default:
398 {
399 if(err >= MBTK_RIL_ERR_CME) {
400 return "CME ERROR";
401 }
402
403 return "UNKNOWN";
404 }
405 }
406}
407
408void *mbtk_memcpy(const void *src, unsigned int n)
409{
410 void *dest = malloc(n);
411 if(dest) {
412 return memcpy(dest, src, n);
413 } else {
414 return NULL;
415 }
416}
417
418/*
4190 GSM
4201 GSM_COMPACT
4212 UTRAN
4223 GSM_EGPRS
4234 UTRAN_HSDPA
4245 UTRAN_HSUPA
4256 UTRAN_HSDPA_HSUPA
4267 EUTRAN
4278 ECGSM
428*/
429mbtk_net_type_enum mbtk_net_type_get(mbtk_radio_technology_enum radio_tech)
430{
431 switch(radio_tech)
432 {
433 case MBTK_RADIO_TECH_GSM:
434 case MBTK_RADIO_TECH_GSM_COMPACT:
435 case MBTK_RADIO_TECH_GSM_EGPRS:
436 case MBTK_RADIO_TECH_UTRAN_HSPA:
437 {
438 return MBTK_NET_TYPE_GSM;
439 }
440 case MBTK_RADIO_TECH_UTRAN:
441 case MBTK_RADIO_TECH_UTRAN_HSDPA:
442 case MBTK_RADIO_TECH_UTRAN_HSUPA:
443 case MBTK_RADIO_TECH_UTRAN_HSDPA_HSUPA:
444 {
445 return MBTK_NET_TYPE_UMTS;
446 }
447 case MBTK_RADIO_TECH_E_UTRAN:
448 {
449 return MBTK_NET_TYPE_LTE;
450 }
451 default:
452 {
453 return MBTK_NET_TYPE_UNKNOWN;
454 }
455 }
456}
457
458void ril_msg_pack_free(ril_msg_pack_info_t* pack)
459{
460 if(pack) {
461 LOGV("Free msg pack - %s", id2str(pack->msg_id));
462 if(pack->data) {
463 free(pack->data);
464 }
465 free(pack);
466 }
467}
468
b.liub171c9a2024-11-12 19:23:29 +0800469ril_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 +0800470{
471 ril_msg_pack_info_t *pack = (ril_msg_pack_info_t *)malloc(sizeof(ril_msg_pack_info_t));
472 if(!pack)
473 {
474 LOGE("malloc() error[%d]", errno);
475 return NULL;
476 }
477
478 pack->tag = RIL_SOCK_PACKET_TAG;
b.liub171c9a2024-11-12 19:23:29 +0800479 pack->at_port = (uint8)port;
b.liu87afc4c2024-08-14 17:33:45 +0800480 if(msg_index < 0) {
481 pack->msg_index = ril_index++;
482 } else {
483 pack->msg_index = msg_index;
484 }
485 pack->msg_type = (uint16)msg_type;
486 pack->msg_id = (uint16)msg_id;
487 pack->err = (uint16)0;
488 if(data && data_len > 0) {
489 pack->msg_len = data_len + RIL_SOCK_PACK_LEN_MIN - RIL_SOCK_PACK_EXTRA_LEN;
490 pack->data_len = (uint16)data_len;
491 pack->data = (uint8*)malloc(data_len);
492 if(pack->data == NULL) {
493 LOGE("malloc(%d) fail.", data_len);
494 free(pack);
495 return NULL;
496 }
497 memcpy(pack->data, data, data_len);
498 } else {
499 pack->msg_len = RIL_SOCK_PACK_LEN_MIN - RIL_SOCK_PACK_EXTRA_LEN;
500 pack->data_len = (uint16)0;
501 pack->data = NULL;
502 }
503
504 return pack;
505}
506
507int ril_pack_send(int fd, ril_msg_pack_info_t *pack)
508{
509 if(!pack)
510 {
511 LOG("Packet is NULL.");
512 return -1;
513 }
514
515 uint8 buff[RIL_SOCK_MSG_LEN_MAX] = {0};
516 if(pack->data && pack->data_len > 0) {
517 memcpy(buff, pack, RIL_SOCK_PACK_LEN_MIN);
518 memcpy(buff + RIL_SOCK_PACK_LEN_MIN, pack->data, pack->data_len);
519 return sock_write(fd, buff, RIL_SOCK_PACK_LEN_MIN + pack->data_len);
520 } else {
521 memcpy(buff, pack, RIL_SOCK_PACK_LEN_MIN);
522 return sock_write(fd, buff, RIL_SOCK_PACK_LEN_MIN);
523 }
524}
525
526ril_msg_pack_info_t** ril_pack_recv(int fd, bool is_server, mbtk_ril_err_enum *err)
527{
528 uint8 msg[RIL_SOCK_MSG_LEN_MAX + 1];
529 *err = MBTK_RIL_ERR_SUCCESS;
530 int len = sock_read(fd, msg, RIL_SOCK_MSG_LEN_MAX + 1);
531 if(len < RIL_SOCK_PACK_LEN_MIN)
532 {
533 if(len > 0)
534 {
535 *err = MBTK_RIL_ERR_FORMAT;
536 LOGE("Insufficient packet data.");
537 }
538 return NULL;
539 }
540
541 int pack_count = pack_num_check(msg, len);
542 LOGD("Packet number : %d", pack_count);
543 if(pack_count < 1)
544 {
545 *err = MBTK_RIL_ERR_FORMAT;
546 LOGE("Packet not found.");
547 return NULL;
548 }
549 uint8 *ptr = msg;
550 ril_msg_pack_info_t** packs = (ril_msg_pack_info_t**)malloc(sizeof(ril_msg_pack_info_t*) * (pack_count + 1));
551 int i = 0;
552 while(i < pack_count)
553 {
554 packs[i] = (ril_msg_pack_info_t*)malloc(sizeof(ril_msg_pack_info_t));
555 if(packs[i] == NULL){
556 *err = MBTK_RIL_ERR_MEMORY;
557 goto error;
558 }
559 memcpy(packs[i], ptr, RIL_SOCK_PACK_LEN_MIN);
560
561 // TAG
562 if(RIL_SOCK_PACKET_TAG != packs[i]->tag)
563 {
564 *err = MBTK_RIL_ERR_FORMAT;
565 LOGE("Packet TAG error.");
566 goto error;
567 }
568
569 if(is_server)
570 {
571 // For server,"info_type" must by REQ or IND(Register IND).
572 if(packs[i]->msg_type != RIL_MSG_TYPE_REQ && packs[i]->msg_type != RIL_MSG_TYPE_IND)
573 {
574 *err = MBTK_RIL_ERR_FORMAT;
575 LOGE("Packet Type error : %d", packs[i]->msg_type);
576 goto error;
577 }
578 }
579 else
580 {
581 // For client,"info_type" must by RSP or IND.
582 if(packs[i]->msg_type != RIL_MSG_TYPE_RSP && packs[i]->msg_type != RIL_MSG_TYPE_IND)
583 {
584 *err = MBTK_RIL_ERR_FORMAT;
585 LOG("Packet Type error.");
586 goto error;
587 }
588 }
589
590 ptr += RIL_SOCK_PACK_LEN_MIN;
591 if(packs[i]->data_len > 0) {
592 packs[i]->data = (uint8*)malloc(packs[i]->data_len);
593 if(packs[i]->data == NULL) {
594 *err = MBTK_RIL_ERR_MEMORY;
595 goto error;
596 }
597 memcpy(packs[i]->data, ptr, packs[i]->data_len);
598 ptr += packs[i]->data_len;
599 } else {
600 packs[i]->data = NULL;
601 }
602
603 i++;
604 }
605 packs[i] = NULL;
606
607 return packs;
608
609error:
610 LOGD("mbtk_ril_pack_recv error, will free().");
611 if(packs)
612 {
613 ril_msg_pack_info_t** pack_ptr = packs;
614 while(*pack_ptr)
615 {
616 ril_msg_pack_free(*pack_ptr);
617 pack_ptr++;
618 }
619
620 free(packs);
621 }
622 return NULL;
623}
624