ThingWorx C SDK
twMessages.h
1 /*
2  * Copyright 2017, PTC, Inc.
3  *
4  * Portable ThingWorx Binary Messaging layer
5  */
6 
7 #include "twOSPort.h"
8 #include "twDefinitions.h"
9 #include "twDefaultSettings.h"
10 #include "twErrors.h"
11 #include "twWebsocket.h"
12 #include "twBaseTypes.h"
13 #include "twInfoTable.h"
14 #include "twList.h"
15 
16 #ifndef TW_MESSAGES_H
17 #define TW_MESSAGES_H
18 
19 #define MSG_HEADER_SIZE 15
20 #define MULTIPART_MSG_HEADER_SIZE 6
21 
22 #define DEFLATE_SYNC_TRAILER_SIZE 4
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 /***************************************/
29 /* Entities below this line are */
30 /* typically not directly used */
31 /* by application developers */
32 /***************************************/
33 
37 typedef struct twMessage {
38  enum msgType type;
39  unsigned char version;
40  enum msgCodeEnum code;
41  uint32_t requestId;
42  uint32_t endpointId;
43  uint32_t sessionId;
44  char multipartMarker;
45  uint32_t length;
46  void * body;
47 } twMessage;
48 
49 int twCompressBytes (char * buf, uint32_t length, twStream* s, struct twWs * ws);
50 
51 /* Returns a unique request ID.*/
52 uint32_t twMessage_GetRequestId();
53 
54 twMessage * twMessage_Create(enum msgCodeEnum code, uint32_t reqId); /* Set Reqid to zero to autogenerate ID */
55 twMessage * twMessage_CreateRequestMsg(enum msgCodeEnum code);
56 twMessage * twMessage_CreateResponseMsg(enum msgCodeEnum code, uint32_t id, uint32_t sessionId, uint32_t endpointId);
57 twMessage * twMessage_CreateBindMsg(char * name, char isUnbind);
58 twMessage * twMessage_CreateAuthMsg(char * claimName, char * claimValue);
59 twMessage * twMessage_CreateFromStream(twStream * s);
60 
61 /* twMessage_ZeroCopy will return a pointer to the input message structure and it will set the input message
62 * pointer to NULL, effectively transferring memory control from the input pointer to the output pointer.
63 * This is very important in the twOfflineMsgStore singleton, because the singleton needs to ensure that a particular
64 * message is not deleted by the message handler before the message is written to disk
65 * (esspecially in the case of a multi-threaded environment) */
66 twMessage * twMessage_ZeroCopy(struct twMessage ** msg);
67 
68 void twMessage_Delete(void * input);
69 int twMessage_Send(struct twMessage ** msg, struct twWs * ws);
70 int twMessage_SetBody(struct twMessage * msg, void * body);
71 
75 typedef struct twHeader {
76  char * name;
77  char * value;
78 } twHeader;
79 
80 typedef struct twRequestBody {
81  enum entityTypeEnum entityType;
82  char * entityName;
83  enum characteristicEnum characteristicType;
84  char * characteristicName;
85  char numHeaders;
86  twList * headers;
87  twInfoTable * params;
88  uint32_t length;
90 
91 twRequestBody * twRequestBody_Create();
92 twRequestBody * twRequestBody_CreateFromStream(twStream * s);
93 int twRequestBody_Delete(struct twRequestBody * body);
94 int twRequestBody_SetParams(struct twRequestBody * body, twInfoTable * params);
95 int twRequestBody_SetEntity(struct twRequestBody * body, enum entityTypeEnum entityType, char * entityName);
96 int twRequestBody_SetCharacteristic(struct twRequestBody *body, enum characteristicEnum characteristicType,
97  char *characteristicName);
98 int twRequestBody_AddHeader(struct twRequestBody * body, char * name, char * value);
99 int twRequestBody_ToStream(struct twRequestBody * body, twStream * s);
100 
104 typedef struct twResponseBody {
105  char reasonMarker;
106  char * reason;
107  enum BaseType contentType;
108  twInfoTable * content;
109  uint32_t length;
111 
112 twResponseBody * twResponseBody_Create();
113 twResponseBody * twResponseBody_CreateFromStream(twStream * s);
114 int twResponseBody_Delete(struct twResponseBody * body);
115 int twResponseBody_SetContent(struct twResponseBody * body, twInfoTable * t);
116 int twResponseBody_SetReason(struct twResponseBody * body, char * reason);
117 int twResponseBody_ToStream(struct twResponseBody * body, twStream * s);
118 
122 typedef struct twAuthBody {
123  /* Limit to 1 claim */
124  char * name;
125  char * value;
126  uint32_t length;
127 } twAuthBody;
128 
129 twAuthBody * twAuthBody_Create();
130 twAuthBody * twAuthBody_CreateFromStream(twStream * s);
131 int twAuthBody_Delete(struct twAuthBody * body);
132 int twAuthBody_SetClaim(struct twAuthBody * body, char * name, char * value);
133 int twAuthBody_ToStream(struct twAuthBody * body, twStream * s);
134 
135 
139 typedef struct twBindBody {
140  char * gatewayName;
141  char * gatewayType;
142  uint16_t count;
143  struct twList * names;
144  uint32_t length;
145 } twBindBody;
146 
147 twBindBody * twBindBody_Create(char * name);
148 twBindBody * twBindBody_CreateFromStream(twStream * s);
149 int twBindBody_Delete(struct twBindBody * body);
150 int twBindBody_AddName(struct twBindBody * body, char * name);
151 int twBindBody_ToStream(struct twBindBody * body, twStream * s, char * gatewayName, char * gatewayType);
152 
156 typedef struct twMultipartBody {
157  uint16_t chunkId;
158  uint16_t chunkCount;
159  uint16_t chunkSize;
160  enum entityTypeEnum entityType;
161  char * entityName;
162  char * data;
163  uint16_t length;
165 
166 twMultipartBody * twMultipartBody_CreateFromStream(twStream * s, char isRequest);
167 void twMultipartBody_Delete(void * body);
168 
170  uint64_t expirationTime;
171  uint32_t id;
172  uint16_t chunksExpected;
173  uint16_t chunksReceived;
174  twMessage ** msgs; /* Array of message pointers */
176 
177 mulitpartMessageStoreEntry * mulitpartMessageStoreEntry_Create(twMessage * msg);
178 void mulitpartMessageStoreEntry_Delete(void * entry);
179 
183 typedef struct twMultipartMessageStore {
184  twList * multipartMessageList;
185  TW_MUTEX mtx;
187 
188 twMultipartMessageStore * twMultipartMessageStore_Instance();
189 void twMultipartMessageStore_Delete(void * store);
190 twMessage * twMultipartMessageStore_AddMessage(twMessage * msg);
191 void twMultipartMessageStore_RemoveStaleMessages();
192 
193 #ifdef __cplusplus
194 }
195 #endif
196 
197 #endif
msgType
Enumeration of HTTP message types.
Definition: twDefinitions.h:58
Definition: twMessages.h:104
Websocket client abstraction layer.
Definition: twMessages.h:156
ThingWorx twInfoTable and twDataShape definitions and functions.
ThingWorx C SDK error code definitions.
#define TW_MUTEX
For Linux builds a TW_MUTEX is a pthread_mutex_t.
Definition: twLinux-openssl.h:81
Dynamically allocated byte array. Automatically expands its length as needed.
Definition: twBaseTypes.h:35
Wrappers for OS-specific functionality.
Linked list structure definition.
Definition: twList.h:59
Definition: twMessages.h:75
Definition: twMessages.h:139
Definition: twMessages.h:80
entityTypeEnum
Definition: twDefinitions.h:114
msgCodeEnum
Enumeration of HTTP message codes.
Definition: twDefinitions.h:74
characteristicEnum
Definition: twDefinitions.h:147
Default settings for ThingWorx C SDK.
Common definitions for C SDK.
Definition: inftree9.h:24
ThingWorx BaseType definitions and functions.
Definition: twMessages.h:37
Info table base structure.
Definition: twInfoTable.h:418
Definition: twMessages.h:122
Definition: twMessages.h:183
Websocket entity structure definition.
Definition: twWebsocket.h:87
Definition: twMessages.h:169
BaseType
Definition: twDefinitions.h:156