-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmqttlistener.cpp
181 lines (140 loc) · 5.02 KB
/
mqttlistener.cpp
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
172
173
174
175
176
177
178
179
180
181
#include "mqttlistener.h"
#include <iostream>
using namespace std;
#include <QDataStream>
#include <QVector>
// --- CONSTRUCTOR ---
// Connect to the MQTT broker using the provided host and port details.
MqttListener::MqttListener(QObject *parent, QString clientId, QString host, int port) : QObject(parent), mosquittopp(clientId.toStdString().c_str()) {
this->host = host;
this->port = port;
}
// --- DECONSTRUCTOR ---
MqttListener::~MqttListener() {
mosqpp::lib_cleanup();
emit finished();
}
// --- SET TLS ---
// Set the CA, Cert and Key file for TLS connections.
bool MqttListener::setTLS(string &ca, string &cert, string &key) {
cout << "CA: " << ca << ", cert: " << cert << ", key: " << key << ".\n";
int res = tls_set(ca.c_str(), 0, cert.c_str(), key.c_str(), 0);
if (res != MOSQ_ERR_SUCCESS) {
if (res == MOSQ_ERR_INVAL) {
cerr << "Invalid input parameters for Mosquitto TLS.\n";
}
else if (res == MOSQ_ERR_NOMEM) {
cerr << "Out of memory on Mosquitto TLS.\n";
}
else {
cerr << "Unknown Mosquitto TLS error.\n";
}
return false;
}
tls_opts_set(0, 0, 0); // SSL_VERIFY_NONE
tls_insecure_set(1); // insecure connect.
return true;
}
// --- SET PASSWORD ---
// Set the username and password for this MQTT connection.
bool MqttListener::setPassword(string &username, string &password) {
int res = username_pw_set(username.c_str(), password.c_str());
if (res != MOSQ_ERR_SUCCESS) {
if (res == MOSQ_ERR_INVAL) {
cerr << "Invalid input parameters for Mosquitto PW.\n";
}
else if (res == MOSQ_ERR_NOMEM) {
cerr << "Out of memory on Mosquitto PW.\n";
}
else {
cerr << "Unknown Mosquitto PW error.\n";
}
return false;
}
return true;
}
// --- CONNECT BROKER ---
void MqttListener::connectBroker() {
cout << "Connecting to broker: " << host.toStdString() << ":" << port << "\n";
int keepalive = 60;
mosqpp::mosquittopp::connect(host.toStdString().c_str(), port, keepalive);
int rc;
while (1) {
rc = loop();
if (rc) {
if (rc == MOSQ_ERR_INVAL) {
cerr << "Loop: received ERR_INVAL error.\n";
}
else if (rc == MOSQ_ERR_NO_CONN) {
cerr << "Loop: received ERR_NO_CONN.\n";
}
else if (rc == MOSQ_ERR_SUCCESS) {
cerr << "Loop: received ERR_SUCCESS.\n";
}
else if (rc == MOSQ_ERR_ERRNO) {
cerr << "Loop: received ERR_ERRNO.\n";
#ifdef WIN32
// TODO: Use FormatMessage() to get the error string.
cerr << "Loop: received error: " << strerror(errno) << endl;
#else
// TODO: Use strerror_r() to get the error string.
//cerr << "Loop: system error: " << strerror(errno) << endl;
#endif
}
else {
cerr << "Loop: received error: " << rc << endl;
}
emit failed("Failed to connect to remote server.");
break;
//cout << "Disconnected. Trying to reconnect...\n";
//reconnect();
}
}
}
// --- DISCONNECT BROKER ---
// Disconnect the currently connected broker, if any.
void MqttListener::disconnectBroker() {
mosqpp::mosquittopp::disconnect();
}
// --- ON CONNECT ---
void MqttListener::on_connect(int rc) {
cout << "Connected.\n";
if (rc == 0) {
cout << "Subscribing to topics...\n";
emit connected();
}
else {
// handle.
cerr << "Connection failed. Aborting subscribing.\n";
emit failed("Connecting failed. Aborting subscribing");
}
}
// --- ON SUBSCRIBE ---
void MqttListener::on_subscribe(int mid, int qos_count, const int *granted_qos) {
//
}
// --- ON MESSAGE ---
void MqttListener::on_message(const mosquitto_message *message) {
string topic = message->topic;
string payload = string((const char*) message->payload, message->payloadlen);
cout << "Received for topic: " << topic << ", message: " << payload << endl;
emit receivedMessage(topic, payload);
}
// --- ON LOG ---
void MqttListener::on_log(int level, const char *str) {
cout << "LOG: " << str << ".\n";
}
// --- PUBLISH MESSAGE ---
void MqttListener::publishMessage(string topic, string msg) {
publish(0, topic.c_str(), msg.length(), msg.c_str());
}
// --- ADD SUBSCRIPTION ---
void MqttListener::addSubscription(string topic) {
cout << "MqttListener: Adding subscription for: " << topic << endl;
subscribe(0, topic.c_str(), 1);
}
// --- REMOVE SUBSCRIPTION ---
void MqttListener::removeSubscription(string topic) {
cout << "MqttListener: Removing subscription for: " << topic << endl;
unsubscribe(0, topic.c_str());
}