Skip to content

Commit aeeb573

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

File tree

6 files changed

+48
-14
lines changed

6 files changed

+48
-14
lines changed

applications/firmware_loader/ble_mcumgr/src/main.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ int main(void)
189189
.uuid = BLE_MCUMGR_SERVICE_UUID_SUB,
190190
},
191191
};
192+
struct ble_mcumgr_config mcumgr_cfg = {
193+
.sec_mode = BLE_MCUMGR_CONFIG_SEC_MODE_DEFAULT,
194+
};
192195

193196
LOG_INF("BLE MCUmgr sample started");
194197
mgmt_callback_register(&os_mgmt_reboot_callback);
@@ -211,7 +214,7 @@ int main(void)
211214

212215
LOG_INF("Bluetooth enabled");
213216

214-
nrf_err = ble_mcumgr_init();
217+
nrf_err = ble_mcumgr_init(&mcumgr_cfg);
215218

216219
if (nrf_err) {
217220
LOG_ERR("Failed to initialize MCUmgr service, nrf_error %#x", nrf_err);

doc/nrf-bm/libraries/bluetooth/services/ble_mcumgr.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Configuration
1515
*************
1616

1717
Set the :kconfig:option:`CONFIG_BLE_MCUMGR` Kconfig option to enable the service.
18+
The characteristic security mode is configured in the :c:struct:`ble_mcumgr_config` structure provided during initialization.
1819

1920
Initialization
2021
==============

doc/nrf-bm/release_notes/release_notes_changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ BLE Services
143143
* :ref:`lib_ble_service_hids` service.
144144
* :ref:`lib_ble_service_hrs` service.
145145
* :ref:`lib_ble_service_lbs` service.
146+
* :ref:`lib_ble_service_mcumgr` service.
146147

147148
* :ref:`lib_ble_service_nus` service:
148149

include/bm/bluetooth/services/ble_mcumgr.h

Lines changed: 30 additions & 1 deletion
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
@@ -34,13 +35,41 @@ extern "C" {
3435
#define BLE_MCUMGR_SERVICE_UUID_SUB 0xdc1d
3536
#define BLE_MCUMGR_CHARACTERISTIC_UUID_SUB 0x7828
3637

38+
/** @brief Default security configuration. */
39+
#define BLE_MCUMGR_CONFIG_SEC_MODE_DEFAULT \
40+
{ \
41+
.mcumgr_char = { \
42+
.read = BLE_GAP_CONN_SEC_MODE_OPEN, \
43+
.write = BLE_GAP_CONN_SEC_MODE_OPEN, \
44+
.cccd_write = BLE_GAP_CONN_SEC_MODE_OPEN, \
45+
}, \
46+
}
47+
48+
/**
49+
* @brief MCUmgr service configuration.
50+
*/
51+
struct ble_mcumgr_config {
52+
/** Security configuration. */
53+
struct {
54+
/** MCUmgr characteristic */
55+
struct {
56+
/** Security requirement for reading MCUmgr characteristic value. */
57+
ble_gap_conn_sec_mode_t read;
58+
/** Security requirement for writing MCUmgr characteristic value. */
59+
ble_gap_conn_sec_mode_t write;
60+
/** Security requirement for writing MCUmgr characteristic CCCD. */
61+
ble_gap_conn_sec_mode_t cccd_write;
62+
} mcumgr_char;
63+
} sec_mode;
64+
};
65+
3766
/**
3867
* @brief Function for initializing the MCUmgr Bluetooth service.
3968
*
4069
* @retval NRF_SUCCESS On success.
4170
* @retval NRF_ERROR_INVALID_PARAM Invalid parameters.
4271
*/
43-
uint32_t ble_mcumgr_init(void);
72+
uint32_t ble_mcumgr_init(struct ble_mcumgr_config *cfg);
4473

4574
/**
4675
* @brief Function for getting the MCUmgr Bluetooth service UUID type.

samples/boot/mcuboot_recovery_entry/src/main.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ int main(void)
167167
.uuid = BLE_MCUMGR_SERVICE_UUID_SUB,
168168
},
169169
};
170+
struct ble_mcumgr_config mcumgr_cfg = {
171+
.sec_mode = BLE_MCUMGR_CONFIG_SEC_MODE_DEFAULT,
172+
};
170173

171174
mgmt_callback_register(&os_mgmt_reboot_callback);
172175

@@ -186,7 +189,7 @@ int main(void)
186189

187190
LOG_INF("Bluetooth enabled");
188191

189-
nrf_err = ble_mcumgr_init();
192+
nrf_err = ble_mcumgr_init(&mcumgr_cfg);
190193

191194
if (nrf_err) {
192195
LOG_ERR("Failed to initialize MCUmgr, nrf_error %#x", nrf_err);

subsys/bluetooth/services/ble_mcumgr/mcumgr.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,16 @@ static struct ble_mcumgr_client_context *ble_mcumgr_client_context_get(uint16_t
7979
return ((idx >= 0) ? &contexts[idx] : NULL);
8080
}
8181

82-
static uint32_t mcumgr_characteristic_add(struct ble_mcumgr *service)
82+
static uint32_t mcumgr_characteristic_add(struct ble_mcumgr *service, struct ble_mcumgr_config *cfg)
8383
{
8484
ble_uuid_t char_uuid = {
8585
.type = service->uuid_type_characteristic,
8686
.uuid = BLE_MCUMGR_CHARACTERISTIC_UUID_SUB,
8787
};
8888
ble_gatts_attr_md_t cccd_md = {
89-
.vloc = BLE_GATTS_VLOC_STACK
89+
.vloc = BLE_GATTS_VLOC_STACK,
90+
.read_perm = BLE_GAP_CONN_SEC_MODE_OPEN,
91+
.write_perm = cfg->sec_mode.mcumgr_char.cccd_write,
9092
};
9193
ble_gatts_char_md_t char_md = {
9294
.char_props = {
@@ -98,6 +100,8 @@ static uint32_t mcumgr_characteristic_add(struct ble_mcumgr *service)
98100
ble_gatts_attr_md_t attr_md = {
99101
.vloc = BLE_GATTS_VLOC_STACK,
100102
.vlen = true,
103+
.read_perm = cfg->sec_mode.mcumgr_char.read,
104+
.write_perm = cfg->sec_mode.mcumgr_char.write,
101105
};
102106
ble_gatts_attr_t attr_char_value = {
103107
.p_uuid = &char_uuid,
@@ -107,13 +111,6 @@ static uint32_t mcumgr_characteristic_add(struct ble_mcumgr *service)
107111
.max_len = BLE_GATT_MAX_DATA_LEN,
108112
};
109113

110-
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm);
111-
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm);
112-
113-
/* Setup CCCD */
114-
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm);
115-
BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm);
116-
117114
return sd_ble_gatts_characteristic_add(service->service_handle, &char_md, &attr_char_value,
118115
&service->characteristic_handle);
119116
}
@@ -374,7 +371,7 @@ static void on_ble_evt(const ble_evt_t *evt, void *ctx)
374371

375372
NRF_SDH_BLE_OBSERVER(sdh_ble, on_ble_evt, &ble_mcumgr, 0);
376373

377-
uint32_t ble_mcumgr_init(void)
374+
uint32_t ble_mcumgr_init(struct ble_mcumgr_config *cfg)
378375
{
379376
uint32_t nrf_err;
380377
ble_uuid_t ble_uuid;
@@ -418,7 +415,7 @@ uint32_t ble_mcumgr_init(void)
418415
}
419416

420417
/* Add MCUmgr characteristic */
421-
nrf_err = mcumgr_characteristic_add(&ble_mcumgr);
418+
nrf_err = mcumgr_characteristic_add(&ble_mcumgr, cfg);
422419

423420
if (nrf_err) {
424421
LOG_ERR("mcumgr_characteristic_add failed, nrf_error %#x", nrf_err);

0 commit comments

Comments
 (0)