For mbed OS-5 version for WIZnet Ethernet Interface, this is Library using Hardware TCP/IP chip, W5500 and TCP/IP Offload Engine, W7500.

Dependents:   ledMapperTest

Warning

  • If you want to use existing codes, you need to change the class used as EthernetInterface to WIZnetInterface.

This is WIZnet Ethernet Interface using Hardware TCP/IP chip, W5500 and TCP/IP Offload Engine, W7500.

https://developer.mbed.org/media/cache/platforms/WIZwiki_W7500_enabled.JPG.250x250_q85.jpg

https://developer.mbed.org/media/cache/platforms/WIZwiki_W7500P_enabled2.JPG.250x250_q85.jpg

https://developer.mbed.org/media/cache/platforms/WIZwiki_W7500ECO_enabled2.JPG.250x250_q85.jpg

https://developer.mbed.org/media/cache/components/components/fetch.phpmediaoshw5500_ethernet_shieldw5500_main_picture2.png.200x200_q85.jpg

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

Committer:
justinkim
Date:
Mon Sep 04 00:23:04 2017 +0000
Revision:
0:d4c8fe4d9b29
mbed OS 5 version migration...

Who changed what in which revision?

UserRevisionLine numberNew contents of line
justinkim 0:d4c8fe4d9b29 1 /* Copyright (C) 2012 mbed.org, MIT License
justinkim 0:d4c8fe4d9b29 2 *
justinkim 0:d4c8fe4d9b29 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
justinkim 0:d4c8fe4d9b29 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
justinkim 0:d4c8fe4d9b29 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
justinkim 0:d4c8fe4d9b29 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
justinkim 0:d4c8fe4d9b29 7 * furnished to do so, subject to the following conditions:
justinkim 0:d4c8fe4d9b29 8 *
justinkim 0:d4c8fe4d9b29 9 * The above copyright notice and this permission notice shall be included in all copies or
justinkim 0:d4c8fe4d9b29 10 * substantial portions of the Software.
justinkim 0:d4c8fe4d9b29 11 *
justinkim 0:d4c8fe4d9b29 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
justinkim 0:d4c8fe4d9b29 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
justinkim 0:d4c8fe4d9b29 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
justinkim 0:d4c8fe4d9b29 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
justinkim 0:d4c8fe4d9b29 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
justinkim 0:d4c8fe4d9b29 17 */
justinkim 0:d4c8fe4d9b29 18
justinkim 0:d4c8fe4d9b29 19 #include "WIZnet_UDPSocket.h"
justinkim 0:d4c8fe4d9b29 20
justinkim 0:d4c8fe4d9b29 21 static int udp_local_port;
justinkim 0:d4c8fe4d9b29 22
justinkim 0:d4c8fe4d9b29 23 WIZnet_UDPSocket::WIZnet_UDPSocket()
justinkim 0:d4c8fe4d9b29 24 {
justinkim 0:d4c8fe4d9b29 25 }
justinkim 0:d4c8fe4d9b29 26 // After init function, bind() should be called.
justinkim 0:d4c8fe4d9b29 27 int WIZnet_UDPSocket::init(void)
justinkim 0:d4c8fe4d9b29 28 {
justinkim 0:d4c8fe4d9b29 29 if (_sock_fd < 0) {
justinkim 0:d4c8fe4d9b29 30 _sock_fd = eth->new_socket();
justinkim 0:d4c8fe4d9b29 31 }
justinkim 0:d4c8fe4d9b29 32 if (eth->setProtocol(_sock_fd, WIZnet_Chip::UDP) == false) return -1;
justinkim 0:d4c8fe4d9b29 33 return 0;
justinkim 0:d4c8fe4d9b29 34 }
justinkim 0:d4c8fe4d9b29 35
justinkim 0:d4c8fe4d9b29 36 // Server initialization
justinkim 0:d4c8fe4d9b29 37 int WIZnet_UDPSocket::bind(int port)
justinkim 0:d4c8fe4d9b29 38 {
justinkim 0:d4c8fe4d9b29 39 if (_sock_fd < 0) {
justinkim 0:d4c8fe4d9b29 40 _sock_fd = eth->new_socket();
justinkim 0:d4c8fe4d9b29 41 if (_sock_fd < 0) {
justinkim 0:d4c8fe4d9b29 42 return -1;
justinkim 0:d4c8fe4d9b29 43 }
justinkim 0:d4c8fe4d9b29 44 }
justinkim 0:d4c8fe4d9b29 45 // set local port
justinkim 0:d4c8fe4d9b29 46 if (port != 0) {
justinkim 0:d4c8fe4d9b29 47 eth->sreg<uint16_t>(_sock_fd, Sn_PORT, port);
justinkim 0:d4c8fe4d9b29 48 } else {
justinkim 0:d4c8fe4d9b29 49 udp_local_port++;
justinkim 0:d4c8fe4d9b29 50 eth->sreg<uint16_t>(_sock_fd, Sn_PORT, udp_local_port);
justinkim 0:d4c8fe4d9b29 51 }
justinkim 0:d4c8fe4d9b29 52 // set udp protocol
justinkim 0:d4c8fe4d9b29 53 eth->setProtocol(_sock_fd, WIZnet_Chip::UDP);
justinkim 0:d4c8fe4d9b29 54 eth->scmd(_sock_fd, WIZnet_Chip::OPEN);
justinkim 0:d4c8fe4d9b29 55 return 0;
justinkim 0:d4c8fe4d9b29 56 }
justinkim 0:d4c8fe4d9b29 57
justinkim 0:d4c8fe4d9b29 58 // -1 if unsuccessful, else number of bytes written
justinkim 0:d4c8fe4d9b29 59 int WIZnet_UDPSocket::sendTo(Endpoint &remote, char *packet, int length)
justinkim 0:d4c8fe4d9b29 60 {
justinkim 0:d4c8fe4d9b29 61 int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout, length-1);
justinkim 0:d4c8fe4d9b29 62 if (size < 0) {
justinkim 0:d4c8fe4d9b29 63 return -1;
justinkim 0:d4c8fe4d9b29 64 }
justinkim 0:d4c8fe4d9b29 65 confEndpoint(remote);
justinkim 0:d4c8fe4d9b29 66 int ret = eth->send(_sock_fd, packet, length);
justinkim 0:d4c8fe4d9b29 67 return ret;
justinkim 0:d4c8fe4d9b29 68 }
justinkim 0:d4c8fe4d9b29 69
justinkim 0:d4c8fe4d9b29 70 // -1 if unsuccessful, else number of bytes received
justinkim 0:d4c8fe4d9b29 71 int WIZnet_UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length)
justinkim 0:d4c8fe4d9b29 72 {
justinkim 0:d4c8fe4d9b29 73 uint8_t info[8];
justinkim 0:d4c8fe4d9b29 74 int size = eth->wait_readable(_sock_fd, _blocking ? -1 : _timeout, sizeof(info));
justinkim 0:d4c8fe4d9b29 75 if (size < 0) {
justinkim 0:d4c8fe4d9b29 76 return -1;
justinkim 0:d4c8fe4d9b29 77 }
justinkim 0:d4c8fe4d9b29 78 eth->recv(_sock_fd, (char*)info, sizeof(info));
justinkim 0:d4c8fe4d9b29 79 readEndpoint(remote, info);
justinkim 0:d4c8fe4d9b29 80 int udp_size = info[6]<<8|info[7];
justinkim 0:d4c8fe4d9b29 81 //TEST_ASSERT(udp_size <= (size-sizeof(info)));
justinkim 0:d4c8fe4d9b29 82 if (udp_size > (size-sizeof(info))) {
justinkim 0:d4c8fe4d9b29 83 return -1;
justinkim 0:d4c8fe4d9b29 84 }
justinkim 0:d4c8fe4d9b29 85
justinkim 0:d4c8fe4d9b29 86 /* Perform Length check here to prevent buffer overrun */
justinkim 0:d4c8fe4d9b29 87 /* fixed by Sean Newton (https://developer.mbed.org/users/SeanNewton/) */
justinkim 0:d4c8fe4d9b29 88 if (udp_size > length) {
justinkim 0:d4c8fe4d9b29 89 //printf("udp_size: %d\n",udp_size);
justinkim 0:d4c8fe4d9b29 90 return -1;
justinkim 0:d4c8fe4d9b29 91 }
justinkim 0:d4c8fe4d9b29 92 return eth->recv(_sock_fd, buffer, udp_size);
justinkim 0:d4c8fe4d9b29 93 }
justinkim 0:d4c8fe4d9b29 94
justinkim 0:d4c8fe4d9b29 95 void WIZnet_UDPSocket::confEndpoint(Endpoint & ep)
justinkim 0:d4c8fe4d9b29 96 {
justinkim 0:d4c8fe4d9b29 97 char * host = ep.get_address();
justinkim 0:d4c8fe4d9b29 98 // set remote host
justinkim 0:d4c8fe4d9b29 99 eth->sreg_ip(_sock_fd, Sn_DIPR, host);
justinkim 0:d4c8fe4d9b29 100 // set remote port
justinkim 0:d4c8fe4d9b29 101 eth->sreg<uint16_t>(_sock_fd, Sn_DPORT, ep.get_port());
justinkim 0:d4c8fe4d9b29 102 }
justinkim 0:d4c8fe4d9b29 103
justinkim 0:d4c8fe4d9b29 104 void WIZnet_UDPSocket::readEndpoint(Endpoint & ep, uint8_t info[])
justinkim 0:d4c8fe4d9b29 105 {
justinkim 0:d4c8fe4d9b29 106 char addr[17];
justinkim 0:d4c8fe4d9b29 107 snprintf(addr, sizeof(addr), "%d.%d.%d.%d", info[0], info[1], info[2], info[3]);
justinkim 0:d4c8fe4d9b29 108 uint16_t port = info[4]<<8|info[5];
justinkim 0:d4c8fe4d9b29 109 ep.set_address(addr, port);
justinkim 0:d4c8fe4d9b29 110 }