Add mbedtls support to MIP

This commit is contained in:
cpq committed 2022-09-09 15:53:42 +01:00
1 parent f76a7e36e8
commit 124ac12150
16 files changed
+446 -303

No files matched your search

+6 -4
View File
@@ -3,7 +3,9 @@
#include "mongoose.h"
#if !defined(MQTT_SERVER)
#define MQTT_SERVER "mqtt://broker.hivemq.com:1883"
#endif
#define MQTT_PUBLISH_TOPIC "mg/my_device"
#define MQTT_SUBSCRIBE_TOPIC "mg/#"
@@ -111,7 +113,7 @@ static void mqtt_fn(struct mg_connection *c, int ev, void *ev_data, void *fnd) {
"qos", (int) mm->qos);
} else if (ev == MG_EV_MQTT_CMD) {
struct mg_mqtt_message *mm = (struct mg_mqtt_message *) ev_data;
MG_DEBUG(("cmd %d qos %d", mm->cmd, mm->qos));
MG_DEBUG(("%lu cmd %d qos %d", c->id, mm->cmd, mm->qos));
} else if (ev == MG_EV_CLOSE) {
s_mqtt = NULL;
if (s_connected) {
@@ -136,7 +138,6 @@ static void timer_mqtt_fn(void *param) {
static void sfn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
if (ev == MG_EV_SNTP_TIME) {
uint64_t t = *(uint64_t *) ev_data;
MG_INFO(("%lu SNTP: %lld ms from epoch", c->id, t));
s_boot_timestamp = (time_t) ((t - mg_millis()) / 1000);
c->is_closing = 1;
} else if (ev == MG_EV_CLOSE) {
@@ -211,7 +212,8 @@ void device_dashboard_fn(struct mg_connection *c, int ev, void *ev_data,
#endif
mg_http_serve_dir(c, ev_data, &opts);
}
MG_DEBUG(("%.*s %.*s -> %.*s", (int) hm->method.len, hm->method.ptr,
(int) hm->uri.len, hm->uri.ptr, (int) 3, &c->send.buf[9]));
MG_DEBUG(("%lu %.*s %.*s -> %.*s", c->id, (int) hm->method.len,
hm->method.ptr, (int) hm->uri.len, hm->uri.ptr, (int) 3,
&c->send.buf[9]));
}
}
+10 -2
View File
@@ -1,9 +1,17 @@
PROG ?= example
DEFS ?= -DMG_ENABLE_LINES=1 -DMG_ENABLE_MIP=1 -DMG_ENABLE_SOCKET=0 -DMG_ENABLE_WINSOCK=0 -DMG_ENABLE_PACKED_FS=1 -I../..
CFLAGS ?= -W -Wall
DEFS ?= -DMG_ENABLE_LINES=1 -DMG_ENABLE_MIP=1 -DMG_ENABLE_SOCKET=0 -DMG_ENABLE_PACKED_FS=1 -I../..
CFLAGS ?= -W -Wall $(EXTRA_CFLAGS)
LIBS ?= -lpcap
SOURCES = main.c ../../mongoose.c ../device-dashboard/net.c ../device-dashboard/packed_fs.c
ifeq "$(SSL)" "MBEDTLS"
CFLAGS += -DMG_ENABLE_MBEDTLS=1 -lmbedtls -lmbedcrypto -lmbedx509 -I$(MBEDTLS)/include -L$(MBEDTLS)/lib
endif
ifeq "$(SSL)" "OPENSSL"
CFLAGS += -DMG_ENABLE_OPENSSL=1 -lssl -lcrypto -I$(OPENSSL)/include -L$(OPENSSL)/lib
endif
all: $(PROG)
$(RUN) ./$(PROG) $(ARGS)
+1
View File
@@ -0,0 +1 @@
../../test/data/ca.pem
+4 -11
View File
@@ -30,19 +30,11 @@ static size_t pcap_rx(void *buf, size_t len, void *userdata) {
size_t received = 0;
struct pcap_pkthdr *hdr = NULL;
const unsigned char *pkt = NULL;
// To avoid busy-loop and 100% CPU time, wait on pcap for some time
int fd = pcap_get_selectable_fd((pcap_t *) userdata); // Pcap file descriptor
struct timeval tv = {0, 50000}; // 50 ms
fd_set rset;
FD_ZERO(&rset);
FD_SET(fd, &rset);
if (select(fd + 1, &rset, 0, 0, &tv) == 1 && // Have data ?
pcap_next_ex((pcap_t *) userdata, &hdr, &pkt) == 1) { // Yes, read
usleep(1); // This is to avoid 100% CPU
if (pcap_next_ex((pcap_t *) userdata, &hdr, &pkt) == 1) { // Yes, read
received = hdr->len < len ? hdr->len : len;
memcpy(buf, pkt, received);
}
return received;
}
@@ -60,6 +52,8 @@ int main(int argc, char *argv[]) {
mac = argv[++i];
} else if (strcmp(argv[i], "-bpf") == 0 && i + 1 < argc) {
bpf = argv[++i];
} else if (strcmp(argv[i], "-v") == 0 && i + 1 < argc) {
mg_log_set(atoi(argv[++i]));
} else {
MG_ERROR(("unknown option %s", argv[i]));
return EXIT_FAILURE;
@@ -96,7 +90,6 @@ int main(int argc, char *argv[]) {
struct mg_mgr mgr; // Event manager
mg_mgr_init(&mgr); // Initialise event manager
mg_log_set(MG_LL_DEBUG); // Set debug log level
struct mip_cfg c = {.ip = 0, .mask = 0, .gw = 0};
sscanf(mac, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &c.mac[0], &c.mac[1], &c.mac[2],