WIZ820io(W5200) network interface, EthernetInterface compatible.

Dependents:   Seeed_Ethernet_Shield_V2_HelloWorld Seeed_Ethernet_Shield Cayenne-WIZ820ioInterface Seeed_Ethernet_Shield

Fork of WiflyInterface by mbed official

WIZ820io

Socket/UDPSocket.cpp

Committer:
va009039
Date:
2013-08-27
Revision:
5:fb15c35d1e28
Parent:
4:0bcec6272784

File content as of revision 5:fb15c35d1e28:

/* Copyright (C) 2012 mbed.org, MIT License
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
 * and associated documentation files (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#include "UDPSocket.h"

UDPSocket::UDPSocket()
{
}

int UDPSocket::init(void)
{
    if (_sock_fd < 0) {
        _sock_fd = eth->new_socket();
    }
    eth->setProtocol(_sock_fd, UDP);
    return 0;
}

// Server initialization
int UDPSocket::bind(int port)
{
    if (_sock_fd < 0) {
        _sock_fd = eth->new_socket();
        if (_sock_fd < 0) {
            return -1;
        }
    }
    // set local port
    eth->sreg<uint16_t>(_sock_fd, Sn_PORT, port);
    // set udp protocol
    eth->setProtocol(_sock_fd, UDP);
    eth->scmd(_sock_fd, OPEN);
    return 0;
}

// -1 if unsuccessful, else number of bytes written
int UDPSocket::sendTo(Endpoint &remote, char *packet, int length)
{
    int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout, length-1);
    if (size < 0) {
        return -1;
    }
    confEndpoint(remote);
    int ret = eth->send(_sock_fd, packet, length);
    return ret;
}

// -1 if unsuccessful, else number of bytes received
int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length)
{
    uint8_t info[8];
    int size = eth->wait_readable(_sock_fd, _blocking ? -1 : _timeout, sizeof(info));
    if (size < 0) {
        return -1;
    }
    eth->recv(_sock_fd, (char*)info, sizeof(info));
    readEndpoint(remote, info);
    int udp_size = info[6]<<8|info[7]; 
    //TEST_ASSERT(udp_size <= (size-sizeof(info)));
    if (udp_size > (size-sizeof(info))) {
        return -1;
    }
    return eth->recv(_sock_fd, buffer, udp_size);
}

void UDPSocket::confEndpoint(Endpoint & ep)
{
    char * host = ep.get_address();
    // set remote host
    eth->sreg_ip(_sock_fd, Sn_DIPR, host);
    // set remote port
    eth->sreg<uint16_t>(_sock_fd, Sn_DPORT, ep.get_port());
}

void UDPSocket::readEndpoint(Endpoint & ep, uint8_t info[])
{
    char addr[17];
    snprintf(addr, sizeof(addr), "%d.%d.%d.%d", info[0], info[1], info[2], info[3]);
    uint16_t port = info[4]<<8|info[5];
    ep.set_address(addr, port);
}