/****************************************************************************
 * 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.
 ****************************************************************************/

#ifndef AQH_M_MQTT_H
#define AQH_M_MQTT_H


#include <aqhome/api.h>
#include <aqhome/ipc2/message.h>

#include <gwenhywfar/buffer.h>



#define AQH_MQTTMSG_OFFS_CONTROL           0
#define AQH_MQTTMSG_OFFS_REMAINING_LENGTH  1


/* from https://docs.solace.com/API/MQTT-311-Prtl-Conformance-Spec/MQTT%20Control%20Packet%20format.htm
 */
#define AQH_MQTTMSG_MSGTYPE_CONNECT      0x10u
#define AQH_MQTTMSG_MSGTYPE_CONNACK      0x20u
#define AQH_MQTTMSG_MSGTYPE_PUBLISH      0x30u
#define AQH_MQTTMSG_MSGTYPE_PUBACK       0x40u
#define AQH_MQTTMSG_MSGTYPE_PUBREC       0x50u /* assured delivery part 1 */
#define AQH_MQTTMSG_MSGTYPE_PUBREL       0x62u /* assured delivery part 2 */
#define AQH_MQTTMSG_MSGTYPE_PUBCOMP      0x70u /* assured delivery part 3 */
#define AQH_MQTTMSG_MSGTYPE_SUBSCRIBE    0x82u
#define AQH_MQTTMSG_MSGTYPE_SUBACK       0x90u
#define AQH_MQTTMSG_MSGTYPE_UNSUBSCRIBE  0xa2u
#define AQH_MQTTMSG_MSGTYPE_UNSUBACK     0xb0u
#define AQH_MQTTMSG_MSGTYPE_PINGREQ      0xc0u
#define AQH_MQTTMSG_MSGTYPE_PINGRESP     0xd0u
#define AQH_MQTTMSG_MSGTYPE_DISCONNECT   0xe0u


#define AQH_MQTTMSG_FLAGS_DUP    0x08u
#define AQH_MQTTMSG_FLAGS_QOS2   0x04u
#define AQH_MQTTMSG_FLAGS_QOS1   0x02u
#define AQH_MQTTMSG_FLAGS_RETAIN 0x01u



AQHOME_API AQH_MESSAGE *AQH_MqttMessage_new(uint8_t typeAndFlags, uint32_t payloadLen, const uint8_t *payload);

AQHOME_API int AQH_MqttMessage_GetTypeAndFlags(const AQH_MESSAGE *msg);

AQHOME_API void AQH_MqttMessage_AppendStringWithLenToBuffer(GWEN_BUFFER *buf, const char *s);
AQHOME_API char *AQH_MqttMessage_ExtractStringAt(const uint8_t *ptr, uint32_t len);
AQHOME_API int AQH_MqttMessage_SkipStringAt(const uint8_t *ptr, uint32_t len);

AQHOME_API int AQH_MqttMessage_GetOffsetOfPayload(const AQH_MESSAGE *msg);

AQHOME_API const char *AQH_MqttMessage_MsgTypeToString(uint8_t t);



#endif
