* # License
* Copyright 2019 Silicon Laboratories Inc.
* The licensor of this software is Silicon Laboratories Inc. Your use of this
* software is governed by the terms of Silicon Labs Master Software License
* Agreement (MSLA) available at
* This
* software is distributed to you in Source Code format and is governed by the
* sections of the MSLA applicable to Source Code.
#include "gos.h"
#include "mqtt.h"
#include "mqtt_demo_common.h"
#include "nvm_settings.h"
#include "string.h"
#include "example_app_util.h"
#define APPLICATION_START_LINE "\r\nMQTT Demo Application Started:"
// Variables used externally by mqtt_cli_variables.c to connection configuration
gos_mqtt_configuration_t* mqtt_configuration;
gos_mqtt_will_t* mqtt_will;
mqtt_demo_settings_t* settings;
// Handle for broker connection
gos_mqtt_handle_t mqtt_handle;
void gos_app_init(void)
gos_result_t result;
// Retrieve the MQTT settings from non-volatile memory (NVM)
if (GOS_FAILED(result, GOS_NVM_GET(MQTT, DEMO, settings)))
GOS_LOG("Error retrieving configuration from NVM, error code: %d", result);
// Check the magic number has been set to avoid attempting to initialize using random values
if( settings->magic != MQTT_DEMO_SETTINGS_MAGIC )
GOS_LOG("NVM settings uninitialized");
// Setup variables used by the CLI for connection configuration
mqtt_configuration = &(settings->configuration);
mqtt_will = &(settings->will);
mqtt_will->payload = (uint8_t*) settings->will_message;
mqtt_will->payload_length = strnlen(settings->will_message, MAX_WILL_MESSAGE_LENGTH);
if ( GOS_FAILED( result, gos_mqtt_init( ) ) )
GOS_LOG( "Failed to initialize MQTT: %d", result );
// Set the MQTT client ID if not already set
if(strnlen(settings->configuration.client_id, MAX_CLIENT_ID_LENGTH) == 0)
// This must be unique so append last 3 digits of our MAC address to avoid name collisions
char mac_str[20];
gos_settings_get_print_str("wlan.mac", mac_str, sizeof(mac_str));
sprintf(settings->configuration.client_id, "gecko-os-mqtt-%c%c%c", mac_str[13], mac_str[15], mac_str[16]);
// Bring up the network interface
GOS_LOG( "Exiting example app" );
// Print helpful information
GOS_LOG(" - List all variables : get mqtt");
GOS_LOG(" - Set variable : set mqtt.<var> <val>");
GOS_LOG(" - Connect to broker : mqtt_connect");
GOS_LOG(" - Disconnect from broker : mqtt_disconnect");
GOS_LOG(" - Subscribe to topic : mqtt_subscribe <topic> [qos_level]");
GOS_LOG(" - Unsubscribe from topic : mqtt_unsubscribe <topic>");
GOS_LOG(" - Publish to topic : mqtt_publish <topic> <message> [qos_level] [retained]");
GOS_LOG(" - Check if connected : mqtt_is_connected");
GOS_LOG("MQTT variables description:");
GOS_LOG(" - mqtt.auto_reconnect : Automatically attempt to reconnect to the broker if the underlying network connection becomes disconnected");
GOS_LOG(" - mqtt.broker_hostname : Host or IP address of the broker");
GOS_LOG(" - mqtt.broker_port : Port to use for the broker connection (typically 1883, or 8883 for TLS)");
GOS_LOG(" - mqtt.clean_session : When true the broker deletes any previous undelivered messages and subscription information when connecting");
GOS_LOG(" - mqtt.client_id : A client ID for this connection (must be unique for the broker). Must be null terminated");
GOS_LOG(" - mqtt.tx_buffer_size : Size of the send/publish buffer (dynamically allocated, 256bytes to 64kbytes)");
GOS_LOG(" - mqtt.rx_buffer_size : Size of the receive/read buffer (dynamically allocated, 256bytes to 64kbytes)");
GOS_LOG(" - mqtt.keep_alive_interval : MQTT keep alive interval in seconds");
GOS_LOG(" - mqtt.password : Optional password for this connection. Must be null terminated");
GOS_LOG(" - mqtt.username : Optional username for this connection. Must be null terminated");
GOS_LOG(" - mqtt.use_tls : If the connection should use TLS instead of TCP");
GOS_LOG(" - mqtt.use_secure_element : If the connection should use the secure element to supply TLS credentials");
GOS_LOG(" - mqtt.version : MQTT protocol to use for this connection, '3.1' or '3.1.1' are supported");
GOS_LOG(" - mqtt.will.message : MQTT will message to be sent. Must be null terminated");
GOS_LOG(" - mqtt.will.qos_level : MQTT QoS level for the will message");
GOS_LOG(" - mqtt.will.retained : If the will message should be retained by the broker");
GOS_LOG(" - mqtt.will.topic : Topic the will message should be published to");
void default_incoming_message_handler(gos_mqtt_handle_t handle, gos_mqtt_message_t* message)
GOS_LOG("MQTT Message Received");
gos_write_log(message->topic, strlen(message->topic));
gos_write_log((char*)message->payload, message->payload_length);