b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame^] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <stdbool.h> |
| 4 | #include <dlfcn.h> |
| 5 | #include <stdint.h> |
| 6 | #include <string.h> |
| 7 | #include <pthread.h> |
| 8 | |
| 9 | // mbtk includes |
| 10 | |
| 11 | typedef unsigned int uint32; |
| 12 | typedef unsigned char uint8; |
| 13 | typedef unsigned short uint16; |
| 14 | typedef void (*mbtk_info_callback_func)(const void* data, int data_len); |
| 15 | |
| 16 | typedef struct |
| 17 | { |
| 18 | int client_fd; |
| 19 | pthread_t read_thread_id; |
| 20 | int exit_fd[2]; |
| 21 | bool is_waitting; |
| 22 | pthread_cond_t cond; |
| 23 | pthread_mutex_t mutex; |
| 24 | |
| 25 | pthread_mutex_t send_mutex; |
| 26 | |
| 27 | // Temp response data. |
| 28 | uint16 info_err; |
| 29 | uint16 data_len; |
| 30 | void *data; |
| 31 | |
| 32 | //mbtk wyq for server_ready_status add start |
| 33 | char server_ready_status; |
| 34 | //mbtk wyq for server_ready_status add end |
| 35 | |
| 36 | mbtk_info_callback_func net_state_cb; |
| 37 | mbtk_info_callback_func call_state_cb; |
| 38 | mbtk_info_callback_func sms_state_cb; |
| 39 | mbtk_info_callback_func radio_state_cb; |
| 40 | mbtk_info_callback_func sim_state_cb; |
| 41 | mbtk_info_callback_func pdp_state_cb; |
| 42 | //add signal by xr |
| 43 | mbtk_info_callback_func signal_state_cb; |
| 44 | } mbtk_info_handle_t; |
| 45 | |
| 46 | typedef struct |
| 47 | { |
| 48 | uint8 call_wait; |
| 49 | uint8 dir1; |
| 50 | uint8 dir; |
| 51 | uint8 state; |
| 52 | uint8 mode; |
| 53 | uint8 mpty; |
| 54 | char phone_number[100]; |
| 55 | uint8 type; |
| 56 | uint8 pas; |
| 57 | uint8 disconnected_id; |
| 58 | } __attribute__((packed)) mbtk_call_info_t; |
| 59 | |
| 60 | typedef enum { |
| 61 | MBTK_CLCC = 1, |
| 62 | MBTK_CPAS, |
| 63 | MBTK_DISCONNECTED, |
| 64 | } mbtk_call_enum; |
| 65 | |
| 66 | typedef enum { |
| 67 | MBTK_CALL_RADY, //MT allows commands from TA/TE |
| 68 | MBTK_CALL_UNAVAILABLE, //MT does not allow commands from TA/TE |
| 69 | MBTK_CALL_UNKNOWN, //MT is not guaranteed to respond to instructions |
| 70 | MBTK_CALL_RINGING, //MT is ready for commands from TA/TE, but the ringer is active |
| 71 | MBTK_CALL_PROGRESS, //MT is ready for commands from TA/TE, but a call is in progress |
| 72 | MBTK_CALL_ASLEEP, //MT is unable to process commands from TA/TE because it is in a low functionality state |
| 73 | MBTK_CALL_ACTIVE, |
| 74 | } mbtk_call_pas_enum; |
| 75 | |
| 76 | typedef uint32_t voice_client_handle_type; |
| 77 | |
| 78 | |
| 79 | // GSW includes |
| 80 | #define GSW_HAL_SUCCESS 0 |
| 81 | #define GSW_HAL_FAIL -1 |
| 82 | #define GSW_HAL_MEM_INVAILD -2 |
| 83 | |
| 84 | |
| 85 | typedef int CallHandle; |
| 86 | typedef enum { |
| 87 | GSW_VOICE_CALL_HOLDING = 0, |
| 88 | GSW_VOICE_CALL_DIALING, |
| 89 | GSW_VOICE_CALL_ALERTING, |
| 90 | GSW_VOICE_CALL_CONNECTED, |
| 91 | GSW_VOICE_CALL_INCOMING, |
| 92 | GSW_VOICE_CALL_WAITING, |
| 93 | GSW_VOICE_CALL_END, |
| 94 | }VoiceCallState; |
| 95 | |
| 96 | typedef enum{ |
| 97 | GSW_AUDIO_MODE_CODEC = 0, |
| 98 | GSW_AUDIO_MODE_RTP = 1, |
| 99 | }AudioMode; |
| 100 | |
| 101 | typedef enum { |
| 102 | GSW_RTP_CLIENT = 0, |
| 103 | GSW_RTP_SERVER, |
| 104 | }RTPMode; |
| 105 | |
| 106 | typedef void (*CallStateInd)(CallHandle, VoiceCallState); |
| 107 | |
| 108 | |
| 109 | int32_t (*gsw_voice_sdk_init)(CallStateInd ind); |
| 110 | int32_t (*gsw_voice_set_speaker_volume)(int32_t volume); |
| 111 | int32_t (*gsw_voice_normal_voice_start)(CallHandle *handle, const char *callNumber); |
| 112 | int32_t (*gsw_voice_answer)(CallHandle handle); |
| 113 | int32_t (*gsw_voice_hangup)(CallHandle handle); |
| 114 | int32_t (*gsw_voice_set_auto_answer_mode)(int32_t mode); |
| 115 | int32_t (*gsw_voice_get_current_call_end_reason)(CallHandle handle); |
| 116 | /*#############################rtp begin*/ |
| 117 | int32_t (*gsw_voice_set_audio_mode)(AudioMode audioMode); |
| 118 | int32_t (*gsw_voice_set_remote_rtp_ip)(const char *ip, int32_t len); |
| 119 | int32_t (*gsw_voice_set_rtp_port)(RTPMode rtpMode, int32_t port); |
| 120 | int32_t (*gsw_voice_set_rtp_param)(int32_t clockRate, int32_t channel, int32_t latency); |
| 121 | /*#############################rtp end*/ |
| 122 | |
| 123 | CallHandle call_handle; |
| 124 | |
| 125 | #define lib_gsw_voice_path "/lib/libgsw_lib.so" |
| 126 | static void *dlHandle_voice = NULL; |
| 127 | |
| 128 | void gsw_test_callback(CallHandle handle, VoiceCallState state) |
| 129 | { |
| 130 | |
| 131 | printf("gsw_test_callback succeess!\n"); |
| 132 | printf("id = %d, state = %d\n", handle, state); |
| 133 | |
| 134 | call_handle = handle; |
| 135 | printf("set call_handle = %d\n", call_handle); |
| 136 | printf("call_handle = %d\n", call_handle); |
| 137 | |
| 138 | } |
| 139 | |
| 140 | static int gsw_rtp_api_import() |
| 141 | { |
| 142 | gsw_voice_set_audio_mode = (int32_t (*)(AudioMode audioMode))dlsym(dlHandle_voice, "gsw_voice_set_audio_mode"); |
| 143 | if(gsw_voice_set_audio_mode == NULL) { |
| 144 | printf("dlsym gsw_voice_set_audio_mode failed: %s\n", dlerror()); |
| 145 | return -1; |
| 146 | } |
| 147 | |
| 148 | |
| 149 | gsw_voice_set_remote_rtp_ip = (int32_t (*)(const char *ip, int32_t len))dlsym(dlHandle_voice, "gsw_voice_set_remote_rtp_ip"); |
| 150 | if(gsw_voice_set_remote_rtp_ip == NULL) { |
| 151 | printf("dlsym gsw_voice_set_remote_rtp_ip failed: %s\n", dlerror()); |
| 152 | return -1; |
| 153 | } |
| 154 | |
| 155 | |
| 156 | gsw_voice_set_rtp_port = (int32_t (*)(RTPMode rtpMode, int32_t port))dlsym(dlHandle_voice, "gsw_voice_set_rtp_port"); |
| 157 | if(gsw_voice_set_rtp_port == NULL) { |
| 158 | printf("dlsym gsw_voice_set_rtp_port failed: %s\n", dlerror()); |
| 159 | return -1; |
| 160 | } |
| 161 | |
| 162 | |
| 163 | gsw_voice_set_rtp_param = (int32_t (*)(int32_t clockRate, int32_t channel, int32_t latency))dlsym(dlHandle_voice, "gsw_voice_set_rtp_param"); |
| 164 | if(gsw_voice_set_rtp_param == NULL) { |
| 165 | printf("dlsym gsw_voice_set_rtp_param failed: %s\n", dlerror()); |
| 166 | return -1; |
| 167 | } |
| 168 | |
| 169 | gsw_voice_get_current_call_end_reason = (int32_t (*)(CallHandle handle))dlsym(dlHandle_voice, "gsw_voice_get_current_call_end_reason"); |
| 170 | if(gsw_voice_get_current_call_end_reason == NULL) { |
| 171 | printf("dlsym gsw_voice_get_current_call_end_reason failed: %s\n", dlerror()); |
| 172 | return -1; |
| 173 | } |
| 174 | |
| 175 | return 0; |
| 176 | |
| 177 | } |
| 178 | |
| 179 | |
| 180 | |
| 181 | static int gsw_call_api_import() |
| 182 | { |
| 183 | dlHandle_voice = dlopen(lib_gsw_voice_path, RTLD_NOW); |
| 184 | if (!dlHandle_voice) { |
| 185 | printf("dlopen %s failed: %s\n", lib_gsw_voice_path, dlerror()); |
| 186 | return -1; |
| 187 | } |
| 188 | |
| 189 | gsw_voice_sdk_init = (int32_t (*)(CallStateInd ind))dlsym(dlHandle_voice, "gsw_voice_sdk_init"); |
| 190 | if(gsw_voice_sdk_init == NULL) { |
| 191 | printf("dlsym gsw_voice_sdk_init failed: %s\n", dlerror()); |
| 192 | return -1; |
| 193 | } |
| 194 | |
| 195 | gsw_voice_set_speaker_volume = (int32_t (*)(int32_t volume))dlsym(dlHandle_voice, "gsw_voice_set_speaker_volume"); |
| 196 | if(gsw_voice_set_speaker_volume == NULL) { |
| 197 | printf("dlsym gsw_voice_set_speaker_volume failed: %s\n", dlerror()); |
| 198 | return -1; |
| 199 | } |
| 200 | |
| 201 | gsw_voice_normal_voice_start = (int32_t (*)(CallHandle *, const char *))dlsym(dlHandle_voice, "gsw_voice_normal_voice_start"); |
| 202 | if(gsw_voice_normal_voice_start == NULL) { |
| 203 | printf("dlsym gsw_voice_normal_voice_start failed: %s\n", dlerror()); |
| 204 | return -1; |
| 205 | } |
| 206 | |
| 207 | gsw_voice_answer = (int32_t (*)(CallHandle handle))dlsym(dlHandle_voice, "gsw_voice_answer"); |
| 208 | if(gsw_voice_answer == NULL) { |
| 209 | printf("dlsym gsw_voice_answer failed: %s\n", dlerror()); |
| 210 | return -1; |
| 211 | |
| 212 | } |
| 213 | |
| 214 | gsw_voice_hangup = (int32_t (*)(CallHandle handle))dlsym(dlHandle_voice, "gsw_voice_hangup"); |
| 215 | if(gsw_voice_hangup == NULL) { |
| 216 | printf("dlsym gsw_voice_hangup failed: %s\n", dlerror()); |
| 217 | return -1; |
| 218 | } |
| 219 | |
| 220 | gsw_voice_set_auto_answer_mode = (int32_t (*)(int32_t mode))dlsym(dlHandle_voice, "gsw_voice_set_auto_answer_mode"); |
| 221 | if(gsw_voice_set_auto_answer_mode == NULL) { |
| 222 | printf("dlsym gsw_voice_set_auto_answer_mode failed: %s\n", dlerror()); |
| 223 | return -1; |
| 224 | } |
| 225 | |
| 226 | return gsw_rtp_api_import(); |
| 227 | } |
| 228 | |
| 229 | int main() |
| 230 | { |
| 231 | char operator[10]; |
| 232 | int opt; |
| 233 | int ret; |
| 234 | |
| 235 | gsw_call_api_import(); |
| 236 | |
| 237 | while(1) |
| 238 | { |
| 239 | printf("=========gsw voice main=========\n" |
| 240 | "\t-1 exit\n" |
| 241 | "\t1 voice init\n" |
| 242 | "\t2 voice dial\n" |
| 243 | "\t3 voice hangup\n" |
| 244 | "\t4 voice answer\n" |
| 245 | "\t5 set auto answer mode\n" |
| 246 | "\t6 set speaker volume\n" |
| 247 | "\t7 set audio mode (local codec or rtp) \n" |
| 248 | "\t8 set remote rtp ip\n" |
| 249 | "\t9 set rtp port\n" |
| 250 | "\t10 set rtp param\n" |
| 251 | "\t11 get current call end reason\n" |
| 252 | "operator: >> \n"); |
| 253 | |
| 254 | memset(operator, 0, sizeof(operator)); |
| 255 | printf("%s\n",fgets(operator, sizeof(operator), stdin)); |
| 256 | fflush(stdin); |
| 257 | opt = atoi(operator); |
| 258 | switch (opt) |
| 259 | { |
| 260 | case -1: |
| 261 | printf("main exit\n"); |
| 262 | return 0; |
| 263 | case 1: |
| 264 | { |
| 265 | printf(">>>>>voice init\n"); |
| 266 | ret = gsw_voice_sdk_init(gsw_test_callback); |
| 267 | if(ret != 0) |
| 268 | { |
| 269 | printf("gsw_voice_sdk_init fail\n"); |
| 270 | } |
| 271 | else |
| 272 | { |
| 273 | printf("gsw_voice_sdk_init success\n"); |
| 274 | } |
| 275 | } |
| 276 | break; |
| 277 | case 2: |
| 278 | { |
| 279 | printf(">>>>>Input gsw_voice_normal_voice_start<<<<<\n"); |
| 280 | char number[16] = {0}; |
| 281 | printf("Enter call number\n"); |
| 282 | printf("%d\n",scanf("%15s", number)); |
| 283 | printf("call number is %s\n", number); |
| 284 | |
| 285 | ret = gsw_voice_normal_voice_start(&call_handle, number); |
| 286 | if(ret < 0) |
| 287 | { |
| 288 | printf("gsw_voice_normal_voice_start fail\n"); |
| 289 | } |
| 290 | else |
| 291 | { |
| 292 | printf("gsw_voice_normal_voice_start success\n"); |
| 293 | } |
| 294 | } |
| 295 | break; |
| 296 | case 3: |
| 297 | { |
| 298 | printf(">>>>>Input gsw_voice_hangup<<<<<\n"); |
| 299 | printf("call_handle is %d\n", call_handle); |
| 300 | ret = gsw_voice_hangup(call_handle); |
| 301 | if(ret < 0) |
| 302 | { |
| 303 | printf("gsw_voice_hangup fail\n"); |
| 304 | } |
| 305 | else |
| 306 | { |
| 307 | printf("gsw_voice_hangup success\n"); |
| 308 | } |
| 309 | } |
| 310 | break; |
| 311 | case 4: |
| 312 | { |
| 313 | printf(">>>>>Input gsw_voice_answer<<<<<\n"); |
| 314 | printf("call_handle is %d\n", call_handle); |
| 315 | ret = gsw_voice_answer(call_handle); |
| 316 | if(ret < 0) |
| 317 | { |
| 318 | printf("gsw_voice_answer fail\n"); |
| 319 | } |
| 320 | else |
| 321 | { |
| 322 | printf("gsw_voice_answer success\n"); |
| 323 | } |
| 324 | } |
| 325 | break; |
| 326 | case 5: |
| 327 | { |
| 328 | printf(">>>>>Input gsw_voice_set_auto_answer_mode<<<<<\n"); |
| 329 | |
| 330 | int mode = 0; |
| 331 | printf("Enter set mode\n"); |
| 332 | printf("%d\n",scanf("%d", &mode)); |
| 333 | fflush(stdin); |
| 334 | printf("mode is %d\n", mode); |
| 335 | |
| 336 | printf("start set auto answer mode(0:disable, 1:enable)\n"); |
| 337 | ret = gsw_voice_set_auto_answer_mode(mode); |
| 338 | if(ret < 0) |
| 339 | { |
| 340 | printf("gsw_voice_set_auto_answer_mode fail\n"); |
| 341 | } |
| 342 | else |
| 343 | { |
| 344 | printf("gsw_voice_set_auto_answer_mode success\n"); |
| 345 | } |
| 346 | } |
| 347 | break; |
| 348 | case 6: |
| 349 | { |
| 350 | printf(">>>>>Input gsw_voice_set_speaker_volume<<<<<\n"); |
| 351 | |
| 352 | |
| 353 | int volume; |
| 354 | |
| 355 | printf("Enter set volume\n"); |
| 356 | printf("%d\n",scanf("%d", &volume)); |
| 357 | fflush(stdin); |
| 358 | printf("volume is %d\n", volume); |
| 359 | |
| 360 | ret = gsw_voice_set_speaker_volume(volume); |
| 361 | if(ret < 0) |
| 362 | { |
| 363 | printf("gsw_voice_set_speaker_volume fail\n"); |
| 364 | } |
| 365 | else |
| 366 | { |
| 367 | printf("gsw_voice_set_speaker_volume success\n"); |
| 368 | } |
| 369 | } |
| 370 | break; |
| 371 | /*#############################rtp begin*/ |
| 372 | case 7: |
| 373 | { |
| 374 | printf(">>>>>Input gsw_voice_set_audio_mode<<<<<\n"); |
| 375 | |
| 376 | int mode; |
| 377 | |
| 378 | printf("Enter set mode (0: local codec, 1:rtp)\n"); |
| 379 | printf("%d\n",scanf("%d", &mode)); |
| 380 | fflush(stdin); |
| 381 | printf("mode is %d\n", mode); |
| 382 | |
| 383 | ret = gsw_voice_set_audio_mode(mode); |
| 384 | if(ret != 0) |
| 385 | { |
| 386 | printf("gsw_voice_set_audio_mode fail\n"); |
| 387 | } |
| 388 | else |
| 389 | { |
| 390 | printf("gsw_voice_set_audio_mode success\n"); |
| 391 | } |
| 392 | } |
| 393 | break; |
| 394 | |
| 395 | case 8: |
| 396 | { |
| 397 | printf(">>>>>Input gsw_voice_set_remote_rtp_ip<<<<<\n"); |
| 398 | |
| 399 | char remote_ip_addr[100] = {0}; |
| 400 | |
| 401 | printf("Enter set remote rtp ip address (***.***.***.***) \n"); |
| 402 | printf("%d\n",scanf("%s", remote_ip_addr)); |
| 403 | fflush(stdin); |
| 404 | printf("remote rtp ip address is %s\n", remote_ip_addr); |
| 405 | |
| 406 | ret = gsw_voice_set_remote_rtp_ip(remote_ip_addr,strlen(remote_ip_addr)+1); |
| 407 | if(ret != 0) |
| 408 | { |
| 409 | printf("gsw_voice_set_remote_rtp_ip fail\n"); |
| 410 | } |
| 411 | else |
| 412 | { |
| 413 | printf("gsw_voice_set_remote_rtp_ip success\n"); |
| 414 | } |
| 415 | } |
| 416 | break; |
| 417 | |
| 418 | case 9: |
| 419 | { |
| 420 | printf(">>>>>Input gsw_voice_set_rtp_port<<<<<\n"); |
| 421 | |
| 422 | int mode; |
| 423 | int port; |
| 424 | printf("Enter set mode (0: server, 1:client)\n"); |
| 425 | printf("%d\n",scanf("%d", &mode)); |
| 426 | fflush(stdin); |
| 427 | printf("mode is %d\n", mode); |
| 428 | |
| 429 | printf("Enter set port\n"); |
| 430 | printf("%d\n",scanf("%d", &port)); |
| 431 | fflush(stdin); |
| 432 | printf("port is %d\n", port); |
| 433 | |
| 434 | ret = gsw_voice_set_rtp_port(mode,port); |
| 435 | if(ret != 0) |
| 436 | { |
| 437 | printf("gsw_voice_set_rtp_port fail\n"); |
| 438 | } |
| 439 | else |
| 440 | { |
| 441 | printf("gsw_voice_set_rtp_port success\n"); |
| 442 | } |
| 443 | } |
| 444 | break; |
| 445 | |
| 446 | |
| 447 | case 10: |
| 448 | { |
| 449 | printf(">>>>>Input gsw_voice_set_rtp_param<<<<<\n"); |
| 450 | |
| 451 | int clockRate; |
| 452 | int channel; |
| 453 | int latency=400; |
| 454 | |
| 455 | printf("Enter set clockRate (only 8000 and 16000 supoort) \n"); |
| 456 | printf("%d\n",scanf("%d", &clockRate)); |
| 457 | fflush(stdin); |
| 458 | printf("clockRate is %d\n", clockRate); |
| 459 | |
| 460 | printf("Enter set channel (only 1 support)\n"); |
| 461 | printf("%d\n",scanf("%d", &channel)); |
| 462 | fflush(stdin); |
| 463 | printf("channel is %d\n", channel); |
| 464 | |
| 465 | ret = gsw_voice_set_rtp_param(clockRate, channel, latency); |
| 466 | if(ret != 0) |
| 467 | { |
| 468 | printf("gsw_voice_set_rtp_param fail\n"); |
| 469 | } |
| 470 | else |
| 471 | { |
| 472 | printf("gsw_voice_set_rtp_param success\n"); |
| 473 | } |
| 474 | } |
| 475 | break; |
| 476 | /*#############################rtp end*/ |
| 477 | |
| 478 | case 11: |
| 479 | { |
| 480 | printf(">>>>gsw_voice_get_current_call_end_reason<<<\n"); |
| 481 | ret = gsw_voice_get_current_call_end_reason(call_handle); |
| 482 | printf("gsw_voice_get_current_call_end_reason = %d\n", ret); |
| 483 | break; |
| 484 | } |
| 485 | |
| 486 | default: |
| 487 | break; |
| 488 | } |
| 489 | |
| 490 | } |
| 491 | |
| 492 | return 0; |
| 493 | |
| 494 | } |