summaryrefslogtreecommitdiff
path: root/system/bta/hh/bta_hh_api.cc
blob: 6cc4612ab4b43e1d628d34802a6defb0a5df9a89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/******************************************************************************
 *
 *  Copyright 2005-2012 Broadcom Corporation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/

/******************************************************************************
 *
 *  This file contains the HID HOST API in the subsystem of BTA.
 *
 ******************************************************************************/

#define LOG_TAG "bt_bta_hh"

#include <cstdint>

#include "bt_target.h"  // Must be first to define build configuration
#include "bta/hh/bta_hh_int.h"
#include "bta/sys/bta_sys.h"
#include "osi/include/allocator.h"
#include "osi/include/osi.h"  // UNUSED_ATTR
#include "stack/include/bt_hdr.h"
#include "stack/include/btu.h"
#include "types/raw_address.h"

/*****************************************************************************
 *  Constants
 ****************************************************************************/

static const tBTA_SYS_REG bta_hh_reg = {bta_hh_hdl_event, BTA_HhDisable};

/*******************************************************************************
 *
 * Function         BTA_HhEnable
 *
 * Description      Enable the HID host.  This function must be called before
 *                  any other functions in the HID host API are called. When the
 *                  enable operation is complete the callback function will be
 *                  called with BTA_HH_ENABLE_EVT.
 *
 *
 * Returns          void
 *
 ******************************************************************************/
void BTA_HhEnable(tBTA_HH_CBACK* p_cback) {
  /* register with BTA system manager */
  bta_sys_register(BTA_ID_HH, &bta_hh_reg);

  post_on_bt_main([p_cback]() {
    tBTA_HH_DATA data = {
        .api_enable =
            {
                .hdr =
                    {
                        .event = BTA_HH_API_ENABLE_EVT,
                    },
                .p_cback = p_cback,
            },
    };
    bta_hh_api_enable(&data);
  });
}

/*******************************************************************************
 *
 * Function         BTA_HhDisable
 *
 * Description      Disable the HID host. If the server is currently
 *                  connected, the connection will be closed.
 *
 * Returns          void
 *
 ******************************************************************************/
void BTA_HhDisable(void) {
  bta_sys_deregister(BTA_ID_HH);

  post_on_bt_main([]() { bta_hh_api_disable(); });
}

/*******************************************************************************
 *
 * Function         BTA_HhClose
 *
 * Description      Disconnect a connection.
 *
 * Returns          void
 *
 ******************************************************************************/
void BTA_HhClose(uint8_t dev_handle) {
  BT_HDR* p_buf = (BT_HDR*)osi_calloc(sizeof(BT_HDR));

  p_buf->event = BTA_HH_API_CLOSE_EVT;
  p_buf->layer_specific = (uint16_t)dev_handle;

  bta_sys_sendmsg(p_buf);
}

/*******************************************************************************
 *
 * Function         BTA_HhOpen
 *
 * Description      Connect to a device of specified BD address in specified
 *                  protocol mode and security level.
 *
 * Returns          void
 *
 ******************************************************************************/
void BTA_HhOpen(const RawAddress& dev_bda) {
  tBTA_HH_API_CONN* p_buf =
      (tBTA_HH_API_CONN*)osi_calloc(sizeof(tBTA_HH_API_CONN));
  tBTA_HH_PROTO_MODE mode = BTA_HH_PROTO_RPT_MODE;

  p_buf->hdr.event = BTA_HH_API_OPEN_EVT;
  p_buf->hdr.layer_specific = BTA_HH_INVALID_HANDLE;
  p_buf->mode = mode;
  p_buf->bd_addr = dev_bda;

  bta_sys_sendmsg((void*)p_buf);
}

/*******************************************************************************
 *
 * Function  bta_hh_snd_write_dev
 *
 ******************************************************************************/
static void bta_hh_snd_write_dev(uint8_t dev_handle, uint8_t t_type,
                                 uint8_t param, uint16_t data, uint8_t rpt_id,
                                 BT_HDR* p_data) {
  tBTA_HH_CMD_DATA* p_buf =
      (tBTA_HH_CMD_DATA*)osi_calloc(sizeof(tBTA_HH_CMD_DATA));

  p_buf->hdr.event = BTA_HH_API_WRITE_DEV_EVT;
  p_buf->hdr.layer_specific = (uint16_t)dev_handle;
  p_buf->t_type = t_type;
  p_buf->data = data;
  p_buf->param = param;
  p_buf->p_data = p_data;
  p_buf->rpt_id = rpt_id;

  bta_sys_sendmsg(p_buf);
}

/*******************************************************************************
 *
 * Function         BTA_HhSetReport
 *
 * Description      send SET_REPORT to device.
 *
 * Parameter        dev_handle: device handle
 *                  r_type:     report type, could be BTA_HH_RPTT_OUTPUT or
 *                              BTA_HH_RPTT_FEATURE.
 * Returns          void
 *
 ******************************************************************************/
void BTA_HhSetReport(uint8_t dev_handle, tBTA_HH_RPT_TYPE r_type,
                     BT_HDR* p_data) {
  bta_hh_snd_write_dev(dev_handle, HID_TRANS_SET_REPORT, r_type, 0, 0, p_data);
}
/*******************************************************************************
 *
 * Function         BTA_HhGetReport
 *
 * Description      Send a GET_REPORT to HID device.
 *
 * Returns          void
 *
 ******************************************************************************/
void BTA_HhGetReport(uint8_t dev_handle, tBTA_HH_RPT_TYPE r_type,
                     uint8_t rpt_id, uint16_t buf_size) {
  uint8_t param = (buf_size) ? (r_type | 0x08) : r_type;

  bta_hh_snd_write_dev(dev_handle, HID_TRANS_GET_REPORT, param, buf_size,
                       rpt_id, NULL);
}
/*******************************************************************************
 *
 * Function         BTA_HhSetProtoMode
 *
 * Description      This function set the protocol mode at specified HID handle
 *
 * Returns          void
 *
 ******************************************************************************/
void BTA_HhSetProtoMode(uint8_t dev_handle, tBTA_HH_PROTO_MODE p_type) {
  bta_hh_snd_write_dev(dev_handle, HID_TRANS_SET_PROTOCOL, (uint8_t)p_type, 0,
                       0, NULL);
}
/*******************************************************************************
 *
 * Function         BTA_HhGetProtoMode
 *
 * Description      This function get protocol mode information.
 *
 * Returns          void
 *
 ******************************************************************************/
void BTA_HhGetProtoMode(uint8_t dev_handle) {
  bta_hh_snd_write_dev(dev_handle, HID_TRANS_GET_PROTOCOL, 0, 0, 0, NULL);
}
/*******************************************************************************
 *
 * Function         BTA_HhSetIdle
 *
 * Description      send SET_IDLE to device.
 *
 * Returns          void
 *
 ******************************************************************************/
void BTA_HhSetIdle(uint8_t dev_handle, uint16_t idle_rate) {
  bta_hh_snd_write_dev(dev_handle, HID_TRANS_SET_IDLE, 0, idle_rate, 0, NULL);
}

/*******************************************************************************
 *
 * Function         BTA_HhGetIdle
 *
 * Description      Send a GET_IDLE from HID device.
 *
 * Returns          void
 *
 ******************************************************************************/
void BTA_HhGetIdle(uint8_t dev_handle) {
  bta_hh_snd_write_dev(dev_handle, HID_TRANS_GET_IDLE, 0, 0, 0, NULL);
}
/*******************************************************************************
 *
 * Function         BTA_HhSendCtrl
 *
 * Description      Send a control command to HID device.
 *
 * Returns          void
 *
 ******************************************************************************/
void BTA_HhSendCtrl(uint8_t dev_handle, tBTA_HH_TRANS_CTRL_TYPE c_type) {
  bta_hh_snd_write_dev(dev_handle, HID_TRANS_CONTROL, (uint8_t)c_type, 0, 0,
                       NULL);
}
/*******************************************************************************
 *
 * Function         BTA_HhSendData
 *
 * Description      This function send DATA transaction to HID device.
 *
 * Parameter        dev_handle: device handle
 *                  dev_bda: remote device address
 *                  p_data: data to be sent in the DATA transaction; or
 *                          the data to be write into the Output Report of a LE
 *                          HID device. The report is identified the report ID
 *                          which is the value of the byte
 *                          (uint8_t *)(p_buf + 1) + *p_buf->offset.
 *                          p_data->layer_specific needs to be set to the report
 *                          type. It can be OUTPUT report, or FEATURE report.
 *
 * Returns          void
 *
 ******************************************************************************/
void BTA_HhSendData(uint8_t dev_handle, UNUSED_ATTR const RawAddress& dev_bda,
                    BT_HDR* p_data) {
  if (p_data->layer_specific != BTA_HH_RPTT_OUTPUT) {
    APPL_TRACE_ERROR(
        "ERROR! Wrong report type! Write Command only valid for output "
        "report!");
    return;
  }
  bta_hh_snd_write_dev(dev_handle, HID_TRANS_DATA,
                       (uint8_t)p_data->layer_specific, 0, 0, p_data);
}

/*******************************************************************************
 *
 * Function         BTA_HhGetDscpInfo
 *
 * Description      Get HID device report descriptor
 *
 * Returns          void
 *
 ******************************************************************************/
void BTA_HhGetDscpInfo(uint8_t dev_handle) {
  BT_HDR* p_buf = (BT_HDR*)osi_calloc(sizeof(BT_HDR));

  p_buf->event = BTA_HH_API_GET_DSCP_EVT;
  p_buf->layer_specific = (uint16_t)dev_handle;

  bta_sys_sendmsg(p_buf);
}

/*******************************************************************************
 *
 * Function         BTA_HhAddDev
 *
 * Description      Add a virtually cabled device into HID-Host device list
 *                  to manage and assign a device handle for future API call,
 *                  host applciation call this API at start-up to initialize its
 *                  virtually cabled devices.
 *
 * Returns          void
 *
 ******************************************************************************/
void BTA_HhAddDev(const RawAddress& bda, tBTA_HH_ATTR_MASK attr_mask,
                  uint8_t sub_class, uint8_t app_id,
                  tBTA_HH_DEV_DSCP_INFO dscp_info) {
  size_t len = sizeof(tBTA_HH_MAINT_DEV) + dscp_info.descriptor.dl_len;
  tBTA_HH_MAINT_DEV* p_buf = (tBTA_HH_MAINT_DEV*)osi_calloc(len);

  p_buf->hdr.event = BTA_HH_API_MAINT_DEV_EVT;
  p_buf->sub_event = BTA_HH_ADD_DEV_EVT;
  p_buf->hdr.layer_specific = BTA_HH_INVALID_HANDLE;

  p_buf->attr_mask = (uint16_t)attr_mask;
  p_buf->sub_class = sub_class;
  p_buf->app_id = app_id;
  p_buf->bda = bda;

  memcpy(&p_buf->dscp_info, &dscp_info, sizeof(tBTA_HH_DEV_DSCP_INFO));
  if (dscp_info.descriptor.dl_len != 0 && dscp_info.descriptor.dsc_list) {
    p_buf->dscp_info.descriptor.dl_len = dscp_info.descriptor.dl_len;
    p_buf->dscp_info.descriptor.dsc_list = (uint8_t*)(p_buf + 1);
    memcpy(p_buf->dscp_info.descriptor.dsc_list, dscp_info.descriptor.dsc_list,
           dscp_info.descriptor.dl_len);
  } else {
    p_buf->dscp_info.descriptor.dsc_list = NULL;
    p_buf->dscp_info.descriptor.dl_len = 0;
  }

  bta_sys_sendmsg(p_buf);
}

/*******************************************************************************
 *
 * Function         BTA_HhRemoveDev
 *
 * Description      Remove a device from the HID host devices list.
 *
 * Returns          void
 *
 ******************************************************************************/
void BTA_HhRemoveDev(uint8_t dev_handle) {
  tBTA_HH_MAINT_DEV* p_buf =
      (tBTA_HH_MAINT_DEV*)osi_calloc(sizeof(tBTA_HH_MAINT_DEV));

  p_buf->hdr.event = BTA_HH_API_MAINT_DEV_EVT;
  p_buf->sub_event = BTA_HH_RMV_DEV_EVT;
  p_buf->hdr.layer_specific = (uint16_t)dev_handle;

  bta_sys_sendmsg(p_buf);
}