network/http_server/requests/set_light.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 "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;
}