Morse Micro IoT SDK  2.9.7
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
56#if !defined(LWIP_HTTPD_CGI)
57#error "http_rest requires HTTPD CGI"
58#endif
59
61static char cgi_string[32] = {0};
62
79static const char* cgi_set_string(int index, int nparams, char *params[], char *values[])
80{
81 int i;
82
83 (void)index;
84
85 for (i = 0; i < nparams; i++)
86 {
87 if (!strncmp(params[i], "value", sizeof("value")))
88 {
89 mmosal_safer_strcpy(cgi_string, values[i], sizeof(cgi_string));
90 return "success.html";
91 }
92 }
93
94 return "failed.html";
95}
96
102static void rest_ep_getstring(struct restfs_file *fil)
103{
104 restfs_alloc_buffer(fil, sizeof(cgi_string));
105
106 restfs_write(fil, (const uint8_t *)cgi_string, strlen(cgi_string));
107}
108
114static void rest_ep_success(struct restfs_file *fil)
115{
116 static const char successhtml[] = "<html><body>Success</body></html>";
117
118 restfs_write_const(fil, successhtml);
119}
120
126static void rest_ep_failed(struct restfs_file *fil)
127{
128 static const char failedhtml[] = "<html><body>Failed</body></html>";
129
130 restfs_write_const(fil, failedhtml);
131}
132
138static void rest_ep_hello(struct restfs_file *fil)
139{
140 restfs_alloc_buffer(fil, 20);
141
142 restfs_printf(fil, "Hello World");
143}
144
150static const struct rest_endpoint rest_endpoints[] = {
151 {"success.html", rest_ep_success},
152 {"failed.html", rest_ep_failed},
153 {"/rest/hello", rest_ep_hello},
154 {"/rest/get_string.txt", rest_ep_getstring},
155};
156
163static const tCGI cgi_endpoints[] = {
164 {"/rest/set_string", cgi_set_string}
165};
166
167
172void app_init(void)
173{
174 printf("\n\nMorse HTTP Demo (Built " __DATE__ " " __TIME__ ")\n\n");
175
176 /* Initialize and connect to Wi-Fi, blocks till connected */
179
180 LOCK_TCPIP_CORE();
181 rest_init_endpoints(rest_endpoints, LWIP_ARRAYSIZE(rest_endpoints));
182 http_set_cgi_handlers(cgi_endpoints, LWIP_ARRAYSIZE(cgi_endpoints));
183 httpd_init();
184 UNLOCK_TCPIP_CORE();
185
186 /* We idle till we get a connection */
187}
static bool mmosal_safer_strcpy(char *dst, const char *src, size_t size)
A safer version of strncpy.
Definition: mmosal.h:1030
static void rest_ep_failed(struct restfs_file *fil)
Example endpoint to return fixed html string.
Definition: http.c:126
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
static const struct rest_endpoint rest_endpoints[]
Vector table of rest endpoints.
Definition: http.c:150
static void rest_ep_success(struct restfs_file *fil)
Example endpoint to return fixed html string.
Definition: http.c:114
static const tCGI cgi_endpoints[]
Vector table of LWIP CGI endpoints.
Definition: http.c:163
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
static char cgi_string[32]
Buffer to store the string set by cgi_set_string()
Definition: http.c:61
void app_init(void)
Main entry point to the application.
Definition: http.c:172
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:28
Opaque object used for writing REST output data.
Definition: restfs.c:20