Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: HTTP_SDcard_file_server_WIZwiki-W7500 SSD1306_smart_watch TCPEchoServer-WIZwiki-W7500 httpServer-WIZwiki-W7500 ... more
Fork of WIZnetInterface by
This is WIZnet Ethernet Interface using Hardware TCP/IP chip, W5500 and TCP/IP Offload Engine, W7500.
[Users » embeddist » Code » WIZnetInterface](https://developer.mbed.org/users/embeddist/code/WIZnetInterface/) -> WIZnetInterface Lib will be released on [Team WIZnet](https://developer.mbed.org/teams/WIZnet/)
- WIZwiki_W7500 : /platforms/WIZwiki-W7500/

- WIZwiki_W7500P : /platforms/WIZwiki-W7500P/

- WIZwiki_W7500ECO : /platforms/WIZwiki-W7500ECO/

- W5500 Ethernet Shield : /components/W5500-Ethernet-Kit-for-IoT/

This library is an Ethernet Interface library port-based on [EthernetInterface](https://developer.mbed.org/users/mbed_official/code/EthernetInterface/docs/tip/).
For more detail, visit http://embeddist.blogspot.kr/2015/06/wiznetinterface-for-armmbed.html
Socket/UDPSocket.cpp
- Committer:
- Soohwan Kim
- Date:
- 2015-06-16
- Revision:
- 12:99e8386ba225
- Parent:
- 0:6f28332c466f
File content as of revision 12:99e8386ba225:
/* 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"
static int udp_local_port;
UDPSocket::UDPSocket()
{
}
// After init function, bind() should be called.
int UDPSocket::init(void)
{
if (_sock_fd < 0) {
_sock_fd = eth->new_socket();
}
if (eth->setProtocol(_sock_fd, WIZnet_Chip::UDP) == false) return -1;
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
if (port != 0) {
eth->sreg<uint16_t>(_sock_fd, Sn_PORT, port);
} else {
udp_local_port++;
eth->sreg<uint16_t>(_sock_fd, Sn_PORT, udp_local_port);
}
// set udp protocol
eth->setProtocol(_sock_fd, WIZnet_Chip::UDP);
eth->scmd(_sock_fd, WIZnet_Chip::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;
}
/* Perform Length check here to prevent buffer overrun */
/* fixed by Sean Newton (https://developer.mbed.org/users/SeanNewton/) */
if (udp_size > length) {
//printf("udp_size: %d\n",udp_size);
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);
}

Network-Shield W5500
M0-Switch WIZnet W7500
Serial-to-Ethernet Controller WIZ750SR