nRF Connect SDK Bare Metal API 2.0.99
Loading...
Searching...
No Matches
ble_hids.h
1/*
2 * Copyright (c) 2012 - 2025 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5 */
6
13#ifndef BLE_HIDS_H__
14#define BLE_HIDS_H__
15
16#include <stdint.h>
17#include <stdbool.h>
18#include <ble.h>
19#include <ble_gap.h>
20#include <ble_gatts.h>
21#include <bm/bluetooth/ble_common.h>
22#include <bm/softdevice_handler/nrf_sdh_ble.h>
23
24#ifdef __cplusplus
25extern "C" {
26#endif
27
35#define BLE_HIDS_DEF(_name) \
36 static struct ble_hids _name = { \
37 .link_ctx_storage = { \
38 .max_links_cnt = CONFIG_BLE_HIDS_MAX_CLIENTS, \
39 .link_ctx_size = sizeof(uint32_t) * BYTES_TO_WORDS(BLE_HIDS_LINK_CTX_SIZE),\
40 }, \
41 }; \
42 NRF_SDH_BLE_OBSERVER(_name##_obs, ble_hids_on_ble_evt, &_name, HIGH)
43
45#define BLE_HIDS_CONFIG_SEC_MODE_DEFAULT_MOUSE \
46 { \
47 .hid_info_char.read = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, \
48 .report_map_char.read = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, \
49 .protocol_mode_char = { \
50 .read = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, \
51 .write = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, \
52 }, \
53 .ctrl_point_char = { \
54 .write = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, \
55 }, \
56 .boot_mouse_inp_rep_char = { \
57 .read = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, \
58 .write = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, \
59 .cccd_write = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, \
60 }, \
61 }
62
64#define BLE_HIDS_CONFIG_SEC_MODE_DEFAULT_KEYBOARD \
65 { \
66 .hid_info_char.read = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, \
67 .report_map_char.read = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, \
68 .protocol_mode_char = { \
69 .read = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, \
70 .write = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, \
71 }, \
72 .ctrl_point_char = { \
73 .write = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, \
74 }, \
75 .boot_kb_inp_rep_char = { \
76 .read = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, \
77 .write = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, \
78 .cccd_write = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, \
79 }, \
80 .boot_kb_outp_rep_char = { \
81 .read = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, \
82 .write = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, \
83 .cccd_write = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, \
84 }, \
85 }
86
90#define BLE_HIDS_BOOT_KB_INPUT_REPORT_MAX_SIZE 8
94#define BLE_HIDS_BOOT_KB_OUTPUT_REPORT_MAX_SIZE 1
98#define BLE_HIDS_BOOT_MOUSE_INPUT_REPORT_MAX_SIZE 8
99
121
134 uint8_t *data;
138 uint16_t len;
139};
140
148 uint16_t len;
152 uint8_t *data;
153};
154
180
192
193#define BLE_HIDS_LINK_CTX_SIZE \
194 (sizeof(struct ble_hids_client_context) + CONFIG_BLE_HIDS_INPUT_REPORT_MAX_LEN + \
195 CONFIG_BLE_HIDS_OUTPUT_REPORT_MAX_LEN + CONFIG_BLE_HIDS_FEATURE_REPORT_MAX_LEN + \
196 (BLE_HIDS_BOOT_KB_INPUT_REPORT_MAX_SIZE) + (BLE_HIDS_BOOT_KB_OUTPUT_REPORT_MAX_SIZE) + \
197 (BLE_HIDS_BOOT_MOUSE_INPUT_REPORT_MAX_SIZE))
198
200#define CTX_DATA_POOL_SIZE ((CONFIG_BLE_HIDS_MAX_CLIENTS) * BYTES_TO_WORDS(BLE_HIDS_LINK_CTX_SIZE))
201
216
217/* Forward declaration of ble_hids structure. */
218struct ble_hids;
219
225 uint16_t uuid;
231 uint8_t report_type;
238};
239
281
293 uint16_t conn_handle;
297 const ble_evt_t *ble_evt;
301 union {
307 struct {
316 struct {
320 struct ble_hids_char_id char_id;
324 uint16_t offset;
328 uint16_t len;
332 const uint8_t *data;
337 struct {
341 struct ble_hids_char_id char_id;
346 struct {
350 uint32_t reason;
352 };
353};
354
358typedef void (*ble_hids_evt_handler_t)(struct ble_hids *hids, const struct ble_hids_evt *evt);
359
365 uint16_t len;
369 uint8_t report_id;
373 uint8_t report_type;
377 struct {
381 ble_gap_conn_sec_mode_t read;
385 ble_gap_conn_sec_mode_t write;
389 ble_gap_conn_sec_mode_t cccd_write;
391};
392
401 uint8_t *data;
405 uint16_t len;
413 const ble_uuid_t *ext_rep_ref;
414};
415
421 ble_gatts_char_handles_t char_handles;
425 uint16_t ref_handle;
426};
427
467 struct {
472 uint16_t bcd_hid;
479 struct {
487 uint8_t remote_wake: 1;
491 uint8_t reserved: 6;
492 } flags;
505 struct {
509 struct {
514 ble_gap_conn_sec_mode_t read;
519 struct {
524 ble_gap_conn_sec_mode_t read;
529 ble_gap_conn_sec_mode_t write;
534 struct {
539 ble_gap_conn_sec_mode_t write;
544 struct {
548 ble_gap_conn_sec_mode_t read;
553 struct {
558 ble_gap_conn_sec_mode_t read;
563 ble_gap_conn_sec_mode_t write;
568 ble_gap_conn_sec_mode_t cccd_write;
573 struct {
578 ble_gap_conn_sec_mode_t read;
583 ble_gap_conn_sec_mode_t write;
588 ble_gap_conn_sec_mode_t cccd_write;
593 struct {
598 ble_gap_conn_sec_mode_t read;
603 ble_gap_conn_sec_mode_t write;
608 ble_gap_conn_sec_mode_t cccd_write;
611};
612
614struct ble_hids {
628 ble_gatts_char_handles_t protocol_mode_handles;
636 struct ble_hids_rep_char inp_rep_array[CONFIG_BLE_HIDS_INPUT_REPORT_MAX_NUM];
644 struct ble_hids_rep_char outp_rep_array[CONFIG_BLE_HIDS_OUTPUT_REPORT_MAX_NUM];
652 struct ble_hids_rep_char feature_rep_array[CONFIG_BLE_HIDS_FEATURE_REPORT_MAX_NUM];
656 ble_gatts_char_handles_t rep_map_handles;
665 ble_gatts_char_handles_t boot_kb_inp_rep_handles;
670 ble_gatts_char_handles_t boot_kb_outp_rep_handles;
675 ble_gatts_char_handles_t boot_mouse_inp_rep_handles;
679 ble_gatts_char_handles_t hid_information_handles;
683 ble_gatts_char_handles_t hid_control_point_handles;
700};
701
715void ble_hids_on_ble_evt(const ble_evt_t *ble_evt, void *ble_hids);
716
729uint32_t ble_hids_init(struct ble_hids *hids, const struct ble_hids_config *hids_cfg);
730
746uint32_t ble_hids_inp_rep_send(struct ble_hids *hids, uint16_t conn_handle,
747 struct ble_hids_input_report *report);
748
762uint32_t ble_hids_boot_kb_inp_rep_send(struct ble_hids *hids, uint16_t conn_handle,
764
778uint32_t ble_hids_boot_mouse_inp_rep_send(struct ble_hids *hids, uint16_t conn_handle,
779 struct ble_hids_boot_mouse_input_report *report);
780
800uint32_t ble_hids_outp_rep_get(struct ble_hids *hids, uint8_t report_index, uint16_t len,
801 uint8_t offset, uint16_t conn_handle, uint8_t *outp_rep);
802
803#ifdef __cplusplus
804}
805#endif
806
807#endif /* BLE_HIDS_H__ */
808
uint32_t ble_hids_inp_rep_send(struct ble_hids *hids, uint16_t conn_handle, struct ble_hids_input_report *report)
Function for sending Input Report.
ble_hids_report_type
HID report types as defined in the Report Reference Characteristic descriptor.
Definition ble_hids.h:103
@ BLE_HIDS_REPORT_TYPE_FEATURE
Feature report.
Definition ble_hids.h:119
@ BLE_HIDS_REPORT_TYPE_RESERVED
Reserved.
Definition ble_hids.h:107
@ BLE_HIDS_REPORT_TYPE_OUTPUT
Output report.
Definition ble_hids.h:115
@ BLE_HIDS_REPORT_TYPE_INPUT
Input report.
Definition ble_hids.h:111
uint32_t ble_hids_boot_mouse_inp_rep_send(struct ble_hids *hids, uint16_t conn_handle, struct ble_hids_boot_mouse_input_report *report)
Function for sending Boot Mouse Input Report.
void ble_hids_on_ble_evt(const ble_evt_t *ble_evt, void *ble_hids)
Bluetooth LE event handler for the HID Service.
ble_hids_evt_type
HID Service event type.
Definition ble_hids.h:243
@ BLE_HIDS_EVT_REP_CHAR_WRITE
A new value has been written to an Report characteristic.
Definition ble_hids.h:263
@ BLE_HIDS_EVT_HOST_SUSP
Suspend command received.
Definition ble_hids.h:247
@ BLE_HIDS_EVT_REPORT_MODE_ENTERED
Report mode entered.
Definition ble_hids.h:271
@ BLE_HIDS_EVT_BOOT_MODE_ENTERED
Boot mode entered.
Definition ble_hids.h:267
@ BLE_HIDS_EVT_NOTIF_ENABLED
Notification enabled event.
Definition ble_hids.h:255
@ BLE_HIDS_EVT_HOST_EXIT_SUSP
Exit suspend command received.
Definition ble_hids.h:251
@ BLE_HIDS_EVT_NOTIF_DISABLED
Notification disabled event.
Definition ble_hids.h:259
@ BLE_HIDS_EVT_ERROR
Error.
Definition ble_hids.h:279
@ BLE_HIDS_EVT_REPORT_READ
Read with response.
Definition ble_hids.h:275
uint32_t ble_hids_outp_rep_get(struct ble_hids *hids, uint8_t report_index, uint16_t len, uint8_t offset, uint16_t conn_handle, uint8_t *outp_rep)
Function for getting the current value of Output Report from the stack.
void(* ble_hids_evt_handler_t)(struct ble_hids *hids, const struct ble_hids_evt *evt)
HID service event handler type.
Definition ble_hids.h:358
#define CTX_DATA_POOL_SIZE
Size of links context memory pool.
Definition ble_hids.h:200
uint32_t ble_hids_boot_kb_inp_rep_send(struct ble_hids *hids, uint16_t conn_handle, struct ble_hids_boot_keyboard_input_report *report)
Function for sending Boot Keyboard Input Report.
uint32_t ble_hids_init(struct ble_hids *hids, const struct ble_hids_config *hids_cfg)
Function for initializing the HID Service.
uint16_t len
Length of boot keyboard input data.
Definition ble_hids.h:148
uint8_t * data
Boot keyboard input data.
Definition ble_hids.h:152
Bluetooth LE HID service boot keyboard input report.
Definition ble_hids.h:144
int8_t delta_y
Vertical movement.
Definition ble_hids.h:170
uint8_t optional_data[5]
Optional data.
Definition ble_hids.h:178
uint16_t optional_data_len
Optional data length.
Definition ble_hids.h:174
int8_t delta_x
Horizontal movement.
Definition ble_hids.h:166
uint8_t buttons
Buttons mask.
Definition ble_hids.h:162
Bluetooth LE HID service boot mouse input report.
Definition ble_hids.h:158
uint8_t report_index
Index of the characteristic.
Definition ble_hids.h:237
uint8_t report_type
Type of report.
Definition ble_hids.h:231
uint16_t uuid
Characteristic UUID.
Definition ble_hids.h:225
HID Service characteristic id.
Definition ble_hids.h:221
uint8_t ctrl_pt
HID Control Point.
Definition ble_hids.h:190
uint8_t protocol_mode
Protocol mode.
Definition ble_hids.h:186
HID Host context structure. It keeps information relevant to a single host.
Definition ble_hids.h:182
struct ble_hids_rep_map_config report_map
Information nedeed for initialization of the Report Map characteristic.
Definition ble_hids.h:463
uint8_t output_report_count
Number of Output Report characteristics.
Definition ble_hids.h:447
uint8_t remote_wake
Device can be waked remotely.
Definition ble_hids.h:487
struct ble_hids_config::@82 hid_information
Value of the HID Information characteristic.
const struct ble_hids_report_config * input_report
Information about the Input Report characteristics.
Definition ble_hids.h:443
const struct ble_hids_report_config * output_report
Information about the Output Report characteristics.
Definition ble_hids.h:451
struct ble_hids_config::@83::@89 boot_mouse_inp_rep_char
Security requirements for HID Boot Keyboard Input Report characteristic.
uint16_t * included_services_array
Array of services to include in HID service.
Definition ble_hids.h:501
uint16_t bcd_hid
16-bit unsigned integer representing version number of base USB HID Specification implemented by HID ...
Definition ble_hids.h:472
struct ble_hids_config::@83::@90 boot_kb_inp_rep_char
Security requirements for HID Boot Keyboard Input Report characteristic.
ble_gap_conn_sec_mode_t write
Security requirement for writing HID Service Protocol Mode characteristic value.
Definition ble_hids.h:529
const struct ble_hids_report_config * feature_report
Information about the Feature Report characteristics.
Definition ble_hids.h:459
uint8_t input_report_count
Number of Input Report characteristics.
Definition ble_hids.h:439
struct ble_hids_config::@83::@91 boot_kb_outp_rep_char
Security requirements for HID Boot Keyboard Output Report characteristic.
uint8_t reserved
Reserved.
Definition ble_hids.h:491
struct ble_hids_config::@83::@88 report_map_char
Security requirement for HID service Report Map characteristic.
struct ble_hids_config::@83 sec_mode
Security configuration.
struct ble_hids_config::@83::@87 ctrl_point_char
Security requirement for HID service Control Point characteristic.
ble_gap_conn_sec_mode_t read
Security requirement for reading HID Information characteristic value.
Definition ble_hids.h:514
uint8_t normally_connectable
Device is normally connectable.
Definition ble_hids.h:483
struct ble_hids_config::@83::@85 hid_info_char
Security requirement for HID information characteristic.
uint8_t feature_report_count
Number of Feature Report characteristics.
Definition ble_hids.h:455
ble_gap_conn_sec_mode_t cccd_write
Security requirement for writing HID Boot Mouse Input Report characteristic CCCD.
Definition ble_hids.h:568
struct ble_hids_config::@83::@86 protocol_mode_char
Security requirement for HID service Protocol Mode characteristic.
uint8_t b_country_code
Identifies which country the hardware is localized for. Most hardware is not localized and thus this ...
Definition ble_hids.h:477
ble_hids_evt_handler_t evt_handler
HID service event handler.
Definition ble_hids.h:435
uint8_t included_services_count
Number of services to include in HID service.
Definition ble_hids.h:497
HID service configuration.
Definition ble_hids.h:431
struct ble_hids_evt::@75::@77 notification
Parameters for.
struct ble_hids_evt::@75::@80 error
Parameters for BLE_HIDS_EVT_ERROR.
const uint8_t * data
Incoming data.
Definition ble_hids.h:332
struct ble_hids_evt::@75::@79 char_auth_read
Parameters for BLE_HIDS_EVT_REPORT_READ.
uint16_t offset
Offset of the write operation.
Definition ble_hids.h:324
uint32_t reason
Error reason.
Definition ble_hids.h:350
struct ble_hids_char_id char_id
Characteristic ID.
Definition ble_hids.h:311
uint16_t len
Length of the write operation.
Definition ble_hids.h:328
enum ble_hids_evt_type evt_type
Event type.
Definition ble_hids.h:289
struct ble_hids_evt::@75::@78 char_write
Parameters for BLE_HIDS_EVT_REP_CHAR_WRITE.
const ble_evt_t * ble_evt
Bluetooth LE event.
Definition ble_hids.h:297
uint16_t conn_handle
Connection handle for which the event applies.
Definition ble_hids.h:293
HID Service event.
Definition ble_hids.h:285
uint8_t * data
Data to be sent.
Definition ble_hids.h:134
uint16_t len
Size of data to be sent.
Definition ble_hids.h:138
uint8_t report_index
Index of the characteristic. Corresponding to the index in struct ble_hids.inp_rep_array as passed to...
Definition ble_hids.h:130
HID input report.
Definition ble_hids.h:125
uint16_t ref_handle
Handle of the Report Reference descriptor.
Definition ble_hids.h:425
ble_gatts_char_handles_t char_handles
Handles related to the Report characteristic.
Definition ble_hids.h:421
HID Report characteristic structure.
Definition ble_hids.h:417
uint8_t * data
Report map data.
Definition ble_hids.h:401
uint8_t ext_rep_ref_num
Number of Optional External Report Reference descriptors.
Definition ble_hids.h:409
const ble_uuid_t * ext_rep_ref
Optional External Report Reference descriptor (will be added if != NULL).
Definition ble_hids.h:413
uint16_t len
Length of report map data.
Definition ble_hids.h:405
HID Service Report Map characteristic init structure. This contains all options and data needed for i...
Definition ble_hids.h:397
uint16_t len
Maximum length of characteristic value.
Definition ble_hids.h:365
uint8_t report_id
Non-zero value if there is more than one instance of the same Report Type.
Definition ble_hids.h:369
struct ble_hids_report_config::@81 sec_mode
Security requirements for HID Service Input Report characteristic.
ble_gap_conn_sec_mode_t write
Security requirement for writing HID Service characteristic value.
Definition ble_hids.h:385
ble_gap_conn_sec_mode_t read
Security requirement for reading HID Service characteristic value.
Definition ble_hids.h:381
ble_gap_conn_sec_mode_t cccd_write
Security requirement for writing HID Service characteristic CCCD.
Definition ble_hids.h:389
uint8_t report_type
Type of Report characteristic (see BLE_HIDS_REPORT_TYPE)
Definition ble_hids.h:373
HID Report configuration.
Definition ble_hids.h:361
ble_gatts_char_handles_t rep_map_handles
Handles related to the Report Map characteristic.
Definition ble_hids.h:656
ble_gatts_char_handles_t boot_kb_outp_rep_handles
Handles related to the Boot Keyboard Output Report characteristic (will only be created if struct ble...
Definition ble_hids.h:670
uint8_t feature_report_count
Number of Feature Report characteristics.
Definition ble_hids.h:648
struct ble_hids_link_ctx_storage link_ctx_storage
Link context storage with handles of all current connections and its data context.
Definition ble_hids.h:687
struct ble_hids_rep_char outp_rep_array[CONFIG_BLE_HIDS_OUTPUT_REPORT_MAX_NUM]
Information about the Output Report characteristics.
Definition ble_hids.h:644
uint16_t service_handle
Handle of HID Service (as provided by the Bluetooth LE stack).
Definition ble_hids.h:622
uint16_t rep_map_ext_rep_ref_handle
Handle of the Report Map External Report Reference descriptor.
Definition ble_hids.h:660
struct ble_hids_rep_char inp_rep_array[CONFIG_BLE_HIDS_INPUT_REPORT_MAX_NUM]
Information about the Input Report characteristics.
Definition ble_hids.h:636
ble_gatts_char_handles_t boot_kb_inp_rep_handles
Handles related to the Boot Keyboard Input Report characteristic (will only be created if struct ble_...
Definition ble_hids.h:665
const struct ble_hids_report_config * feature_rep_init_array
Information about the Feature Report characteristics.
Definition ble_hids.h:699
struct ble_hids_rep_char feature_rep_array[CONFIG_BLE_HIDS_FEATURE_REPORT_MAX_NUM]
Information about the Feature Report characteristics.
Definition ble_hids.h:652
uint8_t input_report_count
Number of Input Report characteristics.
Definition ble_hids.h:632
ble_gatts_char_handles_t hid_information_handles
Handles related to the Report Map characteristic.
Definition ble_hids.h:679
ble_gatts_char_handles_t hid_control_point_handles
Handles related to the Report Map characteristic.
Definition ble_hids.h:683
uint8_t output_report_count
Number of Output Report characteristics.
Definition ble_hids.h:640
ble_hids_evt_handler_t evt_handler
Event handler to be called for handling events in the HID Service.
Definition ble_hids.h:618
ble_gatts_char_handles_t boot_mouse_inp_rep_handles
Handles related to the Boot Mouse Input Report characteristic (will only be created if struct ble_hid...
Definition ble_hids.h:675
const struct ble_hids_report_config * outp_rep_init_array
Information about the Output Report characteristics.
Definition ble_hids.h:695
ble_gatts_char_handles_t protocol_mode_handles
Handles related to the Protocol Mode characteristic (will only be created if CONFIG_BLE_HIDS_BOOT_KEY...
Definition ble_hids.h:628
const struct ble_hids_report_config * inp_rep_init_array
Information about the Input Report characteristics.
Definition ble_hids.h:691
HID Service structure. This contains various status information for the service.
Definition ble_hids.h:614