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:
Matt Briggs
Date:
Tue Mar 21 14:29:32 2017 -0600
Revision:
68:51c25f4f6d9a
Parent:
67:2115a2f1b945
Child:
69:eb391644b346
Changed how long between messages in continous alert condition.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Matt Briggs 34:5618603e5fc3 1 #include <math.h>
Matt Briggs 22:9453658b8d4b 2 #include "config.h"
Matt Briggs 30:2e673a672884 3 #include "xdot_flash.h"
Mike Fiore 11:d2e31743433a 4 #include "dot_util.h"
Mike Fiore 14:19fae4509473 5 #include "RadioEvent.h"
Matt Briggs 47:a68747642a7a 6 #include "WinbondSPIFlash.h"
Matt Briggs 58:15aa7a785b9f 7 #include "UserInterface.h"
Matt Briggs 41:9ef4c4d77711 8 //#include <xdot_low_power.h>
Matt Briggs 58:15aa7a785b9f 9 #include "mDot.h"
Matt Briggs 63:e1efbe3402d9 10 #include "MyLog.h"
mfiore 17:d4f82e16de5f 11
Matt Briggs 47:a68747642a7a 12 #include "BaseboardIO.h"
Matt Briggs 41:9ef4c4d77711 13 #include "CommProtocolPeerBrute.h"
Matt Briggs 31:9c535a708ae9 14
Matt Briggs 47:a68747642a7a 15 #ifndef __TEST__ // Exclude code for tests
Mike Fiore 11:d2e31743433a 16 Serial pc(USBTX, USBRX);
Mike Fiore 11:d2e31743433a 17
Matt Briggs 47:a68747642a7a 18 mDot* dot = NULL; // Used by dot-utils
Matt Briggs 39:64f79fa6e3cc 19
Matt Briggs 50:e89647e77fd5 20 volatile bool ccIntFlag;
Matt Briggs 50:e89647e77fd5 21 volatile bool tamperIntFlag;
Matt Briggs 50:e89647e77fd5 22 volatile bool pairBtnIntFlag;
Matt Briggs 50:e89647e77fd5 23 void ccInIntCallback () {
Matt Briggs 50:e89647e77fd5 24 ccIntFlag = true;
Matt Briggs 50:e89647e77fd5 25 }
Matt Briggs 50:e89647e77fd5 26 void tamperIntCallback () {
Matt Briggs 50:e89647e77fd5 27 tamperIntFlag = true;
Matt Briggs 50:e89647e77fd5 28 }
Matt Briggs 50:e89647e77fd5 29 void pairBtnIntCallback () {
Matt Briggs 50:e89647e77fd5 30 pairBtnIntFlag = true;
Matt Briggs 50:e89647e77fd5 31 }
Matt Briggs 50:e89647e77fd5 32
Mike Fiore 11:d2e31743433a 33 int main() {
Matt Briggs 53:a1563574a980 34 CommProtocolPeerBrute *protocol = new CommProtocolPeerBrute();
Matt Briggs 53:a1563574a980 35 BaseboardIO *bbio = new BaseboardIO();
Matt Briggs 58:15aa7a785b9f 36 WinbondSPIFlash *flash = new WinbondSPIFlash(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_NSS);
Matt Briggs 58:15aa7a785b9f 37
Matt Briggs 52:64a2c71c7c49 38 CmdResult result;
Matt Briggs 50:e89647e77fd5 39 ccIntFlag = false;
Matt Briggs 50:e89647e77fd5 40 tamperIntFlag = false;
Matt Briggs 50:e89647e77fd5 41 pairBtnIntFlag = false;
Matt Briggs 48:bab9f747d9ed 42 pc.baud(115200);
Matt Briggs 48:bab9f747d9ed 43
Matt Briggs 31:9c535a708ae9 44 RadioEvent events; // Custom event handler for automatically displaying RX data
Mike Fiore 11:d2e31743433a 45
Matt Briggs 27:6b68ff715ae1 46 mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL);
Matt Briggs 66:bcaa6dbf538a 47 MyLog::setLogLevel(MyLog::DEBUG_LEVEL);
Matt Briggs 47:a68747642a7a 48
Mike Fiore 11:d2e31743433a 49 dot = mDot::getInstance();
Mike Fiore 11:d2e31743433a 50
Mike Fiore 11:d2e31743433a 51 // make sure library logging is turned on
Matt Briggs 66:bcaa6dbf538a 52 dot->setLogLevel(mts::MTSLog::WARNING_LEVEL);
Mike Fiore 11:d2e31743433a 53
Matt Briggs 63:e1efbe3402d9 54 myLogInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION);
Matt Briggs 63:e1efbe3402d9 55 myLogInfo("libxDot-mbed5 library ID: %s", dot->getId().c_str());
Matt Briggs 62:9751a8504c82 56
Mike Fiore 11:d2e31743433a 57 // attach the custom events handler
Matt Briggs 54:c04d7b6fa075 58 dot->setEvents(&events); // Little bonus event debug information
Matt Briggs 53:a1563574a980 59
Matt Briggs 53:a1563574a980 60 // Finish radio init
Matt Briggs 63:e1efbe3402d9 61 myLogInfo("= Protocol Init Starting =\r\n");
Matt Briggs 53:a1563574a980 62 protocol->init();
Matt Briggs 62:9751a8504c82 63 if (result == cmdSuccess) {
Matt Briggs 63:e1efbe3402d9 64 myLogInfo("= Protocol Init Finished Successfully =\r\n");
Matt Briggs 62:9751a8504c82 65 }
Matt Briggs 62:9751a8504c82 66 else {
Matt Briggs 63:e1efbe3402d9 67 myLogInfo("= Protocol Init Finished with Error =\r\n");
Matt Briggs 62:9751a8504c82 68 }
Matt Briggs 63:e1efbe3402d9 69 protocol->printDotConfig();
Matt Briggs 53:a1563574a980 70
Matt Briggs 53:a1563574a980 71 // save changes to configuration
Matt Briggs 53:a1563574a980 72 // logInfo("saving configuration");
Matt Briggs 53:a1563574a980 73 // core_util_critical_section_enter();
Matt Briggs 53:a1563574a980 74 // bool dotConfigSaved = dot->saveConfig();
Matt Briggs 53:a1563574a980 75 // core_util_critical_section_exit();
Matt Briggs 53:a1563574a980 76 // if (!dotConfigSaved) {
Matt Briggs 53:a1563574a980 77 // logError("failed to save configuration");
Matt Briggs 53:a1563574a980 78 // }
Mike Fiore 11:d2e31743433a 79
Matt Briggs 31:9c535a708ae9 80 // Setup programmable voltage detector
Matt Briggs 31:9c535a708ae9 81 // PVD_LEVEL0 Falling 1.85
Matt Briggs 31:9c535a708ae9 82 // PVD_LEVEL1 Falling 2.04
Matt Briggs 31:9c535a708ae9 83 // PVD_LEVEL2 Falling 2.24
Matt Briggs 31:9c535a708ae9 84 // PVD_LEVEL3 Falling 2.44
Matt Briggs 31:9c535a708ae9 85 // PVD_LEVEL4 Falling 2.64
Matt Briggs 31:9c535a708ae9 86 // PVD_LEVEL5 Falling 2.84
Matt Briggs 31:9c535a708ae9 87 // PVD_LEVEL6 Falling 3.05
Matt Briggs 50:e89647e77fd5 88 // PWR_PVDTypeDef pvdConfig;
Matt Briggs 50:e89647e77fd5 89 // pvdConfig.Mode = PWR_PVD_MODE_NORMAL;
Matt Briggs 50:e89647e77fd5 90 // pvdConfig.PVDLevel = PWR_PVDLEVEL_5;
Matt Briggs 50:e89647e77fd5 91 //
Matt Briggs 50:e89647e77fd5 92 // HAL_PWR_ConfigPVD(&pvdConfig);
Matt Briggs 50:e89647e77fd5 93 // HAL_PWR_EnablePVD();
Matt Briggs 50:e89647e77fd5 94 // logInfo("Programmable Voltage Detector set for level: %d", pvdConfig.PVDLevel);
Matt Briggs 50:e89647e77fd5 95 // // HAL_PWR_PVDCallback need to define this I think this will override the current implementation
Mike Fiore 11:d2e31743433a 96
Matt Briggs 58:15aa7a785b9f 97 dot->setWakeMode(mDot::RTC_ALARM_OR_INTERRUPT);
Matt Briggs 54:c04d7b6fa075 98 dot->setWakePin(UART1_RX);
Matt Briggs 24:fdf87e4b72e5 99
Matt Briggs 62:9751a8504c82 100 display_config(); // Print configuration for now
Matt Briggs 62:9751a8504c82 101
Mike Fiore 11:d2e31743433a 102 // display configuration
Matt Briggs 53:a1563574a980 103 // display_config();
Matt Briggs 52:64a2c71c7c49 104
Matt Briggs 63:e1efbe3402d9 105 myLogInfo("= Baseboard Init Starting =\r\n");
Matt Briggs 53:a1563574a980 106 result = bbio->init();
Matt Briggs 52:64a2c71c7c49 107 if (result == cmdSuccess) {
Matt Briggs 63:e1efbe3402d9 108 myLogInfo("= Baseboard Init Finished Successfully =\r\n");
Matt Briggs 52:64a2c71c7c49 109 }
Matt Briggs 52:64a2c71c7c49 110 else {
Matt Briggs 63:e1efbe3402d9 111 myLogInfo("= Baseboard Init Finished with Error =\r\n");
Matt Briggs 52:64a2c71c7c49 112 }
Matt Briggs 64:46c8819c07cc 113 LedPatterns ledPatterns(bbio);
Mike Fiore 11:d2e31743433a 114
Matt Briggs 50:e89647e77fd5 115 Callback<void()> ccInIntObj (&ccInIntCallback);
Matt Briggs 50:e89647e77fd5 116 Callback<void()> tamperIntObj (&tamperIntCallback);
Matt Briggs 50:e89647e77fd5 117 Callback<void()> pairBtnIntObj (&pairBtnIntCallback);
Matt Briggs 50:e89647e77fd5 118
Matt Briggs 53:a1563574a980 119 bbio->sampleUserSwitches();
Matt Briggs 53:a1563574a980 120 bbio->relayNormal(); // Always force relay in known state
Matt Briggs 58:15aa7a785b9f 121 bbio->regCCInInt(ccInIntObj);
Matt Briggs 58:15aa7a785b9f 122 bbio->regTamperInt(tamperIntObj);
Matt Briggs 58:15aa7a785b9f 123 bbio->regPairBtnInt(pairBtnIntObj);
Matt Briggs 26:9411b26a5084 124
Matt Briggs 66:bcaa6dbf538a 125 if (bbio->isTx()) {
Matt Briggs 66:bcaa6dbf538a 126 protocol->setTx(true);
Matt Briggs 66:bcaa6dbf538a 127 }
Matt Briggs 66:bcaa6dbf538a 128 else { // RX
Matt Briggs 66:bcaa6dbf538a 129 protocol->setTx(false);
Matt Briggs 66:bcaa6dbf538a 130 }
Matt Briggs 66:bcaa6dbf538a 131
Matt Briggs 58:15aa7a785b9f 132 // Start flash powered down
Matt Briggs 58:15aa7a785b9f 133 flash->powerDown();
Matt Briggs 58:15aa7a785b9f 134
Matt Briggs 58:15aa7a785b9f 135 unsigned int loopCnt = 0; // Just a quick temp varaible to keep track of loopNums
Matt Briggs 58:15aa7a785b9f 136 bool prevCCNormallyOpen;
Matt Briggs 61:8d9efd33cac9 137 PairBtnState pairBtnState;
Matt Briggs 67:2115a2f1b945 138 uint32_t rxMsgCnt = 0;
Matt Briggs 47:a68747642a7a 139 /**
Matt Briggs 47:a68747642a7a 140 * Main Loop
Matt Briggs 47:a68747642a7a 141 */
Mike Fiore 11:d2e31743433a 142 while (true) {
Matt Briggs 66:bcaa6dbf538a 143 myLogInfo("Start of loop time %d", us_ticker_read());
Matt Briggs 64:46c8819c07cc 144 ledPatterns.turnOff();
Matt Briggs 47:a68747642a7a 145
Matt Briggs 66:bcaa6dbf538a 146 // Pair logic and switch sampling
Matt Briggs 61:8d9efd33cac9 147 if (pairBtnIntFlag) {
Matt Briggs 61:8d9efd33cac9 148 pairBtnState = PairBtnInterp::read(bbio);
Matt Briggs 66:bcaa6dbf538a 149
Matt Briggs 66:bcaa6dbf538a 150 // Sample IO and update any configuration
Matt Briggs 66:bcaa6dbf538a 151 prevCCNormallyOpen = bbio->isCCNO();
Matt Briggs 66:bcaa6dbf538a 152 bbio->sampleUserSwitches();
Matt Briggs 66:bcaa6dbf538a 153 if (prevCCNormallyOpen != bbio->isCCNO()) { // Only activate the coil if the DIP SW has changed
Matt Briggs 66:bcaa6dbf538a 154 bbio->regCCInInt(ccInIntObj);
Matt Briggs 66:bcaa6dbf538a 155 bbio->relayNormal();
Matt Briggs 66:bcaa6dbf538a 156 }
Matt Briggs 66:bcaa6dbf538a 157 if (bbio->isTx()) {
Matt Briggs 66:bcaa6dbf538a 158 protocol->setTx(true);
Matt Briggs 66:bcaa6dbf538a 159 }
Matt Briggs 66:bcaa6dbf538a 160 else { // RX
Matt Briggs 66:bcaa6dbf538a 161 protocol->setTx(false);
Matt Briggs 66:bcaa6dbf538a 162 }
Matt Briggs 61:8d9efd33cac9 163 if (protocol->isTx()) {
Matt Briggs 61:8d9efd33cac9 164 if (pairBtnState == pairBtnMediumPress) {
Matt Briggs 62:9751a8504c82 165 protocol->configForPairingNetwork();
Matt Briggs 61:8d9efd33cac9 166 protocol->sendPairReq();
Matt Briggs 63:e1efbe3402d9 167 myLogInfo("Sent pair request. Waiting %f secs for accept.", TX_ACCEPT_WAIT_TIME);
Matt Briggs 61:8d9efd33cac9 168 result = protocol->waitForAccept(TX_ACCEPT_WAIT_TIME);
Matt Briggs 61:8d9efd33cac9 169 if (result == cmdSuccess) {
Matt Briggs 63:e1efbe3402d9 170 myLogInfo("Got accept");
Matt Briggs 64:46c8819c07cc 171 ledPatterns.turnOff();
Matt Briggs 64:46c8819c07cc 172 wait(0.5);
Matt Briggs 64:46c8819c07cc 173 ledPatterns.tripleBlink();
Matt Briggs 61:8d9efd33cac9 174 }
Matt Briggs 61:8d9efd33cac9 175 else {
Matt Briggs 63:e1efbe3402d9 176 myLogInfo("Did not receive accept");
Matt Briggs 61:8d9efd33cac9 177 }
Matt Briggs 62:9751a8504c82 178 protocol->configForSavedNetwork();
Matt Briggs 63:e1efbe3402d9 179 protocol->printDotConfig();
Matt Briggs 64:46c8819c07cc 180 protocol->resetCounters();
Matt Briggs 61:8d9efd33cac9 181 }
Matt Briggs 61:8d9efd33cac9 182 }
Matt Briggs 61:8d9efd33cac9 183 if (protocol->isRx()) {
Matt Briggs 61:8d9efd33cac9 184 if (pairBtnState == pairBtnMediumPress) {
Matt Briggs 62:9751a8504c82 185 protocol->configForPairingNetwork();
Matt Briggs 64:46c8819c07cc 186 ledPatterns.turnOn();
Matt Briggs 63:e1efbe3402d9 187 myLogInfo("Waiting for pair request for %f seconds", RX_PAIR_WAIT_TIME);
Matt Briggs 61:8d9efd33cac9 188 result = protocol->waitForPairing(RX_PAIR_WAIT_TIME);
Matt Briggs 64:46c8819c07cc 189 ledPatterns.turnOff();
Matt Briggs 61:8d9efd33cac9 190 if (result == cmdSuccess) {
Matt Briggs 63:e1efbe3402d9 191 myLogInfo("Got pair request and responded");
Matt Briggs 64:46c8819c07cc 192 ledPatterns.tripleBlink();
Matt Briggs 61:8d9efd33cac9 193 }
Matt Briggs 61:8d9efd33cac9 194 else if (result == cmdTimeout) {
Matt Briggs 63:e1efbe3402d9 195 myLogInfo("Did not receive request");
Matt Briggs 61:8d9efd33cac9 196 }
Matt Briggs 61:8d9efd33cac9 197 else {
Matt Briggs 63:e1efbe3402d9 198 myLogInfo("Unknown pair error");
Matt Briggs 61:8d9efd33cac9 199 }
Matt Briggs 62:9751a8504c82 200 protocol->configForSavedNetwork();
Matt Briggs 63:e1efbe3402d9 201 protocol->printDotConfig();
Matt Briggs 64:46c8819c07cc 202 protocol->resetCounters();
Matt Briggs 61:8d9efd33cac9 203 }
Matt Briggs 61:8d9efd33cac9 204 else if (pairBtnState == pairBtnLongPress) {
Matt Briggs 63:e1efbe3402d9 205 myLogInfo("Clearing pair values and generating new ones.");
Matt Briggs 61:8d9efd33cac9 206 protocol->clearPair();
Matt Briggs 63:e1efbe3402d9 207 protocol->printDotConfig();
Matt Briggs 64:46c8819c07cc 208 ledPatterns.tenBlinks();
Matt Briggs 64:46c8819c07cc 209 protocol->resetCounters();
Matt Briggs 61:8d9efd33cac9 210 }
Matt Briggs 61:8d9efd33cac9 211 }
Matt Briggs 61:8d9efd33cac9 212 }
Matt Briggs 47:a68747642a7a 213
Matt Briggs 61:8d9efd33cac9 214 // Alert code
Matt Briggs 53:a1563574a980 215 if (protocol->isTx()) {
Matt Briggs 63:e1efbe3402d9 216 myLogInfo("Loop #%d. CCFlag %d, CCAlertState %d, TamperFlag %d, PairBtnFlag %d",
Matt Briggs 58:15aa7a785b9f 217 loopCnt, ccIntFlag, bbio->isCCInAlert(), tamperIntFlag, pairBtnIntFlag);
Matt Briggs 53:a1563574a980 218 // TODO add tamper
Matt Briggs 50:e89647e77fd5 219 if (ccIntFlag || // If contact closure in
Matt Briggs 57:bdac7dd17af2 220 bbio->isCCInAlert() || // If closure remains in effect
Matt Briggs 61:8d9efd33cac9 221 (pairBtnIntFlag && (pairBtnState == pairBtnShortPress))) {
Matt Briggs 50:e89647e77fd5 222 ccIntFlag = false;
Matt Briggs 64:46c8819c07cc 223 ledPatterns.turnOn();
Matt Briggs 50:e89647e77fd5 224
Matt Briggs 66:bcaa6dbf538a 225 myLogInfo("Sending msg num: %d.", protocol->getSeqNum());
Matt Briggs 65:d546060aa03d 226 protocol->sendAlert(0xBEEF); // TODO use this field to encode the alert type e.g. CCIN vs tamper
Matt Briggs 64:46c8819c07cc 227 ledPatterns.turnOff();
Matt Briggs 50:e89647e77fd5 228 }
Matt Briggs 50:e89647e77fd5 229
Matt Briggs 64:46c8819c07cc 230 ledPatterns.turnOff();
Matt Briggs 62:9751a8504c82 231 pairBtnIntFlag = false;
Matt Briggs 62:9751a8504c82 232 tamperIntFlag = false;
Matt Briggs 58:15aa7a785b9f 233 bbio->prepareSleep();
Matt Briggs 57:bdac7dd17af2 234 if (bbio->isCCInAlert()) { // Still in alert mode
Matt Briggs 68:51c25f4f6d9a 235 // Sleep for 5 seconds to ensure that receiver does not miss a message sequence
Matt Briggs 68:51c25f4f6d9a 236 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 237 }
Matt Briggs 57:bdac7dd17af2 238 else {
Matt Briggs 57:bdac7dd17af2 239 dot->sleep(0, mDot::INTERRUPT, false); // Go to sleep until wake button
Matt Briggs 57:bdac7dd17af2 240 }
Matt Briggs 58:15aa7a785b9f 241 bbio->exitSleep();
Matt Briggs 47:a68747642a7a 242 }
Mike Fiore 11:d2e31743433a 243
Matt Briggs 53:a1563574a980 244 if (protocol->isRx()) {
Matt Briggs 66:bcaa6dbf538a 245 ccIntFlag = false;
Matt Briggs 66:bcaa6dbf538a 246 pairBtnIntFlag = false;
Matt Briggs 66:bcaa6dbf538a 247 tamperIntFlag = false;
Matt Briggs 66:bcaa6dbf538a 248 myLogInfo("Before listen time %d", us_ticker_read());
Matt Briggs 47:a68747642a7a 249 bool msgPending;
Matt Briggs 53:a1563574a980 250 protocol->listen(msgPending);
Matt Briggs 63:e1efbe3402d9 251 myLogInfo("Loop Cnt %d. Listening.", loopCnt);
Matt Briggs 47:a68747642a7a 252 if (msgPending) {
Matt Briggs 65:d546060aa03d 253 std::vector<uint8_t> txEui;
Matt Briggs 65:d546060aa03d 254 txEui.reserve(8);
Matt Briggs 65:d546060aa03d 255 uint16_t data;
Matt Briggs 65:d546060aa03d 256 uint32_t msgSeqNum;
Matt Briggs 65:d546060aa03d 257 protocol->recvAlert(txEui, data, msgSeqNum);
Matt Briggs 67:2115a2f1b945 258 myLogInfo("Got msg #%d, with Seqnum: %d", rxMsgCnt, msgSeqNum);
Matt Briggs 53:a1563574a980 259 bbio->relayAlert();
Matt Briggs 64:46c8819c07cc 260 ledPatterns.turnOn();
Matt Briggs 66:bcaa6dbf538a 261 myLogInfo("Holding alert for %f secs", HoldTimeSetting::rotVal2Sec(bbio->rotarySwitch1()));
Matt Briggs 58:15aa7a785b9f 262 // Hold time for alert
Matt Briggs 58:15aa7a785b9f 263 // TODO maybe use sleep instead of wait
Matt Briggs 58:15aa7a785b9f 264 wait(HoldTimeSetting::rotVal2Sec(bbio->rotarySwitch1()));
Matt Briggs 64:46c8819c07cc 265 ledPatterns.turnOff();
Matt Briggs 58:15aa7a785b9f 266 bbio->relayNormal();
Matt Briggs 47:a68747642a7a 267 }
Matt Briggs 63:e1efbe3402d9 268 myLogInfo("Sleeping. Time %d", us_ticker_read());
Matt Briggs 58:15aa7a785b9f 269 bbio->prepareSleep();
Matt Briggs 47:a68747642a7a 270 dot->sleep(2, mDot::RTC_ALARM_OR_INTERRUPT, false); // Go to sleep until wake button
Matt Briggs 58:15aa7a785b9f 271 bbio->exitSleep();
Matt Briggs 47:a68747642a7a 272 }
Matt Briggs 31:9c535a708ae9 273
Matt Briggs 64:46c8819c07cc 274 protocol->resetCounters();
Matt Briggs 63:e1efbe3402d9 275 myLogInfo("\r\n================================");
Matt Briggs 58:15aa7a785b9f 276 loopCnt++;
Mike Fiore 11:d2e31743433a 277 }
Matt Briggs 47:a68747642a7a 278
Matt Briggs 55:79ab0bbc5008 279 delete protocol;
Matt Briggs 55:79ab0bbc5008 280 delete bbio;
Mike Fiore 11:d2e31743433a 281 return 0;
Mike Fiore 11:d2e31743433a 282 }
Matt Briggs 47:a68747642a7a 283 #endif
Mike Fiore 11:d2e31743433a 284
Matt Briggs 27:6b68ff715ae1 285