Ethernet interface for W5500 with bug fixed in socket::close()
Fork of EthernetInterfaceW5500 by
Socket/TCPSocketServer.cpp@10:cadac6bcd169, 2014-07-17 (annotated)
- Committer:
- Bongjun
- Date:
- Thu Jul 17 07:10:36 2014 +0000
- Revision:
- 10:cadac6bcd169
- Parent:
- 5:fb15c35d1e28
- Child:
- 12:d130abe9d4a8
Some update & code clean for W5500 only refer from WIZ550ioInterface, WIZnetLibrary and WiflyInterface.
Who changed what in which revision?
User | Revision | Line number | New 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 | { |
va009039 | 5:fb15c35d1e28 | 26 | if (_sock_fd < 0) { |
va009039 | 5:fb15c35d1e28 | 27 | _sock_fd = eth->new_socket(); |
va009039 | 5:fb15c35d1e28 | 28 | if (_sock_fd < 0) { |
va009039 | 5:fb15c35d1e28 | 29 | return -1; |
va009039 | 5:fb15c35d1e28 | 30 | } |
va009039 | 5:fb15c35d1e28 | 31 | } |
samux | 1:fb4494783863 | 32 | // set TCP protocol |
va009039 | 5:fb15c35d1e28 | 33 | eth->setProtocol(_sock_fd, TCP); |
samux | 1:fb4494783863 | 34 | // set local port |
va009039 | 5:fb15c35d1e28 | 35 | eth->sreg<uint16_t>(_sock_fd, Sn_PORT, port); |
samux | 3:9aa05e19c62e | 36 | // connect the network |
va009039 | 5:fb15c35d1e28 | 37 | eth->scmd(_sock_fd, OPEN); |
samux | 1:fb4494783863 | 38 | return 0; |
samux | 1:fb4494783863 | 39 | } |
samux | 1:fb4494783863 | 40 | |
Bongjun | 10:cadac6bcd169 | 41 | int TCPSocketServer::listen(int backlog) |
Bongjun | 10:cadac6bcd169 | 42 | { |
va009039 | 5:fb15c35d1e28 | 43 | if (_sock_fd < 0) { |
samux | 1:fb4494783863 | 44 | return -1; |
va009039 | 5:fb15c35d1e28 | 45 | } |
va009039 | 5:fb15c35d1e28 | 46 | if (backlog != 1) { |
va009039 | 5:fb15c35d1e28 | 47 | return -1; |
va009039 | 5:fb15c35d1e28 | 48 | } |
va009039 | 5:fb15c35d1e28 | 49 | eth->scmd(_sock_fd, LISTEN); |
samux | 1:fb4494783863 | 50 | return 0; |
samux | 1:fb4494783863 | 51 | } |
samux | 1:fb4494783863 | 52 | |
samux | 1:fb4494783863 | 53 | |
Bongjun | 10:cadac6bcd169 | 54 | int TCPSocketServer::accept(TCPSocketConnection& connection) |
Bongjun | 10:cadac6bcd169 | 55 | { |
va009039 | 5:fb15c35d1e28 | 56 | if (_sock_fd < 0) { |
va009039 | 5:fb15c35d1e28 | 57 | return -1; |
va009039 | 5:fb15c35d1e28 | 58 | } |
va009039 | 5:fb15c35d1e28 | 59 | Timer t; |
va009039 | 5:fb15c35d1e28 | 60 | t.reset(); |
va009039 | 5:fb15c35d1e28 | 61 | t.start(); |
va009039 | 5:fb15c35d1e28 | 62 | while(1) { |
va009039 | 5:fb15c35d1e28 | 63 | if (t.read_ms() > _timeout && _blocking == false) { |
va009039 | 5:fb15c35d1e28 | 64 | return -1; |
va009039 | 5:fb15c35d1e28 | 65 | } |
va009039 | 5:fb15c35d1e28 | 66 | if (eth->sreg<uint8_t>(_sock_fd, Sn_SR) == SOCK_ESTABLISHED) { |
va009039 | 5:fb15c35d1e28 | 67 | break; |
samux | 1:fb4494783863 | 68 | } |
samux | 1:fb4494783863 | 69 | } |
va009039 | 5:fb15c35d1e28 | 70 | uint32_t ip = eth->sreg<uint32_t>(_sock_fd, Sn_DIPR); |
va009039 | 5:fb15c35d1e28 | 71 | char host[16]; |
va009039 | 5:fb15c35d1e28 | 72 | snprintf(host, sizeof(host), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff); |
va009039 | 5:fb15c35d1e28 | 73 | uint16_t port = eth->sreg<uint16_t>(_sock_fd, Sn_DPORT); |
va009039 | 5:fb15c35d1e28 | 74 | connection._sock_fd = _sock_fd; |
Bongjun | 10:cadac6bcd169 | 75 | connection._is_connected = true; |
va009039 | 5:fb15c35d1e28 | 76 | connection.set_address(host, port); |
va009039 | 5:fb15c35d1e28 | 77 | return 0; |
va009039 | 5:fb15c35d1e28 | 78 | } |