Skip to content

Commit

Permalink
Add driver init to mg_mgr_init()
Browse files Browse the repository at this point in the history
  • Loading branch information
scaprile committed Mar 12, 2024
1 parent 9f498d2 commit c8fd3d6
Show file tree
Hide file tree
Showing 29 changed files with 455 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,11 @@ s390: CC = $(DOCKER) mdashnet/s390 cc
s390: RUN = $(DOCKER) mdashnet/s390
s390: test

arm: DEFS += -DMG_ENABLE_POSIX_FS=0 -DMG_ENABLE_TCPIP=1 -DMG_ARCH=MG_ARCH_NEWLIB
arm: DEFS += -DMG_ENABLE_POSIX_FS=0 -DMG_ENABLE_TCPIP=1 -DMG_ENABLE_TCPIP_DRIVER_INIT=0 -DMG_ARCH=MG_ARCH_NEWLIB
arm: mongoose.h $(SRCS)
$(DOCKER) mdashnet/armgcc arm-none-eabi-gcc -mcpu=cortex-m3 -mthumb $(SRCS) $(OPTS) $(WARN) $(INCS) $(DEFS) $(TFLAGS) -o unit_test -nostartfiles --specs nosys.specs -e 0

riscv: DEFS += -DMG_ENABLE_POSIX_FS=0 -DMG_ENABLE_TCPIP=1 -DMG_ARCH=MG_ARCH_NEWLIB
riscv: DEFS += -DMG_ENABLE_POSIX_FS=0 -DMG_ENABLE_TCPIP=1 -DMG_ENABLE_TCPIP_DRIVER_INIT=0 -DMG_ARCH=MG_ARCH_NEWLIB
riscv: mongoose.h $(SRCS)
$(DOCKER) mdashnet/riscv riscv-none-elf-gcc -march=rv32imc -mabi=ilp32 $(SRCS) $(OPTS) $(WARN) $(INCS) $(DEFS) $(TFLAGS) -o unit_test

Expand Down
1 change: 1 addition & 0 deletions examples/arduino/w5500-http/mongoose_custom.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define MG_ARCH MG_ARCH_CUSTOM
#define MG_ENABLE_SOCKET 0
#define MG_ENABLE_TCPIP 1
#define MG_ENABLE_DRIVER_W5500 1
#define mkdir(a, b) (-1)
#define MG_IO_SIZE 128
//#define MG_ENABLE_LOG 0
1 change: 1 addition & 0 deletions examples/arduino/w5500-mqtt/mongoose_custom.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define MG_ARCH MG_ARCH_CUSTOM
#define MG_ENABLE_SOCKET 0
#define MG_ENABLE_TCPIP 1
#define MG_ENABLE_DRIVER_W5500 1
#define mkdir(a, b) (-1)
#define MG_IO_SIZE 512
//#define MG_ENABLE_LOG 0
2 changes: 1 addition & 1 deletion examples/mip-pcap/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ CFLAGS = -W -Wall -Wextra -g -I. # Build options
CFLAGS += -lpcap # link with libpcap

# Mongoose build options. See https://mongoose.ws/documentation/#build-options
CFLAGS_MONGOOSE += -DMG_ENABLE_LINES=1 -DMG_ENABLE_TCPIP=1 -DMG_ENABLE_SOCKET=0
CFLAGS_MONGOOSE += -DMG_ENABLE_LINES=1 -DMG_ENABLE_TCPIP=1 -DMG_ENABLE_SOCKET=0 -DMG_ENABLE_TCPIP_DRIVER_INIT=0

ifeq ($(OS),Windows_NT) # Windows settings. Assume MinGW compiler. To use VC: make CC=cl CFLAGS=/MD OUT=/Feprog.exe
PROG ?= example.exe # Use .exe suffix for the binary
Expand Down
2 changes: 1 addition & 1 deletion examples/mip-tap/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ SOURCES = main.c mongoose.c net.c packed_fs.c # Source code files
CFLAGS = -W -Wall -Wextra -g -I. # Build options

# Mongoose build options. See https://mongoose.ws/documentation/#build-options
CFLAGS_MONGOOSE += -DMG_ENABLE_LINES=1 -DMG_ENABLE_TCPIP=1 -DMG_ENABLE_SOCKET=0 -DMG_ENABLE_PACKED_FS=1
CFLAGS_MONGOOSE += -DMG_ENABLE_LINES=1 -DMG_ENABLE_TCPIP=1 -DMG_ENABLE_SOCKET=0 -DMG_ENABLE_PACKED_FS=1 -DMG_ENABLE_TCPIP_DRIVER_INIT=0

# Example specific build options. See README.md
CFLAGS += -DHTTP_URL=\"http://0.0.0.0/\" -DHTTPS_URL=\"https://0.0.0.0/\"
Expand Down
1 change: 1 addition & 0 deletions examples/rp2040/pico-rmii/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ pico_enable_stdio_uart(firmware 0) # to USB

# Mongoose build flags
add_definitions(-DMG_ENABLE_TCPIP=1)
add_definitions(-DMG_ENABLE_TCPIP_DRIVER_INIT=0)
add_definitions(-DMG_ENABLE_PACKED_FS=1)
1 change: 1 addition & 0 deletions examples/rp2040/pico-rndis-dashboard/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pico_enable_stdio_uart(firmware 1) # to the UART
# Mongoose build flags
add_definitions(-DMG_ENABLE_TCPIP=1)
add_definitions(-DMG_ENABLE_PACKED_FS=1)
add_definitions(-DMG_ENABLE_TCPIP_DRIVER_INIT=0)
add_definitions(-DMG_ENABLE_POSIX_FS=0)
add_definitions(-DDISABLE_ROUTING=1)

Expand Down
1 change: 1 addition & 0 deletions examples/rp2040/pico-rndis-device/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pico_enable_stdio_uart(firmware 1) # to the UART

# Mongoose build flags
add_definitions(-DMG_ENABLE_TCPIP=1)
add_definitions(-DMG_ENABLE_TCPIP_DRIVER_INIT=0)
add_definitions(-DMG_ENABLE_POSIX_FS=0)

# Example build options
1 change: 1 addition & 0 deletions examples/rp2040/pico-w5500/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pico_enable_stdio_uart(firmware 1) # to the UART, for remote testing

# Mongoose build flags
add_definitions(-DMG_ENABLE_TCPIP=1)
add_definitions(-DMG_ENABLE_DRIVER_W5500=1)
add_definitions(-DMG_ENABLE_PACKED_FS=1)
add_definitions(-DMG_ENABLE_CUSTOM_RANDOM=1)
add_definitions(-DMG_ENABLE_POSIX_FS=0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#define MG_ARCH MG_ARCH_NEWLIB

#define MG_ENABLE_TCPIP 1
#define MG_ENABLE_DRIVER_W5500 1
#define MG_ENABLE_CUSTOM_MILLIS 1
#define MG_ENABLE_CUSTOM_RANDOM 1
#define MG_IO_SIZE 128
Expand Down
38 changes: 36 additions & 2 deletions mongoose.c
Original file line number Diff line number Diff line change
Expand Up @@ -4851,6 +4851,11 @@ void mg_mgr_free(struct mg_mgr *mgr) {
mg_tls_ctx_free(mgr);
}


#if MG_ENABLE_TCPIP && MG_ENABLE_TCPIP_DRIVER_INIT
void mg_tcpip_auto_init(struct mg_mgr *);
#endif

void mg_mgr_init(struct mg_mgr *mgr) {
memset(mgr, 0, sizeof(*mgr));
#if MG_ENABLE_EPOLL
Expand All @@ -4869,6 +4874,8 @@ void mg_mgr_init(struct mg_mgr *mgr) {
// Ignore SIGPIPE signal, so if client cancels the request, it
// won't kill the whole process.
signal(SIGPIPE, SIG_IGN);
#elif MG_ENABLE_TCPIP && MG_ENABLE_TCPIP_DRIVER_INIT
mg_tcpip_auto_init(mgr);
#endif
mgr->pipe = MG_INVALID_SOCKET;
mgr->dnstimeout = 3000;
Expand Down Expand Up @@ -5747,6 +5754,14 @@ static void mg_tcpip_poll(struct mg_tcpip_if *ifp, uint64_t now) {
bool expired_1000ms = mg_timer_expired(&ifp->timer_1000ms, 1000, now);
ifp->now = now;

#if MG_ENABLE_TCPIP_PRINT_DEBUG_STATS
if (expired_1000ms) {
const char *names[] = {"down", "up", "req", "ready"};
MG_INFO(("Ethernet: %s, IP: %M, rx:%u, tx:%u, dr:%u, er:%u",
names[ifp->state], mg_print_ip4, &ifp->ip, ifp->nrecv, ifp->nsent,
ifp->ndrop, ifp->nerr));
}
#endif
// Handle physical interface up/down status
if (expired_1000ms && ifp->driver->up) {
bool up = ifp->driver->up(ifp);
Expand Down Expand Up @@ -6007,6 +6022,24 @@ bool mg_send(struct mg_connection *c, const void *buf, size_t len) {
}
return res;
}

#if MG_ENABLE_TCPIP_DRIVER_INIT && defined(MG_TCPIP_DRIVER_DATA)
void mg_tcpip_auto_init(struct mg_mgr *mgr);
void mg_tcpip_auto_init(struct mg_mgr *mgr) {
MG_TCPIP_DRIVER_DATA // static ... driver_data
struct mg_tcpip_if i = {
// let the compiler solve the macros at run time
.mac = MG_MAC_ADDRESS, .ip = MG_TCPIP_IP,
.mask = MG_TCPIP_MASK, .gw = MG_TCPIP_GW,
.driver = MG_TCPIP_DRIVER_CODE, .driver_data = &driver_data,
};
static struct mg_tcpip_if mif;

mif = i; // copy the initialized structure to a static to be exported
mg_tcpip_init(mgr, &mif);
MG_INFO(("Driver: " MG_TCPIP_DRIVER_NAME ", MAC: %M", mg_print_mac, mif.mac));
}
#endif
#endif // MG_ENABLE_TCPIP

#ifdef MG_ENABLE_LINES
Expand Down Expand Up @@ -14939,7 +14972,8 @@ struct mg_tcpip_driver mg_tcpip_driver_ra = {mg_tcpip_driver_ra_init,
#endif


#if defined(MG_ENABLE_DRIVER_SAME54) && MG_ENABLE_DRIVER_SAME54
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_SAME54) && MG_ENABLE_DRIVER_SAME54

#include <sam.h>

#define ETH_PKT_SIZE 1536 // Max frame size
Expand Down Expand Up @@ -15942,7 +15976,7 @@ struct mg_tcpip_driver mg_tcpip_driver_tm4c = {mg_tcpip_driver_tm4c_init,
#endif


#if MG_ENABLE_TCPIP
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_W5500) && MG_ENABLE_DRIVER_W5500

enum { W5500_CR = 0, W5500_S0 = 1, W5500_TX0 = 2, W5500_RX0 = 3 };

Expand Down
Loading

0 comments on commit c8fd3d6

Please sign in to comment.