Fork to see if I can get working

Dependencies:   BufferedSerial OneWire WinbondSPIFlash libxDot-dev-mbed5-deprecated

Fork of xDotBridge_update_test20180823 by Matt Briggs

Committer:
Matt Briggs
Date:
Thu Mar 09 16:47:42 2017 -0700
Revision:
61:8d9efd33cac9
Parent:
55:79ab0bbc5008
Child:
62:9751a8504c82
Slight modification of pair code and documentation.  Also some test code to all xDot dev boards to act like bridges.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Matt Briggs 41:9ef4c4d77711 1 /*
Matt Briggs 41:9ef4c4d77711 2 * CommProtocolPeerBrute.cpp
Matt Briggs 41:9ef4c4d77711 3 *
Matt Briggs 41:9ef4c4d77711 4 * Created on: Jan 30, 2017
Matt Briggs 41:9ef4c4d77711 5 * Author: mbriggs
Matt Briggs 41:9ef4c4d77711 6 */
Matt Briggs 41:9ef4c4d77711 7
Matt Briggs 41:9ef4c4d77711 8 #include "CommProtocolPeerBrute.h"
Matt Briggs 41:9ef4c4d77711 9 #include "MTSLog.h"
Matt Briggs 41:9ef4c4d77711 10 #include "dot_util.h"
Matt Briggs 41:9ef4c4d77711 11
Matt Briggs 41:9ef4c4d77711 12 // wireless bridge protocol
Matt Briggs 41:9ef4c4d77711 13 const uint8_t TX_PWR = 20; // 20 dBm
Matt Briggs 41:9ef4c4d77711 14 const float RX_SLEEP_TIME = 2000; // ms (one second resolution, min 2 seconds)
Matt Briggs 41:9ef4c4d77711 15 const uint8_t TX_TIME = 30; // in ms
Matt Briggs 41:9ef4c4d77711 16 const unsigned int nTimesToTx = ceil(RX_SLEEP_TIME / ((float)TX_TIME));
Matt Briggs 41:9ef4c4d77711 17 //const uint8_t maxPayloadSize = 10; // Number of bytes (used for toa calcultion)
Matt Briggs 41:9ef4c4d77711 18
Matt Briggs 44:ece6330e9b57 19 CommProtocolPeerBrute::CommProtocolPeerBrute()
Matt Briggs 41:9ef4c4d77711 20 {
Matt Briggs 41:9ef4c4d77711 21 logInfo("RX_SLEEP_TIME %f, timeOnAir %lu, nTimesToTx %lu", RX_SLEEP_TIME, TX_TIME, nTimesToTx);
Matt Briggs 41:9ef4c4d77711 22
Matt Briggs 44:ece6330e9b57 23 mIsTx = true; // default to TX
Matt Briggs 61:8d9efd33cac9 24 // mPrevDownLinkCnt = dot->getDownLinkCounter();
Matt Briggs 41:9ef4c4d77711 25 }
Matt Briggs 41:9ef4c4d77711 26
Matt Briggs 41:9ef4c4d77711 27 CmdResult CommProtocolPeerBrute::init()
Matt Briggs 41:9ef4c4d77711 28 {
Matt Briggs 55:79ab0bbc5008 29 // FIXME add NVM stuff
Matt Briggs 55:79ab0bbc5008 30 dotDefaults();
Matt Briggs 55:79ab0bbc5008 31
Matt Briggs 61:8d9efd33cac9 32 return cmdSuccess;
Matt Briggs 55:79ab0bbc5008 33 }
Matt Briggs 55:79ab0bbc5008 34 CmdResult CommProtocolPeerBrute::dotDefaults()
Matt Briggs 55:79ab0bbc5008 35 {
Matt Briggs 55:79ab0bbc5008 36 // TODO add more error checking
Matt Briggs 55:79ab0bbc5008 37 logWarning("defaulting Dot configuration");
Matt Briggs 55:79ab0bbc5008 38 dot->resetConfig();
Matt Briggs 55:79ab0bbc5008 39
Matt Briggs 41:9ef4c4d77711 40 // Common Configuration
Matt Briggs 41:9ef4c4d77711 41 dot->setAesEncryption(true); // Enable encryption
Matt Briggs 41:9ef4c4d77711 42 dot->setTxWait(false);
Matt Briggs 41:9ef4c4d77711 43 dot->setAck(0); // Disable Ack
Matt Briggs 41:9ef4c4d77711 44 dot->setClass("C"); // Set class C
Matt Briggs 41:9ef4c4d77711 45 dot->setTxPower(TX_PWR);
Matt Briggs 61:8d9efd33cac9 46 dot->setPreserveSession(false); // TODO need to better handle counters
Matt Briggs 41:9ef4c4d77711 47
Matt Briggs 41:9ef4c4d77711 48 // TODO break out in a utility function
Matt Briggs 41:9ef4c4d77711 49 // update configuration if necessary
Matt Briggs 41:9ef4c4d77711 50 logInfo("Setting up peer to peer configuration");
Matt Briggs 41:9ef4c4d77711 51 if (dot->getJoinMode() != mDot::PEER_TO_PEER) {
Matt Briggs 41:9ef4c4d77711 52 logInfo("changing network join mode to PEER_TO_PEER");
Matt Briggs 41:9ef4c4d77711 53 if (dot->setJoinMode(mDot::PEER_TO_PEER) != mDot::MDOT_OK) {
Matt Briggs 41:9ef4c4d77711 54 logError("failed to set network join mode to PEER_TO_PEER");
Matt Briggs 41:9ef4c4d77711 55 }
Matt Briggs 41:9ef4c4d77711 56 }
Matt Briggs 41:9ef4c4d77711 57 uint8_t tx_power;
Matt Briggs 41:9ef4c4d77711 58 uint8_t tx_datarate;
Matt Briggs 41:9ef4c4d77711 59 uint32_t tx_frequency;
Matt Briggs 41:9ef4c4d77711 60 uint8_t frequency_band = dot->getFrequencyBand();
Matt Briggs 41:9ef4c4d77711 61 switch (frequency_band) {
Matt Briggs 41:9ef4c4d77711 62 case mDot::FB_EU868:
Matt Briggs 41:9ef4c4d77711 63 // 250kHz channels achieve higher throughput
Matt Briggs 41:9ef4c4d77711 64 // DR6 : SF7 @ 250kHz
Matt Briggs 41:9ef4c4d77711 65 // DR0 - DR5 (125kHz channels) available but much slower
Matt Briggs 41:9ef4c4d77711 66 tx_frequency = 869850000;
Matt Briggs 41:9ef4c4d77711 67 tx_datarate = mDot::DR6;
Matt Briggs 41:9ef4c4d77711 68 // the 869850000 frequency is 100% duty cycle if the total power is under 7 dBm - tx power 4 + antenna gain 3 = 7
Matt Briggs 41:9ef4c4d77711 69 tx_power = 4;
Matt Briggs 41:9ef4c4d77711 70 break;
Matt Briggs 41:9ef4c4d77711 71 case mDot::FB_US915:
Matt Briggs 41:9ef4c4d77711 72 case mDot::FB_AU915:
Matt Briggs 41:9ef4c4d77711 73 default:
Matt Briggs 41:9ef4c4d77711 74 // 500kHz channels achieve highest throughput
Matt Briggs 41:9ef4c4d77711 75 // DR8 : SF12 @ 500kHz
Matt Briggs 41:9ef4c4d77711 76 // DR9 : SF11 @ 500kHz
Matt Briggs 41:9ef4c4d77711 77 // DR10 : SF10 @ 500kHz
Matt Briggs 41:9ef4c4d77711 78 // DR11 : SF9 @ 500kHz
Matt Briggs 41:9ef4c4d77711 79 // DR12 : SF8 @ 500kHz
Matt Briggs 41:9ef4c4d77711 80 // DR13 : SF7 @ 500kHz
Matt Briggs 41:9ef4c4d77711 81 // DR0 - DR3 (125kHz channels) available but much slower
Matt Briggs 41:9ef4c4d77711 82 tx_frequency = 915500000;
Matt Briggs 41:9ef4c4d77711 83 tx_datarate = mDot::DR13;
Matt Briggs 41:9ef4c4d77711 84 // 915 bands have no duty cycle restrictions, set tx power to max
Matt Briggs 41:9ef4c4d77711 85 tx_power = 20;
Matt Briggs 41:9ef4c4d77711 86 break;
Matt Briggs 41:9ef4c4d77711 87 }
Matt Briggs 41:9ef4c4d77711 88 // in PEER_TO_PEER mode there is no join request/response transaction
Matt Briggs 41:9ef4c4d77711 89 // as long as both Dots are configured correctly, they should be able to communicate
Matt Briggs 41:9ef4c4d77711 90
Matt Briggs 41:9ef4c4d77711 91 // FIXME just using pairing keys for now
Matt Briggs 41:9ef4c4d77711 92 update_peer_to_peer_config(pair_network_address, pair_network_session_key, pair_data_session_key, tx_frequency, tx_datarate, tx_power);
Matt Briggs 53:a1563574a980 93 dot->saveConfig();
Matt Briggs 41:9ef4c4d77711 94 return cmdSuccess;
Matt Briggs 41:9ef4c4d77711 95 }
Matt Briggs 61:8d9efd33cac9 96 CmdResult CommProtocolPeerBrute::configForPairingNetwork()
Matt Briggs 61:8d9efd33cac9 97 {
Matt Briggs 61:8d9efd33cac9 98 uint8_t tx_power;
Matt Briggs 61:8d9efd33cac9 99 uint8_t tx_datarate;
Matt Briggs 61:8d9efd33cac9 100 uint32_t tx_frequency;
Matt Briggs 61:8d9efd33cac9 101 uint8_t frequency_band = dot->getFrequencyBand();
Matt Briggs 61:8d9efd33cac9 102 switch (frequency_band) {
Matt Briggs 61:8d9efd33cac9 103 case mDot::FB_EU868:
Matt Briggs 61:8d9efd33cac9 104 // 250kHz channels achieve higher throughput
Matt Briggs 61:8d9efd33cac9 105 // DR6 : SF7 @ 250kHz
Matt Briggs 61:8d9efd33cac9 106 // DR0 - DR5 (125kHz channels) available but much slower
Matt Briggs 61:8d9efd33cac9 107 tx_frequency = 869850000;
Matt Briggs 61:8d9efd33cac9 108 tx_datarate = mDot::DR6;
Matt Briggs 61:8d9efd33cac9 109 // the 869850000 frequency is 100% duty cycle if the total power is under 7 dBm - tx power 4 + antenna gain 3 = 7
Matt Briggs 61:8d9efd33cac9 110 tx_power = 4;
Matt Briggs 61:8d9efd33cac9 111 break;
Matt Briggs 61:8d9efd33cac9 112 case mDot::FB_US915:
Matt Briggs 61:8d9efd33cac9 113 case mDot::FB_AU915:
Matt Briggs 61:8d9efd33cac9 114 default:
Matt Briggs 61:8d9efd33cac9 115 // 500kHz channels achieve highest throughput
Matt Briggs 61:8d9efd33cac9 116 // DR8 : SF12 @ 500kHz
Matt Briggs 61:8d9efd33cac9 117 // DR9 : SF11 @ 500kHz
Matt Briggs 61:8d9efd33cac9 118 // DR10 : SF10 @ 500kHz
Matt Briggs 61:8d9efd33cac9 119 // DR11 : SF9 @ 500kHz
Matt Briggs 61:8d9efd33cac9 120 // DR12 : SF8 @ 500kHz
Matt Briggs 61:8d9efd33cac9 121 // DR13 : SF7 @ 500kHz
Matt Briggs 61:8d9efd33cac9 122 // DR0 - DR3 (125kHz channels) available but much slower
Matt Briggs 61:8d9efd33cac9 123 tx_frequency = 915500000;
Matt Briggs 61:8d9efd33cac9 124 tx_datarate = mDot::DR13;
Matt Briggs 61:8d9efd33cac9 125 // 915 bands have no duty cycle restrictions, set tx power to max
Matt Briggs 61:8d9efd33cac9 126 tx_power = 20;
Matt Briggs 61:8d9efd33cac9 127 break;
Matt Briggs 61:8d9efd33cac9 128 }
Matt Briggs 61:8d9efd33cac9 129 update_peer_to_peer_config(pair_network_address, pair_network_session_key, pair_data_session_key, tx_frequency, tx_datarate, tx_power);
Matt Briggs 61:8d9efd33cac9 130 // TODO experiment if we need to save as I will need to save off previous settings to revert back
Matt Briggs 61:8d9efd33cac9 131 // dot->saveConfig();
Matt Briggs 61:8d9efd33cac9 132 return cmdSuccess;
Matt Briggs 61:8d9efd33cac9 133 }
Matt Briggs 44:ece6330e9b57 134 void CommProtocolPeerBrute::setTx(bool isTx)
Matt Briggs 44:ece6330e9b57 135 {
Matt Briggs 44:ece6330e9b57 136 mIsTx = isTx;
Matt Briggs 44:ece6330e9b57 137 }
Matt Briggs 41:9ef4c4d77711 138 bool CommProtocolPeerBrute::isTx()
Matt Briggs 41:9ef4c4d77711 139 {
Matt Briggs 41:9ef4c4d77711 140 return mIsTx;
Matt Briggs 41:9ef4c4d77711 141 }
Matt Briggs 41:9ef4c4d77711 142 CmdResult CommProtocolPeerBrute::clearPair()
Matt Briggs 41:9ef4c4d77711 143 {
Matt Briggs 61:8d9efd33cac9 144 CmdResult result;
Matt Briggs 61:8d9efd33cac9 145
Matt Briggs 61:8d9efd33cac9 146 // TODO generate possibly random channel for new pair
Matt Briggs 61:8d9efd33cac9 147 std::vector<uint8_t> key;
Matt Briggs 61:8d9efd33cac9 148 key.reserve(16);
Matt Briggs 61:8d9efd33cac9 149
Matt Briggs 61:8d9efd33cac9 150 result = genEncypKey(key, 8);
Matt Briggs 61:8d9efd33cac9 151 if (result != cmdSuccess) {
Matt Briggs 61:8d9efd33cac9 152 logError("Error generating network ID.");
Matt Briggs 61:8d9efd33cac9 153 return cmdError;
Matt Briggs 61:8d9efd33cac9 154 }
Matt Briggs 61:8d9efd33cac9 155 dot->setNetworkId(key);
Matt Briggs 61:8d9efd33cac9 156
Matt Briggs 61:8d9efd33cac9 157 result = genEncypKey(key, 16);
Matt Briggs 61:8d9efd33cac9 158 if (result != cmdSuccess) {
Matt Briggs 61:8d9efd33cac9 159 logError("Error generating network encryption keys.");
Matt Briggs 61:8d9efd33cac9 160 return cmdError;
Matt Briggs 61:8d9efd33cac9 161 }
Matt Briggs 61:8d9efd33cac9 162 dot->setNetworkKey(key);
Matt Briggs 61:8d9efd33cac9 163
Matt Briggs 61:8d9efd33cac9 164 result = genEncypKey(key, 16);
Matt Briggs 61:8d9efd33cac9 165 if (result != cmdSuccess) {
Matt Briggs 61:8d9efd33cac9 166 logError("Error generating data session encryption keys.");
Matt Briggs 61:8d9efd33cac9 167 return cmdError;
Matt Briggs 61:8d9efd33cac9 168 }
Matt Briggs 61:8d9efd33cac9 169 dot->setDataSessionKey(key);
Matt Briggs 61:8d9efd33cac9 170
Matt Briggs 61:8d9efd33cac9 171 return cmdSuccess;
Matt Briggs 41:9ef4c4d77711 172 }
Matt Briggs 41:9ef4c4d77711 173
Matt Briggs 41:9ef4c4d77711 174 // TX focused
Matt Briggs 41:9ef4c4d77711 175 CmdResult CommProtocolPeerBrute::send (const std::vector<uint8_t> &msg)
Matt Briggs 41:9ef4c4d77711 176 {
Matt Briggs 41:9ef4c4d77711 177 if (!dot->getNetworkJoinStatus()) {
Matt Briggs 41:9ef4c4d77711 178 join_network();
Matt Briggs 41:9ef4c4d77711 179 }
Matt Briggs 41:9ef4c4d77711 180 logInfo("Starting TX. Time: %lu", us_ticker_read());
Matt Briggs 53:a1563574a980 181 for(uint8_t i=0;i<nTimesToTx;++i) {
Matt Briggs 41:9ef4c4d77711 182 dot->send(msg);
Matt Briggs 41:9ef4c4d77711 183 }
Matt Briggs 41:9ef4c4d77711 184 logInfo("Finished TX. Time: %lu", us_ticker_read());
Matt Briggs 41:9ef4c4d77711 185 return cmdError;
Matt Briggs 41:9ef4c4d77711 186 }
Matt Briggs 41:9ef4c4d77711 187
Matt Briggs 41:9ef4c4d77711 188 CmdResult CommProtocolPeerBrute::sendPairReq()
Matt Briggs 41:9ef4c4d77711 189 {
Matt Briggs 41:9ef4c4d77711 190 if (!dot->getNetworkJoinStatus()) {
Matt Briggs 41:9ef4c4d77711 191 join_network();
Matt Briggs 41:9ef4c4d77711 192 }
Matt Briggs 61:8d9efd33cac9 193 // Request Message
Matt Briggs 61:8d9efd33cac9 194 std::vector<uint8_t> msg;
Matt Briggs 61:8d9efd33cac9 195 msg.reserve(16);
Matt Briggs 61:8d9efd33cac9 196 // Flag 2 Bytes
Matt Briggs 61:8d9efd33cac9 197 msg.push_back(0xFE);
Matt Briggs 61:8d9efd33cac9 198 msg.push_back(0x01);
Matt Briggs 61:8d9efd33cac9 199 // EUI 8 Bytes
Matt Briggs 61:8d9efd33cac9 200 std::vector<uint8_t> eui(dot->getDeviceId());
Matt Briggs 61:8d9efd33cac9 201 msg.insert(msg.end(), eui.begin(), eui.end());
Matt Briggs 61:8d9efd33cac9 202 // Reserved 6 Bytes
Matt Briggs 61:8d9efd33cac9 203 for (uint8_t i=0;i<6;i++) {
Matt Briggs 61:8d9efd33cac9 204 msg.push_back(0x00);
Matt Briggs 61:8d9efd33cac9 205 }
Matt Briggs 61:8d9efd33cac9 206 dot->send(msg); // Can just send once since the RX should be always listening
Matt Briggs 61:8d9efd33cac9 207 return cmdSuccess;
Matt Briggs 41:9ef4c4d77711 208 }
Matt Briggs 41:9ef4c4d77711 209
Matt Briggs 41:9ef4c4d77711 210 // RX focused
Matt Briggs 41:9ef4c4d77711 211 CmdResult CommProtocolPeerBrute::listen (bool &msgPending)
Matt Briggs 41:9ef4c4d77711 212 {
Matt Briggs 41:9ef4c4d77711 213 if (!dot->getNetworkJoinStatus()) {
Matt Briggs 41:9ef4c4d77711 214 join_network();
Matt Briggs 41:9ef4c4d77711 215 }
Matt Briggs 41:9ef4c4d77711 216
Matt Briggs 54:c04d7b6fa075 217 // uint32_t cDwnLink = dot->getDownLinkCounter();
Matt Briggs 41:9ef4c4d77711 218
Matt Briggs 41:9ef4c4d77711 219 wait(TX_TIME/1000.0); // Wait TX_TIME
Matt Briggs 41:9ef4c4d77711 220
Matt Briggs 54:c04d7b6fa075 221 if (mPrevDownLinkCnt < dot->getDownLinkCounter()) {
Matt Briggs 41:9ef4c4d77711 222 msgPending = true;
Matt Briggs 41:9ef4c4d77711 223 }
Matt Briggs 41:9ef4c4d77711 224 else {
Matt Briggs 41:9ef4c4d77711 225 msgPending = false;
Matt Briggs 41:9ef4c4d77711 226 }
Matt Briggs 54:c04d7b6fa075 227 mPrevDownLinkCnt = dot->getDownLinkCounter();
Matt Briggs 41:9ef4c4d77711 228 return cmdSuccess; // Maybe add timeout as a possible return value
Matt Briggs 41:9ef4c4d77711 229 }
Matt Briggs 41:9ef4c4d77711 230
Matt Briggs 54:c04d7b6fa075 231 CmdResult CommProtocolPeerBrute::sampleDLC () {
Matt Briggs 54:c04d7b6fa075 232 mPrevDownLinkCnt = dot->getDownLinkCounter();
Matt Briggs 54:c04d7b6fa075 233 return cmdSuccess;
Matt Briggs 54:c04d7b6fa075 234 }
Matt Briggs 54:c04d7b6fa075 235
Matt Briggs 41:9ef4c4d77711 236 CmdResult CommProtocolPeerBrute::recv (std::vector<uint8_t> &msg)
Matt Briggs 41:9ef4c4d77711 237 {
Matt Briggs 41:9ef4c4d77711 238 dot->recv(msg);
Matt Briggs 41:9ef4c4d77711 239 return cmdSuccess;
Matt Briggs 41:9ef4c4d77711 240 }
Matt Briggs 41:9ef4c4d77711 241
Matt Briggs 41:9ef4c4d77711 242 CmdResult CommProtocolPeerBrute::waitForPairing(float waitTime)
Matt Briggs 41:9ef4c4d77711 243 {
Matt Briggs 61:8d9efd33cac9 244 float t = 0.0;
Matt Briggs 61:8d9efd33cac9 245 bool msgPending;
Matt Briggs 61:8d9efd33cac9 246 do {
Matt Briggs 61:8d9efd33cac9 247 listen(msgPending);
Matt Briggs 61:8d9efd33cac9 248 t += TX_TIME/1000.0;
Matt Briggs 61:8d9efd33cac9 249 }
Matt Briggs 61:8d9efd33cac9 250 while (t < waitTime && !msgPending);
Matt Briggs 61:8d9efd33cac9 251
Matt Briggs 61:8d9efd33cac9 252 if (!msgPending) {
Matt Briggs 61:8d9efd33cac9 253 return cmdTimeout;
Matt Briggs 61:8d9efd33cac9 254 }
Matt Briggs 61:8d9efd33cac9 255 // TODO check request
Matt Briggs 61:8d9efd33cac9 256
Matt Briggs 61:8d9efd33cac9 257 wait(1.0); // Wait just a little so it is clear for requester that it is a pair message
Matt Briggs 61:8d9efd33cac9 258 return sendPairAccepted();
Matt Briggs 41:9ef4c4d77711 259 }
Matt Briggs 41:9ef4c4d77711 260
Matt Briggs 41:9ef4c4d77711 261 CmdResult CommProtocolPeerBrute::sendPairAccepted()
Matt Briggs 41:9ef4c4d77711 262 {
Matt Briggs 61:8d9efd33cac9 263 bool sendResult;
Matt Briggs 61:8d9efd33cac9 264 // Request Message
Matt Briggs 61:8d9efd33cac9 265 std::vector<uint8_t> *msg = new std::vector<uint8_t>;
Matt Briggs 61:8d9efd33cac9 266 msg->reserve(34);
Matt Briggs 61:8d9efd33cac9 267 // Flag
Matt Briggs 61:8d9efd33cac9 268 msg->push_back(0xFD);
Matt Briggs 61:8d9efd33cac9 269 msg->push_back(0x02);
Matt Briggs 61:8d9efd33cac9 270 logInfo("flag msg size %d", msg->size());
Matt Briggs 61:8d9efd33cac9 271 // EUI
Matt Briggs 61:8d9efd33cac9 272 std::vector<uint8_t> *eui = new std::vector<uint8_t>(dot->getDeviceId());
Matt Briggs 61:8d9efd33cac9 273 msg->insert(msg->end(),eui->begin(), eui->end());
Matt Briggs 61:8d9efd33cac9 274 delete eui;
Matt Briggs 61:8d9efd33cac9 275 logInfo("eui msg size %d", msg->size());
Matt Briggs 61:8d9efd33cac9 276
Matt Briggs 61:8d9efd33cac9 277 // Reserved for Freq
Matt Briggs 61:8d9efd33cac9 278 for(uint8_t i=0;i<4;i++) {
Matt Briggs 61:8d9efd33cac9 279 msg->push_back(0x00);
Matt Briggs 61:8d9efd33cac9 280 }
Matt Briggs 61:8d9efd33cac9 281 logInfo("freq msg size %d", msg->size());
Matt Briggs 61:8d9efd33cac9 282
Matt Briggs 61:8d9efd33cac9 283 // Network ID
Matt Briggs 61:8d9efd33cac9 284 std::vector<uint8_t> *networkId = new std::vector<uint8_t>(dot->getNetworkId());
Matt Briggs 61:8d9efd33cac9 285 msg->insert(msg->end(),networkId->begin(), networkId->end());
Matt Briggs 61:8d9efd33cac9 286 delete networkId;
Matt Briggs 61:8d9efd33cac9 287 logInfo("netid msg size %d", msg->size());
Matt Briggs 61:8d9efd33cac9 288
Matt Briggs 61:8d9efd33cac9 289 // Network key
Matt Briggs 61:8d9efd33cac9 290 std::vector<uint8_t> *networkKey = new std::vector<uint8_t>(dot->getNetworkKey());
Matt Briggs 61:8d9efd33cac9 291 msg->insert(msg->end(),networkKey->begin(), networkKey->end());
Matt Briggs 61:8d9efd33cac9 292 delete networkKey;
Matt Briggs 61:8d9efd33cac9 293 logInfo("netKey msg size %d", msg->size());
Matt Briggs 61:8d9efd33cac9 294
Matt Briggs 61:8d9efd33cac9 295 // Data session key
Matt Briggs 61:8d9efd33cac9 296 std::vector<uint8_t> *dataSessionKey = new std::vector<uint8_t>(dot->getDataSessionKey());
Matt Briggs 61:8d9efd33cac9 297 msg->insert(msg->end(),dataSessionKey->begin(), dataSessionKey->end());
Matt Briggs 61:8d9efd33cac9 298 delete dataSessionKey;
Matt Briggs 61:8d9efd33cac9 299
Matt Briggs 61:8d9efd33cac9 300 logInfo("msg size %d", msg->size());
Matt Briggs 61:8d9efd33cac9 301 // Only send once since requester should be listening always
Matt Briggs 61:8d9efd33cac9 302 sendResult = mDot::MDOT_OK == dot->send(*msg);
Matt Briggs 61:8d9efd33cac9 303 delete msg;
Matt Briggs 61:8d9efd33cac9 304 if (sendResult)
Matt Briggs 61:8d9efd33cac9 305 return cmdSuccess;
Matt Briggs 61:8d9efd33cac9 306 else
Matt Briggs 61:8d9efd33cac9 307 return cmdError;
Matt Briggs 61:8d9efd33cac9 308 }
Matt Briggs 61:8d9efd33cac9 309
Matt Briggs 61:8d9efd33cac9 310 CmdResult CommProtocolPeerBrute::waitForAccept(float waitTime)
Matt Briggs 61:8d9efd33cac9 311 {
Matt Briggs 61:8d9efd33cac9 312 float t = 0.0;
Matt Briggs 61:8d9efd33cac9 313 bool msgPending = false;
Matt Briggs 61:8d9efd33cac9 314 do {
Matt Briggs 61:8d9efd33cac9 315 listen(msgPending);
Matt Briggs 61:8d9efd33cac9 316 t += TX_TIME/1000.0;
Matt Briggs 61:8d9efd33cac9 317 }
Matt Briggs 61:8d9efd33cac9 318 while (t < waitTime && !msgPending);
Matt Briggs 61:8d9efd33cac9 319
Matt Briggs 61:8d9efd33cac9 320 if (!msgPending) {
Matt Briggs 61:8d9efd33cac9 321 return cmdTimeout;
Matt Briggs 61:8d9efd33cac9 322 }
Matt Briggs 61:8d9efd33cac9 323 // TODO check request
Matt Briggs 61:8d9efd33cac9 324
Matt Briggs 61:8d9efd33cac9 325 return cmdSuccess;
Matt Briggs 41:9ef4c4d77711 326 }
Matt Briggs 41:9ef4c4d77711 327
Matt Briggs 41:9ef4c4d77711 328 // xDot Peer to Peer Specific
Matt Briggs 41:9ef4c4d77711 329 uint32_t CommProtocolPeerBrute::getDLC()
Matt Briggs 41:9ef4c4d77711 330 {
Matt Briggs 41:9ef4c4d77711 331 return dot->getDownLinkCounter();
Matt Briggs 41:9ef4c4d77711 332 }
Matt Briggs 41:9ef4c4d77711 333
Matt Briggs 41:9ef4c4d77711 334 uint32_t CommProtocolPeerBrute::getULC()
Matt Briggs 41:9ef4c4d77711 335 {
Matt Briggs 41:9ef4c4d77711 336 return dot->getUpLinkCounter();
Matt Briggs 41:9ef4c4d77711 337 }
Matt Briggs 41:9ef4c4d77711 338
Matt Briggs 41:9ef4c4d77711 339 // private:
Matt Briggs 41:9ef4c4d77711 340
Matt Briggs 41:9ef4c4d77711 341 CmdResult CommProtocolPeerBrute::readInfoFromNVM()
Matt Briggs 41:9ef4c4d77711 342 {
Matt Briggs 55:79ab0bbc5008 343 // TODO store / retrieve network keys
Matt Briggs 55:79ab0bbc5008 344 bool nvmReadResult;
Matt Briggs 55:79ab0bbc5008 345 uint8_t *data = new uint8_t [PROTOCOL_NVM_SIZE];
Matt Briggs 55:79ab0bbc5008 346
Matt Briggs 55:79ab0bbc5008 347 nvmReadResult = dot->nvmRead(PROTOCOL_NVM_START_ADDR, data, PROTOCOL_NVM_SIZE);
Matt Briggs 55:79ab0bbc5008 348 if (!nvmReadResult) {
Matt Briggs 55:79ab0bbc5008 349 delete [] data;
Matt Briggs 55:79ab0bbc5008 350 return cmdError;
Matt Briggs 55:79ab0bbc5008 351 }
Matt Briggs 55:79ab0bbc5008 352 mMemObj.fromBytes(data, PROTOCOL_NVM_SIZE);
Matt Briggs 55:79ab0bbc5008 353 delete [] data;
Matt Briggs 55:79ab0bbc5008 354 if (!mMemObj.validProtocolFlag()) {
Matt Briggs 55:79ab0bbc5008 355 logWarning("Invalid Protocol Flag. Using default values.");
Matt Briggs 55:79ab0bbc5008 356 mMemObj.setDefaults();
Matt Briggs 55:79ab0bbc5008 357 dotDefaults();
Matt Briggs 55:79ab0bbc5008 358 }
Matt Briggs 55:79ab0bbc5008 359 else if (!mMemObj.validProtocolRev()) {
Matt Briggs 55:79ab0bbc5008 360 logWarning("Invalid Protocol Rev. Using default values.");
Matt Briggs 55:79ab0bbc5008 361 mMemObj.setDefaults();
Matt Briggs 55:79ab0bbc5008 362 dotDefaults();
Matt Briggs 55:79ab0bbc5008 363 }
Matt Briggs 55:79ab0bbc5008 364 return cmdSuccess;
Matt Briggs 41:9ef4c4d77711 365 }
Matt Briggs 41:9ef4c4d77711 366
Matt Briggs 41:9ef4c4d77711 367 CmdResult CommProtocolPeerBrute::writeInfoToNVM()
Matt Briggs 41:9ef4c4d77711 368 {
Matt Briggs 55:79ab0bbc5008 369 uint8_t *data = new uint8_t [PROTOCOL_NVM_SIZE];
Matt Briggs 55:79ab0bbc5008 370 uint8_t size = PROTOCOL_NVM_SIZE;
Matt Briggs 55:79ab0bbc5008 371 mMemObj.toBytes(data, size);
Matt Briggs 55:79ab0bbc5008 372 dot->nvmWrite(PROTOCOL_NVM_START_ADDR, data, PROTOCOL_NVM_SIZE);
Matt Briggs 55:79ab0bbc5008 373
Matt Briggs 55:79ab0bbc5008 374 delete [] data;
Matt Briggs 55:79ab0bbc5008 375 return cmdSuccess;
Matt Briggs 41:9ef4c4d77711 376 }
Matt Briggs 41:9ef4c4d77711 377
Matt Briggs 41:9ef4c4d77711 378 CmdResult CommProtocolPeerBrute::resetCounters()
Matt Briggs 41:9ef4c4d77711 379 {
Matt Briggs 41:9ef4c4d77711 380 dot->setDownLinkCounter(0);
Matt Briggs 41:9ef4c4d77711 381 dot->setUpLinkCounter(0);
Matt Briggs 41:9ef4c4d77711 382 return cmdSuccess;
Matt Briggs 41:9ef4c4d77711 383 }
Matt Briggs 41:9ef4c4d77711 384
Matt Briggs 61:8d9efd33cac9 385 CmdResult CommProtocolPeerBrute::genEncypKey(std::vector<uint8_t> &newKey, uint8_t keySize)
Matt Briggs 41:9ef4c4d77711 386 {
Matt Briggs 61:8d9efd33cac9 387 newKey.clear();
Matt Briggs 61:8d9efd33cac9 388 for (uint8_t i=0;i<keySize; i++){
Matt Briggs 61:8d9efd33cac9 389 newKey.push_back(dot->getRadioRandom());
Matt Briggs 61:8d9efd33cac9 390 }
Matt Briggs 61:8d9efd33cac9 391 return cmdSuccess;
Matt Briggs 41:9ef4c4d77711 392 }
Matt Briggs 55:79ab0bbc5008 393
Matt Briggs 55:79ab0bbc5008 394 // NvmProtocolObj
Matt Briggs 55:79ab0bbc5008 395 NvmProtocolObj::NvmProtocolObj()
Matt Briggs 55:79ab0bbc5008 396 {
Matt Briggs 55:79ab0bbc5008 397 setDefaults();
Matt Briggs 55:79ab0bbc5008 398 }
Matt Briggs 55:79ab0bbc5008 399 void NvmProtocolObj::setDefaults()
Matt Briggs 55:79ab0bbc5008 400 {
Matt Briggs 55:79ab0bbc5008 401 mProtocolFlag = PROTOCOL_FLAG;
Matt Briggs 55:79ab0bbc5008 402 mProtocolRev = PROTOCOL_REV;
Matt Briggs 55:79ab0bbc5008 403 mLogicalAddr = 0x00000000;
Matt Briggs 55:79ab0bbc5008 404 mSeqNum = 0x00000000;
Matt Briggs 55:79ab0bbc5008 405 }
Matt Briggs 55:79ab0bbc5008 406 CmdResult NvmProtocolObj::fromBytes(uint8_t *data, uint8_t size)
Matt Briggs 55:79ab0bbc5008 407 {
Matt Briggs 55:79ab0bbc5008 408 if (size != PROTOCOL_NVM_SIZE) {
Matt Briggs 55:79ab0bbc5008 409 return cmdError;
Matt Briggs 55:79ab0bbc5008 410 }
Matt Briggs 55:79ab0bbc5008 411
Matt Briggs 55:79ab0bbc5008 412 mProtocolFlag = *((uint16_t *) (data));
Matt Briggs 55:79ab0bbc5008 413 mProtocolRev = *((uint16_t *) (data+2));
Matt Briggs 55:79ab0bbc5008 414 mLogicalAddr = *((uint32_t *) (data+4));
Matt Briggs 55:79ab0bbc5008 415 mSeqNum = *((uint32_t *) (data+6));
Matt Briggs 55:79ab0bbc5008 416
Matt Briggs 55:79ab0bbc5008 417 return cmdSuccess;
Matt Briggs 55:79ab0bbc5008 418 }
Matt Briggs 55:79ab0bbc5008 419 CmdResult NvmProtocolObj::toBytes(uint8_t *data, uint8_t &size) {
Matt Briggs 55:79ab0bbc5008 420 // TODO check data size
Matt Briggs 55:79ab0bbc5008 421
Matt Briggs 55:79ab0bbc5008 422 *((uint16_t *) (data)) = mProtocolFlag;
Matt Briggs 55:79ab0bbc5008 423 *((uint16_t *) (data+2)) = mProtocolRev;
Matt Briggs 55:79ab0bbc5008 424 *((uint32_t *) (data+4)) = mLogicalAddr;
Matt Briggs 55:79ab0bbc5008 425 *((uint32_t *) (data+6)) = mSeqNum;
Matt Briggs 55:79ab0bbc5008 426
Matt Briggs 55:79ab0bbc5008 427 size = PROTOCOL_NVM_SIZE;
Matt Briggs 55:79ab0bbc5008 428
Matt Briggs 55:79ab0bbc5008 429 return cmdSuccess;
Matt Briggs 55:79ab0bbc5008 430 }
Matt Briggs 55:79ab0bbc5008 431 uint16_t NvmProtocolObj::getProtocolFlag()
Matt Briggs 55:79ab0bbc5008 432 {
Matt Briggs 55:79ab0bbc5008 433 return mProtocolFlag;
Matt Briggs 55:79ab0bbc5008 434 }
Matt Briggs 55:79ab0bbc5008 435 bool NvmProtocolObj::validProtocolFlag()
Matt Briggs 55:79ab0bbc5008 436 {
Matt Briggs 55:79ab0bbc5008 437 return mProtocolFlag == PROTOCOL_FLAG;
Matt Briggs 55:79ab0bbc5008 438 }
Matt Briggs 55:79ab0bbc5008 439 uint16_t NvmProtocolObj::getProtocolRev()
Matt Briggs 55:79ab0bbc5008 440 {
Matt Briggs 55:79ab0bbc5008 441 return mProtocolRev;
Matt Briggs 55:79ab0bbc5008 442 }
Matt Briggs 55:79ab0bbc5008 443 bool NvmProtocolObj::validProtocolRev()
Matt Briggs 55:79ab0bbc5008 444 {
Matt Briggs 55:79ab0bbc5008 445 return mProtocolRev == PROTOCOL_REV;
Matt Briggs 55:79ab0bbc5008 446 }
Matt Briggs 55:79ab0bbc5008 447 uint16_t NvmProtocolObj::getLogicalAddr()
Matt Briggs 55:79ab0bbc5008 448 {
Matt Briggs 55:79ab0bbc5008 449 return mLogicalAddr;
Matt Briggs 55:79ab0bbc5008 450 }
Matt Briggs 55:79ab0bbc5008 451 void NvmProtocolObj::setLogicalAddr(uint16_t in)
Matt Briggs 55:79ab0bbc5008 452 {
Matt Briggs 55:79ab0bbc5008 453 mLogicalAddr= in;
Matt Briggs 55:79ab0bbc5008 454 }
Matt Briggs 55:79ab0bbc5008 455 uint32_t NvmProtocolObj::getLastMsgSeq()
Matt Briggs 55:79ab0bbc5008 456 {
Matt Briggs 55:79ab0bbc5008 457 return mSeqNum;
Matt Briggs 55:79ab0bbc5008 458 }