2022-06-08 08:58:32 +01:00
|
|
|
// Copyright (c) 2022 Cesanta Software Limited
|
|
|
|
// All rights reserved
|
|
|
|
|
|
|
|
#include "mongoose.h"
|
|
|
|
|
2022-06-18 04:25:05 +01:00
|
|
|
#define DEFAULT_TCP "tcp://0.0.0.0:4001"
|
|
|
|
#define DEFAULT_WEBSOCKET "ws://0.0.0.0:4002"
|
|
|
|
#define DEFAULT_MQTT "mqtt://broker.hivemq.com:1883?tx=b/tx&rx=b/rx"
|
|
|
|
|
|
|
|
struct endpoint {
|
|
|
|
char *url;
|
|
|
|
bool enable;
|
|
|
|
struct mg_connection *c;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct state {
|
|
|
|
struct endpoint tcp, websocket, mqtt;
|
|
|
|
int tx, rx, baud;
|
|
|
|
} s_state = {.tcp = {.enable = true},
|
|
|
|
.websocket = {.enable = true},
|
2022-06-19 18:27:37 +01:00
|
|
|
.mqtt = {.enable = false},
|
2022-06-18 04:25:05 +01:00
|
|
|
.tx = 5,
|
|
|
|
.rx = 4,
|
|
|
|
.baud = 115200};
|
2022-06-08 08:58:32 +01:00
|
|
|
|
2022-06-11 17:29:45 +01:00
|
|
|
void uart_init(int tx, int rx, int baud);
|
2022-06-18 20:41:04 +01:00
|
|
|
int uart_read(void *buf, size_t len);
|
2022-06-11 17:29:45 +01:00
|
|
|
void uart_write(const void *buf, size_t len);
|
2022-06-18 20:41:04 +01:00
|
|
|
void config_write(struct mg_str config);
|
2022-06-11 17:29:45 +01:00
|
|
|
|
2022-06-08 08:58:32 +01:00
|
|
|
// Let users define their own UART API. If they don't, use a dummy one
|
|
|
|
#if defined(UART_API_IMPLEMENTED)
|
|
|
|
#else
|
|
|
|
void uart_init(int tx, int rx, int baud) {
|
|
|
|
// We use stdin/stdout as UART. Make stdin non-blocking
|
2023-02-23 11:51:21 -03:00
|
|
|
#if MG_ARCH != MG_ARCH_WIN32
|
2022-06-08 08:58:32 +01:00
|
|
|
fcntl(0, F_SETFL, fcntl(0, F_GETFL) | O_NONBLOCK);
|
2023-02-23 11:51:21 -03:00
|
|
|
#endif
|
2022-06-08 08:58:32 +01:00
|
|
|
(void) tx, (void) rx, (void) baud;
|
|
|
|
}
|
|
|
|
|
|
|
|
void uart_write(const void *buf, size_t len) {
|
|
|
|
fwrite(buf, 1, len, stdout); // Write to stdout
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
2022-06-18 20:41:04 +01:00
|
|
|
int uart_read(void *buf, size_t len) {
|
2023-02-23 11:51:21 -03:00
|
|
|
#if MG_ARCH == MG_ARCH_WIN32
|
|
|
|
(void) buf, (void) len;
|
|
|
|
return 0;
|
|
|
|
#else
|
2022-06-08 08:58:32 +01:00
|
|
|
return read(0, buf, len); // Read from stdin
|
2023-02-23 11:51:21 -03:00
|
|
|
#endif
|
2022-06-08 08:58:32 +01:00
|
|
|
}
|
2022-06-18 11:30:44 +01:00
|
|
|
|
|
|
|
void config_write(struct mg_str config) {
|
|
|
|
mg_file_write(&mg_fs_posix, "config.json", config.ptr, config.len);
|
|
|
|
}
|
2022-06-08 08:58:32 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Event handler for a connected Websocket client
|
2024-01-08 17:34:34 -03:00
|
|
|
static void ws_fn(struct mg_connection *c, int ev, void *ev_data) {
|
2022-06-08 08:58:32 +01:00
|
|
|
if (ev == MG_EV_HTTP_MSG) {
|
2024-01-08 17:34:34 -03:00
|
|
|
mg_ws_upgrade(c, ev_data, NULL);
|
2022-06-08 08:58:32 +01:00
|
|
|
} else if (ev == MG_EV_WS_OPEN) {
|
|
|
|
// c->is_hexdumping = 1;
|
2023-01-09 10:58:07 +00:00
|
|
|
c->data[0] = 'W'; // When WS handhake is done, mark us as WS client
|
2022-06-08 08:58:32 +01:00
|
|
|
} else if (ev == MG_EV_WS_MSG) {
|
2024-01-08 17:34:34 -03:00
|
|
|
struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
|
2022-06-08 08:58:32 +01:00
|
|
|
uart_write(wm->data.ptr, wm->data.len); // Send to UART
|
|
|
|
c->recv.len = 0; // Discard received data
|
|
|
|
} else if (ev == MG_EV_CLOSE) {
|
2022-06-18 04:25:05 +01:00
|
|
|
if (c->is_listening) s_state.websocket.c = NULL;
|
2022-06-08 08:58:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Event handler for a connected TCP client
|
2024-01-08 17:34:34 -03:00
|
|
|
static void tcp_fn(struct mg_connection *c, int ev, void *ev_data) {
|
2022-07-21 15:05:01 -03:00
|
|
|
if (ev == MG_EV_ACCEPT) {
|
2022-06-08 08:58:32 +01:00
|
|
|
// c->is_hexdumping = 1;
|
2023-01-09 10:58:07 +00:00
|
|
|
c->data[0] = 'T'; // When client is connected, mark us as TCP client
|
2022-06-08 08:58:32 +01:00
|
|
|
} else if (ev == MG_EV_READ) {
|
|
|
|
uart_write(c->recv.buf, c->recv.len); // Send to UART
|
|
|
|
c->recv.len = 0; // Discard received data
|
|
|
|
} else if (ev == MG_EV_CLOSE) {
|
2022-06-18 04:25:05 +01:00
|
|
|
if (c->is_listening) s_state.tcp.c = NULL;
|
|
|
|
}
|
2024-01-08 17:34:34 -03:00
|
|
|
(void) ev_data;
|
2022-06-18 04:25:05 +01:00
|
|
|
}
|
|
|
|
|
2022-06-18 11:30:44 +01:00
|
|
|
// Extract topic name from the MQTT address
|
|
|
|
static struct mg_str mqtt_topic(const char *name, const char *dflt) {
|
|
|
|
struct mg_str qs = mg_str(strchr(s_state.mqtt.url, '?'));
|
|
|
|
struct mg_str v = mg_http_var(qs, mg_str(name));
|
|
|
|
return v.ptr == NULL ? mg_str(dflt) : v;
|
2022-06-18 04:25:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Event handler for MQTT connection
|
2024-01-08 17:34:34 -03:00
|
|
|
static void mq_fn(struct mg_connection *c, int ev, void *ev_data) {
|
2022-06-18 04:25:05 +01:00
|
|
|
if (ev == MG_EV_OPEN) {
|
|
|
|
// c->is_hexdumping = 1;
|
|
|
|
} else if (ev == MG_EV_MQTT_OPEN) {
|
2023-01-09 10:58:07 +00:00
|
|
|
c->data[0] = 'M';
|
2023-04-20 09:59:27 +03:00
|
|
|
struct mg_mqtt_opts sub_opts;
|
|
|
|
memset(&sub_opts, 0, sizeof(sub_opts));
|
|
|
|
sub_opts.topic = mqtt_topic("rx", "b/rx");
|
|
|
|
sub_opts.qos = 1;
|
|
|
|
mg_mqtt_sub(c, &sub_opts); // Subscribe to RX topic
|
2022-06-18 04:25:05 +01:00
|
|
|
} else if (ev == MG_EV_MQTT_MSG) {
|
2024-01-08 17:34:34 -03:00
|
|
|
struct mg_mqtt_message *mm = ev_data; // MQTT message
|
2022-06-18 04:25:05 +01:00
|
|
|
uart_write(mm->data.ptr, mm->data.len); // Send to UART
|
|
|
|
} else if (ev == MG_EV_CLOSE) {
|
|
|
|
s_state.mqtt.c = NULL;
|
2022-06-08 08:58:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Software timer with a frequency close to the scheduling time slot
|
|
|
|
static void timer_fn(void *param) {
|
|
|
|
// Start listeners if they're stopped for any reason
|
|
|
|
struct mg_mgr *mgr = (struct mg_mgr *) param;
|
2022-06-18 04:25:05 +01:00
|
|
|
if (s_state.tcp.c == NULL && s_state.tcp.enable) {
|
|
|
|
s_state.tcp.c = mg_listen(mgr, s_state.tcp.url, tcp_fn, 0);
|
|
|
|
}
|
|
|
|
if (s_state.websocket.c == NULL && s_state.websocket.enable) {
|
|
|
|
s_state.websocket.c = mg_http_listen(mgr, s_state.websocket.url, ws_fn, 0);
|
|
|
|
}
|
|
|
|
if (s_state.mqtt.c == NULL && s_state.mqtt.enable) {
|
2022-06-18 11:30:44 +01:00
|
|
|
struct mg_mqtt_opts opts = {.clean = true};
|
2022-06-18 04:25:05 +01:00
|
|
|
s_state.mqtt.c = mg_mqtt_connect(mgr, s_state.mqtt.url, &opts, mq_fn, 0);
|
|
|
|
}
|
2022-06-08 08:58:32 +01:00
|
|
|
|
|
|
|
// Read UART
|
|
|
|
char buf[512];
|
|
|
|
int len = uart_read(buf, sizeof(buf));
|
|
|
|
if (len > 0) {
|
|
|
|
// Iterate over all connections. Send data to WS and TCP clients
|
|
|
|
for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next) {
|
2023-01-09 10:58:07 +00:00
|
|
|
if (c->data[0] == 'W') mg_ws_send(c, buf, len, WEBSOCKET_OP_TEXT);
|
|
|
|
if (c->data[0] == 'T') mg_send(c, buf, len);
|
2023-04-20 09:59:27 +03:00
|
|
|
if (c->data[0] == 'M') {
|
|
|
|
struct mg_mqtt_opts pub_opts;
|
|
|
|
memset(&pub_opts, 0, sizeof(pub_opts));
|
|
|
|
pub_opts.topic = mqtt_topic("tx", "b/tx");
|
|
|
|
pub_opts.message = mg_str_n(buf, len);
|
|
|
|
pub_opts.qos = 1, pub_opts.retain = false;
|
|
|
|
mg_mqtt_pub(c, &pub_opts);
|
|
|
|
}
|
2022-06-08 08:58:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-21 15:05:01 -03:00
|
|
|
static void update_string(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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-18 11:30:44 +01:00
|
|
|
static void config_apply(struct mg_str s) {
|
|
|
|
MG_INFO(("Applying config: %.*s", (int) s.len, s.ptr));
|
|
|
|
|
2022-07-21 15:05:01 -03:00
|
|
|
bool b;
|
|
|
|
if (mg_json_get_bool(s, "$.tcp.enable", &b)) s_state.tcp.enable = b;
|
|
|
|
if (mg_json_get_bool(s, "$.ws.enable", &b)) s_state.websocket.enable = b;
|
|
|
|
if (mg_json_get_bool(s, "$.mqtt.enable", &b)) s_state.mqtt.enable = b;
|
2022-06-18 11:30:44 +01:00
|
|
|
|
2022-07-21 15:05:01 -03:00
|
|
|
update_string(s, "$.tcp.url", &s_state.tcp.url);
|
|
|
|
update_string(s, "$.mqtt.url", &s_state.mqtt.url);
|
|
|
|
update_string(s, "$.ws.url", &s_state.websocket.url);
|
2022-06-18 11:30:44 +01:00
|
|
|
|
|
|
|
double v;
|
|
|
|
if (mg_json_get_num(s, "$.rx", &v)) s_state.rx = (int) v;
|
|
|
|
if (mg_json_get_num(s, "$.tx", &v)) s_state.tx = (int) v;
|
|
|
|
if (mg_json_get_num(s, "$.baud", &v)) s_state.baud = (int) v;
|
|
|
|
|
2023-08-17 16:45:10 -03:00
|
|
|
if (s_state.mqtt.c) s_state.mqtt.c->is_draining = 1;
|
|
|
|
if (s_state.tcp.c) s_state.tcp.c->is_draining = 1;
|
|
|
|
if (s_state.websocket.c) s_state.websocket.c->is_draining = 1;
|
2022-06-18 11:30:44 +01:00
|
|
|
}
|
|
|
|
|
2022-06-08 08:58:32 +01:00
|
|
|
// HTTP request handler function
|
2024-02-16 10:44:56 +00:00
|
|
|
void uart_bridge_fn(struct mg_connection *c, int ev, void *ev_data) {
|
2022-06-08 08:58:32 +01:00
|
|
|
if (ev == MG_EV_OPEN && c->is_listening) {
|
2024-02-16 10:44:56 +00:00
|
|
|
struct mg_str config = mg_file_read(&mg_fs_posix, "config.json");
|
|
|
|
if (config.ptr != NULL) config_apply(config);
|
|
|
|
free((char *) config.ptr);
|
2022-06-18 04:25:05 +01:00
|
|
|
s_state.tcp.url = strdup(DEFAULT_TCP);
|
|
|
|
s_state.websocket.url = strdup(DEFAULT_WEBSOCKET);
|
|
|
|
s_state.mqtt.url = strdup(DEFAULT_MQTT);
|
2022-06-08 08:58:32 +01:00
|
|
|
mg_timer_add(c->mgr, 20, MG_TIMER_REPEAT, timer_fn, c->mgr);
|
2022-06-18 04:25:05 +01:00
|
|
|
uart_init(s_state.tx, s_state.rx, s_state.baud);
|
2022-08-01 11:19:32 +01:00
|
|
|
// mg_log_set(MG_LL_DEBUG); // Set log level
|
2022-06-08 08:58:32 +01:00
|
|
|
} 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/hi")) {
|
|
|
|
mg_http_reply(c, 200, "", "hi\n"); // Testing endpoint
|
2022-06-18 11:30:44 +01:00
|
|
|
} else if (mg_http_match_uri(hm, "/api/config/set")) {
|
|
|
|
config_apply(hm->body);
|
|
|
|
config_write(hm->body);
|
|
|
|
mg_http_reply(c, 200, "", "true\n");
|
2022-06-08 08:58:32 +01:00
|
|
|
} else if (mg_http_match_uri(hm, "/api/config/get")) {
|
2022-06-18 04:25:05 +01:00
|
|
|
mg_http_reply(c, 200, "Content-Type: application/json\r\n",
|
2023-04-14 10:11:12 -03:00
|
|
|
"{%m:{%m:%m,%m:%s},%m:{%m:%m,%m:%s},%m:{%m:%m,%m:%s},"
|
|
|
|
"%m:%d,%m:%d,%m:%d}\n",
|
|
|
|
mg_print_esc, 0, "tcp", mg_print_esc, 0, "url",
|
|
|
|
mg_print_esc, 0, s_state.tcp.url, mg_print_esc, 0, "enable",
|
|
|
|
s_state.tcp.enable ? "true" : "false", mg_print_esc, 0,
|
|
|
|
"ws", mg_print_esc, 0, "url", mg_print_esc, 0,
|
|
|
|
s_state.websocket.url, mg_print_esc, 0, "enable",
|
|
|
|
s_state.websocket.enable ? "true" : "false", mg_print_esc,
|
|
|
|
0, "mqtt", mg_print_esc, 0, "url", mg_print_esc, 0,
|
|
|
|
s_state.mqtt.url, mg_print_esc, 0, "enable",
|
|
|
|
s_state.mqtt.enable ? "true" : "false", mg_print_esc, 0,
|
|
|
|
"rx", s_state.rx, mg_print_esc, 0, "tx", s_state.tx,
|
|
|
|
mg_print_esc, 0, "baud", s_state.baud);
|
2022-06-08 08:58:32 +01:00
|
|
|
} else {
|
2023-02-23 11:32:53 -03:00
|
|
|
struct mg_http_serve_opts opts;
|
|
|
|
memset(&opts, 0, sizeof(opts));
|
2022-06-18 11:30:44 +01:00
|
|
|
#if 1
|
2022-06-08 08:58:32 +01:00
|
|
|
opts.root_dir = "/web_root";
|
|
|
|
opts.fs = &mg_fs_packed;
|
|
|
|
#else
|
|
|
|
opts.root_dir = "web_root";
|
|
|
|
#endif
|
|
|
|
mg_http_serve_dir(c, ev_data, &opts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|