btif_sock_thread.c revision aba88aee024266b4b8375d9b27b94469f15c2716
1/****************************************************************************** 2 * 3 * Copyright (C) 2009-2012 Broadcom Corporation 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at: 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 ******************************************************************************/ 18 19/************************************************************************************ 20 * 21 * Filename: btif_sock_thread.c 22 * 23 * Description: socket select thread 24 * 25 * 26 ***********************************************************************************/ 27 28#include <hardware/bluetooth.h> 29#include <hardware/bt_sock.h> 30 31//bta_jv_co_rfc_data 32#include <stdio.h> 33#include <stdlib.h> 34#include <errno.h> 35#include <features.h> 36#include <string.h> 37#include <sys/types.h> 38#include <sys/socket.h> 39#include <sys/un.h> 40#include <time.h> 41#include <fcntl.h> 42#include <unistd.h> 43#include <signal.h> 44#include <pthread.h> 45#include <ctype.h> 46 47#include <sys/select.h> 48#include <sys/poll.h> 49#include <cutils/sockets.h> 50#include <alloca.h> 51 52#define LOG_TAG "bt_btif_sock" 53#include "btif_common.h" 54#include "btif_util.h" 55 56 57#include "bta_api.h" 58#include "btif_sock.h" 59#include "btif_sock_thread.h" 60#include "btif_sock_util.h" 61 62#define asrt(s) if(!(s)) APPL_TRACE_ERROR("## %s assert %s failed at line:%d ##",__FUNCTION__, #s, __LINE__) 63#define print_events(events) do { \ 64 APPL_TRACE_DEBUG("print poll event:%x", events); \ 65 if (events & POLLIN) APPL_TRACE_DEBUG( " POLLIN "); \ 66 if (events & POLLPRI) APPL_TRACE_DEBUG( " POLLPRI "); \ 67 if (events & POLLOUT) APPL_TRACE_DEBUG( " POLLOUT "); \ 68 if (events & POLLERR) APPL_TRACE_DEBUG( " POLLERR "); \ 69 if (events & POLLHUP) APPL_TRACE_DEBUG( " POLLHUP "); \ 70 if (events & POLLNVAL) APPL_TRACE_DEBUG(" POLLNVAL "); \ 71 if (events & POLLRDHUP) APPL_TRACE_DEBUG(" POLLRDHUP"); \ 72 } while(0) 73 74#define MAX_THREAD 8 75#define MAX_POLL 64 76#define POLL_EXCEPTION_EVENTS (POLLHUP | POLLRDHUP | POLLERR | POLLNVAL) 77#define IS_EXCEPTION(e) ((e) & POLL_EXCEPTION_EVENTS) 78#define IS_READ(e) ((e) & POLLIN) 79#define IS_WRITE(e) ((e) & POLLOUT) 80/*cmd executes in socket poll thread */ 81#define CMD_WAKEUP 1 82#define CMD_EXIT 2 83#define CMD_ADD_FD 3 84#define CMD_REMOVE_FD 4 85#define CMD_USER_PRIVATE 5 86 87typedef struct { 88 struct pollfd pfd; 89 uint32_t user_id; 90 int type; 91 int flags; 92} poll_slot_t; 93typedef struct { 94 int cmd_fdr, cmd_fdw; 95 int poll_count; 96 poll_slot_t ps[MAX_POLL]; 97 int psi[MAX_POLL]; //index of poll slot 98 volatile pthread_t thread_id; 99 btsock_signaled_cb callback; 100 btsock_cmd_cb cmd_callback; 101 int used; 102} thread_slot_t; 103static thread_slot_t ts[MAX_THREAD]; 104 105 106 107static void *sock_poll_thread(void *arg); 108static inline void close_cmd_fd(int h); 109 110static inline void add_poll(int h, int fd, int type, int flags, uint32_t user_id); 111 112#if __GLIBC__ 113static pthread_mutex_t thread_slot_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; 114#else 115static pthread_mutex_t thread_slot_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER; 116#endif 117 118static inline void set_socket_blocking(int s, int blocking) 119{ 120 int opts; 121 opts = fcntl(s, F_GETFL); 122 if (opts<0) APPL_TRACE_ERROR("set blocking (%s)", strerror(errno)); 123 if(blocking) 124 opts &= ~O_NONBLOCK; 125 else opts |= O_NONBLOCK; 126 fcntl(s, F_SETFL, opts); 127} 128 129static inline int create_server_socket(const char* name) 130{ 131 int s = socket(AF_LOCAL, SOCK_STREAM, 0); 132 APPL_TRACE_DEBUG("covert name to android abstract name:%s", name); 133 if(socket_local_server_bind(s, name, ANDROID_SOCKET_NAMESPACE_ABSTRACT) >= 0) 134 { 135 if(listen(s, 5) == 0) 136 { 137 APPL_TRACE_DEBUG("listen to local socket:%s, fd:%d", name, s); 138 return s; 139 } 140 else APPL_TRACE_ERROR("listen to local socket:%s, fd:%d failed, errno:%d", name, s, errno); 141 } 142 else APPL_TRACE_ERROR("create local socket:%s fd:%d, failed, errno:%d", name, s, errno); 143 close(s); 144 return -1; 145} 146static inline int connect_server_socket(const char* name) 147{ 148 int s = socket(AF_LOCAL, SOCK_STREAM, 0); 149 set_socket_blocking(s, TRUE); 150 if(socket_local_client_connect(s, name, ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM) >= 0) 151 { 152 APPL_TRACE_DEBUG("connected to local socket:%s, fd:%d", name, s); 153 return s; 154 } 155 else APPL_TRACE_ERROR("connect to local socket:%s, fd:%d failed, errno:%d", name, s, errno); 156 close(s); 157 return -1; 158} 159static inline int accept_server_socket(int s) 160{ 161 struct sockaddr_un client_address; 162 socklen_t clen; 163 int fd = accept(s, (struct sockaddr*)&client_address, &clen); 164 APPL_TRACE_DEBUG("accepted fd:%d for server fd:%d", fd, s); 165 return fd; 166} 167 168static inline int create_thread(void *(*start_routine)(void *), void * arg, 169 pthread_t * thread_id) 170{ 171 pthread_attr_t thread_attr; 172 pthread_attr_init(&thread_attr); 173 pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE); 174 return pthread_create(thread_id, &thread_attr, start_routine, arg); 175} 176 177static void init_poll(int cmd_fd); 178static int alloc_thread_slot() 179{ 180 int i; 181 //revserd order to save guard uninitialized access to 0 index 182 for(i = MAX_THREAD - 1; i >=0; i--) 183 { 184 APPL_TRACE_DEBUG("ts[%d].used:%d", i, ts[i].used); 185 if(!ts[i].used) 186 { 187 ts[i].used = 1; 188 return i; 189 } 190 } 191 APPL_TRACE_ERROR("execeeded max thread count"); 192 return -1; 193} 194static void free_thread_slot(int h) 195{ 196 if(0 <= h && h < MAX_THREAD) 197 { 198 close_cmd_fd(h); 199 ts[h].used = 0; 200 } 201 else APPL_TRACE_ERROR("invalid thread handle:%d", h); 202} 203int btsock_thread_init() 204{ 205 static int initialized; 206 APPL_TRACE_DEBUG("in initialized:%d", initialized); 207 if(!initialized) 208 { 209 initialized = 1; 210 int h; 211 for(h = 0; h < MAX_THREAD; h++) 212 { 213 ts[h].cmd_fdr = ts[h].cmd_fdw = -1; 214 ts[h].used = 0; 215 ts[h].thread_id = -1; 216 ts[h].poll_count = 0; 217 ts[h].callback = NULL; 218 ts[h].cmd_callback = NULL; 219 } 220 } 221 return TRUE; 222} 223int btsock_thread_create(btsock_signaled_cb callback, btsock_cmd_cb cmd_callback) 224{ 225 int ret = FALSE; 226 asrt(callback || cmd_callback); 227 pthread_mutex_lock(&thread_slot_lock); 228 int h = alloc_thread_slot(); 229 pthread_mutex_unlock(&thread_slot_lock); 230 APPL_TRACE_DEBUG("alloc_thread_slot ret:%d", h); 231 if(h >= 0) 232 { 233 init_poll(h); 234 pthread_t thread; 235 int status = create_thread(sock_poll_thread, (void*)(uintptr_t)h, &thread); 236 if (status) 237 { 238 APPL_TRACE_ERROR("create_thread failed: %s", strerror(status)); 239 free_thread_slot(h); 240 return -1; 241 } 242 243 ts[h].thread_id = thread; 244 APPL_TRACE_DEBUG("h:%d, thread id:%d", h, ts[h].thread_id); 245 ts[h].callback = callback; 246 ts[h].cmd_callback = cmd_callback; 247 } 248 return h; 249} 250 251/* create dummy socket pair used to wake up select loop */ 252static inline void init_cmd_fd(int h) 253{ 254 asrt(ts[h].cmd_fdr == -1 && ts[h].cmd_fdw == -1); 255 if(socketpair(AF_UNIX, SOCK_STREAM, 0, &ts[h].cmd_fdr) < 0) 256 { 257 APPL_TRACE_ERROR("socketpair failed: %s", strerror(errno)); 258 return; 259 } 260 APPL_TRACE_DEBUG("h:%d, cmd_fdr:%d, cmd_fdw:%d", h, ts[h].cmd_fdr, ts[h].cmd_fdw); 261 //add the cmd fd for read & write 262 add_poll(h, ts[h].cmd_fdr, 0, SOCK_THREAD_FD_RD, 0); 263} 264static inline void close_cmd_fd(int h) 265{ 266 if(ts[h].cmd_fdr != -1) 267 { 268 close(ts[h].cmd_fdr); 269 ts[h].cmd_fdr = -1; 270 } 271 if(ts[h].cmd_fdw != -1) 272 { 273 close(ts[h].cmd_fdw); 274 ts[h].cmd_fdw = -1; 275 } 276} 277typedef struct 278{ 279 int id; 280 int fd; 281 int type; 282 int flags; 283 uint32_t user_id; 284} sock_cmd_t; 285int btsock_thread_add_fd(int h, int fd, int type, int flags, uint32_t user_id) 286{ 287 if(h < 0 || h >= MAX_THREAD) 288 { 289 APPL_TRACE_ERROR("invalid bt thread handle:%d", h); 290 return FALSE; 291 } 292 if(ts[h].cmd_fdw == -1) 293 { 294 APPL_TRACE_ERROR("cmd socket is not created. socket thread may not initialized"); 295 return FALSE; 296 } 297 if(flags & SOCK_THREAD_ADD_FD_SYNC) 298 { 299 //must executed in socket poll thread 300 if(ts[h].thread_id == pthread_self()) 301 { 302 //cleanup one-time flags 303 flags &= ~SOCK_THREAD_ADD_FD_SYNC; 304 add_poll(h, fd, type, flags, user_id); 305 return TRUE; 306 } 307 APPL_TRACE_DEBUG("THREAD_ADD_FD_SYNC is not called in poll thread, fallback to async"); 308 } 309 sock_cmd_t cmd = {CMD_ADD_FD, fd, type, flags, user_id}; 310 APPL_TRACE_DEBUG("adding fd:%d, flags:0x%x", fd, flags); 311 return send(ts[h].cmd_fdw, &cmd, sizeof(cmd), 0) == sizeof(cmd); 312} 313 314bool btsock_thread_remove_fd_and_close(int thread_handle, int fd) 315{ 316 if (thread_handle < 0 || thread_handle >= MAX_THREAD) 317 { 318 APPL_TRACE_ERROR("%s invalid thread handle: %d", __func__, thread_handle); 319 return false; 320 } 321 if (fd == -1) 322 { 323 APPL_TRACE_ERROR("%s invalid file descriptor.", __func__); 324 return false; 325 } 326 327 sock_cmd_t cmd = {CMD_REMOVE_FD, fd, 0, 0, 0}; 328 return send(ts[thread_handle].cmd_fdw, &cmd, sizeof(cmd), 0) == sizeof(cmd); 329} 330 331int btsock_thread_post_cmd(int h, int type, const unsigned char* data, int size, uint32_t user_id) 332{ 333 if(h < 0 || h >= MAX_THREAD) 334 { 335 APPL_TRACE_ERROR("invalid bt thread handle:%d", h); 336 return FALSE; 337 } 338 if(ts[h].cmd_fdw == -1) 339 { 340 APPL_TRACE_ERROR("cmd socket is not created. socket thread may not initialized"); 341 return FALSE; 342 } 343 sock_cmd_t cmd = {CMD_USER_PRIVATE, 0, type, size, user_id}; 344 APPL_TRACE_DEBUG("post cmd type:%d, size:%d, h:%d, ", type, size, h); 345 sock_cmd_t* cmd_send = &cmd; 346 int size_send = sizeof(cmd); 347 if(data && size) 348 { 349 size_send = sizeof(cmd) + size; 350 cmd_send = (sock_cmd_t*)alloca(size_send); 351 if(cmd_send) 352 { 353 *cmd_send = cmd; 354 memcpy(cmd_send + 1, data, size); 355 } 356 else 357 { 358 APPL_TRACE_ERROR("alloca failed at h:%d, cmd type:%d, size:%d", h, type, size_send); 359 return FALSE; 360 } 361 } 362 return send(ts[h].cmd_fdw, cmd_send, size_send, 0) == size_send; 363} 364int btsock_thread_wakeup(int h) 365{ 366 if(h < 0 || h >= MAX_THREAD) 367 { 368 APPL_TRACE_ERROR("invalid bt thread handle:%d", h); 369 return FALSE; 370 } 371 if(ts[h].cmd_fdw == -1) 372 { 373 APPL_TRACE_ERROR("thread handle:%d, cmd socket is not created", h); 374 return FALSE; 375 } 376 sock_cmd_t cmd = {CMD_WAKEUP, 0, 0, 0, 0}; 377 return send(ts[h].cmd_fdw, &cmd, sizeof(cmd), 0) == sizeof(cmd); 378} 379int btsock_thread_exit(int h) 380{ 381 if(h < 0 || h >= MAX_THREAD) 382 { 383 APPL_TRACE_ERROR("invalid bt thread handle:%d", h); 384 return FALSE; 385 } 386 if(ts[h].cmd_fdw == -1) 387 { 388 APPL_TRACE_ERROR("cmd socket is not created"); 389 return FALSE; 390 } 391 sock_cmd_t cmd = {CMD_EXIT, 0, 0, 0, 0}; 392 if(send(ts[h].cmd_fdw, &cmd, sizeof(cmd), 0) == sizeof(cmd)) 393 { 394 pthread_join(ts[h].thread_id, 0); 395 pthread_mutex_lock(&thread_slot_lock); 396 free_thread_slot(h); 397 pthread_mutex_unlock(&thread_slot_lock); 398 return TRUE; 399 } 400 return FALSE; 401} 402static void init_poll(int h) 403{ 404 int i; 405 ts[h].poll_count = 0; 406 ts[h].thread_id = -1; 407 ts[h].callback = NULL; 408 ts[h].cmd_callback = NULL; 409 for(i = 0; i < MAX_POLL; i++) 410 { 411 ts[h].ps[i].pfd.fd = -1; 412 ts[h].psi[i] = -1; 413 } 414 init_cmd_fd(h); 415} 416static inline unsigned int flags2pevents(int flags) 417{ 418 unsigned int pevents = 0; 419 if(flags & SOCK_THREAD_FD_WR) 420 pevents |= POLLOUT; 421 if(flags & SOCK_THREAD_FD_RD) 422 pevents |= POLLIN; 423 pevents |= POLL_EXCEPTION_EVENTS; 424 return pevents; 425} 426 427static inline void set_poll(poll_slot_t* ps, int fd, int type, int flags, uint32_t user_id) 428{ 429 ps->pfd.fd = fd; 430 ps->user_id = user_id; 431 if(ps->type != 0 && ps->type != type) 432 APPL_TRACE_ERROR("poll socket type should not changed! type was:%d, type now:%d", ps->type, type); 433 ps->type = type; 434 ps->flags = flags; 435 ps->pfd.events = flags2pevents(flags); 436 ps->pfd.revents = 0; 437} 438static inline void add_poll(int h, int fd, int type, int flags, uint32_t user_id) 439{ 440 asrt(fd != -1); 441 int i; 442 int empty = -1; 443 poll_slot_t* ps = ts[h].ps; 444 445 for(i = 0; i < MAX_POLL; i++) 446 { 447 if(ps[i].pfd.fd == fd) 448 { 449 asrt(ts[h].poll_count < MAX_POLL); 450 451 set_poll(&ps[i], fd, type, flags | ps[i].flags, user_id); 452 return; 453 } 454 else if(empty < 0 && ps[i].pfd.fd == -1) 455 empty = i; 456 } 457 if(empty >= 0) 458 { 459 asrt(ts[h].poll_count < MAX_POLL); 460 set_poll(&ps[empty], fd, type, flags, user_id); 461 ++ts[h].poll_count; 462 return; 463 } 464 APPL_TRACE_ERROR("exceeded max poll slot:%d!", MAX_POLL); 465} 466static inline void remove_poll(int h, poll_slot_t* ps, int flags) 467{ 468 if(flags == ps->flags) 469 { 470 //all monitored events signaled. To remove it, just clear the slot 471 --ts[h].poll_count; 472 memset(ps, 0, sizeof(*ps)); 473 ps->pfd.fd = -1; 474 } 475 else 476 { 477 //one read or one write monitor event signaled, removed the accordding bit 478 ps->flags &= ~flags; 479 //update the poll events mask 480 ps->pfd.events = flags2pevents(ps->flags); 481 } 482} 483static int process_cmd_sock(int h) 484{ 485 sock_cmd_t cmd = {-1, 0, 0, 0, 0}; 486 int fd = ts[h].cmd_fdr; 487 if(recv(fd, &cmd, sizeof(cmd), MSG_WAITALL) != sizeof(cmd)) 488 { 489 APPL_TRACE_ERROR("recv cmd errno:%d", errno); 490 return FALSE; 491 } 492 APPL_TRACE_DEBUG("cmd.id:%d", cmd.id); 493 switch(cmd.id) 494 { 495 case CMD_ADD_FD: 496 add_poll(h, cmd.fd, cmd.type, cmd.flags, cmd.user_id); 497 break; 498 case CMD_REMOVE_FD: 499 for (int i = 1; i < MAX_POLL; ++i) 500 { 501 poll_slot_t *poll_slot = &ts[h].ps[i]; 502 if (poll_slot->pfd.fd == cmd.fd) 503 { 504 remove_poll(h, poll_slot, poll_slot->flags); 505 break; 506 } 507 } 508 close(cmd.fd); 509 break; 510 case CMD_WAKEUP: 511 break; 512 case CMD_USER_PRIVATE: 513 asrt(ts[h].cmd_callback); 514 if(ts[h].cmd_callback) 515 ts[h].cmd_callback(fd, cmd.type, cmd.flags, cmd.user_id); 516 break; 517 case CMD_EXIT: 518 return FALSE; 519 default: 520 APPL_TRACE_DEBUG("unknown cmd: %d", cmd.id); 521 break; 522 } 523 return TRUE; 524} 525static void process_data_sock(int h, struct pollfd *pfds, int count) 526{ 527 asrt(count <= ts[h].poll_count); 528 int i; 529 for( i= 1; i < ts[h].poll_count; i++) 530 { 531 if(pfds[i].revents) 532 { 533 int ps_i = ts[h].psi[i]; 534 asrt(pfds[i].fd == ts[h].ps[ps_i].pfd.fd); 535 uint32_t user_id = ts[h].ps[ps_i].user_id; 536 int type = ts[h].ps[ps_i].type; 537 int flags = 0; 538 print_events(pfds[i].revents); 539 if(IS_READ(pfds[i].revents)) 540 { 541 flags |= SOCK_THREAD_FD_RD; 542 } 543 if(IS_WRITE(pfds[i].revents)) 544 { 545 flags |= SOCK_THREAD_FD_WR; 546 } 547 if(IS_EXCEPTION(pfds[i].revents)) 548 { 549 flags |= SOCK_THREAD_FD_EXCEPTION; 550 //remove the whole slot not flags 551 remove_poll(h, &ts[h].ps[ps_i], ts[h].ps[ps_i].flags); 552 } 553 else if(flags) 554 remove_poll(h, &ts[h].ps[ps_i], flags); //remove the monitor flags that already processed 555 if(flags) 556 ts[h].callback(pfds[i].fd, type, flags, user_id); 557 } 558 } 559} 560 561static void prepare_poll_fds(int h, struct pollfd* pfds) 562{ 563 int count = 0; 564 int ps_i = 0; 565 int pfd_i = 0; 566 asrt(ts[h].poll_count <= MAX_POLL); 567 memset(pfds, 0, sizeof(pfds[0])*ts[h].poll_count); 568 while(count < ts[h].poll_count) 569 { 570 if(ps_i >= MAX_POLL) 571 { 572 APPL_TRACE_ERROR("exceed max poll range, ps_i:%d, MAX_POLL:%d, count:%d, ts[h].poll_count:%d", 573 ps_i, MAX_POLL, count, ts[h].poll_count); 574 return; 575 } 576 if(ts[h].ps[ps_i].pfd.fd >= 0) 577 { 578 pfds[pfd_i] = ts[h].ps[ps_i].pfd; 579 ts[h].psi[pfd_i] = ps_i; 580 count++; 581 pfd_i++; 582 } 583 ps_i++; 584 } 585} 586static void *sock_poll_thread(void *arg) 587{ 588 struct pollfd pfds[MAX_POLL]; 589 memset(pfds, 0, sizeof(pfds)); 590 int h = (intptr_t)arg; 591 for(;;) 592 { 593 prepare_poll_fds(h, pfds); 594 int ret = poll(pfds, ts[h].poll_count, -1); 595 if(ret == -1) 596 { 597 APPL_TRACE_ERROR("poll ret -1, exit the thread, errno:%d, err:%s", errno, strerror(errno)); 598 break; 599 } 600 if(ret != 0) 601 { 602 int need_process_data_fd = TRUE; 603 if(pfds[0].revents) //cmd fd always is the first one 604 { 605 asrt(pfds[0].fd == ts[h].cmd_fdr); 606 if(!process_cmd_sock(h)) 607 { 608 APPL_TRACE_DEBUG("h:%d, process_cmd_sock return false, exit...", h); 609 break; 610 } 611 if(ret == 1) 612 need_process_data_fd = FALSE; 613 else ret--; //exclude the cmd fd 614 } 615 if(need_process_data_fd) 616 process_data_sock(h, pfds, ret); 617 } 618 else {APPL_TRACE_DEBUG("no data, select ret: %d", ret)}; 619 } 620 ts[h].thread_id = -1; 621 APPL_TRACE_DEBUG("socket poll thread exiting, h:%d", h); 622 return 0; 623} 624