tinyos

Parameter passing to the TinyOS Timer.


I am completely new to the tinyos and related API. I have defined a timer and starting it as below.

uses interface Timer<TMilli> as DelayTimer;
call DelayTimer.startOneShot(TIMER_PERIOD_MILLI);

Also defined a timer expiry handler as below,

event void DelayTimer.fired() {
   //...
}

My requirement is that to pass an argument to this timer so that same can be used in the the timer handler function.

Can some one provide how it can be done?


Solution

  • There is no way to pass any parameter to the Timer directly. You need to save it in your component's state before calling startOneShot:

    implementation {
        uint16_t parameter;
    
        // ...
    
        void function(uint16_t value) {
            parameter = value;
            call DelayTimer.startOneShot(TIMER_PERIOD_MILLI);
        }
    
        event void DelayTimer.fired() {
            // use variable parameter
        }
    }
    

    However, if your case is simple and you need only to distinct between various "reasons" of calling Timer, you may use different Timer instances for different purposes:

    uses interface Timer<TMilli> as LogTimer;
    uses interface Timer<TMilli> as SendTimer;
    

    And then, in an implementation:

    void someFunction() {
        call LogTimer.startPeriodic(5000);
        // ...
    }
    
    void anotherFunction() {
        call SendTimer.startOneShot(SEND_DELAY);
        // ...
    }
    
    event void LogTimer.fired() {
        // perform logging
    }
    
    event void SendTimer.fired() {
        // send a packet
    }