From a9b3a2057be33d09cbf27e012e9a3bd385686273 Mon Sep 17 00:00:00 2001 From: gc87 Date: Tue, 18 Jan 2022 18:31:12 +0800 Subject: [PATCH] Modify libbase's compilation configuration Add the nng-mqtt switch to the compilation process of libbase --- CMakeLists.txt | 12 ++++++++++++ src/connection/mqtt_client_intf.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 38d83d67..e0a706d1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -152,6 +152,14 @@ set(NEURON_BASE_SOURCES src/utils/uuid4.c src/config.c) +if(USE_NNG_MQTT) + message("-- USE_NNG_MQTT is defined") + add_definitions(-DUSE_NNG_MQTT) + set(NEURON_BASE_SOURCES + ${NEURON_BASE_SOURCES} + src/connection/mqtt_nng_client.c) +endif() + set(NEURON_BASE_INCLUDE_DIRECTORIES include src) add_library(neuron-base SHARED) target_sources(neuron-base PRIVATE ${NEURON_BASE_SOURCES} ${NEURON_SRC_PARSE} ${NEURON_SRC_SCHEMA}) @@ -162,6 +170,10 @@ target_compile_definitions(neuron-base PRIVATE MQTT_USE_BIO) target_link_libraries(neuron-base vector-static csptr jansson crypto ssl mqttc yaml jwt ${CMAKE_THREAD_LIBS_INIT}) +if(USE_NNG_MQTT) + target_link_libraries(neuron-base mbedtls mbedx509 mbedcrypto nng) +endif() + #copy file for run and unit test file(COPY ${CMAKE_SOURCE_DIR}/neuron.yaml DESTINATION ${CMAKE_BINARY_DIR}) file(COPY ${CMAKE_SOURCE_DIR}/plugin_param_schema.json DESTINATION ${CMAKE_BINARY_DIR}) diff --git a/src/connection/mqtt_client_intf.c b/src/connection/mqtt_client_intf.c index f3cf7947..f2c8eba5 100644 --- a/src/connection/mqtt_client_intf.c +++ b/src/connection/mqtt_client_intf.c @@ -18,41 +18,70 @@ **/ #include "connection/mqtt_client_intf.h" +#ifdef USE_NNG_MQTT +#include "mqtt_nng_client.h" +#else #include "mqtt_c_client.h" +#endif neu_err_code_e neu_mqtt_client_open(neu_mqtt_client_t * p_client, const neu_mqtt_option_t *option, void * context) { +#ifdef USE_NNG_MQTT + return mqtt_nng_client_open((mqtt_nng_client_t **) p_client, option, + context); +#else return mqtt_c_client_open((mqtt_c_client_t **) p_client, option, context); +#endif } neu_err_code_e neu_mqtt_client_is_connected(neu_mqtt_client_t client) { +#ifdef USE_NNG_MQTT + return mqtt_nng_client_is_connected(client); +#else return mqtt_c_client_is_connected(client); +#endif } neu_err_code_e neu_mqtt_client_subscribe(neu_mqtt_client_t client, const char *topic, const int qos, neu_subscribe_handle handle) { +#ifdef USE_NNG_MQTT + return mqtt_nng_client_subscribe(client, topic, qos, handle); +#else return mqtt_c_client_subscribe(client, topic, qos, handle); +#endif } neu_err_code_e neu_mqtt_client_unsubscribe(neu_mqtt_client_t client, const char * topic) { +#ifdef USE_NNG_MQTT + return mqtt_nng_client_unsubscribe(client, topic); +#else return mqtt_c_client_unsubscribe(client, topic); +#endif } neu_err_code_e neu_mqtt_client_publish(neu_mqtt_client_t client, const char *topic, int qos, unsigned char *payload, size_t len) { +#ifdef USE_NNG_MQTT + return mqtt_nng_client_publish(client, topic, qos, payload, len); +#else return mqtt_c_client_publish(client, topic, qos, payload, len); +#endif } neu_err_code_e neu_mqtt_client_close(neu_mqtt_client_t client) { +#ifdef USE_NNG_MQTT + return mqtt_nng_client_close(client); +#else return mqtt_c_client_close(client); +#endif }