![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Fork to see if I can get working
Dependencies: BufferedSerial OneWire WinbondSPIFlash libxDot-dev-mbed5-deprecated
Fork of xDotBridge_update_test20180823 by
Diff: xDotBridge/src/CommProtocolPeerBrute.cpp
- Revision:
- 55:79ab0bbc5008
- Parent:
- 54:c04d7b6fa075
- Child:
- 61:8d9efd33cac9
--- a/xDotBridge/src/CommProtocolPeerBrute.cpp Wed Feb 22 15:39:40 2017 -0700 +++ b/xDotBridge/src/CommProtocolPeerBrute.cpp Mon Feb 27 08:06:36 2017 -0700 @@ -27,12 +27,23 @@ CmdResult CommProtocolPeerBrute::init() { + // FIXME add NVM stuff + dotDefaults(); + +} +CmdResult CommProtocolPeerBrute::dotDefaults() +{ + // TODO add more error checking + logWarning("defaulting Dot configuration"); + dot->resetConfig(); + // Common Configuration dot->setAesEncryption(true); // Enable encryption dot->setTxWait(false); dot->setAck(0); // Disable Ack dot->setClass("C"); // Set class C dot->setTxPower(TX_PWR); + dot->setPreserveSession(true); // TODO need to better handle counters // TODO break out in a utility function // update configuration if necessary @@ -78,9 +89,7 @@ // as long as both Dots are configured correctly, they should be able to communicate // FIXME just using pairing keys for now - wait(0.1); update_peer_to_peer_config(pair_network_address, pair_network_session_key, pair_data_session_key, tx_frequency, tx_datarate, tx_power); - wait(0.1); dot->saveConfig(); return cmdSuccess; } @@ -180,14 +189,39 @@ CmdResult CommProtocolPeerBrute::readInfoFromNVM() { - logError("Not implemented yet!!!"); - return cmdError; + // TODO store / retrieve network keys + bool nvmReadResult; + uint8_t *data = new uint8_t [PROTOCOL_NVM_SIZE]; + + nvmReadResult = dot->nvmRead(PROTOCOL_NVM_START_ADDR, data, PROTOCOL_NVM_SIZE); + if (!nvmReadResult) { + delete [] data; + return cmdError; + } + mMemObj.fromBytes(data, PROTOCOL_NVM_SIZE); + delete [] data; + if (!mMemObj.validProtocolFlag()) { + logWarning("Invalid Protocol Flag. Using default values."); + mMemObj.setDefaults(); + dotDefaults(); + } + else if (!mMemObj.validProtocolRev()) { + logWarning("Invalid Protocol Rev. Using default values."); + mMemObj.setDefaults(); + dotDefaults(); + } + return cmdSuccess; } CmdResult CommProtocolPeerBrute::writeInfoToNVM() { - logError("Not implemented yet!!!"); - return cmdError; + uint8_t *data = new uint8_t [PROTOCOL_NVM_SIZE]; + uint8_t size = PROTOCOL_NVM_SIZE; + mMemObj.toBytes(data, size); + dot->nvmWrite(PROTOCOL_NVM_START_ADDR, data, PROTOCOL_NVM_SIZE); + + delete [] data; + return cmdSuccess; } CmdResult CommProtocolPeerBrute::resetCounters() @@ -202,3 +236,69 @@ logError("Not implemented yet!!!"); return cmdError; } + +// NvmProtocolObj +NvmProtocolObj::NvmProtocolObj() +{ + setDefaults(); +} +void NvmProtocolObj::setDefaults() +{ + mProtocolFlag = PROTOCOL_FLAG; + mProtocolRev = PROTOCOL_REV; + mLogicalAddr = 0x00000000; + mSeqNum = 0x00000000; +} +CmdResult NvmProtocolObj::fromBytes(uint8_t *data, uint8_t size) +{ + if (size != PROTOCOL_NVM_SIZE) { + return cmdError; + } + + mProtocolFlag = *((uint16_t *) (data)); + mProtocolRev = *((uint16_t *) (data+2)); + mLogicalAddr = *((uint32_t *) (data+4)); + mSeqNum = *((uint32_t *) (data+6)); + + return cmdSuccess; +} +CmdResult NvmProtocolObj::toBytes(uint8_t *data, uint8_t &size) { + // TODO check data size + + *((uint16_t *) (data)) = mProtocolFlag; + *((uint16_t *) (data+2)) = mProtocolRev; + *((uint32_t *) (data+4)) = mLogicalAddr; + *((uint32_t *) (data+6)) = mSeqNum; + + size = PROTOCOL_NVM_SIZE; + + return cmdSuccess; +} +uint16_t NvmProtocolObj::getProtocolFlag() +{ + return mProtocolFlag; +} +bool NvmProtocolObj::validProtocolFlag() +{ + return mProtocolFlag == PROTOCOL_FLAG; +} +uint16_t NvmProtocolObj::getProtocolRev() +{ + return mProtocolRev; +} +bool NvmProtocolObj::validProtocolRev() +{ + return mProtocolRev == PROTOCOL_REV; +} +uint16_t NvmProtocolObj::getLogicalAddr() +{ + return mLogicalAddr; +} +void NvmProtocolObj::setLogicalAddr(uint16_t in) +{ + mLogicalAddr= in; +} +uint32_t NvmProtocolObj::getLastMsgSeq() +{ + return mSeqNum; +}