Fork to see if I can get working

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

Fork of xDotBridge_update_test20180823 by Matt Briggs

Revision:
41:9ef4c4d77711
Child:
44:ece6330e9b57
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/xDotBridge/src/CommProtocolPeerBrute.cpp	Mon Jan 30 17:25:08 2017 -0700
@@ -0,0 +1,197 @@
+/*
+ * CommProtocolPeerBrute.cpp
+ *
+ *  Created on: Jan 30, 2017
+ *      Author: mbriggs
+ */
+
+#include "CommProtocolPeerBrute.h"
+#include "MTSLog.h"
+#include "dot_util.h"
+
+// wireless bridge protocol
+const uint8_t TX_PWR = 20; // 20 dBm
+const float RX_SLEEP_TIME = 2000; // ms (one second resolution, min 2 seconds)
+const uint8_t TX_TIME = 30; // in ms
+const unsigned int nTimesToTx = ceil(RX_SLEEP_TIME / ((float)TX_TIME));
+//const uint8_t maxPayloadSize = 10; // Number of bytes (used for toa calcultion)
+
+CommProtocolPeerBrute::CommProtocolPeerBrute(bool isTx)
+{
+    logInfo("RX_SLEEP_TIME %f, timeOnAir %lu, nTimesToTx %lu", RX_SLEEP_TIME, TX_TIME, nTimesToTx);
+
+    mIsTx = isTx;
+    if (mIsTx) {
+        mWakeMode = mDot::INTERRUPT;
+    }
+    else {
+        mWakeMode = mDot::RTC_ALARM_OR_INTERRUPT;
+    }
+    dot = mDot::getInstance();
+}
+
+CmdResult CommProtocolPeerBrute::init()
+{
+    // Common Configuration
+    dot->setAesEncryption(true);  // Enable encryption
+    dot->setTxWait(false);
+    dot->setAck(0);  // Disable Ack
+    dot->setClass("C"); // Set class C
+    dot->setTxPower(TX_PWR);
+
+    // TODO break out in a utility function
+    // update configuration if necessary
+    logInfo("Setting up peer to peer configuration");
+    if (dot->getJoinMode() != mDot::PEER_TO_PEER) {
+        logInfo("changing network join mode to PEER_TO_PEER");
+        if (dot->setJoinMode(mDot::PEER_TO_PEER) != mDot::MDOT_OK) {
+            logError("failed to set network join mode to PEER_TO_PEER");
+        }
+    }
+    uint8_t tx_power;
+    uint8_t tx_datarate;
+    uint32_t tx_frequency;
+    uint8_t frequency_band = dot->getFrequencyBand();
+    switch (frequency_band) {
+        case mDot::FB_EU868:
+            // 250kHz channels achieve higher throughput
+            // DR6 : SF7 @ 250kHz
+            // DR0 - DR5 (125kHz channels) available but much slower
+            tx_frequency = 869850000;
+            tx_datarate = mDot::DR6;
+            // the 869850000 frequency is 100% duty cycle if the total power is under 7 dBm - tx power 4 + antenna gain 3 = 7
+            tx_power = 4;
+            break;
+        case mDot::FB_US915:
+        case mDot::FB_AU915:
+        default:
+            // 500kHz channels achieve highest throughput
+            // DR8 : SF12 @ 500kHz
+            // DR9 : SF11 @ 500kHz
+            // DR10 : SF10 @ 500kHz
+            // DR11 : SF9 @ 500kHz
+            // DR12 : SF8 @ 500kHz
+            // DR13 : SF7 @ 500kHz
+            // DR0 - DR3 (125kHz channels) available but much slower
+            tx_frequency = 915500000;
+            tx_datarate = mDot::DR13;
+            // 915 bands have no duty cycle restrictions, set tx power to max
+            tx_power = 20;
+            break;
+    }
+    // in PEER_TO_PEER mode there is no join request/response transaction
+    // as long as both Dots are configured correctly, they should be able to communicate
+
+    // FIXME just using pairing keys for now
+    update_peer_to_peer_config(pair_network_address, pair_network_session_key, pair_data_session_key, tx_frequency, tx_datarate, tx_power);
+    return cmdSuccess;
+}
+
+bool CommProtocolPeerBrute::isTx()
+{
+    return mIsTx;
+}
+CmdResult CommProtocolPeerBrute::clearPair()
+{
+    logError("Not implemented yet!!!");
+    return cmdError;
+}
+
+// TX focused
+CmdResult CommProtocolPeerBrute::send (const std::vector<uint8_t> &msg)
+{
+    if (!dot->getNetworkJoinStatus()) {
+        join_network();
+    }
+    logInfo("Starting TX.  Time: %lu", us_ticker_read());
+    for(uint i=0;i<nTimesToTx;++i) {
+        dot->send(msg);
+    }
+    logInfo("Finished TX.  Time: %lu", us_ticker_read());
+    return cmdError;
+}
+
+CmdResult CommProtocolPeerBrute::sendPairReq()
+{
+    if (!dot->getNetworkJoinStatus()) {
+        join_network();
+    }
+    logError("Not implemented yet!!!");
+    return cmdError;
+}
+
+// RX focused
+CmdResult CommProtocolPeerBrute::listen (bool &msgPending)
+{
+    if (!dot->getNetworkJoinStatus()) {
+        join_network();
+    }
+
+    uint32_t cDwnLink = dot->getDownLinkCounter();
+
+    wait(TX_TIME/1000.0); // Wait TX_TIME
+
+    if (cDwnLink < dot->getDownLinkCounter()) {
+        msgPending = true;
+    }
+    else {
+        msgPending = false;
+    }
+    return cmdSuccess;  // Maybe add timeout as a possible return value
+}
+
+CmdResult CommProtocolPeerBrute::recv (std::vector<uint8_t> &msg)
+{
+    dot->recv(msg);
+    return cmdSuccess;
+}
+
+CmdResult CommProtocolPeerBrute::waitForPairing(float waitTime)
+{
+    logError("Not implemented yet!!!");
+    return cmdError;
+}
+
+CmdResult CommProtocolPeerBrute::sendPairAccepted()
+{
+    logError("Not implemented yet!!!");
+    return cmdError;
+}
+
+// xDot Peer to Peer Specific
+uint32_t CommProtocolPeerBrute::getDLC()
+{
+    return dot->getDownLinkCounter();
+}
+
+uint32_t CommProtocolPeerBrute::getULC()
+{
+    return dot->getUpLinkCounter();
+}
+
+// private:
+
+CmdResult CommProtocolPeerBrute::readInfoFromNVM()
+{
+    logError("Not implemented yet!!!");
+    return cmdError;
+}
+
+CmdResult CommProtocolPeerBrute::writeInfoToNVM()
+{
+    logError("Not implemented yet!!!");
+    return cmdError;
+}
+
+CmdResult CommProtocolPeerBrute::resetCounters()
+{
+    dot->setDownLinkCounter(0);
+    dot->setUpLinkCounter(0);
+    return cmdSuccess;
+}
+
+CmdResult CommProtocolPeerBrute::genEncypKeys()
+{
+    logError("Not implemented yet!!!");
+    return cmdError;
+}