/****************************************************************************
 * This file is part of the project AqHome.
 * AqHome (c) by 2025 Martin Preuss, all rights reserved.
 *
 * The license for this file can be found in the file COPYING which you
 * should have received along with this file.
 ****************************************************************************/

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include "aqhome/msg/mqtt/m_mqtt_subscribe.h"
#include "aqhome/msg/mqtt/m_mqtt.h"

#include <gwenhywfar/text.h>
#include <gwenhywfar/debug.h>



/* ------------------------------------------------------------------------------------------------
 * implementation
 * ------------------------------------------------------------------------------------------------
 */

AQH_MESSAGE *AQH_MqttMessageSubscribe_new(uint8_t flags, uint16_t packetId, const char *sTopic, uint8_t requestedQos)
{
  AQH_MESSAGE *msg;
  GWEN_BUFFER *buf;

  buf=GWEN_Buffer_new(0, 64, 0, 1);
  GWEN_Buffer_AppendByte(buf, (packetId>>8) & 0xff);
  GWEN_Buffer_AppendByte(buf, packetId & 0xff);
  /* add topic filter / qos pair */
  AQH_MqttMessage_AppendStringWithLenToBuffer(buf, sTopic);
  GWEN_Buffer_AppendByte(buf, requestedQos);

  msg=AQH_MqttMessage_new(AQH_MQTTMSG_MSGTYPE_SUBSCRIBE | flags,
                          GWEN_Buffer_GetUsedBytes(buf),
                          (const uint8_t*) GWEN_Buffer_GetStart(buf));
  GWEN_Buffer_free(buf);
  return msg;
}



