Semtech stack for ELMO - ver. 4.1.0.

Dependencies:   SX1272lib mbed

Fork of LoRaWAN_Semtech_stack_v4.1 by Michal Leksinski

Committer:
mleksio
Date:
Wed Dec 16 14:25:16 2015 +0000
Revision:
0:c58229885f95
Child:
1:2be292bd43f9
first commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mleksio 0:c58229885f95 1 /*
mleksio 0:c58229885f95 2
mleksio 0:c58229885f95 3 Description: MBED LoRaWAN example application
mleksio 0:c58229885f95 4
mleksio 0:c58229885f95 5 License: Revised BSD License, see LICENSE.TXT file include in the project
mleksio 0:c58229885f95 6
mleksio 0:c58229885f95 7 Maintainer: Espotel
mleksio 0:c58229885f95 8 */
mleksio 0:c58229885f95 9
mleksio 0:c58229885f95 10 #include "mbed.h"
mleksio 0:c58229885f95 11
mleksio 0:c58229885f95 12 #include <string.h>
mleksio 0:c58229885f95 13 #include <math.h>
mleksio 0:c58229885f95 14 //#include "board.h"
mleksio 0:c58229885f95 15
mleksio 0:c58229885f95 16 #include "LoRaMac.h"
mleksio 0:c58229885f95 17 #include "utilities.h"
mleksio 0:c58229885f95 18
mleksio 0:c58229885f95 19 #define USE_BAND_868
mleksio 0:c58229885f95 20 /*!
mleksio 0:c58229885f95 21 * When set to 1 the application uses the Over-the-Air activation procedure
mleksio 0:c58229885f95 22 * When set to 0 the application uses the Personalization activation procedure
mleksio 0:c58229885f95 23 */
mleksio 0:c58229885f95 24 #define OVER_THE_AIR_ACTIVATION 1
mleksio 0:c58229885f95 25
mleksio 0:c58229885f95 26 /*!
mleksio 0:c58229885f95 27 * Indicates if the end-device is to be connected to a private or public network
mleksio 0:c58229885f95 28 */
mleksio 0:c58229885f95 29 #define LORAWAN_PUBLIC_NETWORK true
mleksio 0:c58229885f95 30
mleksio 0:c58229885f95 31 #if( OVER_THE_AIR_ACTIVATION != 0 )
mleksio 0:c58229885f95 32
mleksio 0:c58229885f95 33 /*!
mleksio 0:c58229885f95 34 * Join requests trials duty cycle.
mleksio 0:c58229885f95 35 */
mleksio 0:c58229885f95 36 #define OVER_THE_AIR_ACTIVATION_DUTYCYCLE 10000000 // 10 [s] value in us
mleksio 0:c58229885f95 37
mleksio 0:c58229885f95 38 /*!
mleksio 0:c58229885f95 39 * Mote device IEEE EUI
mleksio 0:c58229885f95 40 *
mleksio 0:c58229885f95 41 * \remark must be written as a little endian value (reverse order of normal reading)
mleksio 0:c58229885f95 42 *
mleksio 0:c58229885f95 43 * \remark In this application the value is automatically generated by calling
mleksio 0:c58229885f95 44 * BoardGetUniqueId function
mleksio 0:c58229885f95 45 */
mleksio 0:c58229885f95 46 #define LORAWAN_DEVICE_EUI { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
mleksio 0:c58229885f95 47
mleksio 0:c58229885f95 48 /*!
mleksio 0:c58229885f95 49 * Application IEEE EUI
mleksio 0:c58229885f95 50 *
mleksio 0:c58229885f95 51 * \remark must be written as a little endian value (reverse order of normal reading)
mleksio 0:c58229885f95 52 */
mleksio 0:c58229885f95 53 //#define LORAWAN_APPLICATION_EUI { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 }
mleksio 0:c58229885f95 54 #define LORAWAN_APPLICATION_EUI { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
mleksio 0:c58229885f95 55
mleksio 0:c58229885f95 56 /*!
mleksio 0:c58229885f95 57 * AES encryption/decryption cipher application key
mleksio 0:c58229885f95 58 */
mleksio 0:c58229885f95 59
mleksio 0:c58229885f95 60 #define LORAWAN_APPLICATION_KEY { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00 }
mleksio 0:c58229885f95 61 //#define LORAWAN_APPLICATION_KEY { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }
mleksio 0:c58229885f95 62
mleksio 0:c58229885f95 63 #else
mleksio 0:c58229885f95 64
mleksio 0:c58229885f95 65 /*!
mleksio 0:c58229885f95 66 * Current network ID
mleksio 0:c58229885f95 67 */
mleksio 0:c58229885f95 68 #define LORAWAN_NETWORK_ID ( uint32_t )0
mleksio 0:c58229885f95 69
mleksio 0:c58229885f95 70 /*!
mleksio 0:c58229885f95 71 * Device address on the network
mleksio 0:c58229885f95 72 *
mleksio 0:c58229885f95 73 * \remark must be written as a big endian value (normal reading order)
mleksio 0:c58229885f95 74 *
mleksio 0:c58229885f95 75 * \remark In this application the value is automatically generated using
mleksio 0:c58229885f95 76 * a pseudo random generator seeded with a value derived from
mleksio 0:c58229885f95 77 * BoardUniqueId value
mleksio 0:c58229885f95 78 */
mleksio 0:c58229885f95 79 #define LORAWAN_DEVICE_ADDRESS ( uint32_t )0x00000000
mleksio 0:c58229885f95 80
mleksio 0:c58229885f95 81 /*!
mleksio 0:c58229885f95 82 * AES encryption/decryption cipher network session key
mleksio 0:c58229885f95 83 */
mleksio 0:c58229885f95 84 #define LORAWAN_NWKSKEY { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C }
mleksio 0:c58229885f95 85
mleksio 0:c58229885f95 86 /*!
mleksio 0:c58229885f95 87 * AES encryption/decryption cipher application session key
mleksio 0:c58229885f95 88 */
mleksio 0:c58229885f95 89 #define LORAWAN_APPSKEY { 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6, 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C }
mleksio 0:c58229885f95 90
mleksio 0:c58229885f95 91 #endif
mleksio 0:c58229885f95 92
mleksio 0:c58229885f95 93 /*!
mleksio 0:c58229885f95 94 * Defines the application data transmission duty cycle
mleksio 0:c58229885f95 95 */
mleksio 0:c58229885f95 96 #define APP_TX_DUTYCYCLE 5000000 // 5 [s] value in us
mleksio 0:c58229885f95 97 #define APP_TX_DUTYCYCLE_RND 1000000 // 1 [s] value in us
mleksio 0:c58229885f95 98
mleksio 0:c58229885f95 99 /*!
mleksio 0:c58229885f95 100 * LoRaWAN confirmed messages
mleksio 0:c58229885f95 101 */
mleksio 0:c58229885f95 102 #define LORAWAN_CONFIRMED_MSG_ON false
mleksio 0:c58229885f95 103
mleksio 0:c58229885f95 104 /*!
mleksio 0:c58229885f95 105 * LoRaWAN Adaptative Data Rate
mleksio 0:c58229885f95 106 *
mleksio 0:c58229885f95 107 * \remark Please note that when ADR is enabled the end-device should be static
mleksio 0:c58229885f95 108 */
mleksio 0:c58229885f95 109 #define LORAWAN_ADR_ON 1
mleksio 0:c58229885f95 110
mleksio 0:c58229885f95 111 /*!
mleksio 0:c58229885f95 112 * LoRaWAN ETSI duty cycle control enable/disable
mleksio 0:c58229885f95 113 *
mleksio 0:c58229885f95 114 * \remark Please note that ETSI mandates duty cycled transmissions. Use only for test purposes
mleksio 0:c58229885f95 115 */
mleksio 0:c58229885f95 116 #define LORAWAN_DUTYCYCLE_ON true
mleksio 0:c58229885f95 117 #define LORAWAN_DUTYCYCLE_OFF false
mleksio 0:c58229885f95 118
mleksio 0:c58229885f95 119 /*!
mleksio 0:c58229885f95 120 * LoRaWAN application port
mleksio 0:c58229885f95 121 */
mleksio 0:c58229885f95 122 #define LORAWAN_APP_PORT 2
mleksio 0:c58229885f95 123
mleksio 0:c58229885f95 124 /*!
mleksio 0:c58229885f95 125 * User application data buffer size
mleksio 0:c58229885f95 126 */
mleksio 0:c58229885f95 127 #define LORAWAN_APP_DATA_SIZE 16
mleksio 0:c58229885f95 128
mleksio 0:c58229885f95 129 #if( OVER_THE_AIR_ACTIVATION != 0 )
mleksio 0:c58229885f95 130
mleksio 0:c58229885f95 131 static uint8_t DevEui[] = LORAWAN_DEVICE_EUI;
mleksio 0:c58229885f95 132 static uint8_t AppEui[] = LORAWAN_APPLICATION_EUI;
mleksio 0:c58229885f95 133 static uint8_t AppKey[] = LORAWAN_APPLICATION_KEY;
mleksio 0:c58229885f95 134
mleksio 0:c58229885f95 135 #else
mleksio 0:c58229885f95 136
mleksio 0:c58229885f95 137 static uint8_t NwkSKey[] = LORAWAN_NWKSKEY;
mleksio 0:c58229885f95 138 static uint8_t AppSKey[] = LORAWAN_APPSKEY;
mleksio 0:c58229885f95 139
mleksio 0:c58229885f95 140 /*!
mleksio 0:c58229885f95 141 * Device address
mleksio 0:c58229885f95 142 */
mleksio 0:c58229885f95 143 static uint32_t DevAddr;
mleksio 0:c58229885f95 144
mleksio 0:c58229885f95 145 #endif
mleksio 0:c58229885f95 146
mleksio 0:c58229885f95 147 /*!
mleksio 0:c58229885f95 148 * Indicates if the MAC layer has already joined a network.
mleksio 0:c58229885f95 149 */
mleksio 0:c58229885f95 150 static bool IsNetworkJoined = false;
mleksio 0:c58229885f95 151
mleksio 0:c58229885f95 152 /*!
mleksio 0:c58229885f95 153 * Application port
mleksio 0:c58229885f95 154 */
mleksio 0:c58229885f95 155 static uint8_t AppPort = LORAWAN_APP_PORT;
mleksio 0:c58229885f95 156
mleksio 0:c58229885f95 157 /*!
mleksio 0:c58229885f95 158 * User application data size
mleksio 0:c58229885f95 159 */
mleksio 0:c58229885f95 160 static uint8_t AppDataSize = LORAWAN_APP_DATA_SIZE;
mleksio 0:c58229885f95 161
mleksio 0:c58229885f95 162 /*!
mleksio 0:c58229885f95 163 * User application data buffer size
mleksio 0:c58229885f95 164 */
mleksio 0:c58229885f95 165 #define LORAWAN_APP_DATA_MAX_SIZE 64
mleksio 0:c58229885f95 166
mleksio 0:c58229885f95 167 /*!
mleksio 0:c58229885f95 168 * User application data
mleksio 0:c58229885f95 169 */
mleksio 0:c58229885f95 170 static uint8_t AppData[LORAWAN_APP_DATA_MAX_SIZE];
mleksio 0:c58229885f95 171
mleksio 0:c58229885f95 172
mleksio 0:c58229885f95 173 /*!
mleksio 0:c58229885f95 174 * Indicates if the node is sending confirmed or unconfirmed messages
mleksio 0:c58229885f95 175 */
mleksio 0:c58229885f95 176 static uint8_t IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON;
mleksio 0:c58229885f95 177
mleksio 0:c58229885f95 178 /*!
mleksio 0:c58229885f95 179 * Defines the application data transmission duty cycle
mleksio 0:c58229885f95 180 */
mleksio 0:c58229885f95 181 static uint32_t TxDutyCycleTime;
mleksio 0:c58229885f95 182
mleksio 0:c58229885f95 183
mleksio 0:c58229885f95 184 static void OnTxNextPacketTimerEvent( void );
mleksio 0:c58229885f95 185 static Timeout TxNextPacketTimer;//(OnTxNextPacketTimerEvent, osTimerOnce);
mleksio 0:c58229885f95 186
mleksio 0:c58229885f95 187 #if( OVER_THE_AIR_ACTIVATION != 0 )
mleksio 0:c58229885f95 188
mleksio 0:c58229885f95 189 static void OnJoinReqTimerEvent( void );
mleksio 0:c58229885f95 190 /*!
mleksio 0:c58229885f95 191 * Defines the join request timer
mleksio 0:c58229885f95 192 */
mleksio 0:c58229885f95 193 static Timeout JoinReqTimer;//(OnJoinReqTimerEvent, osTimerOnce);
mleksio 0:c58229885f95 194
mleksio 0:c58229885f95 195 #endif
mleksio 0:c58229885f95 196
mleksio 0:c58229885f95 197 /*!
mleksio 0:c58229885f95 198 * Indicates if a new packet can be sent
mleksio 0:c58229885f95 199 */
mleksio 0:c58229885f95 200 static bool TxNextPacket = true;
mleksio 0:c58229885f95 201 static bool ScheduleNextTx = false;
mleksio 0:c58229885f95 202 static bool DownlinkStatusUpdate = false;
mleksio 0:c58229885f95 203
mleksio 0:c58229885f95 204 static bool AppLedStateOn = false;
mleksio 0:c58229885f95 205
mleksio 0:c58229885f95 206 static LoRaMacCallbacks_t LoRaMacCallbacks;
mleksio 0:c58229885f95 207 /*
mleksio 0:c58229885f95 208 static TimerEvent_t Led1Timer;
mleksio 0:c58229885f95 209 volatile bool Led1StateChanged = false;
mleksio 0:c58229885f95 210 static TimerEvent_t Led2Timer;
mleksio 0:c58229885f95 211 volatile bool Led2StateChanged = false;
mleksio 0:c58229885f95 212 static TimerEvent_t Led4Timer;
mleksio 0:c58229885f95 213 volatile bool Led4StateChanged = false;
mleksio 0:c58229885f95 214
mleksio 0:c58229885f95 215 volatile bool Led3StateChanged = false;
mleksio 0:c58229885f95 216 */
mleksio 0:c58229885f95 217 static bool ComplianceTestOn = false;
mleksio 0:c58229885f95 218 static uint8_t ComplianceTestState = 0;
mleksio 0:c58229885f95 219 static uint16_t ComplianceTestDownLinkCounter = 0;
mleksio 0:c58229885f95 220 static bool ComplianceTestLinkCheck = false;
mleksio 0:c58229885f95 221 static uint8_t ComplianceTestDemodMargin = 0;
mleksio 0:c58229885f95 222 static uint8_t ComplianceTestNbGateways = 0;
mleksio 0:c58229885f95 223
mleksio 0:c58229885f95 224 /*!
mleksio 0:c58229885f95 225 * Prepares the frame buffer to be sent
mleksio 0:c58229885f95 226 */
mleksio 0:c58229885f95 227 static void PrepareTxFrame( uint8_t port )
mleksio 0:c58229885f95 228 {
mleksio 0:c58229885f95 229 switch( port )
mleksio 0:c58229885f95 230 {
mleksio 0:c58229885f95 231 case 2:
mleksio 0:c58229885f95 232 {
mleksio 0:c58229885f95 233 uint16_t pressure = 0;
mleksio 0:c58229885f95 234 int16_t altitudeBar = 0;
mleksio 0:c58229885f95 235 int16_t temperature = 0;
mleksio 0:c58229885f95 236 int32_t latitude, longitude = 0;
mleksio 0:c58229885f95 237 uint16_t altitudeGps = 0xFFFF;
mleksio 0:c58229885f95 238 uint8_t batteryLevel = 0;
mleksio 0:c58229885f95 239
mleksio 0:c58229885f95 240 //pressure = ( uint16_t )( MPL3115ReadPressure( ) / 10 ); // in hPa / 10
mleksio 0:c58229885f95 241 //temperature = ( int16_t )( MPL3115ReadTemperature( ) * 100 ); // in °C * 100
mleksio 0:c58229885f95 242 //altitudeBar = ( int16_t )( MPL3115ReadAltitude( ) * 10 ); // in m * 10
mleksio 0:c58229885f95 243 //batteryLevel = BoardGetBatteryLevel( ); // 1 (very low) to 254 (fully charged)
mleksio 0:c58229885f95 244 //GpsGetLatestGpsPositionBinary( &latitude, &longitude );
mleksio 0:c58229885f95 245 //altitudeGps = GpsGetLatestGpsAltitude( ); // in m
mleksio 0:c58229885f95 246
mleksio 0:c58229885f95 247 AppData[0] = AppLedStateOn;
mleksio 0:c58229885f95 248 AppData[1] = ( pressure >> 8 ) & 0xFF;
mleksio 0:c58229885f95 249 AppData[2] = pressure & 0xFF;
mleksio 0:c58229885f95 250 AppData[3] = ( temperature >> 8 ) & 0xFF;
mleksio 0:c58229885f95 251 AppData[4] = temperature & 0xFF;
mleksio 0:c58229885f95 252 AppData[5] = ( altitudeBar >> 8 ) & 0xFF;
mleksio 0:c58229885f95 253 AppData[6] = altitudeBar & 0xFF;
mleksio 0:c58229885f95 254 AppData[7] = batteryLevel;
mleksio 0:c58229885f95 255 AppData[8] = ( latitude >> 16 ) & 0xFF;
mleksio 0:c58229885f95 256 AppData[9] = ( latitude >> 8 ) & 0xFF;
mleksio 0:c58229885f95 257 AppData[10] = latitude & 0xFF;
mleksio 0:c58229885f95 258 AppData[11] = ( longitude >> 16 ) & 0xFF;
mleksio 0:c58229885f95 259 AppData[12] = ( longitude >> 8 ) & 0xFF;
mleksio 0:c58229885f95 260 AppData[13] = longitude & 0xFF;
mleksio 0:c58229885f95 261 AppData[14] = ( altitudeGps >> 8 ) & 0xFF;
mleksio 0:c58229885f95 262 AppData[15] = altitudeGps & 0xFF;
mleksio 0:c58229885f95 263 }
mleksio 0:c58229885f95 264 break;
mleksio 0:c58229885f95 265 case 224:
mleksio 0:c58229885f95 266 if( ComplianceTestLinkCheck == true )
mleksio 0:c58229885f95 267 {
mleksio 0:c58229885f95 268 printf("ComplianceTestLinkCheck\r\n");
mleksio 0:c58229885f95 269 ComplianceTestLinkCheck = false;
mleksio 0:c58229885f95 270 AppDataSize = 3;
mleksio 0:c58229885f95 271 AppData[0] = 5;
mleksio 0:c58229885f95 272 AppData[1] = ComplianceTestDemodMargin;
mleksio 0:c58229885f95 273 AppData[2] = ComplianceTestNbGateways;
mleksio 0:c58229885f95 274 ComplianceTestState = 1;
mleksio 0:c58229885f95 275 }
mleksio 0:c58229885f95 276 else
mleksio 0:c58229885f95 277 {
mleksio 0:c58229885f95 278 if( ComplianceTestState == 1 )
mleksio 0:c58229885f95 279 {
mleksio 0:c58229885f95 280 printf("Sending downlink COUNTER!!!!\r\n");
mleksio 0:c58229885f95 281 AppDataSize = 2;
mleksio 0:c58229885f95 282 AppData[0] = ComplianceTestDownLinkCounter >> 8;
mleksio 0:c58229885f95 283 AppData[1] = ComplianceTestDownLinkCounter;
mleksio 0:c58229885f95 284 }
mleksio 0:c58229885f95 285 }
mleksio 0:c58229885f95 286 default:
mleksio 0:c58229885f95 287 break;
mleksio 0:c58229885f95 288 }
mleksio 0:c58229885f95 289 }
mleksio 0:c58229885f95 290
mleksio 0:c58229885f95 291 static void ProcessRxFrame( LoRaMacEventFlags_t *flags, LoRaMacEventInfo_t *info )
mleksio 0:c58229885f95 292 {
mleksio 0:c58229885f95 293 switch( info->RxPort ) // Check Rx port number
mleksio 0:c58229885f95 294 {
mleksio 0:c58229885f95 295 case 1: // The application LED can be controlled on port 1 or 2
mleksio 0:c58229885f95 296 case 2:
mleksio 0:c58229885f95 297 if( info->RxBufferSize == 1 )
mleksio 0:c58229885f95 298 {
mleksio 0:c58229885f95 299 AppLedStateOn = info->RxBuffer[0] & 0x01;
mleksio 0:c58229885f95 300 //Led3StateChanged = true;
mleksio 0:c58229885f95 301 }
mleksio 0:c58229885f95 302 break;
mleksio 0:c58229885f95 303 case 224:
mleksio 0:c58229885f95 304 if( ComplianceTestOn == false )
mleksio 0:c58229885f95 305 {
mleksio 0:c58229885f95 306 // Check compliance test enable command (i)
mleksio 0:c58229885f95 307 if( ( info->RxBufferSize == 4 ) &&
mleksio 0:c58229885f95 308 ( info->RxBuffer[0] == 0x01 ) &&
mleksio 0:c58229885f95 309 ( info->RxBuffer[1] == 0x01 ) &&
mleksio 0:c58229885f95 310 ( info->RxBuffer[2] == 0x01 ) &&
mleksio 0:c58229885f95 311 ( info->RxBuffer[3] == 0x01 ) )
mleksio 0:c58229885f95 312 {
mleksio 0:c58229885f95 313 IsTxConfirmed = false;
mleksio 0:c58229885f95 314 AppPort = 224;
mleksio 0:c58229885f95 315 AppDataSize = 2;
mleksio 0:c58229885f95 316 ComplianceTestDownLinkCounter = 0;
mleksio 0:c58229885f95 317 ComplianceTestLinkCheck = false;
mleksio 0:c58229885f95 318 ComplianceTestDemodMargin = 0;
mleksio 0:c58229885f95 319 ComplianceTestNbGateways = 0;
mleksio 0:c58229885f95 320 ComplianceTestOn = true;
mleksio 0:c58229885f95 321 ComplianceTestState = 1;
mleksio 0:c58229885f95 322
mleksio 0:c58229885f95 323 LoRaMacSetAdrOn( true );
mleksio 0:c58229885f95 324 LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_OFF );
mleksio 0:c58229885f95 325 }
mleksio 0:c58229885f95 326 }
mleksio 0:c58229885f95 327 else
mleksio 0:c58229885f95 328 {
mleksio 0:c58229885f95 329 ComplianceTestState = info->RxBuffer[0];
mleksio 0:c58229885f95 330 switch( ComplianceTestState )
mleksio 0:c58229885f95 331 {
mleksio 0:c58229885f95 332 case 0: // Check compliance test disable command (ii)
mleksio 0:c58229885f95 333 IsTxConfirmed = LORAWAN_CONFIRMED_MSG_ON;
mleksio 0:c58229885f95 334 AppPort = LORAWAN_APP_PORT;
mleksio 0:c58229885f95 335 AppDataSize = LORAWAN_APP_DATA_SIZE;
mleksio 0:c58229885f95 336 ComplianceTestDownLinkCounter = 0;
mleksio 0:c58229885f95 337 ComplianceTestOn = false;
mleksio 0:c58229885f95 338
mleksio 0:c58229885f95 339 LoRaMacSetAdrOn( LORAWAN_ADR_ON );
mleksio 0:c58229885f95 340 LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON );
mleksio 0:c58229885f95 341 break;
mleksio 0:c58229885f95 342 case 1: // (iii, iv)
mleksio 0:c58229885f95 343 AppDataSize = 2;
mleksio 0:c58229885f95 344 break;
mleksio 0:c58229885f95 345 case 2: // Enable confirmed messages (v)
mleksio 0:c58229885f95 346 IsTxConfirmed = true;
mleksio 0:c58229885f95 347 ComplianceTestState = 1;
mleksio 0:c58229885f95 348 break;
mleksio 0:c58229885f95 349 case 3: // Disable confirmed messages (vi)
mleksio 0:c58229885f95 350 IsTxConfirmed = false;
mleksio 0:c58229885f95 351 ComplianceTestState = 1;
mleksio 0:c58229885f95 352 break;
mleksio 0:c58229885f95 353 case 4: // (vii)
mleksio 0:c58229885f95 354 AppDataSize = info->RxBufferSize;
mleksio 0:c58229885f95 355 printf("VII <------------------- rozmiar %i \r\n", AppDataSize);
mleksio 0:c58229885f95 356 AppData[0] = 4;
mleksio 0:c58229885f95 357 for( uint8_t i = 1; i < AppDataSize; i++ )
mleksio 0:c58229885f95 358 {
mleksio 0:c58229885f95 359
mleksio 0:c58229885f95 360 printf("VII <-------------------dodaje \r\n");
mleksio 0:c58229885f95 361 AppData[i] = info->RxBuffer[i] + 1;
mleksio 0:c58229885f95 362 }
mleksio 0:c58229885f95 363 break;
mleksio 0:c58229885f95 364 case 5: // (viii)
mleksio 0:c58229885f95 365 LoRaMacLinkCheckReq( );
mleksio 0:c58229885f95 366 break;
mleksio 0:c58229885f95 367 default:
mleksio 0:c58229885f95 368 break;
mleksio 0:c58229885f95 369 }
mleksio 0:c58229885f95 370 }
mleksio 0:c58229885f95 371 break;
mleksio 0:c58229885f95 372 default:
mleksio 0:c58229885f95 373 break;
mleksio 0:c58229885f95 374 }
mleksio 0:c58229885f95 375
mleksio 0:c58229885f95 376 if( ( ComplianceTestOn == true ) && ( flags->Bits.LinkCheck == 1 ) )
mleksio 0:c58229885f95 377 {
mleksio 0:c58229885f95 378 ComplianceTestLinkCheck = true;
mleksio 0:c58229885f95 379 ComplianceTestDemodMargin = info->DemodMargin;
mleksio 0:c58229885f95 380 ComplianceTestNbGateways = info->NbGateways;
mleksio 0:c58229885f95 381 }
mleksio 0:c58229885f95 382
mleksio 0:c58229885f95 383 #warning needed extension to check counter scopes
mleksio 0:c58229885f95 384 }
mleksio 0:c58229885f95 385
mleksio 0:c58229885f95 386 static bool SendFrame( void )
mleksio 0:c58229885f95 387 {
mleksio 0:c58229885f95 388 uint8_t sendFrameStatus = 0;
mleksio 0:c58229885f95 389
mleksio 0:c58229885f95 390 if( IsTxConfirmed == false )
mleksio 0:c58229885f95 391 {
mleksio 0:c58229885f95 392 sendFrameStatus = LoRaMacSendFrame( AppPort, AppData, AppDataSize );
mleksio 0:c58229885f95 393 }
mleksio 0:c58229885f95 394 else
mleksio 0:c58229885f95 395 {
mleksio 0:c58229885f95 396 sendFrameStatus = LoRaMacSendConfirmedFrame( AppPort, AppData, AppDataSize, 8 );
mleksio 0:c58229885f95 397 }
mleksio 0:c58229885f95 398
mleksio 0:c58229885f95 399 switch( sendFrameStatus )
mleksio 0:c58229885f95 400 {
mleksio 0:c58229885f95 401 case 5: // NO_FREE_CHANNEL
mleksio 0:c58229885f95 402 // Try again later
mleksio 0:c58229885f95 403 return true;
mleksio 0:c58229885f95 404 default:
mleksio 0:c58229885f95 405 return false;
mleksio 0:c58229885f95 406 }
mleksio 0:c58229885f95 407 }
mleksio 0:c58229885f95 408
mleksio 0:c58229885f95 409 #if( OVER_THE_AIR_ACTIVATION != 0 )
mleksio 0:c58229885f95 410
mleksio 0:c58229885f95 411 /*!
mleksio 0:c58229885f95 412 * \brief Function executed on JoinReq Timeout event
mleksio 0:c58229885f95 413 */
mleksio 0:c58229885f95 414 static void OnJoinReqTimerEvent( void )
mleksio 0:c58229885f95 415 {
mleksio 0:c58229885f95 416 JoinReqTimer.detach();
mleksio 0:c58229885f95 417 TxNextPacket = true;
mleksio 0:c58229885f95 418 }
mleksio 0:c58229885f95 419
mleksio 0:c58229885f95 420 #endif
mleksio 0:c58229885f95 421
mleksio 0:c58229885f95 422 /*!
mleksio 0:c58229885f95 423 * \brief Function executed on TxNextPacket Timeout event
mleksio 0:c58229885f95 424 */
mleksio 0:c58229885f95 425 static void OnTxNextPacketTimerEvent( void )
mleksio 0:c58229885f95 426 {
mleksio 0:c58229885f95 427 //TimerStop( &TxNextPacketTimer );
mleksio 0:c58229885f95 428 //TxNextPacketTimer.stop();
mleksio 0:c58229885f95 429 TxNextPacketTimer.detach();
mleksio 0:c58229885f95 430 TxNextPacket = true;
mleksio 0:c58229885f95 431 }
mleksio 0:c58229885f95 432
mleksio 0:c58229885f95 433 /*!
mleksio 0:c58229885f95 434 * \brief Function executed on Led 1 Timeout event
mleksio 0:c58229885f95 435 */
mleksio 0:c58229885f95 436 /*static void OnLed1TimerEvent( void )
mleksio 0:c58229885f95 437 {
mleksio 0:c58229885f95 438 TimerStop( &Led1Timer );
mleksio 0:c58229885f95 439 Led1StateChanged = true;
mleksio 0:c58229885f95 440 }*/
mleksio 0:c58229885f95 441
mleksio 0:c58229885f95 442 /*!
mleksio 0:c58229885f95 443 * \brief Function executed on Led 2 Timeout event
mleksio 0:c58229885f95 444 */
mleksio 0:c58229885f95 445 /*static void OnLed2TimerEvent( void )
mleksio 0:c58229885f95 446 {
mleksio 0:c58229885f95 447 TimerStop( &Led2Timer );
mleksio 0:c58229885f95 448 Led2StateChanged = true;
mleksio 0:c58229885f95 449 }*/
mleksio 0:c58229885f95 450
mleksio 0:c58229885f95 451 /*!
mleksio 0:c58229885f95 452 * \brief Function executed on Led 4 Timeout event
mleksio 0:c58229885f95 453 */
mleksio 0:c58229885f95 454 /*void OnLed4TimerEvent( void )
mleksio 0:c58229885f95 455 {
mleksio 0:c58229885f95 456 TimerStop( &Led4Timer );
mleksio 0:c58229885f95 457 Led4StateChanged = true;
mleksio 0:c58229885f95 458 }*/
mleksio 0:c58229885f95 459
mleksio 0:c58229885f95 460 /*!
mleksio 0:c58229885f95 461 * \brief Function to be executed on MAC layer event
mleksio 0:c58229885f95 462 */
mleksio 0:c58229885f95 463 static void OnMacEvent( LoRaMacEventFlags_t *flags, LoRaMacEventInfo_t *info )
mleksio 0:c58229885f95 464 {
mleksio 0:c58229885f95 465 if( flags->Bits.JoinAccept == 1 )
mleksio 0:c58229885f95 466 {
mleksio 0:c58229885f95 467 #if( OVER_THE_AIR_ACTIVATION != 0 )
mleksio 0:c58229885f95 468 JoinReqTimer.detach();
mleksio 0:c58229885f95 469 #endif
mleksio 0:c58229885f95 470 IsNetworkJoined = true;
mleksio 0:c58229885f95 471 }
mleksio 0:c58229885f95 472 else
mleksio 0:c58229885f95 473 {
mleksio 0:c58229885f95 474 if( flags->Bits.Tx == 1 )
mleksio 0:c58229885f95 475 {
mleksio 0:c58229885f95 476 }
mleksio 0:c58229885f95 477
mleksio 0:c58229885f95 478 if( flags->Bits.Rx == 1 )
mleksio 0:c58229885f95 479 {
mleksio 0:c58229885f95 480 if( ComplianceTestOn == true )
mleksio 0:c58229885f95 481 {
mleksio 0:c58229885f95 482 ComplianceTestDownLinkCounter++;
mleksio 0:c58229885f95 483 }
mleksio 0:c58229885f95 484 if( flags->Bits.RxData == true )
mleksio 0:c58229885f95 485 {
mleksio 0:c58229885f95 486 ProcessRxFrame( flags, info );
mleksio 0:c58229885f95 487 }
mleksio 0:c58229885f95 488
mleksio 0:c58229885f95 489 DownlinkStatusUpdate = true;
mleksio 0:c58229885f95 490 //TimerStart( &Led2Timer );
mleksio 0:c58229885f95 491 }
mleksio 0:c58229885f95 492 }
mleksio 0:c58229885f95 493 // Schedule a new transmission
mleksio 0:c58229885f95 494 ScheduleNextTx = true;
mleksio 0:c58229885f95 495 }
mleksio 0:c58229885f95 496
mleksio 0:c58229885f95 497 /**
mleksio 0:c58229885f95 498 * Main application entry point.
mleksio 0:c58229885f95 499 */
mleksio 0:c58229885f95 500
mleksio 0:c58229885f95 501 int main( void )
mleksio 0:c58229885f95 502 {
mleksio 0:c58229885f95 503 #if( OVER_THE_AIR_ACTIVATION != 0 )
mleksio 0:c58229885f95 504 uint8_t sendFrameStatus = 0;
mleksio 0:c58229885f95 505 #endif
mleksio 0:c58229885f95 506 bool trySendingFrameAgain = false;
mleksio 0:c58229885f95 507
mleksio 0:c58229885f95 508 #warning HW and RADIO CHIP must be configured!!!!
mleksio 0:c58229885f95 509 //BoardInitMcu( );
mleksio 0:c58229885f95 510 //BoardInitPeriph( );
mleksio 0:c58229885f95 511 //hal_init();
mleksio 0:c58229885f95 512 //radio_init();
mleksio 0:c58229885f95 513
mleksio 0:c58229885f95 514 LoRaMacCallbacks.MacEvent = OnMacEvent;
mleksio 0:c58229885f95 515 //LoRaMacCallbacks.GetBatteryLevel = BoardGetBatteryLevel;
mleksio 0:c58229885f95 516 LoRaMacInit( &LoRaMacCallbacks );
mleksio 0:c58229885f95 517
mleksio 0:c58229885f95 518 IsNetworkJoined = false;
mleksio 0:c58229885f95 519
mleksio 0:c58229885f95 520 #if( OVER_THE_AIR_ACTIVATION == 0 )
mleksio 0:c58229885f95 521 // Random seed initialization
mleksio 0:c58229885f95 522 #warning seed initialization must be implemented
mleksio 0:c58229885f95 523 srand1( 1 );
mleksio 0:c58229885f95 524 // Choose a random device address based on Board unique ID
mleksio 0:c58229885f95 525 // NwkAddr rand [0, 33554431]
mleksio 0:c58229885f95 526 DevAddr = randr( 0, 0x01FFFFFF );
mleksio 0:c58229885f95 527
mleksio 0:c58229885f95 528 LoRaMacInitNwkIds( LORAWAN_NETWORK_ID, DevAddr, NwkSKey, AppSKey );
mleksio 0:c58229885f95 529 IsNetworkJoined = true;
mleksio 0:c58229885f95 530 #else
mleksio 0:c58229885f95 531 // Initialize LoRaMac device unique ID
mleksio 0:c58229885f95 532 //BoardGetUniqueId( DevEui );
mleksio 0:c58229885f95 533 #endif
mleksio 0:c58229885f95 534
mleksio 0:c58229885f95 535 TxNextPacket = true;
mleksio 0:c58229885f95 536
mleksio 0:c58229885f95 537 //TimerInit( &Led1Timer, OnLed1TimerEvent );
mleksio 0:c58229885f95 538 //TimerSetValue( &Led1Timer, 25000 );
mleksio 0:c58229885f95 539
mleksio 0:c58229885f95 540 //TimerInit( &Led2Timer, OnLed2TimerEvent );
mleksio 0:c58229885f95 541 //TimerSetValue( &Led2Timer, 25000 );
mleksio 0:c58229885f95 542
mleksio 0:c58229885f95 543 //TimerInit( &Led4Timer, OnLed4TimerEvent );
mleksio 0:c58229885f95 544 //TimerSetValue( &Led4Timer, 25000 );
mleksio 0:c58229885f95 545
mleksio 0:c58229885f95 546 LoRaMacSetAdrOn( LORAWAN_ADR_ON );
mleksio 0:c58229885f95 547 #warning dutycycleON OFFFFFFFFFFFFF!!!!!!!!!!!!!!!!!!!!!!!
mleksio 0:c58229885f95 548 // LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON );
mleksio 0:c58229885f95 549 LoRaMacTestSetDutyCycleOn( 0 );
mleksio 0:c58229885f95 550 LoRaMacSetPublicNetwork( LORAWAN_PUBLIC_NETWORK );
mleksio 0:c58229885f95 551
mleksio 0:c58229885f95 552 while( 1 )
mleksio 0:c58229885f95 553 {
mleksio 0:c58229885f95 554 while( IsNetworkJoined == false )
mleksio 0:c58229885f95 555 {
mleksio 0:c58229885f95 556 #if( OVER_THE_AIR_ACTIVATION != 0 )
mleksio 0:c58229885f95 557 if( TxNextPacket == true )
mleksio 0:c58229885f95 558 {
mleksio 0:c58229885f95 559 printf("Trial to join network... txnextpacket %i \r\n", TxNextPacket);
mleksio 0:c58229885f95 560 TxNextPacket = false;
mleksio 0:c58229885f95 561
mleksio 0:c58229885f95 562 sendFrameStatus = LoRaMacJoinReq( DevEui, AppEui, AppKey );
mleksio 0:c58229885f95 563 printf("status = %i\r\n", sendFrameStatus);
mleksio 0:c58229885f95 564 switch( sendFrameStatus )
mleksio 0:c58229885f95 565 {
mleksio 0:c58229885f95 566 case 1: // BUSY
mleksio 0:c58229885f95 567 printf("busy...\r\n");
mleksio 0:c58229885f95 568 break;
mleksio 0:c58229885f95 569 case 0: // OK
mleksio 0:c58229885f95 570 case 2: // NO_NETWORK_JOINED
mleksio 0:c58229885f95 571 case 3: // LENGTH_PORT_ERROR
mleksio 0:c58229885f95 572 case 4: // MAC_CMD_ERROR
mleksio 0:c58229885f95 573 case 6: // DEVICE_OFF
mleksio 0:c58229885f95 574 default:
mleksio 0:c58229885f95 575 // Relaunch timer for next trial
mleksio 0:c58229885f95 576 JoinReqTimer.attach_us(OnJoinReqTimerEvent, OVER_THE_AIR_ACTIVATION_DUTYCYCLE);
mleksio 0:c58229885f95 577 break;
mleksio 0:c58229885f95 578 }
mleksio 0:c58229885f95 579 }
mleksio 0:c58229885f95 580 //TimerLowPowerHandler( );
mleksio 0:c58229885f95 581 #endif
mleksio 0:c58229885f95 582 }
mleksio 0:c58229885f95 583
mleksio 0:c58229885f95 584 /*
mleksio 0:c58229885f95 585 if( Led1StateChanged == true )
mleksio 0:c58229885f95 586 {
mleksio 0:c58229885f95 587 Led1StateChanged = false;
mleksio 0:c58229885f95 588 // Switch LED 1 OFF
mleksio 0:c58229885f95 589 GpioWrite( &Led1, 1 );
mleksio 0:c58229885f95 590 }
mleksio 0:c58229885f95 591 if( Led2StateChanged == true )
mleksio 0:c58229885f95 592 {
mleksio 0:c58229885f95 593 Led2StateChanged = false;
mleksio 0:c58229885f95 594 // Switch LED 2 OFF
mleksio 0:c58229885f95 595 GpioWrite( &Led2, 1 );
mleksio 0:c58229885f95 596 }
mleksio 0:c58229885f95 597 if( Led3StateChanged == true )
mleksio 0:c58229885f95 598 {
mleksio 0:c58229885f95 599 Led3StateChanged = false;
mleksio 0:c58229885f95 600 GpioWrite( &Led3, ( ( AppLedStateOn & 0x01 ) != 0 ) ? 0 : 1 );
mleksio 0:c58229885f95 601 }
mleksio 0:c58229885f95 602 if( Led4StateChanged == true )
mleksio 0:c58229885f95 603 {
mleksio 0:c58229885f95 604 Led4StateChanged = false;
mleksio 0:c58229885f95 605 // Switch LED 4 OFF
mleksio 0:c58229885f95 606 GpioWrite( &Led4, 1 );
mleksio 0:c58229885f95 607 }*/
mleksio 0:c58229885f95 608 if( DownlinkStatusUpdate == true )
mleksio 0:c58229885f95 609 {
mleksio 0:c58229885f95 610 DownlinkStatusUpdate = false;
mleksio 0:c58229885f95 611 // Switch LED 2 ON for each received downlink
mleksio 0:c58229885f95 612 //GpioWrite( &Led2, 0 );
mleksio 0:c58229885f95 613 }
mleksio 0:c58229885f95 614
mleksio 0:c58229885f95 615 if( ScheduleNextTx == true )
mleksio 0:c58229885f95 616 {
mleksio 0:c58229885f95 617 printf("Sending data...");
mleksio 0:c58229885f95 618 ScheduleNextTx = false;
mleksio 0:c58229885f95 619
mleksio 0:c58229885f95 620 // Schedule next packet transmission
mleksio 0:c58229885f95 621 TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND );
mleksio 0:c58229885f95 622 TxNextPacketTimer.attach_us(OnTxNextPacketTimerEvent, TxDutyCycleTime);
mleksio 0:c58229885f95 623 }
mleksio 0:c58229885f95 624
mleksio 0:c58229885f95 625 if( trySendingFrameAgain == true )
mleksio 0:c58229885f95 626 {
mleksio 0:c58229885f95 627 trySendingFrameAgain = SendFrame( );
mleksio 0:c58229885f95 628 }
mleksio 0:c58229885f95 629 if( TxNextPacket == true )
mleksio 0:c58229885f95 630 {
mleksio 0:c58229885f95 631 TxNextPacket = false;
mleksio 0:c58229885f95 632
mleksio 0:c58229885f95 633 PrepareTxFrame( AppPort );
mleksio 0:c58229885f95 634
mleksio 0:c58229885f95 635 // Switch LED 1 ON
mleksio 0:c58229885f95 636 //GpioWrite( &Led1, 0 );
mleksio 0:c58229885f95 637 //TimerStart( &Led1Timer );
mleksio 0:c58229885f95 638
mleksio 0:c58229885f95 639 trySendingFrameAgain = SendFrame( );
mleksio 0:c58229885f95 640 }
mleksio 0:c58229885f95 641
mleksio 0:c58229885f95 642 //TimerLowPowerHandler( );
mleksio 0:c58229885f95 643 }
mleksio 0:c58229885f95 644 }