WIZ820io(W5200) network interface、EthernetNetIf compatible.
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(); } }
MyNetDnsRequest.cpp@0:bdeec5f86894, 2013-03-22 (annotated)
- Committer:
- va009039
- Date:
- Fri Mar 22 11:51:24 2013 +0000
- Revision:
- 0:bdeec5f86894
- Child:
- 1:22b9052d864d
[mbed] converted /w5200NetIf/w5200NetIf
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
va009039 | 0:bdeec5f86894 | 1 | // MyNetDnsRequest.cpp 2012/4/22 |
va009039 | 0:bdeec5f86894 | 2 | #include "mbed.h" |
va009039 | 0:bdeec5f86894 | 3 | #include "MyNetDnsRequest.h" |
va009039 | 0:bdeec5f86894 | 4 | #include "UDPSocket.h" |
va009039 | 0:bdeec5f86894 | 5 | #include <string> |
va009039 | 0:bdeec5f86894 | 6 | #include "dnsname.h" |
va009039 | 0:bdeec5f86894 | 7 | #include "W5200NetIf.h" |
va009039 | 0:bdeec5f86894 | 8 | //#define __DEBUG |
va009039 | 0:bdeec5f86894 | 9 | #include "dbg/dbg.h" |
va009039 | 0:bdeec5f86894 | 10 | |
va009039 | 0:bdeec5f86894 | 11 | #ifdef __DEBUG |
va009039 | 0:bdeec5f86894 | 12 | #define DBG2(...) do{ DebugStream::debug("%p %d %s ", this,__LINE__,__PRETTY_FUNCTION__); DebugStream::debug(__VA_ARGS__); } while(0); |
va009039 | 0:bdeec5f86894 | 13 | #else |
va009039 | 0:bdeec5f86894 | 14 | #define DBG2(...) while(0); |
va009039 | 0:bdeec5f86894 | 15 | #endif //__DEBUG |
va009039 | 0:bdeec5f86894 | 16 | |
va009039 | 0:bdeec5f86894 | 17 | //#define DEBUG |
va009039 | 0:bdeec5f86894 | 18 | |
va009039 | 0:bdeec5f86894 | 19 | #ifdef DEBUG |
va009039 | 0:bdeec5f86894 | 20 | #include "Utils.h" |
va009039 | 0:bdeec5f86894 | 21 | #define PRINT_FUNC() printf("%p %d:%s\n", this,__LINE__,__PRETTY_FUNCTION__) |
va009039 | 0:bdeec5f86894 | 22 | #else //DEBUG |
va009039 | 0:bdeec5f86894 | 23 | #define PRINT_FUNC() |
va009039 | 0:bdeec5f86894 | 24 | #endif //DEBUG |
va009039 | 0:bdeec5f86894 | 25 | |
va009039 | 0:bdeec5f86894 | 26 | |
va009039 | 0:bdeec5f86894 | 27 | MyNetDnsRequest::MyNetDnsRequest(const char* hostname) : NetDnsRequest(hostname), |
va009039 | 0:bdeec5f86894 | 28 | m_state(MYNETDNS_START), m_cbFired(false), m_closing(false), m_udp(NULL) { |
va009039 | 0:bdeec5f86894 | 29 | PRINT_FUNC(); |
va009039 | 0:bdeec5f86894 | 30 | } |
va009039 | 0:bdeec5f86894 | 31 | |
va009039 | 0:bdeec5f86894 | 32 | MyNetDnsRequest::MyNetDnsRequest(Host* pHost) : NetDnsRequest(pHost), |
va009039 | 0:bdeec5f86894 | 33 | m_state(MYNETDNS_START), m_cbFired(false), m_closing(false), m_udp(NULL) { |
va009039 | 0:bdeec5f86894 | 34 | PRINT_FUNC(); |
va009039 | 0:bdeec5f86894 | 35 | } |
va009039 | 0:bdeec5f86894 | 36 | |
va009039 | 0:bdeec5f86894 | 37 | MyNetDnsRequest::~MyNetDnsRequest() { |
va009039 | 0:bdeec5f86894 | 38 | PRINT_FUNC(); |
va009039 | 0:bdeec5f86894 | 39 | if (m_udp) { |
va009039 | 0:bdeec5f86894 | 40 | delete m_udp; |
va009039 | 0:bdeec5f86894 | 41 | } |
va009039 | 0:bdeec5f86894 | 42 | } |
va009039 | 0:bdeec5f86894 | 43 | |
va009039 | 0:bdeec5f86894 | 44 | void MyNetDnsRequest::callback(UDPSocketEvent e) |
va009039 | 0:bdeec5f86894 | 45 | { |
va009039 | 0:bdeec5f86894 | 46 | PRINT_FUNC(); |
va009039 | 0:bdeec5f86894 | 47 | DBG2("m_id[]=%02x:%02x\n", m_id[0], m_id[1]); |
va009039 | 0:bdeec5f86894 | 48 | uint8_t buf[512]; |
va009039 | 0:bdeec5f86894 | 49 | Host host; |
va009039 | 0:bdeec5f86894 | 50 | int len = m_udp->recvfrom((char*)buf, sizeof(buf), &host); |
va009039 | 0:bdeec5f86894 | 51 | if (memcmp(buf+0, m_id, 2) != 0) { //verify |
va009039 | 0:bdeec5f86894 | 52 | return; |
va009039 | 0:bdeec5f86894 | 53 | } |
va009039 | 0:bdeec5f86894 | 54 | int rcode = response(buf, len); |
va009039 | 0:bdeec5f86894 | 55 | if (rcode == 0) { |
va009039 | 0:bdeec5f86894 | 56 | m_state = MYNETDNS_OK; |
va009039 | 0:bdeec5f86894 | 57 | } else { |
va009039 | 0:bdeec5f86894 | 58 | m_state = MYNETDNS_NOTFOUND; |
va009039 | 0:bdeec5f86894 | 59 | } |
va009039 | 0:bdeec5f86894 | 60 | } |
va009039 | 0:bdeec5f86894 | 61 | |
va009039 | 0:bdeec5f86894 | 62 | int MyNetDnsRequest::response(uint8_t buf[], int size) { |
va009039 | 0:bdeec5f86894 | 63 | PRINT_FUNC(); |
va009039 | 0:bdeec5f86894 | 64 | #ifdef DEBUG |
va009039 | 0:bdeec5f86894 | 65 | printHex(buf, size); |
va009039 | 0:bdeec5f86894 | 66 | #endif //DEBUG |
va009039 | 0:bdeec5f86894 | 67 | int rcode = buf[3] & 0x0f; |
va009039 | 0:bdeec5f86894 | 68 | if (rcode != 0) { |
va009039 | 0:bdeec5f86894 | 69 | return rcode; |
va009039 | 0:bdeec5f86894 | 70 | } |
va009039 | 0:bdeec5f86894 | 71 | int qdcount = buf[4]<<8|buf[5]; |
va009039 | 0:bdeec5f86894 | 72 | int ancount = buf[6]<<8|buf[7]; |
va009039 | 0:bdeec5f86894 | 73 | int pos = 12; |
va009039 | 0:bdeec5f86894 | 74 | while(qdcount-- > 0) { |
va009039 | 0:bdeec5f86894 | 75 | dnsname qname(buf); |
va009039 | 0:bdeec5f86894 | 76 | pos = qname.decode(pos); // qname |
va009039 | 0:bdeec5f86894 | 77 | pos += 4; // qtype qclass |
va009039 | 0:bdeec5f86894 | 78 | } |
va009039 | 0:bdeec5f86894 | 79 | while(ancount-- > 0) { |
va009039 | 0:bdeec5f86894 | 80 | dnsname name(buf); |
va009039 | 0:bdeec5f86894 | 81 | pos = name.decode(pos); // name |
va009039 | 0:bdeec5f86894 | 82 | int type = buf[pos]<<8|buf[pos+1]; |
va009039 | 0:bdeec5f86894 | 83 | pos += 8; // type class TTL |
va009039 | 0:bdeec5f86894 | 84 | int rdlength = buf[pos]<<8|buf[pos+1]; pos += 2; |
va009039 | 0:bdeec5f86894 | 85 | int rdata_pos = pos; |
va009039 | 0:bdeec5f86894 | 86 | pos += rdlength; |
va009039 | 0:bdeec5f86894 | 87 | if (type == 1) { // A record |
va009039 | 0:bdeec5f86894 | 88 | m_ip = IpAddr(buf[rdata_pos],buf[rdata_pos+1],buf[rdata_pos+2],buf[rdata_pos+3]); |
va009039 | 0:bdeec5f86894 | 89 | } |
va009039 | 0:bdeec5f86894 | 90 | #ifdef DEBUG |
va009039 | 0:bdeec5f86894 | 91 | printf("%s", name.str.c_str()); |
va009039 | 0:bdeec5f86894 | 92 | if (type == 1) { |
va009039 | 0:bdeec5f86894 | 93 | printf(" A %d.%d.%d.%d\n", |
va009039 | 0:bdeec5f86894 | 94 | buf[rdata_pos],buf[rdata_pos+1],buf[rdata_pos+2],buf[rdata_pos+3]); |
va009039 | 0:bdeec5f86894 | 95 | } else if (type == 5) { |
va009039 | 0:bdeec5f86894 | 96 | dnsname rdname(buf); |
va009039 | 0:bdeec5f86894 | 97 | rdname.decode(rdata_pos); |
va009039 | 0:bdeec5f86894 | 98 | printf(" CNAME %s\n", rdname.str.c_str()); |
va009039 | 0:bdeec5f86894 | 99 | } else { |
va009039 | 0:bdeec5f86894 | 100 | printf(" TYPE:%d", type); |
va009039 | 0:bdeec5f86894 | 101 | printfBytes(" RDATA:", &buf[rdata_pos], rdlength); |
va009039 | 0:bdeec5f86894 | 102 | } |
va009039 | 0:bdeec5f86894 | 103 | #endif //DEBUG |
va009039 | 0:bdeec5f86894 | 104 | } |
va009039 | 0:bdeec5f86894 | 105 | return rcode; |
va009039 | 0:bdeec5f86894 | 106 | } |
va009039 | 0:bdeec5f86894 | 107 | |
va009039 | 0:bdeec5f86894 | 108 | int MyNetDnsRequest::query(uint8_t buf[], int size, const char* hostname) { |
va009039 | 0:bdeec5f86894 | 109 | PRINT_FUNC(); |
va009039 | 0:bdeec5f86894 | 110 | const uint8_t header[] = { |
va009039 | 0:bdeec5f86894 | 111 | 0x00,0x00,0x01,0x00, // id=0x0000 QR=0 rd=1 opcode=0 rcode=0 |
va009039 | 0:bdeec5f86894 | 112 | 0x00,0x01,0x00,0x00, // qdcount=1 ancount=0 |
va009039 | 0:bdeec5f86894 | 113 | 0x00,0x00,0x00,0x00};// nscount=0 arcount=0 |
va009039 | 0:bdeec5f86894 | 114 | const uint8_t tail[] = {0x00,0x01,0x00,0x01}; // qtype=A qclass=IN |
va009039 | 0:bdeec5f86894 | 115 | memcpy(buf, header, sizeof(header)); |
va009039 | 0:bdeec5f86894 | 116 | int t = clock(); |
va009039 | 0:bdeec5f86894 | 117 | m_id[0] = t>>8; |
va009039 | 0:bdeec5f86894 | 118 | m_id[1] = t; |
va009039 | 0:bdeec5f86894 | 119 | memcpy(buf, m_id, 2); |
va009039 | 0:bdeec5f86894 | 120 | dnsname qname(buf); |
va009039 | 0:bdeec5f86894 | 121 | int pos = qname.encode(sizeof(header), (char*)hostname); |
va009039 | 0:bdeec5f86894 | 122 | memcpy(buf+pos, tail, sizeof(tail)); |
va009039 | 0:bdeec5f86894 | 123 | pos += sizeof(tail); |
va009039 | 0:bdeec5f86894 | 124 | return pos; |
va009039 | 0:bdeec5f86894 | 125 | } |
va009039 | 0:bdeec5f86894 | 126 | |
va009039 | 0:bdeec5f86894 | 127 | void MyNetDnsRequest::resolve(const char* hostname) { |
va009039 | 0:bdeec5f86894 | 128 | PRINT_FUNC(); |
va009039 | 0:bdeec5f86894 | 129 | if (m_udp == NULL) { |
va009039 | 0:bdeec5f86894 | 130 | m_udp = new UDPSocket; |
va009039 | 0:bdeec5f86894 | 131 | } |
va009039 | 0:bdeec5f86894 | 132 | m_udp->setOnEvent(this, &MyNetDnsRequest::callback); |
va009039 | 0:bdeec5f86894 | 133 | Host local(IpAddr(0,0,0,0), 1024 + rand()&0x7fff); |
va009039 | 0:bdeec5f86894 | 134 | IpAddr dns(8,8,8,8); |
va009039 | 0:bdeec5f86894 | 135 | NetIf* pIf = Net::getDefaultIf(); |
va009039 | 0:bdeec5f86894 | 136 | if (pIf) { |
va009039 | 0:bdeec5f86894 | 137 | dns = ((W5200NetIf*)pIf)->m_dns; |
va009039 | 0:bdeec5f86894 | 138 | } |
va009039 | 0:bdeec5f86894 | 139 | Host server(dns, 53); // DNS |
va009039 | 0:bdeec5f86894 | 140 | m_udp->bind(local); |
va009039 | 0:bdeec5f86894 | 141 | uint8_t buf[256]; |
va009039 | 0:bdeec5f86894 | 142 | int size = query(buf, sizeof(buf), hostname); |
va009039 | 0:bdeec5f86894 | 143 | #ifdef DEBUG |
va009039 | 0:bdeec5f86894 | 144 | printf("hostname:[%s]\n", hostname); |
va009039 | 0:bdeec5f86894 | 145 | printHex(buf, size); |
va009039 | 0:bdeec5f86894 | 146 | #endif |
va009039 | 0:bdeec5f86894 | 147 | m_udp->sendto((char*)buf, size, &server); |
va009039 | 0:bdeec5f86894 | 148 | m_interval.reset(); |
va009039 | 0:bdeec5f86894 | 149 | m_interval.start(); |
va009039 | 0:bdeec5f86894 | 150 | } |
va009039 | 0:bdeec5f86894 | 151 | |
va009039 | 0:bdeec5f86894 | 152 | void MyNetDnsRequest::poll() { |
va009039 | 0:bdeec5f86894 | 153 | PRINT_FUNC(); |
va009039 | 0:bdeec5f86894 | 154 | #ifdef DEBUG |
va009039 | 0:bdeec5f86894 | 155 | printf("%p m_state: %d, m_udp: %p\n", this, m_state, m_udp); |
va009039 | 0:bdeec5f86894 | 156 | wait_ms(400); |
va009039 | 0:bdeec5f86894 | 157 | #endif //DEBUG |
va009039 | 0:bdeec5f86894 | 158 | switch(m_state) { |
va009039 | 0:bdeec5f86894 | 159 | case MYNETDNS_START: |
va009039 | 0:bdeec5f86894 | 160 | m_retry = 0; |
va009039 | 0:bdeec5f86894 | 161 | resolve(m_hostname); |
va009039 | 0:bdeec5f86894 | 162 | m_state = MYNETDNS_PROCESSING; |
va009039 | 0:bdeec5f86894 | 163 | break; |
va009039 | 0:bdeec5f86894 | 164 | case MYNETDNS_PROCESSING: |
va009039 | 0:bdeec5f86894 | 165 | break; |
va009039 | 0:bdeec5f86894 | 166 | case MYNETDNS_NOTFOUND: |
va009039 | 0:bdeec5f86894 | 167 | onReply(NETDNS_FOUND); |
va009039 | 0:bdeec5f86894 | 168 | break; |
va009039 | 0:bdeec5f86894 | 169 | case MYNETDNS_ERROR: |
va009039 | 0:bdeec5f86894 | 170 | onReply(NETDNS_ERROR); |
va009039 | 0:bdeec5f86894 | 171 | break; |
va009039 | 0:bdeec5f86894 | 172 | case MYNETDNS_OK: |
va009039 | 0:bdeec5f86894 | 173 | DBG2("m_retry=%d, m_interval=%d\n", m_retry, m_interval.read_ms()); |
va009039 | 0:bdeec5f86894 | 174 | onReply(NETDNS_FOUND); |
va009039 | 0:bdeec5f86894 | 175 | break; |
va009039 | 0:bdeec5f86894 | 176 | } |
va009039 | 0:bdeec5f86894 | 177 | if (m_interval.read_ms() > 1000) { |
va009039 | 0:bdeec5f86894 | 178 | m_interval.stop(); |
va009039 | 0:bdeec5f86894 | 179 | DBG2("timeout m_retry=%d\n", m_retry); |
va009039 | 0:bdeec5f86894 | 180 | if (++m_retry >= 2) { |
va009039 | 0:bdeec5f86894 | 181 | m_state = MYNETDNS_ERROR; |
va009039 | 0:bdeec5f86894 | 182 | } else { |
va009039 | 0:bdeec5f86894 | 183 | resolve(m_hostname); |
va009039 | 0:bdeec5f86894 | 184 | m_state = MYNETDNS_PROCESSING; |
va009039 | 0:bdeec5f86894 | 185 | } |
va009039 | 0:bdeec5f86894 | 186 | } |
va009039 | 0:bdeec5f86894 | 187 | if(m_closing && (m_state!=MYNETDNS_PROCESSING)) { |
va009039 | 0:bdeec5f86894 | 188 | NetDnsRequest::close(); |
va009039 | 0:bdeec5f86894 | 189 | } |
va009039 | 0:bdeec5f86894 | 190 | } |
va009039 | 0:bdeec5f86894 | 191 | |
va009039 | 0:bdeec5f86894 | 192 | void MyNetDnsRequest::close() { |
va009039 | 0:bdeec5f86894 | 193 | PRINT_FUNC(); |
va009039 | 0:bdeec5f86894 | 194 | if(m_state != MYNETDNS_PROCESSING) { |
va009039 | 0:bdeec5f86894 | 195 | NetDnsRequest::close(); |
va009039 | 0:bdeec5f86894 | 196 | } else { |
va009039 | 0:bdeec5f86894 | 197 | m_closing = true; |
va009039 | 0:bdeec5f86894 | 198 | } |
va009039 | 0:bdeec5f86894 | 199 | } |