-
Notifications
You must be signed in to change notification settings - Fork 0
/
actuator.c
354 lines (313 loc) · 9.91 KB
/
actuator.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
* 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
* Testing the broadcast layer in Rime
* \author
* Adam Dunkels <adam@sics.se>
*/
#include "contiki.h"
#include "net/rime/rime.h"
#include "random.h"
#include "dev/button-sensor.h"
#include "dev/leds.h"
#include <stdio.h>
static int flag = 0;
static int check = 0;
static int schedule_flag = 0;
static int nr_neighbors = 0;
/* This is the structure of broadcast messages. */
struct broadcast_message {
uint8_t seqno;
};
/* This is the structure of unicast ping messages. */
struct unicast_message {
uint8_t type;
};
/* This structure holds information about neighbors. */
struct neighbor {
/* The ->next pointer is needed since we are placing these on a
Contiki list. */
struct neighbor *next;
/* The ->addr field holds the Rime address of the neighbor. */
linkaddr_t addr;
/* The ->last_rssi and ->last_lqi fields hold the Received Signal
Strength Indicator (RSSI) and CC2420 Link Quality Indicator (LQI)
values that are received for the incoming broadcast packets. */
uint16_t last_rssi, last_lqi;
/* Each broadcast packet contains a sequence number (seqno). The
->last_seqno field holds the last sequenuce number we saw from
this neighbor. */
uint8_t last_seqno;
/* The ->avg_gap contains the average seqno gap that we have seen
from this neighbor. */
uint32_t avg_seqno_gap;
};
/* This #define defines the maximum amount of neighbors we can remember. */
#define MAX_NEIGHBORS 16
/* This MEMB() definition defines a memory pool from which we allocate
neighbor entries. */
MEMB(neighbors_memb, struct neighbor, MAX_NEIGHBORS);
/* The neighbors_list is a Contiki list that holds the neighbors we
have seen thus far. */
LIST(neighbors_list);
/* These hold the broadcast and unicast structures, respectively. */
static struct broadcast_conn broadcast;
#define MAX_RETRANSMISSIONS 4
#define NUM_HISTORY_ENTRIES 4
/*---------------------------------------------------------------------------*/
PROCESS(initialization_process, "initialization");
PROCESS(schedule_process, "schedule");
AUTOSTART_PROCESSES(&initialization_process, &schedule_process);
/*---------------------------------------------------------------------------*/
/*static void
broadcast_recv(struct broadcast_conn *c, const linkaddr_t *from)
{
char * rcv_bfr;
char * address = "address";
struct neighbor *n;
struct broadcast_message *m;
uint8_t seqno_gap;
}*/
/* This function is called for every incoming unicast packet. */
struct history_entry {
struct history_entry *next;
linkaddr_t addr;
uint8_t seq;
};
LIST(history_table);
MEMB(history_mem, struct history_entry, NUM_HISTORY_ENTRIES);
/*---------------------------------------------------------------------------*/
static void
recv_runicast(struct runicast_conn *c, const linkaddr_t *from, uint8_t seqno)
{
/* OPTIONAL: Sender history */
struct history_entry *e = NULL;
char * receive_msg = "null";
char * address = "address";
struct neighbor *n;
struct broadcast_message *m;
for(e = list_head(history_table); e != NULL; e = e->next) {
if(linkaddr_cmp(&e->addr, from)) {
break;
}
}
if(e == NULL) {
/* Create new history entry */
e = memb_alloc(&history_mem);
if(e == NULL) {
e = list_chop(history_table); /* Remove oldest at full history */
}
linkaddr_copy(&e->addr, from);
e->seq = seqno;
list_push(history_table, e);
} else {
/* Detect duplicate callback */
if(e->seq == seqno) {
printf("runicast message received from %d.%d, seqno %d (DUPLICATE)\n",
from->u8[0], from->u8[1], seqno);
return;
}
/* Update existing history entry */
e->seq = seqno;
}
/* Grab the pointer to the incoming data. */
receive_msg = packetbuf_dataptr();
printf("runicast received with message %s\n",receive_msg);
if ((flag == 1) && (strcmp(receive_msg,address)==0)){
/* The packetbuf_dataptr() returns a pointer to the first data byte
in the received packet. */
m = packetbuf_dataptr();
/* Check if we already know this neighbor. */
for(n = list_head(neighbors_list); n != NULL; n = list_item_next(n)) {
/* We break out of the loop if the address of the neighbor matches
the address of the neighbor from which we received this
broadcast message. */
if(linkaddr_cmp(&n->addr, from)) {
break;
}
}
/* If n is NULL, this neighbor was not found in our list, and we
allocate a new struct neighbor from the neighbors_memb memory
pool. */
if(n == NULL) {
n = memb_alloc(&neighbors_memb);
/* If we could not allocate a new neighbor entry, we give up. We
could have reused an old neighbor entry, but we do not do this
for now. */
if(n == NULL) {
return;
}
/* Initialize the fields. */
linkaddr_copy(&n->addr, from);
/* Place the neighbor on the neighbor list. */
list_add(neighbors_list, n);
nr_neighbors = nr_neighbors + 1;
printf("neighbor added\n");
}
}
receive_msg = "null";
}
static const struct broadcast_callbacks broadcast_call = {};
static struct broadcast_conn broadcast;
static void
sent_runicast(struct runicast_conn *c, const linkaddr_t *to, uint8_t retransmissions)
{
printf("runicast message sent to %d.%d, retransmissions %d\n",
to->u8[0], to->u8[1], retransmissions);
check = 0;
}
static void
timedout_runicast(struct runicast_conn *c, const linkaddr_t *to, uint8_t retransmissions)
{
printf("runicast message timed out when sending to %d.%d, retransmissions %d\n",
to->u8[0], to->u8[1], retransmissions);
check = 1;
}
static const struct runicast_callbacks runicast_callbacks = {recv_runicast,
sent_runicast,
timedout_runicast};
static struct runicast_conn runicast;
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(initialization_process, ev, data)
{
static struct etimer et,dt;
char * msg;
struct neighbor *n;
int randneighbor, i, j, x;
static int k;
PROCESS_EXITHANDLER(runicast_close(&runicast);)
PROCESS_BEGIN();
broadcast_open(&broadcast, 129, &broadcast_call);
while(1) {
if(check == 1)
{
printf("unresponsive neighbor deleted\n");
list_remove(neighbors_list, n);
nr_neighbors--;
check = 0;
}
/* Delay 2-4 seconds */
if (flag == 3)
{
etimer_set(&et, CLOCK_SECOND * 60);
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&dt));
}
else if(flag == 1)
{
etimer_set(&dt, CLOCK_SECOND * 20);
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&dt));
}
else
{
etimer_set(&et, CLOCK_SECOND * 1);
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
}
switch (flag){
case 0:
broadcast_open(&broadcast, 129, &broadcast_call);
packetbuf_copyfrom("req", 4);
broadcast_send(&broadcast);
flag = 1;
printf("broadcast message sent\n");
runicast_open(&runicast, 146, &runicast_callbacks);
broadcast_close(&broadcast);
break;
case 1:
printf("actuator: waited for addresses\n");
if(schedule_flag != 1)
{
flag = 2;
}
else
{
flag = 3;
}
break;
case 2:
if(list_length(neighbors_list) > 0)
{
if(k >= nr_neighbors)
{
k = 0;
}
n = list_head(neighbors_list);
for(j = 0; j < k; j=j+1){
n = list_item_next(n);
}
k++;
msg = "schedule";
printf("sending runicast to %d.%d with message %s\n", n->addr.u8[0], n->addr.u8[1], msg);
packetbuf_copyfrom(msg, (strlen(msg)+1));
runicast_send(&runicast, &n->addr, MAX_RETRANSMISSIONS);
}
if(k == nr_neighbors)
{
flag = 3;
schedule_flag = 1;
printf("schedule sent\n");
}
//printf("%d, %d\n",k,nr_neighbors);
break;
case 3:
flag = 0;
printf("Rerun initialization broadcast \n");
break;
default:
break;
}
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(schedule_process, ev, data)
{
static struct etimer et;
PROCESS_EXITHANDLER(broadcast_close(&broadcast);)
PROCESS_BEGIN();
while(1)
{
etimer_set(&et, CLOCK_SECOND * 1);
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
switch(schedule_flag){
case 1:
//printf("")
break;
case 2:
break;
default:
break;
}
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/