aboutsummaryrefslogtreecommitdiffstats
path: root/src/MqttClient.cpp
blob: 38c8ec64477b646aae22b1c4601dd4d56842c130 (plain)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
 * Copyright (C) 2019,2024 Konsulko Group
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <iostream>
#include <cstring>

#include "MqttClient.h"
#include "GlobalConfig.h"

MqttClient::MqttClient(const MqttConfig &config) :
	m_config(config)
{
	mosquitto_lib_init();
	m_mosq = mosquitto_new(m_config.clientId().c_str(), m_config.cleanOnDisconnect(), this);

	mosquitto_connect_callback_set(m_mosq, onConnect);
	mosquitto_disconnect_callback_set(m_mosq, onDisconnect);
	
	if(m_config.username().size()) {
		mosquitto_username_pw_set(m_mosq,
					  m_config.username().c_str(),
					  m_config.password().c_str());
	}

	m_loop = mosquitto_loop_start(m_mosq);
	if(m_loop != MOSQ_ERR_SUCCESS){
		std::cerr << __FUNCTION__ << ": Unable to start loop, error = " << m_loop << std::endl;
	}
}

bool MqttClient::start()
{
	if (!(m_mosq && m_loop == MOSQ_ERR_SUCCESS))
		return false;

	if (!(m_config.username().empty() || m_config.password().empty())) {
		std::cout << "Using MQTT username & password" << std::endl;
		if (mosquitto_username_pw_set(m_mosq,
					      m_config.username().c_str(),
					      m_config.password().c_str()) != MOSQ_ERR_SUCCESS) {
			return false;
		}
	}

	if (m_config.useTls() && !m_config.caCertFile().empty()) {
		std::cout << "Using MQTT TLS server certificate" << std::endl;
		char *clientCertFile = NULL;
		char *clientKeyFile = NULL;
		if (!(m_config.clientCertFile().empty() || m_config.clientKeyFile().empty())) {
			clientCertFile = strdup(m_config.clientCertFile().c_str());
			clientKeyFile = strdup(m_config.clientKeyFile().c_str());
			std::cout << "Using MQTT TLS client certificate" << std::endl;
		}
		if (mosquitto_tls_set(m_mosq,
				      m_config.caCertFile().c_str(),
				      NULL,
				      clientCertFile,
				      clientKeyFile,
				      NULL) != MOSQ_ERR_SUCCESS) {
			std::cerr << "Error configuring MQTT TLS support" << std::endl;
			free(clientCertFile);
			free(clientKeyFile);
			return false;
		}

		if (!m_config.verifyServerHostname()) {
			if (mosquitto_tls_insecure_set(m_mosq, true) != MOSQ_ERR_SUCCESS) {
				return false;
			}
		}
	}

	bool rc = true;
	std::cout << "Using MQTT server " << m_config.hostname() << ":" << m_config.port() << std::endl;
	if(mosquitto_connect_async(m_mosq,
				   m_config.hostname().c_str(),
				   m_config.port(),
				   m_config.keepalive())) {
		std::cerr << "Unable to start MQTT connection to " << m_config.hostname() << std::endl;
		rc = false;
	}

	return rc;
}

MqttClient::~MqttClient(void)
{
	mosquitto_disconnect(m_mosq);
	mosquitto_loop_stop(m_mosq, true);
	mosquitto_destroy(m_mosq);
	mosquitto_lib_cleanup();
}

int MqttClient::publish(const std::string &topic, const char *data, const int len, const unsigned qos, const bool retain)
{
	if (len <= 0 || qos > 2)
		return -1;

	return mosquitto_publish(m_mosq,
				 NULL,
				 topic.c_str(),
				 len,
				 data,
				 qos,
				 retain);
}

int MqttClient::publish(const std::string &topic, const char *data, const int len)
{
	if (len <= 0)
		return -1;

	return mosquitto_publish(m_mosq,
				 NULL,
				 topic.c_str(),
				 len,
				 data,
				 m_config.qos(),
				 m_config.retain());
}

int MqttClient::publish(const char *data, const int len)
{
	if (len <= 0)
		return -1;

	return mosquitto_publish(m_mosq,
				 NULL,
				 m_config.topic().c_str(),
				 len,
				 data,
				 m_config.qos(),
				 m_config.retain());
}

// Private

void MqttClient::onConnect(struct mosquitto *mosq, void *obj, int rc)
{
	if (!obj)
		return;

	MqttClient *p = (MqttClient*) obj;
	p->handleConnect(rc);
}

void MqttClient::onDisconnect(struct mosquitto *mosq, void *obj, int rc)
{
	if (!obj)
		return;

	MqttClient *p = (MqttClient*) obj;
	p->handleDisconnect(rc);
}

void MqttClient::handleConnect(int rc)
{
	m_connected = true;
	if (g_config.verbose())
		std::cerr << "MQTT Connected, rc = " << rc << std::endl;
}

void MqttClient::handleDisconnect(int rc)
{
	m_connected = false;
	if (g_config.verbose())
		std::cerr << "MQTT Disconnected, rc = " << rc << std::endl;
}