work1
Dependencies: mbed MAX44009 mbed-os Si7021
AppLora.cpp@0:d3e390d62607, 2020-05-27 (annotated)
- Committer:
- danaeb
- Date:
- Wed May 27 11:37:29 2020 +0000
- Revision:
- 0:d3e390d62607
create
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
danaeb | 0:d3e390d62607 | 1 | |
danaeb | 0:d3e390d62607 | 2 | #include <stdio.h> |
danaeb | 0:d3e390d62607 | 3 | |
danaeb | 0:d3e390d62607 | 4 | #include "lorawan/LoRaWANInterface.h" |
danaeb | 0:d3e390d62607 | 5 | #include "lorawan/system/lorawan_data_structures.h" |
danaeb | 0:d3e390d62607 | 6 | #include "events/EventQueue.h" |
danaeb | 0:d3e390d62607 | 7 | |
danaeb | 0:d3e390d62607 | 8 | #include "mbed.h" |
danaeb | 0:d3e390d62607 | 9 | #include "AppLora.h" |
danaeb | 0:d3e390d62607 | 10 | #include "AppMain.h" |
danaeb | 0:d3e390d62607 | 11 | #include "tools.h" |
danaeb | 0:d3e390d62607 | 12 | |
danaeb | 0:d3e390d62607 | 13 | // Application helpers |
danaeb | 0:d3e390d62607 | 14 | //#include "trace_helper.h" |
danaeb | 0:d3e390d62607 | 15 | #include "lora_radio_helper.h" |
danaeb | 0:d3e390d62607 | 16 | #include "hal/pinmap.h" |
danaeb | 0:d3e390d62607 | 17 | #include "PeripheralPins.h" |
danaeb | 0:d3e390d62607 | 18 | #include "board/PowerManager.h" |
danaeb | 0:d3e390d62607 | 19 | |
danaeb | 0:d3e390d62607 | 20 | #include "mbed_trace.h" |
danaeb | 0:d3e390d62607 | 21 | #define TRACE_GROUP "lora" |
danaeb | 0:d3e390d62607 | 22 | |
danaeb | 0:d3e390d62607 | 23 | #ifdef __cplusplus |
danaeb | 0:d3e390d62607 | 24 | extern "C" |
danaeb | 0:d3e390d62607 | 25 | { |
danaeb | 0:d3e390d62607 | 26 | #endif |
danaeb | 0:d3e390d62607 | 27 | |
danaeb | 0:d3e390d62607 | 28 | #include "tools.h" |
danaeb | 0:d3e390d62607 | 29 | |
danaeb | 0:d3e390d62607 | 30 | #ifdef __cplusplus |
danaeb | 0:d3e390d62607 | 31 | } |
danaeb | 0:d3e390d62607 | 32 | #endif |
danaeb | 0:d3e390d62607 | 33 | |
danaeb | 0:d3e390d62607 | 34 | |
danaeb | 0:d3e390d62607 | 35 | using namespace events; |
danaeb | 0:d3e390d62607 | 36 | EventQueue * AppLora::_queue; |
danaeb | 0:d3e390d62607 | 37 | Callback<void(AppLora::EVENT)> AppLora::_lora_callback; |
danaeb | 0:d3e390d62607 | 38 | DigitalInOut AppLora::_txco(LORA_TXCO); |
danaeb | 0:d3e390d62607 | 39 | |
danaeb | 0:d3e390d62607 | 40 | // Max payload size can be LORAMAC_PHY_MAXPAYLOAD. |
danaeb | 0:d3e390d62607 | 41 | // This example only communicates with much shorter messages (<30 bytes). |
danaeb | 0:d3e390d62607 | 42 | // If longer messages are used, these buffers must be changed accordingly. |
danaeb | 0:d3e390d62607 | 43 | uint8_t tx_buffer[30]; |
danaeb | 0:d3e390d62607 | 44 | uint8_t rx_buffer[30]; |
danaeb | 0:d3e390d62607 | 45 | |
danaeb | 0:d3e390d62607 | 46 | |
danaeb | 0:d3e390d62607 | 47 | /** |
danaeb | 0:d3e390d62607 | 48 | * Maximum number of retries for CONFIRMED messages before giving up |
danaeb | 0:d3e390d62607 | 49 | */ |
danaeb | 0:d3e390d62607 | 50 | #define CONFIRMED_MSG_RETRY_COUNTER 3 |
danaeb | 0:d3e390d62607 | 51 | |
danaeb | 0:d3e390d62607 | 52 | /** |
danaeb | 0:d3e390d62607 | 53 | * Constructing Mbed LoRaWANInterface and passing it down the radio object. |
danaeb | 0:d3e390d62607 | 54 | */ |
danaeb | 0:d3e390d62607 | 55 | static LoRaWANInterface lorawan(radio); |
danaeb | 0:d3e390d62607 | 56 | |
danaeb | 0:d3e390d62607 | 57 | /** |
danaeb | 0:d3e390d62607 | 58 | * Application specific callbacks |
danaeb | 0:d3e390d62607 | 59 | */ |
danaeb | 0:d3e390d62607 | 60 | lorawan_app_callbacks_t AppLora::_callbacks; |
danaeb | 0:d3e390d62607 | 61 | |
danaeb | 0:d3e390d62607 | 62 | void AppLora::_pin_active_mode(void){ |
danaeb | 0:d3e390d62607 | 63 | |
danaeb | 0:d3e390d62607 | 64 | // enable MOSI pin |
danaeb | 0:d3e390d62607 | 65 | pinmap_pinout(LORA_SPI_MOSI, PinMap_SPI_MOSI); |
danaeb | 0:d3e390d62607 | 66 | pinmap_pinout(LORA_SPI_MISO, PinMap_SPI_MISO); |
danaeb | 0:d3e390d62607 | 67 | pinmap_pinout(LORA_SPI_SCLK, PinMap_SPI_SCLK); |
danaeb | 0:d3e390d62607 | 68 | |
danaeb | 0:d3e390d62607 | 69 | // enable high precision oscillator (around 500uA) |
danaeb | 0:d3e390d62607 | 70 | _txco.mode(PullNone); |
danaeb | 0:d3e390d62607 | 71 | _txco = 1; |
danaeb | 0:d3e390d62607 | 72 | _txco.output(); |
danaeb | 0:d3e390d62607 | 73 | } |
danaeb | 0:d3e390d62607 | 74 | |
danaeb | 0:d3e390d62607 | 75 | void AppLora::_pin_lowpower_mode(void){ |
danaeb | 0:d3e390d62607 | 76 | DigitalIn lora_spi_miso(LORA_SPI_MISO); |
danaeb | 0:d3e390d62607 | 77 | DigitalIn lora_spi_mosi(LORA_SPI_MOSI); |
danaeb | 0:d3e390d62607 | 78 | |
danaeb | 0:d3e390d62607 | 79 | // That was a big mistake that consume 30µA |
danaeb | 0:d3e390d62607 | 80 | // It must be corrected on tag |
danaeb | 0:d3e390d62607 | 81 | //DigitalIn lora_spi_cs(SPI_MOSI); |
danaeb | 0:d3e390d62607 | 82 | //lora_spi_cs.mode(PullNone); |
danaeb | 0:d3e390d62607 | 83 | |
danaeb | 0:d3e390d62607 | 84 | lora_spi_miso.mode(PullNone); |
danaeb | 0:d3e390d62607 | 85 | lora_spi_mosi.mode(PullNone); |
danaeb | 0:d3e390d62607 | 86 | #if 0 |
danaeb | 0:d3e390d62607 | 87 | GPIO_InitTypeDef GPIO_InitStruct; |
danaeb | 0:d3e390d62607 | 88 | |
danaeb | 0:d3e390d62607 | 89 | GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; |
danaeb | 0:d3e390d62607 | 90 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
danaeb | 0:d3e390d62607 | 91 | |
danaeb | 0:d3e390d62607 | 92 | // for whatever reason the mosi pin draw approximatly 300uA. |
danaeb | 0:d3e390d62607 | 93 | // So it need to be disabled when loRa is not used |
danaeb | 0:d3e390d62607 | 94 | // disable LORA_MOSI_SPI |
danaeb | 0:d3e390d62607 | 95 | GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7 /* GPIO_PIN_7 | | GPIO_PIN_15*/; |
danaeb | 0:d3e390d62607 | 96 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
danaeb | 0:d3e390d62607 | 97 | |
danaeb | 0:d3e390d62607 | 98 | GPIO_InitStruct.Pin = GPIO_PIN_3; |
danaeb | 0:d3e390d62607 | 99 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
danaeb | 0:d3e390d62607 | 100 | |
danaeb | 0:d3e390d62607 | 101 | #endif |
danaeb | 0:d3e390d62607 | 102 | |
danaeb | 0:d3e390d62607 | 103 | // disable high precision oscillator |
danaeb | 0:d3e390d62607 | 104 | _txco = 0; |
danaeb | 0:d3e390d62607 | 105 | _txco.input(); |
danaeb | 0:d3e390d62607 | 106 | } |
danaeb | 0:d3e390d62607 | 107 | |
danaeb | 0:d3e390d62607 | 108 | void AppLora::_enter_lowpower_mode(void){ |
danaeb | 0:d3e390d62607 | 109 | _pin_lowpower_mode(); |
danaeb | 0:d3e390d62607 | 110 | PowerManager *pm= PowerManager::get_instance(); |
danaeb | 0:d3e390d62607 | 111 | pm->sleep_mode(); |
danaeb | 0:d3e390d62607 | 112 | } |
danaeb | 0:d3e390d62607 | 113 | |
danaeb | 0:d3e390d62607 | 114 | void AppLora::_enter_active_mode(void){ |
danaeb | 0:d3e390d62607 | 115 | PowerManager *pm= PowerManager::get_instance(); |
danaeb | 0:d3e390d62607 | 116 | pm->run_mode(); |
danaeb | 0:d3e390d62607 | 117 | _pin_active_mode(); |
danaeb | 0:d3e390d62607 | 118 | } |
danaeb | 0:d3e390d62607 | 119 | |
danaeb | 0:d3e390d62607 | 120 | void AppLora::initialize(EventQueue *queue) |
danaeb | 0:d3e390d62607 | 121 | { |
danaeb | 0:d3e390d62607 | 122 | _queue = queue; |
danaeb | 0:d3e390d62607 | 123 | |
danaeb | 0:d3e390d62607 | 124 | _enter_active_mode(); |
danaeb | 0:d3e390d62607 | 125 | |
danaeb | 0:d3e390d62607 | 126 | tr_debug("setting voltage to 3V"); |
danaeb | 0:d3e390d62607 | 127 | // setup tracing |
danaeb | 0:d3e390d62607 | 128 | //setup_trace(); |
danaeb | 0:d3e390d62607 | 129 | |
danaeb | 0:d3e390d62607 | 130 | // Initialize LoRaWAN stack |
danaeb | 0:d3e390d62607 | 131 | if (lorawan.initialize(_queue) != LORAWAN_STATUS_OK) { |
danaeb | 0:d3e390d62607 | 132 | tr_err("LoRa initialization failed!"); |
danaeb | 0:d3e390d62607 | 133 | //return -1; |
danaeb | 0:d3e390d62607 | 134 | } |
danaeb | 0:d3e390d62607 | 135 | |
danaeb | 0:d3e390d62607 | 136 | tr_info("mbed LoRaWANStack initialized"); |
danaeb | 0:d3e390d62607 | 137 | |
danaeb | 0:d3e390d62607 | 138 | // prepare application callbacks |
danaeb | 0:d3e390d62607 | 139 | _callbacks.events = mbed::callback(AppLora::_lora_event_handler); |
danaeb | 0:d3e390d62607 | 140 | lorawan.add_app_callbacks(&_callbacks); |
danaeb | 0:d3e390d62607 | 141 | |
danaeb | 0:d3e390d62607 | 142 | // Set number of retries in case of CONFIRMED messages |
danaeb | 0:d3e390d62607 | 143 | if (lorawan.set_confirmed_msg_retries(CONFIRMED_MSG_RETRY_COUNTER) |
danaeb | 0:d3e390d62607 | 144 | != LORAWAN_STATUS_OK) { |
danaeb | 0:d3e390d62607 | 145 | tr_err("set_confirmed_msg_retries failed!"); |
danaeb | 0:d3e390d62607 | 146 | //return -1; |
danaeb | 0:d3e390d62607 | 147 | } |
danaeb | 0:d3e390d62607 | 148 | |
danaeb | 0:d3e390d62607 | 149 | tr_debug("confirmed message retries : %d", CONFIRMED_MSG_RETRY_COUNTER); |
danaeb | 0:d3e390d62607 | 150 | |
danaeb | 0:d3e390d62607 | 151 | // Enable adaptive data rate |
danaeb | 0:d3e390d62607 | 152 | /* |
danaeb | 0:d3e390d62607 | 153 | if (lorawan.enable_adaptive_datarate() != LORAWAN_STATUS_OK) { |
danaeb | 0:d3e390d62607 | 154 | PRINT("\r\n enable_adaptive_datarate failed! \r\n"); |
danaeb | 0:d3e390d62607 | 155 | //return -1; |
danaeb | 0:d3e390d62607 | 156 | } |
danaeb | 0:d3e390d62607 | 157 | */ |
danaeb | 0:d3e390d62607 | 158 | |
danaeb | 0:d3e390d62607 | 159 | //PRINT("\r\n Adaptive data rate (ADR) - Enabled \r\n"); |
danaeb | 0:d3e390d62607 | 160 | |
danaeb | 0:d3e390d62607 | 161 | _enter_lowpower_mode(); |
danaeb | 0:d3e390d62607 | 162 | |
danaeb | 0:d3e390d62607 | 163 | } |
danaeb | 0:d3e390d62607 | 164 | u32 AppLora::get_random(void){ |
danaeb | 0:d3e390d62607 | 165 | u32 rand; |
danaeb | 0:d3e390d62607 | 166 | _enter_active_mode(); |
danaeb | 0:d3e390d62607 | 167 | rand = radio.random(); |
danaeb | 0:d3e390d62607 | 168 | _enter_lowpower_mode(); |
danaeb | 0:d3e390d62607 | 169 | return rand; |
danaeb | 0:d3e390d62607 | 170 | |
danaeb | 0:d3e390d62607 | 171 | } |
danaeb | 0:d3e390d62607 | 172 | |
danaeb | 0:d3e390d62607 | 173 | u8 AppLora::get_next_transmission_max_size(void){ |
danaeb | 0:d3e390d62607 | 174 | /* |
danaeb | 0:d3e390d62607 | 175 | * find a better way to get the max payload size |
danaeb | 0:d3e390d62607 | 176 | */ |
danaeb | 0:d3e390d62607 | 177 | #if 0 |
danaeb | 0:d3e390d62607 | 178 | lorawan_tx_metadata meta; |
danaeb | 0:d3e390d62607 | 179 | lorawan.get_tx_metadata(meta); |
danaeb | 0:d3e390d62607 | 180 | return meta.max_payload; |
danaeb | 0:d3e390d62607 | 181 | #else |
danaeb | 0:d3e390d62607 | 182 | return 51; // minimum payload size |
danaeb | 0:d3e390d62607 | 183 | #endif |
danaeb | 0:d3e390d62607 | 184 | |
danaeb | 0:d3e390d62607 | 185 | |
danaeb | 0:d3e390d62607 | 186 | } |
danaeb | 0:d3e390d62607 | 187 | |
danaeb | 0:d3e390d62607 | 188 | void AppLora::connect(void){ |
danaeb | 0:d3e390d62607 | 189 | lorawan_status_t retcode; |
danaeb | 0:d3e390d62607 | 190 | _enter_active_mode(); |
danaeb | 0:d3e390d62607 | 191 | retcode = lorawan.connect(); |
danaeb | 0:d3e390d62607 | 192 | |
danaeb | 0:d3e390d62607 | 193 | if (retcode == LORAWAN_STATUS_OK || |
danaeb | 0:d3e390d62607 | 194 | retcode == LORAWAN_STATUS_CONNECT_IN_PROGRESS) { |
danaeb | 0:d3e390d62607 | 195 | } else { |
danaeb | 0:d3e390d62607 | 196 | tr_err("connection error, code = %d", retcode); |
danaeb | 0:d3e390d62607 | 197 | _enter_lowpower_mode(); |
danaeb | 0:d3e390d62607 | 198 | _call_callback(AppLora::EVENT_CONNECTION_ERROR); |
danaeb | 0:d3e390d62607 | 199 | //return -1; |
danaeb | 0:d3e390d62607 | 200 | return; |
danaeb | 0:d3e390d62607 | 201 | } |
danaeb | 0:d3e390d62607 | 202 | |
danaeb | 0:d3e390d62607 | 203 | tr_info("connection - in progress..."); |
danaeb | 0:d3e390d62607 | 204 | } |
danaeb | 0:d3e390d62607 | 205 | |
danaeb | 0:d3e390d62607 | 206 | void AppLora::disconnect(void){ |
danaeb | 0:d3e390d62607 | 207 | lorawan.cancel_sending(); |
danaeb | 0:d3e390d62607 | 208 | lorawan.disconnect(); |
danaeb | 0:d3e390d62607 | 209 | } |
danaeb | 0:d3e390d62607 | 210 | |
danaeb | 0:d3e390d62607 | 211 | /** |
danaeb | 0:d3e390d62607 | 212 | * Sends a message to the Network Server |
danaeb | 0:d3e390d62607 | 213 | */ |
danaeb | 0:d3e390d62607 | 214 | s16 AppLora::send_message(u8 *data, u8 data_size, u8 app_port, bool confirmed) |
danaeb | 0:d3e390d62607 | 215 | { |
danaeb | 0:d3e390d62607 | 216 | s16 retcode; |
danaeb | 0:d3e390d62607 | 217 | static u8 would_block_error_count = 0; |
danaeb | 0:d3e390d62607 | 218 | |
danaeb | 0:d3e390d62607 | 219 | int flags = MSG_UNCONFIRMED_FLAG; |
danaeb | 0:d3e390d62607 | 220 | tr_info("sending message"); |
danaeb | 0:d3e390d62607 | 221 | tr_debug("\t confirmed: %d", confirmed); |
danaeb | 0:d3e390d62607 | 222 | tr_debug("\t port: 0x%02x", app_port); |
danaeb | 0:d3e390d62607 | 223 | tr_debug("\t size: %d", data_size); |
danaeb | 0:d3e390d62607 | 224 | tr_info("\t data: "); |
danaeb | 0:d3e390d62607 | 225 | tools_print_to_hex(data, data_size); |
danaeb | 0:d3e390d62607 | 226 | if(confirmed){ |
danaeb | 0:d3e390d62607 | 227 | flags = MSG_CONFIRMED_FLAG; |
danaeb | 0:d3e390d62607 | 228 | } |
danaeb | 0:d3e390d62607 | 229 | _enter_active_mode(); |
danaeb | 0:d3e390d62607 | 230 | |
danaeb | 0:d3e390d62607 | 231 | retcode = lorawan.send(app_port, data, data_size, flags); |
danaeb | 0:d3e390d62607 | 232 | if(retcode < 0){ |
danaeb | 0:d3e390d62607 | 233 | /* |
danaeb | 0:d3e390d62607 | 234 | * LoRa error code are between -1000 and -1024 |
danaeb | 0:d3e390d62607 | 235 | * -(retcode+1000) convert the error in a number between |
danaeb | 0:d3e390d62607 | 236 | * 1 and 24 |
danaeb | 0:d3e390d62607 | 237 | * |
danaeb | 0:d3e390d62607 | 238 | * Then the value 1 is saved @ the virtual address va between 1 and 24 |
danaeb | 0:d3e390d62607 | 239 | */ |
danaeb | 0:d3e390d62607 | 240 | tr_warn("\t error : %d", retcode); |
danaeb | 0:d3e390d62607 | 241 | } |
danaeb | 0:d3e390d62607 | 242 | |
danaeb | 0:d3e390d62607 | 243 | if(retcode == LORAWAN_STATUS_WOULD_BLOCK){ |
danaeb | 0:d3e390d62607 | 244 | ++would_block_error_count; |
danaeb | 0:d3e390d62607 | 245 | // Reset only if this error appear three time in a row |
danaeb | 0:d3e390d62607 | 246 | // Because in some case this error can appear during the join and this |
danaeb | 0:d3e390d62607 | 247 | // is the expected behaviour |
danaeb | 0:d3e390d62607 | 248 | if(would_block_error_count > 2){ |
danaeb | 0:d3e390d62607 | 249 | NVIC_SystemReset(); |
danaeb | 0:d3e390d62607 | 250 | } |
danaeb | 0:d3e390d62607 | 251 | } |
danaeb | 0:d3e390d62607 | 252 | else if(retcode >= 0){ |
danaeb | 0:d3e390d62607 | 253 | would_block_error_count = 0; |
danaeb | 0:d3e390d62607 | 254 | } |
danaeb | 0:d3e390d62607 | 255 | |
danaeb | 0:d3e390d62607 | 256 | return retcode; |
danaeb | 0:d3e390d62607 | 257 | |
danaeb | 0:d3e390d62607 | 258 | } |
danaeb | 0:d3e390d62607 | 259 | |
danaeb | 0:d3e390d62607 | 260 | /** |
danaeb | 0:d3e390d62607 | 261 | * Receive a message from the Network Server |
danaeb | 0:d3e390d62607 | 262 | */ |
danaeb | 0:d3e390d62607 | 263 | s16 AppLora::get_rx_message(u8 *data, u16 data_size, u8 &app_port) |
danaeb | 0:d3e390d62607 | 264 | { |
danaeb | 0:d3e390d62607 | 265 | s16 received_size; |
danaeb | 0:d3e390d62607 | 266 | int flags; |
danaeb | 0:d3e390d62607 | 267 | received_size = lorawan.receive(data, data_size, app_port, flags); |
danaeb | 0:d3e390d62607 | 268 | |
danaeb | 0:d3e390d62607 | 269 | if (received_size < 0) { |
danaeb | 0:d3e390d62607 | 270 | tr_err("receive() - Error code %d", received_size); |
danaeb | 0:d3e390d62607 | 271 | // return; |
danaeb | 0:d3e390d62607 | 272 | } |
danaeb | 0:d3e390d62607 | 273 | else{ |
danaeb | 0:d3e390d62607 | 274 | tr_info("received message"); |
danaeb | 0:d3e390d62607 | 275 | tr_debug("\t flags: %d", flags); |
danaeb | 0:d3e390d62607 | 276 | tr_debug("\t port: %d", app_port); |
danaeb | 0:d3e390d62607 | 277 | tr_debug("\t max size: %d", data_size); |
danaeb | 0:d3e390d62607 | 278 | tr_debug("\t received size: %d", received_size); |
danaeb | 0:d3e390d62607 | 279 | tr_info("\t data: "); |
danaeb | 0:d3e390d62607 | 280 | tools_print_to_hex(data, received_size); |
danaeb | 0:d3e390d62607 | 281 | } |
danaeb | 0:d3e390d62607 | 282 | return received_size; |
danaeb | 0:d3e390d62607 | 283 | |
danaeb | 0:d3e390d62607 | 284 | } |
danaeb | 0:d3e390d62607 | 285 | |
danaeb | 0:d3e390d62607 | 286 | void AppLora::_send_empty_uplink(void){ |
danaeb | 0:d3e390d62607 | 287 | int retcode; |
danaeb | 0:d3e390d62607 | 288 | _enter_active_mode(); |
danaeb | 0:d3e390d62607 | 289 | //wait(0.05); |
danaeb | 0:d3e390d62607 | 290 | wait_us(50000); |
danaeb | 0:d3e390d62607 | 291 | // we could add more information in this frame |
danaeb | 0:d3e390d62607 | 292 | retcode = lorawan.send(1, NULL, 0, MSG_CONFIRMED_FLAG); |
danaeb | 0:d3e390d62607 | 293 | if (retcode < 0) { |
danaeb | 0:d3e390d62607 | 294 | tr_err("uplink send() - Error code %d", retcode); |
danaeb | 0:d3e390d62607 | 295 | _enter_lowpower_mode(); |
danaeb | 0:d3e390d62607 | 296 | |
danaeb | 0:d3e390d62607 | 297 | // return; |
danaeb | 0:d3e390d62607 | 298 | } |
danaeb | 0:d3e390d62607 | 299 | //_enter_lowpower_mode(); |
danaeb | 0:d3e390d62607 | 300 | |
danaeb | 0:d3e390d62607 | 301 | } |
danaeb | 0:d3e390d62607 | 302 | |
danaeb | 0:d3e390d62607 | 303 | void AppLora::set_lora_callback(Callback<void(AppLora::EVENT)> lora_callback){ |
danaeb | 0:d3e390d62607 | 304 | _lora_callback = lora_callback; |
danaeb | 0:d3e390d62607 | 305 | } |
danaeb | 0:d3e390d62607 | 306 | |
danaeb | 0:d3e390d62607 | 307 | void AppLora::_call_callback(AppLora::EVENT lora_event){ |
danaeb | 0:d3e390d62607 | 308 | if(_lora_callback){ |
danaeb | 0:d3e390d62607 | 309 | _lora_callback(lora_event); |
danaeb | 0:d3e390d62607 | 310 | } |
danaeb | 0:d3e390d62607 | 311 | } |
danaeb | 0:d3e390d62607 | 312 | /** |
danaeb | 0:d3e390d62607 | 313 | * Event handler |
danaeb | 0:d3e390d62607 | 314 | */ |
danaeb | 0:d3e390d62607 | 315 | void AppLora::_lora_event_handler(lorawan_event_t event) |
danaeb | 0:d3e390d62607 | 316 | { |
danaeb | 0:d3e390d62607 | 317 | switch (event) { |
danaeb | 0:d3e390d62607 | 318 | case CONNECTED: |
danaeb | 0:d3e390d62607 | 319 | tr_info("connection - successful"); |
danaeb | 0:d3e390d62607 | 320 | _enter_lowpower_mode(); |
danaeb | 0:d3e390d62607 | 321 | _call_callback(AppLora::EVENT_CONNECTED); |
danaeb | 0:d3e390d62607 | 322 | |
danaeb | 0:d3e390d62607 | 323 | break; |
danaeb | 0:d3e390d62607 | 324 | case DISCONNECTED: |
danaeb | 0:d3e390d62607 | 325 | //AppLora::_queue->break_dispatch(); |
danaeb | 0:d3e390d62607 | 326 | tr_info("disconnected successfully"); |
danaeb | 0:d3e390d62607 | 327 | _enter_lowpower_mode(); |
danaeb | 0:d3e390d62607 | 328 | _call_callback(AppLora::EVENT_DISCONNECTED); |
danaeb | 0:d3e390d62607 | 329 | break; |
danaeb | 0:d3e390d62607 | 330 | case TX_DONE: |
danaeb | 0:d3e390d62607 | 331 | tr_info("message sent to network server"); |
danaeb | 0:d3e390d62607 | 332 | _enter_lowpower_mode(); |
danaeb | 0:d3e390d62607 | 333 | _call_callback(AppLora::EVENT_TX_DONE); |
danaeb | 0:d3e390d62607 | 334 | break; |
danaeb | 0:d3e390d62607 | 335 | case UPLINK_REQUIRED: |
danaeb | 0:d3e390d62607 | 336 | // Uplink are requested when the pending bit is set |
danaeb | 0:d3e390d62607 | 337 | // or when more downlink messages are waiting |
danaeb | 0:d3e390d62607 | 338 | // ACK seems to be handled directly by the stack |
danaeb | 0:d3e390d62607 | 339 | tr_info("LoRaStack requesting uplink"); |
danaeb | 0:d3e390d62607 | 340 | _send_empty_uplink(); |
danaeb | 0:d3e390d62607 | 341 | _call_callback(AppLora::EVENT_UPLINK_REQUIERED); |
danaeb | 0:d3e390d62607 | 342 | break; |
danaeb | 0:d3e390d62607 | 343 | case TX_TIMEOUT: |
danaeb | 0:d3e390d62607 | 344 | case TX_ERROR: |
danaeb | 0:d3e390d62607 | 345 | case TX_CRYPTO_ERROR: |
danaeb | 0:d3e390d62607 | 346 | case TX_SCHEDULING_ERROR: |
danaeb | 0:d3e390d62607 | 347 | tr_err("transmission error - event code = %d", event); |
danaeb | 0:d3e390d62607 | 348 | _enter_lowpower_mode(); |
danaeb | 0:d3e390d62607 | 349 | _call_callback(AppLora::EVENT_TX_ERROR); |
danaeb | 0:d3e390d62607 | 350 | break; |
danaeb | 0:d3e390d62607 | 351 | case RX_DONE: |
danaeb | 0:d3e390d62607 | 352 | tr_info("received message from network server"); |
danaeb | 0:d3e390d62607 | 353 | AppMain::incoming_lora_message_callback(); |
danaeb | 0:d3e390d62607 | 354 | _enter_lowpower_mode(); |
danaeb | 0:d3e390d62607 | 355 | _call_callback(AppLora::EVENT_RX_DONE); |
danaeb | 0:d3e390d62607 | 356 | //receive_message(); |
danaeb | 0:d3e390d62607 | 357 | break; |
danaeb | 0:d3e390d62607 | 358 | case RX_TIMEOUT: |
danaeb | 0:d3e390d62607 | 359 | case RX_ERROR: |
danaeb | 0:d3e390d62607 | 360 | tr_err("error in reception - code = %d", event); |
danaeb | 0:d3e390d62607 | 361 | _enter_lowpower_mode(); |
danaeb | 0:d3e390d62607 | 362 | _call_callback(AppLora::EVENT_RX_ERROR); |
danaeb | 0:d3e390d62607 | 363 | break; |
danaeb | 0:d3e390d62607 | 364 | case JOIN_FAILURE: |
danaeb | 0:d3e390d62607 | 365 | tr_err("OTAA failed - check keys"); |
danaeb | 0:d3e390d62607 | 366 | _enter_lowpower_mode(); |
danaeb | 0:d3e390d62607 | 367 | _call_callback(AppLora::EVENT_JOIN_FAILURE); |
danaeb | 0:d3e390d62607 | 368 | break; |
danaeb | 0:d3e390d62607 | 369 | default: |
danaeb | 0:d3e390d62607 | 370 | MBED_ASSERT("Unknown Event"); |
danaeb | 0:d3e390d62607 | 371 | _enter_lowpower_mode(); |
danaeb | 0:d3e390d62607 | 372 | } |
danaeb | 0:d3e390d62607 | 373 | } |
danaeb | 0:d3e390d62607 | 374 | |
danaeb | 0:d3e390d62607 | 375 | // EOF |
danaeb | 0:d3e390d62607 | 376 | |
danaeb | 0:d3e390d62607 | 377 |