Skip to content

Commit 05e2d29

Browse files
Hasnain Virktheotherjimmy
authored andcommitted
Reworking callback API
Application should be able to add some optional callbacks if it needs to. Ofcourse there is a penalty of 8-12 bytes per callback, but there can be certain needs of the application that needs to be met for example setting up a link check request etc. We have introduced a structure that contains callbacks for the application use. - 'events' callback is mandatory, user must assign it. Because this callback brings state changes for the application. We cannot segregate this into individual handlers because of RAM penalty. - Other calbacks (none of them are implemented yet are optional). Example of using the API is provided with doxygen
1 parent e60227c commit 05e2d29

File tree

8 files changed

+215
-44
lines changed

8 files changed

+215
-44
lines changed

features/lorawan/LoRaWANInterface.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,15 @@ int16_t LoRaWANInterface::receive(uint8_t port, uint8_t* data, uint16_t length,
177177
}
178178
}
179179

180-
void LoRaWANInterface::lora_event_callback(mbed::Callback<void(lora_events_t)> event_cb)
181-
{
182-
stk_obj().set_lora_event_cb(event_cb);
183-
}
180+
lora_mac_status_t LoRaWANInterface::add_app_callbacks(lorawan_app_callbacks_t *callbacks)
181+
{
182+
183+
if (!callbacks || !callbacks->events) {
184+
// Event Callback is mandatory
185+
return LORA_MAC_STATUS_PARAMETER_INVALID;
186+
}
187+
188+
stk_obj().set_lora_callbacks(callbacks);
189+
190+
return LORA_MAC_STATUS_OK;
191+
}

features/lorawan/LoRaWANInterface.h

Lines changed: 69 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -318,24 +318,75 @@ class LoRaWANInterface: public LoRaWANBase {
318318
virtual int16_t receive(uint8_t port, uint8_t* data, uint16_t length,
319319
int flags);
320320

321-
/** Callback handler.
322-
*
323-
* Events that can be posted to user:
324-
*
325-
* CONNECTED - When the connection is complete
326-
* DISCONNECTED - When the protocol is shut down in response to disconnect()
327-
* TX_DONE - When a packet is sent
328-
* TX_TIMEOUT, - When stack was unable to send packet in TX window
329-
* TX_ERROR, - A general TX error
330-
* TX_CRYPTO_ERROR, - If MIC fails, or any other crypto relted error
331-
* TX_SCHEDULING_ERROR, - When stack is unable to schedule packet
332-
* RX_DONE, - When there is something to receive
333-
* RX_TIMEOUT, - Not yet mapped
334-
* RX_ERROR - A general RX error
335-
*
336-
* @param event_cb A callback function for catching events from the stack.
337-
*/
338-
virtual void lora_event_callback(mbed::Callback<void(lora_events_t)> event_cb);
321+
/** Add application callbacks to the stack.
322+
*
323+
* 'lorawan_app_callbacks' is a structure that holds pointers to the application
324+
* provided methods which are needed to be called in response to certain
325+
* requests. The structure is default constructed to set all pointers to NULL.
326+
* So if the user does not provide the pointer, a response will not be posted.
327+
* However, the 'lorawan_events' callback is mandatory to be provided as it
328+
* contains essential events.
329+
*
330+
* Events that can be posted to user via 'lorawan_events' are:
331+
*
332+
* CONNECTED - When the connection is complete
333+
* DISCONNECTED - When the protocol is shut down in response to disconnect()
334+
* TX_DONE - When a packet is sent
335+
* TX_TIMEOUT, - When stack was unable to send packet in TX window
336+
* TX_ERROR, - A general TX error
337+
* TX_CRYPTO_ERROR, - If MIC fails, or any other crypto relted error
338+
* TX_SCHEDULING_ERROR, - When stack is unable to schedule packet
339+
* RX_DONE, - When there is something to receive
340+
* RX_TIMEOUT, - Not yet mapped
341+
* RX_ERROR - A general RX error
342+
*
343+
* Other responses to certain standard requests are an item for the future.
344+
* For example, a link check request could be sent whenever the device tries
345+
* to send a message and if the network server responds with a link check resposne,
346+
* the stack notifies the application be calling the appropriate method. For example,
347+
* 'link_check_resp' callback could be used to collect a response for a link check
348+
* request MAC command and the result is thus transported to the application
349+
* via callback function provided.
350+
*
351+
* As can be seen from declaration, mbed::Callback<void(uint8_t, uint8_t)> *link_check_resp)
352+
* carries two parameters. First one is Demodulation Margin and the second one
353+
* is number of gateways involved in the path to network server.
354+
*
355+
* An example of using this API with a latch onto 'lorawan_events' could be:
356+
*
357+
* LoRaWANInterface lorawan(radio);
358+
* lorawan_app_callbacks cbs;
359+
* static void my_event_handler();
360+
*
361+
* int main()
362+
* {
363+
* lorawan.initialize(&queue);
364+
* cbs.lorawan_events = mbed::callback(my_event_handler);
365+
* lorawan.add_app_callbacks(&cbs);
366+
* lorawan.connect();
367+
* }
368+
*
369+
* static void my_event_handler(lora_events_t events)
370+
* {
371+
* switch(events) {
372+
* case CONNECTED:
373+
* //do something
374+
* break;
375+
* case DISCONNECTED:
376+
* //do something
377+
* break;
378+
* case TX_DONE:
379+
* //do something
380+
* break;
381+
* default:
382+
* break;
383+
* }
384+
* }
385+
*
386+
* @param callbacks A pointer to the structure containing application
387+
* callbacks.
388+
*/
389+
virtual lora_mac_status_t add_app_callbacks(lorawan_app_callbacks_t *callbacks);
339390
};
340391

341392
#endif /* LORAWANINTERFACE_H_ */

features/lorawan/LoRaWANStack.cpp

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -483,9 +483,18 @@ void LoRaWANStack::mlme_confirm(MlmeConfirm_t *mlme_confirm)
483483
mlme_confirm_handler(&lora_mlme_confirm);
484484
}
485485

486-
void LoRaWANStack::set_lora_event_cb(mbed::Callback<void(lora_events_t)> event_cb)
486+
void LoRaWANStack::set_lora_callbacks(lorawan_app_callbacks_t *cbs)
487487
{
488-
_events = event_cb;
488+
489+
if (cbs) {
490+
if (cbs->events) {
491+
_callbacks.events = cbs->events;
492+
}
493+
494+
if (cbs->link_check_resp) {
495+
_callbacks.link_check_resp = cbs->link_check_resp;
496+
}
497+
}
489498
}
490499

491500
lora_mac_status_t LoRaWANStack::add_channels(const lora_channelplan_t &channel_plan)
@@ -1051,7 +1060,10 @@ void LoRaWANStack::mlme_confirm_handler(lora_mac_mlme_confirm_t *mlme_confirm)
10511060
// Join attempt failed.
10521061
set_device_state(DEVICE_STATE_IDLE);
10531062
lora_state_machine();
1054-
_queue->call(_events, JOIN_FAILURE);
1063+
1064+
if (_callbacks.events) {
1065+
_queue->call(_callbacks.events, JOIN_FAILURE);
1066+
}
10551067
}
10561068
break;
10571069
case LORA_MLME_LINK_CHECK:
@@ -1132,14 +1144,18 @@ void LoRaWANStack::mcps_confirm_handler(lora_mac_mcps_confirm_t *mcps_confirm)
11321144

11331145
// If sending timed out, we have a special event for that
11341146
if (mcps_confirm->status == LORA_EVENT_INFO_STATUS_TX_TIMEOUT) {
1135-
_queue->call(_events, TX_TIMEOUT);
1147+
if (_callbacks.events) {
1148+
_queue->call(_callbacks.events, TX_TIMEOUT);
1149+
}
11361150
return;
11371151
} if (mcps_confirm->status == LORA_EVENT_INFO_STATUS_RX2_TIMEOUT) {
11381152
tr_debug("Did not receive Ack");
11391153
}
11401154

11411155
// Otherwise send a general TX_ERROR event
1142-
_queue->call(_events, TX_ERROR);
1156+
if (_callbacks.events) {
1157+
_queue->call(_callbacks.events, TX_ERROR);
1158+
}
11431159
return;
11441160
}
11451161

@@ -1159,7 +1175,9 @@ void LoRaWANStack::mcps_confirm_handler(lora_mac_mcps_confirm_t *mcps_confirm)
11591175
// data rate plus frame counter.
11601176
_lw_session.uplink_counter = mcps_confirm->uplink_counter;
11611177
_tx_msg.tx_ongoing = false;
1162-
_queue->call(_events, TX_DONE);
1178+
if (_callbacks.events) {
1179+
_queue->call(_callbacks.events, TX_DONE);
1180+
}
11631181
}
11641182

11651183
/** MCPS-Indication event function
@@ -1175,7 +1193,9 @@ void LoRaWANStack::mcps_indication_handler(lora_mac_mcps_indication_t *mcps_indi
11751193
}
11761194

11771195
if (mcps_indication->status != LORA_EVENT_INFO_STATUS_OK) {
1178-
_queue->call(_events, RX_ERROR);
1196+
if (_callbacks.events) {
1197+
_queue->call(_callbacks.events, RX_ERROR);
1198+
}
11791199
return;
11801200
}
11811201

@@ -1224,7 +1244,9 @@ void LoRaWANStack::mcps_indication_handler(lora_mac_mcps_indication_t *mcps_indi
12241244
// This may never happen as both radio and MAC are limited
12251245
// to the size 255 bytes
12261246
tr_debug("Cannot receive more than buffer capacity!");
1227-
_queue->call(_events, RX_ERROR);
1247+
if (_callbacks.events) {
1248+
_queue->call(_callbacks.events, RX_ERROR);
1249+
}
12281250
return;
12291251
} else {
12301252
_rx_msg.type = LORAMAC_RX_MCPS_INDICATION;
@@ -1238,7 +1260,9 @@ void LoRaWANStack::mcps_indication_handler(lora_mac_mcps_indication_t *mcps_indi
12381260
// Notify application about received frame..
12391261
tr_debug("Received %d bytes", _rx_msg.rx_message.mcps_indication.buffer_size);
12401262
_rx_msg.receive_ready = true;
1241-
_queue->call(_events, RX_DONE);
1263+
if (_callbacks.events) {
1264+
_queue->call(_callbacks.events, RX_DONE);
1265+
}
12421266
} else {
12431267
// Invalid port, ports 0, 224 and 225-255 are reserved.
12441268
}
@@ -1805,7 +1829,9 @@ lora_mac_status_t LoRaWANStack::lora_state_machine()
18051829
_lw_session.active = false;
18061830

18071831
tr_debug("LoRaWAN protocol has been shut down.");
1808-
_queue->call(_events, DISCONNECTED);
1832+
if (_callbacks.events) {
1833+
_queue->call(_callbacks.events, DISCONNECTED);
1834+
}
18091835
status = LORA_MAC_STATUS_DEVICE_OFF;
18101836
break;
18111837
case DEVICE_STATE_NOT_INITIALIZED:
@@ -1847,7 +1873,9 @@ lora_mac_status_t LoRaWANStack::lora_state_machine()
18471873
// Session is now active
18481874
_lw_session.active = true;
18491875
// Tell the application that we are connected
1850-
_queue->call(_events, CONNECTED);
1876+
if (_callbacks.events) {
1877+
_queue->call(_callbacks.events, CONNECTED);
1878+
}
18511879
break;
18521880
case DEVICE_STATE_ABP_CONNECTING:
18531881
/*
@@ -1879,7 +1907,9 @@ lora_mac_status_t LoRaWANStack::lora_state_machine()
18791907
status = LORA_MAC_STATUS_OK;
18801908
// Session is now active
18811909
_lw_session.active = true;
1882-
_queue->call(_events, CONNECTED);
1910+
if (_callbacks.events) {
1911+
_queue->call(_callbacks.events, CONNECTED);
1912+
}
18831913
break;
18841914
case DEVICE_STATE_SEND:
18851915
// If a transmission is ongoing, don't interrupt
@@ -1895,11 +1925,15 @@ lora_mac_status_t LoRaWANStack::lora_state_machine()
18951925
break;
18961926
case LORA_MAC_STATUS_CRYPTO_FAIL:
18971927
tr_error("Crypto failed. Clearing TX buffers");
1898-
_queue->call(_events, TX_CRYPTO_ERROR);
1928+
if (_callbacks.events) {
1929+
_queue->call(_callbacks.events, TX_CRYPTO_ERROR);
1930+
}
18991931
break;
19001932
default:
19011933
tr_error("Failure to schedule TX!");
1902-
_queue->call(_events, TX_SCHEDULING_ERROR);
1934+
if (_callbacks.events) {
1935+
_queue->call(_callbacks.events, TX_SCHEDULING_ERROR);
1936+
}
19031937
break;
19041938
}
19051939
}

features/lorawan/LoRaWANStack.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ class LoRaWANStack: private mbed::NonCopyable<LoRaWANStack> {
6565
*/
6666
lora_mac_status_t initialize_mac_layer(events::EventQueue *queue);
6767

68-
/** Sets all callbacks of the LoRaWAN interface.
68+
/** Sets all callbacks for the application.
6969
*
70-
* @param *event_cb An event structure representing all possible callbacks.
70+
* \param callbacks A pointer to the structure carrying callbacks.
7171
*/
72-
void set_lora_event_cb(mbed::Callback<void(lora_events_t)> event_cb);
72+
void set_lora_callbacks(lorawan_app_callbacks_t *callbacks);
7373

7474
/** Adds channels to use.
7575
*
@@ -412,7 +412,7 @@ class LoRaWANStack: private mbed::NonCopyable<LoRaWANStack> {
412412

413413
compliance_test_t _compliance_test;
414414
device_states_t _device_current_state;
415-
mbed::Callback<void(lora_events_t)> _events;
415+
lorawan_app_callbacks_t _callbacks;
416416
radio_events_t *_mac_handlers;
417417
lorawan_session_t _lw_session;
418418
lora_mac_tx_message_t _tx_msg;

features/lorawan/lorastack/mac/LoRaMac.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2136,6 +2136,7 @@ static LoRaMacStatus_t ScheduleTx( void )
21362136
// Send later - prepare timer
21372137
LoRaMacState |= LORAMAC_TX_DELAYED;
21382138
tr_debug("Next Transmission in %lu ms", dutyCycleTimeOff);
2139+
21392140
TimerSetValue( &TxDelayedTimer, dutyCycleTimeOff );
21402141
TimerStart( &TxDelayedTimer );
21412142

features/lorawan/system/lorawan_data_structures.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2884,6 +2884,18 @@ typedef enum lora_events {
28842884
JOIN_FAILURE,
28852885
} lora_events_t;
28862886

2887+
typedef struct {
2888+
// Mandatory. Event Callback must be provided
2889+
mbed::Callback<void(lora_events_t)> events;
2890+
2891+
// Rest are optional
2892+
// If the user do not assign these callbacks, these callbacks would return
2893+
// null if checked with bool operator
2894+
// link_check_resp callback and other such callbacks will be maped in
2895+
// future releases of Mbed-OS
2896+
mbed::Callback<void(uint8_t, uint8_t)> link_check_resp;
2897+
}lorawan_app_callbacks_t;
2898+
28872899
typedef struct lora_channelplan {
28882900
uint8_t nb_channels; // number of channels
28892901
lora_channels_t *channels;

features/netsocket/LoRaRadio.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@
1818
#ifndef LORARADIO_H_
1919
#define LORARADIO_H_
2020

21+
/**
22+
* Structure to hold RF controls for LoRa Radio.
23+
* SX1276 have an extra control for the crystal (used in DOSCO-L072CZ)
24+
*/
25+
typedef struct {
26+
PinName rf_switch_ctl1;
27+
PinName rf_switch_ctl2;
28+
PinName txctl;
29+
PinName rxctl;
30+
PinName ant_switch;
31+
PinName pwr_amp_ctl;
32+
PinName tcxo;
33+
} rf_ctrls;
34+
2135
/** Radio driver internal state.
2236
* State machine states definition.
2337
*/

0 commit comments

Comments
 (0)