cloud/mqtt_demo/main.c

/*
* EVALUATION AND USE OF THIS SOFTWARE IS SUBJECT TO THE TERMS AND
* CONDITIONS OF THE CONTROLLING LICENSE AGREEMENT FOUND AT LICENSE.md
* IN THIS SDK. IF YOU DO NOT AGREE TO THE LICENSE TERMS AND CONDITIONS,
* PLEASE RETURN ALL SOURCE FILES TO SILICON LABORATORIES.
* (c) Copyright 2018, Silicon Laboratories Inc. All rights reserved.
*/
#include "gos.h"
#include "mqtt.h"
#include "mqtt_demo_common.h"
#include "nvm_settings.h"
#include "string.h"
// Length of the buffer for a received message payload
#define MESSAGE_BUFFER_LENGTH 256
// 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);
return;
}
// 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");
return;
}
// 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 = strlen(settings->will_message);
if ( GOS_FAILED( result, gos_mqtt_init( ) ) )
{
GOS_LOG( "Failed to initialize MQTT: %d", result );
return;
}
// Set the MQTT client ID.
// 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
if(example_app_util_network_up(GOS_INTERFACE_ANY, false, NULL) != GOS_SUCCESS)
{
GOS_LOG( "Exiting example app" );
return;
}
// Print helpful information
GOS_LOG("MQTT Demo Application Started:");
GOS_LOG(" - List all variables : get mqtt");
GOS_LOG(" - Set variable mqtt.<var> to value <val> : 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 currently connect to broker : mqtt_is_connected");
}
void publish_handler(gos_mqtt_handle_t handle, gos_mqtt_message_t* message)
{
char message_buffer[MESSAGE_BUFFER_LENGTH];
uint32_t payload_length = MIN( MESSAGE_BUFFER_LENGTH - 1, message->payload_length );
strncpy( message_buffer, (char *) message->payload, payload_length );
message_buffer[payload_length] = '\0';
GOS_LOG( "[MQTT] Received from topic: '%s', Message: '%s'", message->topic, message_buffer );
}