學習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.