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:
Wed Nov 29 13:54:36 2017 -0700
Revision:
100:0882cf295f8e
Parent:
99:83b54c851187
Adding relaese bin to repo

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 99:83b54c851187 21 const float BridgeVersion = 1.03; // Bumped 2017/11/29
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
mbriggs_vortex 95:aa9908d459c2 33 lora::ChannelPlan* plan = NULL;
Matt Briggs 39:64f79fa6e3cc 34
Matt Briggs 75:600cb3a9f126 35 volatile bool uartRxFlag;
Matt Briggs 50:e89647e77fd5 36 volatile bool ccIntFlag;
Matt Briggs 50:e89647e77fd5 37 volatile bool tamperIntFlag;
Matt Briggs 50:e89647e77fd5 38 volatile bool pairBtnIntFlag;
Matt Briggs 75:600cb3a9f126 39 void uart1RxCallback () {
Matt Briggs 75:600cb3a9f126 40 uartRxFlag = true;
Matt Briggs 75:600cb3a9f126 41 }
Matt Briggs 50:e89647e77fd5 42 void ccInIntCallback () {
Matt Briggs 50:e89647e77fd5 43 ccIntFlag = true;
Matt Briggs 50:e89647e77fd5 44 }
Matt Briggs 50:e89647e77fd5 45 void tamperIntCallback () {
Matt Briggs 50:e89647e77fd5 46 tamperIntFlag = true;
Matt Briggs 50:e89647e77fd5 47 }
Matt Briggs 50:e89647e77fd5 48 void pairBtnIntCallback () {
Matt Briggs 50:e89647e77fd5 49 pairBtnIntFlag = true;
Matt Briggs 50:e89647e77fd5 50 }
Matt Briggs 50:e89647e77fd5 51
Mike Fiore 11:d2e31743433a 52 int main() {
Matt Briggs 53:a1563574a980 53 CommProtocolPeerBrute *protocol = new CommProtocolPeerBrute();
Matt Briggs 53:a1563574a980 54 BaseboardIO *bbio = new BaseboardIO();
Matt Briggs 58:15aa7a785b9f 55 WinbondSPIFlash *flash = new WinbondSPIFlash(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_NSS);
Matt Briggs 77:176e3eb8f712 56 SerialTermMgr serialTermMgr(bbio, flash, BridgeVersion);
Matt Briggs 58:15aa7a785b9f 57
Matt Briggs 75:600cb3a9f126 58 pc.baud(115200);
Matt Briggs 52:64a2c71c7c49 59 CmdResult result;
Matt Briggs 75:600cb3a9f126 60 uartRxFlag = false;
Matt Briggs 50:e89647e77fd5 61 ccIntFlag = false;
Matt Briggs 50:e89647e77fd5 62 tamperIntFlag = false;
Matt Briggs 50:e89647e77fd5 63 pairBtnIntFlag = false;
Matt Briggs 48:bab9f747d9ed 64
Matt Briggs 31:9c535a708ae9 65 RadioEvent events; // Custom event handler for automatically displaying RX data
Matt Briggs 69:eb391644b346 66 #if RX_SEQ_LOG
Matt Briggs 69:eb391644b346 67 SimpleRxSeqLog rxSeqLog(0x0000);
Matt Briggs 69:eb391644b346 68 rxSeqLog.read();
Matt Briggs 69:eb391644b346 69 #endif
Mike Fiore 11:d2e31743433a 70
Matt Briggs 27:6b68ff715ae1 71 mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
Matt Briggs 66:bcaa6dbf538a 72 MyLog::setLogLevel(MyLog::DEBUG_LEVEL);
Matt Briggs 47:a68747642a7a 73
mbriggs_vortex 81:1eb0f16b2ab9 74 // use US915 plan
mbriggs_vortex 95:aa9908d459c2 75 plan = new lora::ChannelPlan_US915();
mbriggs_vortex 81:1eb0f16b2ab9 76 // use EU868 plan
mbriggs_vortex 81:1eb0f16b2ab9 77 // lora::ChannelPlan* plan = new lora::ChannelPlan_EU868();
mbriggs_vortex 81:1eb0f16b2ab9 78 assert(plan);
mbriggs_vortex 81:1eb0f16b2ab9 79
mbriggs_vortex 81:1eb0f16b2ab9 80 dot = mDot::getInstance(plan);
Mike Fiore 11:d2e31743433a 81
Mike Fiore 11:d2e31743433a 82 // make sure library logging is turned on
Matt Briggs 66:bcaa6dbf538a 83 dot->setLogLevel(mts::MTSLog::WARNING_LEVEL);
Mike Fiore 11:d2e31743433a 84
Matt Briggs 63:e1efbe3402d9 85 myLogInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);
Matt Briggs 63:e1efbe3402d9 86 myLogInfo("libxDot-mbed5 library ID: %s", dot->getId().c_str());
Matt Briggs 69:eb391644b346 87 myLogInfo("Vortex Wireless Bridge SW Version %0.2f", BridgeVersion);
Matt Briggs 62:9751a8504c82 88
Mike Fiore 11:d2e31743433a 89 // attach the custom events handler
Matt Briggs 54:c04d7b6fa075 90 dot->setEvents(&events); // Little bonus event debug information
Matt Briggs 53:a1563574a980 91
Matt Briggs 53:a1563574a980 92 // Finish radio init
Matt Briggs 63:e1efbe3402d9 93 myLogInfo("= Protocol Init Starting =\r\n");
Matt Briggs 53:a1563574a980 94 protocol->init();
Matt Briggs 62:9751a8504c82 95 if (result == cmdSuccess) {
Matt Briggs 63:e1efbe3402d9 96 myLogInfo("= Protocol Init Finished Successfully =\r\n");
Matt Briggs 62:9751a8504c82 97 }
Matt Briggs 62:9751a8504c82 98 else {
Matt Briggs 63:e1efbe3402d9 99 myLogInfo("= Protocol Init Finished with Error =\r\n");
Matt Briggs 62:9751a8504c82 100 }
Matt Briggs 63:e1efbe3402d9 101 protocol->printDotConfig();
Matt Briggs 53:a1563574a980 102
Matt Briggs 58:15aa7a785b9f 103 dot->setWakeMode(mDot::RTC_ALARM_OR_INTERRUPT);
Matt Briggs 75:600cb3a9f126 104 dot->setWakePin(GPIO0);
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");
mbriggs_vortex 95:aa9908d459c2 197 myLogInfo("Resetting...");
mbriggs_vortex 95:aa9908d459c2 198 dot->resetCpu();
Matt Briggs 61:8d9efd33cac9 199 }
Matt Briggs 61:8d9efd33cac9 200 }
Matt Briggs 61:8d9efd33cac9 201 if (protocol->isRx()) {
Matt Briggs 61:8d9efd33cac9 202 if (pairBtnState == pairBtnMediumPress) {
Matt Briggs 62:9751a8504c82 203 protocol->configForPairingNetwork();
Matt Briggs 64:46c8819c07cc 204 ledPatterns.turnOn();
Matt Briggs 63:e1efbe3402d9 205 myLogInfo("Waiting for pair request for %f seconds", RX_PAIR_WAIT_TIME);
Matt Briggs 61:8d9efd33cac9 206 result = protocol->waitForPairing(RX_PAIR_WAIT_TIME);
Matt Briggs 64:46c8819c07cc 207 ledPatterns.turnOff();
Matt Briggs 61:8d9efd33cac9 208 if (result == cmdSuccess) {
Matt Briggs 63:e1efbe3402d9 209 myLogInfo("Got pair request and responded");
Matt Briggs 64:46c8819c07cc 210 ledPatterns.tripleBlink();
Matt Briggs 61:8d9efd33cac9 211 }
Matt Briggs 61:8d9efd33cac9 212 else if (result == cmdTimeout) {
Matt Briggs 63:e1efbe3402d9 213 myLogInfo("Did not receive request");
Matt Briggs 61:8d9efd33cac9 214 }
Matt Briggs 61:8d9efd33cac9 215 else {
Matt Briggs 63:e1efbe3402d9 216 myLogInfo("Unknown pair error");
Matt Briggs 61:8d9efd33cac9 217 }
Matt Briggs 62:9751a8504c82 218 protocol->configForSavedNetwork();
Matt Briggs 63:e1efbe3402d9 219 protocol->printDotConfig();
Matt Briggs 64:46c8819c07cc 220 protocol->resetCounters();
Matt Briggs 71:2b57529df137 221
Matt Briggs 71:2b57529df137 222 // Clear test log
Matt Briggs 71:2b57529df137 223 rxSeqLog.clear();
Matt Briggs 71:2b57529df137 224 rxSeqLog.save();
Matt Briggs 71:2b57529df137 225 myLogInfo("NVM Cleared Seq Log");
mbriggs_vortex 95:aa9908d459c2 226 myLogInfo("Resetting...");
mbriggs_vortex 95:aa9908d459c2 227 dot->resetCpu();
Matt Briggs 61:8d9efd33cac9 228 }
Matt Briggs 61:8d9efd33cac9 229 else if (pairBtnState == pairBtnLongPress) {
Matt Briggs 63:e1efbe3402d9 230 myLogInfo("Clearing pair values and generating new ones.");
Matt Briggs 61:8d9efd33cac9 231 protocol->clearPair();
Matt Briggs 63:e1efbe3402d9 232 protocol->printDotConfig();
Matt Briggs 64:46c8819c07cc 233 ledPatterns.tenBlinks();
Matt Briggs 64:46c8819c07cc 234 protocol->resetCounters();
Matt Briggs 61:8d9efd33cac9 235 }
Matt Briggs 61:8d9efd33cac9 236 }
Matt Briggs 61:8d9efd33cac9 237 }
Matt Briggs 69:eb391644b346 238 else {
Matt Briggs 69:eb391644b346 239 pairBtnState = pairBtnNoPress;
Matt Briggs 69:eb391644b346 240 }
Matt Briggs 69:eb391644b346 241 pairBtnIntFlag = false;
Matt Briggs 47:a68747642a7a 242
Matt Briggs 75:600cb3a9f126 243 // Serial Terminal
Matt Briggs 75:600cb3a9f126 244 if (uartRxFlag) {
Matt Briggs 75:600cb3a9f126 245 uart1RxIntIn.disable_irq();
mbriggs_vortex 99:83b54c851187 246 prevCCNormallyOpen = bbio->isCCNO();
Matt Briggs 75:600cb3a9f126 247 uartRxFlag = false;
mbriggs_vortex 78:43f074baac34 248 outPc = new BufferedSerial(SER_TERM_TX, SER_TERM_RX);
Matt Briggs 76:d1b20a259d8f 249 outPc->baud(TERM_BAUD);
Matt Briggs 75:600cb3a9f126 250 serialTermMgr.regSerial(outPc);
Matt Briggs 75:600cb3a9f126 251 char c;
Matt Briggs 75:600cb3a9f126 252 if (outPc->readable()) {
Matt Briggs 75:600cb3a9f126 253 c = outPc->getc(); // Throw away the first char
Matt Briggs 75:600cb3a9f126 254 pc.printf("Got %d, %c\r\n", c, c);
Matt Briggs 75:600cb3a9f126 255 }
Matt Briggs 75:600cb3a9f126 256
Matt Briggs 75:600cb3a9f126 257 outPc->printf("Starting Terminal...\r\n");
Matt Briggs 75:600cb3a9f126 258 serialTermMgr.printScreen();
Matt Briggs 75:600cb3a9f126 259 time_t termLastAction = time(NULL);
Matt Briggs 75:600cb3a9f126 260 bool quit = false;
Matt Briggs 75:600cb3a9f126 261 while(!quit && (time(NULL) < (termLastAction + TERM_TIMEOUT))) {
Matt Briggs 75:600cb3a9f126 262 if (outPc->readable()) {
Matt Briggs 77:176e3eb8f712 263 quit = serialTermMgr.input();
Matt Briggs 75:600cb3a9f126 264 termLastAction = time(NULL);
Matt Briggs 75:600cb3a9f126 265 }
Matt Briggs 75:600cb3a9f126 266 }
Matt Briggs 75:600cb3a9f126 267 if (quit) {
Matt Briggs 75:600cb3a9f126 268 outPc->printf("Terminal quit resuming operation <press any key to reactivate>...\r\n");
Matt Briggs 75:600cb3a9f126 269 }
Matt Briggs 75:600cb3a9f126 270 else {
Matt Briggs 75:600cb3a9f126 271 outPc->printf("Terminal timeout resuming operation <press any key to reactivate>...\r\n");
Matt Briggs 75:600cb3a9f126 272 }
mbriggs_vortex 99:83b54c851187 273 // Update any configuration in case switches have been resampled or
mbriggs_vortex 99:83b54c851187 274 // changed via settings
mbriggs_vortex 99:83b54c851187 275 if (prevCCNormallyOpen != bbio->isCCNO()) { // Only activate the coil if the setting changed
mbriggs_vortex 99:83b54c851187 276 bbio->regCCInInt(ccInIntObj);
mbriggs_vortex 99:83b54c851187 277 bbio->relayNormal();
mbriggs_vortex 99:83b54c851187 278 }
mbriggs_vortex 99:83b54c851187 279 if (bbio->isTx()) {
mbriggs_vortex 99:83b54c851187 280 protocol->setTx(true);
mbriggs_vortex 99:83b54c851187 281 }
mbriggs_vortex 99:83b54c851187 282 else { // RX
mbriggs_vortex 99:83b54c851187 283 protocol->setTx(false);
mbriggs_vortex 99:83b54c851187 284 }
mbriggs_vortex 99:83b54c851187 285
Matt Briggs 77:176e3eb8f712 286 wait(0.01); // Wait for end message to be shifted out
Matt Briggs 75:600cb3a9f126 287 serialTermMgr.regSerial(NULL);
Matt Briggs 75:600cb3a9f126 288 delete outPc;
Matt Briggs 75:600cb3a9f126 289 uart1RxIntIn.enable_irq();
Matt Briggs 75:600cb3a9f126 290 }
Matt Briggs 75:600cb3a9f126 291 wait(0.1);
Matt Briggs 75:600cb3a9f126 292
Matt Briggs 69:eb391644b346 293 myLogInfo("Loop #%d. isTX %d, CCFlag %d, CCAlertState %d, TamperFlag %d, PairBtnState %d",
Matt Briggs 69:eb391644b346 294 loopCnt, protocol->isTx(), ccIntFlag, bbio->isCCInAlert(), tamperIntFlag, pairBtnState);
Matt Briggs 61:8d9efd33cac9 295 // Alert code
Matt Briggs 53:a1563574a980 296 if (protocol->isTx()) {
Matt Briggs 53:a1563574a980 297 // TODO add tamper
Matt Briggs 50:e89647e77fd5 298 if (ccIntFlag || // If contact closure in
Matt Briggs 57:bdac7dd17af2 299 bbio->isCCInAlert() || // If closure remains in effect
Matt Briggs 69:eb391644b346 300 pairBtnState == pairBtnShortPress) {
Matt Briggs 50:e89647e77fd5 301 ccIntFlag = false;
Matt Briggs 64:46c8819c07cc 302 ledPatterns.turnOn();
Matt Briggs 50:e89647e77fd5 303
Matt Briggs 66:bcaa6dbf538a 304 myLogInfo("Sending msg num: %d.", protocol->getSeqNum());
Matt Briggs 65:d546060aa03d 305 protocol->sendAlert(0xBEEF); // TODO use this field to encode the alert type e.g. CCIN vs tamper
Matt Briggs 64:46c8819c07cc 306 ledPatterns.turnOff();
Matt Briggs 50:e89647e77fd5 307 }
Matt Briggs 50:e89647e77fd5 308
Matt Briggs 64:46c8819c07cc 309 ledPatterns.turnOff();
Matt Briggs 69:eb391644b346 310 // tamperIntFlag = false;
Matt Briggs 69:eb391644b346 311 if (pairBtnIntFlag) {// An Interrupt happened during transmission
Matt Briggs 69:eb391644b346 312 continue; // Go straight to pair handling
Matt Briggs 69:eb391644b346 313 }
Matt Briggs 58:15aa7a785b9f 314 bbio->prepareSleep();
Matt Briggs 57:bdac7dd17af2 315 if (bbio->isCCInAlert()) { // Still in alert mode
Matt Briggs 68:51c25f4f6d9a 316 // Sleep for 5 seconds to ensure that receiver does not miss a message sequence
Matt Briggs 75:600cb3a9f126 317 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 318 }
Matt Briggs 57:bdac7dd17af2 319 else {
Matt Briggs 75:600cb3a9f126 320 dot->sleep(0, mDot::INTERRUPT, false); // Go to sleep until interrupt event
Matt Briggs 75:600cb3a9f126 321 // wait(0.5);
Matt Briggs 57:bdac7dd17af2 322 }
Matt Briggs 58:15aa7a785b9f 323 bbio->exitSleep();
Matt Briggs 47:a68747642a7a 324 }
Mike Fiore 11:d2e31743433a 325
Matt Briggs 53:a1563574a980 326 if (protocol->isRx()) {
Matt Briggs 66:bcaa6dbf538a 327 ccIntFlag = false;
Matt Briggs 69:eb391644b346 328 // tamperIntFlag = false;
Matt Briggs 69:eb391644b346 329 // myLogInfo("Before listen time %d", us_ticker_read());
Matt Briggs 47:a68747642a7a 330 bool msgPending;
Matt Briggs 53:a1563574a980 331 protocol->listen(msgPending);
Matt Briggs 47:a68747642a7a 332 if (msgPending) {
Matt Briggs 65:d546060aa03d 333 std::vector<uint8_t> txEui;
Matt Briggs 65:d546060aa03d 334 txEui.reserve(8);
Matt Briggs 65:d546060aa03d 335 uint16_t data;
Matt Briggs 65:d546060aa03d 336 uint32_t msgSeqNum;
Matt Briggs 65:d546060aa03d 337 protocol->recvAlert(txEui, data, msgSeqNum);
Matt Briggs 69:eb391644b346 338 if (msgSeqNum > maxSeenMsgSeqNum)
Matt Briggs 69:eb391644b346 339 maxSeenMsgSeqNum = msgSeqNum;
Matt Briggs 69:eb391644b346 340 rxMsgCnt++; // for message
Matt Briggs 69:eb391644b346 341 myLogInfo("Got rxMsgCnt #%d, with Seqnum: %d", rxMsgCnt, msgSeqNum);
Matt Briggs 53:a1563574a980 342 bbio->relayAlert();
Matt Briggs 64:46c8819c07cc 343 ledPatterns.turnOn();
Matt Briggs 66:bcaa6dbf538a 344 myLogInfo("Holding alert for %f secs", HoldTimeSetting::rotVal2Sec(bbio->rotarySwitch1()));
Matt Briggs 58:15aa7a785b9f 345 // Hold time for alert
Matt Briggs 58:15aa7a785b9f 346 // TODO maybe use sleep instead of wait
Matt Briggs 58:15aa7a785b9f 347 wait(HoldTimeSetting::rotVal2Sec(bbio->rotarySwitch1()));
Matt Briggs 64:46c8819c07cc 348 ledPatterns.turnOff();
Matt Briggs 58:15aa7a785b9f 349 bbio->relayNormal();
Matt Briggs 47:a68747642a7a 350 }
Matt Briggs 69:eb391644b346 351 if (pairBtnIntFlag) {// An Interrupt happened during reception
Matt Briggs 69:eb391644b346 352 continue; // Go straight to pair handling
Matt Briggs 69:eb391644b346 353 }
Matt Briggs 69:eb391644b346 354 //// TEMP LOGGING
Matt Briggs 69:eb391644b346 355 #if RX_SEQ_LOG
Matt Briggs 69:eb391644b346 356 if ((loopCnt % 1000 == 0) &&
Matt Briggs 69:eb391644b346 357 rxMsgCnt > rxSeqLog.rxMsgCount()) {
Matt Briggs 69:eb391644b346 358 rxSeqLog.setLoopCount(loopCnt);
Matt Briggs 69:eb391644b346 359 rxSeqLog.setRxMsgCount(rxMsgCnt);
Matt Briggs 69:eb391644b346 360 rxSeqLog.setMaxSeenMsgSeqNum(maxSeenMsgSeqNum);
Matt Briggs 69:eb391644b346 361 rxSeqLog.save();
Matt Briggs 69:eb391644b346 362 myLogInfo("EEPROM Saved.");
Matt Briggs 69:eb391644b346 363 }
Matt Briggs 69:eb391644b346 364 myLogInfo("NVM Log: Loop Cnt: %d, RxMsgCnt %d, MaxSeenSeqNum %d",
Matt Briggs 69:eb391644b346 365 rxSeqLog.loopCount(), rxSeqLog.rxMsgCount(), rxSeqLog.maxSeenMsgSeqNum());
Matt Briggs 69:eb391644b346 366 #endif
Matt Briggs 69:eb391644b346 367
Matt Briggs 63:e1efbe3402d9 368 myLogInfo("Sleeping. Time %d", us_ticker_read());
Matt Briggs 58:15aa7a785b9f 369 bbio->prepareSleep();
Matt Briggs 47:a68747642a7a 370 dot->sleep(2, mDot::RTC_ALARM_OR_INTERRUPT, false); // Go to sleep until wake button
Matt Briggs 58:15aa7a785b9f 371 bbio->exitSleep();
Matt Briggs 47:a68747642a7a 372 }
Matt Briggs 31:9c535a708ae9 373
Matt Briggs 64:46c8819c07cc 374 protocol->resetCounters();
Matt Briggs 63:e1efbe3402d9 375 myLogInfo("\r\n================================");
Matt Briggs 58:15aa7a785b9f 376 loopCnt++;
Mike Fiore 11:d2e31743433a 377 }
Matt Briggs 47:a68747642a7a 378
Matt Briggs 55:79ab0bbc5008 379 delete protocol;
Matt Briggs 55:79ab0bbc5008 380 delete bbio;
Mike Fiore 11:d2e31743433a 381 return 0;
Mike Fiore 11:d2e31743433a 382 }
Matt Briggs 47:a68747642a7a 383 #endif
Mike Fiore 11:d2e31743433a 384
Matt Briggs 27:6b68ff715ae1 385