mbed OS5

Fork of UIPEthernet by Zoltan Hudak

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers UIPUdp.h Source File

UIPUdp.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 UIPUDP_H
00022 #define UIPUDP_H
00023 
00024 #include "ethernet_comp.h"
00025 #include "mbed.h"
00026 #include "Udp.h"
00027 #include "utility/mempool.h"
00028 extern "C"
00029 {
00030 #include "utility/uip.h"
00031 }
00032 #define UIP_UDP_MAXDATALEN      1500
00033 #define UIP_UDP_PHYH_LEN        UIP_LLH_LEN + UIP_IPUDPH_LEN
00034 #define UIP_UDP_MAXPACKETSIZE   UIP_UDP_MAXDATALEN + UIP_UDP_PHYH_LEN
00035 
00036 typedef struct
00037 {
00038     memaddress  out_pos;
00039     memhandle   packet_next;
00040     memhandle   packet_in;
00041     memhandle   packet_out;
00042     bool        send;
00043 } uip_udp_userdata_t;
00044 
00045 class UIPUDP :
00046     public UDP
00047 {
00048 private:
00049     struct uip_udp_conn*    _uip_udp_conn;
00050 
00051     uip_udp_userdata_t      appdata;
00052 public:
00053     UIPUDP(void);   // Constructor
00054     uint8_t     begin(uint16_t);    // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
00055     void        stop(void);         // Finish with the UDP socket
00056 
00057     // Sending UDP packets
00058     // Start building up a packet to send to the remote host specific in ip and port
00059     // Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
00060     int         beginPacket(IPAddress ip, uint16_t port);
00061 
00062     // Start building up a packet to send to the remote host specific in host and port
00063     // Returns 1 if successful, 0 if there was a problem resolving the hostname or port
00064     int         beginPacket(const char* host, uint16_t port);
00065 
00066     // Finish off this packet and send it
00067     // Returns 1 if the packet was sent successfully, 0 if there was an error
00068     int         endPacket(void);
00069 
00070     // Write a single byte into the packet
00071     size_t      write(uint8_t);
00072 
00073     // Write size bytes from buffer into the packet
00074     size_t      write(const uint8_t* buffer, size_t size);
00075 
00076     //  using Print::write;
00077     // Start processing the next available incoming packet
00078     // Returns the size of the packet in bytes, or 0 if no packets are available
00079     int         parsePacket(void);
00080 
00081     // Number of bytes remaining in the current packet
00082     int         available(void);
00083 
00084     // Read a single byte from the current packet
00085     int         read(void);
00086 
00087     // Read up to len bytes from the current packet and place them into buffer
00088     // Returns the number of bytes read, or 0 if none are available
00089     int         read(unsigned char* buffer, size_t len);
00090     // Read up to len characters from the current packet and place them into buffer
00091 
00092     // Returns the number of characters read, or 0 if none are available
00093     int         read(char* buffer, size_t len)  { return read((unsigned char*)buffer, len); }
00094 
00095     // Return the next byte from the current packet without moving on to the next byte
00096     int         peek(void);
00097     void        flush(void);    // Finish reading the current packet
00098 
00099     // Return the IP address of the host who sent the current incoming packet
00100     IPAddress   remoteIP(void);
00101 
00102     // Return the port of the host who sent the current incoming packet
00103     uint16_t    remotePort(void);
00104 private:
00105     friend void     uipudp_appcall(void);
00106 
00107     friend class    UIPEthernet;
00108     static void     _send(uip_udp_userdata_t* data);
00109 };
00110 #endif