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" 52a4bfc4feSMatthias Ringwald 530235c9e5SMilanka Ringwald #define HIDS_DEVICE_ERROR_CODE_INAPPROPRIATE_CONNECTION_PARAMETERS 0x80 540235c9e5SMilanka Ringwald 55*f4f6c196SMilanka Ringwald static hids_device_report_t hid_reports_storage[3]; 56*f4f6c196SMilanka Ringwald 570235c9e5SMilanka Ringwald typedef struct{ 580235c9e5SMilanka Ringwald uint16_t con_handle; 590235c9e5SMilanka Ringwald 600235c9e5SMilanka Ringwald uint8_t hid_country_code; 610235c9e5SMilanka Ringwald const uint8_t * hid_descriptor; 620235c9e5SMilanka Ringwald uint16_t hid_descriptor_size; 630235c9e5SMilanka Ringwald 640235c9e5SMilanka Ringwald uint16_t hid_report_map_handle; 65c1dbba9dSMatthias Ringwald uint8_t hid_protocol_mode; 660235c9e5SMilanka Ringwald uint16_t hid_protocol_mode_value_handle; 670235c9e5SMilanka Ringwald 680235c9e5SMilanka Ringwald uint16_t hid_boot_mouse_input_value_handle; 690235c9e5SMilanka Ringwald uint16_t hid_boot_mouse_input_client_configuration_handle; 700235c9e5SMilanka Ringwald uint16_t hid_boot_mouse_input_client_configuration_value; 710235c9e5SMilanka Ringwald 720235c9e5SMilanka Ringwald uint16_t hid_boot_keyboard_input_value_handle; 730235c9e5SMilanka Ringwald uint16_t hid_boot_keyboard_input_client_configuration_handle; 740235c9e5SMilanka Ringwald uint16_t hid_boot_keyboard_input_client_configuration_value; 750235c9e5SMilanka Ringwald 76*f4f6c196SMilanka Ringwald hids_device_report_t * hid_reports; 770235c9e5SMilanka Ringwald 78*f4f6c196SMilanka Ringwald uint8_t hid_input_reports_num; 79*f4f6c196SMilanka Ringwald uint8_t hid_output_reports_num; 80*f4f6c196SMilanka Ringwald uint8_t hid_feature_reports_num; 810235c9e5SMilanka Ringwald 820235c9e5SMilanka Ringwald uint16_t hid_control_point_value_handle; 830235c9e5SMilanka Ringwald uint8_t hid_control_point_suspend; 840235c9e5SMilanka Ringwald 850235c9e5SMilanka Ringwald btstack_context_callback_registration_t battery_callback; 860235c9e5SMilanka Ringwald } hids_device_t; 870235c9e5SMilanka Ringwald 880235c9e5SMilanka Ringwald static hids_device_t hids_device; 890235c9e5SMilanka Ringwald 90a4bfc4feSMatthias Ringwald static btstack_packet_handler_t packet_handler; 91a4bfc4feSMatthias Ringwald static att_service_handler_t hid_service; 92a4bfc4feSMatthias Ringwald 930235c9e5SMilanka Ringwald // TODO: store hids device connection into list 940235c9e5SMilanka Ringwald static hids_device_t * hids_device_get_instance_for_con_handle(uint16_t con_handle){ 950235c9e5SMilanka Ringwald UNUSED(con_handle); 960235c9e5SMilanka Ringwald return &hids_device; 970235c9e5SMilanka Ringwald } 980235c9e5SMilanka Ringwald 990235c9e5SMilanka Ringwald static hids_device_t * hids_device_create_instance(void){ 100*f4f6c196SMilanka Ringwald memset(&hids_device, 0, sizeof(hids_device_t)); 1010235c9e5SMilanka Ringwald return &hids_device; 1020235c9e5SMilanka Ringwald } 1030235c9e5SMilanka Ringwald 104*f4f6c196SMilanka Ringwald static hids_device_report_t * hids_device_get_report_for_client_configuration_handle(hids_device_t * device, uint16_t client_configuration_handle){ 105*f4f6c196SMilanka Ringwald uint8_t pos = 0; 106*f4f6c196SMilanka Ringwald while (pos < (device->hid_input_reports_num + device->hid_output_reports_num + device->hid_feature_reports_num)){ 107*f4f6c196SMilanka Ringwald if (device->hid_reports[pos].client_configuration_handle == client_configuration_handle){ 108*f4f6c196SMilanka Ringwald return &device->hid_reports[pos]; 109*f4f6c196SMilanka Ringwald } 110*f4f6c196SMilanka Ringwald } 111*f4f6c196SMilanka Ringwald return NULL; 112*f4f6c196SMilanka Ringwald } 113*f4f6c196SMilanka Ringwald 114*f4f6c196SMilanka Ringwald static hids_device_report_t * hids_device_get_report_for_id(hids_device_t * device, uint16_t report_id){ 115*f4f6c196SMilanka Ringwald uint8_t pos = 0; 116*f4f6c196SMilanka Ringwald while (pos < (device->hid_input_reports_num + device->hid_output_reports_num + device->hid_feature_reports_num)){ 117*f4f6c196SMilanka Ringwald if (device->hid_reports[pos].id == report_id){ 118*f4f6c196SMilanka Ringwald return &device->hid_reports[pos]; 119*f4f6c196SMilanka Ringwald } 120*f4f6c196SMilanka Ringwald } 121*f4f6c196SMilanka Ringwald return NULL; 122*f4f6c196SMilanka Ringwald } 1230235c9e5SMilanka Ringwald 124a4bfc4feSMatthias Ringwald static void hids_device_emit_event_with_uint8(uint8_t event, hci_con_handle_t con_handle, uint8_t value){ 1250235c9e5SMilanka Ringwald hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle); 1260235c9e5SMilanka Ringwald if (!instance){ 1270235c9e5SMilanka Ringwald log_error("no instance for handle 0x%02x", con_handle); 1280235c9e5SMilanka Ringwald return; 1290235c9e5SMilanka Ringwald } 1300235c9e5SMilanka Ringwald 131a4bfc4feSMatthias Ringwald if (!packet_handler) return; 132a4bfc4feSMatthias Ringwald uint8_t buffer[6]; 133a4bfc4feSMatthias Ringwald buffer[0] = HCI_EVENT_HIDS_META; 134a4bfc4feSMatthias Ringwald buffer[1] = 4; 135a4bfc4feSMatthias Ringwald buffer[2] = event; 136a4bfc4feSMatthias Ringwald little_endian_store_16(buffer, 3, (uint16_t) con_handle); 137a4bfc4feSMatthias Ringwald buffer[5] = value; 138a4bfc4feSMatthias Ringwald (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer)); 139a4bfc4feSMatthias Ringwald } 140a4bfc4feSMatthias Ringwald 1410235c9e5SMilanka Ringwald static void hids_device_emit_event(uint8_t event, hci_con_handle_t con_handle){ 1420235c9e5SMilanka Ringwald hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle); 1430235c9e5SMilanka Ringwald if (!instance){ 1440235c9e5SMilanka Ringwald log_error("no instance for handle 0x%02x", con_handle); 1450235c9e5SMilanka Ringwald return; 1460235c9e5SMilanka Ringwald } 1470235c9e5SMilanka Ringwald 1480235c9e5SMilanka Ringwald if (!packet_handler) return; 1490235c9e5SMilanka Ringwald uint8_t buffer[5]; 1500235c9e5SMilanka Ringwald buffer[0] = HCI_EVENT_HIDS_META; 1510235c9e5SMilanka Ringwald buffer[1] = 4; 1520235c9e5SMilanka Ringwald buffer[2] = event; 1530235c9e5SMilanka Ringwald little_endian_store_16(buffer, 3, (uint16_t) con_handle); 1540235c9e5SMilanka Ringwald (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer)); 1550235c9e5SMilanka Ringwald } 1560235c9e5SMilanka Ringwald 157a4bfc4feSMatthias Ringwald static void hids_device_can_send_now(void * context){ 158a4bfc4feSMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context; 159a4bfc4feSMatthias Ringwald // notify client 1600235c9e5SMilanka Ringwald hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle); 1610235c9e5SMilanka Ringwald if (!instance){ 1620235c9e5SMilanka Ringwald log_error("no instance for handle 0x%02x", con_handle); 1630235c9e5SMilanka Ringwald return; 1640235c9e5SMilanka Ringwald } 1650235c9e5SMilanka Ringwald 166a4bfc4feSMatthias Ringwald if (!packet_handler) return; 167a4bfc4feSMatthias Ringwald uint8_t buffer[5]; 168a4bfc4feSMatthias Ringwald buffer[0] = HCI_EVENT_HIDS_META; 169a4bfc4feSMatthias Ringwald buffer[1] = 3; 170a4bfc4feSMatthias Ringwald buffer[2] = HIDS_SUBEVENT_CAN_SEND_NOW; 171a4bfc4feSMatthias Ringwald little_endian_store_16(buffer, 3, (uint16_t) con_handle); 172a4bfc4feSMatthias Ringwald (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer)); 173a4bfc4feSMatthias Ringwald } 174a4bfc4feSMatthias Ringwald 175a4bfc4feSMatthias Ringwald // ATT Client Read Callback for Dynamic Data 176a4bfc4feSMatthias Ringwald // - if buffer == NULL, don't copy data, just return size of value 177a4bfc4feSMatthias Ringwald // - if buffer != NULL, copy data and return number bytes copied 1780235c9e5SMilanka 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){ 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 HIDS_DEVICE_ERROR_CODE_INAPPROPRIATE_CONNECTION_PARAMETERS; 183a4bfc4feSMatthias Ringwald } 1840235c9e5SMilanka Ringwald 1850235c9e5SMilanka Ringwald if (att_handle == instance->hid_protocol_mode_value_handle){ 1860235c9e5SMilanka Ringwald log_info("Read protocol mode"); 1870235c9e5SMilanka Ringwald return att_read_callback_handle_byte(instance->hid_protocol_mode, offset, buffer, buffer_size); 1880235c9e5SMilanka Ringwald } 189c9d24807SMilanka Ringwald 1900235c9e5SMilanka Ringwald if (att_handle == instance->hid_report_map_handle){ 191523edd21SMatthias Ringwald log_info("Read report map"); 1920235c9e5SMilanka Ringwald return att_read_callback_handle_blob(instance->hid_descriptor, instance->hid_descriptor_size, offset, buffer, buffer_size); 193a4bfc4feSMatthias Ringwald } 194c9d24807SMilanka Ringwald 1950235c9e5SMilanka Ringwald if (att_handle == instance->hid_boot_mouse_input_client_configuration_handle){ 1960235c9e5SMilanka Ringwald return att_read_callback_handle_little_endian_16(instance->hid_boot_mouse_input_client_configuration_value, offset, buffer, buffer_size); 197a4bfc4feSMatthias Ringwald } 198c9d24807SMilanka Ringwald 1990235c9e5SMilanka Ringwald if (att_handle == instance->hid_boot_keyboard_input_client_configuration_handle){ 2000235c9e5SMilanka Ringwald return att_read_callback_handle_little_endian_16(instance->hid_boot_keyboard_input_client_configuration_value, offset, buffer, buffer_size); 201a4bfc4feSMatthias Ringwald } 202c9d24807SMilanka Ringwald 2030235c9e5SMilanka Ringwald if (att_handle == instance->hid_control_point_value_handle){ 2044ea43905SMatthias Ringwald if (buffer && (buffer_size >= 1u)){ 2050235c9e5SMilanka Ringwald buffer[0] = instance->hid_control_point_suspend; 2060235c9e5SMilanka Ringwald } 2070235c9e5SMilanka Ringwald return 1; 208a4bfc4feSMatthias Ringwald } 209*f4f6c196SMilanka Ringwald 210*f4f6c196SMilanka Ringwald hids_device_report_t * report = hids_device_get_report_for_client_configuration_handle(instance, att_handle); 211*f4f6c196SMilanka Ringwald if (report != NULL){ 212*f4f6c196SMilanka Ringwald return att_read_callback_handle_little_endian_16(report->client_configuration_value, offset, buffer, buffer_size); 213*f4f6c196SMilanka Ringwald } 214a4bfc4feSMatthias Ringwald return 0; 215a4bfc4feSMatthias Ringwald } 216a4bfc4feSMatthias Ringwald 217a4bfc4feSMatthias 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){ 218a4bfc4feSMatthias Ringwald UNUSED(transaction_mode); 219a4bfc4feSMatthias Ringwald UNUSED(buffer_size); 220a4bfc4feSMatthias Ringwald UNUSED(offset); 221a4bfc4feSMatthias Ringwald 2220235c9e5SMilanka Ringwald hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle); 2230235c9e5SMilanka Ringwald if (!instance){ 2240235c9e5SMilanka Ringwald log_error("no instance for handle 0x%02x", con_handle); 2250235c9e5SMilanka Ringwald return HIDS_DEVICE_ERROR_CODE_INAPPROPRIATE_CONNECTION_PARAMETERS; 2260235c9e5SMilanka Ringwald } 2270235c9e5SMilanka Ringwald 2280235c9e5SMilanka Ringwald if (att_handle == instance->hid_boot_mouse_input_client_configuration_handle){ 22985828295SMatthias Ringwald uint16_t new_value = little_endian_read_16(buffer, 0); 2300235c9e5SMilanka Ringwald instance->hid_boot_mouse_input_client_configuration_value = new_value; 231c1dbba9dSMatthias Ringwald hids_device_emit_event_with_uint8(HIDS_SUBEVENT_BOOT_MOUSE_INPUT_REPORT_ENABLE, con_handle, (uint8_t) new_value); 232a4bfc4feSMatthias Ringwald } 2330235c9e5SMilanka Ringwald if (att_handle == instance->hid_boot_keyboard_input_client_configuration_handle){ 23485828295SMatthias Ringwald uint16_t new_value = little_endian_read_16(buffer, 0); 2350235c9e5SMilanka Ringwald instance->hid_boot_keyboard_input_client_configuration_value = new_value; 236c1dbba9dSMatthias Ringwald hids_device_emit_event_with_uint8(HIDS_SUBEVENT_BOOT_KEYBOARD_INPUT_REPORT_ENABLE, con_handle, (uint8_t) new_value); 237a4bfc4feSMatthias Ringwald } 2380235c9e5SMilanka Ringwald 2390235c9e5SMilanka Ringwald if (att_handle == instance->hid_protocol_mode_value_handle){ 2400235c9e5SMilanka Ringwald instance->hid_protocol_mode = buffer[0]; 2410235c9e5SMilanka Ringwald log_info("Set protocol mode: %u", instance->hid_protocol_mode); 2420235c9e5SMilanka Ringwald hids_device_emit_event_with_uint8(HIDS_SUBEVENT_PROTOCOL_MODE, con_handle, instance->hid_protocol_mode); 2430235c9e5SMilanka Ringwald } 2440235c9e5SMilanka Ringwald 2450235c9e5SMilanka Ringwald if (att_handle == instance->hid_control_point_value_handle){ 2464ea43905SMatthias Ringwald if (buffer_size < 1u){ 2470235c9e5SMilanka Ringwald return ATT_ERROR_INVALID_OFFSET; 2480235c9e5SMilanka Ringwald } 2490235c9e5SMilanka Ringwald instance->hid_control_point_suspend = buffer[0]; 2500235c9e5SMilanka Ringwald instance->con_handle = con_handle; 2510235c9e5SMilanka Ringwald log_info("Set suspend tp: %u", instance->hid_control_point_suspend ); 2524ea43905SMatthias Ringwald if (instance->hid_control_point_suspend == 0u){ 2530235c9e5SMilanka Ringwald hids_device_emit_event(HIDS_SUBEVENT_SUSPEND, con_handle); 2544ea43905SMatthias Ringwald } else if (instance->hid_control_point_suspend == 1u){ 2550235c9e5SMilanka Ringwald hids_device_emit_event(HIDS_SUBEVENT_EXIT_SUSPEND, con_handle); 2560235c9e5SMilanka Ringwald } 2570235c9e5SMilanka Ringwald } 258*f4f6c196SMilanka Ringwald 259*f4f6c196SMilanka Ringwald hids_device_report_t * report = hids_device_get_report_for_client_configuration_handle(instance, att_handle); 260*f4f6c196SMilanka Ringwald if (report != NULL){ 261*f4f6c196SMilanka Ringwald uint16_t new_value = little_endian_read_16(buffer, 0); 262*f4f6c196SMilanka Ringwald report->client_configuration_value = new_value; 263*f4f6c196SMilanka Ringwald log_info("Enable Report (type %u) notifications: %x", (uint8_t) report->type, new_value); 264*f4f6c196SMilanka Ringwald 265*f4f6c196SMilanka Ringwald switch (report->type){ 266*f4f6c196SMilanka Ringwald case HID_REPORT_TYPE_INPUT: 267*f4f6c196SMilanka Ringwald hids_device_emit_event_with_uint8(HIDS_SUBEVENT_INPUT_REPORT_ENABLE, con_handle, (uint8_t) new_value); 268*f4f6c196SMilanka Ringwald break; 269*f4f6c196SMilanka Ringwald case HID_REPORT_TYPE_OUTPUT: 270*f4f6c196SMilanka Ringwald hids_device_emit_event_with_uint8(HIDS_SUBEVENT_OUTPUT_REPORT_ENABLE, con_handle, (uint8_t) new_value); 271*f4f6c196SMilanka Ringwald break; 272*f4f6c196SMilanka Ringwald case HID_REPORT_TYPE_FEATURE: 273*f4f6c196SMilanka Ringwald hids_device_emit_event_with_uint8(HIDS_SUBEVENT_FEATURE_REPORT_ENABLE, con_handle, (uint8_t) new_value); 274*f4f6c196SMilanka Ringwald break; 275*f4f6c196SMilanka Ringwald default: 276*f4f6c196SMilanka Ringwald btstack_unreachable(); 277*f4f6c196SMilanka Ringwald break; 278*f4f6c196SMilanka Ringwald } 279*f4f6c196SMilanka Ringwald } 280a4bfc4feSMatthias Ringwald return 0; 281a4bfc4feSMatthias Ringwald } 282a4bfc4feSMatthias Ringwald 283a4bfc4feSMatthias Ringwald /** 284a4bfc4feSMatthias Ringwald * @brief Set up HIDS Device 285a4bfc4feSMatthias Ringwald */ 286a4bfc4feSMatthias Ringwald void hids_device_init(uint8_t country_code, const uint8_t * descriptor, uint16_t descriptor_size){ 2870235c9e5SMilanka Ringwald hids_device_t * instance = hids_device_create_instance(); 288a4bfc4feSMatthias Ringwald 2890235c9e5SMilanka Ringwald instance->hid_country_code = country_code; 2900235c9e5SMilanka Ringwald instance->hid_descriptor = descriptor; 2910235c9e5SMilanka Ringwald instance->hid_descriptor_size = descriptor_size; 292a4bfc4feSMatthias Ringwald 293a4bfc4feSMatthias Ringwald // default 2940235c9e5SMilanka Ringwald instance->hid_protocol_mode = 1; 295a4bfc4feSMatthias Ringwald 296a4bfc4feSMatthias Ringwald // get service handle range 297a4bfc4feSMatthias Ringwald uint16_t start_handle = 0; 298843359edSMatthias Ringwald uint16_t end_handle = 0xffff; 299c436b760SMilanka Ringwald int service_found = gatt_server_get_handle_range_for_service_with_uuid16(ORG_BLUETOOTH_SERVICE_HUMAN_INTERFACE_DEVICE, &start_handle, &end_handle); 300a2489f29SMatthias Ringwald btstack_assert(service_found != 0); 301a2489f29SMatthias Ringwald UNUSED(service_found); 302a4bfc4feSMatthias Ringwald 303a4bfc4feSMatthias Ringwald // get report map handle 3040235c9e5SMilanka Ringwald instance->hid_report_map_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT_MAP); 305a4bfc4feSMatthias Ringwald 306a4bfc4feSMatthias Ringwald // get report map handle 3070235c9e5SMilanka 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); 308a4bfc4feSMatthias Ringwald 309a4bfc4feSMatthias Ringwald // get value and client configuration handles for boot mouse input, boot keyboard input and report input 3100235c9e5SMilanka 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); 3110235c9e5SMilanka 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); 312a4bfc4feSMatthias Ringwald 3130235c9e5SMilanka 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); 3140235c9e5SMilanka 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); 315a4bfc4feSMatthias 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_control_point_value_handle 0x%02x", instance->hid_control_point_value_handle); 325a4bfc4feSMatthias Ringwald 326*f4f6c196SMilanka Ringwald instance->hid_reports = hid_reports_storage; 327*f4f6c196SMilanka Ringwald 328*f4f6c196SMilanka Ringwald uint16_t hid_reports_num = sizeof(hid_reports_storage)/sizeof(hids_device_report_t); 329*f4f6c196SMilanka Ringwald uint16_t assigned_reports_num = 0; 330*f4f6c196SMilanka Ringwald uint16_t start_chr_handle = start_handle; 331*f4f6c196SMilanka Ringwald 332*f4f6c196SMilanka Ringwald while ( (start_chr_handle < end_handle) && (assigned_reports_num < hid_reports_num)) { 333*f4f6c196SMilanka Ringwald uint16_t chr_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_chr_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT); 334*f4f6c196SMilanka Ringwald if (chr_value_handle == 0){ 335*f4f6c196SMilanka Ringwald break; 336*f4f6c196SMilanka Ringwald } 337*f4f6c196SMilanka Ringwald 338*f4f6c196SMilanka 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); 339*f4f6c196SMilanka 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); 340*f4f6c196SMilanka Ringwald uint16_t report_reference_value_len; 341*f4f6c196SMilanka Ringwald const uint8_t * report_reference_value = gatt_server_get_const_value_for_handle(report_reference_handle, &report_reference_value_len); 342*f4f6c196SMilanka Ringwald 343*f4f6c196SMilanka Ringwald if (report_reference_value == NULL){ 344*f4f6c196SMilanka Ringwald break; 345*f4f6c196SMilanka Ringwald } 346*f4f6c196SMilanka Ringwald if (report_reference_value_len != 2){ 347*f4f6c196SMilanka Ringwald break; 348*f4f6c196SMilanka Ringwald } 349*f4f6c196SMilanka Ringwald 350*f4f6c196SMilanka Ringwald hids_device_report_t * report = &hid_reports_storage[assigned_reports_num]; 351*f4f6c196SMilanka Ringwald report->value_handle = chr_value_handle; 352*f4f6c196SMilanka Ringwald report->client_configuration_handle = chr_client_configuration_handle; 353*f4f6c196SMilanka Ringwald report->client_configuration_value = 0; 354*f4f6c196SMilanka Ringwald 355*f4f6c196SMilanka Ringwald report->id = report_reference_value[0]; 356*f4f6c196SMilanka Ringwald report->type = (hid_report_type_t)report_reference_value[1]; 357*f4f6c196SMilanka Ringwald 358*f4f6c196SMilanka Ringwald switch (report->type){ 359*f4f6c196SMilanka Ringwald case HID_REPORT_TYPE_INPUT: 360*f4f6c196SMilanka Ringwald instance->hid_input_reports_num++; 361*f4f6c196SMilanka Ringwald break; 362*f4f6c196SMilanka Ringwald case HID_REPORT_TYPE_OUTPUT: 363*f4f6c196SMilanka Ringwald instance->hid_output_reports_num++; 364*f4f6c196SMilanka Ringwald break; 365*f4f6c196SMilanka Ringwald case HID_REPORT_TYPE_FEATURE: 366*f4f6c196SMilanka Ringwald instance->hid_feature_reports_num++; 367*f4f6c196SMilanka Ringwald break; 368*f4f6c196SMilanka Ringwald default: 369*f4f6c196SMilanka Ringwald btstack_unreachable(); 370*f4f6c196SMilanka Ringwald return; 371*f4f6c196SMilanka Ringwald } 372*f4f6c196SMilanka Ringwald log_info("hid_report_value_handle 0x%02x, id %u, type %u", report->value_handle, report->id, (uint8_t)report->type); 373*f4f6c196SMilanka Ringwald log_info("hid_report_client_configuration_handle 0x%02x", report->client_configuration_handle); 374*f4f6c196SMilanka Ringwald 375*f4f6c196SMilanka Ringwald assigned_reports_num++; 376*f4f6c196SMilanka Ringwald start_chr_handle = chr_client_configuration_handle + 1; 377*f4f6c196SMilanka Ringwald } 378*f4f6c196SMilanka Ringwald 3793d71c7a4SMatthias Ringwald // register service with ATT Server 380a4bfc4feSMatthias Ringwald hid_service.start_handle = start_handle; 381a4bfc4feSMatthias Ringwald hid_service.end_handle = end_handle; 382a4bfc4feSMatthias Ringwald hid_service.read_callback = &att_read_callback; 383a4bfc4feSMatthias Ringwald hid_service.write_callback = &att_write_callback; 3843d71c7a4SMatthias Ringwald att_server_register_service_handler(&hid_service); 385a4bfc4feSMatthias Ringwald } 386a4bfc4feSMatthias Ringwald 387a4bfc4feSMatthias Ringwald /** 388a4bfc4feSMatthias Ringwald * @brief Register callback for the HIDS Device client. 389a4bfc4feSMatthias Ringwald * @param callback 390a4bfc4feSMatthias Ringwald */ 391a4bfc4feSMatthias Ringwald void hids_device_register_packet_handler(btstack_packet_handler_t callback){ 392a4bfc4feSMatthias Ringwald packet_handler = callback; 393a4bfc4feSMatthias Ringwald } 394a4bfc4feSMatthias Ringwald 395a4bfc4feSMatthias Ringwald /** 396a4bfc4feSMatthias Ringwald * @brief Request can send now event to send HID Report 397a4bfc4feSMatthias Ringwald * Generates an HIDS_SUBEVENT_CAN_SEND_NOW subevent 398a4bfc4feSMatthias Ringwald * @param hid_cid 399a4bfc4feSMatthias Ringwald */ 400a4bfc4feSMatthias Ringwald void hids_device_request_can_send_now_event(hci_con_handle_t con_handle){ 4010235c9e5SMilanka Ringwald hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle); 4020235c9e5SMilanka Ringwald if (!instance){ 4030235c9e5SMilanka Ringwald log_error("no instance for handle 0x%02x", con_handle); 4040235c9e5SMilanka Ringwald return; 4050235c9e5SMilanka Ringwald } 4060235c9e5SMilanka Ringwald 4070235c9e5SMilanka Ringwald instance->battery_callback.callback = &hids_device_can_send_now; 4080235c9e5SMilanka Ringwald instance->battery_callback.context = (void*) (uintptr_t) con_handle; 4090235c9e5SMilanka Ringwald att_server_register_can_send_now_callback(&instance->battery_callback, con_handle); 410a4bfc4feSMatthias Ringwald } 411a4bfc4feSMatthias Ringwald 412a4bfc4feSMatthias Ringwald /** 413a4bfc4feSMatthias Ringwald * @brief Send HID Report: Input 414a4bfc4feSMatthias Ringwald */ 415*f4f6c196SMilanka Ringwald 416*f4f6c196SMilanka Ringwald uint8_t hids_device_send_report_with_id(hci_con_handle_t con_handle, uint16_t report_id, 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); 420*f4f6c196SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 4210235c9e5SMilanka Ringwald } 422*f4f6c196SMilanka Ringwald 423*f4f6c196SMilanka Ringwald hids_device_report_t * report_storage = hids_device_get_report_for_id(instance, report_id); 424*f4f6c196SMilanka Ringwald if (report_storage == NULL){ 425*f4f6c196SMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 426*f4f6c196SMilanka Ringwald } 427*f4f6c196SMilanka Ringwald 428*f4f6c196SMilanka Ringwald return att_server_notify(con_handle, report_storage->value_handle, report, report_len); 429*f4f6c196SMilanka Ringwald } 430*f4f6c196SMilanka Ringwald 431*f4f6c196SMilanka Ringwald static uint8_t hids_device_send_report_with_type(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len, hid_report_type_t report_type){ 432*f4f6c196SMilanka Ringwald hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle); 433*f4f6c196SMilanka Ringwald if (!instance){ 434*f4f6c196SMilanka Ringwald log_error("no instance for handle 0x%02x", con_handle); 435*f4f6c196SMilanka Ringwald return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 436*f4f6c196SMilanka Ringwald } 437*f4f6c196SMilanka Ringwald 438*f4f6c196SMilanka Ringwald uint8_t pos = 0; 439*f4f6c196SMilanka Ringwald while (pos < (instance->hid_input_reports_num + instance->hid_output_reports_num + instance->hid_feature_reports_num)){ 440*f4f6c196SMilanka Ringwald hids_device_report_t * report_storage = &instance->hid_reports[pos]; 441*f4f6c196SMilanka Ringwald 442*f4f6c196SMilanka Ringwald if (report_storage->type == report_type){ 443*f4f6c196SMilanka Ringwald return att_server_notify(con_handle, report_storage->value_handle, report, report_len); 444*f4f6c196SMilanka Ringwald } 445*f4f6c196SMilanka Ringwald } 446*f4f6c196SMilanka Ringwald return ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE; 447*f4f6c196SMilanka Ringwald } 448*f4f6c196SMilanka Ringwald 449*f4f6c196SMilanka Ringwald void hids_device_send_input_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){ 450*f4f6c196SMilanka Ringwald (void)hids_device_send_report_with_type(con_handle, report, report_len, HID_REPORT_TYPE_INPUT); 4510235c9e5SMilanka Ringwald } 4520235c9e5SMilanka Ringwald 4530235c9e5SMilanka Ringwald /** 4540235c9e5SMilanka Ringwald * @brief Send HID Report: Output 4550235c9e5SMilanka Ringwald */ 4560235c9e5SMilanka Ringwald void hids_device_send_output_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){ 457*f4f6c196SMilanka Ringwald (void)hids_device_send_report_with_type(con_handle, report, report_len, HID_REPORT_TYPE_OUTPUT); 4580235c9e5SMilanka Ringwald } 4590235c9e5SMilanka Ringwald 4600235c9e5SMilanka Ringwald /** 4610235c9e5SMilanka Ringwald * @brief Send HID Report: Feature 4620235c9e5SMilanka Ringwald */ 4630235c9e5SMilanka Ringwald void hids_device_send_feature_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){ 464*f4f6c196SMilanka Ringwald (void)hids_device_send_report_with_type(con_handle, report, report_len, HID_REPORT_TYPE_FEATURE); 465a4bfc4feSMatthias Ringwald } 466a4bfc4feSMatthias Ringwald 467a4bfc4feSMatthias Ringwald /** 468a4bfc4feSMatthias Ringwald * @brief Send HID Boot Mouse Input Report 469a4bfc4feSMatthias Ringwald */ 470a4bfc4feSMatthias Ringwald void hids_device_send_boot_mouse_input_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){ 4710235c9e5SMilanka Ringwald hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle); 4720235c9e5SMilanka Ringwald if (!instance){ 4730235c9e5SMilanka Ringwald log_error("no instance for handle 0x%02x", con_handle); 4740235c9e5SMilanka Ringwald return; 4750235c9e5SMilanka Ringwald } 476842f9974SMilanka Ringwald att_server_notify(con_handle, instance->hid_boot_mouse_input_value_handle, report, report_len); 477a4bfc4feSMatthias Ringwald } 478a4bfc4feSMatthias Ringwald 479a4bfc4feSMatthias Ringwald /** 480a4bfc4feSMatthias Ringwald * @brief Send HID Boot Mouse Input Report 481a4bfc4feSMatthias Ringwald */ 482a4bfc4feSMatthias Ringwald void hids_device_send_boot_keyboard_input_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){ 4830235c9e5SMilanka Ringwald hids_device_t * instance = hids_device_get_instance_for_con_handle(con_handle); 4840235c9e5SMilanka Ringwald if (!instance){ 4850235c9e5SMilanka Ringwald log_error("no instance for handle 0x%02x", con_handle); 4860235c9e5SMilanka Ringwald return; 4870235c9e5SMilanka Ringwald } 488842f9974SMilanka Ringwald att_server_notify(con_handle, instance->hid_boot_keyboard_input_value_handle, report, report_len); 489a4bfc4feSMatthias Ringwald } 490