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