Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BufferedSerial OneWire WinbondSPIFlash libxDot-dev-mbed5-deprecated
Fork of xDotBridge_update_test20180823 by
xDotBridge/src/CommProtocolPeerBrute.cpp@63:e1efbe3402d9, 2017-03-14 (annotated)
- Committer:
- Matt Briggs
- Date:
- Tue Mar 14 09:54:04 2017 -0600
- Revision:
- 63:e1efbe3402d9
- Parent:
- 62:9751a8504c82
- Child:
- 64:46c8819c07cc
Quick refactor of logging
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 | 63:e1efbe3402d9 | 10 | #include "MyLog.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 | 63:e1efbe3402d9 | 21 | myLogDebug("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 | 63:e1efbe3402d9 | 34 | myLogWarning("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 | 63:e1efbe3402d9 | 50 | // myLogInfo("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 | 63:e1efbe3402d9 | 63 | myLogDebug("Setting up peer to peer configuration"); |
Matt Briggs | 41:9ef4c4d77711 | 64 | if (dot->getJoinMode() != mDot::PEER_TO_PEER) { |
Matt Briggs | 63:e1efbe3402d9 | 65 | myLogDebug("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 | 63:e1efbe3402d9 | 67 | myLogError("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 | 63:e1efbe3402d9 | 115 | // myLogInfo("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 | 63:e1efbe3402d9 | 127 | myLogDebug("Setting up peer to peer configuration"); |
Matt Briggs | 62:9751a8504c82 | 128 | if (dot->getJoinMode() != mDot::PEER_TO_PEER) { |
Matt Briggs | 63:e1efbe3402d9 | 129 | myLogDebug("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 | 63:e1efbe3402d9 | 131 | myLogError("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 | 63:e1efbe3402d9 | 189 | // myLogError("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 | 63:e1efbe3402d9 | 193 | // myLogError("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 | 63:e1efbe3402d9 | 199 | myLogError("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 | 63:e1efbe3402d9 | 204 | // myLogError("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 | 63:e1efbe3402d9 | 210 | myLogError("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 | 63:e1efbe3402d9 | 215 | // myLogError("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 | 63:e1efbe3402d9 | 221 | myLogError("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 | 63:e1efbe3402d9 | 226 | // myLogError("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 | 63:e1efbe3402d9 | 243 | myLogDebug("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 | 63:e1efbe3402d9 | 247 | myLogDebug("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 | 63:e1efbe3402d9 | 332 | myLogDebug("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 | 63:e1efbe3402d9 | 337 | myLogDebug("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 | 63:e1efbe3402d9 | 343 | myLogDebug("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 | 63:e1efbe3402d9 | 350 | myLogDebug("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 | 63:e1efbe3402d9 | 357 | myLogDebug("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 | 63:e1efbe3402d9 | 365 | myLogDebug("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 | 63:e1efbe3402d9 | 392 | myLogError("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 | 63:e1efbe3402d9 | 439 | myLogWarning("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 | 63:e1efbe3402d9 | 444 | myLogWarning("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 | 63:e1efbe3402d9 | 487 | void CommProtocolPeerBrute::printDotConfig() |
Matt Briggs | 63:e1efbe3402d9 | 488 | { |
Matt Briggs | 63:e1efbe3402d9 | 489 | // display configuration and library version information |
Matt Briggs | 63:e1efbe3402d9 | 490 | myLogInfo("====================="); |
Matt Briggs | 63:e1efbe3402d9 | 491 | myLogInfo("general configuration"); |
Matt Briggs | 63:e1efbe3402d9 | 492 | myLogInfo("====================="); |
Matt Briggs | 63:e1efbe3402d9 | 493 | myLogInfo("version ------------------ %s", dot->getId().c_str()); |
Matt Briggs | 63:e1efbe3402d9 | 494 | myLogInfo("device ID/EUI ------------ %s", mts::Text::bin2hexString(dot->getDeviceId()).c_str()); |
Matt Briggs | 63:e1efbe3402d9 | 495 | myLogInfo("frequency band ----------- %s", mDot::FrequencyBandStr(dot->getFrequencyBand()).c_str()); |
Matt Briggs | 63:e1efbe3402d9 | 496 | if (dot->getFrequencySubBand() != mDot::FB_EU868) { |
Matt Briggs | 63:e1efbe3402d9 | 497 | myLogInfo("frequency sub band ------- %u", dot->getFrequencySubBand()); |
Matt Briggs | 63:e1efbe3402d9 | 498 | } |
Matt Briggs | 63:e1efbe3402d9 | 499 | myLogInfo("public network ----------- %s", dot->getPublicNetwork() ? "on" : "off"); |
Matt Briggs | 63:e1efbe3402d9 | 500 | myLogInfo("========================="); |
Matt Briggs | 63:e1efbe3402d9 | 501 | myLogInfo("credentials configuration"); |
Matt Briggs | 63:e1efbe3402d9 | 502 | myLogInfo("========================="); |
Matt Briggs | 63:e1efbe3402d9 | 503 | myLogInfo("device class ------------- %s", dot->getClass().c_str()); |
Matt Briggs | 63:e1efbe3402d9 | 504 | myLogInfo("network join mode -------- %s", mDot::JoinModeStr(dot->getJoinMode()).c_str()); |
Matt Briggs | 63:e1efbe3402d9 | 505 | if (dot->getJoinMode() == mDot::MANUAL || dot->getJoinMode() == mDot::PEER_TO_PEER) { |
Matt Briggs | 63:e1efbe3402d9 | 506 | myLogInfo("network address ---------- %s", mts::Text::bin2hexString(dot->getNetworkAddress()).c_str()); |
Matt Briggs | 63:e1efbe3402d9 | 507 | myLogInfo("network session key------- %s", mts::Text::bin2hexString(dot->getNetworkSessionKey()).c_str()); |
Matt Briggs | 63:e1efbe3402d9 | 508 | myLogInfo("data session key---------- %s", mts::Text::bin2hexString(dot->getDataSessionKey()).c_str()); |
Matt Briggs | 63:e1efbe3402d9 | 509 | } else { |
Matt Briggs | 63:e1efbe3402d9 | 510 | myLogInfo("network name ------------- %s", dot->getNetworkName().c_str()); |
Matt Briggs | 63:e1efbe3402d9 | 511 | myLogInfo("network phrase ----------- %s", dot->getNetworkPassphrase().c_str()); |
Matt Briggs | 63:e1efbe3402d9 | 512 | myLogInfo("network EUI -------------- %s", mts::Text::bin2hexString(dot->getNetworkId()).c_str()); |
Matt Briggs | 63:e1efbe3402d9 | 513 | myLogInfo("network KEY -------------- %s", mts::Text::bin2hexString(dot->getNetworkKey()).c_str()); |
Matt Briggs | 63:e1efbe3402d9 | 514 | } |
Matt Briggs | 63:e1efbe3402d9 | 515 | myLogInfo("========================"); |
Matt Briggs | 63:e1efbe3402d9 | 516 | myLogInfo("communication parameters"); |
Matt Briggs | 63:e1efbe3402d9 | 517 | myLogInfo("========================"); |
Matt Briggs | 63:e1efbe3402d9 | 518 | if (dot->getJoinMode() == mDot::PEER_TO_PEER) { |
Matt Briggs | 63:e1efbe3402d9 | 519 | myLogInfo("TX frequency ------------- %lu", dot->getTxFrequency()); |
Matt Briggs | 63:e1efbe3402d9 | 520 | } else { |
Matt Briggs | 63:e1efbe3402d9 | 521 | myLogInfo("acks --------------------- %s, %u attempts", dot->getAck() > 0 ? "on" : "off", dot->getAck()); |
Matt Briggs | 63:e1efbe3402d9 | 522 | } |
Matt Briggs | 63:e1efbe3402d9 | 523 | myLogInfo("TX datarate -------------- %s", mDot::DataRateStr(dot->getTxDataRate()).c_str()); |
Matt Briggs | 63:e1efbe3402d9 | 524 | myLogInfo("TX power ----------------- %lu dBm", dot->getTxPower()); |
Matt Briggs | 63:e1efbe3402d9 | 525 | myLogInfo("atnenna gain ------------- %u dBm", dot->getAntennaGain()); |
Matt Briggs | 63:e1efbe3402d9 | 526 | } |
Matt Briggs | 63:e1efbe3402d9 | 527 | |
Matt Briggs | 55:79ab0bbc5008 | 528 | // NvmProtocolObj |
Matt Briggs | 55:79ab0bbc5008 | 529 | NvmProtocolObj::NvmProtocolObj() |
Matt Briggs | 55:79ab0bbc5008 | 530 | { |
Matt Briggs | 55:79ab0bbc5008 | 531 | setDefaults(); |
Matt Briggs | 55:79ab0bbc5008 | 532 | } |
Matt Briggs | 55:79ab0bbc5008 | 533 | void NvmProtocolObj::setDefaults() |
Matt Briggs | 55:79ab0bbc5008 | 534 | { |
Matt Briggs | 55:79ab0bbc5008 | 535 | mProtocolFlag = PROTOCOL_FLAG; |
Matt Briggs | 55:79ab0bbc5008 | 536 | mProtocolRev = PROTOCOL_REV; |
Matt Briggs | 62:9751a8504c82 | 537 | mFreq = 915500000; |
Matt Briggs | 62:9751a8504c82 | 538 | std::memset(mNetworkAddr, 0x00, sizeof(mNetworkAddr)); |
Matt Briggs | 62:9751a8504c82 | 539 | std::memset(mNetworkSessionKey, 0x00, sizeof(mNetworkSessionKey)); |
Matt Briggs | 62:9751a8504c82 | 540 | std::memset(mDataSessionKey, 0x00, sizeof(mDataSessionKey)); |
Matt Briggs | 55:79ab0bbc5008 | 541 | mLogicalAddr = 0x00000000; |
Matt Briggs | 55:79ab0bbc5008 | 542 | mSeqNum = 0x00000000; |
Matt Briggs | 55:79ab0bbc5008 | 543 | } |
Matt Briggs | 55:79ab0bbc5008 | 544 | CmdResult NvmProtocolObj::fromBytes(uint8_t *data, uint8_t size) |
Matt Briggs | 55:79ab0bbc5008 | 545 | { |
Matt Briggs | 55:79ab0bbc5008 | 546 | if (size != PROTOCOL_NVM_SIZE) { |
Matt Briggs | 55:79ab0bbc5008 | 547 | return cmdError; |
Matt Briggs | 55:79ab0bbc5008 | 548 | } |
Matt Briggs | 55:79ab0bbc5008 | 549 | |
Matt Briggs | 55:79ab0bbc5008 | 550 | mProtocolFlag = *((uint16_t *) (data)); |
Matt Briggs | 62:9751a8504c82 | 551 | mProtocolRev = *((uint16_t *) (data+0x02)); |
Matt Briggs | 62:9751a8504c82 | 552 | mFreq = *((uint32_t *) (data+0x04)); |
Matt Briggs | 62:9751a8504c82 | 553 | std::memcpy(&mNetworkAddr, data+0x08, 4); |
Matt Briggs | 62:9751a8504c82 | 554 | std::memcpy(&mNetworkSessionKey, data+0x10, 16); |
Matt Briggs | 62:9751a8504c82 | 555 | std::memcpy(&mDataSessionKey, data+0x20, 16); |
Matt Briggs | 62:9751a8504c82 | 556 | mLogicalAddr = *((uint32_t *) (data+0x30)); |
Matt Briggs | 62:9751a8504c82 | 557 | mSeqNum = *((uint32_t *) (data+0x32)); |
Matt Briggs | 55:79ab0bbc5008 | 558 | |
Matt Briggs | 55:79ab0bbc5008 | 559 | return cmdSuccess; |
Matt Briggs | 55:79ab0bbc5008 | 560 | } |
Matt Briggs | 55:79ab0bbc5008 | 561 | CmdResult NvmProtocolObj::toBytes(uint8_t *data, uint8_t &size) { |
Matt Briggs | 55:79ab0bbc5008 | 562 | // TODO check data size |
Matt Briggs | 55:79ab0bbc5008 | 563 | |
Matt Briggs | 55:79ab0bbc5008 | 564 | *((uint16_t *) (data)) = mProtocolFlag; |
Matt Briggs | 55:79ab0bbc5008 | 565 | *((uint16_t *) (data+2)) = mProtocolRev; |
Matt Briggs | 62:9751a8504c82 | 566 | *((uint32_t *) (data+0x04)) = mFreq; |
Matt Briggs | 62:9751a8504c82 | 567 | std::memcpy(data+0x08, &mNetworkAddr, 4); |
Matt Briggs | 62:9751a8504c82 | 568 | std::memcpy(data+0x10, &mNetworkSessionKey, 16); |
Matt Briggs | 62:9751a8504c82 | 569 | std::memcpy(data+0x20, &mDataSessionKey, 16); |
Matt Briggs | 62:9751a8504c82 | 570 | *((uint32_t *) (data+0x30)) = mLogicalAddr; |
Matt Briggs | 62:9751a8504c82 | 571 | *((uint32_t *) (data+0x32)) = mSeqNum; |
Matt Briggs | 55:79ab0bbc5008 | 572 | |
Matt Briggs | 55:79ab0bbc5008 | 573 | size = PROTOCOL_NVM_SIZE; |
Matt Briggs | 55:79ab0bbc5008 | 574 | |
Matt Briggs | 55:79ab0bbc5008 | 575 | return cmdSuccess; |
Matt Briggs | 55:79ab0bbc5008 | 576 | } |
Matt Briggs | 55:79ab0bbc5008 | 577 | uint16_t NvmProtocolObj::getProtocolFlag() |
Matt Briggs | 55:79ab0bbc5008 | 578 | { |
Matt Briggs | 55:79ab0bbc5008 | 579 | return mProtocolFlag; |
Matt Briggs | 55:79ab0bbc5008 | 580 | } |
Matt Briggs | 55:79ab0bbc5008 | 581 | bool NvmProtocolObj::validProtocolFlag() |
Matt Briggs | 55:79ab0bbc5008 | 582 | { |
Matt Briggs | 55:79ab0bbc5008 | 583 | return mProtocolFlag == PROTOCOL_FLAG; |
Matt Briggs | 55:79ab0bbc5008 | 584 | } |
Matt Briggs | 55:79ab0bbc5008 | 585 | uint16_t NvmProtocolObj::getProtocolRev() |
Matt Briggs | 55:79ab0bbc5008 | 586 | { |
Matt Briggs | 55:79ab0bbc5008 | 587 | return mProtocolRev; |
Matt Briggs | 55:79ab0bbc5008 | 588 | } |
Matt Briggs | 55:79ab0bbc5008 | 589 | bool NvmProtocolObj::validProtocolRev() |
Matt Briggs | 55:79ab0bbc5008 | 590 | { |
Matt Briggs | 55:79ab0bbc5008 | 591 | return mProtocolRev == PROTOCOL_REV; |
Matt Briggs | 55:79ab0bbc5008 | 592 | } |
Matt Briggs | 62:9751a8504c82 | 593 | void NvmProtocolObj::getNetworkAddr(std::vector<uint8_t> &addr) |
Matt Briggs | 62:9751a8504c82 | 594 | { |
Matt Briggs | 62:9751a8504c82 | 595 | addr.clear(); |
Matt Briggs | 62:9751a8504c82 | 596 | addr.assign(mNetworkAddr, mNetworkAddr+sizeof(mNetworkAddr)); |
Matt Briggs | 62:9751a8504c82 | 597 | } |
Matt Briggs | 62:9751a8504c82 | 598 | void NvmProtocolObj::setNetworkAddr(const std::vector<uint8_t> &addr) |
Matt Briggs | 62:9751a8504c82 | 599 | { |
Matt Briggs | 62:9751a8504c82 | 600 | if (addr.size() > sizeof(mNetworkAddr)) { |
Matt Briggs | 62:9751a8504c82 | 601 | // TODO log error |
Matt Briggs | 62:9751a8504c82 | 602 | return; |
Matt Briggs | 62:9751a8504c82 | 603 | } |
Matt Briggs | 62:9751a8504c82 | 604 | for (uint8_t i=0; i < sizeof(mNetworkAddr); i++) { |
Matt Briggs | 62:9751a8504c82 | 605 | mNetworkAddr[i] = addr[i]; |
Matt Briggs | 62:9751a8504c82 | 606 | } |
Matt Briggs | 62:9751a8504c82 | 607 | } |
Matt Briggs | 62:9751a8504c82 | 608 | void NvmProtocolObj::getNetworkSessionKey(std::vector<uint8_t> &key) |
Matt Briggs | 62:9751a8504c82 | 609 | { |
Matt Briggs | 62:9751a8504c82 | 610 | key.clear(); |
Matt Briggs | 62:9751a8504c82 | 611 | key.assign(mNetworkSessionKey, mNetworkSessionKey+sizeof(mNetworkSessionKey)); |
Matt Briggs | 62:9751a8504c82 | 612 | } |
Matt Briggs | 62:9751a8504c82 | 613 | void NvmProtocolObj::setNetworkSessionKey(const std::vector<uint8_t> &key) |
Matt Briggs | 62:9751a8504c82 | 614 | { |
Matt Briggs | 62:9751a8504c82 | 615 | if (key.size() > sizeof(mNetworkSessionKey)) { |
Matt Briggs | 62:9751a8504c82 | 616 | // TODO log error |
Matt Briggs | 62:9751a8504c82 | 617 | return; |
Matt Briggs | 62:9751a8504c82 | 618 | } |
Matt Briggs | 62:9751a8504c82 | 619 | for (uint8_t i=0; i < sizeof(mNetworkSessionKey); i++) { |
Matt Briggs | 62:9751a8504c82 | 620 | mNetworkSessionKey[i] = key[i]; |
Matt Briggs | 62:9751a8504c82 | 621 | } |
Matt Briggs | 62:9751a8504c82 | 622 | } |
Matt Briggs | 62:9751a8504c82 | 623 | void NvmProtocolObj::getDataSessionKey(std::vector<uint8_t> &key) |
Matt Briggs | 62:9751a8504c82 | 624 | { |
Matt Briggs | 62:9751a8504c82 | 625 | key.clear(); |
Matt Briggs | 62:9751a8504c82 | 626 | key.assign(mDataSessionKey, mDataSessionKey+sizeof(mDataSessionKey)); |
Matt Briggs | 62:9751a8504c82 | 627 | } |
Matt Briggs | 62:9751a8504c82 | 628 | void NvmProtocolObj::setDataSessionKey(const std::vector<uint8_t> &key) |
Matt Briggs | 62:9751a8504c82 | 629 | { |
Matt Briggs | 62:9751a8504c82 | 630 | if (key.size() > sizeof(mDataSessionKey)) { |
Matt Briggs | 62:9751a8504c82 | 631 | // TODO log error |
Matt Briggs | 62:9751a8504c82 | 632 | return; |
Matt Briggs | 62:9751a8504c82 | 633 | } |
Matt Briggs | 62:9751a8504c82 | 634 | for (uint8_t i=0; i < sizeof(mDataSessionKey); i++) { |
Matt Briggs | 62:9751a8504c82 | 635 | mDataSessionKey[i] = key[i]; |
Matt Briggs | 62:9751a8504c82 | 636 | } |
Matt Briggs | 62:9751a8504c82 | 637 | } |
Matt Briggs | 55:79ab0bbc5008 | 638 | uint16_t NvmProtocolObj::getLogicalAddr() |
Matt Briggs | 55:79ab0bbc5008 | 639 | { |
Matt Briggs | 55:79ab0bbc5008 | 640 | return mLogicalAddr; |
Matt Briggs | 55:79ab0bbc5008 | 641 | } |
Matt Briggs | 55:79ab0bbc5008 | 642 | void NvmProtocolObj::setLogicalAddr(uint16_t in) |
Matt Briggs | 55:79ab0bbc5008 | 643 | { |
Matt Briggs | 55:79ab0bbc5008 | 644 | mLogicalAddr= in; |
Matt Briggs | 55:79ab0bbc5008 | 645 | } |
Matt Briggs | 55:79ab0bbc5008 | 646 | uint32_t NvmProtocolObj::getLastMsgSeq() |
Matt Briggs | 55:79ab0bbc5008 | 647 | { |
Matt Briggs | 55:79ab0bbc5008 | 648 | return mSeqNum; |
Matt Briggs | 55:79ab0bbc5008 | 649 | } |