2 Copyright (c) 2010-2013 Roger Light <roger@atchoo.org>
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 3. Neither the name of mosquitto nor the names of its
14 contributors may be used to endorse or promote products derived from
15 this software without specific prior written permission.
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE.
30 This product includes software developed by the OpenSSL Project for use in the
31 OpenSSL Toolkit. (http://www.openssl.org/)
32 This product includes cryptographic software written by Eric Young
34 This product includes software written by Tim Hudson (tjh@cryptsoft.com)
37 #ifndef _MOSQUITTOPP_H_
38 #define _MOSQUITTOPP_H_
41 # ifdef mosquittopp_EXPORTS
42 # define mosqpp_EXPORT __declspec(dllexport)
44 # define mosqpp_EXPORT __declspec(dllimport)
47 # define mosqpp_EXPORT
52 #include "../mosquitto.h"
56 mosqpp_EXPORT const char *strerror(int mosq_errno);
57 mosqpp_EXPORT const char *connack_string(int connack_code);
58 mosqpp_EXPORT int sub_topic_tokenise(const char *subtopic, char ***topics, int *count);
59 mosqpp_EXPORT int sub_topic_tokens_free(char ***topics, int count);
60 mosqpp_EXPORT int lib_version(int *major, int *minor, int *revision);
61 mosqpp_EXPORT int lib_init();
62 mosqpp_EXPORT int lib_cleanup();
63 mosqpp_EXPORT int topic_matches_sub(const char *sub, const char *topic, bool *result);
68 * A mosquitto client class. This is a C++ wrapper class for the mosquitto C
69 * library. Please see mosquitto.h for details of the functions.
71 class mosqpp_EXPORT mosquittopp {
73 struct mosquitto *m_mosq;
75 mosquittopp(const char *id=NULL, bool clean_session=true);
78 int reinitialise(const char *id, bool clean_session);
80 int will_set(const char *topic, int payloadlen=0, const void *payload=NULL, int qos=0, bool retain=false);
82 int username_pw_set(const char *username, const char *password=NULL);
83 int connect(const char *host, int port=1883, int keepalive=60);
84 int connect_async(const char *host, int port=1883, int keepalive=60);
85 int connect(const char *host, int port, int keepalive, const char *bind_address);
86 int connect_async(const char *host, int port, int keepalive, const char *bind_address);
88 int reconnect_async();
90 int publish(int *mid, const char *topic, int payloadlen=0, const void *payload=NULL, int qos=0, bool retain=false);
91 int subscribe(int *mid, const char *sub, int qos=0);
92 int unsubscribe(int *mid, const char *sub);
93 void reconnect_delay_set(unsigned int reconnect_delay, unsigned int reconnect_delay_max, bool reconnect_exponential_backoff);
94 int max_inflight_messages_set(unsigned int max_inflight_messages);
95 void message_retry_set(unsigned int message_retry);
96 void user_data_set(void *userdata);
97 int tls_set(const char *cafile, const char *capath=NULL, const char *certfile=NULL, const char *keyfile=NULL, int (*pw_callback)(char *buf, int size, int rwflag, void *userdata)=NULL);
98 int tls_opts_set(int cert_reqs, const char *tls_version=NULL, const char *ciphers=NULL);
99 int tls_insecure_set(bool value);
100 int tls_psk_set(const char *psk, const char *identity, const char *ciphers=NULL);
102 int loop(int timeout=-1, int max_packets=1);
104 int loop_read(int max_packets=1);
105 int loop_write(int max_packets=1);
106 int loop_forever(int timeout=-1, int max_packets=1);
108 int loop_stop(bool force=false);
111 virtual void on_connect(int rc) {return;};
112 virtual void on_disconnect(int rc) {return;};
113 virtual void on_publish(int mid) {return;};
114 virtual void on_message(const struct mosquitto_message *message) {return;};
115 virtual void on_subscribe(int mid, int qos_count, const int *granted_qos) {return;};
116 virtual void on_unsubscribe(int mid) {return;};
117 virtual void on_log(int level, const char *str) {return;};
118 virtual void on_error() {return;};