-
Notifications
You must be signed in to change notification settings - Fork 158
/
mqttsimple.c
470 lines (406 loc) · 13 KB
/
mqttsimple.c
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/* mqttsimple.c
*
* Copyright (C) 2006-2024 wolfSSL Inc.
*
* This file is part of wolfMQTT.
*
* wolfMQTT 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 of the License, or
* (at your option) any later version.
*
* wolfMQTT 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
/* Standalone Example */
/* Include the autoconf generated config.h */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "wolfmqtt/mqtt_client.h"
#include "examples/mqttport.h"
#include "mqttsimple.h"
/* Requires BSD Style Socket */
#ifdef HAVE_SOCKET
#ifndef ENABLE_MQTT_TLS
#include <sys/types.h>
#include <sys/socket.h>
#ifndef WOLFSSL_LWIP
#include <netinet/in.h>
#endif
#include <netdb.h>
#include <unistd.h>
#endif
/* Configuration */
#define MQTT_HOST "test.mosquitto.org" /* broker.hivemq.com */
#define MQTT_QOS MQTT_QOS_0
#define MQTT_KEEP_ALIVE_SEC 60
#define MQTT_CMD_TIMEOUT_MS 30000
#define MQTT_CON_TIMEOUT_MS 5000
#define MQTT_CLIENT_ID "WolfMQTTClientSimple"
#define MQTT_TOPIC_NAME "wolfMQTT/example/testTopic"
#define MQTT_PUBLISH_MSG "Test Publish"
#define MQTT_USERNAME NULL
#define MQTT_PASSWORD NULL
#ifdef ENABLE_MQTT_TLS
#define MQTT_USE_TLS 1
#define MQTT_PORT 8883
#else
#define MQTT_USE_TLS 0
#define MQTT_PORT 1883
#endif
#define MQTT_MAX_PACKET_SZ 1024
#define INVALID_SOCKET_FD -1
#define PRINT_BUFFER_SIZE 80
/* Local Variables */
static MqttClient mClient;
static MqttNet mNetwork;
static int mSockFd = INVALID_SOCKET_FD;
static byte mSendBuf[MQTT_MAX_PACKET_SZ];
static byte mReadBuf[MQTT_MAX_PACKET_SZ];
static volatile word16 mPacketIdLast;
/* Local Functions */
/* msg_new on first data callback */
/* msg_done on last data callback */
/* msg->total_len: Payload total length */
/* msg->buffer: Payload buffer */
/* msg->buffer_len: Payload buffer length */
/* msg->buffer_pos: Payload buffer position */
static int mqtt_message_cb(MqttClient *client, MqttMessage *msg,
byte msg_new, byte msg_done)
{
byte buf[PRINT_BUFFER_SIZE+1];
word32 len;
(void)client;
if (msg_new) {
/* Determine min size to dump */
len = msg->topic_name_len;
if (len > PRINT_BUFFER_SIZE) {
len = PRINT_BUFFER_SIZE;
}
XMEMCPY(buf, msg->topic_name, len);
buf[len] = '\0'; /* Make sure its null terminated */
/* Print incoming message */
PRINTF("MQTT Message: Topic %s, Qos %d, Len %u",
buf, msg->qos, msg->total_len);
}
/* Print message payload */
len = msg->buffer_len;
if (len > PRINT_BUFFER_SIZE) {
len = PRINT_BUFFER_SIZE;
}
XMEMCPY(buf, msg->buffer, len);
buf[len] = '\0'; /* Make sure its null terminated */
PRINTF("Payload (%d - %d) printing %d bytes:" LINE_END "%s",
msg->buffer_pos, msg->buffer_pos + msg->buffer_len, len, buf);
if (msg_done) {
PRINTF("MQTT Message: Done");
}
/* Return negative to terminate publish processing */
return MQTT_CODE_SUCCESS;
}
static void setup_timeout(struct timeval* tv, int timeout_ms)
{
tv->tv_sec = timeout_ms / 1000;
tv->tv_usec = (timeout_ms % 1000) * 1000;
/* Make sure there is a minimum value specified */
if (tv->tv_sec < 0 || (tv->tv_sec == 0 && tv->tv_usec <= 0)) {
tv->tv_sec = 0;
tv->tv_usec = 100;
}
}
static int socket_get_error(int sockFd)
{
int so_error = 0;
socklen_t len = sizeof(so_error);
(void)getsockopt(sockFd, SOL_SOCKET, SO_ERROR, &so_error, &len);
return so_error;
}
static int mqtt_net_connect(void *context, const char* host, word16 port,
int timeout_ms)
{
int rc;
int sockFd, *pSockFd = (int*)context;
struct sockaddr_in addr;
struct addrinfo *result = NULL;
struct addrinfo hints;
if (pSockFd == NULL) {
return MQTT_CODE_ERROR_BAD_ARG;
}
(void)timeout_ms;
/* get address */
XMEMSET(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
XMEMSET(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
rc = getaddrinfo(host, NULL, &hints, &result);
if (rc >= 0 && result != NULL) {
struct addrinfo* res = result;
/* prefer ip4 addresses */
while (res) {
if (res->ai_family == AF_INET) {
break;
}
res = res->ai_next;
}
if (res) {
addr.sin_port = htons(port);
addr.sin_family = AF_INET;
addr.sin_addr =
((struct sockaddr_in*)(res->ai_addr))->sin_addr;
}
else {
rc = -1;
}
freeaddrinfo(result);
}
if (rc < 0) {
return MQTT_CODE_ERROR_NETWORK;
}
sockFd = socket(addr.sin_family, SOCK_STREAM, 0);
if (sockFd < 0) {
return MQTT_CODE_ERROR_NETWORK;
}
/* Start connect */
rc = connect(sockFd, (struct sockaddr*)&addr, sizeof(addr));
if (rc < 0) {
PRINTF("NetConnect: Error %d (Sock Err %d)",
rc, socket_get_error(*pSockFd));
close(sockFd);
return MQTT_CODE_ERROR_NETWORK;
}
/* save socket number to context */
*pSockFd = sockFd;
return MQTT_CODE_SUCCESS;
}
static int mqtt_net_read(void *context, byte* buf, int buf_len, int timeout_ms)
{
int rc;
int *pSockFd = (int*)context;
int bytes = 0;
struct timeval tv;
if (pSockFd == NULL) {
return MQTT_CODE_ERROR_BAD_ARG;
}
/* Setup timeout */
setup_timeout(&tv, timeout_ms);
(void)setsockopt(*pSockFd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,
sizeof(tv));
/* Loop until buf_len has been read, error or timeout */
while (bytes < buf_len) {
rc = (int)recv(*pSockFd, &buf[bytes], buf_len - bytes, 0);
if (rc < 0) {
rc = socket_get_error(*pSockFd);
if (rc == 0)
break; /* timeout */
PRINTF("NetRead: Error %d", rc);
return MQTT_CODE_ERROR_NETWORK;
}
bytes += rc; /* Data */
}
if (bytes == 0) {
return MQTT_CODE_ERROR_TIMEOUT;
}
return bytes;
}
static int mqtt_net_write(void *context, const byte* buf, int buf_len,
int timeout_ms)
{
int rc;
int *pSockFd = (int*)context;
struct timeval tv;
if (pSockFd == NULL) {
return MQTT_CODE_ERROR_BAD_ARG;
}
/* Setup timeout */
setup_timeout(&tv, timeout_ms);
(void)setsockopt(*pSockFd, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv,
sizeof(tv));
rc = (int)send(*pSockFd, buf, buf_len, 0);
if (rc < 0) {
PRINTF("NetWrite: Error %d (Sock Err %d)",
rc, socket_get_error(*pSockFd));
return MQTT_CODE_ERROR_NETWORK;
}
return rc;
}
static int mqtt_net_disconnect(void *context)
{
int *pSockFd = (int*)context;
if (pSockFd == NULL) {
return MQTT_CODE_ERROR_BAD_ARG;
}
close(*pSockFd);
*pSockFd = INVALID_SOCKET_FD;
return MQTT_CODE_SUCCESS;
}
#ifdef ENABLE_MQTT_TLS
static int mqtt_tls_verify_cb(int preverify, WOLFSSL_X509_STORE_CTX* store)
{
char buffer[WOLFSSL_MAX_ERROR_SZ];
PRINTF("MQTT TLS Verify Callback: PreVerify %d, Error %d (%s)",
preverify, store->error, store->error != 0 ?
wolfSSL_ERR_error_string(store->error, buffer) : "none");
PRINTF(" Subject's domain name is %s", store->domain);
if (store->error != 0) {
/* Allowing to continue */
/* Should check certificate and return 0 if not okay */
PRINTF(" Allowing cert anyways");
}
return 1;
}
/* Use this callback to setup TLS certificates and verify callbacks */
static int mqtt_tls_cb(MqttClient* client)
{
int rc = WOLFSSL_FAILURE;
/* Use highest available and allow downgrade. If wolfSSL is built with
* old TLS support, it is possible for a server to force a downgrade to
* an insecure version. */
client->tls.ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
if (client->tls.ctx) {
wolfSSL_CTX_set_verify(client->tls.ctx, WOLFSSL_VERIFY_PEER,
mqtt_tls_verify_cb);
/* default to success */
rc = WOLFSSL_SUCCESS;
#if !defined(NO_CERT)
#if 0
/* Load CA certificate buffer */
rc = wolfSSL_CTX_load_verify_buffer(client->tls.ctx, caCertBuf,
caCertSize, WOLFSSL_FILETYPE_PEM);
#endif
#if 0
/* If using a client certificate it can be loaded using: */
rc = wolfSSL_CTX_use_certificate_buffer(client->tls.ctx,
clientCertBuf, clientCertSize, WOLFSSL_FILETYPE_PEM);
#endif
#endif /* !NO_CERT */
}
PRINTF("MQTT TLS Setup (%d)", rc);
return rc;
}
#else
static int mqtt_tls_cb(MqttClient* client)
{
(void)client;
return 0;
}
#endif /* ENABLE_MQTT_TLS */
static word16 mqtt_get_packetid(void)
{
/* Check rollover */
if (mPacketIdLast >= MAX_PACKET_ID) {
mPacketIdLast = 0;
}
return ++mPacketIdLast;
}
/* Public Function */
int mqttsimple_test(void)
{
int rc = 0;
MqttObject mqttObj;
MqttTopic topics[1];
/* Initialize MQTT client */
XMEMSET(&mNetwork, 0, sizeof(mNetwork));
mNetwork.connect = mqtt_net_connect;
mNetwork.read = mqtt_net_read;
mNetwork.write = mqtt_net_write;
mNetwork.disconnect = mqtt_net_disconnect;
mNetwork.context = &mSockFd;
rc = MqttClient_Init(&mClient, &mNetwork, mqtt_message_cb,
mSendBuf, sizeof(mSendBuf), mReadBuf, sizeof(mReadBuf),
MQTT_CON_TIMEOUT_MS);
if (rc != MQTT_CODE_SUCCESS) {
goto exit;
}
PRINTF("MQTT Init Success");
/* Connect to broker */
rc = MqttClient_NetConnect(&mClient, MQTT_HOST, MQTT_PORT,
MQTT_CON_TIMEOUT_MS, MQTT_USE_TLS, mqtt_tls_cb);
if (rc != MQTT_CODE_SUCCESS) {
goto exit;
}
PRINTF("MQTT Network Connect Success: Host %s, Port %d, UseTLS %d",
MQTT_HOST, MQTT_PORT, MQTT_USE_TLS);
/* Send Connect and wait for Ack */
XMEMSET(&mqttObj, 0, sizeof(mqttObj));
mqttObj.connect.keep_alive_sec = MQTT_KEEP_ALIVE_SEC;
mqttObj.connect.client_id = MQTT_CLIENT_ID;
mqttObj.connect.username = MQTT_USERNAME;
mqttObj.connect.password = MQTT_PASSWORD;
rc = MqttClient_Connect(&mClient, &mqttObj.connect);
if (rc != MQTT_CODE_SUCCESS) {
goto exit;
}
PRINTF("MQTT Broker Connect Success: ClientID %s, Username %s, Password %s",
MQTT_CLIENT_ID,
(MQTT_USERNAME == NULL) ? "Null" : MQTT_USERNAME,
(MQTT_PASSWORD == NULL) ? "Null" : MQTT_PASSWORD);
/* Subscribe and wait for Ack */
XMEMSET(&mqttObj, 0, sizeof(mqttObj));
topics[0].topic_filter = MQTT_TOPIC_NAME;
topics[0].qos = MQTT_QOS;
mqttObj.subscribe.packet_id = mqtt_get_packetid();
mqttObj.subscribe.topic_count = sizeof(topics) / sizeof(MqttTopic);
mqttObj.subscribe.topics = topics;
rc = MqttClient_Subscribe(&mClient, &mqttObj.subscribe);
if (rc != MQTT_CODE_SUCCESS) {
goto exit;
}
PRINTF("MQTT Subscribe Success: Topic %s, QoS %d",
MQTT_TOPIC_NAME, MQTT_QOS);
/* Publish */
XMEMSET(&mqttObj, 0, sizeof(mqttObj));
mqttObj.publish.qos = MQTT_QOS;
mqttObj.publish.topic_name = MQTT_TOPIC_NAME;
mqttObj.publish.packet_id = mqtt_get_packetid();
mqttObj.publish.buffer = (byte*)MQTT_PUBLISH_MSG;
mqttObj.publish.total_len = XSTRLEN(MQTT_PUBLISH_MSG);
rc = MqttClient_Publish(&mClient, &mqttObj.publish);
if (rc != MQTT_CODE_SUCCESS) {
goto exit;
}
PRINTF("MQTT Publish: Topic %s, ID %d, Qos %d, Message %s",
mqttObj.publish.topic_name, mqttObj.publish.packet_id,
mqttObj.publish.qos, mqttObj.publish.buffer);
/* Wait for messages */
while (1) {
rc = MqttClient_WaitMessage_ex(&mClient, &mqttObj, MQTT_CMD_TIMEOUT_MS);
if (rc == MQTT_CODE_ERROR_TIMEOUT) {
/* send keep-alive ping */
rc = MqttClient_Ping_ex(&mClient, &mqttObj.ping);
if (rc != MQTT_CODE_SUCCESS) {
break;
}
PRINTF("MQTT Keep-Alive Ping");
}
else if (rc != MQTT_CODE_SUCCESS) {
break;
}
}
exit:
if (rc != MQTT_CODE_SUCCESS) {
PRINTF("MQTT Error %d: %s", rc, MqttClient_ReturnCodeToString(rc));
}
return rc;
}
#endif /* HAVE_SOCKET */
#ifndef NO_MAIN_DRIVER
int main(int argc, char** argv)
{
int rc = -1;
(void)argc;
(void)argv;
#ifdef HAVE_SOCKET
rc = mqttsimple_test();
#endif
return (rc == 0) ? 0 : EXIT_FAILURE;
}
#endif /* !NO_MAIN_DRIVER */