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 22:58:20 2017 +0000
Revision:
90:32f9d043561a
Parent:
89:edfe5d376e55
Child:
91:8196900df6fe
Fixed version number

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