-
Notifications
You must be signed in to change notification settings - Fork 589
/
hids_device.c
626 lines (540 loc) · 26.9 KB
/
hids_device.c
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
/*
* Copyright (C) 2014 BlueKitchen GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* 4. Any redistribution, use, or modification is done solely for
* personal benefit and not for any commercial purpose or for
* monetary gain.
*
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
* GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Please inquire about commercial licensing options at
* contact@bluekitchen-gmbh.com
*
*/
#define BTSTACK_FILE__ "hids_device.c"
/**
* Implementation of the GATT HIDS Device
* To use with your application, add '#import <hids.gatt>' to your .gatt file
*/
#include "hids_device.h"
#include "ble/att_db.h"
#include "ble/att_server.h"
#include "bluetooth_gatt.h"
#include "btstack_util.h"
#include "btstack_debug.h"
#include "btstack_hid_parser.h"
#define HIDS_DEVICE_ERROR_CODE_INAPPROPRIATE_CONNECTION_PARAMETERS 0x80
// storage for 'generic' HID Device with single Input, Output, and Feature Report
static hids_device_report_t hid_reports_generic_storage[3];
typedef struct{
uint16_t con_handle;
uint8_t hid_country_code;
const uint8_t * hid_descriptor;
uint16_t hid_descriptor_size;
uint16_t hid_report_map_handle;
uint8_t hid_protocol_mode;
uint16_t hid_protocol_mode_value_handle;
uint16_t hid_boot_mouse_input_value_handle;
uint16_t hid_boot_mouse_input_client_configuration_handle;
uint16_t hid_boot_mouse_input_client_configuration_value;
uint16_t hid_boot_keyboard_input_value_handle;
uint16_t hid_boot_keyboard_input_client_configuration_handle;
uint16_t hid_boot_keyboard_input_client_configuration_value;
uint16_t hid_boot_keyboard_output_value_handle;
hids_device_report_t * hid_reports;
uint8_t hid_input_reports_num;
uint8_t hid_output_reports_num;
uint8_t hid_feature_reports_num;
uint16_t hid_control_point_value_handle;
uint8_t hid_control_point_suspend;
btstack_context_callback_registration_t can_send_now_callback;
} hids_device_t;
static hids_device_t hids_device;
static btstack_packet_handler_t packet_handler;
static att_service_handler_t hid_service;
static void (*hids_device_get_report_callback)(hci_con_handle_t hid_cid, hid_report_type_t report_type, uint16_t report_id, uint16_t max_report_size, uint8_t * out_report);
// TODO: store hids device connection into list
static hids_device_t * hids_device_get_instance_for_con_handle(uint16_t con_handle){
UNUSED(con_handle);
return &hids_device;
}
static hids_device_t * hids_device_create_instance(void){
memset(&hids_device, 0, sizeof(hids_device_t));
return &hids_device;
}
static hids_device_report_t * hids_device_get_report_for_value_handle(hids_device_t * device, uint16_t value_handle){
uint8_t pos;
uint8_t total_reports = device->hid_input_reports_num + device->hid_output_reports_num + device->hid_feature_reports_num;
for (pos = 0 ; pos < total_reports ; pos++){
if (device->hid_reports[pos].value_handle == value_handle){
return &device->hid_reports[pos];
}
}
return NULL;
}
static hids_device_report_t * hids_device_get_report_for_client_configuration_handle(hids_device_t * device, uint16_t client_configuration_handle){
uint8_t pos;
uint8_t total_reports = device->hid_input_reports_num + device->hid_output_reports_num + device->hid_feature_reports_num;
for (pos = 0 ; pos < total_reports ; pos++){
if (device->hid_reports[pos].client_configuration_handle == client_configuration_handle){
return &device->hid_reports[pos];
}
}
return NULL;
}
static hids_device_report_t *
hids_device_get_report_for_id_and_type(hids_device_t *device, uint16_t report_id, hid_report_type_t type) {
uint8_t pos;
uint8_t total_reports = device->hid_input_reports_num + device->hid_output_reports_num + device->hid_feature_reports_num;
for (pos = 0 ; pos < total_reports ; pos++){
if ((device->hid_reports[pos].type == type) && (device->hid_reports[pos].id == report_id)){
return &device->hid_reports[pos];
}
}
return NULL;
}
static void hids_device_emit_event_with_uint8(uint8_t event, hci_con_handle_t con_handle, uint8_t value){
hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
if (!instance){
log_error("no instance for handle 0x%02x", con_handle);
return;
}
if (!packet_handler) return;
uint8_t buffer[6];
buffer[0] = HCI_EVENT_HIDS_META;
buffer[1] = 4;
buffer[2] = event;
little_endian_store_16(buffer, 3, (uint16_t) con_handle);
buffer[5] = value;
(*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer));
}
static void hids_device_emit_event_with_uint8_uint8_t(uint8_t event, hci_con_handle_t con_handle, uint8_t value_1, uint8_t value_2){
hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
if (!instance){
log_error("no instance for handle 0x%02x", con_handle);
return;
}
if (!packet_handler) return;
uint8_t buffer[7];
buffer[0] = HCI_EVENT_HIDS_META;
buffer[1] = 4;
buffer[2] = event;
little_endian_store_16(buffer, 3, (uint16_t) con_handle);
buffer[5] = value_1;
buffer[6] = value_2;
(*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer));
}
static void hids_device_emit_event(uint8_t event, hci_con_handle_t con_handle){
hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
if (!instance){
log_error("no instance for handle 0x%02x", con_handle);
return;
}
if (!packet_handler) return;
uint8_t buffer[5];
buffer[0] = HCI_EVENT_HIDS_META;
buffer[1] = 4;
buffer[2] = event;
little_endian_store_16(buffer, 3, (uint16_t) con_handle);
(*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer));
}
static void hids_device_emit_report(hci_con_handle_t con_handle, const uint8_t report_id, const uint8_t report_type,
const uint8_t *buffer, uint16_t buffer_size) {
// assemble event in buffer
uint8_t event[257];
uint16_t pos = 0;
event[pos++] = HCI_EVENT_HIDS_META;
// skip length
pos++;
event[pos++] = HIDS_SUBEVENT_SET_REPORT;
little_endian_store_16(event, pos, con_handle);
pos += 2;
event[pos++] = report_id;
event[pos++] = report_type;
uint8_t length_to_copy = btstack_min(buffer_size, 250);
event[pos++] = length_to_copy;
memcpy(&event[pos], buffer, length_to_copy);
pos += length_to_copy;
// set event length
event[1] = pos - 2;
(*packet_handler)(HCI_EVENT_PACKET, 0, event, pos);
}
static void hids_device_can_send_now(void * context){
hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context;
// notify client
hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
if (!instance){
log_error("no instance for handle 0x%02x", con_handle);
return;
}
if (!packet_handler) return;
uint8_t buffer[5];
buffer[0] = HCI_EVENT_HIDS_META;
buffer[1] = 3;
buffer[2] = HIDS_SUBEVENT_CAN_SEND_NOW;
little_endian_store_16(buffer, 3, (uint16_t) con_handle);
(*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer));
}
// ATT Client Read Callback for Dynamic Data
// - if buffer == NULL, don't copy data, just return size of value
// - if buffer != NULL, copy data and return number bytes copied
static uint16_t att_read_callback(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
if (!instance){
log_error("no instance for handle 0x%02x", con_handle);
return HIDS_DEVICE_ERROR_CODE_INAPPROPRIATE_CONNECTION_PARAMETERS;
}
if (att_handle == instance->hid_protocol_mode_value_handle){
log_info("Read protocol mode");
return att_read_callback_handle_byte(instance->hid_protocol_mode, offset, buffer, buffer_size);
}
if (att_handle == instance->hid_report_map_handle){
log_info("Read report map");
return att_read_callback_handle_blob(instance->hid_descriptor, instance->hid_descriptor_size, offset, buffer, buffer_size);
}
if (att_handle == instance->hid_boot_mouse_input_client_configuration_handle){
return att_read_callback_handle_little_endian_16(instance->hid_boot_mouse_input_client_configuration_value, offset, buffer, buffer_size);
}
if (att_handle == instance->hid_boot_keyboard_input_client_configuration_handle){
return att_read_callback_handle_little_endian_16(instance->hid_boot_keyboard_input_client_configuration_value, offset, buffer, buffer_size);
}
if (att_handle == instance->hid_control_point_value_handle){
if (buffer && (buffer_size >= 1u)){
buffer[0] = instance->hid_control_point_suspend;
}
return 1;
}
uint8_t boot_report_size = 0;
// avoid "value unused" warning by smart compiler
// not so smart compiler will complain that value is used without initialization
UNUSED(boot_report_size);
uint8_t boot_report_id = 0;
hid_report_type_t boot_report_type = HID_REPORT_TYPE_RESERVED;
if (att_handle == instance->hid_boot_mouse_input_value_handle){
boot_report_type = HID_REPORT_TYPE_INPUT;
boot_report_size = 3;
}
if (att_handle == instance->hid_boot_keyboard_input_value_handle){
boot_report_type = HID_REPORT_TYPE_INPUT;
boot_report_size = 8;
}
if (att_handle == instance->hid_boot_keyboard_output_value_handle){
boot_report_type = HID_REPORT_TYPE_OUTPUT;
boot_report_size = 1;
}
if (boot_report_type != HID_REPORT_TYPE_RESERVED){
// no callback, no report
if (hids_device_get_report_callback == NULL){
return 0;
}
// answer length request by ATT Server
if (buffer == NULL){
return boot_report_size;
} else {
// Report ID 0, Type Input
(*hids_device_get_report_callback)(con_handle, boot_report_type, boot_report_id, boot_report_size, buffer);
return boot_report_size;
}
}
hids_device_report_t * report;
report = hids_device_get_report_for_value_handle(instance, att_handle);
if (report != NULL){
// no callback, no report
if (hids_device_get_report_callback == NULL){
return 0;
}
// answer length request by ATT Server
if (buffer == NULL){
return report->size;
} else {
uint16_t max_size = btstack_min(report->size, buffer_size);
(*hids_device_get_report_callback)(con_handle, report->type, report->id, max_size, buffer);
return report->size;
}
}
report = hids_device_get_report_for_client_configuration_handle(instance, att_handle);
if (report != NULL){
return att_read_callback_handle_little_endian_16(report->client_configuration_value, offset, buffer, buffer_size);
}
return 0;
}
static int att_write_callback(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size){
UNUSED(buffer_size);
UNUSED(offset);
if (transaction_mode != ATT_TRANSACTION_MODE_NONE){
return 0;
}
hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
if (!instance){
log_error("no instance for handle 0x%02x", con_handle);
return HIDS_DEVICE_ERROR_CODE_INAPPROPRIATE_CONNECTION_PARAMETERS;
}
if (att_handle == instance->hid_boot_mouse_input_client_configuration_handle){
uint16_t new_value = little_endian_read_16(buffer, 0);
instance->hid_boot_mouse_input_client_configuration_value = new_value;
hids_device_emit_event_with_uint8(HIDS_SUBEVENT_BOOT_MOUSE_INPUT_REPORT_ENABLE, con_handle, (uint8_t) new_value);
return 0;
}
if (att_handle == instance->hid_boot_keyboard_input_client_configuration_handle){
uint16_t new_value = little_endian_read_16(buffer, 0);
instance->hid_boot_keyboard_input_client_configuration_value = new_value;
hids_device_emit_event_with_uint8(HIDS_SUBEVENT_BOOT_KEYBOARD_INPUT_REPORT_ENABLE, con_handle, (uint8_t) new_value);
return 0;
}
if (att_handle == instance->hid_protocol_mode_value_handle){
instance->hid_protocol_mode = buffer[0];
log_info("Set protocol mode: %u", instance->hid_protocol_mode);
hids_device_emit_event_with_uint8(HIDS_SUBEVENT_PROTOCOL_MODE, con_handle, instance->hid_protocol_mode);
return 0;
}
if (att_handle == instance->hid_control_point_value_handle){
if (buffer_size < 1u){
return ATT_ERROR_INVALID_OFFSET;
}
instance->hid_control_point_suspend = buffer[0];
instance->con_handle = con_handle;
log_info("Set suspend tp: %u", instance->hid_control_point_suspend );
if (instance->hid_control_point_suspend == 0u){
hids_device_emit_event(HIDS_SUBEVENT_SUSPEND, con_handle);
} else if (instance->hid_control_point_suspend == 1u){
hids_device_emit_event(HIDS_SUBEVENT_EXIT_SUSPEND, con_handle);
}
return 0;
}
if (att_handle == instance->hid_boot_keyboard_output_value_handle){
hids_device_emit_report(con_handle, 0, HID_REPORT_TYPE_OUTPUT, buffer, buffer_size);
return 0;
}
hids_device_report_t * report;
report = hids_device_get_report_for_value_handle(instance, att_handle);
if (report != NULL){
hids_device_emit_report(con_handle, report->id, (uint8_t) report->type, buffer, buffer_size);
return 0;
}
report = hids_device_get_report_for_client_configuration_handle(instance, att_handle);
if (report != NULL){
uint16_t new_value = little_endian_read_16(buffer, 0);
report->client_configuration_value = new_value;
log_info("Enable Report (type %u) notifications: %x", (uint8_t) report->type, new_value);
switch (report->type){
case HID_REPORT_TYPE_INPUT:
hids_device_emit_event_with_uint8_uint8_t(HIDS_SUBEVENT_INPUT_REPORT_ENABLE, con_handle, report->id, (uint8_t) new_value);
break;
case HID_REPORT_TYPE_OUTPUT:
hids_device_emit_event_with_uint8_uint8_t(HIDS_SUBEVENT_OUTPUT_REPORT_ENABLE, con_handle, report->id, (uint8_t) new_value);
break;
case HID_REPORT_TYPE_FEATURE:
hids_device_emit_event_with_uint8_uint8_t(HIDS_SUBEVENT_FEATURE_REPORT_ENABLE, con_handle, report->id, (uint8_t) new_value);
break;
default:
btstack_unreachable();
break;
}
}
return 0;
}
void hids_device_init_with_storage(uint8_t hid_country_code, const uint8_t * hid_descriptor, uint16_t hid_descriptor_size,
uint16_t num_reports, hids_device_report_t * report_storage){
hids_device_t * instance = hids_device_create_instance();
btstack_assert(num_reports > 0);
btstack_assert(report_storage != NULL);
instance->hid_country_code = hid_country_code;
instance->hid_descriptor = hid_descriptor;
instance->hid_descriptor_size = hid_descriptor_size;
// default
instance->hid_protocol_mode = 1;
// get service handle range
uint16_t start_handle = 0;
uint16_t end_handle = 0xffff;
int service_found = gatt_server_get_handle_range_for_service_with_uuid16(ORG_BLUETOOTH_SERVICE_HUMAN_INTERFACE_DEVICE, &start_handle, &end_handle);
btstack_assert(service_found != 0);
UNUSED(service_found);
// get report map handle
instance->hid_report_map_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT_MAP);
// get report map handle
instance->hid_protocol_mode_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_PROTOCOL_MODE);
// get value and client configuration handles for boot mouse input, boot keyboard input and report input
instance->hid_boot_mouse_input_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_BOOT_MOUSE_INPUT_REPORT);
instance->hid_boot_mouse_input_client_configuration_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_BOOT_MOUSE_INPUT_REPORT);
instance->hid_boot_keyboard_input_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_BOOT_KEYBOARD_INPUT_REPORT);
instance->hid_boot_keyboard_input_client_configuration_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_BOOT_KEYBOARD_INPUT_REPORT);
instance->hid_boot_keyboard_output_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_BOOT_KEYBOARD_OUTPUT_REPORT);
instance->hid_control_point_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_HID_CONTROL_POINT);
log_info("hid_report_map_handle 0x%02x", instance->hid_report_map_handle);
log_info("hid_protocol_mode_value_handle 0x%02x", instance->hid_protocol_mode_value_handle);
log_info("hid_boot_mouse_input_value_handle 0x%02x", instance->hid_boot_mouse_input_value_handle);
log_info("hid_boot_mouse_input_client_configuration_handle 0x%02x", instance->hid_boot_mouse_input_client_configuration_handle);
log_info("hid_boot_keyboard_input_value_handle 0x%02x", instance->hid_boot_keyboard_input_value_handle);
log_info("hid_boot_keyboard_output_value_handle 0x%02x", instance->hid_boot_keyboard_output_value_handle);
log_info("hid_boot_keyboard_input_client_configuration_handle 0x%02x", instance->hid_boot_keyboard_input_client_configuration_handle);
log_info("hid_control_point_value_handle 0x%02x", instance->hid_control_point_value_handle);
instance->hid_reports = report_storage;
uint16_t hid_reports_num = num_reports;
uint16_t assigned_reports_num = 0;
uint16_t start_chr_handle = start_handle;
while ( (start_chr_handle < end_handle) && (assigned_reports_num < hid_reports_num)) {
// mandatory
uint16_t chr_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_chr_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT);
if (chr_value_handle == 0){
break;
}
// optional
uint16_t chr_client_configuration_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(start_chr_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT);
// mandatory
uint16_t report_reference_handle = gatt_server_get_descriptor_handle_for_characteristic_with_uuid16(start_chr_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT, ORG_BLUETOOTH_DESCRIPTOR_REPORT_REFERENCE);
if (report_reference_handle == 0){
break;
}
// get report id and type from report reference
uint16_t report_reference_value_len;
const uint8_t * report_reference_value = gatt_server_get_const_value_for_handle(report_reference_handle, &report_reference_value_len);
if (report_reference_value == NULL){
break;
}
if (report_reference_value_len != 2){
break;
}
uint8_t report_id = report_reference_value[0];
hid_report_type_t report_type = (hid_report_type_t) report_reference_value[1];
// store report info
hids_device_report_t * report = &report_storage[assigned_reports_num];
report->value_handle = chr_value_handle;
report->client_configuration_handle = chr_client_configuration_handle;
report->client_configuration_value = 0;
report->id = report_id;
report->type = report_type;
report->size = btstack_hid_get_report_size_for_id(report_id, report_type, hid_descriptor_size, hid_descriptor);
switch (report->type){
case HID_REPORT_TYPE_INPUT:
instance->hid_input_reports_num++;
break;
case HID_REPORT_TYPE_OUTPUT:
instance->hid_output_reports_num++;
break;
case HID_REPORT_TYPE_FEATURE:
instance->hid_feature_reports_num++;
break;
default:
btstack_unreachable();
return;
}
log_info("hid_report_value_handle 0x%02x, id %u, type %u", report->value_handle, report->id, (uint8_t)report->type);
if (report->client_configuration_handle != 0){
log_info("hid_report_client_configuration_handle 0x%02x", report->client_configuration_handle);
}
assigned_reports_num++;
start_chr_handle = report_reference_handle + 1;
}
// register service with ATT Server
hid_service.start_handle = start_handle;
hid_service.end_handle = end_handle;
hid_service.read_callback = &att_read_callback;
hid_service.write_callback = &att_write_callback;
att_server_register_service_handler(&hid_service);
}
/**
* @brief Set up HIDS Device
*/
void hids_device_init(uint8_t country_code, const uint8_t * descriptor, uint16_t descriptor_size){
uint16_t hid_reports_num = sizeof(hid_reports_generic_storage) / sizeof(hids_device_report_t);
hids_device_init_with_storage(country_code, descriptor, descriptor_size, hid_reports_num, hid_reports_generic_storage);
}
/**
* @brief Register callback for the HIDS Device client.
* @param callback
*/
void hids_device_register_packet_handler(btstack_packet_handler_t callback){
packet_handler = callback;
}
void hids_device_register_get_report_callback(void (*callback)(hci_con_handle_t con_handle, hid_report_type_t report_type, uint16_t report_id, uint16_t max_report_size, uint8_t * out_report)){
hids_device_get_report_callback = callback;
}
/**
* @brief Request can send now event to send HID Report
* Generates an HIDS_SUBEVENT_CAN_SEND_NOW subevent
* @param hid_cid
*/
void hids_device_request_can_send_now_event(hci_con_handle_t con_handle){
hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
if (!instance){
log_error("no instance for handle 0x%02x", con_handle);
return;
}
instance->can_send_now_callback.callback = &hids_device_can_send_now;
instance->can_send_now_callback.context = (void*) (uintptr_t) con_handle;
att_server_register_can_send_now_callback(&instance->can_send_now_callback, con_handle);
}
uint8_t hids_device_send_input_report_for_id(hci_con_handle_t con_handle, uint16_t report_id, const uint8_t * report, uint16_t report_len){
hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
if (!instance){
log_error("no instance for handle 0x%02x", con_handle);
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}
hids_device_report_t * report_storage = hids_device_get_report_for_id_and_type(instance, report_id,
HID_REPORT_TYPE_INPUT);
if (report_storage == NULL){
return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
}
return att_server_notify(con_handle, report_storage->value_handle, report, report_len);
}
uint8_t hids_device_send_input_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){
hids_device_t * device = hids_device_get_instance_for_con_handle(con_handle);
if (!device){
log_error("no instance for handle 0x%02x", con_handle);
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}
uint8_t pos;
uint8_t total_reports = device->hid_input_reports_num + device->hid_output_reports_num + device->hid_feature_reports_num;
for (pos = 0 ; pos < total_reports ; pos++){
hids_device_report_t * report_storage = &device->hid_reports[pos];
if (report_storage->type == HID_REPORT_TYPE_INPUT){
return att_server_notify(con_handle, report_storage->value_handle, report, report_len);
}
}
return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
}
/**
* @brief Send HID Boot Mouse Input Report
*/
uint8_t hids_device_send_boot_mouse_input_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){
hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
if (!instance){
log_error("no instance for handle 0x%02x", con_handle);
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}
return att_server_notify(con_handle, instance->hid_boot_mouse_input_value_handle, report, report_len);
}
/**
* @brief Send HID Boot Mouse Input Report
*/
uint8_t hids_device_send_boot_keyboard_input_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){
hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
if (!instance){
log_error("no instance for handle 0x%02x", con_handle);
return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
}
return att_server_notify(con_handle, instance->hid_boot_keyboard_input_value_handle, report, report_len);
}