1 /* 2 * Copyright (C) 2014 BlueKitchen GmbH 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at 34 * [email protected] 35 * 36 */ 37 38 #define __BTSTACK_FILE__ "hid_device_demo.c" 39 40 // ***************************************************************************** 41 /* EXAMPLE_START(hid_device_demo): HID Device (Server) Demo 42 * 43 * @text This HID Device example demonstrates how to implement 44 * an HID keyboard. Without a HAVE_POSIX_STDIN, a fixed message is sent 45 * If HAVE_POSIX_STDIN is defined, you can control and type from the terminal 46 */ 47 // ***************************************************************************** 48 49 50 #include <stdint.h> 51 #include <stdio.h> 52 #include <stdlib.h> 53 #include <string.h> 54 #include <unistd.h> 55 56 #include "btstack.h" 57 #ifdef HAVE_POSIX_STDIN 58 #include "stdin_support.h" 59 #endif 60 61 uint8_t hid_service_buffer[200]; 62 const char hid_device_name[] = "BTstack HID Keyboard"; 63 static btstack_packet_callback_registration_t hci_event_callback_registration; 64 65 // from USB HID Specification 1.1, Appendix B.1 66 const uint8_t hid_descriptor_keyboard_boot_mode[] = { 67 68 0x05, 0x01, // Usage Page (Generic Desktop) 69 0x09, 0x06, // Usage (Keyboard) 70 0xa1, 0x01, // Collection (Application) 71 72 // Modifier byte 73 74 0x75, 0x01, // Report Size (1) 75 0x95, 0x08, // Report Count (8) 76 0x05, 0x07, // Usage Page (Key codes) 77 0x19, 0xe0, // Usage Minimum (Keyboard LeftControl) 78 0x29, 0xe7, // Usage Maxium (Keyboard Right GUI) 79 0x15, 0x00, // Logical Minimum (0) 80 0x25, 0x01, // Logical Maximum (1) 81 0x81, 0x02, // Input (Data, Variable, Absolute) 82 83 // Reserved byte 84 85 0x75, 0x01, // Report Size (1) 86 0x95, 0x08, // Report Count (8) 87 0x81, 0x03, // Input (Constant, Variable, Absolute) 88 89 // LED report 90 91 0x95, 0x05, // Report Count (5) 92 0x75, 0x01, // Report Size (1) 93 0x05, 0x08, // Usage Page (LEDs) 94 0x19, 0x01, // Usage Minimum (Num Lock) 95 0x29, 0x05, // Usage Maxium (Kana) 96 0x91, 0x02, // Output (Data, Variable, Absolute) 97 98 // LED padding 99 100 0x95, 0x01, // Report Count (1) 101 0x75, 0x03, // Report Size (3) 102 0x91, 0x03, // Output (Constant, Variable, Absolute) 103 104 // Keycodes 105 106 0x95, 0x06, // Report Count (6) 107 0x75, 0x08, // Report Size (8) 108 0x15, 0x00, // Logical Minimum (0) 109 0x25, 0xff, // Logical Maximum (1) 110 0x05, 0x07, // Usage Page (Key codes) 111 0x19, 0x00, // Usage Minimum (Reserved (no event indicated)) 112 0x29, 0xff, // Usage Maxium (Reserved) 113 0x81, 0x00, // Input (Data, Array) 114 115 0xc0, // End collection 116 }; 117 118 void hid_create_sdp_record( 119 uint8_t *service, 120 uint32_t service_record_handle, 121 uint16_t hid_device_subclass, 122 uint8_t hid_country_code, 123 uint8_t hid_virtual_cable, 124 uint8_t hid_reconnect_initiate, 125 uint8_t hid_boot_device, 126 const uint8_t * hid_descriptor, uint16_t hid_descriptor_size, 127 const char *device_name); 128 void hid_create_sdp_record( 129 uint8_t *service, 130 uint32_t service_record_handle, 131 uint16_t hid_device_subclass, 132 uint8_t hid_country_code, 133 uint8_t hid_virtual_cable, 134 uint8_t hid_reconnect_initiate, 135 uint8_t hid_boot_device, 136 const uint8_t * hid_descriptor, uint16_t hid_descriptor_size, 137 const char *device_name){ 138 139 uint8_t * attribute; 140 de_create_sequence(service); 141 142 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_RECORD_HANDLE); 143 de_add_number(service, DE_UINT, DE_SIZE_32, service_record_handle); 144 145 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_SERVICE_CLASS_ID_LIST); 146 attribute = de_push_sequence(service); 147 { 148 de_add_number(attribute, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_HUMAN_INTERFACE_DEVICE_SERVICE); 149 } 150 de_pop_sequence(service, attribute); 151 152 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST); 153 attribute = de_push_sequence(service); 154 { 155 uint8_t * l2cpProtocol = de_push_sequence(attribute); 156 { 157 de_add_number(l2cpProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP); 158 de_add_number(l2cpProtocol, DE_UINT, DE_SIZE_16, PSM_HID_CONTROL); 159 } 160 de_pop_sequence(attribute, l2cpProtocol); 161 162 uint8_t * hidProtocol = de_push_sequence(attribute); 163 { 164 de_add_number(hidProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_HIDP); 165 } 166 de_pop_sequence(attribute, hidProtocol); 167 } 168 de_pop_sequence(service, attribute); 169 170 // TODO? 171 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_LANGUAGE_BASE_ATTRIBUTE_ID_LIST); 172 attribute = de_push_sequence(service); 173 { 174 de_add_number(attribute, DE_UINT, DE_SIZE_16, 0x656e); 175 de_add_number(attribute, DE_UINT, DE_SIZE_16, 0x006a); 176 de_add_number(attribute, DE_UINT, DE_SIZE_16, 0x0100); 177 } 178 de_pop_sequence(service, attribute); 179 180 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_ADDITIONAL_PROTOCOL_DESCRIPTOR_LISTS); 181 attribute = de_push_sequence(service); 182 { 183 uint8_t * additionalDescriptorAttribute = de_push_sequence(attribute); 184 { 185 uint8_t * l2cpProtocol = de_push_sequence(additionalDescriptorAttribute); 186 { 187 de_add_number(l2cpProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_L2CAP); 188 de_add_number(l2cpProtocol, DE_UINT, DE_SIZE_16, PSM_HID_INTERRUPT); 189 } 190 de_pop_sequence(additionalDescriptorAttribute, l2cpProtocol); 191 192 uint8_t * hidProtocol = de_push_sequence(attribute); 193 { 194 de_add_number(hidProtocol, DE_UUID, DE_SIZE_16, BLUETOOTH_PROTOCOL_HIDP); 195 } 196 de_pop_sequence(attribute, hidProtocol); 197 } 198 de_pop_sequence(attribute, additionalDescriptorAttribute); 199 } 200 de_pop_sequence(service, attribute); 201 202 // 0x0100 "ServiceName" 203 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0100); 204 de_add_data(service, DE_STRING, strlen(device_name), (uint8_t *) device_name); 205 206 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_BLUETOOTH_PROFILE_DESCRIPTOR_LIST); 207 attribute = de_push_sequence(service); 208 { 209 uint8_t * hidProfile = de_push_sequence(attribute); 210 { 211 de_add_number(hidProfile, DE_UUID, DE_SIZE_16, BLUETOOTH_SERVICE_CLASS_HUMAN_INTERFACE_DEVICE_SERVICE); 212 de_add_number(hidProfile, DE_UINT, DE_SIZE_16, 0x0101); // Version 1.1 213 } 214 de_pop_sequence(attribute, hidProfile); 215 } 216 de_pop_sequence(service, attribute); 217 218 // Deprecated in v1.1.1 219 // de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_HID_DEVICE_RELEASE_NUMBER); 220 // de_add_number(service, DE_UINT, DE_SIZE_16, 0x0101); 221 222 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_HID_PARSER_VERSION); 223 de_add_number(service, DE_UINT, DE_SIZE_16, 0x0111); // v1.1.1 224 225 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_HID_DEVICE_SUBCLASS); 226 de_add_number(service, DE_UINT, DE_SIZE_16, hid_device_subclass); 227 228 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_HID_COUNTRY_CODE); 229 de_add_number(service, DE_UINT, DE_SIZE_16, hid_country_code); 230 231 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_HID_VIRTUAL_CABLE); 232 de_add_number(service, DE_BOOL, DE_SIZE_8, hid_virtual_cable); 233 234 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_HID_RECONNECT_INITIATE); 235 de_add_number(service, DE_BOOL, DE_SIZE_8, hid_reconnect_initiate); 236 237 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_HID_DESCRIPTOR_LIST); 238 attribute = de_push_sequence(service); 239 { 240 uint8_t* hidDescriptor = de_push_sequence(attribute); 241 { 242 de_add_number(hidDescriptor, DE_UINT, DE_SIZE_8, 0x22); // Report Descriptor 243 de_add_data(hidDescriptor, DE_STRING, hid_descriptor_size, (uint8_t *) hid_descriptor); 244 } 245 de_pop_sequence(attribute, hidDescriptor); 246 } 247 de_pop_sequence(service, attribute); 248 249 de_add_number(service, DE_UINT, DE_SIZE_16, BLUETOOTH_ATTRIBUTE_HID_BOOT_DEVICE); 250 de_add_number(service, DE_BOOL, DE_SIZE_8, hid_boot_device); 251 } 252 253 #ifdef HAVE_POSIX_STDIN 254 255 // prototypes 256 static void show_usage(void); 257 258 // Testig User Interface 259 static void show_usage(void){ 260 bd_addr_t iut_address; 261 gap_local_bd_addr(iut_address); 262 263 printf("\n--- Bluetooth HID Device Test Console %s ---\n", bd_addr_to_str(iut_address)); 264 printf("\n"); 265 printf("---\n"); 266 printf("Ctrl-c - exit\n"); 267 printf("---\n"); 268 } 269 270 static void stdin_process(btstack_data_source_t *ds, btstack_data_source_callback_type_t callback_type){ 271 UNUSED(ds); 272 UNUSED(callback_type); 273 274 char cmd = btstack_stdin_read(); 275 switch (cmd){ 276 default: 277 show_usage(); 278 break; 279 } 280 } 281 #endif 282 283 static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * event, uint16_t event_size){ 284 UNUSED(channel); 285 UNUSED(event_size); 286 switch (packet_type){ 287 case HCI_EVENT_PACKET: 288 switch (event[0]){ 289 default: 290 break; 291 } 292 break; 293 default: 294 break; 295 } 296 } 297 298 /* @section Main Application Setup 299 * 300 * @text Listing MainConfiguration shows main application code. 301 * To run a HID Device service you need to initialize the SDP, and to create and register HID Device record with it. 302 * At the end the Bluetooth stack is started. 303 */ 304 305 /* LISTING_START(MainConfiguration): Setup HID Device */ 306 307 int btstack_main(int argc, const char * argv[]); 308 int btstack_main(int argc, const char * argv[]){ 309 (void)argc; 310 (void)argv; 311 312 // register for HCI events 313 hci_event_callback_registration.callback = &packet_handler; 314 hci_add_event_handler(&hci_event_callback_registration); 315 hci_register_sco_packet_handler(&packet_handler); 316 317 gap_discoverable_control(1); 318 gap_set_class_of_device(0x2540); 319 320 // L2CAP 321 l2cap_init(); 322 323 // HID Device / Server 324 325 // SDP Server 326 sdp_init(); 327 memset(hid_service_buffer, 0, sizeof(hid_service_buffer)); 328 // hid sevice subclass 2540 Keyboard, hid counntry code 33 US, hid virtual cable off, hid reconnect initiate off, hid boot device off 329 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); 330 printf("SDP service record size: %u\n", de_get_len( hid_service_buffer)); 331 sdp_register_service(hid_service_buffer); 332 333 #ifdef HAVE_POSIX_STDIN 334 btstack_stdin_setup(stdin_process); 335 #endif 336 // turn on! 337 hci_power_control(HCI_POWER_ON); 338 return 0; 339 } 340 /* LISTING_END */ 341 /* EXAMPLE_END */ 342