Ethernet interface for W5500 with bug fixed in socket::close()

Fork of EthernetInterfaceW5500 by W5500-Ethernet-Interface Makers

Committer:
va009039
Date:
Tue Aug 27 12:50:11 2013 +0000
Revision:
5:fb15c35d1e28
Parent:
4:0bcec6272784
Child:
10:cadac6bcd169
WIZ820ioInterface?first commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 1:fb4494783863 1 /* Copyright (C) 2012 mbed.org, MIT License
samux 1:fb4494783863 2 *
samux 1:fb4494783863 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
samux 1:fb4494783863 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
samux 1:fb4494783863 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
samux 1:fb4494783863 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
samux 1:fb4494783863 7 * furnished to do so, subject to the following conditions:
samux 1:fb4494783863 8 *
samux 1:fb4494783863 9 * The above copyright notice and this permission notice shall be included in all copies or
samux 1:fb4494783863 10 * substantial portions of the Software.
samux 1:fb4494783863 11 *
samux 1:fb4494783863 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
samux 1:fb4494783863 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
samux 1:fb4494783863 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
samux 1:fb4494783863 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
samux 1:fb4494783863 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
samux 1:fb4494783863 17 */
samux 1:fb4494783863 18
samux 1:fb4494783863 19 #include "TCPSocketServer.h"
samux 1:fb4494783863 20
samux 1:fb4494783863 21 TCPSocketServer::TCPSocketServer() {}
samux 1:fb4494783863 22
samux 1:fb4494783863 23 // Server initialization
samux 1:fb4494783863 24 int TCPSocketServer::bind(int port) {
va009039 5:fb15c35d1e28 25 if (_sock_fd < 0) {
va009039 5:fb15c35d1e28 26 _sock_fd = eth->new_socket();
va009039 5:fb15c35d1e28 27 if (_sock_fd < 0) {
va009039 5:fb15c35d1e28 28 return -1;
va009039 5:fb15c35d1e28 29 }
va009039 5:fb15c35d1e28 30 }
samux 1:fb4494783863 31 // set TCP protocol
va009039 5:fb15c35d1e28 32 eth->setProtocol(_sock_fd, TCP);
samux 1:fb4494783863 33 // set local port
va009039 5:fb15c35d1e28 34 eth->sreg<uint16_t>(_sock_fd, Sn_PORT, port);
samux 3:9aa05e19c62e 35 // connect the network
va009039 5:fb15c35d1e28 36 eth->scmd(_sock_fd, OPEN);
samux 1:fb4494783863 37 return 0;
samux 1:fb4494783863 38 }
samux 1:fb4494783863 39
samux 1:fb4494783863 40 int TCPSocketServer::listen(int backlog) {
va009039 5:fb15c35d1e28 41 if (_sock_fd < 0) {
samux 1:fb4494783863 42 return -1;
va009039 5:fb15c35d1e28 43 }
va009039 5:fb15c35d1e28 44 if (backlog != 1) {
va009039 5:fb15c35d1e28 45 return -1;
va009039 5:fb15c35d1e28 46 }
va009039 5:fb15c35d1e28 47 eth->scmd(_sock_fd, LISTEN);
samux 1:fb4494783863 48 return 0;
samux 1:fb4494783863 49 }
samux 1:fb4494783863 50
samux 1:fb4494783863 51
samux 1:fb4494783863 52 int TCPSocketServer::accept(TCPSocketConnection& connection) {
va009039 5:fb15c35d1e28 53 if (_sock_fd < 0) {
va009039 5:fb15c35d1e28 54 return -1;
va009039 5:fb15c35d1e28 55 }
va009039 5:fb15c35d1e28 56 Timer t;
va009039 5:fb15c35d1e28 57 t.reset();
va009039 5:fb15c35d1e28 58 t.start();
va009039 5:fb15c35d1e28 59 while(1) {
va009039 5:fb15c35d1e28 60 if (t.read_ms() > _timeout && _blocking == false) {
va009039 5:fb15c35d1e28 61 return -1;
va009039 5:fb15c35d1e28 62 }
va009039 5:fb15c35d1e28 63 if (eth->sreg<uint8_t>(_sock_fd, Sn_SR) == SOCK_ESTABLISHED) {
va009039 5:fb15c35d1e28 64 break;
samux 1:fb4494783863 65 }
samux 1:fb4494783863 66 }
va009039 5:fb15c35d1e28 67 uint32_t ip = eth->sreg<uint32_t>(_sock_fd, Sn_DIPR);
va009039 5:fb15c35d1e28 68 char host[16];
va009039 5:fb15c35d1e28 69 snprintf(host, sizeof(host), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
va009039 5:fb15c35d1e28 70 uint16_t port = eth->sreg<uint16_t>(_sock_fd, Sn_DPORT);
va009039 5:fb15c35d1e28 71 connection._sock_fd = _sock_fd;
va009039 5:fb15c35d1e28 72 connection.set_address(host, port);
va009039 5:fb15c35d1e28 73 return 0;
va009039 5:fb15c35d1e28 74 }