Arianna autonomous DAQ firmware

Dependencies:   mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW

Committer:
uci1
Date:
Wed May 29 00:20:31 2013 +0000
Revision:
37:ff95e7070f26
Parent:
SnCommAfarNetIfTwitter.cpp@28:484943132bb0
Child:
40:1324da35afd4
SBD only and DEBUG enabled. Safety nets enabled. Protocol and Comms as separate packages. Compiles, but untested.

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 37:ff95e7070f26 28 dynamic_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 37:ff95e7070f26 57 int mlen = fComm->SendAll(post.c_str(), ps, timeout);
uci1 28:484943132bb0 58 if (ps==mlen) {
uci1 28:484943132bb0 59 // send content
uci1 28:484943132bb0 60 #ifdef DEBUG
uci1 28:484943132bb0 61 printf("%s\r\n",str);
uci1 28:484943132bb0 62 #endif
uci1 37:ff95e7070f26 63 mlen = fComm->SendAll(str, len, timeout);
uci1 28:484943132bb0 64 if (len==mlen) {
uci1 37:ff95e7070f26 65 mlen = fComm->SendAll("\r\n\r\n\r\n", 6u, timeout);
uci1 28:484943132bb0 66 if (6u==mlen) {
uci1 28:484943132bb0 67 res = SnCommWin::kOkMsgSent;
uci1 28:484943132bb0 68 }
uci1 28:484943132bb0 69 }
uci1 28:484943132bb0 70 }
uci1 28:484943132bb0 71
uci1 28:484943132bb0 72 return res;
uci1 28:484943132bb0 73 }
uci1 28:484943132bb0 74
uci1 37:ff95e7070f26 75 SnCommWin::ECommWinResult SnCommWinTwitter::Tweet(
uci1 28:484943132bb0 76 const SnConfigFrame& conf,
uci1 28:484943132bb0 77 const float thmrate,
uci1 28:484943132bb0 78 const float evtrate,
uci1 28:484943132bb0 79 char* genBuf,
uci1 28:484943132bb0 80 const uint32_t timeout) {
uci1 28:484943132bb0 81 #ifdef DEBUG
uci1 28:484943132bb0 82 printf("calling tweet with thmr=%g, evtr=%g, to=%u\r\n",
uci1 28:484943132bb0 83 thmrate, evtrate, timeout);
uci1 28:484943132bb0 84 #endif
uci1 28:484943132bb0 85 GetTweet(genBuf, conf, thmrate, evtrate);
uci1 28:484943132bb0 86 return SendTweet(genBuf, timeout);
uci1 28:484943132bb0 87 }
uci1 28:484943132bb0 88
uci1 37:ff95e7070f26 89 void SnCommWinTwitter::GetTweet(char* genBuf,
uci1 28:484943132bb0 90 const SnConfigFrame& conf,
uci1 28:484943132bb0 91 const float thmrate,
uci1 28:484943132bb0 92 const float evtrate) {
uci1 28:484943132bb0 93 const bool rok = (thmrate>0) && (evtrate>0);
uci1 28:484943132bb0 94 const int r = rok ? (rand() % 4) : (rand() % 3);
uci1 28:484943132bb0 95 char* b = genBuf;
uci1 28:484943132bb0 96 b += sprintf(b, "status=");
uci1 28:484943132bb0 97 if (r==0) {
uci1 28:484943132bb0 98 GetTimeTweet(b, conf);
uci1 28:484943132bb0 99 } else if (r==1) {
uci1 28:484943132bb0 100 GetRunTweet(b, conf);
uci1 28:484943132bb0 101 } else if (r==2) {
uci1 28:484943132bb0 102 GetBytesTweet(b, conf);
uci1 28:484943132bb0 103 } else if (r==3) {
uci1 28:484943132bb0 104 GetRatesTweet(b, conf, thmrate, evtrate);
uci1 28:484943132bb0 105 }
uci1 28:484943132bb0 106 }
uci1 28:484943132bb0 107
uci1 37:ff95e7070f26 108 void SnCommWinTwitter::GetTimeTweet(char* genBuf,
uci1 28:484943132bb0 109 const SnConfigFrame& conf) {
uci1 28:484943132bb0 110 time_t s = time(0);
uci1 28:484943132bb0 111 char* b = AppendMacId(genBuf, conf);
uci1 28:484943132bb0 112 b += sprintf(b, ": It's ");
uci1 28:484943132bb0 113 b += strftime(b, 32, "%H:%M:%S on %Y-%m-%d", localtime(&s));
uci1 28:484943132bb0 114 b += sprintf(b, " and I'm buried in snow.");
uci1 28:484943132bb0 115 }
uci1 28:484943132bb0 116
uci1 37:ff95e7070f26 117 void SnCommWinTwitter::GetRunTweet(char* genBuf,
uci1 28:484943132bb0 118 const SnConfigFrame& conf) {
uci1 28:484943132bb0 119 char* b = GetGreeting(genBuf, conf);
uci1 28:484943132bb0 120 b += sprintf(b, " I'm making run %u, sequence %hu. "
uci1 28:484943132bb0 121 "It's a little #cold.",
uci1 28:484943132bb0 122 conf.GetRun(), SnSDUtils::GetCurSeqNum());
uci1 28:484943132bb0 123 }
uci1 28:484943132bb0 124
uci1 37:ff95e7070f26 125 void SnCommWinTwitter::GetRatesTweet(char* genBuf,
uci1 28:484943132bb0 126 const SnConfigFrame& conf,
uci1 28:484943132bb0 127 const float thmrate,
uci1 28:484943132bb0 128 const float evtrate) {
uci1 28:484943132bb0 129 char* b = GetGreeting(genBuf, conf);
uci1 28:484943132bb0 130 b += sprintf(b, " I'm getting %2.2f #triggers and %2.2f "
uci1 28:484943132bb0 131 "#events per second!", thmrate, evtrate);
uci1 28:484943132bb0 132 }
uci1 28:484943132bb0 133
uci1 37:ff95e7070f26 134 void SnCommWinTwitter::GetBytesTweet(char* genBuf,
uci1 28:484943132bb0 135 const SnConfigFrame& conf) {
uci1 28:484943132bb0 136 char* b = GetGreeting(genBuf, conf);
uci1 28:484943132bb0 137 b += sprintf(b, " I've got %2.2f KB of #data in %u files in my #icebox.",
uci1 28:484943132bb0 138 SnStatusFrame::GetTotKbytes(), SnStatusFrame::GetNfiles());
uci1 28:484943132bb0 139 }
uci1 28:484943132bb0 140
uci1 37:ff95e7070f26 141 char* SnCommWinTwitter::GetGreeting(char* genBuf,
uci1 28:484943132bb0 142 const SnConfigFrame& conf) {
uci1 28:484943132bb0 143 const int r = rand() % 5;
uci1 28:484943132bb0 144 char* b = genBuf;
uci1 28:484943132bb0 145 if (r==0) {
uci1 28:484943132bb0 146 b = AppendMacId(b, conf);
uci1 28:484943132bb0 147 b += sprintf(b, " here.");
uci1 28:484943132bb0 148 } else if (r==1) {
uci1 28:484943132bb0 149 b += sprintf(b, "It's ");
uci1 28:484943132bb0 150 b = AppendMacId(b, conf);
uci1 28:484943132bb0 151 b += sprintf(b, ".");
uci1 28:484943132bb0 152 } else if (r==2) {
uci1 28:484943132bb0 153 b = AppendMacId(b, conf);
uci1 28:484943132bb0 154 b += sprintf(b, " reporting in!");
uci1 28:484943132bb0 155 } else if (r==3) {
uci1 28:484943132bb0 156 b += sprintf(b, "Hi, I'm ");
uci1 28:484943132bb0 157 b = AppendMacId(b, conf);
uci1 28:484943132bb0 158 b += sprintf(b,". You may remember me.");
uci1 28:484943132bb0 159 } else {
uci1 28:484943132bb0 160 b = AppendMacId(b, conf);
uci1 28:484943132bb0 161 b += sprintf(b, ". Again.");
uci1 28:484943132bb0 162 }
uci1 28:484943132bb0 163
uci1 28:484943132bb0 164 return b;
uci1 28:484943132bb0 165 }
uci1 28:484943132bb0 166
uci1 37:ff95e7070f26 167 char* SnCommWinTwitter::AppendMacId(char* genBuf,
uci1 28:484943132bb0 168 const SnConfigFrame& conf) {
uci1 28:484943132bb0 169 char* b = genBuf;
uci1 28:484943132bb0 170 b += sprintf(b, "%04llX", ((conf.GetMacAddress() >> 16) & 0xFFFF));
uci1 28:484943132bb0 171 return b;
uci1 28:484943132bb0 172 }
uci1 37:ff95e7070f26 173 /*
uci1 37:ff95e7070f26 174 bool SnCommWinTwitter::Connect(const uint32_t timeout) {
uci1 28:484943132bb0 175
uci1 28:484943132bb0 176 #ifdef DEBUG
uci1 28:484943132bb0 177 printf("SnCommAfarNetIf::Connect : bind\r\n");
uci1 28:484943132bb0 178 #endif
uci1 28:484943132bb0 179
uci1 28:484943132bb0 180 ip_addr_t rserv_n;
uci1 28:484943132bb0 181 inet_aton(fRserv.c_str(), &rserv_n);
uci1 28:484943132bb0 182 Host server( IpAddr(&rserv_n), fRport );
uci1 28:484943132bb0 183 //Host server( IpAddr(128,195,204,151), 6655 );
uci1 28:484943132bb0 184
uci1 28:484943132bb0 185 SockConnect(server, timeout);
uci1 28:484943132bb0 186
uci1 28:484943132bb0 187 // catch events
uci1 37:ff95e7070f26 188 fSock->setOnEvent(this, &SnCommWinTwitter::onTCPSocketEvent);
uci1 28:484943132bb0 189
uci1 28:484943132bb0 190 #ifdef DEBUG
uci1 28:484943132bb0 191 printf("SnCommAfarNetIf::Connect : tcp connect\r\n");
uci1 28:484943132bb0 192 #endif
uci1 28:484943132bb0 193 //fEthConnected = false;
uci1 28:484943132bb0 194 while ( (fEthConnected==false) && (IsTimedOut(timeout)==false) ) {
uci1 28:484943132bb0 195 Net::poll();
uci1 28:484943132bb0 196 if (fConnAborted) {
uci1 28:484943132bb0 197 fConnAborted = false;
uci1 28:484943132bb0 198 //NewSocket();
uci1 28:484943132bb0 199 SockConnect(server, timeout);
uci1 28:484943132bb0 200 }
uci1 28:484943132bb0 201 }
uci1 28:484943132bb0 202
uci1 28:484943132bb0 203 fEthWriteable = fEthConnected;
uci1 28:484943132bb0 204 return fEthConnected;
uci1 28:484943132bb0 205
uci1 28:484943132bb0 206 }
uci1 28:484943132bb0 207
uci1 37:ff95e7070f26 208 void SnCommWinTwitter::onTCPSocketEvent(TCPSocketEvent e) {
uci1 28:484943132bb0 209 SnCommAfarNetIf::onTCPSocketEvent(e);
uci1 28:484943132bb0 210 }
uci1 37:ff95e7070f26 211 */