cloud/mqtt_demo/mqtt_cli_variables.c

/*******************************************************************************
* # License
* Copyright 2019 Silicon Laboratories Inc. www.silabs.com
*******************************************************************************
*
* 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
* www.silabs.com/about-us/legal/master-software-license-agreement. 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_demo_common.h"
// Setters and getters for connection configuration
GOS_CMD_CREATE_GETTER_SETTER(mqtt, auto_reconnect, "mqtt.auto_reconnect", SC3('m', 'q', 'a', 'r'), 0, 1);
GOS_CMD_CREATE_GETTER_SETTER(mqtt, broker_hostname, "mqtt.broker_hostname", SC3('m', 'q', 'b', 'h'), 0, 1);
GOS_CMD_CREATE_GETTER_SETTER(mqtt, broker_port, "mqtt.broker_port", SC3('m', 'q', 'b', 'p'), 0, 1);
GOS_CMD_CREATE_GETTER_SETTER(mqtt, clean_session, "mqtt.clean_session", SC3('m', 'q', 'c', 's'), 0, 1);
GOS_CMD_CREATE_GETTER_SETTER(mqtt, client_id, "mqtt.client_id", SC3('m', 'q', 'c', 'i'), 0, 1);
GOS_CMD_CREATE_GETTER_SETTER(mqtt, keep_alive_interval, "mqtt.keep_alive_interval", SC3('m', 'q', 'k', 'a'), 0, 1);
GOS_CMD_CREATE_GETTER_SETTER(mqtt, tx_buffer_size, "mqtt.tx_buffer_size", SC3('m', 'q', 't', 'b'), 0, 1);
GOS_CMD_CREATE_GETTER_SETTER(mqtt, rx_buffer_size, "mqtt.rx_buffer_size", SC3('m', 'q', 'r', 'b'), 0, 1);
GOS_CMD_CREATE_GETTER_SETTER(mqtt, password, "mqtt.password", SC3('m', 'q', 'p', 'w'), 0, 1);
GOS_CMD_CREATE_GETTER_SETTER(mqtt, username, "mqtt.username", SC3('m', 'q', 'u', 'n'), 0, 1);
GOS_CMD_CREATE_GETTER_SETTER(mqtt, use_tls, "mqtt.use_tls", SC3('m', 'q', 'u', 't'), 0, 1);
GOS_CMD_CREATE_GETTER_SETTER(mqtt, use_secure_element, "mqtt.use_secure_element", SC3('m', 'q', 'u', 's'), 0, 1);
GOS_CMD_CREATE_GETTER_SETTER(mqtt, mqtt_version, "mqtt.version", SC3('m', 'q', 'v', 'e'), 0, 1);
GOS_CMD_CREATE_GETTER_SETTER(mqtt, will_message, "mqtt.will.message", SC3('m', 'q', 'w', 'm'), 0, 1);
GOS_CMD_CREATE_GETTER_SETTER(mqtt, will_qos_level, "mqtt.will.qos_level", SC3('m', 'q', 'w', 'q'), 0, 1);
GOS_CMD_CREATE_GETTER_SETTER(mqtt, will_retained, "mqtt.will.retained", SC3('m', 'q', 'w', 'r'), 0, 1);
GOS_CMD_CREATE_GETTER_SETTER(mqtt, will_topic, "mqtt.will.topic", SC3('m', 'q', 'w', 't'), 0, 1);
// Setter and getter implementations
/******************************************************************************
* Auto reconnect
*/
GOS_DEFINE_GETTER(mqtt, auto_reconnect)
{
gos_cmd_format_response(GOS_CMD_SUCCESS, "%d", mqtt_configuration->automatic_reconnect);
}
GOS_DEFINE_SETTER(mqtt, auto_reconnect)
{
return gos_cmd_set_bool(argc, argv, &(mqtt_configuration->automatic_reconnect));
}
/******************************************************************************
* Broker port
*/
GOS_DEFINE_GETTER(mqtt, broker_port)
{
gos_cmd_format_response(GOS_CMD_SUCCESS, "%d", mqtt_configuration->broker_port);
}
GOS_DEFINE_SETTER(mqtt, broker_port)
{
GOS_CMD_PARSE_INT_ARG_WITH_VAR(uint16_t, mqtt_configuration->broker_port, argv[1], 0, 65535);
}
/******************************************************************************
* Clean session
*/
GOS_DEFINE_GETTER(mqtt, clean_session)
{
gos_cmd_format_response(GOS_CMD_SUCCESS, "%d", mqtt_configuration->clean_session);
}
GOS_DEFINE_SETTER(mqtt, clean_session)
{
return gos_cmd_set_bool(argc, argv, &(mqtt_configuration->clean_session));
}
/******************************************************************************
* Keep alive interval
*/
GOS_DEFINE_GETTER(mqtt, keep_alive_interval)
{
gos_cmd_format_response(GOS_CMD_SUCCESS, "%d", mqtt_configuration->keep_alive_interval_seconds);
}
GOS_DEFINE_SETTER(mqtt, keep_alive_interval)
{
GOS_CMD_PARSE_INT_ARG_WITH_VAR(uint16_t, mqtt_configuration->keep_alive_interval_seconds, argv[1], 0, 65535);
}
/******************************************************************************
* RX buffer
*/
GOS_DEFINE_GETTER(mqtt, rx_buffer_size)
{
gos_cmd_format_response(GOS_CMD_SUCCESS, "%d", mqtt_configuration->rx_buffer_size);
}
GOS_DEFINE_SETTER(mqtt, rx_buffer_size)
{
GOS_CMD_PARSE_INT_ARG_WITH_VAR(uint16_t, mqtt_configuration->rx_buffer_size, argv[1], MIN_BUFFER_SIZE, MAX_BUFFER_SIZE);
}
/******************************************************************************
* TX buffer
*/
GOS_DEFINE_GETTER(mqtt, tx_buffer_size)
{
gos_cmd_format_response(GOS_CMD_SUCCESS, "%d", mqtt_configuration->tx_buffer_size);
}
GOS_DEFINE_SETTER(mqtt, tx_buffer_size)
{
GOS_CMD_PARSE_INT_ARG_WITH_VAR(uint16_t, mqtt_configuration->tx_buffer_size, argv[1], MIN_BUFFER_SIZE, MAX_BUFFER_SIZE);
}
/******************************************************************************
* Use TLS
*/
GOS_DEFINE_GETTER(mqtt, use_tls)
{
gos_cmd_format_response(GOS_CMD_SUCCESS, "%d", mqtt_configuration->use_tls);
}
GOS_DEFINE_SETTER(mqtt, use_tls)
{
return gos_cmd_set_bool(argc, argv, &(mqtt_configuration->use_tls));
}
/******************************************************************************
* Use Secure Element
*/
GOS_DEFINE_GETTER(mqtt, use_secure_element)
{
gos_cmd_format_response(GOS_CMD_SUCCESS, "%d", mqtt_configuration->use_secure_element);
}
GOS_DEFINE_SETTER(mqtt, use_secure_element)
{
return gos_cmd_set_bool(argc, argv, &(mqtt_configuration->use_secure_element));
}
/******************************************************************************
* Retaining will message
*/
GOS_DEFINE_GETTER(mqtt, will_retained)
{
gos_cmd_format_response(GOS_CMD_SUCCESS, "%d", mqtt_will->retained);
}
GOS_DEFINE_SETTER(mqtt, will_retained)
{
return gos_cmd_set_bool(argc, argv, &(mqtt_will->retained));
}
/******************************************************************************
* Will QoS level
*/
GOS_DEFINE_GETTER(mqtt, will_qos_level)
{
gos_cmd_format_response(GOS_CMD_SUCCESS, "%d", mqtt_will->qos_level);
}
GOS_DEFINE_SETTER(mqtt, will_qos_level)
{
GOS_CMD_PARSE_INT_ARG_WITH_VAR(uint8_t, mqtt_will->qos_level, argv[1], 0, 2);
}
/******************************************************************************
* Broker hostname/ip address
*/
GOS_DEFINE_GETTER(mqtt, broker_hostname)
{
gos_cmd_write_response(GOS_CMD_SUCCESS, mqtt_configuration->broker_hostname, strlen(mqtt_configuration->broker_hostname));
}
GOS_DEFINE_SETTER(mqtt, broker_hostname)
{
if(strlen(argv[1]) > sizeof(mqtt_configuration->broker_hostname))
{
GOS_LOG("Failed (maximum length is %u)", sizeof(mqtt_configuration->broker_hostname));
}
else
{
strcpy((char*)mqtt_configuration->broker_hostname, argv[1]);
}
}
/******************************************************************************
* MQTT client ID
*/
GOS_DEFINE_GETTER(mqtt, client_id)
{
gos_cmd_write_response(GOS_CMD_SUCCESS, mqtt_configuration->client_id, strlen(mqtt_configuration->client_id));
}
GOS_DEFINE_SETTER(mqtt, client_id)
{
if(strlen(argv[1]) > sizeof(mqtt_configuration->client_id))
{
GOS_LOG("Failed (maximum length is %u)", sizeof(mqtt_configuration->client_id));
}
else
{
strcpy((char*)mqtt_configuration->client_id, argv[1]);
}
}
/******************************************************************************
* MQTT broker connection password (username & password are optional)
*/
GOS_DEFINE_GETTER(mqtt, password)
{
gos_cmd_write_response(GOS_CMD_SUCCESS, mqtt_configuration->password, strlen(mqtt_configuration->password));
}
GOS_DEFINE_SETTER(mqtt, password)
{
if(strlen(argv[1]) > sizeof(mqtt_configuration->password))
{
GOS_LOG("Failed (maximum length is %u)", sizeof(mqtt_configuration->password));
}
else
{
strcpy((char*)mqtt_configuration->password, argv[1]);
}
}
/******************************************************************************
* MQTT broker connection username (username & password are optional)
*/
GOS_DEFINE_GETTER(mqtt, username)
{
gos_cmd_write_response(GOS_CMD_SUCCESS, mqtt_configuration->username, strlen(mqtt_configuration->username));
}
GOS_DEFINE_SETTER(mqtt, username)
{
if(strlen(argv[1]) > sizeof(mqtt_configuration->username))
{
GOS_LOG("Failed (maximum length is %u)", sizeof(mqtt_configuration->username));
}
else
{
strcpy((char*)mqtt_configuration->username, argv[1]);
}
}
/******************************************************************************
* MQTT will message (a will is optional)
*/
GOS_DEFINE_GETTER(mqtt, will_message)
{
// There is no guarantee by the library that the payload
// is a null-terminated string. However the setter requires it
// hence the payload will be a null terminated string
gos_cmd_format_response(GOS_CMD_SUCCESS, "%s", settings->will_message);
}
GOS_DEFINE_SETTER(mqtt, will_message)
{
if(strlen(argv[1]) > sizeof(settings->will_message))
{
GOS_LOG("Failed (maximum length is %u)", sizeof(settings->will_message));
}
else
{
strcpy((char*)settings->will_message, argv[1]);
mqtt_will->payload = (uint8_t*) settings->will_message;
mqtt_will->payload_length = strlen(argv[1]);
}
}
/******************************************************************************
* MQTT will topic (a will is optional)
*/
GOS_DEFINE_GETTER(mqtt, will_topic)
{
gos_cmd_format_response(GOS_CMD_SUCCESS, "%s", mqtt_will->topic);
}
GOS_DEFINE_SETTER(mqtt, will_topic)
{
if(strlen(argv[1]) > sizeof(mqtt_will->topic))
{
GOS_LOG("Failed (maximum length is %u)", sizeof(mqtt_will->topic));
}
else
{
strcpy((char*)mqtt_will->topic, argv[1]);
}
}
/******************************************************************************
* MQTT version (either '3.1' or '3.1.1' is supported)
*/
GOS_DEFINE_GETTER(mqtt, mqtt_version)
{
if (mqtt_configuration->mqtt_version == MQTT_VERSION_3_1)
{
}
else if (mqtt_configuration->mqtt_version == MQTT_VERSION_3_1_1)
{
}
}
GOS_DEFINE_SETTER(mqtt, mqtt_version)
{
if(strcmp(argv[1], "3.1") == 0)
{
mqtt_configuration->mqtt_version = MQTT_VERSION_3_1;
}
else if(strcmp(argv[1], "3.1.1") == 0)
{
mqtt_configuration->mqtt_version = MQTT_VERSION_3_1_1;
}
else
{
GOS_LOG("Failed, valid options are: '3.1', '3.1.1'");
}
}