Skip to content

Commit f62253c

Browse files
author
Hasnain Virk
committed
Changing Timer callback to Mbed Callback
Time handler class had a c style callback attached to it which had been hampering us to be fully object oriented. That particular callback is changed to Mbed Callback which is attatched to a specific object hence allowing us to be fully object oriented.
1 parent e18d76a commit f62253c

File tree

4 files changed

+51
-58
lines changed

4 files changed

+51
-58
lines changed

features/lorawan/lorastack/mac/LoRaMac.cpp

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,6 @@ using namespace events;
4141
*/
4242
static EventQueue *ev_queue;
4343

44-
/*!
45-
* TODO: We should get rid of this
46-
*
47-
* Static handle to LoRaMac object. This is needed for static callback methods.
48-
*/
49-
static LoRaMac* isrHandler = NULL;
50-
5144
/*!
5245
* Maximum length of the fOpts field
5346
*/
@@ -93,8 +86,6 @@ LoRaMac::LoRaMac(LoRaWANTimeHandler &lora_time)
9386
: mac_commands(*this),
9487
_lora_time(lora_time)
9588
{
96-
isrHandler = this;
97-
9889
lora_phy = NULL;
9990
//radio_events_t RadioEvents;
10091
LoRaMacDevEui = NULL;
@@ -172,64 +163,64 @@ LoRaMac::~LoRaMac()
172163
**************************************************************************/
173164
void LoRaMac::handle_tx_done(void)
174165
{
175-
ev_queue->call(isrHandler, &LoRaMac::OnRadioTxDone);
166+
ev_queue->call(this, &LoRaMac::OnRadioTxDone);
176167
}
177168

178169
void LoRaMac::handle_rx_done(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr)
179170
{
180-
ev_queue->call(isrHandler, &LoRaMac::OnRadioRxDone, payload, size, rssi, snr);
171+
ev_queue->call(this, &LoRaMac::OnRadioRxDone, payload, size, rssi, snr);
181172
}
182173

183174
void LoRaMac::handle_rx_error(void)
184175
{
185-
ev_queue->call(isrHandler, &LoRaMac::OnRadioRxError);
176+
ev_queue->call(this, &LoRaMac::OnRadioRxError);
186177
}
187178

188179
void LoRaMac::handle_rx_timeout(void)
189180
{
190-
ev_queue->call(isrHandler, &LoRaMac::OnRadioRxTimeout);
181+
ev_queue->call(this, &LoRaMac::OnRadioRxTimeout);
191182
}
192183

193184
void LoRaMac::handle_tx_timeout(void)
194185
{
195-
ev_queue->call(isrHandler, &LoRaMac::OnRadioTxTimeout);
186+
ev_queue->call(this, &LoRaMac::OnRadioTxTimeout);
196187
}
197188

198189
void LoRaMac::handle_cad_done(bool cad)
199190
{
200191
//TODO Not implemented yet
201-
//ev_queue->call(isrHandler, &LoRaMac::OnRadioCadDone, cad);
192+
//ev_queue->call(this, &LoRaMac::OnRadioCadDone, cad);
202193
}
203194

204195
void LoRaMac::handle_fhss_change_channel(uint8_t cur_channel)
205196
{
206197
// TODO Not implemented yet
207-
//ev_queue->call(isrHandler, &LoRaMac::OnRadioFHSSChangeChannel, cur_channel);
198+
//ev_queue->call(this, &LoRaMac::OnRadioFHSSChangeChannel, cur_channel);
208199
}
209200

210201
void LoRaMac::handle_mac_state_check_timer_event(void)
211202
{
212-
ev_queue->call(isrHandler, &LoRaMac::OnMacStateCheckTimerEvent);
203+
ev_queue->call(this, &LoRaMac::OnMacStateCheckTimerEvent);
213204
}
214205

215206
void LoRaMac::handle_delayed_tx_timer_event(void)
216207
{
217-
ev_queue->call(isrHandler, &LoRaMac::OnTxDelayedTimerEvent);
208+
ev_queue->call(this, &LoRaMac::OnTxDelayedTimerEvent);
218209
}
219210

220211
void LoRaMac::handle_ack_timeout()
221212
{
222-
ev_queue->call(isrHandler, &LoRaMac::OnAckTimeoutTimerEvent);
213+
ev_queue->call(this, &LoRaMac::OnAckTimeoutTimerEvent);
223214
}
224215

225216
void LoRaMac::handle_rx1_timer_event(void)
226217
{
227-
ev_queue->call(isrHandler, &LoRaMac::OnRxWindow1TimerEvent);
218+
ev_queue->call(this, &LoRaMac::OnRxWindow1TimerEvent);
228219
}
229220

230221
void LoRaMac::handle_rx2_timer_event(void)
231222
{
232-
ev_queue->call(isrHandler, &LoRaMac::OnRxWindow2TimerEvent);
223+
ev_queue->call(this, &LoRaMac::OnRxWindow2TimerEvent);
233224
}
234225

235226
/***************************************************************************
@@ -1794,13 +1785,13 @@ LoRaMacStatus_t LoRaMac::LoRaMacInitialization(LoRaMacPrimitives_t *primitives,
17941785
lora_phy->put_radio_to_sleep();
17951786

17961787
// Initialize timers
1797-
_lora_time.TimerInit(&MacStateCheckTimer, handle_mac_state_check_timer_event);
1788+
_lora_time.TimerInit(&MacStateCheckTimer, mbed::callback(this, &LoRaMac::handle_mac_state_check_timer_event));
17981789
_lora_time.TimerSetValue(&MacStateCheckTimer, MAC_STATE_CHECK_TIMEOUT);
17991790

1800-
_lora_time.TimerInit(&TxDelayedTimer, handle_delayed_tx_timer_event);
1801-
_lora_time.TimerInit(&RxWindowTimer1, handle_rx1_timer_event);
1802-
_lora_time.TimerInit(&RxWindowTimer2, handle_rx2_timer_event);
1803-
_lora_time.TimerInit(&AckTimeoutTimer, handle_ack_timeout);
1791+
_lora_time.TimerInit(&TxDelayedTimer, mbed::callback(this, &LoRaMac::handle_delayed_tx_timer_event));
1792+
_lora_time.TimerInit(&RxWindowTimer1, mbed::callback(this, &LoRaMac::handle_rx1_timer_event));
1793+
_lora_time.TimerInit(&RxWindowTimer2, mbed::callback(this, &LoRaMac::handle_rx2_timer_event));
1794+
_lora_time.TimerInit(&AckTimeoutTimer, mbed::callback(this, &LoRaMac::handle_ack_timeout));
18041795

18051796
// Store the current initialization time
18061797
LoRaMacInitializationTime = _lora_time.TimerGetCurrentTime();
@@ -2694,11 +2685,11 @@ LoRaMacStatus_t LoRaMac::LoRaMacMcpsRequest( McpsReq_t *mcpsRequest )
26942685

26952686
radio_events_t *LoRaMac::GetPhyEventHandlers()
26962687
{
2697-
RadioEvents.tx_done = mbed::callback(handle_tx_done);
2698-
RadioEvents.rx_done = mbed::callback(handle_rx_done);
2699-
RadioEvents.rx_error = mbed::callback(handle_rx_error);
2700-
RadioEvents.tx_timeout = mbed::callback(handle_tx_timeout);
2701-
RadioEvents.rx_timeout = mbed::callback(handle_rx_timeout);
2688+
RadioEvents.tx_done = mbed::callback(this, &LoRaMac::handle_tx_done);
2689+
RadioEvents.rx_done = mbed::callback(this, &LoRaMac::handle_rx_done);
2690+
RadioEvents.rx_error = mbed::callback(this, &LoRaMac::handle_rx_error);
2691+
RadioEvents.tx_timeout = mbed::callback(this, &LoRaMac::handle_tx_timeout);
2692+
RadioEvents.rx_timeout = mbed::callback(this, &LoRaMac::handle_rx_timeout);
27022693

27032694
return &RadioEvents;
27042695
}

features/lorawan/lorastack/mac/LoRaMac.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -583,25 +583,25 @@ class LoRaMac
583583
* \brief Opens up a continuous RX 2 window. This is used for
584584
* class c devices.
585585
*/
586-
void OpenContinuousRx2Window( void );
586+
void OpenContinuousRx2Window(void);
587587

588588
/**
589589
* Prototypes for ISR handlers
590590
*/
591-
static void handle_cad_done(bool cad);
592-
static void handle_tx_done(void);
593-
static void handle_rx_done(uint8_t *payload, uint16_t size, int16_t rssi,
594-
int8_t snr);
595-
static void handle_rx_error(void);
596-
static void handle_rx_timeout(void);
597-
static void handle_tx_timeout(void);
598-
static void handle_fhss_change_channel(uint8_t cur_channel);
599-
static void handle_rx1_timer_event(void);
600-
static void handle_rx2_timer_event(void);
601-
static void handle_ack_timeout(void);
602-
static void handle_delayed_tx_timer_event(void);
603-
static void handle_mac_state_check_timer_event(void);
604-
static void handle_next_tx_timer_event(void);
591+
void handle_cad_done(bool cad);
592+
void handle_tx_done(void);
593+
void handle_rx_done(uint8_t *payload, uint16_t size, int16_t rssi,
594+
int8_t snr);
595+
void handle_rx_error(void);
596+
void handle_rx_timeout(void);
597+
void handle_tx_timeout(void);
598+
void handle_fhss_change_channel(uint8_t cur_channel);
599+
void handle_rx1_timer_event(void);
600+
void handle_rx2_timer_event(void);
601+
void handle_ack_timeout(void);
602+
void handle_delayed_tx_timer_event(void);
603+
void handle_mac_state_check_timer_event(void);
604+
void handle_next_tx_timer_event(void);
605605

606606
private:
607607
/**

features/lorawan/system/LoRaWANTimer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ TimerTime_t LoRaWANTimeHandler::TimerGetElapsedTime( TimerTime_t savedTime )
4545
return TimerGetCurrentTime() - savedTime;
4646
}
4747

48-
void LoRaWANTimeHandler::TimerInit( TimerEvent_t *obj, void ( *callback )( void ) )
48+
void LoRaWANTimeHandler::TimerInit( TimerEvent_t *obj, mbed::Callback<void()> callback)
4949
{
5050
obj->value = 0;
5151
obj->Callback = callback;

features/lorawan/system/LoRaWANTimer.h

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ SPDX-License-Identifier: BSD-3-Clause
3333
typedef struct TimerEvent_s
3434
{
3535
uint32_t value;
36-
void ( *Callback )( void );
36+
mbed::Callback<void()> Callback;
3737
SingletonPtr<mbed::Ticker> Timer;
3838
}TimerEvent_t;
3939

@@ -52,6 +52,17 @@ class LoRaWANTimeHandler
5252
*/
5353
void TimerTimeCounterInit(events::EventQueue *queue);
5454

55+
/*!
56+
* \brief Initializes the timer object.
57+
*
58+
* \remark The TimerSetValue function must be called before starting the timer.
59+
* This function initializes the timestamp and reloads the value at 0.
60+
*
61+
* \param [in] obj The structure containing the timer object parameters.
62+
* \param [in] callback The function callback called at the end of the timeout.
63+
*/
64+
void TimerInit( TimerEvent_t *obj, mbed::Callback<void()> callback);
65+
5566
/*!
5667
* \brief Read the current time.
5768
*
@@ -67,16 +78,7 @@ class LoRaWANTimeHandler
6778
*/
6879
TimerTime_t TimerGetElapsedTime( TimerTime_t savedTime );
6980

70-
/*!
71-
* \brief Initializes the timer object.
72-
*
73-
* \remark The TimerSetValue function must be called before starting the timer.
74-
* This function initializes the timestamp and reloads the value at 0.
75-
*
76-
* \param [in] obj The structure containing the timer object parameters.
77-
* \param [in] callback The function callback called at the end of the timeout.
78-
*/
79-
void TimerInit( TimerEvent_t *obj, void ( *callback )( void ) );
81+
8082

8183
/*!
8284
* \brief Starts and adds the timer object to the list of timer events.

0 commit comments

Comments
 (0)