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