ThingWorx C SDK
twWebsocket.h
Go to the documentation of this file.
1 /***************************************
2  * Copyright 2017, PTC, Inc.
3  ***************************************/
4 
12 #ifndef TW_WEBSOCKET_H
13 #define TW_WEBSOCKET_H
14 
15 #include "twOSPort.h"
16 #include "twDefinitions.h"
17 #include "twBaseTypes.h"
18 #include "twPasswds.h"
19 
20 #include "zlib.h"
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 /* Web socket frame op codes */
27 #define FRAME_OPCODE_CONTINUATION 0x00
28 #define FRAME_OPCODE_TEXT 0x01
29 #define FRAME_OPCODE_BINARY 0x02
30 #define FRAME_OPCODE_CONNECTION_CLOSE 0x08
31 #define FRAME_OPCODE_PING 0x09
32 #define FRAME_OPCODE_PONG 0x0A
33 
34 /* Internal state machine states */
35 #define READ_HEADER 0
36 #define READ_CONTROL_FRAME 1
37 #define READ_TEXT_FRAME 2
38 #define READ_BINARY_FRAME 3
39 
40 /* WebSocket compression */
41 #define ZLIB_WINDOW_BITS_RAW_DEFLATE -15
42 #define ZLIB_DEFAULT_MEM_LEVEL 8
43 
44 /*
45 HTTP Parser declarations for the
46 joyent http-parser library
47 */
48 struct http_parser;
49 struct http_parser_settings;
50 
51 /*
52 Forward declarations of the struct and call back functions
53 used by the http-parser library
54 */
55 struct twWs;
56 typedef int (*ws_cb) (struct twWs * ws);
57 typedef int (*ws_data_cb) (struct twWs * ws, const char *at, size_t length);
58 typedef int (*ws_binary_data_cb) (struct twWs * ws, const char *at, size_t length, char bCompressed);
59 
60 /*
61 Helper macros
62 */
63 #define WS_TLS_CONN(a) (twTlsClient *)a->connection
64 
70  ,NORMAL_CLOSE = 1000
82 };
83 
87 typedef struct twWs{
89  uint32_t messageChunkSize;
90  int32_t bytesNeeded;
91  char read_state;
92  char previous_read_state;
93  uint16_t frameSize;
94  char * frameBuffer;
95  char * frameBufferPtr;
96  unsigned char ws_header[64];
97  unsigned char * headerPtr;
99  char * host;
100  uint16_t port;
101  twPasswdCallbackFunction api_key_callback;
114  char * gatewayName;
116  char * gatewayType;
118  unsigned char * security_key;
119  uint32_t sessionId;
120  char * resource;
124  signed char connect_state;
125  signed char isConnected;
127  ws_binary_data_cb on_ws_binaryMessage;
128  ws_data_cb on_ws_textMessage;
129  ws_data_cb on_ws_ping;
130  ws_data_cb on_ws_pong;
131  ws_data_cb on_ws_close;
132  z_stream defstream;
143 } twWs;
144 
176 int twWs_Create(char * host, uint16_t port, char * resource, twPasswdCallbackFunction app_key_function, char * gatewayName,
177  uint32_t messageChunkSize, uint16_t frameSize, twWs ** entity);
178 
191 int twWs_Delete(twWs * ws);
192 
203 int twWs_Connect(twWs * ws, uint32_t timeout);
204 
215 int twWs_Disconnect(twWs * ws, enum close_status code, char * reason);
216 
225 char twWs_IsConnected(twWs * ws);
226 
237 int twWs_RegisterConnectCallback(twWs * ws, ws_cb cb);
238 
249 int twWs_RegisterCloseCallback(twWs * ws, ws_data_cb cb);
250 
261 int twWs_RegisterBinaryMessageCallback(twWs * ws, ws_binary_data_cb cb);
262 
273 int twWs_RegisterTextMessageCallback(twWs * ws, ws_data_cb cb);
274 
285 int twWs_RegisterPingCallback(twWs * ws, ws_data_cb cb);
286 
297 int twWs_RegisterPongCallback(twWs * ws, ws_data_cb cb);
298 
314 int twWs_Receive(twWs * ws, uint32_t timeout);
315 
331 int twWs_SendMessage(twWs * ws, char * buf, uint32_t length, char isText);
332 
346 int twWs_SendPing(twWs * ws, char * msg);
347 
361 int twWs_SendPong(twWs * ws, char * msg);
362 
366 int twWs_SendDataFrame (twWs * ws, char * msg, uint16_t length, char isContinuation, char isFinal, char isText);
367 
368 #ifdef __cplusplus
369 }
370 #endif
371 
372 #endif
373 
374 
375 
char bSupportsPermessageDeflate
Definition: twWebsocket.h:141
struct twWs twWs
Websocket entity structure definition.
Definition: twWebsocket.h:76
Definition: twWebsocket.h:81
int32_t bytesNeeded
Definition: twWebsocket.h:90
char bCompressedMsg
Definition: twWebsocket.h:138
int twWs_RegisterPongCallback(twWs *ws, ws_data_cb cb)
Registers a function to be called when the websocket receives a Pong message.
Definition: twWebsocket.c:632
int twWs_Create(char *host, uint16_t port, char *resource, twPasswdCallbackFunction app_key_function, char *gatewayName, uint32_t messageChunkSize, uint16_t frameSize, twWs **entity)
Creates a new websocket struct and the underlying dependent components.
Definition: twWebsocket.c:149
char twWs_IsConnected(twWs *ws)
Gets the connection status of a websocket entity structure.
Definition: twWebsocket.c:514
ws_binary_data_cb on_ws_binaryMessage
Definition: twWebsocket.h:127
char bInflateInitialized
Definition: twWebsocket.h:136
#define TW_MUTEX
For Linux builds a TW_MUTEX is a pthread_mutex_t.
Definition: twLinux-openssl.h:81
char bDisableCompression
Definition: twWebsocket.h:142
uint32_t sessionId
Definition: twWebsocket.h:119
signed char isConnected
Definition: twWebsocket.h:125
uint16_t frameSize
Definition: twWebsocket.h:93
char * gatewayType
Definition: twWebsocket.h:116
int twWs_SendPing(twWs *ws, char *msg)
Send a Ping message over the websocket.
Definition: twWebsocket.c:1072
Dynamically allocated byte array. Automatically expands its length as needed.
Definition: twBaseTypes.h:35
int twWs_Disconnect(twWs *ws, enum close_status code, char *reason)
Disconnect a websocket connection from the server.
Definition: twWebsocket.c:519
Wrappers for OS-specific functionality.
int twWs_SendDataFrame(twWs *ws, char *msg, uint16_t length, char isContinuation, char isFinal, char isText)
Definition: twWebsocket.c:1142
unsigned char * security_key
Definition: twWebsocket.h:118
Definition: twWebsocket.h:77
ws_cb on_ws_connected
Definition: twWebsocket.h:126
char * frameBufferPtr
Definition: twWebsocket.h:95
int twWs_RegisterPingCallback(twWs *ws, ws_data_cb cb)
Registers a function to be called when the websocket receives a Ping message.
Definition: twWebsocket.c:623
struct twTlsClient * connection
Definition: twWebsocket.h:88
twStream * multiframeRecvStream
Definition: twWebsocket.h:98
TW_MUTEX decompressionMutex
Definition: twWebsocket.h:137
Definition: twWebsocket.h:78
Definition: twWebsocket.h:74
int twWs_RegisterCloseCallback(twWs *ws, ws_data_cb cb)
Registers a function to be called when the websocket is closed by the server.
Definition: twWebsocket.c:596
char bDeflateInitialized
Definition: twWebsocket.h:133
uint32_t messageChunkSize
Definition: twWebsocket.h:89
uint16_t port
Definition: twWebsocket.h:100
signed char connect_state
Definition: twWebsocket.h:124
Definition: twWebsocket.h:69
int twWs_SendMessage(twWs *ws, char *buf, uint32_t length, char isText)
Send a message over the websocket.
Definition: twWebsocket.c:985
char read_state
Definition: twWebsocket.h:91
Common definitions for C SDK.
Definition: twWebsocket.h:70
int twWs_RegisterConnectCallback(twWs *ws, ws_cb cb)
Registers a function to be called when the websocket is successfully connected.
Definition: twWebsocket.c:587
ws_data_cb on_ws_close
Definition: twWebsocket.h:131
int twWs_RegisterBinaryMessageCallback(twWs *ws, ws_binary_data_cb cb)
Registers a function to be called when the websocket receives a complete binary message.
Definition: twWebsocket.c:605
Definition: inftree9.h:24
Definition: twWebsocket.h:71
ThingWorx BaseType definitions and functions.
Definition: twWebsocket.h:79
int twWs_Receive(twWs *ws, uint32_t timeout)
Check the websocket for data and drive the state machine of the websocket.
Definition: twWebsocket.c:642
Definition: twWebsocket.h:80
close_status
Websocket close reasoning enumeration.
Definition: twWebsocket.h:68
ws_data_cb on_ws_pong
Definition: twWebsocket.h:130
int twWs_SendPong(twWs *ws, char *msg)
Send a Pong message over the websocket.
Definition: twWebsocket.c:1082
char * resource
Definition: twWebsocket.h:120
int twWs_RegisterTextMessageCallback(twWs *ws, ws_data_cb cb)
Registers a function to be called when the websocket receives a complete text message.
Definition: twWebsocket.c:614
ws_data_cb on_ws_textMessage
Definition: twWebsocket.h:128
TW_MUTEX compressionMutex
Definition: twWebsocket.h:134
TW_MUTEX sendMessageMutex
Definition: twWebsocket.h:121
TW_MUTEX recvMutex
Definition: twWebsocket.h:123
unsigned char ws_header[64]
Definition: twWebsocket.h:96
TW_MUTEX sendFrameMutex
Definition: twWebsocket.h:122
Definition: twWebsocket.h:75
TLS client structure definition.
Definition: twTls.h:34
Websocket entity structure definition.
Definition: twWebsocket.h:87
Definition: twWebsocket.h:72
int twWs_Delete(twWs *ws)
Frees all memory associated with a twWs structure and all its owned substructures.
Definition: twWebsocket.c:238
char * gatewayName
Definition: twWebsocket.h:114
ws_data_cb on_ws_ping
Definition: twWebsocket.h:129
char * host
Definition: twWebsocket.h:99
int twWs_Connect(twWs *ws, uint32_t timeout)
Establishes a websocket connection to the server.
Definition: twWebsocket.c:267
Definition: zlib.h:86
z_stream inflstream
Definition: twWebsocket.h:135
Definition: twWebsocket.h:73
char * frameBuffer
Definition: twWebsocket.h:94
unsigned char * headerPtr
Definition: twWebsocket.h:97