WIZNet W5500 with additional enhancements

Fork of WIZnetInterface by WIZnet

Committer:
Helmut Tschemernjak
Date:
Mon Oct 09 19:58:19 2017 +0200
Revision:
34:7d44648ec5f2
Parent:
32:f6d76a55a50b
Added support for manual DNS server config or DHCP DNS config.
Now the DNS 8.8.8.8 is only a fallback if no DNS server is specified.
Replaced error() messages with printf() to avoid hanging code in error()

Who changed what in which revision?

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