UIPEthernet library for Arduino IDE, Eclipse with arduino plugin and MBED/SMeshStudio (AVR,STM32F,ESP8266,Intel ARC32,Nordic nRF51,Teensy boards,Realtek Ameba(RTL8195A,RTL8710)), ENC28j60 network chip. Compatible with Wiznet W5100 Ethernet library API. Compiled and tested on Nucleo-F302R8. Master repository is: https://github.com/UIPEthernet/UIPEthernet/

Committer:
cassyarduino
Date:
Tue Dec 27 11:34:26 2016 +0000
Revision:
2:2f693560ad53
Parent:
0:e3fb1267e3c3
Child:
3:6b1c9bd7773a
Correct warnings

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cassyarduino 0:e3fb1267e3c3 1 /*
cassyarduino 0:e3fb1267e3c3 2 UIPUdp.h - Arduino implementation of a uIP wrapper class
cassyarduino 0:e3fb1267e3c3 3 Copyright (c) 2013 Norbert Truchsess <norbert.truchsess@t-online.de>
cassyarduino 0:e3fb1267e3c3 4 All rights reserved.
cassyarduino 0:e3fb1267e3c3 5
cassyarduino 0:e3fb1267e3c3 6 This program is free software: you can redistribute it and/or modify
cassyarduino 0:e3fb1267e3c3 7 it under the terms of the GNU General Public License as published by
cassyarduino 0:e3fb1267e3c3 8 the Free Software Foundation, either version 3 of the License, or
cassyarduino 0:e3fb1267e3c3 9 (at your option) any later version.
cassyarduino 0:e3fb1267e3c3 10
cassyarduino 0:e3fb1267e3c3 11 This program is distributed in the hope that it will be useful,
cassyarduino 0:e3fb1267e3c3 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
cassyarduino 0:e3fb1267e3c3 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
cassyarduino 0:e3fb1267e3c3 14 GNU General Public License for more details.
cassyarduino 0:e3fb1267e3c3 15
cassyarduino 0:e3fb1267e3c3 16 You should have received a copy of the GNU General Public License
cassyarduino 0:e3fb1267e3c3 17 along with this program. If not, see <http://www.gnu.org/licenses/>.
cassyarduino 0:e3fb1267e3c3 18 */
cassyarduino 0:e3fb1267e3c3 19
cassyarduino 0:e3fb1267e3c3 20 #ifndef UIPUDP_H
cassyarduino 0:e3fb1267e3c3 21 #define UIPUDP_H
cassyarduino 0:e3fb1267e3c3 22
cassyarduino 0:e3fb1267e3c3 23 #include "ethernet_comp.h"
cassyarduino 0:e3fb1267e3c3 24 #if defined(__MBED__)
cassyarduino 0:e3fb1267e3c3 25 #include <mbed.h>
cassyarduino 0:e3fb1267e3c3 26 #include "mbed/Udp.h"
cassyarduino 0:e3fb1267e3c3 27 #endif
cassyarduino 0:e3fb1267e3c3 28 #if defined(ARDUINO)
cassyarduino 0:e3fb1267e3c3 29 #include <Arduino.h>
cassyarduino 0:e3fb1267e3c3 30 #include <Udp.h>
cassyarduino 0:e3fb1267e3c3 31 #endif
cassyarduino 0:e3fb1267e3c3 32 #include "utility/mempool.h"
cassyarduino 0:e3fb1267e3c3 33 extern "C" {
cassyarduino 0:e3fb1267e3c3 34 #include "utility/uip.h"
cassyarduino 0:e3fb1267e3c3 35 }
cassyarduino 0:e3fb1267e3c3 36
cassyarduino 0:e3fb1267e3c3 37 #define UIP_UDP_MAXDATALEN 1500
cassyarduino 0:e3fb1267e3c3 38 #define UIP_UDP_PHYH_LEN UIP_LLH_LEN+UIP_IPUDPH_LEN
cassyarduino 0:e3fb1267e3c3 39 #define UIP_UDP_MAXPACKETSIZE UIP_UDP_MAXDATALEN+UIP_UDP_PHYH_LEN
cassyarduino 0:e3fb1267e3c3 40
cassyarduino 0:e3fb1267e3c3 41 typedef struct {
cassyarduino 0:e3fb1267e3c3 42 memaddress out_pos;
cassyarduino 0:e3fb1267e3c3 43 memhandle packet_next;
cassyarduino 0:e3fb1267e3c3 44 memhandle packet_in;
cassyarduino 0:e3fb1267e3c3 45 memhandle packet_out;
cassyarduino 0:e3fb1267e3c3 46 bool send;
cassyarduino 0:e3fb1267e3c3 47 } uip_udp_userdata_t;
cassyarduino 0:e3fb1267e3c3 48
cassyarduino 0:e3fb1267e3c3 49 class UIPUDP : public UDP
cassyarduino 0:e3fb1267e3c3 50 {
cassyarduino 0:e3fb1267e3c3 51
cassyarduino 0:e3fb1267e3c3 52 private:
cassyarduino 0:e3fb1267e3c3 53 struct uip_udp_conn *_uip_udp_conn;
cassyarduino 0:e3fb1267e3c3 54
cassyarduino 0:e3fb1267e3c3 55 uip_udp_userdata_t appdata;
cassyarduino 0:e3fb1267e3c3 56
cassyarduino 0:e3fb1267e3c3 57 public:
cassyarduino 2:2f693560ad53 58 UIPUDP(void); // Constructor
cassyarduino 2:2f693560ad53 59 virtual uint8_t begin(uint16_t);// initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
cassyarduino 2:2f693560ad53 60 virtual void stop(void); // Finish with the UDP socket
cassyarduino 0:e3fb1267e3c3 61
cassyarduino 0:e3fb1267e3c3 62 // Sending UDP packets
cassyarduino 0:e3fb1267e3c3 63
cassyarduino 0:e3fb1267e3c3 64 // Start building up a packet to send to the remote host specific in ip and port
cassyarduino 0:e3fb1267e3c3 65 // Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
cassyarduino 2:2f693560ad53 66 virtual int beginPacket(IPAddress ip, uint16_t port);
cassyarduino 0:e3fb1267e3c3 67 // Start building up a packet to send to the remote host specific in host and port
cassyarduino 0:e3fb1267e3c3 68 // Returns 1 if successful, 0 if there was a problem resolving the hostname or port
cassyarduino 2:2f693560ad53 69 virtual int beginPacket(const char *host, uint16_t port);
cassyarduino 0:e3fb1267e3c3 70 // Finish off this packet and send it
cassyarduino 0:e3fb1267e3c3 71 // Returns 1 if the packet was sent successfully, 0 if there was an error
cassyarduino 2:2f693560ad53 72 virtual int endPacket(void);
cassyarduino 0:e3fb1267e3c3 73 // Write a single byte into the packet
cassyarduino 2:2f693560ad53 74 virtual size_t write(uint8_t);
cassyarduino 0:e3fb1267e3c3 75 // Write size bytes from buffer into the packet
cassyarduino 2:2f693560ad53 76 virtual size_t write(const uint8_t *buffer, size_t size);
cassyarduino 0:e3fb1267e3c3 77
cassyarduino 0:e3fb1267e3c3 78 #if defined(ARDUINO)
cassyarduino 0:e3fb1267e3c3 79 using Print::write;
cassyarduino 0:e3fb1267e3c3 80 #endif
cassyarduino 0:e3fb1267e3c3 81 // Start processing the next available incoming packet
cassyarduino 0:e3fb1267e3c3 82 // Returns the size of the packet in bytes, or 0 if no packets are available
cassyarduino 2:2f693560ad53 83 virtual int parsePacket(void);
cassyarduino 0:e3fb1267e3c3 84 // Number of bytes remaining in the current packet
cassyarduino 2:2f693560ad53 85 virtual int available(void);
cassyarduino 0:e3fb1267e3c3 86 // Read a single byte from the current packet
cassyarduino 2:2f693560ad53 87 virtual int read(void);
cassyarduino 0:e3fb1267e3c3 88 // Read up to len bytes from the current packet and place them into buffer
cassyarduino 0:e3fb1267e3c3 89 // Returns the number of bytes read, or 0 if none are available
cassyarduino 2:2f693560ad53 90 virtual int read(unsigned char* buffer, size_t len);
cassyarduino 0:e3fb1267e3c3 91 // Read up to len characters from the current packet and place them into buffer
cassyarduino 0:e3fb1267e3c3 92 // Returns the number of characters read, or 0 if none are available
cassyarduino 2:2f693560ad53 93 virtual int read(char* buffer, size_t len)
cassyarduino 0:e3fb1267e3c3 94 {
cassyarduino 0:e3fb1267e3c3 95 return read((unsigned char*) buffer, len);
cassyarduino 0:e3fb1267e3c3 96 }
cassyarduino 0:e3fb1267e3c3 97 ;
cassyarduino 0:e3fb1267e3c3 98 // Return the next byte from the current packet without moving on to the next byte
cassyarduino 2:2f693560ad53 99 virtual int peek(void);
cassyarduino 2:2f693560ad53 100 virtual void flush(void); // Finish reading the current packet
cassyarduino 0:e3fb1267e3c3 101
cassyarduino 0:e3fb1267e3c3 102 // Return the IP address of the host who sent the current incoming packet
cassyarduino 2:2f693560ad53 103 virtual IPAddress remoteIP(void);
cassyarduino 0:e3fb1267e3c3 104
cassyarduino 0:e3fb1267e3c3 105 // Return the port of the host who sent the current incoming packet
cassyarduino 2:2f693560ad53 106 virtual uint16_t remotePort(void);
cassyarduino 0:e3fb1267e3c3 107
cassyarduino 0:e3fb1267e3c3 108 private:
cassyarduino 0:e3fb1267e3c3 109
cassyarduino 0:e3fb1267e3c3 110 friend void uipudp_appcall(void);
cassyarduino 0:e3fb1267e3c3 111
cassyarduino 0:e3fb1267e3c3 112 friend class UIPEthernetClass;
cassyarduino 0:e3fb1267e3c3 113 static void _send(uip_udp_userdata_t *data);
cassyarduino 0:e3fb1267e3c3 114
cassyarduino 0:e3fb1267e3c3 115 };
cassyarduino 0:e3fb1267e3c3 116
cassyarduino 0:e3fb1267e3c3 117 #endif