Morse Micro IoT SDK  2.10.4
http.c
Go to the documentation of this file.
1/*
2 * Copyright 2022-2023 Morse Micro
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
41#include <string.h>
42#include "mmosal.h"
43#include "mmwlan.h"
44
45#include "mmipal.h"
46#include "lwip/apps/httpd.h"
47
48#include "lwip/def.h"
49#include "lwip/mem.h"
50#include "lwip/tcpip.h"
51
52#include "restfs.h"
53#include "mm_app_common.h"
54
55#if !defined(LWIP_HTTPD_CGI)
56#error "http_rest requires HTTPD CGI"
57#endif
58
60static char cgi_string[32] = { 0 };
61
78static const char *cgi_set_string(int index, int nparams, char *params[], char *values[])
79{
80 int i;
81
82 (void)index;
83
84 for (i = 0; i < nparams; i++)
85 {
86 if (!strncmp(params[i], "value", sizeof("value")))
87 {
88 mmosal_safer_strcpy(cgi_string, values[i], sizeof(cgi_string));
89 return "success.html";
90 }
91 }
92
93 return "failed.html";
94}
95
101static void rest_ep_getstring(struct restfs_file *fil)
102{
103 restfs_alloc_buffer(fil, sizeof(cgi_string));
104
105 restfs_write(fil, (const uint8_t *)cgi_string, strlen(cgi_string));
106}
107
113static void rest_ep_success(struct restfs_file *fil)
114{
115 static const char successhtml[] = "<html><body>Success</body></html>";
116
117 restfs_write_const(fil, successhtml);
118}
119
125static void rest_ep_failed(struct restfs_file *fil)
126{
127 static const char failedhtml[] = "<html><body>Failed</body></html>";
128
129 restfs_write_const(fil, failedhtml);
130}
131
137static void rest_ep_hello(struct restfs_file *fil)
138{
139 restfs_alloc_buffer(fil, 20);
140
141 restfs_printf(fil, "Hello World");
142}
143
149static const struct rest_endpoint rest_endpoints[] = {
150 { "success.html", rest_ep_success },
151 { "failed.html", rest_ep_failed },
152 { "/rest/hello", rest_ep_hello },
153 { "/rest/get_string.txt", rest_ep_getstring },
154};
155
162static const tCGI cgi_endpoints[] = { { "/rest/set_string", cgi_set_string } };
163
168void app_init(void)
169{
170 printf("\n\nMorse HTTP Demo (Built " __DATE__ " " __TIME__ ")\n\n");
171
172 /* Initialize and connect to Wi-Fi, blocks till connected */
175
176 LOCK_TCPIP_CORE();
177 rest_init_endpoints(rest_endpoints, LWIP_ARRAYSIZE(rest_endpoints));
178 http_set_cgi_handlers(cgi_endpoints, LWIP_ARRAYSIZE(cgi_endpoints));
179 httpd_init();
180 UNLOCK_TCPIP_CORE();
181
182 /* We idle till we get a connection */
183}
static bool mmosal_safer_strcpy(char *dst, const char *src, size_t size)
A safer version of strncpy.
Definition: mmosal.h:1039
static void rest_ep_failed(struct restfs_file *fil)
Example endpoint to return fixed html string.
Definition: http.c:125
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:78
static const struct rest_endpoint rest_endpoints[]
Vector table of rest endpoints.
Definition: http.c:149
static void rest_ep_success(struct restfs_file *fil)
Example endpoint to return fixed html string.
Definition: http.c:113
static const tCGI cgi_endpoints[]
Vector table of LWIP CGI endpoints.
Definition: http.c:162
static void rest_ep_getstring(struct restfs_file *fil)
Get the string previously set by set_string.
Definition: http.c:101
static void rest_ep_hello(struct restfs_file *fil)
Hello world example endpoint.
Definition: http.c:137
static char cgi_string[32]
Buffer to store the string set by cgi_set_string()
Definition: http.c:60
void app_init(void)
Main entry point to the application.
Definition: http.c:168
Morse Micro application helper routines for initializing/de-initializing the Wireless LAN interface a...
void app_wlan_init(void)
Initializes the WLAN interface (and dependencies) using settings specified in the config store.
void app_wlan_start(void)
Starts the WLAN interface and connects to Wi-Fi using settings specified in the config store.
A REST endpoint.
Definition: restfs.h:29
Opaque object used for writing REST output data.
Definition: restfs.c:21