-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNode.nc
More file actions
411 lines (343 loc) · 10.8 KB
/
Node.nc
File metadata and controls
411 lines (343 loc) · 10.8 KB
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
* ANDES Lab - University of California, Merced
This class provides the basic functions of a network node.
*
* @author UCM ANDES Lab
* @date 2013/09/03
*Adam Pluguez CSE160 Project2 Updated 2/21/19
*/
#include <Timer.h>
#include "includes/command.h"
#include "includes/packet.h"
#include "includes/CommandMsg.h"
#include "includes/sendInfo.h"
#include "includes/channels.h"
#include "includes/moteN.h"
module Node{
uses interface Boot;
uses interface SplitControl as AMControl;
uses interface Receive;
uses interface SimpleSend as Sender;
uses interface CommandHandler;
uses interface NeighborDiscovery;
uses interface Flooding;
uses interface Routing;
//Added stuff
uses interface Timer<TMilli> as neighborTimer;
uses interface List<moteN> as neighborList;
uses interface List<linkState> as linkStateList;
uses interface List<pack> as sentList;
}
implementation{
uint8_t sequence = 0;
pack sendPackage;
// Prototypes
void makePack(pack *Package, uint16_t src, uint16_t dest, uint16_t TTL, uint16_t Protocol, uint16_t seq, uint8_t *payload, uint8_t length);
//Added
bool checkSentList(pack *Package);
void updateLinkList();
void removeNeighbor(uint16_t id);
void sendLinkState();
bool inList(uint16_t id);
void removeLink(uint16_t id);
moteN* list;
linkState listL[20];
linkState empty;
event void Boot.booted(){
call AMControl.start();
dbg(GENERAL_CHANNEL, "Booted\n");
call NeighborDiscovery.run();
//sendLinkState();
call neighborTimer.startPeriodic(50000);
}
event void AMControl.startDone(error_t err){
if(err == SUCCESS){
dbg(GENERAL_CHANNEL, "Radio On\n");
//call NeighborDiscovery.run();
//call Routing.createTable();
}else{
//Retry until successful
call AMControl.start();
}
}
event void AMControl.stopDone(error_t err){}
event void neighborTimer.fired(){
uint16_t i;
//char* payload = "";
updateLinkList();
/*
moteN neighbor, temp;
uint16_t i ,listSize = call neighborList.size();
for(i=0;i< listSize;i++){
temp = call neighborList.get(i);
temp.tls--;
removeNeighbor(i);
//call neighborList.remove(i);
call neighborList.pushback(temp);
}
for(i=0;i< listSize;i++){
temp = call neighborList.get(i);
if(temp.tls <= 0){
//call neighborList.remove(i);
removeNeighbor(i);
listSize--;
i--;
}
}
*/
//Send out Discovery Packet, start neighbor list creation
//makePack(&sendPackage, TOS_NODE_ID, AM_BROADCAST_ADDR, 2, PROTOCOL_PING, 50, (uint8_t*)payload, PACKET_MAX_PAYLOAD_SIZE);
//call sentList.pushback(sendPackage);
//call Sender.send(sendPackage, AM_BROADCAST_ADDR);
// for(i=0;i< MAX_NODES;i++){
// listL[i].id == 0;
//}
sendLinkState();
}
event message_t* Receive.receive(message_t* msg, void* payload, uint8_t len){
//dbg(GENERAL_CHANNEL, "Packet Received\n");
moteN found;
moteN temp;
bool inNList = FALSE;
uint16_t listSize;
uint16_t i;
linkState* Payload;
if(len==sizeof(pack)){
pack* myMsg=(pack*) payload;
if(!(checkSentList(myMsg)) && myMsg->TTL != 0 && myMsg->dest == AM_BROADCAST_ADDR ){
/*if(myMsg->protocol == PROTOCOL_PING){
makePack(&sendPackage, TOS_NODE_ID, AM_BROADCAST_ADDR, --myMsg->TTL, PROTOCOL_PINGREPLY, ++myMsg->seq,(uint8_t*) myMsg->payload, PACKET_MAX_PAYLOAD_SIZE);
call sentList.pushback(sendPackage);
call Sender.send(sendPackage, myMsg->src);
return msg;
}
else if( myMsg->protocol == PROTOCOL_PINGREPLY){
//dbg(NEIGHBOR_CHANNEL,"revievedNeighbor packet at %d, from %d\n" ,TOS_NODE_ID,myMsg->src);
found.id = myMsg->src;
found.tls = 5;
if(!(call neighborList.isEmpty())){
listSize = call neighborList.size();
for(i = 0; i < listSize;i++){
temp = call neighborList.popfront();
if(temp.id == found.id){
inNList = TRUE;
temp.tls++;
}
call neighborList.pushback(temp);
}
}
}
else */if( myMsg->protocol == PROTOCOL_LINKSTATE){
Payload = (linkState*) myMsg->payload;
for(i =0; i < Payload->size;i++){
temp = Payload->neighborList[i];
//dbg(ROUTING_CHANNEL,"node %d neighbor: %d \n",TOS_NODE_ID, temp.id);
}
//update link linkStateList
//if(listL[myMsg->src].tls == 0){
//listL[myMsg->src].tls = 5;
//dbg(ROUTING_CHANNEL,"sent fromnode %d \n",myMsg->src);
// }
//removeLink(Payload->id);
//if(!inList(Payload->id)){
//if(Payload->tls < 5)
//Payload->tls = Payload->tls+1 ;
//listL[myMsg->src] = *Payload;
//call linkStateList.pushback(*Payload);
//}
listL[myMsg->src] = *Payload;
if(listL[myMsg->src].tls == 0){
listL[myMsg->src].tls = 5;
}
//dbg(ROUTING_CHANNEL,"tls %d \n",Payload->tls);
makePack(&sendPackage, myMsg->src, myMsg->dest, --myMsg->TTL, myMsg->protocol, myMsg->seq,(uint8_t*) myMsg->payload, PACKET_MAX_PAYLOAD_SIZE);
call sentList.pushback(sendPackage);
call Sender.send(sendPackage, AM_BROADCAST_ADDR);
return msg;
}
if(!inNList){
call neighborList.pushback(found);
//sendLinkState();
call Routing.createTable();
return msg;
}
//sendLinkState();
}
return msg;
}
dbg(GENERAL_CHANNEL, "Unknown Packet Type %d\n", len);
return msg;
}
event void CommandHandler.ping(uint16_t destination, uint8_t *payload){
dbg(GENERAL_CHANNEL, "PING EVENT \n");
makePack(&sendPackage, TOS_NODE_ID, destination, 20, PROTOCOL_PING, ++sequence, payload, PACKET_MAX_PAYLOAD_SIZE);
call Sender.send(sendPackage, destination);
//call Flooding.floodSend(sendPackage, destination);
}
event void CommandHandler.printNeighbors(){
call NeighborDiscovery.printNeighbors();
/*
uint16_t i;
moteN neighbor;
for(i = 0; i < call neighborList.size();i++){
neighbor = call neighborList.get(i);
dbg(NEIGHBOR_CHANNEL, "Node %d, Neighbor:%d\n",TOS_NODE_ID,neighbor.id);
}*/
}
event void CommandHandler.printRouteTable(){
//call Routing.printRouteTable();
}
event void CommandHandler.printLinkState(){
//call Routing.printLinkState();
uint16_t i;
uint16_t k;
dbg(ROUTING_CHANNEL, "linkstatelist size: %d\n", call linkStateList.size());
for(i= 0; i < 20;i++){
linkState temp = listL[i];
if(temp.tls >= 0){
dbg(ROUTING_CHANNEL, "Node: %d %d\n",temp.id, temp.tls);
for(k=0;k < temp.size; k++){
dbg(ROUTING_CHANNEL, "Neighbor: %d\n",temp.neighborList[k].id);
}
}
}
/*
for(i= 0; i < call linkStateList.size();i++){
linkState temp = call linkStateList.get(i);
dbg(ROUTING_CHANNEL, "Node: %d, tls:%d\n",temp.id, temp.tls);
for(k=0;k < temp.size; k++){
dbg(ROUTING_CHANNEL, "Neighbor: %d\n",temp.neighborList[k].id);
}
}
*/
}
event void CommandHandler.printDistanceVector(){}
event void CommandHandler.setTestServer(){}
event void CommandHandler.setTestClient(){}
event void CommandHandler.setAppServer(){}
event void CommandHandler.setAppClient(){}
void makePack(pack *Package, uint16_t src, uint16_t dest, uint16_t TTL, uint16_t protocol, uint16_t seq, uint8_t* payload, uint8_t length){
Package->src = src;
Package->dest = dest;
Package->TTL = TTL;
Package->seq = seq;
Package->protocol = protocol;
memcpy(Package->payload, payload, length);
}
void updateLinkList(){
uint16_t i;
uint16_t size = 20;
linkState temp;
//Age list
for(i=0;i< 20;i++){
if(listL[i].tls > 0)
listL[i].tls--;
}
for(i=0;i< call linkStateList.size();i++){
temp =call linkStateList.popfront();
if(temp.tls > 0){
temp.tls--;
call linkStateList.pushback(temp);
}
}
for(i=0;i< call linkStateList.size();i++){
temp =call linkStateList.popfront();
if(temp.tls >= 2){
call linkStateList.pushback(temp);
}
}
//Remove non existant neighbors
for(i=0;i< 20;i++){
//dbg(NEIGHBOR_CHANNEL,"node %d,tls%d\n",listL[i].id ,listL[i].tls);
if(listL[i].tls <= 2){
//listL[i]= listL[0];
}
}
}
bool checkSentList(pack *Package){
uint16_t i;
for(i = 0; i < call sentList.size(); i++){
pack current = call sentList.get(i);
if(current.src == Package->src)
if(current.seq == Package->seq )
return TRUE;
}
return FALSE;
}
void removeNeighbor(uint16_t id){
uint16_t i;
for(i = 0; i < call neighborList.size(); i++){
moteN current = call neighborList.popfront();
if(current.id == id){
return;
}
else{
call neighborList.pushback(current);
}
}
return;
}
void removeLink(uint16_t id){
uint16_t i;
for(i = 0; i < call linkStateList.size(); i++){
linkState current = call linkStateList.popfront();
if(current.id == id){
return;
}
else{
call linkStateList.pushback(current);
}
}
return;
}
void sendLinkState(){
linkState Payload;
linkState* payload = &Payload;
uint16_t listSize = call NeighborDiscovery.neighborListSize();
uint16_t i;
list[listSize];
/*
for(i =0; i < listSize;i++){
list[i] = call neighborList.get(i);
dbg(ROUTING_CHANNEL,"node %d neighbors: %d \n",TOS_NODE_ID, list[i].id);
}
*/
//dbg(ROUTING_CHANNEL,"LINKSTATE PACKET BROADCAST %d\n", TOS_NODE_ID);
payload->id = TOS_NODE_ID;
payload->neighborList = call NeighborDiscovery.neighborList();
payload->size = listSize;
payload->tls = 5;
/*
for(i =0; i < listSize;i++){
list[i] = payload->neighborList[i];
dbg(ROUTING_CHANNEL,"node %d neighbors: %d \n",TOS_NODE_ID, list[i].id);
}
*/
makePack(&sendPackage, TOS_NODE_ID, AM_BROADCAST_ADDR, 20, PROTOCOL_LINKSTATE, sequence++,(uint8_t*) payload, PACKET_MAX_PAYLOAD_SIZE);
call sentList.pushback(sendPackage);
listL[TOS_NODE_ID] = Payload;
/*
//if(!inList(Payload.id)){
//call linkStateList.pushback(Payload);
listL[TOS_NODE_ID] = Payload;
call linkStateList.pushback(Payload);
//}
//else{
//removeLink(Payload.id);
//call linkStateList.pushback(Payload);
//}
*/
call Sender.send(sendPackage, AM_BROADCAST_ADDR);
makePack(&sendPackage, TOS_NODE_ID, AM_BROADCAST_ADDR, 20, PROTOCOL_LINKSTATE, sequence++,(uint8_t*) payload, PACKET_MAX_PAYLOAD_SIZE);
call sentList.pushback(sendPackage);
}
bool inList(uint16_t id){
uint16_t i;
for(i = 0; i < call linkStateList.size(); i++){
linkState current = call linkStateList.get(i);
if(current.id == id)
return TRUE;
}
return FALSE;
}
}