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 <sys/un.h> |
| 7 | #include <netinet/in.h> |
| 8 | #include <pthread.h> |
| 9 | #include <sys/epoll.h> |
| 10 | #include <fcntl.h> |
| 11 | #include <signal.h> |
| 12 | |
| 13 | #include "mbtk_type.h" |
| 14 | #include "mbtk_ril.h" |
| 15 | #include "atchannel.h" |
| 16 | #include "at_tok.h" |
| 17 | #include "mbtk_utils.h" |
| 18 | #include "ril_info.h" |
| 19 | |
| 20 | void ril_rsp_pack_send(int fd, int ril_id, int msg_index, const void* data, int data_len); |
| 21 | |
| 22 | /* |
| 23 | ATDXXXXXXX; |
| 24 | OK |
| 25 | |
| 26 | */ |
| 27 | static int req_call_start(char *phont_number, int *cme_err) |
| 28 | { |
| 29 | ATResponse *response = NULL; |
| 30 | int tmp_int; |
| 31 | char *tmp_str = NULL; |
| 32 | char cmd[100] = {0}; |
| 33 | sprintf(cmd, "ATD%s;", phont_number); |
| 34 | int err = at_send_command(cmd, &response); |
| 35 | if (err < 0 || response->success == 0){ |
| 36 | *cme_err = at_get_cme_error(response); |
| 37 | goto exit; |
| 38 | } |
| 39 | |
| 40 | exit: |
| 41 | at_response_free(response); |
| 42 | return err; |
| 43 | } |
| 44 | |
| 45 | /* |
| 46 | |
| 47 | AT+CHLD=2 |
| 48 | OK |
| 49 | |
| 50 | */ |
| 51 | static int req_answer_call(int *cme_err) |
| 52 | { |
| 53 | ATResponse *response = NULL; |
| 54 | int err = at_send_command("AT+CHLD=2", &response); |
| 55 | if (err < 0 || response->success == 0){ |
| 56 | *cme_err = at_get_cme_error(response); |
| 57 | goto exit; |
| 58 | } |
| 59 | |
| 60 | exit: |
| 61 | at_response_free(response); |
| 62 | return err; |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | ATH |
| 67 | OK |
| 68 | |
| 69 | */ |
| 70 | static int req_hangup_call(int *cme_err) |
| 71 | { |
| 72 | ATResponse *response = NULL; |
| 73 | int err = at_send_command("ATH", &response); |
| 74 | if (err < 0 || response->success == 0){ |
| 75 | *cme_err = at_get_cme_error(response); |
| 76 | goto exit; |
| 77 | } |
| 78 | |
| 79 | exit: |
| 80 | at_response_free(response); |
| 81 | return err; |
| 82 | } |
| 83 | |
| 84 | /* |
| 85 | AT+CHLD=1x |
| 86 | OK |
| 87 | |
| 88 | */ |
| 89 | static int req_hangup_a_call(int phone_id, int *cme_err) |
| 90 | { |
| 91 | ATResponse *response = NULL; |
| 92 | char cmd[100] = {0}; |
| 93 | sprintf(cmd, "AT+CHLD=1%d", phone_id); |
| 94 | int err = at_send_command(cmd, &response); |
| 95 | if (err < 0 || response->success == 0){ |
| 96 | *cme_err = at_get_cme_error(response); |
| 97 | goto exit; |
| 98 | } |
| 99 | |
| 100 | exit: |
| 101 | at_response_free(response); |
| 102 | return err; |
| 103 | } |
| 104 | |
| 105 | /* |
| 106 | AT+CHLD=0 |
| 107 | OK |
| 108 | |
| 109 | */ |
| 110 | static int req_hangup_waiting_or_background_call(int *cme_err) |
| 111 | { |
| 112 | ATResponse *response = NULL; |
| 113 | int err = at_send_command("AT+CHLD=0", &response); |
| 114 | if (err < 0 || response->success == 0){ |
| 115 | *cme_err = at_get_cme_error(response); |
| 116 | goto exit; |
| 117 | } |
| 118 | |
| 119 | exit: |
| 120 | at_response_free(response); |
| 121 | return err; |
| 122 | } |
| 123 | |
| 124 | /* |
| 125 | AT+CHLD=1 |
| 126 | OK |
| 127 | |
| 128 | */ |
| 129 | static int req_hangup_foreground_resume_background_call(int *cme_err) |
| 130 | { |
| 131 | ATResponse *response = NULL; |
| 132 | int err = at_send_command("AT+CHLD=1", &response); |
| 133 | if (err < 0 || response->success == 0){ |
| 134 | *cme_err = at_get_cme_error(response); |
| 135 | goto exit; |
| 136 | } |
| 137 | |
| 138 | exit: |
| 139 | at_response_free(response); |
| 140 | return err; |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | AT+CLCC |
| 145 | OK |
| 146 | |
| 147 | */ |
| 148 | static int req_waitin_call(mbtk_call_info_t *reg, int *cme_err) |
| 149 | { |
| 150 | ATResponse *response = NULL; |
| 151 | int tmp_int; |
| 152 | char *tmp_str = NULL; |
| 153 | |
| 154 | int err = at_send_command_multiline("AT+CLCC", "+CLCC:", &response); |
| 155 | if (err < 0 || response->success == 0){ |
| 156 | *cme_err = at_get_cme_error(response); |
| 157 | goto exit; |
| 158 | } |
| 159 | |
| 160 | if(response->success == 1 && !response->p_intermediates) |
| 161 | { |
| 162 | reg->call_wait = 0; |
| 163 | goto exit; |
| 164 | } |
| 165 | |
| 166 | reg->call_wait = 1; |
| 167 | char *line = response->p_intermediates->line; |
| 168 | err = at_tok_start(&line); |
| 169 | if (err < 0) |
| 170 | { |
| 171 | goto exit; |
| 172 | } |
| 173 | err = at_tok_nextint(&line, &tmp_int); // dir1 |
| 174 | if (err < 0) |
| 175 | { |
| 176 | goto exit; |
| 177 | } |
| 178 | reg->dir1 = (uint8)tmp_int; |
| 179 | err = at_tok_nextint(&line, &tmp_int);// dir |
| 180 | if (err < 0) |
| 181 | { |
| 182 | goto exit; |
| 183 | } |
| 184 | reg->dir = (uint8)tmp_int; |
| 185 | err = at_tok_nextint(&line, &tmp_int);// state |
| 186 | if (err < 0) |
| 187 | { |
| 188 | goto exit; |
| 189 | } |
| 190 | reg->state = (uint8)tmp_int; |
| 191 | err = at_tok_nextint(&line, &tmp_int);// mode |
| 192 | if (err < 0) |
| 193 | { |
| 194 | goto exit; |
| 195 | } |
| 196 | reg->mode = (uint8)tmp_int; |
| 197 | err = at_tok_nextint(&line, &tmp_int);// mpty |
| 198 | if (err < 0) |
| 199 | { |
| 200 | goto exit; |
| 201 | } |
| 202 | reg->mpty = (uint8)tmp_int; |
| 203 | err = at_tok_nextstr(&line, &tmp_str); // phone_number |
| 204 | if (err < 0) |
| 205 | { |
| 206 | goto exit; |
| 207 | } |
| 208 | memcpy(reg->phone_number, tmp_str, strlen(tmp_str)); |
| 209 | err = at_tok_nextint(&line, &tmp_int);// tpye |
| 210 | if (err < 0) |
| 211 | { |
| 212 | goto exit; |
| 213 | } |
| 214 | reg->type = (uint8)tmp_int; |
| 215 | |
| 216 | exit: |
| 217 | at_response_free(response); |
| 218 | return err; |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | AT+CMUT? |
| 223 | +CMUT: 0 |
| 224 | |
| 225 | OK |
| 226 | */ |
| 227 | static int req_mute_get(int *state, int *cme_err) |
| 228 | { |
| 229 | ATResponse *response = NULL; |
| 230 | int tmp_int; |
| 231 | int err = at_send_command_singleline("AT+CMUT?", "+CMUT:", &response); |
| 232 | |
| 233 | if (err < 0 || response->success == 0 || !response->p_intermediates){ |
| 234 | *cme_err = at_get_cme_error(response); |
| 235 | goto exit; |
| 236 | } |
| 237 | |
| 238 | char *line = response->p_intermediates->line; |
| 239 | err = at_tok_start(&line); |
| 240 | if (err < 0) |
| 241 | { |
| 242 | goto exit; |
| 243 | } |
| 244 | err = at_tok_nextint(&line, &tmp_int); |
| 245 | if (err < 0) |
| 246 | { |
| 247 | goto exit; |
| 248 | } |
| 249 | *state = tmp_int; |
| 250 | |
| 251 | exit: |
| 252 | at_response_free(response); |
| 253 | return err; |
| 254 | } |
| 255 | |
| 256 | /* |
| 257 | AT+CMUT=0; |
| 258 | OK |
| 259 | |
| 260 | */ |
| 261 | static int req_mute_set(int state, int *cme_err) |
| 262 | { |
| 263 | ATResponse *response = NULL; |
| 264 | char cmd[100] = {0}; |
| 265 | sprintf(cmd, "AT+CMUT=%d", state); |
| 266 | LOG("Set the mute command is = %s.\n", cmd); |
| 267 | int err = at_send_command(cmd, &response); |
| 268 | if (err < 0 || response->success == 0){ |
| 269 | *cme_err = at_get_cme_error(response); |
| 270 | goto exit; |
| 271 | } |
| 272 | |
| 273 | exit: |
| 274 | at_response_free(response); |
| 275 | return err; |
| 276 | } |
| 277 | |
| 278 | /* |
| 279 | AT+VTS=0; //0, 1, ..., 9, A, B, C, D, *, # |
| 280 | OK |
| 281 | |
| 282 | */ |
| 283 | static int req_dtmf_set(mbtk_call_dtmf_info_t *state, int *cme_err) |
| 284 | { |
| 285 | ATResponse *response = NULL; |
| 286 | char cmd[100] = {0}; |
| 287 | sprintf(cmd, "AT+VTS=%c,%d", state->character, state->duration); |
| 288 | LOG("Set the DTMF command is = %s.\n", cmd); |
| 289 | int err = at_send_command(cmd, &response); |
| 290 | if (err < 0 || response->success == 0){ |
| 291 | *cme_err = at_get_cme_error(response); |
| 292 | goto exit; |
| 293 | } |
| 294 | |
| 295 | exit: |
| 296 | at_response_free(response); |
| 297 | return err; |
| 298 | } |
| 299 | |
| 300 | //void net_list_free(void *data); |
| 301 | // Return MBTK_INFO_ERR_SUCCESS,will call pack_error_send() to send RSP. |
| 302 | // Otherwise, do not call pack_error_send(). |
| 303 | mbtk_ril_err_enum call_pack_req_process(sock_cli_info_t* cli_info, ril_msg_pack_info_t* pack) |
| 304 | { |
| 305 | mbtk_ril_err_enum err = MBTK_RIL_ERR_SUCCESS; |
| 306 | int cme_err = MBTK_RIL_ERR_CME_NON; |
| 307 | switch(pack->msg_id) |
| 308 | { |
| 309 | case RIL_MSG_ID_CALL_START: // <string> phone number |
| 310 | { |
| 311 | if(pack->data_len == 0 || pack->data == NULL) |
| 312 | { |
| 313 | err = MBTK_RIL_ERR_UNSUPPORTED; |
| 314 | } |
| 315 | else // Set |
| 316 | { |
| 317 | char *pn = (char*)(pack->data); |
| 318 | if(req_call_start(pn, &cme_err) || cme_err != MBTK_RIL_ERR_CME_NON) |
| 319 | { |
| 320 | if(cme_err != MBTK_RIL_ERR_CME_NON) { |
| 321 | err = MBTK_RIL_ERR_CME + cme_err; |
| 322 | } else { |
| 323 | err = MBTK_RIL_ERR_UNKNOWN; |
| 324 | } |
| 325 | LOG("ATD fail."); |
| 326 | } |
| 327 | else |
| 328 | { |
| 329 | ril_rsp_pack_send(cli_info->fd, pack->msg_id, pack->msg_index, NULL, 0); |
| 330 | } |
| 331 | } |
| 332 | break; |
| 333 | } |
| 334 | case RIL_MSG_ID_CALL_ANSWER: |
| 335 | { |
| 336 | if(pack->data_len == 0 || pack->data == NULL) |
| 337 | { |
| 338 | if(req_answer_call(&cme_err) || cme_err != MBTK_RIL_ERR_CME_NON) |
| 339 | { |
| 340 | if(cme_err != MBTK_RIL_ERR_CME_NON) { |
| 341 | err = MBTK_RIL_ERR_CME + cme_err; |
| 342 | } else { |
| 343 | err = MBTK_RIL_ERR_UNKNOWN; |
| 344 | } |
| 345 | LOG("Answer call fail."); |
| 346 | } |
| 347 | else |
| 348 | { |
| 349 | ril_rsp_pack_send(cli_info->fd, pack->msg_id, pack->msg_index, NULL, 0); |
| 350 | } |
| 351 | } |
| 352 | else // Set |
| 353 | { |
| 354 | err = MBTK_RIL_ERR_UNSUPPORTED; |
| 355 | LOG("Answer call fail. NO DATA\n"); |
| 356 | } |
| 357 | break; |
| 358 | } |
| 359 | case RIL_MSG_ID_CALL_HANGUP: |
| 360 | { |
| 361 | if(pack->data_len == 0 || pack->data == NULL) |
| 362 | { |
| 363 | if(req_hangup_call(&cme_err) || cme_err != MBTK_RIL_ERR_CME_NON) |
| 364 | { |
| 365 | if(cme_err != MBTK_RIL_ERR_CME_NON) { |
| 366 | err = MBTK_RIL_ERR_CME + cme_err; |
| 367 | } else { |
| 368 | err = MBTK_RIL_ERR_UNKNOWN; |
| 369 | } |
| 370 | LOG("Hang up call fail."); |
| 371 | } |
| 372 | else |
| 373 | { |
| 374 | ril_rsp_pack_send(cli_info->fd, pack->msg_id, pack->msg_index, NULL, 0); |
| 375 | } |
| 376 | } |
| 377 | else // Set |
| 378 | { |
| 379 | err = MBTK_RIL_ERR_UNSUPPORTED; |
| 380 | LOG("Hang up call fail."); |
| 381 | } |
| 382 | break; |
| 383 | } |
| 384 | case RIL_MSG_ID_CALL_HANGUP_A: |
| 385 | { |
| 386 | if(pack->data_len == 0 || pack->data == NULL) |
| 387 | { |
| 388 | err = MBTK_RIL_ERR_UNSUPPORTED; |
| 389 | LOG("Hang up a call fail."); |
| 390 | } |
| 391 | else |
| 392 | { |
| 393 | uint8 phone_id = *(pack->data); |
| 394 | if(req_hangup_a_call(phone_id, &cme_err) || cme_err != MBTK_RIL_ERR_CME_NON) |
| 395 | { |
| 396 | if(cme_err != MBTK_RIL_ERR_CME_NON) { |
| 397 | err = MBTK_RIL_ERR_CME + cme_err; |
| 398 | } else { |
| 399 | err = MBTK_RIL_ERR_UNKNOWN; |
| 400 | } |
| 401 | LOG("Hang up a call fail."); |
| 402 | } |
| 403 | else |
| 404 | { |
| 405 | ril_rsp_pack_send(cli_info->fd, pack->msg_id, pack->msg_index, NULL, 0); |
| 406 | } |
| 407 | } |
| 408 | break; |
| 409 | } |
| 410 | case RIL_MSG_ID_CALL_HANGUP_B: |
| 411 | { |
| 412 | if(pack->data_len == 0 || pack->data == NULL) |
| 413 | { |
| 414 | if(req_hangup_waiting_or_background_call(&cme_err) || cme_err != MBTK_RIL_ERR_CME_NON) |
| 415 | { |
| 416 | if(cme_err != MBTK_RIL_ERR_CME_NON) { |
| 417 | err = MBTK_RIL_ERR_CME + cme_err; |
| 418 | } else { |
| 419 | err = MBTK_RIL_ERR_UNKNOWN; |
| 420 | } |
| 421 | LOG("Hang up waiting_or_background call fail."); |
| 422 | } |
| 423 | else |
| 424 | { |
| 425 | ril_rsp_pack_send(cli_info->fd, pack->msg_id, pack->msg_index, NULL, 0); |
| 426 | } |
| 427 | } |
| 428 | else |
| 429 | { |
| 430 | err = MBTK_RIL_ERR_UNSUPPORTED; |
| 431 | LOG("Hang up waiting_or_background call fail."); |
| 432 | } |
| 433 | break; |
| 434 | } |
| 435 | case RIL_MSG_ID_CALL_HANGUP_C: |
| 436 | { |
| 437 | if(pack->data_len == 0 || pack->data == NULL) |
| 438 | { |
| 439 | if(req_hangup_foreground_resume_background_call(&cme_err) || cme_err != MBTK_RIL_ERR_CME_NON) |
| 440 | { |
| 441 | if(cme_err != MBTK_RIL_ERR_CME_NON) { |
| 442 | err = MBTK_RIL_ERR_CME + cme_err; |
| 443 | } else { |
| 444 | err = MBTK_RIL_ERR_UNKNOWN; |
| 445 | } |
| 446 | LOG("Hang up waiting_or_background call fail."); |
| 447 | } |
| 448 | else |
| 449 | { |
| 450 | ril_rsp_pack_send(cli_info->fd, pack->msg_id, pack->msg_index, NULL, 0); |
| 451 | } |
| 452 | } |
| 453 | else |
| 454 | { |
| 455 | err = MBTK_RIL_ERR_UNSUPPORTED; |
| 456 | LOG("Hang up waiting_or_background call fail."); |
| 457 | } |
| 458 | break; |
| 459 | } |
| 460 | case RIL_MSG_ID_CALL_WAITIN: |
| 461 | { |
| 462 | if(pack->data_len == 0 || pack->data == NULL) |
| 463 | { |
| 464 | mbtk_call_info_t reg; |
| 465 | memset(®, 0, sizeof(mbtk_call_info_t)); |
| 466 | if(req_waitin_call(®, &cme_err) || cme_err != MBTK_RIL_ERR_CME_NON) |
| 467 | { |
| 468 | if(cme_err != MBTK_RIL_ERR_CME_NON) { |
| 469 | err = MBTK_RIL_ERR_CME + cme_err; |
| 470 | } else { |
| 471 | err = MBTK_RIL_ERR_UNKNOWN; |
| 472 | } |
| 473 | LOG("Wait incoing call fail."); |
| 474 | } |
| 475 | else |
| 476 | { |
| 477 | ril_rsp_pack_send(cli_info->fd, pack->msg_id, pack->msg_index, ®, sizeof(mbtk_call_info_t)); |
| 478 | } |
| 479 | } |
| 480 | else // Set |
| 481 | { |
| 482 | err = MBTK_RIL_ERR_UNSUPPORTED; |
| 483 | LOG("Wait incoing call fail."); |
| 484 | } |
| 485 | break; |
| 486 | } |
| 487 | case RIL_MSG_ID_CALL_MUTE: |
| 488 | { |
| 489 | if(pack->data_len == 0 || pack->data == NULL) // Get VoLTE state. |
| 490 | { |
| 491 | int state; |
| 492 | if(req_mute_get(&state, &cme_err) || cme_err != MBTK_RIL_ERR_CME_NON) |
| 493 | { |
| 494 | if(cme_err != MBTK_RIL_ERR_CME_NON) { |
| 495 | err = MBTK_RIL_ERR_CME + cme_err; |
| 496 | } else { |
| 497 | err = MBTK_RIL_ERR_UNKNOWN; |
| 498 | } |
| 499 | LOG("Get mute state fail."); |
| 500 | } |
| 501 | else |
| 502 | { |
| 503 | ril_rsp_pack_send(cli_info->fd, pack->msg_id, pack->msg_index, &state, sizeof(uint8)); |
| 504 | } |
| 505 | } |
| 506 | else // Set mute state. |
| 507 | { |
| 508 | uint8 on = *(pack->data); |
| 509 | if(pack->data_len != sizeof(uint8) || (on != 0 && on != 1)) |
| 510 | { |
| 511 | err = MBTK_RIL_ERR_REQ_PARAMETER; |
| 512 | LOG("Set mute parameter error."); |
| 513 | break; |
| 514 | } |
| 515 | |
| 516 | if(req_mute_set(on, &cme_err) || cme_err != MBTK_RIL_ERR_CME_NON) |
| 517 | { |
| 518 | if(cme_err != MBTK_RIL_ERR_CME_NON) { |
| 519 | err = MBTK_RIL_ERR_CME + cme_err; |
| 520 | } else { |
| 521 | err = MBTK_RIL_ERR_UNKNOWN; |
| 522 | } |
| 523 | LOG("Set mute state fail."); |
| 524 | } |
| 525 | else |
| 526 | { |
| 527 | ril_rsp_pack_send(cli_info->fd, pack->msg_id, pack->msg_index, NULL, 0); |
| 528 | } |
| 529 | } |
| 530 | break; |
| 531 | } |
| 532 | case RIL_MSG_ID_CALL_DTMF: |
| 533 | { |
| 534 | if(pack->data_len == 0 || pack->data == NULL) |
| 535 | { |
| 536 | err = MBTK_RIL_ERR_UNSUPPORTED; |
| 537 | LOG("Wait incoing call fail."); |
| 538 | } |
| 539 | else // Set |
| 540 | { |
| 541 | mbtk_call_dtmf_info_t *reg = (mbtk_call_dtmf_info_t *)pack->data; |
| 542 | if(req_dtmf_set(reg, &cme_err) || cme_err != MBTK_RIL_ERR_CME_NON) |
| 543 | { |
| 544 | if(cme_err != MBTK_RIL_ERR_CME_NON) { |
| 545 | err = MBTK_RIL_ERR_CME + cme_err; |
| 546 | } else { |
| 547 | err = MBTK_RIL_ERR_UNKNOWN; |
| 548 | } |
| 549 | LOG("Wait incoing call fail."); |
| 550 | } |
| 551 | else |
| 552 | { |
| 553 | ril_rsp_pack_send(cli_info->fd, pack->msg_id, pack->msg_index, NULL, 0); |
| 554 | } |
| 555 | } |
| 556 | break; |
| 557 | } |
| 558 | default: |
| 559 | { |
| 560 | err = MBTK_RIL_ERR_REQ_UNKNOWN; |
| 561 | LOG("Unknown request : %s", id2str(pack->msg_id)); |
| 562 | break; |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | return err; |
| 567 | } |
| 568 | |
| 569 | |
| 570 | |