Skip to content

Commit 06c7e41

Browse files
Kimmo Vaisanenkivaisan
authored andcommitted
Fix construction of LoRaWANInterface object
Since radio object is constructed by another source file, we have a race condition. In order to prevent this race condition, LoRaWANInterface object is now created in main().
1 parent 92fe694 commit 06c7e41

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

main.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ static EventQueue ev_queue(MAX_NUMBER_OF_EVENTS * EVENTS_EVENT_SIZE);
7474
static void lora_event_handler(lorawan_event_t event);
7575

7676
/**
77-
* Constructing Mbed LoRaWANInterface and passing it down the radio object.
77+
* Pointer to LoRaWAN interface
7878
*/
79-
static LoRaWANInterface lorawan(get_lora_radio());
79+
static LoRaWANInterface* lorawan = NULL;
8080

8181
/**
8282
* Application specific callbacks
@@ -88,14 +88,18 @@ static lorawan_app_callbacks_t callbacks;
8888
*/
8989
int main (void)
9090
{
91+
// Constructing Mbed LoRaWANInterface and passing it down the radio object.
92+
LoRaWANInterface lora(get_lora_radio());
93+
lorawan = &lora;
94+
9195
// setup tracing
9296
setup_trace();
9397

9498
// stores the status of a call to LoRaWAN protocol
9599
lorawan_status_t retcode;
96100

97101
// Initialize LoRaWAN stack
98-
if (lorawan.initialize(&ev_queue) != LORAWAN_STATUS_OK) {
102+
if (lorawan->initialize(&ev_queue) != LORAWAN_STATUS_OK) {
99103
printf("\r\n LoRa initialization failed! \r\n");
100104
return -1;
101105
}
@@ -104,10 +108,10 @@ int main (void)
104108

105109
// prepare application callbacks
106110
callbacks.events = mbed::callback(lora_event_handler);
107-
lorawan.add_app_callbacks(&callbacks);
111+
lorawan->add_app_callbacks(&callbacks);
108112

109113
// Set number of retries in case of CONFIRMED messages
110-
if (lorawan.set_confirmed_msg_retries(CONFIRMED_MSG_RETRY_COUNTER)
114+
if (lorawan->set_confirmed_msg_retries(CONFIRMED_MSG_RETRY_COUNTER)
111115
!= LORAWAN_STATUS_OK) {
112116
printf("\r\n set_confirmed_msg_retries failed! \r\n\r\n");
113117
return -1;
@@ -117,14 +121,14 @@ int main (void)
117121
CONFIRMED_MSG_RETRY_COUNTER);
118122

119123
// Enable adaptive data rate
120-
if (lorawan.enable_adaptive_datarate() != LORAWAN_STATUS_OK) {
124+
if (lorawan->enable_adaptive_datarate() != LORAWAN_STATUS_OK) {
121125
printf("\r\n enable_adaptive_datarate failed! \r\n");
122126
return -1;
123127
}
124128

125129
printf("\r\n Adaptive data rate (ADR) - Enabled \r\n");
126130

127-
retcode = lorawan.connect();
131+
retcode = lorawan->connect();
128132

129133
if (retcode == LORAWAN_STATUS_OK ||
130134
retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) {
@@ -161,7 +165,7 @@ static void send_message()
161165
packet_len = sprintf((char*) tx_buffer, "Dummy Sensor Value is %3.1f",
162166
sensor_value);
163167

164-
retcode = lorawan.send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len,
168+
retcode = lorawan->send(MBED_CONF_LORA_APP_PORT, tx_buffer, packet_len,
165169
MSG_CONFIRMED_FLAG);
166170

167171
if (retcode < 0) {
@@ -180,7 +184,7 @@ static void send_message()
180184
static void receive_message()
181185
{
182186
int16_t retcode;
183-
retcode = lorawan.receive(MBED_CONF_LORA_APP_PORT, rx_buffer,
187+
retcode = lorawan->receive(MBED_CONF_LORA_APP_PORT, rx_buffer,
184188
LORAMAC_PHY_MAXPAYLOAD,
185189
MSG_CONFIRMED_FLAG|MSG_UNCONFIRMED_FLAG);
186190

0 commit comments

Comments
 (0)