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-2024 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.4.0
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  //Successful connection?
280  if(!error)
281  {
282  //Save TLS session
283  error = tlsSaveSessionState(context->tlsContext, &context->tlsSession);
284  }
285  }
286 #endif
287 #if (MQTT_CLIENT_WS_SUPPORT == ENABLED)
288  //WebSocket transport protocol?
289  else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WS ||
290  context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WSS)
291  {
292  //Set timeout
293  error = webSocketSetTimeout(context->webSocket, context->settings.timeout);
294 
295  //Check status code
296  if(!error)
297  {
298  //Set the hostname of the remote server
299  error = webSocketSetHost(context->webSocket, context->settings.host);
300  }
301 
302  //Check status code
303  if(!error)
304  {
305  //The client MUST include "mqtt" in the list of WebSocket
306  //sub-protocols it offers
307  error = webSocketSetSubProtocol(context->webSocket, "mqtt");
308  }
309 
310  //Check status code
311  if(!error)
312  {
313  //Connect to the MQTT server using WebSocket
314  error = webSocketConnect(context->webSocket, serverIpAddr,
315  serverPort, context->settings.uri);
316  }
317  }
318 #endif
319  //Unknown transport protocol?
320  else
321  {
322  //Report an error
323  error = ERROR_INVALID_PROTOCOL;
324  }
325 
326  //Return status code
327  return error;
328 }
329 
330 
331 /**
332  * @brief Shutdown network connection
333  * @param[in] context Pointer to the MQTT client context
334  * @return Error code
335  **/
336 
338 {
339  error_t error;
340 
341  //TCP transport protocol?
342  if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TCP)
343  {
344  //Set timeout
345  error = socketSetTimeout(context->socket, context->settings.timeout);
346 
347  //Check status code
348  if(!error)
349  {
350  //Shutdown TCP connection
351  error = socketShutdown(context->socket, SOCKET_SD_BOTH);
352  }
353  }
354 #if (MQTT_CLIENT_TLS_SUPPORT == ENABLED)
355  //TLS transport protocol?
356  else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TLS)
357  {
358  //Set timeout
359  error = socketSetTimeout(context->socket, context->settings.timeout);
360 
361  //Check status code
362  if(!error)
363  {
364  //Shutdown TLS session
365  error = tlsShutdown(context->tlsContext);
366  }
367 
368  //Check status code
369  if(!error)
370  {
371  //Shutdown TCP connection
372  error = socketShutdown(context->socket, SOCKET_SD_BOTH);
373  }
374  }
375 #endif
376 #if (MQTT_CLIENT_WS_SUPPORT == ENABLED)
377  //WebSocket transport protocol?
378  else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WS ||
379  context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WSS)
380  {
381  //Set timeout
382  error = webSocketSetTimeout(context->webSocket, context->settings.timeout);
383 
384  //Check status code
385  if(!error)
386  {
387  //Shutdown WebSocket connection
388  error = webSocketShutdown(context->webSocket);
389  }
390  }
391 #endif
392  //Unknown transport protocol?
393  else
394  {
395  //Report an error
396  error = ERROR_INVALID_PROTOCOL;
397  }
398 
399  //Return status code
400  return error;
401 }
402 
403 
404 /**
405  * @brief Close network connection
406  * @param[in] context Pointer to the MQTT client context
407  **/
408 
410 {
411  //TCP transport protocol?
412  if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TCP)
413  {
414  //Close TCP connection
415  if(context->socket != NULL)
416  {
417  socketClose(context->socket);
418  context->socket = NULL;
419  }
420  }
421 #if (MQTT_CLIENT_TLS_SUPPORT == ENABLED)
422  //TLS transport protocol?
423  else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TLS)
424  {
425  //Release TLS context
426  if(context->tlsContext != NULL)
427  {
428  tlsFree(context->tlsContext);
429  context->tlsContext = NULL;
430  }
431 
432  //Close TCP connection
433  if(context->socket != NULL)
434  {
435  socketClose(context->socket);
436  context->socket = NULL;
437  }
438  }
439 #endif
440 #if (MQTT_CLIENT_WS_SUPPORT == ENABLED)
441  //WebSocket transport protocol?
442  else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WS ||
443  context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WSS)
444  {
445  //Close WebSocket connection
446  if(context->webSocket != NULL)
447  {
448  webSocketClose(context->webSocket);
449  context->webSocket = NULL;
450  }
451  }
452 #endif
453 }
454 
455 
456 /**
457  * @brief Send data using the relevant transport protocol
458  * @param[in] context Pointer to the MQTT client context
459  * @param[in] data Pointer to a buffer containing the data to be transmitted
460  * @param[in] length Number of bytes to be transmitted
461  * @param[out] written Actual number of bytes written (optional parameter)
462  * @param[in] flags Set of flags that influences the behavior of this function
463  * @return Error code
464  **/
465 
467  const void *data, size_t length, size_t *written, uint_t flags)
468 {
469  error_t error;
470 
471  //TCP transport protocol?
472  if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TCP)
473  {
474  //Set timeout
475  error = socketSetTimeout(context->socket, context->settings.timeout);
476 
477  //Check status code
478  if(!error)
479  {
480  //Transmit data
481  error = socketSend(context->socket, data, length, written, flags);
482  }
483  }
484 #if (MQTT_CLIENT_TLS_SUPPORT == ENABLED)
485  //TLS transport protocol?
486  else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TLS)
487  {
488  //Set timeout
489  error = socketSetTimeout(context->socket, context->settings.timeout);
490 
491  //Check status code
492  if(!error)
493  {
494  //Transmit data
495  error = tlsWrite(context->tlsContext, data, length, written, flags);
496  }
497  }
498 #endif
499 #if (MQTT_CLIENT_WS_SUPPORT == ENABLED)
500  //WebSocket transport protocol?
501  else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WS ||
502  context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WSS)
503  {
504  //Set timeout
505  error = webSocketSetTimeout(context->webSocket, context->settings.timeout);
506 
507  //Check status code
508  if(!error)
509  {
510  //MQTT control packets must be sent in WebSocket binary data frames
511  error = webSocketSend(context->webSocket, data, length,
512  WS_FRAME_TYPE_BINARY, written);
513  }
514  }
515 #endif
516  //Unknown transport protocol?
517  else
518  {
519  //Report an error
520  error = ERROR_INVALID_PROTOCOL;
521  }
522 
523  //Return status code
524  return error;
525 }
526 
527 
528 /**
529  * @brief Receive data using the relevant transport protocol
530  * @param[in] context Pointer to the MQTT client context
531  * @param[out] data Buffer into which received data will be placed
532  * @param[in] size Maximum number of bytes that can be received
533  * @param[out] received Number of bytes that have been received
534  * @param[in] flags Set of flags that influences the behavior of this function
535  * @return Error code
536  **/
537 
539  void *data, size_t size, size_t *received, uint_t flags)
540 {
541  error_t error;
542 
543  //No data has been read yet
544  *received = 0;
545 
546  //TCP transport protocol?
547  if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TCP)
548  {
549  //Set timeout
550  error = socketSetTimeout(context->socket, context->settings.timeout);
551 
552  //Check status code
553  if(!error)
554  {
555  //Receive data
556  error = socketReceive(context->socket, data, size, received, flags);
557  }
558  }
559 #if (MQTT_CLIENT_TLS_SUPPORT == ENABLED)
560  //TLS transport protocol?
561  else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TLS)
562  {
563  //Set timeout
564  error = socketSetTimeout(context->socket, context->settings.timeout);
565 
566  //Check status code
567  if(!error)
568  {
569  //Receive data
570  error = tlsRead(context->tlsContext, data, size, received, flags);
571  }
572  }
573 #endif
574 #if (MQTT_CLIENT_WS_SUPPORT == ENABLED)
575  //WebSocket transport protocol?
576  else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WS ||
577  context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WSS)
578  {
580 
581  //Set timeout
582  error = webSocketSetTimeout(context->webSocket, context->settings.timeout);
583 
584  //Check status code
585  if(!error)
586  {
587  //Receive data
588  error = webSocketReceive(context->webSocket, data, size, &type, received);
589  }
590 
591  //Check status code
592  if(!error)
593  {
594  //MQTT control packets must be sent in WebSocket binary data frames. If
595  //any other type of data frame is received the recipient must close the
596  //network connection
598  error = ERROR_INVALID_TYPE;
599  }
600  }
601 #endif
602  //Unknown transport protocol?
603  else
604  {
605  //Report an error
606  error = ERROR_INVALID_PROTOCOL;
607  }
608 
609  //Return status code
610  return error;
611 }
612 
613 
614 /**
615  * @brief Wait for incoming data
616  * @param[in] context Pointer to the MQTT client context
617  * @param[in] timeout Maximum time to wait before returning
618  * @return Error code
619  **/
620 
622 {
623  uint_t event;
624 
625  //TCP transport protocol?
626  if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TCP)
627  {
628  //Get exclusive access
630  //Wait for some data to be available for reading
631  event = tcpWaitForEvents(context->socket, SOCKET_EVENT_RX_READY, timeout);
632  //Release exclusive access
634  }
635 #if (MQTT_CLIENT_TLS_SUPPORT == ENABLED)
636  //TLS transport protocol?
637  else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_TLS)
638  {
639  //Sanity check
640  if(context->tlsContext == NULL)
641  return ERROR_FAILURE;
642 
643  //Check whether some data is pending in the receive buffer
644  if(context->tlsContext->rxBufferLen > 0)
645  {
646  //No need to poll the underlying socket for incoming traffic...
647  event = SOCKET_EVENT_RX_READY;
648  }
649  else
650  {
651  //Get exclusive access
653  //Wait for some data to be available for reading
654  event = tcpWaitForEvents(context->socket, SOCKET_EVENT_RX_READY, timeout);
655  //Release exclusive access
657  }
658  }
659 #endif
660 #if (MQTT_CLIENT_WS_SUPPORT == ENABLED)
661  //WebSocket transport protocol?
662  else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WS)
663  {
664  //Sanity check
665  if(context->webSocket == NULL)
666  return ERROR_FAILURE;
667 
668  //Get exclusive access
670  //Wait for some data to be available for reading
671  event = tcpWaitForEvents(context->webSocket->socket, SOCKET_EVENT_RX_READY, timeout);
672  //Release exclusive access
674  }
675 #endif
676 #if (MQTT_CLIENT_WS_SUPPORT == ENABLED && WEB_SOCKET_TLS_SUPPORT)
677  //Secure WebSocket transport protocol?
678  else if(context->settings.transportProtocol == MQTT_TRANSPORT_PROTOCOL_WSS)
679  {
680  //Sanity check
681  if(context->webSocket == NULL || context->webSocket->tlsContext == NULL)
682  return ERROR_FAILURE;
683 
684  //Check whether some data is pending in the receive buffer
685  if(context->webSocket->tlsContext->rxBufferLen > 0)
686  {
687  //No need to poll the underlying socket for incoming traffic...
688  event = SOCKET_EVENT_RX_READY;
689  }
690  else
691  {
692  //Get exclusive access
694  //Wait for some data to be available for reading
695  event = tcpWaitForEvents(context->webSocket->socket, SOCKET_EVENT_RX_READY, timeout);
696  //Release exclusive access
698  }
699  }
700 #endif
701  //Unknown transport protocol?
702  else
703  {
704  //Report an error
705  return ERROR_INVALID_PROTOCOL;
706  }
707 
708  //Check whether some data is available for reading
709  if(event == SOCKET_EVENT_RX_READY)
710  {
711  return NO_ERROR;
712  }
713  else
714  {
715  return ERROR_TIMEOUT;
716  }
717 }
718 
719 #endif
uint8_t type
Definition: coap_common.h:176
unsigned int uint_t
Definition: compiler_port.h:50
Debugging facilities.
error_t
Error codes.
Definition: error.h:43
@ ERROR_INVALID_TYPE
Definition: error.h:115
@ ERROR_TIMEOUT
Definition: error.h:95
@ ERROR_INVALID_PROTOCOL
Definition: error.h:101
@ ERROR_OPEN_FAILED
Definition: error.h:75
@ NO_ERROR
Success.
Definition: error.h:44
@ ERROR_FAILURE
Generic error code.
Definition: error.h:45
uint8_t data[]
Definition: ethernet.h:222
MQTT client.
#define MqttClientContext
Definition: mqtt_client.h:147
Helper functions for MQTT client.
MQTT packet parsing and formatting.
error_t mqttClientOpenConnection(MqttClientContext *context)
Open network connection.
void mqttClientCloseConnection(MqttClientContext *context)
Close 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.
error_t mqttClientShutdownConnection(MqttClientContext *context)
Shutdown network connection.
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 mqttClientWaitForData(MqttClientContext *context, systime_t timeout)
Wait for incoming data.
error_t mqttClientWebSocketTlsInitCallback(WebSocket *webSocket, TlsContext *tlsContext)
TLS initialization callback.
error_t mqttClientEstablishConnection(MqttClientContext *context, const IpAddr *serverIpAddr, uint16_t serverPort)
Establish network connection.
Transport protocol abstraction layer.
@ MQTT_TRANSPORT_PROTOCOL_WSS
WebSocket protocol.
Definition: mqtt_common.h:78
@ MQTT_TRANSPORT_PROTOCOL_TLS
TCP protocol.
Definition: mqtt_common.h:76
@ MQTT_TRANSPORT_PROTOCOL_WS
TLS protocol.
Definition: mqtt_common.h:77
@ MQTT_TRANSPORT_PROTOCOL_TCP
Definition: mqtt_common.h:75
TCP/IP stack core.
#define netMutex
Definition: net_legacy.h:195
#define socketBindToInterface
Definition: net_legacy.h:193
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
uint32_t systime_t
System time.
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:1152
Socket * socketOpen(uint_t type, uint_t protocol)
Create a socket (UDP or TCP)
Definition: socket.c:125
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:946
error_t socketSetTimeout(Socket *socket, systime_t timeout)
Set timeout value for blocking operations.
Definition: socket.c:148
void socketClose(Socket *socket)
Close an existing socket.
Definition: socket.c:1517
error_t socketShutdown(Socket *socket, uint_t how)
Disable reception, transmission, or both.
Definition: socket.c:1480
error_t socketConnect(Socket *socket, const IpAddr *remoteIpAddr, uint16_t remotePort)
Establish a connection to a specified socket.
Definition: socket.c:811
@ SOCKET_SD_BOTH
Definition: socket.h:151
@ SOCKET_IP_PROTO_TCP
Definition: socket.h:100
@ SOCKET_TYPE_STREAM
Definition: socket.h:85
@ SOCKET_EVENT_RX_READY
Definition: socket.h:169
IP network address.
Definition: ip.h:79
uint8_t length
Definition: tcp.h:368
uint8_t flags
Definition: tcp.h:351
uint_t tcpWaitForEvents(Socket *socket, uint_t eventMask, systime_t timeout)
Wait for a particular TCP event.
Definition: tcp_misc.c:2200
Helper functions for TCP.
error_t tlsRestoreSessionState(TlsContext *context, const TlsSessionState *session)
Restore TLS session.
Definition: tls.c:2690
error_t tlsConnect(TlsContext *context)
Initiate the TLS handshake.
Definition: tls.c:1758
error_t tlsSaveSessionState(const TlsContext *context, TlsSessionState *session)
Save TLS session.
Definition: tls.c:2621
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:1849
TlsContext * tlsInit(void)
TLS context initialization.
Definition: tls.c:65
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:1984
error_t tlsSetConnectionEnd(TlsContext *context, TlsConnectionEnd entity)
Set operation mode (client or server)
Definition: tls.c:344
error_t tlsShutdown(TlsContext *context)
Gracefully close TLS session.
Definition: tls.c:2302
void tlsFree(TlsContext *context)
Release TLS context.
Definition: tls.c:2464
#define TlsContext
Definition: tls.h:36
@ TLS_CONNECTION_END_CLIENT
Definition: tls.h:953
#define tlsSetSocket(context, socket)
Definition: tls.h:912
error_t webSocketRegisterTlsInitCallback(WebSocket *webSocket, WebSocketTlsInitCallback callback)
Register TLS initialization callback function.
Definition: web_socket.c:233
error_t webSocketConnect(WebSocket *webSocket, const IpAddr *serverIpAddr, uint16_t serverPort, const char_t *uri)
Establish a WebSocket connection.
Definition: web_socket.c:422
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:970
WebSocket * webSocketOpen(void)
Create a WebSocket.
Definition: web_socket.c:95
error_t webSocketSetTimeout(WebSocket *webSocket, systime_t timeout)
Set timeout value for blocking operations.
Definition: web_socket.c:257
error_t webSocketSetSubProtocol(WebSocket *webSocket, const char_t *subProtocol)
Set the sub-protocol header field.
Definition: web_socket.c:332
error_t webSocketShutdown(WebSocket *webSocket)
Gracefully close a WebSocket connection.
Definition: web_socket.c:1470
void webSocketClose(WebSocket *webSocket)
Close a WebSocket connection.
Definition: web_socket.c:1590
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:1155
error_t webSocketSetHost(WebSocket *webSocket, const char_t *host)
Set the domain name of the server (for virtual hosting)
Definition: web_socket.c:282
error_t webSocketBindToInterface(WebSocket *webSocket, NetInterface *interface)
Bind the WebSocket to a particular network interface.
Definition: web_socket.c:398
WebSocketFrameType
WebSocket frame types.
Definition: web_socket.h:263
@ WS_FRAME_TYPE_BINARY
Definition: web_socket.h:266
@ WS_FRAME_TYPE_CONTINUATION
Definition: web_socket.h:264
#define WebSocket
Definition: web_socket.h:177