system/settings_file/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/settings-file
*/
/*
* This demonstrates how to save and load configuration files
* to/from the extended flash file system.
*/
#include "gos.h"
#include "nvm_settings.h"
GOS_CMD_CREATE_GETTER_SETTER(myapp, version, "myapp.version", SC2('m','y','v'), 0, 1);
GOS_CMD_CREATE_GETTER_SETTER(myapp, timeout, "myapp.timeout", SC2('m','y','t'), 0, 1);
GOS_CMD_CREATE_GETTER_SETTER(myapp, password, "myapp.password",SC2('m','y','p'), 0, 1);
#define CONFIG_FILE "example_settings.csv"
#define APPLICATION_START_LINE "\r\nSettings File example starting ..."
static myapp_settings_t *myapp_settings;
/*************************************************************************************************/
void gos_app_init(void)
{
gos_result_t result;
GOS_LOG(APPLICATION_START_LINE);
GOS_LOG("NOTE: 'default_config.csv' will automatically be loaded after programming or DFU");
if (GOS_FAILED(result, GOS_NVM_GET(MYAPP, SETTINGS, myapp_settings)))
{
GOS_LOG("Failed to retrieve reference to MYAPP.SETTINGS settings from NVM, err:%d", result);
return;
}
// If myapp.version is zero then load the settings from the config file
if (myapp_settings->version == 0)
{
GOS_LOG("Verbosely loading configuration file: %s", CONFIG_FILE);
// NOTE: This will reboot the system if the file is successfully loaded
if (GOS_FAILED(result, gos_settings_load_from_file(CONFIG_FILE, true)))
{
GOS_LOG("Error loading file, err:%d", result);
}
}
else
{
GOS_LOG("Example settings up-to-date");
}
GOS_LOG("Issue the command:");
GOS_LOG("- get myapp : View the current app settings");
GOS_LOG("- save my_settings.csv -u : Save all current settings to a file");
GOS_LOG("- load my_settings.csv -v : Load the saved settings file");
GOS_LOG("- load example_settings.csv -v' : Reload the example app settings file");
}
/*************************************************************************************************/
static gos_cmd_result_t myapp_set_version_command(int argc, char **argv)
{
GOS_CMD_PARSE_INT_ARG_WITH_VAR(uint32_t, myapp_settings->version, argv[1], 0, UINT32_MAX);
}
/*************************************************************************************************/
static gos_cmd_result_t myapp_get_version_command(int argc, char **argv)
{
gos_cmd_format_response(GOS_CMD_SUCCESS, "%u", myapp_settings->version);
}
/*************************************************************************************************/
static gos_cmd_result_t myapp_get_timeout_command(int argc, char **argv)
{
gos_cmd_format_response(GOS_CMD_SUCCESS, "%u", myapp_settings->timeout);
}
/*************************************************************************************************/
static gos_cmd_result_t myapp_set_timeout_command(int argc, char **argv)
{
GOS_CMD_PARSE_INT_ARG_WITH_VAR(uint32_t, myapp_settings->timeout, argv[1], 0, UINT32_MAX);
}
/*************************************************************************************************/
static gos_cmd_result_t myapp_get_password_command(int argc, char **argv)
{
gos_cmd_format_response(GOS_CMD_SUCCESS, "%s", myapp_settings->password);
}
/*************************************************************************************************/
static gos_cmd_result_t myapp_set_password_command(int argc, char **argv)
{
return gos_cmd_set_str(argc, argv, myapp_settings->password, sizeof(myapp_settings->password));
}