Home | History | Annotate | Download | only in src
      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:      bluetooth.c
     22  *
     23  *  Description:   Bluetooth HAL implementation
     24  *
     25  ***********************************************************************************/
     26 
     27 #include <stdio.h>
     28 #include <stdlib.h>
     29 #include <unistd.h>
     30 
     31 #include <hardware/bluetooth.h>
     32 #include <hardware/bt_hf.h>
     33 #include <hardware/bt_av.h>
     34 #include <hardware/bt_sock.h>
     35 #include <hardware/bt_hh.h>
     36 #include <hardware/bt_hl.h>
     37 #include <hardware/bt_pan.h>
     38 #include <hardware/bt_gatt.h>
     39 #include <hardware/bt_rc.h>
     40 
     41 #define LOG_NDDEBUG 0
     42 #define LOG_TAG "bluedroid"
     43 
     44 #include "btif_api.h"
     45 #include "bt_utils.h"
     46 
     47 /************************************************************************************
     48 **  Constants & Macros
     49 ************************************************************************************/
     50 
     51 #define is_profile(profile, str) ((strlen(str) == strlen(profile)) && strncmp((const char *)profile, str, strlen(str)) == 0)
     52 
     53 /************************************************************************************
     54 **  Local type definitions
     55 ************************************************************************************/
     56 
     57 /************************************************************************************
     58 **  Static variables
     59 ************************************************************************************/
     60 
     61 bt_callbacks_t *bt_hal_cbacks = NULL;
     62 
     63 /************************************************************************************
     64 **  Static functions
     65 ************************************************************************************/
     66 
     67 /************************************************************************************
     68 **  Externs
     69 ************************************************************************************/
     70 
     71 /* list all extended interfaces here */
     72 
     73 /* handsfree profile */
     74 extern bthf_interface_t *btif_hf_get_interface();
     75 /* advanced audio profile */
     76 extern btav_interface_t *btif_av_get_interface();
     77 /*rfc l2cap*/
     78 extern btsock_interface_t *btif_sock_get_interface();
     79 /* hid host profile */
     80 extern bthh_interface_t *btif_hh_get_interface();
     81 /* health device profile */
     82 extern bthl_interface_t *btif_hl_get_interface();
     83 /*pan*/
     84 extern btpan_interface_t *btif_pan_get_interface();
     85 /* gatt */
     86 extern btgatt_interface_t *btif_gatt_get_interface();
     87 /* avrc */
     88 extern btrc_interface_t *btif_rc_get_interface();
     89 
     90 /************************************************************************************
     91 **  Functions
     92 ************************************************************************************/
     93 
     94 static uint8_t interface_ready(void)
     95 {
     96     /* add checks here that would prevent API calls other than init to be executed */
     97     if (bt_hal_cbacks == NULL)
     98         return FALSE;
     99 
    100     return TRUE;
    101 }
    102 
    103 
    104 /*****************************************************************************
    105 **
    106 **   BLUETOOTH HAL INTERFACE FUNCTIONS
    107 **
    108 *****************************************************************************/
    109 
    110 static int init(bt_callbacks_t* callbacks )
    111 {
    112     ALOGI("init");
    113 
    114     /* sanity check */
    115     if (interface_ready() == TRUE)
    116         return BT_STATUS_DONE;
    117 
    118     /* store reference to user callbacks */
    119     bt_hal_cbacks = callbacks;
    120 
    121     /* add checks for individual callbacks ? */
    122 
    123     bt_utils_init();
    124 
    125     /* init btif */
    126     btif_init_bluetooth();
    127 
    128     return BT_STATUS_SUCCESS;
    129 }
    130 
    131 static int enable( void )
    132 {
    133     ALOGI("enable");
    134 
    135     /* sanity check */
    136     if (interface_ready() == FALSE)
    137         return BT_STATUS_NOT_READY;
    138 
    139     return btif_enable_bluetooth();
    140 }
    141 
    142 static int disable(void)
    143 {
    144     /* sanity check */
    145     if (interface_ready() == FALSE)
    146         return BT_STATUS_NOT_READY;
    147 
    148     return btif_disable_bluetooth();
    149 }
    150 
    151 static void cleanup( void )
    152 {
    153     /* sanity check */
    154     if (interface_ready() == FALSE)
    155         return;
    156 
    157     btif_shutdown_bluetooth();
    158     bt_utils_cleanup();
    159 
    160     /* hal callbacks reset upon shutdown complete callback */
    161 
    162     return;
    163 }
    164 
    165 static int get_adapter_properties(void)
    166 {
    167     /* sanity check */
    168     if (interface_ready() == FALSE)
    169         return BT_STATUS_NOT_READY;
    170 
    171     return btif_get_adapter_properties();
    172 }
    173 
    174 static int get_adapter_property(bt_property_type_t type)
    175 {
    176     /* sanity check */
    177     if (interface_ready() == FALSE)
    178         return BT_STATUS_NOT_READY;
    179 
    180     return btif_get_adapter_property(type);
    181 }
    182 
    183 static int set_adapter_property(const bt_property_t *property)
    184 {
    185     /* sanity check */
    186     if (interface_ready() == FALSE)
    187         return BT_STATUS_NOT_READY;
    188 
    189     return btif_set_adapter_property(property);
    190 }
    191 
    192 int get_remote_device_properties(bt_bdaddr_t *remote_addr)
    193 {
    194     /* sanity check */
    195     if (interface_ready() == FALSE)
    196         return BT_STATUS_NOT_READY;
    197 
    198     return btif_get_remote_device_properties(remote_addr);
    199 }
    200 
    201 int get_remote_device_property(bt_bdaddr_t *remote_addr, bt_property_type_t type)
    202 {
    203     /* sanity check */
    204     if (interface_ready() == FALSE)
    205         return BT_STATUS_NOT_READY;
    206 
    207     return btif_get_remote_device_property(remote_addr, type);
    208 }
    209 
    210 int set_remote_device_property(bt_bdaddr_t *remote_addr, const bt_property_t *property)
    211 {
    212     /* sanity check */
    213     if (interface_ready() == FALSE)
    214         return BT_STATUS_NOT_READY;
    215 
    216     return btif_set_remote_device_property(remote_addr, property);
    217 }
    218 
    219 int get_remote_service_record(bt_bdaddr_t *remote_addr, bt_uuid_t *uuid)
    220 {
    221     /* sanity check */
    222     if (interface_ready() == FALSE)
    223         return BT_STATUS_NOT_READY;
    224 
    225     return btif_get_remote_service_record(remote_addr, uuid);
    226 }
    227 
    228 int get_remote_services(bt_bdaddr_t *remote_addr)
    229 {
    230     /* sanity check */
    231     if (interface_ready() == FALSE)
    232         return BT_STATUS_NOT_READY;
    233 
    234     return btif_dm_get_remote_services(remote_addr);
    235 }
    236 
    237 static int start_discovery(void)
    238 {
    239     /* sanity check */
    240     if (interface_ready() == FALSE)
    241         return BT_STATUS_NOT_READY;
    242 
    243     return btif_dm_start_discovery();
    244 }
    245 
    246 static int cancel_discovery(void)
    247 {
    248     /* sanity check */
    249     if (interface_ready() == FALSE)
    250         return BT_STATUS_NOT_READY;
    251 
    252     return btif_dm_cancel_discovery();
    253 }
    254 
    255 static int create_bond(const bt_bdaddr_t *bd_addr)
    256 {
    257     /* sanity check */
    258     if (interface_ready() == FALSE)
    259         return BT_STATUS_NOT_READY;
    260 
    261     return btif_dm_create_bond(bd_addr);
    262 }
    263 
    264 static int cancel_bond(const bt_bdaddr_t *bd_addr)
    265 {
    266     /* sanity check */
    267     if (interface_ready() == FALSE)
    268         return BT_STATUS_NOT_READY;
    269 
    270     return btif_dm_cancel_bond(bd_addr);
    271 }
    272 
    273 static int remove_bond(const bt_bdaddr_t *bd_addr)
    274 {
    275     /* sanity check */
    276     if (interface_ready() == FALSE)
    277         return BT_STATUS_NOT_READY;
    278 
    279     return btif_dm_remove_bond(bd_addr);
    280 }
    281 
    282 static int pin_reply(const bt_bdaddr_t *bd_addr, uint8_t accept,
    283                  uint8_t pin_len, bt_pin_code_t *pin_code)
    284 {
    285     /* sanity check */
    286     if (interface_ready() == FALSE)
    287         return BT_STATUS_NOT_READY;
    288 
    289     return btif_dm_pin_reply(bd_addr, accept, pin_len, pin_code);
    290 }
    291 
    292 static int ssp_reply(const bt_bdaddr_t *bd_addr, bt_ssp_variant_t variant,
    293                        uint8_t accept, uint32_t passkey)
    294 {
    295     /* sanity check */
    296     if (interface_ready() == FALSE)
    297         return BT_STATUS_NOT_READY;
    298 
    299     return btif_dm_ssp_reply(bd_addr, variant, accept, passkey);
    300 }
    301 
    302 static const void* get_profile_interface (const char *profile_id)
    303 {
    304     ALOGI("get_profile_interface %s", profile_id);
    305 
    306     /* sanity check */
    307     if (interface_ready() == FALSE)
    308         return NULL;
    309 
    310     /* check for supported profile interfaces */
    311     if (is_profile(profile_id, BT_PROFILE_HANDSFREE_ID))
    312         return btif_hf_get_interface();
    313 
    314     if (is_profile(profile_id, BT_PROFILE_SOCKETS_ID))
    315         return btif_sock_get_interface();
    316 
    317     if (is_profile(profile_id, BT_PROFILE_PAN_ID))
    318         return btif_pan_get_interface();
    319 
    320     if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_ID))
    321         return btif_av_get_interface();
    322 
    323     if (is_profile(profile_id, BT_PROFILE_HIDHOST_ID))
    324         return btif_hh_get_interface();
    325 
    326     if (is_profile(profile_id, BT_PROFILE_HEALTH_ID))
    327         return btif_hl_get_interface();
    328 
    329 #if BTA_GATT_INCLUDED == TRUE
    330     if (is_profile(profile_id, BT_PROFILE_GATT_ID))
    331         return btif_gatt_get_interface();
    332 #endif
    333 
    334     if (is_profile(profile_id, BT_PROFILE_AV_RC_ID))
    335         return btif_rc_get_interface();
    336 
    337     return NULL;
    338 }
    339 
    340 int dut_mode_configure(uint8_t enable)
    341 {
    342     ALOGI("dut_mode_configure");
    343 
    344     /* sanity check */
    345     if (interface_ready() == FALSE)
    346         return BT_STATUS_NOT_READY;
    347 
    348     return btif_dut_mode_configure(enable);
    349 }
    350 
    351 int dut_mode_send(uint16_t opcode, uint8_t* buf, uint8_t len)
    352 {
    353     ALOGI("dut_mode_send");
    354 
    355     /* sanity check */
    356     if (interface_ready() == FALSE)
    357         return BT_STATUS_NOT_READY;
    358 
    359     return btif_dut_mode_send(opcode, buf, len);
    360 }
    361 
    362 #if BLE_INCLUDED == TRUE
    363 int le_test_mode(uint16_t opcode, uint8_t* buf, uint8_t len)
    364 {
    365     ALOGI("le_test_mode");
    366 
    367     /* sanity check */
    368     if (interface_ready() == FALSE)
    369         return BT_STATUS_NOT_READY;
    370 
    371     return btif_le_test_mode(opcode, buf, len);
    372 }
    373 #endif
    374 
    375 static const bt_interface_t bluetoothInterface = {
    376     sizeof(bluetoothInterface),
    377     init,
    378     enable,
    379     disable,
    380     cleanup,
    381     get_adapter_properties,
    382     get_adapter_property,
    383     set_adapter_property,
    384     get_remote_device_properties,
    385     get_remote_device_property,
    386     set_remote_device_property,
    387     get_remote_service_record,
    388     get_remote_services,
    389     start_discovery,
    390     cancel_discovery,
    391     create_bond,
    392     remove_bond,
    393     cancel_bond,
    394     pin_reply,
    395     ssp_reply,
    396     get_profile_interface,
    397     dut_mode_configure,
    398     dut_mode_send,
    399 #if BLE_INCLUDED == TRUE
    400     le_test_mode
    401 #else
    402     NULL
    403 #endif
    404 };
    405 
    406 const bt_interface_t* bluetooth__get_bluetooth_interface ()
    407 {
    408     /* fixme -- add property to disable bt interface ? */
    409 
    410     return &bluetoothInterface;
    411 }
    412 
    413 static int close_bluetooth_stack(struct hw_device_t* device)
    414 {
    415     cleanup();
    416     return 0;
    417 }
    418 
    419 static int open_bluetooth_stack (const struct hw_module_t* module, char const* name,
    420 struct hw_device_t** abstraction)
    421 {
    422     bluetooth_device_t *stack = malloc(sizeof(bluetooth_device_t) );
    423     memset(stack, 0, sizeof(bluetooth_device_t) );
    424     stack->common.tag = HARDWARE_DEVICE_TAG;
    425     stack->common.version = 0;
    426     stack->common.module = (struct hw_module_t*)module;
    427     stack->common.close = close_bluetooth_stack;
    428     stack->get_bluetooth_interface = bluetooth__get_bluetooth_interface;
    429     *abstraction = (struct hw_device_t*)stack;
    430     return 0;
    431 }
    432 
    433 
    434 static struct hw_module_methods_t bt_stack_module_methods = {
    435     .open = open_bluetooth_stack,
    436 };
    437 
    438 struct hw_module_t HAL_MODULE_INFO_SYM = {
    439     .tag = HARDWARE_MODULE_TAG,
    440     .version_major = 1,
    441     .version_minor = 0,
    442     .id = BT_HARDWARE_MODULE_ID,
    443     .name = "Bluetooth Stack",
    444     .author = "The Android Open Source Project",
    445     .methods = &bt_stack_module_methods
    446 };
    447 
    448