Skip to content

Commit

Permalink
MQTT -- add code to deal with empty payloads by adding a dummy payloa…
Browse files Browse the repository at this point in the history
…d to empty messages by default.
  • Loading branch information
mikebrady committed Feb 7, 2022
1 parent 8b483c0 commit 3d720d6
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
1 change: 1 addition & 0 deletions common.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ typedef struct {
int mqtt_publish_parsed;
int mqtt_publish_cover;
int mqtt_enable_remote;
char *mqtt_empty_payload_substitute;
#endif
uint8_t hw_addr[8]; // only needs 6 but 8 is handy when converting this to a number
int port;
Expand Down
12 changes: 10 additions & 2 deletions mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,20 @@ void on_connect(struct mosquitto *mosq, __attribute__((unused)) void *userdata,
}

// helper function to publish under a topic and automatically append the main topic
void mqtt_publish(char *topic, char *data, uint32_t length) {
void mqtt_publish(char *topic, char *data_in, uint32_t length_in) {
char *data = data_in;
uint32_t length = length_in;

if ((length == 0) && (config.mqtt_empty_payload_substitute != NULL)) {
length = strlen(config.mqtt_empty_payload_substitute);
data = config.mqtt_empty_payload_substitute;
}

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(2, "[MQTT]: publishing under %s", fulltopic);

int rc;
if ((rc = mosquitto_publish(global_mosq, NULL, fulltopic, length, data, 0, 0)) !=
MOSQ_ERR_SUCCESS) {
Expand Down
10 changes: 9 additions & 1 deletion scripts/shairport-sync.conf
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,12 @@ metadata =
// How to enable the MQTT-metadata/remote-service
// For this section to be operative, Shairport Sync must be built with the following configuration flag:
// --with-mqtt-client

// Note that, for compatability with many MQTT brokers and applications,
// every message that has no extra data is given a
// payload consisting of the string "--".
// You can change this or you can enable empty payloads -- see below.

mqtt =
{
// enabled = "no"; // set this to yes to enable the mqtt-metadata-service
Expand All @@ -238,8 +244,10 @@ mqtt =
// topic = NULL; //MQTT topic where this instance of shairport-sync should publish. If not set, the general.name value is used.
// publish_raw = "no"; //whether to publish all available metadata under the codes given in the 'metadata' docs.
// publish_parsed = "no"; //whether to publish a small (but useful) subset of metadata under human-understandable topics
// empty_payload_substitute = "--"; // MQTT messages with empty payloads often are invisible or have special significance to MQTT brokers and readers.
// To avoid empty payload problems, the string here is used instead of any empty payload. Set it to the empty string -- "" -- to leave the payload empty.
// Currently published topics:artist,album,title,genre,format,songalbum,volume,client_ip,
// Additionally, empty messages at the topics play_start,play_end,play_flush,play_resume are published
// Additionally, messages at the topics play_start,play_end,play_flush,play_resume are published
// publish_cover = "no"; //whether to publish the cover over mqtt in binary form. This may lead to a bit of load on the broker
// enable_remote = "no"; //whether to remote control via MQTT. RC is available under `topic`/remote.
// Available commands are "command", "beginff", "beginrew", "mutetoggle", "nextitem", "previtem", "pause", "playpause", "play", "stop", "playresume", "shuffle_songs", "volumedown", "volumeup"
Expand Down
15 changes: 14 additions & 1 deletion shairport.c
Original file line number Diff line number Diff line change
Expand Up @@ -1181,6 +1181,14 @@ int parse_options(int argc, char **argv) {
die("You need to have metadata.include_cover_art enabled in order to use mqtt.publish_cover");
}
config_set_lookup_bool(config.cfg, "mqtt.enable_remote", &config.mqtt_enable_remote);
if (config_lookup_string(config.cfg, "mqtt.empty_payload_substitute", &str)) {
if (strlen(str) == 0)
config.mqtt_empty_payload_substitute = NULL;
else
config.mqtt_empty_payload_substitute = strdup(str);
} else {
config.mqtt_empty_payload_substitute = strdup("--");
}
#ifndef CONFIG_AVAHI
if (config.mqtt_enable_remote) {
die("You have enabled MQTT remote control which requires shairport-sync to be built with "
Expand All @@ -1191,7 +1199,7 @@ int parse_options(int argc, char **argv) {
#endif

#ifdef CONFIG_AIRPLAY_2
int64_t aid;
long long aid;

// replace the airplay_device_id with this, if provided
if (config_lookup_int64(config.cfg, "general.airplay_device_id", &aid)) {
Expand Down Expand Up @@ -1510,6 +1518,11 @@ void exit_function() {
if (config.service_name)
free(config.service_name);

#ifdef CONFIG_MQTT
if (config.mqtt_empty_payload_substitute)
free(config.mqtt_empty_payload_substitute);
#endif

#ifdef CONFIG_CONVOLUTION
if (config.convolution_ir_file)
free(config.convolution_ir_file);
Expand Down

0 comments on commit 3d720d6

Please sign in to comment.