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:
Wed Jan 04 18:32:00 2017 +0100
Revision:
19:e416943f7119
Parent:
10:cd62b2205c2a
Child:
33:7ba5d53df0f2
Changes

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 4:5c7337d5ba8e 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 4:5c7337d5ba8e 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 4:5c7337d5ba8e 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 4:5c7337d5ba8e 19
cassyarduino 0:e3fb1267e3c3 20 #ifndef UIPUDP_H
cassyarduino 0:e3fb1267e3c3 21 #define UIPUDP_H
cassyarduino 4:5c7337d5ba8e 22
cassyarduino 0:e3fb1267e3c3 23 #include "ethernet_comp.h"
cassyarduino 0:e3fb1267e3c3 24 #if defined(ARDUINO)
cassyarduino 0:e3fb1267e3c3 25 #include <Arduino.h>
cassyarduino 9:312e0937630f 26 #include "Print.h"
cassyarduino 19:e416943f7119 27 #if defined(__STM32F3__) || defined(STM32F3)
cassyarduino 19:e416943f7119 28 #include "mbed/Udp.h"
cassyarduino 19:e416943f7119 29 #else
cassyarduino 19:e416943f7119 30 #include <Udp.h>
cassyarduino 19:e416943f7119 31 #endif
cassyarduino 0:e3fb1267e3c3 32 #endif
cassyarduino 8:b9332109461d 33 #if defined(__MBED__)
cassyarduino 8:b9332109461d 34 #include <mbed.h>
cassyarduino 8:b9332109461d 35 #include "mbed/Print.h"
cassyarduino 8:b9332109461d 36 #include "mbed/Udp.h"
cassyarduino 8:b9332109461d 37 #endif
cassyarduino 0:e3fb1267e3c3 38 #include "utility/mempool.h"
cassyarduino 0:e3fb1267e3c3 39 extern "C" {
cassyarduino 0:e3fb1267e3c3 40 #include "utility/uip.h"
cassyarduino 0:e3fb1267e3c3 41 }
cassyarduino 4:5c7337d5ba8e 42
cassyarduino 0:e3fb1267e3c3 43 #define UIP_UDP_MAXDATALEN 1500
cassyarduino 0:e3fb1267e3c3 44 #define UIP_UDP_PHYH_LEN UIP_LLH_LEN+UIP_IPUDPH_LEN
cassyarduino 0:e3fb1267e3c3 45 #define UIP_UDP_MAXPACKETSIZE UIP_UDP_MAXDATALEN+UIP_UDP_PHYH_LEN
cassyarduino 4:5c7337d5ba8e 46
cassyarduino 0:e3fb1267e3c3 47 typedef struct {
cassyarduino 0:e3fb1267e3c3 48 memaddress out_pos;
cassyarduino 0:e3fb1267e3c3 49 memhandle packet_next;
cassyarduino 0:e3fb1267e3c3 50 memhandle packet_in;
cassyarduino 0:e3fb1267e3c3 51 memhandle packet_out;
cassyarduino 0:e3fb1267e3c3 52 bool send;
cassyarduino 0:e3fb1267e3c3 53 } uip_udp_userdata_t;
cassyarduino 4:5c7337d5ba8e 54
cassyarduino 19:e416943f7119 55 #if defined(ARDUINO) && !defined(STM32F3)
cassyarduino 10:cd62b2205c2a 56 class UIPUDP : public UDP {
cassyarduino 10:cd62b2205c2a 57 #endif
cassyarduino 19:e416943f7119 58 #if defined(__MBED__) || defined(STM32F3)
cassyarduino 10:cd62b2205c2a 59 class UIPUDP : public Print, public UDP {
cassyarduino 10:cd62b2205c2a 60 #endif
cassyarduino 0:e3fb1267e3c3 61 private:
cassyarduino 0:e3fb1267e3c3 62 struct uip_udp_conn *_uip_udp_conn;
cassyarduino 4:5c7337d5ba8e 63
cassyarduino 0:e3fb1267e3c3 64 uip_udp_userdata_t appdata;
cassyarduino 4:5c7337d5ba8e 65
cassyarduino 0:e3fb1267e3c3 66 public:
cassyarduino 4:5c7337d5ba8e 67 UIPUDP(void); // Constructor
cassyarduino 4:5c7337d5ba8e 68 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 4:5c7337d5ba8e 69 virtual void stop(void); // Finish with the UDP socket
cassyarduino 4:5c7337d5ba8e 70
cassyarduino 0:e3fb1267e3c3 71 // Sending UDP packets
cassyarduino 4:5c7337d5ba8e 72
cassyarduino 0:e3fb1267e3c3 73 // Start building up a packet to send to the remote host specific in ip and port
cassyarduino 0:e3fb1267e3c3 74 // Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
cassyarduino 4:5c7337d5ba8e 75 virtual int beginPacket(IPAddress ip, uint16_t port);
cassyarduino 0:e3fb1267e3c3 76 // Start building up a packet to send to the remote host specific in host and port
cassyarduino 0:e3fb1267e3c3 77 // Returns 1 if successful, 0 if there was a problem resolving the hostname or port
cassyarduino 4:5c7337d5ba8e 78 virtual int beginPacket(const char *host, uint16_t port);
cassyarduino 0:e3fb1267e3c3 79 // Finish off this packet and send it
cassyarduino 0:e3fb1267e3c3 80 // Returns 1 if the packet was sent successfully, 0 if there was an error
cassyarduino 4:5c7337d5ba8e 81 virtual int endPacket(void);
cassyarduino 0:e3fb1267e3c3 82 // Write a single byte into the packet
cassyarduino 4:5c7337d5ba8e 83 virtual size_t write(uint8_t);
cassyarduino 0:e3fb1267e3c3 84 // Write size bytes from buffer into the packet
cassyarduino 4:5c7337d5ba8e 85 virtual size_t write(const uint8_t *buffer, size_t size);
cassyarduino 9:312e0937630f 86
cassyarduino 8:b9332109461d 87 using Print::write;
cassyarduino 9:312e0937630f 88
cassyarduino 0:e3fb1267e3c3 89 // Start processing the next available incoming packet
cassyarduino 0:e3fb1267e3c3 90 // Returns the size of the packet in bytes, or 0 if no packets are available
cassyarduino 4:5c7337d5ba8e 91 virtual int parsePacket(void);
cassyarduino 0:e3fb1267e3c3 92 // Number of bytes remaining in the current packet
cassyarduino 4:5c7337d5ba8e 93 virtual int available(void);
cassyarduino 0:e3fb1267e3c3 94 // Read a single byte from the current packet
cassyarduino 4:5c7337d5ba8e 95 virtual int read(void);
cassyarduino 0:e3fb1267e3c3 96 // Read up to len bytes from the current packet and place them into buffer
cassyarduino 0:e3fb1267e3c3 97 // Returns the number of bytes read, or 0 if none are available
cassyarduino 4:5c7337d5ba8e 98 virtual int read(unsigned char* buffer, size_t len);
cassyarduino 0:e3fb1267e3c3 99 // Read up to len characters from the current packet and place them into buffer
cassyarduino 0:e3fb1267e3c3 100 // Returns the number of characters read, or 0 if none are available
cassyarduino 4:5c7337d5ba8e 101 virtual int read(char* buffer, size_t len)
cassyarduino 0:e3fb1267e3c3 102 {
cassyarduino 0:e3fb1267e3c3 103 return read((unsigned char*) buffer, len);
cassyarduino 0:e3fb1267e3c3 104 }
cassyarduino 0:e3fb1267e3c3 105 ;
cassyarduino 0:e3fb1267e3c3 106 // Return the next byte from the current packet without moving on to the next byte
cassyarduino 4:5c7337d5ba8e 107 virtual int peek(void);
cassyarduino 4:5c7337d5ba8e 108 virtual void flush(void); // Finish reading the current packet
cassyarduino 4:5c7337d5ba8e 109
cassyarduino 0:e3fb1267e3c3 110 // Return the IP address of the host who sent the current incoming packet
cassyarduino 4:5c7337d5ba8e 111 virtual IPAddress remoteIP(void);
cassyarduino 4:5c7337d5ba8e 112
cassyarduino 0:e3fb1267e3c3 113 // Return the port of the host who sent the current incoming packet
cassyarduino 4:5c7337d5ba8e 114 virtual uint16_t remotePort(void);
cassyarduino 4:5c7337d5ba8e 115
cassyarduino 0:e3fb1267e3c3 116 private:
cassyarduino 4:5c7337d5ba8e 117
cassyarduino 0:e3fb1267e3c3 118 friend void uipudp_appcall(void);
cassyarduino 4:5c7337d5ba8e 119
cassyarduino 0:e3fb1267e3c3 120 friend class UIPEthernetClass;
cassyarduino 0:e3fb1267e3c3 121 static void _send(uip_udp_userdata_t *data);
cassyarduino 4:5c7337d5ba8e 122
cassyarduino 0:e3fb1267e3c3 123 };
cassyarduino 4:5c7337d5ba8e 124
cassyarduino 0:e3fb1267e3c3 125 #endif