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 15:39:40 2017 -0700
Revision:
54:c04d7b6fa075
Parent:
53:a1563574a980
Child:
55:79ab0bbc5008
Working static network bridge.  Woot!

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