Webserver+3d print

Dependents:   Nucleo

Committer:
Sergunb
Date:
Sat Feb 04 18:15:49 2017 +0000
Revision:
0:8918a71cdbe9
nothing else

Who changed what in which revision?

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