Fork to see if I can get working

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

Fork of xDotBridge_update_test20180823 by Matt Briggs

Files at this revision

API Documentation at this revision

Comitter:
Matt Briggs
Date:
Wed Mar 15 09:49:58 2017 -0600
Parent:
64:46c8819c07cc
Child:
66:bcaa6dbf538a
Commit message:
Added better alert message code. Quick commit before debug.

Changed in this revision

xDotBridge/inc/CommProtocolPeerBrute.h Show annotated file Show diff for this revision Revisions of this file
xDotBridge/src/CommProtocolPeerBrute.cpp Show annotated file Show diff for this revision Revisions of this file
xDotBridge/src/main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/xDotBridge/inc/CommProtocolPeerBrute.h	Tue Mar 14 11:53:53 2017 -0600
+++ b/xDotBridge/inc/CommProtocolPeerBrute.h	Wed Mar 15 09:49:58 2017 -0600
@@ -45,6 +45,7 @@
     void setLogicalAddr(uint16_t in);
     uint32_t getLastMsgSeq();
     void setLastMsgSeq(uint32_t in);
+    void incLastMsgSeq();
 
     // TODO some day make these private with setters and getters
     uint8_t  mNetworkAddr[4];
@@ -136,6 +137,7 @@
     CmdResult clearPair();
 
     // TX focused
+    uint32_t getSeqNum();
     /**
      * @brief Transmit the msg attached
      *
@@ -148,6 +150,16 @@
     CmdResult send (const std::vector<uint8_t> &msg);
 
     /**
+     * @brief Transmit an alert per documentation
+     *
+     * @details Sends EUI, uint16 data, alert seqNum
+     *
+     * @param data Input data to send
+     * @return if transmission is successful (note no ACK checked)
+     */
+    CmdResult sendAlert (uint16_t data);
+
+    /**
      * @brief This function sends a request to pair with a RX.  If successful a pair accept message is received.
      *
      * @return Returns status of all commands completed
@@ -172,6 +184,7 @@
      * @return Returns status of all commands completed
      */
     CmdResult recv (std::vector<uint8_t> &msg);
+    CmdResult recvAlert (std::vector<uint8_t> &eui, uint16_t &data, uint32_t &seqNum);
 
     /**
      * @brief This fucntion enables the radio to listen for pair requests.
@@ -192,6 +205,7 @@
 
     CmdResult waitForAccept(float waitTime);
 
+
     // xDot Peer to Peer Specific
     /**
      * Convenience function to get the internal downlink count from radio
--- a/xDotBridge/src/CommProtocolPeerBrute.cpp	Tue Mar 14 11:53:53 2017 -0600
+++ b/xDotBridge/src/CommProtocolPeerBrute.cpp	Wed Mar 15 09:49:58 2017 -0600
@@ -7,6 +7,7 @@
 
 #include "CommProtocolPeerBrute.h"
 #include "dot_util.h"
+#include "util.h"
 #include "MyLog.h"
 
 // wireless bridge protocol
@@ -214,6 +215,10 @@
 }
 
 // TX focused
+uint32_t CommProtocolPeerBrute::getSeqNum()
+{
+    return mMemObj.getLastMsgSeq();
+}
 CmdResult CommProtocolPeerBrute::send (const std::vector<uint8_t> &msg)
 {
     if (!dot->getNetworkJoinStatus()) {
@@ -227,6 +232,33 @@
     return cmdError;
 }
 
+CmdResult CommProtocolPeerBrute::sendAlert(uint16_t data)
+{
+    if (!dot->getNetworkJoinStatus()) {
+        join_network();
+    }
+    // Request Message
+    std::vector<uint8_t> msg;
+    msg.reserve(16);
+    // Flag 2 Bytes
+    msg.push_back(0xEF);
+    msg.push_back(0x10);
+    // EUI 8 Bytes
+    std::vector<uint8_t> eui(dot->getDeviceId());
+    msg.insert(msg.end(), eui.begin(), eui.end());
+    // Data 2 Bytes
+    appendUint16ToVector(msg, data);
+    // SeqNum 4 Bytes
+    appendUint32ToVector(msg, mMemObj.getLastMsgSeq());
+    mMemObj.incLastMsgSeq();
+    if (dot->send(msg) == mDot::MDOT_OK) {// Can just send once since the RX should be always listening
+        return cmdSuccess;
+    }
+    else {
+        return cmdError;
+    }
+}
+
 CmdResult CommProtocolPeerBrute::sendPairReq()
 {
     if (!dot->getNetworkJoinStatus()) {
@@ -282,6 +314,31 @@
     return cmdSuccess;
 }
 
+CmdResult CommProtocolPeerBrute::recvAlert (std::vector<uint8_t> &eui, uint16_t &data, uint32_t &seqNum)
+{
+    std::vector<uint8_t> alertMsg;
+    dot->recv(alertMsg);
+    if (alertMsg[0] != 0xEF || alertMsg[1] != 0x10) {
+        myLogError("Invalid alert message flag.");
+        return cmdError;
+    }
+    if (alertMsg.size() < 16) {
+        myLogError("Alert message too short");
+        return cmdError;
+    }
+    eui.clear();
+    eui.assign(alertMsg.begin()+2, alertMsg.begin()+2+8);
+
+    data = (alertMsg[10] << 8) + alertMsg[11];
+
+    seqNum =  alertMsg[12] << 24;
+    seqNum += alertMsg[13] << 16;
+    seqNum += alertMsg[14] << 8;
+    seqNum += alertMsg[15];
+
+    return cmdSuccess;
+}
+
 CmdResult CommProtocolPeerBrute::waitForPairing(float waitTime)
 {
     float t = 0.0;
@@ -627,3 +684,7 @@
 {
     return mSeqNum;
 }
+void NvmProtocolObj::incLastMsgSeq()
+{
+    mSeqNum++;
+}
--- a/xDotBridge/src/main.cpp	Tue Mar 14 11:53:53 2017 -0600
+++ b/xDotBridge/src/main.cpp	Wed Mar 15 09:49:58 2017 -0600
@@ -49,8 +49,7 @@
     dot = mDot::getInstance();
 
     // make sure library logging is turned on
-//    dot->setLogLevel(mts::MTSLog::WARNING_LEVEL);
-    dot->setLogLevel(mts::MTSLog::WARNING_LEVEL);
+    dot->setLogLevel(mts::MTSLog::INFO_LEVEL);
 
     myLogInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);
     myLogInfo("libxDot-mbed5 library ID: %s", dot->getId().c_str());
@@ -113,8 +112,6 @@
     }
     LedPatterns ledPatterns(bbio);
 
-    uint16_t txSeqNum=0;
-    uint16_t rxSeqNum=0;
     Callback<void()> ccInIntObj (&ccInIntCallback);
     Callback<void()> tamperIntObj (&tamperIntCallback);
     Callback<void()> pairBtnIntObj (&pairBtnIntCallback);
@@ -135,7 +132,7 @@
      * Main Loop
      */
     while (true) {
-        std::vector<uint8_t> data;
+//        std::vector<uint8_t> data;
         ledPatterns.turnOff();
 
         // Sample IO and update any configuration
@@ -219,13 +216,8 @@
                 ccIntFlag = false;
                 ledPatterns.turnOn();
 
-                data.clear();
-                data.push_back((txSeqNum >> 8) & 0xFF);
-                data.push_back(txSeqNum & 0xFF);
-                std::string dataStr(data.begin(), data.end());
-                myLogInfo("Sent msg num: %d, payload: %s", txSeqNum, dataStr.c_str());
-                protocol->send(data);
-                txSeqNum++;
+                protocol->sendAlert(0xBEEF); // TODO use this field to encode the alert type e.g. CCIN vs tamper
+                myLogInfo("Sent msg num: %d.", protocol->getSeqNum());
                 ledPatterns.turnOff();
 		    }
 
@@ -247,12 +239,13 @@
 		    protocol->listen(msgPending);
 		    myLogInfo("Loop Cnt %d.  Listening.", loopCnt);
 		    if (msgPending) {
-		        data.clear();
-		        protocol->recv(data);
-                std::string dataStr(data.begin(), data.end());
-                myLogInfo("Got msg num: %d, payload: %s", rxSeqNum, dataStr.c_str());
+		        std::vector<uint8_t> txEui;
+		        txEui.reserve(8);
+		        uint16_t data;
+		        uint32_t msgSeqNum;
+		        protocol->recvAlert(txEui, data, msgSeqNum);
+                myLogInfo("Got msg num: TODO");
                 bbio->relayAlert();
-                rxSeqNum++;
                 ledPatterns.turnOn();
                 // Hold time for alert
                 // TODO maybe use sleep instead of wait