From 78900304bfd27a8577f91ffe707451c8f2ccebba Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Thu, 28 Nov 2024 08:46:32 +0000 Subject: [PATCH] More PPP nitpicks --- examples/arduino/sim800-mqtt/mongoose_config.h | 6 ++---- examples/arduino/sim800-mqtt/sim800-mqtt.ino | 16 +++++++++++++++- mongoose.c | 2 +- src/drivers/ppp.c | 2 +- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/examples/arduino/sim800-mqtt/mongoose_config.h b/examples/arduino/sim800-mqtt/mongoose_config.h index 94c0a1ea..803047e2 100644 --- a/examples/arduino/sim800-mqtt/mongoose_config.h +++ b/examples/arduino/sim800-mqtt/mongoose_config.h @@ -14,8 +14,6 @@ #define MG_ENABLE_TCPIP_DRIVER_INIT 0 #define MG_ENABLE_TCPIP_PRINT_DEBUG_STATS 0 #define MG_ENABLE_CUSTOM_MILLIS 1 +#define MG_ENABLE_CUSTOM_RANDOM 1 +#define MG_TLS MG_TLS_BUILTIN #define MG_IO_SIZE 128 - -// Enable TLS -// #define MG_TLS MG_TLS_BUILTIN -// #define MG_ENABLE_CUSTOM_RANDOM 1 diff --git a/examples/arduino/sim800-mqtt/sim800-mqtt.ino b/examples/arduino/sim800-mqtt/sim800-mqtt.ino index d1d99e6d..be04ab20 100644 --- a/examples/arduino/sim800-mqtt/sim800-mqtt.ino +++ b/examples/arduino/sim800-mqtt/sim800-mqtt.ino @@ -1,7 +1,7 @@ #include #include "mongoose.h" -#define MQTT_SERVER "mqtt://broker.hivemq.com:1883" +#define MQTT_SERVER "mqtts://broker.hivemq.com:8883" #define MQTT_SUB_TOPIC "mg/rx" // Subscribe to this topic #define MQTT_PUB_TOPIC "mg/tx" // Publish to this topic @@ -15,6 +15,10 @@ static const char *script[] = { NULL, }; +// Visit https://mongoose.ws/tls/ and copy-paste CA certificate for your server +#define TLS_CA \ + "" + // We use Serial1 or software serial to communicate with the modem #define LED_PIN LED_BUILTIN #define RST_PIN 10 @@ -31,6 +35,11 @@ struct mg_tcpip_if mif = {.mac = {2, 0, 1, 2, 3, 5}}; // Network interface uint64_t mg_millis(void) { return millis(); } +bool mg_random(void *buf, size_t len) { // For TLS + uint8_t *p = (uint8_t *) buf; + while (len--) *p++ = (unsigned char) (rand() & 255); + return true; +} void mqtt_publish(const char *message) { struct mg_mqtt_opts opts = {}; @@ -50,6 +59,11 @@ void handle_command(struct mg_str msg) { } static void mqtt_ev_handler(struct mg_connection *c, int ev, void *ev_data) { + if (ev == MG_EV_CONNECT && mg_url_is_ssl(MQTT_SERVER)) { + struct mg_tls_opts opts = {}; + opts.ca = mg_str(TLS_CA); + mg_tls_init(c, &opts); + } if (ev == MG_EV_MQTT_OPEN) { MG_INFO(("%lu CONNECTED to %s", c->id, MQTT_SERVER)); struct mg_mqtt_opts opts = {}; diff --git a/mongoose.c b/mongoose.c index e4ec04e0..96237c3e 100644 --- a/mongoose.c +++ b/mongoose.c @@ -18095,7 +18095,7 @@ static size_t mg_ppp_rx(void *ethbuf, size_t ethlen, struct mg_tcpip_if *ifp) { case MG_PPP_PROTO_LCP: mg_ppp_handle_lcp(ifp, buf + 4, bufsz - 4); break; case MG_PPP_PROTO_IPCP: mg_ppp_handle_ipcp(ifp, buf + 4, bufsz - 4); break; case MG_PPP_PROTO_IP: - MG_DEBUG(("got IP packet of %d bytes", bufsz - 4)); + MG_VERBOSE(("got IP packet of %d bytes", bufsz - 4)); memmove(eth + 14, buf + 4, bufsz - 4); memmove(eth, ifp->mac, 6); memmove(eth + 6, "\xff\xff\xff\xff\xff\xff", 6); diff --git a/src/drivers/ppp.c b/src/drivers/ppp.c index 78cfd089..dc072202 100644 --- a/src/drivers/ppp.c +++ b/src/drivers/ppp.c @@ -301,7 +301,7 @@ static size_t mg_ppp_rx(void *ethbuf, size_t ethlen, struct mg_tcpip_if *ifp) { case MG_PPP_PROTO_LCP: mg_ppp_handle_lcp(ifp, buf + 4, bufsz - 4); break; case MG_PPP_PROTO_IPCP: mg_ppp_handle_ipcp(ifp, buf + 4, bufsz - 4); break; case MG_PPP_PROTO_IP: - MG_DEBUG(("got IP packet of %d bytes", bufsz - 4)); + MG_VERBOSE(("got IP packet of %d bytes", bufsz - 4)); memmove(eth + 14, buf + 4, bufsz - 4); memmove(eth, ifp->mac, 6); memmove(eth + 6, "\xff\xff\xff\xff\xff\xff", 6);