-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib_mysqludf_mqtt.h
297 lines (278 loc) · 13.1 KB
/
lib_mysqludf_mqtt.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#define LIBVERSION "1.1.0"
#define LIBNAME "lib_mysqludf_mqtt"
#define LIBBUILD __DATE__ " " __TIME__
#define DEFAULT_QOS 0 // default MQTT QOS
#define DEFAULT_RETAINED 0 // default MQTT retain
#define DEFAULT_TIMEOUT 5000L // default MQTT timeout
#define DEFAULT_KEEPALIVEINTERVAL 20 // default MQTT keepalive
#define UUID_LEN 8 // number of hex chars for MQTT unique client id
#define MAX_RET_STRLEN 2048 // max string length returned by functions using strings
//#define DEBUG // debug output via syslog
#if defined(_WIN32) || defined(_WIN64) || defined(__WIN32__) || defined(WIN32)
#define DLLEXP __declspec(dllexport)
#else
#define DLLEXP
#endif
#ifdef __WIN__
typedef unsigned __int64 ulonglong;
typedef __int64 longlong;
#else
typedef unsigned long long ulonglong;
typedef long long longlong;
#endif // __WIN__
// get_json_value() return codes
#define JSON_OK 0
#define JSON_ERROR_EMPTY_STR -1
#define JSON_ERROR_INVALID_STR -2
#define JSON_ERROR_WRONG_TYPE -3
#define JSON_ERROR_WRONG_VALUE -4
#define JSON_ERROR_NOT_FOUND -5
/* MQTT connection information for MySQL UDF */
typedef struct CONNECTION {
MQTTClient client;
MQTTClient_connectOptions conn_opts;
MQTTClient_SSLOptions ssl_opts;
MQTTClient_willOptions will_opts;
int mqtt_publish_format;
int mqtt_subscribe_format;
int rc;
} connection;
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
/**
* mqtt_info
*
* Returns udf library info as JSON string
* mqtt_info()
*
*/
DLLEXP bool mqtt_info_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
DLLEXP void mqtt_info_deinit(UDF_INIT *initid);
DLLEXP char* mqtt_info(UDF_INIT *initid, UDF_ARGS *args, char* result, unsigned long* length, char *is_null, char *error);
/**
* mqtt_lasterror
*
* Returns last error as JSON string
* mqtt_lasterror()
*
*/
DLLEXP bool mqtt_lasterror_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
DLLEXP void mqtt_lasterror_deinit(UDF_INIT *initid);
DLLEXP char* mqtt_lasterror(UDF_INIT *initid, UDF_ARGS *args, char* result, unsigned long* length, char *is_null, char *error);
/**
* mqtt_connect
*
* Connect to a mqtt server and returns a handle.
* mqtt_connect(server {,[username]} {,[password] {,[options]}}})
*
* Parameter in {} are optional an can be omit.
* Parameter in [] can be NULL - in this case a default value is used.
* To use optional parameters after omitting other optional parameters, use NULL, e.g.
* mqtt_connect(server ,NULL, NULL, options)
*
* server String
* Specifying the server to which the client will
* connect. It takes the form protocol://host:port.
* Currently, protocol must be tcp or ssl.
* For host, you can specify either an IP address or a
* host name.
* For instance, to connect to a server running on the
* local machines with the default MQTT port, specify
* "tcp://localhost:1883".
* username String - default ''
* Username for authentification or NULL if unused
* password String - default ''
* Password for authentification or NULL if unused
* options String - default ''
* JSON string containing additonal options or NULL if unused
* The following JSON objects are accept:
* "CApath": String
* Points to a directory containing CA certificates in PEM format
* "CAfile": String
* The file in PEM format containing the public digital
* certificates trusted by the client.
* "keyStore": String
* The file in PEM format containing the public certificate
* chain of the client. It may also include the client's
* private key.
* "privateKey": String
* If not included in the sslKeyStore, this setting points
* to the file in PEM format containing the client's private key.
* "privateKeyPassword": String
* The password to load the client's privateKey if encrypted.
* "enabledCipherSuites": String
* The list of cipher suites that the client will present to the
* server during the SSL handshake.
* "verify": bool
* Whether to carry out post-connect checks, including
* that a certificate matches the given host name.
* "enableServerCertAuth": bool
* True/False option to enable verification of the server
* certificate.
* "sslVersion": integer
* The SSL/TLS version to use. Specify one of:
* 0 = MQTT_SSL_VERSION_DEFAULT
* 1 = MQTT_SSL_VERSION_TLS_1_0
* 2 = MQTT_SSL_VERSION_TLS_1_1
* 3 = MQTT_SSL_VERSION_TLS_1_2
* "keepAliveInterval": integer
* The "keep alive" interval, measured in seconds, defines
* the maximum time that should pass without communication
* between the client and the server.
* "cleansession": bool
* The cleansession setting controls the behaviour of both
* the client and the server at connection and
* disconnection time.
* "reliable": bool
* This is a boolean value that controls how many messages
* can be in-flight simultaneously. Setting reliable to
* true means that a published message must be completed
* (acknowledgements received) before another can be sent.
* "MQTTVersion": String
* Sets the version of MQTT to be used on the connect:
* 0 = MQTTVERSION_DEFAULT start with 3.1.1, and if that
* fails, fall back to 3.1
* 3 = MQTTVERSION_3_1
* 4 = MQTTVERSION_3_1_1
* 5 = MQTTVERSION_5
* "maxInflightMessages": integer
* The maximum number of messages in flight
* "willTopic": String
* The LWT topic to which the LWT message will be published.
* "willMessage": String
* The LWT payload.
* "willRetained": bool
* The retained flag for the LWT messag
* "willQos": integer
* The quality of service setting for the LWT message
*
* returns valid handle or 0 on errors
*/
DLLEXP bool mqtt_connect_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
DLLEXP void mqtt_connect_deinit(UDF_INIT *initid);
DLLEXP ulonglong mqtt_connect(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error);
/**
* mqtt_disconnect
*
* Disconnect from a mqtt server.
* mqtt_disconnect(handle, timeout)
* returns 0 if successful
*/
DLLEXP bool mqtt_disconnect_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
DLLEXP void mqtt_disconnect_deinit(UDF_INIT *initid);
DLLEXP ulonglong mqtt_disconnect(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error);
/**
* mqtt_publish
*
* Publish a mqtt payload and returns its status.
* Possible calls
* mqtt_publish(server, [username], [password], topic, [payload] {,[qos] {,[retained] {,[timeout] {,[options]}}}})
* mqtt_publish(client, topic, [payload] {,[qos] {,[retained] {,[timeout] {,[options]}}}})
*
* Parameter in {} are optional an can be omit.
* Parameter in [] can be NULL - in this case a default value is used.
* To use optional parameters after omitting other optional parameters, use NULL.
*
*
* server String
* Specifying the server to which the client will
* connect. It takes the form protocol://host:port.
* Currently, protocol must be tcp or ssl.
* For host, you can specify either an IP address or a
* host name.
* For instance, to connect to a server running on the
* local machines with the default MQTT port, specify
* "tcp://localhost:1883".
* username String - default ''
* Username for authentification or NULL if unused
* password String - default ''
* Password for authentification or NULL if unused
* client Handle
* A valid handle returned from mqtt_connect() call.
* topic String
* The topic to be published
* payload String - default ''
* The message published for the topic
* qos Integer [0-2] - default 0
* The QOS (Quality Of Service) number
* retained Integer [0,1] - default 0
* Flag if message should be retained (1) or not (0)
* timeout Integer (ms] - default 5000
* Timeout value for connecting to MQTT server (in ms)
* options String - default ''
* JSON string containing additonal options or NULL if unused.
* For details see mqtt_connect()
*
* returns 0 for success, otherwise error code from MQTTClient_connect() call
* (see http://www.eclipse.org/paho/files/mqttdoc/MQTTClient/html/_m_q_t_t_client_8h.html)
*
* If this function is called with server as first parameter, the function will
* connect to MQTT, publish the payload and disconnnect after publish.
* Because this may slow down when a lot of publishing will be done you can do
* publish using an alternate way with a client handle:
* 1. Call mqtt_connect() to get a valid mqtt connection handle
* 2. Call mqtt_publish() using 'client' as first parameter.
* 3. Repeat step 2. as long as possible
* 4. Call mqtt_disconnect() to free the handle
*
*/
DLLEXP bool mqtt_publish_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
DLLEXP void mqtt_publish_deinit(UDF_INIT *initid);
DLLEXP ulonglong mqtt_publish(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error);
/**
* mqtt_subscribe
*
* Subsribe to a mqtt topic and returns the payload if any.
* Possible calls
* mqtt_subscribe(server, [username], [password], topic, {,[qos] {,[timeout] {,[options]}}})
* mqtt_subscribe(client, topic, [payload] {,[qos] {,[timeout] {,[options]}}})
*
* Parameter in {} are optional an can be omit.
* Parameter in [] can be NULL - in this case a default value is used.
* To use optional parameters after omitting other optional parameters, use NULL.
*
* server String
* Specifying the server to which the client will
* connect. It takes the form protocol://host:port.
* Currently, protocol must be tcp or ssl.
* For host, you can specify either an IP address or a
* host name.
* For instance, to connect to a server running on the
* local machines with the default MQTT port, specify
* "tcp://localhost:1883".
* username String - default ''
* Username for authentification or NULL if unused
* password String - default ''
* Password for authentification or NULL if unused
* client Handle
* A valid handle returned from mqtt_connect() call.
* topic String
* The topic to be subscribe
* qos Integer [0..2] - default 0
* The QOS (Quality Of Service) number
* timeout Integer (ms] - default 5000
* Timeout value for connecting to MQTT server (in ms)
* options String - default ''
* JSON string containing additonal options or NULL if unused.
* For detateils see mqtt_connect()
*
* returns the payload as string for success, otherwise it returns NULL
* (see http://www.eclipse.org/paho/files/mqttdoc/MQTTClient/html/_m_q_t_t_client_8h.html)
*
* If this function is called with server as first parameter, the function will
* connect to MQTT, subscribe to topic and disconnnect after subscribed.
* Because this may slow down when a lot of subscribes will be done you can do
* subscribing use an alternate way with a client handle:
* 1. Call mqtt_connect() to get a valid mqtt connection handle
* 2. Call mqtt_subscribe() using 'client' as first parameter.
* 3. Repeat step 2. as long as possible
* 4. Call mqtt_disconnect() to free the handle
*
*/
DLLEXP bool mqtt_subscribe_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
DLLEXP void mqtt_subscribe_deinit(UDF_INIT *initid);
DLLEXP char* mqtt_subscribe(UDF_INIT *initid, UDF_ARGS *args, char* result, unsigned long* length, char *is_null, char *error);
#ifdef __cplusplus
}
#endif // __cplusplus