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:
mbriggs_vortex
Date:
Sat Nov 18 18:01:43 2017 -0700
Revision:
91:8196900df6fe
Parent:
90:32f9d043561a
Child:
92:1f86edb14cbe
Added new 1.01 bootloader, removed unimplemented prints, removed secrets from being printed (using MACRO in dot_util.h)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbriggs_vortex 81:1eb0f16b2ab9 1 #include "mbed.h"
Matt Briggs 34:5618603e5fc3 2 #include <math.h>
Matt Briggs 22:9453658b8d4b 3 #include "config.h"
Mike Fiore 11:d2e31743433a 4 #include "dot_util.h"
Mike Fiore 14:19fae4509473 5 #include "RadioEvent.h"
Matt Briggs 77:176e3eb8f712 6 #include "BufferedSerial.h"
Matt Briggs 47:a68747642a7a 7 #include "WinbondSPIFlash.h"
Matt Briggs 58:15aa7a785b9f 8 #include "UserInterface.h"
Matt Briggs 41:9ef4c4d77711 9 //#include <xdot_low_power.h>
mbriggs_vortex 81:1eb0f16b2ab9 10 #include "ChannelPlans.h"
Matt Briggs 58:15aa7a785b9f 11 #include "mDot.h"
Matt Briggs 63:e1efbe3402d9 12 #include "MyLog.h"
Matt Briggs 69:eb391644b346 13 #include "SimpleRxSeqLog.h"
mfiore 17:d4f82e16de5f 14
Matt Briggs 47:a68747642a7a 15 #include "BaseboardIO.h"
Matt Briggs 41:9ef4c4d77711 16 #include "CommProtocolPeerBrute.h"
Matt Briggs 75:600cb3a9f126 17 #include "SerialTermMgr.h"
Matt Briggs 31:9c535a708ae9 18
Matt Briggs 69:eb391644b346 19 #define RX_SEQ_LOG 1
Matt Briggs 69:eb391644b346 20
mbriggs_vortex 91:8196900df6fe 21 const float BridgeVersion = 1.99; // Bumped 2017/11/TODO
mbriggs_vortex 78:43f074baac34 22
mbriggs_vortex 78:43f074baac34 23 // 232 Pins
mbriggs_vortex 78:43f074baac34 24 const PinName SER_TERM_TX = UART_TX;
mbriggs_vortex 78:43f074baac34 25 const PinName SER_TERM_RX = UART_RX;
Matt Briggs 69:eb391644b346 26
Matt Briggs 47:a68747642a7a 27 #ifndef __TEST__ // Exclude code for tests
Mike Fiore 11:d2e31743433a 28 Serial pc(USBTX, USBRX);
Matt Briggs 77:176e3eb8f712 29 BufferedSerial *outPc;
mbriggs_vortex 78:43f074baac34 30 InterruptIn uart1RxIntIn (SER_TERM_RX);
Matt Briggs 75:600cb3a9f126 31
Matt Briggs 47:a68747642a7a 32 mDot* dot = NULL; // Used by dot-utils
Matt Briggs 39:64f79fa6e3cc 33
Matt Briggs 75:600cb3a9f126 34 volatile bool uartRxFlag;
Matt Briggs 50:e89647e77fd5 35 volatile bool ccIntFlag;
Matt Briggs 50:e89647e77fd5 36 volatile bool tamperIntFlag;
Matt Briggs 50:e89647e77fd5 37 volatile bool pairBtnIntFlag;
Matt Briggs 75:600cb3a9f126 38 void uart1RxCallback () {
Matt Briggs 75:600cb3a9f126 39 uartRxFlag = true;
Matt Briggs 75:600cb3a9f126 40 }
Matt Briggs 50:e89647e77fd5 41 void ccInIntCallback () {
Matt Briggs 50:e89647e77fd5 42 ccIntFlag = true;
Matt Briggs 50:e89647e77fd5 43 }
Matt Briggs 50:e89647e77fd5 44 void tamperIntCallback () {
Matt Briggs 50:e89647e77fd5 45 tamperIntFlag = true;
Matt Briggs 50:e89647e77fd5 46 }
Matt Briggs 50:e89647e77fd5 47 void pairBtnIntCallback () {
Matt Briggs 50:e89647e77fd5 48 pairBtnIntFlag = true;
Matt Briggs 50:e89647e77fd5 49 }
Matt Briggs 50:e89647e77fd5 50
Mike Fiore 11:d2e31743433a 51 int main() {
Matt Briggs 53:a1563574a980 52 CommProtocolPeerBrute *protocol = new CommProtocolPeerBrute();
Matt Briggs 53:a1563574a980 53 BaseboardIO *bbio = new BaseboardIO();
Matt Briggs 58:15aa7a785b9f 54 WinbondSPIFlash *flash = new WinbondSPIFlash(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_NSS);
Matt Briggs 77:176e3eb8f712 55 SerialTermMgr serialTermMgr(bbio, flash, BridgeVersion);
Matt Briggs 58:15aa7a785b9f 56
Matt Briggs 75:600cb3a9f126 57 pc.baud(115200);
Matt Briggs 52:64a2c71c7c49 58 CmdResult result;
Matt Briggs 75:600cb3a9f126 59 uartRxFlag = false;
Matt Briggs 50:e89647e77fd5 60 ccIntFlag = false;
Matt Briggs 50:e89647e77fd5 61 tamperIntFlag = false;
Matt Briggs 50:e89647e77fd5 62 pairBtnIntFlag = false;
Matt Briggs 48:bab9f747d9ed 63
Matt Briggs 31:9c535a708ae9 64 RadioEvent events; // Custom event handler for automatically displaying RX data
Matt Briggs 69:eb391644b346 65 #if RX_SEQ_LOG
Matt Briggs 69:eb391644b346 66 SimpleRxSeqLog rxSeqLog(0x0000);
Matt Briggs 69:eb391644b346 67 rxSeqLog.read();
Matt Briggs 69:eb391644b346 68 #endif
Mike Fiore 11:d2e31743433a 69
Matt Briggs 27:6b68ff715ae1 70 mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
Matt Briggs 66:bcaa6dbf538a 71 MyLog::setLogLevel(MyLog::DEBUG_LEVEL);
Matt Briggs 47:a68747642a7a 72
mbriggs_vortex 81:1eb0f16b2ab9 73 // use US915 plan
mbriggs_vortex 81:1eb0f16b2ab9 74 lora::ChannelPlan* plan = new lora::ChannelPlan_US915();
mbriggs_vortex 81:1eb0f16b2ab9 75 // use EU868 plan
mbriggs_vortex 81:1eb0f16b2ab9 76 // lora::ChannelPlan* plan = new lora::ChannelPlan_EU868();
mbriggs_vortex 81:1eb0f16b2ab9 77 assert(plan);
mbriggs_vortex 81:1eb0f16b2ab9 78
mbriggs_vortex 81:1eb0f16b2ab9 79 dot = mDot::getInstance(plan);
Mike Fiore 11:d2e31743433a 80
Mike Fiore 11:d2e31743433a 81 // make sure library logging is turned on
Matt Briggs 66:bcaa6dbf538a 82 dot->setLogLevel(mts::MTSLog::WARNING_LEVEL);
Mike Fiore 11:d2e31743433a 83
Matt Briggs 63:e1efbe3402d9 84 myLogInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);
Matt Briggs 63:e1efbe3402d9 85 myLogInfo("libxDot-mbed5 library ID: %s", dot->getId().c_str());
Matt Briggs 69:eb391644b346 86 myLogInfo("Vortex Wireless Bridge SW Version %0.2f", BridgeVersion);
Matt Briggs 62:9751a8504c82 87
Mike Fiore 11:d2e31743433a 88 // attach the custom events handler
Matt Briggs 54:c04d7b6fa075 89 dot->setEvents(&events); // Little bonus event debug information
Matt Briggs 53:a1563574a980 90
Matt Briggs 53:a1563574a980 91 // Finish radio init
Matt Briggs 63:e1efbe3402d9 92 myLogInfo("= Protocol Init Starting =\r\n");
Matt Briggs 53:a1563574a980 93 protocol->init();
Matt Briggs 62:9751a8504c82 94 if (result == cmdSuccess) {
Matt Briggs 63:e1efbe3402d9 95 myLogInfo("= Protocol Init Finished Successfully =\r\n");
Matt Briggs 62:9751a8504c82 96 }
Matt Briggs 62:9751a8504c82 97 else {
Matt Briggs 63:e1efbe3402d9 98 myLogInfo("= Protocol Init Finished with Error =\r\n");
Matt Briggs 62:9751a8504c82 99 }
Matt Briggs 63:e1efbe3402d9 100 protocol->printDotConfig();
Matt Briggs 53:a1563574a980 101
Matt Briggs 58:15aa7a785b9f 102 dot->setWakeMode(mDot::RTC_ALARM_OR_INTERRUPT);
Matt Briggs 75:600cb3a9f126 103 dot->setWakePin(GPIO0);
Matt Briggs 75:600cb3a9f126 104 // dot->setWakePin(UART_RX);
Matt Briggs 24:fdf87e4b72e5 105
Matt Briggs 62:9751a8504c82 106 display_config(); // Print configuration for now
Matt Briggs 62:9751a8504c82 107
Mike Fiore 11:d2e31743433a 108 // display configuration
Matt Briggs 53:a1563574a980 109 // display_config();
Matt Briggs 52:64a2c71c7c49 110
Matt Briggs 63:e1efbe3402d9 111 myLogInfo("= Baseboard Init Starting =\r\n");
Matt Briggs 53:a1563574a980 112 result = bbio->init();
Matt Briggs 52:64a2c71c7c49 113 if (result == cmdSuccess) {
Matt Briggs 63:e1efbe3402d9 114 myLogInfo("= Baseboard Init Finished Successfully =\r\n");
Matt Briggs 52:64a2c71c7c49 115 }
Matt Briggs 52:64a2c71c7c49 116 else {
Matt Briggs 63:e1efbe3402d9 117 myLogInfo("= Baseboard Init Finished with Error =\r\n");
Matt Briggs 52:64a2c71c7c49 118 }
Matt Briggs 64:46c8819c07cc 119 LedPatterns ledPatterns(bbio);
Mike Fiore 11:d2e31743433a 120
Matt Briggs 76:d1b20a259d8f 121 uart1RxIntIn.rise(&uart1RxCallback); // Rising does appear to work better than falling edge
Matt Briggs 75:600cb3a9f126 122 uart1RxIntIn.enable_irq();
Matt Briggs 50:e89647e77fd5 123 Callback<void()> ccInIntObj (&ccInIntCallback);
Matt Briggs 69:eb391644b346 124 // Callback<void()> tamperIntObj (&tamperIntCallback);
Matt Briggs 50:e89647e77fd5 125 Callback<void()> pairBtnIntObj (&pairBtnIntCallback);
Matt Briggs 50:e89647e77fd5 126
Matt Briggs 53:a1563574a980 127 bbio->sampleUserSwitches();
Matt Briggs 53:a1563574a980 128 bbio->relayNormal(); // Always force relay in known state
Matt Briggs 58:15aa7a785b9f 129 bbio->regCCInInt(ccInIntObj);
Matt Briggs 69:eb391644b346 130 // bbio->regTamperInt(tamperIntObj);
Matt Briggs 58:15aa7a785b9f 131 bbio->regPairBtnInt(pairBtnIntObj);
Matt Briggs 26:9411b26a5084 132
Matt Briggs 66:bcaa6dbf538a 133 if (bbio->isTx()) {
Matt Briggs 66:bcaa6dbf538a 134 protocol->setTx(true);
Matt Briggs 66:bcaa6dbf538a 135 }
Matt Briggs 66:bcaa6dbf538a 136 else { // RX
Matt Briggs 66:bcaa6dbf538a 137 protocol->setTx(false);
Matt Briggs 66:bcaa6dbf538a 138 }
Matt Briggs 66:bcaa6dbf538a 139
Matt Briggs 58:15aa7a785b9f 140 // Start flash powered down
Matt Briggs 58:15aa7a785b9f 141 flash->powerDown();
Matt Briggs 58:15aa7a785b9f 142
Matt Briggs 69:eb391644b346 143 unsigned int loopCnt = 0; // Just a quick temp variable to keep track of loopNums
Matt Briggs 58:15aa7a785b9f 144 bool prevCCNormallyOpen;
Matt Briggs 61:8d9efd33cac9 145 PairBtnState pairBtnState;
Matt Briggs 67:2115a2f1b945 146 uint32_t rxMsgCnt = 0;
Matt Briggs 69:eb391644b346 147 uint32_t maxSeenMsgSeqNum = 0;
Matt Briggs 47:a68747642a7a 148 /**
Matt Briggs 47:a68747642a7a 149 * Main Loop
Matt Briggs 47:a68747642a7a 150 */
Mike Fiore 11:d2e31743433a 151 while (true) {
Matt Briggs 69:eb391644b346 152 // myLogInfo("Start of loop time %d", us_ticker_read());
Matt Briggs 64:46c8819c07cc 153 ledPatterns.turnOff();
mbriggs_vortex 78:43f074baac34 154 bbio->serialRx(true); // FIXME find a better home
mbriggs_vortex 78:43f074baac34 155 bbio->serialTx(false); // FIXME only turn on during TX
Matt Briggs 47:a68747642a7a 156
Matt Briggs 66:bcaa6dbf538a 157 // Pair logic and switch sampling
Matt Briggs 61:8d9efd33cac9 158 if (pairBtnIntFlag) {
Matt Briggs 61:8d9efd33cac9 159 pairBtnState = PairBtnInterp::read(bbio);
Matt Briggs 66:bcaa6dbf538a 160
Matt Briggs 66:bcaa6dbf538a 161 // Sample IO and update any configuration
Matt Briggs 66:bcaa6dbf538a 162 prevCCNormallyOpen = bbio->isCCNO();
Matt Briggs 66:bcaa6dbf538a 163 bbio->sampleUserSwitches();
Matt Briggs 66:bcaa6dbf538a 164 if (prevCCNormallyOpen != bbio->isCCNO()) { // Only activate the coil if the DIP SW has changed
Matt Briggs 66:bcaa6dbf538a 165 bbio->regCCInInt(ccInIntObj);
Matt Briggs 66:bcaa6dbf538a 166 bbio->relayNormal();
Matt Briggs 66:bcaa6dbf538a 167 }
Matt Briggs 66:bcaa6dbf538a 168 if (bbio->isTx()) {
Matt Briggs 66:bcaa6dbf538a 169 protocol->setTx(true);
Matt Briggs 66:bcaa6dbf538a 170 }
Matt Briggs 66:bcaa6dbf538a 171 else { // RX
Matt Briggs 66:bcaa6dbf538a 172 protocol->setTx(false);
Matt Briggs 66:bcaa6dbf538a 173 }
Matt Briggs 61:8d9efd33cac9 174 if (protocol->isTx()) {
Matt Briggs 61:8d9efd33cac9 175 if (pairBtnState == pairBtnMediumPress) {
Matt Briggs 62:9751a8504c82 176 protocol->configForPairingNetwork();
Matt Briggs 61:8d9efd33cac9 177 protocol->sendPairReq();
Matt Briggs 63:e1efbe3402d9 178 myLogInfo("Sent pair request. Waiting %f secs for accept.", TX_ACCEPT_WAIT_TIME);
Matt Briggs 61:8d9efd33cac9 179 result = protocol->waitForAccept(TX_ACCEPT_WAIT_TIME);
Matt Briggs 61:8d9efd33cac9 180 if (result == cmdSuccess) {
Matt Briggs 63:e1efbe3402d9 181 myLogInfo("Got accept");
Matt Briggs 64:46c8819c07cc 182 ledPatterns.turnOff();
Matt Briggs 64:46c8819c07cc 183 wait(0.5);
Matt Briggs 64:46c8819c07cc 184 ledPatterns.tripleBlink();
Matt Briggs 61:8d9efd33cac9 185 }
Matt Briggs 61:8d9efd33cac9 186 else {
Matt Briggs 63:e1efbe3402d9 187 myLogInfo("Did not receive accept");
Matt Briggs 61:8d9efd33cac9 188 }
Matt Briggs 62:9751a8504c82 189 protocol->configForSavedNetwork();
Matt Briggs 63:e1efbe3402d9 190 protocol->printDotConfig();
Matt Briggs 64:46c8819c07cc 191 protocol->resetCounters();
Matt Briggs 71:2b57529df137 192
Matt Briggs 71:2b57529df137 193 // Clear test log
Matt Briggs 71:2b57529df137 194 rxSeqLog.clear();
Matt Briggs 71:2b57529df137 195 rxSeqLog.save();
Matt Briggs 71:2b57529df137 196 myLogInfo("NVM Cleared Seq Log");
Matt Briggs 61:8d9efd33cac9 197 }
Matt Briggs 61:8d9efd33cac9 198 }
Matt Briggs 61:8d9efd33cac9 199 if (protocol->isRx()) {
Matt Briggs 61:8d9efd33cac9 200 if (pairBtnState == pairBtnMediumPress) {
Matt Briggs 62:9751a8504c82 201 protocol->configForPairingNetwork();
Matt Briggs 64:46c8819c07cc 202 ledPatterns.turnOn();
Matt Briggs 63:e1efbe3402d9 203 myLogInfo("Waiting for pair request for %f seconds", RX_PAIR_WAIT_TIME);
Matt Briggs 61:8d9efd33cac9 204 result = protocol->waitForPairing(RX_PAIR_WAIT_TIME);
Matt Briggs 64:46c8819c07cc 205 ledPatterns.turnOff();
Matt Briggs 61:8d9efd33cac9 206 if (result == cmdSuccess) {
Matt Briggs 63:e1efbe3402d9 207 myLogInfo("Got pair request and responded");
Matt Briggs 64:46c8819c07cc 208 ledPatterns.tripleBlink();
Matt Briggs 61:8d9efd33cac9 209 }
Matt Briggs 61:8d9efd33cac9 210 else if (result == cmdTimeout) {
Matt Briggs 63:e1efbe3402d9 211 myLogInfo("Did not receive request");
Matt Briggs 61:8d9efd33cac9 212 }
Matt Briggs 61:8d9efd33cac9 213 else {
Matt Briggs 63:e1efbe3402d9 214 myLogInfo("Unknown pair error");
Matt Briggs 61:8d9efd33cac9 215 }
Matt Briggs 62:9751a8504c82 216 protocol->configForSavedNetwork();
Matt Briggs 63:e1efbe3402d9 217 protocol->printDotConfig();
Matt Briggs 64:46c8819c07cc 218 protocol->resetCounters();
Matt Briggs 71:2b57529df137 219
Matt Briggs 71:2b57529df137 220 // Clear test log
Matt Briggs 71:2b57529df137 221 rxSeqLog.clear();
Matt Briggs 71:2b57529df137 222 rxSeqLog.save();
Matt Briggs 71:2b57529df137 223 myLogInfo("NVM Cleared Seq Log");
Matt Briggs 61:8d9efd33cac9 224 }
Matt Briggs 61:8d9efd33cac9 225 else if (pairBtnState == pairBtnLongPress) {
Matt Briggs 63:e1efbe3402d9 226 myLogInfo("Clearing pair values and generating new ones.");
Matt Briggs 61:8d9efd33cac9 227 protocol->clearPair();
Matt Briggs 63:e1efbe3402d9 228 protocol->printDotConfig();
Matt Briggs 64:46c8819c07cc 229 ledPatterns.tenBlinks();
Matt Briggs 64:46c8819c07cc 230 protocol->resetCounters();
Matt Briggs 61:8d9efd33cac9 231 }
Matt Briggs 61:8d9efd33cac9 232 }
Matt Briggs 61:8d9efd33cac9 233 }
Matt Briggs 69:eb391644b346 234 else {
Matt Briggs 69:eb391644b346 235 pairBtnState = pairBtnNoPress;
Matt Briggs 69:eb391644b346 236 }
Matt Briggs 69:eb391644b346 237 pairBtnIntFlag = false;
Matt Briggs 47:a68747642a7a 238
Matt Briggs 75:600cb3a9f126 239 // Serial Terminal
Matt Briggs 75:600cb3a9f126 240 if (uartRxFlag) {
mbriggs_vortex 78:43f074baac34 241 pc.printf("Got RX Int\r\n");
Matt Briggs 75:600cb3a9f126 242 uart1RxIntIn.disable_irq();
Matt Briggs 75:600cb3a9f126 243 // pc.printf("Got uart flag!!!\r\n");
Matt Briggs 75:600cb3a9f126 244 uartRxFlag = false;
mbriggs_vortex 78:43f074baac34 245 outPc = new BufferedSerial(SER_TERM_TX, SER_TERM_RX);
Matt Briggs 76:d1b20a259d8f 246 outPc->baud(TERM_BAUD);
Matt Briggs 75:600cb3a9f126 247 serialTermMgr.regSerial(outPc);
Matt Briggs 75:600cb3a9f126 248 char c;
Matt Briggs 75:600cb3a9f126 249 if (outPc->readable()) {
Matt Briggs 75:600cb3a9f126 250 c = outPc->getc(); // Throw away the first char
Matt Briggs 75:600cb3a9f126 251 pc.printf("Got %d, %c\r\n", c, c);
Matt Briggs 75:600cb3a9f126 252 }
Matt Briggs 75:600cb3a9f126 253
Matt Briggs 75:600cb3a9f126 254 outPc->printf("Starting Terminal...\r\n");
Matt Briggs 75:600cb3a9f126 255 serialTermMgr.printScreen();
Matt Briggs 75:600cb3a9f126 256 time_t termLastAction = time(NULL);
Matt Briggs 75:600cb3a9f126 257 bool quit = false;
Matt Briggs 75:600cb3a9f126 258 while(!quit && (time(NULL) < (termLastAction + TERM_TIMEOUT))) {
Matt Briggs 75:600cb3a9f126 259 if (outPc->readable()) {
Matt Briggs 75:600cb3a9f126 260 // pc.printf("Got %d\r\n", outPc->getc());
Matt Briggs 77:176e3eb8f712 261 quit = serialTermMgr.input();
Matt Briggs 75:600cb3a9f126 262 termLastAction = time(NULL);
Matt Briggs 75:600cb3a9f126 263 }
Matt Briggs 75:600cb3a9f126 264 }
Matt Briggs 75:600cb3a9f126 265 if (quit) {
Matt Briggs 75:600cb3a9f126 266 outPc->printf("Terminal quit resuming operation <press any key to reactivate>...\r\n");
Matt Briggs 75:600cb3a9f126 267 }
Matt Briggs 75:600cb3a9f126 268 else {
Matt Briggs 75:600cb3a9f126 269 outPc->printf("Terminal timeout resuming operation <press any key to reactivate>...\r\n");
Matt Briggs 75:600cb3a9f126 270 }
Matt Briggs 77:176e3eb8f712 271 wait(0.01); // Wait for end message to be shifted out
Matt Briggs 75:600cb3a9f126 272 serialTermMgr.regSerial(NULL);
Matt Briggs 75:600cb3a9f126 273 delete outPc;
Matt Briggs 75:600cb3a9f126 274 uart1RxIntIn.enable_irq();
Matt Briggs 75:600cb3a9f126 275 }
Matt Briggs 75:600cb3a9f126 276 wait(0.1);
Matt Briggs 75:600cb3a9f126 277
Matt Briggs 69:eb391644b346 278 myLogInfo("Loop #%d. isTX %d, CCFlag %d, CCAlertState %d, TamperFlag %d, PairBtnState %d",
Matt Briggs 69:eb391644b346 279 loopCnt, protocol->isTx(), ccIntFlag, bbio->isCCInAlert(), tamperIntFlag, pairBtnState);
Matt Briggs 61:8d9efd33cac9 280 // Alert code
Matt Briggs 53:a1563574a980 281 if (protocol->isTx()) {
Matt Briggs 53:a1563574a980 282 // TODO add tamper
Matt Briggs 50:e89647e77fd5 283 if (ccIntFlag || // If contact closure in
Matt Briggs 57:bdac7dd17af2 284 bbio->isCCInAlert() || // If closure remains in effect
Matt Briggs 69:eb391644b346 285 pairBtnState == pairBtnShortPress) {
Matt Briggs 50:e89647e77fd5 286 ccIntFlag = false;
Matt Briggs 64:46c8819c07cc 287 ledPatterns.turnOn();
Matt Briggs 50:e89647e77fd5 288
Matt Briggs 66:bcaa6dbf538a 289 myLogInfo("Sending msg num: %d.", protocol->getSeqNum());
Matt Briggs 65:d546060aa03d 290 protocol->sendAlert(0xBEEF); // TODO use this field to encode the alert type e.g. CCIN vs tamper
Matt Briggs 64:46c8819c07cc 291 ledPatterns.turnOff();
Matt Briggs 50:e89647e77fd5 292 }
Matt Briggs 50:e89647e77fd5 293
Matt Briggs 64:46c8819c07cc 294 ledPatterns.turnOff();
Matt Briggs 69:eb391644b346 295 // tamperIntFlag = false;
Matt Briggs 69:eb391644b346 296 if (pairBtnIntFlag) {// An Interrupt happened during transmission
Matt Briggs 69:eb391644b346 297 continue; // Go straight to pair handling
Matt Briggs 69:eb391644b346 298 }
Matt Briggs 58:15aa7a785b9f 299 bbio->prepareSleep();
Matt Briggs 57:bdac7dd17af2 300 if (bbio->isCCInAlert()) { // Still in alert mode
Matt Briggs 68:51c25f4f6d9a 301 // Sleep for 5 seconds to ensure that receiver does not miss a message sequence
Matt Briggs 75:600cb3a9f126 302 dot->sleep(5, mDot::RTC_ALARM_OR_INTERRUPT, false); // Go to sleep and check in 5 secs if CCInAlert is asserted
Matt Briggs 57:bdac7dd17af2 303 }
Matt Briggs 57:bdac7dd17af2 304 else {
Matt Briggs 75:600cb3a9f126 305 dot->sleep(0, mDot::INTERRUPT, false); // Go to sleep until interrupt event
Matt Briggs 75:600cb3a9f126 306 // wait(0.5);
Matt Briggs 57:bdac7dd17af2 307 }
Matt Briggs 58:15aa7a785b9f 308 bbio->exitSleep();
Matt Briggs 47:a68747642a7a 309 }
Mike Fiore 11:d2e31743433a 310
Matt Briggs 53:a1563574a980 311 if (protocol->isRx()) {
Matt Briggs 66:bcaa6dbf538a 312 ccIntFlag = false;
Matt Briggs 69:eb391644b346 313 // tamperIntFlag = false;
Matt Briggs 69:eb391644b346 314 // myLogInfo("Before listen time %d", us_ticker_read());
Matt Briggs 47:a68747642a7a 315 bool msgPending;
Matt Briggs 53:a1563574a980 316 protocol->listen(msgPending);
Matt Briggs 47:a68747642a7a 317 if (msgPending) {
Matt Briggs 65:d546060aa03d 318 std::vector<uint8_t> txEui;
Matt Briggs 65:d546060aa03d 319 txEui.reserve(8);
Matt Briggs 65:d546060aa03d 320 uint16_t data;
Matt Briggs 65:d546060aa03d 321 uint32_t msgSeqNum;
Matt Briggs 65:d546060aa03d 322 protocol->recvAlert(txEui, data, msgSeqNum);
Matt Briggs 69:eb391644b346 323 if (msgSeqNum > maxSeenMsgSeqNum)
Matt Briggs 69:eb391644b346 324 maxSeenMsgSeqNum = msgSeqNum;
Matt Briggs 69:eb391644b346 325 rxMsgCnt++; // for message
Matt Briggs 69:eb391644b346 326 myLogInfo("Got rxMsgCnt #%d, with Seqnum: %d", rxMsgCnt, msgSeqNum);
Matt Briggs 53:a1563574a980 327 bbio->relayAlert();
Matt Briggs 64:46c8819c07cc 328 ledPatterns.turnOn();
Matt Briggs 66:bcaa6dbf538a 329 myLogInfo("Holding alert for %f secs", HoldTimeSetting::rotVal2Sec(bbio->rotarySwitch1()));
Matt Briggs 58:15aa7a785b9f 330 // Hold time for alert
Matt Briggs 58:15aa7a785b9f 331 // TODO maybe use sleep instead of wait
Matt Briggs 58:15aa7a785b9f 332 wait(HoldTimeSetting::rotVal2Sec(bbio->rotarySwitch1()));
Matt Briggs 64:46c8819c07cc 333 ledPatterns.turnOff();
Matt Briggs 58:15aa7a785b9f 334 bbio->relayNormal();
Matt Briggs 47:a68747642a7a 335 }
Matt Briggs 69:eb391644b346 336 if (pairBtnIntFlag) {// An Interrupt happened during reception
Matt Briggs 69:eb391644b346 337 continue; // Go straight to pair handling
Matt Briggs 69:eb391644b346 338 }
Matt Briggs 69:eb391644b346 339 //// TEMP LOGGING
Matt Briggs 69:eb391644b346 340 #if RX_SEQ_LOG
Matt Briggs 69:eb391644b346 341 if ((loopCnt % 1000 == 0) &&
Matt Briggs 69:eb391644b346 342 rxMsgCnt > rxSeqLog.rxMsgCount()) {
Matt Briggs 69:eb391644b346 343 rxSeqLog.setLoopCount(loopCnt);
Matt Briggs 69:eb391644b346 344 rxSeqLog.setRxMsgCount(rxMsgCnt);
Matt Briggs 69:eb391644b346 345 rxSeqLog.setMaxSeenMsgSeqNum(maxSeenMsgSeqNum);
Matt Briggs 69:eb391644b346 346 rxSeqLog.save();
Matt Briggs 69:eb391644b346 347 myLogInfo("EEPROM Saved.");
Matt Briggs 69:eb391644b346 348 }
Matt Briggs 69:eb391644b346 349 myLogInfo("NVM Log: Loop Cnt: %d, RxMsgCnt %d, MaxSeenSeqNum %d",
Matt Briggs 69:eb391644b346 350 rxSeqLog.loopCount(), rxSeqLog.rxMsgCount(), rxSeqLog.maxSeenMsgSeqNum());
Matt Briggs 69:eb391644b346 351 #endif
Matt Briggs 69:eb391644b346 352
Matt Briggs 63:e1efbe3402d9 353 myLogInfo("Sleeping. Time %d", us_ticker_read());
Matt Briggs 58:15aa7a785b9f 354 bbio->prepareSleep();
Matt Briggs 47:a68747642a7a 355 dot->sleep(2, mDot::RTC_ALARM_OR_INTERRUPT, false); // Go to sleep until wake button
Matt Briggs 58:15aa7a785b9f 356 bbio->exitSleep();
Matt Briggs 47:a68747642a7a 357 }
Matt Briggs 31:9c535a708ae9 358
Matt Briggs 64:46c8819c07cc 359 protocol->resetCounters();
Matt Briggs 63:e1efbe3402d9 360 myLogInfo("\r\n================================");
Matt Briggs 58:15aa7a785b9f 361 loopCnt++;
Mike Fiore 11:d2e31743433a 362 }
Matt Briggs 47:a68747642a7a 363
Matt Briggs 55:79ab0bbc5008 364 delete protocol;
Matt Briggs 55:79ab0bbc5008 365 delete bbio;
Mike Fiore 11:d2e31743433a 366 return 0;
Mike Fiore 11:d2e31743433a 367 }
Matt Briggs 47:a68747642a7a 368 #endif
Mike Fiore 11:d2e31743433a 369
Matt Briggs 27:6b68ff715ae1 370