Information Technology Reference
In-Depth Information
which is important in situations where multiple components demand access to the
single physical radio medium at the same time. To this end, AM addresses are intro-
duced: an AM message carries a “destination AM” field, in order to identify the
node which should receive the message.
TinyOS uses two special interfaces to enable components to use Active
Messages: AMSend and AMPacket . The physical format of the messages is
defined in the message_t abstract type.
TinyOS uses a split-phase operation to send messages: a send message com-
mand (AMSend.send(…)) merely initializes the message sending. This command
call returns SUCCESS, if the attempt to gain control over the radio has been suc-
cessful and radio has begun sending. The programmer is now responsible not to
issue any new requests to transmit on the radio (because calls for such requests will
not have resulted in a SUCCESS and sending of a new message would not have
started) until an event AMSend.sendDone is signaled.
Therefore, a general template for a message sending code looks like this:
typedef nx_struct MyCustomMsg {
// insert message fields here
} MyCustomMsg;
implementation {
bool busy = FALSE ;
message_t packet ;
… int DEST_ADDR = ... // specify destination for the message
/* inside some event handler, command handler or a task */
if (! busy ()) {
MyCustomMsg* mycustmsg = (MyCustomMsg*)
(call Packet.getPayload(&packet, NULL));
// fill mycustmsg's fields here
if (call AMSend.send(DEST_ADDR, &packet, sizeof(MyCustomMsg))
== SUCCESS)
{
busy = TRUE; // prevent future req for radio
communication
}
}
// event handler for a sendDone event
event void AMSend.sendDone(message_t* msg, error_t error) {
if (&pkt == msg) {
busy = FALSE; // allow future requests for radio
communication
}
}
}
Receiving messages is realized through writing a handler for Receive.receive
event:
Search WWH ::




Custom Search