Ottawa Bootcamp Publish
Dependencies: SX127x lorawan1v1
sensorDemoVT100.cpp@2:da3c8d5b3f49, 2018-03-05 (annotated)
- Committer:
- Wayne Roberts
- Date:
- Mon Mar 05 16:51:20 2018 -0800
- Revision:
- 2:da3c8d5b3f49
- Parent:
- 1:3c1d13a0489e
- Child:
- 3:fc23bbc14475
update for current lorawan1v1 library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Wayne Roberts |
0:62e456e60083 | 1 | /* |
Wayne Roberts |
0:62e456e60083 | 2 | / _____) _ | | |
Wayne Roberts |
0:62e456e60083 | 3 | ( (____ _____ ____ _| |_ _____ ____| |__ |
Wayne Roberts |
0:62e456e60083 | 4 | \____ \| ___ | (_ _) ___ |/ ___) _ \ |
Wayne Roberts |
0:62e456e60083 | 5 | _____) ) ____| | | || |_| ____( (___| | | | |
Wayne Roberts |
0:62e456e60083 | 6 | (______/|_____)_|_|_| \__)_____)\____)_| |_| |
Wayne Roberts |
0:62e456e60083 | 7 | (C)2018 Semtech |
Wayne Roberts |
0:62e456e60083 | 8 | |
Wayne Roberts |
0:62e456e60083 | 9 | Description: LoRaMac classB device implementation |
Wayne Roberts |
0:62e456e60083 | 10 | |
Wayne Roberts |
0:62e456e60083 | 11 | License: Revised BSD License, see LICENSE.TXT file include in the project |
Wayne Roberts |
0:62e456e60083 | 12 | |
Wayne Roberts |
0:62e456e60083 | 13 | */ |
Wayne Roberts |
0:62e456e60083 | 14 | |
Wayne Roberts |
0:62e456e60083 | 15 | #include "LoRaMac.h" |
Wayne Roberts |
0:62e456e60083 | 16 | #include "SerialDisplay.h" |
Wayne Roberts |
0:62e456e60083 | 17 | #include "LoRaMacString.h" |
Wayne Roberts |
0:62e456e60083 | 18 | #ifdef ENABLE_VT100 |
Wayne Roberts |
0:62e456e60083 | 19 | |
Wayne Roberts |
0:62e456e60083 | 20 | /*! |
Wayne Roberts |
0:62e456e60083 | 21 | * Defines the application data transmission duty cycle. 5s, value in [ms]. |
Wayne Roberts |
0:62e456e60083 | 22 | */ |
Wayne Roberts |
0:62e456e60083 | 23 | #define APP_TX_DUTYCYCLE_us 8000000 |
Wayne Roberts |
0:62e456e60083 | 24 | |
Wayne Roberts |
0:62e456e60083 | 25 | /*! |
Wayne Roberts |
0:62e456e60083 | 26 | * Defines a random delay for application data transmission duty cycle. 1s, |
Wayne Roberts |
0:62e456e60083 | 27 | * value in [ms]. |
Wayne Roberts |
0:62e456e60083 | 28 | */ |
Wayne Roberts |
0:62e456e60083 | 29 | #define APP_TX_DUTYCYCLE_RND_us 2000000 |
Wayne Roberts |
0:62e456e60083 | 30 | |
Wayne Roberts |
0:62e456e60083 | 31 | /*! |
Wayne Roberts |
0:62e456e60083 | 32 | * Default datarate |
Wayne Roberts |
0:62e456e60083 | 33 | */ |
Wayne Roberts |
0:62e456e60083 | 34 | |
Wayne Roberts |
0:62e456e60083 | 35 | #if defined( USE_BAND_ARIB_8CH ) |
Wayne Roberts |
0:62e456e60083 | 36 | #define LORAWAN_DEFAULT_DATARATE DR_3 |
Wayne Roberts |
0:62e456e60083 | 37 | #else |
Wayne Roberts |
0:62e456e60083 | 38 | #define LORAWAN_DEFAULT_DATARATE DR_0 |
Wayne Roberts |
0:62e456e60083 | 39 | #endif |
Wayne Roberts |
0:62e456e60083 | 40 | |
Wayne Roberts |
0:62e456e60083 | 41 | /*! |
Wayne Roberts |
0:62e456e60083 | 42 | * LoRaWAN confirmed messages |
Wayne Roberts |
0:62e456e60083 | 43 | */ |
Wayne Roberts |
0:62e456e60083 | 44 | #define LORAWAN_CONFIRMED_MSG_ON true |
Wayne Roberts |
0:62e456e60083 | 45 | |
Wayne Roberts |
0:62e456e60083 | 46 | /*! |
Wayne Roberts |
0:62e456e60083 | 47 | * LoRaWAN Adaptive Data Rate |
Wayne Roberts |
0:62e456e60083 | 48 | * |
Wayne Roberts |
0:62e456e60083 | 49 | * \remark Please note that when ADR is enabled the end-device should be static |
Wayne Roberts |
0:62e456e60083 | 50 | */ |
Wayne Roberts |
0:62e456e60083 | 51 | #define LORAWAN_ADR_ON 1 |
Wayne Roberts |
0:62e456e60083 | 52 | |
Wayne Roberts |
0:62e456e60083 | 53 | #if defined( USE_BAND_868 ) |
Wayne Roberts |
0:62e456e60083 | 54 | |
Wayne Roberts |
0:62e456e60083 | 55 | /*! |
Wayne Roberts |
0:62e456e60083 | 56 | * LoRaWAN ETSI duty cycle control enable/disable |
Wayne Roberts |
0:62e456e60083 | 57 | * |
Wayne Roberts |
0:62e456e60083 | 58 | * \remark Please note that ETSI mandates duty cycled transmissions. Use only for test purposes |
Wayne Roberts |
0:62e456e60083 | 59 | */ |
Wayne Roberts |
0:62e456e60083 | 60 | #define LORAWAN_DUTYCYCLE_ON false |
Wayne Roberts |
0:62e456e60083 | 61 | |
Wayne Roberts |
0:62e456e60083 | 62 | #define USE_SEMTECH_DEFAULT_CHANNEL_LINEUP 1 |
Wayne Roberts |
0:62e456e60083 | 63 | |
Wayne Roberts |
0:62e456e60083 | 64 | #if( USE_SEMTECH_DEFAULT_CHANNEL_LINEUP == 1 ) |
Wayne Roberts |
0:62e456e60083 | 65 | |
Wayne Roberts |
0:62e456e60083 | 66 | #define LC4 { 867100000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } |
Wayne Roberts |
0:62e456e60083 | 67 | #define LC5 { 867300000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } |
Wayne Roberts |
0:62e456e60083 | 68 | #define LC6 { 867500000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } |
Wayne Roberts |
0:62e456e60083 | 69 | #define LC7 { 867700000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } |
Wayne Roberts |
0:62e456e60083 | 70 | #define LC8 { 867900000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } |
Wayne Roberts |
0:62e456e60083 | 71 | #define LC9 { 868800000, { ( ( DR_7 << 4 ) | DR_7 ) }, 2 } |
Wayne Roberts |
0:62e456e60083 | 72 | #define LC10 { 868300000, { ( ( DR_6 << 4 ) | DR_6 ) }, 1 } |
Wayne Roberts |
0:62e456e60083 | 73 | |
Wayne Roberts |
0:62e456e60083 | 74 | #endif |
Wayne Roberts |
0:62e456e60083 | 75 | |
Wayne Roberts |
0:62e456e60083 | 76 | #endif |
Wayne Roberts |
0:62e456e60083 | 77 | |
Wayne Roberts |
0:62e456e60083 | 78 | /*! |
Wayne Roberts |
0:62e456e60083 | 79 | * LoRaWAN application port |
Wayne Roberts |
0:62e456e60083 | 80 | */ |
Wayne Roberts |
0:62e456e60083 | 81 | #define LORAWAN_APP_PORT 2 |
Wayne Roberts |
0:62e456e60083 | 82 | |
Wayne Roberts |
0:62e456e60083 | 83 | /*! |
Wayne Roberts |
0:62e456e60083 | 84 | * User application data buffer size |
Wayne Roberts |
0:62e456e60083 | 85 | */ |
Wayne Roberts |
0:62e456e60083 | 86 | #define LORAWAN_APP_DATA_SIZE 3 |
Wayne Roberts |
0:62e456e60083 | 87 | |
Wayne Roberts |
0:62e456e60083 | 88 | |
Wayne Roberts |
0:62e456e60083 | 89 | #ifdef LORAWAN_JOIN_EUI |
Wayne Roberts |
0:62e456e60083 | 90 | static uint8_t DevEui[] = LORAWAN_DEVICE_EUI; |
Wayne Roberts |
0:62e456e60083 | 91 | static const uint8_t JoinEui[] = LORAWAN_JOIN_EUI; |
Wayne Roberts |
0:62e456e60083 | 92 | static const uint8_t NwkKey[] = LORAWAN_ROOT_NWKKEY; |
Wayne Roberts |
0:62e456e60083 | 93 | #ifdef LORAWAN_ROOT_APPKEY |
Wayne Roberts |
0:62e456e60083 | 94 | static const uint8_t AppKey[] = LORAWAN_ROOT_APPKEY; |
Wayne Roberts |
0:62e456e60083 | 95 | #endif |
Wayne Roberts |
0:62e456e60083 | 96 | #else |
Wayne Roberts |
0:62e456e60083 | 97 | static const uint8_t FNwkSIntKey[] = LORAWAN_FNwkSIntKey; |
Wayne Roberts |
0:62e456e60083 | 98 | static const uint8_t AppSKey[] = LORAWAN_APPSKEY; |
Wayne Roberts |
0:62e456e60083 | 99 | static const uint32_t DevAddr = LORAWAN_DEVICE_ADDRESS; |
Wayne Roberts |
0:62e456e60083 | 100 | #if defined(LORAWAN_SNwkSIntKey) && defined(LORAWAN_NwkSEncKey) |
Wayne Roberts |
0:62e456e60083 | 101 | static const uint8_t SNwkSIntKey[] = LORAWAN_SNwkSIntKey; |
Wayne Roberts |
0:62e456e60083 | 102 | static const uint8_t NwkSEncKey[] = LORAWAN_NwkSEncKey; |
Wayne Roberts |
0:62e456e60083 | 103 | #endif |
Wayne Roberts |
0:62e456e60083 | 104 | #endif |
Wayne Roberts |
0:62e456e60083 | 105 | |
Wayne Roberts |
0:62e456e60083 | 106 | /*! |
Wayne Roberts |
0:62e456e60083 | 107 | * Application port |
Wayne Roberts |
0:62e456e60083 | 108 | */ |
Wayne Roberts |
0:62e456e60083 | 109 | static uint8_t AppPort = LORAWAN_APP_PORT; |
Wayne Roberts |
0:62e456e60083 | 110 | |
Wayne Roberts |
0:62e456e60083 | 111 | /*! |
Wayne Roberts |
0:62e456e60083 | 112 | * User application data size |
Wayne Roberts |
0:62e456e60083 | 113 | */ |
Wayne Roberts |
0:62e456e60083 | 114 | static uint8_t gAppDataSize = LORAWAN_APP_DATA_SIZE; |
Wayne Roberts |
0:62e456e60083 | 115 | |
Wayne Roberts |
0:62e456e60083 | 116 | /*! |
Wayne Roberts |
0:62e456e60083 | 117 | * User application data buffer size |
Wayne Roberts |
0:62e456e60083 | 118 | */ |
Wayne Roberts |
0:62e456e60083 | 119 | #define LORAWAN_APP_DATA_MAX_SIZE 64 |
Wayne Roberts |
0:62e456e60083 | 120 | |
Wayne Roberts |
0:62e456e60083 | 121 | /*! |
Wayne Roberts |
0:62e456e60083 | 122 | * User application data |
Wayne Roberts |
0:62e456e60083 | 123 | */ |
Wayne Roberts |
0:62e456e60083 | 124 | static uint8_t AppData[LORAWAN_APP_DATA_MAX_SIZE]; |
Wayne Roberts |
0:62e456e60083 | 125 | |
Wayne Roberts |
0:62e456e60083 | 126 | /*! |
Wayne Roberts |
0:62e456e60083 | 127 | * Indicates if the node is sending confirmed or unconfirmed messages |
Wayne Roberts |
0:62e456e60083 | 128 | */ |
Wayne Roberts |
0:62e456e60083 | 129 | static uint8_t gIsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON; |
Wayne Roberts |
0:62e456e60083 | 130 | |
Wayne Roberts |
0:62e456e60083 | 131 | /*! |
Wayne Roberts |
0:62e456e60083 | 132 | * Timer to handle the application data transmission duty cycle |
Wayne Roberts |
0:62e456e60083 | 133 | * |
Wayne Roberts |
0:62e456e60083 | 134 | */ |
Wayne Roberts |
0:62e456e60083 | 135 | LowPowerTimeout tx_timeout; |
Wayne Roberts |
0:62e456e60083 | 136 | |
Wayne Roberts |
0:62e456e60083 | 137 | /*! |
Wayne Roberts |
0:62e456e60083 | 138 | * Indicates if a new packet can be sent |
Wayne Roberts |
0:62e456e60083 | 139 | */ |
Wayne Roberts |
0:62e456e60083 | 140 | static volatile struct { |
Wayne Roberts |
0:62e456e60083 | 141 | uint8_t gmi : 1; |
Wayne Roberts |
0:62e456e60083 | 142 | uint8_t gmc : 1; |
Wayne Roberts |
0:62e456e60083 | 143 | } flags; |
Wayne Roberts |
0:62e456e60083 | 144 | |
Wayne Roberts |
0:62e456e60083 | 145 | /*! |
Wayne Roberts |
0:62e456e60083 | 146 | * Device states |
Wayne Roberts |
0:62e456e60083 | 147 | */ |
Wayne Roberts |
0:62e456e60083 | 148 | volatile enum eDevicState |
Wayne Roberts |
0:62e456e60083 | 149 | { |
Wayne Roberts |
0:62e456e60083 | 150 | /* 0 */ DEVICE_STATE_INIT = 0, |
Wayne Roberts |
0:62e456e60083 | 151 | /* 1 */ DEVICE_STATE_SEND, |
Wayne Roberts |
0:62e456e60083 | 152 | /* 2 */ DEVICE_STATE_TRIGGER, |
Wayne Roberts |
0:62e456e60083 | 153 | /* 3 */ DEVICE_STATE_SLEEP, |
Wayne Roberts |
0:62e456e60083 | 154 | #ifdef LORAWAN_JOIN_EUI |
Wayne Roberts |
0:62e456e60083 | 155 | /* 4 */ DEVICE_STATE_JOIN, |
Wayne Roberts |
0:62e456e60083 | 156 | /* 5 */ DEVICE_STATE_JOIN_OK |
Wayne Roberts |
0:62e456e60083 | 157 | #endif /* LORAWAN_JOIN_EUI */ |
Wayne Roberts |
0:62e456e60083 | 158 | } DeviceState, WakeUpState; |
Wayne Roberts |
0:62e456e60083 | 159 | |
Wayne Roberts |
0:62e456e60083 | 160 | #if defined(TARGET_MOTE_L152RC) && !defined(TARGET_FF_ARDUINO) |
Wayne Roberts |
0:62e456e60083 | 161 | #define TARGET_FF_ARDUINO |
Wayne Roberts |
0:62e456e60083 | 162 | #endif |
Wayne Roberts |
0:62e456e60083 | 163 | |
Wayne Roberts |
0:62e456e60083 | 164 | #if defined(TARGET_FF_ARDUINO) |
Wayne Roberts |
0:62e456e60083 | 165 | DigitalIn d8(D8); |
Wayne Roberts |
0:62e456e60083 | 166 | DigitalOut extLed(D15); |
Wayne Roberts |
0:62e456e60083 | 167 | #endif /* TARGET_FF_ARDUINO */ |
Wayne Roberts |
0:62e456e60083 | 168 | |
Wayne Roberts |
0:62e456e60083 | 169 | #if defined(TARGET_FF_MORPHO) && !defined(TARGET_DISCO_L072CZ_LRWAN1) |
Wayne Roberts |
0:62e456e60083 | 170 | #define JUMPER_ENABLE |
Wayne Roberts |
0:62e456e60083 | 171 | #endif /* */ |
Wayne Roberts |
0:62e456e60083 | 172 | |
Wayne Roberts |
0:62e456e60083 | 173 | #ifdef JUMPER_ENABLE |
Wayne Roberts |
0:62e456e60083 | 174 | #define TX_INTERVAL_US 15000000 |
Wayne Roberts |
0:62e456e60083 | 175 | DigitalOut jumper_out(PC_10); |
Wayne Roberts |
0:62e456e60083 | 176 | InterruptIn jumper_in(PC_12); |
Wayne Roberts |
0:62e456e60083 | 177 | #endif /* JUMPER_ENABLE */ |
Wayne Roberts |
0:62e456e60083 | 178 | |
Wayne Roberts |
0:62e456e60083 | 179 | uint8_t c_ch; |
Wayne Roberts |
0:62e456e60083 | 180 | us_timestamp_t buttonStartAt; |
Wayne Roberts |
0:62e456e60083 | 181 | #ifdef TARGET_DISCO_L072CZ_LRWAN1 |
Wayne Roberts |
0:62e456e60083 | 182 | PwmOut pwm(PA_0); |
Wayne Roberts |
0:62e456e60083 | 183 | #elif defined(TARGET_FF_ARDUINO) |
Wayne Roberts |
0:62e456e60083 | 184 | PwmOut pwm(PB_11); |
Wayne Roberts |
0:62e456e60083 | 185 | #endif /* TARGET_DISCO_L072CZ_LRWAN1 */ |
Wayne Roberts |
0:62e456e60083 | 186 | volatile int cayenne_ack_ch; |
Wayne Roberts |
0:62e456e60083 | 187 | |
Wayne Roberts |
0:62e456e60083 | 188 | /*! |
Wayne Roberts |
0:62e456e60083 | 189 | * LoRaWAN compliance tests support data |
Wayne Roberts |
0:62e456e60083 | 190 | */ |
Wayne Roberts |
0:62e456e60083 | 191 | struct ComplianceTest_s |
Wayne Roberts |
0:62e456e60083 | 192 | { |
Wayne Roberts |
0:62e456e60083 | 193 | bool Running; |
Wayne Roberts |
0:62e456e60083 | 194 | uint8_t State; |
Wayne Roberts |
0:62e456e60083 | 195 | bool IsTxConfirmed; |
Wayne Roberts |
0:62e456e60083 | 196 | uint8_t AppPort; |
Wayne Roberts |
0:62e456e60083 | 197 | uint8_t AppDataSize; |
Wayne Roberts |
0:62e456e60083 | 198 | uint8_t *AppDataBuffer; |
Wayne Roberts |
0:62e456e60083 | 199 | uint16_t DownLinkCounter; |
Wayne Roberts |
0:62e456e60083 | 200 | bool LinkCheck; |
Wayne Roberts |
0:62e456e60083 | 201 | uint8_t DemodMargin; |
Wayne Roberts |
0:62e456e60083 | 202 | uint8_t NbGateways; |
Wayne Roberts |
0:62e456e60083 | 203 | }ComplianceTest; |
Wayne Roberts |
0:62e456e60083 | 204 | |
Wayne Roberts |
0:62e456e60083 | 205 | McpsIndication_t gmi; |
Wayne Roberts |
0:62e456e60083 | 206 | |
Wayne Roberts |
0:62e456e60083 | 207 | McpsConfirm_t gmc; |
Wayne Roberts |
0:62e456e60083 | 208 | |
Wayne Roberts |
0:62e456e60083 | 209 | #ifdef JUMPER_ENABLE |
Wayne Roberts |
0:62e456e60083 | 210 | void autoUplink() |
Wayne Roberts |
0:62e456e60083 | 211 | { |
Wayne Roberts |
0:62e456e60083 | 212 | if (jumper_in.read() == 1) { |
Wayne Roberts |
0:62e456e60083 | 213 | tx_timeout.attach_us(autoUplink, TX_INTERVAL_US); |
Wayne Roberts |
0:62e456e60083 | 214 | } |
Wayne Roberts |
0:62e456e60083 | 215 | |
Wayne Roberts |
0:62e456e60083 | 216 | c_ch = 0xff; |
Wayne Roberts |
0:62e456e60083 | 217 | DeviceState = DEVICE_STATE_SEND; |
Wayne Roberts |
0:62e456e60083 | 218 | } |
Wayne Roberts |
0:62e456e60083 | 219 | |
Wayne Roberts |
0:62e456e60083 | 220 | void jumper_callback() |
Wayne Roberts |
0:62e456e60083 | 221 | { |
Wayne Roberts |
0:62e456e60083 | 222 | tx_timeout.attach_us(autoUplink, TX_INTERVAL_US); |
Wayne Roberts |
0:62e456e60083 | 223 | } |
Wayne Roberts |
0:62e456e60083 | 224 | #endif /* JUMPER_ENABLE */ |
Wayne Roberts |
0:62e456e60083 | 225 | |
Wayne Roberts |
0:62e456e60083 | 226 | static void |
Wayne Roberts |
0:62e456e60083 | 227 | clearIndications() |
Wayne Roberts |
0:62e456e60083 | 228 | { |
Wayne Roberts |
0:62e456e60083 | 229 | vt.SetCursorPos(ROW_MCPS_CONF, 1); |
Wayne Roberts |
0:62e456e60083 | 230 | vt.printf("\e[K"); |
Wayne Roberts |
0:62e456e60083 | 231 | vt.SetCursorPos(ROW_MCPS_IND, 1); |
Wayne Roberts |
0:62e456e60083 | 232 | vt.printf("\e[K"); |
Wayne Roberts |
0:62e456e60083 | 233 | vt.SetCursorPos(ROW_MLME_IND, 1); |
Wayne Roberts |
0:62e456e60083 | 234 | vt.printf("\e[K"); |
Wayne Roberts |
0:62e456e60083 | 235 | vt.SetCursorPos(ROW_MLME_CONF, 1); |
Wayne Roberts |
0:62e456e60083 | 236 | vt.printf("\e[K"); |
Wayne Roberts |
0:62e456e60083 | 237 | |
Wayne Roberts |
0:62e456e60083 | 238 | vt.SetCursorPos(ROW_MIC+3, 1); |
Wayne Roberts |
0:62e456e60083 | 239 | vt.printf("\e[K"); |
Wayne Roberts |
0:62e456e60083 | 240 | } |
Wayne Roberts |
0:62e456e60083 | 241 | |
Wayne Roberts |
0:62e456e60083 | 242 | #define LPP_DIGITAL_INPUT 0 // 1 byte |
Wayne Roberts |
0:62e456e60083 | 243 | #define LPP_DIGITAL_OUTPUT 1 // 1 byte |
Wayne Roberts |
0:62e456e60083 | 244 | #define LPP_ANALOG_INPUT 2 // 2 bytes, 0.01 signed |
Wayne Roberts |
0:62e456e60083 | 245 | #define LPP_ANALOG_OUTPUT 3 // 2 bytes, 0.01 signed |
Wayne Roberts |
0:62e456e60083 | 246 | #define LPP_LUMINOSITY 101 // 2 bytes, 1 lux unsigned |
Wayne Roberts |
0:62e456e60083 | 247 | #define LPP_PRESENCE 102 // 1 byte, 1 |
Wayne Roberts |
0:62e456e60083 | 248 | #define LPP_TEMPERATURE 103 // 2 bytes, 0.1°C signed |
Wayne Roberts |
0:62e456e60083 | 249 | #define LPP_RELATIVE_HUMIDITY 104 // 1 byte, 0.5% unsigned |
Wayne Roberts |
0:62e456e60083 | 250 | #define LPP_ACCELEROMETER 113 // 2 bytes per axis, 0.001G |
Wayne Roberts |
0:62e456e60083 | 251 | #define LPP_BAROMETRIC_PRESSURE 115 // 2 bytes 0.1 hPa Unsigned |
Wayne Roberts |
0:62e456e60083 | 252 | #define LPP_GYROMETER 134 // 2 bytes per axis, 0.01 °/s |
Wayne Roberts |
0:62e456e60083 | 253 | #define LPP_GPS 136 // 3 byte lon/lat 0.0001 °, 3 bytes alt 0.01m |
Wayne Roberts |
0:62e456e60083 | 254 | |
Wayne Roberts |
0:62e456e60083 | 255 | |
Wayne Roberts |
0:62e456e60083 | 256 | // Data ID + Data Type + Data Size |
Wayne Roberts |
0:62e456e60083 | 257 | #define LPP_DIGITAL_INPUT_SIZE 3 |
Wayne Roberts |
0:62e456e60083 | 258 | #define LPP_DIGITAL_OUTPUT_SIZE 3 |
Wayne Roberts |
0:62e456e60083 | 259 | #define LPP_ANALOG_INPUT_SIZE 4 |
Wayne Roberts |
0:62e456e60083 | 260 | #define LPP_ANALOG_OUTPUT_SIZE 4 |
Wayne Roberts |
0:62e456e60083 | 261 | #define LPP_LUMINOSITY_SIZE 4 |
Wayne Roberts |
0:62e456e60083 | 262 | #define LPP_PRESENCE_SIZE 3 |
Wayne Roberts |
0:62e456e60083 | 263 | #define LPP_TEMPERATURE_SIZE 4 |
Wayne Roberts |
0:62e456e60083 | 264 | #define LPP_RELATIVE_HUMIDITY_SIZE 3 |
Wayne Roberts |
0:62e456e60083 | 265 | #define LPP_ACCELEROMETER_SIZE 8 |
Wayne Roberts |
0:62e456e60083 | 266 | #define LPP_BAROMETRIC_PRESSURE_SIZE 4 |
Wayne Roberts |
0:62e456e60083 | 267 | #define LPP_GYROMETER_SIZE 8 |
Wayne Roberts |
0:62e456e60083 | 268 | #define LPP_GPS_SIZE 11 |
Wayne Roberts |
0:62e456e60083 | 269 | |
Wayne Roberts |
0:62e456e60083 | 270 | #define CAYENNE_CH_DOUT 2 |
Wayne Roberts |
0:62e456e60083 | 271 | #define CAYENNE_CH_AOUT 3 |
Wayne Roberts |
0:62e456e60083 | 272 | #define CAYENNE_CH_TEMP 0 |
Wayne Roberts |
0:62e456e60083 | 273 | #define CAYENNE_CH_POT 1 |
Wayne Roberts |
0:62e456e60083 | 274 | |
Wayne Roberts |
0:62e456e60083 | 275 | AnalogIn a1(A1); |
Wayne Roberts |
0:62e456e60083 | 276 | AnalogIn a3(A3); |
Wayne Roberts |
0:62e456e60083 | 277 | |
Wayne Roberts |
0:62e456e60083 | 278 | const unsigned R0 = 100000; |
Wayne Roberts |
0:62e456e60083 | 279 | const unsigned B = 4275; |
Wayne Roberts |
0:62e456e60083 | 280 | |
Wayne Roberts |
0:62e456e60083 | 281 | /*! |
Wayne Roberts |
0:62e456e60083 | 282 | * \brief Prepares the payload of the frame |
Wayne Roberts |
0:62e456e60083 | 283 | */ |
Wayne Roberts |
0:62e456e60083 | 284 | static void PrepareTxFrame( uint8_t port ) |
Wayne Roberts |
0:62e456e60083 | 285 | { |
Wayne Roberts |
0:62e456e60083 | 286 | uint16_t u16, rot; |
Wayne Roberts |
0:62e456e60083 | 287 | float t, f, R; |
Wayne Roberts |
0:62e456e60083 | 288 | |
Wayne Roberts |
0:62e456e60083 | 289 | if (c_ch != 0xff) { |
Wayne Roberts |
0:62e456e60083 | 290 | gAppDataSize = 0; |
Wayne Roberts |
0:62e456e60083 | 291 | AppData[gAppDataSize++] = c_ch; |
Wayne Roberts |
0:62e456e60083 | 292 | switch (c_ch) { |
Wayne Roberts |
0:62e456e60083 | 293 | case CAYENNE_CH_TEMP: |
Wayne Roberts |
0:62e456e60083 | 294 | AppData[gAppDataSize++] = LPP_TEMPERATURE; |
Wayne Roberts |
0:62e456e60083 | 295 | u16 = a3.read_u16() >> 4; |
Wayne Roberts |
0:62e456e60083 | 296 | R = 4096.0 / u16 - 1.0; |
Wayne Roberts |
0:62e456e60083 | 297 | R = R0 * R; |
Wayne Roberts |
0:62e456e60083 | 298 | t = 1.0/(log(R/R0)/B+1/298.15)-273.15; |
Wayne Roberts |
0:62e456e60083 | 299 | u16 = t * 10; // 0.1C per bit |
Wayne Roberts |
0:62e456e60083 | 300 | AppData[gAppDataSize++] = u16 >> 8; |
Wayne Roberts |
0:62e456e60083 | 301 | AppData[gAppDataSize++] = u16; |
Wayne Roberts |
0:62e456e60083 | 302 | break; |
Wayne Roberts |
0:62e456e60083 | 303 | case CAYENNE_CH_POT: |
Wayne Roberts |
0:62e456e60083 | 304 | AppData[gAppDataSize++] = LPP_ANALOG_INPUT; |
Wayne Roberts |
0:62e456e60083 | 305 | u16 = a1.read_u16(); // pot (rotary angle) |
Wayne Roberts |
0:62e456e60083 | 306 | f = u16 / 198.6; // scale 65535/3.3 to 0.01v per bit |
Wayne Roberts |
0:62e456e60083 | 307 | rot = (uint16_t) f; |
Wayne Roberts |
0:62e456e60083 | 308 | AppData[gAppDataSize++] = rot >> 8; |
Wayne Roberts |
0:62e456e60083 | 309 | AppData[gAppDataSize++] = rot; |
Wayne Roberts |
0:62e456e60083 | 310 | break; |
Wayne Roberts |
0:62e456e60083 | 311 | case CAYENNE_CH_DOUT: |
Wayne Roberts |
0:62e456e60083 | 312 | AppData[gAppDataSize++] = LPP_DIGITAL_OUTPUT; |
Wayne Roberts |
0:62e456e60083 | 313 | AppData[gAppDataSize++] = extLed.read(); |
Wayne Roberts |
0:62e456e60083 | 314 | break; |
Wayne Roberts |
0:62e456e60083 | 315 | case CAYENNE_CH_AOUT: |
Wayne Roberts |
0:62e456e60083 | 316 | AppData[gAppDataSize++] = LPP_ANALOG_OUTPUT; |
Wayne Roberts |
0:62e456e60083 | 317 | u16 = pwm.read() * 100; |
Wayne Roberts |
0:62e456e60083 | 318 | AppData[gAppDataSize++] = u16 >> 8; |
Wayne Roberts |
0:62e456e60083 | 319 | AppData[gAppDataSize++] = u16; |
Wayne Roberts |
0:62e456e60083 | 320 | break; |
Wayne Roberts |
0:62e456e60083 | 321 | } |
Wayne Roberts |
0:62e456e60083 | 322 | return; |
Wayne Roberts |
0:62e456e60083 | 323 | } else if (cayenne_ack_ch != -1) { |
Wayne Roberts |
0:62e456e60083 | 324 | switch (cayenne_ack_ch) { |
Wayne Roberts |
0:62e456e60083 | 325 | case CAYENNE_CH_DOUT: |
Wayne Roberts |
0:62e456e60083 | 326 | AppData[gAppDataSize++] = LPP_DIGITAL_OUTPUT; |
Wayne Roberts |
0:62e456e60083 | 327 | AppData[gAppDataSize++] = extLed.read(); |
Wayne Roberts |
0:62e456e60083 | 328 | break; |
Wayne Roberts |
0:62e456e60083 | 329 | case CAYENNE_CH_AOUT: |
Wayne Roberts |
0:62e456e60083 | 330 | AppData[gAppDataSize++] = LPP_ANALOG_OUTPUT; |
Wayne Roberts |
0:62e456e60083 | 331 | u16 = pwm.read() * 100; |
Wayne Roberts |
0:62e456e60083 | 332 | AppData[gAppDataSize++] = u16 >> 8; |
Wayne Roberts |
0:62e456e60083 | 333 | AppData[gAppDataSize++] = u16; |
Wayne Roberts |
0:62e456e60083 | 334 | break; |
Wayne Roberts |
0:62e456e60083 | 335 | } |
Wayne Roberts |
0:62e456e60083 | 336 | cayenne_ack_ch = -1; |
Wayne Roberts |
0:62e456e60083 | 337 | } |
Wayne Roberts |
0:62e456e60083 | 338 | |
Wayne Roberts |
0:62e456e60083 | 339 | while (d8.read() == 1) { |
Wayne Roberts |
0:62e456e60083 | 340 | us_timestamp_t duration = LoRaMacReadTimer() - buttonStartAt; |
Wayne Roberts |
0:62e456e60083 | 341 | if (duration > 1000000) { |
Wayne Roberts |
0:62e456e60083 | 342 | gAppDataSize = 0; |
Wayne Roberts |
0:62e456e60083 | 343 | AppData[gAppDataSize++] = CAYENNE_CH_DOUT; |
Wayne Roberts |
0:62e456e60083 | 344 | AppData[gAppDataSize++] = LPP_DIGITAL_OUTPUT; |
Wayne Roberts |
0:62e456e60083 | 345 | AppData[gAppDataSize++] = extLed.read(); |
Wayne Roberts |
0:62e456e60083 | 346 | return; |
Wayne Roberts |
0:62e456e60083 | 347 | } |
Wayne Roberts |
0:62e456e60083 | 348 | } |
Wayne Roberts |
0:62e456e60083 | 349 | |
Wayne Roberts |
0:62e456e60083 | 350 | switch( port ) { |
Wayne Roberts |
0:62e456e60083 | 351 | case LORAWAN_APP_PORT: |
Wayne Roberts |
0:62e456e60083 | 352 | gAppDataSize = 0; |
Wayne Roberts |
0:62e456e60083 | 353 | AppData[gAppDataSize++] = CAYENNE_CH_TEMP; |
Wayne Roberts |
0:62e456e60083 | 354 | AppData[gAppDataSize++] = LPP_TEMPERATURE; |
Wayne Roberts |
0:62e456e60083 | 355 | u16 = a3.read_u16() >> 4; |
Wayne Roberts |
0:62e456e60083 | 356 | R = 4096.0 / u16 - 1.0; |
Wayne Roberts |
0:62e456e60083 | 357 | R = R0 * R; |
Wayne Roberts |
0:62e456e60083 | 358 | t = 1.0/(log(R/R0)/B+1/298.15)-273.15; |
Wayne Roberts |
0:62e456e60083 | 359 | u16 = t * 10; // 0.1C per bit |
Wayne Roberts |
0:62e456e60083 | 360 | AppData[gAppDataSize++] = u16 >> 8; |
Wayne Roberts |
0:62e456e60083 | 361 | AppData[gAppDataSize++] = u16; |
Wayne Roberts |
0:62e456e60083 | 362 | AppData[gAppDataSize++] = CAYENNE_CH_POT; |
Wayne Roberts |
0:62e456e60083 | 363 | AppData[gAppDataSize++] = LPP_ANALOG_INPUT; |
Wayne Roberts |
0:62e456e60083 | 364 | u16 = a1.read_u16(); // pot (rotary angle) |
Wayne Roberts |
0:62e456e60083 | 365 | f = u16 / 198.6; // scale 65535/3.3 to 0.01v per bit |
Wayne Roberts |
0:62e456e60083 | 366 | rot = (uint16_t) f; |
Wayne Roberts |
0:62e456e60083 | 367 | AppData[gAppDataSize++] = rot >> 8; |
Wayne Roberts |
0:62e456e60083 | 368 | AppData[gAppDataSize++] = rot; |
Wayne Roberts |
0:62e456e60083 | 369 | |
Wayne Roberts |
0:62e456e60083 | 370 | AppData[gAppDataSize++] = CAYENNE_CH_DOUT; |
Wayne Roberts |
0:62e456e60083 | 371 | AppData[gAppDataSize++] = LPP_DIGITAL_OUTPUT; |
Wayne Roberts |
0:62e456e60083 | 372 | AppData[gAppDataSize++] = extLed.read(); |
Wayne Roberts |
0:62e456e60083 | 373 | break; |
Wayne Roberts |
0:62e456e60083 | 374 | case 224: |
Wayne Roberts |
0:62e456e60083 | 375 | if( ComplianceTest.LinkCheck == true ) { |
Wayne Roberts |
0:62e456e60083 | 376 | ComplianceTest.LinkCheck = false; |
Wayne Roberts |
0:62e456e60083 | 377 | gAppDataSize = 3; |
Wayne Roberts |
0:62e456e60083 | 378 | AppData[0] = 5; |
Wayne Roberts |
0:62e456e60083 | 379 | AppData[1] = ComplianceTest.DemodMargin; |
Wayne Roberts |
0:62e456e60083 | 380 | AppData[2] = ComplianceTest.NbGateways; |
Wayne Roberts |
0:62e456e60083 | 381 | ComplianceTest.State = 1; |
Wayne Roberts |
0:62e456e60083 | 382 | } else { |
Wayne Roberts |
0:62e456e60083 | 383 | switch( ComplianceTest.State ) { |
Wayne Roberts |
0:62e456e60083 | 384 | case 4: |
Wayne Roberts |
0:62e456e60083 | 385 | ComplianceTest.State = 1; |
Wayne Roberts |
0:62e456e60083 | 386 | break; |
Wayne Roberts |
0:62e456e60083 | 387 | case 1: |
Wayne Roberts |
0:62e456e60083 | 388 | gAppDataSize = 2; |
Wayne Roberts |
0:62e456e60083 | 389 | AppData[0] = ComplianceTest.DownLinkCounter >> 8; |
Wayne Roberts |
0:62e456e60083 | 390 | AppData[1] = ComplianceTest.DownLinkCounter; |
Wayne Roberts |
0:62e456e60083 | 391 | break; |
Wayne Roberts |
0:62e456e60083 | 392 | } |
Wayne Roberts |
0:62e456e60083 | 393 | } |
Wayne Roberts |
0:62e456e60083 | 394 | break; |
Wayne Roberts |
0:62e456e60083 | 395 | default: |
Wayne Roberts |
0:62e456e60083 | 396 | break; |
Wayne Roberts |
0:62e456e60083 | 397 | } |
Wayne Roberts |
0:62e456e60083 | 398 | } |
Wayne Roberts |
0:62e456e60083 | 399 | |
Wayne Roberts |
0:62e456e60083 | 400 | /*! |
Wayne Roberts |
0:62e456e60083 | 401 | * \brief Prepares the payload of the frame |
Wayne Roberts |
0:62e456e60083 | 402 | * |
Wayne Roberts |
0:62e456e60083 | 403 | * \retval [0: frame could be send, 1: error] |
Wayne Roberts |
0:62e456e60083 | 404 | */ |
Wayne Roberts |
0:62e456e60083 | 405 | static LoRaMacStatus_t SendFrame(bool IsTxConfirmed, uint8_t AppDataSize) |
Wayne Roberts |
0:62e456e60083 | 406 | { |
Wayne Roberts |
0:62e456e60083 | 407 | LoRaMacStatus_t status; |
Wayne Roberts |
0:62e456e60083 | 408 | char str[64]; |
Wayne Roberts |
0:62e456e60083 | 409 | McpsReq_t mcpsReq; |
Wayne Roberts |
0:62e456e60083 | 410 | LoRaMacTxInfo_t txInfo; |
Wayne Roberts |
0:62e456e60083 | 411 | |
Wayne Roberts |
0:62e456e60083 | 412 | if( LoRaMacQueryTxPossible( AppDataSize, &txInfo ) != LORAMAC_STATUS_OK ) |
Wayne Roberts |
0:62e456e60083 | 413 | { |
Wayne Roberts |
0:62e456e60083 | 414 | // Send empty frame in order to flush MAC commands |
Wayne Roberts |
0:62e456e60083 | 415 | mcpsReq.Type = MCPS_UNCONFIRMED; |
Wayne Roberts |
0:62e456e60083 | 416 | mcpsReq.Req.fBuffer = NULL; |
Wayne Roberts |
0:62e456e60083 | 417 | mcpsReq.Req.fBufferSize = 0; |
Wayne Roberts |
0:62e456e60083 | 418 | mcpsReq.Req.Datarate = LORAWAN_DEFAULT_DATARATE; |
Wayne Roberts |
0:62e456e60083 | 419 | } |
Wayne Roberts |
0:62e456e60083 | 420 | else |
Wayne Roberts |
0:62e456e60083 | 421 | { |
Wayne Roberts |
0:62e456e60083 | 422 | SerialDisplayUpdateFrameType(IsTxConfirmed); |
Wayne Roberts |
0:62e456e60083 | 423 | if( IsTxConfirmed == false ) |
Wayne Roberts |
0:62e456e60083 | 424 | { |
Wayne Roberts |
0:62e456e60083 | 425 | mcpsReq.Type = MCPS_UNCONFIRMED; |
Wayne Roberts |
0:62e456e60083 | 426 | mcpsReq.Req.fPort = AppPort; |
Wayne Roberts |
0:62e456e60083 | 427 | mcpsReq.Req.fBuffer = AppData; |
Wayne Roberts |
0:62e456e60083 | 428 | mcpsReq.Req.fBufferSize = AppDataSize; |
Wayne Roberts |
0:62e456e60083 | 429 | mcpsReq.Req.Datarate = LORAWAN_DEFAULT_DATARATE; |
Wayne Roberts |
0:62e456e60083 | 430 | } |
Wayne Roberts |
0:62e456e60083 | 431 | else |
Wayne Roberts |
0:62e456e60083 | 432 | { |
Wayne Roberts |
0:62e456e60083 | 433 | mcpsReq.Type = MCPS_CONFIRMED; |
Wayne Roberts |
0:62e456e60083 | 434 | mcpsReq.Req.fPort = AppPort; |
Wayne Roberts |
0:62e456e60083 | 435 | mcpsReq.Req.fBuffer = AppData; |
Wayne Roberts |
0:62e456e60083 | 436 | mcpsReq.Req.fBufferSize = AppDataSize; |
Wayne Roberts |
0:62e456e60083 | 437 | mcpsReq.Req.Datarate = LORAWAN_DEFAULT_DATARATE; |
Wayne Roberts |
0:62e456e60083 | 438 | } |
Wayne Roberts |
0:62e456e60083 | 439 | } |
Wayne Roberts |
0:62e456e60083 | 440 | |
Wayne Roberts |
0:62e456e60083 | 441 | clearIndications(); |
Wayne Roberts |
0:62e456e60083 | 442 | status = LoRaMacMcpsRequest( &mcpsReq ); |
Wayne Roberts |
0:62e456e60083 | 443 | if (status == LORAMAC_STATUS_OK) { |
Wayne Roberts |
0:62e456e60083 | 444 | SerialDisplayUplink(mcpsReq.Req.fPort, AppData, mcpsReq.Req.fBufferSize); |
Wayne Roberts |
0:62e456e60083 | 445 | vt.SetCursorPos( ROW_END, 1 ); |
Wayne Roberts |
0:62e456e60083 | 446 | vt.printf("sendFrame() OK %u\e[K", AppDataSize); |
Wayne Roberts |
0:62e456e60083 | 447 | } else { |
Wayne Roberts |
0:62e456e60083 | 448 | LoRaMacStatus_to_string(status, str); |
Wayne Roberts |
0:62e456e60083 | 449 | vt.SetCursorPos( ROW_END, 1 ); |
Wayne Roberts |
0:62e456e60083 | 450 | vt.printf("sendFrame() %s rx%d\e[K", str, LoRaMacGetRxSlot()); |
Wayne Roberts |
0:62e456e60083 | 451 | } |
Wayne Roberts |
0:62e456e60083 | 452 | |
Wayne Roberts |
0:62e456e60083 | 453 | return status; |
Wayne Roberts |
0:62e456e60083 | 454 | } // ..SendFrame() |
Wayne Roberts |
0:62e456e60083 | 455 | |
Wayne Roberts |
0:62e456e60083 | 456 | /*! |
Wayne Roberts |
0:62e456e60083 | 457 | * \brief MCPS-Confirm event function |
Wayne Roberts |
0:62e456e60083 | 458 | * |
Wayne Roberts |
0:62e456e60083 | 459 | * \param [IN] mcpsConfirm - Pointer to the confirm structure, |
Wayne Roberts |
0:62e456e60083 | 460 | * containing confirm attributes. |
Wayne Roberts |
0:62e456e60083 | 461 | */ |
Wayne Roberts |
0:62e456e60083 | 462 | static void McpsConfirm( const McpsConfirm_t *mcpsConfirm ) |
Wayne Roberts |
0:62e456e60083 | 463 | { |
Wayne Roberts |
0:62e456e60083 | 464 | char str[64]; |
Wayne Roberts |
0:62e456e60083 | 465 | |
Wayne Roberts |
0:62e456e60083 | 466 | vt.SetCursorPos( ROW_MCPS_CONF, 1); |
Wayne Roberts |
0:62e456e60083 | 467 | vt.printf("McpsConfirm up:%uhz ", mcpsConfirm->UpLinkFreqHz); |
Wayne Roberts |
0:62e456e60083 | 468 | LoRaMacEventInfoStatus_to_string(mcpsConfirm->Status, str); |
Wayne Roberts |
0:62e456e60083 | 469 | if (mcpsConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK) |
Wayne Roberts |
0:62e456e60083 | 470 | vt.printf("%s \e[K", str); |
Wayne Roberts |
0:62e456e60083 | 471 | else |
Wayne Roberts |
0:62e456e60083 | 472 | vt.printf("\e[31m%s\e[0m \e[K", str); |
Wayne Roberts |
0:62e456e60083 | 473 | |
Wayne Roberts |
0:62e456e60083 | 474 | if (mcpsConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK) |
Wayne Roberts |
0:62e456e60083 | 475 | { |
Wayne Roberts |
0:62e456e60083 | 476 | switch( mcpsConfirm->McpsRequest ) |
Wayne Roberts |
0:62e456e60083 | 477 | { |
Wayne Roberts |
0:62e456e60083 | 478 | case MCPS_UNCONFIRMED: |
Wayne Roberts |
0:62e456e60083 | 479 | { |
Wayne Roberts |
0:62e456e60083 | 480 | // Check Datarate |
Wayne Roberts |
0:62e456e60083 | 481 | // Check TxPower |
Wayne Roberts |
0:62e456e60083 | 482 | break; |
Wayne Roberts |
0:62e456e60083 | 483 | } |
Wayne Roberts |
0:62e456e60083 | 484 | case MCPS_CONFIRMED: |
Wayne Roberts |
0:62e456e60083 | 485 | { |
Wayne Roberts |
0:62e456e60083 | 486 | // Check Datarate |
Wayne Roberts |
0:62e456e60083 | 487 | // Check TxPower |
Wayne Roberts |
0:62e456e60083 | 488 | // Check AckReceived |
Wayne Roberts |
0:62e456e60083 | 489 | // Check NbTrials |
Wayne Roberts |
0:62e456e60083 | 490 | //LoRaMacUplinkStatus.Acked = mcpsConfirm->AckReceived; |
Wayne Roberts |
0:62e456e60083 | 491 | break; |
Wayne Roberts |
0:62e456e60083 | 492 | } |
Wayne Roberts |
0:62e456e60083 | 493 | case MCPS_PROPRIETARY: |
Wayne Roberts |
0:62e456e60083 | 494 | { |
Wayne Roberts |
0:62e456e60083 | 495 | break; |
Wayne Roberts |
0:62e456e60083 | 496 | } |
Wayne Roberts |
0:62e456e60083 | 497 | default: |
Wayne Roberts |
0:62e456e60083 | 498 | break; |
Wayne Roberts |
0:62e456e60083 | 499 | } |
Wayne Roberts |
0:62e456e60083 | 500 | } else { |
Wayne Roberts |
0:62e456e60083 | 501 | /* fail */ |
Wayne Roberts |
0:62e456e60083 | 502 | } |
Wayne Roberts |
0:62e456e60083 | 503 | memcpy(&gmc, mcpsConfirm, sizeof(McpsConfirm_t)); |
Wayne Roberts |
0:62e456e60083 | 504 | flags.gmc = true; |
Wayne Roberts |
0:62e456e60083 | 505 | |
Wayne Roberts |
0:62e456e60083 | 506 | DeviceState = DEVICE_STATE_TRIGGER; |
Wayne Roberts |
0:62e456e60083 | 507 | } // ..McpsConfirm() |
Wayne Roberts |
0:62e456e60083 | 508 | |
Wayne Roberts |
0:62e456e60083 | 509 | |
Wayne Roberts |
0:62e456e60083 | 510 | /*! |
Wayne Roberts |
0:62e456e60083 | 511 | * \brief MCPS-Indication event function |
Wayne Roberts |
0:62e456e60083 | 512 | * |
Wayne Roberts |
0:62e456e60083 | 513 | * \param [IN] mcpsIndication - Pointer to the indication structure, |
Wayne Roberts |
0:62e456e60083 | 514 | * containing indication attributes. |
Wayne Roberts |
0:62e456e60083 | 515 | */ |
Wayne Roberts |
0:62e456e60083 | 516 | static void McpsIndication( const McpsIndication_t *mcpsIndication ) |
Wayne Roberts |
0:62e456e60083 | 517 | { |
Wayne Roberts |
0:62e456e60083 | 518 | char str[64]; |
Wayne Roberts |
0:62e456e60083 | 519 | |
Wayne Roberts |
0:62e456e60083 | 520 | memcpy(&gmi, mcpsIndication, sizeof(McpsIndication_t)); |
Wayne Roberts |
0:62e456e60083 | 521 | flags.gmi = true; |
Wayne Roberts |
0:62e456e60083 | 522 | |
Wayne Roberts |
0:62e456e60083 | 523 | vt.SetCursorPos(ROW_MCPS_CONF, 1); |
Wayne Roberts |
0:62e456e60083 | 524 | vt.printf("\e[K"); // clear stale mcpsconf if retrying |
Wayne Roberts |
0:62e456e60083 | 525 | |
Wayne Roberts |
0:62e456e60083 | 526 | vt.SetCursorPos( ROW_MCPS_IND, 0); |
Wayne Roberts |
2:da3c8d5b3f49 | 527 | vt.printf("McpsIndication rx%d ADR_ACK_CNT:%u ", mcpsIndication->RxSlot, mcpsIndication->ADR_ACK_CNT); |
Wayne Roberts |
0:62e456e60083 | 528 | if (mcpsIndication->Status != LORAMAC_EVENT_INFO_STATUS_OK) |
Wayne Roberts |
0:62e456e60083 | 529 | { |
Wayne Roberts |
0:62e456e60083 | 530 | LoRaMacEventInfoStatus_to_string(mcpsIndication->Status, str); |
Wayne Roberts |
2:da3c8d5b3f49 | 531 | vt.printf("\e[31m%s\e[0m\e[K", str); |
Wayne Roberts |
0:62e456e60083 | 532 | return; |
Wayne Roberts |
0:62e456e60083 | 533 | } |
Wayne Roberts |
2:da3c8d5b3f49 | 534 | vt.printf("OK\e[K"); |
Wayne Roberts |
0:62e456e60083 | 535 | |
Wayne Roberts |
0:62e456e60083 | 536 | switch( mcpsIndication->McpsIndication ) |
Wayne Roberts |
0:62e456e60083 | 537 | { |
Wayne Roberts |
0:62e456e60083 | 538 | case MCPS_UNCONFIRMED: |
Wayne Roberts |
0:62e456e60083 | 539 | { |
Wayne Roberts |
0:62e456e60083 | 540 | break; |
Wayne Roberts |
0:62e456e60083 | 541 | } |
Wayne Roberts |
0:62e456e60083 | 542 | case MCPS_CONFIRMED: |
Wayne Roberts |
0:62e456e60083 | 543 | { |
Wayne Roberts |
0:62e456e60083 | 544 | /* ack sent by mac layer */ |
Wayne Roberts |
0:62e456e60083 | 545 | break; |
Wayne Roberts |
0:62e456e60083 | 546 | } |
Wayne Roberts |
0:62e456e60083 | 547 | case MCPS_PROPRIETARY: |
Wayne Roberts |
0:62e456e60083 | 548 | { |
Wayne Roberts |
0:62e456e60083 | 549 | break; |
Wayne Roberts |
0:62e456e60083 | 550 | } |
Wayne Roberts |
0:62e456e60083 | 551 | case MCPS_MULTICAST: |
Wayne Roberts |
0:62e456e60083 | 552 | { |
Wayne Roberts |
0:62e456e60083 | 553 | break; |
Wayne Roberts |
0:62e456e60083 | 554 | } |
Wayne Roberts |
0:62e456e60083 | 555 | default: |
Wayne Roberts |
0:62e456e60083 | 556 | break; |
Wayne Roberts |
0:62e456e60083 | 557 | } |
Wayne Roberts |
0:62e456e60083 | 558 | |
Wayne Roberts |
0:62e456e60083 | 559 | // Check Multicast |
Wayne Roberts |
0:62e456e60083 | 560 | // Check Port |
Wayne Roberts |
0:62e456e60083 | 561 | // Check Datarate |
Wayne Roberts |
0:62e456e60083 | 562 | // Check FramePending |
Wayne Roberts |
0:62e456e60083 | 563 | // Check Buffer |
Wayne Roberts |
0:62e456e60083 | 564 | // Check BufferSize |
Wayne Roberts |
0:62e456e60083 | 565 | // Check Rssi |
Wayne Roberts |
0:62e456e60083 | 566 | // Check Snr |
Wayne Roberts |
0:62e456e60083 | 567 | // Check RxSlot |
Wayne Roberts |
0:62e456e60083 | 568 | |
Wayne Roberts |
0:62e456e60083 | 569 | |
Wayne Roberts |
0:62e456e60083 | 570 | if( ComplianceTest.Running == true ) |
Wayne Roberts |
0:62e456e60083 | 571 | { |
Wayne Roberts |
0:62e456e60083 | 572 | ComplianceTest.DownLinkCounter++; |
Wayne Roberts |
0:62e456e60083 | 573 | } |
Wayne Roberts |
0:62e456e60083 | 574 | |
Wayne Roberts |
0:62e456e60083 | 575 | if( mcpsIndication->RxData == true ) |
Wayne Roberts |
0:62e456e60083 | 576 | { |
Wayne Roberts |
0:62e456e60083 | 577 | unsigned n; |
Wayne Roberts |
0:62e456e60083 | 578 | for (n = 0; n < mcpsIndication->BufferSize; n += 4) { |
Wayne Roberts |
0:62e456e60083 | 579 | uint16_t val = mcpsIndication->Buffer[n+1] << 8; |
Wayne Roberts |
0:62e456e60083 | 580 | val += mcpsIndication->Buffer[n+2]; |
Wayne Roberts |
0:62e456e60083 | 581 | cayenne_ack_ch = mcpsIndication->Buffer[n]; |
Wayne Roberts |
0:62e456e60083 | 582 | switch (mcpsIndication->Buffer[n]) { |
Wayne Roberts |
0:62e456e60083 | 583 | case CAYENNE_CH_DOUT: |
Wayne Roberts |
0:62e456e60083 | 584 | extLed.write(val); |
Wayne Roberts |
0:62e456e60083 | 585 | break; |
Wayne Roberts |
0:62e456e60083 | 586 | case CAYENNE_CH_AOUT: |
Wayne Roberts |
0:62e456e60083 | 587 | pwm.write(val / 100.0); |
Wayne Roberts |
0:62e456e60083 | 588 | break; |
Wayne Roberts |
0:62e456e60083 | 589 | default: |
Wayne Roberts |
0:62e456e60083 | 590 | break; |
Wayne Roberts |
0:62e456e60083 | 591 | } |
Wayne Roberts |
0:62e456e60083 | 592 | } |
Wayne Roberts |
0:62e456e60083 | 593 | |
Wayne Roberts |
0:62e456e60083 | 594 | switch( mcpsIndication->Port ) |
Wayne Roberts |
0:62e456e60083 | 595 | { |
Wayne Roberts |
0:62e456e60083 | 596 | case 1: // The application LED can be controlled on port 1 or 2 |
Wayne Roberts |
0:62e456e60083 | 597 | case 2: |
Wayne Roberts |
0:62e456e60083 | 598 | break; |
Wayne Roberts |
0:62e456e60083 | 599 | case 224: |
Wayne Roberts |
0:62e456e60083 | 600 | if( ComplianceTest.Running == false ) |
Wayne Roberts |
0:62e456e60083 | 601 | { |
Wayne Roberts |
0:62e456e60083 | 602 | // Check compliance test enable command (i) |
Wayne Roberts |
0:62e456e60083 | 603 | if( ( mcpsIndication->BufferSize == 4 ) && |
Wayne Roberts |
0:62e456e60083 | 604 | ( mcpsIndication->Buffer[0] == 0x01 ) && |
Wayne Roberts |
0:62e456e60083 | 605 | ( mcpsIndication->Buffer[1] == 0x01 ) && |
Wayne Roberts |
0:62e456e60083 | 606 | ( mcpsIndication->Buffer[2] == 0x01 ) && |
Wayne Roberts |
0:62e456e60083 | 607 | ( mcpsIndication->Buffer[3] == 0x01 ) ) |
Wayne Roberts |
0:62e456e60083 | 608 | { |
Wayne Roberts |
0:62e456e60083 | 609 | gIsTxConfirmed = false; |
Wayne Roberts |
0:62e456e60083 | 610 | AppPort = 224; |
Wayne Roberts |
0:62e456e60083 | 611 | gAppDataSize = 2; |
Wayne Roberts |
0:62e456e60083 | 612 | ComplianceTest.DownLinkCounter = 0; |
Wayne Roberts |
0:62e456e60083 | 613 | ComplianceTest.LinkCheck = false; |
Wayne Roberts |
0:62e456e60083 | 614 | ComplianceTest.DemodMargin = 0; |
Wayne Roberts |
0:62e456e60083 | 615 | ComplianceTest.NbGateways = 0; |
Wayne Roberts |
0:62e456e60083 | 616 | ComplianceTest.Running = true; |
Wayne Roberts |
0:62e456e60083 | 617 | ComplianceTest.State = 1; |
Wayne Roberts |
0:62e456e60083 | 618 | |
Wayne Roberts |
0:62e456e60083 | 619 | MibRequestConfirm_t mibReq; |
Wayne Roberts |
0:62e456e60083 | 620 | mibReq.Type = MIB_ADR; |
Wayne Roberts |
0:62e456e60083 | 621 | mibReq.Param.AdrEnable = true; |
Wayne Roberts |
0:62e456e60083 | 622 | LoRaMacMibSetRequestConfirm( &mibReq ); |
Wayne Roberts |
0:62e456e60083 | 623 | |
Wayne Roberts |
0:62e456e60083 | 624 | #if defined( USE_BAND_868 ) |
Wayne Roberts |
0:62e456e60083 | 625 | DutyCycleOn = false; |
Wayne Roberts |
0:62e456e60083 | 626 | #endif |
Wayne Roberts |
0:62e456e60083 | 627 | } |
Wayne Roberts |
0:62e456e60083 | 628 | } |
Wayne Roberts |
0:62e456e60083 | 629 | else |
Wayne Roberts |
0:62e456e60083 | 630 | { |
Wayne Roberts |
0:62e456e60083 | 631 | ComplianceTest.State = mcpsIndication->Buffer[0]; |
Wayne Roberts |
0:62e456e60083 | 632 | switch( ComplianceTest.State ) |
Wayne Roberts |
0:62e456e60083 | 633 | { |
Wayne Roberts |
0:62e456e60083 | 634 | case 0: // Check compliance test disable command (ii) |
Wayne Roberts |
0:62e456e60083 | 635 | gIsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON; |
Wayne Roberts |
0:62e456e60083 | 636 | AppPort = LORAWAN_APP_PORT; |
Wayne Roberts |
0:62e456e60083 | 637 | gAppDataSize = LORAWAN_APP_DATA_SIZE; |
Wayne Roberts |
0:62e456e60083 | 638 | ComplianceTest.DownLinkCounter = 0; |
Wayne Roberts |
0:62e456e60083 | 639 | ComplianceTest.Running = false; |
Wayne Roberts |
0:62e456e60083 | 640 | |
Wayne Roberts |
0:62e456e60083 | 641 | MibRequestConfirm_t mibReq; |
Wayne Roberts |
0:62e456e60083 | 642 | mibReq.Type = MIB_ADR; |
Wayne Roberts |
0:62e456e60083 | 643 | mibReq.Param.AdrEnable = LORAWAN_ADR_ON; |
Wayne Roberts |
0:62e456e60083 | 644 | LoRaMacMibSetRequestConfirm( &mibReq ); |
Wayne Roberts |
0:62e456e60083 | 645 | #if defined( USE_BAND_868 ) |
Wayne Roberts |
0:62e456e60083 | 646 | DutyCycleOn = LORAWAN_DUTYCYCLE_ON; |
Wayne Roberts |
0:62e456e60083 | 647 | #endif |
Wayne Roberts |
0:62e456e60083 | 648 | break; |
Wayne Roberts |
0:62e456e60083 | 649 | case 1: // (iii, iv) |
Wayne Roberts |
0:62e456e60083 | 650 | gAppDataSize = 2; |
Wayne Roberts |
0:62e456e60083 | 651 | break; |
Wayne Roberts |
0:62e456e60083 | 652 | case 2: // Enable confirmed messages (v) |
Wayne Roberts |
0:62e456e60083 | 653 | gIsTxConfirmed = true; |
Wayne Roberts |
0:62e456e60083 | 654 | ComplianceTest.State = 1; |
Wayne Roberts |
0:62e456e60083 | 655 | break; |
Wayne Roberts |
0:62e456e60083 | 656 | case 3: // Disable confirmed messages (vi) |
Wayne Roberts |
0:62e456e60083 | 657 | gIsTxConfirmed = false; |
Wayne Roberts |
0:62e456e60083 | 658 | ComplianceTest.State = 1; |
Wayne Roberts |
0:62e456e60083 | 659 | break; |
Wayne Roberts |
0:62e456e60083 | 660 | case 4: // (vii) |
Wayne Roberts |
0:62e456e60083 | 661 | gAppDataSize = mcpsIndication->BufferSize; |
Wayne Roberts |
0:62e456e60083 | 662 | |
Wayne Roberts |
0:62e456e60083 | 663 | AppData[0] = 4; |
Wayne Roberts |
0:62e456e60083 | 664 | for( uint8_t i = 1; i < gAppDataSize; i++ ) |
Wayne Roberts |
0:62e456e60083 | 665 | { |
Wayne Roberts |
0:62e456e60083 | 666 | AppData[i] = mcpsIndication->Buffer[i] + 1; |
Wayne Roberts |
0:62e456e60083 | 667 | } |
Wayne Roberts |
0:62e456e60083 | 668 | break; |
Wayne Roberts |
0:62e456e60083 | 669 | case 5: // (viii) |
Wayne Roberts |
0:62e456e60083 | 670 | { |
Wayne Roberts |
0:62e456e60083 | 671 | MlmeReq_t mlmeReq; |
Wayne Roberts |
0:62e456e60083 | 672 | mlmeReq.Type = MLME_LINK_CHECK; |
Wayne Roberts |
0:62e456e60083 | 673 | LoRaMacMlmeRequest( &mlmeReq ); |
Wayne Roberts |
0:62e456e60083 | 674 | } |
Wayne Roberts |
0:62e456e60083 | 675 | break; |
Wayne Roberts |
0:62e456e60083 | 676 | case 6: // (ix) |
Wayne Roberts |
0:62e456e60083 | 677 | { |
Wayne Roberts |
0:62e456e60083 | 678 | #ifdef LORAWAN_JOIN_EUI |
Wayne Roberts |
0:62e456e60083 | 679 | MlmeReq_t mlmeReq = {}; |
Wayne Roberts |
0:62e456e60083 | 680 | |
Wayne Roberts |
0:62e456e60083 | 681 | // Disable TestMode and revert back to normal operation |
Wayne Roberts |
0:62e456e60083 | 682 | gIsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON; |
Wayne Roberts |
0:62e456e60083 | 683 | AppPort = LORAWAN_APP_PORT; |
Wayne Roberts |
0:62e456e60083 | 684 | gAppDataSize = LORAWAN_APP_DATA_SIZE; |
Wayne Roberts |
0:62e456e60083 | 685 | ComplianceTest.DownLinkCounter = 0; |
Wayne Roberts |
0:62e456e60083 | 686 | ComplianceTest.Running = false; |
Wayne Roberts |
0:62e456e60083 | 687 | |
Wayne Roberts |
0:62e456e60083 | 688 | MibRequestConfirm_t mibReq; |
Wayne Roberts |
0:62e456e60083 | 689 | mibReq.Type = MIB_ADR; |
Wayne Roberts |
0:62e456e60083 | 690 | mibReq.Param.AdrEnable = LORAWAN_ADR_ON; |
Wayne Roberts |
0:62e456e60083 | 691 | LoRaMacMibSetRequestConfirm( &mibReq ); |
Wayne Roberts |
0:62e456e60083 | 692 | #if defined( USE_BAND_868 ) |
Wayne Roberts |
0:62e456e60083 | 693 | DutyCycleOn = LORAWAN_DUTYCYCLE_ON; |
Wayne Roberts |
0:62e456e60083 | 694 | #endif |
Wayne Roberts |
0:62e456e60083 | 695 | |
Wayne Roberts |
0:62e456e60083 | 696 | mlmeReq.Type = MLME_JOIN; |
Wayne Roberts |
0:62e456e60083 | 697 | |
Wayne Roberts |
0:62e456e60083 | 698 | mlmeReq.Req.Join.DevEui = DevEui; |
Wayne Roberts |
0:62e456e60083 | 699 | mlmeReq.Req.Join.JoinEui = JoinEui; |
Wayne Roberts |
0:62e456e60083 | 700 | mlmeReq.Req.Join.NwkKey = NwkKey; |
Wayne Roberts |
0:62e456e60083 | 701 | #ifdef LORAWAN_ROOT_APPKEY |
Wayne Roberts |
0:62e456e60083 | 702 | mlmeReq.Req.Join.AppKey = AppKey; |
Wayne Roberts |
0:62e456e60083 | 703 | #endif /* LORAWAN_ROOT_APPKEY */ |
Wayne Roberts |
0:62e456e60083 | 704 | |
Wayne Roberts |
0:62e456e60083 | 705 | LoRaMacMlmeRequest( &mlmeReq ); |
Wayne Roberts |
0:62e456e60083 | 706 | #endif /* LORAWAN_JOIN_EUI */ |
Wayne Roberts |
0:62e456e60083 | 707 | DeviceState = DEVICE_STATE_SLEEP; |
Wayne Roberts |
0:62e456e60083 | 708 | } |
Wayne Roberts |
0:62e456e60083 | 709 | break; |
Wayne Roberts |
0:62e456e60083 | 710 | case 7: // Switch end device Class |
Wayne Roberts |
0:62e456e60083 | 711 | { |
Wayne Roberts |
0:62e456e60083 | 712 | MlmeReq_t mlmeReq; |
Wayne Roberts |
0:62e456e60083 | 713 | |
Wayne Roberts |
0:62e456e60083 | 714 | mlmeReq.Type = MLME_SWITCH_CLASS; |
Wayne Roberts |
0:62e456e60083 | 715 | |
Wayne Roberts |
0:62e456e60083 | 716 | // CLASS_A = 0, CLASS_B = 1, CLASS_C = 2 |
Wayne Roberts |
0:62e456e60083 | 717 | mlmeReq.Req.SwitchClass.Class = ( DeviceClass_t )mcpsIndication->Buffer[1]; |
Wayne Roberts |
0:62e456e60083 | 718 | |
Wayne Roberts |
0:62e456e60083 | 719 | LoRaMacMlmeRequest( &mlmeReq ); |
Wayne Roberts |
0:62e456e60083 | 720 | |
Wayne Roberts |
0:62e456e60083 | 721 | PrepareTxFrame( AppPort ); |
Wayne Roberts |
0:62e456e60083 | 722 | /*status =*/ SendFrame(gIsTxConfirmed, gAppDataSize); |
Wayne Roberts |
0:62e456e60083 | 723 | } |
Wayne Roberts |
0:62e456e60083 | 724 | break; |
Wayne Roberts |
0:62e456e60083 | 725 | case 8: // Send PingSlotInfoReq |
Wayne Roberts |
0:62e456e60083 | 726 | { |
Wayne Roberts |
0:62e456e60083 | 727 | MlmeReq_t mlmeReq; |
Wayne Roberts |
0:62e456e60083 | 728 | |
Wayne Roberts |
0:62e456e60083 | 729 | mlmeReq.Type = MLME_PING_SLOT_INFO; |
Wayne Roberts |
0:62e456e60083 | 730 | |
Wayne Roberts |
0:62e456e60083 | 731 | mlmeReq.Req.PingSlotInfo.Value = mcpsIndication->Buffer[1]; |
Wayne Roberts |
0:62e456e60083 | 732 | |
Wayne Roberts |
0:62e456e60083 | 733 | LoRaMacMlmeRequest( &mlmeReq ); |
Wayne Roberts |
0:62e456e60083 | 734 | PrepareTxFrame( AppPort ); |
Wayne Roberts |
0:62e456e60083 | 735 | /*status =*/ SendFrame(gIsTxConfirmed, gAppDataSize); |
Wayne Roberts |
0:62e456e60083 | 736 | } |
Wayne Roberts |
0:62e456e60083 | 737 | break; |
Wayne Roberts |
0:62e456e60083 | 738 | case 9: // Send BeaconTimingReq |
Wayne Roberts |
0:62e456e60083 | 739 | { |
Wayne Roberts |
0:62e456e60083 | 740 | MlmeReq_t mlmeReq; |
Wayne Roberts |
0:62e456e60083 | 741 | |
Wayne Roberts |
0:62e456e60083 | 742 | mlmeReq.Type = MLME_BEACON_TIMING; |
Wayne Roberts |
0:62e456e60083 | 743 | |
Wayne Roberts |
0:62e456e60083 | 744 | LoRaMacMlmeRequest( &mlmeReq ); |
Wayne Roberts |
0:62e456e60083 | 745 | PrepareTxFrame( AppPort ); |
Wayne Roberts |
0:62e456e60083 | 746 | /*status =*/ SendFrame(gIsTxConfirmed, gAppDataSize); |
Wayne Roberts |
0:62e456e60083 | 747 | } |
Wayne Roberts |
0:62e456e60083 | 748 | break; |
Wayne Roberts |
0:62e456e60083 | 749 | default: |
Wayne Roberts |
0:62e456e60083 | 750 | break; |
Wayne Roberts |
0:62e456e60083 | 751 | } |
Wayne Roberts |
0:62e456e60083 | 752 | } |
Wayne Roberts |
0:62e456e60083 | 753 | break; |
Wayne Roberts |
0:62e456e60083 | 754 | default: |
Wayne Roberts |
0:62e456e60083 | 755 | break; |
Wayne Roberts |
0:62e456e60083 | 756 | } |
Wayne Roberts |
0:62e456e60083 | 757 | |
Wayne Roberts |
0:62e456e60083 | 758 | } |
Wayne Roberts |
0:62e456e60083 | 759 | |
Wayne Roberts |
0:62e456e60083 | 760 | } // ..McpsIndication() |
Wayne Roberts |
0:62e456e60083 | 761 | |
Wayne Roberts |
0:62e456e60083 | 762 | #ifdef LORAWAN_JOIN_EUI |
Wayne Roberts |
0:62e456e60083 | 763 | void |
Wayne Roberts |
0:62e456e60083 | 764 | join(uint8_t tries) |
Wayne Roberts |
0:62e456e60083 | 765 | { |
Wayne Roberts |
0:62e456e60083 | 766 | char str[64]; |
Wayne Roberts |
0:62e456e60083 | 767 | LoRaMacStatus_t status; |
Wayne Roberts |
0:62e456e60083 | 768 | MlmeReq_t mlmeReq = { }; |
Wayne Roberts |
0:62e456e60083 | 769 | |
Wayne Roberts |
0:62e456e60083 | 770 | mlmeReq.Type = MLME_JOIN; |
Wayne Roberts |
0:62e456e60083 | 771 | |
Wayne Roberts |
0:62e456e60083 | 772 | clearIndications(); |
Wayne Roberts |
0:62e456e60083 | 773 | #ifdef LORAWAN_ROOT_APPKEY |
Wayne Roberts |
0:62e456e60083 | 774 | mlmeReq.Req.Join.AppKey = AppKey; |
Wayne Roberts |
0:62e456e60083 | 775 | #endif |
Wayne Roberts |
0:62e456e60083 | 776 | mlmeReq.Req.Join.DevEui = DevEui; |
Wayne Roberts |
0:62e456e60083 | 777 | mlmeReq.Req.Join.JoinEui = JoinEui; |
Wayne Roberts |
0:62e456e60083 | 778 | mlmeReq.Req.Join.NwkKey = NwkKey; |
Wayne Roberts |
0:62e456e60083 | 779 | mlmeReq.Req.Join.NbTrials = tries; |
Wayne Roberts |
0:62e456e60083 | 780 | status = LoRaMacMlmeRequest( &mlmeReq ); |
Wayne Roberts |
0:62e456e60083 | 781 | if (status != LORAMAC_STATUS_OK) { |
Wayne Roberts |
0:62e456e60083 | 782 | LoRaMacStatus_to_string(status, str); |
Wayne Roberts |
0:62e456e60083 | 783 | } else |
Wayne Roberts |
0:62e456e60083 | 784 | extLed = 1; |
Wayne Roberts |
0:62e456e60083 | 785 | } |
Wayne Roberts |
0:62e456e60083 | 786 | #endif /* LORAWAN_JOIN_EUI */ |
Wayne Roberts |
0:62e456e60083 | 787 | |
Wayne Roberts |
0:62e456e60083 | 788 | /*! |
Wayne Roberts |
0:62e456e60083 | 789 | * \brief MLME-Confirm event function |
Wayne Roberts |
0:62e456e60083 | 790 | * |
Wayne Roberts |
0:62e456e60083 | 791 | * \param [IN] mlmeConfirm - Pointer to the confirm structure, |
Wayne Roberts |
0:62e456e60083 | 792 | * containing confirm attributes. |
Wayne Roberts |
0:62e456e60083 | 793 | */ |
Wayne Roberts |
0:62e456e60083 | 794 | static void MlmeConfirm( const MlmeConfirm_t *mlmeConfirm ) |
Wayne Roberts |
0:62e456e60083 | 795 | { |
Wayne Roberts |
0:62e456e60083 | 796 | char str[64]; |
Wayne Roberts |
0:62e456e60083 | 797 | static uint8_t failCnt = 0; |
Wayne Roberts |
0:62e456e60083 | 798 | |
Wayne Roberts |
0:62e456e60083 | 799 | vt.SetCursorPos(ROW_MLME_CONF, 1); |
Wayne Roberts |
0:62e456e60083 | 800 | Mlme_to_string(mlmeConfirm->MlmeRequest, str); |
Wayne Roberts |
0:62e456e60083 | 801 | vt.printf("MlmeConfirm %s ", str); |
Wayne Roberts |
0:62e456e60083 | 802 | LoRaMacEventInfoStatus_to_string(mlmeConfirm->Status, str); |
Wayne Roberts |
0:62e456e60083 | 803 | if (mlmeConfirm->Status != LORAMAC_EVENT_INFO_STATUS_OK) |
Wayne Roberts |
0:62e456e60083 | 804 | vt.printf("\e[31m%s \e[0m \e[K", str); |
Wayne Roberts |
0:62e456e60083 | 805 | else |
Wayne Roberts |
0:62e456e60083 | 806 | vt.printf("%s \e[K", str); |
Wayne Roberts |
0:62e456e60083 | 807 | |
Wayne Roberts |
0:62e456e60083 | 808 | #if defined(LORAWAN_ROOT_APPKEY) && defined(LORAWAN_JOIN_EUI) |
Wayne Roberts |
0:62e456e60083 | 809 | /* 1v1 joinNonce is incrementing non-volatile value */ |
Wayne Roberts |
0:62e456e60083 | 810 | if (mlmeConfirm->MlmeRequest == MLME_JOIN) { |
Wayne Roberts |
0:62e456e60083 | 811 | vt.printf(" rxJoinNonce:%u vs %u", |
Wayne Roberts |
0:62e456e60083 | 812 | mlmeConfirm->fields.join.rxJoinNonce, |
Wayne Roberts |
0:62e456e60083 | 813 | mlmeConfirm->fields.join.myJoinNonce |
Wayne Roberts |
0:62e456e60083 | 814 | ); |
Wayne Roberts |
0:62e456e60083 | 815 | } |
Wayne Roberts |
0:62e456e60083 | 816 | #endif /* LORAWAN_ROOT_APPKEY */ |
Wayne Roberts |
0:62e456e60083 | 817 | |
Wayne Roberts |
0:62e456e60083 | 818 | if (mlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK) |
Wayne Roberts |
0:62e456e60083 | 819 | { |
Wayne Roberts |
0:62e456e60083 | 820 | failCnt = 0; |
Wayne Roberts |
0:62e456e60083 | 821 | switch (mlmeConfirm->MlmeRequest) |
Wayne Roberts |
0:62e456e60083 | 822 | { |
Wayne Roberts |
0:62e456e60083 | 823 | #ifdef LORAWAN_JOIN_EUI |
Wayne Roberts |
0:62e456e60083 | 824 | case MLME_JOIN: |
Wayne Roberts |
0:62e456e60083 | 825 | { |
Wayne Roberts |
0:62e456e60083 | 826 | // Status is OK, node has joined the network |
Wayne Roberts |
0:62e456e60083 | 827 | /* collect any mac cmds from server until expected channel mask */ |
Wayne Roberts |
0:62e456e60083 | 828 | extLed = 0; |
Wayne Roberts |
0:62e456e60083 | 829 | DeviceState = DEVICE_STATE_JOIN_OK; |
Wayne Roberts |
0:62e456e60083 | 830 | break; |
Wayne Roberts |
0:62e456e60083 | 831 | } |
Wayne Roberts |
0:62e456e60083 | 832 | #endif /* LORAWAN_JOIN_EUI*/ |
Wayne Roberts |
0:62e456e60083 | 833 | case MLME_LINK_CHECK: |
Wayne Roberts |
0:62e456e60083 | 834 | { |
Wayne Roberts |
0:62e456e60083 | 835 | // Check DemodMargin |
Wayne Roberts |
0:62e456e60083 | 836 | // Check NbGateways |
Wayne Roberts |
0:62e456e60083 | 837 | if( ComplianceTest.Running == true ) |
Wayne Roberts |
0:62e456e60083 | 838 | { |
Wayne Roberts |
0:62e456e60083 | 839 | ComplianceTest.LinkCheck = true; |
Wayne Roberts |
0:62e456e60083 | 840 | ComplianceTest.DemodMargin = mlmeConfirm->fields.link.DemodMargin; |
Wayne Roberts |
0:62e456e60083 | 841 | ComplianceTest.NbGateways = mlmeConfirm->fields.link.NbGateways; |
Wayne Roberts |
0:62e456e60083 | 842 | } |
Wayne Roberts |
0:62e456e60083 | 843 | break; |
Wayne Roberts |
0:62e456e60083 | 844 | } |
Wayne Roberts |
0:62e456e60083 | 845 | case MLME_TIME_REQ: |
Wayne Roberts |
0:62e456e60083 | 846 | break; |
Wayne Roberts |
0:62e456e60083 | 847 | default: |
Wayne Roberts |
0:62e456e60083 | 848 | /* TODO: handle unknown MLME request */ |
Wayne Roberts |
0:62e456e60083 | 849 | DeviceState = DEVICE_STATE_SLEEP; |
Wayne Roberts |
0:62e456e60083 | 850 | break; |
Wayne Roberts |
0:62e456e60083 | 851 | } |
Wayne Roberts |
0:62e456e60083 | 852 | } |
Wayne Roberts |
0:62e456e60083 | 853 | else // not ok... |
Wayne Roberts |
0:62e456e60083 | 854 | { |
Wayne Roberts |
0:62e456e60083 | 855 | failCnt++; |
Wayne Roberts |
0:62e456e60083 | 856 | |
Wayne Roberts |
0:62e456e60083 | 857 | #ifdef LORAWAN_JOIN_EUI |
Wayne Roberts |
0:62e456e60083 | 858 | if (failCnt > 5) { |
Wayne Roberts |
0:62e456e60083 | 859 | join(1); |
Wayne Roberts |
0:62e456e60083 | 860 | return; |
Wayne Roberts |
0:62e456e60083 | 861 | } |
Wayne Roberts |
0:62e456e60083 | 862 | #endif |
Wayne Roberts |
0:62e456e60083 | 863 | |
Wayne Roberts |
0:62e456e60083 | 864 | switch( mlmeConfirm->MlmeRequest ) |
Wayne Roberts |
0:62e456e60083 | 865 | { |
Wayne Roberts |
0:62e456e60083 | 866 | #ifdef LORAWAN_JOIN_EUI |
Wayne Roberts |
0:62e456e60083 | 867 | case MLME_JOIN: |
Wayne Roberts |
0:62e456e60083 | 868 | { |
Wayne Roberts |
0:62e456e60083 | 869 | // Join failed, restart join procedure |
Wayne Roberts |
0:62e456e60083 | 870 | break; |
Wayne Roberts |
0:62e456e60083 | 871 | } |
Wayne Roberts |
0:62e456e60083 | 872 | #endif /* LORAWAN_JOIN_EUI */ |
Wayne Roberts |
0:62e456e60083 | 873 | case MLME_LINK_CHECK: |
Wayne Roberts |
0:62e456e60083 | 874 | DeviceState = DEVICE_STATE_SLEEP; |
Wayne Roberts |
0:62e456e60083 | 875 | break; |
Wayne Roberts |
0:62e456e60083 | 876 | #ifdef LORAWAN_JOIN_EUI |
Wayne Roberts |
0:62e456e60083 | 877 | case MLME_REJOIN_0: |
Wayne Roberts |
0:62e456e60083 | 878 | break; |
Wayne Roberts |
0:62e456e60083 | 879 | case MLME_REJOIN_2: |
Wayne Roberts |
0:62e456e60083 | 880 | break; |
Wayne Roberts |
0:62e456e60083 | 881 | case MLME_TIME_REQ: |
Wayne Roberts |
0:62e456e60083 | 882 | break; |
Wayne Roberts |
0:62e456e60083 | 883 | #endif /* LORAWAN_JOIN_EUI */ |
Wayne Roberts |
0:62e456e60083 | 884 | default: |
Wayne Roberts |
0:62e456e60083 | 885 | DeviceState = DEVICE_STATE_SLEEP; |
Wayne Roberts |
0:62e456e60083 | 886 | break; |
Wayne Roberts |
0:62e456e60083 | 887 | } |
Wayne Roberts |
0:62e456e60083 | 888 | } |
Wayne Roberts |
0:62e456e60083 | 889 | } // ..MlmeConfirm |
Wayne Roberts |
0:62e456e60083 | 890 | |
Wayne Roberts |
0:62e456e60083 | 891 | static void MlmeIndication( const MlmeIndication_t *MlmeIndication ) |
Wayne Roberts |
0:62e456e60083 | 892 | { |
Wayne Roberts |
0:62e456e60083 | 893 | char str[48]; |
Wayne Roberts |
0:62e456e60083 | 894 | MibRequestConfirm_t mibReq; |
Wayne Roberts |
0:62e456e60083 | 895 | |
Wayne Roberts |
0:62e456e60083 | 896 | vt.SetCursorPos(ROW_MLME_IND, 1); |
dudmuck | 1:3c1d13a0489e | 897 | Mlme_to_string(MlmeIndication->MlmeIndication, str); |
dudmuck | 1:3c1d13a0489e | 898 | vt.printf("MlmeIndication %s %08x ", str, RCC->CSR); |
Wayne Roberts |
0:62e456e60083 | 899 | LoRaMacEventInfoStatus_to_string(MlmeIndication->Status, str); |
Wayne Roberts |
0:62e456e60083 | 900 | if (MlmeIndication->Status != LORAMAC_EVENT_INFO_STATUS_OK) |
Wayne Roberts |
0:62e456e60083 | 901 | vt.printf("\e[31m%s \e[0m \e[K", str); |
Wayne Roberts |
0:62e456e60083 | 902 | else |
Wayne Roberts |
0:62e456e60083 | 903 | vt.printf("%s \e[K", str); |
Wayne Roberts |
0:62e456e60083 | 904 | |
Wayne Roberts |
0:62e456e60083 | 905 | switch( MlmeIndication->MlmeIndication ) |
Wayne Roberts |
0:62e456e60083 | 906 | { |
Wayne Roberts |
0:62e456e60083 | 907 | case MLME_SWITCH_CLASS: |
Wayne Roberts |
0:62e456e60083 | 908 | { |
Wayne Roberts |
0:62e456e60083 | 909 | /* mac gave up on beacon */ |
Wayne Roberts |
0:62e456e60083 | 910 | mibReq.Type = MIB_DEVICE_CLASS; |
Wayne Roberts |
0:62e456e60083 | 911 | mibReq.Param.Class = CLASS_A; |
Wayne Roberts |
0:62e456e60083 | 912 | LoRaMacMibSetRequestConfirm( &mibReq ); |
Wayne Roberts |
0:62e456e60083 | 913 | |
Wayne Roberts |
0:62e456e60083 | 914 | // Switch to class A again |
Wayne Roberts |
0:62e456e60083 | 915 | DeviceState = DEVICE_STATE_SLEEP; // class-B manual switch |
Wayne Roberts |
0:62e456e60083 | 916 | break; |
Wayne Roberts |
0:62e456e60083 | 917 | } |
Wayne Roberts |
0:62e456e60083 | 918 | case MLME_BEACON: |
Wayne Roberts |
0:62e456e60083 | 919 | { |
Wayne Roberts |
0:62e456e60083 | 920 | LoRaMacEventInfoStatus_to_string(MlmeIndication->Status, str); |
Wayne Roberts |
0:62e456e60083 | 921 | break; |
Wayne Roberts |
0:62e456e60083 | 922 | |
Wayne Roberts |
0:62e456e60083 | 923 | } |
Wayne Roberts |
0:62e456e60083 | 924 | #ifdef LORAWAN_JOIN_EUI |
Wayne Roberts |
0:62e456e60083 | 925 | case MLME_JOIN: |
Wayne Roberts |
0:62e456e60083 | 926 | vt.printf("%uhz try%u", MlmeIndication->freqHz, MlmeIndication->JoinRequestTrials); |
Wayne Roberts |
0:62e456e60083 | 927 | break; |
Wayne Roberts |
0:62e456e60083 | 928 | #endif /* !LORAWAN_JOIN_EUI */ |
Wayne Roberts |
0:62e456e60083 | 929 | default: |
Wayne Roberts |
0:62e456e60083 | 930 | break; |
Wayne Roberts |
0:62e456e60083 | 931 | } |
Wayne Roberts |
0:62e456e60083 | 932 | |
Wayne Roberts |
0:62e456e60083 | 933 | } // ..MlmeIndication() |
Wayne Roberts |
0:62e456e60083 | 934 | |
Wayne Roberts |
0:62e456e60083 | 935 | uint8_t periodicity; |
Wayne Roberts |
0:62e456e60083 | 936 | |
Wayne Roberts |
0:62e456e60083 | 937 | void SerialDisplayRefresh( void ) |
Wayne Roberts |
0:62e456e60083 | 938 | { |
Wayne Roberts |
0:62e456e60083 | 939 | #ifdef LORAWAN_JOIN_EUI |
Wayne Roberts |
0:62e456e60083 | 940 | MibRequestConfirm_t mibReq; |
Wayne Roberts |
0:62e456e60083 | 941 | #endif |
Wayne Roberts |
0:62e456e60083 | 942 | |
Wayne Roberts |
0:62e456e60083 | 943 | SerialDisplayInit( ); |
Wayne Roberts |
0:62e456e60083 | 944 | #ifdef LORAWAN_JOIN_EUI |
Wayne Roberts |
0:62e456e60083 | 945 | SerialDisplayUpdateActivationMode(true); |
Wayne Roberts |
0:62e456e60083 | 946 | SerialDisplayUpdateEui( ROW_DEVEUI, DevEui); |
Wayne Roberts |
0:62e456e60083 | 947 | SerialDisplayUpdateEui( ROW_JOINEUI, JoinEui); |
Wayne Roberts |
0:62e456e60083 | 948 | SerialDisplayUpdateKey( ROW_NWKKEY, NwkKey); |
Wayne Roberts |
0:62e456e60083 | 949 | |
Wayne Roberts |
0:62e456e60083 | 950 | #ifdef LORAWAN_ROOT_APPKEY |
Wayne Roberts |
0:62e456e60083 | 951 | SerialDisplayUpdateKey(ROW_APPKEY, AppKey); |
Wayne Roberts |
0:62e456e60083 | 952 | #endif |
Wayne Roberts |
0:62e456e60083 | 953 | |
Wayne Roberts |
0:62e456e60083 | 954 | mibReq.Type = MIB_NETWORK_JOINED; |
Wayne Roberts |
0:62e456e60083 | 955 | LoRaMacMibGetRequestConfirm( &mibReq ); |
Wayne Roberts |
0:62e456e60083 | 956 | SerialDisplayUpdateNetworkIsJoined( mibReq.Param.IsNetworkJoined ); |
Wayne Roberts |
0:62e456e60083 | 957 | #else |
Wayne Roberts |
0:62e456e60083 | 958 | //SerialDisplayUpdateNwkId( LORAWAN_NETWORK_ID ); |
Wayne Roberts |
0:62e456e60083 | 959 | SerialDisplayUpdateDevAddr( DevAddr ); |
Wayne Roberts |
0:62e456e60083 | 960 | SerialDisplayUpdateKey( ROW_FNwkSIntKey, FNwkSIntKey); |
Wayne Roberts |
0:62e456e60083 | 961 | SerialDisplayUpdateKey( ROW_AppSKey, AppSKey ); |
Wayne Roberts |
0:62e456e60083 | 962 | #if defined(LORAWAN_SNwkSIntKey) && defined(LORAWAN_NwkSEncKey) |
Wayne Roberts |
0:62e456e60083 | 963 | SerialDisplayUpdateKey(ROW_NwkSEncKey, NwkSEncKey); |
Wayne Roberts |
0:62e456e60083 | 964 | SerialDisplayUpdateKey(ROW_SNwkSIntKey, SNwkSIntKey); |
Wayne Roberts |
0:62e456e60083 | 965 | #endif /* 1v1 ABP */ |
Wayne Roberts |
0:62e456e60083 | 966 | |
Wayne Roberts |
0:62e456e60083 | 967 | vt.SetCursorPos( ROW_END, 1 ); |
Wayne Roberts |
0:62e456e60083 | 968 | vt.printf("FCntUp:%08x", eeprom_read(EEPROM_FCNTUP)); |
Wayne Roberts |
0:62e456e60083 | 969 | vt.printf(" AFCntDown:%08x", get_fcntdwn(true)); |
Wayne Roberts |
0:62e456e60083 | 970 | vt.printf(" NFCntDown:%08x", get_fcntdwn(false)); |
Wayne Roberts |
0:62e456e60083 | 971 | #endif |
Wayne Roberts |
0:62e456e60083 | 972 | |
Wayne Roberts |
0:62e456e60083 | 973 | |
Wayne Roberts |
0:62e456e60083 | 974 | SerialDisplayUpdateAdr( LORAWAN_ADR_ON ); |
Wayne Roberts |
0:62e456e60083 | 975 | #if defined( USE_BAND_868 ) |
Wayne Roberts |
0:62e456e60083 | 976 | SerialDisplayUpdateDutyCycle( LORAWAN_DUTYCYCLE_ON ); |
Wayne Roberts |
0:62e456e60083 | 977 | #else |
Wayne Roberts |
0:62e456e60083 | 978 | SerialDisplayUpdateDutyCycle( false ); |
Wayne Roberts |
0:62e456e60083 | 979 | #endif |
Wayne Roberts |
0:62e456e60083 | 980 | SerialDisplayUpdatePublicNetwork( LORAWAN_PUBLIC_NETWORK ); |
Wayne Roberts |
0:62e456e60083 | 981 | |
Wayne Roberts |
0:62e456e60083 | 982 | //SerialDisplayUpdateLedState( 3, AppLedStateOn ); |
Wayne Roberts |
0:62e456e60083 | 983 | } |
Wayne Roberts |
0:62e456e60083 | 984 | |
Wayne Roberts |
0:62e456e60083 | 985 | void SerialRxProcess( void ) |
Wayne Roberts |
0:62e456e60083 | 986 | { |
Wayne Roberts |
0:62e456e60083 | 987 | LoRaMacStatus_t status; |
Wayne Roberts |
0:62e456e60083 | 988 | MlmeReq_t mlmeReq; |
Wayne Roberts |
0:62e456e60083 | 989 | #ifndef LORAWAN_JOIN_EUI |
Wayne Roberts |
0:62e456e60083 | 990 | static uint8_t icnt = 0; |
Wayne Roberts |
0:62e456e60083 | 991 | #endif |
Wayne Roberts |
0:62e456e60083 | 992 | |
Wayne Roberts |
0:62e456e60083 | 993 | if( SerialDisplayReadable( ) == true ) { |
Wayne Roberts |
0:62e456e60083 | 994 | char ch = SerialDisplayGetChar(); |
Wayne Roberts |
0:62e456e60083 | 995 | #ifndef LORAWAN_JOIN_EUI |
Wayne Roberts |
0:62e456e60083 | 996 | if (ch == 'I') { |
Wayne Roberts |
0:62e456e60083 | 997 | if (++icnt == 3) { |
Wayne Roberts |
0:62e456e60083 | 998 | vt.SetCursorPos( ROW_END, 1 ); |
Wayne Roberts |
0:62e456e60083 | 999 | vt.printf("reset-fcnts\e[K"); |
Wayne Roberts |
0:62e456e60083 | 1000 | eeprom_clear(EEPROM_AFCNTDWN); |
Wayne Roberts |
0:62e456e60083 | 1001 | eeprom_clear(EEPROM_NFCNTDWN); |
Wayne Roberts |
0:62e456e60083 | 1002 | eeprom_clear(EEPROM_FCNTUP); |
Wayne Roberts |
0:62e456e60083 | 1003 | } |
Wayne Roberts |
0:62e456e60083 | 1004 | } else |
Wayne Roberts |
0:62e456e60083 | 1005 | icnt = 0; |
Wayne Roberts |
0:62e456e60083 | 1006 | #endif /* !LORAWAN_JOIN_EUI */ |
Wayne Roberts |
0:62e456e60083 | 1007 | |
Wayne Roberts |
0:62e456e60083 | 1008 | if ( ch >= '0' && ch <= '9') { |
Wayne Roberts |
0:62e456e60083 | 1009 | c_ch = ch - '0'; |
Wayne Roberts |
0:62e456e60083 | 1010 | DeviceState = DEVICE_STATE_SEND; |
Wayne Roberts |
0:62e456e60083 | 1011 | return; |
Wayne Roberts |
0:62e456e60083 | 1012 | } |
Wayne Roberts |
0:62e456e60083 | 1013 | switch( ch ) { |
Wayne Roberts |
0:62e456e60083 | 1014 | case 'R': |
Wayne Roberts |
0:62e456e60083 | 1015 | case 'r': |
Wayne Roberts |
0:62e456e60083 | 1016 | // Refresh Serial screen |
Wayne Roberts |
0:62e456e60083 | 1017 | SerialDisplayRefresh( ); |
Wayne Roberts |
0:62e456e60083 | 1018 | break; |
Wayne Roberts |
0:62e456e60083 | 1019 | case 'L': |
Wayne Roberts |
0:62e456e60083 | 1020 | clearIndications(); |
Wayne Roberts |
0:62e456e60083 | 1021 | mlmeReq.Type = MLME_LINK_CHECK; |
Wayne Roberts |
0:62e456e60083 | 1022 | status = LoRaMacMlmeRequest( &mlmeReq ); |
Wayne Roberts |
0:62e456e60083 | 1023 | if (status == LORAMAC_STATUS_OK) |
Wayne Roberts |
0:62e456e60083 | 1024 | SendFrame(0, false); |
Wayne Roberts |
0:62e456e60083 | 1025 | break; |
Wayne Roberts |
0:62e456e60083 | 1026 | #ifdef LORAWAN_JOIN_EUI |
Wayne Roberts |
0:62e456e60083 | 1027 | case 'j': |
Wayne Roberts |
0:62e456e60083 | 1028 | DeviceState = DEVICE_STATE_JOIN; |
Wayne Roberts |
0:62e456e60083 | 1029 | break; |
Wayne Roberts |
0:62e456e60083 | 1030 | #endif |
Wayne Roberts |
0:62e456e60083 | 1031 | default: |
Wayne Roberts |
0:62e456e60083 | 1032 | break; |
Wayne Roberts |
0:62e456e60083 | 1033 | } |
Wayne Roberts |
0:62e456e60083 | 1034 | } |
Wayne Roberts |
0:62e456e60083 | 1035 | } |
Wayne Roberts |
0:62e456e60083 | 1036 | |
Wayne Roberts |
0:62e456e60083 | 1037 | |
Wayne Roberts |
0:62e456e60083 | 1038 | static const LoRaMacPrimitives_t LoRaMacPrimitives = { |
Wayne Roberts |
0:62e456e60083 | 1039 | McpsConfirm, |
Wayne Roberts |
0:62e456e60083 | 1040 | McpsIndication, |
Wayne Roberts |
0:62e456e60083 | 1041 | MlmeConfirm, |
Wayne Roberts |
0:62e456e60083 | 1042 | MlmeIndication |
Wayne Roberts |
0:62e456e60083 | 1043 | }; |
Wayne Roberts |
0:62e456e60083 | 1044 | |
Wayne Roberts |
0:62e456e60083 | 1045 | static const LoRaMacCallback_t LoRaMacCallbacks = { |
Wayne Roberts |
0:62e456e60083 | 1046 | BoardGetBatteryLevel, |
Wayne Roberts |
0:62e456e60083 | 1047 | NULL |
Wayne Roberts |
0:62e456e60083 | 1048 | }; |
Wayne Roberts |
0:62e456e60083 | 1049 | |
Wayne Roberts |
0:62e456e60083 | 1050 | /** |
Wayne Roberts |
0:62e456e60083 | 1051 | * Main application entry point. |
Wayne Roberts |
0:62e456e60083 | 1052 | */ |
Wayne Roberts |
0:62e456e60083 | 1053 | int main() |
Wayne Roberts |
0:62e456e60083 | 1054 | { |
Wayne Roberts |
2:da3c8d5b3f49 | 1055 | LoRaMacStatus_t status; |
Wayne Roberts |
0:62e456e60083 | 1056 | MibRequestConfirm_t mibReq; |
Wayne Roberts |
0:62e456e60083 | 1057 | |
Wayne Roberts |
0:62e456e60083 | 1058 | DeviceState = DEVICE_STATE_INIT; |
Wayne Roberts |
0:62e456e60083 | 1059 | |
Wayne Roberts |
0:62e456e60083 | 1060 | if (sleep_manager_can_deep_sleep()) |
Wayne Roberts |
0:62e456e60083 | 1061 | sleep_manager_lock_deep_sleep(); // prevent deep sleep |
Wayne Roberts |
0:62e456e60083 | 1062 | |
Wayne Roberts |
0:62e456e60083 | 1063 | #ifdef JUMPER_ENABLE |
Wayne Roberts |
0:62e456e60083 | 1064 | jumper_out = 1; |
Wayne Roberts |
0:62e456e60083 | 1065 | jumper_in.mode(PullDown); |
Wayne Roberts |
0:62e456e60083 | 1066 | jumper_in.rise(jumper_callback); |
Wayne Roberts |
0:62e456e60083 | 1067 | #endif /* JUMPER_ENABLE */ |
Wayne Roberts |
0:62e456e60083 | 1068 | |
Wayne Roberts |
0:62e456e60083 | 1069 | while( 1 ) |
Wayne Roberts |
0:62e456e60083 | 1070 | { |
Wayne Roberts |
0:62e456e60083 | 1071 | SerialRxProcess( ); |
Wayne Roberts |
0:62e456e60083 | 1072 | |
Wayne Roberts |
0:62e456e60083 | 1073 | if (flags.gmi) { |
Wayne Roberts |
0:62e456e60083 | 1074 | flags.gmi = false; |
Wayne Roberts |
0:62e456e60083 | 1075 | SerialDisplayMcpsIndication(&gmi); |
Wayne Roberts |
0:62e456e60083 | 1076 | } |
Wayne Roberts |
0:62e456e60083 | 1077 | |
Wayne Roberts |
0:62e456e60083 | 1078 | if (flags.gmc) { |
Wayne Roberts |
0:62e456e60083 | 1079 | flags.gmc = false; |
Wayne Roberts |
0:62e456e60083 | 1080 | SerialDisplayMcpsConfirm(&gmc); |
Wayne Roberts |
0:62e456e60083 | 1081 | } |
Wayne Roberts |
0:62e456e60083 | 1082 | |
Wayne Roberts |
0:62e456e60083 | 1083 | switch( DeviceState ) |
Wayne Roberts |
0:62e456e60083 | 1084 | { |
Wayne Roberts |
0:62e456e60083 | 1085 | case DEVICE_STATE_INIT: |
Wayne Roberts |
0:62e456e60083 | 1086 | { |
Wayne Roberts |
2:da3c8d5b3f49 | 1087 | status = LoRaMacInitialization( &LoRaMacPrimitives, &LoRaMacCallbacks ); |
dudmuck | 1:3c1d13a0489e | 1088 | if (LORAMAC_STATUS_OK != status) { |
dudmuck | 1:3c1d13a0489e | 1089 | char str[48]; |
dudmuck | 1:3c1d13a0489e | 1090 | LoRaMacStatus_to_string(status, str); |
dudmuck | 1:3c1d13a0489e | 1091 | vt.SetCursorPos(1, 1); |
dudmuck | 1:3c1d13a0489e | 1092 | vt.printf("MacInit: %s\e[K", str); |
dudmuck | 1:3c1d13a0489e | 1093 | for (;;) asm("nop"); |
dudmuck | 1:3c1d13a0489e | 1094 | } |
dudmuck | 1:3c1d13a0489e | 1095 | |
Wayne Roberts |
0:62e456e60083 | 1096 | pwm.period(1.0 / 60); |
Wayne Roberts |
0:62e456e60083 | 1097 | cayenne_ack_ch = -1; |
Wayne Roberts |
0:62e456e60083 | 1098 | c_ch = 0xff; |
dudmuck | 1:3c1d13a0489e | 1099 | d8.mode(PullDown); |
Wayne Roberts |
0:62e456e60083 | 1100 | |
Wayne Roberts |
0:62e456e60083 | 1101 | mibReq.Type = MIB_ADR; |
Wayne Roberts |
0:62e456e60083 | 1102 | mibReq.Param.AdrEnable = LORAWAN_ADR_ON; |
Wayne Roberts |
0:62e456e60083 | 1103 | LoRaMacMibSetRequestConfirm( &mibReq ); |
Wayne Roberts |
0:62e456e60083 | 1104 | |
Wayne Roberts |
0:62e456e60083 | 1105 | mibReq.Type = MIB_PUBLIC_NETWORK; |
Wayne Roberts |
0:62e456e60083 | 1106 | mibReq.Param.EnablePublicNetwork = LORAWAN_PUBLIC_NETWORK; |
Wayne Roberts |
0:62e456e60083 | 1107 | LoRaMacMibSetRequestConfirm( &mibReq ); |
Wayne Roberts |
0:62e456e60083 | 1108 | |
Wayne Roberts |
0:62e456e60083 | 1109 | #if defined( USE_BAND_868 ) |
Wayne Roberts |
0:62e456e60083 | 1110 | DutyCycleOn = LORAWAN_DUTYCYCLE_ON ; |
Wayne Roberts |
0:62e456e60083 | 1111 | #if( USE_SEMTECH_DEFAULT_CHANNEL_LINEUP == 1 ) |
Wayne Roberts |
0:62e456e60083 | 1112 | LoRaMacChannelAdd( 3, ( ChannelParams_t )LC4 ); |
Wayne Roberts |
0:62e456e60083 | 1113 | LoRaMacChannelAdd( 4, ( ChannelParams_t )LC5 ); |
Wayne Roberts |
0:62e456e60083 | 1114 | LoRaMacChannelAdd( 5, ( ChannelParams_t )LC6 ); |
Wayne Roberts |
0:62e456e60083 | 1115 | LoRaMacChannelAdd( 6, ( ChannelParams_t )LC7 ); |
Wayne Roberts |
0:62e456e60083 | 1116 | LoRaMacChannelAdd( 7, ( ChannelParams_t )LC8 ); |
Wayne Roberts |
0:62e456e60083 | 1117 | LoRaMacChannelAdd( 8, ( ChannelParams_t )LC9 ); |
Wayne Roberts |
0:62e456e60083 | 1118 | LoRaMacChannelAdd( 9, ( ChannelParams_t )LC10 ); |
Wayne Roberts |
0:62e456e60083 | 1119 | |
Wayne Roberts |
0:62e456e60083 | 1120 | mibReq.Type = MIB_RX2_CHANNEL; |
Wayne Roberts |
0:62e456e60083 | 1121 | mibReq.Param.Rx2Channel = ( Rx2ChannelParams_t ){ 869525000, DR_3 }; |
Wayne Roberts |
0:62e456e60083 | 1122 | LoRaMacMibSetRequestConfirm( &mibReq ); |
Wayne Roberts |
0:62e456e60083 | 1123 | #endif |
Wayne Roberts |
0:62e456e60083 | 1124 | |
Wayne Roberts |
0:62e456e60083 | 1125 | #endif |
Wayne Roberts |
0:62e456e60083 | 1126 | |
Wayne Roberts |
0:62e456e60083 | 1127 | SerialDisplayRefresh(); |
Wayne Roberts |
0:62e456e60083 | 1128 | #ifdef LORAWAN_JOIN_EUI |
Wayne Roberts |
0:62e456e60083 | 1129 | |
Wayne Roberts |
0:62e456e60083 | 1130 | #ifndef SENETCO /* for senet, use network provided DevEUI */ |
Wayne Roberts |
0:62e456e60083 | 1131 | // Initialize LoRaMac device unique ID |
Wayne Roberts |
0:62e456e60083 | 1132 | HardwareIDtoDevEUI(DevEui); |
Wayne Roberts |
0:62e456e60083 | 1133 | #ifdef LORAWAN_ROOT_APPKEY |
Wayne Roberts |
0:62e456e60083 | 1134 | // inverted DevEui provisioned as v1.1 on server (non-inv = lorawan1v0) |
Wayne Roberts |
0:62e456e60083 | 1135 | for (int i = 0; i < 8; i++) |
Wayne Roberts |
0:62e456e60083 | 1136 | DevEui[i] ^= 0xff; |
Wayne Roberts |
0:62e456e60083 | 1137 | #endif /* LORAWAN_ROOT_APPKEY */ |
Wayne Roberts |
0:62e456e60083 | 1138 | #endif /* !SENETCO */ |
Wayne Roberts |
0:62e456e60083 | 1139 | SerialDisplayUpdateEui( 5, DevEui ); |
Wayne Roberts |
0:62e456e60083 | 1140 | DeviceState = DEVICE_STATE_JOIN; |
Wayne Roberts |
0:62e456e60083 | 1141 | #else /* ABP... */ |
Wayne Roberts |
0:62e456e60083 | 1142 | |
Wayne Roberts |
0:62e456e60083 | 1143 | mibReq.Type = MIB_DEV_ADDR; |
Wayne Roberts |
0:62e456e60083 | 1144 | mibReq.Param.DevAddr = DevAddr; |
Wayne Roberts |
0:62e456e60083 | 1145 | LoRaMacMibSetRequestConfirm( &mibReq ); |
Wayne Roberts |
0:62e456e60083 | 1146 | SerialDisplayUpdateDevAddr(DevAddr); |
Wayne Roberts |
0:62e456e60083 | 1147 | |
Wayne Roberts |
0:62e456e60083 | 1148 | mibReq.Type = MIB_APP_SKEY; |
Wayne Roberts |
0:62e456e60083 | 1149 | mibReq.Param.key = AppSKey; |
Wayne Roberts |
0:62e456e60083 | 1150 | LoRaMacMibSetRequestConfirm( &mibReq ); |
Wayne Roberts |
0:62e456e60083 | 1151 | SerialDisplayUpdateKey(ROW_AppSKey, AppSKey); |
Wayne Roberts |
0:62e456e60083 | 1152 | |
Wayne Roberts |
0:62e456e60083 | 1153 | #if defined(LORAWAN_SNwkSIntKey) && defined(LORAWAN_NwkSEncKey) |
Wayne Roberts |
0:62e456e60083 | 1154 | /* lorawan 1v1 ABP */ |
Wayne Roberts |
0:62e456e60083 | 1155 | mibReq.Type = MIB_NwkSEncKey; |
Wayne Roberts |
2:da3c8d5b3f49 | 1156 | mibReq.Param.key = NwkSEncKey; |
Wayne Roberts |
0:62e456e60083 | 1157 | LoRaMacMibSetRequestConfirm( &mibReq ); |
Wayne Roberts |
0:62e456e60083 | 1158 | SerialDisplayUpdateKey(ROW_NwkSEncKey, NwkSEncKey); |
Wayne Roberts |
0:62e456e60083 | 1159 | |
Wayne Roberts |
0:62e456e60083 | 1160 | mibReq.Type = MIB_SNwkSIntKey; |
Wayne Roberts |
2:da3c8d5b3f49 | 1161 | mibReq.Param.key = SNwkSIntKey; |
Wayne Roberts |
0:62e456e60083 | 1162 | LoRaMacMibSetRequestConfirm( &mibReq ); |
Wayne Roberts |
0:62e456e60083 | 1163 | SerialDisplayUpdateKey(ROW_SNwkSIntKey, SNwkSIntKey); |
Wayne Roberts |
0:62e456e60083 | 1164 | |
Wayne Roberts |
0:62e456e60083 | 1165 | mibReq.Type = MIB_FNwkSIntKey; |
Wayne Roberts |
0:62e456e60083 | 1166 | mibReq.Param.key = FNwkSIntKey; |
Wayne Roberts |
0:62e456e60083 | 1167 | LoRaMacMibSetRequestConfirm( &mibReq ); |
Wayne Roberts |
0:62e456e60083 | 1168 | SerialDisplayUpdateKey(ROW_FNwkSIntKey, mibReq.Param.key); |
Wayne Roberts |
0:62e456e60083 | 1169 | #else |
Wayne Roberts |
0:62e456e60083 | 1170 | /* lorawan 1v0 ABP */ |
Wayne Roberts |
0:62e456e60083 | 1171 | mibReq.Type = MIB_NwkSKey; |
Wayne Roberts |
0:62e456e60083 | 1172 | mibReq.Param.key = FNwkSIntKey; |
Wayne Roberts |
0:62e456e60083 | 1173 | LoRaMacMibSetRequestConfirm( &mibReq ); |
Wayne Roberts |
0:62e456e60083 | 1174 | SerialDisplayUpdateKey(ROW_FNwkSIntKey, mibReq.Param.key); |
Wayne Roberts |
0:62e456e60083 | 1175 | #endif |
Wayne Roberts |
0:62e456e60083 | 1176 | |
Wayne Roberts |
0:62e456e60083 | 1177 | DeviceState = DEVICE_STATE_TRIGGER; |
Wayne Roberts |
0:62e456e60083 | 1178 | #endif /* !LORAWAN_JOIN_EUI */ |
Wayne Roberts |
0:62e456e60083 | 1179 | break; |
Wayne Roberts |
0:62e456e60083 | 1180 | } |
Wayne Roberts |
0:62e456e60083 | 1181 | #ifdef LORAWAN_JOIN_EUI |
Wayne Roberts |
0:62e456e60083 | 1182 | case DEVICE_STATE_JOIN: |
Wayne Roberts |
0:62e456e60083 | 1183 | { |
Wayne Roberts |
0:62e456e60083 | 1184 | join(8); |
Wayne Roberts |
0:62e456e60083 | 1185 | DeviceState = DEVICE_STATE_SLEEP; |
Wayne Roberts |
0:62e456e60083 | 1186 | break; |
Wayne Roberts |
0:62e456e60083 | 1187 | } |
Wayne Roberts |
0:62e456e60083 | 1188 | case DEVICE_STATE_JOIN_OK: |
Wayne Roberts |
0:62e456e60083 | 1189 | MibRequestConfirm_t mibReq; |
Wayne Roberts |
0:62e456e60083 | 1190 | mibReq.Type = MIB_NETWORK_JOINED; |
Wayne Roberts |
0:62e456e60083 | 1191 | LoRaMacMibGetRequestConfirm( &mibReq ); |
Wayne Roberts |
0:62e456e60083 | 1192 | SerialDisplayUpdateNetworkIsJoined( mibReq.Param.IsNetworkJoined ); |
Wayne Roberts |
0:62e456e60083 | 1193 | mibReq.Type = MIB_DEV_ADDR; |
Wayne Roberts |
0:62e456e60083 | 1194 | LoRaMacMibGetRequestConfirm( &mibReq ); |
Wayne Roberts |
0:62e456e60083 | 1195 | SerialDisplayUpdateDevAddr(mibReq.Param.DevAddr); |
Wayne Roberts |
0:62e456e60083 | 1196 | mibReq.Type = MIB_FNwkSIntKey; |
Wayne Roberts |
0:62e456e60083 | 1197 | LoRaMacMibGetRequestConfirm( &mibReq ); |
Wayne Roberts |
0:62e456e60083 | 1198 | SerialDisplayUpdateKey( ROW_FNwkSIntKey, mibReq.Param.key ); |
Wayne Roberts |
0:62e456e60083 | 1199 | mibReq.Type = MIB_APP_SKEY; |
Wayne Roberts |
0:62e456e60083 | 1200 | LoRaMacMibGetRequestConfirm( &mibReq ); |
Wayne Roberts |
0:62e456e60083 | 1201 | SerialDisplayUpdateKey( ROW_AppSKey, mibReq.Param.key ); |
Wayne Roberts |
0:62e456e60083 | 1202 | |
Wayne Roberts |
0:62e456e60083 | 1203 | #ifdef LORAWAN_ROOT_APPKEY |
Wayne Roberts |
0:62e456e60083 | 1204 | mibReq.Type = MIB_SNwkSIntKey; |
Wayne Roberts |
0:62e456e60083 | 1205 | LoRaMacMibGetRequestConfirm( &mibReq ); |
Wayne Roberts |
0:62e456e60083 | 1206 | SerialDisplayUpdateKey(ROW_SNwkSIntKey, mibReq.Param.key); |
Wayne Roberts |
0:62e456e60083 | 1207 | mibReq.Type = MIB_NwkSEncKey; |
Wayne Roberts |
0:62e456e60083 | 1208 | LoRaMacMibGetRequestConfirm( &mibReq ); |
Wayne Roberts |
0:62e456e60083 | 1209 | SerialDisplayUpdateKey(ROW_NwkSEncKey, mibReq.Param.key); |
Wayne Roberts |
0:62e456e60083 | 1210 | #endif /* LORAWAN_ROOT_APPKEY */ |
Wayne Roberts |
0:62e456e60083 | 1211 | DeviceState = DEVICE_STATE_TRIGGER; |
Wayne Roberts |
0:62e456e60083 | 1212 | break; |
Wayne Roberts |
0:62e456e60083 | 1213 | #endif /* LORAWAN_JOIN_EUI */ |
Wayne Roberts |
0:62e456e60083 | 1214 | case DEVICE_STATE_SEND: |
Wayne Roberts |
0:62e456e60083 | 1215 | SerialDisplayUpdateUplinkAcked( false ); |
Wayne Roberts |
0:62e456e60083 | 1216 | SerialDisplayUpdateDonwlinkRxData( false ); |
Wayne Roberts |
0:62e456e60083 | 1217 | PrepareTxFrame( AppPort ); |
Wayne Roberts |
2:da3c8d5b3f49 | 1218 | status = SendFrame(gIsTxConfirmed, gAppDataSize); |
Wayne Roberts |
2:da3c8d5b3f49 | 1219 | if (status == LORAMAC_STATUS_OK) { |
Wayne Roberts |
2:da3c8d5b3f49 | 1220 | /* McpsConfirm or McpsIndication callback will continue */ |
Wayne Roberts |
2:da3c8d5b3f49 | 1221 | DeviceState = DEVICE_STATE_SLEEP; |
Wayne Roberts |
2:da3c8d5b3f49 | 1222 | } else |
Wayne Roberts |
2:da3c8d5b3f49 | 1223 | DeviceState = DEVICE_STATE_TRIGGER; |
Wayne Roberts |
0:62e456e60083 | 1224 | break; |
Wayne Roberts |
0:62e456e60083 | 1225 | case DEVICE_STATE_SLEEP: |
Wayne Roberts |
0:62e456e60083 | 1226 | { |
Wayne Roberts |
0:62e456e60083 | 1227 | // Wake up through events |
Wayne Roberts |
0:62e456e60083 | 1228 | sleep_manager_sleep_auto(); |
Wayne Roberts |
0:62e456e60083 | 1229 | break; |
Wayne Roberts |
0:62e456e60083 | 1230 | } |
Wayne Roberts |
0:62e456e60083 | 1231 | case DEVICE_STATE_TRIGGER: |
Wayne Roberts |
0:62e456e60083 | 1232 | sleep_manager_sleep_auto(); |
Wayne Roberts |
0:62e456e60083 | 1233 | if (d8.read() == 1) { |
Wayne Roberts |
0:62e456e60083 | 1234 | c_ch = 0xff; |
Wayne Roberts |
0:62e456e60083 | 1235 | DeviceState = DEVICE_STATE_SEND; |
Wayne Roberts |
0:62e456e60083 | 1236 | buttonStartAt = LoRaMacReadTimer(); |
Wayne Roberts |
0:62e456e60083 | 1237 | } |
Wayne Roberts |
0:62e456e60083 | 1238 | break; |
Wayne Roberts |
0:62e456e60083 | 1239 | default: |
Wayne Roberts |
0:62e456e60083 | 1240 | DeviceState = DEVICE_STATE_INIT; |
Wayne Roberts |
0:62e456e60083 | 1241 | break; |
Wayne Roberts |
0:62e456e60083 | 1242 | } // ..switch( DeviceState ) |
Wayne Roberts |
0:62e456e60083 | 1243 | |
Wayne Roberts |
0:62e456e60083 | 1244 | } // ..while( 1 ) |
Wayne Roberts |
0:62e456e60083 | 1245 | } |
Wayne Roberts |
0:62e456e60083 | 1246 | #endif /* ENABLE_VT100 */ |