WIZ820io(W5200) network interface, EthernetInterface compatible.
Dependents: Seeed_Ethernet_Shield_V2_HelloWorld Seeed_Ethernet_Shield Cayenne-WIZ820ioInterface Seeed_Ethernet_Shield
Fork of WiflyInterface by
UDPSocket.cpp
00001 /* Copyright (C) 2012 mbed.org, MIT License 00002 * 00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00004 * and associated documentation files (the "Software"), to deal in the Software without restriction, 00005 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 00006 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 00007 * furnished to do so, subject to the following conditions: 00008 * 00009 * The above copyright notice and this permission notice shall be included in all copies or 00010 * substantial portions of the Software. 00011 * 00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 */ 00018 00019 #include "UDPSocket.h" 00020 00021 UDPSocket::UDPSocket() 00022 { 00023 } 00024 00025 int UDPSocket::init(void) 00026 { 00027 if (_sock_fd < 0) { 00028 _sock_fd = eth->new_socket(); 00029 } 00030 eth->setProtocol(_sock_fd, UDP); 00031 return 0; 00032 } 00033 00034 // Server initialization 00035 int UDPSocket::bind(int port) 00036 { 00037 if (_sock_fd < 0) { 00038 _sock_fd = eth->new_socket(); 00039 if (_sock_fd < 0) { 00040 return -1; 00041 } 00042 } 00043 // set local port 00044 eth->sreg<uint16_t>(_sock_fd, Sn_PORT, port); 00045 // set udp protocol 00046 eth->setProtocol(_sock_fd, UDP); 00047 eth->scmd(_sock_fd, OPEN); 00048 return 0; 00049 } 00050 00051 // -1 if unsuccessful, else number of bytes written 00052 int UDPSocket::sendTo(Endpoint &remote, char *packet, int length) 00053 { 00054 int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout, length-1); 00055 if (size < 0) { 00056 return -1; 00057 } 00058 confEndpoint(remote); 00059 int ret = eth->send(_sock_fd, packet, length); 00060 return ret; 00061 } 00062 00063 // -1 if unsuccessful, else number of bytes received 00064 int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length) 00065 { 00066 uint8_t info[8]; 00067 int size = eth->wait_readable(_sock_fd, _blocking ? -1 : _timeout, sizeof(info)); 00068 if (size < 0) { 00069 return -1; 00070 } 00071 eth->recv(_sock_fd, (char*)info, sizeof(info)); 00072 readEndpoint(remote, info); 00073 int udp_size = info[6]<<8|info[7]; 00074 //TEST_ASSERT(udp_size <= (size-sizeof(info))); 00075 if (udp_size > (size-sizeof(info))) { 00076 return -1; 00077 } 00078 return eth->recv(_sock_fd, buffer, udp_size); 00079 } 00080 00081 void UDPSocket::confEndpoint(Endpoint & ep) 00082 { 00083 char * host = ep.get_address(); 00084 // set remote host 00085 eth->sreg_ip(_sock_fd, Sn_DIPR, host); 00086 // set remote port 00087 eth->sreg<uint16_t>(_sock_fd, Sn_DPORT, ep.get_port()); 00088 } 00089 00090 void UDPSocket::readEndpoint(Endpoint & ep, uint8_t info[]) 00091 { 00092 char addr[17]; 00093 snprintf(addr, sizeof(addr), "%d.%d.%d.%d", info[0], info[1], info[2], info[3]); 00094 uint16_t port = info[4]<<8|info[5]; 00095 ep.set_address(addr, port); 00096 }
Generated on Thu Jul 14 2022 11:02:04 by 1.7.2