Home | History | Annotate | Download | only in ag
      1 /******************************************************************************
      2  *
      3  *  Copyright 2003-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  *  This is the implementation of the API for the audio gateway (AG)
     22  *  subsystem of BTA, Broadcom's Bluetooth application layer for mobile
     23  *  phones.
     24  *
     25  ******************************************************************************/
     26 
     27 #include "bta_ag_api.h"
     28 #include <base/bind.h>
     29 #include <cstring>
     30 #include "bt_common.h"
     31 #include "bta_ag_int.h"
     32 #include "bta_api.h"
     33 #include "bta_sys.h"
     34 
     35 /*****************************************************************************
     36  *  Constants
     37  ****************************************************************************/
     38 
     39 static const tBTA_SYS_REG bta_ag_reg = {bta_ag_hdl_event, BTA_AgDisable};
     40 const tBTA_AG_RES_DATA tBTA_AG_RES_DATA::kEmpty = {};
     41 
     42 /*******************************************************************************
     43  *
     44  * Function         BTA_AgEnable
     45  *
     46  * Description      Enable the audio gateway service. When the enable
     47  *                  operation is complete the callback function will be
     48  *                  called with a BTA_AG_ENABLE_EVT. This function must
     49  *                  be called before other function in the AG API are
     50  *                  called.
     51  *
     52  * Returns          BTA_SUCCESS if OK, BTA_FAILURE otherwise.
     53  *
     54  ******************************************************************************/
     55 tBTA_STATUS BTA_AgEnable(tBTA_AG_CBACK* p_cback) {
     56   /* Error if AG is already enabled, or AG is in the middle of disabling. */
     57   for (const tBTA_AG_SCB& scb : bta_ag_cb.scb) {
     58     if (scb.in_use) {
     59       APPL_TRACE_ERROR("BTA_AgEnable: FAILED, AG already enabled.");
     60       return BTA_FAILURE;
     61     }
     62   }
     63   bta_sys_register(BTA_ID_AG, &bta_ag_reg);
     64   do_in_bta_thread(FROM_HERE, base::Bind(&bta_ag_api_enable, p_cback));
     65   return BTA_SUCCESS;
     66 }
     67 
     68 /*******************************************************************************
     69  *
     70  * Function         BTA_AgDisable
     71  *
     72  * Description      Disable the audio gateway service
     73  *
     74  *
     75  * Returns          void
     76  *
     77  ******************************************************************************/
     78 void BTA_AgDisable() {
     79   do_in_bta_thread(FROM_HERE, base::Bind(&bta_ag_api_disable));
     80 }
     81 
     82 /*******************************************************************************
     83  *
     84  * Function         BTA_AgRegister
     85  *
     86  * Description      Register an Audio Gateway service.
     87  *
     88  *
     89  * Returns          void
     90  *
     91  ******************************************************************************/
     92 void BTA_AgRegister(tBTA_SERVICE_MASK services, tBTA_SEC sec_mask,
     93                     tBTA_AG_FEAT features,
     94                     const std::vector<std::string>& service_names,
     95                     uint8_t app_id) {
     96   do_in_bta_thread(
     97       FROM_HERE, base::Bind(&bta_ag_api_register, services, sec_mask, features,
     98                             service_names, app_id));
     99 }
    100 
    101 /*******************************************************************************
    102  *
    103  * Function         BTA_AgDeregister
    104  *
    105  * Description      Deregister an audio gateway service.
    106  *
    107  *
    108  * Returns          void
    109  *
    110  ******************************************************************************/
    111 void BTA_AgDeregister(uint16_t handle) {
    112   do_in_bta_thread(FROM_HERE,
    113                    base::Bind(&bta_ag_sm_execute_by_handle, handle,
    114                               BTA_AG_API_DEREGISTER_EVT, tBTA_AG_DATA::kEmpty));
    115 }
    116 
    117 /*******************************************************************************
    118  *
    119  * Function         BTA_AgOpen
    120  *
    121  * Description      Opens a connection to a headset or hands-free device.
    122  *                  When connection is open callback function is called
    123  *                  with a BTA_AG_OPEN_EVT. Only the data connection is
    124  *                  opened. The audio connection is not opened.
    125  *
    126  *
    127  * Returns          void
    128  *
    129  ******************************************************************************/
    130 void BTA_AgOpen(uint16_t handle, const RawAddress& bd_addr, tBTA_SEC sec_mask) {
    131   tBTA_AG_DATA data = {};
    132   data.api_open.bd_addr = bd_addr;
    133   data.api_open.sec_mask = sec_mask;
    134   do_in_bta_thread(FROM_HERE, base::Bind(&bta_ag_sm_execute_by_handle, handle,
    135                                          BTA_AG_API_OPEN_EVT, data));
    136 }
    137 
    138 /*******************************************************************************
    139  *
    140  * Function         BTA_AgClose
    141  *
    142  * Description      Close the current connection to a headset or a handsfree
    143  *                  Any current audio connection will also be closed.
    144  *
    145  *
    146  * Returns          void
    147  *
    148  ******************************************************************************/
    149 void BTA_AgClose(uint16_t handle) {
    150   do_in_bta_thread(FROM_HERE,
    151                    base::Bind(&bta_ag_sm_execute_by_handle, handle,
    152                               BTA_AG_API_CLOSE_EVT, tBTA_AG_DATA::kEmpty));
    153 }
    154 
    155 /*******************************************************************************
    156  *
    157  * Function         BTA_AgAudioOpen
    158  *
    159  * Description      Opens an audio connection to the currently connected
    160  *                  headset or hnadsfree.
    161  *
    162  *
    163  * Returns          void
    164  *
    165  ******************************************************************************/
    166 void BTA_AgAudioOpen(uint16_t handle) {
    167   do_in_bta_thread(FROM_HERE,
    168                    base::Bind(&bta_ag_sm_execute_by_handle, handle,
    169                               BTA_AG_API_AUDIO_OPEN_EVT, tBTA_AG_DATA::kEmpty));
    170 }
    171 
    172 /*******************************************************************************
    173  *
    174  * Function         BTA_AgAudioClose
    175  *
    176  * Description      Close the currently active audio connection to a headset
    177  *                  or hnadsfree. The data connection remains open
    178  *
    179  *
    180  * Returns          void
    181  *
    182  ******************************************************************************/
    183 void BTA_AgAudioClose(uint16_t handle) {
    184   do_in_bta_thread(
    185       FROM_HERE, base::Bind(&bta_ag_sm_execute_by_handle, handle,
    186                             BTA_AG_API_AUDIO_CLOSE_EVT, tBTA_AG_DATA::kEmpty));
    187 }
    188 
    189 /*******************************************************************************
    190  *
    191  * Function         BTA_AgResult
    192  *
    193  * Description      Send an AT result code to a headset or hands-free device.
    194  *                  This function is only used when the AG parse mode is set
    195  *                  to BTA_AG_PARSE.
    196  *
    197  *
    198  * Returns          void
    199  *
    200  ******************************************************************************/
    201 void BTA_AgResult(uint16_t handle, tBTA_AG_RES result,
    202                   const tBTA_AG_RES_DATA& data) {
    203   do_in_bta_thread(FROM_HERE,
    204                    base::Bind(&bta_ag_api_result, handle, result, data));
    205 }
    206 
    207 /*******************************************************************************
    208  *
    209  * Function         BTA_AgSetCodec
    210  *
    211  * Description      Specify the codec type to be used for the subsequent
    212  *                  audio connection.
    213  *
    214  *
    215  *
    216  * Returns          void
    217  *
    218  ******************************************************************************/
    219 void BTA_AgSetCodec(uint16_t handle, tBTA_AG_PEER_CODEC codec) {
    220   tBTA_AG_DATA data = {};
    221   data.api_setcodec.codec = codec;
    222   do_in_bta_thread(FROM_HERE, base::Bind(&bta_ag_sm_execute_by_handle, handle,
    223                                          BTA_AG_API_SETCODEC_EVT, data));
    224 }
    225 
    226 void BTA_AgSetScoAllowed(bool value) {
    227   do_in_bta_thread(FROM_HERE, base::Bind(&bta_ag_set_sco_allowed, value));
    228 }
    229 
    230 void BTA_AgSetActiveDevice(const RawAddress& active_device_addr) {
    231   do_in_bta_thread(
    232       FROM_HERE, base::Bind(&bta_ag_api_set_active_device, active_device_addr));
    233 }
    234