Skip to content

Commit 3bf7d40

Browse files
alxelaxrlubos
authored andcommitted
Bluetooth: Mesh: fix uptime to tai conversion inaccuracy
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]>
1 parent 6586628 commit 3bf7d40

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

subsys/bluetooth/mesh/time_srv.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,27 @@ static inline bool tai_is_unknown(const struct bt_mesh_time_tai *tai)
3636
return !tai->sec && !tai->subsec;
3737
}
3838

39-
static inline struct bt_mesh_time_tai
40-
tai_at(const struct bt_mesh_time_srv *srv, int64_t uptime)
39+
static inline struct bt_mesh_time_tai tai_at(const struct bt_mesh_time_srv *srv, int64_t uptime)
4140
{
4241
const struct bt_mesh_time_tai *sync = &srv->data.sync.status.tai;
43-
int64_t steps = (SUBSEC_STEPS * (uptime - srv->data.sync.uptime)) /
44-
MSEC_PER_SEC;
42+
int64_t steps = (SUBSEC_STEPS * (uptime - srv->data.sync.uptime)) / MSEC_PER_SEC;
43+
int64_t sec;
44+
int64_t subsec;
4545

4646
if (tai_is_unknown(sync)) {
4747
return *sync;
4848
}
4949

50-
return (struct bt_mesh_time_tai) {
51-
.sec = sync->sec + (steps / SUBSEC_STEPS),
52-
.subsec = sync->subsec + steps,
50+
subsec = sync->subsec + steps;
51+
sec = sync->sec + (subsec / SUBSEC_STEPS);
52+
53+
if (subsec >= SUBSEC_STEPS) {
54+
subsec %= SUBSEC_STEPS;
55+
}
56+
57+
return (struct bt_mesh_time_tai){
58+
.sec = sec,
59+
.subsec = subsec,
5360
};
5461
}
5562

0 commit comments

Comments
 (0)