LMIC lib Lora OTA device for Everynet

Fork of LMiC-10secs by Alcatel-Lucent IoT Development

Revision:
10:8f33a411b53f
Parent:
9:c5820ce68bd6
--- a/lmic.cpp	Tue Feb 23 08:41:36 2016 +0000
+++ b/lmic.cpp	Fri Sep 23 09:15:43 2016 +0000
@@ -1052,6 +1052,7 @@
     //debug_val("olen: ", olen);
 
     if( addr != LMIC.devaddr ) {
+        debug_str("Device Address not correct!");
         EV(specCond, WARN, (e_.reason = EV::specCond_t::ALIEN_ADDRESS,
                             e_.eui    = MAIN::CDEV->getEui(),
                             e_.info   = addr,
@@ -1097,6 +1098,7 @@
         }
         // Replay of previous sequence number allowed only if
         // previous frame and repeated both requested confirmation
+        debug_str("Replay Conf\n\r");
         replayConf = 1;
     }
     else {
@@ -1289,7 +1291,7 @@
         
         //if( port >= 0  &&  pend-poff > 0 )
             //aes_cipher(port <= 0 ? LMIC.nwkKey : LMIC.artKey, LMIC.devaddr, seqno, /*dn*/1, d+poff, pend-poff);
-
+        debug_str("HANDLING PAYLOAD...\r\n");
         if( port >= 0  && port != 1 &&  pend-poff > 0 )
         {
             debug_str("Payload decrypting...\r\n");
@@ -1622,7 +1624,11 @@
         goto badframe;
     }
 
-    u4_t addr = os_rlsbf4(LMIC.frame+OFF_JA_DEVADDR);
+    //u4_t addr = os_rlsbf4(LMIC.frame+OFF_JA_DEVADDR);
+    debug_str( "Join accept = ");
+    debug_buf(LMIC.frame, dlen);
+    u4_t addr = os_rlsbf4(&LMIC.frame[OFF_JA_DEVADDR]);
+    debug_val( "Dev Addr = ", addr );
     LMIC.devaddr = addr;
     LMIC.netid = os_rlsbf4(&LMIC.frame[OFF_JA_NETID]) & 0xFFFFFF;
 
@@ -1855,12 +1861,20 @@
         }
         LMIC.frame[end] = LMIC.pendTxPort;
         os_copyMem(LMIC.frame+end+1, LMIC.pendTxData, dlen);
+
+//        debug_str("Data frame before encrypting: ");  
+        debug_buf(LMIC.frame, dlen);
+//        debug_str("Encrypting data frame\r\n");
         aes_cipher(LMIC.pendTxPort==0 ? LMIC.nwkKey : LMIC.artKey,
                    LMIC.devaddr, LMIC.seqnoUp-1,
                    /*up*/0, LMIC.frame+end+1, dlen);
+//        debug_str("Encrypted data frame: ");  
+        debug_buf(LMIC.frame, dlen);         
     }
     aes_appendMic(LMIC.nwkKey, LMIC.devaddr, LMIC.seqnoUp-1, /*up*/0, LMIC.frame, flen-4);
-
+//    debug_str("Frame to be sent: ");  
+    debug_buf(LMIC.frame, dlen);         
+ 
     EV(dfinfo, DEBUG, (e_.deveui  = MAIN::CDEV->getEui(),
                        e_.devaddr = LMIC.devaddr,
                        e_.seqno   = LMIC.seqnoUp-1,
@@ -2009,7 +2023,7 @@
 // ================================================================================
 
 static void processPingRx (xref2osjob_t osjob) {
-    //debug_str("processPingRx\r\n");
+    debug_str("processPingRx\r\n");
     if( LMIC.dataLen != 0 ) {
         LMIC.txrxFlags = TXRX_PING;
         if( decodeFrame() ) {
@@ -2023,7 +2037,7 @@
 
 
 static bit_t processDnData (void) {
-    //debug_str("processDnData\r\n");
+    debug_str("processDnData\r\n");
     ASSERT((LMIC.opmode & OP_TXRXPEND)!=0);
 
     if( LMIC.dataLen == 0 ) {
@@ -2082,7 +2096,9 @@
         }
         return 1;
     }
+    debug_str("processDnData - decodeFrame\r\n");
     if( !decodeFrame() ) {
+        debug_str("decode Frame deconne\r\n");
         if( (LMIC.txrxFlags & TXRX_DNW1) != 0 )
             return 0;
         goto norx;
@@ -2223,7 +2239,7 @@
             goto checkrx;
         }
         // Earliest possible time vs overhead to setup radio
-        /*PANYif( txbeg - (now + TX_RAMPUP) < 0 ) {PANY*/
+        if( txbeg - (now + TX_RAMPUP) < 0 ) {
             // We could send right now!
             debug_str("We could send right now!"); 
             debug_char( '\r' );
@@ -2270,7 +2286,7 @@
             updateTx(txbeg);
             os_radio(RADIO_TX);
             return;
-        /*PANY}PANY*/
+        }
         // Cannot yet TX
         if( (LMIC.opmode & OP_TRACK) == 0 )
         {
@@ -2425,7 +2441,7 @@
 
 
 //
-int LMIC_setTxData2 (u1_t port, xref2u1_t data, u1_t dlen, u1_t confirmed) {
+int LMIC_setTxData2 (u1_t port, xref2u1_t data, u1_t dlen, u1_t confirmed, int encrypted) {
     //debug_str("LMIC_setTxData2\r\n");
 /*    debug_val("Data Length: ", dlen);
     debug_val("Data Length: ", SIZEOFEXPR(LMIC.pendTxData));*/
@@ -2433,12 +2449,27 @@
         return -2;
     if( data != (xref2u1_t)0 )
     {
-        os_copyMem(LMIC.pendTxData, data, dlen);
+     if (encrypted == 1) {
+        debug_str("ENCRYPTING PAYLOAD\n\r");
+        debug_val("COUNTER UP: ", LMIC.seqnoUp);
+        debug_str("Network Key: ");
+        debug_buf(LMIC.nwkKey, 16);
+        debug_val("Device Address: ", LMIC.devaddr);
+        debug_str("Data before encryption: ");
+        debug_buf(data, dlen);
+        aes_cipher(LMIC.nwkKey, LMIC.devaddr, LMIC.seqnoUp, 0, data, dlen);
+        debug_str("Data after encryption: ");
+        debug_buf(data, dlen);
+     }
+       os_copyMem(LMIC.pendTxData, data, dlen);
     }
     //debug_str("Send Data\r\n");
     LMIC.pendTxConf = confirmed;
     LMIC.pendTxPort = port;
     LMIC.pendTxLen  = dlen;
+ /*   aes_cipher(LMIC.pendTxPort==0 ? LMIC.nwkKey : LMIC.artKey,
+                   LMIC.devaddr, LMIC.seqnoUp-1,
+                   0, LMIC.frame+end+1, dlen);*/
     LMIC_setTxData();
     return 0;
 }