48 #include "lib/random.h"
52 #if TIMESYNCH_CONF_ENABLED
53 static int authority_level;
54 static rtimer_clock_t offset;
56 #define TIMESYNCH_CHANNEL 7
58 struct timesynch_msg {
59 uint8_t authority_level;
61 uint16_t authority_offset;
74 PROCESS(timesynch_process,
"Timesynch process");
76 #define MIN_INTERVAL CLOCK_SECOND * 8
77 #define MAX_INTERVAL CLOCK_SECOND * 60 * 5
82 return authority_level;
88 int old_level = authority_level;
90 authority_level = level;
92 if(old_level != authority_level) {
109 return synched_time - offset;
115 return rtimer_time + offset;
125 adjust_offset(rtimer_clock_t authoritative_time, rtimer_clock_t local_time)
127 offset = authoritative_time - local_time;
131 broadcast_recv(
struct broadcast_conn *c,
const rimeaddr_t *from)
133 struct timesynch_msg msg;
141 if(msg.authority_level < authority_level) {
142 adjust_offset(msg.timestamp + msg.authority_offset,
143 packetbuf_attr(PACKETBUF_ATTR_TIMESTAMP));
148 static struct broadcast_conn broadcast;
152 static struct etimer sendtimer, intervaltimer;
153 static clock_time_t interval;
154 struct timesynch_msg msg;
162 interval = MIN_INTERVAL;
166 etimer_set(&sendtimer, random_rand() % interval);
170 msg.authority_level = authority_level;
172 msg.authority_offset = offset;
173 msg.clock_fine = clock_fine();
175 msg.seconds = clock_seconds();
178 packetbuf_set_attr(PACKETBUF_ATTR_PACKET_TYPE,
179 PACKETBUF_ATTR_PACKET_TYPE_TIMESTAMP);
184 if(interval >= MAX_INTERVAL) {
185 interval = MAX_INTERVAL;