network/websocket_client/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/network/websocket-client
*/
#include "gos.h"
#include "example_app_util.h"
#define WEBSOCKET_HOST "ws://echo.websocket.org"
#define NETWORK_UP_PERIOD_MS 7000 // ms
#define ATTEMPT_CONNECT_PERIOD_MS 5000 // ms
#define TRANSMIT_PERIOD_MS 1000 // ms
#define APPLICATION_START_LINE "\r\nWebsocket Client example starting ..."
uint32_t packet_counter;
/*************************************************************************************************/
void gos_app_init(void)
{
gos_result_t result;
GOS_LOG(APPLICATION_START_LINE);
if (GOS_FAILED(result, gos_load_app_settings("settings.ini")))
{
GOS_LOG("Failed to load settings, err:%d", result);
return;
}
if (GOS_FAILED(result, example_app_util_network_up(GOS_INTERFACE_ANY, false, network_event_handler)))
{
}
}
/*************************************************************************************************/
static void network_event_handler(bool is_up)
{
if (is_up == true)
{
GOS_LOG("Network up");
gos_event_issue(websocket_attempt_connect_handler, NULL, 0);
}
else
{
GOS_LOG("Network down");
gos_event_issue(network_up_handler, NULL, 0);
}
}
/*************************************************************************************************/
static void network_up_handler(void *arg)
{
gos_result_t result;
GOS_LOG("Attempting to bring network up ...");
gos_event_unregister(network_up_handler, NULL);
{
GOS_LOG("Failed to bring network up, err:%d", result);
GOS_LOG("Trying again in %d seconds", NETWORK_UP_PERIOD_MS/1000);
gos_event_register_timed(network_up_handler, NULL, NETWORK_UP_PERIOD_MS, 0);
}
}
/*************************************************************************************************/
static void websocket_attempt_connect_handler(void *arg)
{
gos_result_t result;
gos_handle_t stream_handle;
{
network_event_handler(false);
return;
}
GOS_LOG("Attempting to connect to " WEBSOCKET_HOST);
if (GOS_FAILED(result, gos_websocket_connect(GOS_INTERFACE_DEFAULT, WEBSOCKET_HOST, &stream_handle)))
{
GOS_LOG("Failed to connect. Trying again in %d seconds", ATTEMPT_CONNECT_PERIOD_MS/1000);
gos_event_register_timed(websocket_attempt_connect_handler, NULL, ATTEMPT_CONNECT_PERIOD_MS, 0);
return;
}
GOS_LOG("Connected. Stream handle: %d", stream_handle);
packet_counter = 0;
gos_websocket_register_client_event_handlers(stream_handle, websocket_disconnect_handler, websocket_receive_handler);
gos_event_register_periodic(websocket_transmit_handler, (void*)stream_handle, TRANSMIT_PERIOD_MS, GOS_EVENT_FLAGS1(RUN_NOW));
}
/*************************************************************************************************/
static void websocket_disconnect_handler(gos_handle_t handle)
{
GOS_LOG("Server disconnected, attempting to read any remaining data");
gos_event_unregister(websocket_transmit_handler, NULL);
websocket_receive_handler(handle);
GOS_LOG("Closing TCP handle: %d", handle);
GOS_LOG("Restarting connection timer");
gos_event_issue(websocket_attempt_connect_handler, NULL, 0);
}
/*************************************************************************************************/
static void websocket_receive_handler(gos_handle_t handle)
{
gos_result_t result;
gos_buffer_t buffer = {.size = 0};
if (GOS_FAILED(result, gos_websocket_poll(handle, &buffer.size, NULL)))
{
GOS_LOG("Failed to poll websocket stream, err:%d", result);
return;
}
else if (buffer.size == 0)
{
return;
}
if (GOS_FAILED(result, gos_websocket_read_with_buffer(handle, &buffer)))
{
GOS_LOG("Failed to read data, err:%d", result);
return;
}
gos_write_log((char*)buffer.data, buffer.size);
}
/*************************************************************************************************/
static void websocket_transmit_handler(void *arg)
{
gos_handle_t handle = (gos_handle_t)arg;
gos_result_t result;
char buffer[128];
uint32_t gpio_values;
gpio_values = gos_gpio_mask_get(UINT32_MAX);
const int len = sprintf(buffer, "%3u: Time:%s, GPIOs:%08X\r\n", (unsigned int)packet_counter, (const char*)&time_str, (unsigned int)gpio_values);
packet_counter++;
if (GOS_FAILED(result, gos_websocket_write(handle, buffer, len, false)))
{
GOS_LOG("Failed to transmit data, err:%d", result);
}
}