Skip to content

Commit

Permalink
Bluetooth: Mesh: fix uptime to tai conversion inaccuracy
Browse files Browse the repository at this point in the history
Commit fixes uptime to tai inaccuracy conversion.
Previous implementation did not take into account
subseconds overflow that caused -1 second inaccuracy.

Signed-off-by: Aleksandr Khromykh <[email protected]>
  • Loading branch information
alxelax authored and rlubos committed Jan 13, 2025
1 parent 6586628 commit 3bf7d40
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions subsys/bluetooth/mesh/time_srv.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,27 @@ static inline bool tai_is_unknown(const struct bt_mesh_time_tai *tai)
return !tai->sec && !tai->subsec;
}

static inline struct bt_mesh_time_tai
tai_at(const struct bt_mesh_time_srv *srv, int64_t uptime)
static inline struct bt_mesh_time_tai tai_at(const struct bt_mesh_time_srv *srv, int64_t uptime)
{
const struct bt_mesh_time_tai *sync = &srv->data.sync.status.tai;
int64_t steps = (SUBSEC_STEPS * (uptime - srv->data.sync.uptime)) /
MSEC_PER_SEC;
int64_t steps = (SUBSEC_STEPS * (uptime - srv->data.sync.uptime)) / MSEC_PER_SEC;
int64_t sec;
int64_t subsec;

if (tai_is_unknown(sync)) {
return *sync;
}

return (struct bt_mesh_time_tai) {
.sec = sync->sec + (steps / SUBSEC_STEPS),
.subsec = sync->subsec + steps,
subsec = sync->subsec + steps;
sec = sync->sec + (subsec / SUBSEC_STEPS);

if (subsec >= SUBSEC_STEPS) {
subsec %= SUBSEC_STEPS;
}

return (struct bt_mesh_time_tai){
.sec = sec,
.subsec = subsec,
};
}

Expand Down

0 comments on commit 3bf7d40

Please sign in to comment.