Skip to content

Commit

Permalink
Use V307 flash
Browse files Browse the repository at this point in the history
  • Loading branch information
cpq committed Nov 30, 2023
1 parent c74534c commit 85d3e7f
Show file tree
Hide file tree
Showing 17 changed files with 1,518 additions and 1,064 deletions.
2 changes: 1 addition & 1 deletion examples/modbus-dashboard/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
22 changes: 22 additions & 0 deletions examples/modbus-dashboard/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
54 changes: 32 additions & 22 deletions examples/modbus-dashboard/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,35 @@
#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 {
uint64_t expiration_time; // Modbus request timeout
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;
Expand All @@ -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", //
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion examples/modbus-dashboard/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Loading

0 comments on commit 85d3e7f

Please sign in to comment.