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