2008-01-14 14:22:16 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2007, Swedish Institute of Computer Science.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of the Contiki operating system.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* A simple time synchronization mechanism
|
|
|
|
* \author
|
|
|
|
* Adam Dunkels <adam@sics.se>
|
|
|
|
*/
|
|
|
|
|
2014-11-08 00:15:42 +00:00
|
|
|
/**
|
|
|
|
* \addtogroup timesynch
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2010-12-16 22:47:38 +00:00
|
|
|
#include "contiki.h"
|
|
|
|
#include "lib/random.h"
|
2013-12-12 19:50:07 +00:00
|
|
|
#include "net/rime/rime.h"
|
2010-12-16 22:47:38 +00:00
|
|
|
#include "net/rime/timesynch.h"
|
2012-03-06 11:33:47 +00:00
|
|
|
#include <string.h>
|
2008-01-14 14:22:16 +00:00
|
|
|
|
2009-03-17 09:57:01 +00:00
|
|
|
#if TIMESYNCH_CONF_ENABLED
|
2008-01-14 14:22:16 +00:00
|
|
|
static int authority_level;
|
|
|
|
static rtimer_clock_t offset;
|
|
|
|
|
2010-12-16 22:47:38 +00:00
|
|
|
#define TIMESYNCH_CHANNEL 7
|
|
|
|
|
|
|
|
struct timesynch_msg {
|
|
|
|
uint8_t authority_level;
|
|
|
|
uint8_t dummy;
|
|
|
|
uint16_t authority_offset;
|
|
|
|
uint16_t clock_fine;
|
|
|
|
clock_time_t clock_time;
|
|
|
|
uint32_t seconds;
|
|
|
|
/* We need some padding so that the radio has time to update the
|
|
|
|
timestamp at the end of the packet, after the transmission has
|
|
|
|
started. */
|
|
|
|
uint8_t padding[16];
|
|
|
|
|
|
|
|
/* The timestamp must be the last two bytes. */
|
|
|
|
uint16_t timestamp;
|
|
|
|
};
|
|
|
|
|
|
|
|
PROCESS(timesynch_process, "Timesynch process");
|
|
|
|
|
|
|
|
#define MIN_INTERVAL CLOCK_SECOND * 8
|
|
|
|
#define MAX_INTERVAL CLOCK_SECOND * 60 * 5
|
2008-01-14 14:22:16 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
int
|
|
|
|
timesynch_authority_level(void)
|
|
|
|
{
|
|
|
|
return authority_level;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
timesynch_set_authority_level(int level)
|
|
|
|
{
|
2010-12-16 22:47:38 +00:00
|
|
|
int old_level = authority_level;
|
|
|
|
|
2008-01-14 14:22:16 +00:00
|
|
|
authority_level = level;
|
2010-12-16 22:47:38 +00:00
|
|
|
|
|
|
|
if(old_level != authority_level) {
|
|
|
|
/* Restart the timesynch process to restart with a low
|
|
|
|
transmission interval. */
|
|
|
|
process_exit(×ynch_process);
|
|
|
|
process_start(×ynch_process, NULL);
|
|
|
|
}
|
2008-01-14 14:22:16 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
rtimer_clock_t
|
|
|
|
timesynch_time(void)
|
|
|
|
{
|
2009-12-09 18:08:26 +00:00
|
|
|
return RTIMER_NOW() + offset;
|
2008-01-14 14:22:16 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
rtimer_clock_t
|
|
|
|
timesynch_time_to_rtimer(rtimer_clock_t synched_time)
|
|
|
|
{
|
|
|
|
return synched_time - offset;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
rtimer_clock_t
|
2008-01-23 15:07:05 +00:00
|
|
|
timesynch_rtimer_to_time(rtimer_clock_t rtimer_time)
|
|
|
|
{
|
|
|
|
return rtimer_time + offset;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
rtimer_clock_t
|
2008-01-14 14:22:16 +00:00
|
|
|
timesynch_offset(void)
|
|
|
|
{
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
|
|
|
adjust_offset(rtimer_clock_t authoritative_time, rtimer_clock_t local_time)
|
|
|
|
{
|
2010-12-16 22:47:38 +00:00
|
|
|
offset = authoritative_time - local_time;
|
2008-01-14 14:22:16 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-01-14 14:50:01 +00:00
|
|
|
static void
|
2013-12-12 22:58:52 +00:00
|
|
|
broadcast_recv(struct broadcast_conn *c, const linkaddr_t *from)
|
2008-01-14 14:22:16 +00:00
|
|
|
{
|
2010-12-16 22:47:38 +00:00
|
|
|
struct timesynch_msg msg;
|
|
|
|
|
|
|
|
memcpy(&msg, packetbuf_dataptr(), sizeof(msg));
|
|
|
|
|
|
|
|
/* We check the authority level of the sender of the incoming
|
2008-01-14 14:50:01 +00:00
|
|
|
packet. If the sending node has a lower authority level than we
|
|
|
|
have, we synchronize to the time of the sending node and set our
|
|
|
|
own authority level to be one more than the sending node. */
|
2010-12-16 22:47:38 +00:00
|
|
|
if(msg.authority_level < authority_level) {
|
|
|
|
adjust_offset(msg.timestamp + msg.authority_offset,
|
|
|
|
packetbuf_attr(PACKETBUF_ATTR_TIMESTAMP));
|
|
|
|
timesynch_set_authority_level(msg.authority_level + 1);
|
2008-01-14 14:22:16 +00:00
|
|
|
}
|
|
|
|
}
|
2010-12-16 22:47:38 +00:00
|
|
|
static const struct broadcast_callbacks broadcast_call = {broadcast_recv};
|
|
|
|
static struct broadcast_conn broadcast;
|
2008-01-14 14:22:16 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2010-12-16 22:47:38 +00:00
|
|
|
PROCESS_THREAD(timesynch_process, ev, data)
|
2008-01-14 14:22:16 +00:00
|
|
|
{
|
2010-12-16 22:47:38 +00:00
|
|
|
static struct etimer sendtimer, intervaltimer;
|
|
|
|
static clock_time_t interval;
|
|
|
|
struct timesynch_msg msg;
|
|
|
|
|
|
|
|
PROCESS_EXITHANDLER(broadcast_close(&broadcast);)
|
|
|
|
|
|
|
|
PROCESS_BEGIN();
|
|
|
|
|
|
|
|
broadcast_open(&broadcast, TIMESYNCH_CHANNEL, &broadcast_call);
|
|
|
|
|
|
|
|
interval = MIN_INTERVAL;
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
etimer_set(&intervaltimer, interval);
|
|
|
|
etimer_set(&sendtimer, random_rand() % interval);
|
|
|
|
|
|
|
|
PROCESS_WAIT_UNTIL(etimer_expired(&sendtimer));
|
|
|
|
|
|
|
|
msg.authority_level = authority_level;
|
|
|
|
msg.dummy = 0;
|
|
|
|
msg.authority_offset = offset;
|
|
|
|
msg.clock_fine = clock_fine();
|
|
|
|
msg.clock_time = clock_time();
|
|
|
|
msg.seconds = clock_seconds();
|
|
|
|
msg.timestamp = 0;
|
|
|
|
packetbuf_copyfrom(&msg, sizeof(msg));
|
|
|
|
packetbuf_set_attr(PACKETBUF_ATTR_PACKET_TYPE,
|
|
|
|
PACKETBUF_ATTR_PACKET_TYPE_TIMESTAMP);
|
|
|
|
broadcast_send(&broadcast);
|
|
|
|
|
|
|
|
PROCESS_WAIT_UNTIL(etimer_expired(&intervaltimer));
|
|
|
|
interval *= 2;
|
|
|
|
if(interval >= MAX_INTERVAL) {
|
|
|
|
interval = MAX_INTERVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PROCESS_END();
|
2008-01-14 14:22:16 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2008-01-14 14:50:01 +00:00
|
|
|
void
|
|
|
|
timesynch_init(void)
|
2008-01-14 14:22:16 +00:00
|
|
|
{
|
2010-12-16 22:47:38 +00:00
|
|
|
process_start(×ynch_process, NULL);
|
2008-01-14 14:22:16 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2009-02-20 21:23:22 +00:00
|
|
|
#endif /* TIMESYNCH_CONF_ENABLED */
|
2008-01-14 14:22:16 +00:00
|
|
|
/** @} */
|