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 23a4bfc4feSMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24a4bfc4feSMatthias Ringwald * RINGWALD 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" 52a4bfc4feSMatthias Ringwald 530235c9e5SMilanka Ringwald #define HIDS_DEVICE_ERROR_CODE_INAPPROPRIATE_CONNECTION_PARAMETERS 0x80 540235c9e5SMilanka Ringwald 550235c9e5SMilanka Ringwald typedef struct{ 560235c9e5SMilanka Ringwald uint16_t con_handle; 570235c9e5SMilanka Ringwald 580235c9e5SMilanka Ringwald uint8_t hid_country_code; 590235c9e5SMilanka Ringwald const uint8_t * hid_descriptor; 600235c9e5SMilanka Ringwald uint16_t hid_descriptor_size; 610235c9e5SMilanka Ringwald 620235c9e5SMilanka Ringwald uint16_t hid_report_map_handle; 630235c9e5SMilanka Ringwald uint16_t hid_protocol_mode; 640235c9e5SMilanka Ringwald uint16_t hid_protocol_mode_value_handle; 650235c9e5SMilanka Ringwald 660235c9e5SMilanka Ringwald uint16_t hid_boot_mouse_input_value_handle; 670235c9e5SMilanka Ringwald uint16_t hid_boot_mouse_input_client_configuration_handle; 680235c9e5SMilanka Ringwald uint16_t hid_boot_mouse_input_client_configuration_value; 690235c9e5SMilanka Ringwald 700235c9e5SMilanka Ringwald uint16_t hid_boot_keyboard_input_value_handle; 710235c9e5SMilanka Ringwald uint16_t hid_boot_keyboard_input_client_configuration_handle; 720235c9e5SMilanka Ringwald uint16_t hid_boot_keyboard_input_client_configuration_value; 730235c9e5SMilanka Ringwald 740235c9e5SMilanka Ringwald uint16_t hid_report_input_value_handle; 750235c9e5SMilanka Ringwald uint16_t hid_report_input_client_configuration_handle; 760235c9e5SMilanka Ringwald uint16_t hid_report_input_client_configuration_value; 770235c9e5SMilanka Ringwald 780235c9e5SMilanka Ringwald uint16_t hid_report_output_value_handle; 790235c9e5SMilanka Ringwald uint16_t hid_report_output_client_configuration_handle; 800235c9e5SMilanka Ringwald uint16_t hid_report_output_client_configuration_value; 810235c9e5SMilanka Ringwald 820235c9e5SMilanka Ringwald uint16_t hid_report_feature_value_handle; 830235c9e5SMilanka Ringwald uint16_t hid_report_feature_client_configuration_handle; 840235c9e5SMilanka Ringwald uint16_t hid_report_feature_client_configuration_value; 850235c9e5SMilanka Ringwald 860235c9e5SMilanka Ringwald uint16_t hid_control_point_value_handle; 870235c9e5SMilanka Ringwald uint8_t hid_control_point_suspend; 880235c9e5SMilanka Ringwald 890235c9e5SMilanka Ringwald btstack_context_callback_registration_t battery_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; 96a4bfc4feSMatthias Ringwald 970235c9e5SMilanka Ringwald // TODO: store hids device connection into list 980235c9e5SMilanka Ringwald static hids_device_t * hids_device_get_instance_for_con_handle(uint16_t con_handle){ 990235c9e5SMilanka Ringwald UNUSED(con_handle); 1000235c9e5SMilanka Ringwald return &hids_device; 1010235c9e5SMilanka Ringwald } 1020235c9e5SMilanka Ringwald 1030235c9e5SMilanka Ringwald static hids_device_t * hids_device_create_instance(void){ 1040235c9e5SMilanka Ringwald return &hids_device; 1050235c9e5SMilanka Ringwald } 1060235c9e5SMilanka Ringwald 1070235c9e5SMilanka Ringwald 108a4bfc4feSMatthias Ringwald static void hids_device_emit_event_with_uint8(uint8_t event, hci_con_handle_t con_handle, uint8_t value){ 1090235c9e5SMilanka Ringwald hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle); 1100235c9e5SMilanka Ringwald if (!instance){ 1110235c9e5SMilanka Ringwald log_error("no instance for handle 0x%02x", con_handle); 1120235c9e5SMilanka Ringwald return; 1130235c9e5SMilanka Ringwald } 1140235c9e5SMilanka Ringwald 115a4bfc4feSMatthias Ringwald if (!packet_handler) return; 116a4bfc4feSMatthias Ringwald uint8_t buffer[6]; 117a4bfc4feSMatthias Ringwald buffer[0] = HCI_EVENT_HIDS_META; 118a4bfc4feSMatthias Ringwald buffer[1] = 4; 119a4bfc4feSMatthias Ringwald buffer[2] = event; 120a4bfc4feSMatthias Ringwald little_endian_store_16(buffer, 3, (uint16_t) con_handle); 121a4bfc4feSMatthias Ringwald buffer[5] = value; 122a4bfc4feSMatthias Ringwald (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer)); 123a4bfc4feSMatthias Ringwald } 124a4bfc4feSMatthias Ringwald 1250235c9e5SMilanka Ringwald static void hids_device_emit_event(uint8_t event, hci_con_handle_t con_handle){ 1260235c9e5SMilanka Ringwald hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle); 1270235c9e5SMilanka Ringwald if (!instance){ 1280235c9e5SMilanka Ringwald log_error("no instance for handle 0x%02x", con_handle); 1290235c9e5SMilanka Ringwald return; 1300235c9e5SMilanka Ringwald } 1310235c9e5SMilanka Ringwald 1320235c9e5SMilanka Ringwald if (!packet_handler) return; 1330235c9e5SMilanka Ringwald uint8_t buffer[5]; 1340235c9e5SMilanka Ringwald buffer[0] = HCI_EVENT_HIDS_META; 1350235c9e5SMilanka Ringwald buffer[1] = 4; 1360235c9e5SMilanka Ringwald buffer[2] = event; 1370235c9e5SMilanka Ringwald little_endian_store_16(buffer, 3, (uint16_t) con_handle); 1380235c9e5SMilanka Ringwald (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer)); 1390235c9e5SMilanka Ringwald } 1400235c9e5SMilanka Ringwald 141a4bfc4feSMatthias Ringwald static void hids_device_can_send_now(void * context){ 142a4bfc4feSMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context; 143a4bfc4feSMatthias Ringwald // notify client 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[5]; 152a4bfc4feSMatthias Ringwald buffer[0] = HCI_EVENT_HIDS_META; 153a4bfc4feSMatthias Ringwald buffer[1] = 3; 154a4bfc4feSMatthias Ringwald buffer[2] = HIDS_SUBEVENT_CAN_SEND_NOW; 155a4bfc4feSMatthias Ringwald little_endian_store_16(buffer, 3, (uint16_t) con_handle); 156a4bfc4feSMatthias Ringwald (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer)); 157a4bfc4feSMatthias Ringwald } 158a4bfc4feSMatthias Ringwald 159a4bfc4feSMatthias Ringwald // ATT Client Read Callback for Dynamic Data 160a4bfc4feSMatthias Ringwald // - if buffer == NULL, don't copy data, just return size of value 161a4bfc4feSMatthias Ringwald // - if buffer != NULL, copy data and return number bytes copied 1620235c9e5SMilanka 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){ 1630235c9e5SMilanka Ringwald hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle); 1640235c9e5SMilanka Ringwald if (!instance){ 1650235c9e5SMilanka Ringwald log_error("no instance for handle 0x%02x", con_handle); 1660235c9e5SMilanka Ringwald return HIDS_DEVICE_ERROR_CODE_INAPPROPRIATE_CONNECTION_PARAMETERS; 167a4bfc4feSMatthias Ringwald } 1680235c9e5SMilanka Ringwald 1690235c9e5SMilanka Ringwald if (att_handle == instance->hid_protocol_mode_value_handle){ 1700235c9e5SMilanka Ringwald log_info("Read protocol mode"); 1710235c9e5SMilanka Ringwald return att_read_callback_handle_byte(instance->hid_protocol_mode, offset, buffer, buffer_size); 1720235c9e5SMilanka Ringwald } 173c9d24807SMilanka Ringwald 1740235c9e5SMilanka Ringwald if (att_handle == instance->hid_report_map_handle){ 175523edd21SMatthias Ringwald log_info("Read report map"); 1760235c9e5SMilanka Ringwald return att_read_callback_handle_blob(instance->hid_descriptor, instance->hid_descriptor_size, offset, buffer, buffer_size); 177a4bfc4feSMatthias Ringwald } 178c9d24807SMilanka Ringwald 1790235c9e5SMilanka Ringwald if (att_handle == instance->hid_boot_mouse_input_client_configuration_handle){ 1800235c9e5SMilanka Ringwald return att_read_callback_handle_little_endian_16(instance->hid_boot_mouse_input_client_configuration_value, offset, buffer, buffer_size); 181a4bfc4feSMatthias Ringwald } 182c9d24807SMilanka Ringwald 1830235c9e5SMilanka Ringwald if (att_handle == instance->hid_boot_keyboard_input_client_configuration_handle){ 1840235c9e5SMilanka Ringwald return att_read_callback_handle_little_endian_16(instance->hid_boot_keyboard_input_client_configuration_value, offset, buffer, buffer_size); 185a4bfc4feSMatthias Ringwald } 186c9d24807SMilanka Ringwald 1870235c9e5SMilanka Ringwald if (att_handle == instance->hid_report_input_client_configuration_handle){ 1880235c9e5SMilanka Ringwald return att_read_callback_handle_little_endian_16(instance->hid_report_input_client_configuration_value, offset, buffer, buffer_size); 1890235c9e5SMilanka Ringwald } 190c9d24807SMilanka Ringwald 191826b57efSMatthias Ringwald if (att_handle == instance->hid_report_output_client_configuration_handle){ 1920235c9e5SMilanka Ringwald return att_read_callback_handle_little_endian_16(instance->hid_report_output_client_configuration_value, offset, buffer, buffer_size); 1930235c9e5SMilanka Ringwald } 194c9d24807SMilanka Ringwald 195826b57efSMatthias Ringwald if (att_handle == instance->hid_report_feature_client_configuration_handle){ 1960235c9e5SMilanka Ringwald return att_read_callback_handle_little_endian_16(instance->hid_report_feature_client_configuration_value, offset, buffer, buffer_size); 1970235c9e5SMilanka Ringwald } 198c9d24807SMilanka Ringwald 1990235c9e5SMilanka Ringwald if (att_handle == instance->hid_control_point_value_handle){ 2004ea43905SMatthias Ringwald if (buffer && (buffer_size >= 1u)){ 2010235c9e5SMilanka Ringwald buffer[0] = instance->hid_control_point_suspend; 2020235c9e5SMilanka Ringwald } 2030235c9e5SMilanka Ringwald return 1; 204a4bfc4feSMatthias Ringwald } 205a4bfc4feSMatthias Ringwald return 0; 206a4bfc4feSMatthias Ringwald } 207a4bfc4feSMatthias Ringwald 208a4bfc4feSMatthias 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){ 209a4bfc4feSMatthias Ringwald UNUSED(transaction_mode); 210a4bfc4feSMatthias Ringwald UNUSED(buffer_size); 211a4bfc4feSMatthias Ringwald UNUSED(offset); 212a4bfc4feSMatthias Ringwald 2130235c9e5SMilanka Ringwald hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle); 2140235c9e5SMilanka Ringwald if (!instance){ 2150235c9e5SMilanka Ringwald log_error("no instance for handle 0x%02x", con_handle); 2160235c9e5SMilanka Ringwald return HIDS_DEVICE_ERROR_CODE_INAPPROPRIATE_CONNECTION_PARAMETERS; 2170235c9e5SMilanka Ringwald } 2180235c9e5SMilanka Ringwald 2190235c9e5SMilanka Ringwald if (att_handle == instance->hid_boot_mouse_input_client_configuration_handle){ 22085828295SMatthias Ringwald uint16_t new_value = little_endian_read_16(buffer, 0); 2210235c9e5SMilanka Ringwald instance->hid_boot_mouse_input_client_configuration_value = new_value; 2220235c9e5SMilanka Ringwald hids_device_emit_event_with_uint8(HIDS_SUBEVENT_BOOT_MOUSE_INPUT_REPORT_ENABLE, con_handle, new_value); 223a4bfc4feSMatthias Ringwald } 2240235c9e5SMilanka Ringwald if (att_handle == instance->hid_boot_keyboard_input_client_configuration_handle){ 22585828295SMatthias Ringwald uint16_t new_value = little_endian_read_16(buffer, 0); 2260235c9e5SMilanka Ringwald instance->hid_boot_keyboard_input_client_configuration_value = new_value; 2270235c9e5SMilanka Ringwald hids_device_emit_event_with_uint8(HIDS_SUBEVENT_BOOT_KEYBOARD_INPUT_REPORT_ENABLE, con_handle, new_value); 228a4bfc4feSMatthias Ringwald } 2290235c9e5SMilanka Ringwald if (att_handle == instance->hid_report_input_client_configuration_handle){ 23085828295SMatthias Ringwald uint16_t new_value = little_endian_read_16(buffer, 0); 2310235c9e5SMilanka Ringwald instance->hid_report_input_client_configuration_value = new_value; 23285828295SMatthias Ringwald log_info("Enable Report Input notifications: %x", new_value); 2330235c9e5SMilanka Ringwald hids_device_emit_event_with_uint8(HIDS_SUBEVENT_INPUT_REPORT_ENABLE, con_handle, new_value); 234a4bfc4feSMatthias Ringwald } 2350235c9e5SMilanka Ringwald if (att_handle == instance->hid_report_output_client_configuration_handle){ 2360235c9e5SMilanka Ringwald uint16_t new_value = little_endian_read_16(buffer, 0); 2370235c9e5SMilanka Ringwald instance->hid_report_output_client_configuration_value = new_value; 2380235c9e5SMilanka Ringwald log_info("Enable Report Output notifications: %x", new_value); 2390235c9e5SMilanka Ringwald hids_device_emit_event_with_uint8(HIDS_SUBEVENT_OUTPUT_REPORT_ENABLE, con_handle, new_value); 240a4bfc4feSMatthias Ringwald } 2410235c9e5SMilanka Ringwald if (att_handle == instance->hid_report_feature_client_configuration_handle){ 2420235c9e5SMilanka Ringwald uint16_t new_value = little_endian_read_16(buffer, 0); 2430235c9e5SMilanka Ringwald instance->hid_report_feature_client_configuration_value = new_value; 2440235c9e5SMilanka Ringwald log_info("Enable Report Feature notifications: %x", new_value); 2450235c9e5SMilanka Ringwald hids_device_emit_event_with_uint8(HIDS_SUBEVENT_FEATURE_REPORT_ENABLE, con_handle, new_value); 2460235c9e5SMilanka Ringwald } 2470235c9e5SMilanka Ringwald 2480235c9e5SMilanka Ringwald if (att_handle == instance->hid_protocol_mode_value_handle){ 2490235c9e5SMilanka Ringwald instance->hid_protocol_mode = buffer[0]; 2500235c9e5SMilanka Ringwald log_info("Set protocol mode: %u", instance->hid_protocol_mode); 2510235c9e5SMilanka Ringwald hids_device_emit_event_with_uint8(HIDS_SUBEVENT_PROTOCOL_MODE, con_handle, instance->hid_protocol_mode); 2520235c9e5SMilanka Ringwald } 2530235c9e5SMilanka Ringwald 2540235c9e5SMilanka Ringwald if (att_handle == instance->hid_control_point_value_handle){ 2554ea43905SMatthias Ringwald if (buffer_size < 1u){ 2560235c9e5SMilanka Ringwald return ATT_ERROR_INVALID_OFFSET; 2570235c9e5SMilanka Ringwald } 2580235c9e5SMilanka Ringwald instance->hid_control_point_suspend = buffer[0]; 2590235c9e5SMilanka Ringwald instance->con_handle = con_handle; 2600235c9e5SMilanka Ringwald log_info("Set suspend tp: %u", instance->hid_control_point_suspend ); 2614ea43905SMatthias Ringwald if (instance->hid_control_point_suspend == 0u){ 2620235c9e5SMilanka Ringwald hids_device_emit_event(HIDS_SUBEVENT_SUSPEND, con_handle); 2634ea43905SMatthias Ringwald } else if (instance->hid_control_point_suspend == 1u){ 2640235c9e5SMilanka Ringwald hids_device_emit_event(HIDS_SUBEVENT_EXIT_SUSPEND, con_handle); 2650235c9e5SMilanka Ringwald } 2660235c9e5SMilanka Ringwald } 267a4bfc4feSMatthias Ringwald return 0; 268a4bfc4feSMatthias Ringwald } 269a4bfc4feSMatthias Ringwald 270a4bfc4feSMatthias Ringwald /** 271a4bfc4feSMatthias Ringwald * @brief Set up HIDS Device 272a4bfc4feSMatthias Ringwald */ 273a4bfc4feSMatthias Ringwald void hids_device_init(uint8_t country_code, const uint8_t * descriptor, uint16_t descriptor_size){ 2740235c9e5SMilanka Ringwald hids_device_t * instance = hids_device_create_instance(); 2750235c9e5SMilanka Ringwald if (!instance){ 2760235c9e5SMilanka Ringwald log_error("hids_device_init: instance could not be created, not enough memory"); 2770235c9e5SMilanka Ringwald return; 2780235c9e5SMilanka Ringwald } 279a4bfc4feSMatthias Ringwald 2800235c9e5SMilanka Ringwald instance->hid_country_code = country_code; 2810235c9e5SMilanka Ringwald instance->hid_descriptor = descriptor; 2820235c9e5SMilanka Ringwald instance->hid_descriptor_size = descriptor_size; 283a4bfc4feSMatthias Ringwald 284a4bfc4feSMatthias Ringwald // default 2850235c9e5SMilanka Ringwald instance->hid_protocol_mode = 1; 286a4bfc4feSMatthias Ringwald 287a4bfc4feSMatthias Ringwald // get service handle range 288a4bfc4feSMatthias Ringwald uint16_t start_handle = 0; 289a4bfc4feSMatthias Ringwald uint16_t end_handle = 0xfff; 290a4bfc4feSMatthias Ringwald int service_found = gatt_server_get_get_handle_range_for_service_with_uuid16(ORG_BLUETOOTH_SERVICE_HUMAN_INTERFACE_DEVICE, &start_handle, &end_handle); 291*a2489f29SMatthias Ringwald btstack_assert(service_found != 0); 292*a2489f29SMatthias Ringwald UNUSED(service_found); 293a4bfc4feSMatthias Ringwald 294a4bfc4feSMatthias Ringwald // get report map handle 2950235c9e5SMilanka Ringwald instance->hid_report_map_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT_MAP); 296a4bfc4feSMatthias Ringwald 297a4bfc4feSMatthias Ringwald // get report map handle 2980235c9e5SMilanka 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); 299a4bfc4feSMatthias Ringwald 300a4bfc4feSMatthias Ringwald // get value and client configuration handles for boot mouse input, boot keyboard input and report input 3010235c9e5SMilanka 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); 3020235c9e5SMilanka 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); 303a4bfc4feSMatthias Ringwald 3040235c9e5SMilanka 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); 3050235c9e5SMilanka 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); 306a4bfc4feSMatthias Ringwald 3070235c9e5SMilanka Ringwald instance->hid_report_input_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT); 3080235c9e5SMilanka Ringwald instance->hid_report_input_client_configuration_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT); 3090235c9e5SMilanka Ringwald 3104ea43905SMatthias Ringwald instance->hid_report_output_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(instance->hid_report_input_client_configuration_handle+1u, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT); 3110235c9e5SMilanka Ringwald instance->hid_report_output_client_configuration_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(instance->hid_report_input_client_configuration_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT); 3120235c9e5SMilanka Ringwald 3134ea43905SMatthias Ringwald instance->hid_report_feature_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(instance->hid_report_output_client_configuration_handle+1u, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT); 3140235c9e5SMilanka Ringwald instance->hid_report_feature_client_configuration_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(instance->hid_report_output_client_configuration_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT); 3150235c9e5SMilanka Ringwald 3160235c9e5SMilanka 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); 3170235c9e5SMilanka Ringwald 318c9d24807SMilanka Ringwald log_info("hid_report_map_handle 0x%02x", instance->hid_report_map_handle); 319c9d24807SMilanka Ringwald log_info("hid_protocol_mode_value_handle 0x%02x", instance->hid_protocol_mode_value_handle); 320c9d24807SMilanka Ringwald log_info("hid_boot_mouse_input_value_handle 0x%02x", instance->hid_boot_mouse_input_value_handle); 321c9d24807SMilanka Ringwald log_info("hid_boot_mouse_input_client_configuration_handle 0x%02x", instance->hid_boot_mouse_input_client_configuration_handle); 322c9d24807SMilanka Ringwald log_info("hid_boot_keyboard_input_value_handle 0x%02x", instance->hid_boot_keyboard_input_value_handle); 323c9d24807SMilanka Ringwald log_info("hid_boot_keyboard_input_client_configuration_handle 0x%02x", instance->hid_boot_keyboard_input_client_configuration_handle); 324c9d24807SMilanka Ringwald log_info("hid_report_input_value_handle 0x%02x", instance->hid_report_input_value_handle); 325c9d24807SMilanka Ringwald log_info("hid_report_input_client_configuration_handle 0x%02x", instance->hid_report_input_client_configuration_handle); 326c9d24807SMilanka Ringwald log_info("hid_report_output_value_handle 0x%02x", instance->hid_report_output_value_handle); 327c9d24807SMilanka Ringwald log_info("hid_report_output_client_configuration_handle 0x%02x", instance->hid_report_output_client_configuration_handle); 328c9d24807SMilanka Ringwald log_info("hid_report_feature_value_handle 0x%02x", instance->hid_report_feature_value_handle); 329c9d24807SMilanka Ringwald log_info("hid_report_feature_client_configuration_handle 0x%02x", instance->hid_report_feature_client_configuration_handle); 330c9d24807SMilanka Ringwald log_info("hid_control_point_value_handle 0x%02x", instance->hid_control_point_value_handle); 331a4bfc4feSMatthias Ringwald 3323d71c7a4SMatthias Ringwald // register service with ATT Server 333a4bfc4feSMatthias Ringwald hid_service.start_handle = start_handle; 334a4bfc4feSMatthias Ringwald hid_service.end_handle = end_handle; 335a4bfc4feSMatthias Ringwald hid_service.read_callback = &att_read_callback; 336a4bfc4feSMatthias Ringwald hid_service.write_callback = &att_write_callback; 3373d71c7a4SMatthias Ringwald att_server_register_service_handler(&hid_service); 338a4bfc4feSMatthias Ringwald } 339a4bfc4feSMatthias Ringwald 340a4bfc4feSMatthias Ringwald /** 341a4bfc4feSMatthias Ringwald * @brief Register callback for the HIDS Device client. 342a4bfc4feSMatthias Ringwald * @param callback 343a4bfc4feSMatthias Ringwald */ 344a4bfc4feSMatthias Ringwald void hids_device_register_packet_handler(btstack_packet_handler_t callback){ 345a4bfc4feSMatthias Ringwald packet_handler = callback; 346a4bfc4feSMatthias Ringwald } 347a4bfc4feSMatthias Ringwald 348a4bfc4feSMatthias Ringwald /** 349a4bfc4feSMatthias Ringwald * @brief Request can send now event to send HID Report 350a4bfc4feSMatthias Ringwald * Generates an HIDS_SUBEVENT_CAN_SEND_NOW subevent 351a4bfc4feSMatthias Ringwald * @param hid_cid 352a4bfc4feSMatthias Ringwald */ 353a4bfc4feSMatthias Ringwald void hids_device_request_can_send_now_event(hci_con_handle_t con_handle){ 3540235c9e5SMilanka Ringwald hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle); 3550235c9e5SMilanka Ringwald if (!instance){ 3560235c9e5SMilanka Ringwald log_error("no instance for handle 0x%02x", con_handle); 3570235c9e5SMilanka Ringwald return; 3580235c9e5SMilanka Ringwald } 3590235c9e5SMilanka Ringwald 3600235c9e5SMilanka Ringwald instance->battery_callback.callback = &hids_device_can_send_now; 3610235c9e5SMilanka Ringwald instance->battery_callback.context = (void*) (uintptr_t) con_handle; 3620235c9e5SMilanka Ringwald att_server_register_can_send_now_callback(&instance->battery_callback, con_handle); 363a4bfc4feSMatthias Ringwald } 364a4bfc4feSMatthias Ringwald 365a4bfc4feSMatthias Ringwald /** 366a4bfc4feSMatthias Ringwald * @brief Send HID Report: Input 367a4bfc4feSMatthias Ringwald */ 368a4bfc4feSMatthias Ringwald void hids_device_send_input_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){ 3690235c9e5SMilanka Ringwald hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle); 3700235c9e5SMilanka Ringwald if (!instance){ 3710235c9e5SMilanka Ringwald log_error("no instance for handle 0x%02x", con_handle); 3720235c9e5SMilanka Ringwald return; 3730235c9e5SMilanka Ringwald } 374842f9974SMilanka Ringwald att_server_notify(con_handle, instance->hid_report_input_value_handle, report, report_len); 3750235c9e5SMilanka Ringwald } 3760235c9e5SMilanka Ringwald 3770235c9e5SMilanka Ringwald /** 3780235c9e5SMilanka Ringwald * @brief Send HID Report: Output 3790235c9e5SMilanka Ringwald */ 3800235c9e5SMilanka Ringwald void hids_device_send_output_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){ 3810235c9e5SMilanka Ringwald hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle); 3820235c9e5SMilanka Ringwald if (!instance){ 3830235c9e5SMilanka Ringwald log_error("no instance for handle 0x%02x", con_handle); 3840235c9e5SMilanka Ringwald return; 3850235c9e5SMilanka Ringwald } 386842f9974SMilanka Ringwald att_server_notify(con_handle, instance->hid_report_output_value_handle, report, report_len); 3870235c9e5SMilanka Ringwald } 3880235c9e5SMilanka Ringwald 3890235c9e5SMilanka Ringwald /** 3900235c9e5SMilanka Ringwald * @brief Send HID Report: Feature 3910235c9e5SMilanka Ringwald */ 3920235c9e5SMilanka Ringwald void hids_device_send_feature_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){ 3930235c9e5SMilanka Ringwald hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle); 3940235c9e5SMilanka Ringwald if (!instance){ 3950235c9e5SMilanka Ringwald log_error("no instance for handle 0x%02x", con_handle); 3960235c9e5SMilanka Ringwald return; 3970235c9e5SMilanka Ringwald } 398842f9974SMilanka Ringwald att_server_notify(con_handle, instance->hid_report_feature_value_handle, report, report_len); 399a4bfc4feSMatthias Ringwald } 400a4bfc4feSMatthias Ringwald 401a4bfc4feSMatthias Ringwald /** 402a4bfc4feSMatthias Ringwald * @brief Send HID Boot Mouse Input Report 403a4bfc4feSMatthias Ringwald */ 404a4bfc4feSMatthias Ringwald void hids_device_send_boot_mouse_input_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){ 4050235c9e5SMilanka Ringwald hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle); 4060235c9e5SMilanka Ringwald if (!instance){ 4070235c9e5SMilanka Ringwald log_error("no instance for handle 0x%02x", con_handle); 4080235c9e5SMilanka Ringwald return; 4090235c9e5SMilanka Ringwald } 410842f9974SMilanka Ringwald att_server_notify(con_handle, instance->hid_boot_mouse_input_value_handle, report, report_len); 411a4bfc4feSMatthias Ringwald } 412a4bfc4feSMatthias Ringwald 413a4bfc4feSMatthias Ringwald /** 414a4bfc4feSMatthias Ringwald * @brief Send HID Boot Mouse Input Report 415a4bfc4feSMatthias Ringwald */ 416a4bfc4feSMatthias Ringwald void hids_device_send_boot_keyboard_input_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){ 4170235c9e5SMilanka Ringwald hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle); 4180235c9e5SMilanka Ringwald if (!instance){ 4190235c9e5SMilanka Ringwald log_error("no instance for handle 0x%02x", con_handle); 4200235c9e5SMilanka Ringwald return; 4210235c9e5SMilanka Ringwald } 422842f9974SMilanka Ringwald att_server_notify(con_handle, instance->hid_boot_keyboard_input_value_handle, report, report_len); 423a4bfc4feSMatthias Ringwald } 424