Arianna autonomous DAQ firmware
Dependencies: mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW
SnCommWin.h@110:d1da040a0cf2, 2015-11-24 (annotated)
- Committer:
- uci1
- Date:
- Tue Nov 24 21:52:27 2015 +0000
- Revision:
- 110:d1da040a0cf2
- Parent:
- 98:ce72ef143b9b
- Child:
- 116:8099b754fbb4
Stn32 (slow) with conf name. SD stall fix. No interface chip. Safety nets.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
uci1 | 1:e392595b4b76 | 1 | #ifndef SN_SnCommWin |
uci1 | 1:e392595b4b76 | 2 | #define SN_SnCommWin |
uci1 | 1:e392595b4b76 | 3 | |
uci1 | 1:e392595b4b76 | 4 | #include "mbed.h" |
uci1 | 1:e392595b4b76 | 5 | #include <stdint.h> |
uci1 | 21:ce51bb0ba4a5 | 6 | #include <string> |
uci1 | 1:e392595b4b76 | 7 | |
uci1 | 8:95a325df1f6b | 8 | #include "SnConfigFrame.h" |
uci1 | 1:e392595b4b76 | 9 | class SnEventFrame; |
uci1 | 8:95a325df1f6b | 10 | class SnPowerFrame; |
uci1 | 41:d6f5e2f09e07 | 11 | class SnSignalStrengthFrame; |
uci1 | 37:ff95e7070f26 | 12 | class SnCommPeripheral; |
uci1 | 56:0bba0ef15697 | 13 | class SnTempFrame; |
uci1 | 84:80b15993944e | 14 | class SnClockSetFrame; |
uci1 | 84:80b15993944e | 15 | class SnHeartbeatFrame; |
uci1 | 1:e392595b4b76 | 16 | |
uci1 | 37:ff95e7070f26 | 17 | // DAQ station communication utilities |
uci1 | 1:e392595b4b76 | 18 | |
uci1 | 1:e392595b4b76 | 19 | class SnCommWin { |
uci1 | 1:e392595b4b76 | 20 | public: |
uci1 | 1:e392595b4b76 | 21 | enum ECommWinResult { |
uci1 | 1:e392595b4b76 | 22 | kUndefFail, // undefined fail type |
uci1 | 1:e392595b4b76 | 23 | kCanNotConnect, // unable to connect to port |
uci1 | 1:e392595b4b76 | 24 | kFailTimeout, // timed out but message required |
uci1 | 2:e67f7c158087 | 25 | kFailNoneSent, // none of the message sent |
uci1 | 1:e392595b4b76 | 26 | kFailPartSent, // only part of the message sent |
uci1 | 3:24c5f0f50bf1 | 27 | kUnexpectedRec, // unexpected / unhandled message received |
uci1 | 1:e392595b4b76 | 28 | kAllFails, // to be used for "if (ret > kAllFails) ==> success" |
uci1 | 1:e392595b4b76 | 29 | // only successes should go below |
uci1 | 1:e392595b4b76 | 30 | kConnected, // connection established, no messaging attempted |
uci1 | 27:efc4d654b139 | 31 | kOkStopComm, // received signal to stop communicating |
uci1 | 1:e392595b4b76 | 32 | kOkMsgSent, // message sent without waiting for "received" handshake |
uci1 | 1:e392595b4b76 | 33 | kOkNoMsg, // timed out with no message, but that is ok |
uci1 | 12:d472f9811262 | 34 | kOkWithMsg, // successfully received message |
uci1 | 40:1324da35afd4 | 35 | kOkWthMsgNoConf,// successfully received message and it says to stick with the same config |
uci1 | 40:1324da35afd4 | 36 | kOkWthMsgDidDel // successfully received message and deleted a file because of it |
uci1 | 1:e392595b4b76 | 37 | }; |
uci1 | 37:ff95e7070f26 | 38 | |
uci1 | 25:57b2627fe756 | 39 | static const char* kLocalDir; // the local mbed directory |
uci1 | 27:efc4d654b139 | 40 | static const char* kDelAllConfCodeStr; // a magic string to confirm deletion of all files on the SD card |
uci1 | 27:efc4d654b139 | 41 | static const uint8_t kDelAllConfCodeLen; // the length of the magic string |
uci1 | 21:ce51bb0ba4a5 | 42 | |
uci1 | 21:ce51bb0ba4a5 | 43 | private: |
uci1 | 56:0bba0ef15697 | 44 | bool fSendingInHandshake; // true if we are sending files as a result of a handshake. |
uci1 | 56:0bba0ef15697 | 45 | // will ignore further handshake requests to send new files |
uci1 | 56:0bba0ef15697 | 46 | // in order to avoid a stack overflow |
uci1 | 56:0bba0ef15697 | 47 | |
uci1 | 56:0bba0ef15697 | 48 | |
uci1 | 21:ce51bb0ba4a5 | 49 | SnCommWin::ECommWinResult GetFilename(const uint32_t timeout, |
uci1 | 21:ce51bb0ba4a5 | 50 | char* const buf, |
uci1 | 21:ce51bb0ba4a5 | 51 | const uint32_t namelen); |
uci1 | 21:ce51bb0ba4a5 | 52 | SnCommWin::ECommWinResult GetLocalFile(std::string fname, |
uci1 | 21:ce51bb0ba4a5 | 53 | char* const buf, |
uci1 | 21:ce51bb0ba4a5 | 54 | const uint32_t bsize, |
uci1 | 21:ce51bb0ba4a5 | 55 | const uint32_t timeout); |
uci1 | 21:ce51bb0ba4a5 | 56 | int16_t LoopLocalDirBinFiles(const bool doRemove, |
uci1 | 21:ce51bb0ba4a5 | 57 | const std::string& fname); |
uci1 | 21:ce51bb0ba4a5 | 58 | SnCommWin::ECommWinResult GetHeader(const uint32_t timeOut, |
uci1 | 21:ce51bb0ba4a5 | 59 | char* const buf, |
uci1 | 21:ce51bb0ba4a5 | 60 | const uint32_t bsize, |
uci1 | 21:ce51bb0ba4a5 | 61 | uint8_t& mcode, |
uci1 | 21:ce51bb0ba4a5 | 62 | uint32_t& mlen); |
uci1 | 21:ce51bb0ba4a5 | 63 | |
uci1 | 84:80b15993944e | 64 | bool WriteStatusDataPackHeaderTo(char*& b, |
uci1 | 84:80b15993944e | 65 | const uint32_t payloadLen); |
uci1 | 84:80b15993944e | 66 | |
uci1 | 84:80b15993944e | 67 | bool CallHeaderWriteTo(char*& b, |
uci1 | 84:80b15993944e | 68 | const uint8_t msgCode, |
uci1 | 84:80b15993944e | 69 | const uint32_t msgLen); |
uci1 | 84:80b15993944e | 70 | |
uci1 | 84:80b15993944e | 71 | int32_t CallSendAll(const char* const data, const uint32_t length, |
uci1 | 84:80b15993944e | 72 | const uint32_t timeout_clock); |
uci1 | 84:80b15993944e | 73 | |
uci1 | 84:80b15993944e | 74 | |
uci1 | 84:80b15993944e | 75 | template<class T> |
uci1 | 84:80b15993944e | 76 | char* WriteStatusDataPackHeadersFor(const T& x, |
uci1 | 84:80b15993944e | 77 | const uint8_t hdrCode, |
uci1 | 84:80b15993944e | 78 | char*& b, |
uci1 | 84:80b15993944e | 79 | const uint32_t timeout_clock) { |
uci1 | 84:80b15993944e | 80 | // flag that this is a data packet |
uci1 | 84:80b15993944e | 81 | const uint32_t xMsgLen = x.SizeOf(); |
uci1 | 84:80b15993944e | 82 | WriteStatusDataPackHeaderTo(b, xMsgLen); |
uci1 | 84:80b15993944e | 83 | CallHeaderWriteTo(b, hdrCode, xMsgLen); |
uci1 | 84:80b15993944e | 84 | return b; |
uci1 | 84:80b15993944e | 85 | } |
uci1 | 84:80b15993944e | 86 | |
uci1 | 84:80b15993944e | 87 | template<class T> |
uci1 | 84:80b15993944e | 88 | char* SendStatusDataPack(const T& x, |
uci1 | 84:80b15993944e | 89 | const uint8_t hdrCode, |
uci1 | 84:80b15993944e | 90 | int32_t& toBeSent, |
uci1 | 84:80b15993944e | 91 | int32_t& byteSent, |
uci1 | 84:80b15993944e | 92 | char* const genBuf, |
uci1 | 84:80b15993944e | 93 | const uint32_t timeout_clock) { |
uci1 | 84:80b15993944e | 94 | char* b = genBuf; |
uci1 | 84:80b15993944e | 95 | b = WriteStatusDataPackHeadersFor(x, hdrCode, |
uci1 | 84:80b15993944e | 96 | b, timeout_clock); |
uci1 | 84:80b15993944e | 97 | x.WriteTo(b); |
uci1 | 84:80b15993944e | 98 | const int32_t sendNow = (b - genBuf); |
uci1 | 84:80b15993944e | 99 | toBeSent += sendNow; |
uci1 | 84:80b15993944e | 100 | byteSent += CallSendAll(genBuf, sendNow, timeout_clock); |
uci1 | 84:80b15993944e | 101 | return b; |
uci1 | 84:80b15993944e | 102 | } |
uci1 | 84:80b15993944e | 103 | |
uci1 | 1:e392595b4b76 | 104 | |
uci1 | 3:24c5f0f50bf1 | 105 | protected: |
uci1 | 37:ff95e7070f26 | 106 | SnCommPeripheral* fComm; // the communication peripheral. deleted in dtor!! |
uci1 | 37:ff95e7070f26 | 107 | |
uci1 | 40:1324da35afd4 | 108 | virtual int32_t SendFileBlock(FILE* inf, |
uci1 | 40:1324da35afd4 | 109 | const uint8_t blockHeaderCode, |
uci1 | 40:1324da35afd4 | 110 | const uint32_t blockSize, |
uci1 | 40:1324da35afd4 | 111 | char* const genBuf, |
uci1 | 40:1324da35afd4 | 112 | int32_t& bytesToSend, |
uci1 | 40:1324da35afd4 | 113 | const uint32_t timeout); |
uci1 | 12:d472f9811262 | 114 | |
uci1 | 40:1324da35afd4 | 115 | virtual int32_t SendFileContents(FILE* inf, |
uci1 | 40:1324da35afd4 | 116 | const SnConfigFrame& curConf, |
uci1 | 40:1324da35afd4 | 117 | SnEventFrame& evt, |
uci1 | 40:1324da35afd4 | 118 | SnPowerFrame& pow, |
uci1 | 40:1324da35afd4 | 119 | char* const genBuf, |
uci1 | 40:1324da35afd4 | 120 | uint32_t nevts, |
uci1 | 40:1324da35afd4 | 121 | int32_t& bytesToBeSent, |
uci1 | 40:1324da35afd4 | 122 | const uint32_t timeout_clock); |
uci1 | 8:95a325df1f6b | 123 | |
uci1 | 37:ff95e7070f26 | 124 | public: |
uci1 | 37:ff95e7070f26 | 125 | SnCommWin(SnCommPeripheral* p); |
uci1 | 37:ff95e7070f26 | 126 | virtual ~SnCommWin(); |
uci1 | 12:d472f9811262 | 127 | |
uci1 | 37:ff95e7070f26 | 128 | // probably no need to overload |
uci1 | 40:1324da35afd4 | 129 | virtual bool TrySetSysTimeUnix(const uint32_t timeout, |
uci1 | 40:1324da35afd4 | 130 | uint32_t& prvTime, |
uci1 | 40:1324da35afd4 | 131 | uint32_t& setTime); |
uci1 | 37:ff95e7070f26 | 132 | virtual bool Connect(const uint32_t timeout); |
uci1 | 37:ff95e7070f26 | 133 | virtual bool CloseConn(const uint32_t timeout); |
uci1 | 40:1324da35afd4 | 134 | virtual bool PowerDown(const uint32_t timeout); |
uci1 | 1:e392595b4b76 | 135 | |
uci1 | 37:ff95e7070f26 | 136 | // optional overloads |
uci1 | 37:ff95e7070f26 | 137 | virtual void Set(const SnConfigFrame& conf) {} |
uci1 | 16:744ce85aede2 | 138 | |
uci1 | 37:ff95e7070f26 | 139 | // mandatory overloads |
uci1 | 37:ff95e7070f26 | 140 | virtual SnConfigFrame::EDatPackBit GetCommType() const=0; |
uci1 | 8:95a325df1f6b | 141 | |
uci1 | 84:80b15993944e | 142 | virtual ECommWinResult OpenWindow(const bool sendStatus, |
uci1 | 84:80b15993944e | 143 | const SnConfigFrame& conf, |
uci1 | 84:80b15993944e | 144 | const SnPowerFrame& pow, // com win power |
uci1 | 84:80b15993944e | 145 | const SnEventFrame& stEvent, |
uci1 | 84:80b15993944e | 146 | const uint16_t seq, |
uci1 | 84:80b15993944e | 147 | const float thmrate, |
uci1 | 84:80b15993944e | 148 | const float evtrate, |
uci1 | 84:80b15993944e | 149 | const uint32_t powerOnTime, |
uci1 | 84:80b15993944e | 150 | const SnTempFrame& temper, // com win temp |
uci1 | 84:80b15993944e | 151 | char* const genBuf, |
uci1 | 84:80b15993944e | 152 | const uint32_t timeout_clock)=0; |
uci1 | 84:80b15993944e | 153 | /* |
uci1 | 1:e392595b4b76 | 154 | virtual ECommWinResult OpenWindow(const uint32_t timeout, |
uci1 | 1:e392595b4b76 | 155 | const bool sendStatus, |
uci1 | 1:e392595b4b76 | 156 | const SnConfigFrame& conf, |
uci1 | 1:e392595b4b76 | 157 | const SnEventFrame& evt, |
uci1 | 8:95a325df1f6b | 158 | const SnPowerFrame& pow, |
uci1 | 10:3c93db1cfb12 | 159 | const uint16_t seq, |
uci1 | 10:3c93db1cfb12 | 160 | const float thmrate, |
uci1 | 10:3c93db1cfb12 | 161 | const float evtrate, |
uci1 | 56:0bba0ef15697 | 162 | const uint32_t powerOnTime, |
uci1 | 56:0bba0ef15697 | 163 | const SnTempFrame& temper, |
uci1 | 2:e67f7c158087 | 164 | char* const genBuf)=0; |
uci1 | 84:80b15993944e | 165 | */ |
uci1 | 27:efc4d654b139 | 166 | virtual bool GetDeleteAllConfirmCode(const SnConfigFrame& conf, |
uci1 | 27:efc4d654b139 | 167 | const uint32_t length, |
uci1 | 27:efc4d654b139 | 168 | const uint32_t timeout, |
uci1 | 27:efc4d654b139 | 169 | char* const buf, |
uci1 | 27:efc4d654b139 | 170 | const uint32_t bsize); |
uci1 | 21:ce51bb0ba4a5 | 171 | virtual ECommWinResult WaitHandshake(const SnConfigFrame& conf, |
uci1 | 21:ce51bb0ba4a5 | 172 | const uint32_t timeout, |
uci1 | 3:24c5f0f50bf1 | 173 | char* const buf, |
uci1 | 6:6f002d202f59 | 174 | const uint32_t bsize, |
uci1 | 27:efc4d654b139 | 175 | uint8_t& hndShkCode, |
uci1 | 27:efc4d654b139 | 176 | uint32_t* hndShkLen=0); |
uci1 | 40:1324da35afd4 | 177 | |
uci1 | 40:1324da35afd4 | 178 | virtual ECommWinResult HandleHandshake(FILE* inf, const char* infn, |
uci1 | 40:1324da35afd4 | 179 | const SnConfigFrame& curConf, |
uci1 | 40:1324da35afd4 | 180 | SnEventFrame& evt, |
uci1 | 40:1324da35afd4 | 181 | SnPowerFrame& pow, |
uci1 | 40:1324da35afd4 | 182 | char* const genBuf, |
uci1 | 40:1324da35afd4 | 183 | const uint32_t bsize, |
uci1 | 40:1324da35afd4 | 184 | const uint32_t handshakeTimeout, |
uci1 | 40:1324da35afd4 | 185 | const uint8_t hndshk, |
uci1 | 40:1324da35afd4 | 186 | const uint32_t hndlen, |
uci1 | 40:1324da35afd4 | 187 | const uint32_t* nevts=0); |
uci1 | 40:1324da35afd4 | 188 | int32_t SendHndshkReq(char* const genBuf, |
uci1 | 40:1324da35afd4 | 189 | const uint32_t timeout_clock); |
uci1 | 3:24c5f0f50bf1 | 190 | |
uci1 | 41:d6f5e2f09e07 | 191 | ECommWinResult SendSignalStrength(char* const genBuf, |
uci1 | 41:d6f5e2f09e07 | 192 | SnSignalStrengthFrame& sigstr, |
uci1 | 41:d6f5e2f09e07 | 193 | const uint32_t timeout_clock); |
uci1 | 41:d6f5e2f09e07 | 194 | |
uci1 | 8:95a325df1f6b | 195 | ECommWinResult GetConfig(SnConfigFrame& conf, |
uci1 | 8:95a325df1f6b | 196 | const uint32_t timeOut, |
uci1 | 8:95a325df1f6b | 197 | char* const confBuf, |
uci1 | 8:95a325df1f6b | 198 | const uint32_t bsize); |
uci1 | 1:e392595b4b76 | 199 | |
uci1 | 8:95a325df1f6b | 200 | ECommWinResult SendStatus(const SnConfigFrame& conf, |
uci1 | 84:80b15993944e | 201 | const SnPowerFrame& pow, // com win power |
uci1 | 84:80b15993944e | 202 | const SnEventFrame& stEvent, |
uci1 | 84:80b15993944e | 203 | const uint16_t seq, |
uci1 | 84:80b15993944e | 204 | const float thmrate, |
uci1 | 84:80b15993944e | 205 | const float evtrate, |
uci1 | 84:80b15993944e | 206 | const uint32_t powerOnTime, |
uci1 | 84:80b15993944e | 207 | const SnTempFrame& temper, // com win temp |
uci1 | 84:80b15993944e | 208 | char* const genBuf, |
uci1 | 84:80b15993944e | 209 | const uint32_t timeout_clock); |
uci1 | 84:80b15993944e | 210 | /* |
uci1 | 84:80b15993944e | 211 | ECommWinResult SendStatus(const SnConfigFrame& conf, |
uci1 | 8:95a325df1f6b | 212 | const SnEventFrame& evt, |
uci1 | 8:95a325df1f6b | 213 | const SnPowerFrame& pow, |
uci1 | 10:3c93db1cfb12 | 214 | const uint16_t seq, |
uci1 | 10:3c93db1cfb12 | 215 | const float thmrate, |
uci1 | 10:3c93db1cfb12 | 216 | const float evtrate, |
uci1 | 56:0bba0ef15697 | 217 | const uint32_t powerOnTime, |
uci1 | 56:0bba0ef15697 | 218 | const SnTempFrame& temper, |
uci1 | 8:95a325df1f6b | 219 | char* const genBuf, |
uci1 | 8:95a325df1f6b | 220 | const uint32_t timeout_clock); |
uci1 | 84:80b15993944e | 221 | */ |
uci1 | 84:80b15993944e | 222 | |
uci1 | 84:80b15993944e | 223 | virtual ECommWinResult SendStatusData(const SnConfigFrame& conf, |
uci1 | 98:ce72ef143b9b | 224 | const SnConfigFrame& stConf, |
uci1 | 84:80b15993944e | 225 | const SnClockSetFrame& stTrgStartClk, |
uci1 | 84:80b15993944e | 226 | const SnClockSetFrame& stTrgStopClk, |
uci1 | 84:80b15993944e | 227 | const SnPowerFrame& stPower, |
uci1 | 84:80b15993944e | 228 | const SnEventFrame& stEvent, |
uci1 | 84:80b15993944e | 229 | const SnTempFrame& stTemperature, |
uci1 | 84:80b15993944e | 230 | const SnHeartbeatFrame& stHeartbeat, |
uci1 | 84:80b15993944e | 231 | const bool stNewPower, |
uci1 | 84:80b15993944e | 232 | const bool stNewEvent, |
uci1 | 84:80b15993944e | 233 | const bool stNewHeartbeat, |
uci1 | 84:80b15993944e | 234 | const bool stNewTemperature, |
uci1 | 84:80b15993944e | 235 | char* const genBuf, |
uci1 | 84:80b15993944e | 236 | const uint32_t timeout_clock); |
uci1 | 84:80b15993944e | 237 | |
uci1 | 23:ccf39298f205 | 238 | SnCommWin::ECommWinResult SendString(const char* str, |
uci1 | 23:ccf39298f205 | 239 | const uint32_t timeout); |
uci1 | 23:ccf39298f205 | 240 | |
uci1 | 40:1324da35afd4 | 241 | int32_t SendFilename(const char* fn, |
uci1 | 40:1324da35afd4 | 242 | char* const genBuf, |
uci1 | 40:1324da35afd4 | 243 | int32_t& bytesToBeSent, |
uci1 | 40:1324da35afd4 | 244 | const uint32_t timeout_clock); |
uci1 | 6:6f002d202f59 | 245 | |
uci1 | 40:1324da35afd4 | 246 | ECommWinResult SendDataFromFile(FILE* inf, const char* infn, |
uci1 | 8:95a325df1f6b | 247 | const SnConfigFrame& curConf, |
uci1 | 8:95a325df1f6b | 248 | SnEventFrame& evt, |
uci1 | 8:95a325df1f6b | 249 | SnPowerFrame& pow, |
uci1 | 8:95a325df1f6b | 250 | char* const genBuf, |
uci1 | 8:95a325df1f6b | 251 | const uint32_t bsize, |
uci1 | 8:95a325df1f6b | 252 | const uint32_t nevts, |
uci1 | 8:95a325df1f6b | 253 | const uint32_t timeout_clock, |
uci1 | 40:1324da35afd4 | 254 | uint8_t* hndcode=0, |
uci1 | 40:1324da35afd4 | 255 | uint32_t* hndlen=0); |
uci1 | 6:6f002d202f59 | 256 | |
uci1 | 8:95a325df1f6b | 257 | ECommWinResult SendData(SnConfigFrame& conf, |
uci1 | 8:95a325df1f6b | 258 | SnEventFrame& evt, |
uci1 | 8:95a325df1f6b | 259 | SnPowerFrame& pow, |
uci1 | 8:95a325df1f6b | 260 | char* const genBuf, |
uci1 | 8:95a325df1f6b | 261 | const uint32_t bsize, |
uci1 | 40:1324da35afd4 | 262 | const uint32_t timeout); |
uci1 | 18:55f1581f2ee4 | 263 | |
uci1 | 56:0bba0ef15697 | 264 | // assume little endian |
uci1 | 56:0bba0ef15697 | 265 | static |
uci1 | 56:0bba0ef15697 | 266 | uint16_t GetMinSeqFrom(const uint32_t hndshk) { |
uci1 | 56:0bba0ef15697 | 267 | // left-most 2 bytes |
uci1 | 56:0bba0ef15697 | 268 | return (hndshk>>16u); |
uci1 | 56:0bba0ef15697 | 269 | } |
uci1 | 56:0bba0ef15697 | 270 | static |
uci1 | 56:0bba0ef15697 | 271 | uint16_t GetMaxSeqFrom(const uint32_t hndshk) { |
uci1 | 56:0bba0ef15697 | 272 | // right-most 2 bytes |
uci1 | 56:0bba0ef15697 | 273 | return (hndshk&0xFFFF); |
uci1 | 56:0bba0ef15697 | 274 | } |
uci1 | 56:0bba0ef15697 | 275 | |
uci1 | 1:e392595b4b76 | 276 | }; |
uci1 | 1:e392595b4b76 | 277 | |
uci1 | 1:e392595b4b76 | 278 | #endif // SN_SnCommWin |