Semtech stack for ELMO - ver. 4.1.0.
Fork of LoRaWAN_Semtech_stack_v4.1 by
main.cpp@5:cbb921e2a03b, 2016-04-22 (annotated)
- Committer:
- mleksio
- Date:
- Fri Apr 22 07:37:04 2016 +0000
- Revision:
- 5:cbb921e2a03b
- Parent:
- 4:0aa5d153c316
Removed unused files.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mleksio | 1:2be292bd43f9 | 1 | /* |
mleksio | 1:2be292bd43f9 | 2 | / _____) _ | | |
mleksio | 1:2be292bd43f9 | 3 | ( (____ _____ ____ _| |_ _____ ____| |__ |
mleksio | 1:2be292bd43f9 | 4 | \____ \| ___ | (_ _) ___ |/ ___) _ \ |
mleksio | 1:2be292bd43f9 | 5 | _____) ) ____| | | || |_| ____( (___| | | | |
mleksio | 1:2be292bd43f9 | 6 | (______/|_____)_|_|_| \__)_____)\____)_| |_| |
mleksio | 1:2be292bd43f9 | 7 | (C)2013 Semtech |
mleksio | 1:2be292bd43f9 | 8 | Description: LoRaMac classA device implementation |
mleksio | 0:c58229885f95 | 9 | License: Revised BSD License, see LICENSE.TXT file include in the project |
mleksio | 1:2be292bd43f9 | 10 | Maintainer: Miguel Luis and Gregory Cristian |
mleksio | 0:c58229885f95 | 11 | */ |
mleksio | 0:c58229885f95 | 12 | |
mleksio | 1:2be292bd43f9 | 13 | /*! \file classA/LoRaMote/main.c */ |
mleksio | 0:c58229885f95 | 14 | |
mleksio | 0:c58229885f95 | 15 | #include <string.h> |
mleksio | 0:c58229885f95 | 16 | #include <math.h> |
mleksio | 1:2be292bd43f9 | 17 | #include "mbed.h" |
mleksio | 0:c58229885f95 | 18 | |
mleksio | 1:2be292bd43f9 | 19 | #include "utilities.h" |
mleksio | 1:2be292bd43f9 | 20 | #include "LoRaMac.h" |
mleksio | 1:2be292bd43f9 | 21 | #include "Comissioning.h" |
pBiczysko | 4:0aa5d153c316 | 22 | #include "LoRaMac-api-v3.h" |
mleksio | 0:c58229885f95 | 23 | |
mleksio | 1:2be292bd43f9 | 24 | DigitalOut Led1(LED1); |
mleksio | 1:2be292bd43f9 | 25 | DigitalOut Led2(LED2); |
mleksio | 0:c58229885f95 | 26 | |
mleksio | 0:c58229885f95 | 27 | /*! |
mleksio | 0:c58229885f95 | 28 | * Join requests trials duty cycle. |
mleksio | 0:c58229885f95 | 29 | */ |
mleksio | 0:c58229885f95 | 30 | #define OVER_THE_AIR_ACTIVATION_DUTYCYCLE 10000000 // 10 [s] value in us |
mleksio | 0:c58229885f95 | 31 | |
mleksio | 0:c58229885f95 | 32 | /*! |
mleksio | 1:2be292bd43f9 | 33 | * Defines the application data transmission duty cycle. 5s, value in [us]. |
mleksio | 0:c58229885f95 | 34 | */ |
pBiczysko | 3:73c2fa59588c | 35 | #define APP_TX_DUTYCYCLE 10000000 |
mleksio | 0:c58229885f95 | 36 | |
mleksio | 0:c58229885f95 | 37 | /*! |
mleksio | 1:2be292bd43f9 | 38 | * Defines a random delay for application data transmission duty cycle. 1s, |
mleksio | 1:2be292bd43f9 | 39 | * value in [us]. |
mleksio | 0:c58229885f95 | 40 | */ |
mleksio | 1:2be292bd43f9 | 41 | #define APP_TX_DUTYCYCLE_RND 1000000 |
mleksio | 0:c58229885f95 | 42 | |
mleksio | 0:c58229885f95 | 43 | /*! |
mleksio | 0:c58229885f95 | 44 | * LoRaWAN confirmed messages |
mleksio | 0:c58229885f95 | 45 | */ |
mleksio | 0:c58229885f95 | 46 | #define LORAWAN_CONFIRMED_MSG_ON false |
mleksio | 0:c58229885f95 | 47 | |
mleksio | 0:c58229885f95 | 48 | /*! |
mleksio | 1:2be292bd43f9 | 49 | * LoRaWAN Adaptive Data Rate |
mleksio | 0:c58229885f95 | 50 | * |
mleksio | 0:c58229885f95 | 51 | * \remark Please note that when ADR is enabled the end-device should be static |
mleksio | 0:c58229885f95 | 52 | */ |
mleksio | 0:c58229885f95 | 53 | #define LORAWAN_ADR_ON 1 |
mleksio | 0:c58229885f95 | 54 | |
mleksio | 1:2be292bd43f9 | 55 | #if defined( USE_BAND_868 ) |
mleksio | 1:2be292bd43f9 | 56 | |
mleksio | 1:2be292bd43f9 | 57 | #include "LoRaMacTest.h" |
mleksio | 1:2be292bd43f9 | 58 | |
mleksio | 0:c58229885f95 | 59 | /*! |
mleksio | 0:c58229885f95 | 60 | * LoRaWAN ETSI duty cycle control enable/disable |
mleksio | 0:c58229885f95 | 61 | * |
mleksio | 0:c58229885f95 | 62 | * \remark Please note that ETSI mandates duty cycled transmissions. Use only for test purposes |
mleksio | 0:c58229885f95 | 63 | */ |
pBiczysko | 4:0aa5d153c316 | 64 | #define LORAWAN_DUTYCYCLE_ON false |
mleksio | 1:2be292bd43f9 | 65 | |
pBiczysko | 4:0aa5d153c316 | 66 | #define USE_SEMTECH_DEFAULT_CHANNEL_LINEUP 0 |
mleksio | 1:2be292bd43f9 | 67 | |
mleksio | 1:2be292bd43f9 | 68 | #if( USE_SEMTECH_DEFAULT_CHANNEL_LINEUP == 1 ) |
mleksio | 1:2be292bd43f9 | 69 | |
mleksio | 1:2be292bd43f9 | 70 | #define LC4 { 867100000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } |
mleksio | 1:2be292bd43f9 | 71 | #define LC5 { 867300000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } |
mleksio | 1:2be292bd43f9 | 72 | #define LC6 { 867500000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } |
mleksio | 1:2be292bd43f9 | 73 | #define LC7 { 867700000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } |
mleksio | 1:2be292bd43f9 | 74 | #define LC8 { 867900000, { ( ( DR_5 << 4 ) | DR_0 ) }, 0 } |
mleksio | 1:2be292bd43f9 | 75 | #define LC9 { 868800000, { ( ( DR_7 << 4 ) | DR_7 ) }, 2 } |
mleksio | 1:2be292bd43f9 | 76 | |
mleksio | 1:2be292bd43f9 | 77 | #endif |
mleksio | 1:2be292bd43f9 | 78 | |
mleksio | 1:2be292bd43f9 | 79 | #endif |
mleksio | 0:c58229885f95 | 80 | |
mleksio | 0:c58229885f95 | 81 | /*! |
mleksio | 0:c58229885f95 | 82 | * LoRaWAN application port |
mleksio | 0:c58229885f95 | 83 | */ |
mleksio | 0:c58229885f95 | 84 | #define LORAWAN_APP_PORT 2 |
mleksio | 0:c58229885f95 | 85 | |
mleksio | 0:c58229885f95 | 86 | /*! |
mleksio | 0:c58229885f95 | 87 | * User application data buffer size |
mleksio | 0:c58229885f95 | 88 | */ |
mleksio | 1:2be292bd43f9 | 89 | #if defined( USE_BAND_868 ) |
mleksio | 1:2be292bd43f9 | 90 | |
mleksio | 0:c58229885f95 | 91 | #define LORAWAN_APP_DATA_SIZE 16 |
mleksio | 0:c58229885f95 | 92 | |
mleksio | 1:2be292bd43f9 | 93 | #elif defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID ) |
mleksio | 1:2be292bd43f9 | 94 | |
mleksio | 1:2be292bd43f9 | 95 | #define LORAWAN_APP_DATA_SIZE 11 |
mleksio | 1:2be292bd43f9 | 96 | |
mleksio | 1:2be292bd43f9 | 97 | #endif |
mleksio | 1:2be292bd43f9 | 98 | |
mleksio | 0:c58229885f95 | 99 | #if( OVER_THE_AIR_ACTIVATION != 0 ) |
mleksio | 0:c58229885f95 | 100 | |
mleksio | 0:c58229885f95 | 101 | static uint8_t DevEui[] = LORAWAN_DEVICE_EUI; |
mleksio | 0:c58229885f95 | 102 | static uint8_t AppEui[] = LORAWAN_APPLICATION_EUI; |
mleksio | 0:c58229885f95 | 103 | static uint8_t AppKey[] = LORAWAN_APPLICATION_KEY; |
mleksio | 0:c58229885f95 | 104 | |
mleksio | 0:c58229885f95 | 105 | #else |
mleksio | 0:c58229885f95 | 106 | |
mleksio | 0:c58229885f95 | 107 | static uint8_t NwkSKey[] = LORAWAN_NWKSKEY; |
mleksio | 0:c58229885f95 | 108 | static uint8_t AppSKey[] = LORAWAN_APPSKEY; |
mleksio | 0:c58229885f95 | 109 | |
mleksio | 0:c58229885f95 | 110 | /*! |
mleksio | 0:c58229885f95 | 111 | * Device address |
mleksio | 0:c58229885f95 | 112 | */ |
mleksio | 0:c58229885f95 | 113 | static uint32_t DevAddr; |
mleksio | 0:c58229885f95 | 114 | |
mleksio | 0:c58229885f95 | 115 | #endif |
mleksio | 0:c58229885f95 | 116 | |
mleksio | 0:c58229885f95 | 117 | /*! |
mleksio | 0:c58229885f95 | 118 | * Application port |
mleksio | 0:c58229885f95 | 119 | */ |
mleksio | 0:c58229885f95 | 120 | static uint8_t AppPort = LORAWAN_APP_PORT; |
mleksio | 0:c58229885f95 | 121 | |
mleksio | 0:c58229885f95 | 122 | /*! |
mleksio | 0:c58229885f95 | 123 | * User application data size |
mleksio | 0:c58229885f95 | 124 | */ |
mleksio | 0:c58229885f95 | 125 | static uint8_t AppDataSize = LORAWAN_APP_DATA_SIZE; |
mleksio | 0:c58229885f95 | 126 | |
mleksio | 0:c58229885f95 | 127 | /*! |
mleksio | 0:c58229885f95 | 128 | * User application data buffer size |
mleksio | 0:c58229885f95 | 129 | */ |
mleksio | 0:c58229885f95 | 130 | #define LORAWAN_APP_DATA_MAX_SIZE 64 |
mleksio | 0:c58229885f95 | 131 | |
mleksio | 0:c58229885f95 | 132 | /*! |
mleksio | 0:c58229885f95 | 133 | * User application data |
mleksio | 0:c58229885f95 | 134 | */ |
mleksio | 0:c58229885f95 | 135 | static uint8_t AppData[LORAWAN_APP_DATA_MAX_SIZE]; |
mleksio | 0:c58229885f95 | 136 | |
mleksio | 0:c58229885f95 | 137 | /*! |
mleksio | 0:c58229885f95 | 138 | * Indicates if the node is sending confirmed or unconfirmed messages |
mleksio | 0:c58229885f95 | 139 | */ |
mleksio | 0:c58229885f95 | 140 | static uint8_t IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON; |
mleksio | 0:c58229885f95 | 141 | |
mleksio | 0:c58229885f95 | 142 | /*! |
mleksio | 0:c58229885f95 | 143 | * Defines the application data transmission duty cycle |
mleksio | 0:c58229885f95 | 144 | */ |
mleksio | 0:c58229885f95 | 145 | static uint32_t TxDutyCycleTime; |
mleksio | 0:c58229885f95 | 146 | |
mleksio | 1:2be292bd43f9 | 147 | /*! |
mleksio | 1:2be292bd43f9 | 148 | * Timer to handle the application data transmission duty cycle |
mleksio | 1:2be292bd43f9 | 149 | */ |
mleksio | 1:2be292bd43f9 | 150 | static Timeout TxNextPacketTimer; |
mleksio | 0:c58229885f95 | 151 | |
mleksio | 1:2be292bd43f9 | 152 | /*! |
mleksio | 1:2be292bd43f9 | 153 | * Specifies the state of the application LED |
mleksio | 1:2be292bd43f9 | 154 | */ |
mleksio | 1:2be292bd43f9 | 155 | static bool AppLedStateOn = false; |
mleksio | 0:c58229885f95 | 156 | |
mleksio | 0:c58229885f95 | 157 | /*! |
mleksio | 1:2be292bd43f9 | 158 | * Timer to handle the state of LED1 |
mleksio | 0:c58229885f95 | 159 | */ |
mleksio | 1:2be292bd43f9 | 160 | static Timeout Led1Timer; |
mleksio | 0:c58229885f95 | 161 | |
mleksio | 1:2be292bd43f9 | 162 | /*! |
mleksio | 1:2be292bd43f9 | 163 | * Timer to handle the state of LED2 |
mleksio | 1:2be292bd43f9 | 164 | */ |
mleksio | 1:2be292bd43f9 | 165 | static Timeout Led2Timer; |
mleksio | 0:c58229885f95 | 166 | |
mleksio | 0:c58229885f95 | 167 | /*! |
mleksio | 0:c58229885f95 | 168 | * Indicates if a new packet can be sent |
mleksio | 0:c58229885f95 | 169 | */ |
mleksio | 1:2be292bd43f9 | 170 | static bool NextTx = true; |
mleksio | 0:c58229885f95 | 171 | |
mleksio | 1:2be292bd43f9 | 172 | /*! |
mleksio | 1:2be292bd43f9 | 173 | * Device states |
mleksio | 1:2be292bd43f9 | 174 | */ |
mleksio | 1:2be292bd43f9 | 175 | static enum eDevicState |
mleksio | 1:2be292bd43f9 | 176 | { |
mleksio | 1:2be292bd43f9 | 177 | DEVICE_STATE_INIT, |
mleksio | 1:2be292bd43f9 | 178 | DEVICE_STATE_JOIN, |
mleksio | 1:2be292bd43f9 | 179 | DEVICE_STATE_SEND, |
mleksio | 1:2be292bd43f9 | 180 | DEVICE_STATE_CYCLE, |
mleksio | 1:2be292bd43f9 | 181 | DEVICE_STATE_SLEEP |
mleksio | 1:2be292bd43f9 | 182 | }DeviceState; |
mleksio | 0:c58229885f95 | 183 | |
mleksio | 0:c58229885f95 | 184 | /*! |
mleksio | 1:2be292bd43f9 | 185 | * LoRaWAN compliance tests support data |
mleksio | 1:2be292bd43f9 | 186 | */ |
mleksio | 1:2be292bd43f9 | 187 | struct ComplianceTest_s |
mleksio | 1:2be292bd43f9 | 188 | { |
mleksio | 1:2be292bd43f9 | 189 | bool Running; |
mleksio | 1:2be292bd43f9 | 190 | uint8_t State; |
mleksio | 1:2be292bd43f9 | 191 | bool IsTxConfirmed; |
mleksio | 1:2be292bd43f9 | 192 | uint8_t AppPort; |
mleksio | 1:2be292bd43f9 | 193 | uint8_t AppDataSize; |
mleksio | 1:2be292bd43f9 | 194 | uint8_t *AppDataBuffer; |
mleksio | 1:2be292bd43f9 | 195 | uint16_t DownLinkCounter; |
mleksio | 1:2be292bd43f9 | 196 | bool LinkCheck; |
mleksio | 1:2be292bd43f9 | 197 | uint8_t DemodMargin; |
mleksio | 1:2be292bd43f9 | 198 | uint8_t NbGateways; |
mleksio | 1:2be292bd43f9 | 199 | }ComplianceTest; |
mleksio | 1:2be292bd43f9 | 200 | |
mleksio | 1:2be292bd43f9 | 201 | /*! |
mleksio | 1:2be292bd43f9 | 202 | * \brief Prepares the payload of the frame |
mleksio | 0:c58229885f95 | 203 | */ |
mleksio | 0:c58229885f95 | 204 | static void PrepareTxFrame( uint8_t port ) |
mleksio | 0:c58229885f95 | 205 | { |
mleksio | 0:c58229885f95 | 206 | switch( port ) |
mleksio | 0:c58229885f95 | 207 | { |
mleksio | 0:c58229885f95 | 208 | case 2: |
mleksio | 0:c58229885f95 | 209 | { |
mleksio | 1:2be292bd43f9 | 210 | #if defined( USE_BAND_868 ) |
mleksio | 0:c58229885f95 | 211 | uint16_t pressure = 0; |
mleksio | 0:c58229885f95 | 212 | int16_t altitudeBar = 0; |
mleksio | 0:c58229885f95 | 213 | int16_t temperature = 0; |
mleksio | 0:c58229885f95 | 214 | int32_t latitude, longitude = 0; |
mleksio | 0:c58229885f95 | 215 | uint16_t altitudeGps = 0xFFFF; |
mleksio | 0:c58229885f95 | 216 | uint8_t batteryLevel = 0; |
mleksio | 0:c58229885f95 | 217 | |
mleksio | 0:c58229885f95 | 218 | //pressure = ( uint16_t )( MPL3115ReadPressure( ) / 10 ); // in hPa / 10 |
mleksio | 0:c58229885f95 | 219 | //temperature = ( int16_t )( MPL3115ReadTemperature( ) * 100 ); // in °C * 100 |
mleksio | 0:c58229885f95 | 220 | //altitudeBar = ( int16_t )( MPL3115ReadAltitude( ) * 10 ); // in m * 10 |
mleksio | 0:c58229885f95 | 221 | //batteryLevel = BoardGetBatteryLevel( ); // 1 (very low) to 254 (fully charged) |
mleksio | 0:c58229885f95 | 222 | //GpsGetLatestGpsPositionBinary( &latitude, &longitude ); |
mleksio | 0:c58229885f95 | 223 | //altitudeGps = GpsGetLatestGpsAltitude( ); // in m |
mleksio | 0:c58229885f95 | 224 | |
mleksio | 0:c58229885f95 | 225 | AppData[0] = AppLedStateOn; |
mleksio | 0:c58229885f95 | 226 | AppData[1] = ( pressure >> 8 ) & 0xFF; |
mleksio | 0:c58229885f95 | 227 | AppData[2] = pressure & 0xFF; |
mleksio | 0:c58229885f95 | 228 | AppData[3] = ( temperature >> 8 ) & 0xFF; |
mleksio | 0:c58229885f95 | 229 | AppData[4] = temperature & 0xFF; |
mleksio | 0:c58229885f95 | 230 | AppData[5] = ( altitudeBar >> 8 ) & 0xFF; |
mleksio | 0:c58229885f95 | 231 | AppData[6] = altitudeBar & 0xFF; |
mleksio | 0:c58229885f95 | 232 | AppData[7] = batteryLevel; |
mleksio | 0:c58229885f95 | 233 | AppData[8] = ( latitude >> 16 ) & 0xFF; |
mleksio | 0:c58229885f95 | 234 | AppData[9] = ( latitude >> 8 ) & 0xFF; |
mleksio | 0:c58229885f95 | 235 | AppData[10] = latitude & 0xFF; |
mleksio | 0:c58229885f95 | 236 | AppData[11] = ( longitude >> 16 ) & 0xFF; |
mleksio | 0:c58229885f95 | 237 | AppData[12] = ( longitude >> 8 ) & 0xFF; |
mleksio | 0:c58229885f95 | 238 | AppData[13] = longitude & 0xFF; |
mleksio | 0:c58229885f95 | 239 | AppData[14] = ( altitudeGps >> 8 ) & 0xFF; |
mleksio | 0:c58229885f95 | 240 | AppData[15] = altitudeGps & 0xFF; |
mleksio | 1:2be292bd43f9 | 241 | #elif defined( USE_BAND_915 ) || defined( USE_BAND_915_HYBRID ) |
mleksio | 1:2be292bd43f9 | 242 | int16_t temperature = 0; |
mleksio | 1:2be292bd43f9 | 243 | int32_t latitude, longitude = 0; |
mleksio | 1:2be292bd43f9 | 244 | uint16_t altitudeGps = 0xFFFF; |
mleksio | 1:2be292bd43f9 | 245 | uint8_t batteryLevel = 0; |
mleksio | 1:2be292bd43f9 | 246 | |
mleksio | 1:2be292bd43f9 | 247 | AppData[0] = AppLedStateOn; |
mleksio | 1:2be292bd43f9 | 248 | AppData[1] = temperature; |
mleksio | 1:2be292bd43f9 | 249 | AppData[2] = batteryLevel; |
mleksio | 1:2be292bd43f9 | 250 | AppData[3] = ( latitude >> 16 ) & 0xFF; |
mleksio | 1:2be292bd43f9 | 251 | AppData[4] = ( latitude >> 8 ) & 0xFF; |
mleksio | 1:2be292bd43f9 | 252 | AppData[5] = latitude & 0xFF; |
mleksio | 1:2be292bd43f9 | 253 | AppData[6] = ( longitude >> 16 ) & 0xFF; |
mleksio | 1:2be292bd43f9 | 254 | AppData[7] = ( longitude >> 8 ) & 0xFF; |
mleksio | 1:2be292bd43f9 | 255 | AppData[8] = longitude & 0xFF; |
mleksio | 1:2be292bd43f9 | 256 | AppData[9] = ( altitudeGps >> 8 ) & 0xFF; |
mleksio | 1:2be292bd43f9 | 257 | AppData[10] = altitudeGps & 0xFF; |
mleksio | 1:2be292bd43f9 | 258 | #endif |
mleksio | 0:c58229885f95 | 259 | } |
mleksio | 0:c58229885f95 | 260 | break; |
mleksio | 0:c58229885f95 | 261 | case 224: |
mleksio | 1:2be292bd43f9 | 262 | if( ComplianceTest.LinkCheck == true ) |
mleksio | 0:c58229885f95 | 263 | { |
mleksio | 1:2be292bd43f9 | 264 | ComplianceTest.LinkCheck = false; |
mleksio | 0:c58229885f95 | 265 | AppDataSize = 3; |
mleksio | 0:c58229885f95 | 266 | AppData[0] = 5; |
mleksio | 1:2be292bd43f9 | 267 | AppData[1] = ComplianceTest.DemodMargin; |
mleksio | 1:2be292bd43f9 | 268 | AppData[2] = ComplianceTest.NbGateways; |
mleksio | 1:2be292bd43f9 | 269 | ComplianceTest.State = 1; |
mleksio | 0:c58229885f95 | 270 | } |
mleksio | 0:c58229885f95 | 271 | else |
mleksio | 0:c58229885f95 | 272 | { |
mleksio | 1:2be292bd43f9 | 273 | switch( ComplianceTest.State ) |
mleksio | 0:c58229885f95 | 274 | { |
mleksio | 1:2be292bd43f9 | 275 | case 4: |
mleksio | 1:2be292bd43f9 | 276 | ComplianceTest.State = 1; |
mleksio | 1:2be292bd43f9 | 277 | break; |
mleksio | 1:2be292bd43f9 | 278 | case 1: |
mleksio | 0:c58229885f95 | 279 | AppDataSize = 2; |
mleksio | 1:2be292bd43f9 | 280 | AppData[0] = ComplianceTest.DownLinkCounter >> 8; |
mleksio | 1:2be292bd43f9 | 281 | AppData[1] = ComplianceTest.DownLinkCounter; |
mleksio | 1:2be292bd43f9 | 282 | break; |
mleksio | 0:c58229885f95 | 283 | } |
mleksio | 0:c58229885f95 | 284 | } |
mleksio | 1:2be292bd43f9 | 285 | break; |
mleksio | 0:c58229885f95 | 286 | default: |
mleksio | 0:c58229885f95 | 287 | break; |
mleksio | 0:c58229885f95 | 288 | } |
mleksio | 0:c58229885f95 | 289 | } |
mleksio | 0:c58229885f95 | 290 | |
mleksio | 1:2be292bd43f9 | 291 | /*! |
mleksio | 1:2be292bd43f9 | 292 | * \brief Prepares the payload of the frame |
mleksio | 1:2be292bd43f9 | 293 | * |
mleksio | 1:2be292bd43f9 | 294 | * \retval [0: frame could be send, 1: error] |
mleksio | 1:2be292bd43f9 | 295 | */ |
mleksio | 1:2be292bd43f9 | 296 | static bool SendFrame( void ) |
mleksio | 0:c58229885f95 | 297 | { |
mleksio | 1:2be292bd43f9 | 298 | McpsReq_t mcpsReq; |
mleksio | 1:2be292bd43f9 | 299 | LoRaMacTxInfo_t txInfo; |
mleksio | 1:2be292bd43f9 | 300 | |
mleksio | 1:2be292bd43f9 | 301 | if( LoRaMacQueryTxPossible( AppDataSize, &txInfo ) != LORAMAC_STATUS_OK ) |
mleksio | 1:2be292bd43f9 | 302 | { |
mleksio | 1:2be292bd43f9 | 303 | // Send empty frame in order to flush MAC commands |
mleksio | 1:2be292bd43f9 | 304 | mcpsReq.Type = MCPS_UNCONFIRMED; |
mleksio | 1:2be292bd43f9 | 305 | mcpsReq.Req.Unconfirmed.fBuffer = NULL; |
mleksio | 1:2be292bd43f9 | 306 | mcpsReq.Req.Unconfirmed.fBufferSize = 0; |
mleksio | 1:2be292bd43f9 | 307 | mcpsReq.Req.Unconfirmed.Datarate = DR_0; |
mleksio | 1:2be292bd43f9 | 308 | } |
mleksio | 1:2be292bd43f9 | 309 | else |
mleksio | 1:2be292bd43f9 | 310 | { |
mleksio | 1:2be292bd43f9 | 311 | if( IsTxConfirmed == false ) |
mleksio | 1:2be292bd43f9 | 312 | { |
mleksio | 1:2be292bd43f9 | 313 | mcpsReq.Type = MCPS_UNCONFIRMED; |
mleksio | 1:2be292bd43f9 | 314 | mcpsReq.Req.Unconfirmed.fPort = AppPort; |
mleksio | 1:2be292bd43f9 | 315 | mcpsReq.Req.Unconfirmed.fBuffer = AppData; |
mleksio | 1:2be292bd43f9 | 316 | mcpsReq.Req.Unconfirmed.fBufferSize = AppDataSize; |
mleksio | 1:2be292bd43f9 | 317 | mcpsReq.Req.Unconfirmed.Datarate = DR_0; |
mleksio | 1:2be292bd43f9 | 318 | } |
mleksio | 1:2be292bd43f9 | 319 | else |
mleksio | 1:2be292bd43f9 | 320 | { |
mleksio | 1:2be292bd43f9 | 321 | mcpsReq.Type = MCPS_CONFIRMED; |
mleksio | 1:2be292bd43f9 | 322 | mcpsReq.Req.Confirmed.fPort = AppPort; |
mleksio | 1:2be292bd43f9 | 323 | mcpsReq.Req.Confirmed.fBuffer = AppData; |
mleksio | 1:2be292bd43f9 | 324 | mcpsReq.Req.Confirmed.fBufferSize = AppDataSize; |
mleksio | 1:2be292bd43f9 | 325 | mcpsReq.Req.Confirmed.NbTrials = 8; |
mleksio | 1:2be292bd43f9 | 326 | mcpsReq.Req.Confirmed.Datarate = DR_0; |
mleksio | 1:2be292bd43f9 | 327 | } |
mleksio | 1:2be292bd43f9 | 328 | } |
mleksio | 1:2be292bd43f9 | 329 | |
mleksio | 1:2be292bd43f9 | 330 | if( LoRaMacMcpsRequest( &mcpsReq ) == LORAMAC_STATUS_OK ) |
mleksio | 1:2be292bd43f9 | 331 | { |
mleksio | 1:2be292bd43f9 | 332 | return false; |
mleksio | 1:2be292bd43f9 | 333 | } |
mleksio | 1:2be292bd43f9 | 334 | return true; |
mleksio | 1:2be292bd43f9 | 335 | } |
mleksio | 1:2be292bd43f9 | 336 | |
mleksio | 1:2be292bd43f9 | 337 | /*! |
mleksio | 1:2be292bd43f9 | 338 | * \brief Function executed on TxNextPacket Timeout event |
mleksio | 1:2be292bd43f9 | 339 | */ |
mleksio | 1:2be292bd43f9 | 340 | static void OnTxNextPacketTimerEvent( void ) |
mleksio | 1:2be292bd43f9 | 341 | { |
mleksio | 1:2be292bd43f9 | 342 | MibRequestConfirm_t mibReq; |
mleksio | 1:2be292bd43f9 | 343 | LoRaMacStatus_t status; |
mleksio | 1:2be292bd43f9 | 344 | |
mleksio | 1:2be292bd43f9 | 345 | TxNextPacketTimer.detach(); |
mleksio | 1:2be292bd43f9 | 346 | |
mleksio | 1:2be292bd43f9 | 347 | mibReq.Type = MIB_NETWORK_JOINED; |
mleksio | 1:2be292bd43f9 | 348 | status = LoRaMacMibGetRequestConfirm( &mibReq ); |
mleksio | 1:2be292bd43f9 | 349 | |
mleksio | 1:2be292bd43f9 | 350 | if( status == LORAMAC_STATUS_OK ) |
mleksio | 0:c58229885f95 | 351 | { |
mleksio | 1:2be292bd43f9 | 352 | if( mibReq.Param.IsNetworkJoined == true ) |
mleksio | 1:2be292bd43f9 | 353 | { |
mleksio | 1:2be292bd43f9 | 354 | DeviceState = DEVICE_STATE_SEND; |
mleksio | 1:2be292bd43f9 | 355 | NextTx = true; |
mleksio | 1:2be292bd43f9 | 356 | } |
mleksio | 1:2be292bd43f9 | 357 | else |
mleksio | 1:2be292bd43f9 | 358 | { |
mleksio | 1:2be292bd43f9 | 359 | DeviceState = DEVICE_STATE_JOIN; |
mleksio | 1:2be292bd43f9 | 360 | } |
mleksio | 1:2be292bd43f9 | 361 | } |
mleksio | 1:2be292bd43f9 | 362 | } |
mleksio | 1:2be292bd43f9 | 363 | |
mleksio | 1:2be292bd43f9 | 364 | /*! |
mleksio | 1:2be292bd43f9 | 365 | * \brief Function executed on Led 1 Timeout event |
mleksio | 1:2be292bd43f9 | 366 | */ |
mleksio | 1:2be292bd43f9 | 367 | static void OnLed1TimerEvent( void ) |
mleksio | 1:2be292bd43f9 | 368 | { |
mleksio | 1:2be292bd43f9 | 369 | Led1Timer.detach(); |
mleksio | 1:2be292bd43f9 | 370 | // Switch LED 1 OFF |
mleksio | 1:2be292bd43f9 | 371 | Led1 = 1; |
mleksio | 1:2be292bd43f9 | 372 | } |
mleksio | 1:2be292bd43f9 | 373 | |
mleksio | 1:2be292bd43f9 | 374 | /*! |
mleksio | 1:2be292bd43f9 | 375 | * \brief Function executed on Led 2 Timeout event |
mleksio | 1:2be292bd43f9 | 376 | */ |
mleksio | 1:2be292bd43f9 | 377 | static void OnLed2TimerEvent( void ) |
mleksio | 1:2be292bd43f9 | 378 | { |
mleksio | 1:2be292bd43f9 | 379 | Led2Timer.detach(); |
mleksio | 1:2be292bd43f9 | 380 | // Switch LED 2 OFF |
mleksio | 1:2be292bd43f9 | 381 | Led2 = 1; |
mleksio | 1:2be292bd43f9 | 382 | } |
mleksio | 1:2be292bd43f9 | 383 | |
mleksio | 1:2be292bd43f9 | 384 | /*! |
mleksio | 1:2be292bd43f9 | 385 | * \brief MCPS-Confirm event function |
mleksio | 1:2be292bd43f9 | 386 | * |
mleksio | 1:2be292bd43f9 | 387 | * \param [IN] McpsConfirm - Pointer to the confirm structure, |
mleksio | 1:2be292bd43f9 | 388 | * containing confirm attributes. |
mleksio | 1:2be292bd43f9 | 389 | */ |
mleksio | 1:2be292bd43f9 | 390 | static void McpsConfirm( McpsConfirm_t *McpsConfirm ) |
mleksio | 1:2be292bd43f9 | 391 | { |
mleksio | 1:2be292bd43f9 | 392 | if( McpsConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK ) |
mleksio | 1:2be292bd43f9 | 393 | { |
mleksio | 1:2be292bd43f9 | 394 | switch( McpsConfirm->McpsRequest ) |
mleksio | 0:c58229885f95 | 395 | { |
mleksio | 1:2be292bd43f9 | 396 | case MCPS_UNCONFIRMED: |
mleksio | 1:2be292bd43f9 | 397 | { |
mleksio | 1:2be292bd43f9 | 398 | // Check Datarate |
mleksio | 1:2be292bd43f9 | 399 | // Check TxPower |
mleksio | 1:2be292bd43f9 | 400 | break; |
mleksio | 1:2be292bd43f9 | 401 | } |
mleksio | 1:2be292bd43f9 | 402 | case MCPS_CONFIRMED: |
mleksio | 1:2be292bd43f9 | 403 | { |
mleksio | 1:2be292bd43f9 | 404 | // Check Datarate |
mleksio | 1:2be292bd43f9 | 405 | // Check TxPower |
mleksio | 1:2be292bd43f9 | 406 | // Check AckReceived |
mleksio | 1:2be292bd43f9 | 407 | // Check NbTrials |
mleksio | 1:2be292bd43f9 | 408 | break; |
mleksio | 1:2be292bd43f9 | 409 | } |
mleksio | 1:2be292bd43f9 | 410 | case MCPS_PROPRIETARY: |
mleksio | 1:2be292bd43f9 | 411 | { |
mleksio | 1:2be292bd43f9 | 412 | break; |
mleksio | 1:2be292bd43f9 | 413 | } |
mleksio | 1:2be292bd43f9 | 414 | default: |
mleksio | 1:2be292bd43f9 | 415 | break; |
mleksio | 0:c58229885f95 | 416 | } |
mleksio | 1:2be292bd43f9 | 417 | |
mleksio | 1:2be292bd43f9 | 418 | // Switch LED 1 ON |
mleksio | 1:2be292bd43f9 | 419 | Led1 = 0; |
mleksio | 1:2be292bd43f9 | 420 | Led1Timer.attach_us(OnLed1TimerEvent, 25000); |
mleksio | 1:2be292bd43f9 | 421 | } |
mleksio | 1:2be292bd43f9 | 422 | NextTx = true; |
mleksio | 1:2be292bd43f9 | 423 | } |
mleksio | 1:2be292bd43f9 | 424 | |
mleksio | 1:2be292bd43f9 | 425 | /*! |
mleksio | 1:2be292bd43f9 | 426 | * \brief MCPS-Indication event function |
mleksio | 1:2be292bd43f9 | 427 | * |
mleksio | 1:2be292bd43f9 | 428 | * \param [IN] McpsIndication - Pointer to the indication structure, |
mleksio | 1:2be292bd43f9 | 429 | * containing indication attributes. |
mleksio | 1:2be292bd43f9 | 430 | */ |
mleksio | 1:2be292bd43f9 | 431 | static void McpsIndication( McpsIndication_t *McpsIndication ) |
mleksio | 1:2be292bd43f9 | 432 | { |
mleksio | 1:2be292bd43f9 | 433 | if( McpsIndication->Status != LORAMAC_EVENT_INFO_STATUS_OK ) |
mleksio | 1:2be292bd43f9 | 434 | { |
mleksio | 1:2be292bd43f9 | 435 | return; |
mleksio | 1:2be292bd43f9 | 436 | } |
mleksio | 1:2be292bd43f9 | 437 | |
mleksio | 1:2be292bd43f9 | 438 | switch( McpsIndication->McpsIndication ) |
mleksio | 1:2be292bd43f9 | 439 | { |
mleksio | 1:2be292bd43f9 | 440 | case MCPS_UNCONFIRMED: |
mleksio | 1:2be292bd43f9 | 441 | { |
mleksio | 1:2be292bd43f9 | 442 | break; |
mleksio | 1:2be292bd43f9 | 443 | } |
mleksio | 1:2be292bd43f9 | 444 | case MCPS_CONFIRMED: |
mleksio | 1:2be292bd43f9 | 445 | { |
mleksio | 1:2be292bd43f9 | 446 | break; |
mleksio | 1:2be292bd43f9 | 447 | } |
mleksio | 1:2be292bd43f9 | 448 | case MCPS_PROPRIETARY: |
mleksio | 1:2be292bd43f9 | 449 | { |
mleksio | 1:2be292bd43f9 | 450 | break; |
mleksio | 1:2be292bd43f9 | 451 | } |
mleksio | 1:2be292bd43f9 | 452 | case MCPS_MULTICAST: |
mleksio | 1:2be292bd43f9 | 453 | { |
mleksio | 1:2be292bd43f9 | 454 | break; |
mleksio | 1:2be292bd43f9 | 455 | } |
mleksio | 1:2be292bd43f9 | 456 | default: |
mleksio | 1:2be292bd43f9 | 457 | break; |
mleksio | 1:2be292bd43f9 | 458 | } |
mleksio | 1:2be292bd43f9 | 459 | |
mleksio | 1:2be292bd43f9 | 460 | // Check Multicast |
mleksio | 1:2be292bd43f9 | 461 | // Check Port |
mleksio | 1:2be292bd43f9 | 462 | // Check Datarate |
mleksio | 1:2be292bd43f9 | 463 | // Check FramePending |
mleksio | 1:2be292bd43f9 | 464 | // Check Buffer |
mleksio | 1:2be292bd43f9 | 465 | // Check BufferSize |
mleksio | 1:2be292bd43f9 | 466 | // Check Rssi |
mleksio | 1:2be292bd43f9 | 467 | // Check Snr |
mleksio | 1:2be292bd43f9 | 468 | // Check RxSlot |
mleksio | 1:2be292bd43f9 | 469 | |
mleksio | 1:2be292bd43f9 | 470 | if( ComplianceTest.Running == true ) |
mleksio | 1:2be292bd43f9 | 471 | { |
mleksio | 1:2be292bd43f9 | 472 | ComplianceTest.DownLinkCounter++; |
mleksio | 1:2be292bd43f9 | 473 | } |
mleksio | 1:2be292bd43f9 | 474 | |
mleksio | 1:2be292bd43f9 | 475 | if( McpsIndication->RxData == true ) |
mleksio | 1:2be292bd43f9 | 476 | { |
mleksio | 1:2be292bd43f9 | 477 | switch( McpsIndication->Port ) |
mleksio | 1:2be292bd43f9 | 478 | { |
mleksio | 1:2be292bd43f9 | 479 | case 1: // The application LED can be controlled on port 1 or 2 |
mleksio | 1:2be292bd43f9 | 480 | case 2: |
mleksio | 1:2be292bd43f9 | 481 | if( McpsIndication->BufferSize == 1 ) |
mleksio | 1:2be292bd43f9 | 482 | { |
mleksio | 1:2be292bd43f9 | 483 | AppLedStateOn = McpsIndication->Buffer[0] & 0x01; |
mleksio | 1:2be292bd43f9 | 484 | //GpioWrite( &Led3, ( ( AppLedStateOn & 0x01 ) != 0 ) ? 0 : 1 ); |
mleksio | 1:2be292bd43f9 | 485 | } |
mleksio | 1:2be292bd43f9 | 486 | break; |
mleksio | 0:c58229885f95 | 487 | case 224: |
mleksio | 1:2be292bd43f9 | 488 | if( ComplianceTest.Running == false ) |
mleksio | 0:c58229885f95 | 489 | { |
mleksio | 0:c58229885f95 | 490 | // Check compliance test enable command (i) |
mleksio | 1:2be292bd43f9 | 491 | if( ( McpsIndication->BufferSize == 4 ) && |
mleksio | 1:2be292bd43f9 | 492 | ( McpsIndication->Buffer[0] == 0x01 ) && |
mleksio | 1:2be292bd43f9 | 493 | ( McpsIndication->Buffer[1] == 0x01 ) && |
mleksio | 1:2be292bd43f9 | 494 | ( McpsIndication->Buffer[2] == 0x01 ) && |
mleksio | 1:2be292bd43f9 | 495 | ( McpsIndication->Buffer[3] == 0x01 ) ) |
mleksio | 0:c58229885f95 | 496 | { |
mleksio | 0:c58229885f95 | 497 | IsTxConfirmed = false; |
mleksio | 0:c58229885f95 | 498 | AppPort = 224; |
mleksio | 0:c58229885f95 | 499 | AppDataSize = 2; |
mleksio | 1:2be292bd43f9 | 500 | ComplianceTest.DownLinkCounter = 0; |
mleksio | 1:2be292bd43f9 | 501 | ComplianceTest.LinkCheck = false; |
mleksio | 1:2be292bd43f9 | 502 | ComplianceTest.DemodMargin = 0; |
mleksio | 1:2be292bd43f9 | 503 | ComplianceTest.NbGateways = 0; |
mleksio | 1:2be292bd43f9 | 504 | ComplianceTest.Running = true; |
mleksio | 1:2be292bd43f9 | 505 | ComplianceTest.State = 1; |
mleksio | 0:c58229885f95 | 506 | |
mleksio | 1:2be292bd43f9 | 507 | MibRequestConfirm_t mibReq; |
mleksio | 1:2be292bd43f9 | 508 | mibReq.Type = MIB_ADR; |
mleksio | 1:2be292bd43f9 | 509 | mibReq.Param.AdrEnable = true; |
mleksio | 1:2be292bd43f9 | 510 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mleksio | 1:2be292bd43f9 | 511 | |
mleksio | 1:2be292bd43f9 | 512 | #if defined( USE_BAND_868 ) |
mleksio | 1:2be292bd43f9 | 513 | LoRaMacTestSetDutyCycleOn( false ); |
mleksio | 1:2be292bd43f9 | 514 | #endif |
mleksio | 0:c58229885f95 | 515 | } |
mleksio | 0:c58229885f95 | 516 | } |
mleksio | 0:c58229885f95 | 517 | else |
mleksio | 0:c58229885f95 | 518 | { |
mleksio | 1:2be292bd43f9 | 519 | ComplianceTest.State = McpsIndication->Buffer[0]; |
mleksio | 1:2be292bd43f9 | 520 | switch( ComplianceTest.State ) |
mleksio | 0:c58229885f95 | 521 | { |
mleksio | 0:c58229885f95 | 522 | case 0: // Check compliance test disable command (ii) |
mleksio | 0:c58229885f95 | 523 | IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON; |
mleksio | 0:c58229885f95 | 524 | AppPort = LORAWAN_APP_PORT; |
mleksio | 0:c58229885f95 | 525 | AppDataSize = LORAWAN_APP_DATA_SIZE; |
mleksio | 1:2be292bd43f9 | 526 | ComplianceTest.DownLinkCounter = 0; |
mleksio | 1:2be292bd43f9 | 527 | ComplianceTest.Running = false; |
mleksio | 0:c58229885f95 | 528 | |
mleksio | 1:2be292bd43f9 | 529 | MibRequestConfirm_t mibReq; |
mleksio | 1:2be292bd43f9 | 530 | mibReq.Type = MIB_ADR; |
mleksio | 1:2be292bd43f9 | 531 | mibReq.Param.AdrEnable = LORAWAN_ADR_ON; |
mleksio | 1:2be292bd43f9 | 532 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mleksio | 1:2be292bd43f9 | 533 | #if defined( USE_BAND_868 ) |
mleksio | 0:c58229885f95 | 534 | LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON ); |
mleksio | 1:2be292bd43f9 | 535 | #endif |
mleksio | 0:c58229885f95 | 536 | break; |
mleksio | 0:c58229885f95 | 537 | case 1: // (iii, iv) |
mleksio | 0:c58229885f95 | 538 | AppDataSize = 2; |
mleksio | 0:c58229885f95 | 539 | break; |
mleksio | 0:c58229885f95 | 540 | case 2: // Enable confirmed messages (v) |
mleksio | 0:c58229885f95 | 541 | IsTxConfirmed = true; |
mleksio | 1:2be292bd43f9 | 542 | ComplianceTest.State = 1; |
mleksio | 0:c58229885f95 | 543 | break; |
mleksio | 0:c58229885f95 | 544 | case 3: // Disable confirmed messages (vi) |
mleksio | 0:c58229885f95 | 545 | IsTxConfirmed = false; |
mleksio | 1:2be292bd43f9 | 546 | ComplianceTest.State = 1; |
mleksio | 0:c58229885f95 | 547 | break; |
mleksio | 0:c58229885f95 | 548 | case 4: // (vii) |
mleksio | 1:2be292bd43f9 | 549 | AppDataSize = McpsIndication->BufferSize; |
mleksio | 1:2be292bd43f9 | 550 | |
mleksio | 0:c58229885f95 | 551 | AppData[0] = 4; |
mleksio | 0:c58229885f95 | 552 | for( uint8_t i = 1; i < AppDataSize; i++ ) |
mleksio | 0:c58229885f95 | 553 | { |
mleksio | 1:2be292bd43f9 | 554 | AppData[i] = McpsIndication->Buffer[i] + 1; |
mleksio | 0:c58229885f95 | 555 | } |
mleksio | 0:c58229885f95 | 556 | break; |
mleksio | 0:c58229885f95 | 557 | case 5: // (viii) |
mleksio | 1:2be292bd43f9 | 558 | { |
mleksio | 1:2be292bd43f9 | 559 | MlmeReq_t mlmeReq; |
mleksio | 1:2be292bd43f9 | 560 | mlmeReq.Type = MLME_LINK_CHECK; |
mleksio | 1:2be292bd43f9 | 561 | LoRaMacMlmeRequest( &mlmeReq ); |
mleksio | 1:2be292bd43f9 | 562 | } |
mleksio | 0:c58229885f95 | 563 | break; |
mleksio | 0:c58229885f95 | 564 | default: |
mleksio | 0:c58229885f95 | 565 | break; |
mleksio | 0:c58229885f95 | 566 | } |
mleksio | 0:c58229885f95 | 567 | } |
mleksio | 0:c58229885f95 | 568 | break; |
mleksio | 0:c58229885f95 | 569 | default: |
mleksio | 0:c58229885f95 | 570 | break; |
mleksio | 1:2be292bd43f9 | 571 | } |
mleksio | 0:c58229885f95 | 572 | } |
mleksio | 0:c58229885f95 | 573 | |
mleksio | 1:2be292bd43f9 | 574 | // Switch LED 2 ON for each received downlink |
mleksio | 1:2be292bd43f9 | 575 | Led2 = 0; |
mleksio | 1:2be292bd43f9 | 576 | Led2Timer.attach_us(OnLed2TimerEvent, 25000); |
mleksio | 0:c58229885f95 | 577 | } |
mleksio | 0:c58229885f95 | 578 | |
mleksio | 0:c58229885f95 | 579 | /*! |
mleksio | 1:2be292bd43f9 | 580 | * \brief MLME-Confirm event function |
mleksio | 1:2be292bd43f9 | 581 | * |
mleksio | 1:2be292bd43f9 | 582 | * \param [IN] MlmeConfirm - Pointer to the confirm structure, |
mleksio | 1:2be292bd43f9 | 583 | * containing confirm attributes. |
mleksio | 0:c58229885f95 | 584 | */ |
mleksio | 1:2be292bd43f9 | 585 | static void MlmeConfirm( MlmeConfirm_t *MlmeConfirm ) |
mleksio | 0:c58229885f95 | 586 | { |
mleksio | 1:2be292bd43f9 | 587 | if( MlmeConfirm->Status == LORAMAC_EVENT_INFO_STATUS_OK ) |
mleksio | 0:c58229885f95 | 588 | { |
mleksio | 1:2be292bd43f9 | 589 | switch( MlmeConfirm->MlmeRequest ) |
mleksio | 0:c58229885f95 | 590 | { |
mleksio | 1:2be292bd43f9 | 591 | case MLME_JOIN: |
mleksio | 0:c58229885f95 | 592 | { |
mleksio | 1:2be292bd43f9 | 593 | // Status is OK, node has joined the network |
mleksio | 1:2be292bd43f9 | 594 | break; |
mleksio | 0:c58229885f95 | 595 | } |
mleksio | 1:2be292bd43f9 | 596 | case MLME_LINK_CHECK: |
mleksio | 0:c58229885f95 | 597 | { |
mleksio | 1:2be292bd43f9 | 598 | // Check DemodMargin |
mleksio | 1:2be292bd43f9 | 599 | // Check NbGateways |
mleksio | 1:2be292bd43f9 | 600 | if( ComplianceTest.Running == true ) |
mleksio | 1:2be292bd43f9 | 601 | { |
mleksio | 1:2be292bd43f9 | 602 | ComplianceTest.LinkCheck = true; |
mleksio | 1:2be292bd43f9 | 603 | ComplianceTest.DemodMargin = MlmeConfirm->DemodMargin; |
mleksio | 1:2be292bd43f9 | 604 | ComplianceTest.NbGateways = MlmeConfirm->NbGateways; |
mleksio | 1:2be292bd43f9 | 605 | } |
mleksio | 1:2be292bd43f9 | 606 | break; |
mleksio | 0:c58229885f95 | 607 | } |
mleksio | 1:2be292bd43f9 | 608 | default: |
mleksio | 1:2be292bd43f9 | 609 | break; |
mleksio | 0:c58229885f95 | 610 | } |
mleksio | 0:c58229885f95 | 611 | } |
mleksio | 1:2be292bd43f9 | 612 | NextTx = true; |
mleksio | 0:c58229885f95 | 613 | } |
mleksio | 0:c58229885f95 | 614 | |
mleksio | 0:c58229885f95 | 615 | /** |
mleksio | 0:c58229885f95 | 616 | * Main application entry point. |
mleksio | 0:c58229885f95 | 617 | */ |
mleksio | 0:c58229885f95 | 618 | int main( void ) |
mleksio | 0:c58229885f95 | 619 | { |
mleksio | 1:2be292bd43f9 | 620 | LoRaMacPrimitives_t LoRaMacPrimitives; |
mleksio | 1:2be292bd43f9 | 621 | LoRaMacCallback_t LoRaMacCallbacks; |
mleksio | 1:2be292bd43f9 | 622 | MibRequestConfirm_t mibReq; |
mleksio | 0:c58229885f95 | 623 | |
mleksio | 0:c58229885f95 | 624 | //BoardInitMcu( ); |
mleksio | 0:c58229885f95 | 625 | //BoardInitPeriph( ); |
mleksio | 0:c58229885f95 | 626 | |
mleksio | 1:2be292bd43f9 | 627 | DeviceState = DEVICE_STATE_INIT; |
mleksio | 0:c58229885f95 | 628 | |
mleksio | 0:c58229885f95 | 629 | while( 1 ) |
mleksio | 0:c58229885f95 | 630 | { |
mleksio | 1:2be292bd43f9 | 631 | switch( DeviceState ) |
mleksio | 0:c58229885f95 | 632 | { |
mleksio | 1:2be292bd43f9 | 633 | case DEVICE_STATE_INIT: |
mleksio | 1:2be292bd43f9 | 634 | { |
mleksio | 1:2be292bd43f9 | 635 | LoRaMacPrimitives.MacMcpsConfirm = McpsConfirm; |
mleksio | 1:2be292bd43f9 | 636 | LoRaMacPrimitives.MacMcpsIndication = McpsIndication; |
mleksio | 1:2be292bd43f9 | 637 | LoRaMacPrimitives.MacMlmeConfirm = MlmeConfirm; |
mleksio | 1:2be292bd43f9 | 638 | //LoRaMacCallbacks.GetBatteryLevel = BoardGetBatteryLevel; |
mleksio | 1:2be292bd43f9 | 639 | LoRaMacInitialization( &LoRaMacPrimitives, &LoRaMacCallbacks ); |
mleksio | 1:2be292bd43f9 | 640 | |
mleksio | 1:2be292bd43f9 | 641 | mibReq.Type = MIB_ADR; |
mleksio | 1:2be292bd43f9 | 642 | mibReq.Param.AdrEnable = LORAWAN_ADR_ON; |
mleksio | 1:2be292bd43f9 | 643 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mleksio | 1:2be292bd43f9 | 644 | |
mleksio | 1:2be292bd43f9 | 645 | mibReq.Type = MIB_PUBLIC_NETWORK; |
mleksio | 1:2be292bd43f9 | 646 | mibReq.Param.EnablePublicNetwork = LORAWAN_PUBLIC_NETWORK; |
mleksio | 1:2be292bd43f9 | 647 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mleksio | 1:2be292bd43f9 | 648 | |
mleksio | 1:2be292bd43f9 | 649 | #if defined( USE_BAND_868 ) |
mleksio | 1:2be292bd43f9 | 650 | LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON ); |
mleksio | 1:2be292bd43f9 | 651 | |
mleksio | 1:2be292bd43f9 | 652 | #if( USE_SEMTECH_DEFAULT_CHANNEL_LINEUP == 1 ) |
mleksio | 1:2be292bd43f9 | 653 | LoRaMacChannelAdd( 3, ( ChannelParams_t )LC4 ); |
mleksio | 1:2be292bd43f9 | 654 | LoRaMacChannelAdd( 4, ( ChannelParams_t )LC5 ); |
mleksio | 1:2be292bd43f9 | 655 | LoRaMacChannelAdd( 5, ( ChannelParams_t )LC6 ); |
mleksio | 1:2be292bd43f9 | 656 | LoRaMacChannelAdd( 6, ( ChannelParams_t )LC7 ); |
mleksio | 1:2be292bd43f9 | 657 | LoRaMacChannelAdd( 7, ( ChannelParams_t )LC8 ); |
mleksio | 1:2be292bd43f9 | 658 | LoRaMacChannelAdd( 8, ( ChannelParams_t )LC9 ); |
mleksio | 1:2be292bd43f9 | 659 | #endif |
mleksio | 0:c58229885f95 | 660 | |
mleksio | 1:2be292bd43f9 | 661 | #endif |
mleksio | 1:2be292bd43f9 | 662 | DeviceState = DEVICE_STATE_JOIN; |
mleksio | 1:2be292bd43f9 | 663 | break; |
mleksio | 1:2be292bd43f9 | 664 | } |
mleksio | 1:2be292bd43f9 | 665 | case DEVICE_STATE_JOIN: |
mleksio | 1:2be292bd43f9 | 666 | { |
mleksio | 1:2be292bd43f9 | 667 | #if( OVER_THE_AIR_ACTIVATION != 0 ) |
mleksio | 1:2be292bd43f9 | 668 | MlmeReq_t mlmeReq; |
mleksio | 1:2be292bd43f9 | 669 | |
mleksio | 1:2be292bd43f9 | 670 | #warning BoardGetUniqueId not needed in test software - commented out |
mleksio | 1:2be292bd43f9 | 671 | // Initialize LoRaMac device unique ID |
mleksio | 1:2be292bd43f9 | 672 | // BoardGetUniqueId( DevEui ); |
mleksio | 1:2be292bd43f9 | 673 | |
mleksio | 1:2be292bd43f9 | 674 | mlmeReq.Type = MLME_JOIN; |
mleksio | 1:2be292bd43f9 | 675 | |
mleksio | 1:2be292bd43f9 | 676 | mlmeReq.Req.Join.DevEui = DevEui; |
mleksio | 1:2be292bd43f9 | 677 | mlmeReq.Req.Join.AppEui = AppEui; |
mleksio | 1:2be292bd43f9 | 678 | mlmeReq.Req.Join.AppKey = AppKey; |
mleksio | 1:2be292bd43f9 | 679 | |
mleksio | 1:2be292bd43f9 | 680 | if( NextTx == true ) |
mleksio | 1:2be292bd43f9 | 681 | { |
mleksio | 1:2be292bd43f9 | 682 | LoRaMacMlmeRequest( &mlmeReq ); |
mleksio | 1:2be292bd43f9 | 683 | } |
mleksio | 1:2be292bd43f9 | 684 | |
mleksio | 1:2be292bd43f9 | 685 | // Schedule next packet transmission |
mleksio | 1:2be292bd43f9 | 686 | TxDutyCycleTime = OVER_THE_AIR_ACTIVATION_DUTYCYCLE; |
mleksio | 1:2be292bd43f9 | 687 | DeviceState = DEVICE_STATE_CYCLE; |
mleksio | 1:2be292bd43f9 | 688 | |
mleksio | 1:2be292bd43f9 | 689 | #else |
mleksio | 1:2be292bd43f9 | 690 | // Random seed initialization |
mleksio | 1:2be292bd43f9 | 691 | srand1( BoardGetRandomSeed( ) ); |
mleksio | 1:2be292bd43f9 | 692 | |
mleksio | 1:2be292bd43f9 | 693 | // Choose a random device address |
mleksio | 1:2be292bd43f9 | 694 | DevAddr = randr( 0, 0x01FFFFFF ); |
mleksio | 0:c58229885f95 | 695 | |
mleksio | 1:2be292bd43f9 | 696 | mibReq.Type = MIB_NET_ID; |
mleksio | 1:2be292bd43f9 | 697 | mibReq.Param.NetID = LORAWAN_NETWORK_ID; |
mleksio | 1:2be292bd43f9 | 698 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mleksio | 1:2be292bd43f9 | 699 | |
mleksio | 1:2be292bd43f9 | 700 | mibReq.Type = MIB_DEV_ADDR; |
mleksio | 1:2be292bd43f9 | 701 | mibReq.Param.DevAddr = DevAddr; |
mleksio | 1:2be292bd43f9 | 702 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mleksio | 1:2be292bd43f9 | 703 | |
mleksio | 1:2be292bd43f9 | 704 | mibReq.Type = MIB_NWK_SKEY; |
mleksio | 1:2be292bd43f9 | 705 | mibReq.Param.NwkSKey = NwkSKey; |
mleksio | 1:2be292bd43f9 | 706 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mleksio | 1:2be292bd43f9 | 707 | |
mleksio | 1:2be292bd43f9 | 708 | mibReq.Type = MIB_APP_SKEY; |
mleksio | 1:2be292bd43f9 | 709 | mibReq.Param.AppSKey = AppSKey; |
mleksio | 1:2be292bd43f9 | 710 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mleksio | 1:2be292bd43f9 | 711 | |
mleksio | 1:2be292bd43f9 | 712 | mibReq.Type = MIB_NETWORK_JOINED; |
mleksio | 1:2be292bd43f9 | 713 | mibReq.Param.IsNetworkJoined = true; |
mleksio | 1:2be292bd43f9 | 714 | LoRaMacMibSetRequestConfirm( &mibReq ); |
mleksio | 1:2be292bd43f9 | 715 | |
mleksio | 1:2be292bd43f9 | 716 | DeviceState = DEVICE_STATE_SEND; |
mleksio | 1:2be292bd43f9 | 717 | #endif |
mleksio | 1:2be292bd43f9 | 718 | break; |
mleksio | 1:2be292bd43f9 | 719 | } |
mleksio | 1:2be292bd43f9 | 720 | case DEVICE_STATE_SEND: |
mleksio | 1:2be292bd43f9 | 721 | { |
pBiczysko | 4:0aa5d153c316 | 722 | static uint8_t flag = 0; |
mleksio | 1:2be292bd43f9 | 723 | if( NextTx == true ) |
mleksio | 1:2be292bd43f9 | 724 | { |
mleksio | 1:2be292bd43f9 | 725 | PrepareTxFrame( AppPort ); |
mleksio | 0:c58229885f95 | 726 | |
mleksio | 1:2be292bd43f9 | 727 | NextTx = SendFrame( ); |
mleksio | 1:2be292bd43f9 | 728 | } |
mleksio | 1:2be292bd43f9 | 729 | if( ComplianceTest.Running == true ) |
mleksio | 1:2be292bd43f9 | 730 | { |
mleksio | 1:2be292bd43f9 | 731 | // Schedule next packet transmission as soon as possible |
mleksio | 1:2be292bd43f9 | 732 | TxDutyCycleTime = 300000; // 300 ms |
mleksio | 1:2be292bd43f9 | 733 | } |
mleksio | 1:2be292bd43f9 | 734 | else |
mleksio | 1:2be292bd43f9 | 735 | { |
mleksio | 1:2be292bd43f9 | 736 | // Schedule next packet transmission |
mleksio | 1:2be292bd43f9 | 737 | TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND ); |
mleksio | 1:2be292bd43f9 | 738 | } |
mleksio | 1:2be292bd43f9 | 739 | DeviceState = DEVICE_STATE_CYCLE; |
mleksio | 1:2be292bd43f9 | 740 | break; |
mleksio | 1:2be292bd43f9 | 741 | } |
mleksio | 1:2be292bd43f9 | 742 | case DEVICE_STATE_CYCLE: |
mleksio | 1:2be292bd43f9 | 743 | { |
mleksio | 1:2be292bd43f9 | 744 | DeviceState = DEVICE_STATE_SLEEP; |
mleksio | 1:2be292bd43f9 | 745 | |
mleksio | 1:2be292bd43f9 | 746 | // Schedule next packet transmission |
mleksio | 1:2be292bd43f9 | 747 | TxNextPacketTimer.attach_us(OnTxNextPacketTimerEvent, TxDutyCycleTime); |
mleksio | 1:2be292bd43f9 | 748 | break; |
mleksio | 1:2be292bd43f9 | 749 | } |
mleksio | 1:2be292bd43f9 | 750 | case DEVICE_STATE_SLEEP: |
mleksio | 1:2be292bd43f9 | 751 | { |
mleksio | 1:2be292bd43f9 | 752 | // Wake up through events |
mleksio | 1:2be292bd43f9 | 753 | #warning TimerLowPowerHandler disabled |
mleksio | 1:2be292bd43f9 | 754 | //TimerLowPowerHandler( ); |
mleksio | 1:2be292bd43f9 | 755 | break; |
mleksio | 1:2be292bd43f9 | 756 | } |
mleksio | 1:2be292bd43f9 | 757 | default: |
mleksio | 1:2be292bd43f9 | 758 | { |
mleksio | 1:2be292bd43f9 | 759 | DeviceState = DEVICE_STATE_INIT; |
mleksio | 1:2be292bd43f9 | 760 | break; |
mleksio | 1:2be292bd43f9 | 761 | } |
mleksio | 0:c58229885f95 | 762 | } |
mleksio | 0:c58229885f95 | 763 | } |
mleksio | 1:2be292bd43f9 | 764 | } |