Skip to content

Commit

Permalink
Bluetooth: Mesh: Handle dim from zero in applications
Browse files Browse the repository at this point in the history
This updates the samples and tester to correctly transition the
lightness state when dimming up from zero when a RANGE_MIN is set.

It also updates the documentation for the callback to mention this
requirement.

Signed-off-by: Ludvig Samuelsen Jordet <[email protected]>
  • Loading branch information
ludvigsj authored and rlubos committed Oct 10, 2023
1 parent e127f90 commit 6976648
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
20 changes: 20 additions & 0 deletions include/bluetooth/mesh/lightness_srv.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ struct bt_mesh_lightness_srv_handlers {
* @ref bt_mesh_model_transition_time returns a nonzero value, the application is
* responsible for publishing a value of the Light state at the end of the transition.
*
* When performing a non-instantaneous state change, the Light state at any intermediate
* point must be clamped to the current lightness range of the server using
* @ref bt_mesh_lightness_clamp.
*
* @note This handler is mandatory.
*
* @param[in] srv Server to set the Light state of.
Expand All @@ -89,6 +93,10 @@ struct bt_mesh_lightness_srv_handlers {
struct bt_mesh_lightness_status *rsp);

/** @brief Get the current Light state.
*
* When in the middle of a non-instantaneous state change, the Light value filled in
* @c rsp must be clamped to the current lightness range of the server using
* @ref bt_mesh_lightness_clamp.
*
* @note This handler is mandatory.
*
Expand Down Expand Up @@ -190,6 +198,18 @@ struct bt_mesh_lightness_srv {
#endif
};

/** @brief Clamp lightness to lightness range.
*
* @return @c lightness clamped to the lightness range set on the
* @ref bt_mesh_lightness_srv pointed to by @c srv if @c lightness > 0, zero
* otherwise.
*/
static inline uint16_t bt_mesh_lightness_clamp(const struct bt_mesh_lightness_srv *srv,
uint16_t lightness)
{
return lightness == 0 ? 0 : CLAMP(lightness, srv->range.min, srv->range.max);
}

/** @brief Publish the current Light state.
*
* Publishes a Light Lightness status message with the configured publish
Expand Down
8 changes: 5 additions & 3 deletions samples/bluetooth/mesh/light_ctrl/src/model_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,10 @@ static void periodic_led_work(struct k_work *work)

k_work_reschedule(&l_ctx->per_work, K_MSEC(l_ctx->time_per));
apply_and_print:
lc_pwm_led_set(l_ctx->current_lvl);
printk("Current light lvl: %u/65535\n", l_ctx->current_lvl);
uint16_t clamped_lvl = bt_mesh_lightness_clamp(&l_ctx->lightness_srv,
l_ctx->current_lvl);
lc_pwm_led_set(clamped_lvl);
printk("Current light lvl: %u/65535\n", clamped_lvl);
}

static void light_set(struct bt_mesh_lightness_srv *srv,
Expand All @@ -144,7 +146,7 @@ static void light_get(struct bt_mesh_lightness_srv *srv,
struct lightness_ctx *l_ctx =
CONTAINER_OF(srv, struct lightness_ctx, lightness_srv);

rsp->current = l_ctx->current_lvl;
rsp->current = bt_mesh_lightness_clamp(&l_ctx->lightness_srv, l_ctx->current_lvl);
rsp->target = l_ctx->target_lvl;
rsp->remaining_time = l_ctx->rem_time;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/bluetooth/tester/src/model_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ start_new_lightness_trans(uint32_t step_cnt,
static void lightness_status(struct lightness_ctx *ctx,
struct bt_mesh_lightness_status *rsp)
{
rsp->current = ctx->current;
rsp->current = bt_mesh_lightness_clamp(&ctx->srv, ctx->current);
rsp->target = ctx->target;
rsp->remaining_time = ctx->remaining;
}
Expand Down

0 comments on commit 6976648

Please sign in to comment.