OSDN Git Service

resolved conflicts for b8cc54d1 to mnc-dr-dev-plus-aosp
[android-x86/system-bt.git] / btif / co / bta_hl_co.c
1 /******************************************************************************
2  *
3  *  Copyright (C) 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 file for the HeaLth device profile (HL)
22  *  subsystem call-out functions.
23  *
24  ******************************************************************************/
25
26 #include <ctype.h>
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/socket.h>
33 #include <sys/types.h>
34 #include <sys/un.h>
35 #include <time.h>
36
37 #include "bta_api.h"
38 #include "bta_hl_api.h"
39 #include "bta_hl_ci.h"
40 #include "bta_hl_co.h"
41 #include "bta_sys.h"
42 #include "btif_hl.h"
43 #include "btif_util.h"
44 #include "btm_api.h"
45
46 /*****************************************************************************
47 **  Constants and Data Types
48 *****************************************************************************/
49 /**************************
50 **  Common Definitions
51 ***************************/
52
53
54
55
56 /*******************************************************************************
57 **
58 ** Function        bta_hl_co_get_num_of_mdep
59 **
60 ** Description     This function is called to get the number of MDEPs for this
61 **                 application ID
62 **
63 ** Parameters      app_id - application ID
64 **                 p_num_of_mdep (output) - number of MDEP configurations supported
65 **                                          by the application
66 **
67 ** Returns         Bloolean - TRUE success
68 **
69 *******************************************************************************/
70 BOOLEAN bta_hl_co_get_num_of_mdep(UINT8 app_id, UINT8 *p_num_of_mdep)
71 {
72     UINT8 app_idx;
73     BOOLEAN success = FALSE;
74
75     if (btif_hl_find_app_idx(app_id, &app_idx))
76     {
77         *p_num_of_mdep = p_btif_hl_cb->acb[app_idx].sup_feature.num_of_mdeps;
78         success = TRUE;
79     }
80
81
82     BTIF_TRACE_DEBUG("%s success=%d num_mdeps=%d",
83                       __FUNCTION__, success, *p_num_of_mdep );
84     return success;
85 }
86
87 /*******************************************************************************
88 **
89 ** Function        bta_hl_co_advrtise_source_sdp
90 **
91 ** Description     This function is called to find out whether the SOURCE MDEP
92 **                 configuration information should be advertize in the SDP or nopt
93 **
94 ** Parameters      app_id - application ID
95 **
96 ** Returns         Bloolean - TRUE advertise the SOURCE MDEP configuration
97 **                            information
98 **
99 *******************************************************************************/
100 BOOLEAN bta_hl_co_advrtise_source_sdp(UINT8 app_id)
101 {
102     BOOLEAN     advertize_source_sdp=FALSE;
103     UINT8       app_idx;
104
105     if (btif_hl_find_app_idx(app_id, &app_idx))
106     {
107         advertize_source_sdp = p_btif_hl_cb->acb[app_idx].sup_feature.advertize_source_sdp;
108     }
109
110
111     BTIF_TRACE_DEBUG("%s advertize_flag=%d", __FUNCTION__, advertize_source_sdp );
112
113     return advertize_source_sdp;
114 }
115 /*******************************************************************************
116 **
117 ** Function        bta_hl_co_get_mdep_config
118 **
119 ** Description     This function is called to get the supported feature
120 **                 configuration for the specified mdep index and it also assigns
121 **                 the MDEP ID for the specified mdep index
122 **
123 ** Parameters      app_id - HDP application ID
124 **                 mdep_idx - the mdep index
125 **                  mdep_counter - number of mdeps
126 **                 mdep_id  - the assigned MDEP ID for the specified medp_idx
127 **                 p_mdl_cfg (output) - pointer to the MDEP configuration
128 **
129 **
130 ** Returns         Bloolean - TRUE success
131 *******************************************************************************/
132 BOOLEAN bta_hl_co_get_mdep_config(UINT8  app_id,
133                                   UINT8 mdep_idx,
134                                   UINT8 mdep_counter,
135                                   tBTA_HL_MDEP_ID mdep_id,
136                                   tBTA_HL_MDEP_CFG *p_mdep_cfg)
137 {
138     UINT8       idx  ;
139     UINT8       app_idx;
140     BOOLEAN     success = FALSE;
141
142     BTIF_TRACE_DEBUG("%s app_id=%d mdep_idx=%d mdep_id=%d mdep_counter=%d",
143                       __FUNCTION__, app_id,mdep_idx,mdep_id,mdep_counter);
144
145     if (btif_hl_find_app_idx(app_id, &app_idx))
146     {
147         idx = mdep_idx -mdep_counter-1;
148         p_btif_hl_cb->acb[app_idx].sup_feature.mdep[idx].mdep_id = mdep_id;
149         memcpy(p_mdep_cfg,
150                &p_btif_hl_cb->acb[app_idx].sup_feature.mdep[idx].mdep_cfg,
151                sizeof(tBTA_HL_MDEP_CFG));
152
153         success = TRUE;
154     }
155
156     BTIF_TRACE_DEBUG("%s success=%d mdep_idx=%d mdep_id=%d",
157                       __FUNCTION__, success, mdep_idx, mdep_id );
158
159     return success;
160 }
161
162
163 /*******************************************************************************
164 **
165 ** Function        bta_hl_co_get_echo_config
166 **
167 ** Description     This function is called to get the echo test
168 **                 maximum APDU size configurations
169 **
170 ** Parameters      app_id - HDP application ID
171 **                 p_echo_cfg (output) - pointer to the Echo test maximum APDU size
172 **                                       configuration
173 **
174 ** Returns         Bloolean - TRUE success
175 *******************************************************************************/
176 BOOLEAN bta_hl_co_get_echo_config(UINT8  app_id,
177                                   tBTA_HL_ECHO_CFG *p_echo_cfg)
178 {
179     UINT8               app_idx;
180     BOOLEAN             success = FALSE;
181     btif_hl_app_cb_t    *p_acb;
182     tBTA_HL_SUP_FEATURE *p_sup;
183
184     BTIF_TRACE_DEBUG("%s app_id=%d",__FUNCTION__, app_id );
185
186     if (btif_hl_find_app_idx(app_id, &app_idx))
187     {
188         p_acb = BTIF_HL_GET_APP_CB_PTR(app_idx);
189         p_sup = &p_acb->sup_feature;
190         p_echo_cfg->max_rx_apdu_size = p_sup->echo_cfg.max_rx_apdu_size;
191         p_echo_cfg->max_tx_apdu_size = p_sup->echo_cfg.max_tx_apdu_size;
192         success = TRUE;
193     }
194
195     BTIF_TRACE_DEBUG("%s success=%d max tx_size=%d rx_size=%d",
196                       __FUNCTION__, success, p_echo_cfg->max_tx_apdu_size,
197                       p_echo_cfg->max_rx_apdu_size );
198
199     return success;
200 }
201
202
203 /*******************************************************************************
204 **
205 ** Function        bta_hl_co_save_mdl
206 **
207 ** Description     This function is called to save a MDL configuration item in persistent
208 **                 storage
209 **
210 ** Parameters      app_id - HDP application ID
211 **                 item_idx - the MDL configuration storage index
212 **                 p_mdl_cfg - pointer to the MDL configuration data
213 **
214 ** Returns        void
215 **
216 *******************************************************************************/
217 void bta_hl_co_save_mdl(UINT8 mdep_id, UINT8 item_idx, tBTA_HL_MDL_CFG *p_mdl_cfg )
218 {
219
220     BTIF_TRACE_DEBUG("%s mdep_id =%d, item_idx=%d active=%d mdl_id=%d time=%d",
221                       __FUNCTION__, mdep_id, item_idx,
222                       p_mdl_cfg->active,
223                       p_mdl_cfg->mdl_id,
224                       p_mdl_cfg->time);
225
226     btif_hl_save_mdl_cfg(mdep_id, item_idx, p_mdl_cfg);
227
228 }
229
230 /*******************************************************************************
231 **
232 ** Function        bta_hl_co_delete_mdl
233 **
234 ** Description     This function is called to delete a MDL configuration item in persistent
235 **                 storage
236 **
237 ** Parameters      app_id - HDP application ID
238 **                 item_idx - the MDL configuration storage index
239 **
240 ** Returns          void
241 **
242 *******************************************************************************/
243 void bta_hl_co_delete_mdl(UINT8 mdep_id, UINT8 item_idx)
244 {
245
246
247     BTIF_TRACE_DEBUG("%s mdep_id=%d, item_idx=%d", __FUNCTION__, mdep_id, item_idx);
248
249     btif_hl_delete_mdl_cfg(mdep_id, item_idx);
250
251
252 }
253
254 /*******************************************************************************
255 **
256 ** Function         bta_hl_co_get_mdl_config
257 **
258 ** Description     This function is called to get the MDL configuration
259 **                 from the persistent memory. This function shall only be called
260 *8                 once after the device is powered up
261 **
262 ** Parameters      app_id - HDP application ID
263 **                 buffer_size - the unit of the buffer size is sizeof(tBTA_HL_MDL_CFG)
264 **                 p_mdl_buf - Point to the starting location of the buffer
265 **
266 ** Returns         BOOLEAN
267 **
268 **
269 *******************************************************************************/
270 BOOLEAN bta_hl_co_load_mdl_config (UINT8 app_id, UINT8 buffer_size,
271                                    tBTA_HL_MDL_CFG *p_mdl_buf )
272 {
273     BOOLEAN result = TRUE;
274     UINT8 i;
275     tBTA_HL_MDL_CFG *p;
276
277     BTIF_TRACE_DEBUG("%s app_id=%d, num_items=%d",
278                       __FUNCTION__, app_id, buffer_size);
279
280     if (buffer_size > BTA_HL_NUM_MDL_CFGS)
281     {
282         result = FALSE;
283         return result;
284     }
285     result = btif_hl_load_mdl_config(app_id, buffer_size, p_mdl_buf);
286
287     if (result)
288     {
289         for (i=0, p=p_mdl_buf; i<buffer_size; i++, p++ )
290         {
291             if (p->active)
292             {
293                 BTIF_TRACE_DEBUG("i=%d mdl_id=0x%x dch_mode=%d local mdep_role=%d mdep_id=%d mtu=%d",
294                                   i, p->mdl_id, p->dch_mode, p->local_mdep_role, p->local_mdep_role, p->mtu);
295             }
296         }
297     }
298
299     BTIF_TRACE_DEBUG("%s success=%d num_items=%d", __FUNCTION__, result, buffer_size);
300
301     return result;
302 }
303
304 /*******************************************************************************
305 **
306 ** Function         bta_hl_co_get_tx_data
307 **
308 ** Description     Get the data to be sent
309 **
310 ** Parameters      app_id - HDP application ID
311 **                 mdl_handle - MDL handle
312 **                 buf_size - the size of the buffer
313 **                 p_buf - the buffer pointer
314 **                 evt - the evt to be passed back to the HL in the
315 **                       bta_hl_ci_get_tx_data call-in function
316 **
317 ** Returns        Void
318 **
319 *******************************************************************************/
320 void bta_hl_co_get_tx_data (UINT8 app_id, tBTA_HL_MDL_HANDLE mdl_handle,
321                             UINT16 buf_size, UINT8 *p_buf,  UINT16 evt)
322 {
323     UINT8 app_idx, mcl_idx, mdl_idx;
324     btif_hl_mdl_cb_t *p_dcb;
325     tBTA_HL_STATUS status = BTA_HL_STATUS_FAIL;
326
327     BTIF_TRACE_DEBUG("%s app_id=%d mdl_handle=0x%x buf_size=%d",
328                       __FUNCTION__, app_id, mdl_handle, buf_size);
329
330     if (btif_hl_find_mdl_idx_using_handle(mdl_handle, &app_idx, &mcl_idx, &mdl_idx))
331     {
332         p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
333
334         if (p_dcb->tx_size <= buf_size )
335         {
336             memcpy(p_buf, p_dcb->p_tx_pkt, p_dcb->tx_size);
337             btif_hl_free_buf((void **) &p_dcb->p_tx_pkt);
338             p_dcb->tx_size = 0;
339             status = BTA_HL_STATUS_OK;
340         }
341     }
342
343
344     bta_hl_ci_get_tx_data(mdl_handle,  status, evt);
345
346 }
347
348
349 /*******************************************************************************
350 **
351 ** Function        bta_hl_co_put_rx_data
352 **
353 ** Description     Put the received data
354 **
355 ** Parameters      app_id - HDP application ID
356 **                 mdl_handle - MDL handle
357 **                 data_size - the size of the data
358 **                 p_data - the data pointer
359 **                 evt - the evt to be passed back to the HL in the
360 **                       bta_hl_ci_put_rx_data call-in function
361 **
362 ** Returns        Void
363 **
364 *******************************************************************************/
365 void bta_hl_co_put_rx_data (UINT8 app_id, tBTA_HL_MDL_HANDLE mdl_handle,
366                             UINT16 data_size, UINT8 *p_data, UINT16 evt)
367 {
368     UINT8 app_idx, mcl_idx, mdl_idx;
369     btif_hl_mdl_cb_t *p_dcb;
370     tBTA_HL_STATUS status = BTA_HL_STATUS_FAIL;
371     int            r;
372     BTIF_TRACE_DEBUG("%s app_id=%d mdl_handle=0x%x data_size=%d",
373                       __FUNCTION__,app_id, mdl_handle, data_size);
374
375     if (btif_hl_find_mdl_idx_using_handle(mdl_handle, &app_idx, &mcl_idx, &mdl_idx))
376     {
377         p_dcb = BTIF_HL_GET_MDL_CB_PTR(app_idx, mcl_idx, mdl_idx);
378
379         if ((p_dcb->p_rx_pkt = (UINT8 *)btif_hl_get_buf(data_size)) != NULL)
380         {
381             memcpy(p_dcb->p_rx_pkt, p_data, data_size);
382             if (p_dcb->p_scb)
383             {
384                 BTIF_TRACE_DEBUG("app_idx=%d mcl_idx=0x%x mdl_idx=0x%x data_size=%d",
385                                   app_idx, mcl_idx, mdl_idx, data_size);
386                 r = send(p_dcb->p_scb->socket_id[1], p_dcb->p_rx_pkt, data_size, 0);
387
388                 if (r == data_size)
389                 {
390                     BTIF_TRACE_DEBUG("socket send success data_size=%d",  data_size);
391                     status = BTA_HL_STATUS_OK;
392                 }
393                 else
394                 {
395                     BTIF_TRACE_ERROR("socket send failed r=%d data_size=%d",r, data_size);
396                 }
397
398
399             }
400             btif_hl_free_buf((void **) &p_dcb->p_rx_pkt);
401         }
402     }
403
404     bta_hl_ci_put_rx_data(mdl_handle,  status, evt);
405 }
406
407
408 /*******************************************************************************
409 **
410 ** Function         bta_hl_co_get_tx_data
411 **
412 ** Description     Get the Echo data to be sent
413 **
414 ** Parameters      app_id - HDP application ID
415 **                 mcl_handle - MCL handle
416 **                 buf_size - the size of the buffer
417 **                 p_buf - the buffer pointer
418 **                 evt - the evt to be passed back to the HL in the
419 **                       bta_hl_ci_get_tx_data call-in function
420 **
421 ** Returns        Void
422 **
423 *******************************************************************************/
424 void bta_hl_co_get_echo_data (UINT8 app_id, tBTA_HL_MCL_HANDLE mcl_handle,
425                               UINT16 buf_size, UINT8 *p_buf,  UINT16 evt)
426 {
427     tBTA_HL_STATUS status = BTA_HL_STATUS_FAIL;
428     UNUSED(app_id);
429     UNUSED(buf_size);
430     UNUSED(p_buf);
431
432     BTIF_TRACE_ERROR("%s not supported",__FUNCTION__);
433     bta_hl_ci_get_echo_data(mcl_handle,  status, evt);
434 }
435
436
437 /*******************************************************************************
438 **
439 ** Function        bta_hl_co_put_echo_data
440 **
441 ** Description     Put the received loopback echo data
442 **
443 ** Parameters      app_id - HDP application ID
444 **                 mcl_handle - MCL handle
445 **                 data_size - the size of the data
446 **                 p_data - the data pointer
447 **                 evt - the evt to be passed back to the HL in the
448 **                       bta_hl_ci_put_echo_data call-in function
449 **
450 ** Returns        Void
451 **
452 *******************************************************************************/
453 void bta_hl_co_put_echo_data (UINT8 app_id, tBTA_HL_MCL_HANDLE mcl_handle,
454                               UINT16 data_size, UINT8 *p_data, UINT16 evt)
455 {
456     tBTA_HL_STATUS status = BTA_HL_STATUS_FAIL;
457     UNUSED(app_id);
458     UNUSED(data_size);
459     UNUSED(p_data);
460
461     BTIF_TRACE_ERROR("%s not supported",__FUNCTION__);
462     bta_hl_ci_put_echo_data(mcl_handle,  status, evt);
463 }
464