Blame view

src/mqttmessage.cpp 1.84 KB
51becbde   Peter M. Groen   Committed the ent...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
  /* 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;
  }