![](/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
xDotBridge/src/CommProtocolPeerBrute.cpp
- Committer:
- Matt Briggs
- Date:
- 2017-02-27
- Revision:
- 55:79ab0bbc5008
- Parent:
- 54:c04d7b6fa075
- Child:
- 61:8d9efd33cac9
File content as of revision 55:79ab0bbc5008:
/* * CommProtocolPeerBrute.cpp * * Created on: Jan 30, 2017 * Author: mbriggs */ #include "CommProtocolPeerBrute.h" #include "MTSLog.h" #include "dot_util.h" // wireless bridge protocol const uint8_t TX_PWR = 20; // 20 dBm const float RX_SLEEP_TIME = 2000; // ms (one second resolution, min 2 seconds) const uint8_t TX_TIME = 30; // in ms const unsigned int nTimesToTx = ceil(RX_SLEEP_TIME / ((float)TX_TIME)); //const uint8_t maxPayloadSize = 10; // Number of bytes (used for toa calcultion) CommProtocolPeerBrute::CommProtocolPeerBrute() { logInfo("RX_SLEEP_TIME %f, timeOnAir %lu, nTimesToTx %lu", RX_SLEEP_TIME, TX_TIME, nTimesToTx); mIsTx = true; // default to TX mPrevDownLinkCnt = dot->getDownLinkCounter(); // dot = mDot::getInstance(); } 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 logInfo("Setting up peer to peer configuration"); if (dot->getJoinMode() != mDot::PEER_TO_PEER) { logInfo("changing network join mode to PEER_TO_PEER"); if (dot->setJoinMode(mDot::PEER_TO_PEER) != mDot::MDOT_OK) { logError("failed to set network join mode to PEER_TO_PEER"); } } uint8_t tx_power; uint8_t tx_datarate; uint32_t tx_frequency; uint8_t frequency_band = dot->getFrequencyBand(); switch (frequency_band) { case mDot::FB_EU868: // 250kHz channels achieve higher throughput // DR6 : SF7 @ 250kHz // DR0 - DR5 (125kHz channels) available but much slower tx_frequency = 869850000; tx_datarate = mDot::DR6; // the 869850000 frequency is 100% duty cycle if the total power is under 7 dBm - tx power 4 + antenna gain 3 = 7 tx_power = 4; break; case mDot::FB_US915: case mDot::FB_AU915: default: // 500kHz channels achieve highest throughput // DR8 : SF12 @ 500kHz // DR9 : SF11 @ 500kHz // DR10 : SF10 @ 500kHz // DR11 : SF9 @ 500kHz // DR12 : SF8 @ 500kHz // DR13 : SF7 @ 500kHz // DR0 - DR3 (125kHz channels) available but much slower tx_frequency = 915500000; tx_datarate = mDot::DR13; // 915 bands have no duty cycle restrictions, set tx power to max tx_power = 20; break; } // in PEER_TO_PEER mode there is no join request/response transaction // as long as both Dots are configured correctly, they should be able to communicate // FIXME just using pairing keys for now update_peer_to_peer_config(pair_network_address, pair_network_session_key, pair_data_session_key, tx_frequency, tx_datarate, tx_power); dot->saveConfig(); return cmdSuccess; } void CommProtocolPeerBrute::setTx(bool isTx) { mIsTx = isTx; } bool CommProtocolPeerBrute::isTx() { return mIsTx; } CmdResult CommProtocolPeerBrute::clearPair() { logError("Not implemented yet!!!"); return cmdError; } // TX focused CmdResult CommProtocolPeerBrute::send (const std::vector<uint8_t> &msg) { if (!dot->getNetworkJoinStatus()) { join_network(); } logInfo("Starting TX. Time: %lu", us_ticker_read()); for(uint8_t i=0;i<nTimesToTx;++i) { dot->send(msg); } logInfo("Finished TX. Time: %lu", us_ticker_read()); return cmdError; } CmdResult CommProtocolPeerBrute::sendPairReq() { if (!dot->getNetworkJoinStatus()) { join_network(); } logError("Not implemented yet!!!"); return cmdError; } // RX focused CmdResult CommProtocolPeerBrute::listen (bool &msgPending) { if (!dot->getNetworkJoinStatus()) { join_network(); } // uint32_t cDwnLink = dot->getDownLinkCounter(); wait(TX_TIME/1000.0); // Wait TX_TIME if (mPrevDownLinkCnt < dot->getDownLinkCounter()) { msgPending = true; } else { msgPending = false; } mPrevDownLinkCnt = dot->getDownLinkCounter(); return cmdSuccess; // Maybe add timeout as a possible return value } CmdResult CommProtocolPeerBrute::sampleDLC () { mPrevDownLinkCnt = dot->getDownLinkCounter(); return cmdSuccess; } CmdResult CommProtocolPeerBrute::recv (std::vector<uint8_t> &msg) { dot->recv(msg); return cmdSuccess; } CmdResult CommProtocolPeerBrute::waitForPairing(float waitTime) { logError("Not implemented yet!!!"); return cmdError; } CmdResult CommProtocolPeerBrute::sendPairAccepted() { logError("Not implemented yet!!!"); return cmdError; } // xDot Peer to Peer Specific uint32_t CommProtocolPeerBrute::getDLC() { return dot->getDownLinkCounter(); } uint32_t CommProtocolPeerBrute::getULC() { return dot->getUpLinkCounter(); } // private: CmdResult CommProtocolPeerBrute::readInfoFromNVM() { // 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() { 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() { dot->setDownLinkCounter(0); dot->setUpLinkCounter(0); return cmdSuccess; } CmdResult CommProtocolPeerBrute::genEncypKeys() { 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; }