/****************************************************************************
 * 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_connack.h"
#include "aqhome/msg/mqtt/m_mqtt.h"

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



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

AQH_MESSAGE *AQH_MqttMessageConnAck_new(uint8_t flags, uint8_t result)
{
  AQH_MESSAGE *msg;
  GWEN_BUFFER *buf;

  buf=GWEN_Buffer_new(0, 64, 0, 1);
  GWEN_Buffer_AppendByte(buf, flags);
  GWEN_Buffer_AppendByte(buf, result);

  msg=AQH_MqttMessage_new(AQH_MQTTMSG_MSGTYPE_CONNECT, GWEN_Buffer_GetUsedBytes(buf), (const uint8_t*) GWEN_Buffer_GetStart(buf));
  GWEN_Buffer_free(buf);

  return msg;
}



uint8_t AQH_MqttMessageConnAck_GetResultFlags(const AQH_MESSAGE *msg)
{
  int idx;

  idx=AQH_MqttMessage_GetOffsetOfPayload(msg);
  if (idx>0) {
    const uint8_t *ptr;
    int len;

    ptr=AQH_Message_GetMsgPointer(msg)+idx;
    len=(int)AQH_Message_GetUsedSize(msg)-idx;
    if (len>0) {
      return ptr[0];
    }
  }

  return 0;
}



uint8_t AQH_MqttMessageConnAck_GetResultCode(const AQH_MESSAGE *msg)
{
  int idx;

  idx=AQH_MqttMessage_GetOffsetOfPayload(msg);
  if (idx>0) {
    const uint8_t *ptr;
    int len;

    ptr=AQH_Message_GetMsgPointer(msg)+idx;
    len=(int)AQH_Message_GetUsedSize(msg)-idx;
    if (len>1) {
      return ptr[1];
    }
  }

  return 0;
}







