Home | History | Annotate | Download | only in src
      1 /******************************************************************************
      2  *
      3  *  Copyright 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 #define LOG_TAG "bt_btif"
     28 
     29 #include <base/logging.h>
     30 #include <stdio.h>
     31 #include <stdlib.h>
     32 #include <string.h>
     33 #include <unistd.h>
     34 
     35 #include <hardware/bluetooth.h>
     36 #include <hardware/bluetooth_headset_interface.h>
     37 #include <hardware/bt_av.h>
     38 #include <hardware/bt_gatt.h>
     39 #include <hardware/bt_hd.h>
     40 #include <hardware/bt_hearing_aid.h>
     41 #include <hardware/bt_hf_client.h>
     42 #include <hardware/bt_hh.h>
     43 #include <hardware/bt_hl.h>
     44 #include <hardware/bt_mce.h>
     45 #include <hardware/bt_pan.h>
     46 #include <hardware/bt_rc.h>
     47 #include <hardware/bt_sdp.h>
     48 #include <hardware/bt_sock.h>
     49 
     50 #include "avrcp_service.h"
     51 #include "bt_utils.h"
     52 #include "bta/include/bta_hearing_aid_api.h"
     53 #include "bta/include/bta_hf_client_api.h"
     54 #include "btif_a2dp.h"
     55 #include "btif_api.h"
     56 #include "btif_av.h"
     57 #include "btif_config.h"
     58 #include "btif_debug.h"
     59 #include "btif_debug_btsnoop.h"
     60 #include "btif_debug_conn.h"
     61 #include "btif_hf.h"
     62 #include "btif_storage.h"
     63 #include "btsnoop.h"
     64 #include "btsnoop_mem.h"
     65 #include "device/include/interop.h"
     66 #include "osi/include/alarm.h"
     67 #include "osi/include/allocation_tracker.h"
     68 #include "osi/include/log.h"
     69 #include "osi/include/metrics.h"
     70 #include "osi/include/osi.h"
     71 #include "osi/include/wakelock.h"
     72 #include "stack_manager.h"
     73 
     74 /* Test interface includes */
     75 #include "mca_api.h"
     76 
     77 using bluetooth::hearing_aid::HearingAidInterface;
     78 
     79 /*******************************************************************************
     80  *  Static variables
     81  ******************************************************************************/
     82 
     83 bt_callbacks_t* bt_hal_cbacks = NULL;
     84 bool restricted_mode = false;
     85 
     86 /*******************************************************************************
     87  *  Externs
     88  ******************************************************************************/
     89 
     90 /* list all extended interfaces here */
     91 
     92 /* handsfree profile - client */
     93 extern const bthf_client_interface_t* btif_hf_client_get_interface();
     94 /* advanced audio profile */
     95 extern const btav_source_interface_t* btif_av_get_src_interface();
     96 extern const btav_sink_interface_t* btif_av_get_sink_interface();
     97 /*rfc l2cap*/
     98 extern const btsock_interface_t* btif_sock_get_interface();
     99 /* hid host profile */
    100 extern const bthh_interface_t* btif_hh_get_interface();
    101 /* hid device profile */
    102 extern const bthd_interface_t* btif_hd_get_interface();
    103 /* health device profile */
    104 extern const bthl_interface_t* btif_hl_get_interface();
    105 /*pan*/
    106 extern const btpan_interface_t* btif_pan_get_interface();
    107 /*map client*/
    108 extern const btmce_interface_t* btif_mce_get_interface();
    109 /* gatt */
    110 extern const btgatt_interface_t* btif_gatt_get_interface();
    111 /* avrc target */
    112 extern const btrc_interface_t* btif_rc_get_interface();
    113 /* avrc controller */
    114 extern const btrc_ctrl_interface_t* btif_rc_ctrl_get_interface();
    115 /*SDP search client*/
    116 extern const btsdp_interface_t* btif_sdp_get_interface();
    117 /*Hearing Aid client*/
    118 extern HearingAidInterface* btif_hearing_aid_get_interface();
    119 
    120 /* List all test interface here */
    121 extern const btmcap_test_interface_t* stack_mcap_get_interface();
    122 
    123 /*******************************************************************************
    124  *  Functions
    125  ******************************************************************************/
    126 
    127 static bool interface_ready(void) { return bt_hal_cbacks != NULL; }
    128 
    129 static bool is_profile(const char* p1, const char* p2) {
    130   CHECK(p1);
    131   CHECK(p2);
    132   return strlen(p1) == strlen(p2) && strncmp(p1, p2, strlen(p2)) == 0;
    133 }
    134 
    135 /*****************************************************************************
    136  *
    137  *   BLUETOOTH HAL INTERFACE FUNCTIONS
    138  *
    139  ****************************************************************************/
    140 
    141 static int init(bt_callbacks_t* callbacks) {
    142   LOG_INFO(LOG_TAG, "%s", __func__);
    143 
    144   if (interface_ready()) return BT_STATUS_DONE;
    145 
    146 #ifdef BLUEDROID_DEBUG
    147   allocation_tracker_init();
    148 #endif
    149 
    150   bt_hal_cbacks = callbacks;
    151   stack_manager_get_interface()->init_stack();
    152   btif_debug_init();
    153   return BT_STATUS_SUCCESS;
    154 }
    155 
    156 static int enable(bool start_restricted) {
    157   LOG_INFO(LOG_TAG, "%s: start restricted = %d", __func__, start_restricted);
    158 
    159   restricted_mode = start_restricted;
    160 
    161   if (!interface_ready()) return BT_STATUS_NOT_READY;
    162 
    163   stack_manager_get_interface()->start_up_stack_async();
    164   return BT_STATUS_SUCCESS;
    165 }
    166 
    167 static int disable(void) {
    168   if (!interface_ready()) return BT_STATUS_NOT_READY;
    169 
    170   stack_manager_get_interface()->shut_down_stack_async();
    171   return BT_STATUS_SUCCESS;
    172 }
    173 
    174 static void cleanup(void) { stack_manager_get_interface()->clean_up_stack(); }
    175 
    176 bool is_restricted_mode() { return restricted_mode; }
    177 
    178 static int get_adapter_properties(void) {
    179   /* sanity check */
    180   if (!interface_ready()) return BT_STATUS_NOT_READY;
    181 
    182   return btif_get_adapter_properties();
    183 }
    184 
    185 static int get_adapter_property(bt_property_type_t type) {
    186   /* sanity check */
    187   if (!interface_ready()) return BT_STATUS_NOT_READY;
    188 
    189   return btif_get_adapter_property(type);
    190 }
    191 
    192 static int set_adapter_property(const bt_property_t* property) {
    193   /* sanity check */
    194   if (!interface_ready()) return BT_STATUS_NOT_READY;
    195 
    196   return btif_set_adapter_property(property);
    197 }
    198 
    199 int get_remote_device_properties(RawAddress* remote_addr) {
    200   /* sanity check */
    201   if (!interface_ready()) return BT_STATUS_NOT_READY;
    202 
    203   return btif_get_remote_device_properties(remote_addr);
    204 }
    205 
    206 int get_remote_device_property(RawAddress* remote_addr,
    207                                bt_property_type_t type) {
    208   /* sanity check */
    209   if (!interface_ready()) return BT_STATUS_NOT_READY;
    210 
    211   return btif_get_remote_device_property(remote_addr, type);
    212 }
    213 
    214 int set_remote_device_property(RawAddress* remote_addr,
    215                                const bt_property_t* property) {
    216   /* sanity check */
    217   if (!interface_ready()) return BT_STATUS_NOT_READY;
    218 
    219   return btif_set_remote_device_property(remote_addr, property);
    220 }
    221 
    222 int get_remote_service_record(const RawAddress& remote_addr,
    223                               const bluetooth::Uuid& uuid) {
    224   /* sanity check */
    225   if (!interface_ready()) return BT_STATUS_NOT_READY;
    226 
    227   return btif_get_remote_service_record(remote_addr, uuid);
    228 }
    229 
    230 int get_remote_services(RawAddress* remote_addr) {
    231   /* sanity check */
    232   if (!interface_ready()) return BT_STATUS_NOT_READY;
    233 
    234   return btif_dm_get_remote_services(*remote_addr);
    235 }
    236 
    237 static int start_discovery(void) {
    238   /* sanity check */
    239   if (!interface_ready()) return BT_STATUS_NOT_READY;
    240 
    241   return btif_dm_start_discovery();
    242 }
    243 
    244 static int cancel_discovery(void) {
    245   /* sanity check */
    246   if (!interface_ready()) return BT_STATUS_NOT_READY;
    247 
    248   return btif_dm_cancel_discovery();
    249 }
    250 
    251 static int create_bond(const RawAddress* bd_addr, int transport) {
    252   /* sanity check */
    253   if (!interface_ready()) return BT_STATUS_NOT_READY;
    254 
    255   return btif_dm_create_bond(bd_addr, transport);
    256 }
    257 
    258 static int create_bond_out_of_band(const RawAddress* bd_addr, int transport,
    259                                    const bt_out_of_band_data_t* oob_data) {
    260   /* sanity check */
    261   if (!interface_ready()) return BT_STATUS_NOT_READY;
    262 
    263   return btif_dm_create_bond_out_of_band(bd_addr, transport, oob_data);
    264 }
    265 
    266 static int cancel_bond(const RawAddress* bd_addr) {
    267   /* sanity check */
    268   if (!interface_ready()) return BT_STATUS_NOT_READY;
    269 
    270   return btif_dm_cancel_bond(bd_addr);
    271 }
    272 
    273 static int remove_bond(const RawAddress* bd_addr) {
    274   if (is_restricted_mode() && !btif_storage_is_restricted_device(bd_addr))
    275     return BT_STATUS_SUCCESS;
    276 
    277   /* sanity check */
    278   if (!interface_ready()) return BT_STATUS_NOT_READY;
    279 
    280   return btif_dm_remove_bond(bd_addr);
    281 }
    282 
    283 static int get_connection_state(const RawAddress* bd_addr) {
    284   /* sanity check */
    285   if (!interface_ready()) return 0;
    286 
    287   return btif_dm_get_connection_state(bd_addr);
    288 }
    289 
    290 static int pin_reply(const RawAddress* bd_addr, uint8_t accept, uint8_t pin_len,
    291                      bt_pin_code_t* pin_code) {
    292   /* sanity check */
    293   if (!interface_ready()) return BT_STATUS_NOT_READY;
    294 
    295   return btif_dm_pin_reply(bd_addr, accept, pin_len, pin_code);
    296 }
    297 
    298 static int ssp_reply(const RawAddress* bd_addr, bt_ssp_variant_t variant,
    299                      uint8_t accept, uint32_t passkey) {
    300   /* sanity check */
    301   if (!interface_ready()) return BT_STATUS_NOT_READY;
    302 
    303   return btif_dm_ssp_reply(bd_addr, variant, accept, passkey);
    304 }
    305 
    306 static int read_energy_info() {
    307   if (!interface_ready()) return BT_STATUS_NOT_READY;
    308   btif_dm_read_energy_info();
    309   return BT_STATUS_SUCCESS;
    310 }
    311 
    312 static void dump(int fd, const char** arguments) {
    313   btif_debug_conn_dump(fd);
    314   btif_debug_bond_event_dump(fd);
    315   btif_debug_a2dp_dump(fd);
    316   btif_debug_av_dump(fd);
    317   bta_debug_av_dump(fd);
    318   stack_debug_avdtp_api_dump(fd);
    319   bluetooth::avrcp::AvrcpService::DebugDump(fd);
    320   btif_debug_config_dump(fd);
    321   BTA_HfClientDumpStatistics(fd);
    322   wakelock_debug_dump(fd);
    323   osi_allocator_debug_dump(fd);
    324   alarm_debug_dump(fd);
    325   HearingAid::DebugDump(fd);
    326 #if (BTSNOOP_MEM == TRUE)
    327   btif_debug_btsnoop_dump(fd);
    328 #endif
    329 
    330   close(fd);
    331 }
    332 
    333 static void dumpMetrics(std::string* output) {
    334   system_bt_osi::BluetoothMetricsLogger::GetInstance()->WriteString(output);
    335 }
    336 
    337 static const void* get_profile_interface(const char* profile_id) {
    338   LOG_INFO(LOG_TAG, "%s: id = %s", __func__, profile_id);
    339 
    340   /* sanity check */
    341   if (!interface_ready()) return NULL;
    342 
    343   /* check for supported profile interfaces */
    344   if (is_profile(profile_id, BT_PROFILE_HANDSFREE_ID))
    345     return bluetooth::headset::GetInterface();
    346 
    347   if (is_profile(profile_id, BT_PROFILE_HANDSFREE_CLIENT_ID))
    348     return btif_hf_client_get_interface();
    349 
    350   if (is_profile(profile_id, BT_PROFILE_SOCKETS_ID))
    351     return btif_sock_get_interface();
    352 
    353   if (is_profile(profile_id, BT_PROFILE_PAN_ID))
    354     return btif_pan_get_interface();
    355 
    356   if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_ID))
    357     return btif_av_get_src_interface();
    358 
    359   if (is_profile(profile_id, BT_PROFILE_ADVANCED_AUDIO_SINK_ID))
    360     return btif_av_get_sink_interface();
    361 
    362   if (is_profile(profile_id, BT_PROFILE_HIDHOST_ID))
    363     return btif_hh_get_interface();
    364 
    365   if (is_profile(profile_id, BT_PROFILE_HIDDEV_ID))
    366     return btif_hd_get_interface();
    367 
    368   if (is_profile(profile_id, BT_PROFILE_HEALTH_ID))
    369     return btif_hl_get_interface();
    370 
    371   if (is_profile(profile_id, BT_PROFILE_SDP_CLIENT_ID))
    372     return btif_sdp_get_interface();
    373 
    374   if (is_profile(profile_id, BT_PROFILE_GATT_ID))
    375     return btif_gatt_get_interface();
    376 
    377   if (is_profile(profile_id, BT_PROFILE_AV_RC_ID))
    378     return btif_rc_get_interface();
    379 
    380   if (is_profile(profile_id, BT_PROFILE_AV_RC_CTRL_ID))
    381     return btif_rc_ctrl_get_interface();
    382 
    383   if (is_profile(profile_id, BT_TEST_INTERFACE_MCAP_ID))
    384     return stack_mcap_get_interface();
    385 
    386   if (is_profile(profile_id, BT_PROFILE_HEARING_AID_ID))
    387     return btif_hearing_aid_get_interface();
    388   return NULL;
    389 }
    390 
    391 int dut_mode_configure(uint8_t enable) {
    392   LOG_INFO(LOG_TAG, "%s", __func__);
    393 
    394   /* sanity check */
    395   if (!interface_ready()) return BT_STATUS_NOT_READY;
    396 
    397   return btif_dut_mode_configure(enable);
    398 }
    399 
    400 int dut_mode_send(uint16_t opcode, uint8_t* buf, uint8_t len) {
    401   LOG_INFO(LOG_TAG, "%s", __func__);
    402 
    403   /* sanity check */
    404   if (!interface_ready()) return BT_STATUS_NOT_READY;
    405 
    406   return btif_dut_mode_send(opcode, buf, len);
    407 }
    408 
    409 int le_test_mode(uint16_t opcode, uint8_t* buf, uint8_t len) {
    410   LOG_INFO(LOG_TAG, "%s", __func__);
    411 
    412   /* sanity check */
    413   if (!interface_ready()) return BT_STATUS_NOT_READY;
    414 
    415   return btif_le_test_mode(opcode, buf, len);
    416 }
    417 
    418 static int set_os_callouts(bt_os_callouts_t* callouts) {
    419   wakelock_set_os_callouts(callouts);
    420   return BT_STATUS_SUCCESS;
    421 }
    422 
    423 static int config_clear(void) {
    424   LOG_INFO(LOG_TAG, "%s", __func__);
    425   return btif_config_clear() ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
    426 }
    427 
    428 static bluetooth::avrcp::ServiceInterface* get_avrcp_service(void) {
    429   return bluetooth::avrcp::AvrcpService::GetServiceInterface();
    430 }
    431 
    432 EXPORT_SYMBOL bt_interface_t bluetoothInterface = {
    433     sizeof(bluetoothInterface),
    434     init,
    435     enable,
    436     disable,
    437     cleanup,
    438     get_adapter_properties,
    439     get_adapter_property,
    440     set_adapter_property,
    441     get_remote_device_properties,
    442     get_remote_device_property,
    443     set_remote_device_property,
    444     get_remote_service_record,
    445     get_remote_services,
    446     start_discovery,
    447     cancel_discovery,
    448     create_bond,
    449     create_bond_out_of_band,
    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     le_test_mode,
    459     set_os_callouts,
    460     read_energy_info,
    461     dump,
    462     dumpMetrics,
    463     config_clear,
    464     interop_database_clear,
    465     interop_database_add,
    466     get_avrcp_service,
    467 };
    468