/*
 *  Udp.cpp: Library to send/receive UDP packets with the Arduino ethernet shield.
 *  This version only offers minimal wrapping of socket.c/socket.h
 *  Drop Udp.h/.cpp into the Ethernet library directory at hardware/libraries/Ethernet/ 
 *
 * NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these)
 * 1) UDP does not guarantee the order in which assembled UDP packets are received. This
 * might not happen often in practice, but in larger network topologies, a UDP
 * packet can be received out of sequence. 
 * 2) UDP does not guard against lost packets - so packets *can* disappear without the sender being
 * aware of it. Again, this may not be a concern in practice on small local networks.
 * For more information, see http://www.cafeaulait.org/course/week12/35.html
 *
 * MIT License:
 * Copyright (c) 2008 Bjoern Hartmann
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * bjoern@cs.stanford.edu 12/30/2008
 */

#ifndef udp_h
#define udp_h

#define UDP_TX_PACKET_MAX_SIZE 24

class UdpClass {
private:
  uint8_t _sock;  // socket ID for Wiz5100
  uint16_t _port; // local port to listen on
  uint16_t _offset; // offset into the packet being sent (used in streaming API)

public:
  UdpClass() : _sock(MAX_SOCK_NUM) {};
  uint8_t begin(uint16_t);	// initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
  int available();								// has data been received?

  // C-style buffer-oriented functions
  uint16_t sendPacket(uint8_t *, uint16_t, uint8_t *, uint16_t); //send a packet to specified peer 
  uint16_t sendPacket(const char[], uint8_t *, uint16_t);  //send a string as a packet to specified peer
  int readPacket(uint8_t *, uint16_t);		// read a received packet 
  int readPacket(uint8_t *, uint16_t, uint8_t *, uint16_t *);		// read a received packet, also return sender's ip and port 	
  // readPacket that fills a character string buffer
  int readPacket(char *, uint16_t, uint8_t *, uint16_t &);
  // Finish with the UDP socket
  void stop();


  // API to let you build up a packet in a number of steps.  Useful for when
  // you need to send a large packet but don't have enough memory to hold it
  // all in place at one time

  // Start building a packet to send to the specified peer
  // @param ip - destination IP address
  // @param port - destination port number
  // @return 1 if successful, or 0 if there was an error
  uint16_t startSendPacket(uint8_t *ip, uint16_t port);

  // Add data to the packet about to be sent.  Only valid to call this after
  // a call to startSendPacket and before finishSendPacket
  // @param buf - data to append to the outgoing packet
  // @param len - number of bytes to append from buf
  // @return Number of bytes successfully appended
  uint16_t addData(uint8_t *buf, uint16_t len);

  // Send the packet.  Only valid to call this after a call to startSendPacket
  // and at least one call to addData
  // @return 1 if successful, or 0 if there was an error
  uint16_t finishSendPacket(); //send the packet


  // Streaming API for reading packets.  Can't be mixed and matched with
  // the readPacket calls.  Useful for processing large incoming packets
  // where you don't have enough space to read it in one go

  // Start reading in a packet of data
  //  @param ip Buffer to hold the IP address of the server that sent the packet
  //  @param port The remote port that the packet came from
  //  @return Number of bytes of data in the packet or -1 if there was an error
  int startReadPacket(uint8_t *ip, uint16_t &port);

  // Read a bufLen bytes of data from the packet.  Only valid after a call to
  // startReadPacket.
  //  @param buf Buffer to hold the data read in
  //  @param bufLen Number of bytes to read
  //  @return Number of bytes successfully read
  int readPacketData(uint8_t *buf, uint16_t bufLen);

  // Read a single byte from the packet.  Only valid after a call to
  // startReadPacket.
  //  @return Byte read from the packet
  uint8_t readPacketByte();

  // Test whether or not all of the data for the packet has been read.  Only
  // valid after a call to startReadPacket.
  //  @return Returns true if the end of the packet has been reached, or false
  //          if there is still data left to be read
  int readPacketReachedEnd(); 

  // Finish reading in the packet.  This should be called when you have finished
  // with the contents of this packet.  Only valid after calls to
  // startReadPacket, readPacketData and readPacketByte
  void finishReadPacket();
};

#endif
