hid_device: start demo, define keyboard boot protocol descriptor, create sdp record

This commit is contained in:
Matthias Ringwald 2017-04-26 18:14:27 +02:00
parent b3264428f7
commit 2fa56ea6fd
2 changed files with 345 additions and 0 deletions

View File

@ -100,6 +100,7 @@ EXAMPLES = \
gap_inquiry \
gatt_battery_query \
gatt_browser \
hid_keyboard_demo \
le_counter \
le_streamer \
le_streamer_client \
@ -240,6 +241,9 @@ hfp_ag_demo: ${CORE_OBJ} ${COMMON_OBJ} ${CLASSIC_OBJ} ${SDP_CLIENT} ${SBC_DECODE
hfp_hf_demo: ${CORE_OBJ} ${COMMON_OBJ} ${CLASSIC_OBJ} ${SDP_CLIENT} ${SBC_DECODER_OBJ} ${SBC_ENCODER_OBJ} ${CVSD_PLC_OBJ} wav_util.o sco_demo_util.o btstack_ring_buffer.o hfp.o hfp_hf.o hfp_hf_demo.c
${CC} $^ ${CFLAGS} -I${BTSTACK_ROOT}/platform/posix ${LDFLAGS} -o $@
hid_keyboard_demo: ${CORE_OBJ} ${COMMON_OBJ} ${CLASSIC_OBJ} ${SDP_CLIENT} btstack_ring_buffer.o hid_device.o hid_keyboard_demo.o
${CC} $^ ${CFLAGS} -I${BTSTACK_ROOT}/platform/posix ${LDFLAGS} -o $@
clean:
rm -f ${EXAMPLES}
rm -f *.o *.out *.hex *.exe

341
example/hid_keyboard_demo.c Normal file
View File

@ -0,0 +1,341 @@
/*
* 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 <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "btstack.h"
#ifdef HAVE_POSIX_STDIN
#include "stdin_support.h"
#endif
uint8_t hid_service_buffer[200];
const char hid_device_name[] = "BTstack HID Keyboard";
static btstack_packet_callback_registration_t hci_event_callback_registration;
// 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 void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * event, uint16_t event_size){
UNUSED(channel);
UNUSED(event_size);
switch (packet_type){
case HCI_EVENT_PACKET:
switch (event[0]){
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);
// L2CAP
l2cap_init();
// HID Device / Server
// 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 */