system/application_nvm/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.
*/
/* Documentation for this app is available online.
* See https://docs.silabs.com/gecko-os/4/standard/latest/sdk/examples/system/application-nvm
*/
/*
* This demonstrates how to load and save settings to the application NVM.
*/
#include "gos.h"
#include "nvm_settings.h"
#define CLOUD_URL "https://my-other-cloud.com"
#define SETUP_WEB_SSID "example-ssid"
#define APPLICATION_START_LINE "\r\nNVM example starting ..."
GOS_CMD_CREATE_COMMAND(app_nvm, reset, "nvm_reset", "nreset", 0, 0, false);
/*************************************************************************************************/
void gos_app_init(void)
{
gos_result_t result;
char *cloud_url;
GOS_LOG(APPLICATION_START_LINE);
// Obtain a reference to the cloud URL setting in cached NVM
if (GOS_FAILED(result, GOS_NVM_GET(CLOUD, URL, cloud_url)))
{
GOS_LOG("Failed to retrieve CLOUD.URL from NVM, err:%d", result);
return;
}
modify_gecko_os_settings();
// Print the current cloud URL
GOS_LOG("\r\nApp setting: cloud-url = %s", cloud_url);
// Check if cloud URL has been modified
if (strcmp(cloud_url, CLOUD_URL) == 0)
{
GOS_LOG("Up-to-date");
}
else
{
// Update the cloud URL
strcpy(cloud_url, CLOUD_URL);
GOS_LOG("updated to: %s", cloud_url);
GOS_LOG("Saving updated value to application NVM");
if (GOS_FAILED(result, gos_nvm_save()))
{
GOS_LOG("Failed to save application settings to NVM, err:%d", result);
}
else
{
GOS_LOG("NVM settings updated");
}
}
GOS_LOG("\r\nIssue a 'reboot' command to verify the updated settings persist");
GOS_LOG("Issue command: 'nvm_reset' to reset application NVM to default values\r\n");
}
/*************************************************************************************************/
static gos_cmd_result_t app_nvm_reset_command(int argc, char **argv)
{
gos_result_t result;
GOS_LOG("Invoking application NVM reset ...");
GOS_LOG("NOTE: Plugin and Kernel settings should persist after this operation");
// This will factory reset the application NVM ONLY
// Plugins and Kernel settings will not be effected.
// If this API is successful it will reboot the device
{
GOS_LOG("Failed to reset, err:%d", result);
}
else
{
// NOTE: This must be unreachable as the above API should reboot the device
}
}
/*************************************************************************************************
* This demonstrates how to modify Gecko OS Plugins and Kernel settings.
* These settings reside in a different part of NVM than the application NVM.
*/
static void modify_gecko_os_settings(void)
{
gos_result_t result;
char ssid_buffer[128];
// Retrieve the setting from the Plugins' NVM cache
if (GOS_FAILED(result, gos_settings_get_str("setup.web.ssid", ssid_buffer, sizeof(ssid_buffer))))
{
GOS_LOG("Failed to retrieve Gecko OS setting 'setup.web.ssid, err:%d", result);
}
else
{
GOS_LOG("Gecko OS Plugin setting: setup.web.ssid = %s", ssid_buffer);
if (strcmp(ssid_buffer, SETUP_WEB_SSID) != 0)
{
GOS_LOG("Updating to: %s", SETUP_WEB_SSID);
// Update the setting in the Plugins' NVM cache
if (GOS_FAILED(result, gos_settings_set_str("setup.web.ssid", SETUP_WEB_SSID)))
{
GOS_LOG("Failed to update Gecko OS setting 'setup.web.ssid, err:%d", result);
}
// Save ALL cached NVM settings (Application, Plugins, Kernel) to NVM
else if (GOS_FAILED(result, gos_settings_save()))
{
GOS_LOG("Failed to save Gecko OS settings, err:%d", result);
}
else
{
GOS_LOG("Successfully updated");
}
}
else
{
GOS_LOG("Up-to-date");
}
}
}