![](/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@44:ece6330e9b57, 2017-02-01 (annotated)
- Committer:
- Matt Briggs
- Date:
- Wed Feb 01 15:20:45 2017 -0700
- Revision:
- 44:ece6330e9b57
- Parent:
- 41:9ef4c4d77711
- Child:
- 53:a1563574a980
First cut at BaseboardIO class implementation.
Minior cleanup in main and CommProtocolPeerBrute
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Matt Briggs | 41:9ef4c4d77711 | 1 | /* |
Matt Briggs | 41:9ef4c4d77711 | 2 | * CommProtocolPeerBrute.cpp |
Matt Briggs | 41:9ef4c4d77711 | 3 | * |
Matt Briggs | 41:9ef4c4d77711 | 4 | * Created on: Jan 30, 2017 |
Matt Briggs | 41:9ef4c4d77711 | 5 | * Author: mbriggs |
Matt Briggs | 41:9ef4c4d77711 | 6 | */ |
Matt Briggs | 41:9ef4c4d77711 | 7 | |
Matt Briggs | 41:9ef4c4d77711 | 8 | #include "CommProtocolPeerBrute.h" |
Matt Briggs | 41:9ef4c4d77711 | 9 | #include "MTSLog.h" |
Matt Briggs | 41:9ef4c4d77711 | 10 | #include "dot_util.h" |
Matt Briggs | 41:9ef4c4d77711 | 11 | |
Matt Briggs | 41:9ef4c4d77711 | 12 | // wireless bridge protocol |
Matt Briggs | 41:9ef4c4d77711 | 13 | const uint8_t TX_PWR = 20; // 20 dBm |
Matt Briggs | 41:9ef4c4d77711 | 14 | const float RX_SLEEP_TIME = 2000; // ms (one second resolution, min 2 seconds) |
Matt Briggs | 41:9ef4c4d77711 | 15 | const uint8_t TX_TIME = 30; // in ms |
Matt Briggs | 41:9ef4c4d77711 | 16 | const unsigned int nTimesToTx = ceil(RX_SLEEP_TIME / ((float)TX_TIME)); |
Matt Briggs | 41:9ef4c4d77711 | 17 | //const uint8_t maxPayloadSize = 10; // Number of bytes (used for toa calcultion) |
Matt Briggs | 41:9ef4c4d77711 | 18 | |
Matt Briggs | 44:ece6330e9b57 | 19 | CommProtocolPeerBrute::CommProtocolPeerBrute() |
Matt Briggs | 41:9ef4c4d77711 | 20 | { |
Matt Briggs | 41:9ef4c4d77711 | 21 | logInfo("RX_SLEEP_TIME %f, timeOnAir %lu, nTimesToTx %lu", RX_SLEEP_TIME, TX_TIME, nTimesToTx); |
Matt Briggs | 41:9ef4c4d77711 | 22 | |
Matt Briggs | 44:ece6330e9b57 | 23 | mIsTx = true; // default to TX |
Matt Briggs | 41:9ef4c4d77711 | 24 | dot = mDot::getInstance(); |
Matt Briggs | 41:9ef4c4d77711 | 25 | } |
Matt Briggs | 41:9ef4c4d77711 | 26 | |
Matt Briggs | 41:9ef4c4d77711 | 27 | CmdResult CommProtocolPeerBrute::init() |
Matt Briggs | 41:9ef4c4d77711 | 28 | { |
Matt Briggs | 41:9ef4c4d77711 | 29 | // Common Configuration |
Matt Briggs | 41:9ef4c4d77711 | 30 | dot->setAesEncryption(true); // Enable encryption |
Matt Briggs | 41:9ef4c4d77711 | 31 | dot->setTxWait(false); |
Matt Briggs | 41:9ef4c4d77711 | 32 | dot->setAck(0); // Disable Ack |
Matt Briggs | 41:9ef4c4d77711 | 33 | dot->setClass("C"); // Set class C |
Matt Briggs | 41:9ef4c4d77711 | 34 | dot->setTxPower(TX_PWR); |
Matt Briggs | 41:9ef4c4d77711 | 35 | |
Matt Briggs | 41:9ef4c4d77711 | 36 | // TODO break out in a utility function |
Matt Briggs | 41:9ef4c4d77711 | 37 | // update configuration if necessary |
Matt Briggs | 41:9ef4c4d77711 | 38 | logInfo("Setting up peer to peer configuration"); |
Matt Briggs | 41:9ef4c4d77711 | 39 | if (dot->getJoinMode() != mDot::PEER_TO_PEER) { |
Matt Briggs | 41:9ef4c4d77711 | 40 | logInfo("changing network join mode to PEER_TO_PEER"); |
Matt Briggs | 41:9ef4c4d77711 | 41 | if (dot->setJoinMode(mDot::PEER_TO_PEER) != mDot::MDOT_OK) { |
Matt Briggs | 41:9ef4c4d77711 | 42 | logError("failed to set network join mode to PEER_TO_PEER"); |
Matt Briggs | 41:9ef4c4d77711 | 43 | } |
Matt Briggs | 41:9ef4c4d77711 | 44 | } |
Matt Briggs | 41:9ef4c4d77711 | 45 | uint8_t tx_power; |
Matt Briggs | 41:9ef4c4d77711 | 46 | uint8_t tx_datarate; |
Matt Briggs | 41:9ef4c4d77711 | 47 | uint32_t tx_frequency; |
Matt Briggs | 41:9ef4c4d77711 | 48 | uint8_t frequency_band = dot->getFrequencyBand(); |
Matt Briggs | 41:9ef4c4d77711 | 49 | switch (frequency_band) { |
Matt Briggs | 41:9ef4c4d77711 | 50 | case mDot::FB_EU868: |
Matt Briggs | 41:9ef4c4d77711 | 51 | // 250kHz channels achieve higher throughput |
Matt Briggs | 41:9ef4c4d77711 | 52 | // DR6 : SF7 @ 250kHz |
Matt Briggs | 41:9ef4c4d77711 | 53 | // DR0 - DR5 (125kHz channels) available but much slower |
Matt Briggs | 41:9ef4c4d77711 | 54 | tx_frequency = 869850000; |
Matt Briggs | 41:9ef4c4d77711 | 55 | tx_datarate = mDot::DR6; |
Matt Briggs | 41:9ef4c4d77711 | 56 | // the 869850000 frequency is 100% duty cycle if the total power is under 7 dBm - tx power 4 + antenna gain 3 = 7 |
Matt Briggs | 41:9ef4c4d77711 | 57 | tx_power = 4; |
Matt Briggs | 41:9ef4c4d77711 | 58 | break; |
Matt Briggs | 41:9ef4c4d77711 | 59 | case mDot::FB_US915: |
Matt Briggs | 41:9ef4c4d77711 | 60 | case mDot::FB_AU915: |
Matt Briggs | 41:9ef4c4d77711 | 61 | default: |
Matt Briggs | 41:9ef4c4d77711 | 62 | // 500kHz channels achieve highest throughput |
Matt Briggs | 41:9ef4c4d77711 | 63 | // DR8 : SF12 @ 500kHz |
Matt Briggs | 41:9ef4c4d77711 | 64 | // DR9 : SF11 @ 500kHz |
Matt Briggs | 41:9ef4c4d77711 | 65 | // DR10 : SF10 @ 500kHz |
Matt Briggs | 41:9ef4c4d77711 | 66 | // DR11 : SF9 @ 500kHz |
Matt Briggs | 41:9ef4c4d77711 | 67 | // DR12 : SF8 @ 500kHz |
Matt Briggs | 41:9ef4c4d77711 | 68 | // DR13 : SF7 @ 500kHz |
Matt Briggs | 41:9ef4c4d77711 | 69 | // DR0 - DR3 (125kHz channels) available but much slower |
Matt Briggs | 41:9ef4c4d77711 | 70 | tx_frequency = 915500000; |
Matt Briggs | 41:9ef4c4d77711 | 71 | tx_datarate = mDot::DR13; |
Matt Briggs | 41:9ef4c4d77711 | 72 | // 915 bands have no duty cycle restrictions, set tx power to max |
Matt Briggs | 41:9ef4c4d77711 | 73 | tx_power = 20; |
Matt Briggs | 41:9ef4c4d77711 | 74 | break; |
Matt Briggs | 41:9ef4c4d77711 | 75 | } |
Matt Briggs | 41:9ef4c4d77711 | 76 | // in PEER_TO_PEER mode there is no join request/response transaction |
Matt Briggs | 41:9ef4c4d77711 | 77 | // as long as both Dots are configured correctly, they should be able to communicate |
Matt Briggs | 41:9ef4c4d77711 | 78 | |
Matt Briggs | 41:9ef4c4d77711 | 79 | // FIXME just using pairing keys for now |
Matt Briggs | 41:9ef4c4d77711 | 80 | update_peer_to_peer_config(pair_network_address, pair_network_session_key, pair_data_session_key, tx_frequency, tx_datarate, tx_power); |
Matt Briggs | 41:9ef4c4d77711 | 81 | return cmdSuccess; |
Matt Briggs | 41:9ef4c4d77711 | 82 | } |
Matt Briggs | 44:ece6330e9b57 | 83 | void CommProtocolPeerBrute::setTx(bool isTx) |
Matt Briggs | 44:ece6330e9b57 | 84 | { |
Matt Briggs | 44:ece6330e9b57 | 85 | mIsTx = isTx; |
Matt Briggs | 44:ece6330e9b57 | 86 | } |
Matt Briggs | 41:9ef4c4d77711 | 87 | bool CommProtocolPeerBrute::isTx() |
Matt Briggs | 41:9ef4c4d77711 | 88 | { |
Matt Briggs | 41:9ef4c4d77711 | 89 | return mIsTx; |
Matt Briggs | 41:9ef4c4d77711 | 90 | } |
Matt Briggs | 41:9ef4c4d77711 | 91 | CmdResult CommProtocolPeerBrute::clearPair() |
Matt Briggs | 41:9ef4c4d77711 | 92 | { |
Matt Briggs | 41:9ef4c4d77711 | 93 | logError("Not implemented yet!!!"); |
Matt Briggs | 41:9ef4c4d77711 | 94 | return cmdError; |
Matt Briggs | 41:9ef4c4d77711 | 95 | } |
Matt Briggs | 41:9ef4c4d77711 | 96 | |
Matt Briggs | 41:9ef4c4d77711 | 97 | // TX focused |
Matt Briggs | 41:9ef4c4d77711 | 98 | CmdResult CommProtocolPeerBrute::send (const std::vector<uint8_t> &msg) |
Matt Briggs | 41:9ef4c4d77711 | 99 | { |
Matt Briggs | 41:9ef4c4d77711 | 100 | if (!dot->getNetworkJoinStatus()) { |
Matt Briggs | 41:9ef4c4d77711 | 101 | join_network(); |
Matt Briggs | 41:9ef4c4d77711 | 102 | } |
Matt Briggs | 41:9ef4c4d77711 | 103 | logInfo("Starting TX. Time: %lu", us_ticker_read()); |
Matt Briggs | 41:9ef4c4d77711 | 104 | for(uint i=0;i<nTimesToTx;++i) { |
Matt Briggs | 41:9ef4c4d77711 | 105 | dot->send(msg); |
Matt Briggs | 41:9ef4c4d77711 | 106 | } |
Matt Briggs | 41:9ef4c4d77711 | 107 | logInfo("Finished TX. Time: %lu", us_ticker_read()); |
Matt Briggs | 41:9ef4c4d77711 | 108 | return cmdError; |
Matt Briggs | 41:9ef4c4d77711 | 109 | } |
Matt Briggs | 41:9ef4c4d77711 | 110 | |
Matt Briggs | 41:9ef4c4d77711 | 111 | CmdResult CommProtocolPeerBrute::sendPairReq() |
Matt Briggs | 41:9ef4c4d77711 | 112 | { |
Matt Briggs | 41:9ef4c4d77711 | 113 | if (!dot->getNetworkJoinStatus()) { |
Matt Briggs | 41:9ef4c4d77711 | 114 | join_network(); |
Matt Briggs | 41:9ef4c4d77711 | 115 | } |
Matt Briggs | 41:9ef4c4d77711 | 116 | logError("Not implemented yet!!!"); |
Matt Briggs | 41:9ef4c4d77711 | 117 | return cmdError; |
Matt Briggs | 41:9ef4c4d77711 | 118 | } |
Matt Briggs | 41:9ef4c4d77711 | 119 | |
Matt Briggs | 41:9ef4c4d77711 | 120 | // RX focused |
Matt Briggs | 41:9ef4c4d77711 | 121 | CmdResult CommProtocolPeerBrute::listen (bool &msgPending) |
Matt Briggs | 41:9ef4c4d77711 | 122 | { |
Matt Briggs | 41:9ef4c4d77711 | 123 | if (!dot->getNetworkJoinStatus()) { |
Matt Briggs | 41:9ef4c4d77711 | 124 | join_network(); |
Matt Briggs | 41:9ef4c4d77711 | 125 | } |
Matt Briggs | 41:9ef4c4d77711 | 126 | |
Matt Briggs | 41:9ef4c4d77711 | 127 | uint32_t cDwnLink = dot->getDownLinkCounter(); |
Matt Briggs | 41:9ef4c4d77711 | 128 | |
Matt Briggs | 41:9ef4c4d77711 | 129 | wait(TX_TIME/1000.0); // Wait TX_TIME |
Matt Briggs | 41:9ef4c4d77711 | 130 | |
Matt Briggs | 41:9ef4c4d77711 | 131 | if (cDwnLink < dot->getDownLinkCounter()) { |
Matt Briggs | 41:9ef4c4d77711 | 132 | msgPending = true; |
Matt Briggs | 41:9ef4c4d77711 | 133 | } |
Matt Briggs | 41:9ef4c4d77711 | 134 | else { |
Matt Briggs | 41:9ef4c4d77711 | 135 | msgPending = false; |
Matt Briggs | 41:9ef4c4d77711 | 136 | } |
Matt Briggs | 41:9ef4c4d77711 | 137 | return cmdSuccess; // Maybe add timeout as a possible return value |
Matt Briggs | 41:9ef4c4d77711 | 138 | } |
Matt Briggs | 41:9ef4c4d77711 | 139 | |
Matt Briggs | 41:9ef4c4d77711 | 140 | CmdResult CommProtocolPeerBrute::recv (std::vector<uint8_t> &msg) |
Matt Briggs | 41:9ef4c4d77711 | 141 | { |
Matt Briggs | 41:9ef4c4d77711 | 142 | dot->recv(msg); |
Matt Briggs | 41:9ef4c4d77711 | 143 | return cmdSuccess; |
Matt Briggs | 41:9ef4c4d77711 | 144 | } |
Matt Briggs | 41:9ef4c4d77711 | 145 | |
Matt Briggs | 41:9ef4c4d77711 | 146 | CmdResult CommProtocolPeerBrute::waitForPairing(float waitTime) |
Matt Briggs | 41:9ef4c4d77711 | 147 | { |
Matt Briggs | 41:9ef4c4d77711 | 148 | logError("Not implemented yet!!!"); |
Matt Briggs | 41:9ef4c4d77711 | 149 | return cmdError; |
Matt Briggs | 41:9ef4c4d77711 | 150 | } |
Matt Briggs | 41:9ef4c4d77711 | 151 | |
Matt Briggs | 41:9ef4c4d77711 | 152 | CmdResult CommProtocolPeerBrute::sendPairAccepted() |
Matt Briggs | 41:9ef4c4d77711 | 153 | { |
Matt Briggs | 41:9ef4c4d77711 | 154 | logError("Not implemented yet!!!"); |
Matt Briggs | 41:9ef4c4d77711 | 155 | return cmdError; |
Matt Briggs | 41:9ef4c4d77711 | 156 | } |
Matt Briggs | 41:9ef4c4d77711 | 157 | |
Matt Briggs | 41:9ef4c4d77711 | 158 | // xDot Peer to Peer Specific |
Matt Briggs | 41:9ef4c4d77711 | 159 | uint32_t CommProtocolPeerBrute::getDLC() |
Matt Briggs | 41:9ef4c4d77711 | 160 | { |
Matt Briggs | 41:9ef4c4d77711 | 161 | return dot->getDownLinkCounter(); |
Matt Briggs | 41:9ef4c4d77711 | 162 | } |
Matt Briggs | 41:9ef4c4d77711 | 163 | |
Matt Briggs | 41:9ef4c4d77711 | 164 | uint32_t CommProtocolPeerBrute::getULC() |
Matt Briggs | 41:9ef4c4d77711 | 165 | { |
Matt Briggs | 41:9ef4c4d77711 | 166 | return dot->getUpLinkCounter(); |
Matt Briggs | 41:9ef4c4d77711 | 167 | } |
Matt Briggs | 41:9ef4c4d77711 | 168 | |
Matt Briggs | 41:9ef4c4d77711 | 169 | // private: |
Matt Briggs | 41:9ef4c4d77711 | 170 | |
Matt Briggs | 41:9ef4c4d77711 | 171 | CmdResult CommProtocolPeerBrute::readInfoFromNVM() |
Matt Briggs | 41:9ef4c4d77711 | 172 | { |
Matt Briggs | 41:9ef4c4d77711 | 173 | logError("Not implemented yet!!!"); |
Matt Briggs | 41:9ef4c4d77711 | 174 | return cmdError; |
Matt Briggs | 41:9ef4c4d77711 | 175 | } |
Matt Briggs | 41:9ef4c4d77711 | 176 | |
Matt Briggs | 41:9ef4c4d77711 | 177 | CmdResult CommProtocolPeerBrute::writeInfoToNVM() |
Matt Briggs | 41:9ef4c4d77711 | 178 | { |
Matt Briggs | 41:9ef4c4d77711 | 179 | logError("Not implemented yet!!!"); |
Matt Briggs | 41:9ef4c4d77711 | 180 | return cmdError; |
Matt Briggs | 41:9ef4c4d77711 | 181 | } |
Matt Briggs | 41:9ef4c4d77711 | 182 | |
Matt Briggs | 41:9ef4c4d77711 | 183 | CmdResult CommProtocolPeerBrute::resetCounters() |
Matt Briggs | 41:9ef4c4d77711 | 184 | { |
Matt Briggs | 41:9ef4c4d77711 | 185 | dot->setDownLinkCounter(0); |
Matt Briggs | 41:9ef4c4d77711 | 186 | dot->setUpLinkCounter(0); |
Matt Briggs | 41:9ef4c4d77711 | 187 | return cmdSuccess; |
Matt Briggs | 41:9ef4c4d77711 | 188 | } |
Matt Briggs | 41:9ef4c4d77711 | 189 | |
Matt Briggs | 41:9ef4c4d77711 | 190 | CmdResult CommProtocolPeerBrute::genEncypKeys() |
Matt Briggs | 41:9ef4c4d77711 | 191 | { |
Matt Briggs | 41:9ef4c4d77711 | 192 | logError("Not implemented yet!!!"); |
Matt Briggs | 41:9ef4c4d77711 | 193 | return cmdError; |
Matt Briggs | 41:9ef4c4d77711 | 194 | } |