bta_ag_int.h 17 KB

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