Skip to content

Commit

Permalink
Bluetooth: Mesh: avoid double conversion precision losing
Browse files Browse the repository at this point in the history
Commit solves situation when the level client sends
Set command with exact values but Status has different levels.
It happens when Level model extends the temperature model.
Double conversion happens. Conversions from level
to temperature and back have division result rounding
that causes precision losing.

Signed-off-by: Aleksandr Khromykh <aleksandr.khromykh@nordicsemi.no>
  • Loading branch information
alxelax authored and rlubos committed May 6, 2024
1 parent cd1cff8 commit f8565ee
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions subsys/bluetooth/mesh/light_temp_srv.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,10 @@ static void lvl_set(struct bt_mesh_lvl_srv *lvl_srv,
struct bt_mesh_light_temp_srv *srv =
CONTAINER_OF(lvl_srv, struct bt_mesh_light_temp_srv, lvl);
struct bt_mesh_light_temp_status status = { 0 };
uint16_t temp = lvl_to_temp(srv, lvl_set->lvl);
struct bt_mesh_light_temp_set set = {
.params = {
.temp = lvl_to_temp(srv, lvl_set->lvl),
.temp = temp,
.delta_uv = srv->transient.last.delta_uv,
},
.transition = lvl_set->transition,
Expand All @@ -194,8 +195,15 @@ static void lvl_set(struct bt_mesh_lvl_srv *lvl_srv,
}

if (rsp) {
rsp->current = temp_to_lvl(srv, status.current.temp);
rsp->target = temp_to_lvl(srv, status.target.temp);
/* This helps to avoid weird situation when client sets exact level value
* but after conversion to temperature and back from temperature to level,
* the level value is changed in Status.
* It happens due to the division results rounding in both conversions.
*/
rsp->current = temp == status.current.temp ? lvl_set->lvl
: temp_to_lvl(srv, status.current.temp);
rsp->target = temp == status.target.temp ? lvl_set->lvl
: temp_to_lvl(srv, status.target.temp);
rsp->remaining_time = status.remaining_time;
}
}
Expand Down

0 comments on commit f8565ee

Please sign in to comment.