Arianna autonomous DAQ firmware
Dependencies: mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW
SnCommWin.h@125:ce4045184366, 2019-06-05 (annotated)
- Committer:
- uci1
- Date:
- Wed Jun 05 17:29:31 2019 +0000
- Revision:
- 125:ce4045184366
- Parent:
- 122:c1b5023eac69
Added SnRateListner proto-class, publishing this version of the code in order to enable exporting of most recent features.
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 | 122:c1b5023eac69 | 52 | bool BuildLocalFileName(std::string fname, |
uci1 | 122:c1b5023eac69 | 53 | const char* const dir, |
uci1 | 122:c1b5023eac69 | 54 | std::string& lfname); |
uci1 | 21:ce51bb0ba4a5 | 55 | SnCommWin::ECommWinResult GetLocalFile(std::string fname, |
uci1 | 21:ce51bb0ba4a5 | 56 | char* const buf, |
uci1 | 21:ce51bb0ba4a5 | 57 | const uint32_t bsize, |
uci1 | 122:c1b5023eac69 | 58 | const uint32_t timeout, |
uci1 | 122:c1b5023eac69 | 59 | std::string& lfname); |
uci1 | 21:ce51bb0ba4a5 | 60 | int16_t LoopLocalDirBinFiles(const bool doRemove, |
uci1 | 21:ce51bb0ba4a5 | 61 | const std::string& fname); |
uci1 | 21:ce51bb0ba4a5 | 62 | SnCommWin::ECommWinResult GetHeader(const uint32_t timeOut, |
uci1 | 21:ce51bb0ba4a5 | 63 | char* const buf, |
uci1 | 21:ce51bb0ba4a5 | 64 | const uint32_t bsize, |
uci1 | 21:ce51bb0ba4a5 | 65 | uint8_t& mcode, |
uci1 | 21:ce51bb0ba4a5 | 66 | uint32_t& mlen); |
uci1 | 21:ce51bb0ba4a5 | 67 | |
uci1 | 84:80b15993944e | 68 | bool WriteStatusDataPackHeaderTo(char*& b, |
uci1 | 84:80b15993944e | 69 | const uint32_t payloadLen); |
uci1 | 84:80b15993944e | 70 | |
uci1 | 84:80b15993944e | 71 | bool CallHeaderWriteTo(char*& b, |
uci1 | 84:80b15993944e | 72 | const uint8_t msgCode, |
uci1 | 84:80b15993944e | 73 | const uint32_t msgLen); |
uci1 | 84:80b15993944e | 74 | |
uci1 | 84:80b15993944e | 75 | int32_t CallSendAll(const char* const data, const uint32_t length, |
uci1 | 84:80b15993944e | 76 | const uint32_t timeout_clock); |
uci1 | 84:80b15993944e | 77 | |
uci1 | 84:80b15993944e | 78 | |
uci1 | 84:80b15993944e | 79 | template<class T> |
uci1 | 84:80b15993944e | 80 | char* WriteStatusDataPackHeadersFor(const T& x, |
uci1 | 84:80b15993944e | 81 | const uint8_t hdrCode, |
uci1 | 84:80b15993944e | 82 | char*& b, |
uci1 | 84:80b15993944e | 83 | const uint32_t timeout_clock) { |
uci1 | 84:80b15993944e | 84 | // flag that this is a data packet |
uci1 | 84:80b15993944e | 85 | const uint32_t xMsgLen = x.SizeOf(); |
uci1 | 84:80b15993944e | 86 | WriteStatusDataPackHeaderTo(b, xMsgLen); |
uci1 | 84:80b15993944e | 87 | CallHeaderWriteTo(b, hdrCode, xMsgLen); |
uci1 | 84:80b15993944e | 88 | return b; |
uci1 | 84:80b15993944e | 89 | } |
uci1 | 84:80b15993944e | 90 | |
uci1 | 84:80b15993944e | 91 | template<class T> |
uci1 | 84:80b15993944e | 92 | char* SendStatusDataPack(const T& x, |
uci1 | 84:80b15993944e | 93 | const uint8_t hdrCode, |
uci1 | 84:80b15993944e | 94 | int32_t& toBeSent, |
uci1 | 84:80b15993944e | 95 | int32_t& byteSent, |
uci1 | 84:80b15993944e | 96 | char* const genBuf, |
uci1 | 84:80b15993944e | 97 | const uint32_t timeout_clock) { |
uci1 | 84:80b15993944e | 98 | char* b = genBuf; |
uci1 | 84:80b15993944e | 99 | b = WriteStatusDataPackHeadersFor(x, hdrCode, |
uci1 | 84:80b15993944e | 100 | b, timeout_clock); |
uci1 | 84:80b15993944e | 101 | x.WriteTo(b); |
uci1 | 84:80b15993944e | 102 | const int32_t sendNow = (b - genBuf); |
uci1 | 84:80b15993944e | 103 | toBeSent += sendNow; |
uci1 | 84:80b15993944e | 104 | byteSent += CallSendAll(genBuf, sendNow, timeout_clock); |
uci1 | 84:80b15993944e | 105 | return b; |
uci1 | 84:80b15993944e | 106 | } |
uci1 | 84:80b15993944e | 107 | |
uci1 | 1:e392595b4b76 | 108 | |
uci1 | 3:24c5f0f50bf1 | 109 | protected: |
uci1 | 37:ff95e7070f26 | 110 | SnCommPeripheral* fComm; // the communication peripheral. deleted in dtor!! |
uci1 | 37:ff95e7070f26 | 111 | |
uci1 | 40:1324da35afd4 | 112 | virtual int32_t SendFileBlock(FILE* inf, |
uci1 | 40:1324da35afd4 | 113 | const uint8_t blockHeaderCode, |
uci1 | 40:1324da35afd4 | 114 | const uint32_t blockSize, |
uci1 | 40:1324da35afd4 | 115 | char* const genBuf, |
uci1 | 40:1324da35afd4 | 116 | int32_t& bytesToSend, |
uci1 | 40:1324da35afd4 | 117 | const uint32_t timeout); |
uci1 | 12:d472f9811262 | 118 | |
uci1 | 40:1324da35afd4 | 119 | virtual int32_t SendFileContents(FILE* inf, |
uci1 | 40:1324da35afd4 | 120 | const SnConfigFrame& curConf, |
uci1 | 40:1324da35afd4 | 121 | SnEventFrame& evt, |
uci1 | 40:1324da35afd4 | 122 | SnPowerFrame& pow, |
uci1 | 40:1324da35afd4 | 123 | char* const genBuf, |
uci1 | 40:1324da35afd4 | 124 | uint32_t nevts, |
uci1 | 40:1324da35afd4 | 125 | int32_t& bytesToBeSent, |
uci1 | 40:1324da35afd4 | 126 | const uint32_t timeout_clock); |
uci1 | 8:95a325df1f6b | 127 | |
uci1 | 37:ff95e7070f26 | 128 | public: |
uci1 | 37:ff95e7070f26 | 129 | SnCommWin(SnCommPeripheral* p); |
uci1 | 37:ff95e7070f26 | 130 | virtual ~SnCommWin(); |
uci1 | 12:d472f9811262 | 131 | |
uci1 | 37:ff95e7070f26 | 132 | // probably no need to overload |
uci1 | 40:1324da35afd4 | 133 | virtual bool TrySetSysTimeUnix(const uint32_t timeout, |
uci1 | 40:1324da35afd4 | 134 | uint32_t& prvTime, |
uci1 | 40:1324da35afd4 | 135 | uint32_t& setTime); |
uci1 | 37:ff95e7070f26 | 136 | virtual bool Connect(const uint32_t timeout); |
uci1 | 116:8099b754fbb4 | 137 | virtual bool CloseConn(const uint32_t timeout, |
uci1 | 116:8099b754fbb4 | 138 | char* const genBuf=0, |
uci1 | 116:8099b754fbb4 | 139 | const bool sendCloseSignal=false); |
uci1 | 40:1324da35afd4 | 140 | virtual bool PowerDown(const uint32_t timeout); |
uci1 | 1:e392595b4b76 | 141 | |
uci1 | 37:ff95e7070f26 | 142 | // optional overloads |
uci1 | 37:ff95e7070f26 | 143 | virtual void Set(const SnConfigFrame& conf) {} |
uci1 | 16:744ce85aede2 | 144 | |
uci1 | 37:ff95e7070f26 | 145 | // mandatory overloads |
uci1 | 37:ff95e7070f26 | 146 | virtual SnConfigFrame::EDatPackBit GetCommType() const=0; |
uci1 | 8:95a325df1f6b | 147 | |
uci1 | 84:80b15993944e | 148 | virtual ECommWinResult OpenWindow(const bool sendStatus, |
uci1 | 84:80b15993944e | 149 | const SnConfigFrame& conf, |
uci1 | 84:80b15993944e | 150 | const SnPowerFrame& pow, // com win power |
uci1 | 84:80b15993944e | 151 | const SnEventFrame& stEvent, |
uci1 | 84:80b15993944e | 152 | const uint16_t seq, |
uci1 | 116:8099b754fbb4 | 153 | // const float thmrate, |
uci1 | 116:8099b754fbb4 | 154 | // const float evtrate, |
uci1 | 116:8099b754fbb4 | 155 | const uint32_t numThmTrigs, |
uci1 | 116:8099b754fbb4 | 156 | const uint32_t numSavedEvts, |
uci1 | 116:8099b754fbb4 | 157 | const float seqlive, |
uci1 | 84:80b15993944e | 158 | const uint32_t powerOnTime, |
uci1 | 84:80b15993944e | 159 | const SnTempFrame& temper, // com win temp |
uci1 | 84:80b15993944e | 160 | char* const genBuf, |
uci1 | 84:80b15993944e | 161 | const uint32_t timeout_clock)=0; |
uci1 | 116:8099b754fbb4 | 162 | |
uci1 | 27:efc4d654b139 | 163 | virtual bool GetDeleteAllConfirmCode(const SnConfigFrame& conf, |
uci1 | 27:efc4d654b139 | 164 | const uint32_t length, |
uci1 | 27:efc4d654b139 | 165 | const uint32_t timeout, |
uci1 | 27:efc4d654b139 | 166 | char* const buf, |
uci1 | 27:efc4d654b139 | 167 | const uint32_t bsize); |
uci1 | 21:ce51bb0ba4a5 | 168 | virtual ECommWinResult WaitHandshake(const SnConfigFrame& conf, |
uci1 | 21:ce51bb0ba4a5 | 169 | const uint32_t timeout, |
uci1 | 3:24c5f0f50bf1 | 170 | char* const buf, |
uci1 | 6:6f002d202f59 | 171 | const uint32_t bsize, |
uci1 | 27:efc4d654b139 | 172 | uint8_t& hndShkCode, |
uci1 | 27:efc4d654b139 | 173 | uint32_t* hndShkLen=0); |
uci1 | 40:1324da35afd4 | 174 | |
uci1 | 40:1324da35afd4 | 175 | virtual ECommWinResult HandleHandshake(FILE* inf, const char* infn, |
uci1 | 40:1324da35afd4 | 176 | const SnConfigFrame& curConf, |
uci1 | 40:1324da35afd4 | 177 | SnEventFrame& evt, |
uci1 | 40:1324da35afd4 | 178 | SnPowerFrame& pow, |
uci1 | 40:1324da35afd4 | 179 | char* const genBuf, |
uci1 | 40:1324da35afd4 | 180 | const uint32_t bsize, |
uci1 | 40:1324da35afd4 | 181 | const uint32_t handshakeTimeout, |
uci1 | 40:1324da35afd4 | 182 | const uint8_t hndshk, |
uci1 | 40:1324da35afd4 | 183 | const uint32_t hndlen, |
uci1 | 40:1324da35afd4 | 184 | const uint32_t* nevts=0); |
uci1 | 40:1324da35afd4 | 185 | int32_t SendHndshkReq(char* const genBuf, |
uci1 | 40:1324da35afd4 | 186 | const uint32_t timeout_clock); |
uci1 | 3:24c5f0f50bf1 | 187 | |
uci1 | 41:d6f5e2f09e07 | 188 | ECommWinResult SendSignalStrength(char* const genBuf, |
uci1 | 41:d6f5e2f09e07 | 189 | SnSignalStrengthFrame& sigstr, |
uci1 | 41:d6f5e2f09e07 | 190 | const uint32_t timeout_clock); |
uci1 | 41:d6f5e2f09e07 | 191 | |
uci1 | 8:95a325df1f6b | 192 | ECommWinResult GetConfig(SnConfigFrame& conf, |
uci1 | 8:95a325df1f6b | 193 | const uint32_t timeOut, |
uci1 | 8:95a325df1f6b | 194 | char* const confBuf, |
uci1 | 8:95a325df1f6b | 195 | const uint32_t bsize); |
uci1 | 1:e392595b4b76 | 196 | |
uci1 | 8:95a325df1f6b | 197 | ECommWinResult SendStatus(const SnConfigFrame& conf, |
uci1 | 84:80b15993944e | 198 | const SnPowerFrame& pow, // com win power |
uci1 | 84:80b15993944e | 199 | const SnEventFrame& stEvent, |
uci1 | 84:80b15993944e | 200 | const uint16_t seq, |
uci1 | 116:8099b754fbb4 | 201 | // const float thmrate, |
uci1 | 116:8099b754fbb4 | 202 | // const float evtrate, |
uci1 | 116:8099b754fbb4 | 203 | const uint32_t numThmTrigs, |
uci1 | 116:8099b754fbb4 | 204 | const uint32_t numSavedEvts, |
uci1 | 116:8099b754fbb4 | 205 | const float seqlive, |
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 | virtual ECommWinResult SendStatusData(const SnConfigFrame& conf, |
uci1 | 98:ce72ef143b9b | 212 | const SnConfigFrame& stConf, |
uci1 | 84:80b15993944e | 213 | const SnClockSetFrame& stTrgStartClk, |
uci1 | 84:80b15993944e | 214 | const SnClockSetFrame& stTrgStopClk, |
uci1 | 84:80b15993944e | 215 | const SnPowerFrame& stPower, |
uci1 | 84:80b15993944e | 216 | const SnEventFrame& stEvent, |
uci1 | 84:80b15993944e | 217 | const SnTempFrame& stTemperature, |
uci1 | 84:80b15993944e | 218 | const SnHeartbeatFrame& stHeartbeat, |
uci1 | 84:80b15993944e | 219 | const bool stNewPower, |
uci1 | 84:80b15993944e | 220 | const bool stNewEvent, |
uci1 | 84:80b15993944e | 221 | const bool stNewHeartbeat, |
uci1 | 84:80b15993944e | 222 | const bool stNewTemperature, |
uci1 | 84:80b15993944e | 223 | char* const genBuf, |
uci1 | 84:80b15993944e | 224 | const uint32_t timeout_clock); |
uci1 | 84:80b15993944e | 225 | |
uci1 | 23:ccf39298f205 | 226 | SnCommWin::ECommWinResult SendString(const char* str, |
uci1 | 23:ccf39298f205 | 227 | const uint32_t timeout); |
uci1 | 23:ccf39298f205 | 228 | |
uci1 | 40:1324da35afd4 | 229 | int32_t SendFilename(const char* fn, |
uci1 | 40:1324da35afd4 | 230 | char* const genBuf, |
uci1 | 40:1324da35afd4 | 231 | int32_t& bytesToBeSent, |
uci1 | 40:1324da35afd4 | 232 | const uint32_t timeout_clock); |
uci1 | 6:6f002d202f59 | 233 | |
uci1 | 40:1324da35afd4 | 234 | ECommWinResult SendDataFromFile(FILE* inf, const char* infn, |
uci1 | 8:95a325df1f6b | 235 | const SnConfigFrame& curConf, |
uci1 | 8:95a325df1f6b | 236 | SnEventFrame& evt, |
uci1 | 8:95a325df1f6b | 237 | SnPowerFrame& pow, |
uci1 | 8:95a325df1f6b | 238 | char* const genBuf, |
uci1 | 8:95a325df1f6b | 239 | const uint32_t bsize, |
uci1 | 8:95a325df1f6b | 240 | const uint32_t nevts, |
uci1 | 8:95a325df1f6b | 241 | const uint32_t timeout_clock, |
uci1 | 40:1324da35afd4 | 242 | uint8_t* hndcode=0, |
uci1 | 40:1324da35afd4 | 243 | uint32_t* hndlen=0); |
uci1 | 6:6f002d202f59 | 244 | |
uci1 | 8:95a325df1f6b | 245 | ECommWinResult SendData(SnConfigFrame& conf, |
uci1 | 8:95a325df1f6b | 246 | SnEventFrame& evt, |
uci1 | 8:95a325df1f6b | 247 | SnPowerFrame& pow, |
uci1 | 8:95a325df1f6b | 248 | char* const genBuf, |
uci1 | 8:95a325df1f6b | 249 | const uint32_t bsize, |
uci1 | 40:1324da35afd4 | 250 | const uint32_t timeout); |
uci1 | 18:55f1581f2ee4 | 251 | |
uci1 | 56:0bba0ef15697 | 252 | // assume little endian |
uci1 | 56:0bba0ef15697 | 253 | static |
uci1 | 56:0bba0ef15697 | 254 | uint16_t GetMinSeqFrom(const uint32_t hndshk) { |
uci1 | 56:0bba0ef15697 | 255 | // left-most 2 bytes |
uci1 | 56:0bba0ef15697 | 256 | return (hndshk>>16u); |
uci1 | 56:0bba0ef15697 | 257 | } |
uci1 | 56:0bba0ef15697 | 258 | static |
uci1 | 56:0bba0ef15697 | 259 | uint16_t GetMaxSeqFrom(const uint32_t hndshk) { |
uci1 | 56:0bba0ef15697 | 260 | // right-most 2 bytes |
uci1 | 56:0bba0ef15697 | 261 | return (hndshk&0xFFFF); |
uci1 | 56:0bba0ef15697 | 262 | } |
uci1 | 56:0bba0ef15697 | 263 | |
uci1 | 1:e392595b4b76 | 264 | }; |
uci1 | 1:e392595b4b76 | 265 | |
uci1 | 1:e392595b4b76 | 266 | #endif // SN_SnCommWin |