arm studio build

Dependencies:   libxDot-mbed5

Revision:
14:fc836a5a5d2f
Child:
17:74d60177c6b6
--- /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