123
UdpSocket.h@11:647d53d146f1, 2019-08-30 (annotated)
- Committer:
- hudakz
- Date:
- Fri Aug 30 08:11:40 2019 +0000
- Revision:
- 11:647d53d146f1
- Parent:
- 9:a156d3de5647
- Child:
- 14:7648334eb41b
"set_network" method of Mbed style added.
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 | 9:a156d3de5647 | 25 | #include "utility/Udp.h" |
hudakz | 9:a156d3de5647 | 26 | #include "IpAddress.h" |
hudakz | 9:a156d3de5647 | 27 | #include "utility/MemPool.h" |
hudakz | 9:a156d3de5647 | 28 | extern "C" |
hudakz | 9:a156d3de5647 | 29 | { |
hudakz | 9:a156d3de5647 | 30 | #include "utility/uip.h" |
hudakz | 9:a156d3de5647 | 31 | } |
hudakz | 9:a156d3de5647 | 32 | #define UIP_UDP_MAXDATALEN 1500 |
hudakz | 9:a156d3de5647 | 33 | #define UIP_UDP_PHYH_LEN UIP_LLH_LEN + UIP_IPUDPH_LEN |
hudakz | 9:a156d3de5647 | 34 | #define UIP_UDP_MAXPACKETSIZE UIP_UDP_MAXDATALEN + UIP_UDP_PHYH_LEN |
hudakz | 9:a156d3de5647 | 35 | |
hudakz | 9:a156d3de5647 | 36 | typedef struct |
hudakz | 9:a156d3de5647 | 37 | { |
hudakz | 9:a156d3de5647 | 38 | memaddress out_pos; |
hudakz | 9:a156d3de5647 | 39 | memhandle packet_next; |
hudakz | 9:a156d3de5647 | 40 | memhandle packet_in; |
hudakz | 9:a156d3de5647 | 41 | memhandle packet_out; |
hudakz | 9:a156d3de5647 | 42 | bool send; |
hudakz | 9:a156d3de5647 | 43 | } uip_udp_userdata_t; |
hudakz | 9:a156d3de5647 | 44 | |
hudakz | 9:a156d3de5647 | 45 | class UdpSocket : public Udp |
hudakz | 9:a156d3de5647 | 46 | { |
hudakz | 9:a156d3de5647 | 47 | private: |
hudakz | 9:a156d3de5647 | 48 | struct uip_udp_conn* _uip_udp_conn; |
hudakz | 9:a156d3de5647 | 49 | uip_udp_userdata_t appdata; |
hudakz | 9:a156d3de5647 | 50 | public: |
hudakz | 9:a156d3de5647 | 51 | UdpSocket(); // Constructor |
hudakz | 9:a156d3de5647 | 52 | virtual ~UdpSocket() { } // Virtual destructor |
hudakz | 9:a156d3de5647 | 53 | uint8_t begin(uint16_t); // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use |
hudakz | 9:a156d3de5647 | 54 | void stop(); // Finish with the UDP socket |
hudakz | 9:a156d3de5647 | 55 | // Sending UDP packets |
hudakz | 9:a156d3de5647 | 56 | // Start building up a packet to send to the remote host specific in ip and port |
hudakz | 9:a156d3de5647 | 57 | // Returns 1 if successful, 0 if there was a problem with the supplied IP address or port |
hudakz | 9:a156d3de5647 | 58 | int beginPacket(IpAddress ip, uint16_t port); |
hudakz | 9:a156d3de5647 | 59 | |
hudakz | 9:a156d3de5647 | 60 | // Start building up a packet to send to the remote host specific in host and port |
hudakz | 9:a156d3de5647 | 61 | // Returns 1 if successful, 0 if there was a problem resolving the hostname or port |
hudakz | 9:a156d3de5647 | 62 | int beginPacket(const char* host, uint16_t port); |
hudakz | 9:a156d3de5647 | 63 | |
hudakz | 9:a156d3de5647 | 64 | // Finish off this packet and send it |
hudakz | 9:a156d3de5647 | 65 | // Returns 1 if the packet was sent successfully, 0 if there was an error |
hudakz | 9:a156d3de5647 | 66 | int endPacket(); |
hudakz | 9:a156d3de5647 | 67 | |
hudakz | 9:a156d3de5647 | 68 | // Write a single byte into the packet |
hudakz | 9:a156d3de5647 | 69 | size_t write(uint8_t); |
hudakz | 9:a156d3de5647 | 70 | |
hudakz | 9:a156d3de5647 | 71 | // Write size bytes from buffer into the packet |
hudakz | 9:a156d3de5647 | 72 | size_t write(const uint8_t* buffer, size_t size); |
hudakz | 9:a156d3de5647 | 73 | |
hudakz | 9:a156d3de5647 | 74 | // using Print::write; |
hudakz | 9:a156d3de5647 | 75 | // Start processing the next available incoming packet |
hudakz | 9:a156d3de5647 | 76 | // Returns the size of the packet in bytes, or 0 if no packets are available |
hudakz | 9:a156d3de5647 | 77 | int parsePacket(); |
hudakz | 9:a156d3de5647 | 78 | |
hudakz | 9:a156d3de5647 | 79 | // Number of bytes remaining in the current packet |
hudakz | 9:a156d3de5647 | 80 | int available(); |
hudakz | 9:a156d3de5647 | 81 | |
hudakz | 9:a156d3de5647 | 82 | // Read a single byte from the current packet |
hudakz | 9:a156d3de5647 | 83 | int read(); |
hudakz | 9:a156d3de5647 | 84 | |
hudakz | 9:a156d3de5647 | 85 | // Read up to len bytes from the current packet and place them into buffer |
hudakz | 9:a156d3de5647 | 86 | // Returns the number of bytes read, or 0 if none are available |
hudakz | 9:a156d3de5647 | 87 | int read(unsigned char* buffer, size_t len); |
hudakz | 9:a156d3de5647 | 88 | // Read up to len characters from the current packet and place them into buffer |
hudakz | 9:a156d3de5647 | 89 | |
hudakz | 9:a156d3de5647 | 90 | // Returns the number of characters read, or 0 if none are available |
hudakz | 9:a156d3de5647 | 91 | int read(char* buffer, size_t len) { return read((unsigned char*)buffer, len); } |
hudakz | 9:a156d3de5647 | 92 | |
hudakz | 9:a156d3de5647 | 93 | // Return the next byte from the current packet without moving on to the next byte |
hudakz | 9:a156d3de5647 | 94 | int peek(); |
hudakz | 9:a156d3de5647 | 95 | void flush(); // Finish reading the current packet |
hudakz | 9:a156d3de5647 | 96 | |
hudakz | 9:a156d3de5647 | 97 | // Return the IP address of the host who sent the current incoming packet |
hudakz | 9:a156d3de5647 | 98 | IpAddress remoteIP(); |
hudakz | 9:a156d3de5647 | 99 | |
hudakz | 9:a156d3de5647 | 100 | // Return the port of the host who sent the current incoming packet |
hudakz | 9:a156d3de5647 | 101 | uint16_t remotePort(); |
hudakz | 9:a156d3de5647 | 102 | private: |
hudakz | 9:a156d3de5647 | 103 | friend void uipudp_appcall(); |
hudakz | 9:a156d3de5647 | 104 | |
hudakz | 9:a156d3de5647 | 105 | friend class UipEthernet; |
hudakz | 9:a156d3de5647 | 106 | static void _send(uip_udp_userdata_t* data); |
hudakz | 9:a156d3de5647 | 107 | }; |
hudakz | 9:a156d3de5647 | 108 | #endif |