Move properties_to_json to libmosquitto_common

This commit is contained in:
Roger A. Light
2024-06-18 16:29:05 +01:00
parent 7faad7ca78
commit 2f32fcd949
8 changed files with 185 additions and 103 deletions

View File

@@ -232,7 +232,7 @@ ifeq ($(WITH_SHARED_LIBRARIES),yes)
else else
LIBMOSQ:=${R}/lib/libmosquitto.a LIBMOSQ:=${R}/lib/libmosquitto.a
endif endif
LIBMOSQ_COMMON:=-Wl,--whole-archive ${R}/libcommon/libmosquitto_common.a -Wl,--no-whole-archive LIBMOSQ_COMMON:=-Wl,--whole-archive ${R}/libcommon/libmosquitto_common.a -Wl,--no-whole-archive -lcjson
ifeq ($(WITH_TLS),yes) ifeq ($(WITH_TLS),yes)
LOCAL_CPPFLAGS+=-DWITH_TLS LOCAL_CPPFLAGS+=-DWITH_TLS

View File

@@ -37,6 +37,7 @@ extern "C" {
#endif #endif
#include <mosquitto/libcommon_base64.h> #include <mosquitto/libcommon_base64.h>
#include <mosquitto/libcommon_cjson.h>
#include <mosquitto/libcommon_file.h> #include <mosquitto/libcommon_file.h>
#include <mosquitto/libcommon_memory.h> #include <mosquitto/libcommon_memory.h>
#include <mosquitto/libcommon_password.h> #include <mosquitto/libcommon_password.h>

View File

@@ -0,0 +1,38 @@
/*
Copyright (c) 2010-2021 Roger Light <roger@atchoo.org>
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License 2.0
and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at
https://www.eclipse.org/legal/epl-2.0/
and the Eclipse Distribution License is available at
http://www.eclipse.org/org/documents/edl-v10.php.
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
Contributors:
Roger Light - initial implementation and documentation.
*/
#ifndef MOSQUITTO_LIBCOMMON_CJSON_H
#define MOSQUITTO_LIBCOMMON_CJSON_H
/*
* File: mosquitto/libcommon_cjson.h
*
* This header contains functions for handling cJSON objects
*/
#ifdef __cplusplus
extern "C" {
#endif
#include <cjson/cJSON.h>
libmosqcommon_EXPORT cJSON *mosquitto_properties_to_json(const mosquitto_property *properties);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -1,5 +1,6 @@
set(C_SRC set(C_SRC
base64_common.c base64_common.c
cjson_common.c
file_common.c file_common.c
memory_common.c memory_common.c
mqtt_common.c mqtt_common.c
@@ -13,6 +14,7 @@ set(C_SRC
"${mosquitto_SOURCE_DIR}/include/mosquitto/libcommon.h" "${mosquitto_SOURCE_DIR}/include/mosquitto/libcommon.h"
"${mosquitto_SOURCE_DIR}/include/mosquitto/libcommon_base64.h" "${mosquitto_SOURCE_DIR}/include/mosquitto/libcommon_base64.h"
"${mosquitto_SOURCE_DIR}/include/mosquitto/libcommon_cjson.h"
"${mosquitto_SOURCE_DIR}/include/mosquitto/libcommon_file.h" "${mosquitto_SOURCE_DIR}/include/mosquitto/libcommon_file.h"
"${mosquitto_SOURCE_DIR}/include/mosquitto/libcommon_memory.h" "${mosquitto_SOURCE_DIR}/include/mosquitto/libcommon_memory.h"
"${mosquitto_SOURCE_DIR}/include/mosquitto/libcommon_properties.h" "${mosquitto_SOURCE_DIR}/include/mosquitto/libcommon_properties.h"
@@ -42,6 +44,7 @@ target_include_directories(libmosquitto_common
target_link_libraries(libmosquitto_common target_link_libraries(libmosquitto_common
PUBLIC PUBLIC
config-header config-header
cJSON
) )
if(ARGON2_FOUND) if(ARGON2_FOUND)

View File

@@ -25,6 +25,7 @@ endif
OBJS= \ OBJS= \
base64_common.o \ base64_common.o \
cjson_common.o \
file_common.o \ file_common.o \
memory_common.o \ memory_common.o \
mqtt_common.o \ mqtt_common.o \

137
libcommon/cjson_common.c Normal file
View File

@@ -0,0 +1,137 @@
/*
Copyright (c) 2009-2021 Roger Light <roger@atchoo.org>
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License 2.0
and Eclipse Distribution License v1.0 which accompany this distribution.
The Eclipse Public License is available at
https://www.eclipse.org/legal/epl-2.0/
and the Eclipse Distribution License is available at
http://www.eclipse.org/org/documents/edl-v10.php.
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
Contributors:
Roger Light - initial implementation and documentation.
*/
#include "config.h"
#include <cjson/cJSON.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mosquitto.h"
cJSON *mosquitto_properties_to_json(const mosquitto_property *properties)
{
cJSON *array, *obj;
char *name, *value;
uint8_t i8;
uint16_t len;
int propid;
if(!properties) return NULL;
array = cJSON_CreateArray();
if(!array) return NULL;
do{
propid = mosquitto_property_identifier(properties);
obj = cJSON_CreateObject();
if(!obj){
cJSON_Delete(array);
return NULL;
}
cJSON_AddItemToArray(array, obj);
/* identifier, (key), value */
if(cJSON_AddStringToObject(obj,
"identifier",
mosquitto_property_identifier_to_string(propid)) == NULL
){
cJSON_Delete(array);
return NULL;
}
switch(propid){
case MQTT_PROP_PAYLOAD_FORMAT_INDICATOR:
/* byte */
mosquitto_property_read_byte(properties, propid, &i8, false);
if(cJSON_AddNumberToObject(obj, "value", i8) == NULL){
cJSON_Delete(array);
return NULL;
}
break;
case MQTT_PROP_CONTENT_TYPE:
case MQTT_PROP_RESPONSE_TOPIC:
case MQTT_PROP_REASON_STRING:
/* str */
if(mosquitto_property_read_string(properties, propid, &value, false) == NULL){
cJSON_Delete(array);
return NULL;
}
if(cJSON_AddStringToObject(obj, "value", value) == NULL){
free(value);
cJSON_Delete(array);
return NULL;
}
free(value);
break;
case MQTT_PROP_CORRELATION_DATA:
{
/* bin */
void *binval = NULL;
mosquitto_property_read_binary(properties, propid, &binval, &len, false);
char *hexval = malloc(2*(size_t)len + 1);
if(!hexval){
free(binval);
cJSON_Delete(array);
return NULL;
}
for(int i=0; i<len; i++){
sprintf(&hexval[i*2], "%02X", ((uint8_t *)binval)[i]);
}
hexval[2*len] = '\0';
free(binval);
if(cJSON_AddStringToObject(obj, "value", hexval) == NULL){
free(hexval);
cJSON_Delete(array);
return NULL;
}
free(hexval);
}
break;
case MQTT_PROP_USER_PROPERTY:
/* pair */
mosquitto_property_read_string_pair(properties, propid, &name, &value, false);
if(cJSON_AddStringToObject(obj, "name", name) == NULL
|| cJSON_AddStringToObject(obj, "value", value) == NULL){
free(name);
free(value);
cJSON_Delete(array);
return NULL;
}
free(name);
free(value);
break;
default:
break;
}
properties = mosquitto_property_next(properties);
}while(properties);
return array;
}

View File

@@ -4,7 +4,7 @@ include ${R}/config.mk
PLUGIN_NAME=mosquitto_persist_sqlite PLUGIN_NAME=mosquitto_persist_sqlite
LOCAL_CFLAGS+= LOCAL_CFLAGS+=
LOCAL_CPPFLAGS+=-I${R}/src/ -I${R}/plugins/common LOCAL_CPPFLAGS+=-I${R}/src/ -I${R}/plugins/common
LOCAL_LIBADD+=-lsqlite3 -lcjson LOCAL_LIBADD+=-lsqlite3 ${LIBMOSQ_COMMON}
LOCAL_LDFLAGS+= LOCAL_LDFLAGS+=
OBJS = \ OBJS = \

View File

@@ -22,115 +22,17 @@ Contributors:
#include <stdlib.h> #include <stdlib.h>
#include <cjson/cJSON.h> #include <cjson/cJSON.h>
#include "mosquitto/mqtt_protocol.h"
#include "mosquitto.h" #include "mosquitto.h"
#include "mosquitto/broker.h"
#include "persist_sqlite.h" #include "persist_sqlite.h"
static char *properties_to_json(const mosquitto_property *properties) static char *properties_to_json(const mosquitto_property *properties)
{ {
cJSON *array, *obj; cJSON *array;
char *json_str, *name, *value; char *json_str;
uint8_t i8;
uint16_t len;
int propid;
if(!properties) return NULL; array = mosquitto_properties_to_json(properties);
array = cJSON_CreateArray();
if(!array) return NULL; if(!array) return NULL;
do{
propid = mosquitto_property_identifier(properties);
obj = cJSON_CreateObject();
if(!obj){
cJSON_Delete(array);
return NULL;
}
cJSON_AddItemToArray(array, obj);
/* identifier, (key), value */
if(cJSON_AddStringToObject(obj,
"identifier",
mosquitto_property_identifier_to_string(propid)) == NULL
){
cJSON_Delete(array);
return NULL;
}
switch(propid){
case MQTT_PROP_PAYLOAD_FORMAT_INDICATOR:
/* byte */
mosquitto_property_read_byte(properties, propid, &i8, false);
if(cJSON_AddNumberToObject(obj, "value", i8) == NULL){
cJSON_Delete(array);
return NULL;
}
break;
case MQTT_PROP_CONTENT_TYPE:
case MQTT_PROP_RESPONSE_TOPIC:
case MQTT_PROP_REASON_STRING:
/* str */
if(mosquitto_property_read_string(properties, propid, &value, false) == NULL){
cJSON_Delete(array);
return NULL;
}
if(cJSON_AddStringToObject(obj, "value", value) == NULL){
free(value);
cJSON_Delete(array);
return NULL;
}
free(value);
break;
case MQTT_PROP_CORRELATION_DATA:
{
/* bin */
void *binval = NULL;
mosquitto_property_read_binary(properties, propid, &binval, &len, false);
char *hexval = malloc(2*(size_t)len + 1);
if(!hexval){
free(binval);
cJSON_Delete(array);
return NULL;
}
for(int i=0; i<len; i++){
sprintf(&hexval[i*2], "%02X", ((uint8_t *)binval)[i]);
}
hexval[2*len] = '\0';
free(binval);
if(cJSON_AddStringToObject(obj, "value", hexval) == NULL){
free(hexval);
cJSON_Delete(array);
return NULL;
}
free(hexval);
}
break;
case MQTT_PROP_USER_PROPERTY:
/* pair */
mosquitto_property_read_string_pair(properties, propid, &name, &value, false);
if(cJSON_AddStringToObject(obj, "name", name) == NULL
|| cJSON_AddStringToObject(obj, "value", value) == NULL){
free(name);
free(value);
cJSON_Delete(array);
return NULL;
}
free(name);
free(value);
break;
default:
break;
}
properties = mosquitto_property_next(properties);
}while(properties);
json_str = cJSON_PrintUnformatted(array); json_str = cJSON_PrintUnformatted(array);
cJSON_Delete(array); cJSON_Delete(array);
return json_str; return json_str;