-
Notifications
You must be signed in to change notification settings - Fork 69
/
mqtt_client_transport.c
719 lines (641 loc) · 19.7 KB
/
mqtt_client_transport.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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
/**
* @file mqtt_client_transport.c
* @brief Transport protocol abstraction layer
*
* @section License
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* Copyright (C) 2010-2023 Oryx Embedded SARL. All rights reserved.
*
* This file is part of CycloneTCP Open.
*
* 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
* of the License, 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @author Oryx Embedded SARL (www.oryx-embedded.com)
* @version 2.3.0
**/
//Switch to the appropriate trace level
#define TRACE_LEVEL MQTT_TRACE_LEVEL
//Dependencies
#include "core/net.h"
#include "core/tcp_misc.h"
#include "mqtt/mqtt_client.h"
#include "mqtt/mqtt_client_packet.h"
#include "mqtt/mqtt_client_transport.h"
#include "mqtt/mqtt_client_misc.h"
#include "debug.h"
//Check TCP/IP stack configuration
#if (MQTT_CLIENT_SUPPORT == ENABLED)
#if (MQTT_CLIENT_WS_SUPPORT == ENABLED && MQTT_CLIENT_TLS_SUPPORT == ENABLED)
/**
* @brief TLS initialization callback
* @param[in] webSocket Handle to a WebSocket
* @param[in] tlsContext Pointer to the TLS context
* @return Error code
**/
error_t mqttClientWebSocketTlsInitCallback(WebSocket *webSocket,
TlsContext *tlsContext)
{
MqttClientContext *context;
//Point to the MQTT client context
context = webSocket->tlsInitParam;
//Invoke user-defined callback
return context->callbacks.tlsInitCallback(context, tlsContext);
}
#endif
/**
* @brief Open network connection
* @param[in] context Pointer to the MQTT client context
* @return Error code
**/
error_t mqttClientOpenConnection(MqttClientContext *context)
{
error_t error;
//TCP transport protocol?
if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TCP)
{
//Open a TCP socket
context->socket = socketOpen(SOCKET_TYPE_STREAM, SOCKET_IP_PROTO_TCP);
//Valid socket handle?
if(context->socket != NULL)
{
//Associate the socket with the relevant interface
error = socketBindToInterface(context->socket, context->interface);
}
else
{
//Report an error
error = ERROR_OPEN_FAILED;
}
}
#if (MQTT_CLIENT_TLS_SUPPORT == ENABLED)
//TLS transport protocol?
else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TLS)
{
//Open a TCP socket
context->socket = socketOpen(SOCKET_TYPE_STREAM, SOCKET_IP_PROTO_TCP);
//Valid socket handle?
if(context->socket != NULL)
{
//Associate the socket with the relevant interface
error = socketBindToInterface(context->socket, context->interface);
//Check status code
if(!error)
{
//Allocate TLS context
context->tlsContext = tlsInit();
//Valid TLS handle?
if(context->tlsContext != NULL)
{
//Select client operation mode
error = tlsSetConnectionEnd(context->tlsContext,
TLS_CONNECTION_END_CLIENT);
//Check status code
if(!error)
{
//Bind TLS to the relevant socket
error = tlsSetSocket(context->tlsContext, context->socket);
}
//Check status code
if(!error)
{
//Restore TLS session, if any
error = tlsRestoreSessionState(context->tlsContext,
&context->tlsSession);
}
//Check status code
if(!error)
{
//Invoke user-defined callback, if any
if(context->callbacks.tlsInitCallback != NULL)
{
//Perform TLS related initialization
error = context->callbacks.tlsInitCallback(context,
context->tlsContext);
}
}
}
else
{
//Report an error
error = ERROR_OPEN_FAILED;
}
}
}
else
{
//Report an error
error = ERROR_OPEN_FAILED;
}
}
#endif
#if (MQTT_CLIENT_WS_SUPPORT == ENABLED)
//WebSocket transport protocol?
else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WS)
{
//Open a WebSocket
context->webSocket = webSocketOpen();
//Valid WebSocket handle?
if(context->webSocket != NULL)
{
//Associate the WebSocket with the relevant interface
error = webSocketBindToInterface(context->webSocket,
context->interface);
}
else
{
//Report an error
error = ERROR_OPEN_FAILED;
}
}
#endif
#if (MQTT_CLIENT_WS_SUPPORT == ENABLED && MQTT_CLIENT_TLS_SUPPORT == ENABLED && \
WEB_SOCKET_TLS_SUPPORT == ENABLED)
//Secure WebSocket transport protocol?
else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WSS)
{
//Open a WebSocket
context->webSocket = webSocketOpen();
//Valid WebSocket handle?
if(context->webSocket != NULL)
{
//Associate the WebSocket with the relevant interface
error = webSocketBindToInterface(context->webSocket,
context->interface);
//Check status code
if(!error)
{
//Attach MQTT client context
context->webSocket->tlsInitParam = context;
//Register TLS initialization callback
error = webSocketRegisterTlsInitCallback(context->webSocket,
mqttClientWebSocketTlsInitCallback);
}
}
else
{
//Report an error
error = ERROR_OPEN_FAILED;
}
}
#endif
//Unknown transport protocol?
else
{
//Report an error
error = ERROR_INVALID_PROTOCOL;
}
//Return status code
return error;
}
/**
* @brief Establish network connection
* @param[in] context Pointer to the MQTT client context
* @param[in] serverIpAddr IP address of the MQTT server to connect to
* @param[in] serverPort TCP port number that will be used to establish the
* connection
* @return Error code
**/
error_t mqttClientEstablishConnection(MqttClientContext *context,
const IpAddr *serverIpAddr, uint16_t serverPort)
{
error_t error;
//TCP transport protocol?
if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TCP)
{
//Set timeout
error = socketSetTimeout(context->socket, context->settings.timeout);
//Check status code
if(!error)
{
//Connect to the MQTT server using TCP
error = socketConnect(context->socket, serverIpAddr, serverPort);
}
}
#if (MQTT_CLIENT_TLS_SUPPORT == ENABLED)
//TLS transport protocol?
else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TLS)
{
//Set timeout
error = socketSetTimeout(context->socket, context->settings.timeout);
//Check status code
if(!error)
{
//Connect to the MQTT server using TCP
error = socketConnect(context->socket, serverIpAddr, serverPort);
}
//Check status code
if(!error)
{
//Perform TLS handshake
error = tlsConnect(context->tlsContext);
}
//Successful connection?
if(!error)
{
//Save TLS session
error = tlsSaveSessionState(context->tlsContext, &context->tlsSession);
}
}
#endif
#if (MQTT_CLIENT_WS_SUPPORT == ENABLED)
//WebSocket transport protocol?
else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WS ||
context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WSS)
{
//Set timeout
error = webSocketSetTimeout(context->webSocket, context->settings.timeout);
//Check status code
if(!error)
{
//Set the hostname of the remote server
error = webSocketSetHost(context->webSocket, context->settings.host);
}
//Check status code
if(!error)
{
//The client MUST include "mqtt" in the list of WebSocket
//sub-protocols it offers
error = webSocketSetSubProtocol(context->webSocket, "mqtt");
}
//Check status code
if(!error)
{
//Connect to the MQTT server using WebSocket
error = webSocketConnect(context->webSocket, serverIpAddr,
serverPort, context->settings.uri);
}
}
#endif
//Unknown transport protocol?
else
{
//Report an error
error = ERROR_INVALID_PROTOCOL;
}
//Return status code
return error;
}
/**
* @brief Shutdown network connection
* @param[in] context Pointer to the MQTT client context
* @return Error code
**/
error_t mqttClientShutdownConnection(MqttClientContext *context)
{
error_t error;
//TCP transport protocol?
if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TCP)
{
//Set timeout
error = socketSetTimeout(context->socket, context->settings.timeout);
//Check status code
if(!error)
{
//Shutdown TCP connection
error = socketShutdown(context->socket, SOCKET_SD_BOTH);
}
}
#if (MQTT_CLIENT_TLS_SUPPORT == ENABLED)
//TLS transport protocol?
else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TLS)
{
//Set timeout
error = socketSetTimeout(context->socket, context->settings.timeout);
//Check status code
if(!error)
{
//Shutdown TLS session
error = tlsShutdown(context->tlsContext);
}
//Check status code
if(!error)
{
//Shutdown TCP connection
error = socketShutdown(context->socket, SOCKET_SD_BOTH);
}
}
#endif
#if (MQTT_CLIENT_WS_SUPPORT == ENABLED)
//WebSocket transport protocol?
else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WS ||
context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WSS)
{
//Set timeout
error = webSocketSetTimeout(context->webSocket, context->settings.timeout);
//Check status code
if(!error)
{
//Shutdown WebSocket connection
error = webSocketShutdown(context->webSocket);
}
}
#endif
//Unknown transport protocol?
else
{
//Report an error
error = ERROR_INVALID_PROTOCOL;
}
//Return status code
return error;
}
/**
* @brief Close network connection
* @param[in] context Pointer to the MQTT client context
**/
void mqttClientCloseConnection(MqttClientContext *context)
{
//TCP transport protocol?
if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TCP)
{
//Close TCP connection
if(context->socket != NULL)
{
socketClose(context->socket);
context->socket = NULL;
}
}
#if (MQTT_CLIENT_TLS_SUPPORT == ENABLED)
//TLS transport protocol?
else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TLS)
{
//Release TLS context
if(context->tlsContext != NULL)
{
tlsFree(context->tlsContext);
context->tlsContext = NULL;
}
//Close TCP connection
if(context->socket != NULL)
{
socketClose(context->socket);
context->socket = NULL;
}
}
#endif
#if (MQTT_CLIENT_WS_SUPPORT == ENABLED)
//WebSocket transport protocol?
else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WS ||
context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WSS)
{
//Close WebSocket connection
if(context->webSocket != NULL)
{
webSocketClose(context->webSocket);
context->webSocket = NULL;
}
}
#endif
}
/**
* @brief Send data using the relevant transport protocol
* @param[in] context Pointer to the MQTT client context
* @param[in] data Pointer to a buffer containing the data to be transmitted
* @param[in] length Number of bytes to be transmitted
* @param[out] written Actual number of bytes written (optional parameter)
* @param[in] flags Set of flags that influences the behavior of this function
* @return Error code
**/
error_t mqttClientSendData(MqttClientContext *context,
const void *data, size_t length, size_t *written, uint_t flags)
{
error_t error;
//TCP transport protocol?
if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TCP)
{
//Set timeout
error = socketSetTimeout(context->socket, context->settings.timeout);
//Check status code
if(!error)
{
//Transmit data
error = socketSend(context->socket, data, length, written, flags);
}
}
#if (MQTT_CLIENT_TLS_SUPPORT == ENABLED)
//TLS transport protocol?
else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TLS)
{
//Set timeout
error = socketSetTimeout(context->socket, context->settings.timeout);
//Check status code
if(!error)
{
//Transmit data
error = tlsWrite(context->tlsContext, data, length, written, flags);
}
}
#endif
#if (MQTT_CLIENT_WS_SUPPORT == ENABLED)
//WebSocket transport protocol?
else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WS ||
context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WSS)
{
//Set timeout
error = webSocketSetTimeout(context->webSocket, context->settings.timeout);
//Check status code
if(!error)
{
//MQTT control packets must be sent in WebSocket binary data frames
error = webSocketSend(context->webSocket, data, length,
WS_FRAME_TYPE_BINARY, written);
}
}
#endif
//Unknown transport protocol?
else
{
//Report an error
error = ERROR_INVALID_PROTOCOL;
}
//Return status code
return error;
}
/**
* @brief Receive data using the relevant transport protocol
* @param[in] context Pointer to the MQTT client context
* @param[out] data Buffer into which received data will be placed
* @param[in] size Maximum number of bytes that can be received
* @param[out] received Number of bytes that have been received
* @param[in] flags Set of flags that influences the behavior of this function
* @return Error code
**/
error_t mqttClientReceiveData(MqttClientContext *context,
void *data, size_t size, size_t *received, uint_t flags)
{
error_t error;
//No data has been read yet
*received = 0;
//TCP transport protocol?
if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TCP)
{
//Set timeout
error = socketSetTimeout(context->socket, context->settings.timeout);
//Check status code
if(!error)
{
//Receive data
error = socketReceive(context->socket, data, size, received, flags);
}
}
#if (MQTT_CLIENT_TLS_SUPPORT == ENABLED)
//TLS transport protocol?
else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TLS)
{
//Set timeout
error = socketSetTimeout(context->socket, context->settings.timeout);
//Check status code
if(!error)
{
//Receive data
error = tlsRead(context->tlsContext, data, size, received, flags);
}
}
#endif
#if (MQTT_CLIENT_WS_SUPPORT == ENABLED)
//WebSocket transport protocol?
else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WS ||
context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WSS)
{
WebSocketFrameType type;
//Set timeout
error = webSocketSetTimeout(context->webSocket, context->settings.timeout);
//Check status code
if(!error)
{
//Receive data
error = webSocketReceive(context->webSocket, data, size, &type, received);
}
//Check status code
if(!error)
{
//MQTT control packets must be sent in WebSocket binary data frames. If
//any other type of data frame is received the recipient must close the
//network connection
if(type != WS_FRAME_TYPE_BINARY && type != WS_FRAME_TYPE_CONTINUATION)
error = ERROR_INVALID_TYPE;
}
}
#endif
//Unknown transport protocol?
else
{
//Report an error
error = ERROR_INVALID_PROTOCOL;
}
//Return status code
return error;
}
/**
* @brief Wait for incoming data
* @param[in] context Pointer to the MQTT client context
* @param[in] timeout Maximum time to wait before returning
* @return Error code
**/
error_t mqttClientWaitForData(MqttClientContext *context, systime_t timeout)
{
uint_t event;
//TCP transport protocol?
if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TCP)
{
//Get exclusive access
osAcquireMutex(&netMutex);
//Wait for some data to be available for reading
event = tcpWaitForEvents(context->socket, SOCKET_EVENT_RX_READY, timeout);
//Release exclusive access
osReleaseMutex(&netMutex);
}
#if (MQTT_CLIENT_TLS_SUPPORT == ENABLED)
//TLS transport protocol?
else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TLS)
{
//Sanity check
if(context->tlsContext == NULL)
return ERROR_FAILURE;
//Check whether some data is pending in the receive buffer
if(context->tlsContext->rxBufferLen > 0)
{
//No need to poll the underlying socket for incoming traffic...
event = SOCKET_EVENT_RX_READY;
}
else
{
//Get exclusive access
osAcquireMutex(&netMutex);
//Wait for some data to be available for reading
event = tcpWaitForEvents(context->socket, SOCKET_EVENT_RX_READY, timeout);
//Release exclusive access
osReleaseMutex(&netMutex);
}
}
#endif
#if (MQTT_CLIENT_WS_SUPPORT == ENABLED)
//WebSocket transport protocol?
else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WS)
{
//Sanity check
if(context->webSocket == NULL)
return ERROR_FAILURE;
//Get exclusive access
osAcquireMutex(&netMutex);
//Wait for some data to be available for reading
event = tcpWaitForEvents(context->webSocket->socket, SOCKET_EVENT_RX_READY, timeout);
//Release exclusive access
osReleaseMutex(&netMutex);
}
#endif
#if (MQTT_CLIENT_WS_SUPPORT == ENABLED && WEB_SOCKET_TLS_SUPPORT)
//Secure WebSocket transport protocol?
else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WSS)
{
//Sanity check
if(context->webSocket == NULL || context->webSocket->tlsContext == NULL)
return ERROR_FAILURE;
//Check whether some data is pending in the receive buffer
if(context->webSocket->tlsContext->rxBufferLen > 0)
{
//No need to poll the underlying socket for incoming traffic...
event = SOCKET_EVENT_RX_READY;
}
else
{
//Get exclusive access
osAcquireMutex(&netMutex);
//Wait for some data to be available for reading
event = tcpWaitForEvents(context->webSocket->socket, SOCKET_EVENT_RX_READY, timeout);
//Release exclusive access
osReleaseMutex(&netMutex);
}
}
#endif
//Unknown transport protocol?
else
{
//Report an error
return ERROR_INVALID_PROTOCOL;
}
//Check whether some data is available for reading
if(event == SOCKET_EVENT_RX_READY)
{
return NO_ERROR;
}
else
{
return ERROR_TIMEOUT;
}
}
#endif