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