Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / service / protocol-plugin / plugins / mqtt-fan / lib / cpp / mosquittopp.h
1 /*
2 Copyright (c) 2010-2013 Roger Light <roger@atchoo.org>
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
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.
16
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.
28
29
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
33 (eay@cryptsoft.com)
34 This product includes software written by Tim Hudson (tjh@cryptsoft.com)
35 */
36
37 #ifndef _MOSQUITTOPP_H_
38 #define _MOSQUITTOPP_H_
39
40 #ifdef _WIN32
41 #   ifdef mosquittopp_EXPORTS
42 #       define mosqpp_EXPORT  __declspec(dllexport)
43 #   else
44 #       define mosqpp_EXPORT  __declspec(dllimport)
45 #   endif
46 #else
47 #   define mosqpp_EXPORT
48 #endif
49
50 #include <cstdlib>
51 #include <time.h>
52 #include "../mosquitto.h"
53
54 namespace mosqpp
55 {
56
57     mosqpp_EXPORT const char *strerror(int mosq_errno);
58     mosqpp_EXPORT const char *connack_string(int connack_code);
59     mosqpp_EXPORT int sub_topic_tokenise(const char *subtopic, char ***topics, int *count);
60     mosqpp_EXPORT int sub_topic_tokens_free(char ***topics, int count);
61     mosqpp_EXPORT int lib_version(int *major, int *minor, int *revision);
62     mosqpp_EXPORT int lib_init();
63     mosqpp_EXPORT int lib_cleanup();
64     mosqpp_EXPORT int topic_matches_sub(const char *sub, const char *topic, bool *result);
65
66     /*
67      * Class: mosquittopp
68      *
69      * A mosquitto client class. This is a C++ wrapper class for the mosquitto C
70      * library. Please see mosquitto.h for details of the functions.
71      */
72     class mosqpp_EXPORT mosquittopp
73     {
74         private:
75             struct mosquitto *m_mosq;
76         public:
77             mosquittopp(const char *id = NULL, bool clean_session = true);
78             ~mosquittopp();
79
80             int reinitialise(const char *id, bool clean_session);
81             int socket();
82             int will_set(const char *topic, int payloadlen = 0, const void *payload = NULL, int qos = 0,
83                          bool retain = false);
84             int will_clear();
85             int username_pw_set(const char *username, const char *password = NULL);
86             int connect(const char *host, int port = 1883, int keepalive = 60);
87             int connect_async(const char *host, int port = 1883, int keepalive = 60);
88             int connect(const char *host, int port, int keepalive, const char *bind_address);
89             int connect_async(const char *host, int port, int keepalive, const char *bind_address);
90             int reconnect();
91             int reconnect_async();
92             int disconnect();
93             int publish(int *mid, const char *topic, int payloadlen = 0, const void *payload = NULL,
94                         int qos = 0, bool retain = false);
95             int subscribe(int *mid, const char *sub, int qos = 0);
96             int unsubscribe(int *mid, const char *sub);
97             void reconnect_delay_set(unsigned int reconnect_delay, unsigned int reconnect_delay_max,
98                                      bool reconnect_exponential_backoff);
99             int max_inflight_messages_set(unsigned int max_inflight_messages);
100             void message_retry_set(unsigned int message_retry);
101             void user_data_set(void *userdata);
102             int tls_set(const char *cafile, const char *capath = NULL, const char *certfile = NULL,
103                         const char *keyfile = NULL, int (*pw_callback)(char *buf, int size, int rwflag,
104                                 void *userdata) = NULL);
105             int tls_opts_set(int cert_reqs, const char *tls_version = NULL, const char *ciphers = NULL);
106             int tls_insecure_set(bool value);
107             int tls_psk_set(const char *psk, const char *identity, const char *ciphers = NULL);
108
109             int loop(int timeout = -1, int max_packets = 1);
110             int loop_misc();
111             int loop_read(int max_packets = 1);
112             int loop_write(int max_packets = 1);
113             int loop_forever(int timeout = -1, int max_packets = 1);
114             int loop_start();
115             int loop_stop(bool force = false);
116             bool want_write();
117
118             virtual void on_connect(int rc) {return;};
119             virtual void on_disconnect(int rc) {return;};
120             virtual void on_publish(int mid) {return;};
121             virtual void on_message(const struct mosquitto_message *message) {return;};
122             virtual void on_subscribe(int mid, int qos_count, const int *granted_qos) {return;};
123             virtual void on_unsubscribe(int mid) {return;};
124             virtual void on_log(int level, const char *str) {return;};
125             virtual void on_error() {return;};
126     };
127
128 }
129 #endif