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:
Wed Feb 22 10:45:56 2017 -0700
Revision:
53:a1563574a980
Parent:
44:ece6330e9b57
Child:
54:c04d7b6fa075
Mostly working TX in main app

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 53:a1563574a980 24 // dot = mDot::getInstance();
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 41:9ef4c4d77711 29 // Common Configuration
Matt Briggs 41:9ef4c4d77711 30 dot->setAesEncryption(true); // Enable encryption
Matt Briggs 41:9ef4c4d77711 31 dot->setTxWait(false);
Matt Briggs 41:9ef4c4d77711 32 dot->setAck(0); // Disable Ack
Matt Briggs 41:9ef4c4d77711 33 dot->setClass("C"); // Set class C
Matt Briggs 41:9ef4c4d77711 34 dot->setTxPower(TX_PWR);
Matt Briggs 41:9ef4c4d77711 35
Matt Briggs 41:9ef4c4d77711 36 // TODO break out in a utility function
Matt Briggs 41:9ef4c4d77711 37 // update configuration if necessary
Matt Briggs 41:9ef4c4d77711 38 logInfo("Setting up peer to peer configuration");
Matt Briggs 41:9ef4c4d77711 39 if (dot->getJoinMode() != mDot::PEER_TO_PEER) {
Matt Briggs 41:9ef4c4d77711 40 logInfo("changing network join mode to PEER_TO_PEER");
Matt Briggs 41:9ef4c4d77711 41 if (dot->setJoinMode(mDot::PEER_TO_PEER) != mDot::MDOT_OK) {
Matt Briggs 41:9ef4c4d77711 42 logError("failed to set network join mode to PEER_TO_PEER");
Matt Briggs 41:9ef4c4d77711 43 }
Matt Briggs 41:9ef4c4d77711 44 }
Matt Briggs 41:9ef4c4d77711 45 uint8_t tx_power;
Matt Briggs 41:9ef4c4d77711 46 uint8_t tx_datarate;
Matt Briggs 41:9ef4c4d77711 47 uint32_t tx_frequency;
Matt Briggs 41:9ef4c4d77711 48 uint8_t frequency_band = dot->getFrequencyBand();
Matt Briggs 41:9ef4c4d77711 49 switch (frequency_band) {
Matt Briggs 41:9ef4c4d77711 50 case mDot::FB_EU868:
Matt Briggs 41:9ef4c4d77711 51 // 250kHz channels achieve higher throughput
Matt Briggs 41:9ef4c4d77711 52 // DR6 : SF7 @ 250kHz
Matt Briggs 41:9ef4c4d77711 53 // DR0 - DR5 (125kHz channels) available but much slower
Matt Briggs 41:9ef4c4d77711 54 tx_frequency = 869850000;
Matt Briggs 41:9ef4c4d77711 55 tx_datarate = mDot::DR6;
Matt Briggs 41:9ef4c4d77711 56 // 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 57 tx_power = 4;
Matt Briggs 41:9ef4c4d77711 58 break;
Matt Briggs 41:9ef4c4d77711 59 case mDot::FB_US915:
Matt Briggs 41:9ef4c4d77711 60 case mDot::FB_AU915:
Matt Briggs 41:9ef4c4d77711 61 default:
Matt Briggs 41:9ef4c4d77711 62 // 500kHz channels achieve highest throughput
Matt Briggs 41:9ef4c4d77711 63 // DR8 : SF12 @ 500kHz
Matt Briggs 41:9ef4c4d77711 64 // DR9 : SF11 @ 500kHz
Matt Briggs 41:9ef4c4d77711 65 // DR10 : SF10 @ 500kHz
Matt Briggs 41:9ef4c4d77711 66 // DR11 : SF9 @ 500kHz
Matt Briggs 41:9ef4c4d77711 67 // DR12 : SF8 @ 500kHz
Matt Briggs 41:9ef4c4d77711 68 // DR13 : SF7 @ 500kHz
Matt Briggs 41:9ef4c4d77711 69 // DR0 - DR3 (125kHz channels) available but much slower
Matt Briggs 41:9ef4c4d77711 70 tx_frequency = 915500000;
Matt Briggs 41:9ef4c4d77711 71 tx_datarate = mDot::DR13;
Matt Briggs 41:9ef4c4d77711 72 // 915 bands have no duty cycle restrictions, set tx power to max
Matt Briggs 41:9ef4c4d77711 73 tx_power = 20;
Matt Briggs 41:9ef4c4d77711 74 break;
Matt Briggs 41:9ef4c4d77711 75 }
Matt Briggs 41:9ef4c4d77711 76 // in PEER_TO_PEER mode there is no join request/response transaction
Matt Briggs 41:9ef4c4d77711 77 // as long as both Dots are configured correctly, they should be able to communicate
Matt Briggs 41:9ef4c4d77711 78
Matt Briggs 41:9ef4c4d77711 79 // FIXME just using pairing keys for now
Matt Briggs 53:a1563574a980 80 wait(0.1);
Matt Briggs 41:9ef4c4d77711 81 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 82 wait(0.1);
Matt Briggs 53:a1563574a980 83 dot->saveConfig();
Matt Briggs 41:9ef4c4d77711 84 return cmdSuccess;
Matt Briggs 41:9ef4c4d77711 85 }
Matt Briggs 44:ece6330e9b57 86 void CommProtocolPeerBrute::setTx(bool isTx)
Matt Briggs 44:ece6330e9b57 87 {
Matt Briggs 44:ece6330e9b57 88 mIsTx = isTx;
Matt Briggs 44:ece6330e9b57 89 }
Matt Briggs 41:9ef4c4d77711 90 bool CommProtocolPeerBrute::isTx()
Matt Briggs 41:9ef4c4d77711 91 {
Matt Briggs 41:9ef4c4d77711 92 return mIsTx;
Matt Briggs 41:9ef4c4d77711 93 }
Matt Briggs 41:9ef4c4d77711 94 CmdResult CommProtocolPeerBrute::clearPair()
Matt Briggs 41:9ef4c4d77711 95 {
Matt Briggs 41:9ef4c4d77711 96 logError("Not implemented yet!!!");
Matt Briggs 41:9ef4c4d77711 97 return cmdError;
Matt Briggs 41:9ef4c4d77711 98 }
Matt Briggs 41:9ef4c4d77711 99
Matt Briggs 41:9ef4c4d77711 100 // TX focused
Matt Briggs 41:9ef4c4d77711 101 CmdResult CommProtocolPeerBrute::send (const std::vector<uint8_t> &msg)
Matt Briggs 41:9ef4c4d77711 102 {
Matt Briggs 41:9ef4c4d77711 103 if (!dot->getNetworkJoinStatus()) {
Matt Briggs 41:9ef4c4d77711 104 join_network();
Matt Briggs 41:9ef4c4d77711 105 }
Matt Briggs 41:9ef4c4d77711 106 logInfo("Starting TX. Time: %lu", us_ticker_read());
Matt Briggs 53:a1563574a980 107 for(uint8_t i=0;i<nTimesToTx;++i) {
Matt Briggs 41:9ef4c4d77711 108 dot->send(msg);
Matt Briggs 41:9ef4c4d77711 109 }
Matt Briggs 41:9ef4c4d77711 110 logInfo("Finished TX. Time: %lu", us_ticker_read());
Matt Briggs 41:9ef4c4d77711 111 return cmdError;
Matt Briggs 41:9ef4c4d77711 112 }
Matt Briggs 41:9ef4c4d77711 113
Matt Briggs 41:9ef4c4d77711 114 CmdResult CommProtocolPeerBrute::sendPairReq()
Matt Briggs 41:9ef4c4d77711 115 {
Matt Briggs 41:9ef4c4d77711 116 if (!dot->getNetworkJoinStatus()) {
Matt Briggs 41:9ef4c4d77711 117 join_network();
Matt Briggs 41:9ef4c4d77711 118 }
Matt Briggs 41:9ef4c4d77711 119 logError("Not implemented yet!!!");
Matt Briggs 41:9ef4c4d77711 120 return cmdError;
Matt Briggs 41:9ef4c4d77711 121 }
Matt Briggs 41:9ef4c4d77711 122
Matt Briggs 41:9ef4c4d77711 123 // RX focused
Matt Briggs 41:9ef4c4d77711 124 CmdResult CommProtocolPeerBrute::listen (bool &msgPending)
Matt Briggs 41:9ef4c4d77711 125 {
Matt Briggs 41:9ef4c4d77711 126 if (!dot->getNetworkJoinStatus()) {
Matt Briggs 41:9ef4c4d77711 127 join_network();
Matt Briggs 41:9ef4c4d77711 128 }
Matt Briggs 41:9ef4c4d77711 129
Matt Briggs 41:9ef4c4d77711 130 uint32_t cDwnLink = dot->getDownLinkCounter();
Matt Briggs 41:9ef4c4d77711 131
Matt Briggs 41:9ef4c4d77711 132 wait(TX_TIME/1000.0); // Wait TX_TIME
Matt Briggs 41:9ef4c4d77711 133
Matt Briggs 41:9ef4c4d77711 134 if (cDwnLink < dot->getDownLinkCounter()) {
Matt Briggs 41:9ef4c4d77711 135 msgPending = true;
Matt Briggs 41:9ef4c4d77711 136 }
Matt Briggs 41:9ef4c4d77711 137 else {
Matt Briggs 41:9ef4c4d77711 138 msgPending = false;
Matt Briggs 41:9ef4c4d77711 139 }
Matt Briggs 41:9ef4c4d77711 140 return cmdSuccess; // Maybe add timeout as a possible return value
Matt Briggs 41:9ef4c4d77711 141 }
Matt Briggs 41:9ef4c4d77711 142
Matt Briggs 41:9ef4c4d77711 143 CmdResult CommProtocolPeerBrute::recv (std::vector<uint8_t> &msg)
Matt Briggs 41:9ef4c4d77711 144 {
Matt Briggs 41:9ef4c4d77711 145 dot->recv(msg);
Matt Briggs 41:9ef4c4d77711 146 return cmdSuccess;
Matt Briggs 41:9ef4c4d77711 147 }
Matt Briggs 41:9ef4c4d77711 148
Matt Briggs 41:9ef4c4d77711 149 CmdResult CommProtocolPeerBrute::waitForPairing(float waitTime)
Matt Briggs 41:9ef4c4d77711 150 {
Matt Briggs 41:9ef4c4d77711 151 logError("Not implemented yet!!!");
Matt Briggs 41:9ef4c4d77711 152 return cmdError;
Matt Briggs 41:9ef4c4d77711 153 }
Matt Briggs 41:9ef4c4d77711 154
Matt Briggs 41:9ef4c4d77711 155 CmdResult CommProtocolPeerBrute::sendPairAccepted()
Matt Briggs 41:9ef4c4d77711 156 {
Matt Briggs 41:9ef4c4d77711 157 logError("Not implemented yet!!!");
Matt Briggs 41:9ef4c4d77711 158 return cmdError;
Matt Briggs 41:9ef4c4d77711 159 }
Matt Briggs 41:9ef4c4d77711 160
Matt Briggs 41:9ef4c4d77711 161 // xDot Peer to Peer Specific
Matt Briggs 41:9ef4c4d77711 162 uint32_t CommProtocolPeerBrute::getDLC()
Matt Briggs 41:9ef4c4d77711 163 {
Matt Briggs 41:9ef4c4d77711 164 return dot->getDownLinkCounter();
Matt Briggs 41:9ef4c4d77711 165 }
Matt Briggs 41:9ef4c4d77711 166
Matt Briggs 41:9ef4c4d77711 167 uint32_t CommProtocolPeerBrute::getULC()
Matt Briggs 41:9ef4c4d77711 168 {
Matt Briggs 41:9ef4c4d77711 169 return dot->getUpLinkCounter();
Matt Briggs 41:9ef4c4d77711 170 }
Matt Briggs 41:9ef4c4d77711 171
Matt Briggs 41:9ef4c4d77711 172 // private:
Matt Briggs 41:9ef4c4d77711 173
Matt Briggs 41:9ef4c4d77711 174 CmdResult CommProtocolPeerBrute::readInfoFromNVM()
Matt Briggs 41:9ef4c4d77711 175 {
Matt Briggs 41:9ef4c4d77711 176 logError("Not implemented yet!!!");
Matt Briggs 41:9ef4c4d77711 177 return cmdError;
Matt Briggs 41:9ef4c4d77711 178 }
Matt Briggs 41:9ef4c4d77711 179
Matt Briggs 41:9ef4c4d77711 180 CmdResult CommProtocolPeerBrute::writeInfoToNVM()
Matt Briggs 41:9ef4c4d77711 181 {
Matt Briggs 41:9ef4c4d77711 182 logError("Not implemented yet!!!");
Matt Briggs 41:9ef4c4d77711 183 return cmdError;
Matt Briggs 41:9ef4c4d77711 184 }
Matt Briggs 41:9ef4c4d77711 185
Matt Briggs 41:9ef4c4d77711 186 CmdResult CommProtocolPeerBrute::resetCounters()
Matt Briggs 41:9ef4c4d77711 187 {
Matt Briggs 41:9ef4c4d77711 188 dot->setDownLinkCounter(0);
Matt Briggs 41:9ef4c4d77711 189 dot->setUpLinkCounter(0);
Matt Briggs 41:9ef4c4d77711 190 return cmdSuccess;
Matt Briggs 41:9ef4c4d77711 191 }
Matt Briggs 41:9ef4c4d77711 192
Matt Briggs 41:9ef4c4d77711 193 CmdResult CommProtocolPeerBrute::genEncypKeys()
Matt Briggs 41:9ef4c4d77711 194 {
Matt Briggs 41:9ef4c4d77711 195 logError("Not implemented yet!!!");
Matt Briggs 41:9ef4c4d77711 196 return cmdError;
Matt Briggs 41:9ef4c4d77711 197 }