📄 lesson7.html
字号:
<!DOCTYPE doctype PUBLIC "-//w3c//dtd html 4.0 transitional//en"><html><head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <meta name="GENERATOR" content="Mozilla/4.78 [en] (X11; U; Linux 2.4.7-10 i686) [Netscape]"> <title>Lesson 7: Injecting and broadcasting packets</title></head><body bgcolor="#f8f8ff" link="#005bb7" vlink="#005bb7"> <table border="0" cellspacing="2" cellpadding="3" width="100%" hspace="4"> <tbody> <tr bgcolor="#e0e0ff"> <td width="100%"><b><font face="tahoma,arial,helvetica"><font size="-1">Lesson 7: Injecting and broadcasting packets</font></font></b> <p><font face="tahoma,arial,helvetica">Last updated 23 August 2003</font></p> </td> </tr> </tbody></table><p>This lesson discusses the use of simple Java tools to inject packetsfrom a PC into the sensor network, as well as the use of a simplemulti-hop broadcast protocol.<table border="0" cellspacing="2" cellpadding="3" width="100%" hspace="4"> <tbody> <tr bgcolor="#e0e0ff"> <td width="100%"><b><nobr><font face="arial,helvetica">Injectingpackets</font></nobr></b></td> </tr> </tbody></table></p><p>We first demonstrate a simple application that receives "command"packets from the radio and interprets them to perform a number ofactions, such as turning the LEDs on and off. Program one mote with the <tt><a href="../../apps/SimpleCmd">apps/SimpleCmd</a></tt> application, andanother with <tt>apps/TOSBase</tt>. Next connect the programming boardto the serial port of the computer. The mote programmed with <tt>apps/TOSBase</tt>should remain on the programming board. TOS<tt>Base</tt> will act as thebase station; a gateway allowing radio communication between the PC andmotes programmed with <tt>SimpleCmd</tt>. </p><p>Now run the <tt>SerialForwarder</tt> as in Lesson 6 using </p><p> <tt>java net.tinyos.sf.SerialForwarder -commserial@COM1</tt> </p><p>The Java application <tt><a href="../../tools/java/net/tinyos/tools/BcastInject.java">tools/java/net/tinyos/tools/BcastInject</a></tt>is used to inject a command packet into the sensor network from the PC.You can run it using </p><pre> java net.tinyos.tools.BcastInject <group_id> <command></pre>where <tt><group_id></tt> is the Active Message group ID you areusing for your network, specified in decimal. (For example, the defaultgroup ID of 0x7d is 125 decimal.) For <tt>command</tt> you can specifya number of options, but for this lesson the <tt>SimpleCmd</tt>application understands the following:<ul> <li> <tt>led_on</tt> - Turn on the yellow LED</li> <li> <tt>led_off</tt> - Turn off the yellow LED</li> <li> <tt>radio_louder</tt> - Increase radio power</li> <li> <tt>radio_quieter</tt> - Decrease radio power</li></ul>By running <tt>BcastInject</tt> with the appropriate arguments youshould be able to turn the yellow LED on and off. Looking at the codefor <tt>SimpleCmdM.nc</tt>, you can see that the application simplywaits for incoming messages to arrive and interprets one field of themessage as the command type. Note that <tt>BcastInject</tt> and <tt><a href="../../apps/SimpleCmd/SimpleCmdMsg.h">SimpleCmd</a></tt> have toagree on what the command types mean.<table border="0" cellspacing="2" cellpadding="3" width="100%" hspace="4"> <tbody> <tr bgcolor="#e0e0ff"> <td width="100%"><b><nobr><font face="arial,helvetica">Exercise</font></nobr></b></td> </tr> </tbody></table>Add commands that will allow <tt>SimpleCmd</tt> to turn its sounder onand off. You'll need to perform the following modifications:<ul> <li> In <tt>SimpleCmd.nc</tt>, wire the Sounder component into theapplication.</li> <li> In <tt>SimpleCmdM.nc</tt>, initialize the sounder and add a newcommand action that will turn the sounder on and off.</li> <li> In SimpleCmdMsg.h add a new command type for turning the sounderon and off</li> <li> In <tt>BcastInject.java</tt>, add the sounder on/off command tothe set of supported command types. Be sure to use the same commandtype codes as in <tt>SimpleCmdMsg.h</tt>.</li></ul><table border="0" cellspacing="2" cellpadding="3" width="100%" hspace="4"> <tbody> <tr bgcolor="#e0e0ff"> <td width="100%"><b><nobr><font face="arial,helvetica">Multihopbroadcast</font></nobr></b></td> </tr> </tbody></table><p>In this part of the lesson, we extend the <tt>SimpleCmd</tt>application and cause it to forward command messages that it receives toother motes in the network. This is accomplished by re-broadcasting thecommand message once it has been processed. In this way we can form asimple multi-hop routing network, extending the motes' communicationrange. </p><p>This code is in <tt><a href="../../apps/SimpleCmd/BcastM.nc">apps/SimpleCmd/BcastM.nc</a></tt>.In addition to processing the command contained within each receivedmessage, it re-broadcasts the message if it has not been seen before.Note that in order to install the <tt>Bcast</tt> application you needto edit <tt><a href="../../apps/SimpleCmd/Makefile">apps/SimpleCmd/Makefile</a></tt>and change the line </p><pre> COMPONENT=SimpleCmd</pre>to<pre> COMPONENT=Bcast</pre><center><table border="0" cellspacing="2" cellpadding="3" width="80%" hspace="4"> <tbody> <tr bgcolor="#e0e0e0"> <td width="100%"><b>BcastM.nc</b> <pre> event TOS_MsgPtr ReceiveCmdMsg.receive(TOS_MsgPtr pmsg){<br> TOS_MsgPtr ret = msg;<br> result_t retval;<br> struct SimpleCmdMsg *data= (struct SimpleCmdMsg *)pmsg->data;<br><br> // Check if this is a new broadcast message<br> call Leds.greenToggle();<br> if (is_new_msg(data)) {<br> remember_msg(data);<br> retval = call ProcessCmd.execute(pmsg) ;<br><br> // Return a message buffer to the lower levels, and hold on to the<br> // current buffer<br> ret = msg;<br> msg = pmsg;<br><br> }<br> return ret;<br> }</pre> </td> </tr> </tbody></table></center><p>To determine whether a given command message has been seen before,the <tt>BcastM</tt> component tracks the sequence number contained inthe message. If the sequence number of the current message is within127 of the current message, the command is accepted, processed, andforwarded. Otherwise, it's dropped. </p><p>Note that the <tt>BcastInject</tt> program maintains its sequencenumber across invocations (saving it in a file called <tt>tools/java/bcast.properties</tt>).If you remove this file the sequence number generated by <tt>BcastInject</tt>will be reset to 1. You will need to power-cycle the motes if you dothis in order for them to interpret subsequent messages as "new". </p><p>Accepting messages with a wide range of sequence numbers (ratherthan just the previous sequence number plus 1) allows for cases wheremessages are dropped by the radio stack, for example, due tocorruption. This is a fairly crude mechanism, of course, and in a realapplication you might want to do something more involved, such aspacket acknowledgments.<table border="0" cellspacing="2" cellpadding="3" width="100%" hspace="4"> <tbody> <tr bgcolor="#e0e0ff"> <td width="100%"><b><nobr><font face="arial,helvetica">Exercises</font></nobr></b></td> </tr> </tbody></table></p><ol> <li> Look at <tt>SimpleCmdM.nc</tt> again - notice that the green andred LEDs are used to indicate the "hop count" of the message - that is,the number of times that message has been forwarded through a mote. Setup two motes running <tt>Bcast</tt> (from the apps/SimpleCmd directory)and place them so that one mote gets command messages with a hop countof 1, and another gets messages with a hop count of 2.</li> <li> As written, the broadcast protocol is fairly fragile: it dependson a property that the sequence number on the BcastInject and thesequence number on the mote differ by less that 127. A better way todistinguish between the messages would be to create an index of a few(say 5) most recently seen messages. If the message currently receivedis not in that index, then the node should forward it. <tt>BcastInject</tt>can then run unmodified, or it could send out messages with randommessage numbers.</li></ol><hr><b><a href="lesson6.html">< Previous Lesson</a></b> | <b><a href="lesson8.html">Next Lesson ></a></b> | <b><a href="index.html">Top</a></b></body></html><!-- LocalWords: TinyOS nesC nc async norace BlinkM FooM ncc SingleTimer Leds --><!-- LocalWords: LedsC StdControl tos init TimerC redOn redOff uint redToggle --><!-- LocalWords: metadata html nesdoc gcc exe avr objcopy srec uisp mib dprog --><!-- LocalWords: towards dapa xff ThinkPad dlpt Makelocal micasb Wshadow DDEF --><!-- LocalWords: finline fnesc cfile lm Atmel ATmega nesC's nesc SenseM rdata --><!-- LocalWords: ADCControl SounderControl dataReady getData rcombine someval --><!-- LocalWords: ADControl fooControl barControl MyTimer uniqueCount basicsb --><!-- LocalWords: sensorboard Makerules sensorboards SenseTask taskname INTMSG --><!-- LocalWords: SenseTaskM putdata processData CntToLedsAndRfm RfmToLeds Msg --><!-- LocalWords: CntToRfmAndLeds IntToLeds IntToRfm IntOutput outputComplete --><!-- LocalWords: IntToRfmM GenericComm SendMsg bool struct IntMsg src BCAST --><!-- LocalWords: ADDR sizeof TOSH sendDone GenricComm AMStandard RfmToInt pc --><!-- LocalWords: UARTNoCRCPacket RadioCRCPacket ActiveMessage RfmToIntM xfff --><!-- LocalWords: ReceiveIntMsg ReceiveMsg MsgPtr addr SenseToRfm TOSSIM DBG --><!-- LocalWords: TinyViz ffff crc usr dbg const printf prepended bitmask gdb --><!-- LocalWords: ledsOn gdbinit plugin API TestTinyViz cd tinyviz plugins RFM --><!-- LocalWords: FakeLocation RadioModelPlugin LocationPlugin afterwards disc --><!-- LocalWords: DebugMsgEvent TestTinyVizM AutoRun autorun numsec stopstring --><!-- LocalWords: gridrandom DebugMsgPlugin RadioLinkPlugin radiomodel precmd --><!-- LocalWords: radioscaling nummotes logfile txt logfiles arConfig TOSSIM's --><!-- LocalWords: SimpleCmd TOSBase SerialForwarder BcastInject SimpleCmdM ret --><!-- LocalWords: SimpleCmdMsg Multihop Bcast BcastM ReceiveCmdMsg pmsg msg --><!-- LocalWords: retval greenToggle ProcessCmd -->
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -