basic/http_stream/http_stream.c

See examples/basic/http-stream

/*
* ZentriOS SDK LICENSE AGREEMENT | Zentri.com, 2015.
*
* Use of source code and/or libraries contained in the ZentriOS SDK is
* subject to the Zentri Operating System SDK license agreement and
* applicable open source license agreements.
*
*/
/* Documentation for this app is available online.
* See https://docs.zentri.com/wifi/sdk/latest/examples/basic/http-stream
*/
#include "zos.h"
#if defined(PLATFORM_BUTTON1) && defined(PLATFORM_BUTTON2)
#define PLATFORM_HAS_BUTTONS
#else
#define PLATFORM_BUTTON1 0
#define PLATFORM_BUTTON2 0
#endif
#define MAX_EVENTS_FROM_IRQ 8
static zos_hs_handle_t button1_listener = NULL;
static zos_hs_handle_t button2_listener = NULL;
/*************************************************************************************************/
void zn_app_init(void)
{
zos_result_t result;
ZOS_LOG("HTTP Stream example starting...");
ZOS_LOG("Enabling HTTP & mDNS servers");
if(zn_load_app_settings("settings.ini") != ZOS_SUCCESS)
{
ZOS_LOG("Failed to load settings");
return;
}
ZOS_LOG("Initializing LED & Button GPIOs");
PLATFORM_ENABLE_JTAG_GPIOS();
zn_gpio_init(PLATFORM_LED1, GPIO_OUTPUT_PUSHPULL, 0);
zn_gpio_init(PLATFORM_LED2, GPIO_OUTPUT_PUSHPULL, 0);
#ifdef PLATFORM_HAS_BUTTONS
zn_gpio_init(PLATFORM_BUTTON1, GPIO_INPUT_HIGHZ, 0);
zn_gpio_init(PLATFORM_BUTTON2, GPIO_INPUT_HIGHZ, 0);
#endif
ZOS_LOG("Registering stream callbacks");
zn_hs_stream_register_callback("led1", led_stream_callback, (void*)PLATFORM_LED1);
zn_hs_stream_register_callback("led2", led_stream_callback, (void*)PLATFORM_LED2);
zn_hs_stream_register_callback("button1", button_stream_callback, (void*)PLATFORM_BUTTON1);
zn_hs_stream_register_callback("button2", button_stream_callback, (void*)PLATFORM_BUTTON2);
zn_event_enable_irq_events(MAX_EVENTS_FROM_IRQ);
ZOS_LOG("Toggling network to start servers");
{
ZOS_LOG("Failed to restart network: %d\r\n\r\n", result);
ZOS_LOG("----------------------------------------------------------------------");
ZOS_LOG("This basic app expects valid network credentials have been configured.");
ZOS_LOG("Join a network and save credentials to non-volatile memory using the ");
ZOS_LOG("ZentriOS commands shown below ");
ZOS_LOG(" ");
ZOS_LOG("> network_up -s ");
ZOS_LOG("----------------------------------------------------------------------");
ZOS_LOG("\r\n ");
}
}
/*************************************************************************************************/
zos_bool_t zn_app_idle(void)
{
// return TRUE so the event loop idles
return ZOS_TRUE;
}
/*************************************************************************************************/
void zn_app_deinit(void)
{
#ifdef PLATFORM_HAS_BUTTONS
zn_gpio_irq_disable(PLATFORM_BUTTON1);
zn_gpio_irq_disable(PLATFORM_BUTTON2);
#endif
}
/*************************************************************************************************/
static void network_event_handler(uint32_t is_up)
{
char buffer[32];
if(is_up)
{
ZOS_LOG("Network up");
ZOS_LOG("Connect to: http://%s.local/", ZOS_GET_SETTING_STR("mdns.name", buffer));
}
else
{
ZOS_LOG("Network down");
}
}
/*************************************************************************************************/
static zos_result_t led_stream_callback(zos_hs_handle_t handle, const char *stream, zos_hs_stream_method_t method, void *arg)
{
zos_gpio_t led_gpio = (zos_gpio_t)arg;
if(method == HS_STREAM_READ)
{
char *value;
zn_hs_stream_read(handle, &value, NULL);
ZOS_LOG("set %s: %c", stream, *value);
zn_gpio_set(led_gpio, *value == '1');
}
else if(method == HS_STREAM_WRITE)
{
char value_str[4];
int_to_str(zn_gpio_get(led_gpio), value_str);
ZOS_LOG("get LED%d: %s", led_gpio, value_str);
zn_hs_stream_write(handle, value_str, ZOS_TRUE);
}
return ZOS_SUCCESS;
}
/*************************************************************************************************/
static void button_irq_callback(void *arg)
{
// Issue an event so we can do more complex processing
// in the event handler. MUST avoid doing any processing in an IRQ!
zn_event_issue(button_event_handler, arg, EVENT_FLAGS1(FROM_IRQ));
}
/*************************************************************************************************/
static void button_event_handler(void *arg)
{
char value_str[4];
char stream_str[8];
zos_hs_handle_t handle = NULL;
zos_gpio_t button_gpio = (zos_gpio_t)arg;
if(button_gpio == PLATFORM_BUTTON1 && button1_listener != NULL)
{
handle = button1_listener;
strcpy(stream_str, "button1");
}
else if(button_gpio == PLATFORM_BUTTON2 && button2_listener != NULL)
{
handle = button2_listener;
strcpy(stream_str, "button2");
}
if(handle != NULL)
{
int_to_str(zn_gpio_get(button_gpio), value_str);
zn_hs_stream_write_listener(handle, stream_str, value_str, ZOS_TRUE);
}
}
/*************************************************************************************************/
static zos_result_t button_stream_callback(zos_hs_handle_t handle, const char *stream, zos_hs_stream_method_t method, void *arg)
{
#ifdef PLATFORM_HAS_BUTTONS
zos_gpio_t button_gpio = (zos_gpio_t)arg;
if(method == HS_STREAM_WRITE)
{
char value_str[4];
int_to_str(zn_gpio_get(button_gpio), value_str);
zn_hs_stream_write(handle, value_str, ZOS_TRUE);
}
else if(method == HS_STREAM_LISTEN)
{
zn_gpio_irq_enable(button_gpio, GPIO_IRQ_TRIGGER_BOTH_EDGES, button_irq_callback, (void*)button_gpio);
if(button_gpio == PLATFORM_BUTTON1)
{
ZOS_LOG("Registered Button1 listener");
button1_listener = handle;
}
else
{
ZOS_LOG("Registered Button2 listener");
button2_listener = handle;
}
}
else if(method == HS_STREAM_UNLISTEN)
{
zn_gpio_irq_disable(button_gpio);
if(button_gpio == PLATFORM_BUTTON1)
{
ZOS_LOG("Unregistered Button1 listener");
button1_listener = NULL;
}
else
{
ZOS_LOG("Unregistered Button2 listener");
button2_listener = NULL;
}
}
#endif
return ZOS_SUCCESS;
}