2014-10-14 08:14:45 +02:00
|
|
|
/*
|
|
|
|
* Trickle implementation
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013, 2014 INRIA.
|
2017-06-08 21:27:11 +02:00
|
|
|
* 2017 HAW Hamburg
|
2014-10-14 08:14:45 +02:00
|
|
|
*
|
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Eric Engel <eric.engel@fu-berlin.de>
|
2017-06-08 21:27:11 +02:00
|
|
|
* @author Cenk Gündoğan <cenk.guendogan@haw-hamburg.de>
|
2014-10-14 08:14:45 +02:00
|
|
|
*/
|
|
|
|
|
2020-10-21 15:58:33 +02:00
|
|
|
#include <assert.h>
|
|
|
|
|
2014-10-14 08:14:45 +02:00
|
|
|
#include "inttypes.h"
|
2016-04-15 15:12:33 +02:00
|
|
|
#include "random.h"
|
2014-10-14 08:14:45 +02:00
|
|
|
#include "trickle.h"
|
2023-03-17 12:04:44 +01:00
|
|
|
#include "ztimer.h"
|
2014-10-14 08:14:45 +02:00
|
|
|
|
2020-10-22 11:35:22 +02:00
|
|
|
#define ENABLE_DEBUG 0
|
2014-10-14 08:14:45 +02:00
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
void trickle_callback(trickle_t *trickle)
|
|
|
|
{
|
|
|
|
/* Handle k=0 like k=infinity (according to RFC6206, section 6.5) */
|
|
|
|
if ((trickle->c < trickle->k) || (trickle->k == 0)) {
|
|
|
|
(*trickle->callback.func)(trickle->callback.args);
|
|
|
|
}
|
2017-06-08 21:27:11 +02:00
|
|
|
|
|
|
|
trickle_interval(trickle);
|
2014-10-14 08:14:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void trickle_interval(trickle_t *trickle)
|
|
|
|
{
|
2017-06-09 14:08:06 +02:00
|
|
|
assert(trickle->I > 0);
|
|
|
|
|
2017-06-08 21:27:11 +02:00
|
|
|
uint32_t old_interval = trickle->I;
|
|
|
|
uint32_t max_interval = trickle->Imin << trickle->Imax;
|
|
|
|
uint32_t diff = old_interval - trickle->t;
|
2014-10-14 08:14:45 +02:00
|
|
|
|
2017-06-08 21:27:11 +02:00
|
|
|
trickle->I *= 2;
|
2017-06-09 14:08:06 +02:00
|
|
|
if (trickle->I > max_interval) {
|
2016-04-15 15:12:33 +02:00
|
|
|
trickle->I = max_interval;
|
2017-06-09 13:07:21 +02:00
|
|
|
old_interval = max_interval / 2;
|
2014-10-14 08:14:45 +02:00
|
|
|
}
|
|
|
|
|
2017-06-08 21:27:11 +02:00
|
|
|
DEBUG("trickle: I == %" PRIu32 ", diff == %" PRIu32 "\n", trickle->I, diff);
|
2014-10-14 08:14:45 +02:00
|
|
|
|
|
|
|
trickle->c = 0;
|
2017-06-09 14:08:06 +02:00
|
|
|
/* old_interval == trickle->I / 2 */
|
2017-06-08 21:27:11 +02:00
|
|
|
trickle->t = random_uint32_range(old_interval, trickle->I);
|
2014-10-14 08:14:45 +02:00
|
|
|
|
2021-04-13 14:19:42 +02:00
|
|
|
ztimer_set_msg(ZTIMER_MSEC, &trickle->msg_timer, (trickle->t + diff),
|
|
|
|
&trickle->msg, trickle->pid);
|
2014-10-14 08:14:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void trickle_reset_timer(trickle_t *trickle)
|
|
|
|
{
|
2017-06-09 14:08:06 +02:00
|
|
|
assert(trickle->I > trickle->Imin);
|
|
|
|
|
2015-09-04 10:50:11 +02:00
|
|
|
trickle_stop(trickle);
|
2017-10-26 15:13:11 +02:00
|
|
|
trickle->I = trickle->t = trickle->Imin;
|
2017-06-09 14:08:06 +02:00
|
|
|
trickle_interval(trickle);
|
2014-10-14 08:14:45 +02:00
|
|
|
}
|
|
|
|
|
2017-06-08 21:27:11 +02:00
|
|
|
void trickle_start(kernel_pid_t pid, trickle_t *trickle, uint16_t msg_type,
|
|
|
|
uint32_t Imin, uint8_t Imax, uint8_t k)
|
2014-10-14 08:14:45 +02:00
|
|
|
{
|
2017-06-09 14:08:06 +02:00
|
|
|
assert(Imin > 0);
|
|
|
|
assert((Imin << Imax) < (UINT32_MAX / 2));
|
|
|
|
|
2014-10-14 08:14:45 +02:00
|
|
|
trickle->c = 0;
|
|
|
|
trickle->k = k;
|
|
|
|
trickle->Imin = Imin;
|
|
|
|
trickle->Imax = Imax;
|
2017-06-08 21:27:11 +02:00
|
|
|
trickle->I = trickle->t = random_uint32_range(trickle->Imin,
|
|
|
|
4 * trickle->Imin);
|
2014-10-14 08:14:45 +02:00
|
|
|
trickle->pid = pid;
|
2017-06-08 21:27:11 +02:00
|
|
|
trickle->msg.content.ptr = trickle;
|
|
|
|
trickle->msg.type = msg_type;
|
2014-10-14 08:14:45 +02:00
|
|
|
|
|
|
|
trickle_interval(trickle);
|
|
|
|
}
|
|
|
|
|
|
|
|
void trickle_stop(trickle_t *trickle)
|
|
|
|
{
|
2021-04-13 14:19:42 +02:00
|
|
|
ztimer_remove(ZTIMER_MSEC, &trickle->msg_timer);
|
2014-10-14 08:14:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void trickle_increment_counter(trickle_t *trickle)
|
|
|
|
{
|
|
|
|
trickle->c++;
|
|
|
|
}
|