network/http_server/requests/json_parser.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"
#define READ_POST_DATA_TIMEOUT 1000
extern bool led_value;
/*************************************************************************************************
* This handles a POST request with JSON formated data
* POST http://<domain>/json_parser
*
* {
* "light" : <true/false>,
* "msg" : "<msg_str>",
* "retval": "<retval_str>"
* }
*/
{
gos_result_t result;
gos_json_parse_context_t *context;
uint8_t body_data[256];
gos_buffer_t post_buffer = {
.data = body_data,
};
{
}
{
}
{
}
{
.buffer_len = (uint16_t)post_buffer.size,
};
gos_json_parse_context_init(&context, &cfg);
{
gos_hs_return_status_code(request, 400, "<h1>Failed to parse JSON</h1>");
goto exit;
}
(light_tok == NULL) || (light_tok->next == NULL) ||
(retval_tok == NULL) || (retval_tok->next == NULL))
{
gos_hs_return_status_code(request, 400, "<h1>JSON not formatted correctly</h1>" \
"Must be of the form:<br />" \
"{\"light\":<true/false>,\"msg\":\"<msg_str>\",\"retval\":\"<retval_str>\"}");
goto exit;
}
led_value = light_tok->next->data.boolean;
gos_gpio_set(PLATFORM_STD_LED, led_value ? PLATFORM_LED_ACTIVE_STATE : !PLATFORM_LED_ACTIVE_STATE);
char buffer[64];
int len = snprintf(buffer, sizeof(buffer), "<h1>Led value: %d</h1>", led_value);
if (GOS_FAILED(result, gos_hs_write_reply_header(request, "text/html", len, GOS_HS_RESPONSE_FLAG_NONE)))
{
}
{
}
exit:
gos_json_parse_context_deinit(context);
return result;
}