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