embOS/IP WebSocket protocol
The embOS/IP WebSocket IoT (Internet of Things) protocol is an optional extension to embOS/IP. The WebSocket add-on can be used with embOS/IP or with a different TCP/IP stack that uses a socket API.
The WebSocket protocol is an encapsulation transport protocol for use on top of the TCP/IP protocol to simplify communication through firewalls. It is simple, lightweight and open. Its characteristics make it ideal for use in many situations, including constrained environments such as for communication in Machine to Machine (M2M) and Internet of Things (IoT) contexts.
- Converts a synchronous HTTP connection into an asynchronous data connection.
- Can traverse firewalls using the well known HTTP port.
- Can be used with or without web server.
- Seamless integration with the embOS/IP web server add-on.
- Low memory footprint.
- Optimized API for embedded systems.
- Independent of the TCP/IP stack: any stack with sockets can be used.
- Project for executable on PC for Microsoft Visual Studio included.
The embOS/IP WebSocket add-on requires a TCP/IP stack. It is optimized for embOS/IP, but any RFC-compliant TCP/IP stack that has a socket API can be used. The shipment includes a Win32 simulation, which uses the standard Winsock API as well as an implementation which uses the socket API of embOS/IP.
The WebSocket protocol is an IoT (Internet of Things) protocol that allows to upgrade a regular synchronous HTTP connection into an asynchronous bidirectional data tunnel. It allows to establish a regular HTTP connection with a webserver, checking if both sides, client and server, understand the WebSocket protocol depending on the answer sent back by the webserver.
The non intrusive test for checking the WebSocket protocol being supported is compatible with the HTTP/1.1 standard and will return a page with an error code in worst case if the protocol is not supported by the webserver. The webserver service for serving regular pages remains fully operational while coexisting with the WebSocket protocol.
The WebSocket protocol is using the TCP/IP protocol for data, using the HTTP/1.1 protocol only to initially establish a WebSocket connection.
An advantage of the WebSocket protocol is that it establishes a connection through the HTTP protocol. This allows an easier handling in terms of firewalls as typically the standard port TCP 80 is allowed for outgoing connections, allowing WebSocket clients to work without problems. For a WebSocket server, offering webpages and WebSocket tunneled protocols only a single port is required to be opened in the firewall or allowed to be forwarded by a router from the Internet to your local network.
The WebSocket protocol combines the following properties of TCP and UDP:
- Lost data is retransmitted.
- Data is received in the correct order.
- The connection status can be checked by using TCP KEEPALIVES.
- Data can be sent in small frames (while being part of a larger message).
- The length of the frame is known (the complete message length is not).
- Control frames (UDP: other data) can be sent between data frames.
Principle of operation
Application data sent using the WebSocket protocol can be described as UDP like framing of data within a TCP data stream. While this might sound complex at first, the protocol itself is not.
The main purpose of the WebSocket protocol is to allow a device with limited resources sending one big block of data in chunks (called a frames in the WebSocket protocol) whenever data is ready/generated instead of collecting the data before sending. At the same time control commands should be able to be processed anytime between frames. At the same time the TCP protocol ensures that data is not lost and frames will be sent and received in the correct order.
The WebSocket protocol achieves these goals by sending all data in frames that have a header upfront. The encapsulation of the protocol is shown below:
|[RFC 6455]||Direct external download: The WebSocket Protocol|
The following table shows the contents of the embOS/IP WebSocket package root directory:
|.\Application\||Contains the example application to run the a WebSocket printf() server with embOS/IP.|
|.\IP\||Contains the WebSocket sources: IP_WEBSOCKET.c and IP_WEBSOCKET.h|
|.\Windows\||Contains the source, the project files and an executable to run the embOS/IP WebSocket add-on on a Microsoft Windows host.|
The ROM usage depends on the compiler options, the compiler version and the used CPU. The memory requirements of the WebSocket add-on presented in the tables below have been measured on a Cortex-M4 system. Details about the further configuration can be found in the sections of the specific example.
The following resource usage has been measured on a Cortex-M4 system using SEGGER Embedded Studio V3.10e, size optimization.
|embOS/IP WebSocket protocol||Approximately 1.7 kBytes|
|embOS/IP WebSocket context||Approximately 30 bytes|