arm studio build

Dependencies:   libxDot-mbed5

src/wbit_util.cpp

Committer:
alan1974
Date:
2018-06-18
Revision:
0:a91cd1b08360
Child:
1:0d25d9ddbe9f

File content as of revision 0:a91cd1b08360:

#include "wbit_util.h"
#include "dot_util.h"
#include "mbed.h"
#include "mDot.h"

uint8_t j_attempts = 0;
//return number of attempts it took to join the network
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;
}