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 private interface file for the BTA audio gateway.
     22  *
     23  ******************************************************************************/
     24 #ifndef BTA_AG_INT_H
     25 #define BTA_AG_INT_H
     26 
     27 #include "bta_ag_api.h"
     28 #include "bta_ag_at.h"
     29 #include "bta_api.h"
     30 #include "bta_sys.h"
     31 
     32 /*****************************************************************************
     33  *  Constants
     34  ****************************************************************************/
     35 /* Time to wait for retry in case of collision */
     36 #ifndef BTA_AG_COLLISION_TIMEOUT_MS
     37 #define BTA_AG_COLLISION_TIMEOUT_MS (2 * 1000) /* 2 seconds */
     38 #endif
     39 
     40 /* RFCOMM MTU SIZE */
     41 #define BTA_AG_MTU 256
     42 
     43 /* Max number of peer and local HF indicators */
     44 #define BTA_AG_MAX_NUM_PEER_HF_IND 20
     45 #define BTA_AG_MAX_NUM_LOCAL_HF_IND 4
     46 
     47 /* Internal profile indexes */
     48 #define BTA_AG_HSP 0     /* index for HSP */
     49 #define BTA_AG_HFP 1     /* index for HFP */
     50 #define BTA_AG_NUM_IDX 2 /* number of profile indexes */
     51 
     52 /* profile role for connection */
     53 #define BTA_AG_ACP 0 /* accepted connection */
     54 #define BTA_AG_INT 1 /* initiating connection */
     55 
     56 #define BTA_AG_SDP_FEAT_SPEC                                \
     57   (BTA_AG_FEAT_3WAY | BTA_AG_FEAT_ECNR | BTA_AG_FEAT_VREC | \
     58    BTA_AG_FEAT_INBAND | BTA_AG_FEAT_VTAG)
     59 
     60 enum {
     61   /* these events are handled by the state machine */
     62   BTA_AG_API_REGISTER_EVT = BTA_SYS_EVT_START(BTA_ID_AG),
     63   BTA_AG_API_DEREGISTER_EVT,
     64   BTA_AG_API_OPEN_EVT,
     65   BTA_AG_API_CLOSE_EVT,
     66   BTA_AG_API_AUDIO_OPEN_EVT,
     67   BTA_AG_API_AUDIO_CLOSE_EVT,
     68   BTA_AG_API_RESULT_EVT,
     69   BTA_AG_API_SETCODEC_EVT,
     70   BTA_AG_RFC_OPEN_EVT,
     71   BTA_AG_RFC_CLOSE_EVT,
     72   BTA_AG_RFC_SRV_CLOSE_EVT,
     73   BTA_AG_RFC_DATA_EVT,
     74   BTA_AG_SCO_OPEN_EVT,
     75   BTA_AG_SCO_CLOSE_EVT,
     76   BTA_AG_DISC_ACP_RES_EVT,
     77   BTA_AG_DISC_INT_RES_EVT,
     78   BTA_AG_DISC_OK_EVT,
     79   BTA_AG_DISC_FAIL_EVT,
     80   BTA_AG_RING_TIMEOUT_EVT,
     81   BTA_AG_SVC_TIMEOUT_EVT,
     82   BTA_AG_COLLISION_EVT,
     83   BTA_AG_MAX_EVT,
     84 };
     85 
     86 /* Actions to perform after a SCO event */
     87 enum {
     88   BTA_AG_POST_SCO_NONE,      /* no action */
     89   BTA_AG_POST_SCO_CLOSE_RFC, /* close RFCOMM channel after SCO closes */
     90   BTA_AG_POST_SCO_RING,      /* send RING result code after SCO opens */
     91   BTA_AG_POST_SCO_CALL_CONN, /* send call indicators after SCO opens/closes */
     92   BTA_AG_POST_SCO_CALL_ORIG, /* send call indicators after SCO closes */
     93   BTA_AG_POST_SCO_CALL_END,  /* send call indicators after SCO closes */
     94   BTA_AG_POST_SCO_CALL_END_INCALL /* send call indicators for end call &
     95                                      incoming call after SCO closes */
     96 };
     97 
     98 /* sco states */
     99 enum {
    100   BTA_AG_SCO_SHUTDOWN_ST,   /* no sco listening, all sco connections closed */
    101   BTA_AG_SCO_LISTEN_ST,     /* sco listening */
    102   BTA_AG_SCO_CODEC_ST,      /* sco codec negotiation */
    103   BTA_AG_SCO_OPENING_ST,    /* sco connection opening */
    104   BTA_AG_SCO_OPEN_CL_ST,    /* opening sco connection being closed */
    105   BTA_AG_SCO_OPEN_XFER_ST,  /* opening sco connection being transferred */
    106   BTA_AG_SCO_OPEN_ST,       /* sco open */
    107   BTA_AG_SCO_CLOSING_ST,    /* sco closing */
    108   BTA_AG_SCO_CLOSE_OP_ST,   /* closing sco being opened */
    109   BTA_AG_SCO_CLOSE_XFER_ST, /* closing sco being transferred */
    110   BTA_AG_SCO_SHUTTING_ST    /* sco shutting down */
    111 };
    112 
    113 /*****************************************************************************
    114  *  Data types
    115  ****************************************************************************/
    116 
    117 /* data type for BTA_AG_API_REGISTER_EVT */
    118 typedef struct {
    119   char p_name[2][BTA_SERVICE_NAME_LEN + 1];
    120   tBTA_SERVICE_MASK services;
    121   tBTA_SEC sec_mask;
    122   tBTA_AG_FEAT features;
    123   uint8_t app_id;
    124 } tBTA_AG_API_REGISTER;
    125 
    126 /* data type for BTA_AG_API_OPEN_EVT */
    127 typedef struct {
    128   RawAddress bd_addr;
    129   tBTA_SEC sec_mask;
    130 } tBTA_AG_API_OPEN;
    131 
    132 /* data type for BTA_AG_API_RESULT_EVT */
    133 typedef struct {
    134   tBTA_AG_RES result;
    135   tBTA_AG_RES_DATA data;
    136 } tBTA_AG_API_RESULT;
    137 
    138 /* data type for BTA_AG_API_SETCODEC_EVT */
    139 typedef struct {
    140   tBTA_AG_PEER_CODEC codec;
    141 } tBTA_AG_API_SETCODEC;
    142 
    143 /* data type for BTA_AG_DISC_RESULT_EVT */
    144 typedef struct {
    145   uint16_t status;
    146 } tBTA_AG_DISC_RESULT;
    147 
    148 /* data type for RFCOMM events */
    149 typedef struct {
    150   uint16_t port_handle;
    151 } tBTA_AG_RFC;
    152 
    153 /* union of all event datatypes */
    154 union tBTA_AG_DATA {
    155   tBTA_AG_API_REGISTER api_register;
    156   tBTA_AG_API_OPEN api_open;
    157   tBTA_AG_API_RESULT api_result;
    158   tBTA_AG_API_SETCODEC api_setcodec;
    159   tBTA_AG_DISC_RESULT disc_result;
    160   tBTA_AG_RFC rfc;
    161   static const tBTA_AG_DATA kEmpty;
    162 
    163   /**
    164    * Check if two tBTA_AG_DATA are equal in memory
    165    *
    166    * @param rhs other tBTA_AG_DATA
    167    * @return true if both unions are equal in memory
    168    */
    169   bool operator==(const tBTA_AG_DATA& rhs) const {
    170     return (std::memcmp(this, &rhs, sizeof(tBTA_AG_DATA)) == 0);
    171   }
    172 
    173   /**
    174    * Check if this union is empty by comparing it to the kEmpty constant
    175    *
    176    * @return true if this union is empty
    177    */
    178   bool IsEmpty() const { return *this == kEmpty; }
    179 };
    180 
    181 /* type for each profile */
    182 typedef struct {
    183   uint32_t sdp_handle;
    184   uint8_t scn;
    185 } tBTA_AG_PROFILE;
    186 
    187 typedef enum {
    188   BTA_AG_SCO_MSBC_SETTINGS_T2 = 0, /* preferred/default when codec is mSBC */
    189   BTA_AG_SCO_MSBC_SETTINGS_T1,
    190 } tBTA_AG_SCO_MSBC_SETTINGS;
    191 
    192 /* type for each service control block */
    193 struct tBTA_AG_SCB {
    194   char clip[BTA_AG_AT_MAX_LEN + 1];     /* number string used for CLIP */
    195   uint16_t serv_handle[BTA_AG_NUM_IDX]; /* RFCOMM server handles */
    196   tBTA_AG_AT_CB at_cb;                  /* AT command interpreter */
    197   RawAddress peer_addr;                 /* peer bd address */
    198   tSDP_DISCOVERY_DB* p_disc_db;         /* pointer to discovery database */
    199   tBTA_SERVICE_MASK reg_services;       /* services specified in register API */
    200   tBTA_SERVICE_MASK open_services;      /* services specified in open API */
    201   uint16_t conn_handle;                 /* RFCOMM handle of connected service */
    202   tBTA_SEC serv_sec_mask;               /* server security mask */
    203   tBTA_SEC cli_sec_mask;                /* client security mask */
    204   tBTA_AG_FEAT features;                /* features registered by application */
    205   tBTA_AG_PEER_FEAT peer_features;      /* peer device features */
    206   uint16_t peer_sdp_features;           /* peer device SDP features */
    207   uint16_t peer_version;                /* profile version of peer device */
    208   uint16_t hsp_version;                 /* HSP profile version before SDP */
    209   uint16_t sco_idx;                     /* SCO handle */
    210   bool in_use;                          /* scb in use */
    211   bool dealloc;                         /* true if service shutting down */
    212   bool clip_enabled;        /* set to true if HF enables CLIP reporting */
    213   bool ccwa_enabled;        /* set to true if HF enables CCWA reporting */
    214   bool cmer_enabled;        /* set to true if HF enables CMER reporting */
    215   bool cmee_enabled;        /* set to true if HF enables CME ERROR reporting */
    216   bool inband_enabled;      /* set to true if inband ring enabled */
    217   bool svc_conn;            /* set to true when service level connection up */
    218   uint8_t state;            /* state machine state */
    219   uint8_t conn_service;     /* connected service */
    220   uint8_t peer_scn;         /* peer scn */
    221   uint8_t app_id;           /* application id */
    222   uint8_t role;             /* initiator/acceptor role */
    223   uint8_t post_sco;         /* action to perform after sco event */
    224   uint8_t call_ind;         /* CIEV call indicator value */
    225   uint8_t callsetup_ind;    /* CIEV callsetup indicator value */
    226   uint8_t service_ind;      /* CIEV service indicator value */
    227   uint8_t signal_ind;       /* CIEV signal indicator value */
    228   uint8_t roam_ind;         /* CIEV roam indicator value */
    229   uint8_t battchg_ind;      /* CIEV battery charge indicator value */
    230   uint8_t callheld_ind;     /* CIEV call held indicator value */
    231   uint32_t bia_masked_out;  /* indicators HF does not want us to send */
    232   alarm_t* collision_timer;
    233   alarm_t* ring_timer;
    234   alarm_t* codec_negotiation_timer;
    235   bool received_at_bac; /* indicate AT+BAC is received at least once */
    236   tBTA_AG_PEER_CODEC peer_codecs; /* codecs for eSCO supported by the peer */
    237   tBTA_AG_PEER_CODEC sco_codec;   /* codec to be used for eSCO connection */
    238   tBTA_AG_PEER_CODEC
    239       inuse_codec;     /* codec being used for the current SCO connection */
    240   bool codec_updated;  /* set to true whenever the app updates codec type */
    241   bool codec_fallback; /* If sco nego fails for mSBC, fallback to CVSD */
    242   tBTA_AG_SCO_MSBC_SETTINGS
    243       codec_msbc_settings; /* settings to be used for the impending eSCO */
    244 
    245   tBTA_AG_HF_IND
    246       peer_hf_indicators[BTA_AG_MAX_NUM_PEER_HF_IND]; /* Peer supported
    247                                                   HF indicators */
    248   tBTA_AG_HF_IND
    249       local_hf_indicators[BTA_AG_MAX_NUM_LOCAL_HF_IND]; /* Local supported
    250                                                     HF indicators */
    251 };
    252 
    253 /* type for sco data */
    254 typedef struct {
    255   tBTM_ESCO_CONN_REQ_EVT_DATA conn_data; /* SCO data for pending conn request */
    256   tBTA_AG_SCB* p_curr_scb;  /* SCB associated with SCO connection */
    257   tBTA_AG_SCB* p_xfer_scb;  /* SCB associated with SCO transfer */
    258   uint16_t cur_idx;         /* SCO handle */
    259   uint8_t state;            /* SCO state variable */
    260   bool is_local;            /* SCO connection initiated locally or remotely */
    261 } tBTA_AG_SCO_CB;
    262 
    263 /* type for AG control block */
    264 typedef struct {
    265   tBTA_AG_SCB scb[BTA_AG_MAX_NUM_CLIENTS]; /* service control blocks */
    266   tBTA_AG_PROFILE profile[BTA_AG_NUM_IDX]; /* profile-specific data */
    267   tBTA_AG_SCO_CB sco;                      /* SCO data */
    268   tBTA_AG_CBACK* p_cback;                  /* application callback */
    269 } tBTA_AG_CB;
    270 
    271 /*****************************************************************************
    272  *  Global data
    273  ****************************************************************************/
    274 
    275 /* constant lookup tables */
    276 extern const uint16_t bta_ag_uuid[BTA_AG_NUM_IDX];
    277 extern const uint8_t bta_ag_sec_id[BTA_AG_NUM_IDX];
    278 extern const tBTA_AG_AT_CMD* bta_ag_at_tbl[BTA_AG_NUM_IDX];
    279 
    280 /* control block declaration */
    281 extern tBTA_AG_CB bta_ag_cb;
    282 
    283 /* config struct */
    284 extern const tBTA_AG_CFG* p_bta_ag_cfg;
    285 extern const tBTA_AG_HF_IND bta_ag_local_hf_ind_cfg[];
    286 
    287 /*****************************************************************************
    288  *  Function prototypes
    289  ****************************************************************************/
    290 bool bta_ag_hdl_event(BT_HDR* p_msg);
    291 
    292 /* API functions */
    293 extern void bta_ag_api_enable(tBTA_AG_CBACK* p_cback);
    294 extern void bta_ag_api_disable();
    295 extern void bta_ag_api_set_active_device(const RawAddress& new_active_device);
    296 extern void bta_ag_api_register(tBTA_SERVICE_MASK services, tBTA_SEC sec_mask,
    297                                 tBTA_AG_FEAT features,
    298                                 const std::vector<std::string>& service_names,
    299                                 uint8_t app_id);
    300 extern void bta_ag_api_result(uint16_t handle, tBTA_AG_RES result,
    301                               const tBTA_AG_RES_DATA& result_data);
    302 
    303 /* main functions */
    304 extern void bta_ag_scb_dealloc(tBTA_AG_SCB* p_scb);
    305 extern uint16_t bta_ag_scb_to_idx(tBTA_AG_SCB* p_scb);
    306 extern tBTA_AG_SCB* bta_ag_scb_by_idx(uint16_t idx);
    307 extern uint8_t bta_ag_service_to_idx(tBTA_SERVICE_MASK services);
    308 extern uint16_t bta_ag_idx_by_bdaddr(const RawAddress* peer_addr);
    309 extern bool bta_ag_other_scb_open(tBTA_AG_SCB* p_curr_scb);
    310 extern bool bta_ag_scb_open(tBTA_AG_SCB* p_curr_scb);
    311 extern void bta_ag_sm_execute(tBTA_AG_SCB* p_scb, uint16_t event,
    312                               const tBTA_AG_DATA& data);
    313 extern void bta_ag_sm_execute_by_handle(uint16_t handle, uint16_t event,
    314                                         const tBTA_AG_DATA& data);
    315 extern void bta_ag_collision_cback(tBTA_SYS_CONN_STATUS status, uint8_t id,
    316                                    uint8_t app_id, const RawAddress& peer_addr);
    317 extern void bta_ag_resume_open(tBTA_AG_SCB* p_scb);
    318 
    319 /* SDP functions */
    320 extern bool bta_ag_add_record(uint16_t service_uuid, const char* p_service_name,
    321                               uint8_t scn, tBTA_AG_FEAT features,
    322                               uint32_t sdp_handle);
    323 extern void bta_ag_create_records(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
    324 extern void bta_ag_del_records(tBTA_AG_SCB* p_scb);
    325 extern bool bta_ag_sdp_find_attr(tBTA_AG_SCB* p_scb, tBTA_SERVICE_MASK service);
    326 extern void bta_ag_do_disc(tBTA_AG_SCB* p_scb, tBTA_SERVICE_MASK service);
    327 extern void bta_ag_free_db(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
    328 
    329 /* RFCOMM functions */
    330 extern void bta_ag_start_servers(tBTA_AG_SCB* p_scb,
    331                                  tBTA_SERVICE_MASK services);
    332 extern void bta_ag_close_servers(tBTA_AG_SCB* p_scb,
    333                                  tBTA_SERVICE_MASK services);
    334 extern bool bta_ag_is_server_closed(tBTA_AG_SCB* p_scb);
    335 extern void bta_ag_rfc_do_close(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
    336 extern void bta_ag_rfc_do_open(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
    337 
    338 /* SCO functions */
    339 extern bool bta_ag_sco_is_active_device(const RawAddress& bd_addr);
    340 extern bool bta_ag_sco_is_open(tBTA_AG_SCB* p_scb);
    341 extern bool bta_ag_sco_is_opening(tBTA_AG_SCB* p_scb);
    342 extern void bta_ag_sco_conn_rsp(tBTA_AG_SCB* p_scb,
    343                                 tBTM_ESCO_CONN_REQ_EVT_DATA* data);
    344 
    345 /* AT command functions */
    346 extern void bta_ag_at_hsp_cback(tBTA_AG_SCB* p_scb, uint16_t cmd,
    347                                 uint8_t arg_type, char* p_arg, int16_t int_arg);
    348 extern void bta_ag_at_hfp_cback(tBTA_AG_SCB* p_scb, uint16_t cmd,
    349                                 uint8_t arg_type, char* p_arg, int16_t int_arg);
    350 extern void bta_ag_at_err_cback(tBTA_AG_SCB* p_scb, bool unknown,
    351                                 const char* p_arg);
    352 extern bool bta_ag_inband_enabled(tBTA_AG_SCB* p_scb);
    353 extern void bta_ag_send_call_inds(tBTA_AG_SCB* p_scb, tBTA_AG_RES result);
    354 
    355 /* Action functions */
    356 extern void bta_ag_register(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
    357 extern void bta_ag_deregister(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
    358 extern void bta_ag_start_dereg(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
    359 extern void bta_ag_start_close(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
    360 extern void bta_ag_start_open(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
    361 extern void bta_ag_disc_int_res(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
    362 extern void bta_ag_disc_acp_res(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
    363 extern void bta_ag_disc_fail(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
    364 extern void bta_ag_open_fail(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
    365 extern void bta_ag_rfc_fail(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
    366 extern void bta_ag_rfc_close(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
    367 extern void bta_ag_rfc_open(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
    368 extern void bta_ag_rfc_acp_open(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
    369 extern void bta_ag_rfc_data(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
    370 extern void bta_ag_sco_listen(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
    371 extern void bta_ag_sco_open(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
    372 extern void bta_ag_sco_close(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
    373 extern void bta_ag_sco_shutdown(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
    374 extern void bta_ag_sco_conn_open(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
    375 extern void bta_ag_sco_conn_close(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
    376 extern void bta_ag_post_sco_open(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
    377 extern void bta_ag_post_sco_close(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
    378 extern void bta_ag_svc_conn_open(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
    379 extern void bta_ag_result(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
    380 extern void bta_ag_setcodec(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
    381 extern void bta_ag_send_ring(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data);
    382 extern void bta_ag_handle_collision(tBTA_AG_SCB* p_scb,
    383                                     const tBTA_AG_DATA& data);
    384 
    385 /* Internal utility functions */
    386 extern void bta_ag_sco_codec_nego(tBTA_AG_SCB* p_scb, bool result);
    387 extern void bta_ag_codec_negotiate(tBTA_AG_SCB* p_scb);
    388 extern void bta_ag_send_bcs(tBTA_AG_SCB* p_scb);
    389 extern void bta_ag_set_sco_allowed(bool value);
    390 extern const RawAddress& bta_ag_get_active_device();
    391 extern void bta_clear_active_device();
    392 
    393 #endif /* BTA_AG_INT_H */
    394