1*a4bfc4feSMatthias Ringwald /* 2*a4bfc4feSMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 3*a4bfc4feSMatthias Ringwald * 4*a4bfc4feSMatthias Ringwald * Redistribution and use in source and binary forms, with or without 5*a4bfc4feSMatthias Ringwald * modification, are permitted provided that the following conditions 6*a4bfc4feSMatthias Ringwald * are met: 7*a4bfc4feSMatthias Ringwald * 8*a4bfc4feSMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 9*a4bfc4feSMatthias Ringwald * notice, this list of conditions and the following disclaimer. 10*a4bfc4feSMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11*a4bfc4feSMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 12*a4bfc4feSMatthias Ringwald * documentation and/or other materials provided with the distribution. 13*a4bfc4feSMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 14*a4bfc4feSMatthias Ringwald * contributors may be used to endorse or promote products derived 15*a4bfc4feSMatthias Ringwald * from this software without specific prior written permission. 16*a4bfc4feSMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 17*a4bfc4feSMatthias Ringwald * personal benefit and not for any commercial purpose or for 18*a4bfc4feSMatthias Ringwald * monetary gain. 19*a4bfc4feSMatthias Ringwald * 20*a4bfc4feSMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21*a4bfc4feSMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22*a4bfc4feSMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23*a4bfc4feSMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24*a4bfc4feSMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25*a4bfc4feSMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26*a4bfc4feSMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27*a4bfc4feSMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28*a4bfc4feSMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29*a4bfc4feSMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30*a4bfc4feSMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31*a4bfc4feSMatthias Ringwald * SUCH DAMAGE. 32*a4bfc4feSMatthias Ringwald * 33*a4bfc4feSMatthias Ringwald * Please inquire about commercial licensing options at 34*a4bfc4feSMatthias Ringwald * [email protected] 35*a4bfc4feSMatthias Ringwald * 36*a4bfc4feSMatthias Ringwald */ 37*a4bfc4feSMatthias Ringwald 38*a4bfc4feSMatthias Ringwald /** 39*a4bfc4feSMatthias Ringwald * Implementation of the GATT HIDS Device 40*a4bfc4feSMatthias Ringwald * To use with your application, add '#import <hids.gatt>' to your .gatt file 41*a4bfc4feSMatthias Ringwald */ 42*a4bfc4feSMatthias Ringwald 43*a4bfc4feSMatthias Ringwald #include "hids_device.h" 44*a4bfc4feSMatthias Ringwald 45*a4bfc4feSMatthias Ringwald #include "att_db.h" 46*a4bfc4feSMatthias Ringwald #include "att_server.h" 47*a4bfc4feSMatthias Ringwald #include "bluetooth_gatt.h" 48*a4bfc4feSMatthias Ringwald #include "btstack_util.h" 49*a4bfc4feSMatthias Ringwald 50*a4bfc4feSMatthias Ringwald static btstack_packet_handler_t packet_handler; 51*a4bfc4feSMatthias Ringwald static btstack_context_callback_registration_t battery_callback; 52*a4bfc4feSMatthias Ringwald 53*a4bfc4feSMatthias Ringwald static uint8_t hid_country_code; 54*a4bfc4feSMatthias Ringwald static const uint8_t * hid_descriptor; 55*a4bfc4feSMatthias Ringwald static uint16_t hid_descriptor_size; 56*a4bfc4feSMatthias Ringwald 57*a4bfc4feSMatthias Ringwald static uint16_t hid_report_map_handle; 58*a4bfc4feSMatthias Ringwald static uint16_t hid_protocol_mode; 59*a4bfc4feSMatthias Ringwald static uint16_t hid_protocol_mode_value_handle; 60*a4bfc4feSMatthias Ringwald 61*a4bfc4feSMatthias Ringwald static uint16_t hid_boot_mouse_input_value_handle; 62*a4bfc4feSMatthias Ringwald static uint16_t hid_boot_mouse_input_client_configuration_handle; 63*a4bfc4feSMatthias Ringwald static uint16_t hid_boot_mouse_input_client_configuration_value; 64*a4bfc4feSMatthias Ringwald 65*a4bfc4feSMatthias Ringwald static uint16_t hid_boot_keyboard_input_value_handle; 66*a4bfc4feSMatthias Ringwald static uint16_t hid_boot_keyboard_input_client_configuration_handle; 67*a4bfc4feSMatthias Ringwald static uint16_t hid_boot_keyboard_input_client_configuration_value; 68*a4bfc4feSMatthias Ringwald 69*a4bfc4feSMatthias Ringwald static uint16_t hid_report_input_value_handle; 70*a4bfc4feSMatthias Ringwald static uint16_t hid_report_input_client_configuration_handle; 71*a4bfc4feSMatthias Ringwald static uint16_t hid_report_input_client_configuration_value; 72*a4bfc4feSMatthias Ringwald 73*a4bfc4feSMatthias Ringwald static att_service_handler_t hid_service; 74*a4bfc4feSMatthias Ringwald 75*a4bfc4feSMatthias Ringwald static void hids_device_emit_event_with_uint8(uint8_t event, hci_con_handle_t con_handle, uint8_t value){ 76*a4bfc4feSMatthias Ringwald if (!packet_handler) return; 77*a4bfc4feSMatthias Ringwald uint8_t buffer[6]; 78*a4bfc4feSMatthias Ringwald buffer[0] = HCI_EVENT_HIDS_META; 79*a4bfc4feSMatthias Ringwald buffer[1] = 4; 80*a4bfc4feSMatthias Ringwald buffer[2] = event; 81*a4bfc4feSMatthias Ringwald little_endian_store_16(buffer, 3, (uint16_t) con_handle); 82*a4bfc4feSMatthias Ringwald buffer[5] = value; 83*a4bfc4feSMatthias Ringwald (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer)); 84*a4bfc4feSMatthias Ringwald } 85*a4bfc4feSMatthias Ringwald 86*a4bfc4feSMatthias Ringwald static void hids_device_can_send_now(void * context){ 87*a4bfc4feSMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context; 88*a4bfc4feSMatthias Ringwald // notify client 89*a4bfc4feSMatthias Ringwald if (!packet_handler) return; 90*a4bfc4feSMatthias Ringwald uint8_t buffer[5]; 91*a4bfc4feSMatthias Ringwald buffer[0] = HCI_EVENT_HIDS_META; 92*a4bfc4feSMatthias Ringwald buffer[1] = 3; 93*a4bfc4feSMatthias Ringwald buffer[2] = HIDS_SUBEVENT_CAN_SEND_NOW; 94*a4bfc4feSMatthias Ringwald little_endian_store_16(buffer, 3, (uint16_t) con_handle); 95*a4bfc4feSMatthias Ringwald (*packet_handler)(HCI_EVENT_PACKET, 0, buffer, sizeof(buffer)); 96*a4bfc4feSMatthias Ringwald } 97*a4bfc4feSMatthias Ringwald 98*a4bfc4feSMatthias Ringwald // ATT Client Read Callback for Dynamic Data 99*a4bfc4feSMatthias Ringwald // - if buffer == NULL, don't copy data, just return size of value 100*a4bfc4feSMatthias Ringwald // - if buffer != NULL, copy data and return number bytes copied 101*a4bfc4feSMatthias Ringwald static uint16_t att_read_callback(hci_con_handle_t connection_handle, uint16_t att_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){ 102*a4bfc4feSMatthias Ringwald UNUSED(connection_handle); 103*a4bfc4feSMatthias Ringwald 104*a4bfc4feSMatthias Ringwald if (att_handle == hid_protocol_mode_value_handle){ 105*a4bfc4feSMatthias Ringwald return att_read_callback_handle_byte(hid_protocol_mode, offset, buffer, buffer_size); 106*a4bfc4feSMatthias Ringwald } 107*a4bfc4feSMatthias Ringwald if (att_handle == hid_report_map_handle){ 108*a4bfc4feSMatthias Ringwald return att_read_callback_handle_blob(hid_descriptor, hid_descriptor_size, offset, buffer, buffer_size); 109*a4bfc4feSMatthias Ringwald } 110*a4bfc4feSMatthias Ringwald // if (att_handle == hid_boot_mouse_input_value_handle){ 111*a4bfc4feSMatthias Ringwald // } 112*a4bfc4feSMatthias Ringwald if (att_handle == hid_boot_mouse_input_client_configuration_handle){ 113*a4bfc4feSMatthias Ringwald return att_read_callback_handle_little_endian_16(hid_boot_keyboard_input_client_configuration_value, offset, buffer, buffer_size); 114*a4bfc4feSMatthias Ringwald } 115*a4bfc4feSMatthias Ringwald // if (att_handle == hid_boot_keyboard_input_value_handle){ 116*a4bfc4feSMatthias Ringwald // } 117*a4bfc4feSMatthias Ringwald if (att_handle == hid_boot_keyboard_input_client_configuration_handle){ 118*a4bfc4feSMatthias Ringwald return att_read_callback_handle_little_endian_16(hid_boot_keyboard_input_client_configuration_value, offset, buffer, buffer_size); 119*a4bfc4feSMatthias Ringwald } 120*a4bfc4feSMatthias Ringwald // if (att_handle == hid_report_input_value_handle){ 121*a4bfc4feSMatthias Ringwald // } 122*a4bfc4feSMatthias Ringwald if (att_handle == hid_report_input_client_configuration_handle){ 123*a4bfc4feSMatthias Ringwald return att_read_callback_handle_little_endian_16(hid_report_input_client_configuration_value, offset, buffer, buffer_size); 124*a4bfc4feSMatthias Ringwald } 125*a4bfc4feSMatthias Ringwald return 0; 126*a4bfc4feSMatthias Ringwald } 127*a4bfc4feSMatthias Ringwald 128*a4bfc4feSMatthias 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){ 129*a4bfc4feSMatthias Ringwald UNUSED(transaction_mode); 130*a4bfc4feSMatthias Ringwald UNUSED(buffer_size); 131*a4bfc4feSMatthias Ringwald UNUSED(offset); 132*a4bfc4feSMatthias Ringwald 133*a4bfc4feSMatthias Ringwald if (att_handle == hid_boot_mouse_input_client_configuration_handle){ 134*a4bfc4feSMatthias Ringwald hid_boot_mouse_input_client_configuration_value = little_endian_read_16(buffer, 0); 135*a4bfc4feSMatthias Ringwald hids_device_emit_event_with_uint8(HIDS_SUBEVENT_BOOT_MOUSE_INPUT_REPORT_ENABLE, con_handle, hid_protocol_mode); 136*a4bfc4feSMatthias Ringwald } 137*a4bfc4feSMatthias Ringwald if (att_handle == hid_boot_keyboard_input_client_configuration_handle){ 138*a4bfc4feSMatthias Ringwald hid_boot_keyboard_input_client_configuration_value = little_endian_read_16(buffer, 0); 139*a4bfc4feSMatthias Ringwald hids_device_emit_event_with_uint8(HIDS_SUBEVENT_BOOT_KEYBOARD_INPUT_REPORT_ENABLE, con_handle, hid_protocol_mode); 140*a4bfc4feSMatthias Ringwald } 141*a4bfc4feSMatthias Ringwald if (att_handle == hid_report_input_client_configuration_handle){ 142*a4bfc4feSMatthias Ringwald hid_report_input_client_configuration_value = little_endian_read_16(buffer, 0); 143*a4bfc4feSMatthias Ringwald hids_device_emit_event_with_uint8(HIDS_SUBEVENT_INPUT_REPORT_ENABLE, con_handle, hid_protocol_mode); 144*a4bfc4feSMatthias Ringwald } 145*a4bfc4feSMatthias Ringwald if (att_handle == hid_protocol_mode_value_handle){ 146*a4bfc4feSMatthias Ringwald hid_protocol_mode = buffer[0]; 147*a4bfc4feSMatthias Ringwald hids_device_emit_event_with_uint8(HIDS_SUBEVENT_PROTOCOL_MODE, con_handle, hid_protocol_mode); 148*a4bfc4feSMatthias Ringwald } 149*a4bfc4feSMatthias Ringwald return 0; 150*a4bfc4feSMatthias Ringwald } 151*a4bfc4feSMatthias Ringwald 152*a4bfc4feSMatthias Ringwald /** 153*a4bfc4feSMatthias Ringwald * @brief Set up HIDS Device 154*a4bfc4feSMatthias Ringwald */ 155*a4bfc4feSMatthias Ringwald void hids_device_init(uint8_t country_code, const uint8_t * descriptor, uint16_t descriptor_size){ 156*a4bfc4feSMatthias Ringwald 157*a4bfc4feSMatthias Ringwald hid_country_code = country_code; 158*a4bfc4feSMatthias Ringwald hid_descriptor = descriptor; 159*a4bfc4feSMatthias Ringwald hid_descriptor_size = descriptor_size; 160*a4bfc4feSMatthias Ringwald 161*a4bfc4feSMatthias Ringwald // default 162*a4bfc4feSMatthias Ringwald hid_protocol_mode = 1; 163*a4bfc4feSMatthias Ringwald 164*a4bfc4feSMatthias Ringwald // get service handle range 165*a4bfc4feSMatthias Ringwald uint16_t start_handle = 0; 166*a4bfc4feSMatthias Ringwald uint16_t end_handle = 0xfff; 167*a4bfc4feSMatthias Ringwald int service_found = gatt_server_get_get_handle_range_for_service_with_uuid16(ORG_BLUETOOTH_SERVICE_HUMAN_INTERFACE_DEVICE, &start_handle, &end_handle); 168*a4bfc4feSMatthias Ringwald if (!service_found) return; 169*a4bfc4feSMatthias Ringwald 170*a4bfc4feSMatthias Ringwald // get report map handle 171*a4bfc4feSMatthias Ringwald hid_report_map_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT_MAP); 172*a4bfc4feSMatthias Ringwald 173*a4bfc4feSMatthias Ringwald // get report map handle 174*a4bfc4feSMatthias Ringwald hid_protocol_mode_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_PROTOCOL_MODE); 175*a4bfc4feSMatthias Ringwald 176*a4bfc4feSMatthias Ringwald // get value and client configuration handles for boot mouse input, boot keyboard input and report input 177*a4bfc4feSMatthias Ringwald 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); 178*a4bfc4feSMatthias Ringwald 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); 179*a4bfc4feSMatthias Ringwald 180*a4bfc4feSMatthias Ringwald 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); 181*a4bfc4feSMatthias Ringwald 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); 182*a4bfc4feSMatthias Ringwald 183*a4bfc4feSMatthias Ringwald hid_report_input_value_handle = gatt_server_get_value_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT); 184*a4bfc4feSMatthias Ringwald hid_report_input_client_configuration_handle = gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(start_handle, end_handle, ORG_BLUETOOTH_CHARACTERISTIC_REPORT); 185*a4bfc4feSMatthias Ringwald 186*a4bfc4feSMatthias Ringwald // register service with ATT DB 187*a4bfc4feSMatthias Ringwald hid_service.start_handle = start_handle; 188*a4bfc4feSMatthias Ringwald hid_service.end_handle = end_handle; 189*a4bfc4feSMatthias Ringwald hid_service.read_callback = &att_read_callback; 190*a4bfc4feSMatthias Ringwald hid_service.write_callback = &att_write_callback; 191*a4bfc4feSMatthias Ringwald att_register_service_handler(&hid_service); 192*a4bfc4feSMatthias Ringwald } 193*a4bfc4feSMatthias Ringwald 194*a4bfc4feSMatthias Ringwald /** 195*a4bfc4feSMatthias Ringwald * @brief Register callback for the HIDS Device client. 196*a4bfc4feSMatthias Ringwald * @param callback 197*a4bfc4feSMatthias Ringwald */ 198*a4bfc4feSMatthias Ringwald void hids_device_register_packet_handler(btstack_packet_handler_t callback){ 199*a4bfc4feSMatthias Ringwald packet_handler = callback; 200*a4bfc4feSMatthias Ringwald } 201*a4bfc4feSMatthias Ringwald 202*a4bfc4feSMatthias Ringwald /** 203*a4bfc4feSMatthias Ringwald * @brief Request can send now event to send HID Report 204*a4bfc4feSMatthias Ringwald * Generates an HIDS_SUBEVENT_CAN_SEND_NOW subevent 205*a4bfc4feSMatthias Ringwald * @param hid_cid 206*a4bfc4feSMatthias Ringwald */ 207*a4bfc4feSMatthias Ringwald void hids_device_request_can_send_now_event(hci_con_handle_t con_handle){ 208*a4bfc4feSMatthias Ringwald battery_callback.callback = &hids_device_can_send_now; 209*a4bfc4feSMatthias Ringwald battery_callback.context = (void*) (uintptr_t) con_handle; 210*a4bfc4feSMatthias Ringwald att_server_register_can_send_now_callback(&battery_callback, con_handle); 211*a4bfc4feSMatthias Ringwald } 212*a4bfc4feSMatthias Ringwald 213*a4bfc4feSMatthias Ringwald /** 214*a4bfc4feSMatthias Ringwald * @brief Send HID Report: Input 215*a4bfc4feSMatthias Ringwald */ 216*a4bfc4feSMatthias Ringwald void hids_device_send_input_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){ 217*a4bfc4feSMatthias Ringwald att_server_notify(con_handle, hid_report_input_value_handle, (uint8_t*) report, report_len); 218*a4bfc4feSMatthias Ringwald } 219*a4bfc4feSMatthias Ringwald 220*a4bfc4feSMatthias Ringwald /** 221*a4bfc4feSMatthias Ringwald * @brief Send HID Boot Mouse Input Report 222*a4bfc4feSMatthias Ringwald */ 223*a4bfc4feSMatthias Ringwald void hids_device_send_boot_mouse_input_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){ 224*a4bfc4feSMatthias Ringwald att_server_notify(con_handle, hid_boot_mouse_input_value_handle, (uint8_t*) report, report_len); 225*a4bfc4feSMatthias Ringwald } 226*a4bfc4feSMatthias Ringwald 227*a4bfc4feSMatthias Ringwald /** 228*a4bfc4feSMatthias Ringwald * @brief Send HID Boot Mouse Input Report 229*a4bfc4feSMatthias Ringwald */ 230*a4bfc4feSMatthias Ringwald void hids_device_send_boot_keyboard_input_report(hci_con_handle_t con_handle, const uint8_t * report, uint16_t report_len){ 231*a4bfc4feSMatthias Ringwald att_server_notify(con_handle, hid_boot_keyboard_input_value_handle, (uint8_t*) report, report_len); 232*a4bfc4feSMatthias Ringwald } 233*a4bfc4feSMatthias Ringwald 234