Skip to content

Commit a1a64c7

Browse files
bluetooth: services: nus: update security configuration
Update security configuration for NUS service. Signed-off-by: Eivind Jølsgard <[email protected]>
1 parent aeeb573 commit a1a64c7

File tree

4 files changed

+39
-10
lines changed

4 files changed

+39
-10
lines changed

doc/nrf-bm/release_notes/release_notes_changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ BLE Services
144144
* :ref:`lib_ble_service_hrs` service.
145145
* :ref:`lib_ble_service_lbs` service.
146146
* :ref:`lib_ble_service_mcumgr` service.
147+
* :ref:`lib_ble_service_nus` service.
147148

148149
* :ref:`lib_ble_service_nus` service:
149150

include/bm/bluetooth/services/ble_nus.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <stdint.h>
1818
#include <stdbool.h>
1919
#include <ble.h>
20+
#include <bm/bluetooth/services/common.h>
2021
#include <bm/softdevice_handler/nrf_sdh_ble.h>
2122

2223
#ifdef __cplusplus
@@ -50,6 +51,19 @@ void ble_nus_on_ble_evt(ble_evt_t const *ble_evt, void *context);
5051
&_name, \
5152
0)
5253

54+
/** @brief Default security configuration. */
55+
#define BLE_NUS_CONFIG_SEC_MODE_DEFAULT \
56+
{ \
57+
.nus_rx_char = { \
58+
.read = BLE_GAP_CONN_SEC_MODE_OPEN, \
59+
.write = BLE_GAP_CONN_SEC_MODE_OPEN, \
60+
}, \
61+
.nus_tx_char = { \
62+
.read = BLE_GAP_CONN_SEC_MODE_OPEN, \
63+
.cccd_write = BLE_GAP_CONN_SEC_MODE_OPEN, \
64+
}, \
65+
}
66+
5367
#define OPCODE_LENGTH 1
5468
#define HANDLE_LENGTH 2
5569

@@ -132,6 +146,23 @@ typedef void (*ble_nus_evt_handler_t) (const struct ble_nus_evt *evt);
132146
struct ble_nus_config {
133147
/** Event handler to be called for handling received data. */
134148
ble_nus_evt_handler_t evt_handler;
149+
/** Security configuration. */
150+
struct {
151+
/** NUS Service RX characteristic */
152+
struct {
153+
/** Security requirement for reading NUS rx characteristic value. */
154+
ble_gap_conn_sec_mode_t read;
155+
/** Security requirement for writing NUS rx characteristic value. */
156+
ble_gap_conn_sec_mode_t write;
157+
} nus_rx_char;
158+
/** NUS Service TX characteristic */
159+
struct {
160+
/** Security requirement for reading NUS TX characteristic value. */
161+
ble_gap_conn_sec_mode_t read;
162+
/** Security requirement for writing NUS TX characteristic CCCD. */
163+
ble_gap_conn_sec_mode_t cccd_write;
164+
} nus_tx_char;
165+
} sec_mode;
135166
};
136167

137168
/**

samples/bluetooth/ble_nus/src/main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,7 @@ int main(void)
453453

454454
struct ble_nus_config nus_cfg = {
455455
.evt_handler = ble_nus_evt_handler,
456+
.sec_mode = BLE_NUS_CONFIG_SEC_MODE_DEFAULT,
456457
};
457458
struct ble_qwr_config qwr_config = {
458459
.evt_handler = ble_qwr_evt_handler,

subsys/bluetooth/services/ble_nus/nus.c

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ static uint32_t nus_rx_char_add(struct ble_nus *nus, struct ble_nus_config const
3838
ble_gatts_attr_md_t attr_md = {
3939
.vloc = BLE_GATTS_VLOC_STACK,
4040
.vlen = true,
41+
.read_perm = cfg->sec_mode.nus_rx_char.read,
42+
.write_perm = cfg->sec_mode.nus_rx_char.write,
4143
};
4244
ble_gatts_attr_t attr_char_value = {
4345
.p_uuid = &char_uuid,
@@ -47,9 +49,6 @@ static uint32_t nus_rx_char_add(struct ble_nus *nus, struct ble_nus_config const
4749
.max_len = BLE_NUS_MAX_DATA_LEN,
4850
};
4951

50-
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);
51-
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm);
52-
5352
/* Add Nordic UART RX characteristic declaration and value attributes. */
5453
return sd_ble_gatts_characteristic_add(nus->service_handle, &char_md, &attr_char_value,
5554
&nus->rx_handles);
@@ -62,7 +61,9 @@ static uint32_t nus_tx_char_add(struct ble_nus *nus, struct ble_nus_config const
6261
.uuid = BLE_UUID_NUS_TX_CHARACTERISTIC,
6362
};
6463
ble_gatts_attr_md_t cccd_md = {
65-
.vloc = BLE_GATTS_VLOC_STACK
64+
.vloc = BLE_GATTS_VLOC_STACK,
65+
.read_perm = BLE_GAP_CONN_SEC_MODE_OPEN,
66+
.write_perm = cfg->sec_mode.nus_tx_char.cccd_write,
6667
};
6768
ble_gatts_char_md_t char_md = {
6869
.char_props = {
@@ -73,6 +74,7 @@ static uint32_t nus_tx_char_add(struct ble_nus *nus, struct ble_nus_config const
7374
ble_gatts_attr_md_t attr_md = {
7475
.vloc = BLE_GATTS_VLOC_STACK,
7576
.vlen = true,
77+
.read_perm = cfg->sec_mode.nus_tx_char.read,
7678
};
7779
ble_gatts_attr_t attr_char_value = {
7880
.p_uuid = &char_uuid,
@@ -82,12 +84,6 @@ static uint32_t nus_tx_char_add(struct ble_nus *nus, struct ble_nus_config const
8284
.max_len = BLE_NUS_MAX_DATA_LEN,
8385
};
8486

85-
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);
86-
87-
/* Setup CCCD */
88-
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm);
89-
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm);
90-
9187
/* Add Nordic UART TX declaration, value and CCCD attributes */
9288
return sd_ble_gatts_characteristic_add(nus->service_handle, &char_md, &attr_char_value,
9389
&nus->tx_handles);

0 commit comments

Comments
 (0)