This repository has been archived by the owner on Sep 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dis.c
65 lines (53 loc) · 1.56 KB
/
dis.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "app.h"
#include "dis.h"
#define mydis (&mydata->dis)
/* Minimum time dedicated to the discovery protocol */
#define DIS_MIN_TIME (10 * KILO_TICKS_PER_SEC)
/* Time that should be elapsed without new discoveries before quit */
#define DIS_QUIET_TIME (10 * KILO_TICKS_PER_SEC)
/* Time, in kilo ticks, between discovery messages */
#define DIS_RAND_OFF (rand() % (3 * KILO_TICKS_PER_SEC))
static void neighbor_add(addr_t addr)
{
uint8_t nneigh = mydata->nneigh;
app_neighbor_add(addr);
if (mydata->nneigh > nneigh) {
mydis->last = kilo_ticks;
app_neighbor_print();
}
}
void dis_loop(void)
{
addr_t src;
if (mydis->start == 0) {
mydis->start = kilo_ticks; /*First run */
mydis->next = kilo_ticks + DIS_RAND_OFF;
}
if (kilo_ticks - mydis->start >= DIS_MIN_TIME &&
kilo_ticks - mydis->last >= DIS_QUIET_TIME) {
mydis->state = DIS_STATE_DONE;
COLOR_APP(GREEN);
TRACE_APP("DIS DONE\n");
return;
}
if (mydis->state == DIS_STATE_IDLE) {
if (kilo_ticks >= mydis->next) {
mydis->state = DIS_STATE_ACTIVE;
COLOR_APP(RED);
app_send(TPL_BROADCAST_ADDR, NULL, 0);
mydis->next = kilo_ticks + DIS_RAND_OFF;
}
} else {
mydis->state = DIS_STATE_IDLE;
COLOR_APP(WHITE);
}
if (app_recv(&src, NULL, NULL) == 0)
neighbor_add(src);
}
void dis_init(void)
{
memset(mydis, 0, sizeof(*mydis));
mydis->start = kilo_ticks;
mydis->state = DIS_STATE_IDLE;
COLOR_APP(WHITE);
}