Merge pull request #7161 from cgundogan/pr/trickle_enhancements
trickle: enhancements
This commit is contained in:
commit
89716729c8
@ -156,14 +156,9 @@ extern "C" {
|
||||
#define GNRC_RPL_MSG_TYPE_LIFETIME_UPDATE (0x0900)
|
||||
|
||||
/**
|
||||
* @brief Message type for trickle intervals
|
||||
* @brief Message type for trickle
|
||||
*/
|
||||
#define GNRC_RPL_MSG_TYPE_TRICKLE_INTERVAL (0x0901)
|
||||
|
||||
/**
|
||||
* @brief Message type for trickle callbacks
|
||||
*/
|
||||
#define GNRC_RPL_MSG_TYPE_TRICKLE_CALLBACK (0x0902)
|
||||
#define GNRC_RPL_MSG_TYPE_TRICKLE_MSG (0x0901)
|
||||
|
||||
/**
|
||||
* @brief Message type for handling DAO sending
|
||||
|
||||
@ -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
|
||||
|
||||
@ -120,9 +120,9 @@ gnrc_rpl_instance_t *gnrc_rpl_root_init(uint8_t instance_id, ipv6_addr_t *dodag_
|
||||
dodag->dio_opts |= GNRC_RPL_REQ_DIO_OPT_PREFIX_INFO;
|
||||
#endif
|
||||
|
||||
trickle_start(gnrc_rpl_pid, &dodag->trickle, GNRC_RPL_MSG_TYPE_TRICKLE_INTERVAL,
|
||||
GNRC_RPL_MSG_TYPE_TRICKLE_CALLBACK, (1 << dodag->dio_min),
|
||||
dodag->dio_interval_doubl, dodag->dio_redun);
|
||||
trickle_start(gnrc_rpl_pid, &dodag->trickle, GNRC_RPL_MSG_TYPE_TRICKLE_MSG,
|
||||
(1 << dodag->dio_min), dodag->dio_interval_doubl,
|
||||
dodag->dio_redun);
|
||||
|
||||
return inst;
|
||||
}
|
||||
@ -216,15 +216,8 @@ static void *_event_loop(void *args)
|
||||
DEBUG("RPL: GNRC_RPL_MSG_TYPE_LIFETIME_UPDATE received\n");
|
||||
_update_lifetime();
|
||||
break;
|
||||
case GNRC_RPL_MSG_TYPE_TRICKLE_INTERVAL:
|
||||
DEBUG("RPL: GNRC_RPL_MSG_TYPE_TRICKLE_INTERVAL received\n");
|
||||
trickle = msg.content.ptr;
|
||||
if (trickle && (trickle->callback.func != NULL)) {
|
||||
trickle_interval(trickle);
|
||||
}
|
||||
break;
|
||||
case GNRC_RPL_MSG_TYPE_TRICKLE_CALLBACK:
|
||||
DEBUG("RPL: GNRC_RPL_MSG_TYPE_TRICKLE_CALLBACK received\n");
|
||||
case GNRC_RPL_MSG_TYPE_TRICKLE_MSG:
|
||||
DEBUG("RPL: GNRC_RPL_MSG_TYPE_TRICKLE_MSG received\n");
|
||||
trickle = msg.content.ptr;
|
||||
if (trickle && (trickle->callback.func != NULL)) {
|
||||
trickle_callback(trickle);
|
||||
|
||||
@ -551,9 +551,9 @@ void gnrc_rpl_recv_DIO(gnrc_rpl_dio_t *dio, kernel_pid_t iface, ipv6_addr_t *src
|
||||
}
|
||||
|
||||
gnrc_rpl_delay_dao(dodag);
|
||||
trickle_start(gnrc_rpl_pid, &dodag->trickle, GNRC_RPL_MSG_TYPE_TRICKLE_INTERVAL,
|
||||
GNRC_RPL_MSG_TYPE_TRICKLE_CALLBACK, (1 << dodag->dio_min),
|
||||
dodag->dio_interval_doubl, dodag->dio_redun);
|
||||
trickle_start(gnrc_rpl_pid, &dodag->trickle, GNRC_RPL_MSG_TYPE_TRICKLE_MSG,
|
||||
(1 << dodag->dio_min), dodag->dio_interval_doubl,
|
||||
dodag->dio_redun);
|
||||
|
||||
gnrc_rpl_parent_update(dodag, parent);
|
||||
return;
|
||||
|
||||
@ -162,9 +162,9 @@ int _gnrc_rpl_trickle_start(char *arg1)
|
||||
return 1;
|
||||
}
|
||||
|
||||
trickle_start(gnrc_rpl_pid, &(inst->dodag.trickle), GNRC_RPL_MSG_TYPE_TRICKLE_INTERVAL,
|
||||
GNRC_RPL_MSG_TYPE_TRICKLE_CALLBACK, (1 << inst->dodag.dio_min),
|
||||
inst->dodag.dio_interval_doubl, inst->dodag.dio_redun);
|
||||
trickle_start(gnrc_rpl_pid, &(inst->dodag.trickle), GNRC_RPL_MSG_TYPE_TRICKLE_MSG,
|
||||
(1 << inst->dodag.dio_min), inst->dodag.dio_interval_doubl,
|
||||
inst->dodag.dio_redun);
|
||||
|
||||
printf("success: started trickle timer of DODAG (%s) from instance (%d)\n",
|
||||
ipv6_addr_to_str(addr_str, &(inst->dodag.dodag_id), sizeof(addr_str)),
|
||||
@ -258,7 +258,7 @@ int _gnrc_rpl_dodag_show(void)
|
||||
gnrc_rpl_dodag_t *dodag = NULL;
|
||||
char addr_str[IPV6_ADDR_MAX_STR_LEN];
|
||||
int8_t cleanup;
|
||||
uint64_t tc, ti, xnow = xtimer_now_usec64();
|
||||
uint64_t tc, xnow = xtimer_now_usec64();
|
||||
|
||||
for (uint8_t i = 0; i < GNRC_RPL_INSTANCES_NUMOF; ++i) {
|
||||
if (gnrc_rpl_instances[i].state == 0) {
|
||||
@ -272,23 +272,19 @@ int _gnrc_rpl_dodag_show(void)
|
||||
gnrc_rpl_instances[i].mop, gnrc_rpl_instances[i].of->ocp,
|
||||
gnrc_rpl_instances[i].min_hop_rank_inc, gnrc_rpl_instances[i].max_rank_inc);
|
||||
|
||||
tc = (((uint64_t) dodag->trickle.msg_callback_timer.long_target << 32)
|
||||
| dodag->trickle.msg_callback_timer.target) - xnow;
|
||||
tc = (((uint64_t) dodag->trickle.msg_timer.long_target << 32)
|
||||
| dodag->trickle.msg_timer.target) - xnow;
|
||||
tc = (int64_t) tc < 0 ? 0 : tc / US_PER_SEC;
|
||||
|
||||
ti = (((uint64_t) dodag->trickle.msg_interval_timer.long_target << 32)
|
||||
| dodag->trickle.msg_interval_timer.target) - xnow;
|
||||
ti = (int64_t) ti < 0 ? 0 : ti / US_PER_SEC;
|
||||
|
||||
cleanup = dodag->instance->cleanup < 0 ? 0 : dodag->instance->cleanup;
|
||||
|
||||
printf("\tdodag [%s | R: %d | OP: %s | PIO: %s | CL: %ds | "
|
||||
"TR(I=[%d,%d], k=%d, c=%d, TC=%" PRIu32 "s, TI=%" PRIu32 "s)]\n",
|
||||
"TR(I=[%d,%d], k=%d, c=%d, TC=%" PRIu32 "s)]\n",
|
||||
ipv6_addr_to_str(addr_str, &dodag->dodag_id, sizeof(addr_str)),
|
||||
dodag->my_rank, (dodag->node_status == GNRC_RPL_LEAF_NODE ? "Leaf" : "Router"),
|
||||
((dodag->dio_opts & GNRC_RPL_REQ_DIO_OPT_PREFIX_INFO) ? "on" : "off"),
|
||||
(int) cleanup, (1 << dodag->dio_min), dodag->dio_interval_doubl, dodag->trickle.k,
|
||||
dodag->trickle.c, (uint32_t) (tc & 0xFFFFFFFF), (uint32_t) (ti & 0xFFFFFFFF));
|
||||
dodag->trickle.c, (uint32_t) (tc & 0xFFFFFFFF));
|
||||
|
||||
#ifdef MODULE_GNRC_RPL_P2P
|
||||
if (dodag->instance->mop == GNRC_RPL_P2P_MOP) {
|
||||
|
||||
@ -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)
|
||||
|
||||
6
tests/trickle/Makefile
Normal file
6
tests/trickle/Makefile
Normal file
@ -0,0 +1,6 @@
|
||||
APPLICATION = trickle
|
||||
include ../Makefile.tests_common
|
||||
|
||||
USEMODULE += trickle
|
||||
|
||||
include $(RIOTBASE)/Makefile.include
|
||||
67
tests/trickle/main.c
Normal file
67
tests/trickle/main.c
Normal file
@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright (C) 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
|
||||
* directory for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @ingroup tests
|
||||
* @{
|
||||
*
|
||||
* @file
|
||||
* @brief Trickle test application
|
||||
*
|
||||
* @author Cenk Gündoğan <cenk.guendogan@haw-hamburg.de>
|
||||
*
|
||||
* @}
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "trickle.h"
|
||||
#include "thread.h"
|
||||
#include "msg.h"
|
||||
|
||||
#define Q_LEN (8)
|
||||
#define TRICKLE_MSG (0xfeef)
|
||||
#define TR_IMIN (8)
|
||||
#define TR_IDOUBLINGS (20)
|
||||
#define TR_REDCONST (10)
|
||||
|
||||
static msg_t _msg_q[Q_LEN];
|
||||
|
||||
static void callback(void *args)
|
||||
{
|
||||
(void) args;
|
||||
printf("now: %" PRIu32 "\n", xtimer_now_usec());
|
||||
return;
|
||||
}
|
||||
|
||||
static trickle_t trickle = { .callback.func = &callback,
|
||||
.callback.args = NULL };
|
||||
|
||||
int main(void)
|
||||
{
|
||||
msg_t msg;
|
||||
|
||||
msg_init_queue(_msg_q, Q_LEN);
|
||||
|
||||
trickle_start(sched_active_pid, &trickle, TRICKLE_MSG, TR_IMIN,
|
||||
TR_IDOUBLINGS, TR_REDCONST);
|
||||
|
||||
while (1) {
|
||||
msg_receive(&msg);
|
||||
|
||||
switch (msg.type) {
|
||||
case TRICKLE_MSG:
|
||||
trickle_callback((trickle_t *) msg.content.ptr);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user