2019-03-18 09:13:02 +01:00
|
|
|
/******************************************************************************
|
|
|
|
*
|
|
|
|
* Copyright (c) 2017-2019 by Löwenware Ltd
|
|
|
|
* Please, refer LICENSE file for legal information
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file hello-world.c
|
|
|
|
* @author Ilja Kartašov <ik@lowenware.com>
|
|
|
|
* @brief AISL usage example: Hello World
|
|
|
|
*
|
|
|
|
* @see https://lowenware.com/aisl/
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
/* Include library meta header */
|
|
|
|
#include <aisl/aisl.h>
|
|
|
|
|
|
|
|
|
|
|
|
#define DEFAULT_HTTP_PORT 8080 /**< Default HTTP server port */
|
|
|
|
|
|
|
|
|
2019-03-24 21:00:25 +01:00
|
|
|
static void
|
|
|
|
hello_world(aisl_evt_t const evt, void * p_ctx)
|
2019-03-18 09:13:02 +01:00
|
|
|
{
|
2019-03-24 21:00:25 +01:00
|
|
|
if (evt->code != AISL_EVENT_STREAM_REQUEST)
|
|
|
|
return;
|
|
|
|
|
2019-03-18 09:13:02 +01:00
|
|
|
aisl_status_t status;
|
|
|
|
|
2019-03-24 21:00:25 +01:00
|
|
|
aisl_stream_t s = evt->source;
|
|
|
|
|
2019-03-18 09:13:02 +01:00
|
|
|
const char html[] =
|
|
|
|
"<html>"
|
|
|
|
"<head>"
|
|
|
|
"<title>Hello World</title>"
|
|
|
|
"</head>"
|
|
|
|
"<body>"
|
|
|
|
"<h1>Hello World</h1>"
|
|
|
|
"<p>Powered by AISL</p>"
|
|
|
|
"</body>"
|
|
|
|
"</html>";
|
|
|
|
|
|
|
|
status = aisl_response(s, AISL_HTTP_OK, sizeof(html)-1);
|
|
|
|
|
|
|
|
if (status == AISL_SUCCESS)
|
|
|
|
{
|
|
|
|
if (aisl_write(s, html, sizeof(html)-1) != -1)
|
|
|
|
{
|
|
|
|
aisl_flush(s);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
aisl_reject(s);
|
|
|
|
}
|
|
|
|
|
2019-03-24 21:00:25 +01:00
|
|
|
(void) p_ctx;
|
2019-03-18 09:13:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char ** argv)
|
|
|
|
{
|
2019-03-24 21:00:25 +01:00
|
|
|
aisl_t aisl; /**< AISL instance pointer */
|
|
|
|
aisl_status_t status; /**< AISL status code */
|
|
|
|
struct aisl_cfg cfg = AISL_CFG_DEFAULT;
|
|
|
|
struct aisl_cfg_srv srv = {
|
|
|
|
.host = "0.0.0.0",
|
|
|
|
.port = DEFAULT_HTTP_PORT,
|
|
|
|
.secure = false
|
|
|
|
};
|
|
|
|
|
|
|
|
cfg.srv = &srv;
|
|
|
|
cfg.srv_cnt = 1;
|
|
|
|
cfg.callback = hello_world;
|
2019-03-18 09:13:02 +01:00
|
|
|
|
|
|
|
/* Initialize instance */
|
2019-03-24 21:00:25 +01:00
|
|
|
if ( (aisl = aisl_new(&cfg)) != NULL )
|
2019-03-18 09:13:02 +01:00
|
|
|
{
|
2019-03-24 21:00:25 +01:00
|
|
|
/* launch application loop */
|
|
|
|
fprintf(stdout, "Entering main loop" );
|
|
|
|
for(;;)
|
2019-03-18 09:13:02 +01:00
|
|
|
{
|
2019-03-24 21:00:25 +01:00
|
|
|
status = aisl_run_cycle(aisl);
|
|
|
|
|
|
|
|
if ( status != AISL_SUCCESS )
|
|
|
|
aisl_sleep(aisl, 500);
|
2019-03-18 09:13:02 +01:00
|
|
|
}
|
2019-03-24 21:00:25 +01:00
|
|
|
|
|
|
|
aisl_free(aisl);
|
2019-03-18 09:13:02 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
fprintf(stderr, "Failed to initialize AISL");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|