WIZ820io(W5200) network interface、EthernetNetIf compatible.

/media/uploads/va009039/wiz820ionetif.jpg

example

#include "WIZ820ioNetIf.h"
#include "HTTPClient.h"
#include "HTTPServer.h"

#if defined(TARGET_KL25Z)
WIZ820ioNetIf eth(PTD2,PTD3,PTD1,PTD0,PTD5);
#endif
HTTPClient http;
HTTPStream stream;

void callback(HTTPResult r){
    printf("callback %d %s\n", r, HTTPClient::ResultStr(r));
}

int main() {
    int err = eth.setup();
    if (err < 0) {
        printf("setup error %d\n", err);
        exit(-1);
    }    

    HTTPServer svr;
    svr.addHandler<SimpleHandler>("/");
    svr.bind(80);

    const char* uri = "http://va009039-mbed.appspot.com/kl25z/";
    http.get(uri, &stream, callback);
    uint8_t buf[256];
    int total = 0;
    stream.readNext(buf, sizeof(buf));
    while(1) {
        if(stream.readable()) {
            int len = stream.readLen();
            total += len;
            printf("%d %d\n", total, len);
            stream.readNext(buf, sizeof(buf));
        }
        Net::poll();
    }
}
Committer:
va009039
Date:
Sun Mar 24 11:25:31 2013 +0000
Revision:
1:22b9052d864d
WIZ820io(W8200) ethernet interface, EthernetNetIf compatible.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
va009039 1:22b9052d864d 1
va009039 1:22b9052d864d 2 /*
va009039 1:22b9052d864d 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
va009039 1:22b9052d864d 4
va009039 1:22b9052d864d 5 Permission is hereby granted, free of charge, to any person obtaining a copy
va009039 1:22b9052d864d 6 of this software and associated documentation files (the "Software"), to deal
va009039 1:22b9052d864d 7 in the Software without restriction, including without limitation the rights
va009039 1:22b9052d864d 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
va009039 1:22b9052d864d 9 copies of the Software, and to permit persons to whom the Software is
va009039 1:22b9052d864d 10 furnished to do so, subject to the following conditions:
va009039 1:22b9052d864d 11
va009039 1:22b9052d864d 12 The above copyright notice and this permission notice shall be included in
va009039 1:22b9052d864d 13 all copies or substantial portions of the Software.
va009039 1:22b9052d864d 14
va009039 1:22b9052d864d 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
va009039 1:22b9052d864d 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
va009039 1:22b9052d864d 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
va009039 1:22b9052d864d 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
va009039 1:22b9052d864d 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
va009039 1:22b9052d864d 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
va009039 1:22b9052d864d 21 THE SOFTWARE.
va009039 1:22b9052d864d 22 */
va009039 1:22b9052d864d 23
va009039 1:22b9052d864d 24 #include "core/netservice.h"
va009039 1:22b9052d864d 25 #include "HTTPRequestHandler.h"
va009039 1:22b9052d864d 26
va009039 1:22b9052d864d 27 #include <string.h>
va009039 1:22b9052d864d 28
va009039 1:22b9052d864d 29 //#define __DEBUG
va009039 1:22b9052d864d 30 #include "dbg/dbg.h"
va009039 1:22b9052d864d 31
va009039 1:22b9052d864d 32 #define HTTP_REQUEST_TIMEOUT 5000
va009039 1:22b9052d864d 33
va009039 1:22b9052d864d 34 HTTPRequestHandler::HTTPRequestHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket) : NetService(),
va009039 1:22b9052d864d 35 m_pTCPSocket(pTCPSocket), m_reqHeaders(), m_respHeaders(),
va009039 1:22b9052d864d 36 m_rootPath(rootPath), m_path(path), m_errc(200),
va009039 1:22b9052d864d 37 m_watchdog(), m_timeout(0), m_closed(false), m_headersSent(false) //OK
va009039 1:22b9052d864d 38 {
va009039 1:22b9052d864d 39 //Read & parse headers
va009039 1:22b9052d864d 40 readHeaders();
va009039 1:22b9052d864d 41 m_pTCPSocket->setOnEvent(this, &HTTPRequestHandler::onTCPSocketEvent);
va009039 1:22b9052d864d 42 setTimeout(HTTP_REQUEST_TIMEOUT);
va009039 1:22b9052d864d 43 }
va009039 1:22b9052d864d 44
va009039 1:22b9052d864d 45 HTTPRequestHandler::~HTTPRequestHandler()
va009039 1:22b9052d864d 46 {
va009039 1:22b9052d864d 47 close();
va009039 1:22b9052d864d 48 }
va009039 1:22b9052d864d 49
va009039 1:22b9052d864d 50 void HTTPRequestHandler::onTimeout() //Connection has timed out
va009039 1:22b9052d864d 51 {
va009039 1:22b9052d864d 52 close();
va009039 1:22b9052d864d 53 }
va009039 1:22b9052d864d 54
va009039 1:22b9052d864d 55 void HTTPRequestHandler::close() //Close socket and destroy data
va009039 1:22b9052d864d 56 {
va009039 1:22b9052d864d 57 if(m_closed)
va009039 1:22b9052d864d 58 return;
va009039 1:22b9052d864d 59 m_closed = true; //Prevent recursive calling or calling on an object being destructed by someone else
va009039 1:22b9052d864d 60 m_watchdog.detach();
va009039 1:22b9052d864d 61 onClose();
va009039 1:22b9052d864d 62 m_pTCPSocket->resetOnEvent();
va009039 1:22b9052d864d 63 m_pTCPSocket->close();
va009039 1:22b9052d864d 64 delete m_pTCPSocket; //Can safely destroy socket
va009039 1:22b9052d864d 65 NetService::close();
va009039 1:22b9052d864d 66 }
va009039 1:22b9052d864d 67
va009039 1:22b9052d864d 68 map<string, string>& HTTPRequestHandler::reqHeaders() //const
va009039 1:22b9052d864d 69 {
va009039 1:22b9052d864d 70 return m_reqHeaders;
va009039 1:22b9052d864d 71 }
va009039 1:22b9052d864d 72
va009039 1:22b9052d864d 73 string& HTTPRequestHandler::path() //const
va009039 1:22b9052d864d 74 {
va009039 1:22b9052d864d 75 return m_path;
va009039 1:22b9052d864d 76 }
va009039 1:22b9052d864d 77
va009039 1:22b9052d864d 78 int HTTPRequestHandler::dataLen() const
va009039 1:22b9052d864d 79 {
va009039 1:22b9052d864d 80 map<string,string>::iterator it;
va009039 1:22b9052d864d 81 it = m_reqHeaders.find("Content-Length");
va009039 1:22b9052d864d 82 if( it == m_reqHeaders.end() )
va009039 1:22b9052d864d 83 {
va009039 1:22b9052d864d 84 return 0;
va009039 1:22b9052d864d 85 }
va009039 1:22b9052d864d 86 return atoi((*it).second.c_str()); //return 0 if parse fails, so that's fine
va009039 1:22b9052d864d 87 }
va009039 1:22b9052d864d 88
va009039 1:22b9052d864d 89 int HTTPRequestHandler::readData(char* buf, int len)
va009039 1:22b9052d864d 90 {
va009039 1:22b9052d864d 91 return m_pTCPSocket->recv(buf, len);
va009039 1:22b9052d864d 92 }
va009039 1:22b9052d864d 93
va009039 1:22b9052d864d 94 string& HTTPRequestHandler::rootPath() //const
va009039 1:22b9052d864d 95 {
va009039 1:22b9052d864d 96 return m_rootPath;
va009039 1:22b9052d864d 97 }
va009039 1:22b9052d864d 98
va009039 1:22b9052d864d 99 void HTTPRequestHandler::setErrCode(int errc)
va009039 1:22b9052d864d 100 {
va009039 1:22b9052d864d 101 m_errc = errc;
va009039 1:22b9052d864d 102 }
va009039 1:22b9052d864d 103
va009039 1:22b9052d864d 104 void HTTPRequestHandler::setContentLen(int len)
va009039 1:22b9052d864d 105 {
va009039 1:22b9052d864d 106 char len_str[6] = {0};
va009039 1:22b9052d864d 107 sprintf(len_str, "%d", len);
va009039 1:22b9052d864d 108 respHeaders()["Content-Length"] = len_str;
va009039 1:22b9052d864d 109 }
va009039 1:22b9052d864d 110
va009039 1:22b9052d864d 111 map<string, string>& HTTPRequestHandler::respHeaders()
va009039 1:22b9052d864d 112 {
va009039 1:22b9052d864d 113 return m_respHeaders;
va009039 1:22b9052d864d 114 }
va009039 1:22b9052d864d 115
va009039 1:22b9052d864d 116 int HTTPRequestHandler::writeData(const char* buf, int len)
va009039 1:22b9052d864d 117 {
va009039 1:22b9052d864d 118 if(!m_headersSent)
va009039 1:22b9052d864d 119 {
va009039 1:22b9052d864d 120 m_headersSent = true;
va009039 1:22b9052d864d 121 writeHeaders();
va009039 1:22b9052d864d 122 }
va009039 1:22b9052d864d 123
va009039 1:22b9052d864d 124 return m_pTCPSocket->send(buf, len);
va009039 1:22b9052d864d 125 }
va009039 1:22b9052d864d 126
va009039 1:22b9052d864d 127 void HTTPRequestHandler::setTimeout(int ms)
va009039 1:22b9052d864d 128 {
va009039 1:22b9052d864d 129 m_timeout = 1000*ms;
va009039 1:22b9052d864d 130 resetTimeout();
va009039 1:22b9052d864d 131 }
va009039 1:22b9052d864d 132
va009039 1:22b9052d864d 133 void HTTPRequestHandler::resetTimeout()
va009039 1:22b9052d864d 134 {
va009039 1:22b9052d864d 135 m_watchdog.detach();
va009039 1:22b9052d864d 136 m_watchdog.attach_us<HTTPRequestHandler>(this, &HTTPRequestHandler::onTimeout, m_timeout);
va009039 1:22b9052d864d 137 }
va009039 1:22b9052d864d 138
va009039 1:22b9052d864d 139
va009039 1:22b9052d864d 140 void HTTPRequestHandler::readHeaders()
va009039 1:22b9052d864d 141 {
va009039 1:22b9052d864d 142 static char line[128];
va009039 1:22b9052d864d 143 static char key[128];
va009039 1:22b9052d864d 144 static char value[128];
va009039 1:22b9052d864d 145 while( readLine(line, 128) > 0) //if == 0, it is an empty line = end of headers
va009039 1:22b9052d864d 146 {
va009039 1:22b9052d864d 147 int n = sscanf(line, "%[^:]: %[^\n]", key, value);
va009039 1:22b9052d864d 148 if ( n == 2 )
va009039 1:22b9052d864d 149 {
va009039 1:22b9052d864d 150 DBG("\r\nRead header : %s : %s\r\n", key, value);
va009039 1:22b9052d864d 151 m_reqHeaders[key] = value;
va009039 1:22b9052d864d 152 }
va009039 1:22b9052d864d 153 //TODO: Impl n==1 case (part 2 of previous header)
va009039 1:22b9052d864d 154 }
va009039 1:22b9052d864d 155 }
va009039 1:22b9052d864d 156
va009039 1:22b9052d864d 157 void HTTPRequestHandler::writeHeaders() //Called at the first writeData call
va009039 1:22b9052d864d 158 {
va009039 1:22b9052d864d 159 static char line[128];
va009039 1:22b9052d864d 160
va009039 1:22b9052d864d 161 //Response line
va009039 1:22b9052d864d 162 sprintf(line, "HTTP/1.1 %d MbedInfo\r\n", m_errc); //Not a violation of the standard not to include the descriptive text
va009039 1:22b9052d864d 163 m_pTCPSocket->send(line, strlen(line));
va009039 1:22b9052d864d 164
va009039 1:22b9052d864d 165 map<string,string>::iterator it;
va009039 1:22b9052d864d 166 while( !m_respHeaders.empty() )
va009039 1:22b9052d864d 167 {
va009039 1:22b9052d864d 168 it = m_respHeaders.begin();
va009039 1:22b9052d864d 169 sprintf(line, "%s: %s\r\n", (*it).first.c_str(), (*it).second.c_str() );
va009039 1:22b9052d864d 170 DBG("\r\n%s", line);
va009039 1:22b9052d864d 171 m_pTCPSocket->send(line, strlen(line));
va009039 1:22b9052d864d 172 m_respHeaders.erase(it);
va009039 1:22b9052d864d 173 }
va009039 1:22b9052d864d 174 m_pTCPSocket->send("\r\n",2); //End of head
va009039 1:22b9052d864d 175 }
va009039 1:22b9052d864d 176
va009039 1:22b9052d864d 177 int HTTPRequestHandler::readLine(char* str, int maxLen)
va009039 1:22b9052d864d 178 {
va009039 1:22b9052d864d 179 int ret;
va009039 1:22b9052d864d 180 int len = 0;
va009039 1:22b9052d864d 181 for(int i = 0; i < maxLen - 1; i++)
va009039 1:22b9052d864d 182 {
va009039 1:22b9052d864d 183 ret = m_pTCPSocket->recv(str, 1);
va009039 1:22b9052d864d 184 if(!ret)
va009039 1:22b9052d864d 185 {
va009039 1:22b9052d864d 186 break;
va009039 1:22b9052d864d 187 }
va009039 1:22b9052d864d 188 if( (len > 1) && *(str-1)=='\r' && *str=='\n' )
va009039 1:22b9052d864d 189 {
va009039 1:22b9052d864d 190 str--;
va009039 1:22b9052d864d 191 len-=2;
va009039 1:22b9052d864d 192 break;
va009039 1:22b9052d864d 193 }
va009039 1:22b9052d864d 194 else if( *str=='\n' )
va009039 1:22b9052d864d 195 {
va009039 1:22b9052d864d 196 len--;
va009039 1:22b9052d864d 197 break;
va009039 1:22b9052d864d 198 }
va009039 1:22b9052d864d 199 str++;
va009039 1:22b9052d864d 200 len++;
va009039 1:22b9052d864d 201 }
va009039 1:22b9052d864d 202 *str = 0;
va009039 1:22b9052d864d 203 return len;
va009039 1:22b9052d864d 204 }
va009039 1:22b9052d864d 205
va009039 1:22b9052d864d 206 void HTTPRequestHandler::onTCPSocketEvent(TCPSocketEvent e)
va009039 1:22b9052d864d 207 {
va009039 1:22b9052d864d 208
va009039 1:22b9052d864d 209 DBG("\r\nEvent %d in HTTPRequestHandler\r\n", e);
va009039 1:22b9052d864d 210
va009039 1:22b9052d864d 211 if(m_closed)
va009039 1:22b9052d864d 212 {
va009039 1:22b9052d864d 213 DBG("\r\nWARN: Discarded\r\n");
va009039 1:22b9052d864d 214 return;
va009039 1:22b9052d864d 215 }
va009039 1:22b9052d864d 216
va009039 1:22b9052d864d 217 switch(e)
va009039 1:22b9052d864d 218 {
va009039 1:22b9052d864d 219 case TCPSOCKET_READABLE:
va009039 1:22b9052d864d 220 resetTimeout();
va009039 1:22b9052d864d 221 onReadable();
va009039 1:22b9052d864d 222 break;
va009039 1:22b9052d864d 223 case TCPSOCKET_WRITEABLE:
va009039 1:22b9052d864d 224 resetTimeout();
va009039 1:22b9052d864d 225 onWriteable();
va009039 1:22b9052d864d 226 break;
va009039 1:22b9052d864d 227 case TCPSOCKET_CONTIMEOUT:
va009039 1:22b9052d864d 228 case TCPSOCKET_CONRST:
va009039 1:22b9052d864d 229 case TCPSOCKET_CONABRT:
va009039 1:22b9052d864d 230 case TCPSOCKET_ERROR:
va009039 1:22b9052d864d 231 case TCPSOCKET_DISCONNECTED:
va009039 1:22b9052d864d 232 DBG("\r\nConnection error in handler\r\n");
va009039 1:22b9052d864d 233 close();
va009039 1:22b9052d864d 234 break;
va009039 1:22b9052d864d 235 }
va009039 1:22b9052d864d 236
va009039 1:22b9052d864d 237 }