arm studio build

Dependencies:   libxDot-mbed5

src/wbit_util.cpp

Committer:
alan1974
Date:
2018-08-04
Revision:
4:d87f8dcf40ef
Parent:
2:0af50f386eb2
Child:
5:abfe25f0de33

File content as of revision 4:d87f8dcf40ef:

#include "global.h"
#include "wbit_util.h"
#include "dot_util.h"
#include "commI2C.h"
#include "mbed.h"
#include "mDot.h"
extern Serial pc;
uint8_t j_attempts = 0; //return number of attempts it took to join the network


//network keys
//these are used as backup keys in case nvm memory is corrupted and we can't read the keys correctly
uint8_t network_id[] = { 0x90, 0xF1, 0x47, 0x90, 0x6C, 0x48, 0x1D, 0x29 };   //static id not used anymore but don't comment out                                
uint8_t network_key[] = { 0x0F, 0xF9, 0xA2, 0x90, 0x2E, 0xAA, 0x6B, 0x8C, 0x6A, 0x4E, 0xFD, 0x67, 0xF9, 0xA6, 0xF3, 0xD3 };   //OTAA appkey    
//==============================================================================
//printNmvData
//==============================================================================
void printNmvData(nvm *pNvm){

     uint8_t i;    
     pc.printf("\r\nnon-volatile memory (nvm):"); 
     pc.printf("\r\nnetwork_id:"); 
     
     for (i = 0; i < 8;i++) 
      pc.printf(" %x",pNvm->network_id[i]);
         
     //for (i = 0 ; i < sizeof(NvmData.network_id);i++)  pc.printf(" ,%x",NvmData.network_id[i]);
     pc.printf("\r\nnetwork_key:"); 
     for (i = 0 ; i < 16;i++)  pc.printf(" %x",pNvm->network_key[i]);
     pc.printf("\r\nbLogOutputOn: %d\r\n",pNvm->bLogOutputOn); 
    
}                              
//==============================================================================
//getNvmChkSum
//compute chksum for nvm data; don't include chksum byte in nvm struc
//==============================================================================
uint8_t getNvmChkSum(nvm *pNvm){
    uint8_t i;
    uint8_t chksum= 0;   
    uint8_t *pData = (uint8_t *)pNvm;
    
    for (i = 0 ; i < sizeof(nvm)-1;i++)chksum += pData[i];
    return chksum;    
}    
//==============================================================================
//nvmWrite
//write nvmData struc to nvm memory
//==============================================================================
bool nvmWrite(nvm *pNvm){   
   pNvm->chksum = getNvmChkSum(pNvm);
   printNmvData(pNvm);
   return dot->nvmWrite(0,pNvm,sizeof(nvm));      
   
}
//==============================================================================
//nvmRead
//- read nvmData struc from nvm memory
//- if bad chksum then default to hard code network keys 
//==============================================================================
bool nvmRead(nvm *pNvm){     
   uint8_t i;  
   dot->nvmRead(0,pNvm,sizeof(nvm));  
   uint8_t chksum = getNvmChkSum(pNvm);
   if (chksum == pNvm->chksum){
       logInfo("nvmRead: chksum ok");    
       printNmvData(pNvm);
       return true;
   }    
//bad chksum, resort to old hard coded keys  
   pc.printf("nvmRead: bad chksum, using default values");      
   for (i = 0; i < sizeof(network_id);i++){
        pNvm->network_id[i] = network_id[i];   
    }    
    for (i = 0; i < sizeof(network_key);i++){
        pNvm->network_key[i] = network_key[i];   
    }
    pNvm->bLogOutputOn = 1;  //disable log output
    printNmvData(pNvm);
    return false;
}
//============================================================================

uint8_t join_network_attempts_wbit() {
    return j_attempts;
}    

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) {
        j_attempts++;
        logInfo("attempt %d to join network",j_attempts);
        ret = dot->joinNetwork();
        if (ret == mDot::MDOT_OK) return true;
        
        logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
        if (j_attempts >= nmbAttempts)
        {
            logInfo("attempts %d to join network exceeds specified attempts $d ",j_attempts,nmbAttempts);   
            return false;
        }               
        // in some frequency bands we need to wait until another channel is available before transmitting again
        uint32_t delay_s = (dot->getNextTxMs() / 1000) + 1;
        if (delay_s < 2) {
            logInfo("waiting %lu s until next free channel", delay_s);
            wait(delay_s);
        } else {
            logInfo("sleeping %lu s until next free channel", delay_s);
            dot->sleep(delay_s, mDot::RTC_ALARM, false);
        }        
    }//while
    return false;
}