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