paho_mqtt_sys/
lib.rs

1// lib.rs
2//
3// The main library file for `paho-mqtt-sys`.
4// This is the Paho MQTT Rust library low-level C wrapper.
5//
6
7/*******************************************************************************
8 * Copyright (c) 2017-2019 Frank Pagliughi <fpagliughi@mindspring.com>
9 *
10 * All rights reserved. This program and the accompanying materials
11 * are made available under the terms of the Eclipse Public License v2.0
12 * and Eclipse Distribution License v1.0 which accompany this distribution.
13 *
14 * The Eclipse Public License is available at
15 *    http://www.eclipse.org/legal/epl-v20.html
16 * and the Eclipse Distribution License is available at
17 *   http://www.eclipse.org/org/documents/edl-v10.php.
18 *
19 * Contributors:
20 *    Frank Pagliughi - initial implementation and documentation
21 *******************************************************************************/
22
23#![allow(non_upper_case_globals)]
24#![allow(non_camel_case_types)]
25#![allow(non_snake_case)]
26// Weirdness in the bindgen bindings
27#![allow(deref_nullptr)]
28// Temporary
29#![allow(dead_code)]
30
31use std::fmt;
32use std::os::raw::{c_char, c_int};
33use std::ptr;
34
35include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
36
37// The following 'Default' trait implementations contain initializations
38// for the structures from the Paho C library. Each of those structs
39// contains an initializer macro in MQTTAsync.h.
40// By convention, these default initializers match those macros from the
41// C library.
42
43/////////////////////////////////////////////////////////////////////////////
44// Client creation
45
46/// The currently supported version of the C create options struct.
47pub const CREATE_OPTIONS_STRUCT_VERSION: i32 = 2;
48
49impl Default for MQTTAsync_createOptions {
50    /// Creates a client that can connect using MQTT v3.x or v5
51    fn default() -> Self {
52        Self {
53            struct_id: [
54                b'M' as c_char,
55                b'Q' as c_char,
56                b'C' as c_char,
57                b'O' as c_char,
58            ],
59            struct_version: CREATE_OPTIONS_STRUCT_VERSION,
60            sendWhileDisconnected: 0,
61            maxBufferedMessages: 100,
62            MQTTVersion: MQTTVERSION_5 as c_int,
63            allowDisconnectedSendAtAnyTime: 0,
64            deleteOldestMessages: 0,
65            restoreMessages: 1,
66            persistQoS0: 1,
67        }
68    }
69}
70
71impl MQTTAsync_createOptions {
72    /// Creates a client that can connect using MQTT v3.x or v5
73    pub fn new() -> Self {
74        Self::default()
75    }
76
77    /// Creates a client that can only connect using MQTT v3.x
78    pub fn new_v3() -> Self {
79        Self {
80            MQTTVersion: MQTTVERSION_DEFAULT as c_int,
81            ..Self::default()
82        }
83    }
84
85    /// Creates a client that can connect using MQTT v3.x or v5
86    pub fn new_v5() -> Self {
87        Self::default()
88    }
89}
90
91/////////////////////////////////////////////////////////////////////////////
92// Connecting
93
94// Note that this sets up the defaults for MQTT 3.1.1 or earlier.
95// The application must specifically set the version to 5 for MQTT v5, and
96// disable clean sessions (at a minimum).
97
98/// The currently supported version of the C connect options struct.
99pub const CONNECT_OPTIONS_STRUCT_VERSION: i32 = 8;
100
101impl Default for MQTTAsync_connectOptions {
102    fn default() -> Self {
103        Self {
104            struct_id: [
105                b'M' as c_char,
106                b'Q' as c_char,
107                b'T' as c_char,
108                b'C' as c_char,
109            ],
110            struct_version: CONNECT_OPTIONS_STRUCT_VERSION,
111            keepAliveInterval: 60,
112            cleansession: 1,
113            maxInflight: 65535,
114            will: ptr::null_mut(),
115            username: ptr::null(),
116            password: ptr::null(),
117            connectTimeout: 30,
118            retryInterval: 0,
119            ssl: ptr::null_mut(),
120            onSuccess: None,
121            onFailure: None,
122            context: ptr::null_mut(),
123            serverURIcount: 0,
124            serverURIs: ptr::null(),
125            MQTTVersion: 0,
126            automaticReconnect: 0,
127            minRetryInterval: 1,
128            maxRetryInterval: 60,
129            binarypwd: MQTTAsync_connectOptions__bindgen_ty_1 {
130                len: 0,
131                data: ptr::null(),
132            },
133            cleanstart: 0,
134            connectProperties: ptr::null_mut(),
135            willProperties: ptr::null_mut(),
136            onSuccess5: None,
137            onFailure5: None,
138            httpHeaders: ptr::null(),
139            httpProxy: ptr::null(),
140            httpsProxy: ptr::null(),
141        }
142    }
143}
144
145impl MQTTAsync_connectOptions {
146    /// Creates default connect options for v3.x
147    pub fn new() -> Self {
148        Self::default()
149    }
150
151    /// Creates default connect options specifically for v3.x
152    pub fn new_v3() -> Self {
153        Self::default()
154    }
155
156    /// Creates default connect options for v5
157    pub fn new_v5() -> Self {
158        Self {
159            cleansession: 0,
160            cleanstart: 1,
161            MQTTVersion: MQTTVERSION_5 as i32,
162            ..Self::default()
163        }
164    }
165
166    /// Creates default connect options for v3.x over websockets
167    pub fn new_ws() -> Self {
168        Self {
169            keepAliveInterval: 45,
170            ..Self::default()
171        }
172    }
173
174    /// Creates default connect options for v3.x over websockets
175    pub fn new_ws_v5() -> Self {
176        Self {
177            keepAliveInterval: 45,
178            ..Self::new_v5()
179        }
180    }
181}
182
183/////////////////////////////////////////////////////////////////////////////
184// Options
185
186/// The currently supported version of the C will options struct.
187pub const WILL_OPTIONS_STRUCT_VERSION: i32 = 1;
188
189impl Default for MQTTAsync_willOptions {
190    fn default() -> MQTTAsync_willOptions {
191        MQTTAsync_willOptions {
192            struct_id: [
193                b'M' as c_char,
194                b'Q' as c_char,
195                b'T' as c_char,
196                b'W' as c_char,
197            ],
198            struct_version: WILL_OPTIONS_STRUCT_VERSION,
199            topicName: ptr::null(),
200            message: ptr::null(),
201            retained: 0,
202            qos: 0,
203            payload: MQTTAsync_willOptions__bindgen_ty_1 {
204                len: 0,
205                data: ptr::null(),
206            },
207        }
208    }
209}
210
211/// The currently supported version of the C SSL options struct.
212pub const SSL_OPTIONS_STRUCT_VERSION: i32 = 5;
213
214impl Default for MQTTAsync_SSLOptions {
215    fn default() -> MQTTAsync_SSLOptions {
216        MQTTAsync_SSLOptions {
217            struct_id: [
218                b'M' as c_char,
219                b'Q' as c_char,
220                b'T' as c_char,
221                b'S' as c_char,
222            ],
223            struct_version: SSL_OPTIONS_STRUCT_VERSION,
224            trustStore: ptr::null(),
225            keyStore: ptr::null(),
226            privateKey: ptr::null(),
227            privateKeyPassword: ptr::null(),
228            enabledCipherSuites: ptr::null(),
229            enableServerCertAuth: 1,
230            sslVersion: MQTT_SSL_VERSION_DEFAULT as i32,
231            verify: 0,
232            CApath: ptr::null(),
233            ssl_error_cb: None,
234            ssl_error_context: ptr::null_mut(),
235            ssl_psk_cb: None,
236            ssl_psk_context: ptr::null_mut(),
237            disableDefaultTrustStore: 0,
238            protos: ptr::null(),
239            protos_len: 0,
240        }
241    }
242}
243
244/// The currently supported version of the C subscribe options struct.
245pub const SUBSCRIBE_OPTIONS_STRUCT_VERSION: i32 = 0;
246
247// New for MQTT v5
248impl Default for MQTTSubscribe_options {
249    fn default() -> MQTTSubscribe_options {
250        MQTTSubscribe_options {
251            struct_id: [
252                b'M' as c_char,
253                b'Q' as c_char,
254                b'S' as c_char,
255                b'O' as c_char,
256            ],
257            struct_version: SUBSCRIBE_OPTIONS_STRUCT_VERSION,
258            noLocal: 0,
259            retainAsPublished: 0,
260            retainHandling: 0,
261        }
262    }
263}
264
265/// The currently suppoted version of the C response options struct.
266pub const RESPONSE_OPTIONS_STRUCT_VERSION: i32 = 1;
267
268impl Default for MQTTAsync_responseOptions {
269    fn default() -> MQTTAsync_responseOptions {
270        MQTTAsync_responseOptions {
271            struct_id: [
272                b'M' as c_char,
273                b'Q' as c_char,
274                b'T' as c_char,
275                b'R' as c_char,
276            ],
277            struct_version: RESPONSE_OPTIONS_STRUCT_VERSION,
278            onSuccess: None,
279            onFailure: None,
280            context: ptr::null_mut(),
281            token: 0,
282            onSuccess5: None,
283            onFailure5: None,
284            properties: MQTTProperties::default(),
285            subscribeOptions: MQTTSubscribe_options::default(),
286            subscribeOptionsCount: 0,
287            subscribeOptionsList: ptr::null_mut(),
288        }
289    }
290}
291
292/////////////////////////////////////////////////////////////////////////////
293// MQTTProperties (new for v5)
294
295impl fmt::Debug for MQTTProperty {
296    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
297        // TODO: Implement this
298        write!(f, "[]")
299    }
300}
301
302impl Default for MQTTProperties {
303    fn default() -> MQTTProperties {
304        MQTTProperties {
305            count: 0,
306            max_count: 0,
307            length: 0,
308            array: ptr::null_mut(),
309        }
310    }
311}
312
313impl Default for MQTTLenString {
314    fn default() -> MQTTLenString {
315        MQTTLenString {
316            len: 0,
317            data: ptr::null_mut(),
318        }
319    }
320}
321
322/////////////////////////////////////////////////////////////////////////////
323// Messages
324
325/// The currently suppoted version of the C message struct.
326pub const MESSAGE_STRUCT_VERSION: i32 = 1;
327
328impl Default for MQTTAsync_message {
329    fn default() -> MQTTAsync_message {
330        MQTTAsync_message {
331            struct_id: [
332                b'M' as c_char,
333                b'Q' as c_char,
334                b'T' as c_char,
335                b'M' as c_char,
336            ],
337            struct_version: MESSAGE_STRUCT_VERSION,
338            payloadlen: 0,
339            payload: ptr::null_mut(),
340            qos: 0,
341            retained: 0,
342            dup: 0,
343            msgid: 0,
344            properties: MQTTProperties::default(),
345        }
346    }
347}
348
349/////////////////////////////////////////////////////////////////////////////
350// Disconnecting
351
352/// The currently suppoted version of the C disconnect options struct.
353pub const DISCONNECT_OPTIONS_STRUCT_VERSION: i32 = 1;
354
355impl Default for MQTTAsync_disconnectOptions {
356    fn default() -> MQTTAsync_disconnectOptions {
357        MQTTAsync_disconnectOptions {
358            struct_id: [
359                b'M' as c_char,
360                b'Q' as c_char,
361                b'T' as c_char,
362                b'D' as c_char,
363            ],
364            struct_version: DISCONNECT_OPTIONS_STRUCT_VERSION,
365            timeout: 0,
366            onSuccess: None,
367            onFailure: None,
368            context: ptr::null_mut(),
369            properties: MQTTProperties::default(),
370            reasonCode: 0,
371            onSuccess5: None,
372            onFailure5: None,
373        }
374    }
375}
376
377/////////////////////////////////////////////////////////////////////////////
378// Persistence
379
380impl Default for MQTTClient_persistence {
381    fn default() -> Self {
382        MQTTClient_persistence {
383            context: ptr::null_mut(),
384            popen: None,
385            pclose: None,
386            pput: None,
387            pget: None,
388            premove: None,
389            pkeys: None,
390            pclear: None,
391            pcontainskey: None,
392        }
393    }
394}
395
396/////////////////////////////////////////////////////////////////////////////
397// Misc Data Structures
398
399impl MQTTAsync_nameValue {
400    pub fn new(name: *const c_char, value: *const c_char) -> Self {
401        Self { name, value }
402    }
403}
404
405impl Default for MQTTAsync_nameValue {
406    fn default() -> Self {
407        Self {
408            name: ptr::null(),
409            value: ptr::null(),
410        }
411    }
412}
413
414/////////////////////////////////////////////////////////////////////////////
415// Unit Tests
416
417#[cfg(test)]
418mod tests {}