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 12:49:39 2016 +0100
Revision:
3:6b1c9bd7773a
Parent:
2:2f693560ad53
Child:
4:5c7337d5ba8e
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 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 3:6b1c9bd7773a 58 UIPUDP(void); // Constructor
cassyarduino 3:6b1c9bd7773a 59 uint8_t
cassyarduino 3:6b1c9bd7773a 60 begin(uint16_t);// initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
cassyarduino 3:6b1c9bd7773a 61 void
cassyarduino 3:6b1c9bd7773a 62 stop(void); // Finish with the UDP socket
cassyarduino 0:e3fb1267e3c3 63
cassyarduino 0:e3fb1267e3c3 64 // Sending UDP packets
cassyarduino 0:e3fb1267e3c3 65
cassyarduino 0:e3fb1267e3c3 66 // Start building up a packet to send to the remote host specific in ip and port
cassyarduino 0:e3fb1267e3c3 67 // Returns 1 if successful, 0 if there was a problem with the supplied IP address or port
cassyarduino 3:6b1c9bd7773a 68 int
cassyarduino 3:6b1c9bd7773a 69 beginPacket(IPAddress ip, uint16_t port);
cassyarduino 0:e3fb1267e3c3 70 // Start building up a packet to send to the remote host specific in host and port
cassyarduino 0:e3fb1267e3c3 71 // Returns 1 if successful, 0 if there was a problem resolving the hostname or port
cassyarduino 3:6b1c9bd7773a 72 int
cassyarduino 3:6b1c9bd7773a 73 beginPacket(const char *host, uint16_t port);
cassyarduino 0:e3fb1267e3c3 74 // Finish off this packet and send it
cassyarduino 0:e3fb1267e3c3 75 // Returns 1 if the packet was sent successfully, 0 if there was an error
cassyarduino 3:6b1c9bd7773a 76 int
cassyarduino 3:6b1c9bd7773a 77 endPacket(void);
cassyarduino 0:e3fb1267e3c3 78 // Write a single byte into the packet
cassyarduino 3:6b1c9bd7773a 79 size_t
cassyarduino 3:6b1c9bd7773a 80 write(uint8_t);
cassyarduino 0:e3fb1267e3c3 81 // Write size bytes from buffer into the packet
cassyarduino 3:6b1c9bd7773a 82 size_t
cassyarduino 3:6b1c9bd7773a 83 write(const uint8_t *buffer, size_t size);
cassyarduino 0:e3fb1267e3c3 84
cassyarduino 0:e3fb1267e3c3 85 #if defined(ARDUINO)
cassyarduino 0:e3fb1267e3c3 86 using Print::write;
cassyarduino 0:e3fb1267e3c3 87 #endif
cassyarduino 0:e3fb1267e3c3 88 // Start processing the next available incoming packet
cassyarduino 0:e3fb1267e3c3 89 // Returns the size of the packet in bytes, or 0 if no packets are available
cassyarduino 3:6b1c9bd7773a 90 int
cassyarduino 3:6b1c9bd7773a 91 parsePacket(void);
cassyarduino 0:e3fb1267e3c3 92 // Number of bytes remaining in the current packet
cassyarduino 3:6b1c9bd7773a 93 int
cassyarduino 3:6b1c9bd7773a 94 available(void);
cassyarduino 0:e3fb1267e3c3 95 // Read a single byte from the current packet
cassyarduino 3:6b1c9bd7773a 96 int
cassyarduino 3:6b1c9bd7773a 97 read(void);
cassyarduino 0:e3fb1267e3c3 98 // Read up to len bytes from the current packet and place them into buffer
cassyarduino 0:e3fb1267e3c3 99 // Returns the number of bytes read, or 0 if none are available
cassyarduino 3:6b1c9bd7773a 100 int
cassyarduino 3:6b1c9bd7773a 101 read(unsigned char* buffer, size_t len);
cassyarduino 0:e3fb1267e3c3 102 // Read up to len characters from the current packet and place them into buffer
cassyarduino 0:e3fb1267e3c3 103 // Returns the number of characters read, or 0 if none are available
cassyarduino 3:6b1c9bd7773a 104 int
cassyarduino 3:6b1c9bd7773a 105 read(char* buffer, size_t len)
cassyarduino 0:e3fb1267e3c3 106 {
cassyarduino 0:e3fb1267e3c3 107 return read((unsigned char*) buffer, len);
cassyarduino 0:e3fb1267e3c3 108 }
cassyarduino 0:e3fb1267e3c3 109 ;
cassyarduino 0:e3fb1267e3c3 110 // Return the next byte from the current packet without moving on to the next byte
cassyarduino 3:6b1c9bd7773a 111 int
cassyarduino 3:6b1c9bd7773a 112 peek(void);
cassyarduino 3:6b1c9bd7773a 113 void
cassyarduino 3:6b1c9bd7773a 114 flush(void); // Finish reading the current packet
cassyarduino 0:e3fb1267e3c3 115
cassyarduino 0:e3fb1267e3c3 116 // Return the IP address of the host who sent the current incoming packet
cassyarduino 3:6b1c9bd7773a 117 IPAddress
cassyarduino 3:6b1c9bd7773a 118 remoteIP(void);
cassyarduino 0:e3fb1267e3c3 119
cassyarduino 0:e3fb1267e3c3 120 // Return the port of the host who sent the current incoming packet
cassyarduino 3:6b1c9bd7773a 121 uint16_t
cassyarduino 3:6b1c9bd7773a 122 remotePort(void);
cassyarduino 0:e3fb1267e3c3 123
cassyarduino 0:e3fb1267e3c3 124 private:
cassyarduino 0:e3fb1267e3c3 125
cassyarduino 0:e3fb1267e3c3 126 friend void uipudp_appcall(void);
cassyarduino 0:e3fb1267e3c3 127
cassyarduino 0:e3fb1267e3c3 128 friend class UIPEthernetClass;
cassyarduino 0:e3fb1267e3c3 129 static void _send(uip_udp_userdata_t *data);
cassyarduino 0:e3fb1267e3c3 130
cassyarduino 0:e3fb1267e3c3 131 };
cassyarduino 0:e3fb1267e3c3 132
cassyarduino 0:e3fb1267e3c3 133 #endif