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  *  This is the implementation of the API for the HeaLth device profile (HL)
22  *  subsystem of BTA, Broadcom Corp's Bluetooth application layer for mobile
23  *  phones.
24  *
25  ******************************************************************************/
26 
27 #include <string.h>
28 
29 #include "bt_target.h"
30 #if (HL_INCLUDED == TRUE)
31 
32 #include "bt_common.h"
33 #include "bta_hl_api.h"
34 #include "bta_hl_int.h"
35 
36 /*****************************************************************************
37  *  Constants
38  ****************************************************************************/
39 
40 static const tBTA_SYS_REG bta_hl_reg = {bta_hl_hdl_event, BTA_HlDisable};
41 
42 /*******************************************************************************
43  *
44  * Function         BTA_HlEnable
45  *
46  * Description      Enable the HL subsystems.  This function must be
47  *                  called before any other functions in the HL API are called.
48  *                  When the enable operation is completed the callback function
49  *                  will be called with an BTA_HL_CTRL_ENABLE_CFM_EVT event.
50  *
51  * Parameters       p_cback - HL event call back function
52  *
53  * Returns          void
54  *
55  ******************************************************************************/
BTA_HlEnable(tBTA_HL_CTRL_CBACK * p_ctrl_cback)56 void BTA_HlEnable(tBTA_HL_CTRL_CBACK* p_ctrl_cback) {
57   tBTA_HL_API_ENABLE* p_buf =
58       (tBTA_HL_API_ENABLE*)osi_malloc(sizeof(tBTA_HL_API_ENABLE));
59 
60   /* register with BTA system manager */
61   bta_sys_register(BTA_ID_HL, &bta_hl_reg);
62 
63   p_buf->hdr.event = BTA_HL_API_ENABLE_EVT;
64   p_buf->p_cback = p_ctrl_cback;
65 
66   bta_sys_sendmsg(p_buf);
67 }
68 
69 /*******************************************************************************
70  *
71  * Function         BTA_HlDisable
72  *
73  * Description     Disable the HL subsystem.
74  *
75  * Returns          void
76  *
77  ******************************************************************************/
BTA_HlDisable(void)78 void BTA_HlDisable(void) {
79   BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR));
80 
81   bta_sys_deregister(BTA_ID_HL);
82   p_buf->event = BTA_HL_API_DISABLE_EVT;
83 
84   bta_sys_sendmsg(p_buf);
85 }
86 
87 /*******************************************************************************
88  *
89  * Function         BTA_HlUpdate
90  *
91  * Description      Register an HDP application
92  *
93  * Parameters       app_id        - Application ID
94  *                  p_reg_param   - non-platform related parameters for the
95  *                                  HDP application
96  *                  p_cback       - HL event callback fucntion
97  *
98  * Returns          void
99  *
100  ******************************************************************************/
BTA_HlUpdate(uint8_t app_id,tBTA_HL_REG_PARAM * p_reg_param,bool is_register,tBTA_HL_CBACK * p_cback)101 void BTA_HlUpdate(uint8_t app_id, tBTA_HL_REG_PARAM* p_reg_param,
102                   bool is_register, tBTA_HL_CBACK* p_cback) {
103   tBTA_HL_API_UPDATE* p_buf =
104       (tBTA_HL_API_UPDATE*)osi_malloc(sizeof(tBTA_HL_API_UPDATE));
105 
106   APPL_TRACE_DEBUG("%s", __func__);
107 
108   p_buf->hdr.event = BTA_HL_API_UPDATE_EVT;
109   p_buf->app_id = app_id;
110   p_buf->is_register = is_register;
111 
112   if (is_register) {
113     p_buf->sec_mask =
114         (p_reg_param->sec_mask | BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT);
115     p_buf->p_cback = p_cback;
116     if (p_reg_param->p_srv_name)
117       strlcpy(p_buf->srv_name, p_reg_param->p_srv_name, BTA_SERVICE_NAME_LEN);
118     else
119       p_buf->srv_name[0] = 0;
120 
121     if (p_reg_param->p_srv_desp)
122       strlcpy(p_buf->srv_desp, p_reg_param->p_srv_desp, BTA_SERVICE_DESP_LEN);
123     else
124       p_buf->srv_desp[0] = 0;
125 
126     if (p_reg_param->p_provider_name)
127       strlcpy(p_buf->provider_name, p_reg_param->p_provider_name,
128               BTA_PROVIDER_NAME_LEN);
129     else
130       p_buf->provider_name[0] = 0;
131   }
132 
133   bta_sys_sendmsg(p_buf);
134 }
135 
136 /*******************************************************************************
137  *
138  * Function         BTA_HlRegister
139  *
140  * Description      Register an HDP application
141  *
142  * Parameters       app_id        - Application ID
143  *                  p_reg_param   - non-platform related parameters for the
144  *                                  HDP application
145  *                  p_cback       - HL event callback fucntion
146  *
147  * Returns          void
148  *
149  ******************************************************************************/
BTA_HlRegister(uint8_t app_id,tBTA_HL_REG_PARAM * p_reg_param,tBTA_HL_CBACK * p_cback)150 void BTA_HlRegister(uint8_t app_id, tBTA_HL_REG_PARAM* p_reg_param,
151                     tBTA_HL_CBACK* p_cback) {
152   tBTA_HL_API_REGISTER* p_buf =
153       (tBTA_HL_API_REGISTER*)osi_malloc(sizeof(tBTA_HL_API_REGISTER));
154 
155   p_buf->hdr.event = BTA_HL_API_REGISTER_EVT;
156   p_buf->app_id = app_id;
157   p_buf->sec_mask =
158       (p_reg_param->sec_mask | BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT);
159   p_buf->p_cback = p_cback;
160 
161   if (p_reg_param->p_srv_name)
162     strlcpy(p_buf->srv_name, p_reg_param->p_srv_name, BTA_SERVICE_NAME_LEN);
163   else
164     p_buf->srv_name[0] = 0;
165 
166   if (p_reg_param->p_srv_desp)
167     strlcpy(p_buf->srv_desp, p_reg_param->p_srv_desp, BTA_SERVICE_DESP_LEN);
168   else
169     p_buf->srv_desp[0] = 0;
170 
171   if (p_reg_param->p_provider_name)
172     strlcpy(p_buf->provider_name, p_reg_param->p_provider_name,
173             BTA_PROVIDER_NAME_LEN);
174   else
175     p_buf->provider_name[0] = 0;
176 
177   bta_sys_sendmsg(p_buf);
178 }
179 
180 /*******************************************************************************
181  *
182  * Function         BTA_HlDeregister
183  *
184  * Description      Deregister an HDP application
185  *
186  * Parameters       app_handle - Application handle
187  *
188  * Returns           void
189  *
190  ******************************************************************************/
BTA_HlDeregister(uint8_t app_id,tBTA_HL_APP_HANDLE app_handle)191 void BTA_HlDeregister(uint8_t app_id, tBTA_HL_APP_HANDLE app_handle) {
192   tBTA_HL_API_DEREGISTER* p_buf =
193       (tBTA_HL_API_DEREGISTER*)osi_malloc(sizeof(tBTA_HL_API_DEREGISTER));
194 
195   p_buf->hdr.event = BTA_HL_API_DEREGISTER_EVT;
196   p_buf->app_id = app_id;
197   p_buf->app_handle = app_handle;
198 
199   bta_sys_sendmsg(p_buf);
200 }
201 
202 /*******************************************************************************
203  *
204  * Function         BTA_HlCchOpen
205  *
206  * Description      Open a Control channel connection with the specified BD
207  *                  address
208  *
209  * Parameters       app_handle - Application Handle
210  *                  p_open_param - parameters for opening a control channel
211  *
212  * Returns          void
213  *
214  *                  Note: The control PSM value is used to select which
215  *                  HDP insatnce should be used in case the peer device support
216  *                  multiple HDP instances. Also, if the control PSM value is
217  *                  zero then the first HDP instance is used for the control
218  *                  channel setup
219  ******************************************************************************/
BTA_HlCchOpen(uint8_t app_id,tBTA_HL_APP_HANDLE app_handle,tBTA_HL_CCH_OPEN_PARAM * p_open_param)220 void BTA_HlCchOpen(uint8_t app_id, tBTA_HL_APP_HANDLE app_handle,
221                    tBTA_HL_CCH_OPEN_PARAM* p_open_param) {
222   tBTA_HL_API_CCH_OPEN* p_buf =
223       (tBTA_HL_API_CCH_OPEN*)osi_malloc(sizeof(tBTA_HL_API_CCH_OPEN));
224 
225   p_buf->hdr.event = BTA_HL_API_CCH_OPEN_EVT;
226   p_buf->app_id = app_id;
227   p_buf->app_handle = app_handle;
228   p_buf->sec_mask =
229       (p_open_param->sec_mask | BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT);
230   p_buf->bd_addr = p_open_param->bd_addr;
231   p_buf->ctrl_psm = p_open_param->ctrl_psm;
232 
233   bta_sys_sendmsg(p_buf);
234 }
235 
236 /*******************************************************************************
237  *
238  * Function         BTA_HlCchClose
239  *
240  * Description      Close a Control channel connection with the specified MCL
241  *                  handle
242  *
243  * Parameters       mcl_handle - MCL handle
244  *
245  * Returns          void
246  *
247  ******************************************************************************/
BTA_HlCchClose(tBTA_HL_MCL_HANDLE mcl_handle)248 void BTA_HlCchClose(tBTA_HL_MCL_HANDLE mcl_handle) {
249   tBTA_HL_API_CCH_CLOSE* p_buf =
250       (tBTA_HL_API_CCH_CLOSE*)osi_malloc(sizeof(tBTA_HL_API_CCH_CLOSE));
251 
252   p_buf->hdr.event = BTA_HL_API_CCH_CLOSE_EVT;
253   p_buf->mcl_handle = mcl_handle;
254 
255   bta_sys_sendmsg(p_buf);
256 }
257 
258 /*******************************************************************************
259  *
260  * Function         BTA_HlDchOpen
261  *
262  * Description      Open a data channel connection with the specified DCH
263  *                  parameters
264  *
265  * Parameters       mcl_handle - MCL handle
266  *                  p_open_param - parameters for opening a data channel
267  *
268  * Returns          void
269  *
270  ******************************************************************************/
BTA_HlDchOpen(tBTA_HL_MCL_HANDLE mcl_handle,tBTA_HL_DCH_OPEN_PARAM * p_open_param)271 void BTA_HlDchOpen(tBTA_HL_MCL_HANDLE mcl_handle,
272                    tBTA_HL_DCH_OPEN_PARAM* p_open_param) {
273   tBTA_HL_API_DCH_OPEN* p_buf =
274       (tBTA_HL_API_DCH_OPEN*)osi_malloc(sizeof(tBTA_HL_API_DCH_OPEN));
275 
276   p_buf->hdr.event = BTA_HL_API_DCH_OPEN_EVT;
277   p_buf->mcl_handle = mcl_handle;
278   p_buf->ctrl_psm = p_open_param->ctrl_psm;
279   p_buf->local_mdep_id = p_open_param->local_mdep_id;
280   p_buf->peer_mdep_id = p_open_param->peer_mdep_id;
281   p_buf->local_cfg = p_open_param->local_cfg;
282   p_buf->sec_mask =
283       (p_open_param->sec_mask | BTA_SEC_AUTHENTICATE | BTA_SEC_ENCRYPT);
284 
285   bta_sys_sendmsg(p_buf);
286 }
287 
288 /*******************************************************************************
289  *
290  * Function         BTA_HlDchReconnect
291  *
292  * Description      Reconnect a data channel with the specified MDL_ID
293  *
294  * Parameters       mcl_handle      - MCL handle
295 *8                  p_recon_param   - parameters for reconnecting a data channel
296  *
297  * Returns          void
298  *
299  ******************************************************************************/
BTA_HlDchReconnect(tBTA_HL_MCL_HANDLE mcl_handle,tBTA_HL_DCH_RECONNECT_PARAM * p_recon_param)300 void BTA_HlDchReconnect(tBTA_HL_MCL_HANDLE mcl_handle,
301                         tBTA_HL_DCH_RECONNECT_PARAM* p_recon_param) {
302   tBTA_HL_API_DCH_RECONNECT* p_buf =
303       (tBTA_HL_API_DCH_RECONNECT*)osi_malloc(sizeof(tBTA_HL_API_DCH_RECONNECT));
304 
305   p_buf->hdr.event = BTA_HL_API_DCH_RECONNECT_EVT;
306   p_buf->mcl_handle = mcl_handle;
307   p_buf->ctrl_psm = p_recon_param->ctrl_psm;
308   p_buf->mdl_id = p_recon_param->mdl_id;
309 
310   bta_sys_sendmsg(p_buf);
311 }
312 
313 /*******************************************************************************
314  *
315  * Function         BTA_HlDchClose
316  *
317  * Description      Close a data channel with the specified MDL handle
318  *
319  * Parameters       mdl_handle  - MDL handle
320  *
321  * Returns          void
322  *
323  ******************************************************************************/
BTA_HlDchClose(tBTA_HL_MDL_HANDLE mdl_handle)324 void BTA_HlDchClose(tBTA_HL_MDL_HANDLE mdl_handle) {
325   tBTA_HL_API_DCH_CLOSE* p_buf =
326       (tBTA_HL_API_DCH_CLOSE*)osi_malloc(sizeof(tBTA_HL_API_DCH_CLOSE));
327 
328   p_buf->hdr.event = BTA_HL_API_DCH_CLOSE_EVT;
329   p_buf->mdl_handle = mdl_handle;
330 
331   bta_sys_sendmsg(p_buf);
332 }
333 
334 /*******************************************************************************
335  *
336  * Function         BTA_HlDchAbort
337  *
338  * Description      Abort the current data channel setup with the specified MCL
339  *                  handle
340  *
341  * Parameters       mcl_handle  - MCL handle
342  *
343  *
344  * Returns          void
345  *
346  ******************************************************************************/
BTA_HlDchAbort(tBTA_HL_MCL_HANDLE mcl_handle)347 void BTA_HlDchAbort(tBTA_HL_MCL_HANDLE mcl_handle) {
348   tBTA_HL_API_DCH_ABORT* p_buf =
349       (tBTA_HL_API_DCH_ABORT*)osi_malloc(sizeof(tBTA_HL_API_DCH_ABORT));
350 
351   p_buf->hdr.event = BTA_HL_API_DCH_ABORT_EVT;
352   p_buf->mcl_handle = mcl_handle;
353 
354   bta_sys_sendmsg(p_buf);
355 }
356 
357 /*******************************************************************************
358  *
359  * Function         BTA_HlSendData
360  *
361  * Description      Send an APDU to the peer device
362  *
363  * Parameters       mdl_handle  - MDL handle
364  *                  pkt_size    - size of the data packet to be sent
365  *
366  * Returns          void
367  *
368  ******************************************************************************/
BTA_HlSendData(tBTA_HL_MDL_HANDLE mdl_handle,uint16_t pkt_size)369 void BTA_HlSendData(tBTA_HL_MDL_HANDLE mdl_handle, uint16_t pkt_size) {
370   tBTA_HL_API_SEND_DATA* p_buf =
371       (tBTA_HL_API_SEND_DATA*)osi_malloc(sizeof(tBTA_HL_API_SEND_DATA));
372 
373   p_buf->hdr.event = BTA_HL_API_SEND_DATA_EVT;
374   p_buf->mdl_handle = mdl_handle;
375   p_buf->pkt_size = pkt_size;
376 
377   bta_sys_sendmsg(p_buf);
378 }
379 
380 /*******************************************************************************
381  *
382  * Function         BTA_HlDeleteMdl
383  *
384  * Description      Delete the specified MDL_ID within the specified MCL handle
385  *
386  * Parameters       mcl_handle  - MCL handle
387  *                  mdl_id      - MDL ID
388  *
389  * Returns          void
390  *
391  *                  note: If mdl_id = 0xFFFF then this means to delete all MDLs
392  *                        and this value can only be used with DeleteMdl request
393  *                        only not other requests
394  *
395  ******************************************************************************/
BTA_HlDeleteMdl(tBTA_HL_MCL_HANDLE mcl_handle,tBTA_HL_MDL_ID mdl_id)396 void BTA_HlDeleteMdl(tBTA_HL_MCL_HANDLE mcl_handle, tBTA_HL_MDL_ID mdl_id) {
397   tBTA_HL_API_DELETE_MDL* p_buf =
398       (tBTA_HL_API_DELETE_MDL*)osi_malloc(sizeof(tBTA_HL_API_DELETE_MDL));
399 
400   p_buf->hdr.event = BTA_HL_API_DELETE_MDL_EVT;
401   p_buf->mcl_handle = mcl_handle;
402   p_buf->mdl_id = mdl_id;
403 
404   bta_sys_sendmsg(p_buf);
405 }
406 
407 /*******************************************************************************
408  *
409  * Function         BTA_HlDchEchoTest
410  *
411  * Description      Initiate an echo test with the specified MCL handle
412  *
413  * Parameters       mcl_handle           - MCL handle
414 *8                  p_echo_test_param   -  parameters for echo testing
415  *
416  * Returns          void
417  *
418  ******************************************************************************/
BTA_HlDchEchoTest(tBTA_HL_MCL_HANDLE mcl_handle,tBTA_HL_DCH_ECHO_TEST_PARAM * p_echo_test_param)419 void BTA_HlDchEchoTest(tBTA_HL_MCL_HANDLE mcl_handle,
420                        tBTA_HL_DCH_ECHO_TEST_PARAM* p_echo_test_param) {
421   tBTA_HL_API_DCH_ECHO_TEST* p_buf =
422       (tBTA_HL_API_DCH_ECHO_TEST*)osi_malloc(sizeof(tBTA_HL_API_DCH_ECHO_TEST));
423 
424   p_buf->hdr.event = BTA_HL_API_DCH_ECHO_TEST_EVT;
425   p_buf->mcl_handle = mcl_handle;
426   p_buf->ctrl_psm = p_echo_test_param->ctrl_psm;
427   p_buf->local_cfg = p_echo_test_param->local_cfg;
428   p_buf->pkt_size = p_echo_test_param->pkt_size;
429 
430   bta_sys_sendmsg(p_buf);
431 }
432 
433 /*******************************************************************************
434  *
435  * Function         BTA_HlSdpQuery
436  *
437  * Description      SDP query request for the specified BD address
438  *
439  * Parameters       app_handle      - application handle
440  *                  bd_addr         - BD address
441  *
442  * Returns          void
443  *
444  ******************************************************************************/
BTA_HlSdpQuery(uint8_t app_id,tBTA_HL_APP_HANDLE app_handle,const RawAddress & bd_addr)445 void BTA_HlSdpQuery(uint8_t app_id, tBTA_HL_APP_HANDLE app_handle,
446                     const RawAddress& bd_addr) {
447   tBTA_HL_API_SDP_QUERY* p_buf =
448       (tBTA_HL_API_SDP_QUERY*)osi_malloc(sizeof(tBTA_HL_API_SDP_QUERY));
449 
450   p_buf->hdr.event = BTA_HL_API_SDP_QUERY_EVT;
451   p_buf->app_id = app_id;
452   p_buf->app_handle = app_handle;
453   p_buf->bd_addr = bd_addr;
454 
455   bta_sys_sendmsg(p_buf);
456 }
457 
458 /*******************************************************************************
459  *
460  * Function         BTA_HlDchCreateMdlRsp
461  *
462  * Description      Set the Response and configuration values for the Create MDL
463  *                  request
464  *
465  * Parameters       mcl_handle  - MCL handle
466  *                  p_rsp_param - parameters specified whether the request
467  *                                should be accepted or not and if it should be
468  *                                accepted, then it also specified the
469  *                                configuration response value
470  *
471  * Returns          void
472  *
473  ******************************************************************************/
BTA_HlDchCreateRsp(tBTA_HL_MCL_HANDLE mcl_handle,tBTA_HL_DCH_CREATE_RSP_PARAM * p_rsp_param)474 void BTA_HlDchCreateRsp(tBTA_HL_MCL_HANDLE mcl_handle,
475                         tBTA_HL_DCH_CREATE_RSP_PARAM* p_rsp_param) {
476   tBTA_HL_API_DCH_CREATE_RSP* p_buf = (tBTA_HL_API_DCH_CREATE_RSP*)osi_malloc(
477       sizeof(tBTA_HL_API_DCH_CREATE_RSP));
478 
479   p_buf->hdr.event = BTA_HL_API_DCH_CREATE_RSP_EVT;
480   p_buf->mcl_handle = mcl_handle;
481   p_buf->mdl_id = p_rsp_param->mdl_id;
482   p_buf->local_mdep_id = p_rsp_param->local_mdep_id;
483   p_buf->rsp_code = p_rsp_param->rsp_code;
484   p_buf->cfg_rsp = p_rsp_param->cfg_rsp;
485 
486   bta_sys_sendmsg(p_buf);
487 }
488 
489 #endif /* HL_INCLUDED */
490