1a4bfc4feSMatthias Ringwald /*
2a4bfc4feSMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH
3a4bfc4feSMatthias Ringwald *
4a4bfc4feSMatthias Ringwald * Redistribution and use in source and binary forms, with or without
5a4bfc4feSMatthias Ringwald * modification, are permitted provided that the following conditions
6a4bfc4feSMatthias Ringwald * are met:
7a4bfc4feSMatthias Ringwald *
8a4bfc4feSMatthias Ringwald * 1. Redistributions of source code must retain the above copyright
9a4bfc4feSMatthias Ringwald * notice, this list of conditions and the following disclaimer.
10a4bfc4feSMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright
11a4bfc4feSMatthias Ringwald * notice, this list of conditions and the following disclaimer in the
12a4bfc4feSMatthias Ringwald * documentation and/or other materials provided with the distribution.
13a4bfc4feSMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of
14a4bfc4feSMatthias Ringwald * contributors may be used to endorse or promote products derived
15a4bfc4feSMatthias Ringwald * from this software without specific prior written permission.
16a4bfc4feSMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for
17a4bfc4feSMatthias Ringwald * personal benefit and not for any commercial purpose or for
18a4bfc4feSMatthias Ringwald * monetary gain.
19a4bfc4feSMatthias Ringwald *
20a4bfc4feSMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21a4bfc4feSMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22a4bfc4feSMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
232fca4dadSMilanka Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25a4bfc4feSMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26a4bfc4feSMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27a4bfc4feSMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28a4bfc4feSMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29a4bfc4feSMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30a4bfc4feSMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31a4bfc4feSMatthias Ringwald * SUCH DAMAGE.
32a4bfc4feSMatthias Ringwald *
33a4bfc4feSMatthias Ringwald * Please inquire about commercial licensing options at
34a4bfc4feSMatthias Ringwald * [email protected]
35a4bfc4feSMatthias Ringwald *
36a4bfc4feSMatthias Ringwald */
37a4bfc4feSMatthias Ringwald
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "hids_device.c"
39523edd21SMatthias Ringwald
40a4bfc4feSMatthias Ringwald /**
41a4bfc4feSMatthias Ringwald * Implementation of the GATT HIDS Device
42a4bfc4feSMatthias Ringwald * To use with your application, add '#import <hids.gatt>' to your .gatt file
43a4bfc4feSMatthias Ringwald */
44a4bfc4feSMatthias Ringwald
45a4bfc4feSMatthias Ringwald #include "hids_device.h"
46a4bfc4feSMatthias Ringwald
4735b80bb0SMatthias Ringwald #include "ble/att_db.h"
4835b80bb0SMatthias Ringwald #include "ble/att_server.h"
49a4bfc4feSMatthias Ringwald #include "bluetooth_gatt.h"
50a4bfc4feSMatthias Ringwald #include "btstack_util.h"
51523edd21SMatthias Ringwald #include "btstack_debug.h"
521b9864eaSMatthias Ringwald #include "btstack_hid_parser.h"
53a4bfc4feSMatthias Ringwald
540235c9e5SMilanka Ringwald #define HIDS_DEVICE_ERROR_CODE_INAPPROPRIATE_CONNECTION_PARAMETERS 0x80
550235c9e5SMilanka Ringwald
56160eac51SMatthias Ringwald // storage for 'generic' HID Device with single Input, Output, and Feature Report
57160eac51SMatthias Ringwald static hids_device_report_t hid_reports_generic_storage[3];
58f4f6c196SMilanka Ringwald
590235c9e5SMilanka Ringwald typedef struct{
600235c9e5SMilanka Ringwald uint16_t con_handle;
610235c9e5SMilanka Ringwald
620235c9e5SMilanka Ringwald uint8_t hid_country_code;
630235c9e5SMilanka Ringwald const uint8_t * hid_descriptor;
640235c9e5SMilanka Ringwald uint16_t hid_descriptor_size;
650235c9e5SMilanka Ringwald
660235c9e5SMilanka Ringwald uint16_t hid_report_map_handle;
67c1dbba9dSMatthias Ringwald uint8_t hid_protocol_mode;
680235c9e5SMilanka Ringwald uint16_t hid_protocol_mode_value_handle;
690235c9e5SMilanka Ringwald
700235c9e5SMilanka Ringwald uint16_t hid_boot_mouse_input_value_handle;
710235c9e5SMilanka Ringwald uint16_t hid_boot_mouse_input_client_configuration_handle;
720235c9e5SMilanka Ringwald uint16_t hid_boot_mouse_input_client_configuration_value;
730235c9e5SMilanka Ringwald
740235c9e5SMilanka Ringwald uint16_t hid_boot_keyboard_input_value_handle;
750235c9e5SMilanka Ringwald uint16_t hid_boot_keyboard_input_client_configuration_handle;
760235c9e5SMilanka Ringwald uint16_t hid_boot_keyboard_input_client_configuration_value;
770235c9e5SMilanka Ringwald
78bbf45f89SMilanka Ringwald uint16_t hid_boot_keyboard_output_value_handle;
79bbf45f89SMilanka Ringwald
80f4f6c196SMilanka Ringwald hids_device_report_t * hid_reports;
810235c9e5SMilanka Ringwald
82f4f6c196SMilanka Ringwald uint8_t hid_input_reports_num;
83f4f6c196SMilanka Ringwald uint8_t hid_output_reports_num;
84f4f6c196SMilanka Ringwald uint8_t hid_feature_reports_num;
850235c9e5SMilanka Ringwald
860235c9e5SMilanka Ringwald uint16_t hid_control_point_value_handle;
870235c9e5SMilanka Ringwald uint8_t hid_control_point_suspend;
880235c9e5SMilanka Ringwald
89086ea75fSMatthias Ringwald btstack_context_callback_registration_t can_send_now_callback;
900235c9e5SMilanka Ringwald } hids_device_t;
910235c9e5SMilanka Ringwald
920235c9e5SMilanka Ringwald static hids_device_t hids_device;
930235c9e5SMilanka Ringwald
94a4bfc4feSMatthias Ringwald static btstack_packet_handler_t packet_handler;
95a4bfc4feSMatthias Ringwald static att_service_handler_t hid_service;
961b9864eaSMatthias Ringwald 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);
97a4bfc4feSMatthias Ringwald
980235c9e5SMilanka Ringwald // TODO: store hids device connection into list
hids_device_get_instance_for_con_handle(uint16_t con_handle)990235c9e5SMilanka Ringwald static hids_device_t * hids_device_get_instance_for_con_handle(uint16_t con_handle){
1000235c9e5SMilanka Ringwald UNUSED(con_handle);
1010235c9e5SMilanka Ringwald return &hids_device;
1020235c9e5SMilanka Ringwald }
1030235c9e5SMilanka Ringwald
hids_device_create_instance(void)1040235c9e5SMilanka Ringwald static hids_device_t * hids_device_create_instance(void){
105f4f6c196SMilanka Ringwald memset(&hids_device, 0, sizeof(hids_device_t));
1060235c9e5SMilanka Ringwald return &hids_device;
1070235c9e5SMilanka Ringwald }
1080235c9e5SMilanka Ringwald
hids_device_get_report_for_value_handle(hids_device_t * device,uint16_t value_handle)1098af7e88bSMilanka Ringwald static hids_device_report_t * hids_device_get_report_for_value_handle(hids_device_t * device, uint16_t value_handle){
110aa3c9a66SMatthias Ringwald uint8_t pos;
111aa3c9a66SMatthias Ringwald uint8_t total_reports = device->hid_input_reports_num + device->hid_output_reports_num + device->hid_feature_reports_num;
112aa3c9a66SMatthias Ringwald for (pos = 0 ; pos < total_reports ; pos++){
1138af7e88bSMilanka Ringwald if (device->hid_reports[pos].value_handle == value_handle){
114aa3c9a66SMatthias Ringwald return &device->hid_reports[pos];
115aa3c9a66SMatthias Ringwald }
116aa3c9a66SMatthias Ringwald }
117aa3c9a66SMatthias Ringwald return NULL;
118aa3c9a66SMatthias Ringwald }
119aa3c9a66SMatthias Ringwald
hids_device_get_report_for_client_configuration_handle(hids_device_t * device,uint16_t client_configuration_handle)120f4f6c196SMilanka Ringwald static hids_device_report_t * hids_device_get_report_for_client_configuration_handle(hids_device_t * device, uint16_t client_configuration_handle){
121d75a985bSMatthias Ringwald uint8_t pos;
122d75a985bSMatthias Ringwald uint8_t total_reports = device->hid_input_reports_num + device->hid_output_reports_num + device->hid_feature_reports_num;
123d75a985bSMatthias Ringwald for (pos = 0 ; pos < total_reports ; pos++){
124f4f6c196SMilanka Ringwald if (device->hid_reports[pos].client_configuration_handle == client_configuration_handle){
125f4f6c196SMilanka Ringwald return &device->hid_reports[pos];
126f4f6c196SMilanka Ringwald }
127f4f6c196SMilanka Ringwald }
128f4f6c196SMilanka Ringwald return NULL;
129f4f6c196SMilanka Ringwald }
130f4f6c196SMilanka Ringwald
131a3c1c955SMatthias Ringwald 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)132a3c1c955SMatthias Ringwald hids_device_get_report_for_id_and_type(hids_device_t *device, uint16_t report_id, hid_report_type_t type) {
13345569c34SMatthias Ringwald uint8_t pos;
13445569c34SMatthias Ringwald uint8_t total_reports = device->hid_input_reports_num + device->hid_output_reports_num + device->hid_feature_reports_num;
13545569c34SMatthias Ringwald for (pos = 0 ; pos < total_reports ; pos++){
13645569c34SMatthias Ringwald if ((device->hid_reports[pos].type == type) && (device->hid_reports[pos].id == report_id)){
13745569c34SMatthias Ringwald return &device->hid_reports[pos];
13845569c34SMatthias Ringwald }
13945569c34SMatthias Ringwald }
14045569c34SMatthias Ringwald return NULL;
14145569c34SMatthias Ringwald }
14245569c34SMatthias Ringwald
hids_device_emit_event_with_uint8(uint8_t event,hci_con_handle_t con_handle,uint8_t value)143a4bfc4feSMatthias Ringwald static void hids_device_emit_event_with_uint8(uint8_t event, hci_con_handle_t con_handle, uint8_t value){
1440235c9e5SMilanka Ringwald hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
1450235c9e5SMilanka Ringwald if (!instance){
1460235c9e5SMilanka Ringwald log_error("no instance for handle 0x%02x", con_handle);
1470235c9e5SMilanka Ringwald return;
1480235c9e5SMilanka Ringwald }
1490235c9e5SMilanka Ringwald
150a4bfc4feSMatthias Ringwald if (!packet_handler) return;
151a4bfc4feSMatthias Ringwald uint8_t buffer[6];
152a4bfc4feSMatthias Ringwald buffer[0] = HCI_EVENT_HIDS_META;
153a4bfc4feSMatthias Ringwald buffer[1] = 4;
154a4bfc4feSMatthias Ringwald buffer[2] = event;
155a4bfc4feSMatthias Ringwald little_endian_store_16(buffer, 3, (uint16_t) con_handle);
156a4bfc4feSMatthias Ringwald buffer[5] = value;
157a4bfc4feSMatthias Ringwald (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer));
158a4bfc4feSMatthias Ringwald }
159a4bfc4feSMatthias Ringwald
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)160f00767dfSMatthias Ringwald 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){
161f00767dfSMatthias Ringwald hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
162f00767dfSMatthias Ringwald if (!instance){
163f00767dfSMatthias Ringwald log_error("no instance for handle 0x%02x", con_handle);
164f00767dfSMatthias Ringwald return;
165f00767dfSMatthias Ringwald }
166f00767dfSMatthias Ringwald
167f00767dfSMatthias Ringwald if (!packet_handler) return;
168f00767dfSMatthias Ringwald uint8_t buffer[7];
169f00767dfSMatthias Ringwald buffer[0] = HCI_EVENT_HIDS_META;
170f00767dfSMatthias Ringwald buffer[1] = 4;
171f00767dfSMatthias Ringwald buffer[2] = event;
172f00767dfSMatthias Ringwald little_endian_store_16(buffer, 3, (uint16_t) con_handle);
173f00767dfSMatthias Ringwald buffer[5] = value_1;
174f00767dfSMatthias Ringwald buffer[6] = value_2;
175f00767dfSMatthias Ringwald (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer));
176f00767dfSMatthias Ringwald }
177f00767dfSMatthias Ringwald
hids_device_emit_event(uint8_t event,hci_con_handle_t con_handle)1780235c9e5SMilanka Ringwald static void hids_device_emit_event(uint8_t event, hci_con_handle_t con_handle){
1790235c9e5SMilanka Ringwald hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
1800235c9e5SMilanka Ringwald if (!instance){
1810235c9e5SMilanka Ringwald log_error("no instance for handle 0x%02x", con_handle);
1820235c9e5SMilanka Ringwald return;
1830235c9e5SMilanka Ringwald }
1840235c9e5SMilanka Ringwald
1850235c9e5SMilanka Ringwald if (!packet_handler) return;
1860235c9e5SMilanka Ringwald uint8_t buffer[5];
1870235c9e5SMilanka Ringwald buffer[0] = HCI_EVENT_HIDS_META;
1880235c9e5SMilanka Ringwald buffer[1] = 4;
1890235c9e5SMilanka Ringwald buffer[2] = event;
1900235c9e5SMilanka Ringwald little_endian_store_16(buffer, 3, (uint16_t) con_handle);
1910235c9e5SMilanka Ringwald (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer));
1920235c9e5SMilanka Ringwald }
1930235c9e5SMilanka Ringwald
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)194f5fc5029SMatthias Ringwald static void hids_device_emit_report(hci_con_handle_t con_handle, const uint8_t report_id, const uint8_t report_type,
195f5fc5029SMatthias Ringwald const uint8_t *buffer, uint16_t buffer_size) {
196f5fc5029SMatthias Ringwald // assemble event in buffer
197f5fc5029SMatthias Ringwald uint8_t event[257];
198f5fc5029SMatthias Ringwald uint16_t pos = 0;
199f5fc5029SMatthias Ringwald event[pos++] = HCI_EVENT_HIDS_META;
200f5fc5029SMatthias Ringwald // skip length
201f5fc5029SMatthias Ringwald pos++;
202f5fc5029SMatthias Ringwald event[pos++] = HIDS_SUBEVENT_SET_REPORT;
203f5fc5029SMatthias Ringwald little_endian_store_16(event, pos, con_handle);
204f5fc5029SMatthias Ringwald pos += 2;
205f5fc5029SMatthias Ringwald event[pos++] = report_id;
206f5fc5029SMatthias Ringwald event[pos++] = report_type;
207f5fc5029SMatthias Ringwald uint8_t length_to_copy = btstack_min(buffer_size, 250);
208f5fc5029SMatthias Ringwald event[pos++] = length_to_copy;
209f5fc5029SMatthias Ringwald memcpy(&event[pos], buffer, length_to_copy);
210f5fc5029SMatthias Ringwald pos += length_to_copy;
211f5fc5029SMatthias Ringwald // set event length
212f5fc5029SMatthias Ringwald event[1] = pos - 2;
213f5fc5029SMatthias Ringwald (*packet_handler)(HCI_EVENT_PACKET, 0, event, pos);
214f5fc5029SMatthias Ringwald }
215f5fc5029SMatthias Ringwald
hids_device_can_send_now(void * context)216a4bfc4feSMatthias Ringwald static void hids_device_can_send_now(void * context){
217a4bfc4feSMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context;
218a4bfc4feSMatthias Ringwald // notify client
2190235c9e5SMilanka Ringwald hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
2200235c9e5SMilanka Ringwald if (!instance){
2210235c9e5SMilanka Ringwald log_error("no instance for handle 0x%02x", con_handle);
2220235c9e5SMilanka Ringwald return;
2230235c9e5SMilanka Ringwald }
2240235c9e5SMilanka Ringwald
225a4bfc4feSMatthias Ringwald if (!packet_handler) return;
226a4bfc4feSMatthias Ringwald uint8_t buffer[5];
227a4bfc4feSMatthias Ringwald buffer[0] = HCI_EVENT_HIDS_META;
228a4bfc4feSMatthias Ringwald buffer[1] = 3;
229a4bfc4feSMatthias Ringwald buffer[2] = HIDS_SUBEVENT_CAN_SEND_NOW;
230a4bfc4feSMatthias Ringwald little_endian_store_16(buffer, 3, (uint16_t) con_handle);
231a4bfc4feSMatthias Ringwald (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer));
232a4bfc4feSMatthias Ringwald }
233a4bfc4feSMatthias Ringwald
234a4bfc4feSMatthias Ringwald // ATT Client Read Callback for Dynamic Data
235a4bfc4feSMatthias Ringwald // - if buffer == NULL, don't copy data, just return size of value
236a4bfc4feSMatthias Ringwald // - if buffer != NULL, copy data and return number bytes copied
att_read_callback(hci_con_handle_t con_handle,uint16_t att_handle,uint16_t offset,uint8_t * buffer,uint16_t buffer_size)2370235c9e5SMilanka Ringwald 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){
2380235c9e5SMilanka Ringwald hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
2390235c9e5SMilanka Ringwald if (!instance){
2400235c9e5SMilanka Ringwald log_error("no instance for handle 0x%02x", con_handle);
2410235c9e5SMilanka Ringwald return HIDS_DEVICE_ERROR_CODE_INAPPROPRIATE_CONNECTION_PARAMETERS;
242a4bfc4feSMatthias Ringwald }
2430235c9e5SMilanka Ringwald
2440235c9e5SMilanka Ringwald if (att_handle == instance->hid_protocol_mode_value_handle){
2450235c9e5SMilanka Ringwald log_info("Read protocol mode");
2460235c9e5SMilanka Ringwald return att_read_callback_handle_byte(instance->hid_protocol_mode, offset, buffer, buffer_size);
2470235c9e5SMilanka Ringwald }
248c9d24807SMilanka Ringwald
2490235c9e5SMilanka Ringwald if (att_handle == instance->hid_report_map_handle){
250523edd21SMatthias Ringwald log_info("Read report map");
2510235c9e5SMilanka Ringwald return att_read_callback_handle_blob(instance->hid_descriptor, instance->hid_descriptor_size, offset, buffer, buffer_size);
252a4bfc4feSMatthias Ringwald }
253c9d24807SMilanka Ringwald
2540235c9e5SMilanka Ringwald if (att_handle == instance->hid_boot_mouse_input_client_configuration_handle){
2550235c9e5SMilanka Ringwald return att_read_callback_handle_little_endian_16(instance->hid_boot_mouse_input_client_configuration_value, offset, buffer, buffer_size);
256a4bfc4feSMatthias Ringwald }
257c9d24807SMilanka Ringwald
2580235c9e5SMilanka Ringwald if (att_handle == instance->hid_boot_keyboard_input_client_configuration_handle){
2590235c9e5SMilanka Ringwald return att_read_callback_handle_little_endian_16(instance->hid_boot_keyboard_input_client_configuration_value, offset, buffer, buffer_size);
260a4bfc4feSMatthias Ringwald }
261c9d24807SMilanka Ringwald
2620235c9e5SMilanka Ringwald if (att_handle == instance->hid_control_point_value_handle){
2634ea43905SMatthias Ringwald if (buffer && (buffer_size >= 1u)){
2640235c9e5SMilanka Ringwald buffer[0] = instance->hid_control_point_suspend;
2650235c9e5SMilanka Ringwald }
2660235c9e5SMilanka Ringwald return 1;
267a4bfc4feSMatthias Ringwald }
268f4f6c196SMilanka Ringwald
2691b9864eaSMatthias Ringwald uint8_t boot_report_size = 0;
270f5fc5029SMatthias Ringwald // avoid "value unused" warning by smart compiler
271f5fc5029SMatthias Ringwald // not so smart compiler will complain that value is used without initialization
272f5fc5029SMatthias Ringwald UNUSED(boot_report_size);
273f5fc5029SMatthias Ringwald uint8_t boot_report_id = 0;
274f5fc5029SMatthias Ringwald hid_report_type_t boot_report_type = HID_REPORT_TYPE_RESERVED;
2751b9864eaSMatthias Ringwald if (att_handle == instance->hid_boot_mouse_input_value_handle){
276f5fc5029SMatthias Ringwald boot_report_type = HID_REPORT_TYPE_INPUT;
2771b9864eaSMatthias Ringwald boot_report_size = 3;
2781b9864eaSMatthias Ringwald }
2791b9864eaSMatthias Ringwald if (att_handle == instance->hid_boot_keyboard_input_value_handle){
280f5fc5029SMatthias Ringwald boot_report_type = HID_REPORT_TYPE_INPUT;
2811b9864eaSMatthias Ringwald boot_report_size = 8;
2821b9864eaSMatthias Ringwald }
283f5fc5029SMatthias Ringwald if (att_handle == instance->hid_boot_keyboard_output_value_handle){
284f5fc5029SMatthias Ringwald boot_report_type = HID_REPORT_TYPE_OUTPUT;
285f5fc5029SMatthias Ringwald boot_report_size = 1;
286f5fc5029SMatthias Ringwald }
287f5fc5029SMatthias Ringwald if (boot_report_type != HID_REPORT_TYPE_RESERVED){
2881b9864eaSMatthias Ringwald // no callback, no report
2891b9864eaSMatthias Ringwald if (hids_device_get_report_callback == NULL){
2901b9864eaSMatthias Ringwald return 0;
2911b9864eaSMatthias Ringwald }
2921b9864eaSMatthias Ringwald // answer length request by ATT Server
2931b9864eaSMatthias Ringwald if (buffer == NULL){
2941b9864eaSMatthias Ringwald return boot_report_size;
2951b9864eaSMatthias Ringwald } else {
2961b9864eaSMatthias Ringwald // Report ID 0, Type Input
297f5fc5029SMatthias Ringwald (*hids_device_get_report_callback)(con_handle, boot_report_type, boot_report_id, boot_report_size, buffer);
2981b9864eaSMatthias Ringwald return boot_report_size;
2991b9864eaSMatthias Ringwald }
3001b9864eaSMatthias Ringwald }
3011b9864eaSMatthias Ringwald
3021b9864eaSMatthias Ringwald hids_device_report_t * report;
3031b9864eaSMatthias Ringwald report = hids_device_get_report_for_value_handle(instance, att_handle);
3041b9864eaSMatthias Ringwald if (report != NULL){
3051b9864eaSMatthias Ringwald // no callback, no report
3061b9864eaSMatthias Ringwald if (hids_device_get_report_callback == NULL){
3071b9864eaSMatthias Ringwald return 0;
3081b9864eaSMatthias Ringwald }
3091b9864eaSMatthias Ringwald // answer length request by ATT Server
3101b9864eaSMatthias Ringwald if (buffer == NULL){
3111b9864eaSMatthias Ringwald return report->size;
3121b9864eaSMatthias Ringwald } else {
3131b9864eaSMatthias Ringwald uint16_t max_size = btstack_min(report->size, buffer_size);
3141b9864eaSMatthias Ringwald (*hids_device_get_report_callback)(con_handle, report->type, report->id, max_size, buffer);
3151b9864eaSMatthias Ringwald return report->size;
3161b9864eaSMatthias Ringwald }
3171b9864eaSMatthias Ringwald }
3181b9864eaSMatthias Ringwald
3191b9864eaSMatthias Ringwald report = hids_device_get_report_for_client_configuration_handle(instance, att_handle);
320f4f6c196SMilanka Ringwald if (report != NULL){
321f4f6c196SMilanka Ringwald return att_read_callback_handle_little_endian_16(report->client_configuration_value, offset, buffer, buffer_size);
322f4f6c196SMilanka Ringwald }
323a4bfc4feSMatthias Ringwald return 0;
324a4bfc4feSMatthias Ringwald }
325a4bfc4feSMatthias Ringwald
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)326a4bfc4feSMatthias Ringwald 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){
327a4bfc4feSMatthias Ringwald UNUSED(buffer_size);
328a4bfc4feSMatthias Ringwald UNUSED(offset);
329a4bfc4feSMatthias Ringwald
330689e7fb3SMatthias Ringwald if (transaction_mode != ATT_TRANSACTION_MODE_NONE){
331689e7fb3SMatthias Ringwald return 0;
332689e7fb3SMatthias Ringwald }
333689e7fb3SMatthias Ringwald
3340235c9e5SMilanka Ringwald hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
3350235c9e5SMilanka Ringwald if (!instance){
3360235c9e5SMilanka Ringwald log_error("no instance for handle 0x%02x", con_handle);
3370235c9e5SMilanka Ringwald return HIDS_DEVICE_ERROR_CODE_INAPPROPRIATE_CONNECTION_PARAMETERS;
3380235c9e5SMilanka Ringwald }
3390235c9e5SMilanka Ringwald
3400235c9e5SMilanka Ringwald if (att_handle == instance->hid_boot_mouse_input_client_configuration_handle){
34185828295SMatthias Ringwald uint16_t new_value = little_endian_read_16(buffer, 0);
3420235c9e5SMilanka Ringwald instance->hid_boot_mouse_input_client_configuration_value = new_value;
343c1dbba9dSMatthias Ringwald hids_device_emit_event_with_uint8(HIDS_SUBEVENT_BOOT_MOUSE_INPUT_REPORT_ENABLE, con_handle, (uint8_t) new_value);
344b3b57560SMilanka Ringwald return 0;
345a4bfc4feSMatthias Ringwald }
3460235c9e5SMilanka Ringwald if (att_handle == instance->hid_boot_keyboard_input_client_configuration_handle){
34785828295SMatthias Ringwald uint16_t new_value = little_endian_read_16(buffer, 0);
3480235c9e5SMilanka Ringwald instance->hid_boot_keyboard_input_client_configuration_value = new_value;
349c1dbba9dSMatthias Ringwald hids_device_emit_event_with_uint8(HIDS_SUBEVENT_BOOT_KEYBOARD_INPUT_REPORT_ENABLE, con_handle, (uint8_t) new_value);
350b3b57560SMilanka Ringwald return 0;
351a4bfc4feSMatthias Ringwald }
3520235c9e5SMilanka Ringwald
3530235c9e5SMilanka Ringwald if (att_handle == instance->hid_protocol_mode_value_handle){
3540235c9e5SMilanka Ringwald instance->hid_protocol_mode = buffer[0];
3550235c9e5SMilanka Ringwald log_info("Set protocol mode: %u", instance->hid_protocol_mode);
3560235c9e5SMilanka Ringwald hids_device_emit_event_with_uint8(HIDS_SUBEVENT_PROTOCOL_MODE, con_handle, instance->hid_protocol_mode);
357b3b57560SMilanka Ringwald return 0;
3580235c9e5SMilanka Ringwald }
3590235c9e5SMilanka Ringwald
3600235c9e5SMilanka Ringwald if (att_handle == instance->hid_control_point_value_handle){
3614ea43905SMatthias Ringwald if (buffer_size < 1u){
3620235c9e5SMilanka Ringwald return ATT_ERROR_INVALID_OFFSET;
3630235c9e5SMilanka Ringwald }
3640235c9e5SMilanka Ringwald instance->hid_control_point_suspend = buffer[0];
3650235c9e5SMilanka Ringwald instance->con_handle = con_handle;
3660235c9e5SMilanka Ringwald log_info("Set suspend tp: %u", instance->hid_control_point_suspend );
3674ea43905SMatthias Ringwald if (instance->hid_control_point_suspend == 0u){
3680235c9e5SMilanka Ringwald hids_device_emit_event(HIDS_SUBEVENT_SUSPEND, con_handle);
3694ea43905SMatthias Ringwald } else if (instance->hid_control_point_suspend == 1u){
3700235c9e5SMilanka Ringwald hids_device_emit_event(HIDS_SUBEVENT_EXIT_SUSPEND, con_handle);
3710235c9e5SMilanka Ringwald }
372b3b57560SMilanka Ringwald return 0;
373b3b57560SMilanka Ringwald }
374bbf45f89SMilanka Ringwald
375bbf45f89SMilanka Ringwald if (att_handle == instance->hid_boot_keyboard_output_value_handle){
376f5fc5029SMatthias Ringwald hids_device_emit_report(con_handle, 0, HID_REPORT_TYPE_OUTPUT, buffer, buffer_size);
377bbf45f89SMilanka Ringwald return 0;
3780235c9e5SMilanka Ringwald }
379f4f6c196SMilanka Ringwald
380aa3c9a66SMatthias Ringwald hids_device_report_t * report;
381aa3c9a66SMatthias Ringwald report = hids_device_get_report_for_value_handle(instance, att_handle);
382aa3c9a66SMatthias Ringwald if (report != NULL){
383f5fc5029SMatthias Ringwald hids_device_emit_report(con_handle, report->id, (uint8_t) report->type, buffer, buffer_size);
384aa3c9a66SMatthias Ringwald return 0;
385aa3c9a66SMatthias Ringwald }
386aa3c9a66SMatthias Ringwald
387aa3c9a66SMatthias Ringwald report = hids_device_get_report_for_client_configuration_handle(instance, att_handle);
388f4f6c196SMilanka Ringwald if (report != NULL){
389f4f6c196SMilanka Ringwald uint16_t new_value = little_endian_read_16(buffer, 0);
390f4f6c196SMilanka Ringwald report->client_configuration_value = new_value;
391f4f6c196SMilanka Ringwald log_info("Enable Report (type %u) notifications: %x", (uint8_t) report->type, new_value);
392f4f6c196SMilanka Ringwald
393f4f6c196SMilanka Ringwald switch (report->type){
394f4f6c196SMilanka Ringwald case HID_REPORT_TYPE_INPUT:
395f00767dfSMatthias Ringwald hids_device_emit_event_with_uint8_uint8_t(HIDS_SUBEVENT_INPUT_REPORT_ENABLE, con_handle, report->id, (uint8_t) new_value);
396f4f6c196SMilanka Ringwald break;
397f4f6c196SMilanka Ringwald case HID_REPORT_TYPE_OUTPUT:
398f00767dfSMatthias Ringwald hids_device_emit_event_with_uint8_uint8_t(HIDS_SUBEVENT_OUTPUT_REPORT_ENABLE, con_handle, report->id, (uint8_t) new_value);
399f4f6c196SMilanka Ringwald break;
400f4f6c196SMilanka Ringwald case HID_REPORT_TYPE_FEATURE:
401f00767dfSMatthias Ringwald hids_device_emit_event_with_uint8_uint8_t(HIDS_SUBEVENT_FEATURE_REPORT_ENABLE, con_handle, report->id, (uint8_t) new_value);
402f4f6c196SMilanka Ringwald break;
403f4f6c196SMilanka Ringwald default:
404f4f6c196SMilanka Ringwald btstack_unreachable();
405f4f6c196SMilanka Ringwald break;
406f4f6c196SMilanka Ringwald }
407f4f6c196SMilanka Ringwald }
408a4bfc4feSMatthias Ringwald return 0;
409a4bfc4feSMatthias Ringwald }
410a4bfc4feSMatthias Ringwald
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)411f823745aSMilanka Ringwald void hids_device_init_with_storage(uint8_t hid_country_code, const uint8_t * hid_descriptor, uint16_t hid_descriptor_size,
412f823745aSMilanka Ringwald uint16_t num_reports, hids_device_report_t * report_storage){
413f823745aSMilanka Ringwald
4140235c9e5SMilanka Ringwald hids_device_t * instance = hids_device_create_instance();
415a4bfc4feSMatthias Ringwald
416f823745aSMilanka Ringwald btstack_assert(num_reports > 0);
417f823745aSMilanka Ringwald btstack_assert(report_storage != NULL);
418f823745aSMilanka Ringwald
419f823745aSMilanka Ringwald instance->hid_country_code = hid_country_code;
420f823745aSMilanka Ringwald instance->hid_descriptor = hid_descriptor;
421f823745aSMilanka Ringwald instance->hid_descriptor_size = hid_descriptor_size;
422a4bfc4feSMatthias Ringwald
423a4bfc4feSMatthias Ringwald // default
4240235c9e5SMilanka Ringwald instance->hid_protocol_mode = 1;
425a4bfc4feSMatthias Ringwald
426a4bfc4feSMatthias Ringwald // get service handle range
427a4bfc4feSMatthias Ringwald uint16_t start_handle = 0;
428843359edSMatthias Ringwald uint16_t end_handle = 0xffff;
429c436b760SMilanka Ringwald int service_found = gatt_server_get_handle_range_for_service_with_uuid16(ORG_BLUETOOTH_SERVICE_HUMAN_INTERFACE_DEVICE, &start_handle, &end_handle);
430a2489f29SMatthias Ringwald btstack_assert(service_found != 0);
431a2489f29SMatthias Ringwald UNUSED(service_found);
432a4bfc4feSMatthias Ringwald
433a4bfc4feSMatthias Ringwald // get report map handle
4340235c9e5SMilanka Ringwald instance->hid_report_map_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT_MAP);
435a4bfc4feSMatthias Ringwald
436a4bfc4feSMatthias Ringwald // get report map handle
4370235c9e5SMilanka Ringwald instance->hid_protocol_mode_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_PROTOCOL_MODE);
438a4bfc4feSMatthias Ringwald
439a4bfc4feSMatthias Ringwald // get value and client configuration handles for boot mouse input, boot keyboard input and report input
4400235c9e5SMilanka Ringwald 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);
4410235c9e5SMilanka Ringwald 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);
442a4bfc4feSMatthias Ringwald
4430235c9e5SMilanka Ringwald 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);
4440235c9e5SMilanka Ringwald 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);
445a4bfc4feSMatthias Ringwald
446bbf45f89SMilanka Ringwald 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);
447bbf45f89SMilanka Ringwald
4480235c9e5SMilanka Ringwald 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);
4490235c9e5SMilanka Ringwald
450c9d24807SMilanka Ringwald log_info("hid_report_map_handle 0x%02x", instance->hid_report_map_handle);
451c9d24807SMilanka Ringwald log_info("hid_protocol_mode_value_handle 0x%02x", instance->hid_protocol_mode_value_handle);
452c9d24807SMilanka Ringwald log_info("hid_boot_mouse_input_value_handle 0x%02x", instance->hid_boot_mouse_input_value_handle);
453c9d24807SMilanka Ringwald log_info("hid_boot_mouse_input_client_configuration_handle 0x%02x", instance->hid_boot_mouse_input_client_configuration_handle);
454c9d24807SMilanka Ringwald log_info("hid_boot_keyboard_input_value_handle 0x%02x", instance->hid_boot_keyboard_input_value_handle);
455bbf45f89SMilanka Ringwald log_info("hid_boot_keyboard_output_value_handle 0x%02x", instance->hid_boot_keyboard_output_value_handle);
456c9d24807SMilanka Ringwald log_info("hid_boot_keyboard_input_client_configuration_handle 0x%02x", instance->hid_boot_keyboard_input_client_configuration_handle);
457c9d24807SMilanka Ringwald log_info("hid_control_point_value_handle 0x%02x", instance->hid_control_point_value_handle);
458a4bfc4feSMatthias Ringwald
459f823745aSMilanka Ringwald instance->hid_reports = report_storage;
460f4f6c196SMilanka Ringwald
461f823745aSMilanka Ringwald uint16_t hid_reports_num = num_reports;
462f4f6c196SMilanka Ringwald uint16_t assigned_reports_num = 0;
463f4f6c196SMilanka Ringwald uint16_t start_chr_handle = start_handle;
464f4f6c196SMilanka Ringwald
465f4f6c196SMilanka Ringwald while ( (start_chr_handle < end_handle) && (assigned_reports_num < hid_reports_num)) {
4662c84eaa3SMatthias Ringwald // mandatory
467f4f6c196SMilanka Ringwald uint16_t chr_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_chr_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT);
468f4f6c196SMilanka Ringwald if (chr_value_handle == 0){
469f4f6c196SMilanka Ringwald break;
470f4f6c196SMilanka Ringwald }
471f4f6c196SMilanka Ringwald
4722c84eaa3SMatthias Ringwald // optional
473f4f6c196SMilanka Ringwald 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);
4742c84eaa3SMatthias Ringwald
4752c84eaa3SMatthias Ringwald // mandatory
476f4f6c196SMilanka Ringwald 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);
4772c84eaa3SMatthias Ringwald if (report_reference_handle == 0){
4782c84eaa3SMatthias Ringwald break;
4792c84eaa3SMatthias Ringwald }
4802c84eaa3SMatthias Ringwald
4812c84eaa3SMatthias Ringwald // get report id and type from report reference
482f4f6c196SMilanka Ringwald uint16_t report_reference_value_len;
483f4f6c196SMilanka Ringwald const uint8_t * report_reference_value = gatt_server_get_const_value_for_handle(report_reference_handle, &report_reference_value_len);
484f4f6c196SMilanka Ringwald if (report_reference_value == NULL){
485f4f6c196SMilanka Ringwald break;
486f4f6c196SMilanka Ringwald }
487f4f6c196SMilanka Ringwald if (report_reference_value_len != 2){
488f4f6c196SMilanka Ringwald break;
489f4f6c196SMilanka Ringwald }
4902c84eaa3SMatthias Ringwald uint8_t report_id = report_reference_value[0];
4912c84eaa3SMatthias Ringwald hid_report_type_t report_type = (hid_report_type_t) report_reference_value[1];
492f4f6c196SMilanka Ringwald
4932c84eaa3SMatthias Ringwald // store report info
494160eac51SMatthias Ringwald hids_device_report_t * report = &report_storage[assigned_reports_num];
495f4f6c196SMilanka Ringwald report->value_handle = chr_value_handle;
496f4f6c196SMilanka Ringwald report->client_configuration_handle = chr_client_configuration_handle;
497f4f6c196SMilanka Ringwald report->client_configuration_value = 0;
4982c84eaa3SMatthias Ringwald report->id = report_id;
4992c84eaa3SMatthias Ringwald report->type = report_type;
500*2cca3b08SMatthias Ringwald report->size = btstack_hid_get_report_size_for_id(report_id, report_type, hid_descriptor, hid_descriptor_size);
501f4f6c196SMilanka Ringwald
502f4f6c196SMilanka Ringwald switch (report->type){
503f4f6c196SMilanka Ringwald case HID_REPORT_TYPE_INPUT:
504f4f6c196SMilanka Ringwald instance->hid_input_reports_num++;
505f4f6c196SMilanka Ringwald break;
506f4f6c196SMilanka Ringwald case HID_REPORT_TYPE_OUTPUT:
507f4f6c196SMilanka Ringwald instance->hid_output_reports_num++;
508f4f6c196SMilanka Ringwald break;
509f4f6c196SMilanka Ringwald case HID_REPORT_TYPE_FEATURE:
510f4f6c196SMilanka Ringwald instance->hid_feature_reports_num++;
511f4f6c196SMilanka Ringwald break;
512f4f6c196SMilanka Ringwald default:
513f4f6c196SMilanka Ringwald btstack_unreachable();
514f4f6c196SMilanka Ringwald return;
515f4f6c196SMilanka Ringwald }
516f4f6c196SMilanka Ringwald log_info("hid_report_value_handle 0x%02x, id %u, type %u", report->value_handle, report->id, (uint8_t)report->type);
5172c84eaa3SMatthias Ringwald if (report->client_configuration_handle != 0){
518f4f6c196SMilanka Ringwald log_info("hid_report_client_configuration_handle 0x%02x", report->client_configuration_handle);
5192c84eaa3SMatthias Ringwald }
520f4f6c196SMilanka Ringwald
521f4f6c196SMilanka Ringwald assigned_reports_num++;
5222c84eaa3SMatthias Ringwald start_chr_handle = report_reference_handle + 1;
523f4f6c196SMilanka Ringwald }
524f4f6c196SMilanka Ringwald
5253d71c7a4SMatthias Ringwald // register service with ATT Server
526a4bfc4feSMatthias Ringwald hid_service.start_handle = start_handle;
527a4bfc4feSMatthias Ringwald hid_service.end_handle = end_handle;
528a4bfc4feSMatthias Ringwald hid_service.read_callback = &att_read_callback;
529a4bfc4feSMatthias Ringwald hid_service.write_callback = &att_write_callback;
5303d71c7a4SMatthias Ringwald att_server_register_service_handler(&hid_service);
531a4bfc4feSMatthias Ringwald }
532a4bfc4feSMatthias Ringwald
533a4bfc4feSMatthias Ringwald /**
534f823745aSMilanka Ringwald * @brief Set up HIDS Device
535f823745aSMilanka Ringwald */
hids_device_init(uint8_t country_code,const uint8_t * descriptor,uint16_t descriptor_size)536f823745aSMilanka Ringwald void hids_device_init(uint8_t country_code, const uint8_t * descriptor, uint16_t descriptor_size){
537160eac51SMatthias Ringwald uint16_t hid_reports_num = sizeof(hid_reports_generic_storage) / sizeof(hids_device_report_t);
538160eac51SMatthias Ringwald hids_device_init_with_storage(country_code, descriptor, descriptor_size, hid_reports_num, hid_reports_generic_storage);
539f823745aSMilanka Ringwald }
540f823745aSMilanka Ringwald
541f823745aSMilanka Ringwald /**
542a4bfc4feSMatthias Ringwald * @brief Register callback for the HIDS Device client.
543a4bfc4feSMatthias Ringwald * @param callback
544a4bfc4feSMatthias Ringwald */
hids_device_register_packet_handler(btstack_packet_handler_t callback)545a4bfc4feSMatthias Ringwald void hids_device_register_packet_handler(btstack_packet_handler_t callback){
546a4bfc4feSMatthias Ringwald packet_handler = callback;
547a4bfc4feSMatthias Ringwald }
548a4bfc4feSMatthias Ringwald
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))5491b9864eaSMatthias Ringwald 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)){
5501b9864eaSMatthias Ringwald hids_device_get_report_callback = callback;
5511b9864eaSMatthias Ringwald }
5521b9864eaSMatthias Ringwald
553a4bfc4feSMatthias Ringwald /**
554a4bfc4feSMatthias Ringwald * @brief Request can send now event to send HID Report
555a4bfc4feSMatthias Ringwald * Generates an HIDS_SUBEVENT_CAN_SEND_NOW subevent
556a4bfc4feSMatthias Ringwald * @param hid_cid
557a4bfc4feSMatthias Ringwald */
hids_device_request_can_send_now_event(hci_con_handle_t con_handle)5588e7b5449SMarkus Zehnder uint8_t hids_device_request_can_send_now_event(hci_con_handle_t con_handle){
5590235c9e5SMilanka Ringwald hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
5600235c9e5SMilanka Ringwald if (!instance){
5610235c9e5SMilanka Ringwald log_error("no instance for handle 0x%02x", con_handle);
5628e7b5449SMarkus Zehnder return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
5630235c9e5SMilanka Ringwald }
5640235c9e5SMilanka Ringwald
565086ea75fSMatthias Ringwald instance->can_send_now_callback.callback = &hids_device_can_send_now;
566086ea75fSMatthias Ringwald instance->can_send_now_callback.context = (void*) (uintptr_t) con_handle;
5678e7b5449SMarkus Zehnder return att_server_register_can_send_now_callback(&instance->can_send_now_callback, con_handle);
568a4bfc4feSMatthias Ringwald }
569a4bfc4feSMatthias Ringwald
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)57045569c34SMatthias Ringwald 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){
5710235c9e5SMilanka Ringwald hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
5720235c9e5SMilanka Ringwald if (!instance){
5730235c9e5SMilanka Ringwald log_error("no instance for handle 0x%02x", con_handle);
574f4f6c196SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
5750235c9e5SMilanka Ringwald }
576f4f6c196SMilanka Ringwald
577a3c1c955SMatthias Ringwald hids_device_report_t * report_storage = hids_device_get_report_for_id_and_type(instance, report_id,
578a3c1c955SMatthias Ringwald HID_REPORT_TYPE_INPUT);
579f4f6c196SMilanka Ringwald if (report_storage == NULL){
580f4f6c196SMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
581f4f6c196SMilanka Ringwald }
582f4f6c196SMilanka Ringwald
583f4f6c196SMilanka Ringwald return att_server_notify(con_handle, report_storage->value_handle, report, report_len);
584f4f6c196SMilanka Ringwald }
585f4f6c196SMilanka Ringwald
hids_device_send_input_report(hci_con_handle_t con_handle,const uint8_t * report,uint16_t report_len)586d38dfb7fSMatthias Ringwald uint8_t hids_device_send_input_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){
587a90492ddSMatthias Ringwald hids_device_t * device = hids_device_get_instance_for_con_handle(con_handle);
588a90492ddSMatthias Ringwald if (!device){
589f4f6c196SMilanka Ringwald log_error("no instance for handle 0x%02x", con_handle);
590f4f6c196SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
591f4f6c196SMilanka Ringwald }
592f4f6c196SMilanka Ringwald
593a90492ddSMatthias Ringwald uint8_t pos;
594a90492ddSMatthias Ringwald uint8_t total_reports = device->hid_input_reports_num + device->hid_output_reports_num + device->hid_feature_reports_num;
595a90492ddSMatthias Ringwald for (pos = 0 ; pos < total_reports ; pos++){
596a90492ddSMatthias Ringwald hids_device_report_t * report_storage = &device->hid_reports[pos];
597d38dfb7fSMatthias Ringwald if (report_storage->type == HID_REPORT_TYPE_INPUT){
598f4f6c196SMilanka Ringwald return att_server_notify(con_handle, report_storage->value_handle, report, report_len);
599f4f6c196SMilanka Ringwald }
600f4f6c196SMilanka Ringwald }
601f4f6c196SMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE;
602f4f6c196SMilanka Ringwald }
603f4f6c196SMilanka Ringwald
604d38dfb7fSMatthias Ringwald /**
605d38dfb7fSMatthias Ringwald * @brief Send HID Boot Mouse Input Report
606d38dfb7fSMatthias Ringwald */
hids_device_send_boot_mouse_input_report(hci_con_handle_t con_handle,const uint8_t * report,uint16_t report_len)607d38dfb7fSMatthias Ringwald uint8_t hids_device_send_boot_mouse_input_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){
608d38dfb7fSMatthias Ringwald hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
609d38dfb7fSMatthias Ringwald if (!instance){
610d38dfb7fSMatthias Ringwald log_error("no instance for handle 0x%02x", con_handle);
611d38dfb7fSMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
612d38dfb7fSMatthias Ringwald }
613d38dfb7fSMatthias Ringwald return att_server_notify(con_handle, instance->hid_boot_mouse_input_value_handle, report, report_len);
6140235c9e5SMilanka Ringwald }
6150235c9e5SMilanka Ringwald
6160235c9e5SMilanka Ringwald /**
617a4bfc4feSMatthias Ringwald * @brief Send HID Boot Mouse Input Report
618a4bfc4feSMatthias Ringwald */
hids_device_send_boot_keyboard_input_report(hci_con_handle_t con_handle,const uint8_t * report,uint16_t report_len)619d38dfb7fSMatthias Ringwald uint8_t hids_device_send_boot_keyboard_input_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){
6200235c9e5SMilanka Ringwald hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle);
6210235c9e5SMilanka Ringwald if (!instance){
6220235c9e5SMilanka Ringwald log_error("no instance for handle 0x%02x", con_handle);
623d38dfb7fSMatthias Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
6240235c9e5SMilanka Ringwald }
625d38dfb7fSMatthias Ringwald return att_server_notify(con_handle, instance->hid_boot_keyboard_input_value_handle, report, report_len);
626a4bfc4feSMatthias Ringwald }
627