Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Committer:
uci1
Date:
Wed Jun 05 17:29:31 2019 +0000
Revision:
125:ce4045184366
Parent:
41:d6f5e2f09e07
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?

UserRevisionLine numberNew contents of line
uci1 37:ff95e7070f26 1 #include "SnCommWinTwitter.h"
uci1 28:484943132bb0 2
uci1 28:484943132bb0 3 #include "SnSDUtils.h"
uci1 28:484943132bb0 4 #include "SnConfigFrame.h"
uci1 28:484943132bb0 5 #include "SnStatusFrame.h"
uci1 37:ff95e7070f26 6 #include "SnCommAfarNetIf.h"
uci1 28:484943132bb0 7
uci1 28:484943132bb0 8 //#define DEBUG
uci1 28:484943132bb0 9
uci1 28:484943132bb0 10 #define MAX_TWEET_LEN 130
uci1 28:484943132bb0 11 #define TWIT_PORT 80
uci1 37:ff95e7070f26 12 const char* SnCommWinTwitter::kTwitIP = "66.180.175.246"; // api.supertweet.net
uci1 37:ff95e7070f26 13 const char* SnCommWinTwitter::kTwitUrl = "/1.1/statuses/update.json";
uci1 28:484943132bb0 14 // these are for ariannatwittest (the test account)
uci1 37:ff95e7070f26 15 //const char* SnCommWinTwitter::kCred = "ariannatwittest:st11amtice";
uci1 37:ff95e7070f26 16 const char* SnCommWinTwitter::kB64cred = "YXJpYW5uYXR3aXR0ZXN0OnN0MTFhbXRpY2U="; // base64 encoded credential
uci1 28:484943132bb0 17 // these are for arianna.icicle (the "real" account)
uci1 37:ff95e7070f26 18 //const char* SnCommWinTwitter::kCred = "arianna.icicle:st11ibAmicetw";
uci1 37:ff95e7070f26 19 //const char* SnCommWinTwitter::kB64cred = "YXJpYW5uYWljaWNsZTpzdDExaWJBbWljZXR3"; // base64 encoded credential
uci1 28:484943132bb0 20
uci1 37:ff95e7070f26 21 SnCommWinTwitter::SnCommWinTwitter(const SnConfigFrame& conf) :
uci1 37:ff95e7070f26 22 SnCommWinAfar(new SnCommAfarNetIf(kTwitIP, TWIT_PORT,
uci1 37:ff95e7070f26 23 conf.GetMbedIP(),
uci1 37:ff95e7070f26 24 conf.GetMbedMask(),
uci1 37:ff95e7070f26 25 conf.GetMbedGate())) {
uci1 37:ff95e7070f26 26 // prevent this second NetIf from re-initializing the ethernet,
uci1 37:ff95e7070f26 27 // which would cause a crash
uci1 40:1324da35afd4 28 static_cast<SnCommAfarNetIf*>(fComm)->SetEthSetup(true);
uci1 28:484943132bb0 29 // make the random number sequence unique for each station
uci1 28:484943132bb0 30 srand(conf.GetMacAddress() & 0xFFFFFFFF);
uci1 28:484943132bb0 31 }
uci1 28:484943132bb0 32
uci1 37:ff95e7070f26 33 SnCommWin::ECommWinResult SnCommWinTwitter::SendTweet(const char* str,
uci1 28:484943132bb0 34 const uint32_t timeout) {
uci1 28:484943132bb0 35 SnCommWin::ECommWinResult res = SnCommWin::kUndefFail;
uci1 28:484943132bb0 36 // first send headers
uci1 28:484943132bb0 37 const uint32_t slen = strlen(str);
uci1 28:484943132bb0 38 const uint32_t len = (slen>MAX_TWEET_LEN) ? MAX_TWEET_LEN : slen;
uci1 28:484943132bb0 39 char lenstr[16];
uci1 28:484943132bb0 40 sprintf(lenstr, "%u", len);
uci1 28:484943132bb0 41 std::string post(
uci1 28:484943132bb0 42 "POST /1.1/statuses/update.json HTTP/1.0\r\n"\
uci1 28:484943132bb0 43 "HOST: api.supertweet.net\r\n"\
uci1 28:484943132bb0 44 "Authorization: Basic ");
uci1 28:484943132bb0 45 post += kB64cred;
uci1 28:484943132bb0 46 post += "\r\n"\
uci1 28:484943132bb0 47 "Content-length: ";
uci1 28:484943132bb0 48 post += lenstr;
uci1 28:484943132bb0 49 post += "\r\n"\
uci1 28:484943132bb0 50 "Content-Type: application/x-www-form-urlencoded\r\n"\
uci1 28:484943132bb0 51 "\r\n";
uci1 28:484943132bb0 52 #ifdef DEBUG
uci1 28:484943132bb0 53 printf("sending tweet:\r\n");
uci1 28:484943132bb0 54 printf("%s",post.c_str());
uci1 28:484943132bb0 55 #endif
uci1 28:484943132bb0 56 const uint32_t ps = post.size();
uci1 40:1324da35afd4 57 int32_t mlen = fComm->SendAll(post.c_str(), ps, timeout);
uci1 40:1324da35afd4 58 mlen += fComm->FinishSending(timeout);
uci1 28:484943132bb0 59 if (ps==mlen) {
uci1 28:484943132bb0 60 // send content
uci1 28:484943132bb0 61 #ifdef DEBUG
uci1 28:484943132bb0 62 printf("%s\r\n",str);
uci1 28:484943132bb0 63 #endif
uci1 40:1324da35afd4 64 mlen = fComm->SendAll(str, len, timeout);
uci1 40:1324da35afd4 65 mlen += fComm->SendAll("\r\n\r\n\r\n", 6u, timeout);
uci1 40:1324da35afd4 66 mlen += fComm->FinishSending(timeout);
uci1 40:1324da35afd4 67 if ((6u+len)==mlen) {
uci1 40:1324da35afd4 68 res = SnCommWin::kOkMsgSent;
uci1 28:484943132bb0 69 }
uci1 28:484943132bb0 70 }
uci1 28:484943132bb0 71 return res;
uci1 28:484943132bb0 72 }
uci1 28:484943132bb0 73
uci1 37:ff95e7070f26 74 SnCommWin::ECommWinResult SnCommWinTwitter::Tweet(
uci1 28:484943132bb0 75 const SnConfigFrame& conf,
uci1 28:484943132bb0 76 const float thmrate,
uci1 28:484943132bb0 77 const float evtrate,
uci1 28:484943132bb0 78 char* genBuf,
uci1 28:484943132bb0 79 const uint32_t timeout) {
uci1 28:484943132bb0 80 #ifdef DEBUG
uci1 28:484943132bb0 81 printf("calling tweet with thmr=%g, evtr=%g, to=%u\r\n",
uci1 28:484943132bb0 82 thmrate, evtrate, timeout);
uci1 28:484943132bb0 83 #endif
uci1 28:484943132bb0 84 GetTweet(genBuf, conf, thmrate, evtrate);
uci1 28:484943132bb0 85 return SendTweet(genBuf, timeout);
uci1 28:484943132bb0 86 }
uci1 28:484943132bb0 87
uci1 37:ff95e7070f26 88 void SnCommWinTwitter::GetTweet(char* genBuf,
uci1 40:1324da35afd4 89 const SnConfigFrame& conf,
uci1 40:1324da35afd4 90 const float thmrate,
uci1 40:1324da35afd4 91 const float evtrate) {
uci1 28:484943132bb0 92 const bool rok = (thmrate>0) && (evtrate>0);
uci1 28:484943132bb0 93 const int r = rok ? (rand() % 4) : (rand() % 3);
uci1 28:484943132bb0 94 char* b = genBuf;
uci1 28:484943132bb0 95 b += sprintf(b, "status=");
uci1 28:484943132bb0 96 if (r==0) {
uci1 28:484943132bb0 97 GetTimeTweet(b, conf);
uci1 28:484943132bb0 98 } else if (r==1) {
uci1 28:484943132bb0 99 GetRunTweet(b, conf);
uci1 28:484943132bb0 100 } else if (r==2) {
uci1 28:484943132bb0 101 GetBytesTweet(b, conf);
uci1 28:484943132bb0 102 } else if (r==3) {
uci1 28:484943132bb0 103 GetRatesTweet(b, conf, thmrate, evtrate);
uci1 28:484943132bb0 104 }
uci1 28:484943132bb0 105 }
uci1 28:484943132bb0 106
uci1 37:ff95e7070f26 107 void SnCommWinTwitter::GetTimeTweet(char* genBuf,
uci1 40:1324da35afd4 108 const SnConfigFrame& conf) {
uci1 28:484943132bb0 109 time_t s = time(0);
uci1 28:484943132bb0 110 char* b = AppendMacId(genBuf, conf);
uci1 28:484943132bb0 111 b += sprintf(b, ": It's ");
uci1 28:484943132bb0 112 b += strftime(b, 32, "%H:%M:%S on %Y-%m-%d", localtime(&s));
uci1 40:1324da35afd4 113 b += sprintf(b, " in the ice.");
uci1 28:484943132bb0 114 }
uci1 28:484943132bb0 115
uci1 37:ff95e7070f26 116 void SnCommWinTwitter::GetRunTweet(char* genBuf,
uci1 40:1324da35afd4 117 const SnConfigFrame& conf) {
uci1 28:484943132bb0 118 char* b = GetGreeting(genBuf, conf);
uci1 40:1324da35afd4 119 b += sprintf(b, " I'm making run %u, sequence %hu.",
uci1 28:484943132bb0 120 conf.GetRun(), SnSDUtils::GetCurSeqNum());
uci1 28:484943132bb0 121 }
uci1 28:484943132bb0 122
uci1 37:ff95e7070f26 123 void SnCommWinTwitter::GetRatesTweet(char* genBuf,
uci1 40:1324da35afd4 124 const SnConfigFrame& conf,
uci1 40:1324da35afd4 125 const float thmrate,
uci1 40:1324da35afd4 126 const float evtrate) {
uci1 28:484943132bb0 127 char* b = GetGreeting(genBuf, conf);
uci1 40:1324da35afd4 128 b += sprintf(b, " I'm getting %2.2f triggers and %2.2f "
uci1 40:1324da35afd4 129 "events per second!", thmrate, evtrate);
uci1 28:484943132bb0 130 }
uci1 28:484943132bb0 131
uci1 37:ff95e7070f26 132 void SnCommWinTwitter::GetBytesTweet(char* genBuf,
uci1 40:1324da35afd4 133 const SnConfigFrame& conf) {
uci1 28:484943132bb0 134 char* b = GetGreeting(genBuf, conf);
uci1 40:1324da35afd4 135 b += sprintf(b, " I've got %2.2f MB of disk space left.",
uci1 40:1324da35afd4 136 SnStatusFrame::GetFreeMB());
uci1 28:484943132bb0 137 }
uci1 28:484943132bb0 138
uci1 37:ff95e7070f26 139 char* SnCommWinTwitter::GetGreeting(char* genBuf,
uci1 40:1324da35afd4 140 const SnConfigFrame& conf) {
uci1 40:1324da35afd4 141 const int r = rand() % 4;
uci1 28:484943132bb0 142 char* b = genBuf;
uci1 28:484943132bb0 143 if (r==0) {
uci1 28:484943132bb0 144 b = AppendMacId(b, conf);
uci1 28:484943132bb0 145 b += sprintf(b, " here.");
uci1 28:484943132bb0 146 } else if (r==1) {
uci1 28:484943132bb0 147 b += sprintf(b, "It's ");
uci1 28:484943132bb0 148 b = AppendMacId(b, conf);
uci1 28:484943132bb0 149 b += sprintf(b, ".");
uci1 28:484943132bb0 150 } else if (r==2) {
uci1 28:484943132bb0 151 b = AppendMacId(b, conf);
uci1 40:1324da35afd4 152 b += sprintf(b, " reporting in.");
uci1 28:484943132bb0 153 } else {
uci1 28:484943132bb0 154 b = AppendMacId(b, conf);
uci1 40:1324da35afd4 155 b += sprintf(b, " again.");
uci1 28:484943132bb0 156 }
uci1 28:484943132bb0 157
uci1 28:484943132bb0 158 return b;
uci1 28:484943132bb0 159 }
uci1 28:484943132bb0 160
uci1 37:ff95e7070f26 161 char* SnCommWinTwitter::AppendMacId(char* genBuf,
uci1 40:1324da35afd4 162 const SnConfigFrame& conf) {
uci1 28:484943132bb0 163 char* b = genBuf;
uci1 28:484943132bb0 164 b += sprintf(b, "%04llX", ((conf.GetMacAddress() >> 16) & 0xFFFF));
uci1 28:484943132bb0 165 return b;
uci1 28:484943132bb0 166 }
uci1 37:ff95e7070f26 167 /*
uci1 37:ff95e7070f26 168 bool SnCommWinTwitter::Connect(const uint32_t timeout) {
uci1 28:484943132bb0 169
uci1 28:484943132bb0 170 #ifdef DEBUG
uci1 28:484943132bb0 171 printf("SnCommAfarNetIf::Connect : bind\r\n");
uci1 28:484943132bb0 172 #endif
uci1 28:484943132bb0 173
uci1 28:484943132bb0 174 ip_addr_t rserv_n;
uci1 28:484943132bb0 175 inet_aton(fRserv.c_str(), &rserv_n);
uci1 28:484943132bb0 176 Host server( IpAddr(&rserv_n), fRport );
uci1 28:484943132bb0 177 //Host server( IpAddr(128,195,204,151), 6655 );
uci1 28:484943132bb0 178
uci1 28:484943132bb0 179 SockConnect(server, timeout);
uci1 28:484943132bb0 180
uci1 28:484943132bb0 181 // catch events
uci1 37:ff95e7070f26 182 fSock->setOnEvent(this, &SnCommWinTwitter::onTCPSocketEvent);
uci1 28:484943132bb0 183
uci1 28:484943132bb0 184 #ifdef DEBUG
uci1 28:484943132bb0 185 printf("SnCommAfarNetIf::Connect : tcp connect\r\n");
uci1 28:484943132bb0 186 #endif
uci1 28:484943132bb0 187 //fEthConnected = false;
uci1 28:484943132bb0 188 while ( (fEthConnected==false) && (IsTimedOut(timeout)==false) ) {
uci1 28:484943132bb0 189 Net::poll();
uci1 28:484943132bb0 190 if (fConnAborted) {
uci1 28:484943132bb0 191 fConnAborted = false;
uci1 28:484943132bb0 192 //NewSocket();
uci1 28:484943132bb0 193 SockConnect(server, timeout);
uci1 28:484943132bb0 194 }
uci1 28:484943132bb0 195 }
uci1 28:484943132bb0 196
uci1 28:484943132bb0 197 fEthWriteable = fEthConnected;
uci1 28:484943132bb0 198 return fEthConnected;
uci1 28:484943132bb0 199
uci1 28:484943132bb0 200 }
uci1 28:484943132bb0 201
uci1 37:ff95e7070f26 202 void SnCommWinTwitter::onTCPSocketEvent(TCPSocketEvent e) {
uci1 28:484943132bb0 203 SnCommAfarNetIf::onTCPSocketEvent(e);
uci1 28:484943132bb0 204 }
uci1 41:d6f5e2f09e07 205 */