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 21 14:19:42 2015 +0000
Revision:
8:8d9a49aaa323
Parent:
7:f1521b0ecf08
Child:
9:84a69ca4d35a
"LoRa" payload

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 */
pnysten 7:f1521b0ecf08 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 8:8d9a49aaa323 221 // 0x32, 0x74, 0x73, 0x65, 0x74, 0x2D, 0x52, 0x4C // 4c522d7465737430 = "LR-test2"
pnysten 6:540c5d907c90 222
pnysten 6:540c5d907c90 223
pnysten 4:5e274bf85bf0 224 // 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF
pnysten 4:5e274bf85bf0 225 // 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x099, 0xF7
mluis 0:a2929fa6e4f0 226 };
mluis 0:a2929fa6e4f0 227
mluis 0:a2929fa6e4f0 228 // device-specific AES key (derived from device EUI)
mluis 1:60184eda0066 229 static const uint8_t DevKey[16] =
mluis 0:a2929fa6e4f0 230 {
pnysten 6:540c5d907c90 231 // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
pnysten 6:540c5d907c90 232 // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
mluis 1:60184eda0066 233 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
mluis 1:60184eda0066 234 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
mluis 0:a2929fa6e4f0 235 };
mluis 0:a2929fa6e4f0 236
mluis 1:60184eda0066 237 #if( OVER_THE_AIR_ACTIVATION == 0 )
mluis 1:60184eda0066 238 // network session key
mluis 0:a2929fa6e4f0 239 static uint8_t NwkSKey[] =
mluis 0:a2929fa6e4f0 240 {
mluis 0:a2929fa6e4f0 241 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
mluis 0:a2929fa6e4f0 242 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
pnysten 6:540c5d907c90 243 // 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3D
mluis 0:a2929fa6e4f0 244 };
mluis 0:a2929fa6e4f0 245
mluis 1:60184eda0066 246 // application session key
mluis 0:a2929fa6e4f0 247 static uint8_t ArtSKey[] =
mluis 0:a2929fa6e4f0 248 {
mluis 0:a2929fa6e4f0 249 0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
mluis 0:a2929fa6e4f0 250 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
pnysten 6:540c5d907c90 251 // 0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3D
mluis 0:a2929fa6e4f0 252 };
mluis 0:a2929fa6e4f0 253
mluis 1:60184eda0066 254 #endif
mluis 1:60184eda0066 255
mluis 0:a2929fa6e4f0 256 // LEDs and Frame jobs
mluis 0:a2929fa6e4f0 257 osjob_t rxLedJob;
mluis 0:a2929fa6e4f0 258 osjob_t txLedJob;
mluis 0:a2929fa6e4f0 259 osjob_t sendFrameJob;
mluis 0:a2929fa6e4f0 260
mluis 1:60184eda0066 261 // LED state
mluis 0:a2929fa6e4f0 262 static bool AppLedStateOn = false;
mluis 0:a2929fa6e4f0 263
mluis 0:a2929fa6e4f0 264 //////////////////////////////////////////////////
mluis 1:60184eda0066 265 // Utility functions
mluis 1:60184eda0066 266 //////////////////////////////////////////////////
mluis 1:60184eda0066 267 /*!
mluis 1:60184eda0066 268 * \brief Computes a random number between min and max
mluis 1:60184eda0066 269 *
mluis 1:60184eda0066 270 * \param [IN] min range minimum value
mluis 1:60184eda0066 271 * \param [IN] max range maximum value
mluis 1:60184eda0066 272 * \retval random random value in range min..max
mluis 1:60184eda0066 273 */
mluis 1:60184eda0066 274 int32_t randr( int32_t min, int32_t max )
mluis 1:60184eda0066 275 {
mluis 1:60184eda0066 276 return ( int32_t )rand( ) % ( max - min + 1 ) + min;
mluis 1:60184eda0066 277 }
mluis 1:60184eda0066 278
mluis 1:60184eda0066 279 //////////////////////////////////////////////////
mluis 0:a2929fa6e4f0 280 // APPLICATION CALLBACKS
mluis 0:a2929fa6e4f0 281 //////////////////////////////////////////////////
mluis 0:a2929fa6e4f0 282
mluis 0:a2929fa6e4f0 283 // provide application router ID (8 bytes, LSBF)
mluis 1:60184eda0066 284 void os_getArtEui( uint8_t *buf )
mluis 0:a2929fa6e4f0 285 {
mluis 0:a2929fa6e4f0 286 memcpy( buf, AppEui, 8 );
mluis 0:a2929fa6e4f0 287 }
mluis 0:a2929fa6e4f0 288
mluis 0:a2929fa6e4f0 289 // provide device ID (8 bytes, LSBF)
mluis 1:60184eda0066 290 void os_getDevEui( uint8_t *buf )
mluis 0:a2929fa6e4f0 291 {
mluis 0:a2929fa6e4f0 292 memcpy( buf, DevEui, 8 );
mluis 0:a2929fa6e4f0 293 }
mluis 0:a2929fa6e4f0 294
mluis 0:a2929fa6e4f0 295 // provide device key (16 bytes)
mluis 1:60184eda0066 296 void os_getDevKey( uint8_t *buf )
mluis 0:a2929fa6e4f0 297 {
mluis 0:a2929fa6e4f0 298 memcpy( buf, DevKey, 16 );
mluis 0:a2929fa6e4f0 299 }
mluis 0:a2929fa6e4f0 300
mluis 0:a2929fa6e4f0 301 //////////////////////////////////////////////////
mluis 0:a2929fa6e4f0 302 // MAIN - INITIALIZATION AND STARTUP
mluis 0:a2929fa6e4f0 303 //////////////////////////////////////////////////
mluis 0:a2929fa6e4f0 304
mluis 0:a2929fa6e4f0 305 static void onRxLed( osjob_t* j )
mluis 0:a2929fa6e4f0 306 {
mluis 1:60184eda0066 307 debug_val("LED2 = ", 0 );
mluis 0:a2929fa6e4f0 308 }
mluis 0:a2929fa6e4f0 309
mluis 0:a2929fa6e4f0 310 static void onTxLed( osjob_t* j )
mluis 0:a2929fa6e4f0 311 {
mluis 1:60184eda0066 312 debug_val("LED1 = ", 0 );
mluis 0:a2929fa6e4f0 313 }
mluis 0:a2929fa6e4f0 314
pnysten 6:540c5d907c90 315 static void prepareTxCoapFrame( void )
pnysten 6:540c5d907c90 316 {
pnysten 6:540c5d907c90 317 // Create Registration PDU :
pnysten 6:540c5d907c90 318
pnysten 6:540c5d907c90 319 CoapPDU *pdu = new CoapPDU();
pnysten 6:540c5d907c90 320
pnysten 6:540c5d907c90 321 pdu->setCode(CoapPDU::COAP_POST);
pnysten 6:540c5d907c90 322 pdu->setType(CoapPDU::COAP_CONFIRMABLE);
pnysten 6:540c5d907c90 323 int size;
pnysten 6:540c5d907c90 324 uint8_t * token = get_Token(&size);
pnysten 6:540c5d907c90 325 pdu->setToken(token,size);
pnysten 6:540c5d907c90 326 pdu->setMessageID(get_Message_ID());
pnysten 6:540c5d907c90 327 pdu->setURI(get_Registration_Query());
pnysten 6:540c5d907c90 328
pnysten 6:540c5d907c90 329 _payload=get_Registration_Payload(&_payload_size);
pnysten 6:540c5d907c90 330 pdu->setPayload(_payload, (int) _payload_size);
pnysten 6:540c5d907c90 331 int PDUlength = pdu->getPDULength();
pnysten 6:540c5d907c90 332
pnysten 6:540c5d907c90 333 // strncpy((char*) LMIC.frame, (const char*)pdu->getPDUPointer(), PDUlength);
pnysten 6:540c5d907c90 334 memcpy(LMIC.frame, pdu->getPDUPointer(), PDUlength * sizeof(uint8_t));
pnysten 6:540c5d907c90 335
pnysten 6:540c5d907c90 336 #if ( LORAWAN_CONFIRMED_MSG_ON == 1 )
pnysten 6:540c5d907c90 337 LMIC.frame[PDUlength] = LMIC.seqnoDn >> 8;
pnysten 6:540c5d907c90 338 LMIC.frame[PDUlength+1] = LMIC.seqnoDn;
pnysten 6:540c5d907c90 339 LMIC.frame[PDUlength+2] = LMIC.rssi >> 8;
pnysten 6:540c5d907c90 340 LMIC.frame[PDUlength+3] = LMIC.rssi;
pnysten 6:540c5d907c90 341 LMIC.frame[PDUlength+4] = LMIC.snr;
pnysten 6:540c5d907c90 342 #endif
pnysten 6:540c5d907c90 343 debug_str("Frame to be sent: ");
pnysten 6:540c5d907c90 344 debug_buf(LMIC.frame, PDUlength + 5);
pnysten 6:540c5d907c90 345
pnysten 6:540c5d907c90 346 LoRaWAN_data_size = PDUlength + 5;
pnysten 6:540c5d907c90 347 }
pnysten 6:540c5d907c90 348
pnysten 8:8d9a49aaa323 349 static void prepareTxLoraFrame( void )
pnysten 8:8d9a49aaa323 350 {
pnysten 8:8d9a49aaa323 351 const char *frame = "LoRa";
pnysten 8:8d9a49aaa323 352
pnysten 8:8d9a49aaa323 353 strncpy((char*) LMIC.frame, frame, strlen(frame));
pnysten 8:8d9a49aaa323 354
pnysten 8:8d9a49aaa323 355 #if ( LORAWAN_CONFIRMED_MSG_ON == 1 )
pnysten 8:8d9a49aaa323 356 LMIC.frame[strlen(frame)] = LMIC.seqnoDn >> 8;
pnysten 8:8d9a49aaa323 357 LMIC.frame[strlen(frame)+1] = LMIC.seqnoDn;
pnysten 8:8d9a49aaa323 358 LMIC.frame[strlen(frame)+2] = LMIC.rssi >> 8;
pnysten 8:8d9a49aaa323 359 LMIC.frame[strlen(frame)+3] = LMIC.rssi;
pnysten 8:8d9a49aaa323 360 LMIC.frame[strlen(frame)+4] = LMIC.snr;
pnysten 8:8d9a49aaa323 361 #endif
pnysten 8:8d9a49aaa323 362 debug_str("Frame to be sent: ");
pnysten 8:8d9a49aaa323 363 debug_buf(LMIC.frame, strlen(frame) + 5);
pnysten 8:8d9a49aaa323 364
pnysten 8:8d9a49aaa323 365 LoRaWAN_data_size = strlen(frame) + 5;
pnysten 8:8d9a49aaa323 366 }
pnysten 8:8d9a49aaa323 367
mluis 0:a2929fa6e4f0 368 static void prepareTxFrame( void )
mluis 0:a2929fa6e4f0 369 {
mluis 0:a2929fa6e4f0 370 LMIC.frame[0] = AppLedStateOn;
mluis 1:60184eda0066 371 #if ( LORAWAN_CONFIRMED_MSG_ON == 1 )
mluis 1:60184eda0066 372 LMIC.frame[1] = LMIC.seqnoDn >> 8;
mluis 1:60184eda0066 373 LMIC.frame[2] = LMIC.seqnoDn;
mluis 1:60184eda0066 374 LMIC.frame[3] = LMIC.rssi >> 8;
mluis 1:60184eda0066 375 LMIC.frame[4] = LMIC.rssi;
mluis 1:60184eda0066 376 LMIC.frame[5] = LMIC.snr;
mluis 1:60184eda0066 377 #endif
pnysten 6:540c5d907c90 378 debug_str("Frame to be sent: ");
pnysten 6:540c5d907c90 379 debug_buf(LMIC.frame, LORAWAN_APP_DATA_SIZE);
pnysten 6:540c5d907c90 380
pnysten 6:540c5d907c90 381 LoRaWAN_data_size = LORAWAN_APP_DATA_SIZE;
mluis 0:a2929fa6e4f0 382 }
mluis 0:a2929fa6e4f0 383
mluis 0:a2929fa6e4f0 384 void processRxFrame( void )
mluis 0:a2929fa6e4f0 385 {
pnysten 8:8d9a49aaa323 386
pnysten 8:8d9a49aaa323 387 char* frameToDisplay = (char*) (LMIC.frame + LMIC.dataBeg);
pnysten 8:8d9a49aaa323 388 frameToDisplay[LMIC.dataLen] = '\0';
pnysten 4:5e274bf85bf0 389
mluis 0:a2929fa6e4f0 390 switch( LMIC.frame[LMIC.dataBeg - 1] ) // Check Rx port number
mluis 0:a2929fa6e4f0 391 {
pnysten 4:5e274bf85bf0 392 case 0:
pnysten 4:5e274bf85bf0 393 // debug_str("Port 0!!!\r\n");
pnysten 4:5e274bf85bf0 394 // debug_val("Data Len: ", LMIC.dataLen);
pnysten 4:5e274bf85bf0 395
mluis 0:a2929fa6e4f0 396 case 1: // The application LED can be controlled on port 1 or 2
pnysten 8:8d9a49aaa323 397 debug_str("Data received on port 1: ");
pnysten 8:8d9a49aaa323 398 debug_str("Data in hexa: ");
pnysten 8:8d9a49aaa323 399 debug_buf( LMIC.frame + LMIC.dataBeg, LMIC.dataLen );
pnysten 8:8d9a49aaa323 400 debug_str("Data in string: ");
pnysten 8:8d9a49aaa323 401
pnysten 8:8d9a49aaa323 402 debug_str( frameToDisplay );
pnysten 8:8d9a49aaa323 403 debug_str("\r\n");
pnysten 8:8d9a49aaa323 404
pnysten 8:8d9a49aaa323 405 break;
mluis 0:a2929fa6e4f0 406 case 2:
pnysten 8:8d9a49aaa323 407 debug_str("Data received on port 2: ");
pnysten 8:8d9a49aaa323 408 debug_str("Data in hexa: ");
pnysten 8:8d9a49aaa323 409 debug_buf( LMIC.frame + LMIC.dataBeg, LMIC.dataLen );
pnysten 8:8d9a49aaa323 410 debug_str("Data in string: ");
pnysten 8:8d9a49aaa323 411
pnysten 8:8d9a49aaa323 412 debug_str( frameToDisplay );
pnysten 8:8d9a49aaa323 413 debug_str("\r\n");
pnysten 8:8d9a49aaa323 414
mluis 0:a2929fa6e4f0 415 if( LMIC.dataLen == 1 )
mluis 0:a2929fa6e4f0 416 {
pnysten 4:5e274bf85bf0 417 debug_str("Data received on port 2: ");
pnysten 4:5e274bf85bf0 418 debug_hex(LMIC.frame[LMIC.dataBeg]);
pnysten 4:5e274bf85bf0 419 debug_str("\r\n");
mluis 0:a2929fa6e4f0 420 AppLedStateOn = LMIC.frame[LMIC.dataBeg] & 0x01;
mluis 1:60184eda0066 421 debug_val( "LED3 = ", AppLedStateOn );
mluis 0:a2929fa6e4f0 422 }
mluis 0:a2929fa6e4f0 423 break;
mluis 0:a2929fa6e4f0 424 default:
mluis 0:a2929fa6e4f0 425 break;
mluis 0:a2929fa6e4f0 426 }
mluis 0:a2929fa6e4f0 427 }
mluis 0:a2929fa6e4f0 428
mluis 0:a2929fa6e4f0 429 static void onSendFrame( osjob_t* j )
mluis 0:a2929fa6e4f0 430 {
pnysten 6:540c5d907c90 431 //prepareTxFrame( );
pnysten 8:8d9a49aaa323 432 //prepareTxCoapFrame();
pnysten 8:8d9a49aaa323 433 prepareTxLoraFrame();
pnysten 4:5e274bf85bf0 434
pnysten 6:540c5d907c90 435 LMIC_setTxData2( LORAWAN_APP_PORT, LMIC.frame, LoRaWAN_data_size, LORAWAN_CONFIRMED_MSG_ON );
mluis 1:60184eda0066 436
mluis 1:60184eda0066 437 // Blink Tx LED
mluis 1:60184eda0066 438 debug_val( "LED1 = ", 1 );
mluis 1:60184eda0066 439 os_setTimedCallback( &txLedJob, os_getTime( ) + ms2osticks( 25 ), onTxLed );
mluis 0:a2929fa6e4f0 440 }
mluis 0:a2929fa6e4f0 441
mluis 1:60184eda0066 442 // Initialization job
mluis 1:60184eda0066 443 static void onInit( osjob_t* j )
mluis 1:60184eda0066 444 {
mluis 1:60184eda0066 445 // reset MAC state
mluis 1:60184eda0066 446 LMIC_reset( );
mluis 1:60184eda0066 447 LMIC_setAdrMode( LORAWAN_ADR_ON );
mluis 1:60184eda0066 448 LMIC_setDrTxpow( DR_SF12, 14 );
mluis 1:60184eda0066 449
mluis 1:60184eda0066 450 // start joining
mluis 1:60184eda0066 451 #if( OVER_THE_AIR_ACTIVATION != 0 )
mluis 1:60184eda0066 452 LMIC_startJoining( );
mluis 1:60184eda0066 453 #else
mluis 1:60184eda0066 454 LMIC_setSession( LORAWAN_NET_ID, LORAWAN_DEV_ADDR, NwkSKey, ArtSKey );
mluis 1:60184eda0066 455 onSendFrame( NULL );
mluis 1:60184eda0066 456 #endif
mluis 1:60184eda0066 457 // init done - onEvent( ) callback will be invoked...
mluis 1:60184eda0066 458 }
mluis 1:60184eda0066 459
mluis 1:60184eda0066 460 int main( void )
mluis 0:a2929fa6e4f0 461 {
pnysten 4:5e274bf85bf0 462 debug_init();
mluis 0:a2929fa6e4f0 463 osjob_t initjob;
pnysten 6:540c5d907c90 464
mluis 0:a2929fa6e4f0 465 // initialize runtime env
mluis 0:a2929fa6e4f0 466 os_init( );
mluis 0:a2929fa6e4f0 467 // setup initial job
mluis 0:a2929fa6e4f0 468 os_setCallback( &initjob, onInit );
mluis 0:a2929fa6e4f0 469 // execute scheduled jobs and events
mluis 0:a2929fa6e4f0 470 os_runloop( );
mluis 0:a2929fa6e4f0 471 // (not reached)
mluis 0:a2929fa6e4f0 472 }
mluis 0:a2929fa6e4f0 473
mluis 0:a2929fa6e4f0 474 //////////////////////////////////////////////////
mluis 0:a2929fa6e4f0 475 // LMIC EVENT CALLBACK
mluis 0:a2929fa6e4f0 476 //////////////////////////////////////////////////
mluis 0:a2929fa6e4f0 477 void onEvent( ev_t ev )
mluis 0:a2929fa6e4f0 478 {
mluis 0:a2929fa6e4f0 479 bool txOn = false;
mluis 1:60184eda0066 480 debug_event( ev );
mluis 0:a2929fa6e4f0 481
mluis 0:a2929fa6e4f0 482 switch( ev )
mluis 0:a2929fa6e4f0 483 {
mluis 0:a2929fa6e4f0 484 // network joined, session established
mluis 0:a2929fa6e4f0 485 case EV_JOINED:
mluis 1:60184eda0066 486 debug_val( "Net ID = ", LMIC.netid );
mluis 0:a2929fa6e4f0 487 txOn = true;
mluis 0:a2929fa6e4f0 488 break;
mluis 0:a2929fa6e4f0 489 // scheduled data sent (optionally data received)
mluis 0:a2929fa6e4f0 490 case EV_TXCOMPLETE:
mluis 1:60184eda0066 491 debug_val( "Datarate = ", LMIC.datarate );
mluis 0:a2929fa6e4f0 492 // Check if we have a downlink on either Rx1 or Rx2 windows
mluis 1:60184eda0066 493 if( ( LMIC.txrxFlags & ( TXRX_DNW1 | TXRX_DNW2 ) ) != 0 )
mluis 0:a2929fa6e4f0 494 {
mluis 1:60184eda0066 495 debug_val( "LED2 = ", 1 );
mluis 1:60184eda0066 496 os_setTimedCallback( &rxLedJob, os_getTime( ) + ms2osticks( 25 ), onRxLed );
mluis 0:a2929fa6e4f0 497
mluis 1:60184eda0066 498 if( LMIC.dataLen != 0 )
mluis 0:a2929fa6e4f0 499 { // data received in rx slot after tx
pnysten 4:5e274bf85bf0 500 //debug_buf( LMIC.frame + LMIC.dataBeg, LMIC.dataLen );
mluis 0:a2929fa6e4f0 501 processRxFrame( );
mluis 0:a2929fa6e4f0 502 }
mluis 0:a2929fa6e4f0 503 }
mluis 0:a2929fa6e4f0 504 txOn = true;
mluis 0:a2929fa6e4f0 505 break;
mluis 0:a2929fa6e4f0 506 default:
mluis 0:a2929fa6e4f0 507 break;
mluis 0:a2929fa6e4f0 508 }
mluis 0:a2929fa6e4f0 509 if( txOn == true )
mluis 0:a2929fa6e4f0 510 {
mluis 1:60184eda0066 511 //Sends frame every APP_TX_DUTYCYCLE +/- APP_TX_DUTYCYCLE_RND random time (if not duty cycle limited)
mluis 1:60184eda0066 512 os_setTimedCallback( &sendFrameJob,
mluis 1:60184eda0066 513 os_getTime( ) + ms2osticks( APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND ) ),
mluis 1:60184eda0066 514 onSendFrame );
mluis 0:a2929fa6e4f0 515
mluis 1:60184eda0066 516 ////Sends frame as soon as possible (duty cylce limitations)
mluis 1:60184eda0066 517 //onSendFrame( NULL );
mluis 0:a2929fa6e4f0 518 }
mluis 0:a2929fa6e4f0 519 }