Lora Personalized device for Everynet

Dependencies:   LMiCPersonalizedforEverynet SX1276Lib X_NUCLEO_IKS01A1 cantcoap lwip mbed-rtos mbed

Fork of LoRaWAN-test-10secs by Alcatel-Lucent IoT Development

Committer:
pnysten
Date:
Mon Dec 14 10:47:06 2015 +0000
Revision:
6:540c5d907c90
Parent:
4:5e274bf85bf0
Child:
7:f1521b0ecf08
LORA ALU motive version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sam_grove 3:ce28e3313a88 1 /*
mluis 0:a2929fa6e4f0 2 / _____) _ | |
mluis 0:a2929fa6e4f0 3 ( (____ _____ ____ _| |_ _____ ____| |__
mluis 0:a2929fa6e4f0 4 \____ \| ___ | (_ _) ___ |/ ___) _ \
mluis 0:a2929fa6e4f0 5 _____) ) ____| | | || |_| ____( (___| | | |
mluis 0:a2929fa6e4f0 6 (______/|_____)_|_|_| \__)_____)\____)_| |_|
mluis 1:60184eda0066 7 (C)2015 Semtech
mluis 0:a2929fa6e4f0 8
mluis 1:60184eda0066 9 Description: MBED LoRaWAN example application
mluis 0:a2929fa6e4f0 10
mluis 0:a2929fa6e4f0 11 License: Revised BSD License, see LICENSE.TXT file include in the project
mluis 0:a2929fa6e4f0 12
mluis 0:a2929fa6e4f0 13 Maintainer: Miguel Luis and Gregory Cristian
mluis 0:a2929fa6e4f0 14 */
pnysten 6:540c5d907c90 15
pnysten 4:5e274bf85bf0 16 #include <cstdio>
pnysten 4:5e274bf85bf0 17 #include <string>
pnysten 4:5e274bf85bf0 18 #include <cassert>
pnysten 4:5e274bf85bf0 19
mluis 1:60184eda0066 20 #include "mbed.h"
pnysten 6:540c5d907c90 21 //#include "Node.h"
pnysten 4:5e274bf85bf0 22 #include "cantcoap.h"
mluis 1:60184eda0066 23
mluis 0:a2929fa6e4f0 24 #include "lmic.h"
mluis 1:60184eda0066 25 #include "debug.h"
mluis 0:a2929fa6e4f0 26
pnysten 4:5e274bf85bf0 27 const std::string REGISTRATION_SEGMENT ="/rd";
pnysten 4:5e274bf85bf0 28 const std::string ENDPOINT_SEGMENT = "?ep=";
pnysten 4:5e274bf85bf0 29 const std::string LIFETIME ="&lt=";
pnysten 4:5e274bf85bf0 30 const std::string BINDING ="&b=";
pnysten 6:540c5d907c90 31
pnysten 4:5e274bf85bf0 32 const std::string REGISTRATION_OPEN = "<";
pnysten 4:5e274bf85bf0 33 const std::string REGISTRATION_CLOSE = ">";
pnysten 4:5e274bf85bf0 34 const std::string REGISTRATION_SEPARATOR ="/";
pnysten 6:540c5d907c90 35
pnysten 4:5e274bf85bf0 36 int _node_Id=0;
pnysten 6:540c5d907c90 37
pnysten 4:5e274bf85bf0 38 const std::string endPoint_Name = "loraDevice";
pnysten 4:5e274bf85bf0 39 const int lifeTime = 300;
pnysten 4:5e274bf85bf0 40 const std::string binding = "U";
pnysten 6:540c5d907c90 41
pnysten 4:5e274bf85bf0 42 unsigned char * _payload;
pnysten 4:5e274bf85bf0 43 long _payload_size;
pnysten 6:540c5d907c90 44
mluis 0:a2929fa6e4f0 45 /*!
mluis 0:a2929fa6e4f0 46 * When set to 1 the application uses the Over-the-Air activation procedure
mluis 0:a2929fa6e4f0 47 * When set to 0 the application uses the Personalization activation procedure
mluis 0:a2929fa6e4f0 48 */
mluis 0:a2929fa6e4f0 49 #define OVER_THE_AIR_ACTIVATION 0
mluis 0:a2929fa6e4f0 50
mluis 1:60184eda0066 51 #if( OVER_THE_AIR_ACTIVATION == 0 )
mluis 1:60184eda0066 52
mluis 1:60184eda0066 53 /*!
mluis 1:60184eda0066 54 * Defines the network ID when using personalization activation procedure
mluis 1:60184eda0066 55 */
mluis 1:60184eda0066 56 #define LORAWAN_NET_ID ( uint32_t )0x00000000
mluis 1:60184eda0066 57
mluis 1:60184eda0066 58 /*!
mluis 1:60184eda0066 59 * Defines the device address when using personalization activation procedure
mluis 1:60184eda0066 60 */
mluis 1:60184eda0066 61 #define LORAWAN_DEV_ADDR ( uint32_t )0x12345678
pnysten 6:540c5d907c90 62 //#define LORAWAN_DEV_ADDR ( uint32_t )0x12341111
mluis 1:60184eda0066 63
mluis 1:60184eda0066 64 #endif
mluis 1:60184eda0066 65
mluis 1:60184eda0066 66 /*!
mluis 1:60184eda0066 67 * Defines the application data transmission duty cycle
mluis 1:60184eda0066 68 */
mluis 1:60184eda0066 69 #define APP_TX_DUTYCYCLE 5000 // 5 [s] value in ms
mluis 1:60184eda0066 70 #define APP_TX_DUTYCYCLE_RND 1000 // 1 [s] value in ms
mluis 1:60184eda0066 71
mluis 1:60184eda0066 72 /*!
mluis 1:60184eda0066 73 * LoRaWAN Adaptative Data Rate
mluis 1:60184eda0066 74 */
mluis 1:60184eda0066 75 #define LORAWAN_ADR_ON 1
mluis 1:60184eda0066 76
mluis 1:60184eda0066 77 /*!
mluis 1:60184eda0066 78 * LoRaWAN confirmed messages
mluis 1:60184eda0066 79 */
mluis 1:60184eda0066 80 #define LORAWAN_CONFIRMED_MSG_ON 1
mluis 1:60184eda0066 81
mluis 1:60184eda0066 82 /*!
mluis 1:60184eda0066 83 * LoRaWAN application port
mluis 1:60184eda0066 84 */
mluis 1:60184eda0066 85 #define LORAWAN_APP_PORT 15
mluis 1:60184eda0066 86
mluis 1:60184eda0066 87 /*!
mluis 1:60184eda0066 88 * User application data buffer size
mluis 1:60184eda0066 89 */
mluis 1:60184eda0066 90 #if ( LORAWAN_CONFIRMED_MSG_ON == 1 )
mluis 1:60184eda0066 91 #define LORAWAN_APP_DATA_SIZE 6
mluis 1:60184eda0066 92
mluis 1:60184eda0066 93 #else
mluis 1:60184eda0066 94 #define LORAWAN_APP_DATA_SIZE 1
mluis 1:60184eda0066 95
mluis 1:60184eda0066 96 #endif
mluis 0:a2929fa6e4f0 97
pnysten 4:5e274bf85bf0 98 #define UINT16_MAX (65535U)
pnysten 4:5e274bf85bf0 99 #define UINT64_MAX (18446744073709551615ULL)
pnysten 6:540c5d907c90 100
pnysten 6:540c5d907c90 101 //Node lwm2mNode("LR-test0");;
pnysten 6:540c5d907c90 102 unsigned int LoRaWAN_data_size = 0;
pnysten 4:5e274bf85bf0 103
pnysten 4:5e274bf85bf0 104 std::string to_string( int x ) {
pnysten 4:5e274bf85bf0 105 int length = snprintf( NULL, 0, "%d", x );
pnysten 4:5e274bf85bf0 106 assert( length >= 0 );
pnysten 4:5e274bf85bf0 107 char* buf = new char[length + 1];
pnysten 4:5e274bf85bf0 108 snprintf( buf, length + 1, "%d", x );
pnysten 4:5e274bf85bf0 109 std::string str( buf );
pnysten 4:5e274bf85bf0 110 delete[] buf;
pnysten 4:5e274bf85bf0 111 return str;
pnysten 4:5e274bf85bf0 112 }
pnysten 4:5e274bf85bf0 113 unsigned char * get_Registration_Payload(long *payload_size){
pnysten 6:540c5d907c90 114
pnysten 4:5e274bf85bf0 115 string registration_Payload ="";
pnysten 6:540c5d907c90 116
pnysten 4:5e274bf85bf0 117 string s="";
pnysten 6:540c5d907c90 118
pnysten 4:5e274bf85bf0 119 s.append(REGISTRATION_OPEN);
pnysten 4:5e274bf85bf0 120 s.append(REGISTRATION_SEPARATOR);
pnysten 4:5e274bf85bf0 121 s.append("3/0/0");
pnysten 4:5e274bf85bf0 122 s.append(REGISTRATION_CLOSE);
pnysten 4:5e274bf85bf0 123 s.append(",");
pnysten 4:5e274bf85bf0 124 s.append(REGISTRATION_OPEN);
pnysten 4:5e274bf85bf0 125 s.append(REGISTRATION_SEPARATOR);
pnysten 4:5e274bf85bf0 126 s.append("3/0/1");
pnysten 4:5e274bf85bf0 127 s.append(REGISTRATION_CLOSE);
pnysten 4:5e274bf85bf0 128 s.append(",");
pnysten 4:5e274bf85bf0 129 s.append(REGISTRATION_OPEN);
pnysten 4:5e274bf85bf0 130 s.append(REGISTRATION_SEPARATOR);
pnysten 4:5e274bf85bf0 131 s.append("3/0/2");
pnysten 4:5e274bf85bf0 132 s.append(REGISTRATION_CLOSE);
pnysten 6:540c5d907c90 133
pnysten 4:5e274bf85bf0 134 registration_Payload.append(s);
pnysten 4:5e274bf85bf0 135
pnysten 4:5e274bf85bf0 136 unsigned char *c = new unsigned char[registration_Payload.size()+1];
pnysten 4:5e274bf85bf0 137 copy(registration_Payload.begin(),registration_Payload.end(),c);
pnysten 4:5e274bf85bf0 138 c[registration_Payload.size()]='\0';
pnysten 4:5e274bf85bf0 139 *payload_size=registration_Payload.size();
pnysten 4:5e274bf85bf0 140
pnysten 4:5e274bf85bf0 141 return c;
pnysten 6:540c5d907c90 142
pnysten 4:5e274bf85bf0 143 }
pnysten 4:5e274bf85bf0 144 uint8_t * get_Token(int * size){
pnysten 4:5e274bf85bf0 145 srand(time(0)+_node_Id);
pnysten 4:5e274bf85bf0 146 long test=0;
pnysten 4:5e274bf85bf0 147 bool exist=false;
pnysten 4:5e274bf85bf0 148
pnysten 4:5e274bf85bf0 149 do{
pnysten 4:5e274bf85bf0 150 test=(rand() % UINT64_MAX);
pnysten 4:5e274bf85bf0 151
pnysten 4:5e274bf85bf0 152 }while (exist==true);
pnysten 4:5e274bf85bf0 153 uint8_t ones = 0xFF;
pnysten 4:5e274bf85bf0 154 *size=1;
pnysten 4:5e274bf85bf0 155 for (int i=0; i<8; ++i) {
pnysten 4:5e274bf85bf0 156 if ( (test>>8*i & ones) =='\0' || i==8) {
pnysten 4:5e274bf85bf0 157 *size=i;
pnysten 4:5e274bf85bf0 158 break;
pnysten 4:5e274bf85bf0 159 }
pnysten 4:5e274bf85bf0 160 }
pnysten 4:5e274bf85bf0 161 uint8_t * token =new uint8_t[*size];
pnysten 4:5e274bf85bf0 162 for (int i=0; i<*size; ++i){
pnysten 4:5e274bf85bf0 163 token[*size-1-i]=test>>8*i & ones;
pnysten 4:5e274bf85bf0 164 }
pnysten 4:5e274bf85bf0 165 return token;
pnysten 4:5e274bf85bf0 166 }
pnysten 6:540c5d907c90 167
pnysten 4:5e274bf85bf0 168 uint16_t get_Message_ID(){
pnysten 4:5e274bf85bf0 169 srand(time(0)+_node_Id);
pnysten 4:5e274bf85bf0 170 int test=0;
pnysten 4:5e274bf85bf0 171 bool exist=false;
pnysten 4:5e274bf85bf0 172 do{
pnysten 4:5e274bf85bf0 173
pnysten 4:5e274bf85bf0 174 exist=false;
pnysten 4:5e274bf85bf0 175 test=(rand() % UINT16_MAX);
pnysten 4:5e274bf85bf0 176
pnysten 4:5e274bf85bf0 177 }while (exist==true);
pnysten 4:5e274bf85bf0 178
pnysten 4:5e274bf85bf0 179
pnysten 4:5e274bf85bf0 180 return (uint16_t) test;
pnysten 4:5e274bf85bf0 181
pnysten 4:5e274bf85bf0 182 }
pnysten 6:540c5d907c90 183
pnysten 4:5e274bf85bf0 184 char * get_Registration_Query(){
pnysten 4:5e274bf85bf0 185
pnysten 4:5e274bf85bf0 186 string buffer;
pnysten 4:5e274bf85bf0 187 buffer.append(REGISTRATION_SEGMENT);
pnysten 4:5e274bf85bf0 188 buffer.append(ENDPOINT_SEGMENT);
pnysten 4:5e274bf85bf0 189 buffer.append(endPoint_Name);
pnysten 4:5e274bf85bf0 190 buffer.append(LIFETIME);
pnysten 4:5e274bf85bf0 191 buffer.append(to_string(lifeTime));
pnysten 4:5e274bf85bf0 192 buffer.append(BINDING);
pnysten 4:5e274bf85bf0 193 buffer.append(binding);
pnysten 4:5e274bf85bf0 194
pnysten 4:5e274bf85bf0 195 char *c = new char[buffer.size()+1];
pnysten 4:5e274bf85bf0 196 copy(buffer.begin(),buffer.end(),c);
pnysten 4:5e274bf85bf0 197 c[buffer.size()]='\0';
pnysten 4:5e274bf85bf0 198 return c;
pnysten 4:5e274bf85bf0 199
pnysten 6:540c5d907c90 200
pnysten 4:5e274bf85bf0 201 }
pnysten 6:540c5d907c90 202
mluis 0:a2929fa6e4f0 203 //////////////////////////////////////////////////
mluis 0:a2929fa6e4f0 204 // CONFIGURATION (FOR APPLICATION CALLBACKS BELOW)
mluis 0:a2929fa6e4f0 205 //////////////////////////////////////////////////
mluis 0:a2929fa6e4f0 206
mluis 0:a2929fa6e4f0 207 // application router ID (LSBF)
mluis 1:60184eda0066 208 static const uint8_t AppEui[8] =
mluis 0:a2929fa6e4f0 209 {
mluis 1:60184eda0066 210 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
pnysten 6:540c5d907c90 211 // 0x01, 0x02, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00
mluis 0:a2929fa6e4f0 212 };
mluis 0:a2929fa6e4f0 213
mluis 0:a2929fa6e4f0 214 // unique device ID (LSBF)
mluis 0:a2929fa6e4f0 215 static const u1_t DevEui[8] =
mluis 0:a2929fa6e4f0 216 {
pnysten 6:540c5d907c90 217 // 0x00, 0x00, 0x00, 0x00, 0x08, 0x06, 0x02, 0x48
pnysten 6:540c5d907c90 218
pnysten 6:540c5d907c90 219 0x30, 0x74, 0x73, 0x65, 0x74, 0x2D, 0x52, 0x4C // 4c522d7465737430 = "LR-test0"
pnysten 6:540c5d907c90 220 // 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x99, 0xF7
pnysten 6:540c5d907c90 221
pnysten 6:540c5d907c90 222
pnysten 4:5e274bf85bf0 223 // 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF
pnysten 4:5e274bf85bf0 224 // 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x099, 0xF7
mluis 0:a2929fa6e4f0 225 };
mluis 0:a2929fa6e4f0 226
mluis 0:a2929fa6e4f0 227 // device-specific AES key (derived from device EUI)
mluis 1:60184eda0066 228 static const uint8_t DevKey[16] =
mluis 0:a2929fa6e4f0 229 {
pnysten 6:540c5d907c90 230 // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
pnysten 6:540c5d907c90 231 // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
mluis 1:60184eda0066 232 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
mluis 1:60184eda0066 233 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
mluis 0:a2929fa6e4f0 234 };
mluis 0:a2929fa6e4f0 235
mluis 1:60184eda0066 236 #if( OVER_THE_AIR_ACTIVATION == 0 )
mluis 1:60184eda0066 237 // network session key
mluis 0:a2929fa6e4f0 238 static uint8_t NwkSKey[] =
mluis 0:a2929fa6e4f0 239 {
mluis 0:a2929fa6e4f0 240 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
mluis 0:a2929fa6e4f0 241 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
pnysten 6:540c5d907c90 242 // 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3D
mluis 0:a2929fa6e4f0 243 };
mluis 0:a2929fa6e4f0 244
mluis 1:60184eda0066 245 // application session key
mluis 0:a2929fa6e4f0 246 static uint8_t ArtSKey[] =
mluis 0:a2929fa6e4f0 247 {
mluis 0:a2929fa6e4f0 248 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
mluis 0:a2929fa6e4f0 249 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
pnysten 6:540c5d907c90 250 // 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3D
mluis 0:a2929fa6e4f0 251 };
mluis 0:a2929fa6e4f0 252
mluis 1:60184eda0066 253 #endif
mluis 1:60184eda0066 254
mluis 0:a2929fa6e4f0 255 // LEDs and Frame jobs
mluis 0:a2929fa6e4f0 256 osjob_t rxLedJob;
mluis 0:a2929fa6e4f0 257 osjob_t txLedJob;
mluis 0:a2929fa6e4f0 258 osjob_t sendFrameJob;
mluis 0:a2929fa6e4f0 259
mluis 1:60184eda0066 260 // LED state
mluis 0:a2929fa6e4f0 261 static bool AppLedStateOn = false;
mluis 0:a2929fa6e4f0 262
mluis 0:a2929fa6e4f0 263 //////////////////////////////////////////////////
mluis 1:60184eda0066 264 // Utility functions
mluis 1:60184eda0066 265 //////////////////////////////////////////////////
mluis 1:60184eda0066 266 /*!
mluis 1:60184eda0066 267 * \brief Computes a random number between min and max
mluis 1:60184eda0066 268 *
mluis 1:60184eda0066 269 * \param [IN] min range minimum value
mluis 1:60184eda0066 270 * \param [IN] max range maximum value
mluis 1:60184eda0066 271 * \retval random random value in range min..max
mluis 1:60184eda0066 272 */
mluis 1:60184eda0066 273 int32_t randr( int32_t min, int32_t max )
mluis 1:60184eda0066 274 {
mluis 1:60184eda0066 275 return ( int32_t )rand( ) % ( max - min + 1 ) + min;
mluis 1:60184eda0066 276 }
mluis 1:60184eda0066 277
mluis 1:60184eda0066 278 //////////////////////////////////////////////////
mluis 0:a2929fa6e4f0 279 // APPLICATION CALLBACKS
mluis 0:a2929fa6e4f0 280 //////////////////////////////////////////////////
mluis 0:a2929fa6e4f0 281
mluis 0:a2929fa6e4f0 282 // provide application router ID (8 bytes, LSBF)
mluis 1:60184eda0066 283 void os_getArtEui( uint8_t *buf )
mluis 0:a2929fa6e4f0 284 {
mluis 0:a2929fa6e4f0 285 memcpy( buf, AppEui, 8 );
mluis 0:a2929fa6e4f0 286 }
mluis 0:a2929fa6e4f0 287
mluis 0:a2929fa6e4f0 288 // provide device ID (8 bytes, LSBF)
mluis 1:60184eda0066 289 void os_getDevEui( uint8_t *buf )
mluis 0:a2929fa6e4f0 290 {
mluis 0:a2929fa6e4f0 291 memcpy( buf, DevEui, 8 );
mluis 0:a2929fa6e4f0 292 }
mluis 0:a2929fa6e4f0 293
mluis 0:a2929fa6e4f0 294 // provide device key (16 bytes)
mluis 1:60184eda0066 295 void os_getDevKey( uint8_t *buf )
mluis 0:a2929fa6e4f0 296 {
mluis 0:a2929fa6e4f0 297 memcpy( buf, DevKey, 16 );
mluis 0:a2929fa6e4f0 298 }
mluis 0:a2929fa6e4f0 299
mluis 0:a2929fa6e4f0 300 //////////////////////////////////////////////////
mluis 0:a2929fa6e4f0 301 // MAIN - INITIALIZATION AND STARTUP
mluis 0:a2929fa6e4f0 302 //////////////////////////////////////////////////
mluis 0:a2929fa6e4f0 303
mluis 0:a2929fa6e4f0 304 static void onRxLed( osjob_t* j )
mluis 0:a2929fa6e4f0 305 {
mluis 1:60184eda0066 306 debug_val("LED2 = ", 0 );
mluis 0:a2929fa6e4f0 307 }
mluis 0:a2929fa6e4f0 308
mluis 0:a2929fa6e4f0 309 static void onTxLed( osjob_t* j )
mluis 0:a2929fa6e4f0 310 {
mluis 1:60184eda0066 311 debug_val("LED1 = ", 0 );
mluis 0:a2929fa6e4f0 312 }
mluis 0:a2929fa6e4f0 313
pnysten 6:540c5d907c90 314 static void prepareTxCoapFrame( void )
pnysten 6:540c5d907c90 315 {
pnysten 6:540c5d907c90 316 // Create Registration PDU :
pnysten 6:540c5d907c90 317
pnysten 6:540c5d907c90 318 CoapPDU *pdu = new CoapPDU();
pnysten 6:540c5d907c90 319
pnysten 6:540c5d907c90 320 pdu->setCode(CoapPDU::COAP_POST);
pnysten 6:540c5d907c90 321 pdu->setType(CoapPDU::COAP_CONFIRMABLE);
pnysten 6:540c5d907c90 322 int size;
pnysten 6:540c5d907c90 323 uint8_t * token = get_Token(&size);
pnysten 6:540c5d907c90 324 pdu->setToken(token,size);
pnysten 6:540c5d907c90 325 pdu->setMessageID(get_Message_ID());
pnysten 6:540c5d907c90 326 pdu->setURI(get_Registration_Query());
pnysten 6:540c5d907c90 327
pnysten 6:540c5d907c90 328 _payload=get_Registration_Payload(&_payload_size);
pnysten 6:540c5d907c90 329 pdu->setPayload(_payload, (int) _payload_size);
pnysten 6:540c5d907c90 330 int PDUlength = pdu->getPDULength();
pnysten 6:540c5d907c90 331
pnysten 6:540c5d907c90 332 // strncpy((char*) LMIC.frame, (const char*)pdu->getPDUPointer(), PDUlength);
pnysten 6:540c5d907c90 333 memcpy(LMIC.frame, pdu->getPDUPointer(), PDUlength * sizeof(uint8_t));
pnysten 6:540c5d907c90 334
pnysten 6:540c5d907c90 335 #if ( LORAWAN_CONFIRMED_MSG_ON == 1 )
pnysten 6:540c5d907c90 336 LMIC.frame[PDUlength] = LMIC.seqnoDn >> 8;
pnysten 6:540c5d907c90 337 LMIC.frame[PDUlength+1] = LMIC.seqnoDn;
pnysten 6:540c5d907c90 338 LMIC.frame[PDUlength+2] = LMIC.rssi >> 8;
pnysten 6:540c5d907c90 339 LMIC.frame[PDUlength+3] = LMIC.rssi;
pnysten 6:540c5d907c90 340 LMIC.frame[PDUlength+4] = LMIC.snr;
pnysten 6:540c5d907c90 341 #endif
pnysten 6:540c5d907c90 342 debug_str("Frame to be sent: ");
pnysten 6:540c5d907c90 343 debug_buf(LMIC.frame, PDUlength + 5);
pnysten 6:540c5d907c90 344
pnysten 6:540c5d907c90 345 LoRaWAN_data_size = PDUlength + 5;
pnysten 6:540c5d907c90 346 }
pnysten 6:540c5d907c90 347
mluis 0:a2929fa6e4f0 348 static void prepareTxFrame( void )
mluis 0:a2929fa6e4f0 349 {
mluis 0:a2929fa6e4f0 350 LMIC.frame[0] = AppLedStateOn;
mluis 1:60184eda0066 351 #if ( LORAWAN_CONFIRMED_MSG_ON == 1 )
mluis 1:60184eda0066 352 LMIC.frame[1] = LMIC.seqnoDn >> 8;
mluis 1:60184eda0066 353 LMIC.frame[2] = LMIC.seqnoDn;
mluis 1:60184eda0066 354 LMIC.frame[3] = LMIC.rssi >> 8;
mluis 1:60184eda0066 355 LMIC.frame[4] = LMIC.rssi;
mluis 1:60184eda0066 356 LMIC.frame[5] = LMIC.snr;
mluis 1:60184eda0066 357 #endif
pnysten 6:540c5d907c90 358 debug_str("Frame to be sent: ");
pnysten 6:540c5d907c90 359 debug_buf(LMIC.frame, LORAWAN_APP_DATA_SIZE);
pnysten 6:540c5d907c90 360
pnysten 6:540c5d907c90 361 LoRaWAN_data_size = LORAWAN_APP_DATA_SIZE;
mluis 0:a2929fa6e4f0 362 }
mluis 0:a2929fa6e4f0 363
mluis 0:a2929fa6e4f0 364 void processRxFrame( void )
mluis 0:a2929fa6e4f0 365 {
pnysten 4:5e274bf85bf0 366 debug_str("Data: ");
pnysten 4:5e274bf85bf0 367 debug_buf( LMIC.frame + LMIC.dataBeg, LMIC.dataLen );
pnysten 4:5e274bf85bf0 368
mluis 0:a2929fa6e4f0 369 switch( LMIC.frame[LMIC.dataBeg - 1] ) // Check Rx port number
mluis 0:a2929fa6e4f0 370 {
pnysten 4:5e274bf85bf0 371 case 0:
pnysten 4:5e274bf85bf0 372 // debug_str("Port 0!!!\r\n");
pnysten 4:5e274bf85bf0 373 // debug_val("Data Len: ", LMIC.dataLen);
pnysten 4:5e274bf85bf0 374
mluis 0:a2929fa6e4f0 375 case 1: // The application LED can be controlled on port 1 or 2
mluis 0:a2929fa6e4f0 376 case 2:
mluis 0:a2929fa6e4f0 377 if( LMIC.dataLen == 1 )
mluis 0:a2929fa6e4f0 378 {
pnysten 4:5e274bf85bf0 379 debug_str("Data received on port 2: ");
pnysten 4:5e274bf85bf0 380 debug_hex(LMIC.frame[LMIC.dataBeg]);
pnysten 4:5e274bf85bf0 381 debug_str("\r\n");
mluis 0:a2929fa6e4f0 382 AppLedStateOn = LMIC.frame[LMIC.dataBeg] & 0x01;
mluis 1:60184eda0066 383 debug_val( "LED3 = ", AppLedStateOn );
mluis 0:a2929fa6e4f0 384 }
mluis 0:a2929fa6e4f0 385 break;
mluis 0:a2929fa6e4f0 386 default:
mluis 0:a2929fa6e4f0 387 break;
mluis 0:a2929fa6e4f0 388 }
mluis 0:a2929fa6e4f0 389 }
mluis 0:a2929fa6e4f0 390
mluis 0:a2929fa6e4f0 391 static void onSendFrame( osjob_t* j )
mluis 0:a2929fa6e4f0 392 {
pnysten 6:540c5d907c90 393 //prepareTxFrame( );
pnysten 6:540c5d907c90 394 prepareTxCoapFrame();
pnysten 4:5e274bf85bf0 395
pnysten 6:540c5d907c90 396 LMIC_setTxData2( LORAWAN_APP_PORT, LMIC.frame, LoRaWAN_data_size, LORAWAN_CONFIRMED_MSG_ON );
mluis 1:60184eda0066 397
mluis 1:60184eda0066 398 // Blink Tx LED
mluis 1:60184eda0066 399 debug_val( "LED1 = ", 1 );
mluis 1:60184eda0066 400 os_setTimedCallback( &txLedJob, os_getTime( ) + ms2osticks( 25 ), onTxLed );
mluis 0:a2929fa6e4f0 401 }
mluis 0:a2929fa6e4f0 402
mluis 1:60184eda0066 403 // Initialization job
mluis 1:60184eda0066 404 static void onInit( osjob_t* j )
mluis 1:60184eda0066 405 {
mluis 1:60184eda0066 406 // reset MAC state
mluis 1:60184eda0066 407 LMIC_reset( );
mluis 1:60184eda0066 408 LMIC_setAdrMode( LORAWAN_ADR_ON );
mluis 1:60184eda0066 409 LMIC_setDrTxpow( DR_SF12, 14 );
mluis 1:60184eda0066 410
mluis 1:60184eda0066 411 // start joining
mluis 1:60184eda0066 412 #if( OVER_THE_AIR_ACTIVATION != 0 )
mluis 1:60184eda0066 413 LMIC_startJoining( );
mluis 1:60184eda0066 414 #else
mluis 1:60184eda0066 415 LMIC_setSession( LORAWAN_NET_ID, LORAWAN_DEV_ADDR, NwkSKey, ArtSKey );
mluis 1:60184eda0066 416 onSendFrame( NULL );
mluis 1:60184eda0066 417 #endif
mluis 1:60184eda0066 418 // init done - onEvent( ) callback will be invoked...
mluis 1:60184eda0066 419 }
mluis 1:60184eda0066 420
mluis 1:60184eda0066 421 int main( void )
mluis 0:a2929fa6e4f0 422 {
pnysten 4:5e274bf85bf0 423 debug_init();
mluis 0:a2929fa6e4f0 424 osjob_t initjob;
pnysten 6:540c5d907c90 425
mluis 0:a2929fa6e4f0 426 // initialize runtime env
mluis 0:a2929fa6e4f0 427 os_init( );
mluis 0:a2929fa6e4f0 428 // setup initial job
mluis 0:a2929fa6e4f0 429 os_setCallback( &initjob, onInit );
mluis 0:a2929fa6e4f0 430 // execute scheduled jobs and events
mluis 0:a2929fa6e4f0 431 os_runloop( );
mluis 0:a2929fa6e4f0 432 // (not reached)
mluis 0:a2929fa6e4f0 433 }
mluis 0:a2929fa6e4f0 434
mluis 0:a2929fa6e4f0 435 //////////////////////////////////////////////////
mluis 0:a2929fa6e4f0 436 // LMIC EVENT CALLBACK
mluis 0:a2929fa6e4f0 437 //////////////////////////////////////////////////
mluis 0:a2929fa6e4f0 438 void onEvent( ev_t ev )
mluis 0:a2929fa6e4f0 439 {
mluis 0:a2929fa6e4f0 440 bool txOn = false;
mluis 1:60184eda0066 441 debug_event( ev );
mluis 0:a2929fa6e4f0 442
mluis 0:a2929fa6e4f0 443 switch( ev )
mluis 0:a2929fa6e4f0 444 {
mluis 0:a2929fa6e4f0 445 // network joined, session established
mluis 0:a2929fa6e4f0 446 case EV_JOINED:
mluis 1:60184eda0066 447 debug_val( "Net ID = ", LMIC.netid );
mluis 0:a2929fa6e4f0 448 txOn = true;
mluis 0:a2929fa6e4f0 449 break;
mluis 0:a2929fa6e4f0 450 // scheduled data sent (optionally data received)
mluis 0:a2929fa6e4f0 451 case EV_TXCOMPLETE:
mluis 1:60184eda0066 452 debug_val( "Datarate = ", LMIC.datarate );
mluis 0:a2929fa6e4f0 453 // Check if we have a downlink on either Rx1 or Rx2 windows
mluis 1:60184eda0066 454 if( ( LMIC.txrxFlags & ( TXRX_DNW1 | TXRX_DNW2 ) ) != 0 )
mluis 0:a2929fa6e4f0 455 {
mluis 1:60184eda0066 456 debug_val( "LED2 = ", 1 );
mluis 1:60184eda0066 457 os_setTimedCallback( &rxLedJob, os_getTime( ) + ms2osticks( 25 ), onRxLed );
mluis 0:a2929fa6e4f0 458
mluis 1:60184eda0066 459 if( LMIC.dataLen != 0 )
mluis 0:a2929fa6e4f0 460 { // data received in rx slot after tx
pnysten 4:5e274bf85bf0 461 //debug_buf( LMIC.frame + LMIC.dataBeg, LMIC.dataLen );
mluis 0:a2929fa6e4f0 462 processRxFrame( );
mluis 0:a2929fa6e4f0 463 }
mluis 0:a2929fa6e4f0 464 }
mluis 0:a2929fa6e4f0 465 txOn = true;
mluis 0:a2929fa6e4f0 466 break;
mluis 0:a2929fa6e4f0 467 default:
mluis 0:a2929fa6e4f0 468 break;
mluis 0:a2929fa6e4f0 469 }
mluis 0:a2929fa6e4f0 470 if( txOn == true )
mluis 0:a2929fa6e4f0 471 {
mluis 1:60184eda0066 472 //Sends frame every APP_TX_DUTYCYCLE +/- APP_TX_DUTYCYCLE_RND random time (if not duty cycle limited)
mluis 1:60184eda0066 473 os_setTimedCallback( &sendFrameJob,
mluis 1:60184eda0066 474 os_getTime( ) + ms2osticks( APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND ) ),
mluis 1:60184eda0066 475 onSendFrame );
mluis 0:a2929fa6e4f0 476
mluis 1:60184eda0066 477 ////Sends frame as soon as possible (duty cylce limitations)
mluis 1:60184eda0066 478 //onSendFrame( NULL );
mluis 0:a2929fa6e4f0 479 }
mluis 0:a2929fa6e4f0 480 }