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:
Wed Mar 15 09:49:58 2017 -0600
Revision:
65:d546060aa03d
Parent:
64:46c8819c07cc
Child:
66:bcaa6dbf538a
Added better alert message code.  Quick commit before debug.

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 63:e1efbe3402d9 47 MyLog::setLogLevel(MyLog::INFO_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 65:d546060aa03d 52 dot->setLogLevel(mts::MTSLog::INFO_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 58:15aa7a785b9f 125 // Start flash powered down
Matt Briggs 58:15aa7a785b9f 126 flash->powerDown();
Matt Briggs 58:15aa7a785b9f 127
Matt Briggs 58:15aa7a785b9f 128 unsigned int loopCnt = 0; // Just a quick temp varaible to keep track of loopNums
Matt Briggs 58:15aa7a785b9f 129 bool prevCCNormallyOpen;
Matt Briggs 61:8d9efd33cac9 130 PairBtnState pairBtnState;
Matt Briggs 47:a68747642a7a 131 /**
Matt Briggs 47:a68747642a7a 132 * Main Loop
Matt Briggs 47:a68747642a7a 133 */
Mike Fiore 11:d2e31743433a 134 while (true) {
Matt Briggs 65:d546060aa03d 135 // std::vector<uint8_t> data;
Matt Briggs 64:46c8819c07cc 136 ledPatterns.turnOff();
Matt Briggs 47:a68747642a7a 137
Matt Briggs 50:e89647e77fd5 138 // Sample IO and update any configuration
Matt Briggs 58:15aa7a785b9f 139 prevCCNormallyOpen = bbio->isCCNO();
Matt Briggs 53:a1563574a980 140 bbio->sampleUserSwitches();
Matt Briggs 53:a1563574a980 141 if (prevCCNormallyOpen == bbio->isCCNO()) { // Only activate the coil if the DIP SW has changed
Matt Briggs 53:a1563574a980 142 bbio->regCCInInt(ccInIntObj);
Matt Briggs 53:a1563574a980 143 bbio->relayNormal();
Matt Briggs 50:e89647e77fd5 144 }
Matt Briggs 53:a1563574a980 145 if (bbio->isTx()) {
Matt Briggs 53:a1563574a980 146 protocol->setTx(true);
Matt Briggs 50:e89647e77fd5 147 }
Matt Briggs 50:e89647e77fd5 148 else { // RX
Matt Briggs 53:a1563574a980 149 protocol->setTx(false);
Matt Briggs 50:e89647e77fd5 150 }
Matt Briggs 50:e89647e77fd5 151 // End sample and update
Matt Briggs 61:8d9efd33cac9 152 // Pair logic
Matt Briggs 61:8d9efd33cac9 153 if (pairBtnIntFlag) {
Matt Briggs 61:8d9efd33cac9 154 pairBtnState = PairBtnInterp::read(bbio);
Matt Briggs 61:8d9efd33cac9 155 if (protocol->isTx()) {
Matt Briggs 61:8d9efd33cac9 156 if (pairBtnState == pairBtnMediumPress) {
Matt Briggs 64:46c8819c07cc 157 // protocol->sampleDLC();
Matt Briggs 62:9751a8504c82 158 protocol->configForPairingNetwork();
Matt Briggs 61:8d9efd33cac9 159 protocol->sendPairReq();
Matt Briggs 63:e1efbe3402d9 160 myLogInfo("Sent pair request. Waiting %f secs for accept.", TX_ACCEPT_WAIT_TIME);
Matt Briggs 61:8d9efd33cac9 161 result = protocol->waitForAccept(TX_ACCEPT_WAIT_TIME);
Matt Briggs 61:8d9efd33cac9 162 if (result == cmdSuccess) {
Matt Briggs 63:e1efbe3402d9 163 myLogInfo("Got accept");
Matt Briggs 64:46c8819c07cc 164 ledPatterns.turnOff();
Matt Briggs 64:46c8819c07cc 165 wait(0.5);
Matt Briggs 64:46c8819c07cc 166 ledPatterns.tripleBlink();
Matt Briggs 61:8d9efd33cac9 167 }
Matt Briggs 61:8d9efd33cac9 168 else {
Matt Briggs 63:e1efbe3402d9 169 myLogInfo("Did not receive accept");
Matt Briggs 61:8d9efd33cac9 170 }
Matt Briggs 62:9751a8504c82 171 protocol->configForSavedNetwork();
Matt Briggs 63:e1efbe3402d9 172 protocol->printDotConfig();
Matt Briggs 64:46c8819c07cc 173 protocol->resetCounters();
Matt Briggs 61:8d9efd33cac9 174 }
Matt Briggs 61:8d9efd33cac9 175 }
Matt Briggs 61:8d9efd33cac9 176 if (protocol->isRx()) {
Matt Briggs 61:8d9efd33cac9 177 if (pairBtnState == pairBtnMediumPress) {
Matt Briggs 64:46c8819c07cc 178 // protocol->sampleDLC();
Matt Briggs 62:9751a8504c82 179 protocol->configForPairingNetwork();
Matt Briggs 64:46c8819c07cc 180 ledPatterns.turnOn();
Matt Briggs 63:e1efbe3402d9 181 myLogInfo("Waiting for pair request for %f seconds", RX_PAIR_WAIT_TIME);
Matt Briggs 61:8d9efd33cac9 182 result = protocol->waitForPairing(RX_PAIR_WAIT_TIME);
Matt Briggs 64:46c8819c07cc 183 ledPatterns.turnOff();
Matt Briggs 61:8d9efd33cac9 184 if (result == cmdSuccess) {
Matt Briggs 63:e1efbe3402d9 185 myLogInfo("Got pair request and responded");
Matt Briggs 64:46c8819c07cc 186 ledPatterns.tripleBlink();
Matt Briggs 61:8d9efd33cac9 187 }
Matt Briggs 61:8d9efd33cac9 188 else if (result == cmdTimeout) {
Matt Briggs 63:e1efbe3402d9 189 myLogInfo("Did not receive request");
Matt Briggs 61:8d9efd33cac9 190 }
Matt Briggs 61:8d9efd33cac9 191 else {
Matt Briggs 63:e1efbe3402d9 192 myLogInfo("Unknown pair error");
Matt Briggs 61:8d9efd33cac9 193 }
Matt Briggs 62:9751a8504c82 194 protocol->configForSavedNetwork();
Matt Briggs 63:e1efbe3402d9 195 protocol->printDotConfig();
Matt Briggs 64:46c8819c07cc 196 protocol->resetCounters();
Matt Briggs 61:8d9efd33cac9 197 }
Matt Briggs 61:8d9efd33cac9 198 else if (pairBtnState == pairBtnLongPress) {
Matt Briggs 63:e1efbe3402d9 199 myLogInfo("Clearing pair values and generating new ones.");
Matt Briggs 61:8d9efd33cac9 200 protocol->clearPair();
Matt Briggs 63:e1efbe3402d9 201 protocol->printDotConfig();
Matt Briggs 64:46c8819c07cc 202 ledPatterns.tenBlinks();
Matt Briggs 64:46c8819c07cc 203 protocol->resetCounters();
Matt Briggs 61:8d9efd33cac9 204 }
Matt Briggs 61:8d9efd33cac9 205 }
Matt Briggs 61:8d9efd33cac9 206 }
Matt Briggs 47:a68747642a7a 207
Matt Briggs 61:8d9efd33cac9 208 // Alert code
Matt Briggs 53:a1563574a980 209 if (protocol->isTx()) {
Matt Briggs 63:e1efbe3402d9 210 myLogInfo("Loop #%d. CCFlag %d, CCAlertState %d, TamperFlag %d, PairBtnFlag %d",
Matt Briggs 58:15aa7a785b9f 211 loopCnt, ccIntFlag, bbio->isCCInAlert(), tamperIntFlag, pairBtnIntFlag);
Matt Briggs 53:a1563574a980 212 // TODO add tamper
Matt Briggs 50:e89647e77fd5 213 if (ccIntFlag || // If contact closure in
Matt Briggs 57:bdac7dd17af2 214 bbio->isCCInAlert() || // If closure remains in effect
Matt Briggs 61:8d9efd33cac9 215 (pairBtnIntFlag && (pairBtnState == pairBtnShortPress))) {
Matt Briggs 50:e89647e77fd5 216 ccIntFlag = false;
Matt Briggs 64:46c8819c07cc 217 ledPatterns.turnOn();
Matt Briggs 50:e89647e77fd5 218
Matt Briggs 65:d546060aa03d 219 protocol->sendAlert(0xBEEF); // TODO use this field to encode the alert type e.g. CCIN vs tamper
Matt Briggs 65:d546060aa03d 220 myLogInfo("Sent msg num: %d.", protocol->getSeqNum());
Matt Briggs 64:46c8819c07cc 221 ledPatterns.turnOff();
Matt Briggs 50:e89647e77fd5 222 }
Matt Briggs 50:e89647e77fd5 223
Matt Briggs 64:46c8819c07cc 224 ledPatterns.turnOff();
Matt Briggs 62:9751a8504c82 225 pairBtnIntFlag = false;
Matt Briggs 62:9751a8504c82 226 tamperIntFlag = false;
Matt Briggs 58:15aa7a785b9f 227 bbio->prepareSleep();
Matt Briggs 57:bdac7dd17af2 228 if (bbio->isCCInAlert()) { // Still in alert mode
Matt Briggs 58:15aa7a785b9f 229 dot->sleep(2, mDot::RTC_ALARM_OR_INTERRUPT, false); // Go to sleep and check in 2 secs if CCInAlert is asserted
Matt Briggs 57:bdac7dd17af2 230 }
Matt Briggs 57:bdac7dd17af2 231 else {
Matt Briggs 57:bdac7dd17af2 232 dot->sleep(0, mDot::INTERRUPT, false); // Go to sleep until wake button
Matt Briggs 57:bdac7dd17af2 233 }
Matt Briggs 58:15aa7a785b9f 234 bbio->exitSleep();
Matt Briggs 47:a68747642a7a 235 }
Mike Fiore 11:d2e31743433a 236
Matt Briggs 53:a1563574a980 237 if (protocol->isRx()) {
Matt Briggs 47:a68747642a7a 238 bool msgPending;
Matt Briggs 53:a1563574a980 239 protocol->listen(msgPending);
Matt Briggs 63:e1efbe3402d9 240 myLogInfo("Loop Cnt %d. Listening.", loopCnt);
Matt Briggs 47:a68747642a7a 241 if (msgPending) {
Matt Briggs 65:d546060aa03d 242 std::vector<uint8_t> txEui;
Matt Briggs 65:d546060aa03d 243 txEui.reserve(8);
Matt Briggs 65:d546060aa03d 244 uint16_t data;
Matt Briggs 65:d546060aa03d 245 uint32_t msgSeqNum;
Matt Briggs 65:d546060aa03d 246 protocol->recvAlert(txEui, data, msgSeqNum);
Matt Briggs 65:d546060aa03d 247 myLogInfo("Got msg num: TODO");
Matt Briggs 53:a1563574a980 248 bbio->relayAlert();
Matt Briggs 64:46c8819c07cc 249 ledPatterns.turnOn();
Matt Briggs 58:15aa7a785b9f 250 // Hold time for alert
Matt Briggs 58:15aa7a785b9f 251 // TODO maybe use sleep instead of wait
Matt Briggs 63:e1efbe3402d9 252 myLogInfo("Holding alert for %f secs", HoldTimeSetting::rotVal2Sec(bbio->rotarySwitch1()));
Matt Briggs 58:15aa7a785b9f 253 wait(HoldTimeSetting::rotVal2Sec(bbio->rotarySwitch1()));
Matt Briggs 64:46c8819c07cc 254 ledPatterns.turnOff();
Matt Briggs 58:15aa7a785b9f 255 bbio->relayNormal();
Matt Briggs 47:a68747642a7a 256 }
Matt Briggs 63:e1efbe3402d9 257 myLogInfo("Sleeping. Time %d", us_ticker_read());
Matt Briggs 64:46c8819c07cc 258 // protocol->sampleDLC();
Matt Briggs 58:15aa7a785b9f 259 bbio->prepareSleep();
Matt Briggs 47:a68747642a7a 260 dot->sleep(2, mDot::RTC_ALARM_OR_INTERRUPT, false); // Go to sleep until wake button
Matt Briggs 58:15aa7a785b9f 261 bbio->exitSleep();
Matt Briggs 47:a68747642a7a 262 }
Matt Briggs 31:9c535a708ae9 263
Matt Briggs 64:46c8819c07cc 264 protocol->resetCounters();
Matt Briggs 63:e1efbe3402d9 265 myLogInfo("\r\n================================");
Matt Briggs 58:15aa7a785b9f 266 loopCnt++;
Mike Fiore 11:d2e31743433a 267 }
Matt Briggs 47:a68747642a7a 268
Matt Briggs 55:79ab0bbc5008 269 delete protocol;
Matt Briggs 55:79ab0bbc5008 270 delete bbio;
Mike Fiore 11:d2e31743433a 271 return 0;
Mike Fiore 11:d2e31743433a 272 }
Matt Briggs 47:a68747642a7a 273 #endif
Mike Fiore 11:d2e31743433a 274
Matt Briggs 27:6b68ff715ae1 275