Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Committer:
uci1
Date:
Sat Oct 31 02:29:46 2015 +0000
Revision:
85:3ced48ef94c5
Parent:
84:80b15993944e
Child:
86:051a09841ca3
fix default connect and listen timeouts

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