network/http_server/requests/get_params.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 URL parameters
* GET http://<domain>/get_params?msg=<msg_str>&light=<on/off>&retval=<retval_str>
*/
gos_result_t get_params_request(const gos_hs_request_t *request, const char *arg)
{
gos_result_t result;
char buffer[256];
bool value;
const gos_hs_param_t *msg_param = gos_hs_request_get_param(request, "msg");
const gos_hs_param_t *light_param = gos_hs_request_get_param(request, "light");
const gos_hs_param_t *reval_param = gos_hs_request_get_param(request, "retval");
if (msg_param == NULL)
{
return return_error(request, "'msg' variable not found");
}
if (light_param == NULL)
{
return return_error(request, "'light' variable not found");
}
if (reval_param == NULL)
{
return return_error(request, "'retval' variable not found");
}
if (str_parse_bool(light_param->value, &value) != GOS_SUCCESS)
{
snprintf(buffer, sizeof(buffer), "<h1>Bad Request</h1>Bad 'light' value: %s", light_param->value);
return return_error(request, 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);
GOS_LOG("Message: %s", msg_param->value);
int len = sprintf(buffer, "<h3>LED value: %d</h3>Retval: %s", led_value, reval_param->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;
}
/*************************************************************************************************/
static gos_result_t return_error(const gos_hs_request_t *request, const char *msg)
{
char buffer[512];
char *ptr = buffer;
ptr += sprintf(ptr, "<h1>Bad Request</h1><h3>");
ptr += sprintf(ptr, msg);
ptr += sprintf(ptr, "</h3>URL must be of the form:<br />http://[domain]/get_params?msg=[msg_str]&light=[on/off]&retval=[retval_str]<br /><br />");
ptr += sprintf(ptr, "For example, copy and paste the following into your internet browser:<br />");
ptr += sprintf(ptr, "<pre>http://%s/get_params?msg=Heres-a-message&light=on&retval=Heres-a-return-value</pre>", ip_str_buffer);
return gos_hs_return_status_code(request, 400, buffer);
}