No more update~~ please use W5500Interface.

Fork of EthernetInterfaceW5500 by Bongjun Hur


Bongjun Hur 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.

Committer:
ppo
Date:
Fri Aug 29 12:00:38 2014 +0000
Revision:
15:fe68ac753657
Parent:
13:6f469f76a76a
bug fixed in close(), now socket is really closed

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
Bongjun 10:cadac6bcd169 24 int TCPSocketServer::bind(int port)
Bongjun 10:cadac6bcd169 25 {
Bongjun 12:d130abe9d4a8 26 listen_port = port;
va009039 5:fb15c35d1e28 27 if (_sock_fd < 0) {
va009039 5:fb15c35d1e28 28 _sock_fd = eth->new_socket();
va009039 5:fb15c35d1e28 29 if (_sock_fd < 0) {
va009039 5:fb15c35d1e28 30 return -1;
va009039 5:fb15c35d1e28 31 }
va009039 5:fb15c35d1e28 32 }
samux 1:fb4494783863 33 // set TCP protocol
va009039 5:fb15c35d1e28 34 eth->setProtocol(_sock_fd, TCP);
samux 1:fb4494783863 35 // set local port
va009039 5:fb15c35d1e28 36 eth->sreg<uint16_t>(_sock_fd, Sn_PORT, port);
samux 3:9aa05e19c62e 37 // connect the network
va009039 5:fb15c35d1e28 38 eth->scmd(_sock_fd, OPEN);
samux 1:fb4494783863 39 return 0;
samux 1:fb4494783863 40 }
samux 1:fb4494783863 41
Bongjun 10:cadac6bcd169 42 int TCPSocketServer::listen(int backlog)
Bongjun 10:cadac6bcd169 43 {
va009039 5:fb15c35d1e28 44 if (_sock_fd < 0) {
samux 1:fb4494783863 45 return -1;
va009039 5:fb15c35d1e28 46 }
va009039 5:fb15c35d1e28 47 if (backlog != 1) {
va009039 5:fb15c35d1e28 48 return -1;
va009039 5:fb15c35d1e28 49 }
va009039 5:fb15c35d1e28 50 eth->scmd(_sock_fd, LISTEN);
samux 1:fb4494783863 51 return 0;
samux 1:fb4494783863 52 }
samux 1:fb4494783863 53
samux 1:fb4494783863 54
Bongjun 10:cadac6bcd169 55 int TCPSocketServer::accept(TCPSocketConnection& connection)
Bongjun 10:cadac6bcd169 56 {
va009039 5:fb15c35d1e28 57 if (_sock_fd < 0) {
va009039 5:fb15c35d1e28 58 return -1;
va009039 5:fb15c35d1e28 59 }
va009039 5:fb15c35d1e28 60 Timer t;
va009039 5:fb15c35d1e28 61 t.reset();
va009039 5:fb15c35d1e28 62 t.start();
va009039 5:fb15c35d1e28 63 while(1) {
va009039 5:fb15c35d1e28 64 if (t.read_ms() > _timeout && _blocking == false) {
va009039 5:fb15c35d1e28 65 return -1;
va009039 5:fb15c35d1e28 66 }
va009039 5:fb15c35d1e28 67 if (eth->sreg<uint8_t>(_sock_fd, Sn_SR) == SOCK_ESTABLISHED) {
va009039 5:fb15c35d1e28 68 break;
samux 1:fb4494783863 69 }
samux 1:fb4494783863 70 }
va009039 5:fb15c35d1e28 71 uint32_t ip = eth->sreg<uint32_t>(_sock_fd, Sn_DIPR);
va009039 5:fb15c35d1e28 72 char host[16];
va009039 5:fb15c35d1e28 73 snprintf(host, sizeof(host), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
va009039 5:fb15c35d1e28 74 uint16_t port = eth->sreg<uint16_t>(_sock_fd, Sn_DPORT);
Bongjun 12:d130abe9d4a8 75
Bongjun 12:d130abe9d4a8 76 // change this server socket to connection socket.
va009039 5:fb15c35d1e28 77 connection._sock_fd = _sock_fd;
Bongjun 10:cadac6bcd169 78 connection._is_connected = true;
va009039 5:fb15c35d1e28 79 connection.set_address(host, port);
Bongjun 12:d130abe9d4a8 80
Bongjun 12:d130abe9d4a8 81 // and then, for the next connection, server socket should be assigned new one.
Bongjun 12:d130abe9d4a8 82 _sock_fd = -1; // want to assign new available _sock_fd.
Bongjun 12:d130abe9d4a8 83 if(bind(listen_port) < 0) {
ppo 15:fe68ac753657 84 error("No more socket for listening, bind error");
ppo 15:fe68ac753657 85 return -1;
Bongjun 12:d130abe9d4a8 86 } else {
Bongjun 12:d130abe9d4a8 87 //return -1;
Bongjun 12:d130abe9d4a8 88 if(listen(1) < 0) {
ppo 15:fe68ac753657 89 error("No more socket for listening, listen error");
ppo 15:fe68ac753657 90 return -1;
Bongjun 12:d130abe9d4a8 91 }
ppo 15:fe68ac753657 92
Bongjun 12:d130abe9d4a8 93 }
va009039 5:fb15c35d1e28 94 return 0;
va009039 5:fb15c35d1e28 95 }