This is WIZnet Ethernet Interface using Hardware TCP/IP chip, W5500, W5200 and W5100. One of them can be selected by enabling it in wiznet.h.

Fork of WIZnet_Library by WIZnet

Committer:
Bongjun
Date:
Sun May 31 10:06:41 2015 +0000
Revision:
7:7c67a8922ec5
Parent:
3:48348a6eaa72
add set_blocking(false) in connect function, and so on.

Who changed what in which revision?

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