Skip to content
Snippets Groups Projects
Commit 06797745 authored by Rob Swindell's avatar Rob Swindell :speech_balloon:
Browse files

Add configuration for MQTT protocol version, username, password

Default MQTT version is 5
Default username, password are block. Up to 255 chars each.
parent 3fcfd219
No related branches found
No related tags found
1 merge request!463MRC mods by Codefenix (2024-10-20)
Pipeline #3438 passed
...@@ -242,7 +242,14 @@ int mqtt_connect(struct mqtt* mqtt, const char* bind_address) ...@@ -242,7 +242,14 @@ int mqtt_connect(struct mqtt* mqtt, const char* bind_address)
return MQTT_FAILURE; return MQTT_FAILURE;
#ifdef USE_MOSQUITTO #ifdef USE_MOSQUITTO
mosquitto_int_option(mqtt->handle, MOSQ_OPT_PROTOCOL_VERSION, MQTT_PROTOCOL_V5); char* username = mqtt->cfg->mqtt.username;
char* password = mqtt->cfg->mqtt.password;
if(*username == '\0')
username = NULL;
if(*password == '\0')
password = NULL;
mosquitto_int_option(mqtt->handle, MOSQ_OPT_PROTOCOL_VERSION, mqtt->cfg->mqtt.protocol_version);
mosquitto_username_pw_set(mqtt->handle, username, password);
return mosquitto_connect_bind(mqtt->handle, return mosquitto_connect_bind(mqtt->handle,
mqtt->cfg->mqtt.broker_addr, mqtt->cfg->mqtt.broker_addr,
mqtt->cfg->mqtt.broker_port, mqtt->cfg->mqtt.broker_port,
......
...@@ -367,8 +367,11 @@ struct mqtt_cfg { ...@@ -367,8 +367,11 @@ struct mqtt_cfg {
BOOL enabled; BOOL enabled;
char broker_addr[128]; char broker_addr[128];
uint16_t broker_port; uint16_t broker_port;
char username[256];
char password[256];
int keepalive; int keepalive;
int qos; int qos;
int protocol_version;
}; };
typedef struct typedef struct
......
...@@ -204,10 +204,13 @@ BOOL read_main_cfg(scfg_t* cfg, char* error, size_t maxerrlen) ...@@ -204,10 +204,13 @@ BOOL read_main_cfg(scfg_t* cfg, char* error, size_t maxerrlen)
/*****************/ /*****************/
section = iniGetParsedSection(sections, "mqtt", /* cut: */TRUE); section = iniGetParsedSection(sections, "mqtt", /* cut: */TRUE);
cfg->mqtt.enabled = iniGetBool(section, NULL, "enabled", FALSE); cfg->mqtt.enabled = iniGetBool(section, NULL, "enabled", FALSE);
SAFECOPY(cfg->mqtt.username, iniGetString(section, NULL, "username", "", value));
SAFECOPY(cfg->mqtt.password, iniGetString(section, NULL, "password", "", value));
SAFECOPY(cfg->mqtt.broker_addr, iniGetString(section, NULL, "broker_addr", "127.0.0.1", value)); SAFECOPY(cfg->mqtt.broker_addr, iniGetString(section, NULL, "broker_addr", "127.0.0.1", value));
cfg->mqtt.broker_port = iniGetUInt16(section, NULL, "broker_port", IPPORT_MQTT); cfg->mqtt.broker_port = iniGetUInt16(section, NULL, "broker_port", IPPORT_MQTT);
cfg->mqtt.keepalive = iniGetInteger(section, NULL, "keepalive", 10); cfg->mqtt.keepalive = iniGetInteger(section, NULL, "keepalive", 10);
cfg->mqtt.qos = iniGetInteger(section, NULL, "qos", 0); cfg->mqtt.qos = iniGetInteger(section, NULL, "qos", 0);
cfg->mqtt.protocol_version = iniGetInteger(section, NULL, "protocol_version", 5);
/***********/ /***********/
/* Modules */ /* Modules */
......
...@@ -212,8 +212,11 @@ BOOL write_main_cfg(scfg_t* cfg, int backup_level) ...@@ -212,8 +212,11 @@ BOOL write_main_cfg(scfg_t* cfg, int backup_level)
iniSetBool(&ini, name, "enabled", cfg->mqtt.enabled, NULL); iniSetBool(&ini, name, "enabled", cfg->mqtt.enabled, NULL);
iniSetString(&ini, name, "broker_addr", cfg->mqtt.broker_addr, NULL); iniSetString(&ini, name, "broker_addr", cfg->mqtt.broker_addr, NULL);
iniSetUInt16(&ini, name, "broker_port", cfg->mqtt.broker_port, NULL); iniSetUInt16(&ini, name, "broker_port", cfg->mqtt.broker_port, NULL);
iniSetInteger(&ini, name, "protocol_version", cfg->mqtt.protocol_version, NULL);
iniSetInteger(&ini, name, "keepalive", cfg->mqtt.keepalive, NULL); iniSetInteger(&ini, name, "keepalive", cfg->mqtt.keepalive, NULL);
iniSetInteger(&ini, name, "qos", cfg->mqtt.qos, NULL); iniSetInteger(&ini, name, "qos", cfg->mqtt.qos, NULL);
iniSetString(&ini, name, "username", cfg->mqtt.username, NULL);
iniSetString(&ini, name, "password", cfg->mqtt.password, NULL);
} }
{ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment