-
Notifications
You must be signed in to change notification settings - Fork 2
/
example-libp.c
148 lines (117 loc) · 4.02 KB
/
example-libp.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
* \file
* Example of how the LIBP paradigm works.
* \author
* Lutando Ngqakaza <lutando.ngqakaza@gmail.com>
*/
#include "contiki.h"
#include "lib/random.h"
#include "net/rime.h"
#include "libp.h"
#include "dev/leds.h"
#include "dev/button-sensor.h"
#include "tree.h"
#include "node.h"
#include "queue.h"
#include "net/netstack.h"
#include <stdio.h>
#define BEACONING_PERIOD 30
#define CHANNEL 130
static struct libp_conn lc;
static int is_sink = 0;
/*---------------------------------------------------------------------------*/
PROCESS(example_libp_process, "Test LIBP process");
PROCESS(gateway_monitoring_process, "Gateway Monitoring Process");
AUTOSTART_PROCESSES(&example_libp_process);
/*---------------------------------------------------------------------------*/
static void
recv(const rimeaddr_t *originator, uint8_t seqno, uint8_t hops)
{
printf("Sink got message from %d.%d, seqno %d, hops %d: len %d '%s'\n",
originator->u8[0], originator->u8[1],
seqno, hops,
packetbuf_datalen(),
(char *)packetbuf_dataptr());
}
/*---------------------------------------------------------------------------*/
static const struct collect_callbacks callbacks = { recv };
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(example_libp_process, ev, data)
{
static struct etimer periodic;
static struct etimer et;
PROCESS_BEGIN();
libp_open(&lc, CHANNEL, LIBP_ROUTER, &callbacks);
if(rimeaddr_node_addr.u8[0] == 1 &&
rimeaddr_node_addr.u8[1] == 0)
{
clock_time_t period;
period = CLOCK_SECOND * BEACONING_PERIOD;
printf("I am sink\n");
libp_set_sink(&lc, 1);
is_sink = 1;
libp_set_beacon_period(&lc, period);
tree_init(); //only gateway needs to use the tree methods
process_start(&gateway_monitoring_process, NULL);
}
/* Allow some time for the network to settle. */
etimer_set(&et, 120 * CLOCK_SECOND);
PROCESS_WAIT_UNTIL(etimer_expired(&et));
while(1)
{
/* Send a packet every 30 seconds. */
if(etimer_expired(&periodic))
{
etimer_set(&periodic, CLOCK_SECOND * 30);
etimer_set(&et, random_rand() % (CLOCK_SECOND * 30));
}
PROCESS_WAIT_EVENT();
if(etimer_expired(&et))
{
static rimeaddr_t oldparent;
const rimeaddr_t *parent;
printf("Sending\n");
packetbuf_clear();
parent = libp_parent(&lc);
packetbuf_set_datalen(sprintf(packetbuf_dataptr(),
"%s %d", "Hello", (int)parent->u8[0]) + 1);
libp_send(&lc, 15);
parent = libp_parent(&lc);
if(!rimeaddr_cmp(parent, &oldparent))
{
if(!rimeaddr_cmp(&oldparent, &rimeaddr_null))
{
printf("#L %d 0\n", oldparent.u8[0]);
}
if(!rimeaddr_cmp(parent, &rimeaddr_null))
{
printf("#L %d 1\n", parent->u8[0]);
}
rimeaddr_copy(&oldparent, parent);
}
}
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(gateway_monitoring_process, ev, data)
{
static struct etimer monitor_timer;
static struct etimer wait_timer;
//xint monitoring
PROCESS_BEGIN();
//Allow some time for system to detect weather it is a gateway or not.
etimer_set(&wait_timer, 60 * 2 * CLOCK_SECOND);
PROCESS_WAIT_UNTIL(etimer_expired(&wait_timer));
//If the node is not a sink, turn off this process.
printf("Hi from sink thread\n\n");
while(1)
{
//monitoring loop
// Monitor every 20 seconds
etimer_set(&monitor_timer, CLOCK_SECOND * 20 + random_rand() % (CLOCK_SECOND * 20));
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&monitor_timer));
//monitor
}
PROCESS_END();
}