sqlite: Save/restore binary properties.

pull/2558/merge
Roger A. Light 3 years ago
parent dd30dc8bd1
commit d4960d0ead

@ -27,7 +27,7 @@ all : ${ALL_DEPS}
binary : ${PLUGIN_NAME}.so
${PLUGIN_NAME}.so : ${OBJS}
${CROSS_COMPILE}${CC} $(PLUGIN_LDFLAGS) -fPIC -shared $^ -o $@ -lsqlite3
${CROSS_COMPILE}${CC} $(PLUGIN_LDFLAGS) -fPIC -shared $^ -o $@ -lsqlite3 -lcjson
clients.o : clients.c persist_sqlite.h
${CROSS_COMPILE}${CC} $(LOCAL_CPPFLAGS) $(PLUGIN_CPPFLAGS) $(PLUGIN_CFLAGS) -c $< -o $@

@ -33,6 +33,8 @@ static char *properties_to_json(const mosquitto_property *properties)
uint8_t i8;
uint16_t i16;
uint32_t i32;
void *binval;
uint16_t len;
int propid;
if(!properties) return NULL;
@ -130,6 +132,24 @@ static char *properties_to_json(const mosquitto_property *properties)
case MQTT_PROP_CORRELATION_DATA:
case MQTT_PROP_AUTHENTICATION_DATA:
/* bin */
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';
if(cJSON_AddStringToObject(obj, "value", hexval) == NULL){
free(hexval);
cJSON_Delete(array);
return NULL;
}
free(hexval);
break;
case MQTT_PROP_USER_PROPERTY:

@ -26,11 +26,41 @@ Contributors:
#include "mqtt_protocol.h"
#include "persist_sqlite.h"
static uint8_t hex2nibble(char c)
{
switch(c){
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
case 'A': return 10;
case 'a': return 10;
case 'B': return 11;
case 'b': return 11;
case 'C': return 12;
case 'c': return 12;
case 'D': return 13;
case 'd': return 13;
case 'E': return 14;
case 'e': return 14;
case 'F': return 15;
case 'f': return 15;
default: return 0;
}
}
static mosquitto_property *json_to_properties(const char *json)
{
mosquitto_property *properties = NULL;
cJSON *array, *obj, *j_id, *j_value, *j_name;
int propid, proptype;
size_t slen;
if(!json) return NULL;
@ -70,6 +100,13 @@ static mosquitto_property *json_to_properties(const char *json)
mosquitto_property_add_varint(&properties, propid, (uint32_t)j_value->valueint);
break;
case MQTT_PROP_TYPE_BINARY:
if(!cJSON_IsString(j_value)) continue;
slen = strlen(j_value->valuestring);
if(slen/2 > UINT16_MAX) continue;
for(size_t i=0; i<slen; i+=2){
((uint8_t *)j_value->valuestring)[i/2] = (uint8_t)(hex2nibble(j_value->valuestring[i])<<4) + hex2nibble(j_value->valuestring[i+1]);
}
mosquitto_property_add_binary(&properties, propid, (uint8_t *)j_value->valuestring, (uint16_t)(slen/2));
break;
case MQTT_PROP_TYPE_STRING:
if(!cJSON_IsString(j_value)) continue;

Loading…
Cancel
Save