Adapted to LoRa Semtech + Nucleo

Dependencies:   LMiC SX1276Lib cantcoap lwip mbed-rtos mbed

Fork of LoRaWAN-lmic-app by Pascal Nysten

Revision:
6:540c5d907c90
Parent:
4:5e274bf85bf0
Child:
7:f1521b0ecf08
--- a/main.cpp	Mon Nov 23 09:54:36 2015 +0000
+++ b/main.cpp	Mon Dec 14 10:47:06 2015 +0000
@@ -12,11 +12,13 @@
 
 Maintainer: Miguel Luis and Gregory Cristian
 */
+
 #include <cstdio>
 #include <string>
 #include <cassert>
 
 #include "mbed.h"
+//#include "Node.h"
 #include "cantcoap.h"
 
 #include "lmic.h"
@@ -26,20 +28,20 @@
 const std::string ENDPOINT_SEGMENT     = "?ep=";
 const std::string LIFETIME             ="&lt=";
 const std::string BINDING              ="&b=";
-
+ 
 const std::string REGISTRATION_OPEN  =    "<";
 const std::string REGISTRATION_CLOSE =    ">";
 const std::string REGISTRATION_SEPARATOR ="/";
-
+ 
 int _node_Id=0;
-
+ 
 const std::string endPoint_Name = "loraDevice"; 
 const int lifeTime = 300;
 const std::string binding = "U";
-
+ 
 unsigned char * _payload;
 long _payload_size;
-
+ 
 /*!
  * When set to 1 the application uses the Over-the-Air activation procedure
  * When set to 0 the application uses the Personalization activation procedure
@@ -57,6 +59,7 @@
  * Defines the device address when using personalization activation procedure
  */
 #define LORAWAN_DEV_ADDR                            ( uint32_t )0x12345678
+//#define LORAWAN_DEV_ADDR                            ( uint32_t )0x12341111
 
 #endif
 
@@ -94,7 +97,9 @@
 
 #define UINT16_MAX (65535U)
 #define UINT64_MAX (18446744073709551615ULL)
-
+ 
+//Node lwm2mNode("LR-test0");;
+unsigned int LoRaWAN_data_size = 0; 
 
 std::string to_string( int x ) {
   int length = snprintf( NULL, 0, "%d", x );
@@ -106,11 +111,11 @@
   return str;
 }
 unsigned char * get_Registration_Payload(long *payload_size){
-
+ 
     string registration_Payload ="";
-
+ 
     string s="";
-
+ 
     s.append(REGISTRATION_OPEN);
     s.append(REGISTRATION_SEPARATOR);
     s.append("3/0/0");
@@ -125,7 +130,7 @@
     s.append(REGISTRATION_SEPARATOR);
     s.append("3/0/2");
     s.append(REGISTRATION_CLOSE);
-
+ 
     registration_Payload.append(s);
     
     unsigned char *c = new unsigned char[registration_Payload.size()+1];
@@ -134,7 +139,7 @@
     *payload_size=registration_Payload.size();
     
     return c;
-
+ 
 }
 uint8_t * get_Token(int * size){
     srand(time(0)+_node_Id);
@@ -159,7 +164,7 @@
     }
     return token;
 }
-
+ 
 uint16_t get_Message_ID(){
     srand(time(0)+_node_Id);
     int test=0;
@@ -175,7 +180,7 @@
     return (uint16_t) test;
     
 }
-
+ 
 char * get_Registration_Query(){
     
     string buffer;
@@ -192,9 +197,9 @@
     c[buffer.size()]='\0';
     return c;
     
-
+ 
 }
-
+ 
 //////////////////////////////////////////////////
 // CONFIGURATION (FOR APPLICATION CALLBACKS BELOW)
 //////////////////////////////////////////////////
@@ -203,12 +208,18 @@
 static const uint8_t AppEui[8] =
 {
     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
+//    0x01, 0x02, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00
 };
 
 // unique device ID (LSBF)
 static const u1_t DevEui[8] =
 {
-    0x00, 0x00, 0x00, 0x00, 0x08, 0x06, 0x02, 0x48
+//    0x00, 0x00, 0x00, 0x00, 0x08, 0x06, 0x02, 0x48
+
+    0x30, 0x74, 0x73, 0x65, 0x74, 0x2D, 0x52, 0x4C // 4c522d7465737430 = "LR-test0"
+//    0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x99, 0xF7
+
+
 //    0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF
 //    0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x099, 0xF7
 };
@@ -216,6 +227,8 @@
 // device-specific AES key (derived from device EUI)
 static const uint8_t DevKey[16] = 
 {
+//    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+//    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
     0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
     0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
 };
@@ -226,6 +239,7 @@
 { 
     0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
     0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
+//    0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3D
 };
 
 // application session key
@@ -233,6 +247,7 @@
 { 
     0x2B, 0x7E, 0x15, 0x16, 0x28, 0xAE, 0xD2, 0xA6,
     0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3C
+//    0xAB, 0xF7, 0x15, 0x88, 0x09, 0xCF, 0x4F, 0x3D
 };
 
 #endif
@@ -296,6 +311,40 @@
     debug_val("LED1 = ", 0 );
 }
 
+static void prepareTxCoapFrame( void )
+{
+    // Create Registration PDU :
+    
+   CoapPDU *pdu = new CoapPDU();
+    
+    pdu->setCode(CoapPDU::COAP_POST);
+    pdu->setType(CoapPDU::COAP_CONFIRMABLE);
+    int size;
+    uint8_t * token = get_Token(&size);
+    pdu->setToken(token,size);
+    pdu->setMessageID(get_Message_ID());
+    pdu->setURI(get_Registration_Query());
+    
+    _payload=get_Registration_Payload(&_payload_size);
+    pdu->setPayload(_payload, (int) _payload_size);
+    int PDUlength = pdu->getPDULength();
+    
+//    strncpy((char*) LMIC.frame, (const char*)pdu->getPDUPointer(), PDUlength);
+    memcpy(LMIC.frame, pdu->getPDUPointer(), PDUlength * sizeof(uint8_t));
+
+#if ( LORAWAN_CONFIRMED_MSG_ON == 1 )
+    LMIC.frame[PDUlength] = LMIC.seqnoDn >> 8;
+    LMIC.frame[PDUlength+1] = LMIC.seqnoDn;
+    LMIC.frame[PDUlength+2] = LMIC.rssi >> 8;
+    LMIC.frame[PDUlength+3] = LMIC.rssi;
+    LMIC.frame[PDUlength+4] = LMIC.snr;
+#endif
+    debug_str("Frame to be sent: ");
+    debug_buf(LMIC.frame, PDUlength + 5);
+    
+    LoRaWAN_data_size = PDUlength + 5;
+}
+
 static void prepareTxFrame( void )
 {
     LMIC.frame[0] = AppLedStateOn;
@@ -306,23 +355,16 @@
     LMIC.frame[4] = LMIC.rssi;
     LMIC.frame[5] = LMIC.snr;
 #endif    
-/*    debug_str("Frame to be sent: ");
-    debug_val("1: ", LMIC.frame[0]);
-    debug_val("2: ", LMIC.frame[1]);
-    debug_val("3: ", LMIC.frame[2]);
-    debug_val("4: ", LMIC.frame[3]);
-    debug_val("5: ", LMIC.frame[4]);
-    debug_val("6: ", LMIC.frame[5]);
-    debug_str("\r\n");*/
+    debug_str("Frame to be sent: ");
+    debug_buf(LMIC.frame, LORAWAN_APP_DATA_SIZE);
+    
+    LoRaWAN_data_size = LORAWAN_APP_DATA_SIZE;
 }
 
 void processRxFrame( void )
 {
     debug_str("Data: ");
     debug_buf( LMIC.frame + LMIC.dataBeg, LMIC.dataLen );
-/*    debug_str("Data - command: ");
-    debug_hex(LMIC.frame[0]);
-    debug_str("\r\n");*/
 
     switch( LMIC.frame[LMIC.dataBeg - 1] ) // Check Rx port number
     {
@@ -348,45 +390,10 @@
 
 static void onSendFrame( osjob_t* j )
 {
-    prepareTxFrame( );
+    //prepareTxFrame( );
+    prepareTxCoapFrame();
 
-    // Create Registration PDU :
-    
-    CoapPDU *pdu = new CoapPDU();
-    
-    pdu->setCode(CoapPDU::COAP_POST);
-    pdu->setType(CoapPDU::COAP_CONFIRMABLE);
-    int size;
-    uint8_t * token = get_Token(&size);
-    pdu->setToken(token,size);
-    pdu->setMessageID(get_Message_ID());
-    pdu->setURI(get_Registration_Query());
-    
-    _payload=get_Registration_Payload(&_payload_size);
-    pdu->setPayload(_payload, (int) _payload_size);
-    
-    int PDUlength = pdu->getPDULength();
-    u1_t frame[PDUlength+6];
-    
-    memcpy(frame, pdu->getPDUPointer(), PDUlength * sizeof(uint8_t));
-
-    frame[PDUlength] = LMIC.seqnoDn >> 8;
-    frame[PDUlength+1] = LMIC.seqnoDn;
-    frame[PDUlength+2] = LMIC.rssi >> 8;
-    frame[PDUlength+3] = LMIC.rssi;
-    frame[PDUlength+4] = LMIC.snr;
-    frame[PDUlength+5] = '\0';
-    /* debug_str("Frame: ");
-    debug_str(pdu->getPDUPointer());
-    debug_str("  <STOP>\r\n");
-
-    debug_str("Frame: ");
-    debug_str(frame);
-    debug_str("  <STOP>\r\n");
-    debug_val("Frame Length: ", PDUlength+5);*/
-    
-    //LMIC_setTxData2( LORAWAN_APP_PORT, frame, PDUlength+6, LORAWAN_CONFIRMED_MSG_ON );
-    LMIC_setTxData2( LORAWAN_APP_PORT, LMIC.frame, LORAWAN_APP_DATA_SIZE, LORAWAN_CONFIRMED_MSG_ON );
+    LMIC_setTxData2( LORAWAN_APP_PORT, LMIC.frame, LoRaWAN_data_size, LORAWAN_CONFIRMED_MSG_ON );
 
     // Blink Tx LED
     debug_val( "LED1 = ", 1 );
@@ -415,7 +422,7 @@
 {
     debug_init();
     osjob_t initjob;
-
+    
     // initialize runtime env
     os_init( );
     // setup initial job