From: Till Zimmermann Date: Wed, 20 Jun 2018 16:58:34 +0000 (+0200) Subject: Fixed not connected bug, added version string X-Git-Tag: 3.2RC12~4^2~8^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F716%2Fhead;p=thirdparty%2Fshairport-sync.git Fixed not connected bug, added version string --- diff --git a/common.c b/common.c index 306b3c2f..118b6c0f 100644 --- a/common.c +++ b/common.c @@ -1128,6 +1128,9 @@ char *get_version_string() { #ifdef CONFIG_METADATA strcat(version_string, "-metadata"); #endif +#ifdef CONFIG_MQTT + strcat(version_string, "-mqtt"); +#endif #ifdef HAVE_DBUS strcat(version_string, "-dbus"); #endif diff --git a/mqtt.c b/mqtt.c index 85efcf6e..c08a4318 100644 --- a/mqtt.c +++ b/mqtt.c @@ -17,6 +17,7 @@ //this holds the mosquitto client struct mosquitto *global_mosq = NULL; char *topic = NULL; +int connected = 0; //mosquitto logging void _cb_log( __attribute__((unused)) struct mosquitto *mosq, @@ -74,12 +75,14 @@ void on_message( __attribute__((unused)) struct mosquitto* mosq, void on_disconnect( __attribute__((unused)) struct mosquitto* mosq, __attribute__((unused)) void* userdata, __attribute__((unused)) int rc){ + connected = 0; debug(1, "[MQTT]: disconnected"); } void on_connect(struct mosquitto* mosq, __attribute__((unused)) void* userdata, __attribute__((unused)) int rc){ + connected = 1; debug(1, "[MQTT]: connected"); //subscribe if requested @@ -95,11 +98,26 @@ void mqtt_publish(char* topic, char* data, uint32_t length){ char fulltopic[strlen(config.mqtt_topic)+strlen(topic)+3]; snprintf(fulltopic, strlen(config.mqtt_topic)+strlen(topic)+2, "%s/%s", config.mqtt_topic, topic); debug(1, "[MQTT]: publishing under %s",fulltopic); - mosquitto_publish(global_mosq, NULL, fulltopic, length, data, 0, 0); + + int rc; + if((rc=mosquitto_publish(global_mosq, NULL, fulltopic, length, data, 0, 0))!=MOSQ_ERR_SUCCESS) { + switch(rc){ + case MOSQ_ERR_NO_CONN: + debug(1, "[MQTT]: Publish failed: not connected to broker"); + break; + default: + debug(1, "[MQTT]: Publish failed: unknown error"); + break; + } + } } //handler for incoming metadata void mqtt_process_metadata(uint32_t type, uint32_t code, char *data, uint32_t length){ + if(global_mosq==NULL || connected!=1){ + debug(3, "[MQTT]: Client not connected, skipping metadata handling"); + return; + } if(config.mqtt_publish_raw){ uint32_t val; char topic[] = "____/____";