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