network/http_methods/main.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.
*
******************************************************************************/
/*
* Documentation for this app is available online.
* See https://docs.silabs.com/gecko-os/4/standard/latest/sdk/examples/network/http-methods
*/
#include "common.h"
#include "example_app_util.h"
#define APPLICATION_START_LINE "\r\nHTTP Methods example starting ..."
char bin_url[GOS_URL_MAX_LENGTH];
/*************************************************************************************************/
void gos_app_init(void)
{
gos_result_t result;
GOS_LOG(APPLICATION_START_LINE);
if (GOS_FAILED(result, gos_load_app_settings("settings.ini")))
{
GOS_LOG("Failed to load app settings, err:%d", result);
}
{
GOS_LOG("Exiting example app");
}
else
{
GOS_LOG("Using a web browser on your computer, go to: http://postb.in/");
GOS_LOG("When the page opens, click the blue button labeled: 'Create Bin'");
GOS_LOG("This will display a URL that looks something like: http://postb.in/Oph9yFRm\r\n");
GOS_LOG("Copy your 'URL' shown on the web page here, then press Enter: ");
readline();
cleanup_url();
http_get_example();
http_head_example();
http_post_example();
http_put_example();
http_delete_example();
GOS_LOG("\r\nNow refresh the webpage in your browser to view the results.");
GOS_LOG("\r\nIf TLS errors are generated due to invalid certificate, a new certificate can be generated.");
GOS_LOG("Please see: tools/utilities/certificate_fetching_tool/README.txt");
}
}
/*************************************************************************************************/
static void readline(void)
{
// disable the console so we can read from the UART
for (char *ptr = bin_url;; ++ptr)
{
gos_uart_receive_bytes(PLATFORM_STD_UART, ptr, 1, NULL, GOS_WAIT_FOREVER);
gos_uart_transmit_bytes(PLATFORM_STD_UART, ptr, 1, NULL, GOS_WAIT_FOREVER);
if ((ptr != bin_url) && (*ptr == '\r' || *ptr == '\n'))
{
if (*ptr == '\r')
{
*ptr = '\n';
gos_uart_transmit_bytes(PLATFORM_STD_UART, ptr, 1, NULL, GOS_WAIT_FOREVER);
}
*ptr = 0;
break;
}
}
}
static void cleanup_url(void)
{
// Deal with the user entering /b/ in the url.
// If /b/ is present in the url, use strcpy to write over it.
char *ptr_to_b = strnstr( bin_url, "/b/", strlen( bin_url ));
if ( ptr_to_b != NULL )
{
strcpy( ptr_to_b, ptr_to_b + 2 );
}
}