b.liu | f191eb7 | 2024-12-12 10:45:23 +0800 | [diff] [blame] | 1 | /* |
| 2 | * main.c |
| 3 | * |
| 4 | * RTP server main source. |
| 5 | * |
| 6 | */ |
| 7 | /****************************************************************************** |
| 8 | |
| 9 | EDIT HISTORY FOR FILE |
| 10 | |
| 11 | WHEN WHO WHAT,WHERE,WHY |
| 12 | -------- -------- ------------------------------------------------------- |
| 13 | 2024/11/30 LiuBin Initial version |
| 14 | |
| 15 | ******************************************************************************/ |
| 16 | #include <stdio.h> |
| 17 | #include <errno.h> |
| 18 | #include <pthread.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <unistd.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <sys/socket.h> |
| 23 | #include <sys/un.h> |
| 24 | #include <netinet/in.h> |
| 25 | #include <sys/epoll.h> |
| 26 | |
| 27 | #include "mbtk_rtp_internal.h" |
| 28 | #include "mbtk_log.h" |
| 29 | #include "mbtk_utils.h" |
| 30 | |
| 31 | #define SOCK_CLIENT_MAX 5 |
| 32 | #define EPOLL_LISTEN_MAX 100 |
| 33 | |
| 34 | rtp_info_t rtp_info; |
| 35 | static rtp_config_t rtp_confs = |
| 36 | { |
| 37 | .rtp_state_pre = RTP_STATE_DISABLE, |
| 38 | .rtp_state_cur = RTP_STATE_DISABLE, |
| 39 | .volume = 7, |
| 40 | .remote_ip = "198.18.38.15", // 198.18.38.15 |
| 41 | .server_port = RTP_UDP_SER_PORT_DEFAULT, |
| 42 | .client_port = RTP_UDP_CLI_PORT_DEFAULT, |
| 43 | .vlan = {0}, |
| 44 | .sample_rate = MBTK_AUDIO_SAMPLE_RATE_8000, |
| 45 | .channel = 1 |
| 46 | }; |
| 47 | |
| 48 | int rtp_udp_server_start(rtp_config_t *conf_info); |
| 49 | int rtp_udp_server_stop(); |
| 50 | int rtp_voip_server_start(const rtp_config_t *conf_info); |
| 51 | int rtp_voip_server_stop(); |
| 52 | |
| 53 | static void rtp_main_thread_wait(const char* tag) |
| 54 | { |
| 55 | LOGD("main(%s) waitting...", tag); |
| 56 | pthread_mutex_lock(&rtp_info.mutex); |
| 57 | pthread_cond_wait(&rtp_info.cond, &rtp_info.mutex); |
| 58 | pthread_mutex_unlock(&rtp_info.mutex); |
| 59 | LOGD("main(%s) running...", tag); |
| 60 | } |
| 61 | |
| 62 | static void rtp_main_thread_cond() |
| 63 | { |
| 64 | pthread_mutex_lock(&rtp_info.mutex); |
| 65 | pthread_cond_signal(&rtp_info.cond); |
| 66 | pthread_mutex_unlock(&rtp_info.mutex); |
| 67 | } |
| 68 | |
| 69 | static void rtp_msg_process(int fd, const char *msg, int msg_len) |
| 70 | { |
| 71 | LOGD("CMD <%s> <len-%d>", msg, msg_len); |
| 72 | // gnss_init:x |
| 73 | usleep(10 * 1000); // sleep 10ms |
| 74 | /* |
| 75 | volume = 7, |
| 76 | .remote_ip = "127.0.0.1", // 198.18.38.15 |
| 77 | .server_port = RTP_UDP_SER_PORT_DEFAULT, |
| 78 | .client_port = RTP_UDP_CLI_PORT_DEFAULT, |
| 79 | .vlan = {0}, |
| 80 | .sample_rate = MBTK_AUDIO_SAMPLE_RATE_8000, |
| 81 | .channel = 1 |
| 82 | */ |
| 83 | if(memcmp(msg, "rtp_mode", 8) == 0) { // rtp_mode <0/1> |
| 84 | int rtp_mode = atoi(msg + 9); |
| 85 | int ret = 0; |
| 86 | if(rtp_mode == 0) { // Disable RTP |
| 87 | if(rtp_confs.rtp_state_cur == RTP_STATE_ENABLE) { |
| 88 | rtp_confs.rtp_state_pre = rtp_confs.rtp_state_cur; |
| 89 | rtp_confs.rtp_state_cur = RTP_STATE_DISABLE; |
| 90 | rtp_main_thread_cond(); |
| 91 | } else if(rtp_confs.rtp_state_cur == RTP_STATE_VOIP_PROCESS) { |
| 92 | rtp_confs.rtp_state_pre = rtp_confs.rtp_state_cur; |
| 93 | rtp_confs.rtp_state_cur = RTP_STATE_DISABLE; |
| 94 | // rtp_main_thread_cond(); |
| 95 | } |
| 96 | } else { |
| 97 | if(rtp_confs.rtp_state_cur == RTP_STATE_DISABLE) { |
| 98 | rtp_confs.rtp_state_pre = rtp_confs.rtp_state_cur; |
| 99 | rtp_confs.rtp_state_cur = RTP_STATE_ENABLE; |
| 100 | rtp_main_thread_cond(); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | char rsp[100] = {0}; |
| 105 | sprintf(rsp, "%crtp_mode:%d%c", MBTK_IND_START_FLAG, ret, MBTK_IND_END_FLAG); |
| 106 | mbtk_write(fd, rsp, strlen(rsp)); |
| 107 | } else if(memcmp(msg, "volume", 6) == 0) {// volume <0-7> |
| 108 | int volume = atoi(msg + 7); |
| 109 | int ret = -1; |
| 110 | if(volume >= 0 && volume <= 7) { |
| 111 | rtp_confs.volume = volume; |
| 112 | ret = 0; |
| 113 | } |
| 114 | |
| 115 | char rsp[100] = {0}; |
| 116 | sprintf(rsp, "%cvolume:%d%c", MBTK_IND_START_FLAG, ret, MBTK_IND_END_FLAG); |
| 117 | mbtk_write(fd, rsp, strlen(rsp)); |
| 118 | } else if(memcmp(msg, "remote_ip", 9) == 0) {// remote_ip <xxx:xxx:xxx:xxx> |
| 119 | int ret = 0; |
| 120 | memcpy(rtp_confs.remote_ip, msg + 10, strlen(msg + 10) + 1); |
| 121 | |
| 122 | char rsp[100] = {0}; |
| 123 | sprintf(rsp, "%cremote_ip:%d%c", MBTK_IND_START_FLAG, ret, MBTK_IND_END_FLAG); |
| 124 | mbtk_write(fd, rsp, strlen(rsp)); |
| 125 | } else if(memcmp(msg, "server_port", 11) == 0) {// client_port <port> |
| 126 | int port = atoi(msg + 12); |
| 127 | int ret = -1; |
| 128 | if(port >= 0 && port <= 7) { |
| 129 | rtp_confs.server_port = port; |
| 130 | ret = 0; |
| 131 | } |
| 132 | |
| 133 | char rsp[100] = {0}; |
| 134 | sprintf(rsp, "%cserver_port:%d%c", MBTK_IND_START_FLAG, ret, MBTK_IND_END_FLAG); |
| 135 | mbtk_write(fd, rsp, strlen(rsp)); |
| 136 | } else if(memcmp(msg, "client_port", 11) == 0) {// client_port <port> |
| 137 | int port = atoi(msg + 12); |
| 138 | int ret = -1; |
| 139 | if(port > 1024 && port < 65535) { |
| 140 | rtp_confs.client_port = port; |
| 141 | ret = 0; |
| 142 | } |
| 143 | |
| 144 | char rsp[100] = {0}; |
| 145 | sprintf(rsp, "%cclient_port:%d%c", MBTK_IND_START_FLAG, ret, MBTK_IND_END_FLAG); |
| 146 | mbtk_write(fd, rsp, strlen(rsp)); |
| 147 | } else if(memcmp(msg, "sample_rate", 11) == 0) {// client_port <port> |
| 148 | int sample_rate = atoi(msg + 12); |
| 149 | int ret = 0; |
| 150 | if(sample_rate == 8000) { |
| 151 | rtp_confs.sample_rate = MBTK_AUDIO_SAMPLE_RATE_8000; |
| 152 | } else if(sample_rate == 16000) { |
| 153 | rtp_confs.sample_rate = MBTK_AUDIO_SAMPLE_RATE_16000; |
| 154 | } else { |
| 155 | ret = -1; |
| 156 | } |
| 157 | |
| 158 | char rsp[100] = {0}; |
| 159 | sprintf(rsp, "%csample_rate:%d%c", MBTK_IND_START_FLAG, ret, MBTK_IND_END_FLAG); |
| 160 | mbtk_write(fd, rsp, strlen(rsp)); |
| 161 | } else if(memcmp(msg, "channel", 7) == 0) {// client_port <port> |
| 162 | int channel = atoi(msg + 8); |
| 163 | int ret = -1; |
| 164 | if(channel == 1) { |
| 165 | rtp_confs.channel = channel; |
| 166 | ret = 0; |
| 167 | } |
| 168 | |
| 169 | char rsp[100] = {0}; |
| 170 | sprintf(rsp, "%cchannel:%d%c", MBTK_IND_START_FLAG, ret, MBTK_IND_END_FLAG); |
| 171 | mbtk_write(fd, rsp, strlen(rsp)); |
b.liu | 91c281f | 2025-06-06 14:52:01 +0800 | [diff] [blame] | 172 | } else if(memcmp(msg, "vlan", 4) == 0) {// vlan <dev> |
| 173 | int ret = 0; |
| 174 | memcpy(rtp_confs.vlan, msg + 5, strlen(msg + 5) + 1); |
| 175 | |
| 176 | char rsp[100] = {0}; |
| 177 | sprintf(rsp, "%cvlan:%d%c", MBTK_IND_START_FLAG, ret, MBTK_IND_END_FLAG); |
| 178 | mbtk_write(fd, rsp, strlen(rsp)); |
b.liu | f191eb7 | 2024-12-12 10:45:23 +0800 | [diff] [blame] | 179 | } else { |
| 180 | LOGW("Unknown RTP msg : %s", msg); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | int epoll_fd_add(int fd) |
| 185 | { |
| 186 | if(rtp_info.epoll_fd > 0) { |
| 187 | struct epoll_event ev; |
| 188 | memset(&ev, 0, sizeof(struct epoll_event)); |
| 189 | ev.data.fd = fd; |
| 190 | ev.events = EPOLLIN | EPOLLET; |
| 191 | return epoll_ctl(rtp_info.epoll_fd, EPOLL_CTL_ADD, fd, &ev); |
| 192 | } else { |
| 193 | return -1; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | int epoll_fd_del(int fd) |
| 198 | { |
| 199 | if(rtp_info.epoll_fd > 0) { |
| 200 | struct epoll_event ev; |
| 201 | memset(&ev, 0, sizeof(struct epoll_event)); |
| 202 | ev.data.fd = fd; |
| 203 | ev.events = EPOLLIN | EPOLLERR | EPOLLET; |
| 204 | return epoll_ctl(rtp_info.epoll_fd, EPOLL_CTL_DEL, fd, &ev); |
| 205 | } else { |
| 206 | return -1; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | |
| 211 | static void* rtp_ipc_ser_pthread(void* arg) |
| 212 | { |
| 213 | int sock_listen_fd = *((int*)arg); |
| 214 | rtp_info.epoll_fd = epoll_create(SOCK_CLIENT_MAX + 1); |
| 215 | if(rtp_info.epoll_fd < 0) |
| 216 | { |
| 217 | LOGE("epoll_create() fail[%d].", errno); |
| 218 | return NULL; |
| 219 | } |
| 220 | |
| 221 | rtp_info.unix_sock_cli.fd = -1; |
| 222 | rtp_info.unix_sock_cli.read_cb = NULL; |
| 223 | |
| 224 | epoll_fd_add(sock_listen_fd); |
| 225 | |
| 226 | int nready = -1; |
| 227 | int i = 0; |
| 228 | struct epoll_event epoll_events[EPOLL_LISTEN_MAX]; |
| 229 | while(1) |
| 230 | { |
| 231 | nready = epoll_wait(rtp_info.epoll_fd, epoll_events, EPOLL_LISTEN_MAX, -1); |
| 232 | if(nready > 0) |
| 233 | { |
| 234 | for(i = 0; i < nready; i++) |
| 235 | { |
| 236 | LOGV("fd[%d] event = %x",epoll_events[i].data.fd, epoll_events[i].events); |
| 237 | if(epoll_events[i].events & EPOLLHUP) // Client Close. |
| 238 | { |
| 239 | epoll_fd_del(epoll_events[i].data.fd); |
| 240 | close(epoll_events[i].data.fd); |
| 241 | if(rtp_info.unix_sock_cli.fd == epoll_events[i].data.fd) { |
| 242 | rtp_info.unix_sock_cli.fd = -1; |
| 243 | LOGD("Local unix socket client close."); |
| 244 | } else if(rtp_info.udp_recv_sock.fd == epoll_events[i].data.fd) { |
| 245 | rtp_info.udp_recv_sock.fd = -1; |
| 246 | LOGD("RTP UDP socket client close."); |
| 247 | } else { |
| 248 | LOGE("Can not occur."); |
| 249 | } |
| 250 | } |
| 251 | else if(epoll_events[i].events & EPOLLIN) |
| 252 | { |
| 253 | if(epoll_events[i].data.fd == sock_listen_fd) // New clients connected. |
| 254 | { |
| 255 | int client_fd = -1; |
| 256 | while(1) |
| 257 | { |
| 258 | struct sockaddr_in cliaddr; |
| 259 | socklen_t clilen = sizeof(cliaddr); |
| 260 | client_fd = accept(epoll_events[i].data.fd, (struct sockaddr *) &cliaddr, &clilen); |
| 261 | if(client_fd <= 0) |
| 262 | { |
| 263 | if(errno == EAGAIN) |
| 264 | { |
| 265 | LOGE("All client connect get."); |
| 266 | } |
| 267 | else |
| 268 | { |
| 269 | LOGE("accept() error[%d].", errno); |
| 270 | } |
| 271 | break; |
| 272 | } else { |
| 273 | if(rtp_info.unix_sock_cli.fd > 0) { |
| 274 | LOGE("Client is full."); |
| 275 | break; |
| 276 | } |
| 277 | rtp_info.unix_sock_cli.fd = client_fd; |
| 278 | rtp_info.unix_sock_cli.read_cb = NULL; |
| 279 | } |
| 280 | |
| 281 | epoll_fd_add(client_fd); |
| 282 | |
| 283 | LOGD("Start monitor client cmd : %d", client_fd); |
| 284 | } |
| 285 | } |
| 286 | else if(rtp_info.unix_sock_cli.fd == epoll_events[i].data.fd) // Client data arrive. |
| 287 | { |
| 288 | char buff[1024] = {0}; |
| 289 | int len = read(epoll_events[i].data.fd, buff, sizeof(buff)); |
| 290 | if(len > 0) { |
| 291 | rtp_msg_process(epoll_events[i].data.fd, buff, len); |
| 292 | } |
| 293 | } |
| 294 | else if(rtp_info.udp_recv_sock.fd == epoll_events[i].data.fd) // RTP UDP data reach. |
| 295 | { |
| 296 | if(rtp_info.udp_recv_sock.read_cb) { |
| 297 | rtp_info.udp_recv_sock.read_cb(epoll_events[i].data.fd); |
| 298 | } |
| 299 | } |
| 300 | else |
| 301 | { |
| 302 | LOGE("Unknown socket : %d", epoll_events[i].data.fd); |
| 303 | } |
| 304 | } |
| 305 | else |
| 306 | { |
| 307 | LOGE("Unknown event : %x", epoll_events[i].events); |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | else |
| 312 | { |
| 313 | LOGE("epoll_wait() fail[%d].", errno); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | return NULL; |
| 318 | } |
| 319 | |
| 320 | |
| 321 | static int rtp_ipc_server_start() |
| 322 | { |
| 323 | struct sockaddr_un server_addr; |
| 324 | int sock_listen_fd = socket(AF_LOCAL, SOCK_STREAM, 0); |
| 325 | if(sock_listen_fd < 0) |
| 326 | { |
| 327 | LOGE("socket() fail[%d].", errno); |
| 328 | return -1; |
| 329 | } |
| 330 | |
| 331 | #if 1 |
| 332 | // Set O_NONBLOCK |
| 333 | int flags = fcntl(sock_listen_fd, F_GETFL, 0); |
| 334 | if (flags < 0) |
| 335 | { |
| 336 | LOGE("Get flags error:%d", errno); |
| 337 | goto error; |
| 338 | } |
| 339 | flags |= O_NONBLOCK; |
| 340 | if (fcntl(sock_listen_fd, F_SETFL, flags) < 0) |
| 341 | { |
| 342 | LOGE("Set flags error:%d", errno); |
| 343 | goto error; |
| 344 | } |
| 345 | #endif |
| 346 | |
| 347 | unlink(RTP_IPC_SOCK_PATH); |
| 348 | memset(&server_addr, 0, sizeof(struct sockaddr_un)); |
| 349 | server_addr.sun_family = AF_LOCAL; |
| 350 | strcpy(server_addr.sun_path, RTP_IPC_SOCK_PATH); |
| 351 | if(bind(sock_listen_fd, (struct sockaddr *)&server_addr, sizeof(server_addr))) |
| 352 | { |
| 353 | LOGE("bind() fail[%d].", errno); |
| 354 | goto error; |
| 355 | } |
| 356 | |
| 357 | if(listen(sock_listen_fd, SOCK_CLIENT_MAX)) |
| 358 | { |
| 359 | LOGE("listen() fail[%d].", errno); |
| 360 | goto error; |
| 361 | } |
| 362 | |
| 363 | pthread_t pid; |
| 364 | pthread_attr_t thread_attr; |
| 365 | pthread_attr_init(&thread_attr); |
| 366 | if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED)) |
| 367 | { |
| 368 | LOGE("pthread_attr_setdetachstate() fail."); |
| 369 | goto error; |
| 370 | } |
| 371 | |
| 372 | if(pthread_create(&pid, &thread_attr, rtp_ipc_ser_pthread, &sock_listen_fd)) |
| 373 | { |
| 374 | LOGE("pthread_create() fail."); |
| 375 | goto error; |
| 376 | } |
| 377 | |
| 378 | LOGD("RTP IPC service is running..."); |
| 379 | return 0; |
| 380 | error: |
| 381 | close(sock_listen_fd); |
| 382 | return -1; |
| 383 | } |
| 384 | |
b.liu | 30ccef1 | 2024-12-31 10:28:23 +0800 | [diff] [blame] | 385 | #ifdef MBTK_SOURCE_VERSION_2 |
b.liu | f191eb7 | 2024-12-12 10:45:23 +0800 | [diff] [blame] | 386 | static void call_state_change_cb(const void* data, int data_len) |
| 387 | { |
| 388 | if(data) { |
| 389 | mbtk_ril_call_state_info_t *state = (mbtk_ril_call_state_info_t*)data; |
| 390 | LOGD("call state change : call_id-%d, dir-%d, state-%d, num_type-%d,number-%s", state->call_id, |
| 391 | state->dir, state->state, state->num_type, state->call_number); |
| 392 | if(state->state == MBTK_RIL_CALL_STATE_DISCONNECT) { |
| 393 | if(rtp_confs.rtp_state_cur == RTP_STATE_VOIP_PROCESS) { |
| 394 | rtp_confs.rtp_state_pre = rtp_confs.rtp_state_cur; |
| 395 | rtp_confs.rtp_state_cur = RTP_STATE_ENABLE; |
| 396 | rtp_main_thread_cond(); |
| 397 | } else if(rtp_confs.rtp_state_cur == RTP_STATE_DISABLE) { // 通话过程中 Disable,挂断后需要单独处理 |
| 398 | rtp_confs.rtp_state_pre = RTP_STATE_VOIP_PROCESS; |
| 399 | rtp_confs.rtp_state_cur = RTP_STATE_DISABLE; |
| 400 | rtp_main_thread_cond(); |
| 401 | } |
| 402 | } else if(state->state == MBTK_RIL_CALL_STATE_ACTIVE /*state->state == MBTK_RIL_CALL_STATE_ALERTING || state->state == MBTK_RIL_CALL_STATE_INCOMING*/) { |
| 403 | if(rtp_confs.rtp_state_cur == RTP_STATE_ENABLE) { |
| 404 | rtp_confs.rtp_state_pre = rtp_confs.rtp_state_cur; |
| 405 | rtp_confs.rtp_state_cur = RTP_STATE_VOIP_PROCESS; |
| 406 | rtp_main_thread_cond(); |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | static int ril_ser_switch(bool open) |
| 413 | { |
| 414 | if(open) { |
| 415 | if(rtp_info.ril_handle) { |
| 416 | LOGW("RIL has opened."); |
| 417 | return 0; |
| 418 | } |
| 419 | rtp_info.ril_handle = mbtk_ril_open(MBTK_AT_PORT_DEF); |
| 420 | if(rtp_info.ril_handle == NULL) { |
| 421 | LOGE("mbtk_ril_open(MBTK_AT_PORT_DEF) fail."); |
| 422 | return -1; |
| 423 | } |
| 424 | |
| 425 | mbtk_call_state_change_cb_reg(call_state_change_cb); |
| 426 | } else { |
| 427 | if(!rtp_info.ril_handle) { |
| 428 | LOGW("RIL not open."); |
| 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | if(MBTK_RIL_ERR_SUCCESS != mbtk_ril_close(MBTK_AT_PORT_DEF)) { |
| 433 | LOGE("mbtk_ril_close(MBTK_AT_PORT_DEF) fail."); |
| 434 | return -1; |
| 435 | } |
| 436 | |
| 437 | rtp_info.ril_handle = NULL; |
| 438 | } |
| 439 | return 0; |
| 440 | } |
| 441 | |
b.liu | 2631132 | 2024-12-23 18:53:55 +0800 | [diff] [blame] | 442 | #else |
| 443 | |
| 444 | static void call_state_change_cb(const void* data, int data_len) |
| 445 | { |
| 446 | if(data) { |
| 447 | mbtk_call_info_t *state = (mbtk_call_info_t*)data; |
| 448 | LOGD("call state change : call_wait-%d, state-%d", state->call_wait, state->state); |
| 449 | if(state->call_wait == MBTK_DISCONNECTED) { |
| 450 | if(rtp_confs.rtp_state_cur == RTP_STATE_VOIP_PROCESS) { |
| 451 | rtp_confs.rtp_state_pre = rtp_confs.rtp_state_cur; |
| 452 | rtp_confs.rtp_state_cur = RTP_STATE_ENABLE; |
| 453 | rtp_main_thread_cond(); |
| 454 | } else if(rtp_confs.rtp_state_cur == RTP_STATE_DISABLE) { // 通话过程中 Disable,挂断后需要单独处理 |
| 455 | rtp_confs.rtp_state_pre = RTP_STATE_VOIP_PROCESS; |
| 456 | rtp_confs.rtp_state_cur = RTP_STATE_DISABLE; |
| 457 | rtp_main_thread_cond(); |
| 458 | } |
| 459 | } else if(state->call_wait == MBTK_CLCC && state->state == 0) { |
| 460 | if(rtp_confs.rtp_state_cur == RTP_STATE_ENABLE) { |
| 461 | rtp_confs.rtp_state_pre = rtp_confs.rtp_state_cur; |
| 462 | rtp_confs.rtp_state_cur = RTP_STATE_VOIP_PROCESS; |
| 463 | rtp_main_thread_cond(); |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | #if 0 |
| 468 | mbtk_call_info_t *reg = (mbtk_call_info_t *)data; |
| 469 | switch (reg->call_wait) |
| 470 | { |
| 471 | case MBTK_CLCC: |
| 472 | printf("\r\nRING : %d, %d, %d, %d, %d, %s, %d\r\n", reg->dir1, reg->dir, reg->state, reg->mode, reg->mpty, reg->phone_number, reg->type); |
| 473 | break; |
| 474 | case MBTK_DISCONNECTED: |
| 475 | printf("\r\nRING : call dis connected!\r\n"); |
| 476 | break; |
| 477 | case MBTK_CPAS: |
| 478 | printf("\r\nCALL : Call state = %d\r\n", reg->pas); |
| 479 | /* |
| 480 | MBTK_CALL_RADY, //MT allows commands from TA/TE |
| 481 | MBTK_CALL_UNAVAILABLE, //MT does not allow commands from TA/TE |
| 482 | MBTK_CALL_UNKNOWN, //MT is not guaranteed to respond to instructions |
| 483 | MBTK_CALL_RINGING, //MT is ready for commands from TA/TE, but the ringer is active |
| 484 | MBTK_CALL_PROGRESS, //MT is ready for commands from TA/TE, but a call is in progress |
| 485 | MBTK_CALL_ASLEEP, //MT is unable to process commands from TA/TE because it is in a low functionality state |
| 486 | MBTK_CALL_ACTIVE, |
| 487 | */ |
| 488 | switch (reg->pas) |
| 489 | { |
| 490 | case MBTK_CALL_RADY: |
| 491 | printf("CALL: call READY\r\n"); |
| 492 | break; |
| 493 | case MBTK_CALL_UNAVAILABLE: |
| 494 | printf("CALL: call unavaliable\r\n"); |
| 495 | break; |
| 496 | case MBTK_CALL_UNKNOWN: |
| 497 | printf("CALL: call unknown\r\n"); |
| 498 | break; |
| 499 | case MBTK_CALL_RINGING: |
| 500 | printf("CALL: call ringing\r\n"); |
| 501 | break; |
| 502 | case MBTK_CALL_PROGRESS: |
| 503 | printf("CALL: call progress\r\n"); |
| 504 | break; |
| 505 | case MBTK_CALL_ASLEEP: |
| 506 | printf("CALL: call asleep\r\n"); |
| 507 | break; |
| 508 | case MBTK_CALL_ACTIVE: |
| 509 | printf("CALL: call active\r\n"); |
| 510 | break; |
| 511 | default: |
| 512 | printf("\r\n"); |
| 513 | break; |
| 514 | } |
| 515 | break; |
| 516 | default: |
| 517 | printf("\r\nRING : None call_wait = %d\r\n", reg->call_wait); |
| 518 | break; |
| 519 | } |
| 520 | #endif |
| 521 | } |
| 522 | |
| 523 | static int ril_ser_switch(bool open) |
| 524 | { |
| 525 | if(open) { |
| 526 | if(rtp_info.ril_handle) { |
| 527 | LOGW("RIL has opened."); |
| 528 | return 0; |
| 529 | } |
| 530 | rtp_info.ril_handle = mbtk_info_handle_get(); |
| 531 | if(rtp_info.ril_handle == NULL) { |
| 532 | LOGE("mbtk_info_handle_get() fail."); |
| 533 | return -1; |
| 534 | } |
| 535 | |
| 536 | mbtk_call_state_change_cb_reg(rtp_info.ril_handle, call_state_change_cb); |
| 537 | } else { |
| 538 | if(!rtp_info.ril_handle) { |
| 539 | LOGW("RIL not open."); |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | if(mbtk_info_handle_free(&rtp_info.ril_handle)) { |
| 544 | LOGE("mbtk_info_handle_free() fail."); |
| 545 | return -1; |
| 546 | } |
| 547 | |
| 548 | rtp_info.ril_handle = NULL; |
| 549 | } |
| 550 | return 0; |
| 551 | } |
| 552 | |
| 553 | #endif |
| 554 | |
b.liu | f191eb7 | 2024-12-12 10:45:23 +0800 | [diff] [blame] | 555 | static int rtp_start(rtp_state_enum state_pre, rtp_state_enum state_cur) |
| 556 | { |
| 557 | LOGD("RTP start, state : %d -> %d", state_pre, state_cur); |
| 558 | char *tag = NULL; |
| 559 | if(state_cur == RTP_STATE_DISABLE) { |
| 560 | if(state_pre == RTP_STATE_VOIP_PROCESS || state_pre == RTP_STATE_ENABLE) { |
| 561 | // Close ril server. |
| 562 | ril_ser_switch(FALSE); |
| 563 | |
| 564 | // Close RTP UDP forward server. |
| 565 | if(rtp_voip_server_stop()) { |
| 566 | LOGE("rtp_voip_server_stop() fail."); |
| 567 | } |
| 568 | |
| 569 | if(rtp_udp_server_stop()) { |
| 570 | LOGE("rtp_udp_server_stop() fail."); |
| 571 | } |
| 572 | } else { |
| 573 | LOGW("Can not occur[Except for the first time]."); |
| 574 | } |
| 575 | |
| 576 | tag = "RTP_STATE_DISABLE"; |
| 577 | } else if(state_cur == RTP_STATE_ENABLE) { |
| 578 | if(state_pre == RTP_STATE_VOIP_PROCESS) { |
| 579 | // Close RTP UDP forward server. |
| 580 | if(rtp_voip_server_stop()) { |
| 581 | LOGE("rtp_udp_server_stop() fail."); |
| 582 | } |
| 583 | } else if(state_pre == RTP_STATE_DISABLE) { |
| 584 | // Open ril server. |
| 585 | ril_ser_switch(TRUE); |
| 586 | |
| 587 | if(rtp_udp_server_start(&rtp_confs)) { |
| 588 | LOGE("rtp_udp_server_start() fail."); |
| 589 | } |
| 590 | } else { |
| 591 | LOGW("Can not occur."); |
| 592 | } |
| 593 | |
| 594 | tag = "RTP_STATE_ENABLE"; |
| 595 | } else if(state_cur == RTP_STATE_VOIP_PROCESS) { |
| 596 | if(state_pre == RTP_STATE_DISABLE) { |
| 597 | LOGW("Can not occur."); |
| 598 | } else if(state_pre == RTP_STATE_ENABLE) { |
| 599 | // Open RTP UDP forward server. |
| 600 | if(rtp_voip_server_start(&rtp_confs)) { |
| 601 | LOGE("rtp_voip_server_start() fail."); |
| 602 | } |
| 603 | } else { |
| 604 | LOGW("Can not occur."); |
| 605 | } |
| 606 | |
| 607 | tag = "RTP_STATE_VOIP_PROCESS"; |
| 608 | } else { |
| 609 | LOGE("Unknown state : %d", state_cur); |
| 610 | return -1; |
| 611 | } |
| 612 | |
| 613 | // Wait for state change. |
| 614 | rtp_main_thread_wait(tag); |
| 615 | return 0; |
| 616 | } |
| 617 | |
| 618 | int main(int argc, char *argv[]) |
| 619 | { |
| 620 | mbtk_log_init("radio", "MBTK_RTP"); |
| 621 | |
| 622 | LOGD("mbtk_rtpd start."); |
| 623 | |
| 624 | memset(&rtp_info, 0, sizeof(rtp_info_t)); |
| 625 | pthread_mutex_init(&rtp_info.mutex, NULL); |
| 626 | pthread_cond_init(&rtp_info.cond, NULL); |
| 627 | |
| 628 | // Start server to monitor client messages. |
| 629 | if(rtp_ipc_server_start()) { |
| 630 | LOGE("rtp_ipc_server_start() fail."); |
| 631 | return -1; |
| 632 | } |
| 633 | |
| 634 | while(!rtp_start(rtp_confs.rtp_state_pre, rtp_confs.rtp_state_cur)) |
| 635 | { |
| 636 | LOGD("RTP will restart with state %d -> %d", rtp_confs.rtp_state_pre, rtp_confs.rtp_state_cur); |
| 637 | } |
| 638 | |
| 639 | LOGE("RTP exit. rtp_start() fail."); |
| 640 | |
| 641 | return 0; |
| 642 | } |
| 643 | |