学习TestSerial
1、建立模块 TestSerialC
2、使用到的接口有:
uses {
interface Leds;
interface Boot;
/*主要是下面几个*/
interfaceSplitControl asControl;
interface Receive;
interface AMSend;
interface Timer<TMilli> asMilliTimer;
interface Packet;
}
查找tinyos自带的可以知道共有以下接口可以使用
3、实现中常用函数(implementation)
3.1、Packet中的函数有:
command void clear(message_t* msg); //清除packet内容
command uint8_t payloadLength(message_t* msg);//返回msg的数据(payload)长度
command void setPayloadLength(message_t* msg,uint8_t len); //设定packet的长度
command uint8_t maxPayloadLength();Return the maximum payload length that thiscommunication layer
command void* getPayload(message_t* msg, uint8_tlen);Return a pointer to aprotocol's payload region in a packet.例如:test_serial_msg*rcm=( test_serial_msg*)(callPacket.getPayload(&msg, sizeof(test_serial_msg_t))) 即返回test_serial_msg_t类型的结构体指针指向消息包的有效载荷区(数据区)
3.2、Receive中的函数有
event message_t* receive(message_t* msg, void* payload, uint8_tlen);
* Receivea packet buffer, returning a buffer for the signaling component to use for thenext reception. The return value can bethe same as <tt>msg</tt>, as long as the handling component copies out the data it needs
3.3 Send中函数有
command error_t send(message_t* msg, uint8_tlen);
commanderror_t cancel(message_t* msg);
event void sendDone(message_t* msg, error_terror);
command uint8_t maxPayloadLength();
command void* getPayload(message_t* msg,uint8_t len);
3.4、SplitControl中的函数有
command error_t start();
Start this component and all of itssubcomponents. Return values of SUCCESSwill always result in a <code>startDone()</code> event being signalled.
event void startDone(error_t error);
/**
*Notify caller that the component has been started and is ready to
* receiveother commands.
*
*@param <b>error</b> -- SUCCESS if the component was successfully
* turnedon, FAIL otherwise
*/
command error_t stop();
Startthis component and all of its subcomponents. Return values of SUCCESS will always result in a<code>startDone()</code> event being signalled.
event void stopDone(error_t error);
*Notify caller that the component has been stopped.
* @param<b>error</b> -- SUCCESS if the component was successfully
* turned off, FAIL otherwise
3.5、AMSend中的函数有
command error_tsend(am_addr_t addr, message_t* msg, uint8_t len);
* Send a packet with a data payload of<tt>len</tt> to address
command error_tcancel(message_t* msg);
* Cancel a requested transmission. ReturnsSUCCESS if the
* transmission was canceled properly (notsent in its entirety).
event void sendDone(message_t* msg, error_t error);
* Signaled in response to an accepted sendrequest. <tt>msg</tt> is
* the message buffer sent, and<tt>error</tt> indicates whether the send was successful.
command uint8_t maxPayloadLength();
*Return the maximum payload lengththat this communication layer can provide.
*This command behaves identically to<tt>Packet.maxPayloadLength</tt> and is included in this
command void* getPayload(message_t* msg, uint8_t len);
* Return a pointer to a protocol's payloadregion in a packet.
* This command behaves identically to<tt>Packet.getPayload</tt>
* (minus the length parameter) and isincluded in this interface
下面是基于主动消息的接口
3.5、AMPacket中的函数有
command am_addr_taddress();
command am_addr_tdestination(message_t* amsg);
command am_addr_tsource(message_t* amsg);
command voidsetDestination(message_t* amsg, am_addr_t addr);
command voidsetSource(message_t* amsg, am_addr_t addr);
command boolisForMe(message_t* amsg);
command am_id_ttype(message_t* amsg);
command voidsetType(message_t* amsg, am_id_t t);
command am_group_tgroup(message_t* amsg);
command voidsetGroup(message_t* amsg, am_group_t grp);
command am_group_tlocalGroup();
4、建立连接线
components TestSerialC as App, LedsC, MainC;
components SerialActiveMessageC as AM;
components new TimerMilliC();
App.Boot -> MainC.Boot;
App.Control -> AM;
App.Receive -> AM.Receive[AM_TEST_SERIAL_MSG];
App.AMSend -> AM.AMSend[AM_TEST_SERIAL_MSG];
App.Leds -> LedsC;
App.MilliTimer -> TimerMilliC;
App.Packet -> AM;
如下图:
学习内容:
i. 在这个例子中,主要是了解tinyos系统的基于主动消息的通信模型。在主动消息通信中,每个消息都维护一个应用层的处理器(处理子程序)。当目标节点收到这个消息后,会把这个消息中的数据作为参数,传递给应用层的处理器进行处理。
ii. 主动消息的缓存管理机制
iii. 主动消息的显示确认机制
iv. 消息缓存抽象
/*
* This resource is used toarbitrate access between ActiveMessageC,
* Ieee154MessageC andpossibly future MessageC components to the
* underlying radio driver.
*/
typedef nx_struct message_t {
nx_uint8_theader[sizeof(message_header_t)]; //头部
nx_uint8_tdata[TOSH_DATA_LENGTH];//有下载何区,数据
nx_uint8_tfooter[sizeof(message_footer_t)];//尾部
nx_uint8_tmetadata[sizeof(message_metadata_t)];//元数据
} message_t;
注意:headr/footer/metadata 都是不透明的,不可以直接访问。Data字节的访问必须通通过packet、Ampacket.