/*
 * Copyright (C) 2020-2021 Aloysius Indrayanto
 *
 * This file is part of the eCxx library, see LICENSE file for the license details.
 */

/*
 * ----------------------------------------------------------------------------------
 * The code in this file is copied (with a slight modifications) from:
 *     Udp.h - a base class to send/receive UDP packets
 * ----------------------------------------------------------------------------------
 * Copyright (C) 2008 Bjoern Hartmann (bjoern@cs.stanford.edu)
 *
 * MIT License
 *
 * 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.
 * ----------------------------------------------------------------------------------
 */


#ifndef ECXX_PLATFORM_NODEMCU_UDP_H
#define ECXX_PLATFORM_NODEMCU_UDP_H


#include "IPAddress.h"
#include "Stream.h"


namespace eCxx {

namespace NodeMCU {


class UDP: public Stream {
    public:
        virtual ~UDP() {};
        virtual uint8_t begin(uint16_t) = 0;
        virtual void stop() = 0;

        // Sending UDP packets
        virtual int32_t beginPacket(IPAddress ip, uint16_t port) = 0;
        virtual int32_t beginPacket(const char* host, uint16_t port) = 0;
        virtual int32_t endPacket() = 0;
        virtual size_t write(uint8_t c) = 0;
        virtual size_t write(const uint8_t* buffer, size_t size) = 0;

        // Start processing the next available incoming packet
        virtual int32_t parsePacket() = 0;
        virtual int32_t available() = 0;
        virtual int32_t read() = 0;
        virtual int32_t read(unsigned char* buffer, size_t len) = 0;
        virtual int32_t read(char* buffer, size_t len) = 0;
        virtual int32_t peek() = 0;
        virtual void flush() = 0;

        virtual IPAddress remoteIP() = 0;
        virtual uint16_t remotePort() = 0;

    protected:
        inline uint8_t* rawIPAddress(IPAddress& addr)
        { return addr.raw_address(); }

        inline const uint8_t* rawIPAddress(const IPAddress& addr)
        { return addr.raw_address(); }
};


} // namespace NodeMCU

} // namespace eCxx


//
// For compatibility with the Arduino library part
//
typedef eCxx::NodeMCU::UDP UDP;


#endif
