arm studio build

Dependencies:   libxDot-mbed5

Files at this revision

API Documentation at this revision

Comitter:
alan1974
Date:
Fri Jan 18 18:15:29 2019 +0000
Parent:
13:1f3a8d0be511
Child:
15:ed77b752eaa9
Commit message:
upload of ABP credentials done

Changed in this revision

inc/commI2C.h Show annotated file Show diff for this revision Revisions of this file
inc/global.h Show annotated file Show diff for this revision Revisions of this file
inc/history.h Show annotated file Show diff for this revision Revisions of this file
inc/multicast.h Show annotated file Show diff for this revision Revisions of this file
src/main.cpp Show annotated file Show diff for this revision Revisions of this file
src/multicast.cpp Show annotated file Show diff for this revision Revisions of this file
src/wbit_util.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/inc/commI2C.h	Thu Jan 17 20:17:31 2019 +0000
+++ b/inc/commI2C.h	Fri Jan 18 18:15:29 2019 +0000
@@ -29,13 +29,14 @@
 enum{
    XDOT_CMD_XMIT_PKT = 1,   //transmit data
    XDOT_CMD_GET_EUI,        //get xdot EUI       
-   XDOT_CMD_SET_KEY_X,      //set one of the security keys
+   XDOT_CMD_SET_KEY_X,      //set one of the security keys -> not used ?
    XDOT_CMD_GOTO_SLEEP,     //go to sleep 
    XDOT_CMD_GATEWAY_PING,   //ping gateway for RSSI
    XDOT_CMD_SET_NTWKSESS,   //write/read network session to/from flash      
    XDOT_CMD_SET_RADIO,      //set's radio parameters   
    XDOT_CMD_NVM,            // xdot nvm memory 
    XDOT_CMD_NVM_ABP,        // xdot nvm for multicast ABP creds
+   XDOT_CMD_SET_RADIO_CLASS, // change radio class to A or C
    XDOT_CMD_UNDEFINED,      //last cmd, not used 
 };
 
@@ -131,8 +132,8 @@
   nvm       nvmData;
 }pkt_setnvmdwn;
 //--------------------------------------------------------------------------
-//nvm ptks for ABP creds
-
+//nvm ptks for ABP creds ---> REMOVE ???????????
+//--------------------------------------------------------------------------
 //upstream pkt struc
 typedef struct {  
   uint8_t   cmd;                        //cmd received from proc
@@ -149,6 +150,22 @@
   nvmABP    nvmData;
 }pkt_setnvmABPdwn;
 
+//--------------------------------------------------------------------------
+//ptks for class A/B radio change
+//--------------------------------------------------------------------------
+//upstream pkt struc
+typedef struct {  
+  uint8_t       cmd;                        //cmd received from proc
+  uint8_t       dataLen;                    //datalen found in pkt  
+  class_switch  classData;
+}pkt_setClassUp;
+//dwnstream pkt struc
+typedef struct {  
+  uint8_t   ack;                        //ack header response
+  uint8_t   cmd;                        //cmd received from proc    
+  uint8_t   dataLen;                    //datalen found in pkt  
+  bool      bSwitched;                  //1 = switched OK  
+}pkt_setClassDwn;
 
 
 
--- a/inc/global.h	Thu Jan 17 20:17:31 2019 +0000
+++ b/inc/global.h	Fri Jan 18 18:15:29 2019 +0000
@@ -37,13 +37,23 @@
   bool      bLogOutputOn;               //true if log output on
   uint8_t   chksum;                     //chk to make sure nvm memory ok
  }nvm;
-
-//struc to set nvm params for multicast ABP
+//NOT USED !!!!!!!!!! REMOVE
+//struc to set nvm params for multicast ABP -----> NOT USED ?????
 typedef struct {   
+  bool      bClassC;                    //true if switch from class C from A, else A from C
   uint8_t   key_nsk[16];                //ABP network seesion key
   uint8_t   key_aps[16];                //ABP application session key
-  uint8_t   devAdr[8];                  //device address
+  uint8_t   devAdr[4];                  //device address
   uint8_t   chksum;                     //chk to make sure nvm memory ok
  }nvmABP;
 
+typedef struct {   
+  bool      bClassC;                    //true if switch from class C from A, else A from C
+  uint8_t   key_nsk[16];                //ABP network seesion key
+  uint8_t   key_aps[16];                //ABP application session key
+  uint8_t   devAdr[4];                  //device address  
+ }class_switch;
+
+
+
 #endif
\ No newline at end of file
--- a/inc/history.h	Thu Jan 17 20:17:31 2019 +0000
+++ b/inc/history.h	Fri Jan 18 18:15:29 2019 +0000
@@ -4,7 +4,9 @@
                             - return downstream appPort after receiving rx pkt (does not work in 3.08)
                             - changed names of security keys to be same as used by Loriot to reduce confusion when using multicast ABP keys
                               (key_AppEUI,key_AppKey)
-                            - added uplink (from proc) and nvm storage of static ABP credentials for multicast  
+                            - uplink (from proc) and nvm storage of static ABP credentials for multicast  !!!!!!!!! remove?????
+                            - uplink (from proc) to change from class (A or C) with class C ADP credentials
+                            
 10Oct2018  0008    003      - added check for bad downlink frame counters due to spurious downlink cntr reset
                               either by Loriot or xdot bug. If  bad value detected return xdot err code                              
 26July2018 0007    003      - added new radio commands.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/inc/multicast.h	Fri Jan 18 18:15:29 2019 +0000
@@ -0,0 +1,42 @@
+#ifndef __MULTICAST_H__
+#define __MULTICAST_H__
+
+#include "mbed.h"
+#include "mDot.h"
+#include "ChannelPlans.h"
+#include "MTSLog.h"
+#include "MTSText.h"
+#include "global.h"
+
+//commands to xdot from loriot for multicast operations
+enum CMDS_MULTICAST{
+ MULTICAST_CMD_NORMAL             =0, //in normal,Class A state
+ MULTICAST_CMD_SPOOF              =1, //xmit fake data to loriot as MULTICAST NODE
+ MULTICAST_CMD_GOTOCLASSC         =2, //acknowledge cmd to go to class C, 
+ MULTICAST_CMD_GOTOCLASSA         =3, //revert back to classA
+ MULTICAST_CMD_SENDFRAGDATA       =4, //send info on last frag download
+
+ };  
+ 
+ //node device states
+enum NODE_DEVICE_STATES{
+  STATE_CLASSA              = 0,  // send data as normal device on port 1  
+  STATE_CLASSC              = 1,  
+  STATE_SPOOF               = 2,  //send data in spoof mode
+  STATE_MULTICAST_ACK_RCVD  = 3,  // class C cmd rcvd, sent ack
+  STATE_MULTICAST_ACK_SENT  = 4,  //  class C cmd ack sent
+  STATE_MULTICAST_FRAG_INFO  = 5, // send frag multicat info
+  
+  
+};
+static uint8_t port_device         = 1;      //normal device data port 
+static uint8_t port_multicast_cmd  = 210;    //port for multicast Rx command messages 
+static uint8_t port_multicast_data = 211;    //port for multicast Rx data messages 
+
+//void multicast_change_creds(bool bNodeX); 
+void save_OTAA_session_keys();
+bool multicast_change_class(class_switch *sClass);
+void multicast_cmd_process(std::vector<uint8_t> rx_packet);
+void multicast_data_process(std::vector<uint8_t> rx_packet);
+
+#endif
\ No newline at end of file
--- a/src/main.cpp	Thu Jan 17 20:17:31 2019 +0000
+++ b/src/main.cpp	Fri Jan 18 18:15:29 2019 +0000
@@ -3,6 +3,7 @@
 #include "commI2C.h"
 #include "dot_util.h"
 #include "wbit_util.h"
+#include "multicast.h"
 #include "mDot.h"
 #include "RadioEvent.h"
 #include "Lora.h"
@@ -304,11 +305,14 @@
                         if(!joined) {                           //if not previously joined, then need to join now
                             pAck->bJoined = 0;                 
                             if(verbose)pc.printf("\r\n----------- NETWORK NOT JOINED YET, WILL TRY TO JOIN %d TIMES\r\n",pUp->joinAttemps);                                                            
-                            joined = join_network_wbit(pUp->joinAttemps);
-                            pAck->joinAttempts = join_network_attempts_wbit();
-                            pAck->mdot_ret = dot->send(upstream_packet);
+                            joined = join_network_wbit(pUp->joinAttemps);      //try joinAttemps times to join network
+                            pAck->joinAttempts = join_network_attempts_wbit(); //# of join attemps made
+                            pAck->mdot_ret = dot->send(upstream_packet);  //<--------------------------------------- extra xmit ???????????????
                             if (!joined)
                                if(verbose)pc.printf("\r\n----------- FAILED TO JOIN...GIVING UP\r\n");  // join network if not joined                                                         
+                            else {
+                                save_OTAA_session_keys();
+                            }       
                         }         
                         if (joined)
                         {          
@@ -466,6 +470,24 @@
                         printNvmABPData(&pDwnNvmABP->nvmData);    
                                             
                         break;   
+
+                    case XDOT_CMD_SET_RADIO_CLASS:  //multicast_change_class A or C (change only after a join!)                      
+                        if(verbose)pc.printf("\n\r proc cmd: change radio class\r\n");                                  
+                        pkt_setClassUp  *pUpSetClass =  (pkt_setClassUp*)&buf_rcv[0];                
+                        pkt_setClassDwn *pDwnSetClass = (pkt_setClassDwn*)&buf_xmt[0];                                                 
+                        pDwnSetClass->ack = I2C_ACK_PROC;
+                        pDwnSetClass->cmd = XDOT_CMD_SET_RADIO_CLASS;
+                        pDwnSetClass->dataLen = sizeof(class_switch)-2;                        
+                        bool bOk = multicast_change_class(&pUpSetClass->classData);
+                        pDwnSetClass->bSwitched = bOk;                                                                                                               
+                        break;   
+                       
+
+
+
+
+
+
                         
                     case XDOT_CMD_SET_NTWKSESS:  //read or write network seesion to xdot flash
                         bool bWriteSession = (bool)buf_rcv[1];
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/multicast.cpp	Fri Jan 18 18:15:29 2019 +0000
@@ -0,0 +1,199 @@
+#include "mbed.h"
+#include "dot_util.h"
+#include "mDot.h"
+#include "RadioEvent.h"
+#include "ChannelPlans.h"
+#include "Lora.h"
+#include "multicast.h"
+
+ 
+ extern uint8_t nodeState;
+ //OTAA keys
+extern uint8_t app_eui[];
+extern uint8_t app_key[];
+
+//arrays to hold OTAA keys
+std::vector<uint8_t> netwrk_address_OTAA;
+std::vector<uint8_t> netwrk_session_key_OTAA;
+std::vector<uint8_t> data_session_key_OTAA;
+uint32_t downcounter_OTAA;
+uint32_t upcounter_OTAA;
+
+//local
+uint8_t frags_old_nmb = 0;
+uint8_t frags_missing_cnts = 0;   
+uint8_t frags_nmb_sending = 0;   //# of frags to be send
+uint8_t frags_size = 0;          //size of each frag to be sent
+uint32_t frag_missd = 0;
+//========================================================================================================
+//save_OTAA_session
+// save OTAA session parameters
+//========================================================================================================
+void save_OTAA_session_keys()
+{
+    netwrk_address_OTAA = dot->getNetworkAddress();
+    logInfo(" OTAA devAddr %s", mts::Text::bin2hexString(netwrk_address_OTAA).c_str());      
+    netwrk_session_key_OTAA = dot->getNetworkSessionKey();
+    logInfo(" OTAA network session key %s", mts::Text::bin2hexString(netwrk_session_key_OTAA).c_str());      
+    data_session_key_OTAA = dot->getDataSessionKey();
+    logInfo(" OTAA app session key %s", mts::Text::bin2hexString(data_session_key_OTAA).c_str());      
+}
+//========================================================================================================
+//change radio class to class C or class A
+// bClass_C:true => change class C credentials
+//         :false=> change class A credentials
+// sClass contains class C credentials
+// returns true if class change OK
+//========================================================================================================
+bool multicast_change_class(class_switch *sClass)
+{
+  if (sClass->bClassC)
+  {   
+    uint8_t joined = dot->getNetworkJoinStatus();   //are we joined to Lorawan?
+    if (!joined){
+         printf("\r\n cannot change radio class unless already joined");
+         return false;  //don't switch if not joined
+     }    
+    downcounter_OTAA = dot->getDownLinkCounter(); //save frame counters before switching
+    upcounter_OTAA = dot->getUpLinkCounter();
+    
+    dot->setDownLinkCounter(0);//do all nodes need to reset, else reject packts with high DownlinkCounter values?
+    
+//load ABP keys into arrays    
+    std::vector<uint8_t> dev_adr;
+    std::vector<uint8_t> nwky;
+    std::vector<uint8_t> dataky;
+    bool bKeyOk = true;
+    uint8_t indx;
+       
+//set ABP keys
+    for (indx =0; indx < sizeof(sClass->devAdr);indx++)      dev_adr.push_back(sClass->devAdr[indx]);   
+    for (indx =0; indx < sizeof(sClass->key_nsk);indx++)     nwky.push_back(sClass->key_nsk[indx]);         
+    for (indx =0; indx < sizeof(sClass->key_aps);indx++)     dataky.push_back(sClass->key_aps[indx]);              
+//set ABP keys
+    printf("\r\nsetting ABP network address %s\r\n", mts::Text::bin2hexString(dev_adr).c_str());    
+    if (dot->setNetworkAddress(dev_adr) != mDot::MDOT_OK){
+         printf(" failed to set key\r\n");
+         bKeyOk = false;
+    }     
+    printf("\r\nsetting ABP network session key %sr\n", mts::Text::bin2hexString(nwky).c_str());
+    if (dot->setNetworkSessionKey(nwky) != mDot::MDOT_OK){
+         printf(" failed to set key\r\n");
+         bKeyOk = false;
+    }         
+    printf("\r\nsettomg ABP data session key %sr\n", mts::Text::bin2hexString(dataky).c_str());
+    if (dot->setDataSessionKey(dataky) != mDot::MDOT_OK){
+         printf(" failed to set key\r\n");
+         bKeyOk = false;
+    }     
+    if (!bKeyOk) return false;
+            
+    if (dot->setClass("C") == mDot::MDOT_OK){
+          printf("\r\n set network mode to class C\r\n");
+          return true;
+    }      
+    else{
+         printf("\r\n  FAILED to set network mode to class C\r\n");
+         return false;
+    }        
+ } //if Class C
+ else{   //change back to OTAA
+    printf("\r\nchanging network address to OTAA device address %s\r\n", mts::Text::bin2hexString(netwrk_address_OTAA).c_str());
+    if (dot->setNetworkAddress(netwrk_address_OTAA) != mDot::MDOT_OK) {
+    printf("\r\nfailed to set network device address to %s", mts::Text::bin2hexString(netwrk_address_OTAA).c_str());
+    } 
+    printf("\r\nchanging network session key  to OTAA device key %s", mts::Text::bin2hexString(netwrk_session_key_OTAA).c_str());
+    if (dot->setNetworkSessionKey(netwrk_session_key_OTAA) != mDot::MDOT_OK) {
+            printf("\r\nfailed to set network session key to %s", mts::Text::bin2hexString(netwrk_session_key_OTAA).c_str());
+    }      
+    printf("\r\nchanging data session key to OTAA device data key %s", mts::Text::bin2hexString(data_session_key_OTAA).c_str());
+    if (dot->setDataSessionKey(data_session_key_OTAA) != mDot::MDOT_OK) {
+        printf("\r\nfailed to set data session key to %s", mts::Text::bin2hexString(data_session_key_OTAA).c_str());
+     }
+     dot->setDownLinkCounter(downcounter_OTAA); //restore frame counters before switching
+     dot->setUpLinkCounter(upcounter_OTAA);
+     
+     if (dot->setClass("A") == mDot::MDOT_OK){
+          printf("\r\nset network mode to class A");
+          return true;
+    }      
+    else{
+         printf("\r\n FAILED set network mode to class A");
+         return false;
+    }     
+
+
+ } 
+        
+}
+/*
+//========================================================================================================
+//multicast_cmd_process()            
+//process a rx incoming multicast packet
+//========================================================================================================
+void multicast_cmd_process(std::vector<uint8_t> rx_packet){            
+   uint8_t i;                                
+   uint8_t rxLen = rx_packet.size();     
+   printf("\r\n**********************************************");      
+   printf("\r\nmulticast cmd len, %d, data: ",rxLen);         
+   for (i=0; i< rxLen;i++)printf(" %x",rx_packet[i]);            
+   printf("\r\n");                                                                                                  
+
+   uint8_t mCmd = rx_packet[0];
+   switch (mCmd){
+    case MULTICAST_CMD_SPOOF:
+        printf("\r\nmulticast to change to spoof mode: cmd#: %d",mCmd);      
+        multicast_change_creds(true);  //change credentials to NodeX 
+        nodeState = STATE_SPOOF;             
+        break;
+    case MULTICAST_CMD_GOTOCLASSC:
+        nodeState = STATE_MULTICAST_ACK_RCVD;             
+        frags_missing_cnts = 0;
+        printf("\r\nmulticast to change to class c: cmd#: %d",mCmd);      
+        frags_nmb_sending = rx_packet[1];   //# of frags to be sent
+        frags_size = rx_packet[2];          //size of each frag to be sent 
+        frag_missd = 0xffffffff;            //preload to all frags missed
+        printf(" frags2BeRcvd: %d frag_size(bytes): %d",frags_nmb_sending,frags_size);            
+        break;             
+     case MULTICAST_CMD_GOTOCLASSA:
+        printf("\r\nmulticast to change back to class A: cmd#: %d",mCmd);      
+        multicast_change_creds(false);  //change credentials to OTAA
+        nodeState = STATE_CLASSA;             
+        break;  
+      case MULTICAST_CMD_SENDFRAGDATA:  //send the frag multicast info status
+        printf("\r\nmulticast send frag data: cmd#: %d",mCmd);      
+        nodeState = STATE_MULTICAST_FRAG_INFO;             
+        break;
+      
+   } //switch
+   printf("\r\n **********************************************\r\n");      
+}
+//========================================================================================================
+//multicast_cmd_process()            
+//process a rx incoming multicast packet
+//missing frags are a bit location in uint32_t frag_missd; a bit location = 1 => missing frag
+//========================================================================================================
+void multicast_data_process(std::vector<uint8_t> rx_packet){            
+   uint8_t i;                                
+   uint8_t rxLen = rx_packet.size();     
+   uint8_t frag_nmb = rx_packet[0];
+   printf("\r\n**********************************************");   
+   printf("\r\n frag nmb: %d",frag_nmb);             
+   frag_missd = frag_missd &~(1UL << (frag_nmb-1));
+   printf(" missdd frag bit pattern: %x,",frag_missd);         
+   if (frag_nmb > frags_old_nmb+1) frags_missing_cnts++;
+   
+   printf("\n\rmulticast data rcvd, len: %d,missing frag cnt: %d ",rxLen,frags_missing_cnts);         
+   printf("\r\ndata: ");            
+   for (i=1; i< rxLen;i++)printf(" %x",rx_packet[i]);            
+   printf("\r\n");                                                                                                    
+   printf("\r\n**********************************************\r\n");      
+   frags_old_nmb = frag_nmb;
+   
+   if (frag_nmb == frags_nmb_sending){
+        printf("last frag rcvd, changing to classA");
+        multicast_change_creds(false);  //change credentials to OTAA
+        nodeState = STATE_CLASSA;       
+   }
+}
+*/
\ No newline at end of file
--- a/src/wbit_util.cpp	Thu Jan 17 20:17:31 2019 +0000
+++ b/src/wbit_util.cpp	Fri Jan 18 18:15:29 2019 +0000
@@ -15,9 +15,9 @@
 uint8_t key_AppKey[] = { 0x0F, 0xF9, 0xA2, 0x90, 0x2E, 0xAA, 0x6B, 0x8C, 0x6A, 0x4E, 0xFD, 0x67, 0xF9, 0xA6, 0xF3, 0xD3 };   ////OTAA appkey    => loriot "Appkey" -> common to all nodes    
 
 //ADP keys: for xdot spoofer eui ---> need to be uploaded from proc, using EUI: #spoofer (EUI 00-80-00-00-04-00-FF-FF) 
-static uint8_t network_address[] =  {0x01,0x59,0xE2,0xC7};
-static uint8_t network_session_key[] = {0xBA, 0xEF, 0x4D, 0xA3, 0x66, 0xD1, 0x85, 0xDE, 0xB1, 0xCD, 0x79, 0x15, 0xC7, 0xEB, 0x72, 0x35};
-static uint8_t data_session_key[] = {0xF9,0x13,0x94,0x0D,0x12,0x95,0x96,0x5A,0xF4,0x34,0xBB,0xA9,0x8E,0x2A,0x83,0x10};
+//static uint8_t network_address[] =  {0x01,0x59,0xE2,0xC7};
+//static uint8_t network_session_key[] = {0xBA, 0xEF, 0x4D, 0xA3, 0x66, 0xD1, 0x85, 0xDE, 0xB1, 0xCD, 0x79, 0x15, 0xC7, 0xEB, 0x72, 0x35};
+//static uint8_t data_session_key[] = {0xF9,0x13,0x94,0x0D,0x12,0x95,0x96,0x5A,0xF4,0x34,0xBB,0xA9,0x8E,0x2A,0x83,0x10};
 
 
 //============================================================================
@@ -215,12 +215,12 @@
 }    
 
 bool join_network_wbit(uint8_t nmbAttempts) {
-    //int32_t j_attempts = 0;
     j_attempts = 0;
     int32_t ret = mDot::MDOT_ERROR;
     
 // attempt to join the network
-    while (ret != mDot::MDOT_OK) {
+    while (ret != mDot::MDOT_OK)
+    {
         j_attempts++;
         logInfo("attempt %d to join network",j_attempts);
         ret = dot->joinNetwork();