Arianna autonomous DAQ firmware
Dependencies: mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW
SnCommAfarNetIfTwitter.cpp@28:484943132bb0, 2012-11-24 (annotated)
- Committer:
- uci1
- Date:
- Sat Nov 24 06:38:43 2012 +0000
- Revision:
- 28:484943132bb0
Afar, SBD, twitter enabled. Debug disabled. Safety nets on config parameters. Patched up netif code to connect to multiple sockets. Add default mbed IP according to mac address.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
uci1 | 28:484943132bb0 | 1 | #include "SnCommAfarNetIfTwitter.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 | 28:484943132bb0 | 6 | |
uci1 | 28:484943132bb0 | 7 | #include "lwip/inet.h" |
uci1 | 28:484943132bb0 | 8 | |
uci1 | 28:484943132bb0 | 9 | //#define DEBUG |
uci1 | 28:484943132bb0 | 10 | |
uci1 | 28:484943132bb0 | 11 | #define MAX_TWEET_LEN 130 |
uci1 | 28:484943132bb0 | 12 | #define TWIT_PORT 80 |
uci1 | 28:484943132bb0 | 13 | const char* SnCommAfarNetIfTwitter::kTwitIP = "66.180.175.246"; // api.supertweet.net |
uci1 | 28:484943132bb0 | 14 | const char* SnCommAfarNetIfTwitter::kTwitUrl = "/1.1/statuses/update.json"; |
uci1 | 28:484943132bb0 | 15 | // these are for ariannatwittest (the test account) |
uci1 | 28:484943132bb0 | 16 | //const char* SnCommAfarNetIfTwitter::kCred = "ariannatwittest:st11amtice"; |
uci1 | 28:484943132bb0 | 17 | const char* SnCommAfarNetIfTwitter::kB64cred = "YXJpYW5uYXR3aXR0ZXN0OnN0MTFhbXRpY2U="; // base64 encoded credential |
uci1 | 28:484943132bb0 | 18 | // these are for arianna.icicle (the "real" account) |
uci1 | 28:484943132bb0 | 19 | //const char* SnCommAfarNetIfTwitter::kCred = "arianna.icicle:st11ibAmicetw"; |
uci1 | 28:484943132bb0 | 20 | //const char* SnCommAfarNetIfTwitter::kB64cred = "YXJpYW5uYWljaWNsZTpzdDExaWJBbWljZXR3"; // base64 encoded credential |
uci1 | 28:484943132bb0 | 21 | |
uci1 | 28:484943132bb0 | 22 | SnCommAfarNetIfTwitter::SnCommAfarNetIfTwitter(const SnConfigFrame& conf) : |
uci1 | 28:484943132bb0 | 23 | SnCommAfarNetIf(kTwitIP, TWIT_PORT, |
uci1 | 28:484943132bb0 | 24 | conf.GetMbedIP(), |
uci1 | 28:484943132bb0 | 25 | conf.GetMbedMask(), |
uci1 | 28:484943132bb0 | 26 | conf.GetMbedGate()) { |
uci1 | 28:484943132bb0 | 27 | // make the random number sequence unique for each station |
uci1 | 28:484943132bb0 | 28 | srand(conf.GetMacAddress() & 0xFFFFFFFF); |
uci1 | 28:484943132bb0 | 29 | } |
uci1 | 28:484943132bb0 | 30 | |
uci1 | 28:484943132bb0 | 31 | SnCommWin::ECommWinResult SnCommAfarNetIfTwitter::SendTweet(const char* str, |
uci1 | 28:484943132bb0 | 32 | const uint32_t timeout) { |
uci1 | 28:484943132bb0 | 33 | SnCommWin::ECommWinResult res = SnCommWin::kUndefFail; |
uci1 | 28:484943132bb0 | 34 | // first send headers |
uci1 | 28:484943132bb0 | 35 | const uint32_t slen = strlen(str); |
uci1 | 28:484943132bb0 | 36 | const uint32_t len = (slen>MAX_TWEET_LEN) ? MAX_TWEET_LEN : slen; |
uci1 | 28:484943132bb0 | 37 | char lenstr[16]; |
uci1 | 28:484943132bb0 | 38 | sprintf(lenstr, "%u", len); |
uci1 | 28:484943132bb0 | 39 | std::string post( |
uci1 | 28:484943132bb0 | 40 | "POST /1.1/statuses/update.json HTTP/1.0\r\n"\ |
uci1 | 28:484943132bb0 | 41 | "HOST: api.supertweet.net\r\n"\ |
uci1 | 28:484943132bb0 | 42 | "Authorization: Basic "); |
uci1 | 28:484943132bb0 | 43 | post += kB64cred; |
uci1 | 28:484943132bb0 | 44 | post += "\r\n"\ |
uci1 | 28:484943132bb0 | 45 | "Content-length: "; |
uci1 | 28:484943132bb0 | 46 | post += lenstr; |
uci1 | 28:484943132bb0 | 47 | post += "\r\n"\ |
uci1 | 28:484943132bb0 | 48 | "Content-Type: application/x-www-form-urlencoded\r\n"\ |
uci1 | 28:484943132bb0 | 49 | "\r\n"; |
uci1 | 28:484943132bb0 | 50 | #ifdef DEBUG |
uci1 | 28:484943132bb0 | 51 | printf("sending tweet:\r\n"); |
uci1 | 28:484943132bb0 | 52 | printf("%s",post.c_str()); |
uci1 | 28:484943132bb0 | 53 | #endif |
uci1 | 28:484943132bb0 | 54 | const uint32_t ps = post.size(); |
uci1 | 28:484943132bb0 | 55 | int mlen = SendAll(post.c_str(), ps, timeout); |
uci1 | 28:484943132bb0 | 56 | if (ps==mlen) { |
uci1 | 28:484943132bb0 | 57 | // send content |
uci1 | 28:484943132bb0 | 58 | #ifdef DEBUG |
uci1 | 28:484943132bb0 | 59 | printf("%s\r\n",str); |
uci1 | 28:484943132bb0 | 60 | #endif |
uci1 | 28:484943132bb0 | 61 | mlen = SendAll(str, len, timeout); |
uci1 | 28:484943132bb0 | 62 | if (len==mlen) { |
uci1 | 28:484943132bb0 | 63 | mlen = SendAll("\r\n\r\n\r\n", 6u, timeout); |
uci1 | 28:484943132bb0 | 64 | if (6u==mlen) { |
uci1 | 28:484943132bb0 | 65 | res = SnCommWin::kOkMsgSent; |
uci1 | 28:484943132bb0 | 66 | } |
uci1 | 28:484943132bb0 | 67 | } |
uci1 | 28:484943132bb0 | 68 | } |
uci1 | 28:484943132bb0 | 69 | |
uci1 | 28:484943132bb0 | 70 | return res; |
uci1 | 28:484943132bb0 | 71 | } |
uci1 | 28:484943132bb0 | 72 | |
uci1 | 28:484943132bb0 | 73 | SnCommWin::ECommWinResult SnCommAfarNetIfTwitter::Tweet( |
uci1 | 28:484943132bb0 | 74 | const SnConfigFrame& conf, |
uci1 | 28:484943132bb0 | 75 | const float thmrate, |
uci1 | 28:484943132bb0 | 76 | const float evtrate, |
uci1 | 28:484943132bb0 | 77 | char* genBuf, |
uci1 | 28:484943132bb0 | 78 | const uint32_t timeout) { |
uci1 | 28:484943132bb0 | 79 | #ifdef DEBUG |
uci1 | 28:484943132bb0 | 80 | printf("calling tweet with thmr=%g, evtr=%g, to=%u\r\n", |
uci1 | 28:484943132bb0 | 81 | thmrate, evtrate, timeout); |
uci1 | 28:484943132bb0 | 82 | #endif |
uci1 | 28:484943132bb0 | 83 | GetTweet(genBuf, conf, thmrate, evtrate); |
uci1 | 28:484943132bb0 | 84 | return SendTweet(genBuf, timeout); |
uci1 | 28:484943132bb0 | 85 | } |
uci1 | 28:484943132bb0 | 86 | |
uci1 | 28:484943132bb0 | 87 | void SnCommAfarNetIfTwitter::GetTweet(char* genBuf, |
uci1 | 28:484943132bb0 | 88 | const SnConfigFrame& conf, |
uci1 | 28:484943132bb0 | 89 | const float thmrate, |
uci1 | 28:484943132bb0 | 90 | const float evtrate) { |
uci1 | 28:484943132bb0 | 91 | const bool rok = (thmrate>0) && (evtrate>0); |
uci1 | 28:484943132bb0 | 92 | const int r = rok ? (rand() % 4) : (rand() % 3); |
uci1 | 28:484943132bb0 | 93 | char* b = genBuf; |
uci1 | 28:484943132bb0 | 94 | b += sprintf(b, "status="); |
uci1 | 28:484943132bb0 | 95 | if (r==0) { |
uci1 | 28:484943132bb0 | 96 | GetTimeTweet(b, conf); |
uci1 | 28:484943132bb0 | 97 | } else if (r==1) { |
uci1 | 28:484943132bb0 | 98 | GetRunTweet(b, conf); |
uci1 | 28:484943132bb0 | 99 | } else if (r==2) { |
uci1 | 28:484943132bb0 | 100 | GetBytesTweet(b, conf); |
uci1 | 28:484943132bb0 | 101 | } else if (r==3) { |
uci1 | 28:484943132bb0 | 102 | GetRatesTweet(b, conf, thmrate, evtrate); |
uci1 | 28:484943132bb0 | 103 | } |
uci1 | 28:484943132bb0 | 104 | } |
uci1 | 28:484943132bb0 | 105 | |
uci1 | 28:484943132bb0 | 106 | void SnCommAfarNetIfTwitter::GetTimeTweet(char* genBuf, |
uci1 | 28:484943132bb0 | 107 | const SnConfigFrame& conf) { |
uci1 | 28:484943132bb0 | 108 | time_t s = time(0); |
uci1 | 28:484943132bb0 | 109 | char* b = AppendMacId(genBuf, conf); |
uci1 | 28:484943132bb0 | 110 | b += sprintf(b, ": It's "); |
uci1 | 28:484943132bb0 | 111 | b += strftime(b, 32, "%H:%M:%S on %Y-%m-%d", localtime(&s)); |
uci1 | 28:484943132bb0 | 112 | b += sprintf(b, " and I'm buried in snow."); |
uci1 | 28:484943132bb0 | 113 | } |
uci1 | 28:484943132bb0 | 114 | |
uci1 | 28:484943132bb0 | 115 | void SnCommAfarNetIfTwitter::GetRunTweet(char* genBuf, |
uci1 | 28:484943132bb0 | 116 | const SnConfigFrame& conf) { |
uci1 | 28:484943132bb0 | 117 | char* b = GetGreeting(genBuf, conf); |
uci1 | 28:484943132bb0 | 118 | b += sprintf(b, " I'm making run %u, sequence %hu. " |
uci1 | 28:484943132bb0 | 119 | "It's a little #cold.", |
uci1 | 28:484943132bb0 | 120 | conf.GetRun(), SnSDUtils::GetCurSeqNum()); |
uci1 | 28:484943132bb0 | 121 | } |
uci1 | 28:484943132bb0 | 122 | |
uci1 | 28:484943132bb0 | 123 | void SnCommAfarNetIfTwitter::GetRatesTweet(char* genBuf, |
uci1 | 28:484943132bb0 | 124 | const SnConfigFrame& conf, |
uci1 | 28:484943132bb0 | 125 | const float thmrate, |
uci1 | 28:484943132bb0 | 126 | const float evtrate) { |
uci1 | 28:484943132bb0 | 127 | char* b = GetGreeting(genBuf, conf); |
uci1 | 28:484943132bb0 | 128 | b += sprintf(b, " I'm getting %2.2f #triggers and %2.2f " |
uci1 | 28:484943132bb0 | 129 | "#events per second!", thmrate, evtrate); |
uci1 | 28:484943132bb0 | 130 | } |
uci1 | 28:484943132bb0 | 131 | |
uci1 | 28:484943132bb0 | 132 | void SnCommAfarNetIfTwitter::GetBytesTweet(char* genBuf, |
uci1 | 28:484943132bb0 | 133 | const SnConfigFrame& conf) { |
uci1 | 28:484943132bb0 | 134 | char* b = GetGreeting(genBuf, conf); |
uci1 | 28:484943132bb0 | 135 | b += sprintf(b, " I've got %2.2f KB of #data in %u files in my #icebox.", |
uci1 | 28:484943132bb0 | 136 | SnStatusFrame::GetTotKbytes(), SnStatusFrame::GetNfiles()); |
uci1 | 28:484943132bb0 | 137 | } |
uci1 | 28:484943132bb0 | 138 | |
uci1 | 28:484943132bb0 | 139 | char* SnCommAfarNetIfTwitter::GetGreeting(char* genBuf, |
uci1 | 28:484943132bb0 | 140 | const SnConfigFrame& conf) { |
uci1 | 28:484943132bb0 | 141 | const int r = rand() % 5; |
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 | 28:484943132bb0 | 152 | b += sprintf(b, " reporting in!"); |
uci1 | 28:484943132bb0 | 153 | } else if (r==3) { |
uci1 | 28:484943132bb0 | 154 | b += sprintf(b, "Hi, I'm "); |
uci1 | 28:484943132bb0 | 155 | b = AppendMacId(b, conf); |
uci1 | 28:484943132bb0 | 156 | b += sprintf(b,". You may remember me."); |
uci1 | 28:484943132bb0 | 157 | } else { |
uci1 | 28:484943132bb0 | 158 | b = AppendMacId(b, conf); |
uci1 | 28:484943132bb0 | 159 | b += sprintf(b, ". Again."); |
uci1 | 28:484943132bb0 | 160 | } |
uci1 | 28:484943132bb0 | 161 | |
uci1 | 28:484943132bb0 | 162 | return b; |
uci1 | 28:484943132bb0 | 163 | } |
uci1 | 28:484943132bb0 | 164 | |
uci1 | 28:484943132bb0 | 165 | char* SnCommAfarNetIfTwitter::AppendMacId(char* genBuf, |
uci1 | 28:484943132bb0 | 166 | const SnConfigFrame& conf) { |
uci1 | 28:484943132bb0 | 167 | char* b = genBuf; |
uci1 | 28:484943132bb0 | 168 | b += sprintf(b, "%04llX", ((conf.GetMacAddress() >> 16) & 0xFFFF)); |
uci1 | 28:484943132bb0 | 169 | return b; |
uci1 | 28:484943132bb0 | 170 | } |
uci1 | 28:484943132bb0 | 171 | |
uci1 | 28:484943132bb0 | 172 | bool SnCommAfarNetIfTwitter::Connect(const uint32_t timeout) { |
uci1 | 28:484943132bb0 | 173 | |
uci1 | 28:484943132bb0 | 174 | #ifdef DEBUG |
uci1 | 28:484943132bb0 | 175 | printf("SnCommAfarNetIf::Connect : bind\r\n"); |
uci1 | 28:484943132bb0 | 176 | #endif |
uci1 | 28:484943132bb0 | 177 | |
uci1 | 28:484943132bb0 | 178 | ip_addr_t rserv_n; |
uci1 | 28:484943132bb0 | 179 | inet_aton(fRserv.c_str(), &rserv_n); |
uci1 | 28:484943132bb0 | 180 | Host server( IpAddr(&rserv_n), fRport ); |
uci1 | 28:484943132bb0 | 181 | //Host server( IpAddr(128,195,204,151), 6655 ); |
uci1 | 28:484943132bb0 | 182 | |
uci1 | 28:484943132bb0 | 183 | SockConnect(server, timeout); |
uci1 | 28:484943132bb0 | 184 | |
uci1 | 28:484943132bb0 | 185 | // catch events |
uci1 | 28:484943132bb0 | 186 | fSock->setOnEvent(this, &SnCommAfarNetIfTwitter::onTCPSocketEvent); |
uci1 | 28:484943132bb0 | 187 | |
uci1 | 28:484943132bb0 | 188 | #ifdef DEBUG |
uci1 | 28:484943132bb0 | 189 | printf("SnCommAfarNetIf::Connect : tcp connect\r\n"); |
uci1 | 28:484943132bb0 | 190 | #endif |
uci1 | 28:484943132bb0 | 191 | //fEthConnected = false; |
uci1 | 28:484943132bb0 | 192 | while ( (fEthConnected==false) && (IsTimedOut(timeout)==false) ) { |
uci1 | 28:484943132bb0 | 193 | Net::poll(); |
uci1 | 28:484943132bb0 | 194 | if (fConnAborted) { |
uci1 | 28:484943132bb0 | 195 | fConnAborted = false; |
uci1 | 28:484943132bb0 | 196 | //NewSocket(); |
uci1 | 28:484943132bb0 | 197 | SockConnect(server, timeout); |
uci1 | 28:484943132bb0 | 198 | } |
uci1 | 28:484943132bb0 | 199 | } |
uci1 | 28:484943132bb0 | 200 | |
uci1 | 28:484943132bb0 | 201 | fEthWriteable = fEthConnected; |
uci1 | 28:484943132bb0 | 202 | return fEthConnected; |
uci1 | 28:484943132bb0 | 203 | |
uci1 | 28:484943132bb0 | 204 | } |
uci1 | 28:484943132bb0 | 205 | |
uci1 | 28:484943132bb0 | 206 | void SnCommAfarNetIfTwitter::onTCPSocketEvent(TCPSocketEvent e) { |
uci1 | 28:484943132bb0 | 207 | SnCommAfarNetIf::onTCPSocketEvent(e); |
uci1 | 28:484943132bb0 | 208 | } |