mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-23 20:11:49 +00:00
Merge branch 'master' into hdrs
This commit is contained in:
45 files changed
+980
-403
No files matched your search
@@ -0,0 +1,4 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
set(IDF_TARGET esp32c6)
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(mongoose-minimal-802154)
|
||||
@@ -0,0 +1,23 @@
|
||||
# Environment setup: https://mongoose.ws/documentation/tutorials/tools/
|
||||
# Build: ESP-IDF from the espressif/idf Docker image. Flash: esptool.
|
||||
# Monitor: esputil, https://github.com/cpq/esputil
|
||||
|
||||
THISDIR = $(realpath $(CURDIR))
|
||||
ROOTDIR = $(realpath $(CURDIR)/../../..)
|
||||
DOCKER ?= docker run --rm -v $(ROOTDIR):$(ROOTDIR) -w $(THISDIR) espressif/idf
|
||||
PORT ?= /dev/cu.usbmodem2101
|
||||
|
||||
all build:
|
||||
$(DOCKER) idf.py build
|
||||
|
||||
flash: build
|
||||
cd build && esptool --chip esp32c6 -p $(PORT) -b 460800 write-flash @flash_args
|
||||
|
||||
monitor:
|
||||
esputil -p $(PORT) monitor
|
||||
|
||||
menuconfig:
|
||||
docker run --rm -it -v $(ROOTDIR):$(ROOTDIR) -w $(THISDIR) espressif/idf idf.py menuconfig
|
||||
|
||||
clean:
|
||||
test -d build && $(DOCKER) rm -rf build sdkconfig || true
|
||||
@@ -0,0 +1,5 @@
|
||||
idf_component_register(SRCS "main.c" "../../../../mongoose.c"
|
||||
"../../../mqtt/mqtt-client/mongoose_mqtt.c"
|
||||
INCLUDE_DIRS "." "../../../.."
|
||||
REQUIRES ieee802154 esp_timer esp_hw_support
|
||||
esp_driver_gpio app_update)
|
||||
@@ -0,0 +1,99 @@
|
||||
// Copyright (c) 2026 Cesanta Software Limited
|
||||
// All rights reserved
|
||||
|
||||
#include <driver/gpio.h>
|
||||
#include <esp_heap_caps.h>
|
||||
|
||||
#include "mongoose.h"
|
||||
|
||||
void mg_mqtt_init(struct mg_mgr *mgr); // From tutorials/mqtt/mqtt-client
|
||||
void mg_mqtt_poll(struct mg_mgr *mgr);
|
||||
|
||||
// Seeed XIAO ESP32-C6
|
||||
#define LED1 GPIO_NUM_15 // User LED, active low
|
||||
#define RF_SW_PWR GPIO_NUM_3 // RF switch power, active low
|
||||
#define RF_SW_SEL GPIO_NUM_14 // Antenna select: low built-in, high U.FL
|
||||
|
||||
// Self-signed P-256 certificate and key for the HTTPS listener. Created by:
|
||||
// openssl ecparam -genkey -name prime256v1 -noout -out key.pem
|
||||
// openssl req -x509 -key key.pem -out cert.pem -days 3650 -subj /CN=mg
|
||||
#define TLS_CERT \
|
||||
"-----BEGIN CERTIFICATE-----\n" \
|
||||
"MIIBgjCCASegAwIBAgIUIJOq77Qn7lHbH6WseMemeqorR50wCgYIKoZIzj0EAwIw\n" \
|
||||
"FjEUMBIGA1UEAwwLbnVjbGVvLWg3MjMwHhcNMjYxMDEyMTUwMjE5WhcNMzYxMDA5\n" \
|
||||
"MTUwMjE5WjAWMRQwEgYDVQQDDAtudWNsZW8taDcyMzBZMBMGByqGSM49AgEGCCqG\n" \
|
||||
"SM49AwEHA0IABB6IQJNmzrsdQL+2BnZYS7GYOaj7sz8f6cjBmz3n8D9Em19XodlU\n" \
|
||||
"uy4ybIvyhr766lL0ERoTmMUivb/lGmzx9C+jUzBRMB0GA1UdDgQWBBQqxMuc4gj7\n" \
|
||||
"MTMzreWlRMNI3QMqTzAfBgNVHSMEGDAWgBQqxMuc4gj7MTMzreWlRMNI3QMqTzAP\n" \
|
||||
"BgNVHRMBAf8EBTADAQH/MAoGCCqGSM49BAMCA0kAMEYCIQDY4xaJpuxq6b3rHK3b\n" \
|
||||
"lOIQyfkQX+orCRqQUkseplqpqwIhAKswZFNn+2zUNRRNY8+cHmdIwmdpsmxtoWYP\n" \
|
||||
"BCjX++LI\n" \
|
||||
"-----END CERTIFICATE-----\n"
|
||||
|
||||
#define TLS_KEY \
|
||||
"-----BEGIN EC PRIVATE KEY-----\n" \
|
||||
"MHcCAQEEIJ/GBl0TlD52q9PMg1KWaNIdXqp8b/HyIsABmBpqC+O1oAoGCCqGSM49\n" \
|
||||
"AwEHoUQDQgAEHohAk2bOux1Av7YGdlhLsZg5qPuzPx/pyMGbPefwP0SbX1eh2VS7\n" \
|
||||
"LjJsi/KGvvrqUvQRGhOYxSK9v+UabPH0Lw==\n" \
|
||||
"-----END EC PRIVATE KEY-----\n"
|
||||
|
||||
// Power the XIAO RF switch and select the built-in antenna
|
||||
static void antenna_init(void) {
|
||||
gpio_set_direction(RF_SW_PWR, GPIO_MODE_OUTPUT);
|
||||
gpio_set_level(RF_SW_PWR, 0);
|
||||
gpio_set_direction(RF_SW_SEL, GPIO_MODE_OUTPUT);
|
||||
gpio_set_level(RF_SW_SEL, 0);
|
||||
}
|
||||
|
||||
// Toggle the user LED twice a second
|
||||
static void blink_task(void) {
|
||||
static uint64_t blink_timer = 0;
|
||||
static int on = 0;
|
||||
if (mg_timer_expired(&blink_timer, 500, mg_millis())) {
|
||||
gpio_set_level(LED1, on ^= 1);
|
||||
}
|
||||
}
|
||||
|
||||
static void http_ev_handler(struct mg_connection *c, int ev, void *ev_data) {
|
||||
if (ev == MG_EV_ACCEPT && c->is_tls) { // Accepted on the https:// listener
|
||||
struct mg_tls_opts opts = {.cert = mg_str(TLS_CERT),
|
||||
.key = mg_str(TLS_KEY)};
|
||||
mg_tls_init(c, &opts);
|
||||
} else if (ev == MG_EV_HTTP_MSG) {
|
||||
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
||||
if (mg_match(hm->uri, mg_str("/api/tick"), NULL)) {
|
||||
mg_http_reply(c, 200, "", "{%m:%llu}\n", MG_ESC("tick"), mg_millis());
|
||||
} else if (mg_match(hm->uri, mg_str("/api/crash"), NULL)) {
|
||||
__asm volatile("unimp"); // Illegal instruction: ESP-IDF prints a panic
|
||||
} else {
|
||||
mg_http_reply(c, 200, "",
|
||||
"Hi from Mongoose!\n"
|
||||
"tick: %llu\n"
|
||||
"ram free: %zu\n"
|
||||
"ram min: %zu\n",
|
||||
mg_millis(), heap_caps_get_free_size(MALLOC_CAP_DEFAULT),
|
||||
heap_caps_get_minimum_free_size(MALLOC_CAP_DEFAULT));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void app_main(void) {
|
||||
struct mg_mgr mgr;
|
||||
|
||||
antenna_init();
|
||||
gpio_set_direction(LED1, GPIO_MODE_OUTPUT);
|
||||
|
||||
mg_mgr_init(&mgr); // Also starts the 802.15.4 driver
|
||||
mg_http_listen(&mgr, "http://0.0.0.0", http_ev_handler, NULL);
|
||||
mg_http_listen(&mgr, "https://0.0.0.0", http_ev_handler, NULL);
|
||||
mg_mqtt_init(&mgr);
|
||||
|
||||
MG_INFO(("Initialised. RAM free: %zu",
|
||||
heap_caps_get_free_size(MALLOC_CAP_DEFAULT)));
|
||||
|
||||
for (;;) {
|
||||
mg_mgr_poll(&mgr, 0);
|
||||
mg_mqtt_poll(&mgr);
|
||||
blink_task();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include <esp_mac.h>
|
||||
|
||||
// See https://mongoose.ws/docs/getting-started/build-options/
|
||||
#define MG_ARCH MG_ARCH_ESP32
|
||||
#define MG_TLS MG_TLS_BUILTIN
|
||||
#define MQTT_CLIENT_ID "esp32c6" // For tutorials/mqtt/mqtt-client
|
||||
|
||||
#define MG_ENABLE_TCPIP 1 // Built-in TCP/IP stack, not lwIP
|
||||
#define MG_ENABLE_DRIVER_ESP32_802154 1 // ESP32-C6 802.15.4 radio
|
||||
#define MG_ENABLE_TCPIP_TCPRTX 1 // Link can be lossy, enable retransmit
|
||||
|
||||
// Stable MAC, so the DHCP address stays the same across reboots: the factory
|
||||
// MAC, locally administered
|
||||
#define MG_SET_MAC_ADDRESS(mac) \
|
||||
do { \
|
||||
esp_read_mac(mac, ESP_MAC_BASE); \
|
||||
(mac)[0] |= 2; \
|
||||
} while (0)
|
||||
|
||||
// mdash.net device management service support
|
||||
#define MG_ENABLE_MDASH 0
|
||||
#define MG_MDASH_KEY "..." // Set this - mDash device token
|
||||
#define MG_FIRMWARE_VERSION "esp32c6.1.0.0"
|
||||
@@ -0,0 +1,8 @@
|
||||
CONFIG_IDF_TARGET="esp32c6"
|
||||
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
|
||||
CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG=y
|
||||
CONFIG_IEEE802154_ENABLED=y
|
||||
# Built-in TLS handshakes need a larger stack
|
||||
CONFIG_ESP_MAIN_TASK_STACK_SIZE=12288
|
||||
# The main task busy-loops the mesh, so the idle task does not run
|
||||
CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0=n
|
||||
@@ -1,43 +0,0 @@
|
||||
REPO_ROOT ?= ../../..
|
||||
|
||||
PROG ?= client # Program we are building
|
||||
DELETE = rm -rf # Command to remove files
|
||||
OUT ?= -o $(PROG) # Compiler argument for output file
|
||||
CFLAGS = -I$(REPO_ROOT) -W -Wall -Wextra -g -I. # Build options
|
||||
SOURCES = main.c $(REPO_ROOT)/mongoose.c mongoose_fs.c
|
||||
|
||||
CFLAGS_EXTRA ?= -DMG_TLS=MG_TLS_BUILTIN
|
||||
|
||||
# Mongoose build options. See https://mongoose.ws/docs/getting-started/build-environment/
|
||||
# CFLAGS_MONGOOSE +=
|
||||
|
||||
ifeq ($(OS),Windows_NT) # Windows settings. Assume MinGW compiler. To use VC: make CC=cl CFLAGS=/MD OUT=/Feprog.exe
|
||||
PROG ?= client.exe # Use .exe suffix for the binary
|
||||
CC = gcc # Use MinGW gcc compiler
|
||||
CFLAGS += -lws2_32 # Link against Winsock library
|
||||
DELETE = cmd /C del /Q /F /S # Command prompt command to delete files
|
||||
OUT ?= -o $(PROG) # Build output
|
||||
MAKE += WINDOWS=1 CC=$(CC)
|
||||
endif
|
||||
|
||||
all: $(PROG)
|
||||
$(RUN) ./$(PROG) $(ARGS)
|
||||
|
||||
$(PROG): $(SOURCES) Makefile
|
||||
$(CC) $(SOURCES) $(CFLAGS) $(CFLAGS_MONGOOSE) $(CFLAGS_EXTRA) $(OUT)
|
||||
|
||||
csr: FORCE
|
||||
openssl ecparam -noout -name prime256v1 -genkey -out key.pem
|
||||
openssl req -new -key key.pem -subj /CN=Mongoose -out crt.csr
|
||||
|
||||
mongoose_fs.c: ca.pem crt.pem key.pem
|
||||
node ../../../test/pack.js ca.pem crt.pem key.pem > $@
|
||||
|
||||
clean:
|
||||
$(DELETE) $(PROG) *.o *.obj *.exe *.dSYM mbedtls
|
||||
|
||||
# For automated test purposes only
|
||||
example: FORCE
|
||||
touch mongoose_fs.c
|
||||
|
||||
FORCE:
|
||||
@@ -1,2 +0,0 @@
|
||||
- This example requires generating AWS and user credentials and downloading certificates
|
||||
- This example requires building with TLS support; before running, see detailed tutorial at https://mongoose.ws/tutorials/mqtt/mqtt-client-aws-iot/
|
||||
@@ -1,102 +0,0 @@
|
||||
// Copyright (c) 2020 Cesanta Software Limited
|
||||
// All rights reserved
|
||||
//
|
||||
// Example MQTT client. It performs the following steps:
|
||||
// 1. Connects to the AWS IoT MQTT server
|
||||
// 2. When connected, subscribes to the topic `s_rx_topic`
|
||||
// 3. Publishes message `hello` to the `s_tx_topic` periodically
|
||||
//
|
||||
|
||||
// How to build and run this example:
|
||||
// 1. Login to AWS IoT
|
||||
// 2. Click "Settings" on the left bar, copy the domain, change s_url below
|
||||
// 3. Click Security -> Policies -> Create Policy, fill fields:
|
||||
// Name : PolicyAllow
|
||||
// Action : iot:*
|
||||
// Resource ARN: *
|
||||
// Effect : allow
|
||||
// then, click "Create"
|
||||
// 4. Create EC private key file and CSR (Certificate Signing Request)
|
||||
// type "make csr", see Makefile
|
||||
// 5. Click Security -> Certificates -> Add Certificate -> Create Certificate
|
||||
// Choose "Create certificate with certificate signing request (CSR)"
|
||||
// Choose "crt.csr" created on a previous step
|
||||
// Choose "Active" to activate certificate
|
||||
// Click Create
|
||||
// Attach PolicyAllow policy to the created certificate
|
||||
// Downoad AmazonRootCA1.pem as ca.pem and generated certificate as crt.pem
|
||||
// Select certificate, attach PolicyAllow to it
|
||||
// 6. Type "make" to build and run the example
|
||||
|
||||
static const char *s_url =
|
||||
"mqtts://a1pjwh2bop1ojt-ats.iot.eu-west-1.amazonaws.com";
|
||||
static const char *s_rx_topic = "d/rx";
|
||||
static const char *s_tx_topic = "d/tx";
|
||||
static int s_qos = 1;
|
||||
|
||||
#include "mongoose.h"
|
||||
|
||||
static void fn(struct mg_connection *c, int ev, void *ev_data) {
|
||||
if (ev == MG_EV_OPEN) {
|
||||
// c->is_hexdumping = 1;
|
||||
} else if (ev == MG_EV_CONNECT) {
|
||||
if (c->is_tls) {
|
||||
struct mg_tls_opts opts = {.ca = mg_unpacked("/ca.pem"),
|
||||
.cert = mg_unpacked("/crt.pem"),
|
||||
.key = mg_unpacked("/key.pem"),
|
||||
.name = mg_url_host(s_url)};
|
||||
mg_tls_init(c, &opts);
|
||||
}
|
||||
} else if (ev == MG_EV_ERROR) {
|
||||
// On error, log error message
|
||||
MG_ERROR(("%p %s", c->fd, (char *) ev_data));
|
||||
} else if (ev == MG_EV_MQTT_OPEN) {
|
||||
// MQTT connect is successful
|
||||
struct mg_str topic = mg_str(s_rx_topic);
|
||||
MG_INFO(("Connected to %s", s_url));
|
||||
MG_INFO(("Subscribing to %s", s_rx_topic));
|
||||
struct mg_mqtt_opts sub_opts;
|
||||
memset(&sub_opts, 0, sizeof(sub_opts));
|
||||
sub_opts.topic = topic;
|
||||
sub_opts.qos = s_qos;
|
||||
mg_mqtt_sub(c, &sub_opts);
|
||||
c->data[0] = 'X'; // Set a label that we're logged in
|
||||
} else if (ev == MG_EV_MQTT_MSG) {
|
||||
// When we receive MQTT message, print it
|
||||
struct mg_mqtt_message *mm = (struct mg_mqtt_message *) ev_data;
|
||||
MG_INFO(("Received on %.*s : %.*s", (int) mm->topic.len, mm->topic.buf,
|
||||
(int) mm->data.len, mm->data.buf));
|
||||
} else if (ev == MG_EV_POLL && c->data[0] == 'X') {
|
||||
static unsigned long prev_second;
|
||||
unsigned long now_second = (*(unsigned long *) ev_data) / 1000;
|
||||
if (now_second != prev_second) {
|
||||
struct mg_str topic = mg_str(s_tx_topic), data = mg_str("{\"a\":123}");
|
||||
MG_INFO(("Publishing to %s", s_tx_topic));
|
||||
struct mg_mqtt_opts pub_opts;
|
||||
memset(&pub_opts, 0, sizeof(pub_opts));
|
||||
pub_opts.topic = topic;
|
||||
pub_opts.message = data;
|
||||
pub_opts.qos = s_qos, pub_opts.retain = false;
|
||||
mg_mqtt_pub(c, &pub_opts);
|
||||
prev_second = now_second;
|
||||
}
|
||||
}
|
||||
|
||||
if (ev == MG_EV_ERROR || ev == MG_EV_CLOSE) {
|
||||
MG_INFO(("Got event %d, stopping...", ev));
|
||||
*(bool *) c->fn_data = true; // Signal that we're done
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
struct mg_mgr mgr;
|
||||
struct mg_mqtt_opts opts = {.clean = true};
|
||||
bool done = false;
|
||||
mg_log_set(MG_LL_DEBUG);
|
||||
mg_mgr_init(&mgr); // Initialise event manager
|
||||
MG_INFO(("Connecting to %s", s_url)); // Inform that we're starting
|
||||
mg_mqtt_connect(&mgr, s_url, &opts, fn, &done); // Create client connection
|
||||
while (!done) mg_mgr_poll(&mgr, 1000); // Loop until done
|
||||
mg_mgr_free(&mgr); // Finished, cleanup
|
||||
return 0;
|
||||
}
|
||||
@@ -17,9 +17,6 @@ static void blink_task(void) {
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t mg_millis(void) {
|
||||
return hal_get_tick();
|
||||
}
|
||||
|
||||
bool mg_random(void *buf, size_t len) {
|
||||
for (size_t n = 0; n < len; n += sizeof(uint32_t)) {
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#define MG_ENABLE_TCPIP 1
|
||||
#define MG_ENABLE_DRIVER_NRF52 1
|
||||
#define MG_ENABLE_TCPIP_TCPRTX 1
|
||||
#define MG_ENABLE_CUSTOM_MILLIS 1
|
||||
#define MG_ENABLE_CUSTOM_MICROS 1
|
||||
#define MG_ENABLE_CUSTOM_RANDOM 1
|
||||
#define MG_TLS MG_TLS_BUILTIN
|
||||
#define MQTT_CLIENT_ID "xiao-nrf52840"
|
||||
|
||||
@@ -25,9 +25,6 @@ static void blink_task(void) {
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t mg_millis(void) {
|
||||
return hal_get_tick();
|
||||
}
|
||||
|
||||
bool mg_random(void *buf, size_t len) {
|
||||
for (size_t n = 0; n < len; n += sizeof(uint32_t)) {
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
#define MG_ENABLE_TCPIP 1
|
||||
#define MG_ENABLE_DRIVER_MCXW71 1
|
||||
#define MG_ENABLE_CUSTOM_MILLIS 1
|
||||
#define MG_ENABLE_CUSTOM_MICROS 1
|
||||
#define MG_ENABLE_CUSTOM_RANDOM 1
|
||||
#define MG_ENABLE_TCPIP_TCPRTX 1 // Link can be lossy, enable retransmit
|
||||
|
||||
|
||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
-3
@@ -43,9 +43,6 @@ static void log_fn(char ch, void *arg) {
|
||||
hal_uart_write_buf(arg, &ch, 1);
|
||||
}
|
||||
|
||||
uint64_t mg_millis(void) {
|
||||
return hal_get_tick();
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
static struct mg_tcpip_driver_rw612_data eth_data;
|
||||
+1
-1
@@ -6,7 +6,7 @@
|
||||
#define MG_ENABLE_TCPIP 1
|
||||
#define MG_ENABLE_DRIVER_RW612 1 // Ethernet
|
||||
#define MG_ENABLE_DRIVER_RW612W 1 // Mesh bridge over raw 802.15.4
|
||||
#define MG_ENABLE_CUSTOM_MILLIS 1
|
||||
#define MG_ENABLE_CUSTOM_MICROS 1
|
||||
|
||||
|
||||
// The bridge initializes and polls its two L2 interfaces directly.
|
||||
File renamed without changes.
File renamed without changes.
@@ -26,9 +26,6 @@ static void blink_task(void) {
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t mg_millis(void) {
|
||||
return hal_get_tick();
|
||||
}
|
||||
|
||||
bool mg_random(void *buf, size_t len) {
|
||||
for (size_t n = 0; n < len; n += sizeof(uint32_t)) {
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
#define MG_ENABLE_TCPIP 1
|
||||
#define MG_ENABLE_DRIVER_MG24 1
|
||||
#define MG_ENABLE_CUSTOM_MILLIS 1
|
||||
#define MG_ENABLE_CUSTOM_MICROS 1
|
||||
#define MG_ENABLE_CUSTOM_RANDOM 1 // Secure Engine TRNG, see hal.c
|
||||
#define MG_ENABLE_TCPIP_TCPRTX 1 // Link can be lossy, enable retransmit
|
||||
|
||||
|
||||
Reference in new issue
Block a user