TinyOS / nesC Receive.receive事件定期发出信号,但只处理一次

时间:2013-02-19 21:44:40

标签: events tinyos nesc

我目前正在为TinyOS实施AODV协议,当网络层发出关于收到消息的应用信号时,我看到了奇怪的行为。

下面是相关的应用程序和AODV库代码+一些调试输出,以显示正在发生的事情。

测试申请

配置

configuration BasicTestAppC{
}
implementation{
    components MainC, BasicTestC, AODV, LedsC;

    BasicTestC.Boot->MainC.Boot;
    BasicTestC.SplitControl->AODV.SplitControl;

    BasicTestC.AMSend->AODV.AMSend[1];
    BasicTestC.Receive->AODV.Receive[1];

    ...
}

实施

#include "BasicTest.h"

module BasicTestC {
    uses {
        interface Boot;
        interface SplitControl;
        interface Timer<TMilli> as MilliTimer;
        interface AMSend;
        interface Receive;
        interface Leds;
        interface Packet;
    }
}

implementation {
    message_t pkt;
    message_t * p_pkt;

    uint16_t src = 0x0007;
    uint16_t dest = 0x000A;

    uint16_t ctr = 0;
    test_msg* test_pkt; 
    test_msg* rcv_pkt;

    ...

    //Send counter value to node 10 on every timer tick
    event void MilliTimer.fired() {
        call Leds.led0Toggle();

            ctr = ctr + 1;

        test_pkt = (test_msg*)(call Packet.getPayload(p_pkt, sizeof (test_msg)));
        test_pkt->counter = ctr;
        call AMSend.send(dest, p_pkt, sizeof(test_msg));
    }

    event void AMSend.sendDone(message_t * bufPtr, error_t error) {
        test_pkt = (test_msg*)(call Packet.getPayload(p_pkt, sizeof (test_msg)));
        dbg("APPS", "%s\t APPS: sendDone!! (error=%d) ctr=%u\n", sim_time_string(), error, test_pkt->counter);
    }

    event message_t* Receive.receive(message_t * bufPtr, void * payload, uint8_t len) {
        rcv_pkt = (test_msg * ) payload;
        dbg("APPS", "%s\t APPS: receive!! %u\n", sim_time_string(), rcv_pkt->counter);
        return bufPtr;
    }

}

AODV模块

处理来自AMReceiverC组件的接收事件:

event message_t* SubReceive.receive( message_t* p_msg, void* payload, uint8_t len ) {

    uint8_t i;
    aodv_msg_hdr* aodv_hdr = (aodv_msg_hdr*)(p_msg->data);

    test_msg_y* tmp;
    uint16_t ctr;

    dbg("AODV", "%s\t AODV: SubReceive.receive() dest: %d src:%d\n", sim_time_string(), aodv_hdr->dest, aodv_hdr->src);

    if( aodv_hdr->dest == call AMPacket.address() ) {

        for( i=0;i<len;i++ ) {
            p_app_msg_->data[i] = aodv_hdr->data[i];
        }

        tmp = (test_msg_y*) p_app_msg_->data;
        ctr = tmp->counter;

        //Send signal to application layer
        p_msg = signal Receive.receive[aodv_hdr->app]( p_app_msg_, p_app_msg_->data, len - AODV_MSG_HEADER_LEN );

        dbg("AODV", "%s\t AODV: SubReceive.receive() delivered to upper layer - %u\n", sim_time_string(), ctr);

    } else {

        am_addr_t nexthop = get_next_hop( aodv_hdr->dest );

        dbg("AODV", "%s\t AODV: SubReceive.receive() deliver to next hop:%x\n", sim_time_string(), nexthop);

        /* If there is a next-hop for the destination of the message, 
           the message will be forwarded to the next-hop.            */
        if (nexthop != INVALID_NODE_ID) {
                forwardMSG( p_msg, nexthop, len );
        }
    }
    return p_msg;
}

调试输出

DEBUG (7): 0:0:2.006653503   APPS: sendDone!! (error=0) ctr=2
DEBUG (10): 0:0:2.019577622  AODV: SubReceive.receive() dest: 10 src:7
DEBUG (10): 0:0:2.019577622  AODV: SubReceive.receive() delivered to upper layer - 2
DEBUG (10): 0:0:2.019577622  APPS: receive!! 2
DEBUG (7): 0:0:3.010407143   APPS: sendDone!! (error=0) ctr=3
DEBUG (10): 0:0:3.021820651  AODV: SubReceive.receive() dest: 10 src:7
DEBUG (10): 0:0:3.021820651  AODV: SubReceive.receive() delivered to upper layer - 3
DEBUG (7): 0:0:4.005264961   APPS: sendDone!! (error=0) ctr=4
DEBUG (10): 0:0:4.023239710  AODV: SubReceive.receive() dest: 10 src:7
DEBUG (10): 0:0:4.023239710  AODV: SubReceive.receive() delivered to upper layer - 4
DEBUG (7): 0:0:5.010010417   APPS: sendDone!! (error=0) ctr=5
DEBUG (10): 0:0:5.024780838  AODV: SubReceive.receive() dest: 10 src:7
DEBUG (10): 0:0:5.024780838  AODV: SubReceive.receive() delivered to upper layer - 5
DEBUG (7): 0:0:6.003983230   APPS: sendDone!! (error=0) ctr=6
DEBUG (10): 0:0:6.010147745  AODV: SubReceive.receive() dest: 10 src:7
DEBUG (10): 0:0:6.010147745  AODV: SubReceive.receive() delivered to upper layer - 6
DEBUG (7): 0:0:7.008331960   APPS: sendDone!! (error=0) ctr=7
DEBUG (10): 0:0:7.020187970  AODV: SubReceive.receive() dest: 10 src:7
DEBUG (10): 0:0:7.020187970  AODV: SubReceive.receive() delivered to upper layer - 7
DEBUG (7): 0:0:8.004013748   APPS: sendDone!! (error=0) ctr=8
DEBUG (10): 0:0:8.013474142  AODV: SubReceive.receive() dest: 10 src:7
DEBUG (10): 0:0:8.013474142  AODV: SubReceive.receive() delivered to upper layer - 8
DEBUG (7): 0:0:9.009140671   APPS: sendDone!! (error=0) ctr=9
DEBUG (10): 0:0:9.020233746  AODV: SubReceive.receive() dest: 10 src:7
DEBUG (10): 0:0:9.020233746  AODV: SubReceive.receive() delivered to upper layer - 9
DEBUG (7): 0:0:10.010391884  APPS: sendDone!! (error=0) ctr=10
DEBUG (10): 0:0:10.018341667 AODV: SubReceive.receive() dest: 10 src:7
DEBUG (10): 0:0:10.018341667 AODV: SubReceive.receive() delivered to upper layer - 10

如您所见 - 应用层的接收事件仅被触发/执行一次。以下所有消息都到达目标节点,但未到达网络层之上。

关于这里可能会发生什么的任何想法?

1 个答案:

答案 0 :(得分:2)

问题出在以下几行:

p_msg = signal Receive.receive[aodv_hdr->app]( p_app_msg_, p_app_msg_->data, len - AODV_MSG_HEADER_LEN );

Receive.receive上的文档表明它不应该重用接收消息缓冲区,最常见的做法是返回作为第一个参数传递给它的同一指针。

它的问题是 - 如果用户应用程序在实现Receive.receive事件处理程序时遵循指导原则,它将返回指向消息缓冲区(第一个参数)的指针。但是,在上面的行中传递给Receive.receive的第一个参数是p_app_msg_,这意味着在收到第一个消息后,p_msg不再指向原始消息缓冲区。

仍在考虑解决此问题的最佳方法是,但目前我还没有将Receive.receive的结果分配回p_msg并避免在应用程序代码中重用接收消息缓冲区。