/*
  llrp.h

  Copyright (c) 2021 Kevin Matz (kevin.matz@gmail.com)

  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.
*/
#pragma once

#include "rdmnet/pdu.h"
#include "rdmnet/rdmnet.h"
#include "rdm/uid.h"
#include "rdm/message.h"
#include "uuid/uuid.h"

namespace RDMnet::LLRP {

/**
 * @brief 1.2.1 Low-Level Recovery Protocol (LLRP)
 *
 *  LLRP is a multicast protocol that provides basic IP configuration. It has
 *  its own limited discovery mechanism, which does not require a valid IP
 *  configuration. LLRP is intended as a light-weight protocol, and as such it
 *  is not designed for scalability or for discovery and normal operation on a
 *  large network.
 */


  /**
 * @brief 5.4 Packet Structure
 */
struct llrp_data
    : public ACN::PDU::pdu_data
{
  UUID::uuid destination;              //!< The receiver's unique CID or LLRP_BROADCAST_CID,
  uint32_t transaction;                //!< Used to match request / response messages.
  std::shared_ptr<RDMnet::Pdu> child;  //!< Probe Request PDU, Probe Reply PDU or RDM Command PDU...
  size_t streamSize() const override { return 20 + child->streamSize(); }
  void   iStream(ACN::PDU::Stream) override;
  void   oStream(ACN::PDU::Stream) const override;
};


/**
 * @brief The LLRP::Pdu class
 */
class Pdu
  : public RDMnet::Pdu
{
public:
  Pdu();
  void iStream(ACN::PDU::Stream) override;
};


namespace ProbeRequest {

/**
 * @brief The filter_t struct
 * This bit field indicates a filter to be checked against the state of an
 * LLRP Target.
 */
struct filter_t {
  union {
    uint16_t value = 0;
    struct {
      bool client_tcp_inactive : 1;   //!< filter inactive clients
      bool brokers_only        : 1;   //!< only brokers
      uint16_t reserved        : 14;  //!< reserved bits
    } __attribute__((packed));
  };
};


/**
 * @brief The request_data struct
 * Table 5-2: Probe Request PDU
 */
struct request_data
    : public ACN::PDU::pdu_data
{
  RDM::UID lower;               //!< UID of upper bound
  RDM::UID upper;               //!< UID of lower bound
  filter_t filter;              //!< device vilters
  std::vector<RDM::UID> known;  //!< known UIDs

  size_t streamSize() const override { return 14 + (6 * known.size()); }
  void   iStream(ACN::PDU::Stream) override;
  void   oStream(ACN::PDU::Stream) const override;
};


/**
 * @brief The LLRP::ProbeRequest::Pdu class
 */
class Pdu
  : public RDMnet::Pdu
{
public:
  Pdu();
  void iStream(ACN::PDU::Stream) override;
};

} // namespace ProbeRequest

namespace ProbeReply {

/**
 * @brief The reply_data struct
 */
struct reply_data
    : public ACN::PDU::pdu_data
{
  RDM::UID id;            //!< device UID
  uint8_t  address[6];    //!< device address
  uint8_t  type;          //!< address type

  size_t streamSize() const override { return 13; }
  void   iStream(ACN::PDU::Stream) override;
  void   oStream(ACN::PDU::Stream) const override;
};


/**
 * @brief The LLRP::ProbeReply::Pdu class
 */
class Pdu
  : public RDMnet::Pdu
{
public:
  Pdu();
  void iStream(ACN::PDU::Stream) override;
};

} // namespace ProbeReply


namespace COMMAND {
/// 5.5 Allowed Parameter Messages
/// A limited subset of RDM Parameter Messages are allowed to be sent over LLRP
static const PID allowed_pids[] = {
  COMPONENT_SCOPE,
  SEARCH_DOMAIN,
  TCP_COMMS_STATUS,
  BROKER_STATUS,
  RDM::LIST_INTERFACES,
  RDM::INTERFACE_LABEL,
  RDM::INTERFACE_HARDWARE_ADDRESS_TYPE1,
  RDM::IPV4_DHCP_MODE,
  RDM::IPV4_ZEROCONF_MODE,
  RDM::IPV4_CURRENT_ADDRESS,
  RDM::IPV4_STATIC_ADDRESS,
  RDM::INTERFACE_RENEW_DHCP,
  RDM::INTERFACE_RELEASE_DHCP,
  RDM::INTERFACE_APPLY_CONFIGURATION,
  RDM::IPV4_DEFAULT_ROUTE,
  RDM::DNS_IPV4_NAME_SERVER,
  RDM::DNS_HOSTNAME,
  RDM::DNS_DOMAIN_NAME,
  RDM::DEVICE_INFO,
  RDM::RESET_DEVICE,
  RDM::FACTORY_DEFAULTS,
  RDM::DEVICE_LABEL,
  RDM::MANUFACTURER_LABEL,
  RDM::DEVICE_MODEL_DESCRIPTION,
  RDM::IDENTIFY_DEVICE,
  RDM::LOCK_STATE_DESCRIPTION,
  RDM::LOCK_STATE
};
} // namespace COMMAND

} // namespace RDMnet::LLRP
