-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlibnvds_mqtt_sink.cpp
209 lines (174 loc) · 6.89 KB
/
libnvds_mqtt_sink.cpp
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
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>
#include <string>
#include <sstream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <sys/time.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <netdb.h>
#include <openssl/sha.h>
#include <glib.h>
#include <algorithm>
#include "nvds_logger.h"
#include "nvds_msgapi.h"
#include "nvds_utils.h"
#include "libnvds_mqtt_sink.h"
#include "mqtt_client.h"
using namespace std;
/////////////////////////////////////////////////////////////////////////////
/**
* Connects to a remote mqtt broker based on connection string.
*/
NvDsMsgApiHandle nvds_msgapi_connect(char *connection_str, nvds_msgapi_connect_cb_t connect_cb, char *config_path) {
nvds_log_open();
async_client *ch = new(nothrow) async_client(connect_cb);
if (config_path) {
if (!ch->read_config(config_path)) {
nvds_log(NVDS_MQTT_LOG_CAT, LOG_ERR, "MQTT config parsing failed. Connection failed \n");
return NULL;
}
}
if (!ch->set_connection_str(connection_str)){
nvds_log(NVDS_MQTT_LOG_CAT, LOG_ERR, "MQTT connection string parsing failed. Connection failed \n");
return NULL;
}
if (!ch->connect()){
nvds_log(NVDS_MQTT_LOG_CAT, LOG_ERR, "MQTT Connection failed \n");
return NULL;
}
return (NvDsMsgApiHandle)(ch);
}
/**
* This api will be used to create & intialize mqtt consumer
* A consumer thread is spawned to listen & consume messages on topics specified in the api
*/
NvDsMsgApiErrorType nvds_msgapi_subscribe(NvDsMsgApiHandle h_ptr, char **topics, int num_topics,
nvds_msgapi_subscribe_request_cb_t cb, void *user_ctx) {
async_client *ch = (async_client *) h_ptr;
if (ch == NULL) {
nvds_log(NVDS_MQTT_LOG_CAT, LOG_ERR,
"MQTT connection handle passed for nvds_msgapi_subscribe() = NULL. Subscribe failed\n");
return NVDS_MSGAPI_ERR;
}
if (topics == NULL || num_topics <= 0) {
nvds_log(NVDS_MQTT_LOG_CAT, LOG_ERR, "Topics not specified for subscribe. Subscription failed\n");
return NVDS_MSGAPI_ERR;
}
if (!cb) {
nvds_log(NVDS_MQTT_LOG_CAT, LOG_ERR, "Subscribe callback cannot be NULL. subscription failed\n");
return NVDS_MSGAPI_ERR;
}
for (int i = 0; i < num_topics; i++) {
if (!ch->subscribe(topics[i], cb, user_ctx)) {
nvds_log(NVDS_MQTT_LOG_CAT, LOG_ERR, "Subscription failed\n");
return NVDS_MSGAPI_ERR;
}
}
return NVDS_MSGAPI_OK;
}
/**
* There could be several synchronous and asychronous send operations in flight.
* Once a send operation callback is received the course of action depends on if it's synch or async
* -- if it's sync then the associated complletion flag should be set
* -- if it's asynchronous then completion callback from the user should be called
*/
NvDsMsgApiErrorType nvds_msgapi_send(NvDsMsgApiHandle h_ptr, char *topic, const uint8_t *payload, size_t nbuf) {
async_client *ch = (async_client *) h_ptr;
if (h_ptr == NULL) {
nvds_log(NVDS_MQTT_LOG_CAT, LOG_ERR, "MQTT connection handle passed for send() = NULL. Send failed\n");
return NVDS_MSGAPI_ERR;
}
if (topic == NULL || !strcmp(topic, "")) {
nvds_log(NVDS_MQTT_LOG_CAT, LOG_ERR, "MQTT topic not specified.Send failed\n");
return NVDS_MSGAPI_ERR;
}
if (payload == NULL || nbuf <= 0) {
nvds_log(NVDS_MQTT_LOG_CAT, LOG_ERR, "mqtt: Either send payload is NULL or payload length <=0. Send failed\n");
return NVDS_MSGAPI_ERR;
}
nvds_log(NVDS_MQTT_LOG_CAT, LOG_DEBUG, "nvds_msgapi_send: payload=%.*s \n topic = %s\n", nbuf, payload, topic);
if (!ch->send(topic, payload, nbuf)) {
nvds_log(NVDS_MQTT_LOG_CAT, LOG_ERR, "Send failed\n");
return NVDS_MSGAPI_ERR;
}
return NVDS_MSGAPI_OK;
}
NvDsMsgApiErrorType nvds_msgapi_send_async(NvDsMsgApiHandle h_ptr, char *topic, const uint8_t *payload, size_t nbuf,
nvds_msgapi_send_cb_t cb, void *user_ctx) {
async_client *ch = (async_client *) h_ptr;
if (h_ptr == NULL) {
nvds_log(NVDS_MQTT_LOG_CAT, LOG_ERR, "MQTT connection handle passed for send_async() = NULL. Send failed\n");
return NVDS_MSGAPI_ERR;
}
if (topic == NULL || !strcmp(topic, "")) {
nvds_log(NVDS_MQTT_LOG_CAT, LOG_ERR, "MQTT topic not specified. Send failed\n");
return NVDS_MSGAPI_ERR;
}
if (payload == NULL || nbuf <= 0) {
nvds_log(NVDS_MQTT_LOG_CAT, LOG_ERR,
"mqtt: send_async() either payload is NULL or payload length <=0. Send failed\n");
return NVDS_MSGAPI_ERR;
}
if (!cb) {
nvds_log(NVDS_MQTT_LOG_CAT, LOG_ERR, "Send callback cannot be NULL. Async send failed\n");
return NVDS_MSGAPI_ERR;
}
nvds_log(NVDS_MQTT_LOG_CAT, LOG_DEBUG, "nvds_msgapi_send_async: payload=%.*s \n topic = %s\n", nbuf, payload, topic);
if (!ch->send_async(topic, payload, nbuf, cb, user_ctx)) {
nvds_log(NVDS_MQTT_LOG_CAT, LOG_ERR, "Send async failed\n");
return NVDS_MSGAPI_ERR;
}
return NVDS_MSGAPI_OK;
}
void nvds_msgapi_do_work(NvDsMsgApiHandle h_ptr) {
if (h_ptr == NULL) {
nvds_log(NVDS_MQTT_LOG_CAT, LOG_ERR, "MQTT connection handle passed for dowork() = NULL. No actions taken\n");
return;
}
nvds_log(NVDS_MQTT_LOG_CAT, LOG_DEBUG, "nvds_msgapi_do_work\n");
async_client *ch = (async_client *) h_ptr;
ch->do_work();
}
NvDsMsgApiErrorType nvds_msgapi_disconnect(NvDsMsgApiHandle h_ptr) {
if (!h_ptr) {
nvds_log(NVDS_MQTT_LOG_CAT, LOG_DEBUG, "nvds_msgapi_disconnect called with null handle\n");
return NVDS_MSGAPI_ERR;
}
async_client *ch = (async_client *) h_ptr;
if (!ch->disconnect()) {
nvds_log(NVDS_MQTT_LOG_CAT, LOG_ERR, "Disconnect failed\n");
return NVDS_MSGAPI_ERR;
}
delete ch;
nvds_log_close();
return NVDS_MSGAPI_OK;
}
/**
* Returns version of API supported by this adaptor
*/
char *nvds_msgapi_getversion() {
return (char *) NVDS_MSGAPI_VERSION;
}
//Returns underlying framework/protocol name
char *nvds_msgapi_get_protocol_name() {
return (char *) NVDS_MSGAPI_PROTOCOL;
}
//Query connection signature
NvDsMsgApiErrorType nvds_msgapi_connection_signature(char *broker_str, char *cfg, char *output_str, int max_len) {
strcpy(output_str, "");
if (broker_str == NULL || cfg == NULL) {
nvds_log(NVDS_MQTT_LOG_CAT, LOG_ERR, "nvds_msgapi_connection_signature: broker_str or cfg path cant be NULL");
return NVDS_MSGAPI_ERR;
}
// TODO: Create a valid signature, but for now do this:
nvds_log(NVDS_MQTT_LOG_CAT, LOG_INFO,
"nvds_msgapi_connection_signature: MQTT connection sharing disabled. Hence connection signature cant be returned");
return NVDS_MSGAPI_OK;
}