Skip to content

Commit b8a2f70

Browse files
committed
bluetooth: service: HID service
Add Bluetooth HID service and HID keyboard sample. Signed-off-by: Emanuele Di Santo <[email protected]>
1 parent ffd3376 commit b8a2f70

File tree

11 files changed

+2550
-0
lines changed

11 files changed

+2550
-0
lines changed

include/bluetooth/services/ble_hids.h

Lines changed: 590 additions & 0 deletions
Large diffs are not rendered by default.

include/bluetooth/services/common.h

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11

2+
#include "ble_gap.h"
23
#include <ble.h>
34
#include <zephyr/sys/byteorder.h>
45

@@ -13,3 +14,72 @@ static inline uint16_t is_notification_enabled(const uint8_t *gatts_write_data)
1314
{ \
1415
.sm = ((x) >> 4) & 0xf, .lv = (x) & 0xf, \
1516
}
17+
18+
static inline bool ble_gap_conn_sec_mode_equal(const ble_gap_conn_sec_mode_t *a,
19+
const ble_gap_conn_sec_mode_t *b)
20+
{
21+
return (a->sm == b->sm) && (a->lv == b->lv);
22+
}
23+
24+
/**
25+
* @brief Set sec_mode to have no access rights.
26+
*/
27+
#define BLE_GAP_CONN_SEC_MODE_NO_ACCESS \
28+
(ble_gap_conn_sec_mode_t) \
29+
{ \
30+
.sm = 0, .lv = 0 \
31+
}
32+
33+
/**
34+
* @brief Set sec_mode to require no protection, open link.
35+
*/
36+
#define BLE_GAP_CONN_SEC_MODE_OPEN \
37+
(ble_gap_conn_sec_mode_t) \
38+
{ \
39+
.sm = 1, .lv = 1 \
40+
}
41+
42+
/**
43+
* @brief Set sec_mode to require encryption, but no MITM protection.
44+
*/
45+
#define BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM \
46+
(ble_gap_conn_sec_mode_t) \
47+
{ \
48+
.sm = 1, .lv = 2 \
49+
}
50+
51+
/**
52+
* @brief Set sec_mode to require encryption and MITM protection.
53+
*/
54+
#define BLE_GAP_CONN_SEC_MODE_ENC_WITH_MITM \
55+
(ble_gap_conn_sec_mode_t) \
56+
{ \
57+
.sm = 1, .lv = 3 \
58+
}
59+
60+
/**
61+
* @brief Set sec_mode to require LESC encryption and MITM protection.
62+
*/
63+
#define BLE_GAP_CONN_SEC_MODE_LESC_ENC_WITH_MITM \
64+
(ble_gap_conn_sec_mode_t) \
65+
{ \
66+
.sm = 1, .lv = 4 \
67+
}
68+
69+
/**
70+
* @brief Set sec_mode to require signing or encryption, no MITM protection needed.
71+
*/
72+
#define BLE_GAP_CONN_SEC_MODE_SIGNED_NO_MITM \
73+
(ble_gap_conn_sec_mode_t) \
74+
{ \
75+
.sm = 2, .lv = 1 \
76+
}
77+
78+
/**
79+
* @brief Set sec_mode to require signing or encryption with MITM protection.
80+
*/
81+
#define BLE_GAP_CONN_SEC_MODE_SIGNED_WITH_MITM \
82+
(ble_gap_conn_sec_mode_t) \
83+
{ \
84+
.sm = 2, .lv = 2 \
85+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#
2+
# Copyright (c) 2024 Nordic Semiconductor ASA
3+
#
4+
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5+
#
6+
7+
cmake_minimum_required(VERSION 3.20.0)
8+
9+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
10+
project(ble_hid_keyboard)
11+
12+
target_sources(app PRIVATE src/main.c)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
&flash0 {
3+
partitions {
4+
compatible = "fixed-partitions";
5+
#address-cells = <1>;
6+
#size-cells = <1>;
7+
softdevice: partition@0 {
8+
label = "sd";
9+
reg = <0x00000000 0x00027000>;
10+
};
11+
app_rom: partition@27000 {
12+
label = "app";
13+
reg = <0x00027000 0x000d9000>;
14+
};
15+
};
16+
};
17+
18+
&sram0 {
19+
reg = <0x20003400 DT_SIZE_K(200)>;
20+
};
21+
22+
23+
/ {
24+
chosen {
25+
zephyr,flash = &app_rom;
26+
zephyr,sram = &sram0;
27+
};
28+
};

samples/ble_hids_keyboard/prj.conf

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
CONFIG_MULTITHREADING=n
2+
CONFIG_ZERO_LATENCY_IRQS=y
3+
CONFIG_CLOCK_CONTROL=n
4+
5+
CONFIG_NRF_SDH=y
6+
CONFIG_SOFTDEVICE_S140=y
7+
8+
CONFIG_LOG=y
9+
CONFIG_LOG_MODE_MINIMAL=y
10+
11+
# BLE Advertising
12+
CONFIG_BLE_ADV=y
13+
CONFIG_BLE_ADV_EXTENDED_ADVERTISING=n
14+
CONFIG_BLE_ADV_DIRECTED_ADVERTISING=n
15+
# BLE connection parameter
16+
CONFIG_BLE_CONN_PARAMS=y
17+
18+
# Battery service
19+
CONFIG_BLE_BAS=y
20+
# Device information service
21+
CONFIG_BLE_DIS=y
22+
CONFIG_BLE_DIS_SERIAL_NUMBER="ABCD"
23+
CONFIG_BLE_DIS_HW_REVISION="hw 54.15.0"
24+
CONFIG_BLE_DIS_FW_REVISION="fw 17.2.0" # wink
25+
CONFIG_BLE_DIS_SW_REVISION="sw 1.0.0"
26+
# HID service
27+
CONFIG_BLE_HIDS=y
28+
CONFIG_BLE_HIDS_BOOT_KEYBOARD=y
29+
CONFIG_BLE_HIDS_LOG_LEVEL_DBG=y
30+
31+
# Shrink
32+
CONFIG_GPIO=n
33+
CONFIG_PARTITION_MANAGER_ENABLED=n
34+
CONFIG_LOG_BACKEND_RTT=n

samples/ble_hids_keyboard/sample.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
sample:
2+
name: BLE HID Keyboard
3+
tests:
4+
sample.ble_hid_keyboard:
5+
sysbuild: true
6+
build_only: true
7+
integration_platforms:
8+
- nrf52840dk/nrf52840
9+
platform_allow:
10+
- nrf52840dk/nrf52840
11+
tags: ci_build

0 commit comments

Comments
 (0)