mqtt_client_transport.c
Go to the documentation of this file.
1 /**
2  * @file mqtt_client_transport.c
3  * @brief Transport protocol abstraction layer
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2026 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.6.4
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL MQTT_TRACE_LEVEL
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "core/tcp_misc.h"
37 #include "mqtt/mqtt_client.h"
40 #include "mqtt/mqtt_client_misc.h"
41 #include "debug.h"
42 
43 //Check TCP/IP stack configuration
44 #if (MQTT_CLIENT_SUPPORT == ENABLED)
45 
46 #if (MQTT_CLIENT_WS_SUPPORT == ENABLED && MQTT_CLIENT_TLS_SUPPORT == ENABLED)
47 
48 /**
49  * @brief TLS initialization callback
50  * @param[in] webSocket Handle to a WebSocket
51  * @param[in] tlsContext Pointer to the TLS context
52  * @return Error code
53  **/
54 
56  TlsContext *tlsContext)
57 {
58  MqttClientContext *context;
59 
60  //Point to the MQTT client context
61  context = webSocket->tlsInitParam;
62 
63  //Invoke user-defined callback
64  return context->callbacks.tlsInitCallback(context, tlsContext);
65 }
66 
67 #endif
68 
69 
70 /**
71  * @brief Open network connection
72  * @param[in] context Pointer to the MQTT client context
73  * @return Error code
74  **/
75 
77 {
78  error_t error;
79 
80  //TCP transport protocol?
81  if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TCP)
82  {
83  //Open a TCP socket
85 
86  //Valid socket handle?
87  if(context->socket != NULL)
88  {
89  //Associate the socket with the relevant interface
90  error = socketBindToInterface(context->socket, context->interface);
91  }
92  else
93  {
94  //Report an error
95  error = ERROR_OPEN_FAILED;
96  }
97  }
98 #if (MQTT_CLIENT_TLS_SUPPORT == ENABLED)
99  //TLS transport protocol?
100  else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TLS)
101  {
102  //Open a TCP socket
104 
105  //Valid socket handle?
106  if(context->socket != NULL)
107  {
108  //Associate the socket with the relevant interface
109  error = socketBindToInterface(context->socket, context->interface);
110 
111  //Check status code
112  if(!error)
113  {
114  //Allocate TLS context
115  context->tlsContext = tlsInit();
116 
117  //Valid TLS handle?
118  if(context->tlsContext != NULL)
119  {
120  //Select client operation mode
121  error = tlsSetConnectionEnd(context->tlsContext,
123 
124  //Check status code
125  if(!error)
126  {
127  //Bind TLS to the relevant socket
128  error = tlsSetSocket(context->tlsContext, context->socket);
129  }
130 
131  //Check status code
132  if(!error)
133  {
134  //Restore TLS session, if any
135  error = tlsRestoreSessionState(context->tlsContext,
136  &context->tlsSession);
137  }
138 
139  //Check status code
140  if(!error)
141  {
142  //Invoke user-defined callback, if any
143  if(context->callbacks.tlsInitCallback != NULL)
144  {
145  //Perform TLS related initialization
146  error = context->callbacks.tlsInitCallback(context,
147  context->tlsContext);
148  }
149  }
150  }
151  else
152  {
153  //Report an error
154  error = ERROR_OPEN_FAILED;
155  }
156  }
157  }
158  else
159  {
160  //Report an error
161  error = ERROR_OPEN_FAILED;
162  }
163  }
164 #endif
165 #if (MQTT_CLIENT_WS_SUPPORT == ENABLED)
166  //WebSocket transport protocol?
167  else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WS)
168  {
169  //Open a WebSocket
170  context->webSocket = webSocketOpen();
171 
172  //Valid WebSocket handle?
173  if(context->webSocket != NULL)
174  {
175  //Associate the WebSocket with the relevant interface
176  error = webSocketBindToInterface(context->webSocket,
177  context->interface);
178  }
179  else
180  {
181  //Report an error
182  error = ERROR_OPEN_FAILED;
183  }
184  }
185 #endif
186 #if (MQTT_CLIENT_WS_SUPPORT == ENABLED && MQTT_CLIENT_TLS_SUPPORT == ENABLED && \
187  WEB_SOCKET_TLS_SUPPORT == ENABLED)
188  //Secure WebSocket transport protocol?
189  else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WSS)
190  {
191  //Open a WebSocket
192  context->webSocket = webSocketOpen();
193 
194  //Valid WebSocket handle?
195  if(context->webSocket != NULL)
196  {
197  //Associate the WebSocket with the relevant interface
198  error = webSocketBindToInterface(context->webSocket,
199  context->interface);
200 
201  //Check status code
202  if(!error)
203  {
204  //Attach MQTT client context
205  context->webSocket->tlsInitParam = context;
206 
207  //Register TLS initialization callback
208  error = webSocketRegisterTlsInitCallback(context->webSocket,
210  }
211  }
212  else
213  {
214  //Report an error
215  error = ERROR_OPEN_FAILED;
216  }
217  }
218 #endif
219  //Unknown transport protocol?
220  else
221  {
222  //Report an error
223  error = ERROR_INVALID_PROTOCOL;
224  }
225 
226  //Return status code
227  return error;
228 }
229 
230 
231 /**
232  * @brief Establish network connection
233  * @param[in] context Pointer to the MQTT client context
234  * @param[in] serverIpAddr IP address of the MQTT server to connect to
235  * @param[in] serverPort TCP port number that will be used to establish the
236  * connection
237  * @return Error code
238  **/
239 
241  const IpAddr *serverIpAddr, uint16_t serverPort)
242 {
243  error_t error;
244 
245  //TCP transport protocol?
246  if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TCP)
247  {
248  //Set timeout
249  error = socketSetTimeout(context->socket, context->settings.timeout);
250 
251  //Check status code
252  if(!error)
253  {
254  //Connect to the MQTT server using TCP
255  error = socketConnect(context->socket, serverIpAddr, serverPort);
256  }
257  }
258 #if (MQTT_CLIENT_TLS_SUPPORT == ENABLED)
259  //TLS transport protocol?
260  else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TLS)
261  {
262  //Set timeout
263  error = socketSetTimeout(context->socket, context->settings.timeout);
264 
265  //Check status code
266  if(!error)
267  {
268  //Connect to the MQTT server using TCP
269  error = socketConnect(context->socket, serverIpAddr, serverPort);
270  }
271 
272  //Check status code
273  if(!error)
274  {
275  //Perform TLS handshake
276  error = tlsConnect(context->tlsContext);
277  }
278  }
279 #endif
280 #if (MQTT_CLIENT_WS_SUPPORT == ENABLED)
281  //WebSocket transport protocol?
282  else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WS ||
283  context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WSS)
284  {
285  //Set timeout
286  error = webSocketSetTimeout(context->webSocket, context->settings.timeout);
287 
288  //Check status code
289  if(!error)
290  {
291  //Set the hostname of the remote server
292  error = webSocketSetHost(context->webSocket, context->settings.host);
293  }
294 
295  //Check status code
296  if(!error)
297  {
298  //The client MUST include "mqtt" in the list of WebSocket
299  //sub-protocols it offers
300  error = webSocketSetSubProtocol(context->webSocket, "mqtt");
301  }
302 
303  //Check status code
304  if(!error)
305  {
306  //Connect to the MQTT server using WebSocket
307  error = webSocketConnect(context->webSocket, serverIpAddr,
308  serverPort, context->settings.uri);
309  }
310  }
311 #endif
312  //Unknown transport protocol?
313  else
314  {
315  //Report an error
316  error = ERROR_INVALID_PROTOCOL;
317  }
318 
319  //Return status code
320  return error;
321 }
322 
323 
324 /**
325  * @brief Shutdown network connection
326  * @param[in] context Pointer to the MQTT client context
327  * @return Error code
328  **/
329 
331 {
332  error_t error;
333 
334  //TCP transport protocol?
335  if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TCP)
336  {
337  //Set timeout
338  error = socketSetTimeout(context->socket, context->settings.timeout);
339 
340  //Check status code
341  if(!error)
342  {
343  //Shutdown TCP connection
344  error = socketShutdown(context->socket, SOCKET_SD_BOTH);
345  }
346  }
347 #if (MQTT_CLIENT_TLS_SUPPORT == ENABLED)
348  //TLS transport protocol?
349  else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TLS)
350  {
351  //Set timeout
352  error = socketSetTimeout(context->socket, context->settings.timeout);
353 
354  //Check status code
355  if(!error)
356  {
357  //Shutdown TLS session
358  error = tlsShutdown(context->tlsContext);
359  }
360 
361  //Check status code
362  if(!error)
363  {
364  //Shutdown TCP connection
365  error = socketShutdown(context->socket, SOCKET_SD_BOTH);
366  }
367  }
368 #endif
369 #if (MQTT_CLIENT_WS_SUPPORT == ENABLED)
370  //WebSocket transport protocol?
371  else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WS ||
372  context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WSS)
373  {
374  //Set timeout
375  error = webSocketSetTimeout(context->webSocket, context->settings.timeout);
376 
377  //Check status code
378  if(!error)
379  {
380  //Shutdown WebSocket connection
381  error = webSocketShutdown(context->webSocket);
382  }
383  }
384 #endif
385  //Unknown transport protocol?
386  else
387  {
388  //Report an error
389  error = ERROR_INVALID_PROTOCOL;
390  }
391 
392  //Return status code
393  return error;
394 }
395 
396 
397 /**
398  * @brief Close network connection
399  * @param[in] context Pointer to the MQTT client context
400  **/
401 
403 {
404  //TCP transport protocol?
405  if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TCP)
406  {
407  //Close TCP connection
408  if(context->socket != NULL)
409  {
410  socketClose(context->socket);
411  context->socket = NULL;
412  }
413  }
414 #if (MQTT_CLIENT_TLS_SUPPORT == ENABLED)
415  //TLS transport protocol?
416  else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TLS)
417  {
418  //Release TLS context
419  if(context->tlsContext != NULL)
420  {
421  tlsFree(context->tlsContext);
422  context->tlsContext = NULL;
423  }
424 
425  //Close TCP connection
426  if(context->socket != NULL)
427  {
428  socketClose(context->socket);
429  context->socket = NULL;
430  }
431  }
432 #endif
433 #if (MQTT_CLIENT_WS_SUPPORT == ENABLED)
434  //WebSocket transport protocol?
435  else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WS ||
436  context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WSS)
437  {
438  //Close WebSocket connection
439  if(context->webSocket != NULL)
440  {
441  webSocketClose(context->webSocket);
442  context->webSocket = NULL;
443  }
444  }
445 #endif
446 }
447 
448 
449 /**
450  * @brief Send data using the relevant transport protocol
451  * @param[in] context Pointer to the MQTT client context
452  * @param[in] data Pointer to a buffer containing the data to be transmitted
453  * @param[in] length Number of bytes to be transmitted
454  * @param[out] written Actual number of bytes written (optional parameter)
455  * @param[in] flags Set of flags that influences the behavior of this function
456  * @return Error code
457  **/
458 
460  const void *data, size_t length, size_t *written, uint_t flags)
461 {
462  error_t error;
463 
464  //TCP transport protocol?
465  if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TCP)
466  {
467  //Set timeout
468  error = socketSetTimeout(context->socket, context->settings.timeout);
469 
470  //Check status code
471  if(!error)
472  {
473  //Transmit data
474  error = socketSend(context->socket, data, length, written, flags);
475  }
476  }
477 #if (MQTT_CLIENT_TLS_SUPPORT == ENABLED)
478  //TLS transport protocol?
479  else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TLS)
480  {
481  //Set timeout
482  error = socketSetTimeout(context->socket, context->settings.timeout);
483 
484  //Check status code
485  if(!error)
486  {
487  //Transmit data
488  error = tlsWrite(context->tlsContext, data, length, written, flags);
489  }
490  }
491 #endif
492 #if (MQTT_CLIENT_WS_SUPPORT == ENABLED)
493  //WebSocket transport protocol?
494  else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WS ||
495  context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WSS)
496  {
497  //Set timeout
498  error = webSocketSetTimeout(context->webSocket, context->settings.timeout);
499 
500  //Check status code
501  if(!error)
502  {
503  //MQTT control packets must be sent in WebSocket binary data frames
504  error = webSocketSend(context->webSocket, data, length,
505  WS_FRAME_TYPE_BINARY, written);
506  }
507  }
508 #endif
509  //Unknown transport protocol?
510  else
511  {
512  //Report an error
513  error = ERROR_INVALID_PROTOCOL;
514  }
515 
516  //Return status code
517  return error;
518 }
519 
520 
521 /**
522  * @brief Receive data using the relevant transport protocol
523  * @param[in] context Pointer to the MQTT client context
524  * @param[out] data Buffer into which received data will be placed
525  * @param[in] size Maximum number of bytes that can be received
526  * @param[out] received Number of bytes that have been received
527  * @param[in] flags Set of flags that influences the behavior of this function
528  * @return Error code
529  **/
530 
532  void *data, size_t size, size_t *received, uint_t flags)
533 {
534  error_t error;
535 
536  //No data has been read yet
537  *received = 0;
538 
539  //TCP transport protocol?
540  if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TCP)
541  {
542  //Set timeout
543  error = socketSetTimeout(context->socket, context->settings.timeout);
544 
545  //Check status code
546  if(!error)
547  {
548  //Receive data
549  error = socketReceive(context->socket, data, size, received, flags);
550  }
551  }
552 #if (MQTT_CLIENT_TLS_SUPPORT == ENABLED)
553  //TLS transport protocol?
554  else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TLS)
555  {
556  //Set timeout
557  error = socketSetTimeout(context->socket, context->settings.timeout);
558 
559  //Check status code
560  if(!error)
561  {
562  //Receive data
563  error = tlsRead(context->tlsContext, data, size, received, flags);
564  }
565  }
566 #endif
567 #if (MQTT_CLIENT_WS_SUPPORT == ENABLED)
568  //WebSocket transport protocol?
569  else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WS ||
570  context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WSS)
571  {
573 
574  //Set timeout
575  error = webSocketSetTimeout(context->webSocket, context->settings.timeout);
576 
577  //Check status code
578  if(!error)
579  {
580  //Receive data
581  error = webSocketReceive(context->webSocket, data, size, &type, received);
582  }
583 
584  //Check status code
585  if(!error)
586  {
587  //MQTT control packets must be sent in WebSocket binary data frames. If
588  //any other type of data frame is received the recipient must close the
589  //network connection
591  error = ERROR_INVALID_TYPE;
592  }
593  }
594 #endif
595  //Unknown transport protocol?
596  else
597  {
598  //Report an error
599  error = ERROR_INVALID_PROTOCOL;
600  }
601 
602  //Return status code
603  return error;
604 }
605 
606 
607 /**
608  * @brief Wait for incoming data
609  * @param[in] context Pointer to the MQTT client context
610  * @param[in] timeout Maximum time to wait before returning
611  * @return Error code
612  **/
613 
615 {
616  uint_t event;
617 
618  //TCP transport protocol?
619  if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TCP)
620  {
621  //Get exclusive access
622  netLock(context->socket->netContext);
623  //Wait for some data to be available for reading
624  event = tcpWaitForEvents(context->socket, SOCKET_EVENT_RX_READY, timeout);
625  //Release exclusive access
626  netUnlock(context->socket->netContext);
627  }
628 #if (MQTT_CLIENT_TLS_SUPPORT == ENABLED)
629  //TLS transport protocol?
630  else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TLS)
631  {
632  //Sanity check
633  if(context->tlsContext == NULL)
634  return ERROR_FAILURE;
635 
636  //Check whether some data is pending in the receive buffer
637  if(tlsIsRxReady(context->tlsContext))
638  {
639  //No need to poll the underlying socket for incoming traffic...
640  event = SOCKET_EVENT_RX_READY;
641  }
642  else
643  {
644  //Get exclusive access
645  netLock(context->socket->netContext);
646  //Wait for some data to be available for reading
647  event = tcpWaitForEvents(context->socket, SOCKET_EVENT_RX_READY, timeout);
648  //Release exclusive access
649  netUnlock(context->socket->netContext);
650  }
651  }
652 #endif
653 #if (MQTT_CLIENT_WS_SUPPORT == ENABLED)
654  //WebSocket transport protocol?
655  else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WS)
656  {
657  //Sanity check
658  if(context->webSocket == NULL)
659  return ERROR_FAILURE;
660 
661  //Get exclusive access
662  netLock(context->webSocket->socket->netContext);
663  //Wait for some data to be available for reading
664  event = tcpWaitForEvents(context->webSocket->socket, SOCKET_EVENT_RX_READY, timeout);
665  //Release exclusive access
666  netUnlock(context->webSocket->socket->netContext);
667  }
668 #endif
669 #if (MQTT_CLIENT_WS_SUPPORT == ENABLED && WEB_SOCKET_TLS_SUPPORT)
670  //Secure WebSocket transport protocol?
671  else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WSS)
672  {
673  //Sanity check
674  if(context->webSocket == NULL || context->webSocket->tlsContext == NULL)
675  return ERROR_FAILURE;
676 
677  //Check whether some data is pending in the receive buffer
678  if(tlsIsRxReady(context->webSocket->tlsContext))
679  {
680  //No need to poll the underlying socket for incoming traffic...
681  event = SOCKET_EVENT_RX_READY;
682  }
683  else
684  {
685  //Get exclusive access
686  netLock(context->webSocket->socket->netContext);
687  //Wait for some data to be available for reading
688  event = tcpWaitForEvents(context->webSocket->socket, SOCKET_EVENT_RX_READY, timeout);
689  //Release exclusive access
690  netUnlock(context->webSocket->socket->netContext);
691  }
692  }
693 #endif
694  //Unknown transport protocol?
695  else
696  {
697  //Report an error
698  return ERROR_INVALID_PROTOCOL;
699  }
700 
701  //Check whether some data is available for reading
702  if(event == SOCKET_EVENT_RX_READY)
703  {
704  return NO_ERROR;
705  }
706  else
707  {
708  return ERROR_TIMEOUT;
709  }
710 }
711 
712 
713 /**
714  * @brief Save TLS session
715  * @param[in] context Pointer to the MQTT client context
716  * @return Error code
717  **/
718 
720 {
721  error_t error;
722 
723  //Initialize status code
724  error = NO_ERROR;
725 
726 #if (MQTT_CLIENT_TLS_SUPPORT == ENABLED)
727  //TLS transport protocol?
728  if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TLS)
729  {
730  //Save TLS session
731  error = tlsSaveSessionState(context->tlsContext, &context->tlsSession);
732  }
733 #endif
734 
735  //Return status code
736  return error;
737 }
738 
739 #endif
error_t socketSend(Socket *socket, const void *data, size_t length, size_t *written, uint_t flags)
Send data to a connected socket.
Definition: socket.c:1514
TlsContext * tlsInit(void)
TLS context initialization.
Definition: tls.c:68
void mqttClientCloseConnection(MqttClientContext *context)
Close network connection.
void netUnlock(NetContext *context)
Release exclusive access to the core of the TCP/IP stack.
Definition: net.c:319
error_t mqttClientEstablishConnection(MqttClientContext *context, const IpAddr *serverIpAddr, uint16_t serverPort)
Establish network connection.
error_t tlsSetConnectionEnd(TlsContext *context, TlsConnectionEnd entity)
Set operation mode (client or server)
Definition: tls.c:371
error_t webSocketReceive(WebSocket *webSocket, void *data, size_t size, WebSocketFrameType *type, size_t *received)
Receive data from a WebSocket connection.
Definition: web_socket.c:1168
IP network address.
Definition: ip.h:90
error_t webSocketConnect(WebSocket *webSocket, const IpAddr *serverIpAddr, uint16_t serverPort, const char_t *uri)
Establish a WebSocket connection.
Definition: web_socket.c:426
uint8_t data[]
Definition: ethernet.h:224
void socketClose(Socket *socket)
Close an existing socket.
Definition: socket.c:2094
error_t mqttClientSendData(MqttClientContext *context, const void *data, size_t length, size_t *written, uint_t flags)
Send data using the relevant transport protocol.
error_t mqttClientOpenConnection(MqttClientContext *context)
Open network connection.
@ MQTT_TRANSPORT_PROTOCOL_WS
TLS protocol.
Definition: mqtt_common.h:77
uint8_t type
Definition: coap_common.h:176
error_t webSocketSetTimeout(WebSocket *webSocket, systime_t timeout)
Set timeout value for blocking operations.
Definition: web_socket.c:261
error_t webSocketSend(WebSocket *webSocket, const void *data, size_t length, WebSocketFrameType type, size_t *written)
Transmit data over the WebSocket connection.
Definition: web_socket.c:974
error_t mqttClientWaitForData(MqttClientContext *context, systime_t timeout)
Wait for incoming data.
@ SOCKET_TYPE_STREAM
Definition: socket.h:92
error_t tlsRestoreSessionState(TlsContext *context, const TlsSessionState *session)
Restore TLS session.
Definition: tls.c:3073
error_t mqttClientWebSocketTlsInitCallback(WebSocket *webSocket, TlsContext *tlsContext)
TLS initialization callback.
@ ERROR_OPEN_FAILED
Definition: error.h:75
error_t webSocketSetSubProtocol(WebSocket *webSocket, const char_t *subProtocol)
Set the sub-protocol header field.
Definition: web_socket.c:336
error_t tlsShutdown(TlsContext *context)
Gracefully close TLS session.
Definition: tls.c:2621
Helper functions for TCP.
error_t webSocketBindToInterface(WebSocket *webSocket, NetInterface *interface)
Bind the WebSocket to a particular network interface.
Definition: web_socket.c:402
#define tlsSetSocket(context, socket)
Definition: tls.h:1008
Helper functions for MQTT client.
#define TlsContext
Definition: tls.h:36
error_t
Error codes.
Definition: error.h:43
Transport protocol abstraction layer.
WebSocket * webSocketOpen(void)
Create a WebSocket.
Definition: web_socket.c:95
error_t socketReceive(Socket *socket, void *data, size_t size, size_t *received, uint_t flags)
Receive data from a connected socket.
Definition: socket.c:1724
WebSocketFrameType
WebSocket frame types.
Definition: web_socket.h:263
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
@ MQTT_TRANSPORT_PROTOCOL_TCP
Definition: mqtt_common.h:75
error_t socketConnect(Socket *socket, const IpAddr *remoteIpAddr, uint16_t remotePort)
Establish a connection to a specified socket.
Definition: socket.c:1377
MQTT packet parsing and formatting.
error_t socketShutdown(Socket *socket, uint_t how)
Disable reception, transmission, or both.
Definition: socket.c:2052
@ ERROR_INVALID_TYPE
Definition: error.h:115
error_t tlsSaveSessionState(const TlsContext *context, TlsSessionState *session)
Save TLS session.
Definition: tls.c:3004
uint8_t length
Definition: tcp.h:375
Socket * socketOpen(uint_t type, uint_t protocol)
Create a socket.
Definition: socket.c:126
error_t tlsRead(TlsContext *context, void *data, size_t size, size_t *received, uint_t flags)
Receive application data from a the remote host using TLS.
Definition: tls.c:2286
@ ERROR_INVALID_PROTOCOL
Definition: error.h:101
#define WebSocket
Definition: web_socket.h:177
#define socketBindToInterface
Definition: net_legacy.h:193
error_t mqttClientSaveSession(MqttClientContext *context)
Save TLS session.
error_t mqttClientShutdownConnection(MqttClientContext *context)
Shutdown network connection.
error_t mqttClientReceiveData(MqttClientContext *context, void *data, size_t size, size_t *received, uint_t flags)
Receive data using the relevant transport protocol.
uint32_t systime_t
System time.
bool_t tlsIsRxReady(TlsContext *context)
Check whether some data is available in the receive buffer.
Definition: tls.c:2576
@ ERROR_TIMEOUT
Definition: error.h:95
error_t webSocketRegisterTlsInitCallback(WebSocket *webSocket, WebSocketTlsInitCallback callback)
Register TLS initialization callback function.
Definition: web_socket.c:237
@ SOCKET_EVENT_RX_READY
Definition: socket.h:179
@ MQTT_TRANSPORT_PROTOCOL_TLS
TCP protocol.
Definition: mqtt_common.h:76
void webSocketClose(WebSocket *webSocket)
Close a WebSocket connection.
Definition: web_socket.c:1625
@ WS_FRAME_TYPE_CONTINUATION
Definition: web_socket.h:264
@ TLS_CONNECTION_END_CLIENT
Definition: tls.h:1050
error_t tlsWrite(TlsContext *context, const void *data, size_t length, size_t *written, uint_t flags)
Send application data to the remote host using TLS.
Definition: tls.c:2145
@ MQTT_TRANSPORT_PROTOCOL_WSS
WebSocket protocol.
Definition: mqtt_common.h:78
void netLock(NetContext *context)
Get exclusive access to the core of the TCP/IP stack.
Definition: net.c:307
void tlsFree(TlsContext *context)
Release TLS context.
Definition: tls.c:2816
error_t webSocketShutdown(WebSocket *webSocket)
Gracefully close a WebSocket connection.
Definition: web_socket.c:1503
#define MqttClientContext
Definition: mqtt_client.h:147
error_t webSocketSetHost(WebSocket *webSocket, const char_t *host)
Set the domain name of the server (for virtual hosting)
Definition: web_socket.c:286
uint8_t flags
Definition: tcp.h:358
unsigned int uint_t
Definition: compiler_port.h:57
TCP/IP stack core.
@ SOCKET_SD_BOTH
Definition: socket.h:161
@ SOCKET_IP_PROTO_TCP
Definition: socket.h:107
error_t socketSetTimeout(Socket *socket, systime_t timeout)
Set timeout value for blocking operations.
Definition: socket.c:169
error_t tlsConnect(TlsContext *context)
Initiate the TLS handshake.
Definition: tls.c:1805
@ WS_FRAME_TYPE_BINARY
Definition: web_socket.h:266
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
uint_t tcpWaitForEvents(Socket *socket, uint_t eventMask, systime_t timeout)
Wait for a particular TCP event.
Definition: tcp_misc.c:2319
MQTT client.