Я пытаюсь реализовать код в TINYOS TOSSIM, где узел 1 передает сообщение на узел 2, затем узел 2 передает на узел 3, который передает сообщение обратно на узел 1. Казалось бы, все просто, все идет хорошо До точки, где мне нужно передать от 2 до 3, и мое сообщение фактически передается в соответствии с функцией AMsend.sendDone, однако событие приема не распознает ни одно сообщение, переданное на узел 3. Похоже, что оно просто не может найти TOSSIM_NODE_ID == 3 , однако я указал это в своем файле python и топологии, а также вижу, что узел 3 загружен и прослушивает.
Я разместил код, подскажите, пожалуйста, где ошибка или что я делаю не так в данный момент.
Мой файл C.nc выглядит следующим образом
#include <Timer.h>
#include "BlinkToRadio.h"
module BlinkToRadioC {
uses interface Boot;
uses interface Leds;
uses interface Timer<TMilli> as Timer0;
uses interface Packet;
uses interface AMPacket;
uses interface AMSend;
uses interface SplitControl as AMControl;
uses interface Receive;
}
implementation
{
bool busy = FALSE;
message_t pkt;
uint16_t counter = 0;
task void SendMsg1_2();
task void SendMsg2_Radio();
task void Intercept();
//********************Boot Interface****************//
event void Boot.booted()
{
dbg("Boot","Application booted for node (%d).\n",TOS_NODE_ID);
call AMControl.start();
}
//********************SplitControl Interface*************//
event void AMControl.startDone(error_t err)
{
if (err == SUCCESS)
{
dbg("Radio","Radio is on!\n");
//if (TOS_NODE_ID == 1)
//{
//dbg ("Role","Node 1 starts sending periodical transmission.\n");
call Timer0.startPeriodic(TIMER_PERIOD_MILLI);
}
else
{
call AMControl.start();
}
}
event void AMControl.stopDone(error_t err)
{ }
//************************MilliTimer Interface********************//
event void Timer0.fired()
{
post SendMsg1_2();
}
//***********************Task1 Interface*************************//
task void SendMsg1_2()
{
counter++;
//call Leds.set(counter);
if (!busy)
{
if (TOS_NODE_ID == 1)
{
BlinkToRadioMsg* mesg = (BlinkToRadioMsg*)(call Packet.getPayload(&pkt, sizeof (BlinkToRadioMsg)));
mesg->nodeid = TOS_NODE_ID;
mesg->counter = counter;
dbg ("RadioSend","Sending a message to node 2 \n");
if (call AMSend.send(2, &pkt, sizeof(BlinkToRadioMsg)) == SUCCESS)
{
dbg_clear ("Pkg",">>>Pack \n \t Payload length %hhu \n", call Packet.payloadLength (&pkt));
dbg_clear ("Pkg","\t Source: %hhu \n", call AMPacket.source (&pkt));
dbg_clear ("Pkg","\t Destination: %hhu \n", call AMPacket.destination (&pkt));
dbg_clear ("Pkg","\t AM Type: %hhu \n", call AMPacket.type (&pkt));
dbg_clear ("Pkg","\t\t Payload \n");
dbg_clear ("Pkg","\t\t node_id: %hhu \n", mesg->nodeid);
dbg_clear ("Pkg","\t\t msg_number: %hhu \n", mesg->counter);
dbg_clear ("Pkg","\t\t value: %hhu \n", mesg->value);// call AMPacket.source (&pkt));
dbg_clear ("Pkg","\n");// call AMPacket.source (&pkt));
busy = TRUE;
}
}
}
}
//***********************Task2 Interface*************************//
task void SendMsg2_Radio()
{
counter++;
//call Leds.set(counter);
if (!busy)
{
if (TOS_NODE_ID == 2)
{
BlinkToRadioMsg* mesg = (BlinkToRadioMsg*)(call Packet.getPayload(&pkt, sizeof (BlinkToRadioMsg)));
mesg->nodeid = TOS_NODE_ID;
mesg->counter = counter;
dbg ("RadioSend","Sending a message to node 3 \n");
if (call AMSend.send(3, &pkt, sizeof(BlinkToRadioMsg)) == SUCCESS)
{
dbg_clear ("Pkg",">>>Pack \n \t Payload length %hhu \n", call Packet.payloadLength (&pkt));
dbg_clear ("Pkg","\t Source: %hhu \n", call AMPacket.source (&pkt));
dbg_clear ("Pkg","\t Destination: %hhu \n", call AMPacket.destination (&pkt));
dbg_clear ("Pkg","\t AM Type: %hhu \n", call AMPacket.type (&pkt));
dbg_clear ("Pkg","\t\t Payload \n");
dbg_clear ("Pkg","\t\t node_id: %hhu \n", mesg->nodeid);
dbg_clear ("Pkg","\t\t msg_number: %hhu \n", mesg->counter);
dbg_clear ("Pkg","\t\t value: %hhu \n", mesg->value);// call AMPacket.source (&pkt));
dbg_clear ("Pkg","\n");// call AMPacket.source (&pkt));
busy = TRUE;
}
}
}
}
///***********************Intercept Interface*************************//
task void Intercept()
{
// counter++;
//call Leds.set(counter);
if (!busy)
{
BlinkToRadioMsg* mesg = (BlinkToRadioMsg*)(call Packet.getPayload(&pkt, sizeof (BlinkToRadioMsg)));
mesg->nodeid = TOS_NODE_ID;
mesg->counter = counter;
//dbg ("RadioSend","Sending a corrupted message to node 1 \n");
if (call AMSend.send(1, &pkt, sizeof(BlinkToRadioMsg)) == SUCCESS)
{
busy = TRUE;
}
}
}
//***********************Receive Event Interface*************************//
event message_t* Receive.receive(message_t* msg, void* payload, uint8_t len)
{
if (len == sizeof(BlinkToRadioMsg))
{
if (TOS_NODE_ID == 2)
{
BlinkToRadioMsg* mesg = (BlinkToRadioMsg*)payload;
call Leds.set(mesg->counter);
post SendMsg2_Radio();
dbg("RadioRec","Message successfully received at node 2 at time %s \n",sim_time_string());
dbg_clear ("Pkg",">>>Pack \n \t Payload length %hhu \n", call Packet.payloadLength (&pkt));
dbg_clear ("Pkg","\t Source: %hhu \n", call AMPacket.source (&pkt));
dbg_clear ("Pkg","\t Destination: %hhu \n", call AMPacket.destination (&pkt));
dbg_clear ("Pkg","\t AM Type: %hhu \n", call AMPacket.type (&pkt));
dbg_clear ("Pkg","\t\t Payload \n");
dbg_clear ("Pkg","\t\t node_id: %hhu \n", mesg->nodeid);
dbg_clear ("Pkg","\t\t msg_number: %hhu \n", mesg->counter);
dbg_clear ("Pkg","\t\t value: %hhu \n", mesg->value);
dbg_clear ("Pkg","\n");
}
else if (TOS_NODE_ID == 1)
{
BlinkToRadioMsg* mesg = (BlinkToRadioMsg*)payload;
call Leds.set(mesg->counter);
dbg("RadioRec","Message successfully received at node 1 at time %s \n",sim_time_string());
post SendMsg1_2();
dbg("RadioRec","Message received at node 1 at time %s \n",sim_time_string());
}
else if (TOS_NODE_ID == 3)
{
BlinkToRadioMsg* mesg = (BlinkToRadioMsg*)payload;
call Leds.set(mesg->counter);
dbg("RadioRec","Message is captured by adversary at time %s \n",sim_time_string());
post Intercept();
}
else
{
dbg("RadioRec","Error encountered during reception! \n");
}
}
else
{
dbg("RadioRec","Error encountered during reception! \n");
}
return msg;
}
///***********************Senddone Event Interface*************************//
event void AMSend.sendDone(message_t* msg, error_t error)
{
if (&pkt == msg&&error == SUCCESS)
{
if (TOS_NODE_ID == 1)
{
dbg("RadioSend","Transmitter ID is %hhu \n",TOS_NODE_ID);
dbg("RadioSend","Packet has been successfully transmitted to node 2! \n");
busy = FALSE;
call Timer0.stop();
}
else if (TOS_NODE_ID == 2)
{
dbg("RadioSend","Transmitter ID IS %hhu \n",TOS_NODE_ID);
dbg("RadioSend","Packet has been successfully transmitted to node 3! \n");
busy = FALSE;
}
else if (TOS_NODE_ID == 3)
{
dbg("RadioSend","Transmitter ID IS %hhu \n",TOS_NODE_ID);
dbg("RadioSend","Packet has been successfully transmitted to node 1! \n");
}
else
{
dbg("RadioSend","Error!Transmitter ID is not present! \n");
dbg("RadioSend","Node ID is %hhu \n",TOS_NODE_ID);
post SendMsg2_Radio();
}
}
else
{
dbg("RadioSend","Error encountered during the transmission! \n");
}
}
}
и вот мой скрипт на питоне
#! /usr/bin/python
from TOSSIM import *
import sys
out = sys.stdout
# Number of nodes in the simulated network is 3
number_of_nodes = 3
t = Tossim([])
m = t.mac()
r = t.radio()
#Open a topology file and parse the data, where first linebreak is transmitter nodeID, second is receiver nodeID and third is dBm value
f = open("topo.txt", "r")
for line in f:
s = line.split()
if s:
print " ", s[0], " ", s[1], " ", s[2];
r.add(int(s[0]), int(s[1]), float(s[2]))
# The type of debug messages that will be printed out. [add, comment and uncomment as you need]
t.addChannel("Init",out)
t.addChannel("Boot", out);
t.addChannel("Radio", out);
t.addChannel("Role",out);
t.addChannel("Led", out);
t.addChannel("RadioSend", out);
t.addChannel("RadioRec", out);
t.addChannel("Pkg", out);
#t.addChannel("Drop", sys.stdout);
#t.addChannel("Fwd", sys.stdout);
#t.addChannel("BASE", sys.stdout);
#t.addChannel("DBG", sys.stdout);
#t.addChannel("ERR", sys.stdout);
#t.addChannel("FILE", sys.stdout);
#Boot Nodes
time_boot = 0*t.ticksPerSecond();
print("Creating node 1...");
node1 = t.getNode(1);
#time1 = 0*t.ticksPerSecond();
node1.bootAtTime(time_boot);
print("Creating node 2...");
node2 = t.getNode(2);
#time1 = 0*t.ticksPerSecond();
node2.bootAtTime(time_boot + 10);
print("Creating node 3...");
node3 = t.getNode(3);
#time1 = 0*t.ticksPerSecond();
node3.bootAtTime(time_boot + 15);
#Add noise to the medium channel
noise = open("meyer-heavy.txt", "r")
for line in noise:
str1 = line.strip()
if str1:
val = int(str1)
for i in range(number_of_nodes):
t.getNode(i).addNoiseTraceReading(val)
for i in range(1,3):
print "Creating noise model for ",i;
t.getNode(i).createNoiseModel()
# Simulation time is set to 9999
for i in range(9999):
t.runNextEvent()
это мой вывод
[wsn @ fadvisor Project_new] $ python project.py
1 2 -60.0
2 3 -60.0
3 1 -60.0
1 2 -60.0
Creating node 1...
Creating node 2...
Creating node 3...
Creating noise model for 1
Creating noise model for 2
0:0:0.000000000 DEBUG (1): Application booted for node (1).
0:0:0.000000001 DEBUG (2): Application booted for node (2).
0:0:0.000000001 DEBUG (3): Application booted for node (3).
0:0:0.000000010 DEBUG (1): Radio is on!
0:0:0.000000011 DEBUG (2): Radio is on!
0:0:0.000000011 DEBUG (3): Radio is on!
0:0:0.244140645 DEBUG (1): Sending a message to node 2
>>>Pack
Payload length 6
Source: 1
Destination: 2
AM Type: 6
Payload
node_id: 1
msg_number: 1
value: 0
0:0:0.247619628 DEBUG (2): Message successfully received at node 2 at time 0:0:0.247619628
>>>Pack
Payload length 0
Source: 0
Destination: 0
AM Type: 0
Payload
node_id: 1
msg_number: 1
value: 0
0:0:0.247619638 DEBUG (2): Sending a message to node 3
>>>Pack
Payload length 6
Source: 2
Destination: 3
AM Type: 6
Payload
node_id: 2
msg_number: 2
value: 0
0:0:0.247787474 DEBUG (1): Transmitter ID is 1
0:0:0.247787474 DEBUG (1): Packet has been successfully transmitted to node 2!
0:0:0.251525865 DEBUG (2): Transmitter ID IS 2
0:0:0.251525865 DEBUG (2): Packet has been successfully transmitted to node 3!