NVIDIA DeepStream SDK API Reference

9.1 Release
sources/includes/nvds_mqtt_proto.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include "mqtt_protocol.h"
19 #include "mosquitto.h"
20 
21 #include <mutex>
22 #include <unordered_map>
23 #include <errno.h>
24 #include <syslog.h>
25 #include "glib.h"
26 #include <string.h>
27 #include <string>
28 #include "nvds_msgapi.h"
29 #include "nvds_utils.h"
30 
31 using namespace std;
32 
33 #define MAX_FIELD_LEN 1024
34 
35 // Structure to hold extracted data from protobuf payload
36 struct ExtractedData {
37  std::string sensor_id; // Extracted from metadata
38  std::string timestamp; // Not used - timestamp is always current system time
39 };
40 
41 // Structure to hold protobuf wrapper message
43  std::string payload; // Raw protobuf binary data (no encoding needed)
44  std::string key; // sensorId extracted from metadata
45  std::string timestamp; // current system timestamp when data was sent
46 };
47 #define DEFAULT_LOOP_TIMEOUT 2000
48 #define DEFAULT_KEEP_ALIVE 60
49 /* Message details:
50  * send_callback = user callback func
51  * user_ptr = user pointer passed by async send
52  */
55  void *user_ptr;
56 };
57 
58 /* Details of mqtt connection handle:
59  * mosq : mosquitto client object
60  * sub_callback : user subscription callback func
61  * connect_cb : user connection callback func
62  * user_ctx : user pointer passed by sub
63  * username: username for login to server
64  * password: password for login to server
65  * client_id: name of MQTT client
66  * loop_timeout : time in ms for the call to loop to wait for network activity
67  * keep_alive : number of seconds after which broker should send PING if no messages have been exchanged
68  * subscription_on : Flag to check if subscription is ON
69  * send_msg_info_map : map message info to id assigned by mosquitto broker
70  * map_lock : mutex lock for accessing above map
71  * enable_tls : flag to check if TLS encryption is enabled by the broker
72  * cafile : path to a TLS certificate authority file
73  * capath : path to a directory containing TLS CA files
74  * certfile : path to the client TLS certificate file
75  * keyfile : path to the client TLS key file
76  * disconnect : bool for checking if disconnect has been called
77  * set_threaded : bool for setting mosquitto_threaded_set in proto adaptor
78  */
79 typedef struct {
80  struct mosquitto *mosq = NULL;
83  void* user_ctx;
84  char connection_str[MAX_FIELD_LEN] = {0};
85  char username[MAX_FIELD_LEN] = {0};
86  char password[MAX_FIELD_LEN] = {0};
87  char client_id[MAX_FIELD_LEN] = {0};
88  int loop_timeout = DEFAULT_LOOP_TIMEOUT;
89  int keep_alive = DEFAULT_KEEP_ALIVE;
90  bool subscription_on = false;
91  std::unordered_map<int , send_msg_info_t> send_msg_info_map;
92  std::mutex map_lock;
93  bool enable_tls = false;
94  char cafile[MAX_FIELD_LEN] = {0};
95  char capath[MAX_FIELD_LEN] = {0};
96  char certfile[MAX_FIELD_LEN] = {0};
97  char keyfile[MAX_FIELD_LEN] = {0};
98  bool disconnect = false;
99  bool set_threaded = true;
101 
103 void mosq_mqtt_log_callback(struct mosquitto *mosq, void *obj, int level, const char *str);
104 void my_disconnect_callback(struct mosquitto *mosq, void *obj, int rc, const mosquitto_property *properties);
105 void my_connect_callback(struct mosquitto *mosq, void *obj, int result, int flags, const mosquitto_property *properties);
106 void my_publish_callback(struct mosquitto *mosq, void *obj, int mid, int reason_code, const mosquitto_property *properties);
107 bool is_valid_mqtt_connection_str(char *connection_str, std::string &burl, std::string &bport);
108 
109 // Function declarations for protobuf wrapper
110 ExtractedData extract_data_from_payload(const uint8_t *payload, size_t nbuf);
111 std::string create_protobuf_wrapper(const uint8_t *payload, size_t nbuf);
112 std::string create_enhanced_payload(const uint8_t *payload, size_t nbuf);
113 std::string serialize_protobuf_wrapper(const ProtobufWrapper& wrapper);
send_msg_info_t
Definition: sources/includes/nvds_mqtt_proto.h:53
my_connect_callback
void my_connect_callback(struct mosquitto *mosq, void *obj, int result, int flags, const mosquitto_property *properties)
nvds_msgapi_connect_cb_t
void(* nvds_msgapi_connect_cb_t)(NvDsMsgApiHandle h_ptr, NvDsMsgApiEventType ds_evt)
Type definition for a "handle" callback.
Definition: sources/includes/nvds_msgapi.h:103
mosq_mqtt_log_callback
void mosq_mqtt_log_callback(struct mosquitto *mosq, void *obj, int level, const char *str)
NvDsMqttClientHandle::user_ctx
void * user_ctx
Definition: sources/includes/nvds_mqtt_proto.h:83
NvDsMsgApiErrorType
NvDsMsgApiErrorType
Defines completion codes for operations in the messaging API.
Definition: sources/includes/nvds_msgapi.h:62
MAX_FIELD_LEN
#define MAX_FIELD_LEN
Definition: sources/includes/nvds_mqtt_proto.h:33
ProtobufWrapper
Definition: sources/includes/nvds_mqtt_proto.h:42
ExtractedData
Definition: sources/includes/nvds_mqtt_proto.h:36
ProtobufWrapper::timestamp
std::string timestamp
Definition: sources/includes/nvds_mqtt_proto.h:45
DEFAULT_KEEP_ALIVE
#define DEFAULT_KEEP_ALIVE
Definition: sources/includes/nvds_mqtt_proto.h:48
ExtractedData::sensor_id
std::string sensor_id
Definition: sources/includes/nvds_mqtt_proto.h:37
extract_data_from_payload
ExtractedData extract_data_from_payload(const uint8_t *payload, size_t nbuf)
my_disconnect_callback
void my_disconnect_callback(struct mosquitto *mosq, void *obj, int rc, const mosquitto_property *properties)
ProtobufWrapper::payload
std::string payload
Definition: sources/includes/nvds_mqtt_proto.h:43
NvDsMqttClientHandle::send_msg_info_map
std::unordered_map< int, send_msg_info_t > send_msg_info_map
Definition: sources/includes/nvds_mqtt_proto.h:91
is_valid_mqtt_connection_str
bool is_valid_mqtt_connection_str(char *connection_str, std::string &burl, std::string &bport)
send_msg_info_t::user_ptr
void * user_ptr
Definition: sources/includes/nvds_mqtt_proto.h:55
NvDsMqttClientHandle
Definition: sources/includes/nvds_mqtt_proto.h:79
NvDsMqttClientHandle::map_lock
std::mutex map_lock
Definition: sources/includes/nvds_mqtt_proto.h:92
nvds_msgapi.h
ProtobufWrapper::key
std::string key
Definition: sources/includes/nvds_mqtt_proto.h:44
nvds_mqtt_read_config
NvDsMsgApiErrorType nvds_mqtt_read_config(NvDsMqttClientHandle *mh, char *config_path)
ExtractedData::timestamp
std::string timestamp
Definition: sources/includes/nvds_mqtt_proto.h:38
create_protobuf_wrapper
std::string create_protobuf_wrapper(const uint8_t *payload, size_t nbuf)
NvDsMqttClientHandle::sub_callback
nvds_msgapi_subscribe_request_cb_t sub_callback
Definition: sources/includes/nvds_mqtt_proto.h:81
nvds_msgapi_send_cb_t
void(* nvds_msgapi_send_cb_t)(void *user_ptr, NvDsMsgApiErrorType completion_flag)
Type definition for a "send" callback.
Definition: sources/includes/nvds_msgapi.h:76
create_enhanced_payload
std::string create_enhanced_payload(const uint8_t *payload, size_t nbuf)
serialize_protobuf_wrapper
std::string serialize_protobuf_wrapper(const ProtobufWrapper &wrapper)
my_publish_callback
void my_publish_callback(struct mosquitto *mosq, void *obj, int mid, int reason_code, const mosquitto_property *properties)
NvDsMqttClientHandle::connect_cb
nvds_msgapi_connect_cb_t connect_cb
Definition: sources/includes/nvds_mqtt_proto.h:82
DEFAULT_LOOP_TIMEOUT
#define DEFAULT_LOOP_TIMEOUT
Definition: sources/includes/nvds_mqtt_proto.h:47
nvds_utils.h
nvds_msgapi_subscribe_request_cb_t
void(* nvds_msgapi_subscribe_request_cb_t)(NvDsMsgApiErrorType flag, void *msg, int msg_len, char *topic, void *user_ptr)
Type definition for callback registered during subscribe.
Definition: sources/includes/nvds_msgapi.h:92
send_msg_info_t::send_callback
nvds_msgapi_send_cb_t send_callback
Definition: sources/includes/nvds_mqtt_proto.h:54