system/indicator/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.
*
******************************************************************************/
#include "gos.h"
#include "example_app_util.h"
#define APPLICATION_START_LINE "\r\nSystem indicator example starting ..."
#define SETTINGS_MAGIC_NUMBER 0x00010000
/*************************************************************************************************/
void gos_app_init(void)
{
gos_result_t result;
GOS_LOG(APPLICATION_START_LINE);
// Load the system indicator settings file if necessary
if(GOS_FAILED(result, gos_load_app_settings_once("settings.ini", SETTINGS_MAGIC_NUMBER)))
{
GOS_LOG("Failed to load app settings, err:%d", result);
return;
}
// Initialize the GPIOs
// NOTE: This is not really needed,
// we could just use the variable: system.indicator.gpio
// to achieve the exact same effect.
// In the real-world, the LEDs would be in-directly connected (e.g. I/O expander)
gos_gpio_init(PLATFORM_LED1, GOS_GPIO_OUTPUT_PUSH_PULL, false);
#ifdef PLATFORM_LED2
gos_gpio_init(PLATFORM_LED2, GOS_GPIO_OUTPUT_PUSH_PULL, false);
#endif
GOS_LOG("\r\n**********************");
GOS_LOG("GPIO%d = WLAN indicator", PLATFORM_LED1);
#ifdef PLATFORM_LED2
GOS_LOG("GPIO%d = Network indicator", PLATFORM_LED2);
#endif
GOS_LOG("**********************\r\n");
// Register the WLAN indicator callback
gos_system_indicator_set_callback(GOS_SYSTEM_INDICATOR_WLAN, wlan_system_indicator_callback, (void*)PLATFORM_LED1);
#ifdef PLATFORM_LED2
// Register the Network indicator callback
gos_system_indicator_set_callback(GOS_SYSTEM_INDICATOR_NETWORK, network_system_indicator_callback, (void*)PLATFORM_LED2);
#endif
print_indicator_states("wlan");
#ifdef PLATFORM_LED2
print_indicator_states("network");
#endif
GOS_LOG("\r\nBringing up the WLAN interface which should cause the system indicators to change ...\r\n");
// Bring up the WLAN interface which should cause the system indicators to change
example_app_util_network_up(GOS_INTERFACE_WLAN, true, network_event_handler);
}
/*************************************************************************************************/
static void network_event_handler(bool is_up)
{
GOS_LOG("\r\n*** Network %s, the LEDs should be blinking differently\r\n", is_up ? "up" : "down");
GOS_LOG("Issue the command:\r\n\r\n %s\r\n\r\nTo change the state again\r\n", is_up ? "network_down" : "network_up");
}
/*************************************************************************************************
* This is called when the WLAN indicator state changes
*
* Executes in system event thread
*/
static void wlan_system_indicator_callback(bool value, const gos_system_indicator_callback_info_t *info)
{
const gos_gpio_t gpio = (gos_gpio_t)(uint32_t)info->arg;
// NOTE: For this example the LED is directly connected, thus we could just use the variable 'system.indicator.gpio'
// In the real-world the LED would be indirectly connected, e.g. I/O expander
gos_gpio_set(gpio, value ? PLATFORM_LED_ACTIVE_STATE : !PLATFORM_LED_ACTIVE_STATE);
}
#ifdef PLATFORM_LED2
/*************************************************************************************************
* This is called when the Network indicator state changes
*
* Executes in system event thread
*/
static void network_system_indicator_callback(bool value, const gos_system_indicator_callback_info_t *info)
{
const gos_gpio_t gpio = (gos_gpio_t)(uint32_t)info->arg;
// NOTE: For this example the LED is directly connected, thus we could just use the variable 'system.indicator.gpio'
// In the real-world the LED would be indirectly connected, e.g. I/O expander
gos_gpio_set(gpio, value ? PLATFORM_LED_ACTIVE_STATE : !PLATFORM_LED_ACTIVE_STATE);
}
#endif
/*************************************************************************************************
* Print the system of the `system.indicator.state` variable.
*/
static void print_indicator_states(const char *indicator)
{
gos_result_t result;
char cmd_buffer[64];
char response_buffer[128];
strcpy(cmd_buffer, "system.indicator.state ");
strcat(cmd_buffer, indicator);
if(GOS_FAILED(result, gos_settings_get_str(cmd_buffer, response_buffer, sizeof(response_buffer))))
{
GOS_LOG("Failed to retrieve setting: %s, err:%d", cmd_buffer, result);
}
else
{
GOS_LOG("\r\n----------------");
GOS_LOG("%s = %s", cmd_buffer, response_buffer);
GOS_LOG("\r\nTo update, issue the command:\r\n\r\n set system.indicator.state %s <config>\r\n", indicator);
GOS_LOG("----------------\r\n");
}
}