![](/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@65:d546060aa03d, 2017-03-15 (annotated)
- Committer:
- Matt Briggs
- Date:
- Wed Mar 15 09:49:58 2017 -0600
- Revision:
- 65:d546060aa03d
- Parent:
- 64:46c8819c07cc
- Child:
- 66:bcaa6dbf538a
Added better alert message code. Quick commit before debug.
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 "dot_util.h" |
Matt Briggs | 65:d546060aa03d | 10 | #include "util.h" |
Matt Briggs | 63:e1efbe3402d9 | 11 | #include "MyLog.h" |
Matt Briggs | 41:9ef4c4d77711 | 12 | |
Matt Briggs | 41:9ef4c4d77711 | 13 | // wireless bridge protocol |
Matt Briggs | 41:9ef4c4d77711 | 14 | const uint8_t TX_PWR = 20; // 20 dBm |
Matt Briggs | 41:9ef4c4d77711 | 15 | const float RX_SLEEP_TIME = 2000; // ms (one second resolution, min 2 seconds) |
Matt Briggs | 41:9ef4c4d77711 | 16 | const uint8_t TX_TIME = 30; // in ms |
Matt Briggs | 41:9ef4c4d77711 | 17 | const unsigned int nTimesToTx = ceil(RX_SLEEP_TIME / ((float)TX_TIME)); |
Matt Briggs | 41:9ef4c4d77711 | 18 | //const uint8_t maxPayloadSize = 10; // Number of bytes (used for toa calcultion) |
Matt Briggs | 41:9ef4c4d77711 | 19 | |
Matt Briggs | 44:ece6330e9b57 | 20 | CommProtocolPeerBrute::CommProtocolPeerBrute() |
Matt Briggs | 41:9ef4c4d77711 | 21 | { |
Matt Briggs | 63:e1efbe3402d9 | 22 | myLogDebug("RX_SLEEP_TIME %f, timeOnAir %lu, nTimesToTx %lu", RX_SLEEP_TIME, TX_TIME, nTimesToTx); |
Matt Briggs | 41:9ef4c4d77711 | 23 | |
Matt Briggs | 44:ece6330e9b57 | 24 | mIsTx = true; // default to TX |
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 | 62:9751a8504c82 | 29 | CmdResult result; |
Matt Briggs | 62:9751a8504c82 | 30 | if (dot == NULL) { |
Matt Briggs | 62:9751a8504c82 | 31 | return cmdError; |
Matt Briggs | 62:9751a8504c82 | 32 | } |
Matt Briggs | 62:9751a8504c82 | 33 | result = readInfoFromNVM(); |
Matt Briggs | 62:9751a8504c82 | 34 | if (result != cmdSuccess) { |
Matt Briggs | 63:e1efbe3402d9 | 35 | myLogWarning("Reverting to protocol defaults NVM Read failed"); |
Matt Briggs | 62:9751a8504c82 | 36 | mMemObj.setDefaults(); |
Matt Briggs | 64:46c8819c07cc | 37 | writeInfoToNVM(); |
Matt Briggs | 62:9751a8504c82 | 38 | } |
Matt Briggs | 62:9751a8504c82 | 39 | configForSavedNetwork(); |
Matt Briggs | 55:79ab0bbc5008 | 40 | |
Matt Briggs | 64:46c8819c07cc | 41 | resetCounters(); |
Matt Briggs | 62:9751a8504c82 | 42 | |
Matt Briggs | 62:9751a8504c82 | 43 | mPrevDownLinkCnt = dot->getDownLinkCounter(); |
Matt Briggs | 61:8d9efd33cac9 | 44 | return cmdSuccess; |
Matt Briggs | 55:79ab0bbc5008 | 45 | } |
Matt Briggs | 62:9751a8504c82 | 46 | CmdResult CommProtocolPeerBrute::configForSavedNetwork() |
Matt Briggs | 55:79ab0bbc5008 | 47 | { |
Matt Briggs | 62:9751a8504c82 | 48 | if (dot == NULL) { |
Matt Briggs | 62:9751a8504c82 | 49 | return cmdError; |
Matt Briggs | 62:9751a8504c82 | 50 | } |
Matt Briggs | 62:9751a8504c82 | 51 | |
Matt Briggs | 63:e1efbe3402d9 | 52 | // myLogInfo("defaulting Dot configuration"); |
Matt Briggs | 62:9751a8504c82 | 53 | // dot->resetConfig(); |
Matt Briggs | 55:79ab0bbc5008 | 54 | |
Matt Briggs | 41:9ef4c4d77711 | 55 | // Common Configuration |
Matt Briggs | 41:9ef4c4d77711 | 56 | dot->setAesEncryption(true); // Enable encryption |
Matt Briggs | 41:9ef4c4d77711 | 57 | dot->setTxWait(false); |
Matt Briggs | 41:9ef4c4d77711 | 58 | dot->setAck(0); // Disable Ack |
Matt Briggs | 41:9ef4c4d77711 | 59 | dot->setClass("C"); // Set class C |
Matt Briggs | 41:9ef4c4d77711 | 60 | dot->setTxPower(TX_PWR); |
Matt Briggs | 62:9751a8504c82 | 61 | dot->setPreserveSession(false); |
Matt Briggs | 41:9ef4c4d77711 | 62 | |
Matt Briggs | 41:9ef4c4d77711 | 63 | // TODO break out in a utility function |
Matt Briggs | 41:9ef4c4d77711 | 64 | // update configuration if necessary |
Matt Briggs | 63:e1efbe3402d9 | 65 | myLogDebug("Setting up peer to peer configuration"); |
Matt Briggs | 41:9ef4c4d77711 | 66 | if (dot->getJoinMode() != mDot::PEER_TO_PEER) { |
Matt Briggs | 63:e1efbe3402d9 | 67 | myLogDebug("changing network join mode to PEER_TO_PEER"); |
Matt Briggs | 41:9ef4c4d77711 | 68 | if (dot->setJoinMode(mDot::PEER_TO_PEER) != mDot::MDOT_OK) { |
Matt Briggs | 63:e1efbe3402d9 | 69 | myLogError("failed to set network join mode to PEER_TO_PEER"); |
Matt Briggs | 41:9ef4c4d77711 | 70 | } |
Matt Briggs | 41:9ef4c4d77711 | 71 | } |
Matt Briggs | 41:9ef4c4d77711 | 72 | uint8_t tx_power; |
Matt Briggs | 41:9ef4c4d77711 | 73 | uint8_t tx_datarate; |
Matt Briggs | 41:9ef4c4d77711 | 74 | uint32_t tx_frequency; |
Matt Briggs | 41:9ef4c4d77711 | 75 | uint8_t frequency_band = dot->getFrequencyBand(); |
Matt Briggs | 41:9ef4c4d77711 | 76 | switch (frequency_band) { |
Matt Briggs | 41:9ef4c4d77711 | 77 | case mDot::FB_EU868: |
Matt Briggs | 41:9ef4c4d77711 | 78 | // 250kHz channels achieve higher throughput |
Matt Briggs | 41:9ef4c4d77711 | 79 | // DR6 : SF7 @ 250kHz |
Matt Briggs | 41:9ef4c4d77711 | 80 | // DR0 - DR5 (125kHz channels) available but much slower |
Matt Briggs | 41:9ef4c4d77711 | 81 | tx_frequency = 869850000; |
Matt Briggs | 41:9ef4c4d77711 | 82 | tx_datarate = mDot::DR6; |
Matt Briggs | 41:9ef4c4d77711 | 83 | // 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 | 84 | tx_power = 4; |
Matt Briggs | 41:9ef4c4d77711 | 85 | break; |
Matt Briggs | 41:9ef4c4d77711 | 86 | case mDot::FB_US915: |
Matt Briggs | 41:9ef4c4d77711 | 87 | case mDot::FB_AU915: |
Matt Briggs | 41:9ef4c4d77711 | 88 | default: |
Matt Briggs | 41:9ef4c4d77711 | 89 | // 500kHz channels achieve highest throughput |
Matt Briggs | 41:9ef4c4d77711 | 90 | // DR8 : SF12 @ 500kHz |
Matt Briggs | 41:9ef4c4d77711 | 91 | // DR9 : SF11 @ 500kHz |
Matt Briggs | 41:9ef4c4d77711 | 92 | // DR10 : SF10 @ 500kHz |
Matt Briggs | 41:9ef4c4d77711 | 93 | // DR11 : SF9 @ 500kHz |
Matt Briggs | 41:9ef4c4d77711 | 94 | // DR12 : SF8 @ 500kHz |
Matt Briggs | 41:9ef4c4d77711 | 95 | // DR13 : SF7 @ 500kHz |
Matt Briggs | 41:9ef4c4d77711 | 96 | // DR0 - DR3 (125kHz channels) available but much slower |
Matt Briggs | 41:9ef4c4d77711 | 97 | tx_frequency = 915500000; |
Matt Briggs | 41:9ef4c4d77711 | 98 | tx_datarate = mDot::DR13; |
Matt Briggs | 41:9ef4c4d77711 | 99 | // 915 bands have no duty cycle restrictions, set tx power to max |
Matt Briggs | 41:9ef4c4d77711 | 100 | tx_power = 20; |
Matt Briggs | 41:9ef4c4d77711 | 101 | break; |
Matt Briggs | 41:9ef4c4d77711 | 102 | } |
Matt Briggs | 41:9ef4c4d77711 | 103 | // in PEER_TO_PEER mode there is no join request/response transaction |
Matt Briggs | 41:9ef4c4d77711 | 104 | // as long as both Dots are configured correctly, they should be able to communicate |
Matt Briggs | 41:9ef4c4d77711 | 105 | |
Matt Briggs | 62:9751a8504c82 | 106 | update_peer_to_peer_config(mMemObj.mNetworkAddr, mMemObj.mNetworkSessionKey, |
Matt Briggs | 62:9751a8504c82 | 107 | mMemObj.mDataSessionKey, tx_frequency, tx_datarate, tx_power); |
Matt Briggs | 53:a1563574a980 | 108 | dot->saveConfig(); |
Matt Briggs | 41:9ef4c4d77711 | 109 | return cmdSuccess; |
Matt Briggs | 41:9ef4c4d77711 | 110 | } |
Matt Briggs | 61:8d9efd33cac9 | 111 | CmdResult CommProtocolPeerBrute::configForPairingNetwork() |
Matt Briggs | 61:8d9efd33cac9 | 112 | { |
Matt Briggs | 62:9751a8504c82 | 113 | if (dot == NULL) { |
Matt Briggs | 62:9751a8504c82 | 114 | return cmdError; |
Matt Briggs | 62:9751a8504c82 | 115 | } |
Matt Briggs | 62:9751a8504c82 | 116 | |
Matt Briggs | 63:e1efbe3402d9 | 117 | // myLogInfo("defaulting Dot configuration"); |
Matt Briggs | 62:9751a8504c82 | 118 | // dot->resetConfig(); |
Matt Briggs | 62:9751a8504c82 | 119 | |
Matt Briggs | 62:9751a8504c82 | 120 | // Common Configuration |
Matt Briggs | 62:9751a8504c82 | 121 | dot->setAesEncryption(true); // Enable encryption |
Matt Briggs | 62:9751a8504c82 | 122 | dot->setTxWait(false); |
Matt Briggs | 62:9751a8504c82 | 123 | dot->setAck(0); // Disable Ack |
Matt Briggs | 62:9751a8504c82 | 124 | dot->setClass("C"); // Set class C |
Matt Briggs | 62:9751a8504c82 | 125 | dot->setTxPower(TX_PWR); |
Matt Briggs | 62:9751a8504c82 | 126 | dot->setPreserveSession(false); |
Matt Briggs | 64:46c8819c07cc | 127 | // dot->setDownLinkCounter(0); |
Matt Briggs | 64:46c8819c07cc | 128 | // dot->setUpLinkCounter(1); |
Matt Briggs | 62:9751a8504c82 | 129 | // TODO break out in a utility function |
Matt Briggs | 62:9751a8504c82 | 130 | // update configuration if necessary |
Matt Briggs | 63:e1efbe3402d9 | 131 | myLogDebug("Setting up peer to peer configuration"); |
Matt Briggs | 62:9751a8504c82 | 132 | if (dot->getJoinMode() != mDot::PEER_TO_PEER) { |
Matt Briggs | 63:e1efbe3402d9 | 133 | myLogDebug("changing network join mode to PEER_TO_PEER"); |
Matt Briggs | 62:9751a8504c82 | 134 | if (dot->setJoinMode(mDot::PEER_TO_PEER) != mDot::MDOT_OK) { |
Matt Briggs | 63:e1efbe3402d9 | 135 | myLogError("failed to set network join mode to PEER_TO_PEER"); |
Matt Briggs | 62:9751a8504c82 | 136 | } |
Matt Briggs | 62:9751a8504c82 | 137 | } |
Matt Briggs | 62:9751a8504c82 | 138 | |
Matt Briggs | 61:8d9efd33cac9 | 139 | uint8_t tx_power; |
Matt Briggs | 61:8d9efd33cac9 | 140 | uint8_t tx_datarate; |
Matt Briggs | 61:8d9efd33cac9 | 141 | uint32_t tx_frequency; |
Matt Briggs | 61:8d9efd33cac9 | 142 | uint8_t frequency_band = dot->getFrequencyBand(); |
Matt Briggs | 61:8d9efd33cac9 | 143 | switch (frequency_band) { |
Matt Briggs | 61:8d9efd33cac9 | 144 | case mDot::FB_EU868: |
Matt Briggs | 61:8d9efd33cac9 | 145 | // 250kHz channels achieve higher throughput |
Matt Briggs | 61:8d9efd33cac9 | 146 | // DR6 : SF7 @ 250kHz |
Matt Briggs | 61:8d9efd33cac9 | 147 | // DR0 - DR5 (125kHz channels) available but much slower |
Matt Briggs | 61:8d9efd33cac9 | 148 | tx_frequency = 869850000; |
Matt Briggs | 61:8d9efd33cac9 | 149 | tx_datarate = mDot::DR6; |
Matt Briggs | 61:8d9efd33cac9 | 150 | // the 869850000 frequency is 100% duty cycle if the total power is under 7 dBm - tx power 4 + antenna gain 3 = 7 |
Matt Briggs | 61:8d9efd33cac9 | 151 | tx_power = 4; |
Matt Briggs | 61:8d9efd33cac9 | 152 | break; |
Matt Briggs | 61:8d9efd33cac9 | 153 | case mDot::FB_US915: |
Matt Briggs | 61:8d9efd33cac9 | 154 | case mDot::FB_AU915: |
Matt Briggs | 61:8d9efd33cac9 | 155 | default: |
Matt Briggs | 61:8d9efd33cac9 | 156 | // 500kHz channels achieve highest throughput |
Matt Briggs | 61:8d9efd33cac9 | 157 | // DR8 : SF12 @ 500kHz |
Matt Briggs | 61:8d9efd33cac9 | 158 | // DR9 : SF11 @ 500kHz |
Matt Briggs | 61:8d9efd33cac9 | 159 | // DR10 : SF10 @ 500kHz |
Matt Briggs | 61:8d9efd33cac9 | 160 | // DR11 : SF9 @ 500kHz |
Matt Briggs | 61:8d9efd33cac9 | 161 | // DR12 : SF8 @ 500kHz |
Matt Briggs | 61:8d9efd33cac9 | 162 | // DR13 : SF7 @ 500kHz |
Matt Briggs | 61:8d9efd33cac9 | 163 | // DR0 - DR3 (125kHz channels) available but much slower |
Matt Briggs | 61:8d9efd33cac9 | 164 | tx_frequency = 915500000; |
Matt Briggs | 61:8d9efd33cac9 | 165 | tx_datarate = mDot::DR13; |
Matt Briggs | 61:8d9efd33cac9 | 166 | // 915 bands have no duty cycle restrictions, set tx power to max |
Matt Briggs | 61:8d9efd33cac9 | 167 | tx_power = 20; |
Matt Briggs | 61:8d9efd33cac9 | 168 | break; |
Matt Briggs | 61:8d9efd33cac9 | 169 | } |
Matt Briggs | 61:8d9efd33cac9 | 170 | update_peer_to_peer_config(pair_network_address, pair_network_session_key, pair_data_session_key, tx_frequency, tx_datarate, tx_power); |
Matt Briggs | 62:9751a8504c82 | 171 | dot->saveConfig(); // This is required for network settings to apply |
Matt Briggs | 61:8d9efd33cac9 | 172 | return cmdSuccess; |
Matt Briggs | 61:8d9efd33cac9 | 173 | } |
Matt Briggs | 44:ece6330e9b57 | 174 | void CommProtocolPeerBrute::setTx(bool isTx) |
Matt Briggs | 44:ece6330e9b57 | 175 | { |
Matt Briggs | 44:ece6330e9b57 | 176 | mIsTx = isTx; |
Matt Briggs | 44:ece6330e9b57 | 177 | } |
Matt Briggs | 41:9ef4c4d77711 | 178 | bool CommProtocolPeerBrute::isTx() |
Matt Briggs | 41:9ef4c4d77711 | 179 | { |
Matt Briggs | 41:9ef4c4d77711 | 180 | return mIsTx; |
Matt Briggs | 41:9ef4c4d77711 | 181 | } |
Matt Briggs | 41:9ef4c4d77711 | 182 | CmdResult CommProtocolPeerBrute::clearPair() |
Matt Briggs | 41:9ef4c4d77711 | 183 | { |
Matt Briggs | 61:8d9efd33cac9 | 184 | CmdResult result; |
Matt Briggs | 61:8d9efd33cac9 | 185 | |
Matt Briggs | 61:8d9efd33cac9 | 186 | // TODO generate possibly random channel for new pair |
Matt Briggs | 61:8d9efd33cac9 | 187 | std::vector<uint8_t> key; |
Matt Briggs | 61:8d9efd33cac9 | 188 | key.reserve(16); |
Matt Briggs | 61:8d9efd33cac9 | 189 | |
Matt Briggs | 62:9751a8504c82 | 190 | result = genEncypKey(key, 4, false); |
Matt Briggs | 61:8d9efd33cac9 | 191 | if (result != cmdSuccess) { |
Matt Briggs | 63:e1efbe3402d9 | 192 | myLogError("Error generating network address."); |
Matt Briggs | 61:8d9efd33cac9 | 193 | return cmdError; |
Matt Briggs | 61:8d9efd33cac9 | 194 | } |
Matt Briggs | 62:9751a8504c82 | 195 | mMemObj.setNetworkAddr(key); |
Matt Briggs | 61:8d9efd33cac9 | 196 | |
Matt Briggs | 61:8d9efd33cac9 | 197 | result = genEncypKey(key, 16); |
Matt Briggs | 61:8d9efd33cac9 | 198 | if (result != cmdSuccess) { |
Matt Briggs | 63:e1efbe3402d9 | 199 | myLogError("Error generating network encryption keys."); |
Matt Briggs | 61:8d9efd33cac9 | 200 | return cmdError; |
Matt Briggs | 61:8d9efd33cac9 | 201 | } |
Matt Briggs | 62:9751a8504c82 | 202 | mMemObj.setNetworkSessionKey(key); |
Matt Briggs | 61:8d9efd33cac9 | 203 | |
Matt Briggs | 61:8d9efd33cac9 | 204 | result = genEncypKey(key, 16); |
Matt Briggs | 61:8d9efd33cac9 | 205 | if (result != cmdSuccess) { |
Matt Briggs | 63:e1efbe3402d9 | 206 | myLogError("Error generating data session encryption keys."); |
Matt Briggs | 61:8d9efd33cac9 | 207 | return cmdError; |
Matt Briggs | 61:8d9efd33cac9 | 208 | } |
Matt Briggs | 62:9751a8504c82 | 209 | mMemObj.setDataSessionKey(key); |
Matt Briggs | 62:9751a8504c82 | 210 | |
Matt Briggs | 62:9751a8504c82 | 211 | configForSavedNetwork(); |
Matt Briggs | 62:9751a8504c82 | 212 | writeInfoToNVM(); |
Matt Briggs | 61:8d9efd33cac9 | 213 | |
Matt Briggs | 61:8d9efd33cac9 | 214 | return cmdSuccess; |
Matt Briggs | 41:9ef4c4d77711 | 215 | } |
Matt Briggs | 41:9ef4c4d77711 | 216 | |
Matt Briggs | 41:9ef4c4d77711 | 217 | // TX focused |
Matt Briggs | 65:d546060aa03d | 218 | uint32_t CommProtocolPeerBrute::getSeqNum() |
Matt Briggs | 65:d546060aa03d | 219 | { |
Matt Briggs | 65:d546060aa03d | 220 | return mMemObj.getLastMsgSeq(); |
Matt Briggs | 65:d546060aa03d | 221 | } |
Matt Briggs | 41:9ef4c4d77711 | 222 | CmdResult CommProtocolPeerBrute::send (const std::vector<uint8_t> &msg) |
Matt Briggs | 41:9ef4c4d77711 | 223 | { |
Matt Briggs | 41:9ef4c4d77711 | 224 | if (!dot->getNetworkJoinStatus()) { |
Matt Briggs | 41:9ef4c4d77711 | 225 | join_network(); |
Matt Briggs | 41:9ef4c4d77711 | 226 | } |
Matt Briggs | 63:e1efbe3402d9 | 227 | myLogDebug("Starting TX. Time: %lu", us_ticker_read()); |
Matt Briggs | 53:a1563574a980 | 228 | for(uint8_t i=0;i<nTimesToTx;++i) { |
Matt Briggs | 41:9ef4c4d77711 | 229 | dot->send(msg); |
Matt Briggs | 41:9ef4c4d77711 | 230 | } |
Matt Briggs | 63:e1efbe3402d9 | 231 | myLogDebug("Finished TX. Time: %lu", us_ticker_read()); |
Matt Briggs | 41:9ef4c4d77711 | 232 | return cmdError; |
Matt Briggs | 41:9ef4c4d77711 | 233 | } |
Matt Briggs | 41:9ef4c4d77711 | 234 | |
Matt Briggs | 65:d546060aa03d | 235 | CmdResult CommProtocolPeerBrute::sendAlert(uint16_t data) |
Matt Briggs | 65:d546060aa03d | 236 | { |
Matt Briggs | 65:d546060aa03d | 237 | if (!dot->getNetworkJoinStatus()) { |
Matt Briggs | 65:d546060aa03d | 238 | join_network(); |
Matt Briggs | 65:d546060aa03d | 239 | } |
Matt Briggs | 65:d546060aa03d | 240 | // Request Message |
Matt Briggs | 65:d546060aa03d | 241 | std::vector<uint8_t> msg; |
Matt Briggs | 65:d546060aa03d | 242 | msg.reserve(16); |
Matt Briggs | 65:d546060aa03d | 243 | // Flag 2 Bytes |
Matt Briggs | 65:d546060aa03d | 244 | msg.push_back(0xEF); |
Matt Briggs | 65:d546060aa03d | 245 | msg.push_back(0x10); |
Matt Briggs | 65:d546060aa03d | 246 | // EUI 8 Bytes |
Matt Briggs | 65:d546060aa03d | 247 | std::vector<uint8_t> eui(dot->getDeviceId()); |
Matt Briggs | 65:d546060aa03d | 248 | msg.insert(msg.end(), eui.begin(), eui.end()); |
Matt Briggs | 65:d546060aa03d | 249 | // Data 2 Bytes |
Matt Briggs | 65:d546060aa03d | 250 | appendUint16ToVector(msg, data); |
Matt Briggs | 65:d546060aa03d | 251 | // SeqNum 4 Bytes |
Matt Briggs | 65:d546060aa03d | 252 | appendUint32ToVector(msg, mMemObj.getLastMsgSeq()); |
Matt Briggs | 65:d546060aa03d | 253 | mMemObj.incLastMsgSeq(); |
Matt Briggs | 65:d546060aa03d | 254 | if (dot->send(msg) == mDot::MDOT_OK) {// Can just send once since the RX should be always listening |
Matt Briggs | 65:d546060aa03d | 255 | return cmdSuccess; |
Matt Briggs | 65:d546060aa03d | 256 | } |
Matt Briggs | 65:d546060aa03d | 257 | else { |
Matt Briggs | 65:d546060aa03d | 258 | return cmdError; |
Matt Briggs | 65:d546060aa03d | 259 | } |
Matt Briggs | 65:d546060aa03d | 260 | } |
Matt Briggs | 65:d546060aa03d | 261 | |
Matt Briggs | 41:9ef4c4d77711 | 262 | CmdResult CommProtocolPeerBrute::sendPairReq() |
Matt Briggs | 41:9ef4c4d77711 | 263 | { |
Matt Briggs | 41:9ef4c4d77711 | 264 | if (!dot->getNetworkJoinStatus()) { |
Matt Briggs | 41:9ef4c4d77711 | 265 | join_network(); |
Matt Briggs | 41:9ef4c4d77711 | 266 | } |
Matt Briggs | 61:8d9efd33cac9 | 267 | // Request Message |
Matt Briggs | 61:8d9efd33cac9 | 268 | std::vector<uint8_t> msg; |
Matt Briggs | 61:8d9efd33cac9 | 269 | msg.reserve(16); |
Matt Briggs | 61:8d9efd33cac9 | 270 | // Flag 2 Bytes |
Matt Briggs | 61:8d9efd33cac9 | 271 | msg.push_back(0xFE); |
Matt Briggs | 61:8d9efd33cac9 | 272 | msg.push_back(0x01); |
Matt Briggs | 61:8d9efd33cac9 | 273 | // EUI 8 Bytes |
Matt Briggs | 61:8d9efd33cac9 | 274 | std::vector<uint8_t> eui(dot->getDeviceId()); |
Matt Briggs | 61:8d9efd33cac9 | 275 | msg.insert(msg.end(), eui.begin(), eui.end()); |
Matt Briggs | 61:8d9efd33cac9 | 276 | // Reserved 6 Bytes |
Matt Briggs | 61:8d9efd33cac9 | 277 | for (uint8_t i=0;i<6;i++) { |
Matt Briggs | 61:8d9efd33cac9 | 278 | msg.push_back(0x00); |
Matt Briggs | 61:8d9efd33cac9 | 279 | } |
Matt Briggs | 61:8d9efd33cac9 | 280 | dot->send(msg); // Can just send once since the RX should be always listening |
Matt Briggs | 61:8d9efd33cac9 | 281 | return cmdSuccess; |
Matt Briggs | 41:9ef4c4d77711 | 282 | } |
Matt Briggs | 41:9ef4c4d77711 | 283 | |
Matt Briggs | 41:9ef4c4d77711 | 284 | // RX focused |
Matt Briggs | 41:9ef4c4d77711 | 285 | CmdResult CommProtocolPeerBrute::listen (bool &msgPending) |
Matt Briggs | 41:9ef4c4d77711 | 286 | { |
Matt Briggs | 41:9ef4c4d77711 | 287 | if (!dot->getNetworkJoinStatus()) { |
Matt Briggs | 41:9ef4c4d77711 | 288 | join_network(); |
Matt Briggs | 41:9ef4c4d77711 | 289 | } |
Matt Briggs | 41:9ef4c4d77711 | 290 | |
Matt Briggs | 54:c04d7b6fa075 | 291 | // uint32_t cDwnLink = dot->getDownLinkCounter(); |
Matt Briggs | 41:9ef4c4d77711 | 292 | |
Matt Briggs | 41:9ef4c4d77711 | 293 | wait(TX_TIME/1000.0); // Wait TX_TIME |
Matt Briggs | 41:9ef4c4d77711 | 294 | |
Matt Briggs | 64:46c8819c07cc | 295 | mPrevDownLinkCnt = 0; // FIXME just test code |
Matt Briggs | 54:c04d7b6fa075 | 296 | if (mPrevDownLinkCnt < dot->getDownLinkCounter()) { |
Matt Briggs | 41:9ef4c4d77711 | 297 | msgPending = true; |
Matt Briggs | 41:9ef4c4d77711 | 298 | } |
Matt Briggs | 41:9ef4c4d77711 | 299 | else { |
Matt Briggs | 41:9ef4c4d77711 | 300 | msgPending = false; |
Matt Briggs | 41:9ef4c4d77711 | 301 | } |
Matt Briggs | 54:c04d7b6fa075 | 302 | mPrevDownLinkCnt = dot->getDownLinkCounter(); |
Matt Briggs | 41:9ef4c4d77711 | 303 | return cmdSuccess; // Maybe add timeout as a possible return value |
Matt Briggs | 41:9ef4c4d77711 | 304 | } |
Matt Briggs | 41:9ef4c4d77711 | 305 | |
Matt Briggs | 54:c04d7b6fa075 | 306 | CmdResult CommProtocolPeerBrute::sampleDLC () { |
Matt Briggs | 54:c04d7b6fa075 | 307 | mPrevDownLinkCnt = dot->getDownLinkCounter(); |
Matt Briggs | 54:c04d7b6fa075 | 308 | return cmdSuccess; |
Matt Briggs | 54:c04d7b6fa075 | 309 | } |
Matt Briggs | 54:c04d7b6fa075 | 310 | |
Matt Briggs | 41:9ef4c4d77711 | 311 | CmdResult CommProtocolPeerBrute::recv (std::vector<uint8_t> &msg) |
Matt Briggs | 41:9ef4c4d77711 | 312 | { |
Matt Briggs | 41:9ef4c4d77711 | 313 | dot->recv(msg); |
Matt Briggs | 41:9ef4c4d77711 | 314 | return cmdSuccess; |
Matt Briggs | 41:9ef4c4d77711 | 315 | } |
Matt Briggs | 41:9ef4c4d77711 | 316 | |
Matt Briggs | 65:d546060aa03d | 317 | CmdResult CommProtocolPeerBrute::recvAlert (std::vector<uint8_t> &eui, uint16_t &data, uint32_t &seqNum) |
Matt Briggs | 65:d546060aa03d | 318 | { |
Matt Briggs | 65:d546060aa03d | 319 | std::vector<uint8_t> alertMsg; |
Matt Briggs | 65:d546060aa03d | 320 | dot->recv(alertMsg); |
Matt Briggs | 65:d546060aa03d | 321 | if (alertMsg[0] != 0xEF || alertMsg[1] != 0x10) { |
Matt Briggs | 65:d546060aa03d | 322 | myLogError("Invalid alert message flag."); |
Matt Briggs | 65:d546060aa03d | 323 | return cmdError; |
Matt Briggs | 65:d546060aa03d | 324 | } |
Matt Briggs | 65:d546060aa03d | 325 | if (alertMsg.size() < 16) { |
Matt Briggs | 65:d546060aa03d | 326 | myLogError("Alert message too short"); |
Matt Briggs | 65:d546060aa03d | 327 | return cmdError; |
Matt Briggs | 65:d546060aa03d | 328 | } |
Matt Briggs | 65:d546060aa03d | 329 | eui.clear(); |
Matt Briggs | 65:d546060aa03d | 330 | eui.assign(alertMsg.begin()+2, alertMsg.begin()+2+8); |
Matt Briggs | 65:d546060aa03d | 331 | |
Matt Briggs | 65:d546060aa03d | 332 | data = (alertMsg[10] << 8) + alertMsg[11]; |
Matt Briggs | 65:d546060aa03d | 333 | |
Matt Briggs | 65:d546060aa03d | 334 | seqNum = alertMsg[12] << 24; |
Matt Briggs | 65:d546060aa03d | 335 | seqNum += alertMsg[13] << 16; |
Matt Briggs | 65:d546060aa03d | 336 | seqNum += alertMsg[14] << 8; |
Matt Briggs | 65:d546060aa03d | 337 | seqNum += alertMsg[15]; |
Matt Briggs | 65:d546060aa03d | 338 | |
Matt Briggs | 65:d546060aa03d | 339 | return cmdSuccess; |
Matt Briggs | 65:d546060aa03d | 340 | } |
Matt Briggs | 65:d546060aa03d | 341 | |
Matt Briggs | 41:9ef4c4d77711 | 342 | CmdResult CommProtocolPeerBrute::waitForPairing(float waitTime) |
Matt Briggs | 41:9ef4c4d77711 | 343 | { |
Matt Briggs | 61:8d9efd33cac9 | 344 | float t = 0.0; |
Matt Briggs | 61:8d9efd33cac9 | 345 | bool msgPending; |
Matt Briggs | 61:8d9efd33cac9 | 346 | do { |
Matt Briggs | 61:8d9efd33cac9 | 347 | listen(msgPending); |
Matt Briggs | 61:8d9efd33cac9 | 348 | t += TX_TIME/1000.0; |
Matt Briggs | 61:8d9efd33cac9 | 349 | } |
Matt Briggs | 61:8d9efd33cac9 | 350 | while (t < waitTime && !msgPending); |
Matt Briggs | 61:8d9efd33cac9 | 351 | |
Matt Briggs | 61:8d9efd33cac9 | 352 | if (!msgPending) { |
Matt Briggs | 61:8d9efd33cac9 | 353 | return cmdTimeout; |
Matt Briggs | 61:8d9efd33cac9 | 354 | } |
Matt Briggs | 61:8d9efd33cac9 | 355 | |
Matt Briggs | 61:8d9efd33cac9 | 356 | wait(1.0); // Wait just a little so it is clear for requester that it is a pair message |
Matt Briggs | 61:8d9efd33cac9 | 357 | return sendPairAccepted(); |
Matt Briggs | 41:9ef4c4d77711 | 358 | } |
Matt Briggs | 41:9ef4c4d77711 | 359 | |
Matt Briggs | 41:9ef4c4d77711 | 360 | CmdResult CommProtocolPeerBrute::sendPairAccepted() |
Matt Briggs | 41:9ef4c4d77711 | 361 | { |
Matt Briggs | 61:8d9efd33cac9 | 362 | bool sendResult; |
Matt Briggs | 61:8d9efd33cac9 | 363 | // Request Message |
Matt Briggs | 61:8d9efd33cac9 | 364 | std::vector<uint8_t> *msg = new std::vector<uint8_t>; |
Matt Briggs | 61:8d9efd33cac9 | 365 | msg->reserve(34); |
Matt Briggs | 61:8d9efd33cac9 | 366 | // Flag |
Matt Briggs | 61:8d9efd33cac9 | 367 | msg->push_back(0xFD); |
Matt Briggs | 61:8d9efd33cac9 | 368 | msg->push_back(0x02); |
Matt Briggs | 63:e1efbe3402d9 | 369 | myLogDebug("flag msg size %d", msg->size()); |
Matt Briggs | 61:8d9efd33cac9 | 370 | // EUI |
Matt Briggs | 61:8d9efd33cac9 | 371 | std::vector<uint8_t> *eui = new std::vector<uint8_t>(dot->getDeviceId()); |
Matt Briggs | 61:8d9efd33cac9 | 372 | msg->insert(msg->end(),eui->begin(), eui->end()); |
Matt Briggs | 61:8d9efd33cac9 | 373 | delete eui; |
Matt Briggs | 63:e1efbe3402d9 | 374 | myLogDebug("eui msg size %d", msg->size()); |
Matt Briggs | 61:8d9efd33cac9 | 375 | |
Matt Briggs | 61:8d9efd33cac9 | 376 | // Reserved for Freq |
Matt Briggs | 61:8d9efd33cac9 | 377 | for(uint8_t i=0;i<4;i++) { |
Matt Briggs | 61:8d9efd33cac9 | 378 | msg->push_back(0x00); |
Matt Briggs | 61:8d9efd33cac9 | 379 | } |
Matt Briggs | 63:e1efbe3402d9 | 380 | myLogDebug("freq msg size %d", msg->size()); |
Matt Briggs | 61:8d9efd33cac9 | 381 | |
Matt Briggs | 62:9751a8504c82 | 382 | // Network Address |
Matt Briggs | 62:9751a8504c82 | 383 | std::vector<uint8_t> *networkAddr = new std::vector<uint8_t>; |
Matt Briggs | 62:9751a8504c82 | 384 | mMemObj.getNetworkAddr(*networkAddr); |
Matt Briggs | 62:9751a8504c82 | 385 | msg->insert(msg->end(),networkAddr->begin(), networkAddr->end()); |
Matt Briggs | 62:9751a8504c82 | 386 | delete networkAddr; |
Matt Briggs | 63:e1efbe3402d9 | 387 | myLogDebug("netAddr msg size %d", msg->size()); |
Matt Briggs | 61:8d9efd33cac9 | 388 | |
Matt Briggs | 62:9751a8504c82 | 389 | // Network session key |
Matt Briggs | 62:9751a8504c82 | 390 | std::vector<uint8_t> *networkSessionKey = new std::vector<uint8_t>; |
Matt Briggs | 62:9751a8504c82 | 391 | mMemObj.getNetworkSessionKey(*networkSessionKey); |
Matt Briggs | 62:9751a8504c82 | 392 | msg->insert(msg->end(),networkSessionKey->begin(), networkSessionKey->end()); |
Matt Briggs | 62:9751a8504c82 | 393 | delete networkSessionKey; |
Matt Briggs | 63:e1efbe3402d9 | 394 | myLogDebug("netSessionKey msg size %d", msg->size()); |
Matt Briggs | 61:8d9efd33cac9 | 395 | |
Matt Briggs | 61:8d9efd33cac9 | 396 | // Data session key |
Matt Briggs | 62:9751a8504c82 | 397 | std::vector<uint8_t> *dataSessionKey = new std::vector<uint8_t>; |
Matt Briggs | 62:9751a8504c82 | 398 | mMemObj.getDataSessionKey(*dataSessionKey); |
Matt Briggs | 61:8d9efd33cac9 | 399 | msg->insert(msg->end(),dataSessionKey->begin(), dataSessionKey->end()); |
Matt Briggs | 61:8d9efd33cac9 | 400 | delete dataSessionKey; |
Matt Briggs | 61:8d9efd33cac9 | 401 | |
Matt Briggs | 63:e1efbe3402d9 | 402 | myLogDebug("msg size %d", msg->size()); |
Matt Briggs | 61:8d9efd33cac9 | 403 | // Only send once since requester should be listening always |
Matt Briggs | 61:8d9efd33cac9 | 404 | sendResult = mDot::MDOT_OK == dot->send(*msg); |
Matt Briggs | 61:8d9efd33cac9 | 405 | delete msg; |
Matt Briggs | 61:8d9efd33cac9 | 406 | if (sendResult) |
Matt Briggs | 61:8d9efd33cac9 | 407 | return cmdSuccess; |
Matt Briggs | 61:8d9efd33cac9 | 408 | else |
Matt Briggs | 61:8d9efd33cac9 | 409 | return cmdError; |
Matt Briggs | 61:8d9efd33cac9 | 410 | } |
Matt Briggs | 61:8d9efd33cac9 | 411 | |
Matt Briggs | 61:8d9efd33cac9 | 412 | CmdResult CommProtocolPeerBrute::waitForAccept(float waitTime) |
Matt Briggs | 61:8d9efd33cac9 | 413 | { |
Matt Briggs | 61:8d9efd33cac9 | 414 | float t = 0.0; |
Matt Briggs | 61:8d9efd33cac9 | 415 | bool msgPending = false; |
Matt Briggs | 61:8d9efd33cac9 | 416 | do { |
Matt Briggs | 61:8d9efd33cac9 | 417 | listen(msgPending); |
Matt Briggs | 61:8d9efd33cac9 | 418 | t += TX_TIME/1000.0; |
Matt Briggs | 61:8d9efd33cac9 | 419 | } |
Matt Briggs | 61:8d9efd33cac9 | 420 | while (t < waitTime && !msgPending); |
Matt Briggs | 61:8d9efd33cac9 | 421 | |
Matt Briggs | 61:8d9efd33cac9 | 422 | if (!msgPending) { |
Matt Briggs | 61:8d9efd33cac9 | 423 | return cmdTimeout; |
Matt Briggs | 61:8d9efd33cac9 | 424 | } |
Matt Briggs | 62:9751a8504c82 | 425 | |
Matt Briggs | 62:9751a8504c82 | 426 | std::vector<uint8_t> acceptMsg; |
Matt Briggs | 62:9751a8504c82 | 427 | dot->recv(acceptMsg); |
Matt Briggs | 62:9751a8504c82 | 428 | if (acceptMsg[0] != 0xFD || acceptMsg[1] != 0x02) { |
Matt Briggs | 63:e1efbe3402d9 | 429 | myLogError("Invalid accept message flag."); |
Matt Briggs | 62:9751a8504c82 | 430 | return cmdError; |
Matt Briggs | 62:9751a8504c82 | 431 | } |
Matt Briggs | 62:9751a8504c82 | 432 | |
Matt Briggs | 62:9751a8504c82 | 433 | std::vector<uint8_t> *netAddr = new std::vector<uint8_t>(acceptMsg.begin()+0x0E, acceptMsg.begin()+0x12); |
Matt Briggs | 62:9751a8504c82 | 434 | mMemObj.setNetworkAddr(*netAddr); |
Matt Briggs | 62:9751a8504c82 | 435 | delete netAddr; |
Matt Briggs | 62:9751a8504c82 | 436 | |
Matt Briggs | 62:9751a8504c82 | 437 | std::vector<uint8_t> *netSessionKey = new std::vector<uint8_t>(acceptMsg.begin()+0x12, acceptMsg.begin()+0x22); |
Matt Briggs | 62:9751a8504c82 | 438 | mMemObj.setNetworkSessionKey(*netSessionKey); |
Matt Briggs | 62:9751a8504c82 | 439 | delete netSessionKey; |
Matt Briggs | 62:9751a8504c82 | 440 | |
Matt Briggs | 62:9751a8504c82 | 441 | std::vector<uint8_t> *dataSessionKey = new std::vector<uint8_t>(acceptMsg.begin()+0x22, acceptMsg.begin()+0x32); |
Matt Briggs | 62:9751a8504c82 | 442 | mMemObj.setDataSessionKey(*dataSessionKey); |
Matt Briggs | 62:9751a8504c82 | 443 | delete dataSessionKey; |
Matt Briggs | 62:9751a8504c82 | 444 | |
Matt Briggs | 62:9751a8504c82 | 445 | writeInfoToNVM(); |
Matt Briggs | 61:8d9efd33cac9 | 446 | |
Matt Briggs | 61:8d9efd33cac9 | 447 | return cmdSuccess; |
Matt Briggs | 41:9ef4c4d77711 | 448 | } |
Matt Briggs | 41:9ef4c4d77711 | 449 | |
Matt Briggs | 41:9ef4c4d77711 | 450 | // xDot Peer to Peer Specific |
Matt Briggs | 41:9ef4c4d77711 | 451 | uint32_t CommProtocolPeerBrute::getDLC() |
Matt Briggs | 41:9ef4c4d77711 | 452 | { |
Matt Briggs | 41:9ef4c4d77711 | 453 | return dot->getDownLinkCounter(); |
Matt Briggs | 41:9ef4c4d77711 | 454 | } |
Matt Briggs | 41:9ef4c4d77711 | 455 | |
Matt Briggs | 41:9ef4c4d77711 | 456 | uint32_t CommProtocolPeerBrute::getULC() |
Matt Briggs | 41:9ef4c4d77711 | 457 | { |
Matt Briggs | 41:9ef4c4d77711 | 458 | return dot->getUpLinkCounter(); |
Matt Briggs | 41:9ef4c4d77711 | 459 | } |
Matt Briggs | 41:9ef4c4d77711 | 460 | |
Matt Briggs | 41:9ef4c4d77711 | 461 | // private: |
Matt Briggs | 41:9ef4c4d77711 | 462 | |
Matt Briggs | 41:9ef4c4d77711 | 463 | CmdResult CommProtocolPeerBrute::readInfoFromNVM() |
Matt Briggs | 41:9ef4c4d77711 | 464 | { |
Matt Briggs | 55:79ab0bbc5008 | 465 | bool nvmReadResult; |
Matt Briggs | 55:79ab0bbc5008 | 466 | uint8_t *data = new uint8_t [PROTOCOL_NVM_SIZE]; |
Matt Briggs | 55:79ab0bbc5008 | 467 | |
Matt Briggs | 55:79ab0bbc5008 | 468 | nvmReadResult = dot->nvmRead(PROTOCOL_NVM_START_ADDR, data, PROTOCOL_NVM_SIZE); |
Matt Briggs | 55:79ab0bbc5008 | 469 | if (!nvmReadResult) { |
Matt Briggs | 55:79ab0bbc5008 | 470 | delete [] data; |
Matt Briggs | 55:79ab0bbc5008 | 471 | return cmdError; |
Matt Briggs | 55:79ab0bbc5008 | 472 | } |
Matt Briggs | 55:79ab0bbc5008 | 473 | mMemObj.fromBytes(data, PROTOCOL_NVM_SIZE); |
Matt Briggs | 55:79ab0bbc5008 | 474 | delete [] data; |
Matt Briggs | 55:79ab0bbc5008 | 475 | if (!mMemObj.validProtocolFlag()) { |
Matt Briggs | 63:e1efbe3402d9 | 476 | myLogWarning("Invalid Protocol Flag. Using default values."); |
Matt Briggs | 55:79ab0bbc5008 | 477 | mMemObj.setDefaults(); |
Matt Briggs | 62:9751a8504c82 | 478 | return cmdError; |
Matt Briggs | 55:79ab0bbc5008 | 479 | } |
Matt Briggs | 55:79ab0bbc5008 | 480 | else if (!mMemObj.validProtocolRev()) { |
Matt Briggs | 63:e1efbe3402d9 | 481 | myLogWarning("Invalid Protocol Rev. Using default values."); |
Matt Briggs | 55:79ab0bbc5008 | 482 | mMemObj.setDefaults(); |
Matt Briggs | 62:9751a8504c82 | 483 | return cmdError; |
Matt Briggs | 55:79ab0bbc5008 | 484 | } |
Matt Briggs | 55:79ab0bbc5008 | 485 | return cmdSuccess; |
Matt Briggs | 41:9ef4c4d77711 | 486 | } |
Matt Briggs | 41:9ef4c4d77711 | 487 | |
Matt Briggs | 41:9ef4c4d77711 | 488 | CmdResult CommProtocolPeerBrute::writeInfoToNVM() |
Matt Briggs | 41:9ef4c4d77711 | 489 | { |
Matt Briggs | 55:79ab0bbc5008 | 490 | uint8_t *data = new uint8_t [PROTOCOL_NVM_SIZE]; |
Matt Briggs | 55:79ab0bbc5008 | 491 | uint8_t size = PROTOCOL_NVM_SIZE; |
Matt Briggs | 55:79ab0bbc5008 | 492 | mMemObj.toBytes(data, size); |
Matt Briggs | 55:79ab0bbc5008 | 493 | dot->nvmWrite(PROTOCOL_NVM_START_ADDR, data, PROTOCOL_NVM_SIZE); |
Matt Briggs | 55:79ab0bbc5008 | 494 | |
Matt Briggs | 55:79ab0bbc5008 | 495 | delete [] data; |
Matt Briggs | 55:79ab0bbc5008 | 496 | return cmdSuccess; |
Matt Briggs | 41:9ef4c4d77711 | 497 | } |
Matt Briggs | 41:9ef4c4d77711 | 498 | |
Matt Briggs | 41:9ef4c4d77711 | 499 | CmdResult CommProtocolPeerBrute::resetCounters() |
Matt Briggs | 41:9ef4c4d77711 | 500 | { |
Matt Briggs | 41:9ef4c4d77711 | 501 | dot->setDownLinkCounter(0); |
Matt Briggs | 64:46c8819c07cc | 502 | dot->setUpLinkCounter(1); |
Matt Briggs | 41:9ef4c4d77711 | 503 | return cmdSuccess; |
Matt Briggs | 41:9ef4c4d77711 | 504 | } |
Matt Briggs | 41:9ef4c4d77711 | 505 | |
Matt Briggs | 61:8d9efd33cac9 | 506 | CmdResult CommProtocolPeerBrute::genEncypKey(std::vector<uint8_t> &newKey, uint8_t keySize) |
Matt Briggs | 41:9ef4c4d77711 | 507 | { |
Matt Briggs | 62:9751a8504c82 | 508 | return genEncypKey(newKey, keySize, true); // Default allow zero |
Matt Briggs | 62:9751a8504c82 | 509 | } |
Matt Briggs | 62:9751a8504c82 | 510 | CmdResult CommProtocolPeerBrute::genEncypKey(std::vector<uint8_t> &newKey, uint8_t keySize, bool allowZero) |
Matt Briggs | 62:9751a8504c82 | 511 | { |
Matt Briggs | 61:8d9efd33cac9 | 512 | newKey.clear(); |
Matt Briggs | 61:8d9efd33cac9 | 513 | for (uint8_t i=0;i<keySize; i++){ |
Matt Briggs | 62:9751a8504c82 | 514 | if (allowZero) { |
Matt Briggs | 62:9751a8504c82 | 515 | newKey.push_back(dot->getRadioRandom() & 0xFF); |
Matt Briggs | 62:9751a8504c82 | 516 | } |
Matt Briggs | 62:9751a8504c82 | 517 | else { |
Matt Briggs | 62:9751a8504c82 | 518 | newKey.push_back((dot->getRadioRandom() % 254) + 1); |
Matt Briggs | 62:9751a8504c82 | 519 | } |
Matt Briggs | 61:8d9efd33cac9 | 520 | } |
Matt Briggs | 61:8d9efd33cac9 | 521 | return cmdSuccess; |
Matt Briggs | 41:9ef4c4d77711 | 522 | } |
Matt Briggs | 55:79ab0bbc5008 | 523 | |
Matt Briggs | 63:e1efbe3402d9 | 524 | void CommProtocolPeerBrute::printDotConfig() |
Matt Briggs | 63:e1efbe3402d9 | 525 | { |
Matt Briggs | 63:e1efbe3402d9 | 526 | // display configuration and library version information |
Matt Briggs | 63:e1efbe3402d9 | 527 | myLogInfo("====================="); |
Matt Briggs | 63:e1efbe3402d9 | 528 | myLogInfo("general configuration"); |
Matt Briggs | 63:e1efbe3402d9 | 529 | myLogInfo("====================="); |
Matt Briggs | 63:e1efbe3402d9 | 530 | myLogInfo("version ------------------ %s", dot->getId().c_str()); |
Matt Briggs | 63:e1efbe3402d9 | 531 | myLogInfo("device ID/EUI ------------ %s", mts::Text::bin2hexString(dot->getDeviceId()).c_str()); |
Matt Briggs | 63:e1efbe3402d9 | 532 | myLogInfo("frequency band ----------- %s", mDot::FrequencyBandStr(dot->getFrequencyBand()).c_str()); |
Matt Briggs | 63:e1efbe3402d9 | 533 | if (dot->getFrequencySubBand() != mDot::FB_EU868) { |
Matt Briggs | 63:e1efbe3402d9 | 534 | myLogInfo("frequency sub band ------- %u", dot->getFrequencySubBand()); |
Matt Briggs | 63:e1efbe3402d9 | 535 | } |
Matt Briggs | 63:e1efbe3402d9 | 536 | myLogInfo("public network ----------- %s", dot->getPublicNetwork() ? "on" : "off"); |
Matt Briggs | 63:e1efbe3402d9 | 537 | myLogInfo("========================="); |
Matt Briggs | 63:e1efbe3402d9 | 538 | myLogInfo("credentials configuration"); |
Matt Briggs | 63:e1efbe3402d9 | 539 | myLogInfo("========================="); |
Matt Briggs | 63:e1efbe3402d9 | 540 | myLogInfo("device class ------------- %s", dot->getClass().c_str()); |
Matt Briggs | 63:e1efbe3402d9 | 541 | myLogInfo("network join mode -------- %s", mDot::JoinModeStr(dot->getJoinMode()).c_str()); |
Matt Briggs | 63:e1efbe3402d9 | 542 | if (dot->getJoinMode() == mDot::MANUAL || dot->getJoinMode() == mDot::PEER_TO_PEER) { |
Matt Briggs | 63:e1efbe3402d9 | 543 | myLogInfo("network address ---------- %s", mts::Text::bin2hexString(dot->getNetworkAddress()).c_str()); |
Matt Briggs | 63:e1efbe3402d9 | 544 | myLogInfo("network session key------- %s", mts::Text::bin2hexString(dot->getNetworkSessionKey()).c_str()); |
Matt Briggs | 63:e1efbe3402d9 | 545 | myLogInfo("data session key---------- %s", mts::Text::bin2hexString(dot->getDataSessionKey()).c_str()); |
Matt Briggs | 63:e1efbe3402d9 | 546 | } else { |
Matt Briggs | 63:e1efbe3402d9 | 547 | myLogInfo("network name ------------- %s", dot->getNetworkName().c_str()); |
Matt Briggs | 63:e1efbe3402d9 | 548 | myLogInfo("network phrase ----------- %s", dot->getNetworkPassphrase().c_str()); |
Matt Briggs | 63:e1efbe3402d9 | 549 | myLogInfo("network EUI -------------- %s", mts::Text::bin2hexString(dot->getNetworkId()).c_str()); |
Matt Briggs | 63:e1efbe3402d9 | 550 | myLogInfo("network KEY -------------- %s", mts::Text::bin2hexString(dot->getNetworkKey()).c_str()); |
Matt Briggs | 63:e1efbe3402d9 | 551 | } |
Matt Briggs | 63:e1efbe3402d9 | 552 | myLogInfo("========================"); |
Matt Briggs | 63:e1efbe3402d9 | 553 | myLogInfo("communication parameters"); |
Matt Briggs | 63:e1efbe3402d9 | 554 | myLogInfo("========================"); |
Matt Briggs | 63:e1efbe3402d9 | 555 | if (dot->getJoinMode() == mDot::PEER_TO_PEER) { |
Matt Briggs | 63:e1efbe3402d9 | 556 | myLogInfo("TX frequency ------------- %lu", dot->getTxFrequency()); |
Matt Briggs | 63:e1efbe3402d9 | 557 | } else { |
Matt Briggs | 63:e1efbe3402d9 | 558 | myLogInfo("acks --------------------- %s, %u attempts", dot->getAck() > 0 ? "on" : "off", dot->getAck()); |
Matt Briggs | 63:e1efbe3402d9 | 559 | } |
Matt Briggs | 63:e1efbe3402d9 | 560 | myLogInfo("TX datarate -------------- %s", mDot::DataRateStr(dot->getTxDataRate()).c_str()); |
Matt Briggs | 63:e1efbe3402d9 | 561 | myLogInfo("TX power ----------------- %lu dBm", dot->getTxPower()); |
Matt Briggs | 63:e1efbe3402d9 | 562 | myLogInfo("atnenna gain ------------- %u dBm", dot->getAntennaGain()); |
Matt Briggs | 63:e1efbe3402d9 | 563 | } |
Matt Briggs | 63:e1efbe3402d9 | 564 | |
Matt Briggs | 55:79ab0bbc5008 | 565 | // NvmProtocolObj |
Matt Briggs | 55:79ab0bbc5008 | 566 | NvmProtocolObj::NvmProtocolObj() |
Matt Briggs | 55:79ab0bbc5008 | 567 | { |
Matt Briggs | 55:79ab0bbc5008 | 568 | setDefaults(); |
Matt Briggs | 55:79ab0bbc5008 | 569 | } |
Matt Briggs | 55:79ab0bbc5008 | 570 | void NvmProtocolObj::setDefaults() |
Matt Briggs | 55:79ab0bbc5008 | 571 | { |
Matt Briggs | 55:79ab0bbc5008 | 572 | mProtocolFlag = PROTOCOL_FLAG; |
Matt Briggs | 55:79ab0bbc5008 | 573 | mProtocolRev = PROTOCOL_REV; |
Matt Briggs | 62:9751a8504c82 | 574 | mFreq = 915500000; |
Matt Briggs | 62:9751a8504c82 | 575 | std::memset(mNetworkAddr, 0x00, sizeof(mNetworkAddr)); |
Matt Briggs | 62:9751a8504c82 | 576 | std::memset(mNetworkSessionKey, 0x00, sizeof(mNetworkSessionKey)); |
Matt Briggs | 62:9751a8504c82 | 577 | std::memset(mDataSessionKey, 0x00, sizeof(mDataSessionKey)); |
Matt Briggs | 55:79ab0bbc5008 | 578 | mLogicalAddr = 0x00000000; |
Matt Briggs | 55:79ab0bbc5008 | 579 | mSeqNum = 0x00000000; |
Matt Briggs | 55:79ab0bbc5008 | 580 | } |
Matt Briggs | 55:79ab0bbc5008 | 581 | CmdResult NvmProtocolObj::fromBytes(uint8_t *data, uint8_t size) |
Matt Briggs | 55:79ab0bbc5008 | 582 | { |
Matt Briggs | 55:79ab0bbc5008 | 583 | if (size != PROTOCOL_NVM_SIZE) { |
Matt Briggs | 55:79ab0bbc5008 | 584 | return cmdError; |
Matt Briggs | 55:79ab0bbc5008 | 585 | } |
Matt Briggs | 55:79ab0bbc5008 | 586 | |
Matt Briggs | 55:79ab0bbc5008 | 587 | mProtocolFlag = *((uint16_t *) (data)); |
Matt Briggs | 62:9751a8504c82 | 588 | mProtocolRev = *((uint16_t *) (data+0x02)); |
Matt Briggs | 62:9751a8504c82 | 589 | mFreq = *((uint32_t *) (data+0x04)); |
Matt Briggs | 62:9751a8504c82 | 590 | std::memcpy(&mNetworkAddr, data+0x08, 4); |
Matt Briggs | 62:9751a8504c82 | 591 | std::memcpy(&mNetworkSessionKey, data+0x10, 16); |
Matt Briggs | 62:9751a8504c82 | 592 | std::memcpy(&mDataSessionKey, data+0x20, 16); |
Matt Briggs | 62:9751a8504c82 | 593 | mLogicalAddr = *((uint32_t *) (data+0x30)); |
Matt Briggs | 62:9751a8504c82 | 594 | mSeqNum = *((uint32_t *) (data+0x32)); |
Matt Briggs | 55:79ab0bbc5008 | 595 | |
Matt Briggs | 55:79ab0bbc5008 | 596 | return cmdSuccess; |
Matt Briggs | 55:79ab0bbc5008 | 597 | } |
Matt Briggs | 55:79ab0bbc5008 | 598 | CmdResult NvmProtocolObj::toBytes(uint8_t *data, uint8_t &size) { |
Matt Briggs | 55:79ab0bbc5008 | 599 | // TODO check data size |
Matt Briggs | 55:79ab0bbc5008 | 600 | |
Matt Briggs | 55:79ab0bbc5008 | 601 | *((uint16_t *) (data)) = mProtocolFlag; |
Matt Briggs | 55:79ab0bbc5008 | 602 | *((uint16_t *) (data+2)) = mProtocolRev; |
Matt Briggs | 62:9751a8504c82 | 603 | *((uint32_t *) (data+0x04)) = mFreq; |
Matt Briggs | 62:9751a8504c82 | 604 | std::memcpy(data+0x08, &mNetworkAddr, 4); |
Matt Briggs | 62:9751a8504c82 | 605 | std::memcpy(data+0x10, &mNetworkSessionKey, 16); |
Matt Briggs | 62:9751a8504c82 | 606 | std::memcpy(data+0x20, &mDataSessionKey, 16); |
Matt Briggs | 62:9751a8504c82 | 607 | *((uint32_t *) (data+0x30)) = mLogicalAddr; |
Matt Briggs | 62:9751a8504c82 | 608 | *((uint32_t *) (data+0x32)) = mSeqNum; |
Matt Briggs | 55:79ab0bbc5008 | 609 | |
Matt Briggs | 55:79ab0bbc5008 | 610 | size = PROTOCOL_NVM_SIZE; |
Matt Briggs | 55:79ab0bbc5008 | 611 | |
Matt Briggs | 55:79ab0bbc5008 | 612 | return cmdSuccess; |
Matt Briggs | 55:79ab0bbc5008 | 613 | } |
Matt Briggs | 55:79ab0bbc5008 | 614 | uint16_t NvmProtocolObj::getProtocolFlag() |
Matt Briggs | 55:79ab0bbc5008 | 615 | { |
Matt Briggs | 55:79ab0bbc5008 | 616 | return mProtocolFlag; |
Matt Briggs | 55:79ab0bbc5008 | 617 | } |
Matt Briggs | 55:79ab0bbc5008 | 618 | bool NvmProtocolObj::validProtocolFlag() |
Matt Briggs | 55:79ab0bbc5008 | 619 | { |
Matt Briggs | 55:79ab0bbc5008 | 620 | return mProtocolFlag == PROTOCOL_FLAG; |
Matt Briggs | 55:79ab0bbc5008 | 621 | } |
Matt Briggs | 55:79ab0bbc5008 | 622 | uint16_t NvmProtocolObj::getProtocolRev() |
Matt Briggs | 55:79ab0bbc5008 | 623 | { |
Matt Briggs | 55:79ab0bbc5008 | 624 | return mProtocolRev; |
Matt Briggs | 55:79ab0bbc5008 | 625 | } |
Matt Briggs | 55:79ab0bbc5008 | 626 | bool NvmProtocolObj::validProtocolRev() |
Matt Briggs | 55:79ab0bbc5008 | 627 | { |
Matt Briggs | 55:79ab0bbc5008 | 628 | return mProtocolRev == PROTOCOL_REV; |
Matt Briggs | 55:79ab0bbc5008 | 629 | } |
Matt Briggs | 62:9751a8504c82 | 630 | void NvmProtocolObj::getNetworkAddr(std::vector<uint8_t> &addr) |
Matt Briggs | 62:9751a8504c82 | 631 | { |
Matt Briggs | 62:9751a8504c82 | 632 | addr.clear(); |
Matt Briggs | 62:9751a8504c82 | 633 | addr.assign(mNetworkAddr, mNetworkAddr+sizeof(mNetworkAddr)); |
Matt Briggs | 62:9751a8504c82 | 634 | } |
Matt Briggs | 62:9751a8504c82 | 635 | void NvmProtocolObj::setNetworkAddr(const std::vector<uint8_t> &addr) |
Matt Briggs | 62:9751a8504c82 | 636 | { |
Matt Briggs | 62:9751a8504c82 | 637 | if (addr.size() > sizeof(mNetworkAddr)) { |
Matt Briggs | 62:9751a8504c82 | 638 | // TODO log error |
Matt Briggs | 62:9751a8504c82 | 639 | return; |
Matt Briggs | 62:9751a8504c82 | 640 | } |
Matt Briggs | 62:9751a8504c82 | 641 | for (uint8_t i=0; i < sizeof(mNetworkAddr); i++) { |
Matt Briggs | 62:9751a8504c82 | 642 | mNetworkAddr[i] = addr[i]; |
Matt Briggs | 62:9751a8504c82 | 643 | } |
Matt Briggs | 62:9751a8504c82 | 644 | } |
Matt Briggs | 62:9751a8504c82 | 645 | void NvmProtocolObj::getNetworkSessionKey(std::vector<uint8_t> &key) |
Matt Briggs | 62:9751a8504c82 | 646 | { |
Matt Briggs | 62:9751a8504c82 | 647 | key.clear(); |
Matt Briggs | 62:9751a8504c82 | 648 | key.assign(mNetworkSessionKey, mNetworkSessionKey+sizeof(mNetworkSessionKey)); |
Matt Briggs | 62:9751a8504c82 | 649 | } |
Matt Briggs | 62:9751a8504c82 | 650 | void NvmProtocolObj::setNetworkSessionKey(const std::vector<uint8_t> &key) |
Matt Briggs | 62:9751a8504c82 | 651 | { |
Matt Briggs | 62:9751a8504c82 | 652 | if (key.size() > sizeof(mNetworkSessionKey)) { |
Matt Briggs | 62:9751a8504c82 | 653 | // TODO log error |
Matt Briggs | 62:9751a8504c82 | 654 | return; |
Matt Briggs | 62:9751a8504c82 | 655 | } |
Matt Briggs | 62:9751a8504c82 | 656 | for (uint8_t i=0; i < sizeof(mNetworkSessionKey); i++) { |
Matt Briggs | 62:9751a8504c82 | 657 | mNetworkSessionKey[i] = key[i]; |
Matt Briggs | 62:9751a8504c82 | 658 | } |
Matt Briggs | 62:9751a8504c82 | 659 | } |
Matt Briggs | 62:9751a8504c82 | 660 | void NvmProtocolObj::getDataSessionKey(std::vector<uint8_t> &key) |
Matt Briggs | 62:9751a8504c82 | 661 | { |
Matt Briggs | 62:9751a8504c82 | 662 | key.clear(); |
Matt Briggs | 62:9751a8504c82 | 663 | key.assign(mDataSessionKey, mDataSessionKey+sizeof(mDataSessionKey)); |
Matt Briggs | 62:9751a8504c82 | 664 | } |
Matt Briggs | 62:9751a8504c82 | 665 | void NvmProtocolObj::setDataSessionKey(const std::vector<uint8_t> &key) |
Matt Briggs | 62:9751a8504c82 | 666 | { |
Matt Briggs | 62:9751a8504c82 | 667 | if (key.size() > sizeof(mDataSessionKey)) { |
Matt Briggs | 62:9751a8504c82 | 668 | // TODO log error |
Matt Briggs | 62:9751a8504c82 | 669 | return; |
Matt Briggs | 62:9751a8504c82 | 670 | } |
Matt Briggs | 62:9751a8504c82 | 671 | for (uint8_t i=0; i < sizeof(mDataSessionKey); i++) { |
Matt Briggs | 62:9751a8504c82 | 672 | mDataSessionKey[i] = key[i]; |
Matt Briggs | 62:9751a8504c82 | 673 | } |
Matt Briggs | 62:9751a8504c82 | 674 | } |
Matt Briggs | 55:79ab0bbc5008 | 675 | uint16_t NvmProtocolObj::getLogicalAddr() |
Matt Briggs | 55:79ab0bbc5008 | 676 | { |
Matt Briggs | 55:79ab0bbc5008 | 677 | return mLogicalAddr; |
Matt Briggs | 55:79ab0bbc5008 | 678 | } |
Matt Briggs | 55:79ab0bbc5008 | 679 | void NvmProtocolObj::setLogicalAddr(uint16_t in) |
Matt Briggs | 55:79ab0bbc5008 | 680 | { |
Matt Briggs | 55:79ab0bbc5008 | 681 | mLogicalAddr= in; |
Matt Briggs | 55:79ab0bbc5008 | 682 | } |
Matt Briggs | 55:79ab0bbc5008 | 683 | uint32_t NvmProtocolObj::getLastMsgSeq() |
Matt Briggs | 55:79ab0bbc5008 | 684 | { |
Matt Briggs | 55:79ab0bbc5008 | 685 | return mSeqNum; |
Matt Briggs | 55:79ab0bbc5008 | 686 | } |
Matt Briggs | 65:d546060aa03d | 687 | void NvmProtocolObj::incLastMsgSeq() |
Matt Briggs | 65:d546060aa03d | 688 | { |
Matt Briggs | 65:d546060aa03d | 689 | mSeqNum++; |
Matt Briggs | 65:d546060aa03d | 690 | } |