46 #include "lib/random.h"
51 #if TIMESYNCH_CONF_ENABLED
52 static int authority_level;
53 static rtimer_clock_t offset;
55 #define TIMESYNCH_CHANNEL 7
57 struct timesynch_msg {
58 uint8_t authority_level;
60 uint16_t authority_offset;
73 PROCESS(timesynch_process,
"Timesynch process");
75 #define MIN_INTERVAL CLOCK_SECOND * 8
76 #define MAX_INTERVAL CLOCK_SECOND * 60 * 5
81 return authority_level;
87 int old_level = authority_level;
89 authority_level = level;
91 if(old_level != authority_level) {
108 return synched_time - offset;
114 return rtimer_time + offset;
124 adjust_offset(rtimer_clock_t authoritative_time, rtimer_clock_t local_time)
126 offset = authoritative_time - local_time;
130 broadcast_recv(
struct broadcast_conn *c,
const linkaddr_t *from)
132 struct timesynch_msg msg;
140 if(msg.authority_level < authority_level) {
141 adjust_offset(msg.timestamp + msg.authority_offset,
142 packetbuf_attr(PACKETBUF_ATTR_TIMESTAMP));
147 static struct broadcast_conn broadcast;
151 static struct etimer sendtimer, intervaltimer;
152 static clock_time_t interval;
153 struct timesynch_msg msg;
161 interval = MIN_INTERVAL;
169 msg.authority_level = authority_level;
171 msg.authority_offset = offset;
172 msg.clock_fine = clock_fine();
177 packetbuf_set_attr(PACKETBUF_ATTR_PACKET_TYPE,
178 PACKETBUF_ATTR_PACKET_TYPE_TIMESTAMP);
183 if(interval >= MAX_INTERVAL) {
184 interval = MAX_INTERVAL;
int etimer_expired(struct etimer *et)
Check if an event timer has expired.
#define PROCESS_WAIT_UNTIL(c)
Wait for a condition to occur.
int packetbuf_copyfrom(const void *from, uint16_t len)
Copy from external data into the packetbuf.
void broadcast_close(struct broadcast_conn *c)
Close a broadcast connection.
void timesynch_init(void)
Initialize the timesynch module.
#define PROCESS_BEGIN()
Define the beginning of a process.
int timesynch_authority_level(void)
Get the current authority level of the time-synchronized time.
rtimer_clock_t timesynch_rtimer_to_time(rtimer_clock_t rtimer_time)
Get the synchronized equivalent of an rtimer time.
#define NULL
The null pointer.
Header file for a simple time synchronization mechanism
Header file for the Rime stack
rtimer_clock_t timesynch_time(void)
Get the current time-synchronized time.
void timesynch_set_authority_level(int level)
Set the authority level of the current time.
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
#define PROCESS_END()
Define the end of a process.
CCIF clock_time_t clock_time(void)
Get the current clock time.
#define PROCESS_EXITHANDLER(handler)
Specify an action when a process exits.
Callback structure for broadcast.
rtimer_clock_t timesynch_time_to_rtimer(rtimer_clock_t synched_time)
Get the current time-synchronized time, suitable for use with the rtimer module.
#define PROCESS(name, strname)
Declare a process.
int broadcast_send(struct broadcast_conn *c)
Send an identified best-effort broadcast packet.
CCIF unsigned long clock_seconds(void)
Get the current value of the platform seconds.
void process_start(struct process *p, process_data_t data)
Start a process.
#define RTIMER_NOW()
Get the current clock time.
void etimer_set(struct etimer *et, clock_time_t interval)
Set an event timer.
void * packetbuf_dataptr(void)
Get a pointer to the data in the packetbuf.
void broadcast_open(struct broadcast_conn *c, uint16_t channel, const struct broadcast_callbacks *u)
Set up an identified best-effort broadcast connection.
unsigned short random_rand(void)
Generate the next state and return the upper part of it.
rtimer_clock_t timesynch_offset(void)
Get the current time-synchronized offset from the rtimer clock, which is used mainly for debugging...
void process_exit(struct process *p)
Cause a process to exit.