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.

Dependencies:   mbed

Fork of WIZnet_Library by WIZnet

Committer:
Bongjun
Date:
Thu Jul 10 04:55:47 2014 +0000
Revision:
3:9997e2b7d459
Parent:
0:b72d22e10709
fix Sn_IR read error

Who changed what in which revision?

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