network/http_server/requests/set_light.c

/*
* EVALUATION AND USE OF THIS SOFTWARE IS SUBJECT TO THE TERMS AND
* CONDITIONS OF THE CONTROLLING LICENSE AGREEMENT FOUND AT LICENSE.md
* IN THIS SDK. IF YOU DO NOT AGREE TO THE LICENSE TERMS AND CONDITIONS,
* PLEASE RETURN ALL SOURCE FILES TO SILICON LABORATORIES.
* (c) Copyright 2018, Silicon Laboratories Inc. All rights reserved.
*/
#include "gos.h"
#include "common.h"
extern bool led_value;
/*************************************************************************************************
* This handles a standard GET request with RESTful arguments
* GET http://<domain>/set_light/<on/off>
*/
gos_result_t set_light_request(const gos_hs_request_t *request, const char *arg)
{
gos_result_t result;
char buffer[256];
bool value;
if (str_parse_bool(arg, &value) != GOS_SUCCESS)
{
snprintf(buffer, sizeof(buffer), "<h1>Bad Request</h1>Unknown arg: %s", arg);
return gos_hs_return_status_code(request, 400, buffer);
}
led_value = value;
gos_gpio_set(PLATFORM_STD_LED, led_value ? PLATFORM_LED_ACTIVE_STATE : !PLATFORM_LED_ACTIVE_STATE);
GOS_LOG("New LED value: %d", led_value);
int len = sprintf(buffer, "<h3>LED%d value: %d</h3>", PLATFORM_STD_LED, led_value);
if (GOS_FAILED(result, gos_hs_write_reply_header(request, "text/html", len, GOS_HS_RESPONSE_FLAG_NONE)))
{
}
else if (GOS_FAILED(result, gos_hs_write_data(request, buffer, len, true)))
{
}
return result;
}