diff --git a/Sming/SmingCore/Network/Http/HttpConnection.h b/Sming/SmingCore/Network/Http/HttpConnection.h index f76915c888..8282e0aecc 100644 --- a/Sming/SmingCore/Network/Http/HttpConnection.h +++ b/Sming/SmingCore/Network/Http/HttpConnection.h @@ -17,6 +17,12 @@ #include "../Services/DateTime/DateTime.h" #include "Data/ObjectQueue.h" +/** @defgroup HTTP client connection + * @brief Provides http client connection + * @ingroup http + * @{ + */ + typedef ObjectQueue RequestQueue; class HttpConnection : public HttpConnectionBase @@ -140,4 +146,5 @@ class HttpConnection : public HttpConnectionBase HttpResponse response; }; +/** @} */ #endif /* _SMING_CORE_NETWORK_HTTP_CONNECTION_H_ */ diff --git a/Sming/SmingCore/Network/Http/HttpConnectionBase.h b/Sming/SmingCore/Network/Http/HttpConnectionBase.h index d3fd525ae8..6f97950f50 100644 --- a/Sming/SmingCore/Network/Http/HttpConnectionBase.h +++ b/Sming/SmingCore/Network/Http/HttpConnectionBase.h @@ -21,6 +21,12 @@ #include "HttpResponse.h" #include "HttpRequest.h" +/** @defgroup HTTP base connection + * @brief Provides http base used for client and server connections + * @ingroup http + * @{ + */ + class HttpConnectionBase : public TcpClient { public: @@ -36,6 +42,10 @@ class HttpConnectionBase : public TcpClient protected: void resetHeaders(); + /** + * @brief Initializes the http parser for a specific type of HTTP message + * @param http_parser_type + */ virtual void init(http_parser_type type); // HTTP parser methods @@ -139,4 +149,5 @@ class HttpConnectionBase : public TcpClient HttpConnectionState state = eHCS_Ready; }; +/** @} */ #endif /* _SMING_CORE_NETWORK_HTTP_HTTPCONNECTIONBASE_H_ */ diff --git a/Sming/SmingCore/Network/Http/HttpServerConnection.h b/Sming/SmingCore/Network/Http/HttpServerConnection.h index ac3e4edcab..343642b340 100644 --- a/Sming/SmingCore/Network/Http/HttpServerConnection.h +++ b/Sming/SmingCore/Network/Http/HttpServerConnection.h @@ -19,6 +19,12 @@ #include +/** @defgroup HTTP server connection + * @brief Provides http server connection + * @ingroup http + * @{ + */ + #ifndef HTTP_SERVER_EXPOSE_NAME #define HTTP_SERVER_EXPOSE_NAME 1 #endif @@ -46,7 +52,7 @@ class HttpServerConnection : public HttpConnectionBase using TcpClient::send; - void setUpgdareCallback(HttpServerProtocolUpgradeCallback callback) + void setUpgradeCallback(HttpServerProtocolUpgradeCallback callback) { upgradeCallback = callback; } @@ -120,4 +126,5 @@ class HttpServerConnection : public HttpConnectionBase HttpBodyParserDelegate bodyParser = 0; }; +/** @} */ #endif /* _SMING_CORE_NETWORK_HTTP_HTTPSERVERCONNECTION_H_ */ diff --git a/Sming/SmingCore/Network/Http/Websocket/WebsocketConnection.h b/Sming/SmingCore/Network/Http/Websocket/WebsocketConnection.h index d7a1fbc09c..4819066d75 100644 --- a/Sming/SmingCore/Network/Http/Websocket/WebsocketConnection.h +++ b/Sming/SmingCore/Network/Http/Websocket/WebsocketConnection.h @@ -15,6 +15,12 @@ extern "C" { #include "../ws_parser/ws_parser.h" } +/** @defgroup Websocket connection + * @brief Provides websocket connection (server and client) + * @ingroup websocket http + * @{ + */ + #define WEBSOCKET_VERSION 13 // 1.3 #define WSSTR_CONNECTION _F("connection") @@ -46,24 +52,72 @@ typedef struct { class WebsocketConnection { public: + /** + * @brief Constructs a websocket connection on top of http client or server connection + * @param HttpConnectionBase* connection the transport connection + * @param bool isClientConnection true when the passed connection is an http client conneciton + */ WebsocketConnection(HttpConnectionBase* connection, bool isClientConnection = true); virtual ~WebsocketConnection(); /** * @brief Binds websocket connection to an http server connection + * @param HttpRequest& request + * @param HttpResponse& response + * @return true on success, false otherwise */ bool bind(HttpRequest& request, HttpResponse& response); + /** + * @brief Sends a websocket message + * @param const char* message + * @param int length + * @param ws_frame_type_t type + */ virtual void send(const char* message, int length, ws_frame_type_t type = WS_FRAME_TEXT); + + /** + * @brief Broadcasts a message to all active websocket connections + * @param const char* message + * @param int length + * @param ws_frame_type_t type + */ static void broadcast(const char* message, int length, ws_frame_type_t type = WS_FRAME_TEXT); + /** + * @brief Sends a string websocket message + * @param const String& message + */ void sendString(const String& message); - void sendBinary(const uint8_t* data, int size); + + /** + * @brief Sends a binary websocket message + * @param const uint8_t* data + * @param int length + */ + void sendBinary(const uint8_t* data, int length); + + /** + * @brief Closes a websocket connection (without closing the underlying http connection + */ void close(); + + /** + * @brief Resets a websocket connection + */ void reset(); + /** + * @brief Attaches a user data to a websocket connection + * @param void* userData + */ void setUserData(void* userData); + + /** + * @brief Retrieves user data attached + * @return void* + */ void* getUserData(); // @deprecated @@ -72,27 +126,64 @@ class WebsocketConnection WebsocketList& getActiveWebSockets(); // @end deprecated + /** + * @brief Sets the callback handler to be called after successful websocket connection + * @param WebsocketDelegate handler + */ void setConnectionHandler(WebsocketDelegate handler); + + /** + * @brief Sets the callback handler to be called after a websocket message is received + * @param WebsocketMessageDelegate handler + */ void setMessageHandler(WebsocketMessageDelegate handler); + + /** + * @brief Sets the callback handler to be called after a binary websocket message is received + * @param WebsocketBinaryDelegate handler + */ void setBinaryHandler(WebsocketBinaryDelegate handler); + + /** + * @brief Sets the callback handler to be called before closing a websocket connection + * @param WebsocketDelegate handler + */ void setDisconnectionHandler(WebsocketDelegate handler); + /** + * @brief Should be called after a websocket connection is established to activate + * the websocket parser and allow sending of websocket data + */ void activate(); + + /** + * @brief Call this method when the websocket connection was (re)activated. + * @return bool true on success + */ bool onConnected(); + /** + * @brief Gets the underlying HTTP connection + * @return HttpConnectionBase + */ HttpConnectionBase* getConnection() { return connection; } + /** + * @brief Sets the underlying (transport ) HTTP connection + * @param HttpConnectionBase* connection the transport connection + * @param bool isClientConnection true when the passed connection is an http client conneciton + */ void setConnection(HttpConnectionBase* connection, bool isClientConnection = true) { this->connection = connection; this->isClientConnection = isClientConnection; } - /** @brief Get websocket client mode - * @retval Return websocket client mode + /** @brief Gets the state of the websocket connection + * @retval WsConnectionState */ WsConnectionState getState() { @@ -137,4 +228,5 @@ class WebsocketConnection bool activated = false; }; +/** @} */ #endif /* SMINGCORE_NETWORK_WEBSOCKETCONNECTION_H_ */ diff --git a/Sming/SmingCore/Network/Http/Websocket/WebsocketResource.cpp b/Sming/SmingCore/Network/Http/Websocket/WebsocketResource.cpp index 683fa5ac51..c89cf494f5 100644 --- a/Sming/SmingCore/Network/Http/Websocket/WebsocketResource.cpp +++ b/Sming/SmingCore/Network/Http/Websocket/WebsocketResource.cpp @@ -36,7 +36,7 @@ int WebsocketResource::checkHeaders(HttpServerConnection& connection, HttpReques connection.setTimeOut(USHRT_MAX); //Disable disconnection on connection idle (no rx/tx) - connection.setUpgdareCallback(std::bind(&WebsocketConnection::onConnected, socket)); + connection.setUpgradeCallback(std::bind(&WebsocketConnection::onConnected, socket)); // TODO: Re-Enable Command Executor...