no upgrade or change at this. move to new Library for WIZ550io, W5500 -> http://mbed.org/teams/EthernetInterfaceW5500-makers/code/W5500Interface/

Dependents:   LPC11U68_NTPClient_HelloWorld_WIZ550io

Fork of WIZ550ioInterface by ban4jp -

please get the new Library for WIZ550io, W5500 (WIZnet) http://mbed.org/teams/EthernetInterfaceW5500-makers/code/W5500Interface/

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.

Committer:
Bongjun
Date:
Tue Jul 08 03:49:33 2014 +0000
Revision:
11:5a5a3f373a6b
Parent:
9:615198a7b82b
update some code after comparing WIZnet Library

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