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.
UdpSocket.h
00001 /* 00002 UIPUdp.h - Arduino implementation of a UIP wrapper class. 00003 Copyright (c) 2013 Norbert Truchsess <norbert.truchsess@t-online.de> 00004 All rights reserved. 00005 00006 Modified (ported to mbed) by Zoltan Hudak <hudakz@inbox.com> 00007 00008 This program is free software: you can redistribute it and/or modify 00009 it under the terms of the GNU General Public License as published by 00010 the Free Software Foundation, either version 3 of the License, or 00011 (at your option) any later version. 00012 00013 This program is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 GNU General Public License for more details. 00017 00018 You should have received a copy of the GNU General Public License 00019 along with this program. If not, see <http://www.gnu.org/licenses/>. 00020 */ 00021 #ifndef UDPSOCKET_h 00022 #define UDPSOCKET_h 00023 00024 #include "mbed.h" 00025 #include "SocketAddress.h" 00026 #include "utility/Udp.h" 00027 #include "IpAddress.h" 00028 #include "utility/MemPool.h" 00029 extern "C" 00030 { 00031 #include "utility/uip.h" 00032 } 00033 #define UIP_UDP_MAXDATALEN 1500 00034 #define UIP_UDP_PHYH_LEN UIP_LLH_LEN + UIP_IPUDPH_LEN 00035 #define UIP_UDP_MAXPACKETSIZE UIP_UDP_MAXDATALEN + UIP_UDP_PHYH_LEN 00036 00037 typedef struct 00038 { 00039 memaddress out_pos; 00040 memhandle packet_next; 00041 memhandle packet_in; 00042 memhandle packet_out; 00043 bool send; 00044 } uip_udp_userdata_t; 00045 00046 class UipEthernet; 00047 00048 class UdpSocket : public Udp 00049 { 00050 private: 00051 uip_udp_userdata_t appdata; 00052 struct uip_udp_conn* _uip_udp_conn; 00053 SocketAddress _remote_addr; 00054 int _timeout_ms; 00055 public: 00056 UdpSocket(); // Constructor 00057 UdpSocket(int timeout_ms); // Constructor 00058 UdpSocket(UipEthernet* ethernet, int timeout_ms = 1000); 00059 virtual ~UdpSocket() { } // Virtual destructor 00060 uint8_t begin(uint16_t port); // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use 00061 void stop(); // Finish with the UDP socket 00062 void close(); // Close the UDP socket 00063 // Sending UDP packets 00064 // Start building up a packet to send to the remote host specific in ip and port 00065 // Returns 1 if successful, 0 if there was a problem with the supplied IP address or port 00066 int beginPacket(IpAddress ip, uint16_t port); 00067 00068 // Start building up a packet to send to the remote host specific in host and port 00069 // Returns 1 if successful, 0 if there was a problem resolving the hostname or port 00070 int beginPacket(const char* host, uint16_t port); 00071 00072 // Finish off this packet and send it 00073 // Returns 1 if the packet was sent successfully, 0 if there was an error 00074 int endPacket(); 00075 00076 // Write a single byte into the packet 00077 size_t write(uint8_t); 00078 00079 // Write size bytes from buffer into the packet 00080 size_t write(const uint8_t* buffer, size_t size); 00081 00082 // using Print::write; 00083 // Start processing the next available incoming packet 00084 // Returns the size of the packet in bytes, or 0 if no packets are available 00085 int parsePacket(); 00086 00087 // Number of bytes remaining in the current packet 00088 size_t available(); 00089 00090 // Read a single byte from the current packet 00091 int read(); 00092 00093 // Read up to len bytes from the current packet and place them into buffer 00094 // Returns the number of bytes read, or 0 if none are available 00095 size_t read(unsigned char* buffer, size_t len); 00096 // Read up to len characters from the current packet and place them into buffer 00097 00098 // Returns the number of characters read, or 0 if none are available 00099 size_t read(char* buffer, size_t len) { return read((unsigned char*)buffer, len); } 00100 00101 // Return the next byte from the current packet without moving on to the next byte 00102 int peek(); 00103 void flush(); // Finish reading the current packet 00104 00105 // Return the IP address of the host who sent the current incoming packet 00106 IpAddress remoteIP(); 00107 00108 // Return the port of the host who sent the current incoming packet 00109 uint16_t remotePort(); 00110 00111 // Send data to the specified host and port. 00112 nsapi_size_or_error_t sendto (const char *host, uint16_t port, const void *data, size_t size); 00113 // Send data to the specified address. 00114 nsapi_size_or_error_t sendto (const SocketAddress &address, const void *data, size_t size); 00115 // Receive a datagram and store the source address in address if it's not NULL. 00116 nsapi_size_or_error_t recvfrom (SocketAddress *address, void *data, size_t size); 00117 00118 private: 00119 friend void uipudp_appcall(); 00120 00121 friend class UipEthernet; 00122 static void _send(uip_udp_userdata_t* data); 00123 }; 00124 #endif
Generated on Sun Feb 26 2023 10:14:23 by
1.7.2