system/custom_commands/main.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.
*
******************************************************************************/
/*
* Documentation for this app is available online.
* See https://docs.silabs.com/gecko-os/4/standard/latest/sdk/examples/system/custom-commands
*/
#include "gos.h"
#include "common.h"
#include "nvm_settings.h"
#define APPLICATION_START_LINE "\r\nCustom Commands example starting ..."
GOS_CMD_CREATE_GETTER_SETTER(sensor, temperature_period, "sensor.temperature.period", SC3('s','e','t','p'), 0, 1);
GOS_CMD_CREATE_GETTER_SETTER(sensor, temperature_enabled, "sensor.temperature.enabled",SC3('s','e','t','e'), 0, 1);
GOS_CMD_CREATE_GETTER_SETTER(sensor, humidity_period, "sensor.humidity.period", SC3('s','e','h','p'), 0, 1);
GOS_CMD_CREATE_GETTER_SETTER(sensor, humidity_enabled, "sensor.humidity.enabled", SC3('s','e','h','e'), 0, 1);
GOS_CMD_CREATE_GETTER_SETTER(sensor, cloud_url, "sensor.cloud.url", SC3('s','e','c','u'), 0, 1);
GOS_CMD_CREATE_GETTER_SETTER(sensor, cloud_interval, "sensor.cloud.interval", SC3('s','e','c','i'), 0, 1);
GOS_CMD_CREATE_SETTER (sensor, device_key, "sensor.device_key", SC2('s','e','k'), 1);
GOS_CMD_CREATE_COMMAND (sensor, start, "sensor_start", "start", 0, 1, true);
GOS_CMD_CREATE_COMMAND (sensor, stop, "sensor_stop", "stop", 0, 1, true);
static struct
{
humidity_sensor_settings_t *humidity_sensor;
temperature_sensor_settings_t *temperature_sensor;
cloud_settings_t *cloud;
} settings;
/*************************************************************************************************/
void gos_app_init(void)
{
gos_result_t result;
GOS_LOG(APPLICATION_START_LINE);
GOS_LOG("\r\nView all available commands and shortcuts with command:");
GOS_LOG(" help commands");
GOS_LOG("View all available variables and shortcuts with command:");
GOS_LOG(" help variables");
GOS_LOG(" get sensor");
// Obtain a references to the cached NVM settings
if (GOS_FAILED(result, GOS_NVM_GET(SENSOR, HUMIDITY, settings.humidity_sensor)))
{
GOS_LOG("Failed to retrieve reference to SENSOR.HUMIDITY settings from NVM, err:%d", result);
}
if (GOS_FAILED(result, GOS_NVM_GET(SENSOR, TEMPERATURE, settings.temperature_sensor)))
{
GOS_LOG("Failed to retrieve reference to SENSOR.TEMPERATURE settings from NVM, err:%d", result);
}
if (GOS_FAILED(result, GOS_NVM_GET(CLOUD, SETTINGS, settings.cloud)))
{
GOS_LOG("Failed to retrieve reference to CLOUD.SETTINGS from NVM, err:%d", result);
}
}
/*************************************************************************************************/
gos_cmd_result_t sensor_start_command(int argc, char **argv)
{
if (strcmp(argv[0], "temperature") == 0)
{
gos_event_register_periodic(temperature_event_handler, NULL, settings.temperature_sensor->polling_period, 0);
gos_cmd_format_response(GOS_CMD_SUCCESS, "Starting temperature sensor with period: %dms", settings.temperature_sensor->polling_period);
}
else if (strcmp(argv[0], "humidity") == 0)
{
gos_event_register_periodic(humidity_event_handler, NULL, settings.humidity_sensor->polling_period, 0);
gos_cmd_format_response(GOS_CMD_SUCCESS, "Starting humidity sensor with period: %dms", settings.humidity_sensor->polling_period);
}
else
{
gos_cmd_format_response(GOS_CMD_BAD_ARGS, "Invalid argument must be 'temperature' or 'humidity'");
}
return GOS_CMD_SUCCESS; // We return a success here since we already printed the response
}
/*************************************************************************************************/
gos_cmd_result_t sensor_stop_command(int argc, char **argv)
{
if (strcmp(argv[0], "temperature") == 0)
{
gos_event_unregister(temperature_event_handler, NULL);
gos_cmd_format_response(GOS_CMD_SUCCESS, "Stopping temperature sensor");
}
else if (strcmp(argv[0], "humidity") == 0)
{
gos_event_unregister(humidity_event_handler, NULL);
gos_cmd_format_response(GOS_CMD_SUCCESS, "Stopping humidity sensor");
}
else
{
gos_cmd_format_response(GOS_CMD_BAD_ARGS, "Invalid. Argument must be 'temperature' or 'humidity'");
}
return GOS_CMD_SUCCESS; // We return a success here since we already printed the response
}
/*************************************************************************************************/
gos_cmd_result_t sensor_get_temperature_period_command(int argc, char **argv)
{
gos_cmd_format_response(GOS_CMD_SUCCESS, "%u", settings.temperature_sensor->polling_period);
}
/*************************************************************************************************/
gos_cmd_result_t sensor_set_temperature_period_command(int argc, char **argv)
{
GOS_CMD_PARSE_INT_ARG_WITH_VAR(uint16_t, settings.temperature_sensor->polling_period, argv[1], 100, 2000);
}
/*************************************************************************************************/
gos_cmd_result_t sensor_get_temperature_enabled_command(int argc, char **argv)
{
gos_cmd_format_response(GOS_CMD_SUCCESS, "%u", settings.temperature_sensor->enabled);
}
/*************************************************************************************************/
gos_cmd_result_t sensor_set_temperature_enabled_command(int argc, char **argv)
{
return gos_cmd_set_bool(argc, argv, &settings.temperature_sensor->enabled);
}
/*************************************************************************************************/
gos_cmd_result_t sensor_get_humidity_period_command(int argc, char **argv)
{
gos_cmd_format_response(GOS_CMD_SUCCESS, "%u", settings.humidity_sensor->polling_period);
}
/*************************************************************************************************/
gos_cmd_result_t sensor_set_humidity_period_command(int argc, char **argv)
{
GOS_CMD_PARSE_INT_ARG_WITH_VAR(uint16_t, settings.humidity_sensor->polling_period, argv[1], 100, 2000);
}
/*************************************************************************************************/
gos_cmd_result_t sensor_get_humidity_enabled_command(int argc, char **argv)
{
gos_cmd_format_response(GOS_CMD_SUCCESS, "%u", settings.humidity_sensor->enabled);
}
/*************************************************************************************************/
gos_cmd_result_t sensor_set_humidity_enabled_command(int argc, char **argv)
{
return gos_cmd_set_bool(argc, argv, &settings.humidity_sensor->enabled);
}
/*************************************************************************************************/
gos_cmd_result_t sensor_get_cloud_interval_command(int argc, char **argv)
{
gos_cmd_format_response(GOS_CMD_SUCCESS, "%u", settings.cloud->update_interval);
}
/*************************************************************************************************/
gos_cmd_result_t sensor_set_cloud_interval_command(int argc, char **argv)
{
GOS_CMD_PARSE_INT_ARG_WITH_VAR(uint16_t, settings.cloud->update_interval, argv[1], 100, 2000);
}
/*************************************************************************************************/
gos_cmd_result_t sensor_get_cloud_url_command(int argc, char **argv)
{
const char *cloud_url;
if (GOS_NVM_GET(CLOUD, URL, cloud_url) != GOS_SUCCESS)
{
GOS_LOG("Failed to retrieve cloud URL from NVM");
}
}
/*************************************************************************************************/
gos_cmd_result_t sensor_set_cloud_url_command(int argc, char **argv)
{
char *cloud_url;
if (GOS_NVM_GET(CLOUD, URL, cloud_url) != GOS_SUCCESS)
{
GOS_LOG("Failed to retrieve cloud URL reference from NVM");
}
return gos_cmd_set_str(argc, argv, cloud_url, MAX_CLOUD_URL_LENGTH-1);
}
/*************************************************************************************************/
gos_cmd_result_t sensor_set_device_key_command(int argc, char **argv)
{
if (str_hex_to_binary(argv[1]) != DEVICE_KEY_LENGTH)
{
GOS_LOG("Failed to parse hex string, must be %d hex char string", (DEVICE_KEY_LENGTH * 2));
}
else
{
uint8_t *device_key;
if (GOS_NVM_GET(CLOUD, URL, device_key) != GOS_SUCCESS)
{
GOS_LOG("Failed to retrieve device key reference from NVM");
}
memcpy(device_key, argv[1], DEVICE_KEY_LENGTH);
}
}
/*************************************************************************************************/
static void temperature_event_handler(void *arg)
{
// Poll temperature sensor here ....
}
/*************************************************************************************************/
static void humidity_event_handler(void *arg)
{
// Poll humidity sensor here ....
}