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