-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMqttConnectionClient.h
More file actions
65 lines (51 loc) · 2.59 KB
/
MqttConnectionClient.h
File metadata and controls
65 lines (51 loc) · 2.59 KB
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
#ifndef LIB_AGRIROUTERCLIENT_INC_MQTTCONNECTIONCLIENT_H_
#define LIB_AGRIROUTERCLIENT_INC_MQTTCONNECTIONCLIENT_H_
#include "Definitions.h"
#include "Settings.h"
#include "third_party/mosquitto/mosquitto.h"
#include <mutex>
class MqttConnectionClient {
public:
MqttConnectionClient(const std::string& clientId, const std::string& address, int port, Settings *settings);
~MqttConnectionClient();
// Function pointer for callback functions
typedef void (*MqttCallback)(char *topic, void *payload, int payloadlen, void *member);
typedef void (*MqttErrorCallback)(int errorCode, std::string message, std::string content, void *member);
typedef int (*pw_callback)(char *buf, int size, int rwflag, void *userdata);
int init();
void subscribe(const std::string& topic, int qos);
void publish(const std::string& topic, const std::string& payload, int qos);
void publish(const std::string& topic, char *payload, int size, int qos);
void publish(const std::string& topic, char *payload, int size, int qos, bool retain);
void setMqttCallback(MqttCallback callback);
MqttCallback getMqttCallback();
void setMqttErrorCallback(MqttErrorCallback errorCallback);
MqttErrorCallback getMqttErrorCallback();
void setMember(void* member);
void* getMember();
bool isConnected();
private:
struct mosquitto *m_mosq = nullptr;
std::string m_host = "";
int m_port = 0;
std::string m_clientId = "";
int m_messageId = 1;
bool m_connected = false;
void *m_member = nullptr;
Settings *m_settings = nullptr;
MqttCallback m_mqttCallback = NULL;
MqttErrorCallback m_mqttErrorCallback = NULL;
static int onPWCallback(char *buf, int size, int rwflag, void *userdata);
static void connectCallback(struct mosquitto *mosq, void *obj, int result);
static void disconnectCallback(struct mosquitto *mosq, void *obj, int result);
static void publishCallback(struct mosquitto *mosq, void *obj, int messageId);
static void loggingCallback(struct mosquitto *mosq, void *obj, int level, const char *message);
static void subscribeCallback(struct mosquitto *mosq, void *obj, int messageId, int qosCount, const int *grantedQos);
static void unsubscribeCallback(struct mosquitto *mosq, void *obj, int messageId);
static void messageCallback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message);
static std::string globalSecret;
static std::mutex mutexSecret;
static std::string getStaticSecret();
static void setStaticSecret(std::string secret);
};
#endif // LIB_AGRIROUTERCLIENT_INC_MQTTCONNECTIONCLIENT_H_