Arianna station communication peripherals.

Dependents:   AutonomousDAQ AutonomousDAQ

Committer:
uci1
Date:
Wed Aug 08 21:00:41 2018 +0000
Revision:
10:29301aaa8c33
Parent:
9:1562f78c4d3c
Fixed EOL termination for SBD comms, replacing \r\n with \r (or something like that)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
uci1 0:26c9189e5924 1 #include "SnCommPeripheral.h"
uci1 0:26c9189e5924 2
uci1 0:26c9189e5924 3 #include <string>
uci1 0:26c9189e5924 4
uci1 0:26c9189e5924 5 #include "SnHeaderFrame.h"
uci1 0:26c9189e5924 6
uci1 0:26c9189e5924 7 bool SnCommPeripheral::IsTimedOut(const uint32_t timeout_clock) const {
uci1 4:8328c2972290 8 /* - don't handle the not obeying timeout like this; it seems too risky
uci1 4:8328c2972290 9 * instead, a long timeout will keep refreshing after each handshake
uci1 0:26c9189e5924 10 if (timeout_clock==0) {
uci1 0:26c9189e5924 11 // for not obeying timeout option
uci1 0:26c9189e5924 12 return false;
uci1 0:26c9189e5924 13 } else {
uci1 4:8328c2972290 14 */
uci1 4:8328c2972290 15 const uint32_t ct = time(0);
uci1 4:8328c2972290 16 if ( (ct==0) ||
uci1 4:8328c2972290 17 (fabs(static_cast<double>(timeout_clock-ct))>kSecsPerDay) ) {
uci1 4:8328c2972290 18 // clock problems!
uci1 4:8328c2972290 19 // timeout now. hope the clock problems
uci1 4:8328c2972290 20 // get fixed in the next comm window
uci1 4:8328c2972290 21 return true;
uci1 4:8328c2972290 22 } else {
uci1 4:8328c2972290 23 return (ct>timeout_clock);
uci1 0:26c9189e5924 24 }
uci1 4:8328c2972290 25 /*
uci1 4:8328c2972290 26 }
uci1 4:8328c2972290 27 */
uci1 0:26c9189e5924 28 }
uci1 0:26c9189e5924 29
uci1 0:26c9189e5924 30 bool SnCommPeripheral::SendString(const char* str,
uci1 0:26c9189e5924 31 const uint32_t timeout) {
uci1 0:26c9189e5924 32 #ifdef DEBUG
uci1 9:1562f78c4d3c 33 printf("SnCommPeripheral::SendString %s. to=%u, ct=%u\r\n",
uci1 9:1562f78c4d3c 34 str, timeout, time(0));
uci1 0:26c9189e5924 35 #endif
uci1 0:26c9189e5924 36 const size_t rlen = strlen(str);
uci1 9:1562f78c4d3c 37 const size_t slen = (rlen > kMaxStrLen) ? kMaxStrLen : rlen;
uci1 9:1562f78c4d3c 38 // write header to genBuf and send it
uci1 9:1562f78c4d3c 39 int32_t msiz = SnHeaderFrame::SizeOf();
uci1 9:1562f78c4d3c 40 static char hdrbuf[ SnHeaderFrame::kMaxSizeOf+1 ];
uci1 9:1562f78c4d3c 41 char* t = hdrbuf;
uci1 9:1562f78c4d3c 42 SnHeaderFrame::WriteTo(t, SnHeaderFrame::kStringCode, slen);
uci1 9:1562f78c4d3c 43 int32_t mlen = SendAll(hdrbuf, msiz, timeout);
uci1 9:1562f78c4d3c 44 // send the string
uci1 9:1562f78c4d3c 45 msiz += slen;
uci1 9:1562f78c4d3c 46 mlen += SendAll(str, slen, timeout);
uci1 9:1562f78c4d3c 47 mlen += FinishSending(timeout);
uci1 9:1562f78c4d3c 48 /*
uci1 9:1562f78c4d3c 49 const size_t rlen = strlen(str);
uci1 0:26c9189e5924 50 const size_t slen = rlen > kMaxStrLen ? kMaxStrLen : rlen;
uci1 0:26c9189e5924 51 const int msiz = slen+SnHeaderFrame::SizeOf();
uci1 0:26c9189e5924 52 char* const ts = new char[msiz];
uci1 0:26c9189e5924 53 char* t = ts;
uci1 0:26c9189e5924 54 SnHeaderFrame::WriteTo(t, SnHeaderFrame::kStringCode, slen);
uci1 0:26c9189e5924 55 strncpy(t, str, slen);
uci1 3:a7f72492f19e 56 int32_t mlen = SendAll(ts, msiz, timeout);
uci1 0:26c9189e5924 57 delete[] ts;
uci1 3:a7f72492f19e 58 mlen += FinishSending(timeout);
uci1 9:1562f78c4d3c 59 */
uci1 0:26c9189e5924 60 return (msiz==mlen);
uci1 0:26c9189e5924 61 }
uci1 0:26c9189e5924 62
uci1 0:26c9189e5924 63 void SnCommPeripheral::CapitalizeInPlace(std::string::iterator s,
uci1 0:26c9189e5924 64 const std::string::const_iterator send) {
uci1 0:26c9189e5924 65 static const char upd = 'a' - 'A'; // a>A
uci1 0:26c9189e5924 66 for (; s!=send; ++s) {
uci1 0:26c9189e5924 67 if ( ((*s)>='a') && ((*s)<='z') ) {
uci1 0:26c9189e5924 68 (*s) -= upd;
uci1 0:26c9189e5924 69 }
uci1 0:26c9189e5924 70 }
uci1 0:26c9189e5924 71 }