No more update~~ please use W5500Interface.
Fork of EthernetInterfaceW5500 by
wrote:
NO more update for this library.
Please move to this page W5500Interface for newer version.
Import libraryW5500Interface
This is the Interface library for WIZnet W5500 chip which forked of EthernetInterfaceW5500, WIZnetInterface and WIZ550ioInterface. This library has simple name as "W5500Interface". and can be used for Wiz550io users also.
This Library for W5500 users. no need to use lwIP(or S/W TCP/IP) Some update & code clean for W5500 only refer from WIZ550ioInterface, WIZnetLibrary and WiflyInterface.
Thanks for ban4jp. This library forks of WIZ550ioInterface.
Diff: Socket/TCPSocketServer.cpp
- Revision:
- 5:fb15c35d1e28
- Parent:
- 4:0bcec6272784
- Child:
- 10:cadac6bcd169
diff -r 0bcec6272784 -r fb15c35d1e28 Socket/TCPSocketServer.cpp --- a/Socket/TCPSocketServer.cpp Thu Dec 20 15:08:58 2012 +0000 +++ b/Socket/TCPSocketServer.cpp Tue Aug 27 12:50:11 2013 +0000 @@ -17,74 +17,58 @@ */ #include "TCPSocketServer.h" -#include <string> TCPSocketServer::TCPSocketServer() {} // Server initialization int TCPSocketServer::bind(int port) { - char cmd[20]; - + if (_sock_fd < 0) { + _sock_fd = eth->new_socket(); + if (_sock_fd < 0) { + return -1; + } + } // set TCP protocol - wifi->setProtocol(TCP); - + eth->setProtocol(_sock_fd, TCP); // set local port - sprintf(cmd, "set i l %d\r", port); - if (!wifi->sendCommand(cmd, "AOK")) - return -1; - - // save - if (!wifi->sendCommand("save\r", "Stor")) - return -1; - - // reboot - wifi->reboot(); - + eth->sreg<uint16_t>(_sock_fd, Sn_PORT, port); // connect the network - if (wifi->isDHCP()) { - if (!wifi->sendCommand("join\r", "DHCP=ON", NULL, 10000)) - return -1; - } else { - if (!wifi->sendCommand("join\r", "Associated", NULL, 10000)) - return -1; - } - - // exit - wifi->exit(); - - wait(0.2); - wifi->flush(); + eth->scmd(_sock_fd, OPEN); return 0; } int TCPSocketServer::listen(int backlog) { - if (backlog != 1) + if (_sock_fd < 0) { return -1; + } + if (backlog != 1) { + return -1; + } + eth->scmd(_sock_fd, LISTEN); return 0; } int TCPSocketServer::accept(TCPSocketConnection& connection) { - int nb_available = 0, pos = 0; - char c; - string str; - bool o_find = false; - while (1) { - while(!wifi->readable()); - nb_available = wifi->readable(); - for (int i = 0; i < nb_available; i++) { - c = wifi->getc(); - if (c == '*') { - o_find = true; - } - if (o_find && c != '\r' && c != '\n') { - str += c; - pos = str.find("*OPEN*"); - if (pos != string::npos) { - wifi->flush(); - return 0; - } - } + if (_sock_fd < 0) { + return -1; + } + Timer t; + t.reset(); + t.start(); + while(1) { + if (t.read_ms() > _timeout && _blocking == false) { + return -1; + } + if (eth->sreg<uint8_t>(_sock_fd, Sn_SR) == SOCK_ESTABLISHED) { + break; } } -} \ No newline at end of file + uint32_t ip = eth->sreg<uint32_t>(_sock_fd, Sn_DIPR); + char host[16]; + snprintf(host, sizeof(host), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff); + uint16_t port = eth->sreg<uint16_t>(_sock_fd, Sn_DPORT); + connection._sock_fd = _sock_fd; + connection.set_address(host, port); + return 0; +}