#include "CC2420.h"
#include "AM.h"
+#include "Ieee154.h"
+
+#ifdef IEEE154FRAMES_ENABLED
+#error "CC2420 AM layer cannot work when IEEE 802.15.4 frames only are used"
+#endif
configuration CC2420ActiveMessageC {
provides {
}
}
implementation {
+ enum {
+ CC2420_AM_SEND_ID = unique(IEEE154_SEND_CLIENT),
+ };
+ components CC2420RadioC as Radio;
components CC2420ActiveMessageP as AM;
- components CC2420CsmaC as CsmaC;
components ActiveMessageAddressC;
- components UniqueSendC;
- components UniqueReceiveC;
- components CC2420TinyosNetworkC;
- components CC2420PacketC;
+ components CC2420CsmaC as CsmaC;
components CC2420ControlC;
+ components CC2420PacketC;
-#if defined(LOW_POWER_LISTENING) || defined(ACK_LOW_POWER_LISTENING)
- components DefaultLplC as LplC;
-#else
- components DummyLplC as LplC;
-#endif
-
-#if defined(PACKET_LINK)
- components PacketLinkC as LinkC;
-#else
- components PacketLinkDummyC as LinkC;
-#endif
-
-
+ SplitControl = Radio;
RadioBackoff = AM;
Packet = AM;
AMSend = AM;
Snoop = AM.Snoop;
SnoopDefault = AM.SnoopDefault;
AMPacket = AM;
- PacketLink = LinkC;
- LowPowerListening = LplC;
- CC2420Packet = CC2420PacketC;
- PacketAcknowledgements = CC2420PacketC;
- LinkPacketMetadata = CC2420PacketC;
-
- // SplitControl Layers
- SplitControl = LplC;
- LplC.SubControl -> CsmaC;
-
- // Send Layers
- AM.SubSend -> UniqueSendC;
- UniqueSendC.SubSend -> LinkC;
- LinkC.SubSend -> LplC.Send;
- LplC.SubSend -> CC2420TinyosNetworkC.Send;
- CC2420TinyosNetworkC.SubSend -> CsmaC;
+ PacketLink = Radio;
+ LowPowerListening = Radio;
+ CC2420Packet = Radio;
+ PacketAcknowledgements = Radio;
+ LinkPacketMetadata = Radio;
- // Receive Layers
- AM.SubReceive -> LplC;
- LplC.SubReceive -> UniqueReceiveC.Receive;
- UniqueReceiveC.SubReceive -> CC2420TinyosNetworkC.Receive;
- CC2420TinyosNetworkC.SubReceive -> CsmaC;
+ // Radio resource for the AM layer
+ AM.RadioResource -> Radio.Resource[CC2420_AM_SEND_ID];
+ AM.SubSend -> Radio.ActiveSend;
+ AM.SubReceive -> Radio.ActiveReceive;
AM.ActiveMessageAddress -> ActiveMessageAddressC;
AM.CC2420Packet -> CC2420PacketC;
AM.CC2420Config -> CC2420ControlC;
AM.SubBackoff -> CsmaC;
-
+
+ components LedsC;
+ AM.Leds -> LedsC;
}
+
+
interface CC2420Config;
interface ActiveMessageAddress;
interface RadioBackoff as SubBackoff;
+
+ interface Resource as RadioResource;
+ interface Leds;
}
}
implementation {
+ uint16_t pending_length;
+ message_t *pending_message = NULL;
+ /***************** Resource event ****************/
+ event void RadioResource.granted() {
+ uint8_t rc;
+ cc2420_header_t* header = call CC2420PacketBody.getHeader( pending_message );
+
+ signal SendNotifier.aboutToSend[header->type](header->dest, pending_message);
+ rc = call SubSend.send( pending_message, pending_length );
+ if (rc != SUCCESS) {
+ call RadioResource.release();
+ signal AMSend.sendDone[header->type]( pending_message, rc );
+ }
+ }
/***************** AMSend Commands ****************/
command error_t AMSend.send[am_id_t id](am_addr_t addr,
message_t* msg,
uint8_t len) {
cc2420_header_t* header = call CC2420PacketBody.getHeader( msg );
+
+ if (len > call Packet.maxPayloadLength()) {
+ return ESIZE;
+ }
+
header->type = id;
header->dest = addr;
header->destpan = call CC2420Config.getPanAddr();
header->src = call AMPacket.address();
- signal SendNotifier.aboutToSend[id](addr, msg);
-
- return call SubSend.send( msg, len );
+ if (call RadioResource.immediateRequest() == SUCCESS) {
+ error_t rc;
+ signal SendNotifier.aboutToSend[id](addr, msg);
+
+ rc = call SubSend.send( msg, len );
+ if (rc != SUCCESS) {
+ call RadioResource.release();
+ }
+
+ return rc;
+ } else {
+ pending_length = len;
+ pending_message = msg;
+ return call RadioResource.request();
+ }
}
command error_t AMSend.cancel[am_id_t id](message_t* msg) {
}
command uint8_t Packet.maxPayloadLength() {
- return TOSH_DATA_LENGTH;
+ return call SubSend.maxPayloadLength();
}
command void* Packet.getPayload(message_t* msg, uint8_t len) {
/***************** SubSend Events ****************/
event void SubSend.sendDone(message_t* msg, error_t result) {
+ call RadioResource.release();
signal AMSend.sendDone[call AMPacket.type(msg)](msg, result);
}
/***************** SubReceive Events ****************/
event message_t* SubReceive.receive(message_t* msg, void* payload, uint8_t len) {
- if(!(call CC2420PacketBody.getMetadata(msg))->crc) {
- return msg;
- }
-
if (call AMPacket.isForMe(msg)) {
return signal Receive.receive[call AMPacket.type(msg)](msg, payload, len);
}
async command void RadioBackoff.setCca[am_id_t amId](bool useCca) {
call SubBackoff.setCca(useCca);
}
-
-
/***************** Defaults ****************/
default event message_t* Receive.receive[am_id_t id](message_t* msg, void* payload, uint8_t len) {
default event message_t* SnoopDefault.receive[am_id_t id](message_t* msg, void* payload, uint8_t len) {
return msg;
}
-
+
default event void AMSend.sendDone[uint8_t id](message_t* msg, error_t err) {
+ call RadioResource.release();
}
default event void SendNotifier.aboutToSend[am_id_t amId](am_addr_t addr, message_t *msg) {
}
-
default async event void RadioBackoff.requestInitialBackoff[am_id_t id](
message_t *msg) {
}