Question
This code runs ping, ping reply, TTL, and Flooding, i now need to implement Neighbor Discovery but am unsure how to begin. #include #include includes/command.h
This code runs ping, ping reply, TTL, and Flooding, i now need to implement Neighbor Discovery but am unsure how to begin.
#include
#include "includes/command.h"
#include "includes/packet.h"
#include "includes/CommandMsg.h"
#include "includes/sendInfo.h"
#include "includes/channels.h"
module Node{
uses interface Boot;
uses interface SplitControl as AMControl;
uses interface Receive;
uses interface SimpleSend as Sender;
uses interface CommandHandler;
uses interface List as PackageList;
uses interface List as NeighborList;
}
implementation{
uint16_t sequenceCounter = 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);
bool compPack(pack *Package);
void shovePack(pack Package);
event void Boot.booted(){
call AMControl.start();
dbg(GENERAL_CHANNEL, "Booted ");
}
event void AMControl.startDone(error_t err){
if(err == SUCCESS){
dbg(GENERAL_CHANNEL, "Radio On ");
}else{
//Retry until successful
call AMControl.start();
}
}
event void AMControl.stopDone(error_t err){}
event message_t* Receive.receive(message_t* msg, void* payload, uint8_t len){
dbg(GENERAL_CHANNEL, "Packet Received ");
if(len==sizeof(pack)){
pack* myMsg=(pack*) payload;
//dbg(GENERAL_CHANNEL, "Im in ");
if(compPack(myMsg) || (myMsg->TTL == 0)){
// DROP DA PACKAGE
}else if(myMsg->protocol == 0 && myMsg->dest == TOS_NODE_ID){ // PING
dbg(GENERAL_CHANNEL, "WOOHOO From %d! to %d! ", myMsg->src, myMsg->dest);
dbg(GENERAL_CHANNEL, "Package at destination. Payload: %s ", myMsg->payload);
makePack(&sendPackage, TOS_NODE_ID, myMsg->src, MAX_TTL, PROTOCOL_PINGREPLY, sequenceCounter, (uint8_t*)myMsg->payload, sizeof(myMsg->payload));
sequenceCounter++;
shovePack(sendPackage);
call Sender.send(sendPackage, AM_BROADCAST_ADDR);
}else if(myMsg->protocol == 1 && myMsg->dest == TOS_NODE_ID){ // PING REPLY
dbg(GENERAL_CHANNEL, "Recieved ping reply from Node: %d! ", myMsg->src);
}else{
makePack(&sendPackage, myMsg->src, myMsg->dest, myMsg->TTL-1, myMsg->protocol, myMsg->seq, (uint8_t*)myMsg->payload, sizeof(myMsg->payload));
dbg(GENERAL_CHANNEL, "Received package from %d going to %d, TTL: %d, Rebroad... ", myMsg->src, myMsg->dest, myMsg->TTL);
shovePack(sendPackage);
call Sender.send(sendPackage, AM_BROADCAST_ADDR);
}
return msg;
}
dbg(GENERAL_CHANNEL, "Unknown Packet Type %d ", len);
return msg;
}
event void CommandHandler.ping(uint16_t destination, uint8_t *payload){
dbg(GENERAL_CHANNEL, "PING EVENT!!! ");
makePack(&sendPackage, TOS_NODE_ID, destination, MAX_TTL, 0, 0, payload, PACKET_MAX_PAYLOAD_SIZE);
call Sender.send(sendPackage, AM_BROADCAST_ADDR);
}
event void CommandHandler.printNeighbors(){}
bool compPack(pack *Pack){
uint16_t j = 0;
uint16_t size = call PackageList.size();
pack finder; // temp package to compare to package list for similarities
for(j = 0; j < size; j++){
finder = call PackageList.get(j); //iterate through list and compare
if(finder.src == Pack->src && finder.seq == Pack->seq && finder.dest == Pack->dest){
return TRUE; // found same package at a node
}
}
return FALSE; // did not find package doppleganger
}
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 shovePack(pack Package) {
if (call PackageList.isFull()) {
call PackageList.popfront();
}
call PackageList.pushback(Package);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started