Refactor autoinit code

This commit is contained in:
Sergey Lyubka committed 2024-03-15 20:23:37 +00:00
1 parent 15bd8b4fe9
commit fafc5c8348
14 files changed
+111 -303

No files matched your search

+7 -5
View File
@@ -162,19 +162,21 @@
#define MG_ENABLE_TCPIP_DRIVER_INIT 1 // enabled built-in driver for
#endif // Mongoose built-in network stack
#ifndef MG_TCPIP_IP // e.g. MG_IPV4(192, 168, 0, 223)
#define MG_TCPIP_IP MG_IPV4(0, 0, 0, 0) // or leave as 0 for DHCP
#ifndef MG_TCPIP_IP // e.g. MG_IPV4(192, 168, 0, 223)
#define MG_TCPIP_IP MG_IPV4(0, 0, 0, 0) // Default is 0.0.0.0 (DHCP)
#endif
#ifndef MG_TCPIP_MASK
#define MG_TCPIP_MASK MG_IPV4(255, 255, 255, 0)
#define MG_TCPIP_MASK MG_IPV4(0, 0, 0, 0) // Default is 0.0.0.0 (DHCP)
#endif
#ifndef MG_TCPIP_GW
#define MG_TCPIP_GW MG_IPV4(0, 0, 0, 1)
#define MG_TCPIP_GW MG_IPV4(0, 0, 0, 0) // Default is 0.0.0.0 (DHCP)
#endif
#define MG_MAC_ADDRESS_RANDOM { 0, 0, 0, 0, 0, 0 }
#ifndef MG_SET_MAC_ADDRESS
#define MG_SET_MAC_ADDRESS(mac)
#endif
#ifndef MG_ENABLE_TCPIP_PRINT_DEBUG_STATS
#define MG_ENABLE_TCPIP_PRINT_DEBUG_STATS 0
-9
View File
@@ -5,13 +5,4 @@
#include "Driver_ETH_MAC.h" // keep this include
#include "Driver_ETH_PHY.h" // keep this include
#ifndef MG_MAC_ADDRESS
#define MG_MAC_ADDRESS MG_MAC_ADDRESS_RANDOM
#endif
#define MG_TCPIP_DRIVER_DATA int driver_data;
#define MG_TCPIP_DRIVER_CODE &mg_tcpip_driver_cmsis
#define MG_TCPIP_DRIVER_NAME "cmsis"
#endif
-13
View File
@@ -19,10 +19,6 @@ struct mg_tcpip_driver_imxrt_data {
uint8_t phy_addr; // PHY address
};
#ifndef MG_MAC_ADDRESS
#define MG_MAC_ADDRESS MG_MAC_ADDRESS_RANDOM
#endif
#ifndef MG_TCPIP_PHY_ADDR
#define MG_TCPIP_PHY_ADDR 2
#endif
@@ -31,13 +27,4 @@ struct mg_tcpip_driver_imxrt_data {
#define MG_DRIVER_MDC_CR 24
#endif
#define MG_TCPIP_DRIVER_DATA \
static struct mg_tcpip_driver_imxrt_data driver_data = { \
.mdc_cr = MG_DRIVER_MDC_CR, \
.phy_addr = MG_TCPIP_PHY_ADDR, \
};
#define MG_TCPIP_DRIVER_CODE &mg_tcpip_driver_imxrt
#define MG_TCPIP_DRIVER_NAME "imxrt"
#endif
-29
View File
@@ -9,33 +9,4 @@ struct mg_tcpip_driver_ra_data {
uint8_t phy_addr; // PHY address
};
#undef MG_ENABLE_TCPIP_DRIVER_INIT
#define MG_ENABLE_TCPIP_DRIVER_INIT 0 // TODO(): needs SystemCoreClock
#if 0
#ifndef MG_MAC_ADDRESS
#define MG_MAC_ADDRESS MG_MAC_ADDRESS_RANDOM
#endif
#ifndef MG_TCPIP_PHY_ADDR
#define MG_TCPIP_PHY_ADDR 1
#endif
#ifndef MG_DRIVER_RA_CLOCK
#define MG_DRIVER_RA_CLOCK
#endif
#ifndef MG_DRIVER_RA_IRQNO
#define MG_DRIVER_RA_IRQNO 0
#endif
#define MG_TCPIP_DRIVER_DATA \
static struct mg_tcpip_driver_ra_data driver_data = { \
.clock = MG_DRIVER_RA_CLOCK, \
.irqno = MG_DRIVER_RA_CLOCK, \
.phy_addr = MG_TCPIP_PHY_ADDR, \
};
#define MG_TCPIP_DRIVER_CODE &mg_tcpip_driver_ra
#define MG_TCPIP_DRIVER_NAME "ra"
#endif
#endif
-12
View File
@@ -6,20 +6,8 @@ struct mg_tcpip_driver_same54_data {
int mdc_cr;
};
#ifndef MG_MAC_ADDRESS
#define MG_MAC_ADDRESS MG_MAC_ADDRESS_RANDOM
#endif
#ifndef MG_DRIVER_MDC_CR
#define MG_DRIVER_MDC_CR 5
#endif
#define MG_TCPIP_DRIVER_DATA \
static struct mg_tcpip_driver_same54_data driver_data = { \
.mdc_cr = MG_DRIVER_MDC_CR, \
};
#define MG_TCPIP_DRIVER_CODE &mg_tcpip_driver_same54
#define MG_TCPIP_DRIVER_NAME "same54"
#endif
+15 -12
View File
@@ -20,10 +20,6 @@ struct mg_tcpip_driver_stm32f_data {
uint8_t phy_addr; // PHY address
};
#ifndef MG_MAC_ADDRESS
#define MG_MAC_ADDRESS MG_MAC_ADDRESS_RANDOM
#endif
#ifndef MG_TCPIP_PHY_ADDR
#define MG_TCPIP_PHY_ADDR 0
#endif
@@ -32,13 +28,20 @@ struct mg_tcpip_driver_stm32f_data {
#define MG_DRIVER_MDC_CR 4
#endif
#define MG_TCPIP_DRIVER_DATA \
static struct mg_tcpip_driver_stm32f_data driver_data = { \
.mdc_cr = MG_DRIVER_MDC_CR, \
.phy_addr = MG_TCPIP_PHY_ADDR, \
};
#define MG_TCPIP_DRIVER_CODE &mg_tcpip_driver_stm32f
#define MG_TCPIP_DRIVER_NAME "stm32f"
#define MG_TCPIP_DRIVER_INIT(mgr) \
do { \
static struct mg_tcpip_driver_stm32f_data driver_data_; \
static struct mg_tcpip_if mif_; \
driver_data_.mdc_cr = MG_DRIVER_MDC_CR; \
driver_data_.phy_addr = MG_TCPIP_PHY_ADDR; \
mif_.ip = MG_TCPIP_IP; \
mif_.mask = MG_TCPIP_MASK; \
mif_.gw = MG_TCPIP_GW; \
mif_.driver = &mg_tcpip_driver_stm32f; \
mif_.driver_data = &driver_data_; \
MG_SET_MAC_ADDRESS(mif_.mac); \
mg_tcpip_init(mgr, &mif_); \
MG_INFO(("Driver: stm32f, MAC: %M", mg_print_mac, mif_.mac)); \
} while (0)
#endif
+18 -15
View File
@@ -12,19 +12,15 @@ struct mg_tcpip_driver_stm32h_data {
// 100-150 MHz HCLK/62 1
// 20-35 MHz HCLK/16 2
// 35-60 MHz HCLK/26 3
// 150-250 MHz HCLK/102 4 <-- value for Nucleo-H* on max speed
// driven by HSI 250-300 MHz HCLK/124 5 <-- value for Nucleo-H* on
// max speed driven by CSI 110, 111 Reserved
// 150-250 MHz HCLK/102 4 <-- value for max speed HSI
// 250-300 MHz HCLK/124 5 <-- value for Nucleo-H* on CSI
// 110, 111 Reserved
int mdc_cr; // Valid values: -1, 0, 1, 2, 3, 4, 5
uint8_t phy_addr; // PHY address
uint8_t phy_conf; // PHY config
};
#ifndef MG_MAC_ADDRESS
#define MG_MAC_ADDRESS MG_MAC_ADDRESS_RANDOM
#endif
#ifndef MG_TCPIP_PHY_ADDR
#define MG_TCPIP_PHY_ADDR 0
#endif
@@ -33,13 +29,20 @@ struct mg_tcpip_driver_stm32h_data {
#define MG_DRIVER_MDC_CR 4
#endif
#define MG_TCPIP_DRIVER_DATA \
static struct mg_tcpip_driver_stm32h_data driver_data = { \
.mdc_cr = MG_DRIVER_MDC_CR, \
.phy_addr = MG_TCPIP_PHY_ADDR, \
};
#define MG_TCPIP_DRIVER_CODE &mg_tcpip_driver_stm32h
#define MG_TCPIP_DRIVER_NAME "stm32h"
#define MG_TCPIP_DRIVER_INIT(mgr) \
do { \
static struct mg_tcpip_driver_stm32h_data driver_data_; \
static struct mg_tcpip_if mif_; \
driver_data_.mdc_cr = MG_DRIVER_MDC_CR; \
driver_data_.phy_addr = MG_TCPIP_PHY_ADDR; \
mif_.ip = MG_TCPIP_IP; \
mif_.mask = MG_TCPIP_MASK; \
mif_.gw = MG_TCPIP_GW; \
mif_.driver = &mg_tcpip_driver_stm32h; \
mif_.driver_data = &driver_data_; \
MG_SET_MAC_ADDRESS(mif_.mac); \
mg_tcpip_init(mgr, &mif_); \
MG_INFO(("Driver: stm32h, MAC: %M", mg_print_mac, mif_.mac)); \
} while (0)
#endif
-13
View File
@@ -15,21 +15,8 @@ struct mg_tcpip_driver_tm4c_data {
int mdc_cr; // Valid values: -1, 0, 1, 2, 3
};
#ifndef MG_MAC_ADDRESS
#define MG_MAC_ADDRESS MG_MAC_ADDRESS_RANDOM
#endif
#ifndef MG_DRIVER_MDC_CR
#define MG_DRIVER_MDC_CR 1
#endif
#define MG_TCPIP_DRIVER_DATA \
static struct mg_tcpip_driver_tm4c_data driver_data = { \
.mdc_cr = MG_DRIVER_MDC_CR, \
};
#define MG_TCPIP_DRIVER_CODE &mg_tcpip_driver_tm4c
#define MG_TCPIP_DRIVER_NAME "tm4c"
#endif
+2 -7
View File
@@ -252,11 +252,6 @@ 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
@@ -275,8 +270,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);
#elif MG_ENABLE_TCPIP_DRIVER_INIT && defined(MG_TCPIP_DRIVER_INIT)
MG_TCPIP_DRIVER_INIT(mgr);
#endif
mgr->pipe = MG_INVALID_SOCKET;
mgr->dnstimeout = 3000;
+4 -21
View File
@@ -868,7 +868,7 @@ static void mg_tcpip_poll(struct mg_tcpip_if *ifp, uint64_t 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",
MG_INFO(("Status: %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));
}
@@ -1028,7 +1028,7 @@ void mg_connect_resolved(struct mg_connection *c) {
if (c->is_udp && (rem_ip == 0xffffffff || rem_ip == (ifp->ip | ~ifp->mask))) {
struct connstate *s = (struct connstate *) (c + 1);
memset(s->mac, 0xFF, sizeof(s->mac)); // global or local broadcast
} else if (((rem_ip & ifp->mask) == (ifp->ip & ifp->mask))) {
} else if (ifp->ip && ((rem_ip & ifp->mask) == (ifp->ip & ifp->mask))) {
// If we're in the same LAN, fire an ARP lookup.
MG_DEBUG(("%lu ARP lookup...", c->id));
arp_ask(ifp, rem_ip);
@@ -1099,8 +1099,9 @@ void mg_mgr_poll(struct mg_mgr *mgr, int ms) {
struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) mgr->priv;
struct mg_connection *c, *tmp;
uint64_t now = mg_millis();
if (ifp != NULL && ifp->driver != NULL) mg_tcpip_poll(ifp, now);
mg_timer_poll(&mgr->timers, now);
if (ifp == NULL || ifp->driver == NULL) return;
mg_tcpip_poll(ifp, now);
for (c = mgr->conns; c != NULL; c = tmp) {
tmp = c->next;
struct connstate *s = (struct connstate *) (c + 1);
@@ -1133,22 +1134,4 @@ 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