Bluetooth: Mesh: Avoid using 64-bit storage & math for beacons

It's in practice impossible for the time between two beacons to be
more than 50 hours (the approximate wrap-around time for a 32-bit
millisecond timer), so we can use a 32-bit timestamp instead of a
64-bit one for the beacon tracking.

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
This commit is contained in:
Johan Hedberg 2017-11-10 13:12:53 +02:00 committed by Johan Hedberg
commit 25c5d36b91
2 changed files with 16 additions and 11 deletions

View file

@ -80,11 +80,9 @@ static void beacon_complete(struct net_buf *buf, int err)
BT_DBG("err %d", err);
sub = &bt_mesh.sub[BT_MESH_ADV(buf)->user_data[0]];
sub->beacon_sent = k_uptime_get();
sub->beacon_sent = k_uptime_get_32();
}
#define BEACON_INTERVAL(sub) K_SECONDS(10 * ((sub)->beacons_last + 1))
void bt_mesh_beacon_create(struct bt_mesh_subnet *sub,
struct net_buf_simple *buf)
{
@ -115,27 +113,34 @@ void bt_mesh_beacon_create(struct bt_mesh_subnet *sub,
bt_hex(sub->auth, 8));
}
/* If the interval has passed or is within 5 seconds from now send a beacon */
#define BEACON_THRESHOLD(sub) (K_SECONDS(10 * ((sub)->beacons_last + 1)) - \
K_SECONDS(5))
static int secure_beacon_send(void)
{
s64_t threshold;
u32_t now = k_uptime_get_32();
int i;
BT_DBG("");
/* If the interval has passed or is within 5 seconds from now
* send a beacon.
*/
threshold = k_uptime_get() + K_SECONDS(5);
for (i = 0; i < ARRAY_SIZE(bt_mesh.sub); i++) {
struct bt_mesh_subnet *sub = &bt_mesh.sub[i];
struct net_buf *buf;
u32_t time_diff;
if (sub->net_idx == BT_MESH_KEY_UNUSED) {
continue;
}
if (sub->beacon_sent + BEACON_INTERVAL(sub) > threshold) {
/* Handle time wrap due to 32-bit storage */
if (sub->beacon_sent > now) {
time_diff = (UINT32_MAX - sub->beacon_sent) + now;
} else {
time_diff = now - sub->beacon_sent;
}
if (time_diff < BEACON_THRESHOLD(sub)) {
continue;
}

View file

@ -43,7 +43,7 @@ struct bt_mesh_friend_cred {
};
struct bt_mesh_subnet {
s64_t beacon_sent; /* Time stamp of last sent beacon */
u32_t beacon_sent; /* Timestamp of last sent beacon */
u8_t beacons_last; /* Number of beacons during last
* observation window
*/