HID Mouse Classic
Source Code: hid_mouse_demo.c
This HID Device example demonstrates how to implement an HID keyboard. Without a HAVE_BTSTACK_STDIN, a fixed demo text is sent If HAVE_BTSTACK_STDIN is defined, you can type from the terminal
Main Application Setup
Listing here 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.
int btstack_main(int argc, const char * argv[]);
int btstack_main(int argc, const char * argv[]){
(void)argc;
(void)argv;
// allow to get found by inquiry
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/gap.h#L1256">gap_discoverable_control</a>(1);
// use Limited Discoverable Mode; Peripheral; Pointing Device as CoD
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/gap.h#L432">gap_set_class_of_device</a>(0x2580);
// set local name to be identified - zeroes will be replaced by actual BD ADDR
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/gap.h#L419">gap_set_local_name</a>("HID Mouse Demo 00:00:00:00:00:00");
// allow for role switch in general and sniff mode
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/gap.h#L439">gap_set_default_link_policy_settings</a>( LM_LINK_POLICY_ENABLE_ROLE_SWITCH | LM_LINK_POLICY_ENABLE_SNIFF_MODE );
// allow for role switch on outgoing connections - this allow HID Host to become master when we re-connect to it
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/gap.h#L445">gap_set_allow_role_switch</a>(true);
// L2CAP
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/l2cap.h#L477">l2cap_init</a>();
#ifdef ENABLE_BLE
// Initialize LE Security Manager. Needed for cross-transport key derivation
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/ble/sm.h#L68">sm_init</a>();
#endif
// SDP Server
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_server.h#L72">sdp_init</a>();
uint8_t hid_virtual_cable = 0;
uint8_t hid_remote_wake = 1;
uint8_t hid_reconnect_initiate = 1;
uint8_t hid_normally_connectable = 1;
hid_sdp_record_t hid_params = {
// hid sevice subclass 2580 Mouse, hid counntry code 33 US
0x2580, 33,
hid_virtual_cable, hid_remote_wake,
hid_reconnect_initiate, hid_normally_connectable,
hid_boot_device,
0xFFFF, 0xFFFF, 3200,
hid_descriptor_mouse_boot_mode,
sizeof(hid_descriptor_mouse_boot_mode),
hid_device_name
};
memset(hid_service_buffer, 0, sizeof(hid_service_buffer));
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/hid_device.h#L84">hid_create_sdp_record</a>(hid_service_buffer, <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_server.h#L98">sdp_create_service_record_handle</a>(), &hid_params);
btstack_assert(<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_util.h#L108">de_get_len</a>( hid_service_buffer) <= sizeof(hid_service_buffer));
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_server.h#L81">sdp_register_service</a>(hid_service_buffer);
// See https://www.bluetooth.com/specifications/assigned-numbers/company-identifiers if you don't have a USB Vendor ID and need a Bluetooth Vendor ID
// device info: BlueKitchen GmbH, product 2, version 1
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/device_id_server.h#L65">device_id_create_sdp_record</a>(device_id_sdp_service_buffer, <a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_server.h#L98">sdp_create_service_record_handle</a>(), DEVICE_ID_VENDOR_ID_SOURCE_BLUETOOTH, BLUETOOTH_COMPANY_ID_BLUEKITCHEN_GMBH, 2, 1);
btstack_assert(<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_util.h#L108">de_get_len</a>( device_id_sdp_service_buffer) <= sizeof(device_id_sdp_service_buffer));
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/sdp_server.h#L81">sdp_register_service</a>(device_id_sdp_service_buffer);
// HID Device
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/hid_device.h#L92">hid_device_init</a>(hid_boot_device, sizeof(hid_descriptor_mouse_boot_mode), hid_descriptor_mouse_boot_mode);
// register for HCI events
hci_event_callback_registration.callback = &packet_handler;
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/hci.h#L1580">hci_add_event_handler</a>(&hci_event_callback_registration);
// register for HID
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/classic/hid_device.h#L98">hid_device_register_packet_handler</a>(&packet_handler);
#ifdef HAVE_BTSTACK_STDIN
btstack_stdin_setup(stdin_process);
#endif
// turn on!
<a href="https://github.com/bluekitchen/btstack/blob/develop/src/hci.h#L1566">hci_power_control</a>(HCI_POWER_ON);
return 0;
}