http://mbed.org/users/okini3939/notebook/comet_websocket/

Dependencies:   EthernetNetIf mbed RingBuffer MbedJSONValue

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CometClient.cpp Source File

CometClient.cpp

Go to the documentation of this file.
00001 /*
00002  * mbed pseudo Comet HTTP Client
00003  * Copyright (c) 2011 Hiroshi Suga
00004  * Released under the MIT License: http://mbed.org/license/mit
00005  */
00006 
00007 /** @file
00008  * @brief pseudo Comet HTTP Client
00009  */
00010 
00011 #include "mbed.h"
00012 #include "EthernetNetIf.h"
00013 #include "TCPSocket.h"
00014 #include "DNSRequest.h"
00015 #include "RingBuffer.h"
00016 #include "CometClient.h"
00017 #include <ctype.h>
00018 
00019 #define DEBUG
00020 #include "dbg.h"
00021 
00022 
00023 static TCPSocket *http;
00024 static volatile int tcp_ready = 0, tcp_readable = 0, tcp_writable = 0;
00025 static volatile int tcp_busy = 0, mode = 0;
00026 static volatile int dns_status = 0;
00027 static Host remote;
00028 static RingBuffer fifo(1500);
00029 static char remoteuri[100];
00030 
00031 
00032 // Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
00033 int base64enc(const char *input, unsigned int length, char *output, int len) {
00034   static const char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
00035   unsigned int c, c1, c2, c3;
00036 
00037   if (len < ((((length-1)/3)+1)<<2)) return -1;
00038   for(unsigned int i = 0, j = 0; i<length; i+=3,j+=4) {
00039     c1 = ((((unsigned char)*((unsigned char *)&input[i]))));
00040     c2 = (length>i+1)?((((unsigned char)*((unsigned char *)&input[i+1])))):0;
00041     c3 = (length>i+2)?((((unsigned char)*((unsigned char *)&input[i+2])))):0;
00042 
00043     c = ((c1 & 0xFC) >> 2);
00044     output[j+0] = base64[c];
00045     c = ((c1 & 0x03) << 4) | ((c2 & 0xF0) >> 4);
00046     output[j+1] = base64[c];
00047     c = ((c2 & 0x0F) << 2) | ((c3 & 0xC0) >> 6);
00048     output[j+2] = (length>i+1)?base64[c]:'=';
00049     c = (c3 & 0x3F);
00050     output[j+3] = (length>i+2)?base64[c]:'=';
00051   }
00052   output[(((length-1)/3)+1)<<2] = '\0';
00053   return 0;
00054 }
00055 
00056 // Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
00057 int urlencode(char *str, char *buf, int len) {
00058   static const char to_hex[] = "0123456789ABCDEF";
00059 //  char *pstr = str, *buf = (char*)malloc(strlen(str) * 3 + 1), *pbuf = buf;
00060   char *pstr = str, *pbuf = buf;
00061 
00062   if (len < (strlen(str) * 3 + 1)) return -1;
00063   while (*pstr) {
00064     if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') {
00065       *pbuf++ = *pstr;
00066     } else if (*pstr == ' ') {
00067       *pbuf++ = '+';
00068     } else { 
00069       *pbuf++ = '%';
00070       *pbuf++ = to_hex[(*pstr >> 4) & 0x0f];
00071       *pbuf++ = to_hex[*pstr & 0x0f];
00072     }
00073     pstr++;
00074   }
00075   *pbuf = '\0';
00076   return 0;
00077 }
00078 
00079 
00080 void isr_http (TCPSocketEvent e) {
00081 
00082     DBG("http(%d)\r\n", e);
00083     switch(e) {
00084     case TCPSOCKET_CONNECTED:
00085         tcp_ready = 1;
00086         tcp_busy = 0;
00087         mode = 0;
00088         break;
00089 
00090     case TCPSOCKET_READABLE: //Incoming data
00091         {
00092         char buf[1500];
00093         int i = 0, len;
00094 
00095         len = http->recv(buf, sizeof(buf));
00096         for (i = 0; i < len; i ++) {
00097             switch (mode) {
00098             case 0:
00099                 if (strncmp(buf, "HTTP/", 5) == 0 && atoi(&buf[9]) == 200) {
00100                     mode = 1;
00101                 }
00102                 break;
00103             case 1:
00104             case 2:
00105                 if (buf[i] == '\r') {
00106                     mode ++;
00107                 } else
00108                 if (buf[i] == '\n') {
00109                 } else {
00110                     mode = 1;
00111                 }
00112                 break;
00113             case 3:
00114                 if (buf[i] == 0x7f) {
00115                     mode = 4;
00116                 }
00117                 break;
00118             case 4:
00119                 fifo.put(buf[i]);
00120                 break;
00121             }
00122         }
00123         } // Incoming data
00124         tcp_readable = 1;
00125         break;
00126 
00127     case TCPSOCKET_WRITEABLE: //We can send data
00128         tcp_writable = 1;
00129         break;
00130 
00131     case TCPSOCKET_CONTIMEOUT:
00132     case TCPSOCKET_CONRST:
00133     case TCPSOCKET_CONABRT:
00134     case TCPSOCKET_ERROR:
00135     case TCPSOCKET_DISCONNECTED:
00136         http->close();
00137         tcp_ready = 0;
00138         tcp_readable = 0;
00139         tcp_writable = 0;
00140         tcp_busy = 0;
00141         break;
00142     }
00143 }
00144 
00145 void createauth (char *user, char *pwd, char *buf, int len) {
00146     char tmp[80];
00147 
00148     strncpy(buf, "Authorization: Basic ", len);
00149     snprintf(tmp, sizeof(tmp), "%s:%s", user, pwd);
00150     base64enc(tmp, strlen(tmp), &buf[strlen(buf)], len - strlen(buf));
00151     strncat(buf, "\r\n", len - strlen(buf));
00152 }
00153 
00154 void isr_dns (DNSReply r) {
00155 
00156     DBG("dns(%d)\r\n", r);
00157     if (DNS_FOUND) {
00158         dns_status = 1;
00159     } else {
00160         dns_status = -1;
00161     }
00162 }
00163 
00164 void pollComet () {
00165     Net::poll();
00166 
00167     if (! tcp_ready && ! tcp_busy) {
00168         tcp_busy = 1;
00169         openComet(NULL, NULL, NULL, NULL);
00170     }
00171 }
00172 
00173 int recvComet (char *buf, int size) {
00174     int i, len;
00175 
00176     len = fifo.use();
00177     if (len == 0) return 0;
00178     DBG("readComet %d\r\n", len);
00179     if (len > size) len = size;
00180 
00181     for (i = 0; i < len; i ++) {
00182         if (fifo.get(&buf[i])) break;
00183     }
00184     tcp_readable = 0;
00185     return len;
00186 }
00187 
00188 int sendComet (char *buf) {
00189     while (tcp_busy) {
00190         Net::poll();
00191     }
00192 
00193     tcp_busy = 1;
00194     if (tcp_ready) {
00195         DBG("sendComet close\r\n");
00196         http->close();
00197     }
00198     DBG("sendComet open\r\n");
00199     openComet(NULL, NULL, NULL, buf);
00200     return 0;
00201 }
00202 
00203 int openComet (Host *host, char *uri, char *head, char *body) {
00204     TCPSocketErr err;
00205     Timer timeout;
00206     char buf[1500];
00207 
00208     tcp_busy = 1;
00209     if (host) {
00210       http = new TCPSocket;
00211       http->setOnEvent(isr_http);
00212 
00213       // connect
00214       if (host->getIp().isNull()) {
00215         // resolv
00216         DNSRequest dns;
00217         dns_status = 0;
00218         dns.setOnReply(isr_dns);
00219         if (dns.resolve(host) != DNS_OK) goto exit;
00220         timeout.reset();
00221         timeout.start();
00222         while (timeout.read_ms() < HTTP_TIMEOUT) {
00223             if (dns_status) break;
00224             Net::poll();
00225         }
00226         timeout.stop();
00227         if (dns_status <= 0) goto exit;
00228         DBG("%s [%d.%d.%d.%d]\r\n", host->getName(), (unsigned char)host->getIp()[0], (unsigned char)host->getIp()[1], (unsigned char)host->getIp()[2], (unsigned char)host->getIp()[3]);
00229       }
00230       if (! host->getPort()) {
00231         host->setPort(HTTP_PORT);
00232       }
00233 
00234       remote = *host;
00235       strncpy(remoteuri, uri, sizeof(remoteuri));
00236     } // if (host)
00237 
00238     err = http->connect(remote);
00239     if (err != TCPSOCKET_OK) goto exit;
00240 
00241     // wait connect
00242     timeout.reset();
00243     timeout.start();
00244     while (timeout.read_ms() < HTTP_TIMEOUT) {
00245         if (tcp_ready) break;
00246         Net::poll();
00247     }
00248     timeout.stop();
00249     if (! tcp_ready) goto exit;
00250 
00251     // send request
00252     http->send("POST ", 5);
00253     http->send(remoteuri, strlen(remoteuri));
00254     http->send(" HTTP/1.1\r\nHost: ", 17);
00255     http->send(remote.getName(), strlen(remote.getName()));
00256     http->send("\r\n", 2);
00257     http->send("Connection: close\r\n", 19);
00258     http->send("Content-type: application/x-www-form-urlencoded\r\n", 49);
00259     if (head) {
00260         http->send(head, strlen(head));
00261     }
00262     sprintf(buf, "Content-Length: %d\r\n", strlen(body));
00263     http->send(buf, strlen(buf));
00264     http->send("\r\n", 2);
00265 
00266     // post method
00267     if (body) {
00268         http->send(body, strlen(body));
00269     }
00270 
00271     Net::poll();
00272     return 0;
00273 
00274 exit:
00275     http->resetOnEvent();
00276     http->close();
00277     delete http;
00278 
00279     return -1;
00280 }