Ethernet interface for W5500 with bug fixed in socket::close()

Fork of EthernetInterfaceW5500 by W5500-Ethernet-Interface Makers

Committer:
ppo
Date:
Fri Aug 29 12:00:38 2014 +0000
Revision:
15:fe68ac753657
Parent:
10:cadac6bcd169
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 "UDPSocket.h"
samux 1:fb4494783863 20
ban4jp 9:615198a7b82b 21 static int udp_local_port;
ban4jp 9:615198a7b82b 22
samux 1:fb4494783863 23 UDPSocket::UDPSocket()
samux 1:fb4494783863 24 {
samux 1:fb4494783863 25 }
Bongjun 10:cadac6bcd169 26 // After init function, bind() should be called.
samux 1:fb4494783863 27 int UDPSocket::init(void)
samux 1:fb4494783863 28 {
va009039 5:fb15c35d1e28 29 if (_sock_fd < 0) {
va009039 5:fb15c35d1e28 30 _sock_fd = eth->new_socket();
va009039 5:fb15c35d1e28 31 }
Bongjun 10:cadac6bcd169 32 if (eth->setProtocol(_sock_fd, UDP) == false) return -1;
samux 1:fb4494783863 33 return 0;
samux 1:fb4494783863 34 }
samux 1:fb4494783863 35
samux 1:fb4494783863 36 // Server initialization
samux 1:fb4494783863 37 int UDPSocket::bind(int port)
samux 1:fb4494783863 38 {
va009039 5:fb15c35d1e28 39 if (_sock_fd < 0) {
va009039 5:fb15c35d1e28 40 _sock_fd = eth->new_socket();
va009039 5:fb15c35d1e28 41 if (_sock_fd < 0) {
va009039 5:fb15c35d1e28 42 return -1;
va009039 5:fb15c35d1e28 43 }
va009039 5:fb15c35d1e28 44 }
samux 1:fb4494783863 45 // set local port
ban4jp 9:615198a7b82b 46 if (port != 0) {
ban4jp 9:615198a7b82b 47 eth->sreg<uint16_t>(_sock_fd, Sn_PORT, port);
ban4jp 9:615198a7b82b 48 } else {
ban4jp 9:615198a7b82b 49 udp_local_port++;
ban4jp 9:615198a7b82b 50 eth->sreg<uint16_t>(_sock_fd, Sn_PORT, udp_local_port);
ban4jp 9:615198a7b82b 51 }
samux 4:0bcec6272784 52 // set udp protocol
va009039 5:fb15c35d1e28 53 eth->setProtocol(_sock_fd, UDP);
va009039 5:fb15c35d1e28 54 eth->scmd(_sock_fd, OPEN);
samux 1:fb4494783863 55 return 0;
samux 1:fb4494783863 56 }
samux 1:fb4494783863 57
samux 1:fb4494783863 58 // -1 if unsuccessful, else number of bytes written
samux 1:fb4494783863 59 int UDPSocket::sendTo(Endpoint &remote, char *packet, int length)
samux 1:fb4494783863 60 {
va009039 5:fb15c35d1e28 61 int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout, length-1);
va009039 5:fb15c35d1e28 62 if (size < 0) {
va009039 5:fb15c35d1e28 63 return -1;
va009039 5:fb15c35d1e28 64 }
samux 1:fb4494783863 65 confEndpoint(remote);
va009039 5:fb15c35d1e28 66 int ret = eth->send(_sock_fd, packet, length);
va009039 5:fb15c35d1e28 67 return ret;
samux 1:fb4494783863 68 }
samux 1:fb4494783863 69
samux 1:fb4494783863 70 // -1 if unsuccessful, else number of bytes received
samux 1:fb4494783863 71 int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length)
samux 1:fb4494783863 72 {
va009039 5:fb15c35d1e28 73 uint8_t info[8];
va009039 5:fb15c35d1e28 74 int size = eth->wait_readable(_sock_fd, _blocking ? -1 : _timeout, sizeof(info));
va009039 5:fb15c35d1e28 75 if (size < 0) {
va009039 5:fb15c35d1e28 76 return -1;
samux 1:fb4494783863 77 }
va009039 5:fb15c35d1e28 78 eth->recv(_sock_fd, (char*)info, sizeof(info));
va009039 5:fb15c35d1e28 79 readEndpoint(remote, info);
Bongjun 10:cadac6bcd169 80 int udp_size = info[6]<<8|info[7];
va009039 5:fb15c35d1e28 81 //TEST_ASSERT(udp_size <= (size-sizeof(info)));
va009039 5:fb15c35d1e28 82 if (udp_size > (size-sizeof(info))) {
va009039 5:fb15c35d1e28 83 return -1;
samux 1:fb4494783863 84 }
va009039 5:fb15c35d1e28 85 return eth->recv(_sock_fd, buffer, udp_size);
samux 1:fb4494783863 86 }
samux 1:fb4494783863 87
va009039 5:fb15c35d1e28 88 void UDPSocket::confEndpoint(Endpoint & ep)
samux 1:fb4494783863 89 {
va009039 5:fb15c35d1e28 90 char * host = ep.get_address();
va009039 5:fb15c35d1e28 91 // set remote host
va009039 5:fb15c35d1e28 92 eth->sreg_ip(_sock_fd, Sn_DIPR, host);
va009039 5:fb15c35d1e28 93 // set remote port
va009039 5:fb15c35d1e28 94 eth->sreg<uint16_t>(_sock_fd, Sn_DPORT, ep.get_port());
samux 1:fb4494783863 95 }
samux 1:fb4494783863 96
va009039 5:fb15c35d1e28 97 void UDPSocket::readEndpoint(Endpoint & ep, uint8_t info[])
samux 1:fb4494783863 98 {
va009039 5:fb15c35d1e28 99 char addr[17];
va009039 5:fb15c35d1e28 100 snprintf(addr, sizeof(addr), "%d.%d.%d.%d", info[0], info[1], info[2], info[3]);
va009039 5:fb15c35d1e28 101 uint16_t port = info[4]<<8|info[5];
va009039 5:fb15c35d1e28 102 ep.set_address(addr, port);
samux 1:fb4494783863 103 }