/* * Copyright (C) 2014 BlueKitchen GmbH * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the copyright holders nor the names of * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * 4. Any redistribution, use, or modification is done solely for * personal benefit and not for any commercial purpose or for * monetary gain. * * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * Please inquire about commercial licensing options at * contact@bluekitchen-gmbh.com * */ #define __BTSTACK_FILE__ "hid_device_demo.c" // ***************************************************************************** /* EXAMPLE_START(hid_device_demo): HID Device (Server) Demo * * @text This HID Device example demonstrates how to implement * an HID keyboard. Without a HAVE_POSIX_STDIN, a fixed message is sent * If HAVE_POSIX_STDIN is defined, you can control and type from the terminal */ // ***************************************************************************** #include #include #include #include #include #include #include "btstack.h" #ifdef HAVE_POSIX_STDIN #include "stdin_support.h" #endif uint8_t hid_service_buffer[250]; const char hid_device_name[] = "BTstack HID Keyboard"; static btstack_packet_callback_registration_t hci_event_callback_registration; // hid device state static uint16_t hid_control_cid; static uint16_t hid_interrupt_cid; // from USB HID Specification 1.1, Appendix B.1 const uint8_t hid_descriptor_keyboard_boot_mode[] = { 0x05, 0x01, // Usage Page (Generic Desktop) 0x09, 0x06, // Usage (Keyboard) 0xa1, 0x01, // Collection (Application) // Modifier byte 0x75, 0x01, // Report Size (1) 0x95, 0x08, // Report Count (8) 0x05, 0x07, // Usage Page (Key codes) 0x19, 0xe0, // Usage Minimum (Keyboard LeftControl) 0x29, 0xe7, // Usage Maxium (Keyboard Right GUI) 0x15, 0x00, // Logical Minimum (0) 0x25, 0x01, // Logical Maximum (1) 0x81, 0x02, // Input (Data, Variable, Absolute) // Reserved byte 0x75, 0x01, // Report Size (1) 0x95, 0x08, // Report Count (8) 0x81, 0x03, // Input (Constant, Variable, Absolute) // LED report 0x95, 0x05, // Report Count (5) 0x75, 0x01, // Report Size (1) 0x05, 0x08, // Usage Page (LEDs) 0x19, 0x01, // Usage Minimum (Num Lock) 0x29, 0x05, // Usage Maxium (Kana) 0x91, 0x02, // Output (Data, Variable, Absolute) // LED padding 0x95, 0x01, // Report Count (1) 0x75, 0x03, // Report Size (3) 0x91, 0x03, // Output (Constant, Variable, Absolute) // Keycodes 0x95, 0x06, // Report Count (6) 0x75, 0x08, // Report Size (8) 0x15, 0x00, // Logical Minimum (0) 0x25, 0xff, // Logical Maximum (1) 0x05, 0x07, // Usage Page (Key codes) 0x19, 0x00, // Usage Minimum (Reserved (no event indicated)) 0x29, 0xff, // Usage Maxium (Reserved) 0x81, 0x00, // Input (Data, Array) 0xc0, // End collection }; void hid_create_sdp_record( uint8_t *service, uint32_t service_record_handle, uint16_t hid_device_subclass, uint8_t hid_country_code, uint8_t hid_virtual_cable, uint8_t hid_reconnect_initiate, uint8_t hid_boot_device, const uint8_t * hid_descriptor, uint16_t hid_descriptor_size, const char *device_name); void hid_create_sdp_record( uint8_t *service, uint32_t service_record_handle, uint16_t hid_device_subclass, uint8_t hid_country_code, uint8_t hid_virtual_cable, uint8_t hid_reconnect_initiate, uint8_t hid_boot_device, const uint8_t * hid_descriptor, uint16_t hid_descriptor_size, const char *device_name){ uint8_t * attribute; de_create_sequence(service); de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_RECORD_HANDLE); de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle); de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST); attribute = de_push_sequence(service); { de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_HUMAN_INTERFACE_DEVICE_SERVICE); } de_pop_sequence(service, attribute); de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST); attribute = de_push_sequence(service); { uint8_t * l2cpProtocol = de_push_sequence(attribute); { de_add_number(l2cpProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP); de_add_number(l2cpProtocol, DE_UINT, DE_SIZE_16, PSM_HID_CONTROL); } de_pop_sequence(attribute, l2cpProtocol); uint8_t * hidProtocol = de_push_sequence(attribute); { de_add_number(hidProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_HIDP); } de_pop_sequence(attribute, hidProtocol); } de_pop_sequence(service, attribute); // TODO? de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_LANGUAGE_BASE_ATTRIBUTE_ID_LIST); attribute = de_push_sequence(service); { de_add_number(attribute, DE_UINT, DE_SIZE_16, 0x656e); de_add_number(attribute, DE_UINT, DE_SIZE_16, 0x006a); de_add_number(attribute, DE_UINT, DE_SIZE_16, 0x0100); } de_pop_sequence(service, attribute); de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS); attribute = de_push_sequence(service); { uint8_t * additionalDescriptorAttribute = de_push_sequence(attribute); { uint8_t * l2cpProtocol = de_push_sequence(additionalDescriptorAttribute); { de_add_number(l2cpProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP); de_add_number(l2cpProtocol, DE_UINT, DE_SIZE_16, PSM_HID_INTERRUPT); } de_pop_sequence(additionalDescriptorAttribute, l2cpProtocol); uint8_t * hidProtocol = de_push_sequence(attribute); { de_add_number(hidProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_HIDP); } de_pop_sequence(attribute, hidProtocol); } de_pop_sequence(attribute, additionalDescriptorAttribute); } de_pop_sequence(service, attribute); // 0x0100 "ServiceName" de_add_number(service, DE_UINT, DE_SIZE_16, 0x0100); de_add_data(service, DE_STRING, strlen(device_name), (uint8_t *) device_name); de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST); attribute = de_push_sequence(service); { uint8_t * hidProfile = de_push_sequence(attribute); { de_add_number(hidProfile, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_HUMAN_INTERFACE_DEVICE_SERVICE); de_add_number(hidProfile, DE_UINT, DE_SIZE_16, 0x0101); // Version 1.1 } de_pop_sequence(attribute, hidProfile); } de_pop_sequence(service, attribute); // Deprecated in v1.1.1 // de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_HID_DEVICE_RELEASE_NUMBER); // de_add_number(service, DE_UINT, DE_SIZE_16, 0x0101); de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_HID_PARSER_VERSION); de_add_number(service, DE_UINT, DE_SIZE_16, 0x0111); // v1.1.1 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_HID_DEVICE_SUBCLASS); de_add_number(service, DE_UINT, DE_SIZE_16, hid_device_subclass); de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_HID_COUNTRY_CODE); de_add_number(service, DE_UINT, DE_SIZE_16, hid_country_code); de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_HID_VIRTUAL_CABLE); de_add_number(service, DE_BOOL, DE_SIZE_8, hid_virtual_cable); de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_HID_RECONNECT_INITIATE); de_add_number(service, DE_BOOL, DE_SIZE_8, hid_reconnect_initiate); de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_HID_DESCRIPTOR_LIST); attribute = de_push_sequence(service); { uint8_t* hidDescriptor = de_push_sequence(attribute); { de_add_number(hidDescriptor, DE_UINT, DE_SIZE_8, 0x22); // Report Descriptor de_add_data(hidDescriptor, DE_STRING, hid_descriptor_size, (uint8_t *) hid_descriptor); } de_pop_sequence(attribute, hidDescriptor); } de_pop_sequence(service, attribute); de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_HID_BOOT_DEVICE); de_add_number(service, DE_BOOL, DE_SIZE_8, hid_boot_device); } #ifdef HAVE_POSIX_STDIN // prototypes static void show_usage(void); // Testig User Interface static void show_usage(void){ bd_addr_t iut_address; gap_local_bd_addr(iut_address); printf("\n--- Bluetooth HID Device Test Console %s ---\n", bd_addr_to_str(iut_address)); printf("\n"); printf("---\n"); printf("Ctrl-c - exit\n"); printf("---\n"); } static void stdin_process(btstack_data_source_t *ds, btstack_data_source_callback_type_t callback_type){ UNUSED(ds); UNUSED(callback_type); char cmd = btstack_stdin_read(); switch (cmd){ default: show_usage(); break; } } #endif static int hid_connected(void){ return hid_control_cid && hid_interrupt_cid; } static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * packet, uint16_t packet_size){ UNUSED(channel); UNUSED(packet_size); int connected_before; switch (packet_type){ case HCI_EVENT_PACKET: switch (packet[0]){ case HCI_EVENT_USER_CONFIRMATION_REQUEST: // ssp: inform about user confirmation request log_info("SSP User Confirmation Request with numeric value '%06"PRIu32"'\n", hci_event_user_confirmation_request_get_numeric_value(packet)); log_info("SSP User Confirmation Auto accept\n"); break; // into HID Device/Server case L2CAP_EVENT_INCOMING_CONNECTION: switch (l2cap_event_incoming_connection_get_psm(packet)){ case PSM_HID_CONTROL: case PSM_HID_INTERRUPT: l2cap_accept_connection(channel); break; default: l2cap_decline_connection(channel); break; } break; case L2CAP_EVENT_CHANNEL_OPENED: if (packet[2]) return; connected_before = hid_connected(); switch (l2cap_event_channel_opened_get_psm(packet)){ case PSM_HID_CONTROL: hid_control_cid = l2cap_event_channel_opened_get_local_cid(packet); log_info("HID Control opened, cid 0x%02x", hid_control_cid); break; case PSM_HID_INTERRUPT: hid_interrupt_cid = l2cap_event_channel_opened_get_local_cid(packet); log_info("HID Interrupt opened, cid 0x%02x", hid_interrupt_cid); break; default: break; } if (!connected_before && hid_connected()){ printf("HID Connected\n"); } break; case L2CAP_EVENT_CHANNEL_CLOSED: connected_before = hid_connected(); if (l2cap_event_channel_closed_get_local_cid(packet) == hid_control_cid){ log_info("HID Control closed"); hid_control_cid = 0; } if (l2cap_event_channel_closed_get_local_cid(packet) == hid_interrupt_cid){ log_info("HID Interrupt closed"); hid_interrupt_cid = 0; } if (connected_before && !hid_connected()){ printf("HID Disconnected\n"); } break; default: break; } break; default: break; } } /* @section Main Application Setup * * @text Listing MainConfiguration shows main application code. * To run a HID Device service you need to initialize the SDP, and to create and register HID Device record with it. * At the end the Bluetooth stack is started. */ /* LISTING_START(MainConfiguration): Setup HID Device */ int btstack_main(int argc, const char * argv[]); int btstack_main(int argc, const char * argv[]){ (void)argc; (void)argv; // register for HCI events hci_event_callback_registration.callback = &packet_handler; hci_add_event_handler(&hci_event_callback_registration); hci_register_sco_packet_handler(&packet_handler); gap_discoverable_control(1); gap_set_class_of_device(0x2540); gap_set_local_name(hid_device_name); // L2CAP l2cap_init(); l2cap_register_service(packet_handler, PSM_HID_INTERRUPT, 100, LEVEL_0); l2cap_register_service(packet_handler, PSM_HID_CONTROL, 100, LEVEL_0); // SDP Server sdp_init(); memset(hid_service_buffer, 0, sizeof(hid_service_buffer)); // hid sevice subclass 2540 Keyboard, hid counntry code 33 US, hid virtual cable off, hid reconnect initiate off, hid boot device off hid_create_sdp_record(hid_service_buffer, 0x10001, 0x2540, 33, 0, 0, 0, hid_descriptor_keyboard_boot_mode, sizeof(hid_descriptor_keyboard_boot_mode), hid_device_name); printf("SDP service record size: %u\n", de_get_len( hid_service_buffer)); sdp_register_service(hid_service_buffer); #ifdef HAVE_POSIX_STDIN btstack_stdin_setup(stdin_process); #endif // turn on! hci_power_control(HCI_POWER_ON); return 0; } /* LISTING_END */ /* EXAMPLE_END */