arm studio build

Dependencies:   libxDot-mbed5

Revision:
1:0d25d9ddbe9f
Parent:
0:a91cd1b08360
Child:
2:0af50f386eb2
--- a/src/main.cpp	Mon Jun 18 17:31:35 2018 +0000
+++ b/src/main.cpp	Sat Aug 04 19:25:02 2018 +0000
@@ -1,4 +1,5 @@
 #include "mbed.h"
+#include "global.h"
 #include "commI2C.h"
 #include "dot_util.h"
 #include "wbit_util.h"
@@ -6,6 +7,10 @@
 #include "RadioEvent.h"
 #include "Lora.h"
 
+//nvm storage params
+nvm nvmData;
+nvm *pNvmData = &nvmData;
+
 //=======================================================================================================
 // enable some GPIO for scope trigger or led 
 //=======================================================================================================
@@ -30,24 +35,30 @@
 // * either the network name and passphrase can be used or //
 //     the network ID (8 bytes) and KEY (16 bytes)         //
 //=======================================================================================================
-static std::string network_name = "asdfqwer";
-static std::string network_passphrase = "zxcvasdf";
-
-static uint8_t network_id[] = { 0x90, 0xF1, 0x47, 0x90, 0x6C, 0x48, 0x1D, 0x29 };   //static id not used anymore but don't comment out                                
-static uint8_t network_key[] = { 0x0F, 0xF9, 0xA2, 0x90, 0x2E, 0xAA, 0x6B, 0x8C, 0x6A, 0x4E, 0xFD, 0x67, 0xF9, 0xA6, 0xF3, 0xD3 };                                 
-
+static std::string network_name = "asdfqwer";  //not used
+static std::string network_passphrase = "zxcvasdf"; //not used
+/*
+uint8_t network_id[] = { 0x90, 0xF1, 0x47, 0x90, 0x6C, 0x48, 0x1D, 0x29 };   //static id not used anymore but don't comment out                                
+//OTAA keys
+uint8_t network_key[] = { 0x0F, 0xF9, 0xA2, 0x90, 0x2E, 0xAA, 0x6B, 0x8C, 0x6A, 0x4E, 0xFD, 0x67, 0xF9, 0xA6, 0xF3, 0xD3 };   //appkey                              
+*/
 
 #ifdef MT_PRIVATE_NETWORK
 static uint8_t frequency_sub_band = 4; 
 static bool public_network = false;
 static uint8_t ack = 1; //0;
 #else
-static uint8_t frequency_sub_band = 1; 
-static bool public_network = true; //false
-static uint8_t ack = 1; //0;
+uint8_t frequency_sub_band = 1; 
+bool public_network = true; //false
+uint8_t ack = 1; //0;
+uint8_t link_check_treshold = 100; //5;
 #endif
 
-static bool adr = false;    //set adaptive data rate
+bool adr = false;    //set adaptive data rate
+
+
+
+
 
 //=======================================================================================================
 // deepsleep consumes slightly less current than sleep
@@ -146,20 +157,23 @@
 // in OTA and AUTO_OTA join modes, the credentials can be passed to the library as a name and passphrase or an ID and KEY
 // only one method or the other should be used!
     if (public_network){
-        update_ota_config_id_key(network_id, network_key, frequency_sub_band, public_network, ack);
+   //     update_ota_config_id_key(network_id, network_key, frequency_sub_band, public_network, ack);
+          update_ota_config_id_key(nvmData.network_id,nvmData.network_key, frequency_sub_band, public_network, ack);
+    
+   
+   
         logInfo("--------------  network configured for public access -----------------------------");
     }    
     else{
         update_ota_config_name_phrase(network_name, network_passphrase, frequency_sub_band, public_network, ack);
         logInfo("--------------  network configured for private access -------------------------------");
-    }        
-        
-// configure network link checks
-// network link checks are a good alternative to requiring the gateway to ACK every packet and should allow a single gateway to handle more Dots
-// check the link every count packets
-// declare the Dot disconnected after threshold failed link checks
-// for count = 3 and threshold = 5, the Dot will ask for a link check response every 5 packets and will consider the connection lost if it fails to receive 3 responses in a row
-        update_network_link_check_config(3, 5);   
+    }               
+// configure network link check count
+// declares the Dot disconnected if no acks received within link_check_treshold transmits
+    //update_network_link_check_config(3, 5);
+    dot->setLinkCheckThreshold(link_check_treshold);
+
+
         
 /*  done thru radio cmds        
 // save changes to configuration
@@ -179,6 +193,7 @@
 // (note the calls to wait below for delays)
 //=================================================================================================
 int main() {
+    uint8_t i;
     RadioEvent events;
     
     mDotEvent  mdotevent;   //used to get ping info????
@@ -193,20 +208,40 @@
     pc.printf("\r\n XDOT BOOT\r\n");
     pc.printf("COMM api_level = <HEX> %x.%x.%x.%x\r\n",api_level[0],api_level[1],api_level[2],api_level[3]);
     pc.printf("COMM version   =       %x.%x.%x.%x\r\n",ver_level[0],ver_level[1],ver_level[2],ver_level[3]);
-    pc.printf("**********************************************************\r\n"); 
-//    LED_test(2);             
+    pc.printf("**********************************************************\r\n");   
     assert(plan);
     dot = mDot::getInstance(plan);
-    assert(dot);
-    
-    //static bool deploy_mode = true;        
-    //dot->setLogLevel((deploy_mode) ? mts::MTSLog::INFO_LEVEL : mts::MTSLog::TRACE_LEVEL);  // TRACE_LEVEL        , INFO_LEVEL    
-    dot->setLogLevel((true) ? mts::MTSLog::TRACE_LEVEL : mts::MTSLog::TRACE_LEVEL);  // TRACE_LEVEL        , INFO_LEVEL       
+    assert(dot);  
+      
+    //dot->setLogLevel((true) ? mts::MTSLog::TRACE_LEVEL : mts::MTSLog::TRACE_LEVEL);  // TRACE_LEVEL        , INFO_LEVEL       
+    bool bLogOutput = true;
+    dot->setLogLevel((bLogOutput) ? mts::MTSLog::TRACE_LEVEL : mts::MTSLog::TRACE_LEVEL);  // TRACE_LEVEL        , INFO_LEVEL       
     dot->setEvents(&events); 
- 
-//new !!    
-        // make sure library logging is turned on
-        //dot->setLogLevel(mts::MTSLog::TRACE_LEVEL);  
+
+//nvm nvmData;
+//nvm *pNvmData = &nvmData;
+
+   
+    nvmRead(pNvmData);
+/*
+   pc.printf("****NON-VOLATILE MEMORY TEST*********\r\n");   
+  
+   
+    for (i = 0; i < sizeof(network_id);i++){
+        nvmData.network_id[i] = network_id[i];   
+    }    
+    for (i = 0; i < sizeof(network_key);i++){
+        nvmData.network_key[i] = network_key[i];   
+    }
+
+   uint8_t databytesread[128];
+   dot->nvmWrite(0,pData,128);
+   dot->nvmRead(0, databytesread,128);
+   for (i = 0; i < 64; i++)
+      pc.printf("databyte %d:  %x\r\n",i,databytesread[i]);   
+      
+   pc.printf("****NON-VOLATILE MEMORY TEST*********\r\n");   
+*/  
 
 
 // getStandbyFlag() should return the state of the standby flag directly from the processor
@@ -214,10 +249,10 @@
 //  0: Device has not been in Standby mode
 //  1: Device has been in Standby mode
 //  The xDot should enter standby mode when deep sleep in invoked. So you should see the standby flag set if it came out of deep sleep.
-    if (!dot->getStandbyFlag()) {  //if 0 the power-up/reset which should always be the case at this point
+    if (!dot->getStandbyFlag()) {  //if 0 => power-up/reset which should always be the case at this point
         logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);
 
-        pc.printf("\r\n saved configuration: ");
+        pc.printf("\r\n saved configuration: ");        
         frequency_sub_band = dot->getFrequencySubBand();        
         pc.printf("\r\n subband: %d ",frequency_sub_band);                 
         pc.printf("\r\n ADR: %d ",dot->getAdr());                 
@@ -227,8 +262,9 @@
         pc.printf("\r\n transmit pwr: %d ",dot->getTxPower());                         
         pc.printf("\r\n TxDataRate: %d ",dot->getTxDataRate());                        
         pc.printf("\r\n maxPktLen: %d ",dot->getMaxPacketLength());                                
-        pc.printf("\r\n port nmb: %d ",dot->getAppPort());                         
-        
+//        pc.printf("\r\n port nmb: %d ",dot->getAppPort());                         
+        pc.printf("\r\n link check count: %d ",dot->getLinkCheckThreshold());   
+        pc.printf("\r\n");   
         cfg_network(true,public_network,frequency_sub_band);  //force network cfg,              
         
     } else {
@@ -238,37 +274,25 @@
         dot->restoreNetworkSession();
     }
 //--------------------------------------------------------------------------------------------------------------------------------------------
-// configure network link checks
-// network link checks are a good alternative to requiring the gateway to ACK every packet and should allow a single gateway to handle more Dots
-//asb: not sure this is correct
-// check the link every count packets
-// declare the Dot disconnected after threshold failed link checks
-// for count = 3 and threshold = 5, the Dot will be considered disconnected after 15 missed packets in a row
-// asb: i think this is correct:
-//       : see mdot.h -> setLinkCheckCount(const uint8_t& count); 
-//       : LinkCounts only used if ACKs disabled
-//       only LinkCountThreshold is used => rejoin after LinkCountThreshold acks have been missed
-    update_network_link_check_config(3, 5);
+// configure network link check count
+// declares the Dot disconnected if no acks received within link_check_treshold transmits
+    //update_network_link_check_config(3, 5);
+    dot->setLinkCheckThreshold(link_check_treshold);
 //----------------------------------------------------------------------------------------------------------------------------------------------
 
 // save changes to configuration
 //    logInfo("saving configuration");
   
-  eui = mts::Text::bin2hexString(dot->getDeviceId()).c_str();
-  pc.printf("\r\nEUI: %s",eui);
-       
-    bool joined = false;        
- //   std::vector<uint8_t> upstream_packet; 
-    uint8_t i;  
+   eui = mts::Text::bin2hexString(dot->getDeviceId()).c_str();
+   pc.printf("\r\nEUI: %s\r\n",eui);       
+   bool joined = false;        
+
         
-    i2c_proc_init();  //init i2c comm  
-    pc.printf("\r\nGoing to sleep\r\n");    //3.00 fails on startup to respond,move code further down
-    sleep_wake_interrupt_only(deep_sleep);
+   i2c_proc_init();  //init i2c comm  
+   sleep_wake_interrupt_only(deep_sleep);
     
 //scope test    
 //    gpio1 =1;
-
-
 //==============================================================================
 //  -loop here forever
 //       -sleep until LORA_WAKE goes hi => proc ready to send i2c cmd
@@ -296,7 +320,7 @@
                 bPulseLoraWake = true;                    
                 switch (buf_rcv[0])
                 {
-                    case XDOT_CMD_XMIT_PKT:           
+                    case XDOT_CMD_XMIT_PKT:    
                         pkt_upstrm  *pUp= (pkt_upstrm*)&buf_rcv[0];                       
                         pkt_ack *pAck = (pkt_ack*)&buf_xmt[0];                       
                         pAck->ack = I2C_ACK_PROC;
@@ -317,6 +341,15 @@
                             pAck->chksum_err = 1;
                             break;
                         }
+//rev 0307 parameters  
+                        pc.printf("\n\r setting application port %d ",pUp->appPort);  //appPort not used in rev < 0307                                 
+                        dot->setAppPort(pUp->appPort);
+                        uint8_t linkFailcnt =  dot->getLinkFailCount();
+                        pc.printf("\r\n lINK fail count %d\r\n",linkFailcnt); 
+                        if (pUp->bResetLinkCCounter){
+                            dot->setLinkFailCount(0);
+                            pc.printf("\r\n reset link fail threshold to zero"); 
+                        }                                                
                         if (pUp->dataLen ==  0){ //datalen  non zero?                               
                             pAck->bXmitAttempted = 0;                            
                             break;
@@ -326,25 +359,22 @@
                         pc.printf("\r\n[TEST],Upstream Packet Received"); // no \r\n because it comes below
                         for(std::vector<uint8_t>::iterator it = upstream_packet.begin(); it != upstream_packet.end(); ++it) pc.printf(",0x%x", *it);                                 
                         pc.printf("\r\n"); // see i told you.       
-               
-//not done here, done thru radio commands
-                       // cfg_network(false,pUp->bPublicNetwork,pUp->subBand);  //chk for change in network cfg                                                                                       
-                       // dot->setAdr(pUp->bSetAdr);  // enable or disable Adaptive Data Rate
                         
                         joined = dot->getNetworkJoinStatus();   //are we joined to Lorawan?
                         pAck->joinAttempts = 0;                 //no attempts made yet to join                
-                        if(!joined) {                //if not previously joined, then need to join now
+                        pAck->bAck = 0;                         //won't know if we receive a lorawan ack until after xmit
+                        pAck->bAckdata = 0;                     //won't know if we receive a lorawan ack downstream data until after xmit
+                        pAck->rssi = 0;                         //if not rx1/rx2 then no RSSI value                             
+                        if(!joined) {                           //if not previously joined, then need to join now
                             pAck->bJoined = 0;                 
                             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();
-                            if (!joined)pc.printf("\r\n----------- FAILED TO JOIN...GIVING UP\r\n");  // join network if not joined
+                            pAck->mdot_ret = dot->send(upstream_packet);
+                            if (!joined)pc.printf("\r\n----------- FAILED TO JOIN...GIVING UP\r\n");  // join network if not joined                                                         
                         }         
                         if (joined){          
-                            pAck->bJoined = 1;      //we are joined to the network
-                            pAck->bAck = 0;         //won't know if we receive a lorawan ack until after xmit
-                            pAck->bAckdata = 0;     //won't know if we receive a lorawan ack downstream data until after xmit
-                            pAck->rssi = 0;         //if not rx1/rx2 then no RSSI value                             
+                            pAck->bJoined = 1;      //we are joined to the network                            
                             packets_sent++;  
  //send packet
  //return code indicates results, send return code back to proc Dec14,2017
@@ -356,12 +386,7 @@
                                 pAck->bAck = 1;                //we got a Rx1 or Rx2 ack                           
                                 mDot::rssi_stats rssiStats = dot->getRssiStats();        //rssi stat         
                                 pAck->rssi = (int8_t)rssiStats.last;  
-                                printf("\n\rdata->send()=  true => ack rcvd :ack=: %d, rssi=: %d\r\n",pAck->bAck,pAck->rssi);                                     
-                                
-//test for subband change   
-//                                pc.printf("\r\n----------- CHANGING SUBBANDS-------------------------------------\r\n");                                                                                         
-//                                cfg_network(false,pUp->bPublicNetwork,2);  //chk for change in network cfg                        
-                                
+                                printf("\n\rdata->send()=  true => ack rcvd :ack=: %d, rssi=: %d\r\n",pAck->bAck,pAck->rssi);                                                                     
                                 if (events.is_packet_received()){    //any downstream data from the Rx1/Rx2 pkt?                                     
                                     printf("\n\revents.is_packet_received = true\r\n");                                       
                                     pAck->bAckdata = 1;                                          
@@ -405,8 +430,10 @@
                             dot->setTxPower(pUpRadio->txPowerdBm);
                             pc.printf("\n\r setting tx datarate to %d ",pUpRadio->dataRate);                                   
                             dot->setTxDataRate(pUpRadio->dataRate);                         
-                            pc.printf("\n\r setting application port %d ",pUpRadio->appPort);                                   
-                            dot->setAppPort(pUpRadio->appPort);
+                         //   pc.printf("\n\r setting application port %d ",pUpRadio->appPort);                                   
+                         //   dot->setAppPort(pUpRadio->appPort);
+                            pc.printf("\n\r setting link check threshold %d ",pUpRadio->linkChkCount);                                   
+                            dot->setLinkCheckThreshold(pUpRadio->linkChkCount);
                             pc.printf("\n\r saving configuration");
                             if (!dot->saveConfig())logError("failed to save configuration");                                                        
                             display_config();                              
@@ -414,6 +441,7 @@
                         
                         pDwnRadio->public_network = public_network;
                         pDwnRadio->sub_band = dot->getFrequencySubBand();
+                        pDwnRadio->linkChkCount = dot->getLinkCheckThreshold();
                         pDwnRadio->maxDataLen = dot->getMaxPacketLength();                   
                         pDwnRadio->maxTxPowerdBm = dot->getMaxTxPower();
                         pDwnRadio->minTxPowerdBm = dot->getMinTxPower();
@@ -421,9 +449,8 @@
                         pDwnRadio->antennaGaindBi = dot->getAntennaGain();
                         pDwnRadio->txPowerdBm = dot->getTxPower();                      
                         pDwnRadio->dataRate = dot->getTxDataRate();
-                        pDwnRadio->appPort = dot->getAppPort();                         
+//                        pDwnRadio->appPort = dot->getAppPort();                         
                         break;    
-
                     case XDOT_CMD_GET_EUI:   //0307: modified to include radio parameter settings   
                         pc.printf("\n\r proc cmd: get EUI");                        
                         pkt_eui *peui = (pkt_eui*)&buf_xmt[0];                                
@@ -438,6 +465,24 @@
                         peui->dataLen = sizeof(pkt_eui)-3;  //size of struc minus first 3 bytes                                      
                         pc.printf("\n\r eui data length: %d",peui->dataLen);                                                                                              
                         break;    
+                    case XDOT_CMD_SET_NTWKSESS:  //read or write network seesion to xdot flash
+                        bool bWriteSession = (bool)buf_rcv[1];
+                        if (bWriteSession){
+                             pc.printf("\n\r proc cmd writing network sesion to flash");                        
+                             dot->saveNetworkSession();
+                        }     
+                        else{
+                            pc.printf("\n\r reading network session from flash");                        
+                            dot->restoreNetworkSession();
+                        }    
+                        pkt_ntwrk *pktwrk = (pkt_ntwrk*)&buf_xmt[0];  
+                        pktwrk->ack = I2C_ACK_PROC;
+                        pktwrk->cmd = XDOT_CMD_SET_NTWKSESS;
+                        pktwrk->bSetNetwrk = (uint8_t)bWriteSession;                        
+                        break;    
+                    case XDOT_CMD_SET_NVM:
+                        
+                        break;    
                     case XDOT_CMD_SET_KEY_X:
                         pc.printf("\n\r proc cmd: set a key, simulating minm delay before wake pulse\r\n");                                  
                         wait_ms(I2C_MIN_WAIT_DELAY);