-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmqtt.cpp
266 lines (231 loc) · 5.61 KB
/
mqtt.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include <cstring>
#include <cstdio>
#include <iostream>
#include "mqtt.h"
using namespace std;
string const mqtt::client::PUBLISH_TOPIC = "EXAMPLE_TOPIC";
/**
* @brief mqtt::client
* @param id
* @param host
* @param port
*/
mqtt::client::client(string const& id, string const& host, uint16_t port) : mosquittopp(id.c_str()),last_err(mqtt::errors::UNKNOWN),connected(false){
int rc = connect(host, port, static_cast<int>(DEFAULT_KEEP_ALIVE));
last_err = static_cast<mqtt::errors>(rc);
if(mqtt::errors::SUCCESS == last_err){
connected = true;
}
return;
}
mqtt::client::~client()
{
int rc;
for(string topic:subscribed_to){
rc = unsubscribe(nullptr,topic);
}
rc = disconnect();
last_err = static_cast<mqtt::errors>(rc);
if(mqtt::errors::SUCCESS == last_err){
connected = false;
}
return;
}
//-----
/**
* @brief mqtt::client::do_subscribe
* @param topic
*/
void mqtt::client::do_subscribe(string const& topic){
int rc = subscribe(nullptr, topic);;
last_err = static_cast<mqtt::errors>(rc);
if(mqtt::errors::SUCCESS == last_err){
subscribed_to.push_back(topic);
}
return;
}
/**
* @brief mqtt::client::do_publish
* @param topic
* @param payload
*/
void mqtt::client::do_publish(string const& topic, string const& payload){
int rc = publish(nullptr,topic,
static_cast<int>(payload.size()+1),
reinterpret_cast<const void *>(payload.data()));
last_err = static_cast<mqtt::errors>(rc);
return;
}
/**
* @brief mqtt::client::do_reconnect
*/
void mqtt::client::do_reconnect(){
int rc = reconnect();
last_err = static_cast<mqtt::errors>(rc);
return;
}
/**
* @brief mqtt::client::do_loop
* @param milliseconds
*/
void mqtt::client::do_loop(uint32_t milliseconds){
int rc = loop(int(milliseconds),1);
last_err = static_cast<mqtt::errors>(rc);
return;
}
//-----
/**
* @brief mqtt::client::set_last_err
* @param rc
*/
void mqtt::client::set_last_err(int rc){
last_err = static_cast<mqtt::errors>(rc);
}
/**
* @brief mqtt::client::get_last_err
* @return
*/
mqtt::errors mqtt::client::get_last_err(){
return last_err;
}
/**
* @brief mqtt::client::is_last_err
* @return
*/
bool mqtt::client::is_last_err(){
return (mqtt::errors::SUCCESS != last_err);
}
/**
* @brief mqtt::client::error_to_string
* @return
*/
string mqtt::client::error_to_string(){
return mosqpp::strerror(static_cast<int>(last_err));
}
//-----
//-----
/**
* @brief mqtt::client::on_connect
* @param rc
*/
void mqtt::client::on_connect(int rc){
last_err = static_cast<mqtt::errors>(rc);
if(true){
cout << "Connected - code " << rc << " - " << mosqpp::connack_string(rc) << "\n";
}
return;
}
/**
* @brief mqtt::client::on_connect_with_flags
* @param rc
* @param flags
*/
void mqtt::client::on_connect_with_flags(int rc, int flags){
last_err = static_cast<mqtt::errors>(rc);
cout << strerror(rc) << "\n";
cout << flags << "\n";
return;
}
/**
* @brief mqtt::client::on_disconnect
* @param rc
*/
void mqtt::client::on_disconnect(int rc){
last_err = static_cast<mqtt::errors>(rc);
return;
}
/**
* @brief mqtt::client::on_publish
* @param mid
*/
void mqtt::client::on_publish(int mid){
if(true){
cout << "Publishing succeeded." << "\n";
cout << " -> Message ID: " << mid << "\n";
}
return;
}
/**
* @brief mqtt::client::on_message
* @param message
*/
void mqtt::client::on_message(struct mosquitto_message const* message){
string buff;
//buff.reserve(MAX_PAYLOAD);
if(0==PUBLISH_TOPIC.compare(message->topic)){
char const* cptr = reinterpret_cast<char const*>(message->payload);
size_t const count = size_t(message->payloadlen);
buff.insert(0,cptr,count);
if(true){
cout << "[" << message->payloadlen << "] -" << buff.c_str() << "\n";
}
// Examples of messages for M2M communications...
if (!strcmp(buff.c_str(), "STATUS")){
buff = "This is a Status Message...";
publish(nullptr, PUBLISH_TOPIC.c_str(), static_cast<int>(buff.size()), buff.c_str());
if(true){
cout << "Status Request Recieved." << "\n";
}
}
if (!strcmp(buff.c_str(), "ON")){
buff = "Turning on...";
publish(nullptr, PUBLISH_TOPIC.c_str(), static_cast<int>(buff.size()), buff.c_str());
if(true){
cout << "Request to turn on." << "\n";
}
}
if (!strcmp(buff.c_str(), "OFF")){
buff = "Turning off...";
publish(nullptr, PUBLISH_TOPIC.c_str(), static_cast<int>(buff.size()), buff.c_str());
if(true){
cout << "Request to turn off." << std:: endl;
}
}
}
return;
}
/**
* @brief mqtt::client::on_subscribe
* @param mid
* @param qos_count
* @param granted_qos
*/
void mqtt::client::on_subscribe(int mid, int qos_count, const int *granted_qos){
if(true){
cout << "Subscription succeeded." << "\n";
cout << " -> Message ID : " << mid << "\n";
cout << " -> Quality of Service of subscriptions - " << qos_count << "\n";
for(size_t ii=0; ii<static_cast<size_t>(qos_count); ++ii){
cout << " -> [" << ii << "]: " << granted_qos[ii] << "\n";
}
}
return;
}
/**
* @brief mqtt::client::on_unsubscribe
* @param mid
*/
void mqtt::client::on_unsubscribe(int mid){
cout << "Unsubscribe" << "\n";
cout << " -> Message ID: " << mid << "\n";
return;
}
/**
* @brief mqtt::client::on_log
* @param level
* @param str
*/
void mqtt::client::on_log(int level, std::string const& str){
if(false){
cout << "Log[" << level << "]: " << str << "\n";
}
return;
}
/**
* @brief mqtt::client::on_error
*/
void mqtt::client::on_error(){
last_err = mqtt::errors::UNKNOWN;
cout << "!!!Error!!!" << "\n";
return;
}