mirror of
https://github.com/cesanta/mongoose.git
synced 2026-10-29 12:41:03 +00:00
Use V307 flash
This commit is contained in:
1 parent
c74534cf5e
commit
85d3e7f375
17 files changed
+1518
-1064
No files matched your search
@@ -52,4 +52,4 @@ endif
|
||||
|
||||
# Cleanup. Delete built program and all build artifacts
|
||||
clean:
|
||||
$(DELETE) $(PROG) $(PACK) *.o *.obj *.exe *.dSYM mbedtls
|
||||
$(DELETE) $(PROG) $(PACK) *.o *.obj *.exe *.bin *.dSYM mbedtls
|
||||
@@ -4,12 +4,34 @@
|
||||
#include "mongoose.h"
|
||||
#include "net.h"
|
||||
|
||||
#define CONFIG_FILE "settings.bin"
|
||||
|
||||
static int s_sig_num;
|
||||
static void signal_handler(int sig_num) {
|
||||
signal(sig_num, signal_handler);
|
||||
s_sig_num = sig_num;
|
||||
}
|
||||
|
||||
bool web_load_settings(void *buf, size_t len) {
|
||||
bool ok = false;
|
||||
size_t size = 0;
|
||||
void *data = mg_file_read(&mg_fs_posix, CONFIG_FILE, &size);
|
||||
if (data == NULL) {
|
||||
MG_ERROR(("Error reading %s", CONFIG_FILE));
|
||||
} else if (size != len) {
|
||||
MG_ERROR(("%s size != %lu", CONFIG_FILE, len));
|
||||
} else {
|
||||
memcpy(buf, data, len);
|
||||
}
|
||||
free(data);
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool web_save_settings(void *buf, size_t len) {
|
||||
MG_INFO(("Saving to %s", CONFIG_FILE));
|
||||
return mg_file_write(&mg_fs_posix, CONFIG_FILE, buf, len);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
struct mg_mgr mgr;
|
||||
|
||||
|
||||
@@ -4,12 +4,13 @@
|
||||
#include "net.h"
|
||||
|
||||
// Device settings
|
||||
struct settings {
|
||||
struct device_settings {
|
||||
uint32_t magic;
|
||||
int log_level;
|
||||
bool mqtt_enabled;
|
||||
char *mqtt_server_url;
|
||||
char *mqtt_topic_tx;
|
||||
char *mqtt_topic_rx;
|
||||
char mqtt_server_url[64];
|
||||
char mqtt_topic_tx[16];
|
||||
char mqtt_topic_rx[16];
|
||||
};
|
||||
|
||||
struct conndata {
|
||||
@@ -17,13 +18,21 @@ struct conndata {
|
||||
unsigned long id; // Connection ID waiting for the Modbus response
|
||||
};
|
||||
|
||||
static struct settings s_settings = {3, false, NULL, NULL, NULL};
|
||||
|
||||
static struct device_settings s_settings;
|
||||
static const char *s_json_header =
|
||||
"Content-Type: application/json\r\n"
|
||||
"Cache-Control: no-cache\r\n";
|
||||
static uint64_t s_boot_timestamp = 0; // Updated by SNTP
|
||||
|
||||
static void set_default_settings(struct device_settings *s) {
|
||||
s->magic = SETTINGS_MAGIC;
|
||||
s->log_level = MG_LL_DEBUG;
|
||||
mg_snprintf(s->mqtt_server_url, sizeof(s->mqtt_server_url), "%s",
|
||||
"mqtt://broker.hivemq.com:1883");
|
||||
mg_snprintf(s->mqtt_topic_tx, sizeof(s->mqtt_topic_tx), "%s", "modbus1/tx");
|
||||
mg_snprintf(s->mqtt_topic_rx, sizeof(s->mqtt_topic_rx), "%s", "modbus1/rx");
|
||||
}
|
||||
|
||||
// This is for newlib and TLS (mbedTLS)
|
||||
uint64_t mg_now(void) {
|
||||
return mg_millis() + s_boot_timestamp;
|
||||
@@ -50,25 +59,28 @@ static void timer_sntp_fn(void *param) {
|
||||
mg_sntp_connect(param, "udp://time.google.com:123", sfn, NULL);
|
||||
}
|
||||
|
||||
static void setfromjson(struct mg_str json, const char *jsonpath, char **dst) {
|
||||
static void setfromjson(struct mg_str json, const char *jsonpath, char *buf,
|
||||
size_t len) {
|
||||
char *val = mg_json_get_str(json, jsonpath);
|
||||
if (val != NULL) {
|
||||
free(*dst);
|
||||
*dst = val;
|
||||
}
|
||||
if (val != NULL) mg_snprintf(buf, len, "%s", val);
|
||||
free(val);
|
||||
}
|
||||
|
||||
static void handle_settings_set(struct mg_connection *c, struct mg_str body) {
|
||||
struct settings settings;
|
||||
struct device_settings settings;
|
||||
memset(&settings, 0, sizeof(settings));
|
||||
set_default_settings(&settings);
|
||||
mg_json_get_bool(body, "$.mqtt_enabled", &settings.mqtt_enabled);
|
||||
settings.log_level = mg_json_get_long(body, "$.log_level", MG_LL_INFO);
|
||||
setfromjson(body, "$.mqtt_server_url", &settings.mqtt_server_url);
|
||||
setfromjson(body, "$.mqtt_topic_rx", &settings.mqtt_topic_rx);
|
||||
setfromjson(body, "$.mqtt_topic_tx", &settings.mqtt_topic_tx);
|
||||
setfromjson(body, "$.mqtt_server_url", settings.mqtt_server_url,
|
||||
sizeof(settings.mqtt_server_url));
|
||||
setfromjson(body, "$.mqtt_topic_rx", settings.mqtt_topic_rx,
|
||||
sizeof(settings.mqtt_topic_rx));
|
||||
setfromjson(body, "$.mqtt_topic_tx", settings.mqtt_topic_tx,
|
||||
sizeof(settings.mqtt_topic_tx));
|
||||
|
||||
s_settings = settings; // TODO: save to the device flash
|
||||
bool ok = true;
|
||||
s_settings = settings;
|
||||
bool ok = web_save_settings(&s_settings, sizeof(s_settings));
|
||||
mg_http_reply(c, 200, s_json_header,
|
||||
"{%m:%s,%m:%m}", //
|
||||
MG_ESC("status"), ok ? "true" : "false", //
|
||||
@@ -146,7 +158,7 @@ static struct mg_connection *start_modbus_request(struct mg_mgr *mgr,
|
||||
send16(c, reg); // Start register
|
||||
send16(c, nregs); // Number of registers
|
||||
|
||||
if (func == 16) { // Fill in register values to write
|
||||
if (func == 16) { // Fill in register values to write
|
||||
send8(c, (uint8_t) (nregs * 2)); // Send number of bytes
|
||||
for (uint16_t i = 0; i < nregs; i++) {
|
||||
char path[20];
|
||||
@@ -283,10 +295,8 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||
}
|
||||
|
||||
void web_init(struct mg_mgr *mgr) {
|
||||
// Init default settings
|
||||
s_settings.mqtt_server_url = strdup("mqtt://broker.hivemq.com:1883");
|
||||
s_settings.mqtt_topic_tx = strdup("modbus1/tx");
|
||||
s_settings.mqtt_topic_rx = strdup("modbus1/rx");
|
||||
set_default_settings(&s_settings);
|
||||
web_load_settings(&s_settings, sizeof(s_settings));
|
||||
|
||||
mg_http_listen(mgr, HTTP_URL, fn, NULL);
|
||||
mg_http_listen(mgr, HTTPS_URL, fn, (void *) 1);
|
||||
|
||||
@@ -16,7 +16,11 @@ extern "C" {
|
||||
#define HTTPS_URL "https://0.0.0.0:8443"
|
||||
#endif
|
||||
|
||||
void web_init(struct mg_mgr *mgr);
|
||||
void web_init(struct mg_mgr *mgr); // Initialise Web UI
|
||||
bool web_load_settings(void *buf, size_t len); // Load from disk/flash
|
||||
bool web_save_settings(void *buf, size_t len); // Save to disk/flash
|
||||
|
||||
#define SETTINGS_MAGIC 0xaabbccdd
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -298,13 +298,14 @@ function Settings({}) {
|
||||
return html`
|
||||
<div class="m-4 grid grid-cols-1 gap-4 md:grid-cols-2">
|
||||
|
||||
${saveResult && html`<${Notification} ok=${saveResult.status}
|
||||
text=${saveResult.message} close=${() => setSaveResult(null)} />`}
|
||||
|
||||
<div class="py-1 divide-y border rounded bg-white flex flex-col">
|
||||
<div class="font-light uppercase flex items-center text-gray-600 px-4 py-2">
|
||||
MQTT Forwarding
|
||||
<//>
|
||||
<div class="py-2 px-5 flex-1 flex flex-col relative">
|
||||
${saveResult && html`<${Notification} ok=${saveResult.status}
|
||||
text=${saveResult.message} close=${() => setSaveResult(null)} />`}
|
||||
<${Setting} title="Enable MQTT forwarding" value=${settings.mqtt_enabled} setfn=${mksetfn('mqtt_enabled')} type="switch" />
|
||||
<${Setting} title="MQTT Server URL" value=${settings.mqtt_server_url} setfn=${mksetfn('mqtt_server_url')} type="" disabled=${!settings.mqtt_enabled} />
|
||||
<${Setting} title="MQTT Topic RX" value=${settings.mqtt_topic_rx} setfn=${mksetfn('mqtt_topic_rx')} type="" disabled=${!settings.mqtt_enabled} />
|
||||
@@ -335,8 +336,6 @@ function Settings({}) {
|
||||
Console Log
|
||||
<//>
|
||||
<div class="py-2 px-5 flex-1 flex flex-col relative">
|
||||
${saveResult && html`<${Notification} ok=${saveResult.status}
|
||||
text=${saveResult.message} close=${() => setSaveResult(null)} />`}
|
||||
<${Setting} title="Log Level" value=${settings.log_level} setfn=${mksetfn('log_level')} type="select" addonLeft="0-3" options=${logOptions} />
|
||||
<div class="mb-1 mt-3 flex place-content-end"><${Button} icon=${Icons.save} onclick=${onsave} title="Save Settings" /><//>
|
||||
<//>
|
||||
|
||||
Reference in new issue
Block a user