我正在尝试在 TINYOS TOSSIM 中实现代码,其中节点 1 向节点 2 发送消息,然后节点 2 向节点 3 发送消息,节点 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");
}
}
}
这是我的python脚本
#! /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!