network/http_server/requests/toggle_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
* GET http://<domain>/toggle_light
*/
gos_result_t toggle_light_request(const gos_hs_request_t *request, const char *arg)
{
gos_result_t result;
char buffer[256];
led_value = !led_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;
}