mbed OS5

Fork of UIPEthernet by Zoltan Hudak

Committer:
hudakz
Date:
Mon Sep 15 11:12:30 2014 +0000
Revision:
0:5350a66d5279
Child:
2:049ce85163c5
rev. 00

Who changed what in which revision?

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