1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

trickle: enhancements

- add asserts
    - update docu
This commit is contained in:
smlng 2017-06-09 14:08:06 +02:00
parent a6fcbcde9c
commit 5cb8a86e44
2 changed files with 20 additions and 6 deletions

View File

@ -59,6 +59,9 @@ typedef struct {
/**
* @brief resets the trickle timer
*
* @pre `trickle->I > trickle->Imin`
* @see https://tools.ietf.org/html/rfc6206#section-4.2, number 6
*
* @param[in] trickle the trickle timer
*/
void trickle_reset_timer(trickle_t *trickle);
@ -66,6 +69,9 @@ void trickle_reset_timer(trickle_t *trickle);
/**
* @brief start the trickle timer
*
* @pre `Imin > 0`
* @pre `(Imin << Imax) < (UINT32_MAX / 2)` to avoid overflow of uint32_t
*
* @param[in] pid target thread
* @param[in] trickle trickle timer
* @param[in] msg_type msg_t.type for messages
@ -93,6 +99,8 @@ void trickle_increment_counter(trickle_t *trickle);
/**
* @brief is called after the interval is over and calculates the next interval
*
* @pre `(trickle->I > 0)` required for trickle algorithm to work
*
* @param[in] trickle trickle timer
*/
void trickle_interval(trickle_t *trickle);

View File

@ -18,7 +18,7 @@
#include "random.h"
#include "trickle.h"
#define ENABLE_DEBUG (0)
#define ENABLE_DEBUG (0)
#include "debug.h"
void trickle_callback(trickle_t *trickle)
@ -33,13 +33,14 @@ void trickle_callback(trickle_t *trickle)
void trickle_interval(trickle_t *trickle)
{
assert(trickle->I > 0);
uint32_t old_interval = trickle->I;
uint32_t max_interval = trickle->Imin << trickle->Imax;
uint32_t diff = old_interval - trickle->t;
trickle->I *= 2;
if ((trickle->I == 0) || (trickle->I > max_interval)) {
if (trickle->I > max_interval) {
trickle->I = max_interval;
old_interval = max_interval / 2;
}
@ -47,24 +48,29 @@ void trickle_interval(trickle_t *trickle)
DEBUG("trickle: I == %" PRIu32 ", diff == %" PRIu32 "\n", trickle->I, diff);
trickle->c = 0;
/* old_interval == trickle->I / 2 */
trickle->t = random_uint32_range(old_interval, trickle->I);
trickle->msg_time = (trickle->t + diff) * MS_PER_SEC;
xtimer_set_msg64(&trickle->msg_timer, trickle->msg_time, &trickle->msg,
trickle->pid);
}
void trickle_reset_timer(trickle_t *trickle)
{
assert(trickle->I > trickle->Imin);
trickle_stop(trickle);
trickle_start(trickle->pid, trickle, trickle->msg.type, trickle->Imin,
trickle->Imax, trickle->k);
trickle->I = trickle->Imin;
trickle_interval(trickle);
}
void trickle_start(kernel_pid_t pid, trickle_t *trickle, uint16_t msg_type,
uint32_t Imin, uint8_t Imax, uint8_t k)
{
assert(Imin > 0);
assert((Imin << Imax) < (UINT32_MAX / 2));
trickle->pid = pid;
trickle->c = 0;