b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 1 | #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 | |
| 12 | static uint16 ril_index = 1; |
| 13 | |
| 14 | static 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 | |
| 56 | static 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 | |
| 90 | static 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 | |
| 119 | char* 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 | |
| 136 | char* 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 | /* |
| 156 | IPv6 : 254.128.0.0.0.0.0.0.0.1.0.2.144.5.212.239 -> uint128 |
| 157 | */ |
| 158 | int str_2_ipv6(const void *ip_str, void *ipv6) |
| 159 | { |
| 160 | const uint8 *ptr = (const uint8*)ip_str; |
| 161 | uint8 *ipv6_ptr = (uint8*)ipv6; |
| 162 | ipv6_ptr[0] = (uint8)atoi(ptr); |
| 163 | int i = 1; |
| 164 | while(i < 16) { |
| 165 | ptr = (const uint8*)strstr(ptr, "."); |
| 166 | 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 | /* |
| 177 | IPv6 : uint128 -> fe80::215:1dff:fe81:484c |
| 178 | */ |
| 179 | int 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) { |
| 186 | index += sprintf(ipv6_ptr + index, "%02x%02x", ptr[i], ptr[i + 1]); |
| 187 | index += sprintf(ipv6_ptr + index, ":"); |
| 188 | i += 2; |
| 189 | } |
| 190 | |
| 191 | ipv6_ptr[index - 1] = '\0'; // Delete last ':' |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | |
| 197 | |
| 198 | char* 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.liu | 15f456b | 2024-10-31 20:16:06 +0800 | [diff] [blame] | 261 | case RIL_MSG_ID_DATA_CALL_APN: |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 262 | return "APN"; |
| 263 | // Lock net/cell/frequency |
| 264 | case RIL_MSG_ID_NET_CELL: |
| 265 | return "NET_CELL"; |
b.liu | 15f456b | 2024-10-31 20:16:06 +0800 | [diff] [blame] | 266 | case RIL_MSG_ID_DATA_CALL_OPT: |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 267 | return "DATA_CALL"; |
| 268 | // Call Information |
| 269 | case RIL_MSG_ID_CALL_STATE: |
| 270 | return "CALL_STATE"; |
| 271 | // SMS Information |
| 272 | case RIL_MSG_ID_SMS_STATE: |
| 273 | return "SMS_STATE"; |
| 274 | // PhoneBook Information |
| 275 | case RIL_MSG_ID_PB_STATE: |
| 276 | return "PB_STATE"; |
| 277 | // IND Information |
| 278 | // <uint8> State |
| 279 | case RIL_MSG_ID_IND_SER_READY: |
| 280 | return "IND_SER_READY"; |
| 281 | // <uint8> State |
b.liu | 15f456b | 2024-10-31 20:16:06 +0800 | [diff] [blame] | 282 | case RIL_MSG_ID_IND_NET_REG_STATE_CHANGE: |
| 283 | return "IND_NET_REG_STATE"; |
b.liu | 87afc4c | 2024-08-14 17:33:45 +0800 | [diff] [blame] | 284 | // <uint8> State |
| 285 | case RIL_MSG_ID_IND_CALL_STATE_CHANGE: |
| 286 | return "IND_CALL_STATE"; |
| 287 | // <uint8> State |
| 288 | case RIL_MSG_ID_IND_SMS_STATE_CHANGE: |
| 289 | return "IND_SMS_STATE"; |
| 290 | // <uint8> State |
| 291 | case RIL_MSG_ID_IND_RADIO_STATE_CHANGE: |
| 292 | return "IND_RADIO_STATE"; |
| 293 | // <uint8> State |
| 294 | case RIL_MSG_ID_IND_SIM_STATE_CHANGE: |
| 295 | return "IND_SIM_STATE"; |
| 296 | // <uint8> State |
| 297 | case RIL_MSG_ID_IND_PDP_STATE_CHANGE: |
| 298 | return "IND_PDP_STATE"; |
| 299 | // <uint8> State |
| 300 | case RIL_MSG_ID_IND_SIGNAL_STATE_CHANGE: |
| 301 | return "IND_SIGNAL_STATE"; |
| 302 | default: |
| 303 | { |
| 304 | return "UNKNOWN"; |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | char* err2str(mbtk_ril_err_enum err) |
| 310 | { |
| 311 | switch(err) |
| 312 | { |
| 313 | case MBTK_RIL_ERR_SUCCESS: |
| 314 | return "SUCCESS"; |
| 315 | case MBTK_RIL_ERR_FORMAT: |
| 316 | return "ERR_FORMAT"; |
| 317 | case MBTK_RIL_ERR_REQ_UNKNOWN: |
| 318 | return "ERR_REQ_UNKNOWN"; |
| 319 | case MBTK_RIL_ERR_REQ_PARAMETER: |
| 320 | return "ERR_REQ_PARAMETER"; |
| 321 | case MBTK_RIL_ERR_UNSUPPORTED: |
| 322 | return "ERR_UNSUPPORTED"; |
| 323 | case MBTK_RIL_ERR_MEMORY: |
| 324 | return "ERR_MEMORY"; |
| 325 | case MBTK_RIL_ERR_IND_FULL: |
| 326 | return "ERR_IND_FULL"; |
| 327 | case MBTK_RIL_ERR_IND_UNKNOWN: |
| 328 | return "ERR_IND_UNKNOWN"; |
| 329 | default: |
| 330 | { |
| 331 | if(err >= MBTK_RIL_ERR_CME) { |
| 332 | return "CME ERROR"; |
| 333 | } |
| 334 | |
| 335 | return "UNKNOWN"; |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | void *mbtk_memcpy(const void *src, unsigned int n) |
| 341 | { |
| 342 | void *dest = malloc(n); |
| 343 | if(dest) { |
| 344 | return memcpy(dest, src, n); |
| 345 | } else { |
| 346 | return NULL; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | /* |
| 351 | 0 GSM |
| 352 | 1 GSM_COMPACT |
| 353 | 2 UTRAN |
| 354 | 3 GSM_EGPRS |
| 355 | 4 UTRAN_HSDPA |
| 356 | 5 UTRAN_HSUPA |
| 357 | 6 UTRAN_HSDPA_HSUPA |
| 358 | 7 EUTRAN |
| 359 | 8 ECGSM |
| 360 | */ |
| 361 | mbtk_net_type_enum mbtk_net_type_get(mbtk_radio_technology_enum radio_tech) |
| 362 | { |
| 363 | switch(radio_tech) |
| 364 | { |
| 365 | case MBTK_RADIO_TECH_GSM: |
| 366 | case MBTK_RADIO_TECH_GSM_COMPACT: |
| 367 | case MBTK_RADIO_TECH_GSM_EGPRS: |
| 368 | case MBTK_RADIO_TECH_UTRAN_HSPA: |
| 369 | { |
| 370 | return MBTK_NET_TYPE_GSM; |
| 371 | } |
| 372 | case MBTK_RADIO_TECH_UTRAN: |
| 373 | case MBTK_RADIO_TECH_UTRAN_HSDPA: |
| 374 | case MBTK_RADIO_TECH_UTRAN_HSUPA: |
| 375 | case MBTK_RADIO_TECH_UTRAN_HSDPA_HSUPA: |
| 376 | { |
| 377 | return MBTK_NET_TYPE_UMTS; |
| 378 | } |
| 379 | case MBTK_RADIO_TECH_E_UTRAN: |
| 380 | { |
| 381 | return MBTK_NET_TYPE_LTE; |
| 382 | } |
| 383 | default: |
| 384 | { |
| 385 | return MBTK_NET_TYPE_UNKNOWN; |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | void ril_msg_pack_free(ril_msg_pack_info_t* pack) |
| 391 | { |
| 392 | if(pack) { |
| 393 | LOGV("Free msg pack - %s", id2str(pack->msg_id)); |
| 394 | if(pack->data) { |
| 395 | free(pack->data); |
| 396 | } |
| 397 | free(pack); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | ril_msg_pack_info_t* ril_msg_pack_creat(int msg_type, int msg_id, int msg_index, const void *data, int data_len) |
| 402 | { |
| 403 | ril_msg_pack_info_t *pack = (ril_msg_pack_info_t *)malloc(sizeof(ril_msg_pack_info_t)); |
| 404 | if(!pack) |
| 405 | { |
| 406 | LOGE("malloc() error[%d]", errno); |
| 407 | return NULL; |
| 408 | } |
| 409 | |
| 410 | pack->tag = RIL_SOCK_PACKET_TAG; |
| 411 | |
| 412 | if(msg_index < 0) { |
| 413 | pack->msg_index = ril_index++; |
| 414 | } else { |
| 415 | pack->msg_index = msg_index; |
| 416 | } |
| 417 | pack->msg_type = (uint16)msg_type; |
| 418 | pack->msg_id = (uint16)msg_id; |
| 419 | pack->err = (uint16)0; |
| 420 | if(data && data_len > 0) { |
| 421 | pack->msg_len = data_len + RIL_SOCK_PACK_LEN_MIN - RIL_SOCK_PACK_EXTRA_LEN; |
| 422 | pack->data_len = (uint16)data_len; |
| 423 | pack->data = (uint8*)malloc(data_len); |
| 424 | if(pack->data == NULL) { |
| 425 | LOGE("malloc(%d) fail.", data_len); |
| 426 | free(pack); |
| 427 | return NULL; |
| 428 | } |
| 429 | memcpy(pack->data, data, data_len); |
| 430 | } else { |
| 431 | pack->msg_len = RIL_SOCK_PACK_LEN_MIN - RIL_SOCK_PACK_EXTRA_LEN; |
| 432 | pack->data_len = (uint16)0; |
| 433 | pack->data = NULL; |
| 434 | } |
| 435 | |
| 436 | return pack; |
| 437 | } |
| 438 | |
| 439 | int ril_pack_send(int fd, ril_msg_pack_info_t *pack) |
| 440 | { |
| 441 | if(!pack) |
| 442 | { |
| 443 | LOG("Packet is NULL."); |
| 444 | return -1; |
| 445 | } |
| 446 | |
| 447 | uint8 buff[RIL_SOCK_MSG_LEN_MAX] = {0}; |
| 448 | if(pack->data && pack->data_len > 0) { |
| 449 | memcpy(buff, pack, RIL_SOCK_PACK_LEN_MIN); |
| 450 | memcpy(buff + RIL_SOCK_PACK_LEN_MIN, pack->data, pack->data_len); |
| 451 | return sock_write(fd, buff, RIL_SOCK_PACK_LEN_MIN + pack->data_len); |
| 452 | } else { |
| 453 | memcpy(buff, pack, RIL_SOCK_PACK_LEN_MIN); |
| 454 | return sock_write(fd, buff, RIL_SOCK_PACK_LEN_MIN); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | ril_msg_pack_info_t** ril_pack_recv(int fd, bool is_server, mbtk_ril_err_enum *err) |
| 459 | { |
| 460 | uint8 msg[RIL_SOCK_MSG_LEN_MAX + 1]; |
| 461 | *err = MBTK_RIL_ERR_SUCCESS; |
| 462 | int len = sock_read(fd, msg, RIL_SOCK_MSG_LEN_MAX + 1); |
| 463 | if(len < RIL_SOCK_PACK_LEN_MIN) |
| 464 | { |
| 465 | if(len > 0) |
| 466 | { |
| 467 | *err = MBTK_RIL_ERR_FORMAT; |
| 468 | LOGE("Insufficient packet data."); |
| 469 | } |
| 470 | return NULL; |
| 471 | } |
| 472 | |
| 473 | int pack_count = pack_num_check(msg, len); |
| 474 | LOGD("Packet number : %d", pack_count); |
| 475 | if(pack_count < 1) |
| 476 | { |
| 477 | *err = MBTK_RIL_ERR_FORMAT; |
| 478 | LOGE("Packet not found."); |
| 479 | return NULL; |
| 480 | } |
| 481 | uint8 *ptr = msg; |
| 482 | ril_msg_pack_info_t** packs = (ril_msg_pack_info_t**)malloc(sizeof(ril_msg_pack_info_t*) * (pack_count + 1)); |
| 483 | int i = 0; |
| 484 | while(i < pack_count) |
| 485 | { |
| 486 | packs[i] = (ril_msg_pack_info_t*)malloc(sizeof(ril_msg_pack_info_t)); |
| 487 | if(packs[i] == NULL){ |
| 488 | *err = MBTK_RIL_ERR_MEMORY; |
| 489 | goto error; |
| 490 | } |
| 491 | memcpy(packs[i], ptr, RIL_SOCK_PACK_LEN_MIN); |
| 492 | |
| 493 | // TAG |
| 494 | if(RIL_SOCK_PACKET_TAG != packs[i]->tag) |
| 495 | { |
| 496 | *err = MBTK_RIL_ERR_FORMAT; |
| 497 | LOGE("Packet TAG error."); |
| 498 | goto error; |
| 499 | } |
| 500 | |
| 501 | if(is_server) |
| 502 | { |
| 503 | // For server,"info_type" must by REQ or IND(Register IND). |
| 504 | if(packs[i]->msg_type != RIL_MSG_TYPE_REQ && packs[i]->msg_type != RIL_MSG_TYPE_IND) |
| 505 | { |
| 506 | *err = MBTK_RIL_ERR_FORMAT; |
| 507 | LOGE("Packet Type error : %d", packs[i]->msg_type); |
| 508 | goto error; |
| 509 | } |
| 510 | } |
| 511 | else |
| 512 | { |
| 513 | // For client,"info_type" must by RSP or IND. |
| 514 | if(packs[i]->msg_type != RIL_MSG_TYPE_RSP && packs[i]->msg_type != RIL_MSG_TYPE_IND) |
| 515 | { |
| 516 | *err = MBTK_RIL_ERR_FORMAT; |
| 517 | LOG("Packet Type error."); |
| 518 | goto error; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | ptr += RIL_SOCK_PACK_LEN_MIN; |
| 523 | if(packs[i]->data_len > 0) { |
| 524 | packs[i]->data = (uint8*)malloc(packs[i]->data_len); |
| 525 | if(packs[i]->data == NULL) { |
| 526 | *err = MBTK_RIL_ERR_MEMORY; |
| 527 | goto error; |
| 528 | } |
| 529 | memcpy(packs[i]->data, ptr, packs[i]->data_len); |
| 530 | ptr += packs[i]->data_len; |
| 531 | } else { |
| 532 | packs[i]->data = NULL; |
| 533 | } |
| 534 | |
| 535 | i++; |
| 536 | } |
| 537 | packs[i] = NULL; |
| 538 | |
| 539 | return packs; |
| 540 | |
| 541 | error: |
| 542 | LOGD("mbtk_ril_pack_recv error, will free()."); |
| 543 | if(packs) |
| 544 | { |
| 545 | ril_msg_pack_info_t** pack_ptr = packs; |
| 546 | while(*pack_ptr) |
| 547 | { |
| 548 | ril_msg_pack_free(*pack_ptr); |
| 549 | pack_ptr++; |
| 550 | } |
| 551 | |
| 552 | free(packs); |
| 553 | } |
| 554 | return NULL; |
| 555 | } |
| 556 | |