Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Committer:
uci1
Date:
Fri Oct 31 23:33:31 2014 +0000
Revision:
59:21128cc24b04
Parent:
56:0bba0ef15697
Child:
67:ec999336fcd1
Enable safety nets. Allow IP setting via text files on mbed.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
uci1 0:664899e0b988 1 #include "SnConfigFrame.h"
uci1 0:664899e0b988 2
uci1 0:664899e0b988 3 #include "mbed.h"
uci1 0:664899e0b988 4
uci1 59:21128cc24b04 5 //#define DEBUG
uci1 59:21128cc24b04 6
uci1 0:664899e0b988 7 #include "SnBitUtils.h"
uci1 16:744ce85aede2 8 #include "SnHeaderFrame.h"
uci1 22:f957c4f840ad 9 #include "Watchdog.h"
uci1 0:664899e0b988 10
uci1 0:664899e0b988 11 extern "C" void mbed_mac_address(char *);
uci1 0:664899e0b988 12
uci1 31:b5bd3b189150 13 bool SnConfigFrame::fgApplySafetyNets = true;
uci1 56:0bba0ef15697 14 #if CHIPBOARD==ATWD4CH
uci1 40:1324da35afd4 15 const uint8_t SnConfigFrame::kIOVers = 8;
uci1 56:0bba0ef15697 16 #else // SST
uci1 56:0bba0ef15697 17 const uint8_t SnConfigFrame::kIOVers = 10;
uci1 56:0bba0ef15697 18 #endif
uci1 3:24c5f0f50bf1 19 const char* const SnConfigFrame::kDefConfFile = "/local/DEFCONF.DAT";
uci1 59:21128cc24b04 20 const char* const SnConfigFrame::kDefRemIpFilen = "/local/REMOTIP.TXT";
uci1 59:21128cc24b04 21 const char* const SnConfigFrame::kDefRemPortFilen = "/local/REMOTPRT.TXT";
uci1 59:21128cc24b04 22 const char* const SnConfigFrame::kDefMbedIPFilen = "/local/MBEDIP.TXT";
uci1 59:21128cc24b04 23 const char* const SnConfigFrame::kDefMbedMaskFilen = "/local/MBEDMASK.TXT";
uci1 59:21128cc24b04 24 const char* const SnConfigFrame::kDefMbedGateFilen = "/local/MBEDGATE.TXT";
uci1 28:484943132bb0 25 const char* const SnConfigFrame::kDefIPflag = "DEF";
uci1 1:e392595b4b76 26 const uint32_t SnConfigFrame::kMinCommWinPrdLowPwr = 14400; // exclusive min low power comm win period (s)
uci1 1:e392595b4b76 27 const uint32_t SnConfigFrame::kMaxCommWinPrdLowPwr = 259200; // exclusive max low power comm win period (s)
uci1 1:e392595b4b76 28 const uint32_t SnConfigFrame::kMinCommWinDurLowPwr = 300; // exclusive min low power comm win duration (s)
uci1 1:e392595b4b76 29 const uint32_t SnConfigFrame::kMaxCommWinDurLowPwr = 3600; // exclusive max low power comm win duration (s)
uci1 1:e392595b4b76 30 const uint8_t SnConfigFrame::kConfLblLen;
uci1 0:664899e0b988 31
uci1 0:664899e0b988 32 uint64_t SnConfigFrame::fgMacAdr = 0;
uci1 0:664899e0b988 33
uci1 0:664899e0b988 34 void SnConfigFrame::SetMacAddress() {
uci1 0:664899e0b988 35 static const uint8_t b64 = sizeof(uint64_t);
uci1 0:664899e0b988 36 static char c[b64];
uci1 0:664899e0b988 37 mbed_mac_address(&(c[0]));
uci1 0:664899e0b988 38 // like a big endian union
uci1 0:664899e0b988 39 fgMacAdr = 0;
uci1 0:664899e0b988 40 const char* a = c+(b64-1);
uci1 0:664899e0b988 41 for (uint8_t i=0; i<b64; i++, a--) {
uci1 0:664899e0b988 42 fgMacAdr |= static_cast<uint64_t>(*a) << (i<<3);
uci1 0:664899e0b988 43 }
uci1 0:664899e0b988 44 }
uci1 0:664899e0b988 45
uci1 0:664899e0b988 46 void SnConfigFrame::SetHardDefaults() {
uci1 0:664899e0b988 47 sprintf(fLabel,"HardDefaults");
uci1 0:664899e0b988 48 fConfTime = 1338854400u; // Tue, 05 Jun 2012 00:00:00 GMT
uci1 0:664899e0b988 49 fRun = 0;
uci1 40:1324da35afd4 50 fFirstSeq = 0;
uci1 54:ea1234a44fe8 51 fEvtsPerSeq = 300;
uci1 8:95a325df1f6b 52 fRunMode = 0;
uci1 56:0bba0ef15697 53 #if CHIPBOARD==ATWD4CH
uci1 1:e392595b4b76 54 fStreamHiLoPlas = 0;
uci1 56:0bba0ef15697 55 #endif
uci1 0:664899e0b988 56 fWvLoseLSB = 0;
uci1 1:e392595b4b76 57 fWvLoseMSB = 4;
uci1 0:664899e0b988 58 fWvBaseline = 0;
uci1 0:664899e0b988 59 fDatPackType = ~0;
uci1 0:664899e0b988 60 uint16_t* dc = &(fDAC[0][0]);
uci1 0:664899e0b988 61 for (uint16_t i=0; i<kTotDacs; i++, dc++) {
uci1 0:664899e0b988 62 *dc = 3072u;
uci1 0:664899e0b988 63 }
uci1 56:0bba0ef15697 64 #if CHIPBOARD==ATWD4CH
uci1 0:664899e0b988 65 fNumPlas = 1;
uci1 0:664899e0b988 66 uint16_t* pl = &(fPLA[0]);
uci1 0:664899e0b988 67 for (uint8_t j=0; j<kNplas; j++, pl++) {
uci1 0:664899e0b988 68 *pl = 0x7FFFu;
uci1 0:664899e0b988 69 }
uci1 56:0bba0ef15697 70 #endif
uci1 28:484943132bb0 71 fNumCardsMajLog = 2;
uci1 54:ea1234a44fe8 72 fEnableThermTrig = 0;
uci1 1:e392595b4b76 73 fForceTrigPeriod = 67u;
uci1 0:664899e0b988 74 fHeartBeatPeriod = 0;
uci1 0:664899e0b988 75 fAmpsOn = 0x0Fu;
uci1 0:664899e0b988 76 fEvtThrtlPeriodMs = 50u;
uci1 28:484943132bb0 77 fPowerMode = kAmpsDatTak|kCardDatTak|kAmpsComWin|kCardComWin|kIridComWin|kAfarComWin;
uci1 39:2f17131d22a5 78 fBatVoltToLowPwr = 0;
uci1 39:2f17131d22a5 79 fBatVoltFromLowPwr = 0;
uci1 28:484943132bb0 80 fVoltCheckPeriod = 600u;
uci1 54:ea1234a44fe8 81 fCommWinPeriod = 600u;
uci1 1:e392595b4b76 82 fCommWinDuration = 600u;
uci1 28:484943132bb0 83 fCommSendData = static_cast<int16_t>(kUseBits);
uci1 0:664899e0b988 84 fCommWinPrdLowPwr = 86100u;
uci1 0:664899e0b988 85 fCommWinDurLowPwr = 300u;
uci1 40:1324da35afd4 86 fCommWinConnectTOMin = 3u;
uci1 40:1324da35afd4 87 fCommWinListenTOMin = 3u;
uci1 22:f957c4f840ad 88 fWatchDogPeriod = WDFAILSAFE;
uci1 56:0bba0ef15697 89 fTempCheckPeriod = 0;
uci1 28:484943132bb0 90 SetDefaultIPs();
uci1 8:95a325df1f6b 91 fIsLowPower = false;
uci1 8:95a325df1f6b 92 memcpy(fNormLabel, fLabel, kConfLblLen);
uci1 8:95a325df1f6b 93 fNormPowerMode = fPowerMode;
uci1 8:95a325df1f6b 94
uci1 54:ea1234a44fe8 95 ApplyConnectListenSafetyNets();
uci1 8:95a325df1f6b 96 }
uci1 8:95a325df1f6b 97
uci1 28:484943132bb0 98 void SnConfigFrame::SetDefaultRemoteServ() {
uci1 59:21128cc24b04 99 if (false==ReadDefaultRemoteServer()) {
uci1 59:21128cc24b04 100 sprintf(fRemoteServer,"%s","128.195.204.151");
uci1 59:21128cc24b04 101 }
uci1 28:484943132bb0 102 }
uci1 28:484943132bb0 103
uci1 28:484943132bb0 104 void SnConfigFrame::SetDefaultRemotePort() {
uci1 59:21128cc24b04 105 if (false==ReadDefaultRemotePort()) {
uci1 59:21128cc24b04 106 fRemotePort = 6655;
uci1 59:21128cc24b04 107 }
uci1 28:484943132bb0 108 }
uci1 28:484943132bb0 109
uci1 28:484943132bb0 110 void SnConfigFrame::SetDefaultMbedIP() {
uci1 59:21128cc24b04 111 // see ReadDefaultIPFile
uci1 59:21128cc24b04 112 //
uci1 59:21128cc24b04 113 // these hardcoded defaults are only here
uci1 59:21128cc24b04 114 // "in case". they make it difficult/impossible
uci1 59:21128cc24b04 115 // to swap boards in the field without internet
uci1 59:21128cc24b04 116 // access to change the mbed program
uci1 28:484943132bb0 117 const uint64_t ip = GetMacAddress();
uci1 59:21128cc24b04 118 if (false==ReadDefaultMbedIP()) {
uci1 59:21128cc24b04 119 switch (ip) {
uci1 59:21128cc24b04 120 case 0x0002F7F2244B0000: // stn 13, board 102
uci1 59:21128cc24b04 121 sprintf(fMbedIP,"%s","157.132.94.31");
uci1 59:21128cc24b04 122 break;
uci1 59:21128cc24b04 123 case 0x0002F7F20A9C0000: // stn 14, board 104
uci1 59:21128cc24b04 124 sprintf(fMbedIP,"%s","157.132.94.33");
uci1 59:21128cc24b04 125 break;
uci1 59:21128cc24b04 126 case 0x0002F7F0C3B60000: // station 3
uci1 59:21128cc24b04 127 case 0x0002F7F224440000: // board 105
uci1 59:21128cc24b04 128 sprintf(fMbedIP,"%s","157.132.94.35");
uci1 59:21128cc24b04 129 break;
uci1 59:21128cc24b04 130 case 0x0002F7F0C41C0000: // station 4
uci1 59:21128cc24b04 131 sprintf(fMbedIP,"%s","157.132.94.37");
uci1 59:21128cc24b04 132 break;
uci1 59:21128cc24b04 133 case 0x0002F7F0C61A0000: // station 10
uci1 59:21128cc24b04 134 case 0x0002F7F21A8A0000: // board 111
uci1 59:21128cc24b04 135 sprintf(fMbedIP,"%s","157.132.94.39");
uci1 59:21128cc24b04 136 break;
uci1 59:21128cc24b04 137 case 0x0002F7F0C4450000: // station 6
uci1 59:21128cc24b04 138 case 0x0002F7F1E9ED0000: // board 108
uci1 59:21128cc24b04 139 sprintf(fMbedIP,"%s","157.132.94.41");
uci1 59:21128cc24b04 140 break;
uci1 59:21128cc24b04 141 case 0x0002F7F175B70000: // station 11
uci1 59:21128cc24b04 142 case 0x0002F7F1F2120000: // board 113
uci1 59:21128cc24b04 143 sprintf(fMbedIP,"%s","157.132.94.43");
uci1 59:21128cc24b04 144 break;
uci1 59:21128cc24b04 145 default: // what??
uci1 59:21128cc24b04 146 sprintf(fMbedIP,"%s","157.132.94.45"); // usually for tent
uci1 59:21128cc24b04 147 break;
uci1 59:21128cc24b04 148 };
uci1 59:21128cc24b04 149 }
uci1 28:484943132bb0 150 }
uci1 28:484943132bb0 151 // brian dornick 3107
uci1 28:484943132bb0 152 void SnConfigFrame::SetDefaultMaskIP() {
uci1 59:21128cc24b04 153 if (false==ReadDefaultMbedMask()) {
uci1 59:21128cc24b04 154 sprintf(fMbedMask,"%s","255.255.255.0");
uci1 59:21128cc24b04 155 }
uci1 28:484943132bb0 156 }
uci1 28:484943132bb0 157
uci1 28:484943132bb0 158 void SnConfigFrame::SetDefaultGateIP() {
uci1 59:21128cc24b04 159 if (false==ReadDefaultMbedGate()) {
uci1 59:21128cc24b04 160 sprintf(fMbedGate,"%s","157.132.94.1");
uci1 59:21128cc24b04 161 }
uci1 59:21128cc24b04 162 }
uci1 59:21128cc24b04 163
uci1 59:21128cc24b04 164 bool SnConfigFrame::ReadOneIPFrom(const char* ipfname,
uci1 59:21128cc24b04 165 char* ipstr) {
uci1 59:21128cc24b04 166 bool ok = false;
uci1 59:21128cc24b04 167 FILE* ipf = fopen(ipfname, "r");
uci1 59:21128cc24b04 168 if (ipf!=0) {
uci1 59:21128cc24b04 169 uint8_t ipbytes[4] = {0,0,0,0};
uci1 59:21128cc24b04 170 const int nr = fscanf(ipf,"%hhu.%hhu.%hhu.%hhu\n",
uci1 59:21128cc24b04 171 &(ipbytes[0]), &(ipbytes[1]),
uci1 59:21128cc24b04 172 &(ipbytes[2]), &(ipbytes[3]));
uci1 59:21128cc24b04 173 if (4==nr) {
uci1 59:21128cc24b04 174 const int nc =
uci1 59:21128cc24b04 175 sprintf(ipstr,"%hhu.%hhu.%hhu.%hhu",
uci1 59:21128cc24b04 176 ipbytes[0], ipbytes[1],
uci1 59:21128cc24b04 177 ipbytes[2], ipbytes[3]);
uci1 59:21128cc24b04 178 ok = nc>0;
uci1 59:21128cc24b04 179 }
uci1 59:21128cc24b04 180 }
uci1 59:21128cc24b04 181 fclose(ipf);
uci1 59:21128cc24b04 182 #ifdef DEBUG
uci1 59:21128cc24b04 183 printf("ReadOneIPInto : ipstr=[%s], ok=%d\r\n",
uci1 59:21128cc24b04 184 ipstr, (int)ok);
uci1 59:21128cc24b04 185 #endif
uci1 59:21128cc24b04 186 return ok;
uci1 59:21128cc24b04 187 }
uci1 59:21128cc24b04 188
uci1 59:21128cc24b04 189 bool SnConfigFrame::ReadDefaultRemoteServer() {
uci1 59:21128cc24b04 190 const bool ok = ReadOneIPFrom(kDefRemIpFilen, fRemoteServer);
uci1 59:21128cc24b04 191 #ifdef DEBUG
uci1 59:21128cc24b04 192 printf("remote = %s\r\n", fRemoteServer);
uci1 59:21128cc24b04 193 #endif
uci1 59:21128cc24b04 194 return ok;
uci1 59:21128cc24b04 195 }
uci1 59:21128cc24b04 196
uci1 59:21128cc24b04 197 bool SnConfigFrame::ReadDefaultRemotePort() {
uci1 59:21128cc24b04 198 bool ok = false;
uci1 59:21128cc24b04 199 FILE* pf = fopen(kDefRemPortFilen, "r");
uci1 59:21128cc24b04 200 if (pf!=0) {
uci1 59:21128cc24b04 201 ok = (1==fscanf(pf, "%hu\n", &fRemotePort));
uci1 59:21128cc24b04 202 }
uci1 59:21128cc24b04 203 fclose(pf);
uci1 59:21128cc24b04 204 #ifdef DEBUG
uci1 59:21128cc24b04 205 printf("port = %hu\r\n", fRemotePort);
uci1 59:21128cc24b04 206 #endif
uci1 59:21128cc24b04 207 return ok;
uci1 59:21128cc24b04 208 }
uci1 59:21128cc24b04 209
uci1 59:21128cc24b04 210 bool SnConfigFrame::ReadDefaultMbedIP() {
uci1 59:21128cc24b04 211 const bool ok = ReadOneIPFrom(kDefMbedIPFilen, fMbedIP);
uci1 59:21128cc24b04 212 #ifdef DEBUG
uci1 59:21128cc24b04 213 printf("mbed = %s\r\n", fMbedIP);
uci1 59:21128cc24b04 214 #endif
uci1 59:21128cc24b04 215 return ok;
uci1 59:21128cc24b04 216 }
uci1 59:21128cc24b04 217
uci1 59:21128cc24b04 218 bool SnConfigFrame::ReadDefaultMbedMask() {
uci1 59:21128cc24b04 219 const bool ok = ReadOneIPFrom(kDefMbedMaskFilen, fMbedMask);
uci1 59:21128cc24b04 220 #ifdef DEBUG
uci1 59:21128cc24b04 221 printf("mask = %s\r\n", fMbedMask);
uci1 59:21128cc24b04 222 #endif
uci1 59:21128cc24b04 223 return ok;
uci1 59:21128cc24b04 224 }
uci1 59:21128cc24b04 225
uci1 59:21128cc24b04 226 bool SnConfigFrame::ReadDefaultMbedGate() {
uci1 59:21128cc24b04 227 const bool ok = ReadOneIPFrom(kDefMbedGateFilen, fMbedGate);
uci1 59:21128cc24b04 228 #ifdef DEBUG
uci1 59:21128cc24b04 229 printf("gate = %s\r\n", fMbedGate);
uci1 59:21128cc24b04 230 #endif
uci1 59:21128cc24b04 231 return ok;
uci1 59:21128cc24b04 232 }
uci1 59:21128cc24b04 233
uci1 59:21128cc24b04 234 bool SnConfigFrame::ReadDefaultIPFiles() {
uci1 59:21128cc24b04 235 bool ok = ReadDefaultRemoteServer();
uci1 59:21128cc24b04 236 ok &= ReadDefaultRemotePort();
uci1 59:21128cc24b04 237 ok &= ReadDefaultMbedIP();
uci1 59:21128cc24b04 238 ok &= ReadDefaultMbedMask();
uci1 59:21128cc24b04 239 ok &= ReadDefaultMbedGate();
uci1 59:21128cc24b04 240 return ok;
uci1 28:484943132bb0 241 }
uci1 28:484943132bb0 242
uci1 28:484943132bb0 243 void SnConfigFrame::SetDefaultIPs() {
uci1 59:21128cc24b04 244 GetMacAddress(); // just to make sure it gets read
uci1 59:21128cc24b04 245 if (false==ReadDefaultIPFiles()) {
uci1 59:21128cc24b04 246 SetDefaultRemoteServ();
uci1 59:21128cc24b04 247 SetDefaultRemotePort();
uci1 59:21128cc24b04 248 SetDefaultMbedIP();
uci1 59:21128cc24b04 249 SetDefaultMaskIP();
uci1 59:21128cc24b04 250 SetDefaultGateIP();
uci1 59:21128cc24b04 251 }
uci1 28:484943132bb0 252 }
uci1 28:484943132bb0 253
uci1 40:1324da35afd4 254 void SnConfigFrame::ApplyConnectListenSafetyNets() {
uci1 40:1324da35afd4 255 if (fCommWinConnectTOMin<kDefTimeoutMin) {
uci1 40:1324da35afd4 256 fCommWinConnectTOMin=kDefTimeoutMin;
uci1 40:1324da35afd4 257 }
uci1 56:0bba0ef15697 258 if (fCommWinConnectTOMin>(fCommWinDuration/(60.*static_cast<float>(kNcomms)))) {
uci1 40:1324da35afd4 259 fCommWinConnectTOMin=kDefTimeoutMin;
uci1 40:1324da35afd4 260 }
uci1 40:1324da35afd4 261 if (fCommWinListenTOMin<kDefTimeoutMin) {
uci1 40:1324da35afd4 262 fCommWinListenTOMin=kDefTimeoutMin;
uci1 40:1324da35afd4 263 }
uci1 56:0bba0ef15697 264 if (fCommWinListenTOMin>(fCommWinDuration/(60.*static_cast<float>(kNcomms)))) {
uci1 40:1324da35afd4 265 fCommWinListenTOMin=kDefTimeoutMin;
uci1 40:1324da35afd4 266 }
uci1 40:1324da35afd4 267 }
uci1 40:1324da35afd4 268
uci1 28:484943132bb0 269 void SnConfigFrame::ApplySafetyNets() {
uci1 40:1324da35afd4 270 if (fFirstSeq>kMaxFirstSeq) {
uci1 40:1324da35afd4 271 fFirstSeq=kMaxFirstSeq;
uci1 40:1324da35afd4 272 }
uci1 56:0bba0ef15697 273 #if CHIPBOARD==ATWD4CH
uci1 28:484943132bb0 274 if (fNumPlas>kNplas) {
uci1 28:484943132bb0 275 fNumPlas=kNplas;
uci1 28:484943132bb0 276 }
uci1 56:0bba0ef15697 277 #endif
uci1 28:484943132bb0 278 if (fNumCardsMajLog>kNchans) {
uci1 28:484943132bb0 279 fNumCardsMajLog=kNchans;
uci1 28:484943132bb0 280 }
uci1 28:484943132bb0 281 if (fNumCardsMajLog<1u) {
uci1 28:484943132bb0 282 fNumCardsMajLog=1u;
uci1 28:484943132bb0 283 }
uci1 28:484943132bb0 284 if ( (fForceTrigPeriod>0) &&
uci1 28:484943132bb0 285 (fForceTrigPeriod<kMinForcePer) ) {
uci1 28:484943132bb0 286 fForceTrigPeriod = kMinForcePer;
uci1 28:484943132bb0 287 }
uci1 28:484943132bb0 288 if (fEvtThrtlPeriodMs>kMaxThrottlePerMs) {
uci1 28:484943132bb0 289 fEvtThrtlPeriodMs=kMaxThrottlePerMs;
uci1 28:484943132bb0 290 }
uci1 28:484943132bb0 291 if ( (IsPoweredFor(kIridComWin)==false) &&
uci1 28:484943132bb0 292 (IsPoweredFor(kAfarComWin)==false) ) {
uci1 28:484943132bb0 293 EnablePowerFor(kIridComWin);
uci1 28:484943132bb0 294 EnablePowerFor(kAfarComWin);
uci1 28:484943132bb0 295 }
uci1 39:2f17131d22a5 296 if (fBatVoltToLowPwr>kMaxBatVoltLowPwr) {
uci1 39:2f17131d22a5 297 fBatVoltToLowPwr=kMaxBatVoltLowPwr;
uci1 39:2f17131d22a5 298 }
uci1 39:2f17131d22a5 299 if (fBatVoltFromLowPwr>kMaxBatVoltLowPwr) {
uci1 39:2f17131d22a5 300 fBatVoltFromLowPwr=kMaxBatVoltLowPwr;
uci1 39:2f17131d22a5 301 }
uci1 39:2f17131d22a5 302 if (fBatVoltFromLowPwr<fBatVoltToLowPwr) {
uci1 39:2f17131d22a5 303 fBatVoltFromLowPwr=fBatVoltToLowPwr;
uci1 28:484943132bb0 304 }
uci1 28:484943132bb0 305 if (fCommWinPeriod>kMaxCommWinPeriod) {
uci1 28:484943132bb0 306 fCommWinPeriod=kMaxCommWinPeriod;
uci1 28:484943132bb0 307 }
uci1 40:1324da35afd4 308 if (fCommWinPeriod<kMinCommWinPeriod) {
uci1 40:1324da35afd4 309 fCommWinPeriod=kMinCommWinPeriod;
uci1 40:1324da35afd4 310 }
uci1 28:484943132bb0 311 if (fCommWinDuration<kMinCommWinDur) {
uci1 28:484943132bb0 312 fCommWinDuration=kMinCommWinDur;
uci1 28:484943132bb0 313 }
uci1 28:484943132bb0 314 if (fCommWinPrdLowPwr>kMaxCommWinPeriod) {
uci1 28:484943132bb0 315 fCommWinPrdLowPwr=kMaxCommWinPeriod;
uci1 28:484943132bb0 316 }
uci1 28:484943132bb0 317 if (fCommWinDurLowPwr<kMinCommWinDur) {
uci1 28:484943132bb0 318 fCommWinDurLowPwr=kMinCommWinDur;
uci1 28:484943132bb0 319 }
uci1 40:1324da35afd4 320 ApplyConnectListenSafetyNets();
uci1 28:484943132bb0 321 if (fWatchDogPeriod>kMaxWatchDogPer) {
uci1 28:484943132bb0 322 fWatchDogPeriod=kMaxWatchDogPer;
uci1 28:484943132bb0 323 }
uci1 28:484943132bb0 324 if (fWatchDogPeriod<kMinWatchDogPer) {
uci1 28:484943132bb0 325 fWatchDogPeriod=kMinWatchDogPer;
uci1 28:484943132bb0 326 }
uci1 28:484943132bb0 327 }
uci1 28:484943132bb0 328
uci1 21:ce51bb0ba4a5 329 uint32_t SnConfigFrame::GetTimeoutTime(const uint32_t startTime,
uci1 21:ce51bb0ba4a5 330 const uint32_t delta) const {
uci1 40:1324da35afd4 331 // --.-----lst------.--delta--.--
uci1 40:1324da35afd4 332 // . . .
uci1 40:1324da35afd4 333 // start current returnVal
uci1 40:1324da35afd4 334 //
uci1 40:1324da35afd4 335 // lio=lst+delta bound by comm wind dur
uci1 40:1324da35afd4 336 // returns start + lio
uci1 40:1324da35afd4 337
uci1 21:ce51bb0ba4a5 338 const uint32_t ct = time(0);
uci1 40:1324da35afd4 339 uint32_t lst = ct-startTime;
uci1 21:ce51bb0ba4a5 340 if ( (ct<startTime) || (ct==0) ||
uci1 21:ce51bb0ba4a5 341 (lst>kSecsPerDay) ) {
uci1 21:ce51bb0ba4a5 342 // possible clock problems
uci1 40:1324da35afd4 343 lst = static_cast<uint32_t>(kDefTimeoutMin)*60u;
uci1 21:ce51bb0ba4a5 344 }
uci1 21:ce51bb0ba4a5 345 const uint32_t lio =
uci1 21:ce51bb0ba4a5 346 ((lst+delta) < GetCommWinDuration()) ?
uci1 21:ce51bb0ba4a5 347 lst+delta : GetCommWinDuration();
uci1 21:ce51bb0ba4a5 348 return lio+startTime;
uci1 21:ce51bb0ba4a5 349 }
uci1 21:ce51bb0ba4a5 350
uci1 8:95a325df1f6b 351 void SnConfigFrame::ChangeToLowPower() {
uci1 8:95a325df1f6b 352
uci1 8:95a325df1f6b 353 // save old label
uci1 8:95a325df1f6b 354 memcpy(fNormLabel, fLabel, kConfLblLen);
uci1 8:95a325df1f6b 355
uci1 8:95a325df1f6b 356 // append label
uci1 8:95a325df1f6b 357 // this will allow the new config to be put in the DB
uci1 8:95a325df1f6b 358 int slen = strlen(fLabel);
uci1 8:95a325df1f6b 359 static const char* tag = "_LOWPOW";
uci1 8:95a325df1f6b 360 const int ml = strlen(tag)+1;
uci1 8:95a325df1f6b 361 if (slen > (kConfLblLen-ml) ) {
uci1 8:95a325df1f6b 362 memset(fLabel+kConfLblLen-ml, '\0', ml);
uci1 8:95a325df1f6b 363 }
uci1 8:95a325df1f6b 364 strncat(fLabel, tag, ml-1);
uci1 8:95a325df1f6b 365
uci1 8:95a325df1f6b 366 // save power settings
uci1 8:95a325df1f6b 367 fNormPowerMode = fPowerMode;
uci1 8:95a325df1f6b 368
uci1 8:95a325df1f6b 369 // change power settings
uci1 8:95a325df1f6b 370 DisablePowerFor(kAmpsDatTak);
uci1 8:95a325df1f6b 371 DisablePowerFor(kCardDatTak);
uci1 8:95a325df1f6b 372 DisablePowerFor(kIridDatTak);
uci1 8:95a325df1f6b 373 DisablePowerFor(kAfarDatTak);
uci1 8:95a325df1f6b 374 DisablePowerFor(kAmpsComWin);
uci1 8:95a325df1f6b 375 DisablePowerFor(kCardComWin);
uci1 56:0bba0ef15697 376 if ( IsSBDonlyLowPwrMode() ) {
uci1 56:0bba0ef15697 377 EnablePowerFor(kIridComWin);
uci1 56:0bba0ef15697 378 DisablePowerFor(kAfarComWin);
uci1 56:0bba0ef15697 379 } else if ( (IsPoweredFor(kIridComWin)==false) &&
uci1 56:0bba0ef15697 380 (IsPoweredFor(kAfarComWin)==false) ) {
uci1 8:95a325df1f6b 381 // TODO: turn on only iridum maybe?
uci1 8:95a325df1f6b 382 EnablePowerFor(kIridComWin);
uci1 8:95a325df1f6b 383 EnablePowerFor(kAfarComWin);
uci1 56:0bba0ef15697 384 } // else same as normal for Irid and Afar Com Win
uci1 56:0bba0ef15697 385
uci1 8:95a325df1f6b 386 // set mode to low power
uci1 8:95a325df1f6b 387 fIsLowPower = true;
uci1 8:95a325df1f6b 388 }
uci1 8:95a325df1f6b 389
uci1 8:95a325df1f6b 390 void SnConfigFrame::ChangeToNormPower() {
uci1 8:95a325df1f6b 391 // put label back
uci1 8:95a325df1f6b 392 memcpy(fLabel, fNormLabel, kConfLblLen);
uci1 8:95a325df1f6b 393 // put power settings back
uci1 8:95a325df1f6b 394 fPowerMode = fNormPowerMode;
uci1 8:95a325df1f6b 395 // set mode to normal
uci1 8:95a325df1f6b 396 fIsLowPower = false;
uci1 0:664899e0b988 397 }
uci1 0:664899e0b988 398
uci1 0:664899e0b988 399 void SnConfigFrame::GetPackParsFor(const EDatPackBit d,
uci1 0:664899e0b988 400 uint8_t& loseLSB, uint8_t& loseMSB,
uci1 0:664899e0b988 401 uint16_t& wvBase) const {
uci1 0:664899e0b988 402 const bool pack = IsDatPackedFor(d);
uci1 0:664899e0b988 403 loseLSB = pack ? GetWvLoseLSB() : 0u;
uci1 0:664899e0b988 404 loseMSB = pack ? GetWvLoseMSB() : 0u;
uci1 0:664899e0b988 405 wvBase = pack ? GetWvBaseline() : 0u;
uci1 0:664899e0b988 406 }
uci1 0:664899e0b988 407
uci1 0:664899e0b988 408 void SnConfigFrame::GetHiLoPlas(const uint16_t pla,
uci1 0:664899e0b988 409 uint16_t& hiPla,
uci1 0:664899e0b988 410 uint16_t& loPla,
uci1 0:664899e0b988 411 const bool r2l) {
uci1 0:664899e0b988 412 // split the PLA bitword into 2: one for the high threshold
uci1 0:664899e0b988 413 // and one for the low threshold. "lows" in the string will become
uci1 0:664899e0b988 414 // "highs" in the low threshold PLA.
uci1 0:664899e0b988 415 //
uci1 0:664899e0b988 416 // example 1)
uci1 0:664899e0b988 417 // PLA string = HLHL....
uci1 0:664899e0b988 418 // hi thresh = H.H.....
uci1 0:664899e0b988 419 // lo thresh = .H.H....
uci1 0:664899e0b988 420 //
uci1 0:664899e0b988 421 // example 2)
uci1 0:664899e0b988 422 // PLA string = HBL.....
uci1 0:664899e0b988 423 // hi thresh = HL......
uci1 0:664899e0b988 424 // lo thresh = .LH.....
uci1 0:664899e0b988 425 //
uci1 0:664899e0b988 426 // (with . = A here, to make the example more readable)
uci1 0:664899e0b988 427 //
uci1 0:664899e0b988 428 // A = 11, B = 00
uci1 0:664899e0b988 429 // H = 01 or 10, alternating
uci1 0:664899e0b988 430 // L = 10 or 01, alternating
uci1 0:664899e0b988 431 // 01 at leftmost bits is H
uci1 0:664899e0b988 432 // for example:
uci1 0:664899e0b988 433 // 0x7FFF = 01 11 11 11 11 11 11 11
uci1 0:664899e0b988 434 // => HAAAAAAA for LEFT TO RIGHT
uci1 0:664899e0b988 435 // => AAAAAAAH for RIGHT TO LEFT
uci1 0:664899e0b988 436 // 0x56FF = 01 01 01 10 11 11 11 11
uci1 0:664899e0b988 437 // => HLHHAAAA for LEFT TO RIGHT
uci1 0:664899e0b988 438 // => AAAAHHLH for RIGHT TO LEFT
uci1 0:664899e0b988 439 //
uci1 0:664899e0b988 440 // so HHHHHHHH is
uci1 0:664899e0b988 441 // 01 10 01 10 01 10 01 10 always (r2l or l2r)
uci1 0:664899e0b988 442 //
uci1 0:664899e0b988 443 // r2l = whether to read bits right to left (true) or not (false)
uci1 0:664899e0b988 444 // Mahshid liked right to left
uci1 0:664899e0b988 445 // Liang liked left to right
uci1 0:664899e0b988 446 // so we allow for either
uci1 0:664899e0b988 447
uci1 0:664899e0b988 448 const int8_t start = (r2l) ? 0 : BITS_IN_SHORT-2;
uci1 0:664899e0b988 449 const int8_t end = (r2l) ? BITS_IN_SHORT : -2;
uci1 39:2f17131d22a5 450 const int8_t step = (r2l) ? 2 : -2;
uci1 0:664899e0b988 451
uci1 0:664899e0b988 452 uint8_t hi= (r2l) ? 0x2 : 0x1;
uci1 0:664899e0b988 453 uint8_t lo= (r2l) ? 0x1 : 0x2;
uci1 0:664899e0b988 454
uci1 0:664899e0b988 455 // set all bits to 0
uci1 0:664899e0b988 456 hiPla = 0;
uci1 0:664899e0b988 457 loPla = 0;
uci1 0:664899e0b988 458
uci1 0:664899e0b988 459 for (int8_t i=start; i!=end; i+=step, hi^=0x3, lo^=0x3) {
uci1 0:664899e0b988 460 const uint8_t b = (pla & (0x3<<i)) >> i;
uci1 0:664899e0b988 461 if (b==hi) {
uci1 0:664899e0b988 462 hiPla |= hi << i;
uci1 0:664899e0b988 463 loPla |= 0x3 << i;
uci1 0:664899e0b988 464 } else if (b==lo) {
uci1 0:664899e0b988 465 hiPla |= 0x3 << i;
uci1 0:664899e0b988 466 loPla |= hi << i;
uci1 0:664899e0b988 467 } else if (b==0x3) {
uci1 0:664899e0b988 468 // any
uci1 0:664899e0b988 469 hiPla |= 0x3 << i;
uci1 0:664899e0b988 470 loPla |= 0x3 << i;
uci1 0:664899e0b988 471 } else {
uci1 0:664899e0b988 472 // no check that b is something else.. should be impossible.
uci1 0:664899e0b988 473 // between
uci1 0:664899e0b988 474 hiPla |= lo << i;
uci1 0:664899e0b988 475 loPla |= lo << i;
uci1 0:664899e0b988 476 }
uci1 0:664899e0b988 477 }
uci1 0:664899e0b988 478
uci1 0:664899e0b988 479 }
uci1 0:664899e0b988 480
uci1 0:664899e0b988 481 bool SnConfigFrame::ReadFromFile(const char* cfile) {
uci1 0:664899e0b988 482 // intended only for reading default config file
uci1 0:664899e0b988 483
uci1 3:24c5f0f50bf1 484 /*
uci1 3:24c5f0f50bf1 485 DIR* d;
uci1 3:24c5f0f50bf1 486 struct dirent* dent;
uci1 3:24c5f0f50bf1 487 printf("files in /local:\r\n");
uci1 3:24c5f0f50bf1 488 if ( (d = opendir( "/local" ))!=NULL ) {
uci1 3:24c5f0f50bf1 489 while ( (dent = readdir(d))!=NULL ) {
uci1 3:24c5f0f50bf1 490 printf("%s\r\n",dent->d_name);
uci1 3:24c5f0f50bf1 491 }
uci1 3:24c5f0f50bf1 492 closedir(d);
uci1 3:24c5f0f50bf1 493 }
uci1 3:24c5f0f50bf1 494 */
uci1 56:0bba0ef15697 495 #ifdef DEBUG
uci1 56:0bba0ef15697 496 printf("trying to read config [%s]\r\n",cfile);
uci1 56:0bba0ef15697 497 #endif
uci1 0:664899e0b988 498 bool ret = false;
uci1 0:664899e0b988 499 FILE* cf = fopen(cfile,"rb");
uci1 0:664899e0b988 500 if (cf!=0) {
uci1 56:0bba0ef15697 501 #ifdef DEBUG
uci1 56:0bba0ef15697 502 printf("opened file\r\n");
uci1 56:0bba0ef15697 503 #endif
uci1 16:744ce85aede2 504 // check the header and file size to be
uci1 16:744ce85aede2 505 // protect a bit against corruption
uci1 16:744ce85aede2 506 uint8_t hc; uint32_t hl;
uci1 16:744ce85aede2 507 SnHeaderFrame::ReadFrom(cf, hc, hl);
uci1 56:0bba0ef15697 508 #ifdef DEBUG
uci1 56:0bba0ef15697 509 printf("hc=%hhu, hl=%u\r\n",hc,hl);
uci1 56:0bba0ef15697 510 #endif
uci1 16:744ce85aede2 511 if (hc==SnHeaderFrame::kConfigCode) {
uci1 16:744ce85aede2 512 const int fpos = ftell(cf);
uci1 16:744ce85aede2 513 // how many bytes?
uci1 16:744ce85aede2 514 fseek(cf, 0, SEEK_END); // go to end
uci1 16:744ce85aede2 515 const int fend = ftell(cf);
uci1 16:744ce85aede2 516 fseek(cf, fpos, SEEK_SET); // go back
uci1 56:0bba0ef15697 517
uci1 56:0bba0ef15697 518 #ifdef DEBUG
uci1 56:0bba0ef15697 519 printf("fend-fpos=%d-%d=%d\r\n",fend,fpos,fend-fpos);
uci1 56:0bba0ef15697 520 #endif
uci1 16:744ce85aede2 521 if (hl == fend-fpos) {
uci1 16:744ce85aede2 522 ReadFrom(cf);
uci1 16:744ce85aede2 523 ret = (ferror(cf)==0);
uci1 56:0bba0ef15697 524 #ifdef DEBUG
uci1 56:0bba0ef15697 525 printf("ret = %d\r\n",(int)ret);
uci1 56:0bba0ef15697 526 #endif
uci1 16:744ce85aede2 527 }
uci1 16:744ce85aede2 528 }
uci1 0:664899e0b988 529 fclose(cf);
uci1 0:664899e0b988 530 }
uci1 0:664899e0b988 531 return ret;
uci1 0:664899e0b988 532 }
uci1 0:664899e0b988 533
uci1 0:664899e0b988 534 bool SnConfigFrame::WriteToFile(const char* cfile) const {
uci1 0:664899e0b988 535 // intended only for writing default config file
uci1 0:664899e0b988 536
uci1 0:664899e0b988 537 bool ret = false;
uci1 0:664899e0b988 538 FILE* cf = fopen(cfile,"wb");
uci1 0:664899e0b988 539 if (cf!=0) {
uci1 0:664899e0b988 540 WriteTo(cf);
uci1 0:664899e0b988 541 ret = (ferror(cf)==0);
uci1 0:664899e0b988 542 fclose(cf);
uci1 0:664899e0b988 543 }
uci1 0:664899e0b988 544 return ret;
uci1 0:664899e0b988 545 }