2022-06-08 15:58:32 +08:00
|
|
|
// Copyright (c) 2022 Cesanta Software Limited
|
|
|
|
// All rights reserved
|
|
|
|
|
|
|
|
#include "mongoose.h"
|
|
|
|
|
2022-06-18 11:25:05 +08: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-20 01:27:37 +08:00
|
|
|
.mqtt = {.enable = false},
|
2022-06-18 11:25:05 +08:00
|
|
|
.tx = 5,
|
|
|
|
.rx = 4,
|
|
|
|
.baud = 115200};
|
2022-06-08 15:58:32 +08:00
|
|
|
|
2022-06-12 00:29:45 +08:00
|
|
|
void uart_init(int tx, int rx, int baud);
|
2022-06-19 03:41:04 +08:00
|
|
|
int uart_read(void *buf, size_t len);
|
2022-06-12 00:29:45 +08:00
|
|
|
void uart_write(const void *buf, size_t len);
|
2022-06-19 03:41:04 +08:00
|
|
|
char *config_read(void);
|
|
|
|
void config_write(struct mg_str config);
|
2022-06-12 00:29:45 +08:00
|
|
|
|
2022-06-08 15:58:32 +08: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 22:51:21 +08:00
|
|
|
#if MG_ARCH != MG_ARCH_WIN32
|
2022-06-08 15:58:32 +08:00
|
|
|
fcntl(0, F_SETFL, fcntl(0, F_GETFL) | O_NONBLOCK);
|
2023-02-23 22:51:21 +08:00
|
|
|
#endif
|
2022-06-08 15:58:32 +08: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-19 03:41:04 +08:00
|
|
|
int uart_read(void *buf, size_t len) {
|
2023-02-23 22:51:21 +08:00
|
|
|
#if MG_ARCH == MG_ARCH_WIN32
|
|
|
|
(void) buf, (void) len;
|
|
|
|
return 0;
|
|
|
|
#else
|
2022-06-08 15:58:32 +08:00
|
|
|
return read(0, buf, len); // Read from stdin
|
2023-02-23 22:51:21 +08:00
|
|
|
#endif
|
2022-06-08 15:58:32 +08:00
|
|
|
}
|
2022-06-18 18:30:44 +08:00
|
|
|
|
|
|
|
char *config_read(void) {
|
|
|
|
return mg_file_read(&mg_fs_posix, "config.json", NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void config_write(struct mg_str config) {
|
|
|
|
mg_file_write(&mg_fs_posix, "config.json", config.ptr, config.len);
|
|
|
|
}
|
2022-06-08 15:58:32 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Event handler for a connected Websocket client
|
2024-01-09 04:34:34 +08:00
|
|
|
static void ws_fn(struct mg_connection *c, int ev, void *ev_data) {
|
2022-06-08 15:58:32 +08:00
|
|
|
if (ev == MG_EV_HTTP_MSG) {
|
2024-01-09 04:34:34 +08:00
|
|
|
mg_ws_upgrade(c, ev_data, NULL);
|
2022-06-08 15:58:32 +08:00
|
|
|
} else if (ev == MG_EV_WS_OPEN) {
|
|
|
|
// c->is_hexdumping = 1;
|
2023-01-09 18:58:07 +08:00
|
|
|
c->data[0] = 'W'; // When WS handhake is done, mark us as WS client
|
2022-06-08 15:58:32 +08:00
|
|
|
} else if (ev == MG_EV_WS_MSG) {
|
2024-01-09 04:34:34 +08:00
|
|
|
struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
|
2022-06-08 15:58:32 +08: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 11:25:05 +08:00
|
|
|
if (c->is_listening) s_state.websocket.c = NULL;
|
2022-06-08 15:58:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Event handler for a connected TCP client
|
2024-01-09 04:34:34 +08:00
|
|
|
static void tcp_fn(struct mg_connection *c, int ev, void *ev_data) {
|
2022-07-22 02:05:01 +08:00
|
|
|
if (ev == MG_EV_ACCEPT) {
|
2022-06-08 15:58:32 +08:00
|
|
|
// c->is_hexdumping = 1;
|
2023-01-09 18:58:07 +08:00
|
|
|
c->data[0] = 'T'; // When client is connected, mark us as TCP client
|
2022-06-08 15:58:32 +08: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 11:25:05 +08:00
|
|
|
if (c->is_listening) s_state.tcp.c = NULL;
|
|
|
|
}
|
2024-01-09 04:34:34 +08:00
|
|
|
(void) ev_data;
|
2022-06-18 11:25:05 +08:00
|
|
|
}
|
|
|
|
|
2022-06-18 18:30:44 +08: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 11:25:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Event handler for MQTT connection
|
2024-01-09 04:34:34 +08:00
|
|
|
static void mq_fn(struct mg_connection *c, int ev, void *ev_data) {
|
2022-06-18 11:25:05 +08:00
|
|
|
if (ev == MG_EV_OPEN) {
|
|
|
|
// c->is_hexdumping = 1;
|
|
|
|
} else if (ev == MG_EV_MQTT_OPEN) {
|
2023-01-09 18:58:07 +08:00
|
|
|
c->data[0] = 'M';
|
2023-04-20 14:59:27 +08: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 11:25:05 +08:00
|
|
|
} else if (ev == MG_EV_MQTT_MSG) {
|
2024-01-09 04:34:34 +08:00
|
|
|
struct mg_mqtt_message *mm = ev_data; // MQTT message
|
2022-06-18 11:25:05 +08: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 15:58:32 +08: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 11:25:05 +08: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 18:30:44 +08:00
|
|
|
struct mg_mqtt_opts opts = {.clean = true};
|
2022-06-18 11:25:05 +08:00
|
|
|
s_state.mqtt.c = mg_mqtt_connect(mgr, s_state.mqtt.url, &opts, mq_fn, 0);
|
|
|
|
}
|
2022-06-08 15:58:32 +08: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 18:58:07 +08: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 14:59:27 +08: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 15:58:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-22 02:05:01 +08: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 18:30:44 +08:00
|
|
|
static void config_apply(struct mg_str s) {
|
|
|
|
MG_INFO(("Applying config: %.*s", (int) s.len, s.ptr));
|
|
|
|
|
2022-07-22 02:05:01 +08: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 18:30:44 +08:00
|
|
|
|
2022-07-22 02:05:01 +08: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 18:30:44 +08: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-18 03:45:10 +08: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 18:30:44 +08:00
|
|
|
}
|
|
|
|
|
2022-06-08 15:58:32 +08:00
|
|
|
// HTTP request handler function
|
|
|
|
void uart_bridge_fn(struct mg_connection *c, int ev, void *ev_data,
|
|
|
|
void *fn_data) {
|
|
|
|
if (ev == MG_EV_OPEN && c->is_listening) {
|
2022-06-18 18:30:44 +08:00
|
|
|
char *config = config_read();
|
|
|
|
if (config != NULL) config_apply(mg_str(config));
|
|
|
|
free(config);
|
2022-06-18 11:25:05 +08: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 15:58:32 +08:00
|
|
|
mg_timer_add(c->mgr, 20, MG_TIMER_REPEAT, timer_fn, c->mgr);
|
2022-06-18 11:25:05 +08:00
|
|
|
uart_init(s_state.tx, s_state.rx, s_state.baud);
|
2022-08-01 18:19:32 +08:00
|
|
|
// mg_log_set(MG_LL_DEBUG); // Set log level
|
2022-06-08 15:58:32 +08: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 18:30:44 +08: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 15:58:32 +08:00
|
|
|
} else if (mg_http_match_uri(hm, "/api/config/get")) {
|
2022-06-18 11:25:05 +08:00
|
|
|
mg_http_reply(c, 200, "Content-Type: application/json\r\n",
|
2023-04-14 21:11:12 +08: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 15:58:32 +08:00
|
|
|
} else {
|
2023-02-23 22:32:53 +08:00
|
|
|
struct mg_http_serve_opts opts;
|
|
|
|
memset(&opts, 0, sizeof(opts));
|
2022-06-18 18:30:44 +08:00
|
|
|
#if 1
|
2022-06-08 15:58:32 +08: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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(void) fn_data;
|
|
|
|
}
|