wifi/device_setup/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/wifi/device-setup
*/
#include "gos.h"
/*************************************************************************************************/
void gos_app_init(void)
{
GOS_LOG("Device Setup example starting ...");
gos_load_app_settings("settings.ini");
start_network_interface();
}
/*************************************************************************************************
* Try to start WLAN interface
* If fail to start, then start web setup
* NOTE: Web setup will idle timeout after setup.web.idle_timeout seconds
* Upon timeout, the system is rebooted.
*/
static void start_network_interface(void)
{
gos_result_t result;
GOS_LOG("\r\n\r\nChecking if device is able to connect to local network ...");
{
GOS_LOG("Failed to join local network, starting Web Setup");
if (GOS_FAILED(result, gos_setup_start()))
{
GOS_LOG("Failed to start Web Setup, rebooting");
}
else
{
char buffer_url[128];
char buffer_username[64];
char buffer_passkey[64];
uint32_t idle_timeout;
gos_settings_get_uint32("setup.web.idle_timeout", &idle_timeout);
get_setup_url("setup.web.url", buffer_url, sizeof(buffer_url));
get_device_name("setup.web.ssid", buffer_username, sizeof(buffer_username));
gos_settings_get_print_str("setup.web.passkey", buffer_passkey, sizeof(buffer_passkey));
GOS_LOG("Device Web Setup started");
GOS_LOG("1. Connect to Wi-Fi network: %s with passkey: %s", buffer_username, buffer_passkey);
GOS_LOG("2. Open browser to http://%s", buffer_url);
GOS_LOG("3. Setup the device\r\n");
GOS_LOG("NOTE: Web setup will idle timeout after %d seconds\r\n", idle_timeout);
}
}
else
{
char buffer[128] = { 0 };
GOS_LOG("Device WLAN started");
gos_settings_get_print_str("wlan.ssid", buffer, sizeof(buffer));
GOS_LOG("1. Connect to local Wi-Fi network: %s", buffer);
memset(buffer, 0, sizeof(buffer));
get_device_name("mdns.name", buffer, sizeof(buffer));
GOS_LOG("2. Open browser to:");
GOS_LOG(" Windows/Linux: http://%s/", buffer);
GOS_LOG(" Mac/iOS : http://%s.local/", buffer);
memset(buffer, 0, sizeof(buffer));
gos_settings_get_print_str("wlan.network.ip", buffer, sizeof(buffer));
GOS_LOG(" Android/All : http://%s/", buffer);
GOS_LOG("3. Setup the device\r\n");
}
}
/*************************************************************************************************
* Event handler for WLAN interface events
*/
static void wlan_network_event_handler(bool is_up)
{
// If the WLAN interface has gone down AND softAP interface is NOT up try to restart the WLAN interface
if ((is_up == false) && (gos_network_is_up(GOS_INTERFACE_SOFTAP) == false))
{
// If the network goes down try to restart it, else start web setup
start_network_interface();
}
}
/*************************************************************************************************
* Just reboot the system when web setup finishes
*/
static void setup_finished_handler(void *unsed)
{
GOS_LOG("Web setup finished, rebooting system");
}
/*************************************************************************************************
* Convert '#' to last three chars of device's MAC address
*/
static const char* get_device_name(const char *setting, char *buffer, size_t buffer_length)
{
int setting_len;
char *setting_ptr;
gos_settings_get_print_str(setting, buffer, buffer_length);
setting_len = strlen(buffer);
setting_ptr = &buffer[setting_len-1];
if (*setting_ptr == '#')
{
char mac_str[20];
// Example output:
// 4C:55:CC:10:78:9B
gos_settings_get_print_str("softap.mac", mac_str, sizeof(mac_str));
*setting_ptr++ = mac_str[13];
*setting_ptr++ = mac_str[15];
*setting_ptr++ = mac_str[16];
*setting_ptr = 0;
}
return buffer;
}
/*************************************************************************************************
* When the first entry in the setup.web.url domain list
*/
static const char* get_setup_url(const char *setting, char *buffer, size_t buffer_length)
{
gos_settings_get_print_str("setup.web.url", buffer, buffer_length);
char *comma_ptr = strchr(buffer, ',');
*comma_ptr = 0;
return buffer;
}