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 "net.h"
va009039 1:22b9052d864d 25
va009039 1:22b9052d864d 26 #include "host.h"
va009039 1:22b9052d864d 27 #include "ipaddr.h"
va009039 1:22b9052d864d 28 #include "netservice.h"
va009039 1:22b9052d864d 29 #include "if/net/netif.h"
va009039 1:22b9052d864d 30 #include "if/net/nettcpsocket.h"
va009039 1:22b9052d864d 31 #include "if/net/netudpsocket.h"
va009039 1:22b9052d864d 32 #include "if/net/netdnsrequest.h"
va009039 1:22b9052d864d 33
va009039 1:22b9052d864d 34 //#define __DEBUG
va009039 1:22b9052d864d 35 #include "dbg/dbg.h"
va009039 1:22b9052d864d 36
va009039 1:22b9052d864d 37 Net::Net() : m_defaultIf(NULL), m_lpIf(), m_lpNetTcpSocket(), m_lpNetUdpSocket()
va009039 1:22b9052d864d 38 {
va009039 1:22b9052d864d 39
va009039 1:22b9052d864d 40 }
va009039 1:22b9052d864d 41
va009039 1:22b9052d864d 42 Net::~Net()
va009039 1:22b9052d864d 43 {
va009039 1:22b9052d864d 44
va009039 1:22b9052d864d 45 }
va009039 1:22b9052d864d 46
va009039 1:22b9052d864d 47
va009039 1:22b9052d864d 48 void Net::poll()
va009039 1:22b9052d864d 49 {
va009039 1:22b9052d864d 50 //DBG("\r\nNet : Services polling\r\n");
va009039 1:22b9052d864d 51
va009039 1:22b9052d864d 52 //Poll Services
va009039 1:22b9052d864d 53 NetService::servicesPoll();
va009039 1:22b9052d864d 54
va009039 1:22b9052d864d 55 //DBG("\r\nNet : Interfaces polling\r\n");
va009039 1:22b9052d864d 56
va009039 1:22b9052d864d 57 //Poll Interfaces
va009039 1:22b9052d864d 58 list<NetIf*>::iterator pIfIt;
va009039 1:22b9052d864d 59
va009039 1:22b9052d864d 60 for ( pIfIt = net().m_lpIf.begin() ; pIfIt != net().m_lpIf.end(); pIfIt++ )
va009039 1:22b9052d864d 61 {
va009039 1:22b9052d864d 62 (*pIfIt)->poll();
va009039 1:22b9052d864d 63 }
va009039 1:22b9052d864d 64
va009039 1:22b9052d864d 65 //DBG("\r\nNet : Sockets polling\r\n");
va009039 1:22b9052d864d 66
va009039 1:22b9052d864d 67 //Poll Tcp Sockets
va009039 1:22b9052d864d 68 list<NetTcpSocket*>::iterator pNetTcpSocketIt;
va009039 1:22b9052d864d 69
va009039 1:22b9052d864d 70 for ( pNetTcpSocketIt = net().m_lpNetTcpSocket.begin() ; pNetTcpSocketIt != net().m_lpNetTcpSocket.end(); )
va009039 1:22b9052d864d 71 {
va009039 1:22b9052d864d 72 (*pNetTcpSocketIt)->poll();
va009039 1:22b9052d864d 73
va009039 1:22b9052d864d 74 if( (*pNetTcpSocketIt)->m_closed && !((*pNetTcpSocketIt)->m_refs) )
va009039 1:22b9052d864d 75 {
va009039 1:22b9052d864d 76 (*pNetTcpSocketIt)->m_removed = true;
va009039 1:22b9052d864d 77 delete (*pNetTcpSocketIt);
va009039 1:22b9052d864d 78 (*pNetTcpSocketIt) = NULL;
va009039 1:22b9052d864d 79 pNetTcpSocketIt = net().m_lpNetTcpSocket.erase(pNetTcpSocketIt);
va009039 1:22b9052d864d 80 }
va009039 1:22b9052d864d 81 else
va009039 1:22b9052d864d 82 {
va009039 1:22b9052d864d 83 pNetTcpSocketIt++;
va009039 1:22b9052d864d 84 }
va009039 1:22b9052d864d 85 }
va009039 1:22b9052d864d 86
va009039 1:22b9052d864d 87 //Poll Udp Sockets
va009039 1:22b9052d864d 88 list<NetUdpSocket*>::iterator pNetUdpSocketIt;
va009039 1:22b9052d864d 89
va009039 1:22b9052d864d 90 for ( pNetUdpSocketIt = net().m_lpNetUdpSocket.begin() ; pNetUdpSocketIt != net().m_lpNetUdpSocket.end(); )
va009039 1:22b9052d864d 91 {
va009039 1:22b9052d864d 92 (*pNetUdpSocketIt)->poll();
va009039 1:22b9052d864d 93
va009039 1:22b9052d864d 94 if( (*pNetUdpSocketIt)->m_closed && !((*pNetUdpSocketIt)->m_refs) )
va009039 1:22b9052d864d 95 {
va009039 1:22b9052d864d 96 (*pNetUdpSocketIt)->m_removed = true;
va009039 1:22b9052d864d 97 delete (*pNetUdpSocketIt);
va009039 1:22b9052d864d 98 (*pNetUdpSocketIt) = NULL;
va009039 1:22b9052d864d 99 pNetUdpSocketIt = net().m_lpNetUdpSocket.erase(pNetUdpSocketIt);
va009039 1:22b9052d864d 100 }
va009039 1:22b9052d864d 101 else
va009039 1:22b9052d864d 102 {
va009039 1:22b9052d864d 103 pNetUdpSocketIt++;
va009039 1:22b9052d864d 104 }
va009039 1:22b9052d864d 105 }
va009039 1:22b9052d864d 106
va009039 1:22b9052d864d 107
va009039 1:22b9052d864d 108 }
va009039 1:22b9052d864d 109
va009039 1:22b9052d864d 110 NetTcpSocket* Net::tcpSocket(NetIf& netif) {
va009039 1:22b9052d864d 111 NetTcpSocket* pNetTcpSocket = netif.tcpSocket();
va009039 1:22b9052d864d 112 pNetTcpSocket->m_refs++;
va009039 1:22b9052d864d 113 return pNetTcpSocket;
va009039 1:22b9052d864d 114 }
va009039 1:22b9052d864d 115
va009039 1:22b9052d864d 116 NetTcpSocket* Net::tcpSocket() { //NetTcpSocket on default if
va009039 1:22b9052d864d 117 if ( net().m_defaultIf == NULL )
va009039 1:22b9052d864d 118 return NULL;
va009039 1:22b9052d864d 119 NetTcpSocket* pNetTcpSocket = net().m_defaultIf->tcpSocket();
va009039 1:22b9052d864d 120 pNetTcpSocket->m_refs++;
va009039 1:22b9052d864d 121 return pNetTcpSocket;
va009039 1:22b9052d864d 122 }
va009039 1:22b9052d864d 123
va009039 1:22b9052d864d 124 void Net::releaseTcpSocket(NetTcpSocket* pNetTcpSocket)
va009039 1:22b9052d864d 125 {
va009039 1:22b9052d864d 126 pNetTcpSocket->m_refs--;
va009039 1:22b9052d864d 127 if(!pNetTcpSocket->m_closed && !pNetTcpSocket->m_refs)
va009039 1:22b9052d864d 128 pNetTcpSocket->close();
va009039 1:22b9052d864d 129 }
va009039 1:22b9052d864d 130
va009039 1:22b9052d864d 131 NetUdpSocket* Net::udpSocket(NetIf& netif) {
va009039 1:22b9052d864d 132 NetUdpSocket* pNetUdpSocket = netif.udpSocket();
va009039 1:22b9052d864d 133 pNetUdpSocket->m_refs++;
va009039 1:22b9052d864d 134 return pNetUdpSocket;
va009039 1:22b9052d864d 135 }
va009039 1:22b9052d864d 136
va009039 1:22b9052d864d 137 NetUdpSocket* Net::udpSocket() { //NetTcpSocket on default if
va009039 1:22b9052d864d 138 if ( net().m_defaultIf == NULL )
va009039 1:22b9052d864d 139 return NULL;
va009039 1:22b9052d864d 140 NetUdpSocket* pNetUdpSocket = net().m_defaultIf->udpSocket();
va009039 1:22b9052d864d 141 pNetUdpSocket->m_refs++;
va009039 1:22b9052d864d 142 return pNetUdpSocket;
va009039 1:22b9052d864d 143 }
va009039 1:22b9052d864d 144
va009039 1:22b9052d864d 145 void Net::releaseUdpSocket(NetUdpSocket* pNetUdpSocket)
va009039 1:22b9052d864d 146 {
va009039 1:22b9052d864d 147 pNetUdpSocket->m_refs--;
va009039 1:22b9052d864d 148 if(!pNetUdpSocket->m_closed && !pNetUdpSocket->m_refs)
va009039 1:22b9052d864d 149 pNetUdpSocket->close();
va009039 1:22b9052d864d 150 }
va009039 1:22b9052d864d 151
va009039 1:22b9052d864d 152 NetDnsRequest* Net::dnsRequest(const char* hostname, NetIf& netif) {
va009039 1:22b9052d864d 153 return netif.dnsRequest(hostname);
va009039 1:22b9052d864d 154 }
va009039 1:22b9052d864d 155
va009039 1:22b9052d864d 156 NetDnsRequest* Net::dnsRequest(const char* hostname) { //Create a new NetDnsRequest object from default if
va009039 1:22b9052d864d 157 if ( net().m_defaultIf == NULL )
va009039 1:22b9052d864d 158 return NULL;
va009039 1:22b9052d864d 159 return net().m_defaultIf->dnsRequest(hostname);
va009039 1:22b9052d864d 160 }
va009039 1:22b9052d864d 161
va009039 1:22b9052d864d 162 NetDnsRequest* Net::dnsRequest(Host* pHost, NetIf& netif)
va009039 1:22b9052d864d 163 {
va009039 1:22b9052d864d 164 return netif.dnsRequest(pHost);
va009039 1:22b9052d864d 165 }
va009039 1:22b9052d864d 166
va009039 1:22b9052d864d 167 NetDnsRequest* Net::dnsRequest(Host* pHost) //Creats a new NetDnsRequest object from default if
va009039 1:22b9052d864d 168 {
va009039 1:22b9052d864d 169 if ( net().m_defaultIf == NULL )
va009039 1:22b9052d864d 170 return NULL;
va009039 1:22b9052d864d 171 return net().m_defaultIf->dnsRequest(pHost);
va009039 1:22b9052d864d 172 }
va009039 1:22b9052d864d 173
va009039 1:22b9052d864d 174 void Net::setDefaultIf(NetIf& netif) {
va009039 1:22b9052d864d 175 net().m_defaultIf = &netif;
va009039 1:22b9052d864d 176 }
va009039 1:22b9052d864d 177
va009039 1:22b9052d864d 178 void Net::setDefaultIf(NetIf* pIf)
va009039 1:22b9052d864d 179 {
va009039 1:22b9052d864d 180 net().m_defaultIf = pIf;
va009039 1:22b9052d864d 181 }
va009039 1:22b9052d864d 182
va009039 1:22b9052d864d 183 NetIf* Net::getDefaultIf() {
va009039 1:22b9052d864d 184 return net().m_defaultIf;
va009039 1:22b9052d864d 185 }
va009039 1:22b9052d864d 186
va009039 1:22b9052d864d 187 void Net::registerIf(NetIf* pIf)
va009039 1:22b9052d864d 188 {
va009039 1:22b9052d864d 189 net().m_lpIf.push_back(pIf);
va009039 1:22b9052d864d 190 }
va009039 1:22b9052d864d 191
va009039 1:22b9052d864d 192 void Net::unregisterIf(NetIf* pIf)
va009039 1:22b9052d864d 193 {
va009039 1:22b9052d864d 194 if( net().m_defaultIf == pIf )
va009039 1:22b9052d864d 195 net().m_defaultIf = NULL;
va009039 1:22b9052d864d 196 net().m_lpIf.remove(pIf);
va009039 1:22b9052d864d 197 }
va009039 1:22b9052d864d 198
va009039 1:22b9052d864d 199 void Net::registerNetTcpSocket(NetTcpSocket* pNetTcpSocket)
va009039 1:22b9052d864d 200 {
va009039 1:22b9052d864d 201 net().m_lpNetTcpSocket.push_back(pNetTcpSocket);
va009039 1:22b9052d864d 202 DBG("\r\nNetTcpSocket [ + %p ] %d\r\n", (void*)pNetTcpSocket, net().m_lpNetTcpSocket.size());
va009039 1:22b9052d864d 203 }
va009039 1:22b9052d864d 204
va009039 1:22b9052d864d 205 void Net::unregisterNetTcpSocket(NetTcpSocket* pNetTcpSocket)
va009039 1:22b9052d864d 206 {
va009039 1:22b9052d864d 207 DBG("\r\nNetTcpSocket [ - %p ] %d\r\n", (void*)pNetTcpSocket, net().m_lpNetTcpSocket.size() - 1);
va009039 1:22b9052d864d 208 if(!pNetTcpSocket->m_removed)
va009039 1:22b9052d864d 209 net().m_lpNetTcpSocket.remove(pNetTcpSocket);
va009039 1:22b9052d864d 210 }
va009039 1:22b9052d864d 211
va009039 1:22b9052d864d 212 void Net::registerNetUdpSocket(NetUdpSocket* pNetUdpSocket)
va009039 1:22b9052d864d 213 {
va009039 1:22b9052d864d 214 net().m_lpNetUdpSocket.push_back(pNetUdpSocket);
va009039 1:22b9052d864d 215 DBG("\r\nNetUdpSocket [ + %p ] %d\r\n", (void*)pNetUdpSocket, net().m_lpNetUdpSocket.size());
va009039 1:22b9052d864d 216 }
va009039 1:22b9052d864d 217
va009039 1:22b9052d864d 218 void Net::unregisterNetUdpSocket(NetUdpSocket* pNetUdpSocket)
va009039 1:22b9052d864d 219 {
va009039 1:22b9052d864d 220 DBG("\r\nNetUdpSocket [ - %p ] %d\r\n", (void*)pNetUdpSocket, net().m_lpNetUdpSocket.size() - 1);
va009039 1:22b9052d864d 221 if(!pNetUdpSocket->m_removed)
va009039 1:22b9052d864d 222 net().m_lpNetUdpSocket.remove(pNetUdpSocket);
va009039 1:22b9052d864d 223 }
va009039 1:22b9052d864d 224
va009039 1:22b9052d864d 225 Net& Net::net()
va009039 1:22b9052d864d 226 {
va009039 1:22b9052d864d 227 static Net* pInst = new Net(); //Called only once
va009039 1:22b9052d864d 228 return *pInst;
va009039 1:22b9052d864d 229 }