Home | History | Annotate | Download | only in src
      1 /******************************************************************************
      2  *
      3  *  Copyright (C) 2014 Google, Inc.
      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 #define LOG_TAG "bt_btif_sock"
     20 
     21 #include <atomic>
     22 
     23 #include <base/logging.h>
     24 
     25 #include <hardware/bluetooth.h>
     26 #include <hardware/bt_sock.h>
     27 
     28 #include "bta_api.h"
     29 #include "btif_common.h"
     30 #include "btif_sock_l2cap.h"
     31 #include "btif_sock_rfc.h"
     32 #include "btif_sock_sco.h"
     33 #include "btif_sock_sdp.h"
     34 #include "btif_sock_thread.h"
     35 #include "btif_uid.h"
     36 #include "btif_util.h"
     37 #include "osi/include/thread.h"
     38 
     39 static bt_status_t btsock_listen(btsock_type_t type, const char* service_name,
     40                                  const uint8_t* uuid, int channel, int* sock_fd,
     41                                  int flags, int app_uid);
     42 static bt_status_t btsock_connect(const bt_bdaddr_t* bd_addr,
     43                                   btsock_type_t type, const uint8_t* uuid,
     44                                   int channel, int* sock_fd, int flags,
     45                                   int app_uid);
     46 
     47 static void btsock_signaled(int fd, int type, int flags, uint32_t user_id);
     48 
     49 static std::atomic_int thread_handle{-1};
     50 static thread_t* thread;
     51 
     52 btsock_interface_t* btif_sock_get_interface(void) {
     53   static btsock_interface_t interface = {sizeof(interface), btsock_listen,
     54                                          btsock_connect};
     55 
     56   return &interface;
     57 }
     58 
     59 bt_status_t btif_sock_init(uid_set_t* uid_set) {
     60   CHECK(thread_handle == -1);
     61   CHECK(thread == NULL);
     62 
     63   bt_status_t status;
     64   btsock_thread_init();
     65   thread_handle = btsock_thread_create(btsock_signaled, NULL);
     66   if (thread_handle == -1) {
     67     LOG_ERROR(LOG_TAG, "%s unable to create btsock_thread.", __func__);
     68     goto error;
     69   }
     70 
     71   status = btsock_rfc_init(thread_handle, uid_set);
     72   if (status != BT_STATUS_SUCCESS) {
     73     LOG_ERROR(LOG_TAG, "%s error initializing RFCOMM sockets: %d", __func__,
     74               status);
     75     goto error;
     76   }
     77 
     78   status = btsock_l2cap_init(thread_handle, uid_set);
     79   if (status != BT_STATUS_SUCCESS) {
     80     LOG_ERROR(LOG_TAG, "%s error initializing L2CAP sockets: %d", __func__,
     81               status);
     82     goto error;
     83   }
     84 
     85   thread = thread_new("btif_sock");
     86   if (!thread) {
     87     LOG_ERROR(LOG_TAG, "%s error creating new thread.", __func__);
     88     btsock_rfc_cleanup();
     89     goto error;
     90   }
     91 
     92   status = btsock_sco_init(thread);
     93   if (status != BT_STATUS_SUCCESS) {
     94     LOG_ERROR(LOG_TAG, "%s error initializing SCO sockets: %d", __func__,
     95               status);
     96     btsock_rfc_cleanup();
     97     goto error;
     98   }
     99 
    100   return BT_STATUS_SUCCESS;
    101 
    102 error:;
    103   thread_free(thread);
    104   thread = NULL;
    105   if (thread_handle != -1) btsock_thread_exit(thread_handle);
    106   thread_handle = -1;
    107   uid_set = NULL;
    108   return BT_STATUS_FAIL;
    109 }
    110 
    111 void btif_sock_cleanup(void) {
    112   int saved_handle = thread_handle;
    113   if (std::atomic_exchange(&thread_handle, -1) == -1) return;
    114 
    115   btsock_thread_exit(saved_handle);
    116   btsock_rfc_cleanup();
    117   btsock_sco_cleanup();
    118   btsock_l2cap_cleanup();
    119   thread_free(thread);
    120   thread = NULL;
    121 }
    122 
    123 static bt_status_t btsock_listen(btsock_type_t type, const char* service_name,
    124                                  const uint8_t* service_uuid, int channel,
    125                                  int* sock_fd, int flags, int app_uid) {
    126   if ((flags & BTSOCK_FLAG_NO_SDP) == 0) {
    127     CHECK(service_uuid != NULL || channel > 0);
    128     CHECK(sock_fd != NULL);
    129   }
    130 
    131   *sock_fd = INVALID_FD;
    132   bt_status_t status = BT_STATUS_FAIL;
    133 
    134   switch (type) {
    135     case BTSOCK_RFCOMM:
    136       status = btsock_rfc_listen(service_name, service_uuid, channel, sock_fd,
    137                                  flags, app_uid);
    138       break;
    139     case BTSOCK_L2CAP:
    140       status =
    141           btsock_l2cap_listen(service_name, channel, sock_fd, flags, app_uid);
    142       break;
    143 
    144     case BTSOCK_SCO:
    145       status = btsock_sco_listen(sock_fd, flags);
    146       break;
    147 
    148     default:
    149       LOG_ERROR(LOG_TAG, "%s unknown/unsupported socket type: %d", __func__,
    150                 type);
    151       status = BT_STATUS_UNSUPPORTED;
    152       break;
    153   }
    154   return status;
    155 }
    156 
    157 static bt_status_t btsock_connect(const bt_bdaddr_t* bd_addr,
    158                                   btsock_type_t type, const uint8_t* uuid,
    159                                   int channel, int* sock_fd, int flags,
    160                                   int app_uid) {
    161   CHECK(uuid != NULL || channel > 0);
    162   CHECK(bd_addr != NULL);
    163   CHECK(sock_fd != NULL);
    164 
    165   *sock_fd = INVALID_FD;
    166   bt_status_t status = BT_STATUS_FAIL;
    167 
    168   switch (type) {
    169     case BTSOCK_RFCOMM:
    170       status =
    171           btsock_rfc_connect(bd_addr, uuid, channel, sock_fd, flags, app_uid);
    172       break;
    173 
    174     case BTSOCK_L2CAP:
    175       status = btsock_l2cap_connect(bd_addr, channel, sock_fd, flags, app_uid);
    176       break;
    177 
    178     case BTSOCK_SCO:
    179       status = btsock_sco_connect(bd_addr, sock_fd, flags);
    180       break;
    181 
    182     default:
    183       LOG_ERROR(LOG_TAG, "%s unknown/unsupported socket type: %d", __func__,
    184                 type);
    185       status = BT_STATUS_UNSUPPORTED;
    186       break;
    187   }
    188   return status;
    189 }
    190 
    191 static void btsock_signaled(int fd, int type, int flags, uint32_t user_id) {
    192   switch (type) {
    193     case BTSOCK_RFCOMM:
    194       btsock_rfc_signaled(fd, flags, user_id);
    195       break;
    196     case BTSOCK_L2CAP:
    197       btsock_l2cap_signaled(fd, flags, user_id);
    198       break;
    199     default:
    200       CHECK(false && "Invalid socket type");
    201       break;
    202   }
    203 }
    204