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.
Fork of WIZ820ioInterface 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 static int udp_local_port; 00022 00023 UDPSocket::UDPSocket() 00024 { 00025 } 00026 00027 int UDPSocket::init(void) 00028 { 00029 if (_sock_fd < 0) { 00030 _sock_fd = eth->new_socket(); 00031 } 00032 eth->setProtocol(_sock_fd, UDP); 00033 return 0; 00034 } 00035 00036 // Server initialization 00037 int UDPSocket::bind(int port) 00038 { 00039 if (_sock_fd < 0) { 00040 _sock_fd = eth->new_socket(); 00041 if (_sock_fd < 0) { 00042 return -1; 00043 } 00044 } 00045 // set local port 00046 if (port != 0) { 00047 eth->sreg<uint16_t>(_sock_fd, Sn_PORT, port); 00048 } else { 00049 udp_local_port++; 00050 eth->sreg<uint16_t>(_sock_fd, Sn_PORT, udp_local_port); 00051 } 00052 // set udp protocol 00053 eth->setProtocol(_sock_fd, UDP); 00054 eth->scmd(_sock_fd, OPEN); 00055 return 0; 00056 } 00057 00058 // -1 if unsuccessful, else number of bytes written 00059 int UDPSocket::sendTo(Endpoint &remote, char *packet, int length) 00060 { 00061 int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout, length-1); 00062 if (size < 0) { 00063 return -1; 00064 } 00065 confEndpoint(remote); 00066 int ret = eth->send(_sock_fd, packet, length); 00067 return ret; 00068 } 00069 00070 // -1 if unsuccessful, else number of bytes received 00071 int UDPSocket::receiveFrom(Endpoint &remote, char *buffer, int length) 00072 { 00073 uint8_t info[8]; 00074 int size = eth->wait_readable(_sock_fd, _blocking ? -1 : _timeout, sizeof(info)); 00075 if (size < 0) { 00076 return -1; 00077 } 00078 eth->recv(_sock_fd, (char*)info, sizeof(info)); 00079 readEndpoint(remote, info); 00080 int udp_size = info[6]<<8|info[7]; 00081 //TEST_ASSERT(udp_size <= (size-sizeof(info))); 00082 if (udp_size > (size-sizeof(info))) { 00083 return -1; 00084 } 00085 return eth->recv(_sock_fd, buffer, udp_size); 00086 } 00087 00088 void UDPSocket::confEndpoint(Endpoint & ep) 00089 { 00090 char * host = ep.get_address(); 00091 // set remote host 00092 eth->sreg_ip(_sock_fd, Sn_DIPR, host); 00093 // set remote port 00094 eth->sreg<uint16_t>(_sock_fd, Sn_DPORT, ep.get_port()); 00095 } 00096 00097 void UDPSocket::readEndpoint(Endpoint & ep, uint8_t info[]) 00098 { 00099 char addr[17]; 00100 snprintf(addr, sizeof(addr), "%d.%d.%d.%d", info[0], info[1], info[2], info[3]); 00101 uint16_t port = info[4]<<8|info[5]; 00102 ep.set_address(addr, port); 00103 }
Generated on Thu Jul 14 2022 10:39:51 by
