Skip to content
Snippets Groups Projects
Commit d82d729b 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 2afaf9f8
No related branches found
No related tags found
No related merge requests found
......@@ -242,7 +242,14 @@ int mqtt_connect(struct mqtt* mqtt, const char* bind_address)
return MQTT_FAILURE;
#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,
mqtt->cfg->mqtt.broker_addr,
mqtt->cfg->mqtt.broker_port,
......
......@@ -367,8 +367,11 @@ struct mqtt_cfg {
BOOL enabled;
char broker_addr[128];
uint16_t broker_port;
char username[256];
char password[256];
int keepalive;
int qos;
int protocol_version;
};
typedef struct
......
......@@ -204,10 +204,13 @@ BOOL read_main_cfg(scfg_t* cfg, char* error, size_t maxerrlen)
/*****************/
section = iniGetParsedSection(sections, "mqtt", /* cut: */TRUE);
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));
cfg->mqtt.broker_port = iniGetUInt16(section, NULL, "broker_port", IPPORT_MQTT);
cfg->mqtt.keepalive = iniGetInteger(section, NULL, "keepalive", 10);
cfg->mqtt.qos = iniGetInteger(section, NULL, "qos", 0);
cfg->mqtt.protocol_version = iniGetInteger(section, NULL, "protocol_version", 5);
/***********/
/* Modules */
......
......@@ -212,8 +212,11 @@ BOOL write_main_cfg(scfg_t* cfg, int backup_level)
iniSetBool(&ini, name, "enabled", cfg->mqtt.enabled, NULL);
iniSetString(&ini, name, "broker_addr", cfg->mqtt.broker_addr, 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, "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.
Finish editing this message first!
Please register or to comment