hurricane/arducam/network.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 "common.h"
#include "example_app_util.h"
uint8_t client_count;
/*************************************************************************************************/
gos_result_t network_init(void)
{
hurricane_display_set_font_color(GUI_RED);
hurricane_display_set_background_color(GUI_BLACK);
// Set the websocket connect/disconnect handlers
// These handlers are invoked when a websocket client connects or disconnects
gos_hs_stream_ws_set_handlers(websocket_connect_handler, websocket_disconnect_handler);
gos_hs_stream_ws_set_accept_callback(websocket_accept_callback);
// Bringup the default network interface and register an event handler
example_app_util_network_up(GOS_INTERFACE_DEFAULT, true, network_event_handler);
return GOS_SUCCESS;
}
/*************************************************************************************************/
static void network_event_handler(bool is_up)
{
if(is_up)
{
if(interface == GOS_INTERFACE_SOFTAP)
{
char str_buffer[64];
char password_str[64];
gos_settings_get_print_str("softap.passkey", password_str, sizeof(password_str));
GOS_LOG("Softap interface ready\r\n\r\n");
GOS_LOG("Perform following steps to view demo:");
GOS_LOG("1. Connect your PC/phone to Wi-Fi network: %s", gos_settings_get_print_str("softap.ssid", str_buffer, sizeof(str_buffer)));
if(*password_str != 0)
{
GOS_LOG(" NOTE: The Wi-Fi password is: %s", password_str);
}
GOS_LOG("2. Open browser to: http://%s", gos_settings_get_print_str("softap.static.ip", str_buffer, sizeof(str_buffer)));
GOS_LOG("3. Click the 'connect' button on the webpage");
GOS_LOG("4. Live video should be displayed on the webpage");
GOS_LOG("5. Use the webpage's controls to change camera settings\r\n\r\n");
HURRICANE_WRITE_DISPLAY_START(0, 5, 15, 3, 8);
HURRICANE_WRITE_DISPLAY("1. Connect to Wi-Fi network:");
HURRICANE_WRITE_DISPLAY(" Name: %s", gos_settings_get_print_str("softap.ssid", str_buffer, sizeof(str_buffer)));
if(*password_str != 0)
{
HURRICANE_WRITE_DISPLAY(" Password: %s", password_str);
}
HURRICANE_WRITE_DISPLAY("2. Open web browser:");
HURRICANE_WRITE_DISPLAY(" http://%s", gos_settings_get_print_str("softap.static.ip", str_buffer, sizeof(str_buffer)));
HURRICANE_WRITE_DISPLAY("3. Click 'connect' button");
HURRICANE_WRITE_DISPLAY("4. Live video should display");
HURRICANE_WRITE_DISPLAY_END();
}
else
{
char str_buffer[64];
GOS_LOG("%s interface ready\r\n\r\n", (interface == GOS_INTERFACE_ETHERNET) ? "Ethernet" : "WLAN");
GOS_LOG("Perform following steps to view demo:");
GOS_LOG("1. Connect to network:");
if(interface == GOS_INTERFACE_WLAN)
{
GOS_LOG(" Name: %s", gos_settings_get_print_str("wlan.ssid", str_buffer, sizeof(str_buffer)));
}
GOS_LOG("3. Click the 'connect' button on the webpage");
GOS_LOG("4. Live video should be displayed on the webpage");
HURRICANE_WRITE_DISPLAY_START(0, 5, 15, 3, 8);
HURRICANE_WRITE_DISPLAY("1. Connect to network:");
if(interface == GOS_INTERFACE_WLAN)
{
HURRICANE_WRITE_DISPLAY(" Name: %s", gos_settings_get_print_str("wlan.ssid", str_buffer, sizeof(str_buffer)));
}
HURRICANE_WRITE_DISPLAY("2. Open web browser:");
HURRICANE_WRITE_DISPLAY(" http://%s", example_app_util_get_network_interface_ip_address_str());
HURRICANE_WRITE_DISPLAY("3. Click 'connect' button");
HURRICANE_WRITE_DISPLAY("4. Live video should display");
HURRICANE_WRITE_DISPLAY_END();
}
}
else
{
camera_set_video_enabled(false);
}
}
/*************************************************************************************************/
static bool websocket_accept_callback(void)
{
return (client_count < MAX_CLIENT_COUNT);
}
/*************************************************************************************************
*/
static void websocket_connect_handler(void* handle)
{
client_count++;
GOS_LOG("Client connected");
GOS_LOG("Client count: %d", client_count);
if(client_count == 1)
{
camera_set_video_enabled(true);
}
camera_broadcast_settings_to_all_clients();
}
/*************************************************************************************************
* This is invoked when a client disconnects.
* If no more clients are connected then the accelerometer data loop is stopped.
*/
static void websocket_disconnect_handler(void* handle)
{
client_count--;
GOS_LOG("Client disconnected");
GOS_LOG("Client count: %d", client_count);
if(client_count == 0)
{
camera_set_video_enabled(false);
}
camera_broadcast_settings_to_all_clients();
}