Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/nrf-bm/release_notes/release_notes_changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ No changes since the latest nRF Connect SDK Bare Metal release.
Bluetooth LE Services
---------------------

No changes since the latest nRF Connect SDK Bare Metal release.
* Updated all services to return errors from the SoftDevice directly.

Libraries for NFC
-----------------
Expand Down
20 changes: 9 additions & 11 deletions include/bm/bluetooth/services/ble_bas.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,10 @@ struct ble_bas {
*
* @retval NRF_SUCCESS On success.
* @retval NRF_ERROR_NULL If @p bas or @p bas_config are @c NULL.
* @retval NRF_ERROR_INVALID_PARAM Invalid parameters.
* @return In addition, this function may return any error returned by these SoftDevice functions:
* - @ref sd_ble_gatts_service_add(),
* - @ref sd_ble_gatts_characteristic_add(),
* - @ref sd_ble_gatts_descriptor_add().
*/
uint32_t ble_bas_init(struct ble_bas *bas, const struct ble_bas_config *bas_config);

Expand All @@ -195,28 +198,23 @@ uint32_t ble_bas_init(struct ble_bas *bas, const struct ble_bas_config *bas_conf
*
* @retval NRF_SUCCESS On success.
* @retval NRF_ERROR_NULL If @p bas is @c NULL.
* @retval NRF_ERROR_INVALID_PARAM Invalid parameters.
* @retval NRF_ERROR_NOT_FOUND Invalid connection handle.
* @retval NRF_ERROR_INVALID_STATE Notifications not enabled in the CCCD.
* @return In addition, this function may return any error returned by these SoftDevice functions:
* - @ref sd_ble_gatts_value_set(),
* - @ref sd_ble_gatts_hvx().
*/
uint32_t ble_bas_battery_level_update(struct ble_bas *bas, uint16_t conn_handle,
uint8_t battery_level);

/**
* @brief Notify battery level.
*
* @note For the requirements in the BAS specification to be fulfilled,
* this function must be called upon reconnection if the battery level has changed
* while the service has been disconnected from a bonded client.
*
* @param bas Battery service.
* @param conn_handle Connection handle.
*
* @retval NRF_SUCCESS On success.
* @retval NRF_ERROR_NULL If @p bas is @c NULL.
* @retval NRF_ERROR_INVALID_PARAM Invalid parameters.
* @retval NRF_ERROR_NOT_FOUND Invalid connection handle.
* @retval NRF_ERROR_INVALID_STATE Notifications not enabled in the CCCD.
* @return In addition, this function may return any error returned by these SoftDevice functions:
* - @ref sd_ble_gatts_hvx().
*/
uint32_t ble_bas_battery_level_notify(struct ble_bas *bas, uint16_t conn_handle);

Expand Down
12 changes: 7 additions & 5 deletions include/bm/bluetooth/services/ble_hrs.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@ struct ble_hrs {
*
* @retval NRF_SUCCESS On success.
* @retval NRF_ERROR_NULL If @p hrs or @p hrs_config are @c NULL.
* @retval NRF_ERROR_INVALID_PARAM Invalid parameters.
* @return In addition, this function may return any error returned by these SoftDevice functions:
* - @ref sd_ble_gatts_service_add(),
* - @ref sd_ble_gatts_characteristic_add().
*/
uint32_t ble_hrs_init(struct ble_hrs *hrs, const struct ble_hrs_config *hrs_config);

Expand All @@ -230,9 +232,8 @@ void ble_hrs_conn_params_evt(struct ble_hrs *hrs, const struct ble_conn_params_e
*
* @retval NRF_SUCCESS On success.
* @retval NRF_ERROR_NULL If @p hrs is @c NULL.
* @retval NRF_ERROR_INVALID_PARAM Failed to send notification.
* @retval NRF_ERROR_NOT_FOUND Invalid connection handle.
* @retval NRF_ERROR_INVALID_STATE Notifications not enabled in the CCCD.
* @return In addition, this function may return any error returned by these SoftDevice functions:
* - @ref sd_ble_gatts_hvx().
*/
uint32_t ble_hrs_heart_rate_measurement_send(struct ble_hrs *hrs, uint16_t heart_rate);

Expand Down Expand Up @@ -298,7 +299,8 @@ uint32_t ble_hrs_sensor_contact_detected_update(struct ble_hrs *hrs,
*
* @retval NRF_SUCCESS On success.
* @retval NRF_ERROR_NULL If @p hrs is @c NULL.
* @retval NRF_ERROR_INVALID_PARAM Failed to set new value.
* @return In addition, this function may return any error returned by these SoftDevice functions:
* - @ref sd_ble_gatts_value_set().
*/
uint32_t ble_hrs_body_sensor_location_set(struct ble_hrs *hrs, uint8_t body_sensor_location);

Expand Down
8 changes: 6 additions & 2 deletions include/bm/bluetooth/services/ble_lbs.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,10 @@ struct ble_lbs {
*
* @retval NRF_SUCCESS If the service was initialized successfully.
* @retval NRF_ERROR_NULL If @p lbs or @p cfg is NULL.
* @retval NRF_ERROR_INVALID_PARAM If the supplied configuration is invalid.
* @return In addition, this function may return any error returned by these SoftDevice functions:
* - @ref sd_ble_gatts_service_add(),
* - @ref sd_ble_gatts_characteristic_add(),
* - @ref sd_ble_uuid_vs_add().
*/
uint32_t ble_lbs_init(struct ble_lbs *lbs, const struct ble_lbs_config *cfg);

Expand All @@ -163,7 +166,8 @@ void ble_lbs_on_ble_evt(const ble_evt_t *ble_evt, void *lbs_instance);
*
* @retval NRF_SUCCESS If the notification was sent successfully.
* @retval NRF_ERROR_NULL If @p lbs is NULL.
* @retval NRF_ERROR_INVALID_PARAM If the parameters are invalid.
* @return In addition, this function may return any error returned by these SoftDevice functions:
* - @ref sd_ble_gatts_hvx().
*/
uint32_t ble_lbs_on_button_change(struct ble_lbs *lbs, uint16_t conn_handle, uint8_t button_state);

Expand Down
9 changes: 7 additions & 2 deletions include/bm/bluetooth/services/ble_nus.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,10 @@ struct ble_nus {
*
* @retval NRF_SUCCESS On success.
* @retval NRF_ERROR_NULL If @p nus or @p nus_config is @c NULL.
* @retval NRF_ERROR_INVALID_PARAM Invalid parameters.
* @return In addition, this function may return any error returned by these SoftDevice functions:
* - @ref sd_ble_gatts_service_add(),
* - @ref sd_ble_gatts_characteristic_add(),
* - @ref sd_ble_uuid_vs_add().
*/
uint32_t ble_nus_init(struct ble_nus *nus, const struct ble_nus_config *nus_config);

Expand Down Expand Up @@ -234,7 +237,9 @@ void ble_nus_on_ble_evt(const ble_evt_t *ble_evt, void *context);
* @param[in] conn_handle Connection handle of the destination client.
*
* @retval NRF_SUCCESS On success.
* @return nrf_error on failure.
* @retval NRF_ERROR_NULL If @p nus, @p data or @p length are @c NULL.
* @return In addition, this function may return any error returned by these SoftDevice functions:
* - @ref sd_ble_gatts_hvx().
*/
uint32_t ble_nus_data_send(struct ble_nus *nus, uint8_t *data, uint16_t *length,
uint16_t conn_handle);
Expand Down
23 changes: 15 additions & 8 deletions samples/bluetooth/ble_hrs/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ static struct bm_timer heart_rate_timer;
static struct bm_timer rr_interval_timer;
static struct bm_timer sensor_contact_timer;

static bool hrs_notif_enabled;
static bool bas_notif_enabled;

void battery_level_meas_timeout_handler(void *context)
{
int err;
Expand All @@ -82,6 +85,10 @@ void battery_level_meas_timeout_handler(void *context)
return;
}

if (!bas_notif_enabled) {
return;
}

nrf_err = ble_bas_battery_level_update(&ble_bas, conn_handle, battery_level);
if (nrf_err) {
/* Ignore if not in a connection or notifications disabled in CCCD. */
Expand All @@ -106,6 +113,10 @@ static void heart_rate_meas_timeout_handler(void *context)
return;
}

if (!hrs_notif_enabled) {
return;
}

nrf_err = ble_hrs_heart_rate_measurement_send(&ble_hrs, (uint16_t)heart_rate);
if (nrf_err) {
/* Ignore if not in a connection or notifications disabled in CCCD. */
Expand Down Expand Up @@ -297,13 +308,11 @@ static void ble_bas_evt_handler(struct ble_bas *bas, const struct ble_bas_evt *e
{
switch (evt->evt_type) {
case BLE_BAS_EVT_NOTIFICATION_ENABLED:
/* ignore */
bas_notif_enabled = true;
break;

case BLE_BAS_EVT_NOTIFICATION_DISABLED:
/* ignore */
bas_notif_enabled = false;
break;

default:
break;
}
Expand All @@ -313,13 +322,11 @@ static void ble_hrs_evt_handler(struct ble_hrs *hrs, const struct ble_hrs_evt *e
{
switch (evt->evt_type) {
case BLE_HRS_EVT_NOTIFICATION_ENABLED:
/* ignore */
hrs_notif_enabled = true;
break;

case BLE_HRS_EVT_NOTIFICATION_DISABLED:
/* ignore */
hrs_notif_enabled = false;
break;

default:
break;
}
Expand Down
46 changes: 15 additions & 31 deletions subsys/bluetooth/services/ble_bas/bas.c
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,14 @@ uint32_t ble_bas_init(struct ble_bas *bas, const struct ble_bas_config *cfg)
&bas->service_handle);
if (nrf_err) {
LOG_ERR("Failed to add battery service, nrf_error %#x", nrf_err);
return NRF_ERROR_INVALID_PARAM;
return nrf_err;
}

/* Add battery level characteristic */
nrf_err = battery_level_char_add(bas, cfg);
if (nrf_err) {
LOG_ERR("Failed to add battery service characteristic, nrf_error %#x", nrf_err);
return NRF_ERROR_INVALID_PARAM;
return nrf_err;
}

/* Add reference descriptor if present */
Expand All @@ -160,7 +160,7 @@ uint32_t ble_bas_init(struct ble_bas *bas, const struct ble_bas_config *cfg)
if (nrf_err) {
LOG_ERR("Failed to add report reference descriptor, nrf_error %#x",
nrf_err);
return NRF_ERROR_INVALID_PARAM;
return nrf_err;
}
}

Expand All @@ -182,7 +182,7 @@ uint32_t ble_bas_battery_level_update(

if (bas->battery_level == battery_level) {
/* Nothing to do */
return 0;
return NRF_SUCCESS;
}

/* Update database */
Expand All @@ -193,7 +193,7 @@ uint32_t ble_bas_battery_level_update(
bas->battery_level_handles.value_handle, &gatts_value);
if (nrf_err) {
LOG_ERR("Failed to update battery level, nrf_error %#x", nrf_err);
return NRF_ERROR_INVALID_PARAM;
return nrf_err;
}

LOG_DBG("Battery level: %d%%", battery_level);
Expand All @@ -212,49 +212,33 @@ uint32_t ble_bas_battery_level_update(
hvx.p_data = gatts_value.p_value;

nrf_err = sd_ble_gatts_hvx(conn_handle, &hvx);
switch (nrf_err) {
case NRF_SUCCESS:
return NRF_SUCCESS;
case BLE_ERROR_INVALID_CONN_HANDLE:
return NRF_ERROR_NOT_FOUND;
case NRF_ERROR_INVALID_STATE:
case BLE_ERROR_GATTS_SYS_ATTR_MISSING:
return NRF_ERROR_INVALID_STATE;
default:
if (nrf_err) {
LOG_ERR("Failed to notify battery level, nrf_error %#x", nrf_err);
return NRF_ERROR_INVALID_PARAM;
return nrf_err;
}

return NRF_SUCCESS;
}

uint32_t ble_bas_battery_level_notify(struct ble_bas *bas, uint16_t conn_handle)
{
uint32_t nrf_err;
ble_gatts_hvx_params_t hvx = {0};
uint16_t len = sizeof(uint8_t);

if (!bas) {
return NRF_ERROR_NULL;
}
if (!bas->can_notify) {
return NRF_ERROR_INVALID_PARAM;
}

hvx.handle = bas->battery_level_handles.value_handle;
hvx.type = BLE_GATT_HVX_NOTIFICATION;
hvx.offset = 0;
hvx.p_len = &len;
hvx.handle = bas->battery_level_handles.value_handle;
hvx.p_len = &(uint16_t){sizeof(bas->battery_level)};
hvx.p_data = &bas->battery_level;

nrf_err = sd_ble_gatts_hvx(conn_handle, &hvx);
switch (nrf_err) {
case NRF_SUCCESS:
return NRF_SUCCESS;
case BLE_ERROR_INVALID_CONN_HANDLE:
return NRF_ERROR_NOT_FOUND;
case NRF_ERROR_INVALID_STATE:
return NRF_ERROR_INVALID_STATE;
default:
if (nrf_err) {
LOG_ERR("Failed to notify battery level, nrf_error %#x", nrf_err);
return NRF_ERROR_INVALID_PARAM;
return nrf_err;
}

return NRF_SUCCESS;
}
41 changes: 13 additions & 28 deletions subsys/bluetooth/services/ble_hrs/hrs.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,23 +237,23 @@ uint32_t ble_hrs_init(struct ble_hrs *hrs, const struct ble_hrs_config *cfg)
&hrs->service_handle);
if (nrf_err) {
LOG_ERR("Failed to add heart rate service, nrf_error %#x", nrf_err);
return NRF_ERROR_INVALID_PARAM;
return nrf_err;
}

/* Add Heart rate measurement characteristic. */
nrf_err = heart_rate_measurement_char_add(hrs, cfg);
if (nrf_err) {
LOG_ERR("Failed to add heart rate measurement characteristic, nrf_error %#x",
nrf_err);
return NRF_ERROR_INVALID_PARAM;
return nrf_err;
}

/* Add Body sensor location characteristic. */
nrf_err = body_sensor_location_char_add(hrs, cfg);
if (nrf_err) {
LOG_ERR("Failed to add body sensor location characteristic, nrf_error %#x",
nrf_err);
return NRF_ERROR_INVALID_PARAM;
return nrf_err;
}

return NRF_SUCCESS;
Expand All @@ -262,9 +262,9 @@ uint32_t ble_hrs_init(struct ble_hrs *hrs, const struct ble_hrs_config *cfg)
uint32_t ble_hrs_heart_rate_measurement_send(struct ble_hrs *hrs, uint16_t heart_rate)
{
uint32_t nrf_err;
ble_gatts_hvx_params_t hvx = {0};
uint8_t encoded_hrm[MAX_HRM_LEN_CALC(CONFIG_NRF_SDH_BLE_GATT_MAX_MTU_SIZE)];
uint16_t len;
uint16_t hvx_len;

if (!hrs) {
return NRF_ERROR_NULL;
Expand All @@ -274,34 +274,20 @@ uint32_t ble_hrs_heart_rate_measurement_send(struct ble_hrs *hrs, uint16_t heart

/* Prepare heart rate measurement notification data */
len = hrm_encode(hrs, heart_rate, encoded_hrm);
hvx_len = len;

/* Notify */
ble_gatts_hvx_params_t hvx = {
.handle = hrs->hrm_handles.value_handle,
.type = BLE_GATT_HVX_NOTIFICATION,
.offset = 0,
.p_len = &hvx_len,
.p_data = encoded_hrm,
};
hvx.type = BLE_GATT_HVX_NOTIFICATION;
hvx.handle = hrs->hrm_handles.value_handle;
hvx.p_len = &len;
hvx.p_data = encoded_hrm;

nrf_err = sd_ble_gatts_hvx(hrs->conn_handle, &hvx);
switch (nrf_err) {
case NRF_SUCCESS:
if (hvx_len != len) {
LOG_ERR("Notified %d of %d bytes", hvx_len, len);
return NRF_ERROR_INVALID_PARAM;
}
return NRF_SUCCESS;
Comment on lines -290 to -295
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a change in behavior, where earlier we returned an error now we return success.

case BLE_ERROR_INVALID_CONN_HANDLE:
return NRF_ERROR_NOT_FOUND;
case NRF_ERROR_INVALID_STATE:
case BLE_ERROR_GATTS_SYS_ATTR_MISSING:
return NRF_ERROR_INVALID_STATE;
default:
if (nrf_err) {
LOG_ERR("Failed to notify heart rate measurement, nrf_error %#x", nrf_err);
return NRF_ERROR_INVALID_PARAM;
return nrf_err;
}

return NRF_SUCCESS;
}

uint32_t ble_hrs_rr_interval_add(struct ble_hrs *hrs, uint16_t rr_interval)
Expand Down Expand Up @@ -358,7 +344,6 @@ uint32_t ble_hrs_body_sensor_location_set(struct ble_hrs *hrs, uint8_t body_sens
uint32_t nrf_err;
ble_gatts_value_t gatts_value = {
.len = sizeof(uint8_t),
.offset = 0,
.p_value = &body_sensor_location,
};

Expand All @@ -370,7 +355,7 @@ uint32_t ble_hrs_body_sensor_location_set(struct ble_hrs *hrs, uint8_t body_sens
&gatts_value);
if (nrf_err) {
LOG_ERR("Failed to update body sensor location, nrf_error %#x", nrf_err);
return NRF_ERROR_INVALID_PARAM;
return nrf_err;
}

return NRF_SUCCESS;
Expand Down
Loading