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