Skip to content

Commit 44bca59

Browse files
Kimmo VaisanenHasnain Virk
authored andcommitted
Use EventQueue for timers
Instead of initiating own timer objects we can use EventQueue::call_in() method as we already have handle to EventQueue object. Also setting timeout and starting timer has been combined to TimerStart method.
1 parent f2bdef8 commit 44bca59

File tree

4 files changed

+50
-81
lines changed

4 files changed

+50
-81
lines changed

features/lorawan/lorastack/mac/LoRaMac.cpp

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -214,19 +214,16 @@ void LoRaMac::OnRadioTxDone( void )
214214
// Setup timers
215215
if( _params.is_rx_window_enabled == true )
216216
{
217-
_lora_time.TimerSetValue( &_params.timers.rx_window1_timer, _params.rx_window1_delay );
218-
_lora_time.TimerStart( &_params.timers.rx_window1_timer );
217+
_lora_time.TimerStart( _params.timers.rx_window1_timer, _params.rx_window1_delay );
219218
if( _params.dev_class != CLASS_C )
220219
{
221-
_lora_time.TimerSetValue( &_params.timers.rx_window2_timer, _params.rx_window2_delay );
222-
_lora_time.TimerStart( &_params.timers.rx_window2_timer );
220+
_lora_time.TimerStart( _params.timers.rx_window2_timer, _params.rx_window2_delay );
223221
}
224222
if( ( _params.dev_class == CLASS_C ) || ( _params.is_node_ack_requested == true ) )
225223
{
226224
getPhy.Attribute = PHY_ACK_TIMEOUT;
227225
phyParam = lora_phy->get_phy_params(&getPhy);
228-
_lora_time.TimerSetValue( &_params.timers.ack_timeout_timer, _params.rx_window2_delay + phyParam.Value );
229-
_lora_time.TimerStart( &_params.timers.ack_timeout_timer );
226+
_lora_time.TimerStart( _params.timers.ack_timeout_timer, _params.rx_window2_delay + phyParam.Value );
230227
}
231228
}
232229
else
@@ -281,8 +278,7 @@ void LoRaMac::PrepareRxDoneAbort( void )
281278
_params.flags.bits.mac_done = 1;
282279

283280
// Trig OnMacCheckTimerEvent call as soon as possible
284-
_lora_time.TimerSetValue( &_params.timers.mac_state_check_timer, 1 );
285-
_lora_time.TimerStart( &_params.timers.mac_state_check_timer );
281+
_lora_time.TimerStart( _params.timers.mac_state_check_timer, 1 );
286282
}
287283

288284
void LoRaMac::OnRadioRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
@@ -331,7 +327,7 @@ void LoRaMac::OnRadioRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8
331327

332328
lora_phy->put_radio_to_sleep();
333329

334-
_lora_time.TimerStop( &_params.timers.rx_window2_timer );
330+
_lora_time.TimerStop( _params.timers.rx_window2_timer );
335331

336332
macHdr.value = payload[pktHeaderLen++];
337333

@@ -679,7 +675,7 @@ void LoRaMac::OnRadioRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8
679675

680676
// Stop the AckTimeout timer as no more retransmissions
681677
// are needed.
682-
_lora_time.TimerStop( &_params.timers.ack_timeout_timer );
678+
_lora_time.TimerStop( _params.timers.ack_timeout_timer );
683679
}
684680
else
685681
{
@@ -689,7 +685,7 @@ void LoRaMac::OnRadioRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8
689685
{
690686
// Stop the AckTimeout timer as no more retransmissions
691687
// are needed.
692-
_lora_time.TimerStop( &_params.timers.ack_timeout_timer );
688+
_lora_time.TimerStop( _params.timers.ack_timeout_timer );
693689
}
694690
}
695691
}
@@ -727,8 +723,7 @@ void LoRaMac::OnRadioRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8
727723
_params.flags.bits.mac_done = 1;
728724

729725
// Trig OnMacCheckTimerEvent call as soon as possible
730-
_lora_time.TimerSetValue( &_params.timers.mac_state_check_timer, 1 );
731-
_lora_time.TimerStart( &_params.timers.mac_state_check_timer );
726+
_lora_time.TimerStart( _params.timers.mac_state_check_timer, 1 );
732727
}
733728

734729
void LoRaMac::OnRadioTxTimeout( void )
@@ -771,7 +766,7 @@ void LoRaMac::OnRadioRxError( void )
771766

772767
if( _lora_time.TimerGetElapsedTime( _params.timers.aggregated_last_tx_time ) >= _params.rx_window2_delay )
773768
{
774-
_lora_time.TimerStop( &_params.timers.rx_window2_timer );
769+
_lora_time.TimerStop( _params.timers.rx_window2_timer );
775770
_params.flags.bits.mac_done = 1;
776771
}
777772
}
@@ -809,7 +804,7 @@ void LoRaMac::OnRadioRxTimeout( void )
809804

810805
if( _lora_time.TimerGetElapsedTime( _params.timers.aggregated_last_tx_time ) >= _params.rx_window2_delay )
811806
{
812-
_lora_time.TimerStop( &_params.timers.rx_window2_timer );
807+
_lora_time.TimerStop( _params.timers.rx_window2_timer );
813808
_params.flags.bits.mac_done = 1;
814809
}
815810
}
@@ -838,7 +833,7 @@ void LoRaMac::OnMacStateCheckTimerEvent( void )
838833
PhyParam_t phyParam;
839834
bool txTimeout = false;
840835

841-
_lora_time.TimerStop( &_params.timers.mac_state_check_timer );
836+
_lora_time.TimerStop( _params.timers.mac_state_check_timer );
842837

843838
if( _params.flags.bits.mac_done == 1 )
844839
{
@@ -1024,9 +1019,7 @@ void LoRaMac::OnMacStateCheckTimerEvent( void )
10241019
else
10251020
{
10261021
// Operation not finished restart timer
1027-
_lora_time.TimerSetValue( &_params.timers.mac_state_check_timer,
1028-
MAC_STATE_CHECK_TIMEOUT );
1029-
_lora_time.TimerStart( &_params.timers.mac_state_check_timer );
1022+
_lora_time.TimerStart( _params.timers.mac_state_check_timer, MAC_STATE_CHECK_TIMEOUT );
10301023
}
10311024

10321025
// Handle MCPS indication
@@ -1058,7 +1051,7 @@ void LoRaMac::OnTxDelayedTimerEvent( void )
10581051
loramac_frame_ctrl_t fCtrl;
10591052
AlternateDrParams_t altDr;
10601053

1061-
_lora_time.TimerStop( &_params.timers.tx_delayed_timer );
1054+
_lora_time.TimerStop( _params.timers.tx_delayed_timer );
10621055
_params.mac_state &= ~LORAMAC_TX_DELAYED;
10631056

10641057
if( ( _params.flags.bits.mlme_req == 1 ) && ( mlme.get_confirmation().req_type == MLME_JOIN ) )
@@ -1085,7 +1078,7 @@ void LoRaMac::OnTxDelayedTimerEvent( void )
10851078

10861079
void LoRaMac::OnRxWindow1TimerEvent( void )
10871080
{
1088-
_lora_time.TimerStop( &_params.timers.rx_window1_timer );
1081+
_lora_time.TimerStop( _params.timers.rx_window1_timer );
10891082
_params.rx_slot= RX_SLOT_WIN_1;
10901083

10911084
_params.rx_window1_config.channel = _params.channel;
@@ -1106,7 +1099,7 @@ void LoRaMac::OnRxWindow1TimerEvent( void )
11061099

11071100
void LoRaMac::OnRxWindow2TimerEvent( void )
11081101
{
1109-
_lora_time.TimerStop( &_params.timers.rx_window2_timer );
1102+
_lora_time.TimerStop( _params.timers.rx_window2_timer );
11101103

11111104
_params.rx_window2_config.channel = _params.channel;
11121105
_params.rx_window2_config.frequency = _params.sys_params.rx2_channel.frequency;
@@ -1133,7 +1126,7 @@ void LoRaMac::OnRxWindow2TimerEvent( void )
11331126

11341127
void LoRaMac::OnAckTimeoutTimerEvent( void )
11351128
{
1136-
_lora_time.TimerStop( &_params.timers.ack_timeout_timer );
1129+
_lora_time.TimerStop( _params.timers.ack_timeout_timer );
11371130

11381131
if( _params.is_node_ack_requested == true )
11391132
{
@@ -1305,8 +1298,7 @@ lorawan_status_t LoRaMac::ScheduleTx( void )
13051298
_params.mac_state |= LORAMAC_TX_DELAYED;
13061299
tr_debug("Next Transmission in %lu ms", dutyCycleTimeOff);
13071300

1308-
_lora_time.TimerSetValue( &_params.timers.tx_delayed_timer, dutyCycleTimeOff );
1309-
_lora_time.TimerStart( &_params.timers.tx_delayed_timer );
1301+
_lora_time.TimerStart( _params.timers.tx_delayed_timer, dutyCycleTimeOff );
13101302

13111303
return LORAWAN_STATUS_OK;
13121304
}
@@ -1654,8 +1646,7 @@ lorawan_status_t LoRaMac::SendFrameOnChannel( uint8_t channel )
16541646
mlme.get_confirmation().tx_toa = _params.timers.tx_toa;
16551647

16561648
// Starts the MAC layer status check timer
1657-
_lora_time.TimerSetValue( &_params.timers.mac_state_check_timer, MAC_STATE_CHECK_TIMEOUT );
1658-
_lora_time.TimerStart( &_params.timers.mac_state_check_timer );
1649+
_lora_time.TimerStart( _params.timers.mac_state_check_timer, MAC_STATE_CHECK_TIMEOUT );
16591650

16601651
if( _params.is_nwk_joined == false )
16611652
{
@@ -1684,8 +1675,7 @@ lorawan_status_t LoRaMac::SetTxContinuousWave( uint16_t timeout )
16841675
lora_phy->set_tx_cont_mode(&continuousWave);
16851676

16861677
// Starts the MAC layer status check timer
1687-
_lora_time.TimerSetValue( &_params.timers.mac_state_check_timer, MAC_STATE_CHECK_TIMEOUT );
1688-
_lora_time.TimerStart( &_params.timers.mac_state_check_timer );
1678+
_lora_time.TimerStart( _params.timers.mac_state_check_timer, MAC_STATE_CHECK_TIMEOUT );
16891679

16901680
_params.mac_state |= LORAMAC_TX_RUNNING;
16911681

@@ -1697,8 +1687,7 @@ lorawan_status_t LoRaMac::SetTxContinuousWave1( uint16_t timeout, uint32_t frequ
16971687
lora_phy->setup_tx_cont_wave_mode(frequency, power, timeout);
16981688

16991689
// Starts the MAC layer status check timer
1700-
_lora_time.TimerSetValue( &_params.timers.mac_state_check_timer, MAC_STATE_CHECK_TIMEOUT );
1701-
_lora_time.TimerStart( &_params.timers.mac_state_check_timer );
1690+
_lora_time.TimerStart( _params.timers.mac_state_check_timer, MAC_STATE_CHECK_TIMEOUT );
17021691

17031692
_params.mac_state |= LORAMAC_TX_RUNNING;
17041693

@@ -1828,17 +1817,15 @@ lorawan_status_t LoRaMac::LoRaMacInitialization(loramac_primitives_t *primitives
18281817
lora_phy->put_radio_to_sleep();
18291818

18301819
// Initialize timers
1831-
_lora_time.TimerInit(&_params.timers.mac_state_check_timer,
1820+
_lora_time.TimerInit(_params.timers.mac_state_check_timer,
18321821
mbed::callback(this, &LoRaMac::handle_mac_state_check_timer_event));
1833-
_lora_time.TimerSetValue(&_params.timers.mac_state_check_timer, MAC_STATE_CHECK_TIMEOUT);
1834-
1835-
_lora_time.TimerInit(&_params.timers.tx_delayed_timer,
1822+
_lora_time.TimerInit(_params.timers.tx_delayed_timer,
18361823
mbed::callback(this, &LoRaMac::handle_delayed_tx_timer_event));
1837-
_lora_time.TimerInit(&_params.timers.rx_window1_timer,
1824+
_lora_time.TimerInit(_params.timers.rx_window1_timer,
18381825
mbed::callback(this, &LoRaMac::handle_rx1_timer_event));
1839-
_lora_time.TimerInit(&_params.timers.rx_window2_timer,
1826+
_lora_time.TimerInit(_params.timers.rx_window2_timer,
18401827
mbed::callback(this, &LoRaMac::handle_rx2_timer_event));
1841-
_lora_time.TimerInit(&_params.timers.ack_timeout_timer,
1828+
_lora_time.TimerInit(_params.timers.ack_timeout_timer,
18421829
mbed::callback(this, &LoRaMac::handle_ack_timeout));
18431830

18441831
// Store the current initialization time
@@ -2071,16 +2058,13 @@ radio_events_t *LoRaMac::GetPhyEventHandlers()
20712058

20722059
lorawan_status_t LoRaMac::LoRaMacSetTxTimer( uint32_t TxDutyCycleTime )
20732060
{
2074-
_lora_time.TimerSetValue(&tx_next_packet_timer, TxDutyCycleTime);
2075-
_lora_time.TimerStart(&tx_next_packet_timer);
2076-
2061+
_lora_time.TimerStart(tx_next_packet_timer, TxDutyCycleTime);
20772062
return LORAWAN_STATUS_OK;
20782063
}
20792064

20802065
lorawan_status_t LoRaMac::LoRaMacStopTxTimer( )
20812066
{
2082-
_lora_time.TimerStop(&tx_next_packet_timer);
2083-
2067+
_lora_time.TimerStop(tx_next_packet_timer);
20842068
return LORAWAN_STATUS_OK;
20852069
}
20862070

features/lorawan/system/LoRaWANTimer.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,20 @@ lorawan_time_t LoRaWANTimeHandler::TimerGetElapsedTime( lorawan_time_t savedTime
4545
return TimerGetCurrentTime() - savedTime;
4646
}
4747

48-
void LoRaWANTimeHandler::TimerInit( timer_event_t *obj, mbed::Callback<void()> callback)
48+
void LoRaWANTimeHandler::TimerInit( timer_event_t &obj, mbed::Callback<void()> callback)
4949
{
50-
obj->value = 0;
51-
obj->callback = callback;
50+
obj.callback = callback;
51+
obj.timer_id = 0;
5252
}
5353

54-
void LoRaWANTimeHandler::TimerStart( timer_event_t *obj )
54+
void LoRaWANTimeHandler::TimerStart( timer_event_t &obj, const uint32_t timeout )
5555
{
56-
obj->timer.get()->attach_us(obj->callback, obj->value * 1000 );
56+
obj.timer_id = _queue->call_in(timeout, obj.callback);
57+
MBED_ASSERT(obj.timer_id != 0);
5758
}
5859

59-
void LoRaWANTimeHandler::TimerStop( timer_event_t *obj )
60+
void LoRaWANTimeHandler::TimerStop( timer_event_t &obj )
6061
{
61-
obj->timer.get()->detach( );
62-
}
63-
64-
void LoRaWANTimeHandler::TimerSetValue( timer_event_t *obj, uint32_t value )
65-
{
66-
obj->value = value;
62+
_queue->cancel(obj.timer_id);
63+
obj.timer_id = 0;
6764
}

features/lorawan/system/LoRaWANTimer.h

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,6 @@ class LoRaWANTimeHandler
4040
*/
4141
void TimerTimeCounterInit(events::EventQueue *queue);
4242

43-
/*!
44-
* \brief Initializes the timer object.
45-
*
46-
* \remark The TimerSetValue function must be called before starting the timer.
47-
* This function initializes the timestamp and reloads the value at 0.
48-
*
49-
* \param [in] obj The structure containing the timer object parameters.
50-
* \param [in] callback The function callback called at the end of the timeout.
51-
*/
52-
void TimerInit( timer_event_t *obj, mbed::Callback<void()> callback);
53-
5443
/*!
5544
* \brief Read the current time.
5645
*
@@ -66,29 +55,31 @@ class LoRaWANTimeHandler
6655
*/
6756
lorawan_time_t TimerGetElapsedTime( lorawan_time_t savedTime );
6857

69-
70-
7158
/*!
72-
* \brief Starts and adds the timer object to the list of timer events.
59+
* \brief Initializes the timer object.
7360
*
74-
* \param [in] obj The structure containing the timer object parameters.
61+
* \remark The TimerSetValue function must be called before starting the timer.
62+
* This function initializes the timestamp and reloads the value at 0.
63+
*
64+
* \param [in] obj The structure containing the timer object parameters.
65+
* \param [in] callback The function callback called at the end of the timeout.
7566
*/
76-
void TimerStart( timer_event_t *obj );
67+
void TimerInit( timer_event_t &obj, mbed::Callback<void()> callback);
7768

7869
/*!
79-
* \brief Stops and removes the timer object from the list of timer events.
70+
* \brief Starts and adds the timer object to the list of timer events.
8071
*
8172
* \param [in] obj The structure containing the timer object parameters.
73+
* \param [in] value The new timeout value.
8274
*/
83-
void TimerStop( timer_event_t *obj );
75+
void TimerStart( timer_event_t &obj, const uint32_t timeout );
8476

8577
/*!
86-
* \brief Set a new timeout value.
78+
* \brief Stops and removes the timer object from the list of timer events.
8779
*
88-
* \param [in] obj The structure containing the timer object parameters.
89-
* \param [in] value The new timeout value.
80+
* \param [in] obj The structure containing the timer object parameters.
9081
*/
91-
void TimerSetValue( timer_event_t *obj, uint32_t value );
82+
void TimerStop( timer_event_t &obj );
9283

9384
private:
9485
events::EventQueue *_queue;

features/lorawan/system/lorawan_data_structures.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@
2424
#define LORAWAN_SYSTEM_LORAWAN_DATA_STRUCTURES_H_
2525

2626
#include <inttypes.h>
27-
#include "drivers/Ticker.h"
2827
#include "platform/Callback.h"
29-
#include "platform/SingletonPtr.h"
3028

3129
/*!
3230
* \brief Timer time variable definition
@@ -2052,9 +2050,8 @@ typedef struct {
20522050
* \brief Timer object description
20532051
*/
20542052
typedef struct {
2055-
uint32_t value;
20562053
mbed::Callback<void()> callback;
2057-
SingletonPtr<mbed::Ticker> timer;
2054+
int timer_id;
20582055
} timer_event_t;
20592056

20602057
/*!

0 commit comments

Comments
 (0)