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:
Thu Dec 29 10:41:59 2016 +0000
Revision:
9:312e0937630f
Parent:
8:b9332109461d
Child:
10:cd62b2205c2a
Corrections for MBED support

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 0:e3fb1267e3c3 27 #include <Udp.h>
cassyarduino 0:e3fb1267e3c3 28 #endif
cassyarduino 8:b9332109461d 29 #if defined(__MBED__)
cassyarduino 8:b9332109461d 30 #include <mbed.h>
cassyarduino 8:b9332109461d 31 #include "mbed/Print.h"
cassyarduino 8:b9332109461d 32 #include "mbed/Udp.h"
cassyarduino 8:b9332109461d 33 #endif
cassyarduino 0:e3fb1267e3c3 34 #include "utility/mempool.h"
cassyarduino 0:e3fb1267e3c3 35 extern "C" {
cassyarduino 0:e3fb1267e3c3 36 #include "utility/uip.h"
cassyarduino 0:e3fb1267e3c3 37 }
cassyarduino 4:5c7337d5ba8e 38
cassyarduino 0:e3fb1267e3c3 39 #define UIP_UDP_MAXDATALEN 1500
cassyarduino 0:e3fb1267e3c3 40 #define UIP_UDP_PHYH_LEN UIP_LLH_LEN+UIP_IPUDPH_LEN
cassyarduino 0:e3fb1267e3c3 41 #define UIP_UDP_MAXPACKETSIZE UIP_UDP_MAXDATALEN+UIP_UDP_PHYH_LEN
cassyarduino 4:5c7337d5ba8e 42
cassyarduino 0:e3fb1267e3c3 43 typedef struct {
cassyarduino 0:e3fb1267e3c3 44 memaddress out_pos;
cassyarduino 0:e3fb1267e3c3 45 memhandle packet_next;
cassyarduino 0:e3fb1267e3c3 46 memhandle packet_in;
cassyarduino 0:e3fb1267e3c3 47 memhandle packet_out;
cassyarduino 0:e3fb1267e3c3 48 bool send;
cassyarduino 0:e3fb1267e3c3 49 } uip_udp_userdata_t;
cassyarduino 4:5c7337d5ba8e 50
cassyarduino 9:312e0937630f 51 class UIPUDP : public Print, public UDP {
cassyarduino 0:e3fb1267e3c3 52 private:
cassyarduino 0:e3fb1267e3c3 53 struct uip_udp_conn *_uip_udp_conn;
cassyarduino 4:5c7337d5ba8e 54
cassyarduino 0:e3fb1267e3c3 55 uip_udp_userdata_t appdata;
cassyarduino 4:5c7337d5ba8e 56
cassyarduino 0:e3fb1267e3c3 57 public:
cassyarduino 4:5c7337d5ba8e 58 UIPUDP(void); // Constructor
cassyarduino 4:5c7337d5ba8e 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 4:5c7337d5ba8e 60 virtual void stop(void); // Finish with the UDP socket
cassyarduino 4:5c7337d5ba8e 61
cassyarduino 0:e3fb1267e3c3 62 // Sending UDP packets
cassyarduino 4:5c7337d5ba8e 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 4:5c7337d5ba8e 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 4:5c7337d5ba8e 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 4:5c7337d5ba8e 72 virtual int endPacket(void);
cassyarduino 0:e3fb1267e3c3 73 // Write a single byte into the packet
cassyarduino 4:5c7337d5ba8e 74 virtual size_t write(uint8_t);
cassyarduino 0:e3fb1267e3c3 75 // Write size bytes from buffer into the packet
cassyarduino 4:5c7337d5ba8e 76 virtual size_t write(const uint8_t *buffer, size_t size);
cassyarduino 9:312e0937630f 77
cassyarduino 8:b9332109461d 78 using Print::write;
cassyarduino 9:312e0937630f 79
cassyarduino 0:e3fb1267e3c3 80 // Start processing the next available incoming packet
cassyarduino 0:e3fb1267e3c3 81 // Returns the size of the packet in bytes, or 0 if no packets are available
cassyarduino 4:5c7337d5ba8e 82 virtual int parsePacket(void);
cassyarduino 0:e3fb1267e3c3 83 // Number of bytes remaining in the current packet
cassyarduino 4:5c7337d5ba8e 84 virtual int available(void);
cassyarduino 0:e3fb1267e3c3 85 // Read a single byte from the current packet
cassyarduino 4:5c7337d5ba8e 86 virtual int read(void);
cassyarduino 0:e3fb1267e3c3 87 // Read up to len bytes from the current packet and place them into buffer
cassyarduino 0:e3fb1267e3c3 88 // Returns the number of bytes read, or 0 if none are available
cassyarduino 4:5c7337d5ba8e 89 virtual int read(unsigned char* buffer, size_t len);
cassyarduino 0:e3fb1267e3c3 90 // Read up to len characters from the current packet and place them into buffer
cassyarduino 0:e3fb1267e3c3 91 // Returns the number of characters read, or 0 if none are available
cassyarduino 4:5c7337d5ba8e 92 virtual int read(char* buffer, size_t len)
cassyarduino 0:e3fb1267e3c3 93 {
cassyarduino 0:e3fb1267e3c3 94 return read((unsigned char*) buffer, len);
cassyarduino 0:e3fb1267e3c3 95 }
cassyarduino 0:e3fb1267e3c3 96 ;
cassyarduino 0:e3fb1267e3c3 97 // Return the next byte from the current packet without moving on to the next byte
cassyarduino 4:5c7337d5ba8e 98 virtual int peek(void);
cassyarduino 4:5c7337d5ba8e 99 virtual void flush(void); // Finish reading the current packet
cassyarduino 4:5c7337d5ba8e 100
cassyarduino 0:e3fb1267e3c3 101 // Return the IP address of the host who sent the current incoming packet
cassyarduino 4:5c7337d5ba8e 102 virtual IPAddress remoteIP(void);
cassyarduino 4:5c7337d5ba8e 103
cassyarduino 0:e3fb1267e3c3 104 // Return the port of the host who sent the current incoming packet
cassyarduino 4:5c7337d5ba8e 105 virtual uint16_t remotePort(void);
cassyarduino 4:5c7337d5ba8e 106
cassyarduino 0:e3fb1267e3c3 107 private:
cassyarduino 4:5c7337d5ba8e 108
cassyarduino 0:e3fb1267e3c3 109 friend void uipudp_appcall(void);
cassyarduino 4:5c7337d5ba8e 110
cassyarduino 0:e3fb1267e3c3 111 friend class UIPEthernetClass;
cassyarduino 0:e3fb1267e3c3 112 static void _send(uip_udp_userdata_t *data);
cassyarduino 4:5c7337d5ba8e 113
cassyarduino 0:e3fb1267e3c3 114 };
cassyarduino 4:5c7337d5ba8e 115
cassyarduino 0:e3fb1267e3c3 116 #endif