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

trickle: enhancements

This commit is contained in:
Cenk Gündoğan 2017-06-08 21:27:11 +02:00
parent 2a2855631e
commit e0fbb14963
2 changed files with 36 additions and 45 deletions

View File

@ -2,6 +2,7 @@
* Trickle constants and prototypes
*
* Copyright (C) 2013, 2014 INRIA.
* 2017 HAW Hamburg
*
* 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
@ -12,14 +13,11 @@
* @defgroup sys_trickle Trickle Timer
* @ingroup sys
* @{
*/
/**
* @file
* @brief Implementation of a generic Trickle Algorithm (RFC 6206)
*
* @author Eric Engel <eric.engel@fu-berlin.de>
* @author Cenk Gündoğan <cnkgndgn@gmail.com>
* @author Cenk Gündoğan <cenk.guendogan@haw-hamburg.de>
*/
#ifndef TRICKLE_H
@ -32,10 +30,13 @@
#include "xtimer.h"
#include "thread.h"
/** @brief a generic callback function with arguments that is called by trickle periodically */
/**
* @brief a generic callback function with arguments that is called by
* trickle periodically
*/
typedef struct {
void (*func)(void *); /**< a generic callback function pointer */
void *args; /**< a generic parameter for the callback function pointer */
void (*func)(void *); /**< callback function pointer */
void *args; /**< callback function arguments */
} trickle_callback_t;
/** @brief all state variables for a trickle timer */
@ -49,14 +50,10 @@ typedef struct {
kernel_pid_t pid; /**< pid of trickles target thread */
trickle_callback_t callback; /**< the callback function and parameter that trickle is calling
after each interval */
msg_t msg_interval; /**< the msg_t to use for intervals */
uint64_t msg_interval_time; /**< interval in ms */
xtimer_t msg_interval_timer; /**< xtimer to send a msg_t to the target thread
msg_t msg; /**< the msg_t to use for intervals */
uint64_t msg_time; /**< interval in ms */
xtimer_t msg_timer; /**< xtimer to send a msg_t to the target thread
for a new interval */
msg_t msg_callback; /**< the msg_t to use for callbacks */
uint64_t msg_callback_time; /**< callback interval in ms */
xtimer_t msg_callback_timer; /**< xtimer to send a msg_t to the target thread
for a callback */
} trickle_t;
/**
@ -71,14 +68,13 @@ void trickle_reset_timer(trickle_t *trickle);
*
* @param[in] pid target thread
* @param[in] trickle trickle timer
* @param[in] interval_msg_type msg_t.type for interval messages
* @param[in] callback_msg_type msg_t.type for callback messages
* @param[in] msg_type msg_t.type for messages
* @param[in] Imin minimum interval
* @param[in] Imax maximum interval
* @param[in] k redundancy constant
*/
void trickle_start(kernel_pid_t pid, trickle_t *trickle, uint16_t interval_msg_type,
uint16_t callback_msg_type, uint32_t Imin, uint8_t Imax, uint8_t k);
void trickle_start(kernel_pid_t pid, trickle_t *trickle, uint16_t msg_type,
uint32_t Imin, uint8_t Imax, uint8_t k);
/**
* @brief stops the trickle timer

View File

@ -2,6 +2,7 @@
* Trickle implementation
*
* Copyright (C) 2013, 2014 INRIA.
* 2017 HAW Hamburg
*
* 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
@ -10,13 +11,9 @@
/**
* @author Eric Engel <eric.engel@fu-berlin.de>
* @author Cenk Gündoğan <cnkgndgn@gmail.com>
* @author Cenk Gündoğan <cenk.guendogan@haw-hamburg.de>
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "inttypes.h"
#include "random.h"
#include "trickle.h"
@ -30,42 +27,42 @@ void trickle_callback(trickle_t *trickle)
if ((trickle->c < trickle->k) || (trickle->k == 0)) {
(*trickle->callback.func)(trickle->callback.args);
}
trickle_interval(trickle);
}
void trickle_interval(trickle_t *trickle)
{
uint32_t max_interval;
uint32_t old_interval = trickle->I;
uint32_t max_interval = trickle->Imin << trickle->Imax;
uint32_t diff = old_interval - trickle->t;
trickle->I = trickle->I * 2;
max_interval = trickle->Imin << trickle->Imax;
trickle->I *= 2;
if ((trickle->I == 0) || (trickle->I > max_interval)) {
trickle->I = max_interval;
}
DEBUG("trickle: I == %" PRIu32 "\n", trickle->I);
DEBUG("trickle: I == %" PRIu32 ", diff == %" PRIu32 "\n", trickle->I, diff);
trickle->c = 0;
trickle->t = (trickle->I / 2) + random_uint32_range(0, (trickle->I / 2) + 1);
trickle->t = random_uint32_range(old_interval, trickle->I);
trickle->msg_callback_time = trickle->t * MS_PER_SEC;
xtimer_set_msg64(&trickle->msg_callback_timer, trickle->msg_callback_time,
&trickle->msg_callback, trickle->pid);
trickle->msg_time = (trickle->t + diff) * MS_PER_SEC;
xtimer_set_msg64(&trickle->msg_timer, trickle->msg_time, &trickle->msg,
trickle->pid);
trickle->msg_interval_time = trickle->I * MS_PER_SEC;
xtimer_set_msg64(&trickle->msg_interval_timer, trickle->msg_interval_time,
&trickle->msg_interval, trickle->pid);
}
void trickle_reset_timer(trickle_t *trickle)
{
trickle_stop(trickle);
trickle_start(trickle->pid, trickle, trickle->msg_interval.type, trickle->msg_callback.type,
trickle->Imin, trickle->Imax, trickle->k);
trickle_start(trickle->pid, trickle, trickle->msg.type, trickle->Imin,
trickle->Imax, trickle->k);
}
void trickle_start(kernel_pid_t pid, trickle_t *trickle, uint16_t interval_msg_type,
uint16_t callback_msg_type, uint32_t Imin, uint8_t Imax, uint8_t k)
void trickle_start(kernel_pid_t pid, trickle_t *trickle, uint16_t msg_type,
uint32_t Imin, uint8_t Imax, uint8_t k)
{
trickle->pid = pid;
@ -73,20 +70,18 @@ void trickle_start(kernel_pid_t pid, trickle_t *trickle, uint16_t interval_msg_t
trickle->k = k;
trickle->Imin = Imin;
trickle->Imax = Imax;
trickle->I = trickle->Imin + random_uint32_range(0, 4 * trickle->Imin);
trickle->I = trickle->t = random_uint32_range(trickle->Imin,
4 * trickle->Imin);
trickle->pid = pid;
trickle->msg_interval.content.ptr = trickle;
trickle->msg_interval.type = interval_msg_type;
trickle->msg_callback.content.ptr = trickle;
trickle->msg_callback.type = callback_msg_type;
trickle->msg.content.ptr = trickle;
trickle->msg.type = msg_type;
trickle_interval(trickle);
}
void trickle_stop(trickle_t *trickle)
{
xtimer_remove(&trickle->msg_interval_timer);
xtimer_remove(&trickle->msg_callback_timer);
xtimer_remove(&trickle->msg_timer);
}
void trickle_increment_counter(trickle_t *trickle)