Lora4MakerFaire
Dependencies: HC_SR04_Ultrasonic_Library X_NUCLEO_IKS01A2 mbed LoRaWAN-lib SX1272Lib
Fork of LoRaWANdemo72 by
app/main.cpp@8:346c55cb6033, 2017-10-03 (annotated)
- Committer:
- salvatoregulfo
- Date:
- Tue Oct 03 13:11:33 2017 +0000
- Revision:
- 8:346c55cb6033
- Parent:
- 7:5077515c163b
- Child:
- 9:85c85c65299a
LoRaWan
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mluis | 0:45496a70a8a5 | 1 | /* |
mluis | 0:45496a70a8a5 | 2 | / _____) _ | | |
mluis | 0:45496a70a8a5 | 3 | ( (____ _____ ____ _| |_ _____ ____| |__ |
mluis | 0:45496a70a8a5 | 4 | \____ \| ___ | (_ _) ___ |/ ___) _ \ |
mluis | 0:45496a70a8a5 | 5 | _____) ) ____| | | || |_| ____( (___| | | | |
mluis | 0:45496a70a8a5 | 6 | (______/|_____)_|_|_| \__)_____)\____)_| |_| |
mluis | 0:45496a70a8a5 | 7 | (C)2015 Semtech |
mluis | 0:45496a70a8a5 | 8 | |
mluis | 0:45496a70a8a5 | 9 | Description: LoRaMac classA device implementation |
mluis | 0:45496a70a8a5 | 10 | |
mluis | 0:45496a70a8a5 | 11 | License: Revised BSD License, see LICENSE.TXT file include in the project |
mluis | 0:45496a70a8a5 | 12 | |
mluis | 0:45496a70a8a5 | 13 | Maintainer: Miguel Luis and Gregory Cristian |
mluis | 0:45496a70a8a5 | 14 | */ |
mluis | 0:45496a70a8a5 | 15 | #include "mbed.h" |
mluis | 0:45496a70a8a5 | 16 | #include "board.h" |
mluis | 0:45496a70a8a5 | 17 | #include "radio.h" |
mluis | 0:45496a70a8a5 | 18 | |
mluis | 0:45496a70a8a5 | 19 | #include "LoRaMac.h" |
mluis | 7:5077515c163b | 20 | #include "Commissioning.h" |
mluis | 0:45496a70a8a5 | 21 | #include "SerialDisplay.h" |
salvatoregulfo | 8:346c55cb6033 | 22 | #include "XNucleoIKS01A2.h" |
salvatoregulfo | 8:346c55cb6033 | 23 | #include "string.h" |
salvatoregulfo | 8:346c55cb6033 | 24 | |
salvatoregulfo | 8:346c55cb6033 | 25 | /* Instantiate the expansion board */ |
salvatoregulfo | 8:346c55cb6033 | 26 | static XNucleoIKS01A2 *mems_expansion_board = XNucleoIKS01A2::instance(D14, D15, D4, D5); |
salvatoregulfo | 8:346c55cb6033 | 27 | |
salvatoregulfo | 8:346c55cb6033 | 28 | /* Retrieve the composing elements of the expansion board */ |
salvatoregulfo | 8:346c55cb6033 | 29 | static HTS221Sensor *hum_temp = mems_expansion_board->ht_sensor; |
salvatoregulfo | 8:346c55cb6033 | 30 | //static LSM303AGRAccSensor *accelerometer = mems_expansion_board->accelerometer; |
salvatoregulfo | 8:346c55cb6033 | 31 | //static LSM6DSLSensor *acc_gyro = mems_expansion_board->acc_gyro; |
mluis | 0:45496a70a8a5 | 32 | |
mluis | 0:45496a70a8a5 | 33 | /*! |
mluis | 7:5077515c163b | 34 | * Defines the application data transmission duty cycle. 5s, value in [ms]. |
mluis | 0:45496a70a8a5 | 35 | */ |
mluis | 7:5077515c163b | 36 | #define APP_TX_DUTYCYCLE 5000 |
mluis | 0:45496a70a8a5 | 37 | |
mluis | 0:45496a70a8a5 | 38 | /*! |
mluis | 0:45496a70a8a5 | 39 | * Defines a random delay for application data transmission duty cycle. 1s, |
mluis | 7:5077515c163b | 40 | * value in [ms]. |
mluis | 0:45496a70a8a5 | 41 | */ |
mluis | 7:5077515c163b | 42 | #define APP_TX_DUTYCYCLE_RND 1000 |
mluis | 0:45496a70a8a5 | 43 | |
mluis | 0:45496a70a8a5 | 44 | /*! |
mluis | 3:3152aa75c58d | 45 | * Default datarate |
mluis | 0:45496a70a8a5 | 46 | */ |
salvatoregulfo | 8:346c55cb6033 | 47 | #define LORAWAN_DEFAULT_DATARATE DR_0 //SF 12 |
mluis | 0:45496a70a8a5 | 48 | |
mluis | 0:45496a70a8a5 | 49 | /*! |
mluis | 0:45496a70a8a5 | 50 | * LoRaWAN confirmed messages |
mluis | 0:45496a70a8a5 | 51 | */ |
mluis | 0:45496a70a8a5 | 52 | #define LORAWAN_CONFIRMED_MSG_ON true |
mluis | 0:45496a70a8a5 | 53 | |
mluis | 0:45496a70a8a5 | 54 | /*! |
mluis | 0:45496a70a8a5 | 55 | * LoRaWAN Adaptive Data Rate |
mluis | 0:45496a70a8a5 | 56 | * |
mluis | 0:45496a70a8a5 | 57 | * \remark Please note that when ADR is enabled the end-device should be static |
mluis | 0:45496a70a8a5 | 58 | */ |
mluis | 0:45496a70a8a5 | 59 | #define LORAWAN_ADR_ON 1 |
mluis | 0:45496a70a8a5 | 60 | |
mluis | 0:45496a70a8a5 | 61 | #if defined( USE_BAND_868 ) |
mluis | 0:45496a70a8a5 | 62 | |
mluis | 0:45496a70a8a5 | 63 | #include "LoRaMacTest.h" |
mluis | 0:45496a70a8a5 | 64 | |
mluis | 0:45496a70a8a5 | 65 | /*! |
mluis | 0:45496a70a8a5 | 66 | * LoRaWAN ETSI duty cycle control enable/disable |
mluis | 0:45496a70a8a5 | 67 | * |
mluis | 0:45496a70a8a5 | 68 | * \remark Please note that ETSI mandates duty cycled transmissions. Use only for test purposes |
mluis | 0:45496a70a8a5 | 69 | */ |
salvatoregulfo | 8:346c55cb6033 | 70 | #define LORAWAN_DUTYCYCLE_ON true |
mluis | 0:45496a70a8a5 | 71 | |
mluis | 3:3152aa75c58d | 72 | #define USE_SEMTECH_DEFAULT_CHANNEL_LINEUP 1 |
mluis | 3:3152aa75c58d | 73 | |
mluis | 7:5077515c163b | 74 | #if( USE_SEMTECH_DEFAULT_CHANNEL_LINEUP == 1 ) |
mluis | 3:3152aa75c58d | 75 | |
mluis | 3:3152aa75c58d | 76 | #define LC4 { 867100000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } |
mluis | 3:3152aa75c58d | 77 | #define LC5 { 867300000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } |
mluis | 3:3152aa75c58d | 78 | #define LC6 { 867500000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } |
mluis | 3:3152aa75c58d | 79 | #define LC7 { 867700000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } |
mluis | 3:3152aa75c58d | 80 | #define LC8 { 867900000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } |
mluis | 3:3152aa75c58d | 81 | #define LC9 { 868800000, { ( ( DR_7 << 4 ) | DR_7 ) }, 2 } |
mluis | 3:3152aa75c58d | 82 | #define LC10 { 868300000, { ( ( DR_6 << 4 ) | DR_6 ) }, 1 } |
mluis | 3:3152aa75c58d | 83 | |
mluis | 3:3152aa75c58d | 84 | #endif |
mluis | 3:3152aa75c58d | 85 | |
mluis | 0:45496a70a8a5 | 86 | #endif |
mluis | 0:45496a70a8a5 | 87 | |
mluis | 0:45496a70a8a5 | 88 | /*! |
mluis | 0:45496a70a8a5 | 89 | * LoRaWAN application port |
mluis | 0:45496a70a8a5 | 90 | */ |
salvatoregulfo | 8:346c55cb6033 | 91 | #define LORAWAN_APP_PORT 13 |
mluis | 0:45496a70a8a5 | 92 | |
mluis | 0:45496a70a8a5 | 93 | /*! |
mluis | 0:45496a70a8a5 | 94 | * User application data buffer size |
mluis | 0:45496a70a8a5 | 95 | */ |
mluis | 0:45496a70a8a5 | 96 | #if ( LORAWAN_CONFIRMED_MSG_ON == 1 ) |
mluis | 0:45496a70a8a5 | 97 | #define LORAWAN_APP_DATA_SIZE 6 |
mluis | 0:45496a70a8a5 | 98 | |
mluis | 0:45496a70a8a5 | 99 | #else |
mluis | 0:45496a70a8a5 | 100 | #define LORAWAN_APP_DATA_SIZE 1 |
mluis | 0:45496a70a8a5 | 101 | |
mluis | 0:45496a70a8a5 | 102 | #endif |
mluis | 0:45496a70a8a5 | 103 | |
mluis | 0:45496a70a8a5 | 104 | static uint8_t DevEui[] = LORAWAN_DEVICE_EUI; |
mluis | 0:45496a70a8a5 | 105 | static uint8_t AppEui[] = LORAWAN_APPLICATION_EUI; |
mluis | 0:45496a70a8a5 | 106 | static uint8_t AppKey[] = LORAWAN_APPLICATION_KEY; |
mluis | 0:45496a70a8a5 | 107 | |
mluis | 5:62862ef9480b | 108 | #if( OVER_THE_AIR_ACTIVATION == 0 ) |
mluis | 0:45496a70a8a5 | 109 | |
mluis | 0:45496a70a8a5 | 110 | static uint8_t NwkSKey[] = LORAWAN_NWKSKEY; |
mluis | 0:45496a70a8a5 | 111 | static uint8_t AppSKey[] = LORAWAN_APPSKEY; |
mluis | 0:45496a70a8a5 | 112 | |
mluis | 0:45496a70a8a5 | 113 | /*! |
mluis | 0:45496a70a8a5 | 114 | * Device address |
mluis | 0:45496a70a8a5 | 115 | */ |
mluis | 0:45496a70a8a5 | 116 | static uint32_t DevAddr = LORAWAN_DEVICE_ADDRESS; |
mluis | 0:45496a70a8a5 | 117 | |
mluis | 0:45496a70a8a5 | 118 | #endif |
mluis | 0:45496a70a8a5 | 119 | |
mluis | 0:45496a70a8a5 | 120 | /*! |
mluis | 0:45496a70a8a5 | 121 | * Application port |
mluis | 0:45496a70a8a5 | 122 | */ |
mluis | 0:45496a70a8a5 | 123 | static uint8_t AppPort = LORAWAN_APP_PORT; |
mluis | 0:45496a70a8a5 | 124 | |
mluis | 0:45496a70a8a5 | 125 | /*! |
mluis | 0:45496a70a8a5 | 126 | * User application data size |
mluis | 0:45496a70a8a5 | 127 | */ |
mluis | 0:45496a70a8a5 | 128 | static uint8_t AppDataSize = LORAWAN_APP_DATA_SIZE; |
mluis | 0:45496a70a8a5 | 129 | |
mluis | 0:45496a70a8a5 | 130 | /*! |
mluis | 0:45496a70a8a5 | 131 | * User application data buffer size |
mluis | 0:45496a70a8a5 | 132 | */ |
mluis | 0:45496a70a8a5 | 133 | #define LORAWAN_APP_DATA_MAX_SIZE 64 |
mluis | 0:45496a70a8a5 | 134 | |
mluis | 0:45496a70a8a5 | 135 | /*! |
mluis | 0:45496a70a8a5 | 136 | * User application data |
mluis | 0:45496a70a8a5 | 137 | */ |
mluis | 0:45496a70a8a5 | 138 | static uint8_t AppData[LORAWAN_APP_DATA_MAX_SIZE]; |
mluis | 0:45496a70a8a5 | 139 | |
mluis | 0:45496a70a8a5 | 140 | /*! |
mluis | 0:45496a70a8a5 | 141 | * Indicates if the node is sending confirmed or unconfirmed messages |
mluis | 0:45496a70a8a5 | 142 | */ |
mluis | 0:45496a70a8a5 | 143 | static uint8_t IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON; |
mluis | 0:45496a70a8a5 | 144 | |
mluis | 0:45496a70a8a5 | 145 | /*! |
mluis | 0:45496a70a8a5 | 146 | * Defines the application data transmission duty cycle |
mluis | 0:45496a70a8a5 | 147 | */ |
mluis | 0:45496a70a8a5 | 148 | static uint32_t TxDutyCycleTime; |
mluis | 0:45496a70a8a5 | 149 | |
mluis | 0:45496a70a8a5 | 150 | /*! |
mluis | 0:45496a70a8a5 | 151 | * Timer to handle the application data transmission duty cycle |
mluis | 0:45496a70a8a5 | 152 | */ |
mluis | 0:45496a70a8a5 | 153 | static TimerEvent_t TxNextPacketTimer; |
mluis | 0:45496a70a8a5 | 154 | |
mluis | 0:45496a70a8a5 | 155 | /*! |
mluis | 0:45496a70a8a5 | 156 | * Specifies the state of the application LED |
mluis | 0:45496a70a8a5 | 157 | */ |
mluis | 0:45496a70a8a5 | 158 | static bool AppLedStateOn = false; |
mluis | 0:45496a70a8a5 | 159 | volatile bool Led3StateChanged = false; |
mluis | 0:45496a70a8a5 | 160 | /*! |
mluis | 0:45496a70a8a5 | 161 | * Timer to handle the state of LED1 |
mluis | 0:45496a70a8a5 | 162 | */ |
mluis | 0:45496a70a8a5 | 163 | static TimerEvent_t Led1Timer; |
mluis | 0:45496a70a8a5 | 164 | volatile bool Led1State = false; |
mluis | 0:45496a70a8a5 | 165 | volatile bool Led1StateChanged = false; |
mluis | 0:45496a70a8a5 | 166 | /*! |
mluis | 0:45496a70a8a5 | 167 | * Timer to handle the state of LED2 |
mluis | 0:45496a70a8a5 | 168 | */ |
mluis | 0:45496a70a8a5 | 169 | static TimerEvent_t Led2Timer; |
mluis | 0:45496a70a8a5 | 170 | volatile bool Led2State = false; |
mluis | 0:45496a70a8a5 | 171 | volatile bool Led2StateChanged = false; |
mluis | 0:45496a70a8a5 | 172 | |
mluis | 0:45496a70a8a5 | 173 | /*! |
mluis | 0:45496a70a8a5 | 174 | * Indicates if a new packet can be sent |
mluis | 0:45496a70a8a5 | 175 | */ |
mluis | 0:45496a70a8a5 | 176 | static bool NextTx = true; |
mluis | 0:45496a70a8a5 | 177 | |
salvatoregulfo | 8:346c55cb6033 | 178 | /* Helper function for printing floats & doubles */ |
salvatoregulfo | 8:346c55cb6033 | 179 | static char *print_double(char* str, double v, int decimalDigits=2) |
salvatoregulfo | 8:346c55cb6033 | 180 | { |
salvatoregulfo | 8:346c55cb6033 | 181 | int i = 1; |
salvatoregulfo | 8:346c55cb6033 | 182 | int intPart; |
salvatoregulfo | 8:346c55cb6033 | 183 | int fractPart; |
salvatoregulfo | 8:346c55cb6033 | 184 | int len; |
salvatoregulfo | 8:346c55cb6033 | 185 | char *ptr; |
salvatoregulfo | 8:346c55cb6033 | 186 | |
salvatoregulfo | 8:346c55cb6033 | 187 | /* prepare decimal digits multiplicator */ |
salvatoregulfo | 8:346c55cb6033 | 188 | for (;decimalDigits!=0; i*=10, decimalDigits--); |
salvatoregulfo | 8:346c55cb6033 | 189 | |
salvatoregulfo | 8:346c55cb6033 | 190 | /* calculate integer & fractional parts */ |
salvatoregulfo | 8:346c55cb6033 | 191 | intPart = (int)v; |
salvatoregulfo | 8:346c55cb6033 | 192 | fractPart = (int)((v-(double)(int)v)*i); |
salvatoregulfo | 8:346c55cb6033 | 193 | |
salvatoregulfo | 8:346c55cb6033 | 194 | /* fill in integer part */ |
salvatoregulfo | 8:346c55cb6033 | 195 | sprintf(str, "%i.", intPart); |
salvatoregulfo | 8:346c55cb6033 | 196 | |
salvatoregulfo | 8:346c55cb6033 | 197 | /* prepare fill in of fractional part */ |
salvatoregulfo | 8:346c55cb6033 | 198 | len = strlen(str); |
salvatoregulfo | 8:346c55cb6033 | 199 | ptr = &str[len]; |
salvatoregulfo | 8:346c55cb6033 | 200 | |
salvatoregulfo | 8:346c55cb6033 | 201 | /* fill in leading fractional zeros */ |
salvatoregulfo | 8:346c55cb6033 | 202 | for (i/=10;i>1; i/=10, ptr++) { |
salvatoregulfo | 8:346c55cb6033 | 203 | if (fractPart >= i) { |
salvatoregulfo | 8:346c55cb6033 | 204 | break; |
salvatoregulfo | 8:346c55cb6033 | 205 | } |
salvatoregulfo | 8:346c55cb6033 | 206 | *ptr = '0'; |
salvatoregulfo | 8:346c55cb6033 | 207 | } |
salvatoregulfo | 8:346c55cb6033 | 208 | |
salvatoregulfo | 8:346c55cb6033 | 209 | /* fill in (rest of) fractional part */ |
salvatoregulfo | 8:346c55cb6033 | 210 | sprintf(ptr, "%i", fractPart); |
salvatoregulfo | 8:346c55cb6033 | 211 | |
salvatoregulfo | 8:346c55cb6033 | 212 | return str; |
salvatoregulfo | 8:346c55cb6033 | 213 | } |
salvatoregulfo | 8:346c55cb6033 | 214 | |
mluis | 0:45496a70a8a5 | 215 | /*! |
mluis | 0:45496a70a8a5 | 216 | * Device states |
mluis | 0:45496a70a8a5 | 217 | */ |
mluis | 7:5077515c163b | 218 | static enum eDeviceState |
mluis | 0:45496a70a8a5 | 219 | { |
mluis | 0:45496a70a8a5 | 220 | DEVICE_STATE_INIT, |
mluis | 0:45496a70a8a5 | 221 | DEVICE_STATE_JOIN, |
mluis | 0:45496a70a8a5 | 222 | DEVICE_STATE_SEND, |
mluis | 0:45496a70a8a5 | 223 | DEVICE_STATE_CYCLE, |
mluis | 0:45496a70a8a5 | 224 | DEVICE_STATE_SLEEP |
mluis | 0:45496a70a8a5 | 225 | }DeviceState; |
mluis | 0:45496a70a8a5 | 226 | |
mluis | 0:45496a70a8a5 | 227 | /*! |
mluis | 0:45496a70a8a5 | 228 | * LoRaWAN compliance tests support data |
mluis | 0:45496a70a8a5 | 229 | */ |
mluis | 0:45496a70a8a5 | 230 | struct ComplianceTest_s |
mluis | 0:45496a70a8a5 | 231 | { |
mluis | 0:45496a70a8a5 | 232 | bool Running; |
mluis | 0:45496a70a8a5 | 233 | uint8_t State; |
mluis | 0:45496a70a8a5 | 234 | bool IsTxConfirmed; |
mluis | 0:45496a70a8a5 | 235 | uint8_t AppPort; |
mluis | 0:45496a70a8a5 | 236 | uint8_t AppDataSize; |
mluis | 0:45496a70a8a5 | 237 | uint8_t *AppDataBuffer; |
mluis | 0:45496a70a8a5 | 238 | uint16_t DownLinkCounter; |
mluis | 0:45496a70a8a5 | 239 | bool LinkCheck; |
mluis | 0:45496a70a8a5 | 240 | uint8_t DemodMargin; |
mluis | 0:45496a70a8a5 | 241 | uint8_t NbGateways; |
mluis | 0:45496a70a8a5 | 242 | }ComplianceTest; |
mluis | 0:45496a70a8a5 | 243 | |
mluis | 0:45496a70a8a5 | 244 | /* |
mluis | 0:45496a70a8a5 | 245 | * SerialDisplay managment variables |
mluis | 0:45496a70a8a5 | 246 | */ |
mluis | 0:45496a70a8a5 | 247 | |
mluis | 0:45496a70a8a5 | 248 | /*! |
mluis | 0:45496a70a8a5 | 249 | * Indicates if the MAC layer network join status has changed. |
mluis | 0:45496a70a8a5 | 250 | */ |
mluis | 0:45496a70a8a5 | 251 | static bool IsNetworkJoinedStatusUpdate = false; |
mluis | 0:45496a70a8a5 | 252 | |
mluis | 0:45496a70a8a5 | 253 | /*! |
mluis | 0:45496a70a8a5 | 254 | * Strucure containing the Uplink status |
mluis | 0:45496a70a8a5 | 255 | */ |
mluis | 0:45496a70a8a5 | 256 | struct sLoRaMacUplinkStatus |
mluis | 0:45496a70a8a5 | 257 | { |
mluis | 0:45496a70a8a5 | 258 | uint8_t Acked; |
mluis | 0:45496a70a8a5 | 259 | int8_t Datarate; |
mluis | 0:45496a70a8a5 | 260 | uint16_t UplinkCounter; |
mluis | 0:45496a70a8a5 | 261 | uint8_t Port; |
mluis | 0:45496a70a8a5 | 262 | uint8_t *Buffer; |
mluis | 0:45496a70a8a5 | 263 | uint8_t BufferSize; |
salvatoregulfo | 8:346c55cb6033 | 264 | |
salvatoregulfo | 8:346c55cb6033 | 265 | }LoRaMacUplinkStatus; |
mluis | 0:45496a70a8a5 | 266 | volatile bool UplinkStatusUpdated = false; |
mluis | 0:45496a70a8a5 | 267 | |
mluis | 0:45496a70a8a5 | 268 | /*! |
mluis | 0:45496a70a8a5 | 269 | * Strucure containing the Downlink status |
mluis | 0:45496a70a8a5 | 270 | */ |
mluis | 0:45496a70a8a5 | 271 | struct sLoRaMacDownlinkStatus |
mluis | 0:45496a70a8a5 | 272 | { |
mluis | 0:45496a70a8a5 | 273 | int16_t Rssi; |
mluis | 0:45496a70a8a5 | 274 | int8_t Snr; |
mluis | 0:45496a70a8a5 | 275 | uint16_t DownlinkCounter; |
mluis | 0:45496a70a8a5 | 276 | bool RxData; |
mluis | 0:45496a70a8a5 | 277 | uint8_t Port; |
mluis | 0:45496a70a8a5 | 278 | uint8_t *Buffer; |
mluis | 0:45496a70a8a5 | 279 | uint8_t BufferSize; |
mluis | 0:45496a70a8a5 | 280 | }LoRaMacDownlinkStatus; |
mluis | 0:45496a70a8a5 | 281 | volatile bool DownlinkStatusUpdated = false; |
mluis | 0:45496a70a8a5 | 282 | |
mluis | 0:45496a70a8a5 | 283 | void SerialDisplayRefresh( void ) |
mluis | 0:45496a70a8a5 | 284 | { |
mluis | 0:45496a70a8a5 | 285 | MibRequestConfirm_t mibReq; |
mluis | 0:45496a70a8a5 | 286 | |
mluis | 0:45496a70a8a5 | 287 | SerialDisplayInit( ); |
mluis | 0:45496a70a8a5 | 288 | SerialDisplayUpdateActivationMode( OVER_THE_AIR_ACTIVATION ); |
mluis | 0:45496a70a8a5 | 289 | |
mluis | 0:45496a70a8a5 | 290 | #if( OVER_THE_AIR_ACTIVATION == 0 ) |
mluis | 0:45496a70a8a5 | 291 | SerialDisplayUpdateNwkId( LORAWAN_NETWORK_ID ); |
mluis | 0:45496a70a8a5 | 292 | SerialDisplayUpdateDevAddr( DevAddr ); |
mluis | 0:45496a70a8a5 | 293 | SerialDisplayUpdateKey( 12, NwkSKey ); |
mluis | 0:45496a70a8a5 | 294 | SerialDisplayUpdateKey( 13, AppSKey ); |
mluis | 5:62862ef9480b | 295 | #endif |
mluis | 0:45496a70a8a5 | 296 | SerialDisplayUpdateEui( 5, DevEui ); |
mluis | 0:45496a70a8a5 | 297 | SerialDisplayUpdateEui( 6, AppEui ); |
mluis | 0:45496a70a8a5 | 298 | SerialDisplayUpdateKey( 7, AppKey ); |
mluis | 0:45496a70a8a5 | 299 | |
mluis | 0:45496a70a8a5 | 300 | mibReq.Type = MIB_NETWORK_JOINED; |
mluis | 0:45496a70a8a5 | 301 | LoRaMacMibGetRequestConfirm( &mibReq ); |
mluis | 0:45496a70a8a5 | 302 | SerialDisplayUpdateNetworkIsJoined( mibReq.Param.IsNetworkJoined ); |
mluis | 0:45496a70a8a5 | 303 | |
mluis | 0:45496a70a8a5 | 304 | SerialDisplayUpdateAdr( LORAWAN_ADR_ON ); |
mluis | 0:45496a70a8a5 | 305 | #if defined( USE_BAND_868 ) |
mluis | 0:45496a70a8a5 | 306 | SerialDisplayUpdateDutyCycle( LORAWAN_DUTYCYCLE_ON ); |
mluis | 0:45496a70a8a5 | 307 | #else |
mluis | 0:45496a70a8a5 | 308 | SerialDisplayUpdateDutyCycle( false ); |
mluis | 0:45496a70a8a5 | 309 | #endif |
mluis | 0:45496a70a8a5 | 310 | SerialDisplayUpdatePublicNetwork( LORAWAN_PUBLIC_NETWORK ); |
mluis | 0:45496a70a8a5 | 311 | |
mluis | 0:45496a70a8a5 | 312 | SerialDisplayUpdateLedState( 3, AppLedStateOn ); |
mluis | 0:45496a70a8a5 | 313 | } |
mluis | 0:45496a70a8a5 | 314 | |
mluis | 0:45496a70a8a5 | 315 | void SerialRxProcess( void ) |
mluis | 0:45496a70a8a5 | 316 | { |
mluis | 0:45496a70a8a5 | 317 | if( SerialDisplayReadable( ) == true ) |
mluis | 0:45496a70a8a5 | 318 | { |
mluis | 0:45496a70a8a5 | 319 | switch( SerialDisplayGetChar( ) ) |
mluis | 0:45496a70a8a5 | 320 | { |
mluis | 0:45496a70a8a5 | 321 | case 'R': |
mluis | 0:45496a70a8a5 | 322 | case 'r': |
mluis | 0:45496a70a8a5 | 323 | // Refresh Serial screen |
mluis | 0:45496a70a8a5 | 324 | SerialDisplayRefresh( ); |
mluis | 0:45496a70a8a5 | 325 | break; |
mluis | 0:45496a70a8a5 | 326 | default: |
mluis | 0:45496a70a8a5 | 327 | break; |
mluis | 0:45496a70a8a5 | 328 | } |
mluis | 0:45496a70a8a5 | 329 | } |
mluis | 0:45496a70a8a5 | 330 | } |
mluis | 0:45496a70a8a5 | 331 | |
mluis | 0:45496a70a8a5 | 332 | /*! |
mluis | 0:45496a70a8a5 | 333 | * \brief Prepares the payload of the frame |
mluis | 0:45496a70a8a5 | 334 | */ |
salvatoregulfo | 8:346c55cb6033 | 335 | float value1, value2; |
salvatoregulfo | 8:346c55cb6033 | 336 | char buffer1[32]; |
salvatoregulfo | 8:346c55cb6033 | 337 | // int16_t Myaxes_int[3]; |
salvatoregulfo | 8:346c55cb6033 | 338 | // float Myaxes[3]; |
salvatoregulfo | 8:346c55cb6033 | 339 | // float sensitivity; |
salvatoregulfo | 8:346c55cb6033 | 340 | // int32_t axes[3]; |
salvatoregulfo | 8:346c55cb6033 | 341 | // char Cont[64]; |
salvatoregulfo | 8:346c55cb6033 | 342 | uint32_t Cont[64]; |
salvatoregulfo | 8:346c55cb6033 | 343 | // InterruptIn BlueButton(USER_BUTTON); |
mluis | 0:45496a70a8a5 | 344 | static void PrepareTxFrame( uint8_t port ) |
mluis | 0:45496a70a8a5 | 345 | { |
salvatoregulfo | 8:346c55cb6033 | 346 | |
mluis | 0:45496a70a8a5 | 347 | switch( port ) |
mluis | 0:45496a70a8a5 | 348 | { |
salvatoregulfo | 8:346c55cb6033 | 349 | |
salvatoregulfo | 8:346c55cb6033 | 350 | case 13: |
salvatoregulfo | 8:346c55cb6033 | 351 | |
salvatoregulfo | 8:346c55cb6033 | 352 | hum_temp->get_temperature(&value1); |
salvatoregulfo | 8:346c55cb6033 | 353 | hum_temp->get_humidity(&value2); |
salvatoregulfo | 8:346c55cb6033 | 354 | strcpy((char*)Cont, (char*) print_double(buffer1,value2) ); |
salvatoregulfo | 8:346c55cb6033 | 355 | // strcat((char*)Cont, " " ); |
salvatoregulfo | 8:346c55cb6033 | 356 | // strcat((char*)Cont, print_double(buffer1,value2) ); |
salvatoregulfo | 8:346c55cb6033 | 357 | // strcat((char*)Cont, " "); |
salvatoregulfo | 8:346c55cb6033 | 358 | // strcat((char*)Cont, print_double(buffer1,value2)); |
salvatoregulfo | 8:346c55cb6033 | 359 | // strcat((char*)Cont, " "); |
salvatoregulfo | 8:346c55cb6033 | 360 | |
salvatoregulfo | 8:346c55cb6033 | 361 | /* acc_gyro->get_x_axes(axes); |
salvatoregulfo | 8:346c55cb6033 | 362 | strcpy(Cont,(char*) print_double(buffer1,axes[0])); |
salvatoregulfo | 8:346c55cb6033 | 363 | strcat(Cont, " "); |
salvatoregulfo | 8:346c55cb6033 | 364 | strcat(Cont,(char*)print_double(buffer1,axes[1])); |
salvatoregulfo | 8:346c55cb6033 | 365 | strcat(Cont, " "); |
salvatoregulfo | 8:346c55cb6033 | 366 | strcat(Cont,(char*)print_double(buffer1,axes[2])); |
salvatoregulfo | 8:346c55cb6033 | 367 | strcat(Cont, " "); |
salvatoregulfo | 8:346c55cb6033 | 368 | strcat(Cont, (char*)print_double(buffer1,value1)); |
salvatoregulfo | 8:346c55cb6033 | 369 | strcat(Cont, " "); |
salvatoregulfo | 8:346c55cb6033 | 370 | strcat(Cont, (char*)print_double(buffer1,value2)); |
salvatoregulfo | 8:346c55cb6033 | 371 | AppDataSize = sprintf ((char*)AppData, Cont); |
salvatoregulfo | 8:346c55cb6033 | 372 | |
salvatoregulfo | 8:346c55cb6033 | 373 | acc_gyro->get_x_axes_raw(Myaxes_int); |
salvatoregulfo | 8:346c55cb6033 | 374 | acc_gyro->get_x_sensitivity(&sensitivity); |
salvatoregulfo | 8:346c55cb6033 | 375 | Myaxes[0]=((float)(Myaxes_int[0]))*sensitivity; |
salvatoregulfo | 8:346c55cb6033 | 376 | Myaxes[1]=((float)(Myaxes_int[1]))*sensitivity; |
salvatoregulfo | 8:346c55cb6033 | 377 | Myaxes[2]=((float)(Myaxes_int[2]))*sensitivity; |
salvatoregulfo | 8:346c55cb6033 | 378 | strcpy(Cont, (char*) print_double(buffer1, value1)); |
salvatoregulfo | 8:346c55cb6033 | 379 | strcat(Cont, " "); |
salvatoregulfo | 8:346c55cb6033 | 380 | strcat(Cont, (char*) print_double(buffer1, value2)); |
salvatoregulfo | 8:346c55cb6033 | 381 | strcat(Cont, " "); |
salvatoregulfo | 8:346c55cb6033 | 382 | strcat(Cont, (char*) print_double(buffer1, Myaxes[0])); |
salvatoregulfo | 8:346c55cb6033 | 383 | strcat(Cont, " "); |
salvatoregulfo | 8:346c55cb6033 | 384 | strcat(Cont, (char*) print_double(buffer1, Myaxes[1])); |
salvatoregulfo | 8:346c55cb6033 | 385 | strcat(Cont, " "); |
salvatoregulfo | 8:346c55cb6033 | 386 | strcat(Cont, (char*) print_double(buffer1, Myaxes[2])); |
salvatoregulfo | 8:346c55cb6033 | 387 | strcat(Cont, " "); |
salvatoregulfo | 8:346c55cb6033 | 388 | strcat(Cont, (char*) print_double(buffer1, value1)); |
salvatoregulfo | 8:346c55cb6033 | 389 | strcat(Cont, " "); |
salvatoregulfo | 8:346c55cb6033 | 390 | strcat(Cont, (char*) print_double(buffer1, value2)); */ |
salvatoregulfo | 8:346c55cb6033 | 391 | |
salvatoregulfo | 8:346c55cb6033 | 392 | // while(1) { |
salvatoregulfo | 8:346c55cb6033 | 393 | AppDataSize = sprintf ((char*)AppData,(char*)Cont); |
salvatoregulfo | 8:346c55cb6033 | 394 | /* if (BlueButton) |
salvatoregulfo | 8:346c55cb6033 | 395 | { AppDataSize = sprintf((char*)AppData, "0"); |
salvatoregulfo | 8:346c55cb6033 | 396 | break; } |
salvatoregulfo | 8:346c55cb6033 | 397 | else |
salvatoregulfo | 8:346c55cb6033 | 398 | { AppDataSize = sprintf((char*)AppData, "1"); |
salvatoregulfo | 8:346c55cb6033 | 399 | break; } |
salvatoregulfo | 8:346c55cb6033 | 400 | // AppDataSize = sprintf((char*)AppData, "69"); |
salvatoregulfo | 8:346c55cb6033 | 401 | //} */ |
salvatoregulfo | 8:346c55cb6033 | 402 | break; |
salvatoregulfo | 8:346c55cb6033 | 403 | |
mluis | 0:45496a70a8a5 | 404 | case 15: |
mluis | 0:45496a70a8a5 | 405 | { |
mluis | 0:45496a70a8a5 | 406 | AppData[0] = AppLedStateOn; |
mluis | 0:45496a70a8a5 | 407 | if( IsTxConfirmed == true ) |
mluis | 0:45496a70a8a5 | 408 | { |
mluis | 0:45496a70a8a5 | 409 | AppData[1] = LoRaMacDownlinkStatus.DownlinkCounter >> 8; |
mluis | 0:45496a70a8a5 | 410 | AppData[2] = LoRaMacDownlinkStatus.DownlinkCounter; |
mluis | 0:45496a70a8a5 | 411 | AppData[3] = LoRaMacDownlinkStatus.Rssi >> 8; |
mluis | 0:45496a70a8a5 | 412 | AppData[4] = LoRaMacDownlinkStatus.Rssi; |
mluis | 0:45496a70a8a5 | 413 | AppData[5] = LoRaMacDownlinkStatus.Snr; |
mluis | 0:45496a70a8a5 | 414 | } |
mluis | 0:45496a70a8a5 | 415 | } |
salvatoregulfo | 8:346c55cb6033 | 416 | break; |
mluis | 0:45496a70a8a5 | 417 | case 224: |
mluis | 0:45496a70a8a5 | 418 | if( ComplianceTest.LinkCheck == true ) |
mluis | 0:45496a70a8a5 | 419 | { |
mluis | 0:45496a70a8a5 | 420 | ComplianceTest.LinkCheck = false; |
mluis | 0:45496a70a8a5 | 421 | AppDataSize = 3; |
mluis | 0:45496a70a8a5 | 422 | AppData[0] = 5; |
mluis | 0:45496a70a8a5 | 423 | AppData[1] = ComplianceTest.DemodMargin; |
mluis | 0:45496a70a8a5 | 424 | AppData[2] = ComplianceTest.NbGateways; |
mluis | 0:45496a70a8a5 | 425 | ComplianceTest.State = 1; |
mluis | 0:45496a70a8a5 | 426 | } |
mluis | 0:45496a70a8a5 | 427 | else |
mluis | 0:45496a70a8a5 | 428 | { |
mluis | 0:45496a70a8a5 | 429 | switch( ComplianceTest.State ) |
mluis | 0:45496a70a8a5 | 430 | { |
mluis | 0:45496a70a8a5 | 431 | case 4: |
mluis | 0:45496a70a8a5 | 432 | ComplianceTest.State = 1; |
mluis | 0:45496a70a8a5 | 433 | break; |
mluis | 0:45496a70a8a5 | 434 | case 1: |
mluis | 0:45496a70a8a5 | 435 | AppDataSize = 2; |
mluis | 0:45496a70a8a5 | 436 | AppData[0] = ComplianceTest.DownLinkCounter >> 8; |
mluis | 0:45496a70a8a5 | 437 | AppData[1] = ComplianceTest.DownLinkCounter; |
mluis | 0:45496a70a8a5 | 438 | break; |
mluis | 0:45496a70a8a5 | 439 | } |
mluis | 0:45496a70a8a5 | 440 | } |
mluis | 0:45496a70a8a5 | 441 | break; |
mluis | 0:45496a70a8a5 | 442 | default: |
mluis | 0:45496a70a8a5 | 443 | break; |
mluis | 0:45496a70a8a5 | 444 | } |
mluis | 0:45496a70a8a5 | 445 | } |
mluis | 0:45496a70a8a5 | 446 | |
mluis | 0:45496a70a8a5 | 447 | /*! |
mluis | 0:45496a70a8a5 | 448 | * \brief Prepares the payload of the frame |
mluis | 0:45496a70a8a5 | 449 | * |
mluis | 0:45496a70a8a5 | 450 | * \retval [0: frame could be send, 1: error] |
mluis | 0:45496a70a8a5 | 451 | */ |
salvatoregulfo | 8:346c55cb6033 | 452 | static bool SendFrame(void) |
salvatoregulfo | 8:346c55cb6033 | 453 | |
mluis | 0:45496a70a8a5 | 454 | { |
mluis | 0:45496a70a8a5 | 455 | McpsReq_t mcpsReq; |
mluis | 0:45496a70a8a5 | 456 | LoRaMacTxInfo_t txInfo; |
mluis | 7:5077515c163b | 457 | |
mluis | 0:45496a70a8a5 | 458 | if( LoRaMacQueryTxPossible( AppDataSize, &txInfo ) != LORAMAC_STATUS_OK ) |
mluis | 0:45496a70a8a5 | 459 | { |
mluis | 0:45496a70a8a5 | 460 | // Send empty frame in order to flush MAC commands |
mluis | 0:45496a70a8a5 | 461 | mcpsReq.Type = MCPS_UNCONFIRMED; |
mluis | 0:45496a70a8a5 | 462 | mcpsReq.Req.Unconfirmed.fBuffer = NULL; |
mluis | 0:45496a70a8a5 | 463 | mcpsReq.Req.Unconfirmed.fBufferSize = 0; |
mluis | 0:45496a70a8a5 | 464 | mcpsReq.Req.Unconfirmed.Datarate = LORAWAN_DEFAULT_DATARATE; |
mluis | 3:3152aa75c58d | 465 | |
mluis | 0:45496a70a8a5 | 466 | LoRaMacUplinkStatus.Acked = false; |
mluis | 0:45496a70a8a5 | 467 | LoRaMacUplinkStatus.Port = 0; |
mluis | 0:45496a70a8a5 | 468 | LoRaMacUplinkStatus.Buffer = NULL; |
mluis | 0:45496a70a8a5 | 469 | LoRaMacUplinkStatus.BufferSize = 0; |
mluis | 0:45496a70a8a5 | 470 | SerialDisplayUpdateFrameType( false ); |
mluis | 0:45496a70a8a5 | 471 | } |
mluis | 0:45496a70a8a5 | 472 | else |
mluis | 0:45496a70a8a5 | 473 | { |
mluis | 0:45496a70a8a5 | 474 | LoRaMacUplinkStatus.Acked = false; |
mluis | 0:45496a70a8a5 | 475 | LoRaMacUplinkStatus.Port = AppPort; |
mluis | 0:45496a70a8a5 | 476 | LoRaMacUplinkStatus.Buffer = AppData; |
mluis | 0:45496a70a8a5 | 477 | LoRaMacUplinkStatus.BufferSize = AppDataSize; |
mluis | 0:45496a70a8a5 | 478 | SerialDisplayUpdateFrameType( IsTxConfirmed ); |
mluis | 0:45496a70a8a5 | 479 | |
mluis | 0:45496a70a8a5 | 480 | if( IsTxConfirmed == false ) |
mluis | 0:45496a70a8a5 | 481 | { |
mluis | 0:45496a70a8a5 | 482 | mcpsReq.Type = MCPS_UNCONFIRMED; |
mluis | 0:45496a70a8a5 | 483 | mcpsReq.Req.Unconfirmed.fPort = AppPort; |
mluis | 0:45496a70a8a5 | 484 | mcpsReq.Req.Unconfirmed.fBuffer = AppData; |
mluis | 0:45496a70a8a5 | 485 | mcpsReq.Req.Unconfirmed.fBufferSize = AppDataSize; |
mluis | 0:45496a70a8a5 | 486 | mcpsReq.Req.Unconfirmed.Datarate = LORAWAN_DEFAULT_DATARATE; |
mluis | 0:45496a70a8a5 | 487 | } |
mluis | 0:45496a70a8a5 | 488 | else |
mluis | 0:45496a70a8a5 | 489 | { |
mluis | 0:45496a70a8a5 | 490 | mcpsReq.Type = MCPS_CONFIRMED; |
mluis | 0:45496a70a8a5 | 491 | mcpsReq.Req.Confirmed.fPort = AppPort; |
mluis | 0:45496a70a8a5 | 492 | mcpsReq.Req.Confirmed.fBuffer = AppData; |
mluis | 0:45496a70a8a5 | 493 | mcpsReq.Req.Confirmed.fBufferSize = AppDataSize; |
mluis | 2:2eeaeae2c725 | 494 | mcpsReq.Req.Confirmed.NbTrials = 8; |
mluis | 0:45496a70a8a5 | 495 | mcpsReq.Req.Confirmed.Datarate = LORAWAN_DEFAULT_DATARATE; |
mluis | 0:45496a70a8a5 | 496 | } |
mluis | 0:45496a70a8a5 | 497 | } |
mluis | 0:45496a70a8a5 | 498 | |
mluis | 0:45496a70a8a5 | 499 | if( LoRaMacMcpsRequest( &mcpsReq ) == LORAMAC_STATUS_OK ) |
mluis | 0:45496a70a8a5 | 500 | { |
mluis | 0:45496a70a8a5 | 501 | return false; |
mluis | 0:45496a70a8a5 | 502 | } |
mluis | 0:45496a70a8a5 | 503 | return true; |
mluis | 0:45496a70a8a5 | 504 | } |
mluis | 0:45496a70a8a5 | 505 | |
mluis | 0:45496a70a8a5 | 506 | /*! |
mluis | 0:45496a70a8a5 | 507 | * \brief Function executed on TxNextPacket Timeout event |
mluis | 0:45496a70a8a5 | 508 | */ |
mluis | 0:45496a70a8a5 | 509 | static void OnTxNextPacketTimerEvent( void ) |
mluis | 0:45496a70a8a5 | 510 | { |
mluis | 0:45496a70a8a5 | 511 | MibRequestConfirm_t mibReq; |
mluis | 0:45496a70a8a5 | 512 | LoRaMacStatus_t status; |
mluis | 0:45496a70a8a5 | 513 | |
mluis | 0:45496a70a8a5 | 514 | TimerStop( &TxNextPacketTimer ); |
mluis | 0:45496a70a8a5 | 515 | |
mluis | 0:45496a70a8a5 | 516 | mibReq.Type = MIB_NETWORK_JOINED; |
mluis | 0:45496a70a8a5 | 517 | status = LoRaMacMibGetRequestConfirm( &mibReq ); |
mluis | 0:45496a70a8a5 | 518 | |
mluis | 0:45496a70a8a5 | 519 | if( status == LORAMAC_STATUS_OK ) |
mluis | 0:45496a70a8a5 | 520 | { |
mluis | 0:45496a70a8a5 | 521 | if( mibReq.Param.IsNetworkJoined == true ) |
mluis | 0:45496a70a8a5 | 522 | { |
mluis | 0:45496a70a8a5 | 523 | DeviceState = DEVICE_STATE_SEND; |
mluis | 0:45496a70a8a5 | 524 | NextTx = true; |
mluis | 0:45496a70a8a5 | 525 | } |
mluis | 0:45496a70a8a5 | 526 | else |
mluis | 0:45496a70a8a5 | 527 | { |
mluis | 0:45496a70a8a5 | 528 | DeviceState = DEVICE_STATE_JOIN; |
mluis | 0:45496a70a8a5 | 529 | } |
mluis | 0:45496a70a8a5 | 530 | } |
mluis | 0:45496a70a8a5 | 531 | } |
mluis | 0:45496a70a8a5 | 532 | |
mluis | 0:45496a70a8a5 | 533 | /*! |
mluis | 0:45496a70a8a5 | 534 | * \brief Function executed on Led 1 Timeout event |
mluis | 0:45496a70a8a5 | 535 | */ |
mluis | 0:45496a70a8a5 | 536 | static void OnLed1TimerEvent( void ) |
mluis | 0:45496a70a8a5 | 537 | { |
mluis | 0:45496a70a8a5 | 538 | TimerStop( &Led1Timer ); |
mluis | 0:45496a70a8a5 | 539 | // Switch LED 1 OFF |
mluis | 0:45496a70a8a5 | 540 | Led1State = false; |
mluis | 0:45496a70a8a5 | 541 | Led1StateChanged = true; |
mluis | 0:45496a70a8a5 | 542 | } |
mluis | 0:45496a70a8a5 | 543 | |
mluis | 0:45496a70a8a5 | 544 | /*! |
mluis | 0:45496a70a8a5 | 545 | * \brief Function executed on Led 2 Timeout event |
mluis | 0:45496a70a8a5 | 546 | */ |
mluis | 0:45496a70a8a5 | 547 | static void OnLed2TimerEvent( void ) |
mluis | 0:45496a70a8a5 | 548 | { |
mluis | 0:45496a70a8a5 | 549 | TimerStop( &Led2Timer ); |
mluis | 0:45496a70a8a5 | 550 | // Switch LED 2 OFF |
mluis | 0:45496a70a8a5 | 551 | Led2State = false; |
mluis | 0:45496a70a8a5 | 552 | Led2StateChanged = true; |
mluis | 0:45496a70a8a5 | 553 | } |
mluis | 0:45496a70a8a5 | 554 | |
mluis | 0:45496a70a8a5 | 555 | /*! |
mluis | 0:45496a70a8a5 | 556 | * \brief MCPS-Confirm event function |
mluis | 0:45496a70a8a5 | 557 | * |
mluis | 3:3152aa75c58d | 558 | * \param [IN] mcpsConfirm - Pointer to the confirm structure, |
mluis | 0:45496a70a8a5 | 559 | * containing confirm attributes. |
mluis | 0:45496a70a8a5 | 560 | */ |
mluis | 3:3152aa75c58d | 561 | static void McpsConfirm( McpsConfirm_t *mcpsConfirm ) |
mluis | 0:45496a70a8a5 | 562 | { |
mluis | 3:3152aa75c58d | 563 | if( mcpsConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK ) |
mluis | 0:45496a70a8a5 | 564 | { |
mluis | 3:3152aa75c58d | 565 | switch( mcpsConfirm->McpsRequest ) |
mluis | 0:45496a70a8a5 | 566 | { |
mluis | 0:45496a70a8a5 | 567 | case MCPS_UNCONFIRMED: |
mluis | 0:45496a70a8a5 | 568 | { |
mluis | 0:45496a70a8a5 | 569 | // Check Datarate |
mluis | 0:45496a70a8a5 | 570 | // Check TxPower |
mluis | 0:45496a70a8a5 | 571 | break; |
mluis | 0:45496a70a8a5 | 572 | } |
mluis | 0:45496a70a8a5 | 573 | case MCPS_CONFIRMED: |
mluis | 0:45496a70a8a5 | 574 | { |
mluis | 0:45496a70a8a5 | 575 | // Check Datarate |
mluis | 0:45496a70a8a5 | 576 | // Check TxPower |
mluis | 0:45496a70a8a5 | 577 | // Check AckReceived |
mluis | 3:3152aa75c58d | 578 | // Check NbTrials |
mluis | 3:3152aa75c58d | 579 | LoRaMacUplinkStatus.Acked = mcpsConfirm->AckReceived; |
mluis | 0:45496a70a8a5 | 580 | break; |
mluis | 0:45496a70a8a5 | 581 | } |
mluis | 0:45496a70a8a5 | 582 | case MCPS_PROPRIETARY: |
mluis | 0:45496a70a8a5 | 583 | { |
mluis | 0:45496a70a8a5 | 584 | break; |
mluis | 0:45496a70a8a5 | 585 | } |
mluis | 0:45496a70a8a5 | 586 | default: |
mluis | 0:45496a70a8a5 | 587 | break; |
mluis | 0:45496a70a8a5 | 588 | } |
mluis | 3:3152aa75c58d | 589 | LoRaMacUplinkStatus.Datarate = mcpsConfirm->Datarate; |
mluis | 3:3152aa75c58d | 590 | LoRaMacUplinkStatus.UplinkCounter = mcpsConfirm->UpLinkCounter; |
mluis | 3:3152aa75c58d | 591 | |
mluis | 5:62862ef9480b | 592 | // Switch LED 1 ON |
mluis | 5:62862ef9480b | 593 | Led1State = true; |
mluis | 5:62862ef9480b | 594 | Led1StateChanged = true; |
mluis | 5:62862ef9480b | 595 | TimerStart( &Led1Timer ); |
mluis | 5:62862ef9480b | 596 | |
mluis | 0:45496a70a8a5 | 597 | UplinkStatusUpdated = true; |
mluis | 0:45496a70a8a5 | 598 | } |
mluis | 0:45496a70a8a5 | 599 | NextTx = true; |
mluis | 0:45496a70a8a5 | 600 | } |
mluis | 0:45496a70a8a5 | 601 | |
mluis | 0:45496a70a8a5 | 602 | /*! |
mluis | 0:45496a70a8a5 | 603 | * \brief MCPS-Indication event function |
mluis | 0:45496a70a8a5 | 604 | * |
mluis | 3:3152aa75c58d | 605 | * \param [IN] mcpsIndication - Pointer to the indication structure, |
mluis | 0:45496a70a8a5 | 606 | * containing indication attributes. |
mluis | 0:45496a70a8a5 | 607 | */ |
mluis | 3:3152aa75c58d | 608 | static void McpsIndication( McpsIndication_t *mcpsIndication ) |
mluis | 0:45496a70a8a5 | 609 | { |
mluis | 3:3152aa75c58d | 610 | if( mcpsIndication->Status != LORAMAC_EVENT_INFO_STATUS_OK ) |
mluis | 0:45496a70a8a5 | 611 | { |
mluis | 0:45496a70a8a5 | 612 | return; |
mluis | 0:45496a70a8a5 | 613 | } |
mluis | 0:45496a70a8a5 | 614 | |
mluis | 3:3152aa75c58d | 615 | switch( mcpsIndication->McpsIndication ) |
mluis | 0:45496a70a8a5 | 616 | { |
mluis | 0:45496a70a8a5 | 617 | case MCPS_UNCONFIRMED: |
mluis | 0:45496a70a8a5 | 618 | { |
mluis | 0:45496a70a8a5 | 619 | break; |
mluis | 0:45496a70a8a5 | 620 | } |
mluis | 0:45496a70a8a5 | 621 | case MCPS_CONFIRMED: |
mluis | 0:45496a70a8a5 | 622 | { |
mluis | 0:45496a70a8a5 | 623 | break; |
mluis | 0:45496a70a8a5 | 624 | } |
mluis | 0:45496a70a8a5 | 625 | case MCPS_PROPRIETARY: |
mluis | 0:45496a70a8a5 | 626 | { |
mluis | 0:45496a70a8a5 | 627 | break; |
mluis | 0:45496a70a8a5 | 628 | } |
mluis | 0:45496a70a8a5 | 629 | case MCPS_MULTICAST: |
mluis | 0:45496a70a8a5 | 630 | { |
mluis | 0:45496a70a8a5 | 631 | break; |
mluis | 0:45496a70a8a5 | 632 | } |
mluis | 0:45496a70a8a5 | 633 | default: |
mluis | 0:45496a70a8a5 | 634 | break; |
mluis | 0:45496a70a8a5 | 635 | } |
mluis | 0:45496a70a8a5 | 636 | |
mluis | 0:45496a70a8a5 | 637 | // Check Multicast |
mluis | 0:45496a70a8a5 | 638 | // Check Port |
mluis | 0:45496a70a8a5 | 639 | // Check Datarate |
mluis | 0:45496a70a8a5 | 640 | // Check FramePending |
mluis | 0:45496a70a8a5 | 641 | // Check Buffer |
mluis | 0:45496a70a8a5 | 642 | // Check BufferSize |
mluis | 0:45496a70a8a5 | 643 | // Check Rssi |
mluis | 0:45496a70a8a5 | 644 | // Check Snr |
mluis | 0:45496a70a8a5 | 645 | // Check RxSlot |
mluis | 3:3152aa75c58d | 646 | LoRaMacDownlinkStatus.Rssi = mcpsIndication->Rssi; |
mluis | 3:3152aa75c58d | 647 | if( mcpsIndication->Snr & 0x80 ) // The SNR sign bit is 1 |
mluis | 0:45496a70a8a5 | 648 | { |
mluis | 0:45496a70a8a5 | 649 | // Invert and divide by 4 |
mluis | 3:3152aa75c58d | 650 | LoRaMacDownlinkStatus.Snr = ( ( ~mcpsIndication->Snr + 1 ) & 0xFF ) >> 2; |
mluis | 0:45496a70a8a5 | 651 | LoRaMacDownlinkStatus.Snr = -LoRaMacDownlinkStatus.Snr; |
mluis | 0:45496a70a8a5 | 652 | } |
mluis | 0:45496a70a8a5 | 653 | else |
mluis | 0:45496a70a8a5 | 654 | { |
mluis | 0:45496a70a8a5 | 655 | // Divide by 4 |
mluis | 3:3152aa75c58d | 656 | LoRaMacDownlinkStatus.Snr = ( mcpsIndication->Snr & 0xFF ) >> 2; |
mluis | 0:45496a70a8a5 | 657 | } |
mluis | 0:45496a70a8a5 | 658 | LoRaMacDownlinkStatus.DownlinkCounter++; |
mluis | 3:3152aa75c58d | 659 | LoRaMacDownlinkStatus.RxData = mcpsIndication->RxData; |
mluis | 3:3152aa75c58d | 660 | LoRaMacDownlinkStatus.Port = mcpsIndication->Port; |
mluis | 3:3152aa75c58d | 661 | LoRaMacDownlinkStatus.Buffer = mcpsIndication->Buffer; |
mluis | 3:3152aa75c58d | 662 | LoRaMacDownlinkStatus.BufferSize = mcpsIndication->BufferSize; |
mluis | 0:45496a70a8a5 | 663 | |
mluis | 0:45496a70a8a5 | 664 | if( ComplianceTest.Running == true ) |
mluis | 0:45496a70a8a5 | 665 | { |
mluis | 0:45496a70a8a5 | 666 | ComplianceTest.DownLinkCounter++; |
mluis | 0:45496a70a8a5 | 667 | } |
mluis | 0:45496a70a8a5 | 668 | |
mluis | 3:3152aa75c58d | 669 | if( mcpsIndication->RxData == true ) |
mluis | 0:45496a70a8a5 | 670 | { |
mluis | 3:3152aa75c58d | 671 | switch( mcpsIndication->Port ) |
mluis | 0:45496a70a8a5 | 672 | { |
mluis | 0:45496a70a8a5 | 673 | case 1: // The application LED can be controlled on port 1 or 2 |
mluis | 0:45496a70a8a5 | 674 | case 2: |
mluis | 3:3152aa75c58d | 675 | if( mcpsIndication->BufferSize == 1 ) |
mluis | 0:45496a70a8a5 | 676 | { |
mluis | 3:3152aa75c58d | 677 | AppLedStateOn = mcpsIndication->Buffer[0] & 0x01; |
mluis | 0:45496a70a8a5 | 678 | Led3StateChanged = true; |
mluis | 0:45496a70a8a5 | 679 | } |
mluis | 0:45496a70a8a5 | 680 | break; |
mluis | 0:45496a70a8a5 | 681 | case 224: |
mluis | 0:45496a70a8a5 | 682 | if( ComplianceTest.Running == false ) |
mluis | 0:45496a70a8a5 | 683 | { |
mluis | 0:45496a70a8a5 | 684 | // Check compliance test enable command (i) |
mluis | 3:3152aa75c58d | 685 | if( ( mcpsIndication->BufferSize == 4 ) && |
mluis | 3:3152aa75c58d | 686 | ( mcpsIndication->Buffer[0] == 0x01 ) && |
mluis | 3:3152aa75c58d | 687 | ( mcpsIndication->Buffer[1] == 0x01 ) && |
mluis | 3:3152aa75c58d | 688 | ( mcpsIndication->Buffer[2] == 0x01 ) && |
mluis | 3:3152aa75c58d | 689 | ( mcpsIndication->Buffer[3] == 0x01 ) ) |
mluis | 0:45496a70a8a5 | 690 | { |
mluis | 0:45496a70a8a5 | 691 | IsTxConfirmed = false; |
mluis | 0:45496a70a8a5 | 692 | AppPort = 224; |
mluis | 0:45496a70a8a5 | 693 | AppDataSize = 2; |
mluis | 0:45496a70a8a5 | 694 | ComplianceTest.DownLinkCounter = 0; |
mluis | 0:45496a70a8a5 | 695 | ComplianceTest.LinkCheck = false; |
mluis | 0:45496a70a8a5 | 696 | ComplianceTest.DemodMargin = 0; |
mluis | 0:45496a70a8a5 | 697 | ComplianceTest.NbGateways = 0; |
mluis | 0:45496a70a8a5 | 698 | ComplianceTest.Running = true; |
mluis | 0:45496a70a8a5 | 699 | ComplianceTest.State = 1; |
mluis | 7:5077515c163b | 700 | |
mluis | 0:45496a70a8a5 | 701 | MibRequestConfirm_t mibReq; |
mluis | 0:45496a70a8a5 | 702 | mibReq.Type = MIB_ADR; |
mluis | 0:45496a70a8a5 | 703 | mibReq.Param.AdrEnable = true; |
mluis | 0:45496a70a8a5 | 704 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 0:45496a70a8a5 | 705 | |
mluis | 0:45496a70a8a5 | 706 | #if defined( USE_BAND_868 ) |
mluis | 0:45496a70a8a5 | 707 | LoRaMacTestSetDutyCycleOn( false ); |
mluis | 0:45496a70a8a5 | 708 | #endif |
mluis | 0:45496a70a8a5 | 709 | } |
mluis | 0:45496a70a8a5 | 710 | } |
mluis | 0:45496a70a8a5 | 711 | else |
mluis | 0:45496a70a8a5 | 712 | { |
mluis | 3:3152aa75c58d | 713 | ComplianceTest.State = mcpsIndication->Buffer[0]; |
mluis | 0:45496a70a8a5 | 714 | switch( ComplianceTest.State ) |
mluis | 0:45496a70a8a5 | 715 | { |
mluis | 0:45496a70a8a5 | 716 | case 0: // Check compliance test disable command (ii) |
mluis | 0:45496a70a8a5 | 717 | IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON; |
mluis | 0:45496a70a8a5 | 718 | AppPort = LORAWAN_APP_PORT; |
mluis | 0:45496a70a8a5 | 719 | AppDataSize = LORAWAN_APP_DATA_SIZE; |
mluis | 0:45496a70a8a5 | 720 | ComplianceTest.DownLinkCounter = 0; |
mluis | 0:45496a70a8a5 | 721 | ComplianceTest.Running = false; |
mluis | 7:5077515c163b | 722 | |
mluis | 0:45496a70a8a5 | 723 | MibRequestConfirm_t mibReq; |
mluis | 0:45496a70a8a5 | 724 | mibReq.Type = MIB_ADR; |
mluis | 0:45496a70a8a5 | 725 | mibReq.Param.AdrEnable = LORAWAN_ADR_ON; |
mluis | 0:45496a70a8a5 | 726 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 0:45496a70a8a5 | 727 | #if defined( USE_BAND_868 ) |
mluis | 0:45496a70a8a5 | 728 | LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON ); |
mluis | 0:45496a70a8a5 | 729 | #endif |
mluis | 0:45496a70a8a5 | 730 | break; |
mluis | 0:45496a70a8a5 | 731 | case 1: // (iii, iv) |
mluis | 0:45496a70a8a5 | 732 | AppDataSize = 2; |
mluis | 0:45496a70a8a5 | 733 | break; |
mluis | 0:45496a70a8a5 | 734 | case 2: // Enable confirmed messages (v) |
mluis | 0:45496a70a8a5 | 735 | IsTxConfirmed = true; |
mluis | 0:45496a70a8a5 | 736 | ComplianceTest.State = 1; |
mluis | 0:45496a70a8a5 | 737 | break; |
mluis | 0:45496a70a8a5 | 738 | case 3: // Disable confirmed messages (vi) |
mluis | 0:45496a70a8a5 | 739 | IsTxConfirmed = false; |
mluis | 0:45496a70a8a5 | 740 | ComplianceTest.State = 1; |
mluis | 0:45496a70a8a5 | 741 | break; |
mluis | 0:45496a70a8a5 | 742 | case 4: // (vii) |
mluis | 3:3152aa75c58d | 743 | AppDataSize = mcpsIndication->BufferSize; |
mluis | 0:45496a70a8a5 | 744 | |
mluis | 0:45496a70a8a5 | 745 | AppData[0] = 4; |
mluis | 0:45496a70a8a5 | 746 | for( uint8_t i = 1; i < AppDataSize; i++ ) |
mluis | 0:45496a70a8a5 | 747 | { |
mluis | 3:3152aa75c58d | 748 | AppData[i] = mcpsIndication->Buffer[i] + 1; |
mluis | 0:45496a70a8a5 | 749 | } |
mluis | 0:45496a70a8a5 | 750 | break; |
mluis | 0:45496a70a8a5 | 751 | case 5: // (viii) |
mluis | 0:45496a70a8a5 | 752 | { |
mluis | 0:45496a70a8a5 | 753 | MlmeReq_t mlmeReq; |
mluis | 0:45496a70a8a5 | 754 | mlmeReq.Type = MLME_LINK_CHECK; |
mluis | 0:45496a70a8a5 | 755 | LoRaMacMlmeRequest( &mlmeReq ); |
mluis | 0:45496a70a8a5 | 756 | } |
mluis | 0:45496a70a8a5 | 757 | break; |
mluis | 5:62862ef9480b | 758 | case 6: // (ix) |
mluis | 5:62862ef9480b | 759 | { |
mluis | 5:62862ef9480b | 760 | MlmeReq_t mlmeReq; |
mluis | 5:62862ef9480b | 761 | |
mluis | 7:5077515c163b | 762 | // Disable TestMode and revert back to normal operation |
mluis | 7:5077515c163b | 763 | IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON; |
mluis | 7:5077515c163b | 764 | AppPort = LORAWAN_APP_PORT; |
mluis | 7:5077515c163b | 765 | AppDataSize = LORAWAN_APP_DATA_SIZE; |
mluis | 7:5077515c163b | 766 | ComplianceTest.DownLinkCounter = 0; |
mluis | 7:5077515c163b | 767 | ComplianceTest.Running = false; |
mluis | 7:5077515c163b | 768 | |
mluis | 7:5077515c163b | 769 | MibRequestConfirm_t mibReq; |
mluis | 7:5077515c163b | 770 | mibReq.Type = MIB_ADR; |
mluis | 7:5077515c163b | 771 | mibReq.Param.AdrEnable = LORAWAN_ADR_ON; |
mluis | 7:5077515c163b | 772 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 7:5077515c163b | 773 | #if defined( USE_BAND_868 ) |
mluis | 7:5077515c163b | 774 | LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON ); |
mluis | 7:5077515c163b | 775 | #endif |
mluis | 7:5077515c163b | 776 | |
mluis | 5:62862ef9480b | 777 | mlmeReq.Type = MLME_JOIN; |
mluis | 5:62862ef9480b | 778 | |
mluis | 5:62862ef9480b | 779 | mlmeReq.Req.Join.DevEui = DevEui; |
mluis | 5:62862ef9480b | 780 | mlmeReq.Req.Join.AppEui = AppEui; |
mluis | 5:62862ef9480b | 781 | mlmeReq.Req.Join.AppKey = AppKey; |
mluis | 7:5077515c163b | 782 | mlmeReq.Req.Join.NbTrials = 3; |
mluis | 5:62862ef9480b | 783 | |
mluis | 5:62862ef9480b | 784 | LoRaMacMlmeRequest( &mlmeReq ); |
mluis | 5:62862ef9480b | 785 | DeviceState = DEVICE_STATE_SLEEP; |
mluis | 5:62862ef9480b | 786 | } |
mluis | 5:62862ef9480b | 787 | break; |
mluis | 7:5077515c163b | 788 | case 7: // (x) |
mluis | 7:5077515c163b | 789 | { |
mluis | 7:5077515c163b | 790 | if( mcpsIndication->BufferSize == 3 ) |
mluis | 7:5077515c163b | 791 | { |
mluis | 7:5077515c163b | 792 | MlmeReq_t mlmeReq; |
mluis | 7:5077515c163b | 793 | mlmeReq.Type = MLME_TXCW; |
mluis | 7:5077515c163b | 794 | mlmeReq.Req.TxCw.Timeout = ( uint16_t )( ( mcpsIndication->Buffer[1] << 8 ) | mcpsIndication->Buffer[2] ); |
mluis | 7:5077515c163b | 795 | LoRaMacMlmeRequest( &mlmeReq ); |
mluis | 7:5077515c163b | 796 | } |
mluis | 7:5077515c163b | 797 | else if( mcpsIndication->BufferSize == 7 ) |
mluis | 7:5077515c163b | 798 | { |
mluis | 7:5077515c163b | 799 | MlmeReq_t mlmeReq; |
mluis | 7:5077515c163b | 800 | mlmeReq.Type = MLME_TXCW_1; |
mluis | 7:5077515c163b | 801 | mlmeReq.Req.TxCw.Timeout = ( uint16_t )( ( mcpsIndication->Buffer[1] << 8 ) | mcpsIndication->Buffer[2] ); |
mluis | 7:5077515c163b | 802 | mlmeReq.Req.TxCw.Frequency = ( uint32_t )( ( mcpsIndication->Buffer[3] << 16 ) | ( mcpsIndication->Buffer[4] << 8 ) | mcpsIndication->Buffer[5] ) * 100; |
mluis | 7:5077515c163b | 803 | mlmeReq.Req.TxCw.Power = mcpsIndication->Buffer[6]; |
mluis | 7:5077515c163b | 804 | LoRaMacMlmeRequest( &mlmeReq ); |
mluis | 7:5077515c163b | 805 | } |
mluis | 7:5077515c163b | 806 | ComplianceTest.State = 1; |
mluis | 7:5077515c163b | 807 | } |
mluis | 7:5077515c163b | 808 | break; |
mluis | 0:45496a70a8a5 | 809 | default: |
mluis | 0:45496a70a8a5 | 810 | break; |
mluis | 0:45496a70a8a5 | 811 | } |
mluis | 0:45496a70a8a5 | 812 | } |
mluis | 0:45496a70a8a5 | 813 | break; |
mluis | 0:45496a70a8a5 | 814 | default: |
mluis | 0:45496a70a8a5 | 815 | break; |
mluis | 0:45496a70a8a5 | 816 | } |
mluis | 0:45496a70a8a5 | 817 | } |
mluis | 0:45496a70a8a5 | 818 | |
mluis | 0:45496a70a8a5 | 819 | // Switch LED 2 ON for each received downlink |
mluis | 0:45496a70a8a5 | 820 | Led2State = true; |
mluis | 0:45496a70a8a5 | 821 | Led2StateChanged = true; |
mluis | 0:45496a70a8a5 | 822 | TimerStart( &Led2Timer ); |
mluis | 0:45496a70a8a5 | 823 | DownlinkStatusUpdated = true; |
mluis | 0:45496a70a8a5 | 824 | } |
mluis | 0:45496a70a8a5 | 825 | |
mluis | 0:45496a70a8a5 | 826 | /*! |
mluis | 0:45496a70a8a5 | 827 | * \brief MLME-Confirm event function |
mluis | 0:45496a70a8a5 | 828 | * |
mluis | 3:3152aa75c58d | 829 | * \param [IN] mlmeConfirm - Pointer to the confirm structure, |
mluis | 0:45496a70a8a5 | 830 | * containing confirm attributes. |
mluis | 0:45496a70a8a5 | 831 | */ |
mluis | 3:3152aa75c58d | 832 | static void MlmeConfirm( MlmeConfirm_t *mlmeConfirm ) |
mluis | 0:45496a70a8a5 | 833 | { |
mluis | 7:5077515c163b | 834 | switch( mlmeConfirm->MlmeRequest ) |
mluis | 0:45496a70a8a5 | 835 | { |
mluis | 7:5077515c163b | 836 | case MLME_JOIN: |
mluis | 0:45496a70a8a5 | 837 | { |
mluis | 7:5077515c163b | 838 | if( mlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK ) |
mluis | 0:45496a70a8a5 | 839 | { |
mluis | 0:45496a70a8a5 | 840 | // Status is OK, node has joined the network |
mluis | 0:45496a70a8a5 | 841 | IsNetworkJoinedStatusUpdate = true; |
mluis | 5:62862ef9480b | 842 | DeviceState = DEVICE_STATE_SEND; |
mluis | 7:5077515c163b | 843 | } |
mluis | 7:5077515c163b | 844 | else |
mluis | 7:5077515c163b | 845 | { |
mluis | 7:5077515c163b | 846 | // Join was not successful. Try to join again |
mluis | 7:5077515c163b | 847 | DeviceState = DEVICE_STATE_JOIN; |
mluis | 0:45496a70a8a5 | 848 | } |
mluis | 7:5077515c163b | 849 | break; |
mluis | 7:5077515c163b | 850 | } |
mluis | 7:5077515c163b | 851 | case MLME_LINK_CHECK: |
mluis | 7:5077515c163b | 852 | { |
mluis | 7:5077515c163b | 853 | if( mlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK ) |
mluis | 0:45496a70a8a5 | 854 | { |
mluis | 0:45496a70a8a5 | 855 | // Check DemodMargin |
mluis | 0:45496a70a8a5 | 856 | // Check NbGateways |
mluis | 0:45496a70a8a5 | 857 | if( ComplianceTest.Running == true ) |
mluis | 0:45496a70a8a5 | 858 | { |
mluis | 0:45496a70a8a5 | 859 | ComplianceTest.LinkCheck = true; |
mluis | 3:3152aa75c58d | 860 | ComplianceTest.DemodMargin = mlmeConfirm->DemodMargin; |
mluis | 3:3152aa75c58d | 861 | ComplianceTest.NbGateways = mlmeConfirm->NbGateways; |
mluis | 0:45496a70a8a5 | 862 | } |
mluis | 0:45496a70a8a5 | 863 | } |
mluis | 7:5077515c163b | 864 | break; |
mluis | 0:45496a70a8a5 | 865 | } |
mluis | 7:5077515c163b | 866 | default: |
mluis | 7:5077515c163b | 867 | break; |
mluis | 0:45496a70a8a5 | 868 | } |
mluis | 0:45496a70a8a5 | 869 | NextTx = true; |
mluis | 0:45496a70a8a5 | 870 | UplinkStatusUpdated = true; |
mluis | 0:45496a70a8a5 | 871 | } |
mluis | 0:45496a70a8a5 | 872 | |
mluis | 0:45496a70a8a5 | 873 | /** |
mluis | 0:45496a70a8a5 | 874 | * Main application entry point. |
mluis | 0:45496a70a8a5 | 875 | */ |
mluis | 0:45496a70a8a5 | 876 | int main( void ) |
mluis | 0:45496a70a8a5 | 877 | { |
mluis | 0:45496a70a8a5 | 878 | LoRaMacPrimitives_t LoRaMacPrimitives; |
mluis | 0:45496a70a8a5 | 879 | LoRaMacCallback_t LoRaMacCallbacks; |
mluis | 0:45496a70a8a5 | 880 | MibRequestConfirm_t mibReq; |
salvatoregulfo | 8:346c55cb6033 | 881 | |
salvatoregulfo | 8:346c55cb6033 | 882 | //enable sensors |
salvatoregulfo | 8:346c55cb6033 | 883 | // accelerometer -> enable(); |
salvatoregulfo | 8:346c55cb6033 | 884 | hum_temp -> enable(); |
salvatoregulfo | 8:346c55cb6033 | 885 | // acc_gyro->enable_x(); |
salvatoregulfo | 8:346c55cb6033 | 886 | // acc_gyro->enable_g(); |
mluis | 0:45496a70a8a5 | 887 | |
mluis | 0:45496a70a8a5 | 888 | BoardInit( ); |
mluis | 0:45496a70a8a5 | 889 | SerialDisplayInit( ); |
mluis | 0:45496a70a8a5 | 890 | |
mluis | 5:62862ef9480b | 891 | SerialDisplayUpdateEui( 5, DevEui ); |
mluis | 5:62862ef9480b | 892 | SerialDisplayUpdateEui( 6, AppEui ); |
mluis | 5:62862ef9480b | 893 | SerialDisplayUpdateKey( 7, AppKey ); |
salvatoregulfo | 8:346c55cb6033 | 894 | |
salvatoregulfo | 8:346c55cb6033 | 895 | |
salvatoregulfo | 8:346c55cb6033 | 896 | |
mluis | 5:62862ef9480b | 897 | |
mluis | 5:62862ef9480b | 898 | #if( OVER_THE_AIR_ACTIVATION == 0 ) |
mluis | 5:62862ef9480b | 899 | SerialDisplayUpdateNwkId( LORAWAN_NETWORK_ID ); |
mluis | 5:62862ef9480b | 900 | SerialDisplayUpdateDevAddr( DevAddr ); |
mluis | 5:62862ef9480b | 901 | SerialDisplayUpdateKey( 12, NwkSKey ); |
mluis | 5:62862ef9480b | 902 | SerialDisplayUpdateKey( 13, AppSKey ); |
mluis | 5:62862ef9480b | 903 | #endif |
mluis | 5:62862ef9480b | 904 | |
mluis | 0:45496a70a8a5 | 905 | DeviceState = DEVICE_STATE_INIT; |
mluis | 0:45496a70a8a5 | 906 | |
mluis | 0:45496a70a8a5 | 907 | while( 1 ) |
mluis | 0:45496a70a8a5 | 908 | { |
mluis | 0:45496a70a8a5 | 909 | SerialRxProcess( ); |
mluis | 0:45496a70a8a5 | 910 | if( IsNetworkJoinedStatusUpdate == true ) |
mluis | 0:45496a70a8a5 | 911 | { |
mluis | 0:45496a70a8a5 | 912 | IsNetworkJoinedStatusUpdate = false; |
mluis | 0:45496a70a8a5 | 913 | mibReq.Type = MIB_NETWORK_JOINED; |
mluis | 0:45496a70a8a5 | 914 | LoRaMacMibGetRequestConfirm( &mibReq ); |
mluis | 0:45496a70a8a5 | 915 | SerialDisplayUpdateNetworkIsJoined( mibReq.Param.IsNetworkJoined ); |
mluis | 0:45496a70a8a5 | 916 | } |
mluis | 0:45496a70a8a5 | 917 | if( Led1StateChanged == true ) |
mluis | 0:45496a70a8a5 | 918 | { |
mluis | 0:45496a70a8a5 | 919 | Led1StateChanged = false; |
mluis | 0:45496a70a8a5 | 920 | SerialDisplayUpdateLedState( 1, Led1State ); |
mluis | 0:45496a70a8a5 | 921 | } |
mluis | 0:45496a70a8a5 | 922 | if( Led2StateChanged == true ) |
mluis | 0:45496a70a8a5 | 923 | { |
mluis | 0:45496a70a8a5 | 924 | Led2StateChanged = false; |
mluis | 0:45496a70a8a5 | 925 | SerialDisplayUpdateLedState( 2, Led2State ); |
mluis | 0:45496a70a8a5 | 926 | } |
mluis | 0:45496a70a8a5 | 927 | if( Led3StateChanged == true ) |
mluis | 0:45496a70a8a5 | 928 | { |
mluis | 0:45496a70a8a5 | 929 | Led3StateChanged = false; |
mluis | 0:45496a70a8a5 | 930 | SerialDisplayUpdateLedState( 3, AppLedStateOn ); |
mluis | 0:45496a70a8a5 | 931 | } |
mluis | 0:45496a70a8a5 | 932 | if( UplinkStatusUpdated == true ) |
mluis | 0:45496a70a8a5 | 933 | { |
mluis | 0:45496a70a8a5 | 934 | UplinkStatusUpdated = false; |
mluis | 0:45496a70a8a5 | 935 | SerialDisplayUpdateUplink( LoRaMacUplinkStatus.Acked, LoRaMacUplinkStatus.Datarate, LoRaMacUplinkStatus.UplinkCounter, LoRaMacUplinkStatus.Port, LoRaMacUplinkStatus.Buffer, LoRaMacUplinkStatus.BufferSize ); |
mluis | 0:45496a70a8a5 | 936 | } |
mluis | 0:45496a70a8a5 | 937 | if( DownlinkStatusUpdated == true ) |
mluis | 0:45496a70a8a5 | 938 | { |
mluis | 0:45496a70a8a5 | 939 | DownlinkStatusUpdated = false; |
mluis | 0:45496a70a8a5 | 940 | SerialDisplayUpdateLedState( 2, Led2State ); |
mluis | 0:45496a70a8a5 | 941 | SerialDisplayUpdateDownlink( LoRaMacDownlinkStatus.RxData, LoRaMacDownlinkStatus.Rssi, LoRaMacDownlinkStatus.Snr, LoRaMacDownlinkStatus.DownlinkCounter, LoRaMacDownlinkStatus.Port, LoRaMacDownlinkStatus.Buffer, LoRaMacDownlinkStatus.BufferSize ); |
mluis | 0:45496a70a8a5 | 942 | } |
mluis | 0:45496a70a8a5 | 943 | |
mluis | 0:45496a70a8a5 | 944 | switch( DeviceState ) |
mluis | 0:45496a70a8a5 | 945 | { |
mluis | 0:45496a70a8a5 | 946 | case DEVICE_STATE_INIT: |
mluis | 0:45496a70a8a5 | 947 | { |
mluis | 0:45496a70a8a5 | 948 | LoRaMacPrimitives.MacMcpsConfirm = McpsConfirm; |
mluis | 0:45496a70a8a5 | 949 | LoRaMacPrimitives.MacMcpsIndication = McpsIndication; |
mluis | 0:45496a70a8a5 | 950 | LoRaMacPrimitives.MacMlmeConfirm = MlmeConfirm; |
mluis | 0:45496a70a8a5 | 951 | LoRaMacCallbacks.GetBatteryLevel = BoardGetBatteryLevel; |
mluis | 0:45496a70a8a5 | 952 | LoRaMacInitialization( &LoRaMacPrimitives, &LoRaMacCallbacks ); |
mluis | 0:45496a70a8a5 | 953 | |
mluis | 0:45496a70a8a5 | 954 | TimerInit( &TxNextPacketTimer, OnTxNextPacketTimerEvent ); |
mluis | 0:45496a70a8a5 | 955 | |
mluis | 0:45496a70a8a5 | 956 | TimerInit( &Led1Timer, OnLed1TimerEvent ); |
mluis | 7:5077515c163b | 957 | TimerSetValue( &Led1Timer, 25 ); |
mluis | 0:45496a70a8a5 | 958 | |
mluis | 0:45496a70a8a5 | 959 | TimerInit( &Led2Timer, OnLed2TimerEvent ); |
mluis | 7:5077515c163b | 960 | TimerSetValue( &Led2Timer, 25 ); |
mluis | 0:45496a70a8a5 | 961 | |
mluis | 0:45496a70a8a5 | 962 | mibReq.Type = MIB_ADR; |
mluis | 0:45496a70a8a5 | 963 | mibReq.Param.AdrEnable = LORAWAN_ADR_ON; |
mluis | 0:45496a70a8a5 | 964 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 0:45496a70a8a5 | 965 | |
mluis | 0:45496a70a8a5 | 966 | mibReq.Type = MIB_PUBLIC_NETWORK; |
mluis | 0:45496a70a8a5 | 967 | mibReq.Param.EnablePublicNetwork = LORAWAN_PUBLIC_NETWORK; |
mluis | 0:45496a70a8a5 | 968 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 0:45496a70a8a5 | 969 | |
mluis | 0:45496a70a8a5 | 970 | #if defined( USE_BAND_868 ) |
mluis | 0:45496a70a8a5 | 971 | LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON ); |
mluis | 0:45496a70a8a5 | 972 | SerialDisplayUpdateDutyCycle( LORAWAN_DUTYCYCLE_ON ); |
mluis | 3:3152aa75c58d | 973 | |
mluis | 7:5077515c163b | 974 | #if( USE_SEMTECH_DEFAULT_CHANNEL_LINEUP == 1 ) |
mluis | 3:3152aa75c58d | 975 | LoRaMacChannelAdd( 3, ( ChannelParams_t )LC4 ); |
mluis | 3:3152aa75c58d | 976 | LoRaMacChannelAdd( 4, ( ChannelParams_t )LC5 ); |
mluis | 3:3152aa75c58d | 977 | LoRaMacChannelAdd( 5, ( ChannelParams_t )LC6 ); |
mluis | 3:3152aa75c58d | 978 | LoRaMacChannelAdd( 6, ( ChannelParams_t )LC7 ); |
mluis | 3:3152aa75c58d | 979 | LoRaMacChannelAdd( 7, ( ChannelParams_t )LC8 ); |
mluis | 3:3152aa75c58d | 980 | LoRaMacChannelAdd( 8, ( ChannelParams_t )LC9 ); |
mluis | 3:3152aa75c58d | 981 | LoRaMacChannelAdd( 9, ( ChannelParams_t )LC10 ); |
mluis | 5:62862ef9480b | 982 | |
mluis | 7:5077515c163b | 983 | mibReq.Type = MIB_RX2_DEFAULT_CHANNEL; |
mluis | 7:5077515c163b | 984 | mibReq.Param.Rx2DefaultChannel = ( Rx2ChannelParams_t ){ 869525000, DR_3 }; |
mluis | 7:5077515c163b | 985 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 7:5077515c163b | 986 | |
mluis | 5:62862ef9480b | 987 | mibReq.Type = MIB_RX2_CHANNEL; |
mluis | 5:62862ef9480b | 988 | mibReq.Param.Rx2Channel = ( Rx2ChannelParams_t ){ 869525000, DR_3 }; |
mluis | 5:62862ef9480b | 989 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 3:3152aa75c58d | 990 | #endif |
mluis | 3:3152aa75c58d | 991 | |
mluis | 0:45496a70a8a5 | 992 | #endif |
mluis | 0:45496a70a8a5 | 993 | SerialDisplayUpdateActivationMode( OVER_THE_AIR_ACTIVATION ); |
mluis | 0:45496a70a8a5 | 994 | SerialDisplayUpdateAdr( LORAWAN_ADR_ON ); |
mluis | 0:45496a70a8a5 | 995 | SerialDisplayUpdatePublicNetwork( LORAWAN_PUBLIC_NETWORK ); |
mluis | 0:45496a70a8a5 | 996 | |
mluis | 0:45496a70a8a5 | 997 | LoRaMacDownlinkStatus.DownlinkCounter = 0; |
mluis | 0:45496a70a8a5 | 998 | |
mluis | 0:45496a70a8a5 | 999 | DeviceState = DEVICE_STATE_JOIN; |
mluis | 0:45496a70a8a5 | 1000 | break; |
mluis | 0:45496a70a8a5 | 1001 | } |
mluis | 0:45496a70a8a5 | 1002 | case DEVICE_STATE_JOIN: |
mluis | 0:45496a70a8a5 | 1003 | { |
mluis | 0:45496a70a8a5 | 1004 | #if( OVER_THE_AIR_ACTIVATION != 0 ) |
mluis | 0:45496a70a8a5 | 1005 | MlmeReq_t mlmeReq; |
mluis | 0:45496a70a8a5 | 1006 | |
mluis | 0:45496a70a8a5 | 1007 | mlmeReq.Type = MLME_JOIN; |
mluis | 0:45496a70a8a5 | 1008 | |
mluis | 0:45496a70a8a5 | 1009 | mlmeReq.Req.Join.DevEui = DevEui; |
mluis | 0:45496a70a8a5 | 1010 | mlmeReq.Req.Join.AppEui = AppEui; |
mluis | 0:45496a70a8a5 | 1011 | mlmeReq.Req.Join.AppKey = AppKey; |
mluis | 0:45496a70a8a5 | 1012 | |
mluis | 0:45496a70a8a5 | 1013 | if( NextTx == true ) |
mluis | 0:45496a70a8a5 | 1014 | { |
mluis | 0:45496a70a8a5 | 1015 | LoRaMacMlmeRequest( &mlmeReq ); |
mluis | 0:45496a70a8a5 | 1016 | } |
mluis | 5:62862ef9480b | 1017 | DeviceState = DEVICE_STATE_SLEEP; |
mluis | 0:45496a70a8a5 | 1018 | #else |
mluis | 0:45496a70a8a5 | 1019 | mibReq.Type = MIB_NET_ID; |
mluis | 0:45496a70a8a5 | 1020 | mibReq.Param.NetID = LORAWAN_NETWORK_ID; |
mluis | 0:45496a70a8a5 | 1021 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 0:45496a70a8a5 | 1022 | |
mluis | 0:45496a70a8a5 | 1023 | mibReq.Type = MIB_DEV_ADDR; |
mluis | 0:45496a70a8a5 | 1024 | mibReq.Param.DevAddr = DevAddr; |
mluis | 0:45496a70a8a5 | 1025 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 0:45496a70a8a5 | 1026 | |
mluis | 0:45496a70a8a5 | 1027 | mibReq.Type = MIB_NWK_SKEY; |
mluis | 0:45496a70a8a5 | 1028 | mibReq.Param.NwkSKey = NwkSKey; |
mluis | 0:45496a70a8a5 | 1029 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 0:45496a70a8a5 | 1030 | |
mluis | 0:45496a70a8a5 | 1031 | mibReq.Type = MIB_APP_SKEY; |
mluis | 0:45496a70a8a5 | 1032 | mibReq.Param.AppSKey = AppSKey; |
mluis | 0:45496a70a8a5 | 1033 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 0:45496a70a8a5 | 1034 | |
mluis | 0:45496a70a8a5 | 1035 | mibReq.Type = MIB_NETWORK_JOINED; |
mluis | 0:45496a70a8a5 | 1036 | mibReq.Param.IsNetworkJoined = true; |
mluis | 0:45496a70a8a5 | 1037 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mluis | 0:45496a70a8a5 | 1038 | |
mluis | 0:45496a70a8a5 | 1039 | DeviceState = DEVICE_STATE_SEND; |
mluis | 0:45496a70a8a5 | 1040 | #endif |
mluis | 0:45496a70a8a5 | 1041 | IsNetworkJoinedStatusUpdate = true; |
mluis | 0:45496a70a8a5 | 1042 | break; |
mluis | 0:45496a70a8a5 | 1043 | } |
mluis | 0:45496a70a8a5 | 1044 | case DEVICE_STATE_SEND: |
mluis | 0:45496a70a8a5 | 1045 | { |
mluis | 0:45496a70a8a5 | 1046 | if( NextTx == true ) |
mluis | 0:45496a70a8a5 | 1047 | { |
mluis | 0:45496a70a8a5 | 1048 | SerialDisplayUpdateUplinkAcked( false ); |
mluis | 0:45496a70a8a5 | 1049 | SerialDisplayUpdateDonwlinkRxData( false ); |
mluis | 0:45496a70a8a5 | 1050 | PrepareTxFrame( AppPort ); |
mluis | 0:45496a70a8a5 | 1051 | |
mluis | 0:45496a70a8a5 | 1052 | NextTx = SendFrame( ); |
mluis | 0:45496a70a8a5 | 1053 | } |
mluis | 0:45496a70a8a5 | 1054 | if( ComplianceTest.Running == true ) |
mluis | 0:45496a70a8a5 | 1055 | { |
mluis | 3:3152aa75c58d | 1056 | // Schedule next packet transmission |
mluis | 7:5077515c163b | 1057 | TxDutyCycleTime = 5000; // 5000 ms |
mluis | 0:45496a70a8a5 | 1058 | } |
mluis | 0:45496a70a8a5 | 1059 | else |
mluis | 0:45496a70a8a5 | 1060 | { |
mluis | 0:45496a70a8a5 | 1061 | // Schedule next packet transmission |
mluis | 0:45496a70a8a5 | 1062 | TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND ); |
mluis | 0:45496a70a8a5 | 1063 | } |
mluis | 0:45496a70a8a5 | 1064 | DeviceState = DEVICE_STATE_CYCLE; |
mluis | 0:45496a70a8a5 | 1065 | break; |
mluis | 0:45496a70a8a5 | 1066 | } |
mluis | 0:45496a70a8a5 | 1067 | case DEVICE_STATE_CYCLE: |
mluis | 0:45496a70a8a5 | 1068 | { |
mluis | 3:3152aa75c58d | 1069 | DeviceState = DEVICE_STATE_SLEEP; |
mluis | 3:3152aa75c58d | 1070 | |
mluis | 0:45496a70a8a5 | 1071 | // Schedule next packet transmission |
mluis | 0:45496a70a8a5 | 1072 | TimerSetValue( &TxNextPacketTimer, TxDutyCycleTime ); |
mluis | 0:45496a70a8a5 | 1073 | TimerStart( &TxNextPacketTimer ); |
mluis | 0:45496a70a8a5 | 1074 | break; |
mluis | 0:45496a70a8a5 | 1075 | } |
mluis | 0:45496a70a8a5 | 1076 | case DEVICE_STATE_SLEEP: |
mluis | 0:45496a70a8a5 | 1077 | { |
mluis | 0:45496a70a8a5 | 1078 | // Wake up through events |
mluis | 0:45496a70a8a5 | 1079 | break; |
mluis | 0:45496a70a8a5 | 1080 | } |
mluis | 0:45496a70a8a5 | 1081 | default: |
mluis | 0:45496a70a8a5 | 1082 | { |
mluis | 0:45496a70a8a5 | 1083 | DeviceState = DEVICE_STATE_INIT; |
mluis | 0:45496a70a8a5 | 1084 | break; |
mluis | 0:45496a70a8a5 | 1085 | } |
mluis | 0:45496a70a8a5 | 1086 | } |
mluis | 0:45496a70a8a5 | 1087 | } |
mluis | 0:45496a70a8a5 | 1088 | } |