Add mongoose, add xishutong-arm32.
This commit is contained in:
@@ -20,4 +20,5 @@ menu "Applications"
|
||||
source "$APP_DIR/Applications/sensor_app/Kconfig"
|
||||
source "$APP_DIR/Applications/embedded_database_app/Kconfig"
|
||||
source "$APP_DIR/Applications/webnet/Kconfig"
|
||||
source "$APP_DIR/Applications/mongoose/Kconfig"
|
||||
endmenu
|
||||
|
||||
@@ -39,6 +39,10 @@ ifeq ($(CONFIG_ADD_XIZI_FEATURES),y)
|
||||
ifeq ($(CONFIG_APP_USING_WEBNET),y)
|
||||
SRC_DIR += webnet
|
||||
endif
|
||||
|
||||
ifeq ($(CONFIG_USE_MONGOOSE),y)
|
||||
SRC_DIR += mongoose
|
||||
endif
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
||||
endif
|
||||
3
APP_Framework/Applications/mongoose/Kconfig
Normal file
3
APP_Framework/Applications/mongoose/Kconfig
Normal file
@@ -0,0 +1,3 @@
|
||||
menuconfig USE_MONGOOSE
|
||||
bool "Use mongoose as webserver"
|
||||
default n
|
||||
3
APP_Framework/Applications/mongoose/Makefile
Normal file
3
APP_Framework/Applications/mongoose/Makefile
Normal file
@@ -0,0 +1,3 @@
|
||||
SRC_FILES += mongoose.c netsetting.c project.c
|
||||
|
||||
include $(KERNEL_ROOT)/compiler.mk
|
||||
10610
APP_Framework/Applications/mongoose/mongoose.c
Normal file
10610
APP_Framework/Applications/mongoose/mongoose.c
Normal file
File diff suppressed because it is too large
Load Diff
1757
APP_Framework/Applications/mongoose/mongoose.h
Normal file
1757
APP_Framework/Applications/mongoose/mongoose.h
Normal file
File diff suppressed because it is too large
Load Diff
102
APP_Framework/Applications/mongoose/netsetting.c
Normal file
102
APP_Framework/Applications/mongoose/netsetting.c
Normal file
@@ -0,0 +1,102 @@
|
||||
// Copyright (c) 2022 Cesanta Software Limited
|
||||
// All rights reserved
|
||||
//
|
||||
// UI example
|
||||
// It implements the following endpoints:
|
||||
// /api/config/get - respond with current config
|
||||
// /api/config/set - POST a config change
|
||||
// any other URI serves static files from s_root_dir
|
||||
// Data and results are JSON strings
|
||||
|
||||
#include "ip_addr.h"
|
||||
#include "mongoose.h"
|
||||
#include "netdev.h"
|
||||
|
||||
static const char* s_http_addr = "http://0.0.0.0:8000"; // HTTP port
|
||||
static const char* s_root_dir = "netsetting";
|
||||
|
||||
static struct netdev* p_netdev;
|
||||
|
||||
static struct config {
|
||||
char *ip, *mask, *gw, *dns;
|
||||
} s_config;
|
||||
|
||||
// Try to update a single configuration value
|
||||
static void update_config(struct mg_str json, const char* path, char** value)
|
||||
{
|
||||
char* jval;
|
||||
if ((jval = mg_json_get_str(json, path)) != NULL) {
|
||||
free(*value);
|
||||
*value = strdup(jval);
|
||||
}
|
||||
}
|
||||
|
||||
static void fn(struct mg_connection* c, int ev, void* ev_data, void* fn_data)
|
||||
{
|
||||
if (ev == MG_EV_OPEN && c->is_listening) {
|
||||
s_config.ip = strdup(inet_ntoa(p_netdev->ip_addr));
|
||||
s_config.mask = strdup(inet_ntoa(p_netdev->netmask));
|
||||
s_config.gw = strdup(inet_ntoa(p_netdev->gw));
|
||||
s_config.dns = strdup(inet_ntoa(p_netdev->dns_servers[0]));
|
||||
} else if (ev == MG_EV_HTTP_MSG) {
|
||||
struct mg_http_message* hm = (struct mg_http_message*)ev_data;
|
||||
if (mg_http_match_uri(hm, "/api/config/get")) {
|
||||
mg_http_reply(c, 200, "Content-Type: application/json\r\n",
|
||||
"{%m:%m,%m:%m,%m:%m,%m:%m}\n",
|
||||
MG_ESC("ip"), MG_ESC(s_config.ip),
|
||||
MG_ESC("mask"), MG_ESC(s_config.mask),
|
||||
MG_ESC("gw"), MG_ESC(s_config.gw),
|
||||
MG_ESC("dns"), MG_ESC(s_config.dns));
|
||||
} else if (mg_http_match_uri(hm, "/api/config/set")) {
|
||||
struct mg_str json = hm->body;
|
||||
printf("json: %s\n", json.ptr);
|
||||
update_config(json, "$.ip", &s_config.ip);
|
||||
update_config(json, "$.mask", &s_config.mask);
|
||||
update_config(json, "$.gw", &s_config.gw);
|
||||
update_config(json, "$.dns", &s_config.dns);
|
||||
mg_http_reply(c, 200, "", "ok\n");
|
||||
|
||||
ip_addr_t ipaddr, maskaddr, gwaddr;
|
||||
inet_aton(s_config.ip, &ipaddr);
|
||||
inet_aton(s_config.mask, &maskaddr);
|
||||
inet_aton(s_config.gw, &gwaddr);
|
||||
p_netdev->ops->set_addr_info(p_netdev, &ipaddr, &maskaddr, &gwaddr);
|
||||
|
||||
printf("Board Net Configuration changed to [IP: %s, Mask: %s, GW: %s]\n",
|
||||
s_config.ip,
|
||||
s_config.mask,
|
||||
s_config.gw);
|
||||
} else {
|
||||
struct mg_http_serve_opts opts = { .root_dir = s_root_dir };
|
||||
mg_http_serve_dir(c, ev_data, &opts);
|
||||
}
|
||||
}
|
||||
(void)fn_data;
|
||||
}
|
||||
|
||||
static void* do_net_setting_demo(void* none)
|
||||
{
|
||||
p_netdev = NETDEV_DEFAULT;
|
||||
|
||||
struct mg_mgr mgr; // Event manager
|
||||
mg_log_set(MG_LL_DEBUG); // Set to 3 to enable debug
|
||||
mg_mgr_init(&mgr); // Initialise event manager
|
||||
mg_http_listen(&mgr, s_http_addr, fn, NULL); // Create HTTP listener
|
||||
for (;;)
|
||||
mg_mgr_poll(&mgr, 10); // Infinite event loop
|
||||
mg_mgr_free(&mgr);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int net_setting_demo(int argc, char* argv[])
|
||||
{
|
||||
pthread_t tid = -1;
|
||||
pthread_attr_t attr;
|
||||
attr.schedparam.sched_priority = 30;
|
||||
attr.stacksize = 16384;
|
||||
|
||||
PrivTaskCreate(&tid, &attr, do_net_setting_demo, NULL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(5), NetSettingDemo, net_setting_demo, webserver to set net configurations);
|
||||
140
APP_Framework/Applications/mongoose/project.c
Normal file
140
APP_Framework/Applications/mongoose/project.c
Normal file
@@ -0,0 +1,140 @@
|
||||
// Copyright (c) 2022 Cesanta Software Limited
|
||||
// All rights reserved
|
||||
//
|
||||
// UI example
|
||||
// It implements the following endpoints:
|
||||
// /api/config/get - respond with current config
|
||||
// /api/config/set - POST a config change
|
||||
// any other URI serves static files from s_root_dir
|
||||
// Data and results are JSON strings
|
||||
|
||||
#include "ip_addr.h"
|
||||
#include "mongoose.h"
|
||||
#include "netdev.h"
|
||||
|
||||
char index_path[] = "login.html";
|
||||
|
||||
static const char* s_http_addr = "http://0.0.0.0:8000"; // HTTP port
|
||||
static const char* s_root_dir = "webserver";
|
||||
static const char* s_enable_hexdump = "no";
|
||||
static const char* s_ssi_pattern = "#.html";
|
||||
|
||||
static const char* device_type = "Edu-ARM32";
|
||||
static const char* web_version = "XUOS Webserver 1.0";
|
||||
static int enable_4g = 0;
|
||||
|
||||
static struct netdev* p_netdev;
|
||||
|
||||
static struct config {
|
||||
char *ip, *mask, *gw, *dns;
|
||||
} s_config;
|
||||
|
||||
// Try to update a single configuration value
|
||||
static void update_config(struct mg_str json, const char* path, char** value)
|
||||
{
|
||||
char* jval;
|
||||
if ((jval = mg_json_get_str(json, path)) != NULL) {
|
||||
free(*value);
|
||||
*value = strdup(jval);
|
||||
}
|
||||
}
|
||||
|
||||
static void fn(struct mg_connection* c, int ev, void* ev_data, void* fn_data)
|
||||
{
|
||||
if (ev == MG_EV_HTTP_MSG) {
|
||||
struct mg_http_message *hm = (struct mg_http_message*)ev_data, tmp = { 0 };
|
||||
if (mg_http_match_uri(hm, "/getSystemInfo")) {
|
||||
mg_http_reply(c, 200, "Content-Type: application/json\r\n",
|
||||
"{%m:%m, %m:%m, %m:%m, %m:%m, %m:%m, %m:%d}\n",
|
||||
MG_ESC("ip"), MG_ESC(s_config.ip),
|
||||
MG_ESC("deviceType"), MG_ESC(device_type),
|
||||
MG_ESC("deviceNo"), MG_ESC("0"),
|
||||
MG_ESC("systemTime"), MG_ESC("YYYY:MM:DD hh:mm:ss"),
|
||||
MG_ESC("webVersion"), MG_ESC(web_version),
|
||||
MG_ESC("statusOf4g"), enable_4g);
|
||||
} else if (mg_http_match_uri(hm, "/net/get4gInfo")) {
|
||||
mg_http_reply(c, 200, "Content-Type: application/json\r\n",
|
||||
"{%m:%m}\n",
|
||||
MG_ESC("enable4g"), MG_ESC(enable_4g));
|
||||
} else if (mg_http_match_uri(hm, "/net/set4gInfo")) {
|
||||
struct mg_str json = hm->body;
|
||||
enable_4g = mg_json_get_long(json, "$.enable4g", 0);
|
||||
mg_http_reply(c, 200, "Content-Type: application/json\r\n", "{\"status\":\"success\"}\r\n");
|
||||
printf("Get enable 4g setting: %d\n", enable_4g);
|
||||
} else if (mg_http_match_uri(hm, "/net/getEthernetInfo")) {
|
||||
mg_http_reply(c, 200, "Content-Type: application/json\r\n",
|
||||
"{%m:%m, %m:%m, %m:%m, %m:%m}\n",
|
||||
MG_ESC("ip"), MG_ESC(s_config.ip),
|
||||
MG_ESC("netmask"), MG_ESC(s_config.mask),
|
||||
MG_ESC("gateway"), MG_ESC(s_config.gw),
|
||||
MG_ESC("dns"), MG_ESC(s_config.dns));
|
||||
} else if (mg_http_match_uri(hm, "/net/setEthernetInfo")) {
|
||||
struct mg_str json = hm->body;
|
||||
printf("json: %s\n", json.ptr);
|
||||
update_config(json, "$.ip", &s_config.ip);
|
||||
update_config(json, "$.netmask", &s_config.mask);
|
||||
update_config(json, "$.gateway", &s_config.gw);
|
||||
update_config(json, "$.dns", &s_config.dns);
|
||||
mg_http_reply(c, 200, "Content-Type: application/json\r\n", "{\"status\":\"success\"}\r\n");
|
||||
|
||||
ip_addr_t ipaddr, maskaddr, gwaddr;
|
||||
inet_aton(s_config.ip, &ipaddr);
|
||||
inet_aton(s_config.mask, &maskaddr);
|
||||
inet_aton(s_config.gw, &gwaddr);
|
||||
p_netdev->ops->set_addr_info(p_netdev, &ipaddr, &maskaddr, &gwaddr);
|
||||
|
||||
printf("Board Net Configuration changed to [IP: %s, Mask: %s, GW: %s, DNS: %s]\n",
|
||||
s_config.ip,
|
||||
s_config.mask,
|
||||
s_config.gw,
|
||||
s_config.dns);
|
||||
} else {
|
||||
struct mg_str unknown = mg_str_n("?", 1), *cl;
|
||||
struct mg_http_serve_opts opts = { .root_dir = s_root_dir, .ssi_pattern = s_ssi_pattern };
|
||||
mg_http_serve_dir(c, hm, &opts);
|
||||
mg_http_parse((char*)c->send.buf, c->send.len, &tmp);
|
||||
cl = mg_http_get_header(&tmp, "Content-Length");
|
||||
if (cl == NULL)
|
||||
cl = &unknown;
|
||||
MG_INFO(("%.*s %.*s %.*s %.*s", (int)hm->method.len, hm->method.ptr,
|
||||
(int)hm->uri.len, hm->uri.ptr, (int)tmp.uri.len, tmp.uri.ptr,
|
||||
(int)cl->len, cl->ptr));
|
||||
}
|
||||
}
|
||||
(void)fn_data;
|
||||
}
|
||||
|
||||
extern void LwipSetIPTest(int argc, char* argv[]);
|
||||
static void* do_webserver_demo(void* none)
|
||||
{
|
||||
p_netdev = NETDEV_DEFAULT;
|
||||
s_config.ip = strdup(inet_ntoa(p_netdev->ip_addr));
|
||||
s_config.mask = strdup(inet_ntoa(p_netdev->netmask));
|
||||
s_config.gw = strdup(inet_ntoa(p_netdev->gw));
|
||||
s_config.dns = strdup(inet_ntoa(p_netdev->dns_servers[0]));
|
||||
|
||||
struct mg_mgr mgr; // Event manager
|
||||
mg_log_set(MG_LL_INFO); // Set to 3 to enable debug
|
||||
// mg_log_set(MG_LL_DEBUG); // Set to 3 to enable debug
|
||||
mg_mgr_init(&mgr); // Initialise event manager
|
||||
mg_http_listen(&mgr, s_http_addr, fn, NULL); // Create HTTP listener
|
||||
for (;;)
|
||||
mg_mgr_poll(&mgr, 50); // Infinite event loop
|
||||
mg_mgr_free(&mgr);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int webserver_demo(int argc, char* argv[])
|
||||
{
|
||||
LwipSetIPTest(1, NULL);
|
||||
|
||||
pthread_t tid = -1;
|
||||
pthread_attr_t attr;
|
||||
attr.schedparam.sched_priority = 30;
|
||||
attr.stacksize = 0x4000;
|
||||
|
||||
PrivTaskCreate(&tid, &attr, do_webserver_demo, NULL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN) | SHELL_CMD_PARAM_NUM(5), Webserver, webserver_demo, webserver for project);
|
||||
Reference in New Issue
Block a user