虚拟化的活动消息发送抽象。 AMSenderC的每个实例都有其自己的深度为1的队列。 因此,它不必与队列空间的其他AMSenderC实例竞争。 基础实现使用某种形式的公平共享队列来调度这些队列中的数据包。
//App.Packet -> AMSenderC;
//interface Packet;
typedef nx_struct radio_count_msg {
nx_uint16_t counter;
} radio_count_msg_t;
message_t packet;
radio_count_msg_t* rcm = (radio_count_msg_t*)call Packet.getPayload(&packet, sizeof(radio_count_msg_t));
if (rcm == NULL) {return;}
rcm->counter = counter;//修改payload的值
// App.AMSend -> AMSenderC;
//interface AMSend;
typedef nx_struct radio_count_msg {
nx_uint16_t counter;
} radio_count_msg_t;
message_t packet;//定义packet
radio_count_msg_t* rcm = (radio_count_msg_t*)call Packet.getPayload(&packet, sizeof(radio_count_msg_t));//获取指向packet中有效负载的指针
rcm->counter = counter;//设定有效负载的值
if (call AMSend.send(AM_BROADCAST_ADDR, &packet, sizeof(radio_count_msg_t)) == SUCCESS) //发送数据包packet
{
//do something ...
}
// App.AMSend -> AMSenderC;
//interface AMSend;
message_t packet;
//省略了对packet的一些操作
event void AMSend.sendDone(message_t* bufPtr, error_t error) {
if (&packet == bufPtr) {
locked = FALSE;
}
}
简介:基本消息数据类型访问器。 协议可以将附加的分组接口用于其协议特定的数据/元数据。
//interface Packet;
typedef nx_struct radio_count_msg {
nx_uint16_t counter;
} radio_count_msg_t;
message_t packet;
radio_count_msg_t* rcm = (radio_count_msg_t*)call Packet.getPayload(&packet, sizeof(radio_count_msg_t));
if (rcm == NULL) {return;}
rcm->counter = counter;//修改payload的值
event message_t* Receive.receive(message_t* bufPtr, void* payload, uint8_t len)
{
if (len != sizeof(radio_count_msg_t)) //验证长度是否正确
return bufPtr;
else
{
radio_count_msg_t* rcm = (radio_count_msg_t*)payload;//复制有效负载的值
}
}
这是StdContol接口的分相副本。 它应用于在提供它的组件的打开和关闭电源状态之间切换。 对于每个start()或stop()命令,如果该命令返回SUCCESS,则必须发出相应的startDone()或stopDone()事件的信号。
启动此组件及其所有子组件。
停用此组件及其所有子组件。
略
这里面定义了频段CFLAGS += -DCC2420_DEF_CHANNEL=14
,设置自己的频段避免与其他的设备造成冲突干扰
COMPONENT=RadioCountToLedsAppC
CFLAGS += -DCC2420_DEF_CHANNEL=14
include $(MAKERULES)
#ifndef RADIO_COUNT_TO_LEDS_H
#define RADIO_COUNT_TO_LEDS_H
typedef nx_struct radio_count_msg {
nx_uint16_t counter;
} radio_count_msg_t;
enum {
AM_RADIO_COUNT_MSG = 6,
};
#endif
#include "RadioCountToLeds.h"
configuration RadioCountToLedsAppC {}
implementation {
//main leds
components MainC, RadioCountToLedsC as App, LedsC;
App.Boot -> MainC.Boot;
App.Leds -> LedsC;
//timer
components new TimerMilliC();
App.MilliTimer -> TimerMilliC;
//radio 无线通信
components new AMSenderC(AM_RADIO_COUNT_MSG);
components new AMReceiverC(AM_RADIO_COUNT_MSG);
components ActiveMessageC;
App.Receive -> AMReceiverC;
App.AMSend -> AMSenderC;
App.AMControl -> ActiveMessageC;
App.Packet -> AMSenderC;
}
发送流程:
接收流程:
#include "Timer.h"
#include "RadioCountToLeds.h"
module RadioCountToLedsC @safe() {
uses {
interface Leds;
interface Boot;
interface Receive;
interface AMSend;
interface Timer<TMilli> as MilliTimer;
interface SplitControl as AMControl;
interface Packet;
}
}
implementation {
message_t packet;
bool locked;
uint16_t counter = 0;
event void Boot.booted() {
call AMControl.start();
}
event void AMControl.startDone(error_t err) {
if (err == SUCCESS) {
call MilliTimer.startPeriodic(1000);
}
else {
//失败重新开启控制器
call AMControl.start();
}
}
event void AMControl.stopDone(error_t err) {
// do nothing
}
event void MilliTimer.fired() {
counter++;
dbg("RadioCountToLedsC", "RadioCountToLedsC: timer fired, counter is %hu.\n", counter);
//没有锁上,就进行发送数据
if (locked) {
return;
}
else {
//rcm指向有效负载的地址
radio_count_msg_t* rcm = (radio_count_msg_t*)call Packet.getPayload(&packet, sizeof(radio_count_msg_t));
if (rcm == NULL) {
return;
}
rcm->counter = counter;
//AM_BROADCAST_ADDR = 0xffff,表示广播
// 0x0001,表示发送给1号节点
if (call AMSend.send(AM_BROADCAST_ADDR, &packet, sizeof(radio_count_msg_t)) == SUCCESS) {
dbg("RadioCountToLedsC", "RadioCountToLedsC: packet sent.\n", counter);
locked = TRUE;
}
}
}
//发送成功解锁
event void AMSend.sendDone(message_t* bufPtr, error_t error) {
if (&packet == bufPtr) {
locked = FALSE;
}
}
event message_t* Receive.receive(message_t* bufPtr,
void* payload, uint8_t len) {
dbg("RadioCountToLedsC", "Received packet of length %hhu.\n", len);
if (len != sizeof(radio_count_msg_t)) {return bufPtr;}
else {
radio_count_msg_t* rcm = (radio_count_msg_t*)payload;
if (rcm->counter & 0x1) {
call Leds.led0On();
}
else {
call Leds.led0Off();
}
if (rcm->counter & 0x2) {
call Leds.led1On();
}
else {
call Leds.led1Off();
}
if (rcm->counter & 0x4) {
call Leds.led2On();
}
else {
call Leds.led2Off();
}
return bufPtr;
}
}
}