diff --git a/applications/firmware_loader/ble_mcumgr/src/main.c b/applications/firmware_loader/ble_mcumgr/src/main.c index 481ac736d3..6a1ec1eb08 100644 --- a/applications/firmware_loader/ble_mcumgr/src/main.c +++ b/applications/firmware_loader/ble_mcumgr/src/main.c @@ -189,6 +189,9 @@ int main(void) .uuid = BLE_MCUMGR_SERVICE_UUID_SUB, }, }; + struct ble_mcumgr_config mcumgr_cfg = { + .sec_mode = BLE_MCUMGR_CONFIG_SEC_MODE_DEFAULT, + }; LOG_INF("BLE MCUmgr sample started"); mgmt_callback_register(&os_mgmt_reboot_callback); @@ -211,7 +214,7 @@ int main(void) LOG_INF("Bluetooth enabled"); - nrf_err = ble_mcumgr_init(); + nrf_err = ble_mcumgr_init(&mcumgr_cfg); if (nrf_err) { LOG_ERR("Failed to initialize MCUmgr service, nrf_error %#x", nrf_err); diff --git a/doc/nrf-bm/libraries/bluetooth/services/ble_mcumgr.rst b/doc/nrf-bm/libraries/bluetooth/services/ble_mcumgr.rst index 508e982b32..436606959b 100644 --- a/doc/nrf-bm/libraries/bluetooth/services/ble_mcumgr.rst +++ b/doc/nrf-bm/libraries/bluetooth/services/ble_mcumgr.rst @@ -15,6 +15,7 @@ Configuration ************* Set the :kconfig:option:`CONFIG_BLE_MCUMGR` Kconfig option to enable the service. +The characteristic security mode is configured in the :c:struct:`ble_mcumgr_config` structure provided during initialization. Initialization ============== diff --git a/doc/nrf-bm/release_notes/release_notes_changelog.rst b/doc/nrf-bm/release_notes/release_notes_changelog.rst index ebffc06962..64b8472ea9 100644 --- a/doc/nrf-bm/release_notes/release_notes_changelog.rst +++ b/doc/nrf-bm/release_notes/release_notes_changelog.rst @@ -96,7 +96,7 @@ Libraries * Added the :ref:`lib_ble_radio_notification` library. -* Updated the following libraries and BLE services to return ``nrf_errors`` instead of ``errnos``: +* Updated the following libraries and Bluetooth LE services to return ``nrf_errors`` instead of ``errnos``: * :ref:`lib_ble_adv` library. * :ref:`lib_ble_conn_params` library. @@ -108,7 +108,7 @@ Libraries * :ref:`lib_ble_service_lbs` service. * :ref:`lib_ble_service_mcumgr` service. * :ref:`lib_ble_service_nus` service. - * BLE Record Access Control Point library. + * Bluetooth LE Record Access Control Point library. * :ref:`lib_ble_conn_params` library: @@ -175,6 +175,20 @@ Libraries * To use errno instead of nrf_errors. * The :c:func:`bm_storage_init` function to expect an additional input parameter of type pointer to struct :c:struct:`bm_storage_config` for configuring the storage instance that is being initialized. +Bluetooth LE Services +------------ + +* Updated the characteristic security configuration for the following Bluetooth LE services: + + * :ref:`lib_ble_service_bas` service. + * :ref:`lib_ble_service_cgms` service. + * :ref:`lib_ble_service_dis` service. + * :ref:`lib_ble_service_hids` service. + * :ref:`lib_ble_service_hrs` service. + * :ref:`lib_ble_service_lbs` service. + * :ref:`lib_ble_service_mcumgr` service. + * :ref:`lib_ble_service_nus` service. + * :ref:`lib_ble_service_nus` service: * Fixed an issue where the client context was shared between all instances. diff --git a/include/bm/bluetooth/services/ble_bas.h b/include/bm/bluetooth/services/ble_bas.h index 9ff23c86b9..35db75db24 100644 --- a/include/bm/bluetooth/services/ble_bas.h +++ b/include/bm/bluetooth/services/ble_bas.h @@ -16,6 +16,7 @@ #include #include #include +#include #ifdef __cplusplus extern "C" { @@ -31,6 +32,16 @@ extern "C" { extern void ble_bas_on_ble_evt(const ble_evt_t *ble_evt, void *ctx); \ NRF_SDH_BLE_OBSERVER(_name##_obs, ble_bas_on_ble_evt, &_name, HIGH) +/** @brief Default security configuration. */ +#define BLE_BAS_CONFIG_SEC_MODE_DEFAULT \ + { \ + .battery_lvl_char = { \ + .read = BLE_GAP_CONN_SEC_MODE_OPEN, \ + .cccd_write = BLE_GAP_CONN_SEC_MODE_OPEN, \ + }, \ + .battery_report_ref.read = BLE_GAP_CONN_SEC_MODE_OPEN, \ + } + /** * @brief Battery service event types. */ @@ -102,18 +113,21 @@ struct ble_bas_config { * @brief Initial battery level. */ uint8_t battery_level; - /** - * @brief Security requirement for reading the battery level characteristic value. - */ - ble_gap_conn_sec_mode_t batt_rd_sec; - /** - * @brief Security requirement for writing the battery level characteristic CCCD. - */ - ble_gap_conn_sec_mode_t cccd_wr_sec; - /** - * @brief Security requirement for reading the Report Reference characteristic descriptor. - */ - ble_gap_conn_sec_mode_t report_ref_rd_sec; + /** Characteristic security. */ + struct { + /** Battery Level characteristic */ + struct { + /** Security requirement for reading battery level characteristic value. */ + ble_gap_conn_sec_mode_t read; + /** Security requirement for writing battery level characteristic CCCD. */ + ble_gap_conn_sec_mode_t cccd_write; + } battery_lvl_char; + /** Battery Service report reference. */ + struct { + /** Security requirement for reading Battery Service report reference. */ + ble_gap_conn_sec_mode_t read; + } battery_report_ref; + } sec_mode; }; /** diff --git a/include/bm/bluetooth/services/ble_cgms.h b/include/bm/bluetooth/services/ble_cgms.h index a7b017bd1d..10c3b5c114 100644 --- a/include/bm/bluetooth/services/ble_cgms.h +++ b/include/bm/bluetooth/services/ble_cgms.h @@ -26,6 +26,7 @@ #include #include +#include #include #include @@ -45,6 +46,29 @@ extern "C" { ble_cgms_on_ble_evt, &_name, \ HIGH) +/** @brief Default security configuration. */ +#define BLE_CGMS_CONFIG_SEC_MODE_KCONFIG \ + { \ + .feature_char.read = BLE_GAP_CONN_SEC_MODE_OPEN, \ + .status_char.read = BLE_GAP_CONN_SEC_MODE_OPEN, \ + .srt_char.read = BLE_GAP_CONN_SEC_MODE_OPEN, \ + .meas_char = { \ + .cccd_write = BLE_GAP_CONN_SEC_MODE_OPEN, \ + }, \ + .racp_char = { \ + .write = BLE_GAP_CONN_SEC_MODE_OPEN, \ + .cccd_write = BLE_GAP_CONN_SEC_MODE_OPEN, \ + }, \ + .socp_char = { \ + .write = BLE_GAP_CONN_SEC_MODE_OPEN, \ + .cccd_write = BLE_GAP_CONN_SEC_MODE_OPEN, \ + }, \ + .sst_char = { \ + .read = BLE_GAP_CONN_SEC_MODE_OPEN, \ + .write = BLE_GAP_CONN_SEC_MODE_OPEN, \ + }, \ + } + #define OPCODE_LENGTH 1 #define HANDLE_LENGTH 2 @@ -374,6 +398,64 @@ struct ble_cgms_config { struct ble_cgms_status initial_sensor_status; /** Run time. */ uint16_t initial_run_time; + /** Characteristic security. */ + struct { + /** Feature characteristic */ + struct { + /** Security requirement for reading feature characteristic value. */ + ble_gap_conn_sec_mode_t read; + } feature_char; + /** Status characteristic */ + struct { + /** Security requirement for reading status characteristic value. */ + ble_gap_conn_sec_mode_t read; + } status_char; + /** Session Run Time characteristic */ + struct { + /** Security requirement for reading Session Run Time (SRT) characteristic + * value. + */ + ble_gap_conn_sec_mode_t read; + } srt_char; + /** Measurement characteristic */ + struct { + /** Security requirement for writing measurement characteristic CCCD. */ + ble_gap_conn_sec_mode_t cccd_write; + } meas_char; + /** Record Access Control Point */ + struct { + /** Security requirement for writing Record Access Control Point (RACP) + * characteristic value. + */ + ble_gap_conn_sec_mode_t write; + /** Security requirement for writing Record Access Control Point (RACP) + * characteristic CCCD. + */ + ble_gap_conn_sec_mode_t cccd_write; + } racp_char; + /** Specific Operation Control Point (SOCP) */ + struct { + /** Security requirement for writing Specific Operation Control Point (SOCP) + * characteristic value. + */ + ble_gap_conn_sec_mode_t write; + /** Security requirement for writing Specific Operation Control Point (SOCP) + * characteristic CCCD. + */ + ble_gap_conn_sec_mode_t cccd_write; + } socp_char; + /** Session Start Time (SST) */ + struct { + /** Security requirement for reading Session Start Time (SST) characteristic + * value. + */ + ble_gap_conn_sec_mode_t read; + /** Security requirement for writing Session Start Time (SST) characteristic + * value. + */ + ble_gap_conn_sec_mode_t write; + } sst_char; + } sec_mode; }; /** @brief Specific Operation Control Point response structure. */ diff --git a/include/bm/bluetooth/services/ble_dis.h b/include/bm/bluetooth/services/ble_dis.h index d196f45905..bafd23c631 100644 --- a/include/bm/bluetooth/services/ble_dis.h +++ b/include/bm/bluetooth/services/ble_dis.h @@ -22,22 +22,47 @@ #define BLE_DIS_H__ #include +#include #ifdef __cplusplus extern "C" { #endif +/** @brief Default security configuration. */ +#define BLE_DIS_CONFIG_SEC_MODE_DEFAULT \ + { \ + .device_info_char.read = BLE_GAP_CONN_SEC_MODE_OPEN, \ + } + /** - * @brief Function for initializing the Device Information Service. + * @brief Device Information service configuration. + */ +struct ble_dis_config { + /** Security configuration. */ + struct { + /** Device information characteristic */ + struct { + /** Security requirement for reading device information characteristic + * value. + */ + ble_gap_conn_sec_mode_t read; + } device_info_char; + } sec_mode; +}; + +/** + * @brief Initialize the Device Information Service. * * @details This call allows the application to initialize the device information service. - * It adds the DIS service and DIS characteristics to the database, using the initial - * values supplied through the p_dis_init parameter. Characteristics which are not to be - * added, shall be set to NULL in p_dis_init. + * It adds the DIS service and DIS characteristics to the database, using the + * values supplied through the Kconfig options. + * + * @param dis_config Device Information Service configuration. * - * @return NRF_SUCCESS on successful initialization of service or nrf_error on failure. + * @return NRF_SUCCESS on successful initialization of service. + * @retval NRF_ERROR_NULL If @p dis_config is @c NULL. */ -uint32_t ble_dis_init(void); +uint32_t ble_dis_init(struct ble_dis_config *dis_config); #ifdef __cplusplus } diff --git a/include/bm/bluetooth/services/ble_hids.h b/include/bm/bluetooth/services/ble_hids.h index 70a978fd0d..8eee0e0d9d 100644 --- a/include/bm/bluetooth/services/ble_hids.h +++ b/include/bm/bluetooth/services/ble_hids.h @@ -43,6 +43,47 @@ extern "C" { }; \ NRF_SDH_BLE_OBSERVER(_name##_obs, ble_hids_on_ble_evt, &_name, HIGH) +/** @brief Default mouse security configuration. */ +#define BLE_HIDS_CONFIG_SEC_MODE_DEFAULT_MOUSE \ + { \ + .hid_info.read = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, \ + .protocol_mode_char = { \ + .read = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, \ + .write = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, \ + }, \ + .ctrl_point_char = { \ + .write = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, \ + }, \ + .boot_mouse_inp_rep_char = { \ + .read = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, \ + .write = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, \ + .cccd_write = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, \ + }, \ + } + +/** @brief Default keyboard security configuration. */ +#define BLE_HIDS_CONFIG_SEC_MODE_DEFAULT_KEYBOARD \ + { \ + .hid_info.read = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, \ + .protocol_mode_char = { \ + .read = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, \ + .write = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, \ + }, \ + .ctrl_point_char = { \ + .write = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, \ + }, \ + .boot_kb_inp_rep_char = { \ + .read = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, \ + .write = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, \ + .cccd_write = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, \ + }, \ + .boot_kb_outp_rep_char = { \ + .read = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, \ + .write = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, \ + .cccd_write = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, \ + }, \ + } + /** * @brief HID boot keyboard input report maximum size, in bytes. */ @@ -312,22 +353,6 @@ struct ble_hids_evt { */ typedef void (*ble_hids_evt_handler_t)(struct ble_hids *hids, const struct ble_hids_evt *evt); -/** @brief Security requirements for HID Service characteristic. */ -struct ble_hids_char_sec { - /** - * @brief Security requirement for reading HID Service characteristic value. - */ - ble_gap_conn_sec_mode_t read; - /** - * @brief Security requirement for writing HID Service characteristic value. - */ - ble_gap_conn_sec_mode_t write; - /** - * @brief Security requirement for writing HID Service characteristic CCCD. - */ - ble_gap_conn_sec_mode_t cccd_write; -}; - /** @brief HID Report configuration. */ struct ble_hids_report_config { /** @@ -345,7 +370,20 @@ struct ble_hids_report_config { /** * @brief Security requirements for HID Service Input Report characteristic. */ - struct ble_hids_char_sec sec; + struct { + /** + * @brief Security requirement for reading HID Service characteristic value. + */ + ble_gap_conn_sec_mode_t read; + /** + * @brief Security requirement for writing HID Service characteristic value. + */ + ble_gap_conn_sec_mode_t write; + /** + * @brief Security requirement for writing HID Service characteristic CCCD. + */ + ble_gap_conn_sec_mode_t cccd_write; + } sec; }; /** @@ -369,10 +407,6 @@ struct ble_hids_rep_map_config { * @brief Optional External Report Reference descriptor (will be added if != NULL). */ ble_uuid_t const *ext_rep_ref; - /** - * @brief Security requirement for HID Service Report Map characteristic. - */ - struct ble_hids_char_sec sec; }; /** @brief HID Report characteristic structure. */ @@ -452,10 +486,6 @@ struct ble_hids_config { */ uint8_t reserved: 6; } flags; - /** - * @brief Security requirement for reading HID Information characteristic value. - */ - ble_gap_conn_sec_mode_t rd_sec; } hid_information; /** * @brief Number of services to include in HID service. @@ -466,29 +496,114 @@ struct ble_hids_config { */ uint16_t *included_services_array; /** - * @brief Security requirement for HID service Protocol Mode characteristic. - * - * @note Only read and write are used. - */ - struct ble_hids_char_sec protocol_mode_sec; - /** - * @brief Security requirement for HID service Control Point characteristic. - * - * @note Only write is used. - */ - struct ble_hids_char_sec ctrl_point_sec; - /** - * @brief Security requirements for HID Boot Keyboard Input Report characteristic. - */ - struct ble_hids_char_sec boot_mouse_inp_rep_sec; - /** - * @brief Security requirements for HID Boot Keyboard Input Report characteristic. - */ - struct ble_hids_char_sec boot_kb_inp_rep_sec; - /** - * @brief Security requirements for HID Boot Keyboard Output Report characteristic. + * @brief Security configuration. */ - struct ble_hids_char_sec boot_kb_outp_rep_sec; + struct { + /** + * @brief Security requirement for HID information characteristic. + */ + struct { + /** + * @brief Security requirement for reading HID Information characteristic + * value. + */ + ble_gap_conn_sec_mode_t read; + } hid_info; + /** + * @brief Security requirement for HID service Protocol Mode characteristic. + */ + struct { + /** + * @brief Security requirement for reading HID Service Protocol Mode + * characteristic value. + */ + ble_gap_conn_sec_mode_t read; + /** + * @brief Security requirement for writing HID Service Protocol Mode + * characteristic value. + */ + ble_gap_conn_sec_mode_t write; + } protocol_mode_char; + /** + * @brief Security requirement for HID service Control Point characteristic. + */ + struct { + /** + * @brief Security requirement for writing HID service Control Point + * characteristic value. + */ + ble_gap_conn_sec_mode_t write; + } ctrl_point_char; + /** + * @brief Security requirement for HID service Report Map characteristic. + */ + struct { + /** + * @brief Security requirement for reading HID Service characteristic value. + */ + ble_gap_conn_sec_mode_t read; + } report_map_char; + /** + * @brief Security requirements for HID Boot Keyboard Input Report characteristic. + */ + struct { + /** + * @brief Security requirement for reading HID Boot Mouse Input Report + * characteristic value. + */ + ble_gap_conn_sec_mode_t read; + /** + * @brief Security requirement for writing HID Boot Mouse Input Report + * characteristic value. + */ + ble_gap_conn_sec_mode_t write; + /** + * @brief Security requirement for writing HID Boot Mouse Input Report + * characteristic CCCD. + */ + ble_gap_conn_sec_mode_t cccd_write; + } boot_mouse_inp_rep_char; + /** + * @brief Security requirements for HID Boot Keyboard Input Report characteristic. + */ + struct { + /** + * @brief Security requirement for reading HID Boot Keyboard Input Report + * characteristic value. + */ + ble_gap_conn_sec_mode_t read; + /** + * @brief Security requirement for writing HID Boot Keyboard Input Report + * characteristic value. + */ + ble_gap_conn_sec_mode_t write; + /** + * @brief Security requirement for writing HID Boot Keyboard Input Report + * characteristic CCCD. + */ + ble_gap_conn_sec_mode_t cccd_write; + } boot_kb_inp_rep_char; + /** + * @brief Security requirements for HID Boot Keyboard Output Report characteristic. + */ + struct { + /** + * @brief Security requirement for reading HID Boot Keyboard Output Report + * characteristic value. + */ + ble_gap_conn_sec_mode_t read; + /** + * @brief Security requirement for writing HID Boot Keyboard Output Report + * characteristic value. + */ + ble_gap_conn_sec_mode_t write; + /** + * @brief Security requirement for writing HID Boot Keyboard Output Report + * characteristic CCCD. + */ + ble_gap_conn_sec_mode_t cccd_write; + } boot_kb_outp_rep_char; + } sec_mode; }; /** @brief HID Service structure. This contains various status information for the service. */ diff --git a/include/bm/bluetooth/services/ble_hrs.h b/include/bm/bluetooth/services/ble_hrs.h index 123bd2fa98..c1efc6716c 100644 --- a/include/bm/bluetooth/services/ble_hrs.h +++ b/include/bm/bluetooth/services/ble_hrs.h @@ -32,6 +32,15 @@ extern "C" { extern void ble_hrs_on_ble_evt(const ble_evt_t *ble_evt, void *ctx); \ NRF_SDH_BLE_OBSERVER(_name##_obs, ble_hrs_on_ble_evt, &_name, HIGH) +/** @brief Default security configuration. */ +#define BLE_HRS_CONFIG_SEC_MODE_DEFAULT \ + { \ + .hrm_char = { \ + .cccd_write = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, \ + }, \ + .bsl_char.read = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, \ + } + /** * @defgroup BLE_HRS_BODY_SENSOR_LOCATION HRS Body sensor location * @{ @@ -94,13 +103,30 @@ struct ble_hrs_config { */ uint8_t *body_sensor_location; /** - * @brief Security requirement for writing the heart rate monitor characteristic CCCD. - */ - ble_gap_conn_sec_mode_t hrm_cccd_wr_sec; - /** - * @brief Security requirement for reading the body sensor location characteristic value. + * @brief Security configuration. */ - ble_gap_conn_sec_mode_t bsl_rd_sec; + struct { + /** + * @brief Security requirement for the heart rate monitor characteristic. + */ + struct { + /** + * @brief Security requirement for writing the heart rate monitor + * characteristic CCCD. + */ + ble_gap_conn_sec_mode_t cccd_write; + } hrm_char; + /** + * @brief Security requirement for the body sensor location characteristic. + */ + struct { + /** + * @brief Security requirement for reading the body sensor location + * characteristic value. + */ + ble_gap_conn_sec_mode_t read; + } bsl_char; + } sec_mode; }; /** diff --git a/include/bm/bluetooth/services/ble_lbs.h b/include/bm/bluetooth/services/ble_lbs.h index 4decb8b7bb..b1f73296d8 100644 --- a/include/bm/bluetooth/services/ble_lbs.h +++ b/include/bm/bluetooth/services/ble_lbs.h @@ -14,6 +14,7 @@ #include #include +#include #include #ifdef __cplusplus @@ -39,6 +40,19 @@ struct ble_lbs; extern void ble_lbs_on_ble_evt(const ble_evt_t *ble_evt, void *lbs_instance); \ NRF_SDH_BLE_OBSERVER(_name ## _obs, ble_lbs_on_ble_evt, &_name, HIGH) +/** @brief Default security configuration. */ +#define BLE_LBS_CONFIG_SEC_MODE_DEFAULT \ + { \ + .lbs_button_char = { \ + .read = BLE_GAP_CONN_SEC_MODE_OPEN, \ + .cccd_write = BLE_GAP_CONN_SEC_MODE_OPEN, \ + }, \ + .lbs_led_char = { \ + .read = BLE_GAP_CONN_SEC_MODE_OPEN, \ + .write = BLE_GAP_CONN_SEC_MODE_OPEN, \ + }, \ + } + enum ble_lbs_evt_type { BLE_LBS_EVT_LED_WRITE, }; @@ -66,6 +80,22 @@ typedef void (*lbs_evt_handler_t)(struct ble_lbs *lbs, const struct ble_lbs_evt struct ble_lbs_config { /** @brief Event handler to be called when the LED Characteristic is written. */ lbs_evt_handler_t evt_handler; + /** Security configuration. */ + struct { + /** LBS Button characteristic */ + struct { + /** Security requirement for reading LBS button characteristic value. */ + ble_gap_conn_sec_mode_t read; + /** Security requirement for writing LBS button characteristic CCCD. */ + ble_gap_conn_sec_mode_t cccd_write; + } lbs_button_char; + struct { + /** Security requirement for reading LBS LED characteristic value. */ + ble_gap_conn_sec_mode_t read; + /** Security requirement for wtiring LBS LED characteristic value. */ + ble_gap_conn_sec_mode_t write; + } lbs_led_char; + } sec_mode; }; /** diff --git a/include/bm/bluetooth/services/ble_mcumgr.h b/include/bm/bluetooth/services/ble_mcumgr.h index 56f0698cb4..098506f1bc 100644 --- a/include/bm/bluetooth/services/ble_mcumgr.h +++ b/include/bm/bluetooth/services/ble_mcumgr.h @@ -17,6 +17,7 @@ #include #include #include +#include #include #ifdef __cplusplus @@ -34,13 +35,41 @@ extern "C" { #define BLE_MCUMGR_SERVICE_UUID_SUB 0xdc1d #define BLE_MCUMGR_CHARACTERISTIC_UUID_SUB 0x7828 +/** @brief Default security configuration. */ +#define BLE_MCUMGR_CONFIG_SEC_MODE_DEFAULT \ + { \ + .mcumgr_char = { \ + .read = BLE_GAP_CONN_SEC_MODE_OPEN, \ + .write = BLE_GAP_CONN_SEC_MODE_OPEN, \ + .cccd_write = BLE_GAP_CONN_SEC_MODE_OPEN, \ + }, \ + } + +/** + * @brief MCUmgr service configuration. + */ +struct ble_mcumgr_config { + /** Security configuration. */ + struct { + /** MCUmgr characteristic */ + struct { + /** Security requirement for reading MCUmgr characteristic value. */ + ble_gap_conn_sec_mode_t read; + /** Security requirement for writing MCUmgr characteristic value. */ + ble_gap_conn_sec_mode_t write; + /** Security requirement for writing MCUmgr characteristic CCCD. */ + ble_gap_conn_sec_mode_t cccd_write; + } mcumgr_char; + } sec_mode; +}; + /** * @brief Function for initializing the MCUmgr Bluetooth service. * * @retval NRF_SUCCESS On success. * @retval NRF_ERROR_INVALID_PARAM Invalid parameters. */ -uint32_t ble_mcumgr_init(void); +uint32_t ble_mcumgr_init(struct ble_mcumgr_config *cfg); /** * @brief Function for getting the MCUmgr Bluetooth service UUID type. diff --git a/include/bm/bluetooth/services/ble_nus.h b/include/bm/bluetooth/services/ble_nus.h index 6d6b481927..fa9dc52027 100644 --- a/include/bm/bluetooth/services/ble_nus.h +++ b/include/bm/bluetooth/services/ble_nus.h @@ -17,6 +17,7 @@ #include #include #include +#include #include #ifdef __cplusplus @@ -50,6 +51,19 @@ void ble_nus_on_ble_evt(ble_evt_t const *ble_evt, void *context); &_name, \ HIGH) +/** @brief Default security configuration. */ +#define BLE_NUS_CONFIG_SEC_MODE_DEFAULT \ + { \ + .nus_rx_char = { \ + .read = BLE_GAP_CONN_SEC_MODE_OPEN, \ + .write = BLE_GAP_CONN_SEC_MODE_OPEN, \ + }, \ + .nus_tx_char = { \ + .read = BLE_GAP_CONN_SEC_MODE_OPEN, \ + .cccd_write = BLE_GAP_CONN_SEC_MODE_OPEN, \ + }, \ + } + #define OPCODE_LENGTH 1 #define HANDLE_LENGTH 2 @@ -132,6 +146,23 @@ typedef void (*ble_nus_evt_handler_t) (const struct ble_nus_evt *evt); struct ble_nus_config { /** Event handler to be called for handling received data. */ ble_nus_evt_handler_t evt_handler; + /** Security configuration. */ + struct { + /** NUS Service RX characteristic */ + struct { + /** Security requirement for reading NUS rx characteristic value. */ + ble_gap_conn_sec_mode_t read; + /** Security requirement for writing NUS rx characteristic value. */ + ble_gap_conn_sec_mode_t write; + } nus_rx_char; + /** NUS Service TX characteristic */ + struct { + /** Security requirement for reading NUS TX characteristic value. */ + ble_gap_conn_sec_mode_t read; + /** Security requirement for writing NUS TX characteristic CCCD. */ + ble_gap_conn_sec_mode_t cccd_write; + } nus_tx_char; + } sec_mode; }; /** diff --git a/samples/bluetooth/ble_cgms/src/main.c b/samples/bluetooth/ble_cgms/src/main.c index 748e31f629..615f7a3dd5 100644 --- a/samples/bluetooth/ble_cgms/src/main.c +++ b/samples/bluetooth/ble_cgms/src/main.c @@ -299,6 +299,7 @@ uint16_t qwr_evt_handler(struct ble_qwr *qwr, const struct ble_qwr_evt *evt) static uint32_t services_init(void) { uint32_t nrf_err; + struct ble_cgms_config cgms_config = { .evt_handler = cgms_evt_handler, .gatt_queue = &ble_gatt_gueue, @@ -312,24 +313,26 @@ static uint32_t services_init(void) .type = BLE_CGMS_MEAS_TYPE_VEN_BLOOD, .sample_location = BLE_CGMS_MEAS_LOC_AST, }, + .sec_mode = BLE_CGMS_CONFIG_SEC_MODE_KCONFIG, }; struct ble_bas_config bas_config = { .evt_handler = NULL, .can_notify = true, .report_ref = NULL, .battery_level = 100, + .sec_mode = BLE_BAS_CONFIG_SEC_MODE_DEFAULT, }; - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&bas_config.batt_rd_sec); - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&bas_config.report_ref_rd_sec); - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&bas_config.cccd_wr_sec); - struct ble_qwr_config qwr_config = { .mem_buffer.len = CONFIG_APP_QWR_MEM_BUFF_SIZE, .mem_buffer.p_mem = qwr_mem, .evt_handler = qwr_evt_handler, }; + struct ble_dis_config dis_config = { + .sec_mode = BLE_DIS_CONFIG_SEC_MODE_DEFAULT, + }; + nrf_err = ble_qwr_init(&ble_qwr, &qwr_config); if (nrf_err) { LOG_ERR("Failed to initialize QWR service, nrf_error %#x", nrf_err); @@ -356,7 +359,7 @@ static uint32_t services_init(void) } /* Initialize Device Information Service. */ - nrf_err = ble_dis_init(); + nrf_err = ble_dis_init(&dis_config); if (nrf_err) { LOG_ERR("Failed to initialize DIS service, nrf_error %#x", nrf_err); return nrf_err; diff --git a/samples/bluetooth/ble_hids_keyboard/src/main.c b/samples/bluetooth/ble_hids_keyboard/src/main.c index 801c2d72f6..576c122779 100644 --- a/samples/bluetooth/ble_hids_keyboard/src/main.c +++ b/samples/bluetooth/ble_hids_keyboard/src/main.c @@ -489,27 +489,14 @@ static uint32_t hids_init(void) .remote_wake = 1, .normally_connectable = 1, }, - .rd_sec = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, }, .report_map = { .data = report_map_data, .len = sizeof(report_map_data), - .sec.read = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, }, .included_services_count = 0, .included_services_array = NULL, - .boot_kb_inp_rep_sec = { - .cccd_write = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, - .read = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, - }, - .boot_kb_outp_rep_sec = { - .read = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, - .write = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, - }, - - .protocol_mode_sec.read = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, - .protocol_mode_sec.write = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, - .ctrl_point_sec.write = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, + .sec_mode = BLE_HIDS_CONFIG_SEC_MODE_DEFAULT_KEYBOARD, }; return ble_hids_init(&ble_hids, &hids_config); @@ -859,9 +846,11 @@ int main(void) .can_notify = true, .report_ref = NULL, .battery_level = 100, - .batt_rd_sec = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, - .report_ref_rd_sec = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, - .cccd_wr_sec = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, + .sec_mode = BLE_BAS_CONFIG_SEC_MODE_DEFAULT, + }; + + struct ble_dis_config dis_config = { + .sec_mode = BLE_DIS_CONFIG_SEC_MODE_DEFAULT, }; struct ble_qwr_config qwr_config = { @@ -956,7 +945,7 @@ int main(void) goto idle; } - nrf_err = ble_dis_init(); + nrf_err = ble_dis_init(&dis_config); if (nrf_err) { LOG_ERR("Failed to initialize device information service, nrf_error %#x", nrf_err); goto idle; diff --git a/samples/bluetooth/ble_hids_mouse/src/main.c b/samples/bluetooth/ble_hids_mouse/src/main.c index 6bc477aad2..dc2e5c2b6d 100644 --- a/samples/bluetooth/ble_hids_mouse/src/main.c +++ b/samples/bluetooth/ble_hids_mouse/src/main.c @@ -405,24 +405,14 @@ static uint32_t hids_init(void) .remote_wake = 1, .normally_connectable = 1, }, - .rd_sec = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, }, .report_map = { .data = report_map_data, .len = sizeof(report_map_data), - .sec.read = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, }, .included_services_count = 0, .included_services_array = NULL, - .boot_mouse_inp_rep_sec = { - .cccd_write = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, - .read = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, - .write = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, - }, - - .protocol_mode_sec.read = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, - .protocol_mode_sec.write = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, - .ctrl_point_sec.write = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, + .sec_mode = BLE_HIDS_CONFIG_SEC_MODE_DEFAULT_MOUSE, }; return ble_hids_init(&ble_hids, &hids_config); @@ -709,9 +699,11 @@ int main(void) .can_notify = true, .report_ref = NULL, .battery_level = 100, - .batt_rd_sec = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, - .report_ref_rd_sec = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, - .cccd_wr_sec = BLE_GAP_CONN_SEC_MODE_ENC_NO_MITM, + .sec_mode = BLE_BAS_CONFIG_SEC_MODE_DEFAULT, + }; + + struct ble_dis_config dis_config = { + .sec_mode = BLE_DIS_CONFIG_SEC_MODE_DEFAULT, }; struct ble_qwr_config qwr_config = { @@ -805,7 +797,7 @@ int main(void) goto idle; } - nrf_err = ble_dis_init(); + nrf_err = ble_dis_init(&dis_config); if (nrf_err) { LOG_ERR("Failed to initialize device information service, nrf_error %#x", nrf_err); goto idle; diff --git a/samples/bluetooth/ble_hrs/src/main.c b/samples/bluetooth/ble_hrs/src/main.c index f30c85d0e1..1c63c10e80 100644 --- a/samples/bluetooth/ble_hrs/src/main.c +++ b/samples/bluetooth/ble_hrs/src/main.c @@ -465,19 +465,18 @@ int main(void) .evt_handler = ble_bas_evt_handler, .can_notify = true, .battery_level = CONFIG_APP_BATTERY_LEVEL_MAX, + .sec_mode = BLE_BAS_CONFIG_SEC_MODE_DEFAULT, + }; + struct ble_dis_config dis_config = { + .sec_mode = BLE_DIS_CONFIG_SEC_MODE_DEFAULT, }; struct ble_hrs_config hrs_cfg = { .evt_handler = ble_hrs_evt_handler, .is_sensor_contact_supported = true, .body_sensor_location = &body_sensor_location, + .sec_mode = BLE_HRS_CONFIG_SEC_MODE_DEFAULT, }; - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&bas_cfg.batt_rd_sec); - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&bas_cfg.cccd_wr_sec); - - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&hrs_cfg.hrm_cccd_wr_sec); - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&hrs_cfg.bsl_rd_sec); - LOG_INF("BLE HRS sample started"); simulated_meas_init(); @@ -518,7 +517,7 @@ int main(void) goto idle; } - nrf_err = ble_dis_init(); + nrf_err = ble_dis_init(&dis_config); if (nrf_err) { LOG_ERR("Failed to initialize device information service, nrf_error %#x", nrf_err); goto idle; diff --git a/samples/bluetooth/ble_lbs/src/main.c b/samples/bluetooth/ble_lbs/src/main.c index b72ff9b890..e2d64beaa9 100644 --- a/samples/bluetooth/ble_lbs/src/main.c +++ b/samples/bluetooth/ble_lbs/src/main.c @@ -139,6 +139,10 @@ int main(void) }; struct ble_lbs_config lbs_cfg = { .evt_handler = lbs_evt_handler, + .sec_mode = BLE_LBS_CONFIG_SEC_MODE_DEFAULT, + }; + struct ble_dis_config dis_config = { + .sec_mode = BLE_DIS_CONFIG_SEC_MODE_DEFAULT, }; LOG_INF("BLE LBS sample started"); @@ -187,7 +191,7 @@ int main(void) goto idle; } - nrf_err = ble_dis_init(); + nrf_err = ble_dis_init(&dis_config); if (nrf_err) { LOG_ERR("Failed to initialize device information service, nrf_error %#x", nrf_err); goto idle; diff --git a/samples/bluetooth/ble_nus/src/main.c b/samples/bluetooth/ble_nus/src/main.c index 554480fdfd..7665042c70 100644 --- a/samples/bluetooth/ble_nus/src/main.c +++ b/samples/bluetooth/ble_nus/src/main.c @@ -454,6 +454,7 @@ int main(void) struct ble_nus_config nus_cfg = { .evt_handler = ble_nus_evt_handler, + .sec_mode = BLE_NUS_CONFIG_SEC_MODE_DEFAULT, }; struct ble_qwr_config qwr_config = { .evt_handler = ble_qwr_evt_handler, diff --git a/samples/boot/mcuboot_recovery_entry/src/main.c b/samples/boot/mcuboot_recovery_entry/src/main.c index f2cd04f96f..e5a8c08285 100644 --- a/samples/boot/mcuboot_recovery_entry/src/main.c +++ b/samples/boot/mcuboot_recovery_entry/src/main.c @@ -167,6 +167,9 @@ int main(void) .uuid = BLE_MCUMGR_SERVICE_UUID_SUB, }, }; + struct ble_mcumgr_config mcumgr_cfg = { + .sec_mode = BLE_MCUMGR_CONFIG_SEC_MODE_DEFAULT, + }; mgmt_callback_register(&os_mgmt_reboot_callback); @@ -186,7 +189,7 @@ int main(void) LOG_INF("Bluetooth enabled"); - nrf_err = ble_mcumgr_init(); + nrf_err = ble_mcumgr_init(&mcumgr_cfg); if (nrf_err) { LOG_ERR("Failed to initialize MCUmgr, nrf_error %#x", nrf_err); diff --git a/subsys/bluetooth/services/ble_bas/bas.c b/subsys/bluetooth/services/ble_bas/bas.c index e29dd6d358..d1cba3f894 100644 --- a/subsys/bluetooth/services/ble_bas/bas.c +++ b/subsys/bluetooth/services/ble_bas/bas.c @@ -21,7 +21,8 @@ static uint32_t battery_level_char_add(struct ble_bas *bas, const struct ble_bas }; ble_gatts_attr_md_t cccd_md = { .vloc = BLE_GATTS_VLOC_STACK, - .write_perm = cfg->cccd_wr_sec, + .write_perm = cfg->sec_mode.battery_lvl_char.cccd_write, + .read_perm = BLE_GAP_CONN_SEC_MODE_OPEN, }; ble_gatts_char_md_t char_md = { .char_props = { @@ -31,7 +32,7 @@ static uint32_t battery_level_char_add(struct ble_bas *bas, const struct ble_bas }; ble_gatts_attr_md_t attr_md = { .vloc = BLE_GATTS_VLOC_STACK, - .read_perm = cfg->batt_rd_sec, + .read_perm = cfg->sec_mode.battery_lvl_char.read, }; ble_gatts_attr_t attr_char_value = { .p_uuid = &char_uuid, @@ -41,8 +42,6 @@ static uint32_t battery_level_char_add(struct ble_bas *bas, const struct ble_bas .max_len = sizeof(bas->battery_level), }; - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm); - /* Set CCCD metadata if characteristic value can be notified. */ if (bas->can_notify) { char_md.p_cccd_md = &cccd_md; @@ -62,7 +61,7 @@ static uint32_t report_reference_descriptor_add(struct ble_bas *bas, }; ble_gatts_attr_md_t attr_md = { .vloc = BLE_GATTS_VLOC_STACK, - .read_perm = cfg->report_ref_rd_sec, + .read_perm = cfg->sec_mode.battery_report_ref.read, }; uint8_t encoded_report_ref[sizeof(uint16_t)]; ble_gatts_attr_t descr_params = { diff --git a/subsys/bluetooth/services/ble_cgms/cgms.c b/subsys/bluetooth/services/ble_cgms/cgms.c index 1896a88dcc..9ea28814e8 100644 --- a/subsys/bluetooth/services/ble_cgms/cgms.c +++ b/subsys/bluetooth/services/ble_cgms/cgms.c @@ -50,7 +50,7 @@ static uint8_t encode_feature_location_type(uint8_t *buf_out, struct ble_cgms_fe } /* Add the glucose feature characteristic. */ -static uint32_t feature_char_add(struct ble_cgms *cgms) +static uint32_t feature_char_add(struct ble_cgms *cgms, const struct ble_cgms_config *cgms_cfg) { uint8_t init_value_len; uint8_t encoded_initial_feature[BLE_CGMS_FEATURE_LEN]; @@ -68,6 +68,7 @@ static uint32_t feature_char_add(struct ble_cgms *cgms) }; ble_gatts_attr_md_t attr_md = { .vloc = BLE_GATTS_VLOC_STACK, + .read_perm = cgms_cfg->sec_mode.feature_char.read, }; ble_gatts_attr_t attr_char_value = { .p_uuid = &char_uuid, @@ -77,7 +78,6 @@ static uint32_t feature_char_add(struct ble_cgms *cgms) .max_len = init_value_len, }; - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm); BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&attr_md.write_perm); return sd_ble_gatts_characteristic_add(cgms->service_handle, &char_md, &attr_char_value, @@ -99,7 +99,7 @@ static uint8_t encode_status(uint8_t *buf_out, struct ble_cgms *cgms) } /* Add the CGMS status characteristic. */ -static uint32_t status_char_add(struct ble_cgms *cgms) +static uint32_t status_char_add(struct ble_cgms *cgms, const struct ble_cgms_config *cgms_cfg) { uint8_t init_value_len; uint8_t encoded_initial_status[BLE_CGMS_STATUS_LEN]; @@ -118,6 +118,7 @@ static uint32_t status_char_add(struct ble_cgms *cgms) ble_gatts_attr_md_t attr_md = { .vloc = BLE_GATTS_VLOC_STACK, .vlen = true, + .read_perm = cgms_cfg->sec_mode.status_char.read, }; ble_gatts_attr_t attr_char_value = { .p_uuid = &char_uuid, @@ -127,7 +128,6 @@ static uint32_t status_char_add(struct ble_cgms *cgms) .max_len = init_value_len, }; - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm); BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&attr_md.write_perm); /* Add Nordic UART Service characteristic RX declaration. */ @@ -136,7 +136,7 @@ static uint32_t status_char_add(struct ble_cgms *cgms) } /* Add the Session Run Time characteristic. */ -static uint32_t srt_char_add(struct ble_cgms *cgms) +static uint32_t srt_char_add(struct ble_cgms *cgms, const struct ble_cgms_config *cgms_cfg) { uint8_t len = 0; uint8_t encoded_initial_srt[BLE_CGMS_SRT_LEN]; @@ -156,6 +156,7 @@ static uint32_t srt_char_add(struct ble_cgms *cgms) ble_gatts_attr_md_t attr_md = { .vloc = BLE_GATTS_VLOC_STACK, .vlen = true, + .read_perm = cgms_cfg->sec_mode.srt_char.read, }; ble_gatts_attr_t attr_char_value = { .p_uuid = &char_uuid, @@ -165,17 +166,15 @@ static uint32_t srt_char_add(struct ble_cgms *cgms) .max_len = BLE_CGMS_SRT_LEN, }; - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm); - BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&attr_md.write_perm); return sd_ble_gatts_characteristic_add(cgms->service_handle, &char_md, &attr_char_value, &cgms->char_handles.srt); } -uint32_t ble_cgms_init(struct ble_cgms *cgms, const struct ble_cgms_config *cgms_init) +uint32_t ble_cgms_init(struct ble_cgms *cgms, const struct ble_cgms_config *cgms_cfg) { - if (!cgms || !cgms_init || !cgms_init->evt_handler || !cgms_init->gatt_queue) { + if (!cgms || !cgms_cfg || !cgms_cfg->evt_handler || !cgms_cfg->gatt_queue) { return NRF_ERROR_NULL; } @@ -194,11 +193,11 @@ uint32_t ble_cgms_init(struct ble_cgms *cgms, const struct ble_cgms_config *cgms } /* Initialize service structure. */ - cgms->evt_handler = cgms_init->evt_handler; - cgms->gatt_queue = cgms_init->gatt_queue; - cgms->feature = cgms_init->feature; - cgms->sensor_status = cgms_init->initial_sensor_status; - cgms->session_run_time = cgms_init->initial_run_time; + cgms->evt_handler = cgms_cfg->evt_handler; + cgms->gatt_queue = cgms_cfg->gatt_queue; + cgms->feature = cgms_cfg->feature; + cgms->sensor_status = cgms_cfg->initial_sensor_status; + cgms->session_run_time = cgms_cfg->initial_run_time; cgms->is_session_started = false; cgms->nb_run_session = 0; cgms->conn_handle = BLE_CONN_HANDLE_INVALID; @@ -216,49 +215,49 @@ uint32_t ble_cgms_init(struct ble_cgms *cgms, const struct ble_cgms_config *cgms } /* Add CGM Measurement characteristic. */ - nrf_err = cgms_meas_char_add(cgms); + nrf_err = cgms_meas_char_add(cgms, cgms_cfg); if (nrf_err) { LOG_ERR("Failed to add CGMS measurement characteristic, nrf_error %#x", nrf_err); return nrf_err; } /* Add CGM Feature characteristic. */ - nrf_err = feature_char_add(cgms); + nrf_err = feature_char_add(cgms, cgms_cfg); if (nrf_err) { LOG_ERR("Failed to add CGMS feature characteristic, nrf_error %#x", nrf_err); return nrf_err; } /* Add CGM Status characteristic. */ - nrf_err = status_char_add(cgms); + nrf_err = status_char_add(cgms, cgms_cfg); if (nrf_err) { LOG_ERR("Failed to add CGMS status characteristic, nrf_error %#x", nrf_err); return nrf_err; } /* Add CGM Session Start Time characteristic. */ - nrf_err = cgms_sst_char_add(cgms); + nrf_err = cgms_sst_char_add(cgms, cgms_cfg); if (nrf_err) { LOG_ERR("Failed to add CGMS SST characteristic, nrf_error %#x", nrf_err); return nrf_err; } /* Add CGM Session Run Time characteristic. */ - nrf_err = srt_char_add(cgms); + nrf_err = srt_char_add(cgms, cgms_cfg); if (nrf_err) { LOG_ERR("Failed to add CGMS SRT characteristic, nrf_error %#x", nrf_err); return nrf_err; } /* Add CGM Record Access Control Point characteristic. */ - nrf_err = cgms_racp_char_add(cgms); + nrf_err = cgms_racp_char_add(cgms, cgms_cfg); if (nrf_err) { LOG_ERR("Failed to add CGMS RACP characteristic, nrf_error %#x", nrf_err); return nrf_err; } /* Add CGM Specific Ops Control Point characteristic. */ - nrf_err = cgms_socp_char_add(cgms); + nrf_err = cgms_socp_char_add(cgms, cgms_cfg); if (nrf_err) { LOG_ERR("Failed to add CGMS SOCP characteristic, nrf_error %#x", nrf_err); return nrf_err; diff --git a/subsys/bluetooth/services/ble_cgms/cgms_meas.c b/subsys/bluetooth/services/ble_cgms/cgms_meas.c index 5daa86a71b..023eda2062 100644 --- a/subsys/bluetooth/services/ble_cgms/cgms_meas.c +++ b/subsys/bluetooth/services/ble_cgms/cgms_meas.c @@ -67,7 +67,7 @@ static uint8_t cgms_meas_encode(struct ble_cgms *cgms, } /* Add a characteristic for the Continuous Glucose Meter Measurement. */ -uint32_t cgms_meas_char_add(struct ble_cgms *cgms) +uint32_t cgms_meas_char_add(struct ble_cgms *cgms, const struct ble_cgms_config *cgms_cfg) { uint32_t nrf_err; uint16_t num_recs; @@ -89,7 +89,9 @@ uint32_t cgms_meas_char_add(struct ble_cgms *cgms) .uuid = BLE_UUID_CGM_MEASUREMENT, }; ble_gatts_attr_md_t cccd_md = { - .vloc = BLE_GATTS_VLOC_STACK + .vloc = BLE_GATTS_VLOC_STACK, + .read_perm = BLE_GAP_CONN_SEC_MODE_OPEN, + .write_perm = cgms_cfg->sec_mode.meas_char.cccd_write, }; ble_gatts_char_md_t char_md = { .char_props = { @@ -109,9 +111,6 @@ uint32_t cgms_meas_char_add(struct ble_cgms *cgms) .max_len = BLE_CGMS_MEAS_LEN_MAX, }; - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm); - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm); - return sd_ble_gatts_characteristic_add(cgms->service_handle, &char_md, &attr_char_value, &cgms->char_handles.measurement); } diff --git a/subsys/bluetooth/services/ble_cgms/cgms_meas.h b/subsys/bluetooth/services/ble_cgms/cgms_meas.h index 13ae738727..cfc17c2f65 100644 --- a/subsys/bluetooth/services/ble_cgms/cgms_meas.h +++ b/subsys/bluetooth/services/ble_cgms/cgms_meas.h @@ -35,7 +35,7 @@ extern "C" { * @return If functions from other modules return errors to this function, * the @ref nrf_error are propagated. */ -uint32_t cgms_meas_char_add(struct ble_cgms *cgms); +uint32_t cgms_meas_char_add(struct ble_cgms *cgms, const struct ble_cgms_config *cgms_cfg); /** * @brief Send a CGM Measurement. diff --git a/subsys/bluetooth/services/ble_cgms/cgms_racp.c b/subsys/bluetooth/services/ble_cgms/cgms_racp.c index 29f9235055..63d510f146 100644 --- a/subsys/bluetooth/services/ble_cgms/cgms_racp.c +++ b/subsys/bluetooth/services/ble_cgms/cgms_racp.c @@ -29,14 +29,16 @@ LOG_MODULE_DECLARE(ble_cgms, CONFIG_BLE_CGMS_LOG_LEVEL); + OPERAND_LESS_GREATER_FILTER_PARAM_SIZE /* Add the Record Access Control Point characteristic */ -uint32_t cgms_racp_char_add(struct ble_cgms *cgms) +uint32_t cgms_racp_char_add(struct ble_cgms *cgms, const struct ble_cgms_config *cgms_cfg) { ble_uuid_t char_uuid = { .type = BLE_UUID_TYPE_BLE, .uuid = BLE_UUID_RECORD_ACCESS_CONTROL_POINT_CHAR, }; ble_gatts_attr_md_t cccd_md = { - .vloc = BLE_GATTS_VLOC_STACK + .vloc = BLE_GATTS_VLOC_STACK, + .read_perm = BLE_GAP_CONN_SEC_MODE_OPEN, + .write_perm = cgms_cfg->sec_mode.racp_char.cccd_write, }; ble_gatts_char_md_t char_md = { .char_props = { @@ -49,6 +51,7 @@ uint32_t cgms_racp_char_add(struct ble_cgms *cgms) .vloc = BLE_GATTS_VLOC_STACK, .wr_auth = true, .vlen = true, + .write_perm = cgms_cfg->sec_mode.racp_char.write, }; ble_gatts_attr_t attr_char_value = { .p_uuid = &char_uuid, @@ -58,11 +61,6 @@ uint32_t cgms_racp_char_add(struct ble_cgms *cgms) .max_len = BLE_GATT_ATT_MTU_DEFAULT, }; - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm); - - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm); - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm); - return sd_ble_gatts_characteristic_add(cgms->service_handle, &char_md, &attr_char_value, &cgms->char_handles.racp); } diff --git a/subsys/bluetooth/services/ble_cgms/cgms_racp.h b/subsys/bluetooth/services/ble_cgms/cgms_racp.h index 05b4c986b1..55a8207f62 100644 --- a/subsys/bluetooth/services/ble_cgms/cgms_racp.h +++ b/subsys/bluetooth/services/ble_cgms/cgms_racp.h @@ -36,7 +36,7 @@ extern "C" { * @return If functions from other modules return errors to this function, * the @ref nrf_error are propagated. */ -uint32_t cgms_racp_char_add(struct ble_cgms *cgms); +uint32_t cgms_racp_char_add(struct ble_cgms *cgms, const struct ble_cgms_config *cgms_cfg); /** * @brief Function for handling @ref BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST events. diff --git a/subsys/bluetooth/services/ble_cgms/cgms_socp.c b/subsys/bluetooth/services/ble_cgms/cgms_socp.c index c6747bd173..fb725bf0ea 100644 --- a/subsys/bluetooth/services/ble_cgms/cgms_socp.c +++ b/subsys/bluetooth/services/ble_cgms/cgms_socp.c @@ -303,14 +303,16 @@ static uint8_t ble_socp_encode(const struct ble_socp_rsp *socp_rsp, uint8_t *dat } /* Add the Specific Ops Control Point characteristic. */ -uint32_t cgms_socp_char_add(struct ble_cgms *cgms) +uint32_t cgms_socp_char_add(struct ble_cgms *cgms, const struct ble_cgms_config *cgms_cfg) { ble_uuid_t char_uuid = { .type = BLE_UUID_TYPE_BLE, .uuid = BLE_UUID_CGM_SPECIFIC_OPS_CTRLPT, }; ble_gatts_attr_md_t cccd_md = { - .vloc = BLE_GATTS_VLOC_STACK + .vloc = BLE_GATTS_VLOC_STACK, + .read_perm = BLE_GAP_CONN_SEC_MODE_OPEN, + .write_perm = cgms_cfg->sec_mode.socp_char.cccd_write, }; ble_gatts_char_md_t char_md = { .char_props = { @@ -323,6 +325,7 @@ uint32_t cgms_socp_char_add(struct ble_cgms *cgms) .vloc = BLE_GATTS_VLOC_STACK, .wr_auth = true, .vlen = true, + .write_perm = cgms_cfg->sec_mode.socp_char.write, }; ble_gatts_attr_t attr_char_value = { .p_uuid = &char_uuid, @@ -332,11 +335,6 @@ uint32_t cgms_socp_char_add(struct ble_cgms *cgms) .max_len = BLE_GATT_ATT_MTU_DEFAULT, }; - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm); - - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm); - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm); - return sd_ble_gatts_characteristic_add(cgms->service_handle, &char_md, &attr_char_value, &cgms->char_handles.socp); } diff --git a/subsys/bluetooth/services/ble_cgms/cgms_socp.h b/subsys/bluetooth/services/ble_cgms/cgms_socp.h index d877645cc4..3ed2e15fde 100644 --- a/subsys/bluetooth/services/ble_cgms/cgms_socp.h +++ b/subsys/bluetooth/services/ble_cgms/cgms_socp.h @@ -35,7 +35,7 @@ extern "C" { * @return If functions from other modules return errors to this function, * the @ref nrf_error is propagated. */ -uint32_t cgms_socp_char_add(struct ble_cgms *cgms); +uint32_t cgms_socp_char_add(struct ble_cgms *cgms, const struct ble_cgms_config *cgms_cfg); /** * @brief Function for handling @ref BLE_GATTS_EVT_RW_AUTHORIZE_REQUEST events. diff --git a/subsys/bluetooth/services/ble_cgms/cgms_sst.c b/subsys/bluetooth/services/ble_cgms/cgms_sst.c index 6abb97057d..3c40a3c8b3 100644 --- a/subsys/bluetooth/services/ble_cgms/cgms_sst.c +++ b/subsys/bluetooth/services/ble_cgms/cgms_sst.c @@ -142,7 +142,7 @@ static void on_sst_value_write(struct ble_cgms *cgms, const ble_gatts_evt_write_ } /* Add the Session Start Time characteristic. */ -uint32_t cgms_sst_char_add(struct ble_cgms *cgms) +uint32_t cgms_sst_char_add(struct ble_cgms *cgms, const struct ble_cgms_config *cgms_cfg) { uint8_t init_value[BLE_CGMS_SST_LEN] = {0}; @@ -159,6 +159,8 @@ uint32_t cgms_sst_char_add(struct ble_cgms *cgms) ble_gatts_attr_md_t attr_md = { .vloc = BLE_GATTS_VLOC_STACK, .wr_auth = true, + .read_perm = cgms_cfg->sec_mode.sst_char.read, + .write_perm = cgms_cfg->sec_mode.sst_char.write, }; ble_gatts_attr_t attr_char_value = { .p_uuid = &char_uuid, @@ -168,9 +170,6 @@ uint32_t cgms_sst_char_add(struct ble_cgms *cgms) .max_len = BLE_CGMS_SST_LEN, }; - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm); - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm); - return sd_ble_gatts_characteristic_add(cgms->service_handle, &char_md, &attr_char_value, &cgms->char_handles.sst); } diff --git a/subsys/bluetooth/services/ble_cgms/cgms_sst.h b/subsys/bluetooth/services/ble_cgms/cgms_sst.h index 30568cc8ac..68a393a2bf 100644 --- a/subsys/bluetooth/services/ble_cgms/cgms_sst.h +++ b/subsys/bluetooth/services/ble_cgms/cgms_sst.h @@ -47,7 +47,7 @@ struct ble_cgms_sst { * @return If functions from other modules return errors to this function, * the @ref nrf_error is propagated. */ -uint32_t cgms_sst_char_add(struct ble_cgms *cgms); +uint32_t cgms_sst_char_add(struct ble_cgms *cgms, const struct ble_cgms_config *cgms_cfg); /** * @brief Set the Session Start Time characteristic value. diff --git a/subsys/bluetooth/services/ble_dis/dis.c b/subsys/bluetooth/services/ble_dis/dis.c index 1c775f16ba..4082aa23b8 100644 --- a/subsys/bluetooth/services/ble_dis/dis.c +++ b/subsys/bluetooth/services/ble_dis/dis.c @@ -13,6 +13,7 @@ #include #include #include +#include #define SYS_ID_LEN 8 /* Length of System ID Characteristic Value */ #define PNP_ID_LEN 7 /* Length of PnP ID Characteristic Value */ @@ -58,11 +59,16 @@ static const uint8_t regulatory_certifications[IEEE_CERT_LEN] = LOG_MODULE_REGISTER(ble_dis, CONFIG_BLE_DIS_LOG_LEVEL); -uint32_t ble_dis_init(void) +uint32_t ble_dis_init(struct ble_dis_config *dis_config) { uint32_t nrf_err; uint16_t service_handle; ble_uuid_t ble_uuid; + + if (!dis_config) { + return NRF_ERROR_NULL; + } + ble_gatts_char_handles_t char_handles = {0}; ble_gatts_char_md_t char_md = { .char_props = { @@ -71,7 +77,7 @@ uint32_t ble_dis_init(void) }; ble_gatts_attr_md_t attr_md = { .vloc = BLE_GATTS_VLOC_STACK, - .read_perm = gap_conn_sec_mode_from_u8(CONFIG_BLE_DIS_CHAR_SEC_MODE), + .read_perm = dis_config->sec_mode.device_info_char.read, }; ble_gatts_attr_t attr_char_value = { .p_uuid = &ble_uuid, diff --git a/subsys/bluetooth/services/ble_hids/hids.c b/subsys/bluetooth/services/ble_hids/hids.c index 4008abe4d4..9fd1559e58 100644 --- a/subsys/bluetooth/services/ble_hids/hids.c +++ b/subsys/bluetooth/services/ble_hids/hids.c @@ -628,13 +628,13 @@ static uint32_t protocol_mode_char_add(struct ble_hids *hids, ble_gap_conn_sec_m &hids->protocol_mode_handles); } -static uint32_t rep_char_add(struct ble_hids *hids, ble_gatt_char_props_t *properties, uint16_t len, - uint8_t report_id, uint8_t report_type, - struct ble_hids_char_sec const *const char_sec, +static uint32_t rep_char_add(struct ble_hids *hids, ble_gatt_char_props_t *properties, + struct ble_hids_report_config const *rep_cfg, struct ble_hids_rep_char *rep_char) { uint32_t nrf_err; - uint8_t encoded_rep_ref[BLE_SRV_ENCODED_REPORT_REF_LEN] = { report_id, report_type }; + uint8_t encoded_rep_ref[BLE_SRV_ENCODED_REPORT_REF_LEN] = {rep_cfg->report_id, + rep_cfg->report_type }; ble_gatts_char_md_t char_md = { .char_props = *properties, @@ -652,16 +652,16 @@ static uint32_t rep_char_add(struct ble_hids *hids, ble_gatt_char_props_t *prope ble_gatts_attr_t attr_char_value = { .p_uuid = &char_uuid, .p_attr_md = &attr_md, - .max_len = len, + .max_len = rep_cfg->len, }; ble_gatts_attr_md_t cccd_md = {0}; - attr_md.read_perm = char_sec->read; - attr_md.write_perm = char_sec->write; + attr_md.read_perm = rep_cfg->sec.read; + attr_md.write_perm = rep_cfg->sec.write; if ((properties->notify == 1) || (properties->indicate == 1)) { - cccd_md.write_perm = char_sec->cccd_write; + cccd_md.write_perm = rep_cfg->sec.cccd_write; BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm); cccd_md.vloc = BLE_GATTS_VLOC_STACK; @@ -689,8 +689,8 @@ static uint32_t rep_char_add(struct ble_hids *hids, ble_gatt_char_props_t *prope .p_value = encoded_rep_ref, }; - desc_attr_md.read_perm = char_sec->read; - desc_attr_md.write_perm = char_sec->write; + desc_attr_md.read_perm = rep_cfg->sec.read; + desc_attr_md.write_perm = rep_cfg->sec.write; return sd_ble_gatts_descriptor_add(rep_char->char_handles.value_handle, &desc_params, &rep_char->ref_handle); @@ -711,7 +711,7 @@ static uint32_t rep_map_char_add(struct ble_hids *hids, const struct ble_hids_co ble_gatts_attr_md_t attr_md = { .vlen = 1, .vloc = BLE_GATTS_VLOC_STACK, - .read_perm = hids_cfg->report_map.sec.read, + .read_perm = hids_cfg->sec_mode.report_map_char.read, }; ble_gatts_attr_t attr_char_value = { .p_uuid = &char_uuid, @@ -747,7 +747,7 @@ static uint32_t rep_map_char_add(struct ble_hids *hids, const struct ble_hids_co }; ble_gatts_attr_md_t attr_md = { .vloc = BLE_GATTS_VLOC_STACK, - .read_perm = hids_cfg->report_map.sec.read, + .read_perm = hids_cfg->sec_mode.report_map_char.read, }; ble_gatts_attr_t descr_params = { .p_uuid = &desc_uuid, @@ -769,18 +769,20 @@ static uint32_t rep_map_char_add(struct ble_hids *hids, const struct ble_hids_co } static uint32_t boot_inp_rep_char_add(struct ble_hids *hids, uint16_t uuid, uint16_t max_data_len, - struct ble_hids_char_sec const *const char_sec, + ble_gap_conn_sec_mode_t sec_read, + ble_gap_conn_sec_mode_t sec_write, + ble_gap_conn_sec_mode_t sec_cccd_write, ble_gatts_char_handles_t *char_handles) { ble_gatts_attr_md_t cccd_md = { .vloc = BLE_GATTS_VLOC_STACK, - .write_perm = char_sec->cccd_write, + .write_perm = sec_cccd_write, .read_perm = BLE_GAP_CONN_SEC_MODE_OPEN, }; ble_gatts_char_md_t char_md = { .char_props = { .read = 1, - .write = (char_sec->write.sm && char_sec->write.lv) ? 1 : 0, + .write = (sec_write.sm && sec_write.lv) ? 1 : 0, .notify = 1, }, .p_cccd_md = &cccd_md @@ -792,8 +794,8 @@ static uint32_t boot_inp_rep_char_add(struct ble_hids *hids, uint16_t uuid, uint ble_gatts_attr_md_t attr_md = { .rd_auth = 1, .vloc = BLE_GATTS_VLOC_STACK, - .read_perm = char_sec->read, - .write_perm = char_sec->write, + .read_perm = sec_read, + .write_perm = sec_write, }; ble_gatts_attr_t attr_char_value = { .p_uuid = &char_uuid, @@ -823,6 +825,8 @@ static uint32_t boot_kb_outp_rep_char_add(struct ble_hids *hids, ble_gatts_attr_md_t attr_md = { .rd_auth = 1, .vloc = BLE_GATTS_VLOC_STACK, + .read_perm = hids_cfg->sec_mode.boot_kb_outp_rep_char.read, + .write_perm = hids_cfg->sec_mode.boot_kb_outp_rep_char.write, }; ble_gatts_attr_t attr_char_value = { .p_uuid = &char_uuid, @@ -830,9 +834,6 @@ static uint32_t boot_kb_outp_rep_char_add(struct ble_hids *hids, .max_len = BLE_HIDS_BOOT_KB_OUTPUT_REPORT_MAX_SIZE, }; - attr_md.read_perm = hids_cfg->boot_kb_outp_rep_sec.read; - attr_md.write_perm = hids_cfg->boot_kb_outp_rep_sec.write; - return sd_ble_gatts_characteristic_add(hids->service_handle, &char_md, &attr_char_value, &hids->boot_kb_outp_rep_handles); } @@ -862,7 +863,7 @@ static uint32_t hid_information_char_add(struct ble_hids *hids, }; ble_gatts_attr_md_t attr_md = { .vloc = BLE_GATTS_VLOC_STACK, - .read_perm = hids_cfg->hid_information.rd_sec, + .read_perm = hids_cfg->sec_mode.hid_info.read, }; ble_gatts_attr_t attr_char_value = { .p_uuid = &char_uuid, @@ -920,9 +921,7 @@ static uint32_t inp_rep_characteristics_add(struct ble_hids *hids, .notify = true, }; - nrf_err = rep_char_add(hids, &properties, rep_init->len, - rep_init->report_id, - rep_init->report_type, &rep_init->sec, + nrf_err = rep_char_add(hids, &properties, rep_init, &hids->inp_rep_array[i]); if (nrf_err) { return nrf_err; @@ -948,9 +947,7 @@ static uint32_t outp_rep_characteristics_add(struct ble_hids *hids, .write_wo_resp = true, }; - nrf_err = rep_char_add(hids, &properties, rep_init->len, - rep_init->report_id, - rep_init->report_type, &rep_init->sec, + nrf_err = rep_char_add(hids, &properties, rep_init, &hids->outp_rep_array[i]); if (nrf_err) { return nrf_err; @@ -976,10 +973,8 @@ static uint32_t feature_rep_characteristics_add(struct ble_hids *hids, .write = true, }; - nrf_err = rep_char_add(hids, &properties, rep_init->len, - rep_init->report_id, - rep_init->report_type, &rep_init->sec, - &hids->feature_rep_array[i]); + nrf_err = rep_char_add(hids, &properties, rep_init, + &hids->feature_rep_array[i]); if (nrf_err) { return nrf_err; } @@ -1045,8 +1040,8 @@ uint32_t ble_hids_init(struct ble_hids *hids, const struct ble_hids_config *hids #if defined(CONFIG_BLE_HIDS_BOOT_KEYBOARD) || defined(CONFIG_BLE_HIDS_BOOT_MOUSE) /* Add Protocol Mode characteristic. */ - nrf_err = protocol_mode_char_add(hids, hids_cfg->protocol_mode_sec.read, - hids_cfg->protocol_mode_sec.write); + nrf_err = protocol_mode_char_add(hids, hids_cfg->sec_mode.protocol_mode_char.read, + hids_cfg->sec_mode.protocol_mode_char.write); if (nrf_err) { return nrf_err; } @@ -1080,7 +1075,9 @@ uint32_t ble_hids_init(struct ble_hids *hids, const struct ble_hids_config *hids /* Add Boot Keyboard Input Report characteristic. */ nrf_err = boot_inp_rep_char_add(hids, BLE_UUID_BOOT_KEYBOARD_INPUT_REPORT_CHAR, BLE_HIDS_BOOT_KB_INPUT_REPORT_MAX_SIZE, - &hids_cfg->boot_kb_inp_rep_sec, + hids_cfg->sec_mode.boot_kb_inp_rep_char.read, + hids_cfg->sec_mode.boot_kb_inp_rep_char.write, + hids_cfg->sec_mode.boot_kb_inp_rep_char.cccd_write, &hids->boot_kb_inp_rep_handles); if (nrf_err) { return nrf_err; @@ -1098,7 +1095,10 @@ uint32_t ble_hids_init(struct ble_hids *hids, const struct ble_hids_config *hids nrf_err = boot_inp_rep_char_add( hids, BLE_UUID_BOOT_MOUSE_INPUT_REPORT_CHAR, BLE_HIDS_BOOT_MOUSE_INPUT_REPORT_MAX_SIZE, - &hids_cfg->boot_mouse_inp_rep_sec, &hids->boot_mouse_inp_rep_handles); + hids_cfg->sec_mode.boot_mouse_inp_rep_char.read, + hids_cfg->sec_mode.boot_mouse_inp_rep_char.write, + hids_cfg->sec_mode.boot_mouse_inp_rep_char.cccd_write, + &hids->boot_mouse_inp_rep_handles); if (nrf_err) { return nrf_err; } @@ -1111,12 +1111,7 @@ uint32_t ble_hids_init(struct ble_hids *hids, const struct ble_hids_config *hids } /* Add HID Control Point characteristic. */ - nrf_err = hid_control_point_char_add(hids, hids_cfg->ctrl_point_sec.write); - if (nrf_err) { - return nrf_err; - } - - return nrf_err; + return hid_control_point_char_add(hids, hids_cfg->sec_mode.ctrl_point_char.write); } uint32_t ble_hids_inp_rep_send(struct ble_hids *hids, uint16_t conn_handle, diff --git a/subsys/bluetooth/services/ble_hrs/hrs.c b/subsys/bluetooth/services/ble_hrs/hrs.c index 43b74a2733..a56b3c608e 100644 --- a/subsys/bluetooth/services/ble_hrs/hrs.c +++ b/subsys/bluetooth/services/ble_hrs/hrs.c @@ -95,7 +95,8 @@ static uint32_t heart_rate_measurement_char_add(struct ble_hrs *hrs, }; ble_gatts_attr_md_t cccd_md = { .vloc = BLE_GATTS_VLOC_STACK, - .write_perm = cfg->hrm_cccd_wr_sec, + .read_perm = BLE_GAP_CONN_SEC_MODE_OPEN, + .write_perm = cfg->sec_mode.hrm_char.cccd_write, }; ble_gatts_char_md_t char_md = { .char_props = { @@ -115,8 +116,6 @@ static uint32_t heart_rate_measurement_char_add(struct ble_hrs *hrs, .max_len = sizeof(encoded_initial_hrm), }; - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm); - /* Add Heart rate measurement characteristic declaration, value, and CCCD attributes. */ return sd_ble_gatts_characteristic_add(hrs->service_handle, &char_md, &attr_char_value, &hrs->hrm_handles); @@ -135,7 +134,7 @@ static uint32_t body_sensor_location_char_add(struct ble_hrs *hrs, const struct }; ble_gatts_attr_md_t attr_md = { .vloc = BLE_GATTS_VLOC_STACK, - .read_perm = cfg->bsl_rd_sec, + .read_perm = cfg->sec_mode.bsl_char.read, }; ble_gatts_attr_t attr_char_value = { .p_uuid = &char_uuid, diff --git a/subsys/bluetooth/services/ble_lbs/lbs.c b/subsys/bluetooth/services/ble_lbs/lbs.c index 6f7f14a177..468f1f559d 100644 --- a/subsys/bluetooth/services/ble_lbs/lbs.c +++ b/subsys/bluetooth/services/ble_lbs/lbs.c @@ -92,6 +92,8 @@ uint32_t ble_lbs_init(struct ble_lbs *lbs, const struct ble_lbs_config *cfg) }; ble_gatts_attr_md_t cccd_md = { .vloc = BLE_GATTS_VLOC_STACK, + .read_perm = BLE_GAP_CONN_SEC_MODE_OPEN, + .write_perm = cfg->sec_mode.lbs_button_char.cccd_write, }; ble_gatts_char_md_t char_md = { .char_props = { @@ -102,6 +104,7 @@ uint32_t ble_lbs_init(struct ble_lbs *lbs, const struct ble_lbs_config *cfg) }; ble_gatts_attr_md_t attr_md = { .vloc = BLE_GATTS_VLOC_STACK, + .read_perm = cfg->sec_mode.lbs_button_char.read, }; ble_gatts_attr_t attr_char_value = { .p_uuid = &char_uuid, @@ -110,9 +113,6 @@ uint32_t ble_lbs_init(struct ble_lbs *lbs, const struct ble_lbs_config *cfg) .init_len = sizeof(uint8_t), .max_len = sizeof(uint8_t), }; - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm); - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm); - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm); nrf_err = sd_ble_gatts_characteristic_add(lbs->service_handle, &char_md, &attr_char_value, &lbs->button_char_handles); @@ -135,6 +135,8 @@ uint32_t ble_lbs_init(struct ble_lbs *lbs, const struct ble_lbs_config *cfg) }; attr_md = (ble_gatts_attr_md_t){ .vloc = BLE_GATTS_VLOC_STACK, + .read_perm = cfg->sec_mode.lbs_led_char.read, + .write_perm = cfg->sec_mode.lbs_led_char.write, }; attr_char_value = (ble_gatts_attr_t){ .p_uuid = &char_uuid, @@ -143,8 +145,6 @@ uint32_t ble_lbs_init(struct ble_lbs *lbs, const struct ble_lbs_config *cfg) .init_len = sizeof(uint8_t), .max_len = sizeof(uint8_t), }; - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm); - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm); nrf_err = sd_ble_gatts_characteristic_add(lbs->service_handle, &char_md, &attr_char_value, &lbs->led_char_handles); diff --git a/subsys/bluetooth/services/ble_mcumgr/mcumgr.c b/subsys/bluetooth/services/ble_mcumgr/mcumgr.c index 69bcaa3dfe..1402810b32 100644 --- a/subsys/bluetooth/services/ble_mcumgr/mcumgr.c +++ b/subsys/bluetooth/services/ble_mcumgr/mcumgr.c @@ -79,14 +79,16 @@ static struct ble_mcumgr_client_context *ble_mcumgr_client_context_get(uint16_t return ((idx >= 0) ? &contexts[idx] : NULL); } -static uint32_t mcumgr_characteristic_add(struct ble_mcumgr *service) +static uint32_t mcumgr_characteristic_add(struct ble_mcumgr *service, struct ble_mcumgr_config *cfg) { ble_uuid_t char_uuid = { .type = service->uuid_type_characteristic, .uuid = BLE_MCUMGR_CHARACTERISTIC_UUID_SUB, }; ble_gatts_attr_md_t cccd_md = { - .vloc = BLE_GATTS_VLOC_STACK + .vloc = BLE_GATTS_VLOC_STACK, + .read_perm = BLE_GAP_CONN_SEC_MODE_OPEN, + .write_perm = cfg->sec_mode.mcumgr_char.cccd_write, }; ble_gatts_char_md_t char_md = { .char_props = { @@ -98,6 +100,8 @@ static uint32_t mcumgr_characteristic_add(struct ble_mcumgr *service) ble_gatts_attr_md_t attr_md = { .vloc = BLE_GATTS_VLOC_STACK, .vlen = true, + .read_perm = cfg->sec_mode.mcumgr_char.read, + .write_perm = cfg->sec_mode.mcumgr_char.write, }; ble_gatts_attr_t attr_char_value = { .p_uuid = &char_uuid, @@ -107,13 +111,6 @@ static uint32_t mcumgr_characteristic_add(struct ble_mcumgr *service) .max_len = BLE_GATT_MAX_DATA_LEN, }; - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm); - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm); - - /* Setup CCCD */ - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm); - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm); - return sd_ble_gatts_characteristic_add(service->service_handle, &char_md, &attr_char_value, &service->characteristic_handle); } @@ -374,7 +371,7 @@ static void on_ble_evt(const ble_evt_t *evt, void *ctx) NRF_SDH_BLE_OBSERVER(sdh_ble, on_ble_evt, &ble_mcumgr, HIGH); -uint32_t ble_mcumgr_init(void) +uint32_t ble_mcumgr_init(struct ble_mcumgr_config *cfg) { uint32_t nrf_err; ble_uuid_t ble_uuid; @@ -418,7 +415,7 @@ uint32_t ble_mcumgr_init(void) } /* Add MCUmgr characteristic */ - nrf_err = mcumgr_characteristic_add(&ble_mcumgr); + nrf_err = mcumgr_characteristic_add(&ble_mcumgr, cfg); if (nrf_err) { LOG_ERR("mcumgr_characteristic_add failed, nrf_error %#x", nrf_err); diff --git a/subsys/bluetooth/services/ble_nus/nus.c b/subsys/bluetooth/services/ble_nus/nus.c index 7765916a6d..d028b0f2c2 100644 --- a/subsys/bluetooth/services/ble_nus/nus.c +++ b/subsys/bluetooth/services/ble_nus/nus.c @@ -38,6 +38,8 @@ static uint32_t nus_rx_char_add(struct ble_nus *nus, struct ble_nus_config const ble_gatts_attr_md_t attr_md = { .vloc = BLE_GATTS_VLOC_STACK, .vlen = true, + .read_perm = cfg->sec_mode.nus_rx_char.read, + .write_perm = cfg->sec_mode.nus_rx_char.write, }; ble_gatts_attr_t attr_char_value = { .p_uuid = &char_uuid, @@ -47,9 +49,6 @@ static uint32_t nus_rx_char_add(struct ble_nus *nus, struct ble_nus_config const .max_len = BLE_NUS_MAX_DATA_LEN, }; - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm); - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.write_perm); - /* Add Nordic UART RX characteristic declaration and value attributes. */ return sd_ble_gatts_characteristic_add(nus->service_handle, &char_md, &attr_char_value, &nus->rx_handles); @@ -62,7 +61,9 @@ static uint32_t nus_tx_char_add(struct ble_nus *nus, struct ble_nus_config const .uuid = BLE_UUID_NUS_TX_CHARACTERISTIC, }; ble_gatts_attr_md_t cccd_md = { - .vloc = BLE_GATTS_VLOC_STACK + .vloc = BLE_GATTS_VLOC_STACK, + .read_perm = BLE_GAP_CONN_SEC_MODE_OPEN, + .write_perm = cfg->sec_mode.nus_tx_char.cccd_write, }; ble_gatts_char_md_t char_md = { .char_props = { @@ -73,6 +74,7 @@ static uint32_t nus_tx_char_add(struct ble_nus *nus, struct ble_nus_config const ble_gatts_attr_md_t attr_md = { .vloc = BLE_GATTS_VLOC_STACK, .vlen = true, + .read_perm = cfg->sec_mode.nus_tx_char.read, }; ble_gatts_attr_t attr_char_value = { .p_uuid = &char_uuid, @@ -82,12 +84,6 @@ static uint32_t nus_tx_char_add(struct ble_nus *nus, struct ble_nus_config const .max_len = BLE_NUS_MAX_DATA_LEN, }; - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&attr_md.read_perm); - - /* Setup CCCD */ - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm); - BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm); - /* Add Nordic UART TX declaration, value and CCCD attributes */ return sd_ble_gatts_characteristic_add(nus->service_handle, &char_md, &attr_char_value, &nus->tx_handles); diff --git a/tests/subsys/bluetooth/services/ble_bas/src/unity_test.c b/tests/subsys/bluetooth/services/ble_bas/src/unity_test.c index d3777f88ea..aa71d7e6c3 100644 --- a/tests/subsys/bluetooth/services/ble_bas/src/unity_test.c +++ b/tests/subsys/bluetooth/services/ble_bas/src/unity_test.c @@ -43,10 +43,14 @@ static struct { static struct ble_bas_config bas_cfg_template = { .can_notify = true, .battery_level = BATTERY_REFERENCE_VALUE, - .batt_rd_sec = {.lv = 1, .sm = 2}, - .cccd_wr_sec = {.lv = 3, .sm = 4}, - .report_ref_rd_sec = {.lv = 5, .sm = 6}, .report_ref = (void *)&report_ref, + .sec_mode = { + .battery_lvl_char = { + .read = {.lv = 1, .sm = 2}, + .cccd_write = {.lv = 3, .sm = 4}, + }, + .battery_report_ref.read = {.lv = 5, .sm = 6}, + } }; uint32_t stub_sd_ble_gatts_service_add_success(uint8_t type, ble_uuid_t const *p_uuid, diff --git a/tests/subsys/bluetooth/services/ble_dis/src/unity_test.c b/tests/subsys/bluetooth/services/ble_dis/src/unity_test.c index 366ab4f071..70cecc5eda 100644 --- a/tests/subsys/bluetooth/services/ble_dis/src/unity_test.c +++ b/tests/subsys/bluetooth/services/ble_dis/src/unity_test.c @@ -17,6 +17,10 @@ #define HANDLE 0xa4 +struct ble_dis_config dis_config = { + .sec_mode = BLE_DIS_CONFIG_SEC_MODE_DEFAULT, + }; + uint32_t stub_sd_ble_gatts_service_add_invalid_param(uint8_t type, ble_uuid_t const *p_uuid, uint16_t *p_handle, int cmock_num_calls) { @@ -148,20 +152,28 @@ uint32_t stub_sd_ble_gatts_characteristic_add( return NRF_SUCCESS; } +void test_ble_dis_init_error_null(void) +{ + uint32_t nrf_err; + + nrf_err = ble_dis_init(NULL); + TEST_ASSERT_EQUAL(NRF_ERROR_NULL, nrf_err); +} + void test_ble_dis_init_error_invalid_param(void) { uint32_t nrf_err; __cmock_sd_ble_gatts_service_add_Stub(stub_sd_ble_gatts_service_add_invalid_param); - nrf_err = ble_dis_init(); + nrf_err = ble_dis_init(&dis_config); TEST_ASSERT_EQUAL(NRF_ERROR_INVALID_PARAM, nrf_err); __cmock_sd_ble_gatts_service_add_Stub(stub_sd_ble_gatts_service_add); __cmock_sd_ble_gatts_characteristic_add_Stub( stub_sd_ble_gatts_characteristic_add_invalid_param); - nrf_err = ble_dis_init(); + nrf_err = ble_dis_init(&dis_config); TEST_ASSERT_EQUAL(NRF_ERROR_INVALID_PARAM, nrf_err); } @@ -172,7 +184,7 @@ void test_ble_dis_init(void) __cmock_sd_ble_gatts_service_add_Stub(stub_sd_ble_gatts_service_add); __cmock_sd_ble_gatts_characteristic_add_Stub(stub_sd_ble_gatts_characteristic_add); - nrf_err = ble_dis_init(); + nrf_err = ble_dis_init(&dis_config); TEST_ASSERT_EQUAL(NRF_SUCCESS, nrf_err); }