mqttmessage.cpp 1.84 KB
/* Copyright (C) 2019
 *
 * This file is part of the osdev components suite
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
 */
#include "mqttmessage.h"

using namespace osdev::components::mqtt;

MqttMessage::MqttMessage()
    : m_retained()
    , m_duplicate()
    , m_topic()
    , m_payload()
{}

MqttMessage::MqttMessage(const std::string &_topic, const MQTTAsync_message &message)
    : m_retained( 0 != message.retained )
    , m_duplicate( 0 != message.dup )
    , m_topic( _topic )
    , m_payload()
{
    const char *msg = reinterpret_cast<const char*>(message.payload);
    m_payload = std::string( msg, msg + message.payloadlen );
}

MqttMessage::MqttMessage( const std::string &_topic, bool retainedFlag, bool duplicateFlag, std::string thePayload )
    : m_retained( retainedFlag )
    , m_duplicate( duplicateFlag )
    , m_topic( _topic )
    , m_payload( thePayload )
{}

MQTTAsync_message MqttMessage::toAsyncMessage() const
{
    MQTTAsync_message msg = MQTTAsync_message_initializer;
    msg.payload = reinterpret_cast<void*>( const_cast<char*>( m_payload.c_str() ) );
    msg.payloadlen = static_cast<int>( m_payload.size() );
    msg.qos = -1;
    msg.retained = static_cast<int>( m_retained );

    return msg;
}