mirror of
https://github.com/cesanta/mongoose.git
synced 2026-10-30 12:50:58 +00:00
move Zephyr -> tutorials
This commit is contained in:
1 parent
ea1cbeea9d
commit
bf23a0def7
47 files changed
+12
-7
No files matched your search
@@ -0,0 +1,9 @@
|
||||
# Copyright (c) 2022 Cesanta Software Limited
|
||||
# Mail: support@cesanta.com
|
||||
#
|
||||
# SPDX-License-Identifier: GPL2.0 or commercial
|
||||
|
||||
cmake_minimum_required(VERSION 3.20.0)
|
||||
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
|
||||
project(http_server)
|
||||
target_sources(app PRIVATE src/main.c src/mongoose.c)
|
||||
@@ -0,0 +1,44 @@
|
||||
TOP_DIR ?= $(realpath $(CURDIR)/../../..)
|
||||
ZEPHYR_DIR ?= $(realpath $(CURDIR)/..)/zephyrproject
|
||||
DOCKER ?= docker run --rm \
|
||||
-v $(TOP_DIR):$(TOP_DIR) \
|
||||
-w $(realpath $(CURDIR)) \
|
||||
-e ZEPHYR_BASE=$(ZEPHYR_DIR)/zephyr \
|
||||
$(DOCKER_ARGS) zephyrprojectrtos/ci
|
||||
BUILD_ARGS ?= -b nucleo_f746zg
|
||||
.PHONY: build
|
||||
|
||||
example:
|
||||
true
|
||||
|
||||
zephyr:
|
||||
test -d $(ZEPHYR_DIR) || ( mkdir -p $(ZEPHYR_DIR) ; $(DOCKER) west init $(ZEPHYR_DIR))
|
||||
$(DOCKER) sh -c 'cd $(ZEPHYR_DIR) && west update'
|
||||
|
||||
build:
|
||||
cp $(TOP_DIR)/mongoose.[ch] src/
|
||||
$(DOCKER) west build $(BUILD_ARGS) -p auto $(realpath $(CURDIR)) $(OVERLAY)
|
||||
|
||||
run: DOCKER_ARGS ?= --cap-add=NET_ADMIN --device /dev/net/tun --net=host -it
|
||||
run:
|
||||
$(DOCKER) west build -t run
|
||||
|
||||
clean:
|
||||
-$(DOCKER) west build -t clean
|
||||
|
||||
pristine:
|
||||
-$(DOCKER) west build -t pristine
|
||||
|
||||
purge: clean pristine
|
||||
rm -rf $(TOP_DIR)/.west $(ZEPHYR_DIR) build
|
||||
|
||||
flash debug: DOCKER_ARGS ?= --privileged -v /dev/bus/usb/:/dev/bus/usb -it
|
||||
flash:
|
||||
$(DOCKER) west flash --build-dir $(realpath $(CURDIR))/build
|
||||
|
||||
debug:
|
||||
$(DOCKER) west debug --build-dir $(realpath $(CURDIR))/build
|
||||
|
||||
test: BUILD_ARGS = -b qemu_x86
|
||||
test: OVERLAY = -DOVERLAY_CONFIG=overlay-e1000.conf
|
||||
test: build run
|
||||
+1
@@ -0,0 +1 @@
|
||||
../README.md
|
||||
@@ -0,0 +1,7 @@
|
||||
CONFIG_NET_L2_ETHERNET=y
|
||||
CONFIG_NET_QEMU_ETHERNET=y
|
||||
CONFIG_ETH_QEMU_IFACE_NAME="tap0"
|
||||
|
||||
CONFIG_PCIE=y
|
||||
|
||||
#CONFIG_ETHERNET_LOG_LEVEL_DBG=y
|
||||
@@ -0,0 +1,32 @@
|
||||
CONFIG_NETWORKING=y
|
||||
CONFIG_NET_IPV4=y
|
||||
CONFIG_NET_IPV6=y
|
||||
CONFIG_NET_TCP=y
|
||||
CONFIG_NET_UDP=y
|
||||
CONFIG_NET_DHCPV4=y
|
||||
CONFIG_NET_SOCKETS=y
|
||||
CONFIG_NET_SOCKETS_POLL_MAX=16
|
||||
CONFIG_POSIX_API=y
|
||||
CONFIG_ZVFS_OPEN_MAX=32
|
||||
CONFIG_NET_CONFIG_SETTINGS=y
|
||||
CONFIG_NET_CONNECTION_MANAGER=y
|
||||
CONFIG_NET_LOG=y
|
||||
|
||||
CONFIG_LOG=y
|
||||
CONFIG_ISR_STACK_SIZE=2048
|
||||
CONFIG_MAIN_STACK_SIZE=8192
|
||||
CONFIG_IDLE_STACK_SIZE=1024
|
||||
|
||||
CONFIG_MBEDTLS=y
|
||||
CONFIG_MBEDTLS_SSL_MAX_CONTENT_LEN=2048
|
||||
CONFIG_MBEDTLS_PEM_CERTIFICATE_FORMAT=y
|
||||
CONFIG_MBEDTLS_ECDH_C=y
|
||||
CONFIG_MBEDTLS_ECDSA_C=y
|
||||
CONFIG_MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED=y
|
||||
CONFIG_MBEDTLS_ECP_C=y
|
||||
CONFIG_MBEDTLS_ECP_ALL_ENABLED=y
|
||||
CONFIG_MBEDTLS_TLS_VERSION_1_2=y
|
||||
|
||||
CONFIG_MINIMAL_LIBC=y
|
||||
CONFIG_MINIMAL_LIBC_RAND=y
|
||||
CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE=32756
|
||||
@@ -0,0 +1,24 @@
|
||||
#ifndef CERTS_H_
|
||||
#define CERTS_H_
|
||||
|
||||
static const char *s_ssl_cert =
|
||||
"-----BEGIN CERTIFICATE-----\r\n" \
|
||||
"MIIBhzCCASygAwIBAgIUbnMoVd8TtWH1T09dANkK2LU6IUswCgYIKoZIzj0EAwIw\r\n" \
|
||||
"RDELMAkGA1UEBhMCSUUxDzANBgNVBAcMBkR1YmxpbjEQMA4GA1UECgwHQ2VzYW50\r\n" \
|
||||
"YTESMBAGA1UEAwwJVGVzdCBSb290MB4XDTIwMDUwOTIxNTE0OVoXDTMwMDUwOTIx\r\n" \
|
||||
"NTE0OVowETEPMA0GA1UEAwwGc2VydmVyMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcD\r\n" \
|
||||
"QgAEkuBGnInDN6l06zVVQ1VcrOvH5FDu9MC6FwJc2e201P8hEpq0Q/SJS2nkbSuW\r\n" \
|
||||
"H/wBTTBaeXN2uhlBzMUWK790KKMvMC0wCQYDVR0TBAIwADALBgNVHQ8EBAMCA6gw\r\n" \
|
||||
"EwYDVR0lBAwwCgYIKwYBBQUHAwEwCgYIKoZIzj0EAwIDSQAwRgIhAPo6xx7LjCdZ\r\n" \
|
||||
"QY133XvLjAgVFrlucOZHONFVQuDXZsjwAiEAzHBNligA08c5U3SySYcnkhurGg50\r\n" \
|
||||
"BllCI0eYQ9ggp/o=\r\n" \
|
||||
"-----END CERTIFICATE-----\r\n";
|
||||
|
||||
static const char *s_ssl_key =
|
||||
"-----BEGIN PRIVATE KEY-----\r\n" \
|
||||
"MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQglNni0t9Dg9icgG8w\r\n" \
|
||||
"kbfxWSS+TuNgbtNybIQXcm3NHpmhRANCAASS4EacicM3qXTrNVVDVVys68fkUO70\r\n" \
|
||||
"wLoXAlzZ7bTU/yESmrRD9IlLaeRtK5Yf/AFNMFp5c3a6GUHMxRYrv3Qo\r\n" \
|
||||
"-----END PRIVATE KEY-----\r\n";
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,104 @@
|
||||
// Copyright (c) 2020 Cesanta Software Limited
|
||||
// All rights reserved
|
||||
|
||||
#include "certs.h"
|
||||
#include "mongoose.h"
|
||||
#include <zephyr/net/net_mgmt.h>
|
||||
|
||||
static const char *s_web_dir = "/";
|
||||
static const char *s_http_addr = "http://0.0.0.0:8000";
|
||||
static const char *s_https_addr = "https://0.0.0.0:8443";
|
||||
static time_t s_boot_timestamp = 0;
|
||||
static struct mg_connection *s_sntp_conn = NULL;
|
||||
|
||||
// Event handler for the listening HTTP/HTTPS connection.
|
||||
static void wcb(struct mg_connection *c, int ev, void *ev_data) {
|
||||
if (ev == MG_EV_ACCEPT && c->fn_data != NULL) {
|
||||
struct mg_tls_opts opts = {.cert = (char *) s_ssl_cert, .key = (char *) s_ssl_key};
|
||||
mg_tls_init(c, &opts);
|
||||
} else if (ev == MG_EV_HTTP_MSG) {
|
||||
struct mg_http_message *hm = ev_data;
|
||||
MG_INFO(("%.*s %.*s %ld", (int) hm->method.len, hm->method.buf,
|
||||
(int) hm->uri.len, hm->uri.buf, (long) hm->body.len));
|
||||
if (mg_match(hm->uri, mg_str("/api/#"), NULL)) { // REST API requests
|
||||
// Print some statistics about currently established connections
|
||||
mg_printf(c, "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");
|
||||
mg_http_printf_chunk(c, "ID PROTO TYPE LOCAL REMOTE\n");
|
||||
for (struct mg_connection *t = c->mgr->conns; t != NULL; t = t->next) {
|
||||
mg_http_printf_chunk(c, "%-3lu %4s %s %M %M\n", t->id,
|
||||
t->is_udp ? "UDP" : "TCP",
|
||||
t->is_listening ? "LISTENING"
|
||||
: t->is_accepted ? "ACCEPTED "
|
||||
: "CONNECTED",
|
||||
mg_print_ip, &t->loc, mg_print_ip, &t->rem);
|
||||
}
|
||||
mg_http_printf_chunk(c, ""); // Don't forget the last empty chunk
|
||||
} else {
|
||||
struct mg_http_serve_opts opts = {.root_dir = s_web_dir}; // Serve
|
||||
mg_http_serve_dir(c, hm, &opts); // static files
|
||||
}
|
||||
} else if (ev == MG_EV_OPEN) {
|
||||
// c->is_hexdumping = 1;
|
||||
}
|
||||
}
|
||||
|
||||
// example system time()-like function
|
||||
time_t ourtime(time_t *tp) {
|
||||
time_t t = s_boot_timestamp + mg_millis() / 1000;
|
||||
if (tp != NULL) *tp = t;
|
||||
return t;
|
||||
}
|
||||
|
||||
// SNTP callback. Modifies s_boot_timestamp, to make ourtime() correct
|
||||
static void sfn(struct mg_connection *c, int ev, void *ev_data) {
|
||||
if (ev == MG_EV_SNTP_TIME) {
|
||||
int64_t t = *(int64_t *) ev_data;
|
||||
MG_INFO(("Got SNTP time: %lld ms from epoch", t));
|
||||
s_boot_timestamp = (time_t) ((t - mg_millis()) / 1000);
|
||||
} else if (ev == MG_EV_CLOSE) {
|
||||
s_sntp_conn = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
// Periodic timer syncs time via SNTP
|
||||
static void timer_fn(void *arg) {
|
||||
struct mg_mgr *mgr = (struct mg_mgr *) arg;
|
||||
if (s_sntp_conn == NULL) s_sntp_conn = mg_sntp_connect(mgr, NULL, sfn, NULL);
|
||||
if (s_boot_timestamp < 9999) mg_sntp_request(s_sntp_conn);
|
||||
}
|
||||
|
||||
// Zephyr: Define a semaphore and network management callback to be able to wait
|
||||
// until our IP address is ready. The main function will start and block on this
|
||||
// semaphore until this event handler releases it when the network is ready
|
||||
K_SEM_DEFINE(run, 0, 1);
|
||||
|
||||
static void zeh(struct net_mgmt_event_callback *cb, uint32_t mgmt_event,
|
||||
struct net_if *iface) {
|
||||
if (mgmt_event == NET_EVENT_L4_CONNECTED) k_sem_give(&run);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
// Zephyr: Register the network management callback and block on the semaphore
|
||||
struct net_mgmt_event_callback ncb;
|
||||
net_mgmt_init_event_callback(&ncb, zeh, NET_EVENT_L4_CONNECTED);
|
||||
net_mgmt_add_event_callback(&ncb);
|
||||
k_sem_take(&run, K_FOREVER);
|
||||
|
||||
struct mg_mgr mgr;
|
||||
|
||||
mg_mgr_init(&mgr);
|
||||
mg_log_set(MG_LL_DEBUG);
|
||||
mg_http_listen(&mgr, s_http_addr, wcb, NULL);
|
||||
mg_http_listen(&mgr, s_https_addr, wcb, &mgr);
|
||||
|
||||
mg_timer_add(&mgr, 5000, MG_TIMER_REPEAT | MG_TIMER_RUN_NOW, timer_fn, &mgr);
|
||||
|
||||
// Start infinite event loop
|
||||
MG_INFO(("Mongoose version : v%s", MG_VERSION));
|
||||
MG_INFO(("Listening on : %s", s_http_addr));
|
||||
MG_INFO(("Listening on : %s", s_https_addr));
|
||||
MG_INFO(("Web root : [%s]", s_web_dir));
|
||||
for (;;) mg_mgr_poll(&mgr, 1000);
|
||||
mg_mgr_free(&mgr);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
#define MG_ARCH MG_ARCH_ZEPHYR
|
||||
|
||||
#define MG_ENABLE_PACKED_FS 1
|
||||
#define MG_TLS MG_TLS_MBED
|
||||
Reference in new issue
Block a user