123
UdpSocket.h@14:7648334eb41b, 2019-09-03 (annotated)
- Committer:
- hudakz
- Date:
- Tue Sep 03 09:16:55 2019 +0000
- Revision:
- 14:7648334eb41b
- Parent:
- 11:647d53d146f1
UDP socket improved.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 9:a156d3de5647 | 1 | /* |
hudakz | 9:a156d3de5647 | 2 | UIPUdp.h - Arduino implementation of a UIP wrapper class. |
hudakz | 9:a156d3de5647 | 3 | Copyright (c) 2013 Norbert Truchsess <norbert.truchsess@t-online.de> |
hudakz | 9:a156d3de5647 | 4 | All rights reserved. |
hudakz | 9:a156d3de5647 | 5 | |
hudakz | 9:a156d3de5647 | 6 | Modified (ported to mbed) by Zoltan Hudak <hudakz@inbox.com> |
hudakz | 9:a156d3de5647 | 7 | |
hudakz | 9:a156d3de5647 | 8 | This program is free software: you can redistribute it and/or modify |
hudakz | 9:a156d3de5647 | 9 | it under the terms of the GNU General Public License as published by |
hudakz | 9:a156d3de5647 | 10 | the Free Software Foundation, either version 3 of the License, or |
hudakz | 9:a156d3de5647 | 11 | (at your option) any later version. |
hudakz | 9:a156d3de5647 | 12 | |
hudakz | 9:a156d3de5647 | 13 | This program is distributed in the hope that it will be useful, |
hudakz | 9:a156d3de5647 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
hudakz | 9:a156d3de5647 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
hudakz | 9:a156d3de5647 | 16 | GNU General Public License for more details. |
hudakz | 9:a156d3de5647 | 17 | |
hudakz | 9:a156d3de5647 | 18 | You should have received a copy of the GNU General Public License |
hudakz | 9:a156d3de5647 | 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
hudakz | 9:a156d3de5647 | 20 | */ |
hudakz | 11:647d53d146f1 | 21 | #ifndef UDPSOCKET_h |
hudakz | 11:647d53d146f1 | 22 | #define UDPSOCKET_h |
hudakz | 9:a156d3de5647 | 23 | |
hudakz | 9:a156d3de5647 | 24 | #include "mbed.h" |
hudakz | 14:7648334eb41b | 25 | #include "SocketAddress.h" |
hudakz | 9:a156d3de5647 | 26 | #include "utility/Udp.h" |
hudakz | 9:a156d3de5647 | 27 | #include "IpAddress.h" |
hudakz | 9:a156d3de5647 | 28 | #include "utility/MemPool.h" |
hudakz | 9:a156d3de5647 | 29 | extern "C" |
hudakz | 9:a156d3de5647 | 30 | { |
hudakz | 9:a156d3de5647 | 31 | #include "utility/uip.h" |
hudakz | 9:a156d3de5647 | 32 | } |
hudakz | 9:a156d3de5647 | 33 | #define UIP_UDP_MAXDATALEN 1500 |
hudakz | 9:a156d3de5647 | 34 | #define UIP_UDP_PHYH_LEN UIP_LLH_LEN + UIP_IPUDPH_LEN |
hudakz | 9:a156d3de5647 | 35 | #define UIP_UDP_MAXPACKETSIZE UIP_UDP_MAXDATALEN + UIP_UDP_PHYH_LEN |
hudakz | 9:a156d3de5647 | 36 | |
hudakz | 9:a156d3de5647 | 37 | typedef struct |
hudakz | 9:a156d3de5647 | 38 | { |
hudakz | 9:a156d3de5647 | 39 | memaddress out_pos; |
hudakz | 9:a156d3de5647 | 40 | memhandle packet_next; |
hudakz | 9:a156d3de5647 | 41 | memhandle packet_in; |
hudakz | 9:a156d3de5647 | 42 | memhandle packet_out; |
hudakz | 9:a156d3de5647 | 43 | bool send; |
hudakz | 9:a156d3de5647 | 44 | } uip_udp_userdata_t; |
hudakz | 9:a156d3de5647 | 45 | |
hudakz | 14:7648334eb41b | 46 | class UipEthernet; |
hudakz | 14:7648334eb41b | 47 | |
hudakz | 9:a156d3de5647 | 48 | class UdpSocket : public Udp |
hudakz | 9:a156d3de5647 | 49 | { |
hudakz | 9:a156d3de5647 | 50 | private: |
hudakz | 14:7648334eb41b | 51 | uip_udp_userdata_t appdata; |
hudakz | 9:a156d3de5647 | 52 | struct uip_udp_conn* _uip_udp_conn; |
hudakz | 14:7648334eb41b | 53 | SocketAddress _remote_addr; |
hudakz | 14:7648334eb41b | 54 | int _timeout_ms; |
hudakz | 9:a156d3de5647 | 55 | public: |
hudakz | 14:7648334eb41b | 56 | UdpSocket(); // Constructor |
hudakz | 14:7648334eb41b | 57 | UdpSocket(int timeout_ms); // Constructor |
hudakz | 14:7648334eb41b | 58 | UdpSocket(UipEthernet* ethernet, int timeout_ms = 1000); |
hudakz | 14:7648334eb41b | 59 | virtual ~UdpSocket() { } // Virtual destructor |
hudakz | 14:7648334eb41b | 60 | 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 |
hudakz | 14:7648334eb41b | 61 | void stop(); // Finish with the UDP socket |
hudakz | 14:7648334eb41b | 62 | void close(); // Close the UDP socket |
hudakz | 9:a156d3de5647 | 63 | // Sending UDP packets |
hudakz | 9:a156d3de5647 | 64 | // Start building up a packet to send to the remote host specific in ip and port |
hudakz | 9:a156d3de5647 | 65 | // Returns 1 if successful, 0 if there was a problem with the supplied IP address or port |
hudakz | 9:a156d3de5647 | 66 | int beginPacket(IpAddress ip, uint16_t port); |
hudakz | 9:a156d3de5647 | 67 | |
hudakz | 9:a156d3de5647 | 68 | // Start building up a packet to send to the remote host specific in host and port |
hudakz | 9:a156d3de5647 | 69 | // Returns 1 if successful, 0 if there was a problem resolving the hostname or port |
hudakz | 9:a156d3de5647 | 70 | int beginPacket(const char* host, uint16_t port); |
hudakz | 9:a156d3de5647 | 71 | |
hudakz | 9:a156d3de5647 | 72 | // Finish off this packet and send it |
hudakz | 9:a156d3de5647 | 73 | // Returns 1 if the packet was sent successfully, 0 if there was an error |
hudakz | 9:a156d3de5647 | 74 | int endPacket(); |
hudakz | 9:a156d3de5647 | 75 | |
hudakz | 9:a156d3de5647 | 76 | // Write a single byte into the packet |
hudakz | 9:a156d3de5647 | 77 | size_t write(uint8_t); |
hudakz | 9:a156d3de5647 | 78 | |
hudakz | 9:a156d3de5647 | 79 | // Write size bytes from buffer into the packet |
hudakz | 9:a156d3de5647 | 80 | size_t write(const uint8_t* buffer, size_t size); |
hudakz | 9:a156d3de5647 | 81 | |
hudakz | 9:a156d3de5647 | 82 | // using Print::write; |
hudakz | 9:a156d3de5647 | 83 | // Start processing the next available incoming packet |
hudakz | 9:a156d3de5647 | 84 | // Returns the size of the packet in bytes, or 0 if no packets are available |
hudakz | 9:a156d3de5647 | 85 | int parsePacket(); |
hudakz | 9:a156d3de5647 | 86 | |
hudakz | 9:a156d3de5647 | 87 | // Number of bytes remaining in the current packet |
hudakz | 14:7648334eb41b | 88 | size_t available(); |
hudakz | 9:a156d3de5647 | 89 | |
hudakz | 9:a156d3de5647 | 90 | // Read a single byte from the current packet |
hudakz | 9:a156d3de5647 | 91 | int read(); |
hudakz | 9:a156d3de5647 | 92 | |
hudakz | 9:a156d3de5647 | 93 | // Read up to len bytes from the current packet and place them into buffer |
hudakz | 9:a156d3de5647 | 94 | // Returns the number of bytes read, or 0 if none are available |
hudakz | 14:7648334eb41b | 95 | size_t read(unsigned char* buffer, size_t len); |
hudakz | 9:a156d3de5647 | 96 | // Read up to len characters from the current packet and place them into buffer |
hudakz | 9:a156d3de5647 | 97 | |
hudakz | 9:a156d3de5647 | 98 | // Returns the number of characters read, or 0 if none are available |
hudakz | 14:7648334eb41b | 99 | size_t read(char* buffer, size_t len) { return read((unsigned char*)buffer, len); } |
hudakz | 9:a156d3de5647 | 100 | |
hudakz | 9:a156d3de5647 | 101 | // Return the next byte from the current packet without moving on to the next byte |
hudakz | 9:a156d3de5647 | 102 | int peek(); |
hudakz | 9:a156d3de5647 | 103 | void flush(); // Finish reading the current packet |
hudakz | 9:a156d3de5647 | 104 | |
hudakz | 9:a156d3de5647 | 105 | // Return the IP address of the host who sent the current incoming packet |
hudakz | 9:a156d3de5647 | 106 | IpAddress remoteIP(); |
hudakz | 9:a156d3de5647 | 107 | |
hudakz | 9:a156d3de5647 | 108 | // Return the port of the host who sent the current incoming packet |
hudakz | 9:a156d3de5647 | 109 | uint16_t remotePort(); |
hudakz | 14:7648334eb41b | 110 | |
hudakz | 14:7648334eb41b | 111 | // Send data to the specified host and port. |
hudakz | 14:7648334eb41b | 112 | nsapi_size_or_error_t sendto (const char *host, uint16_t port, const void *data, size_t size); |
hudakz | 14:7648334eb41b | 113 | // Send data to the specified address. |
hudakz | 14:7648334eb41b | 114 | nsapi_size_or_error_t sendto (const SocketAddress &address, const void *data, size_t size); |
hudakz | 14:7648334eb41b | 115 | // Receive a datagram and store the source address in address if it's not NULL. |
hudakz | 14:7648334eb41b | 116 | nsapi_size_or_error_t recvfrom (SocketAddress *address, void *data, size_t size); |
hudakz | 14:7648334eb41b | 117 | |
hudakz | 9:a156d3de5647 | 118 | private: |
hudakz | 9:a156d3de5647 | 119 | friend void uipudp_appcall(); |
hudakz | 9:a156d3de5647 | 120 | |
hudakz | 9:a156d3de5647 | 121 | friend class UipEthernet; |
hudakz | 9:a156d3de5647 | 122 | static void _send(uip_udp_userdata_t* data); |
hudakz | 9:a156d3de5647 | 123 | }; |
hudakz | 9:a156d3de5647 | 124 | #endif |