Morse Micro IoT SDK  2.9.7
http.c File Reference

Detailed Description

HTTP server example, with APIs for RESTful interfaces.

Note
It is assumed that you have followed the steps in the Getting Started guide and are therefore familiar with how to build, flash, and monitor an application using the MM-IoT-SDK framework.

This file contains examples on how to use the lwIP http server. It supports static and dynamically generated pages.

Static pages are specified in the fs/ directory, and on compilation will be automatically converted to C arrays which will be embedded into the binary. Currently the file converter is compiled to run only on x86 linux systems. To support other systems, htmlgen can be recompiled on your development machine from the mainline lwIP sources.

Dynamic pages are specified through the custom file system, restfs, and are registered by specifying the URI and handler function in the rest_endpoints array.

Note that only GET requests are supported at this time.

To view the web-page, navigate to

http://<device_ip>/index.html

on a computer with a route to the Morse Micro IoT device, over HaLow. Alternatively, you can run

curl http://<device_ip>/index.html

from the AP that the device is connected to.

See Application helper routines for Wireless LAN interface for details of WLAN and IP stack configuration.

Definition in file http.c.

#include <string.h>
#include "mmosal.h"
#include "mmwlan.h"
#include "mmipal.h"
#include "lwip/apps/httpd.h"
#include "lwip/def.h"
#include "lwip/mem.h"
#include "lwip/tcpip.h"
#include "restfs.h"
#include "mm_app_common.h"
Include dependency graph for http.c:

Go to the source code of this file.

Functions

static const char * cgi_set_string (int index, int nparams, char *params[], char *values[])
 Example CGI handler to set a global variable based on query parameters. More...
 
static void rest_ep_getstring (struct restfs_file *fil)
 Get the string previously set by set_string. More...
 
static void rest_ep_success (struct restfs_file *fil)
 Example endpoint to return fixed html string. More...
 
static void rest_ep_failed (struct restfs_file *fil)
 Example endpoint to return fixed html string. More...
 
static void rest_ep_hello (struct restfs_file *fil)
 Hello world example endpoint. More...
 
void app_init (void)
 Main entry point to the application. More...
 

Variables

static char cgi_string [32] = {0}
 Buffer to store the string set by cgi_set_string() More...
 
static const struct rest_endpoint rest_endpoints []
 Vector table of rest endpoints. More...
 
static const tCGI cgi_endpoints []
 Vector table of LWIP CGI endpoints. More...
 

Function Documentation

◆ app_init()

void app_init ( void  )

Main entry point to the application.

This will be invoked in a thread once operating system and hardware initialization has completed. It may return, but it does not have to.

Definition at line 172 of file http.c.

◆ cgi_set_string()

static const char * cgi_set_string ( int  index,
int  nparams,
char *  params[],
char *  values[] 
)
static

Example CGI handler to set a global variable based on query parameters.

Query string expected as value=<string to set>; e.g., /rest/set_string?value=test

Parameters
indexThe index of the endpoint in the CGI handlers table.
nparamsNumber of elements in params provided in the URI.
paramsParameter names provided in the URI.
valuesValues corresponding to each parameter given in the URI.
Returns
the URI of the response to return (which can be a REST endpoint).
Note
Strictly speaking GET requests should not be altering state on the server, but for this example we will use it as a lightweight alternative to POST.

See lwIP httpd for more information

Definition at line 79 of file http.c.

◆ rest_ep_failed()

static void rest_ep_failed ( struct restfs_file fil)
static

Example endpoint to return fixed html string.

Parameters
filFile to write to.

Definition at line 126 of file http.c.

◆ rest_ep_getstring()

static void rest_ep_getstring ( struct restfs_file fil)
static

Get the string previously set by set_string.

Parameters
filFile to write to.

Definition at line 102 of file http.c.

◆ rest_ep_hello()

static void rest_ep_hello ( struct restfs_file fil)
static

Hello world example endpoint.

Parameters
filFile to write to.

Definition at line 138 of file http.c.

◆ rest_ep_success()

static void rest_ep_success ( struct restfs_file fil)
static

Example endpoint to return fixed html string.

Parameters
filFile to write to.

Definition at line 114 of file http.c.

Variable Documentation

◆ cgi_endpoints

const tCGI cgi_endpoints[]
static
Initial value:
= {
{"/rest/set_string", cgi_set_string}
}
static const char * cgi_set_string(int index, int nparams, char *params[], char *values[])
Example CGI handler to set a global variable based on query parameters.
Definition: http.c:79

Vector table of LWIP CGI endpoints.

Declare the URI and handlers for CGI endpoints here

Will pass query parameters to function call. For example, <ip_address>/rest/<endpoint>?queryname=queryval&queryname2=queryval2 ... etc.

Definition at line 163 of file http.c.

◆ cgi_string

char cgi_string[32] = {0}
static

Buffer to store the string set by cgi_set_string()

Definition at line 61 of file http.c.

◆ rest_endpoints

const struct rest_endpoint rest_endpoints[]
static
Initial value:
= {
{"success.html", rest_ep_success},
{"failed.html", rest_ep_failed},
{"/rest/hello", rest_ep_hello},
{"/rest/get_string.txt", rest_ep_getstring},
}
static void rest_ep_failed(struct restfs_file *fil)
Example endpoint to return fixed html string.
Definition: http.c:126
static void rest_ep_success(struct restfs_file *fil)
Example endpoint to return fixed html string.
Definition: http.c:114
static void rest_ep_getstring(struct restfs_file *fil)
Get the string previously set by set_string.
Definition: http.c:102
static void rest_ep_hello(struct restfs_file *fil)
Hello world example endpoint.
Definition: http.c:138

Vector table of rest endpoints.

Declare the URI and handlers for REST endpoints here.

For example, HTTP GET on <ip address>/rest/example_endpoint

Definition at line 150 of file http.c.