Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Committer:
uci1
Date:
Sun Dec 15 05:25:40 2013 +0000
Revision:
54:ea1234a44fe8
Parent:
53:9cc158391bea
Child:
56:0bba0ef15697
Like Rev53 but fixed a bug preventing the default config from being changed remotely. Also fixed the hard coded config to have thermal triggers disabled.

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 0:664899e0b988 5 #include "SnBitUtils.h"
uci1 16:744ce85aede2 6 #include "SnHeaderFrame.h"
uci1 22:f957c4f840ad 7 #include "Watchdog.h"
uci1 0:664899e0b988 8
uci1 0:664899e0b988 9 extern "C" void mbed_mac_address(char *);
uci1 0:664899e0b988 10
uci1 31:b5bd3b189150 11 bool SnConfigFrame::fgApplySafetyNets = true;
uci1 40:1324da35afd4 12 const uint8_t SnConfigFrame::kIOVers = 8;
uci1 3:24c5f0f50bf1 13 const char* const SnConfigFrame::kDefConfFile = "/local/DEFCONF.DAT";
uci1 28:484943132bb0 14 const char* const SnConfigFrame::kDefIPflag = "DEF";
uci1 1:e392595b4b76 15 const uint32_t SnConfigFrame::kMinCommWinPrdLowPwr = 14400; // exclusive min low power comm win period (s)
uci1 1:e392595b4b76 16 const uint32_t SnConfigFrame::kMaxCommWinPrdLowPwr = 259200; // exclusive max low power comm win period (s)
uci1 1:e392595b4b76 17 const uint32_t SnConfigFrame::kMinCommWinDurLowPwr = 300; // exclusive min low power comm win duration (s)
uci1 1:e392595b4b76 18 const uint32_t SnConfigFrame::kMaxCommWinDurLowPwr = 3600; // exclusive max low power comm win duration (s)
uci1 1:e392595b4b76 19 const uint8_t SnConfigFrame::kConfLblLen;
uci1 0:664899e0b988 20
uci1 0:664899e0b988 21 uint64_t SnConfigFrame::fgMacAdr = 0;
uci1 0:664899e0b988 22
uci1 0:664899e0b988 23 void SnConfigFrame::SetMacAddress() {
uci1 0:664899e0b988 24 static const uint8_t b64 = sizeof(uint64_t);
uci1 0:664899e0b988 25 static char c[b64];
uci1 0:664899e0b988 26 mbed_mac_address(&(c[0]));
uci1 0:664899e0b988 27 // like a big endian union
uci1 0:664899e0b988 28 fgMacAdr = 0;
uci1 0:664899e0b988 29 const char* a = c+(b64-1);
uci1 0:664899e0b988 30 for (uint8_t i=0; i<b64; i++, a--) {
uci1 0:664899e0b988 31 fgMacAdr |= static_cast<uint64_t>(*a) << (i<<3);
uci1 0:664899e0b988 32 }
uci1 0:664899e0b988 33 }
uci1 0:664899e0b988 34
uci1 0:664899e0b988 35 void SnConfigFrame::SetHardDefaults() {
uci1 0:664899e0b988 36 sprintf(fLabel,"HardDefaults");
uci1 0:664899e0b988 37 fConfTime = 1338854400u; // Tue, 05 Jun 2012 00:00:00 GMT
uci1 0:664899e0b988 38 fRun = 0;
uci1 40:1324da35afd4 39 fFirstSeq = 0;
uci1 54:ea1234a44fe8 40 fEvtsPerSeq = 300;
uci1 8:95a325df1f6b 41 fRunMode = 0;
uci1 1:e392595b4b76 42 fStreamHiLoPlas = 0;
uci1 0:664899e0b988 43 fWvLoseLSB = 0;
uci1 1:e392595b4b76 44 fWvLoseMSB = 4;
uci1 0:664899e0b988 45 fWvBaseline = 0;
uci1 0:664899e0b988 46 fDatPackType = ~0;
uci1 0:664899e0b988 47 uint16_t* dc = &(fDAC[0][0]);
uci1 0:664899e0b988 48 for (uint16_t i=0; i<kTotDacs; i++, dc++) {
uci1 0:664899e0b988 49 *dc = 3072u;
uci1 0:664899e0b988 50 }
uci1 0:664899e0b988 51 fNumPlas = 1;
uci1 0:664899e0b988 52 uint16_t* pl = &(fPLA[0]);
uci1 0:664899e0b988 53 for (uint8_t j=0; j<kNplas; j++, pl++) {
uci1 0:664899e0b988 54 *pl = 0x7FFFu;
uci1 0:664899e0b988 55 }
uci1 28:484943132bb0 56 fNumCardsMajLog = 2;
uci1 54:ea1234a44fe8 57 fEnableThermTrig = 0;
uci1 1:e392595b4b76 58 fForceTrigPeriod = 67u;
uci1 0:664899e0b988 59 fHeartBeatPeriod = 0;
uci1 0:664899e0b988 60 fAmpsOn = 0x0Fu;
uci1 0:664899e0b988 61 fEvtThrtlPeriodMs = 50u;
uci1 28:484943132bb0 62 fPowerMode = kAmpsDatTak|kCardDatTak|kAmpsComWin|kCardComWin|kIridComWin|kAfarComWin;
uci1 39:2f17131d22a5 63 fBatVoltToLowPwr = 0;
uci1 39:2f17131d22a5 64 fBatVoltFromLowPwr = 0;
uci1 28:484943132bb0 65 fVoltCheckPeriod = 600u;
uci1 54:ea1234a44fe8 66 fCommWinPeriod = 600u;
uci1 1:e392595b4b76 67 fCommWinDuration = 600u;
uci1 28:484943132bb0 68 fCommSendData = static_cast<int16_t>(kUseBits);
uci1 0:664899e0b988 69 fCommWinPrdLowPwr = 86100u;
uci1 0:664899e0b988 70 fCommWinDurLowPwr = 300u;
uci1 40:1324da35afd4 71 fCommWinConnectTOMin = 3u;
uci1 40:1324da35afd4 72 fCommWinListenTOMin = 3u;
uci1 22:f957c4f840ad 73 fWatchDogPeriod = WDFAILSAFE;
uci1 28:484943132bb0 74 SetDefaultIPs();
uci1 8:95a325df1f6b 75 fIsLowPower = false;
uci1 8:95a325df1f6b 76 memcpy(fNormLabel, fLabel, kConfLblLen);
uci1 8:95a325df1f6b 77 fNormPowerMode = fPowerMode;
uci1 8:95a325df1f6b 78
uci1 54:ea1234a44fe8 79 ApplyConnectListenSafetyNets();
uci1 8:95a325df1f6b 80 }
uci1 8:95a325df1f6b 81
uci1 28:484943132bb0 82 void SnConfigFrame::SetDefaultRemoteServ() {
uci1 28:484943132bb0 83 sprintf(fRemoteServer,"%s","128.195.204.151");
uci1 28:484943132bb0 84 }
uci1 28:484943132bb0 85
uci1 28:484943132bb0 86 void SnConfigFrame::SetDefaultRemotePort() {
uci1 28:484943132bb0 87 fRemotePort = 6655;
uci1 28:484943132bb0 88 }
uci1 28:484943132bb0 89
uci1 28:484943132bb0 90 void SnConfigFrame::SetDefaultMbedIP() {
uci1 28:484943132bb0 91 const uint64_t ip = GetMacAddress();
uci1 28:484943132bb0 92 switch (ip) {
uci1 28:484943132bb0 93 case 0x0002F7F0C3B60000: // station 3
uci1 28:484943132bb0 94 sprintf(fMbedIP,"%s","157.132.94.35");
uci1 28:484943132bb0 95 break;
uci1 28:484943132bb0 96 case 0x0002F7F0C41C0000: // station 4
uci1 28:484943132bb0 97 sprintf(fMbedIP,"%s","157.132.94.37");
uci1 28:484943132bb0 98 break;
uci1 53:9cc158391bea 99 /*
uci1 28:484943132bb0 100 case 0x0002F7F0AEE00000: // station 5
uci1 28:484943132bb0 101 sprintf(fMbedIP,"%s","157.132.94.39");
uci1 28:484943132bb0 102 break;
uci1 53:9cc158391bea 103 */
uci1 53:9cc158391bea 104 case 0x0002F7F0C61A0000: // station 10
uci1 53:9cc158391bea 105 sprintf(fMbedIP,"%s","157.132.94.39");
uci1 53:9cc158391bea 106 break;
uci1 28:484943132bb0 107 case 0x0002F7F0C4450000: // station 6
uci1 28:484943132bb0 108 sprintf(fMbedIP,"%s","157.132.94.41");
uci1 28:484943132bb0 109 break;
uci1 53:9cc158391bea 110 /*
uci1 28:484943132bb0 111 case 0x0002F7F0D2880000: // station 7
uci1 28:484943132bb0 112 sprintf(fMbedIP,"%s","157.132.94.43");
uci1 28:484943132bb0 113 break;
uci1 28:484943132bb0 114 case 0x0002F7F0C0F80000: // station 8
uci1 28:484943132bb0 115 sprintf(fMbedIP,"%s","157.132.94.45");
uci1 28:484943132bb0 116 break;
uci1 53:9cc158391bea 117 */
uci1 53:9cc158391bea 118 case 0x0002F7F175B70000: // station 11
uci1 53:9cc158391bea 119 sprintf(fMbedIP,"%s","157.132.94.43");
uci1 53:9cc158391bea 120 break;
uci1 53:9cc158391bea 121 case 0x0002F7F0C5610000: // station 12
uci1 53:9cc158391bea 122 sprintf(fMbedIP,"%s","157.132.94.45");
uci1 53:9cc158391bea 123 break;
uci1 28:484943132bb0 124 default: // what??
uci1 28:484943132bb0 125 sprintf(fMbedIP,"%s","157.132.94.30"); // anciet station
uci1 28:484943132bb0 126 break;
uci1 28:484943132bb0 127 };
uci1 28:484943132bb0 128 }
uci1 28:484943132bb0 129 // brian dornick 3107
uci1 28:484943132bb0 130 void SnConfigFrame::SetDefaultMaskIP() {
uci1 28:484943132bb0 131 sprintf(fMbedMask,"%s","255.255.255.0");
uci1 28:484943132bb0 132 }
uci1 28:484943132bb0 133
uci1 28:484943132bb0 134 void SnConfigFrame::SetDefaultGateIP() {
uci1 28:484943132bb0 135 sprintf(fMbedGate,"%s","157.132.94.1");
uci1 28:484943132bb0 136 }
uci1 28:484943132bb0 137
uci1 28:484943132bb0 138 void SnConfigFrame::SetDefaultIPs() {
uci1 28:484943132bb0 139 SetDefaultRemoteServ();
uci1 28:484943132bb0 140 SetDefaultRemotePort();
uci1 28:484943132bb0 141 SetDefaultMbedIP();
uci1 28:484943132bb0 142 SetDefaultMaskIP();
uci1 28:484943132bb0 143 SetDefaultGateIP();
uci1 28:484943132bb0 144 }
uci1 28:484943132bb0 145
uci1 40:1324da35afd4 146 void SnConfigFrame::ApplyConnectListenSafetyNets() {
uci1 40:1324da35afd4 147 if (fCommWinConnectTOMin<kDefTimeoutMin) {
uci1 40:1324da35afd4 148 fCommWinConnectTOMin=kDefTimeoutMin;
uci1 40:1324da35afd4 149 }
uci1 40:1324da35afd4 150 if (fCommWinConnectTOMin>(fCommWinDuration/static_cast<float>(kNcomms))) {
uci1 40:1324da35afd4 151 fCommWinConnectTOMin=kDefTimeoutMin;
uci1 40:1324da35afd4 152 }
uci1 40:1324da35afd4 153 if (fCommWinListenTOMin<kDefTimeoutMin) {
uci1 40:1324da35afd4 154 fCommWinListenTOMin=kDefTimeoutMin;
uci1 40:1324da35afd4 155 }
uci1 40:1324da35afd4 156 if (fCommWinListenTOMin>(fCommWinDuration/static_cast<float>(kNcomms))) {
uci1 40:1324da35afd4 157 fCommWinListenTOMin=kDefTimeoutMin;
uci1 40:1324da35afd4 158 }
uci1 40:1324da35afd4 159 }
uci1 40:1324da35afd4 160
uci1 28:484943132bb0 161 void SnConfigFrame::ApplySafetyNets() {
uci1 40:1324da35afd4 162 if (fFirstSeq>kMaxFirstSeq) {
uci1 40:1324da35afd4 163 fFirstSeq=kMaxFirstSeq;
uci1 40:1324da35afd4 164 }
uci1 28:484943132bb0 165 if (fNumPlas>kNplas) {
uci1 28:484943132bb0 166 fNumPlas=kNplas;
uci1 28:484943132bb0 167 }
uci1 28:484943132bb0 168 if (fNumCardsMajLog>kNchans) {
uci1 28:484943132bb0 169 fNumCardsMajLog=kNchans;
uci1 28:484943132bb0 170 }
uci1 28:484943132bb0 171 if (fNumCardsMajLog<1u) {
uci1 28:484943132bb0 172 fNumCardsMajLog=1u;
uci1 28:484943132bb0 173 }
uci1 28:484943132bb0 174 if ( (fForceTrigPeriod>0) &&
uci1 28:484943132bb0 175 (fForceTrigPeriod<kMinForcePer) ) {
uci1 28:484943132bb0 176 fForceTrigPeriod = kMinForcePer;
uci1 28:484943132bb0 177 }
uci1 28:484943132bb0 178 if (fEvtThrtlPeriodMs>kMaxThrottlePerMs) {
uci1 28:484943132bb0 179 fEvtThrtlPeriodMs=kMaxThrottlePerMs;
uci1 28:484943132bb0 180 }
uci1 28:484943132bb0 181 if ( (IsPoweredFor(kIridComWin)==false) &&
uci1 28:484943132bb0 182 (IsPoweredFor(kAfarComWin)==false) ) {
uci1 28:484943132bb0 183 EnablePowerFor(kIridComWin);
uci1 28:484943132bb0 184 EnablePowerFor(kAfarComWin);
uci1 28:484943132bb0 185 }
uci1 39:2f17131d22a5 186 if (fBatVoltToLowPwr>kMaxBatVoltLowPwr) {
uci1 39:2f17131d22a5 187 fBatVoltToLowPwr=kMaxBatVoltLowPwr;
uci1 39:2f17131d22a5 188 }
uci1 39:2f17131d22a5 189 if (fBatVoltFromLowPwr>kMaxBatVoltLowPwr) {
uci1 39:2f17131d22a5 190 fBatVoltFromLowPwr=kMaxBatVoltLowPwr;
uci1 39:2f17131d22a5 191 }
uci1 39:2f17131d22a5 192 if (fBatVoltFromLowPwr<fBatVoltToLowPwr) {
uci1 39:2f17131d22a5 193 fBatVoltFromLowPwr=fBatVoltToLowPwr;
uci1 28:484943132bb0 194 }
uci1 28:484943132bb0 195 if (fCommWinPeriod>kMaxCommWinPeriod) {
uci1 28:484943132bb0 196 fCommWinPeriod=kMaxCommWinPeriod;
uci1 28:484943132bb0 197 }
uci1 40:1324da35afd4 198 if (fCommWinPeriod<kMinCommWinPeriod) {
uci1 40:1324da35afd4 199 fCommWinPeriod=kMinCommWinPeriod;
uci1 40:1324da35afd4 200 }
uci1 28:484943132bb0 201 if (fCommWinDuration<kMinCommWinDur) {
uci1 28:484943132bb0 202 fCommWinDuration=kMinCommWinDur;
uci1 28:484943132bb0 203 }
uci1 28:484943132bb0 204 if (fCommWinPrdLowPwr>kMaxCommWinPeriod) {
uci1 28:484943132bb0 205 fCommWinPrdLowPwr=kMaxCommWinPeriod;
uci1 28:484943132bb0 206 }
uci1 28:484943132bb0 207 if (fCommWinDurLowPwr<kMinCommWinDur) {
uci1 28:484943132bb0 208 fCommWinDurLowPwr=kMinCommWinDur;
uci1 28:484943132bb0 209 }
uci1 40:1324da35afd4 210 ApplyConnectListenSafetyNets();
uci1 28:484943132bb0 211 if (fWatchDogPeriod>kMaxWatchDogPer) {
uci1 28:484943132bb0 212 fWatchDogPeriod=kMaxWatchDogPer;
uci1 28:484943132bb0 213 }
uci1 28:484943132bb0 214 if (fWatchDogPeriod<kMinWatchDogPer) {
uci1 28:484943132bb0 215 fWatchDogPeriod=kMinWatchDogPer;
uci1 28:484943132bb0 216 }
uci1 28:484943132bb0 217 }
uci1 28:484943132bb0 218
uci1 21:ce51bb0ba4a5 219 uint32_t SnConfigFrame::GetTimeoutTime(const uint32_t startTime,
uci1 21:ce51bb0ba4a5 220 const uint32_t delta) const {
uci1 40:1324da35afd4 221 // --.-----lst------.--delta--.--
uci1 40:1324da35afd4 222 // . . .
uci1 40:1324da35afd4 223 // start current returnVal
uci1 40:1324da35afd4 224 //
uci1 40:1324da35afd4 225 // lio=lst+delta bound by comm wind dur
uci1 40:1324da35afd4 226 // returns start + lio
uci1 40:1324da35afd4 227
uci1 21:ce51bb0ba4a5 228 const uint32_t ct = time(0);
uci1 40:1324da35afd4 229 uint32_t lst = ct-startTime;
uci1 21:ce51bb0ba4a5 230 if ( (ct<startTime) || (ct==0) ||
uci1 21:ce51bb0ba4a5 231 (lst>kSecsPerDay) ) {
uci1 21:ce51bb0ba4a5 232 // possible clock problems
uci1 40:1324da35afd4 233 lst = static_cast<uint32_t>(kDefTimeoutMin)*60u;
uci1 21:ce51bb0ba4a5 234 }
uci1 21:ce51bb0ba4a5 235 const uint32_t lio =
uci1 21:ce51bb0ba4a5 236 ((lst+delta) < GetCommWinDuration()) ?
uci1 21:ce51bb0ba4a5 237 lst+delta : GetCommWinDuration();
uci1 21:ce51bb0ba4a5 238 return lio+startTime;
uci1 21:ce51bb0ba4a5 239 }
uci1 21:ce51bb0ba4a5 240
uci1 8:95a325df1f6b 241 void SnConfigFrame::ChangeToLowPower() {
uci1 8:95a325df1f6b 242
uci1 8:95a325df1f6b 243 // save old label
uci1 8:95a325df1f6b 244 memcpy(fNormLabel, fLabel, kConfLblLen);
uci1 8:95a325df1f6b 245
uci1 8:95a325df1f6b 246 // append label
uci1 8:95a325df1f6b 247 // this will allow the new config to be put in the DB
uci1 8:95a325df1f6b 248 int slen = strlen(fLabel);
uci1 8:95a325df1f6b 249 static const char* tag = "_LOWPOW";
uci1 8:95a325df1f6b 250 const int ml = strlen(tag)+1;
uci1 8:95a325df1f6b 251 if (slen > (kConfLblLen-ml) ) {
uci1 8:95a325df1f6b 252 memset(fLabel+kConfLblLen-ml, '\0', ml);
uci1 8:95a325df1f6b 253 }
uci1 8:95a325df1f6b 254 strncat(fLabel, tag, ml-1);
uci1 8:95a325df1f6b 255
uci1 8:95a325df1f6b 256 // save power settings
uci1 8:95a325df1f6b 257 fNormPowerMode = fPowerMode;
uci1 8:95a325df1f6b 258
uci1 8:95a325df1f6b 259 // change power settings
uci1 8:95a325df1f6b 260 DisablePowerFor(kAmpsDatTak);
uci1 8:95a325df1f6b 261 DisablePowerFor(kCardDatTak);
uci1 8:95a325df1f6b 262 DisablePowerFor(kIridDatTak);
uci1 8:95a325df1f6b 263 DisablePowerFor(kAfarDatTak);
uci1 8:95a325df1f6b 264 DisablePowerFor(kAmpsComWin);
uci1 8:95a325df1f6b 265 DisablePowerFor(kCardComWin);
uci1 8:95a325df1f6b 266 if ( (IsPoweredFor(kIridComWin)==false) &&
uci1 8:95a325df1f6b 267 (IsPoweredFor(kAfarComWin)==false) ) {
uci1 8:95a325df1f6b 268 // TODO: turn on only iridum maybe?
uci1 8:95a325df1f6b 269 EnablePowerFor(kIridComWin);
uci1 8:95a325df1f6b 270 EnablePowerFor(kAfarComWin);
uci1 8:95a325df1f6b 271 }
uci1 8:95a325df1f6b 272 // set mode to low power
uci1 8:95a325df1f6b 273 fIsLowPower = true;
uci1 8:95a325df1f6b 274 }
uci1 8:95a325df1f6b 275
uci1 8:95a325df1f6b 276 void SnConfigFrame::ChangeToNormPower() {
uci1 8:95a325df1f6b 277 // put label back
uci1 8:95a325df1f6b 278 memcpy(fLabel, fNormLabel, kConfLblLen);
uci1 8:95a325df1f6b 279 // put power settings back
uci1 8:95a325df1f6b 280 fPowerMode = fNormPowerMode;
uci1 8:95a325df1f6b 281 // set mode to normal
uci1 8:95a325df1f6b 282 fIsLowPower = false;
uci1 0:664899e0b988 283 }
uci1 0:664899e0b988 284
uci1 0:664899e0b988 285 void SnConfigFrame::GetPackParsFor(const EDatPackBit d,
uci1 0:664899e0b988 286 uint8_t& loseLSB, uint8_t& loseMSB,
uci1 0:664899e0b988 287 uint16_t& wvBase) const {
uci1 0:664899e0b988 288 const bool pack = IsDatPackedFor(d);
uci1 0:664899e0b988 289 loseLSB = pack ? GetWvLoseLSB() : 0u;
uci1 0:664899e0b988 290 loseMSB = pack ? GetWvLoseMSB() : 0u;
uci1 0:664899e0b988 291 wvBase = pack ? GetWvBaseline() : 0u;
uci1 0:664899e0b988 292 }
uci1 0:664899e0b988 293
uci1 0:664899e0b988 294 void SnConfigFrame::GetHiLoPlas(const uint16_t pla,
uci1 0:664899e0b988 295 uint16_t& hiPla,
uci1 0:664899e0b988 296 uint16_t& loPla,
uci1 0:664899e0b988 297 const bool r2l) {
uci1 0:664899e0b988 298 // split the PLA bitword into 2: one for the high threshold
uci1 0:664899e0b988 299 // and one for the low threshold. "lows" in the string will become
uci1 0:664899e0b988 300 // "highs" in the low threshold PLA.
uci1 0:664899e0b988 301 //
uci1 0:664899e0b988 302 // example 1)
uci1 0:664899e0b988 303 // PLA string = HLHL....
uci1 0:664899e0b988 304 // hi thresh = H.H.....
uci1 0:664899e0b988 305 // lo thresh = .H.H....
uci1 0:664899e0b988 306 //
uci1 0:664899e0b988 307 // example 2)
uci1 0:664899e0b988 308 // PLA string = HBL.....
uci1 0:664899e0b988 309 // hi thresh = HL......
uci1 0:664899e0b988 310 // lo thresh = .LH.....
uci1 0:664899e0b988 311 //
uci1 0:664899e0b988 312 // (with . = A here, to make the example more readable)
uci1 0:664899e0b988 313 //
uci1 0:664899e0b988 314 // A = 11, B = 00
uci1 0:664899e0b988 315 // H = 01 or 10, alternating
uci1 0:664899e0b988 316 // L = 10 or 01, alternating
uci1 0:664899e0b988 317 // 01 at leftmost bits is H
uci1 0:664899e0b988 318 // for example:
uci1 0:664899e0b988 319 // 0x7FFF = 01 11 11 11 11 11 11 11
uci1 0:664899e0b988 320 // => HAAAAAAA for LEFT TO RIGHT
uci1 0:664899e0b988 321 // => AAAAAAAH for RIGHT TO LEFT
uci1 0:664899e0b988 322 // 0x56FF = 01 01 01 10 11 11 11 11
uci1 0:664899e0b988 323 // => HLHHAAAA for LEFT TO RIGHT
uci1 0:664899e0b988 324 // => AAAAHHLH for RIGHT TO LEFT
uci1 0:664899e0b988 325 //
uci1 0:664899e0b988 326 // so HHHHHHHH is
uci1 0:664899e0b988 327 // 01 10 01 10 01 10 01 10 always (r2l or l2r)
uci1 0:664899e0b988 328 //
uci1 0:664899e0b988 329 // r2l = whether to read bits right to left (true) or not (false)
uci1 0:664899e0b988 330 // Mahshid liked right to left
uci1 0:664899e0b988 331 // Liang liked left to right
uci1 0:664899e0b988 332 // so we allow for either
uci1 0:664899e0b988 333
uci1 0:664899e0b988 334 const int8_t start = (r2l) ? 0 : BITS_IN_SHORT-2;
uci1 0:664899e0b988 335 const int8_t end = (r2l) ? BITS_IN_SHORT : -2;
uci1 39:2f17131d22a5 336 const int8_t step = (r2l) ? 2 : -2;
uci1 0:664899e0b988 337
uci1 0:664899e0b988 338 uint8_t hi= (r2l) ? 0x2 : 0x1;
uci1 0:664899e0b988 339 uint8_t lo= (r2l) ? 0x1 : 0x2;
uci1 0:664899e0b988 340
uci1 0:664899e0b988 341 // set all bits to 0
uci1 0:664899e0b988 342 hiPla = 0;
uci1 0:664899e0b988 343 loPla = 0;
uci1 0:664899e0b988 344
uci1 0:664899e0b988 345 for (int8_t i=start; i!=end; i+=step, hi^=0x3, lo^=0x3) {
uci1 0:664899e0b988 346 const uint8_t b = (pla & (0x3<<i)) >> i;
uci1 0:664899e0b988 347 if (b==hi) {
uci1 0:664899e0b988 348 hiPla |= hi << i;
uci1 0:664899e0b988 349 loPla |= 0x3 << i;
uci1 0:664899e0b988 350 } else if (b==lo) {
uci1 0:664899e0b988 351 hiPla |= 0x3 << i;
uci1 0:664899e0b988 352 loPla |= hi << i;
uci1 0:664899e0b988 353 } else if (b==0x3) {
uci1 0:664899e0b988 354 // any
uci1 0:664899e0b988 355 hiPla |= 0x3 << i;
uci1 0:664899e0b988 356 loPla |= 0x3 << i;
uci1 0:664899e0b988 357 } else {
uci1 0:664899e0b988 358 // no check that b is something else.. should be impossible.
uci1 0:664899e0b988 359 // between
uci1 0:664899e0b988 360 hiPla |= lo << i;
uci1 0:664899e0b988 361 loPla |= lo << i;
uci1 0:664899e0b988 362 }
uci1 0:664899e0b988 363 }
uci1 0:664899e0b988 364
uci1 0:664899e0b988 365 }
uci1 0:664899e0b988 366
uci1 0:664899e0b988 367 bool SnConfigFrame::ReadFromFile(const char* cfile) {
uci1 0:664899e0b988 368 // intended only for reading default config file
uci1 0:664899e0b988 369
uci1 3:24c5f0f50bf1 370 /*
uci1 3:24c5f0f50bf1 371 DIR* d;
uci1 3:24c5f0f50bf1 372 struct dirent* dent;
uci1 3:24c5f0f50bf1 373 printf("files in /local:\r\n");
uci1 3:24c5f0f50bf1 374 if ( (d = opendir( "/local" ))!=NULL ) {
uci1 3:24c5f0f50bf1 375 while ( (dent = readdir(d))!=NULL ) {
uci1 3:24c5f0f50bf1 376 printf("%s\r\n",dent->d_name);
uci1 3:24c5f0f50bf1 377 }
uci1 3:24c5f0f50bf1 378 closedir(d);
uci1 3:24c5f0f50bf1 379 }
uci1 3:24c5f0f50bf1 380 */
uci1 0:664899e0b988 381 bool ret = false;
uci1 0:664899e0b988 382 FILE* cf = fopen(cfile,"rb");
uci1 0:664899e0b988 383 if (cf!=0) {
uci1 16:744ce85aede2 384 // check the header and file size to be
uci1 16:744ce85aede2 385 // protect a bit against corruption
uci1 16:744ce85aede2 386 uint8_t hc; uint32_t hl;
uci1 16:744ce85aede2 387 SnHeaderFrame::ReadFrom(cf, hc, hl);
uci1 16:744ce85aede2 388 if (hc==SnHeaderFrame::kConfigCode) {
uci1 16:744ce85aede2 389 const int fpos = ftell(cf);
uci1 16:744ce85aede2 390 // how many bytes?
uci1 16:744ce85aede2 391 fseek(cf, 0, SEEK_END); // go to end
uci1 16:744ce85aede2 392 const int fend = ftell(cf);
uci1 16:744ce85aede2 393 fseek(cf, fpos, SEEK_SET); // go back
uci1 16:744ce85aede2 394
uci1 16:744ce85aede2 395 if (hl == fend-fpos) {
uci1 16:744ce85aede2 396 ReadFrom(cf);
uci1 16:744ce85aede2 397 ret = (ferror(cf)==0);
uci1 16:744ce85aede2 398 }
uci1 16:744ce85aede2 399 }
uci1 0:664899e0b988 400 fclose(cf);
uci1 0:664899e0b988 401 }
uci1 0:664899e0b988 402 return ret;
uci1 0:664899e0b988 403 }
uci1 0:664899e0b988 404
uci1 0:664899e0b988 405 bool SnConfigFrame::WriteToFile(const char* cfile) const {
uci1 0:664899e0b988 406 // intended only for writing default config file
uci1 0:664899e0b988 407
uci1 0:664899e0b988 408 bool ret = false;
uci1 0:664899e0b988 409 FILE* cf = fopen(cfile,"wb");
uci1 0:664899e0b988 410 if (cf!=0) {
uci1 0:664899e0b988 411 WriteTo(cf);
uci1 0:664899e0b988 412 ret = (ferror(cf)==0);
uci1 0:664899e0b988 413 fclose(cf);
uci1 0:664899e0b988 414 }
uci1 0:664899e0b988 415 return ret;
uci1 0:664899e0b988 416 }