Add driver init to mg_mgr_init()

This commit is contained in:
Sergio R. Caprile 2024-03-12 14:03:29 -03:00
parent 853de8b623
commit c2d26553c6
6 changed files with 163 additions and 72 deletions

View File

@ -4031,6 +4031,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
@ -4049,8 +4054,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_DRIVER_INIT && defined(MG_TCPIP_DRIVER_INIT)
MG_TCPIP_DRIVER_INIT(mgr);
#elif MG_ENABLE_TCPIP && MG_ENABLE_TCPIP_DRIVER_INIT
mg_tcpip_auto_init(mgr);
#endif
mgr->pipe = MG_INVALID_SOCKET;
mgr->dnstimeout = 3000;
@ -4966,8 +4971,16 @@ static void mg_tcpip_poll(struct mg_tcpip_if *ifp, uint64_t now) {
#if MG_ENABLE_TCPIP_PRINT_DEBUG_STATS
if (expired_1000ms) {
<<<<<<< HEAD
const char *names[] = {"down", "up", "req", "ip", "ready"};
=======
const char *names[] = {"down", "up", "req", "ready"};
<<<<<<< HEAD
>>>>>>> 60cce282 (Add driver init to mg_mgr_init())
MG_INFO(("Status: %s, IP: %M, rx:%u, tx:%u, dr:%u, er:%u",
=======
MG_INFO(("Ethernet: %s, IP: %M, rx:%u, tx:%u, dr:%u, er:%u",
>>>>>>> a0834330 (Add driver init to mg_mgr_init())
names[ifp->state], mg_print_ip4, &ifp->ip, ifp->nrecv, ifp->nsent,
ifp->ndrop, ifp->nerr));
}
@ -5261,6 +5274,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

View File

@ -876,21 +876,19 @@ struct timeval {
#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) // Default is 0.0.0.0 (DHCP)
#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
#endif
#ifndef MG_TCPIP_MASK
#define MG_TCPIP_MASK MG_IPV4(0, 0, 0, 0) // Default is 0.0.0.0 (DHCP)
#define MG_TCPIP_MASK MG_IPV4(255, 255, 255, 0)
#endif
#ifndef MG_TCPIP_GW
#define MG_TCPIP_GW MG_IPV4(0, 0, 0, 0) // Default is 0.0.0.0 (DHCP)
#define MG_TCPIP_GW MG_IPV4(0, 0, 0, 1)
#endif
#ifndef MG_SET_MAC_ADDRESS
#define MG_SET_MAC_ADDRESS(mac)
#endif
#define MG_MAC_ADDRESS_RANDOM { 0, 0, 0, 0, 0, 0 }
#ifndef MG_ENABLE_TCPIP_PRINT_DEBUG_STATS
#define MG_ENABLE_TCPIP_PRINT_DEBUG_STATS 0
@ -1115,6 +1113,8 @@ bool mg_path_is_sane(const struct mg_str path);
#define MG_IPV4(a, b, c, d) mg_htonl(MG_U32(a, b, c, d))
#define MG_IPV4(a, b, c, d) mg_htonl(MG_U32(a, b, c, d))
// For printing IPv4 addresses: printf("%d.%d.%d.%d\n", MG_IPADDR_PARTS(&ip))
#define MG_U8P(ADDR) ((uint8_t *) (ADDR))
#define MG_IPADDR_PARTS(ADDR) \
@ -2873,8 +2873,19 @@ struct mg_profitem {
#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
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_IMXRT) && MG_ENABLE_DRIVER_IMXRT
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_IMXRT) && MG_ENABLE_DRIVER_IMXRT
@ -2895,6 +2906,10 @@ 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
@ -2903,55 +2918,14 @@ struct mg_tcpip_driver_imxrt_data {
#define MG_DRIVER_MDC_CR 24
#endif
#define MG_TCPIP_DRIVER_INIT(mgr) \
do { \
static struct mg_tcpip_driver_imxrt_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_imxrt; \
mif_.driver_data = &driver_data_; \
MG_SET_MAC_ADDRESS(mif_.mac); \
mg_tcpip_init(mgr, &mif_); \
MG_INFO(("Driver: imxrt, MAC: %M", mg_print_mac, mif_.mac)); \
} while (0)
#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, \
};
#endif
struct mg_phy {
uint16_t (*read_reg)(uint8_t addr, uint8_t reg);
void (*write_reg)(uint8_t addr, uint8_t reg, uint16_t value);
};
// PHY configuration settings, bitmask
enum {
// Set if PHY LEDs are connected to ground
MG_PHY_LEDS_ACTIVE_HIGH = (1 << 0),
// Set when PHY clocks MAC. Otherwise, MAC clocks PHY
MG_PHY_CLOCKS_MAC = (1 << 1),
};
enum { MG_PHY_SPEED_10M, MG_PHY_SPEED_100M, MG_PHY_SPEED_1000M };
void mg_phy_init(struct mg_phy *, uint8_t addr, uint8_t config);
bool mg_phy_up(struct mg_phy *, uint8_t addr, bool *full_duplex,
uint8_t *speed);
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_RA) && MG_ENABLE_DRIVER_RA
struct mg_tcpip_driver_ra_data {
// MDC clock "divider". MDC clock is software generated,
uint32_t clock; // core clock frequency in Hz
uint16_t irqno; // IRQn, R_ICU->IELSR[irqno]
uint8_t phy_addr; // PHY address
};
#define MG_TCPIP_DRIVER_CODE &mg_tcpip_driver_imxrt
#define MG_TCPIP_DRIVER_NAME "imxrt"
#endif
@ -2962,10 +2936,22 @@ 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
@ -2989,6 +2975,10 @@ 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
@ -2997,21 +2987,14 @@ struct mg_tcpip_driver_stm32f_data {
#define MG_DRIVER_MDC_CR 4
#endif
#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)
#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"
#endif
@ -3044,18 +3027,27 @@ struct mg_tcpip_driver_stm32h_data {
uint8_t phy_conf; // PHY config
};
<<<<<<< HEAD
#ifndef MG_TCPIP_PHY_CONF
#define MG_TCPIP_PHY_CONF MG_PHY_CLOCKS_MAC
#endif
=======
<<<<<<< HEAD
>>>>>>> 60cce282 (Add driver init to mg_mgr_init())
#ifndef MG_TCPIP_PHY_ADDR
#define MG_TCPIP_PHY_ADDR 0
=======
#ifndef MG_MAC_ADDRESS
#define MG_MAC_ADDRESS MG_MAC_ADDRESS_RANDOM
>>>>>>> a0834330 (Add driver init to mg_mgr_init())
#endif
#ifndef MG_DRIVER_MDC_CR
#define MG_DRIVER_MDC_CR 4
#endif
<<<<<<< HEAD
#define MG_TCPIP_DRIVER_INIT(mgr) \
do { \
static struct mg_tcpip_driver_stm32h_data driver_data_; \
@ -3072,6 +3064,15 @@ struct mg_tcpip_driver_stm32h_data {
mg_tcpip_init(mgr, &mif_); \
MG_INFO(("Driver: stm32h, MAC: %M", mg_print_mac, mif_.mac)); \
} while (0)
=======
#define MG_TCPIP_DRIVER_DATA \
static struct mg_tcpip_driver_stm32h_data driver_data = { \
.mdc_cr = MG_DRIVER_MDC_CR, \
};
#define MG_TCPIP_DRIVER_CODE &mg_tcpip_driver_stm32h
#define MG_TCPIP_DRIVER_NAME "stm32h"
>>>>>>> a0834330 (Add driver init to mg_mgr_init())
#endif
#endif
@ -3092,10 +3093,18 @@ struct mg_tcpip_driver_tm4c_data {
int mdc_cr; // Valid values: -1, 0, 1, 2, 3
};
<<<<<<< HEAD
=======
#ifndef MG_MAC_ADDRESS
#define MG_MAC_ADDRESS MG_MAC_ADDRESS_RANDOM
#endif
>>>>>>> a0834330 (Add driver init to mg_mgr_init())
#ifndef MG_DRIVER_MDC_CR
#define MG_DRIVER_MDC_CR 1
#endif
<<<<<<< HEAD
#define MG_TCPIP_DRIVER_INIT(mgr) \
do { \
static struct mg_tcpip_driver_tm4c_data driver_data_; \
@ -3111,6 +3120,20 @@ struct mg_tcpip_driver_tm4c_data {
MG_INFO(("Driver: tm4c, MAC: %M", mg_print_mac, mif_.mac)); \
} while (0)
=======
<<<<<<< HEAD
=======
#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"
>>>>>>> a0834330 (Add driver init to mg_mgr_init())
>>>>>>> 60cce282 (Add driver init to mg_mgr_init())
#endif
@ -3151,6 +3174,7 @@ struct mg_tcpip_driver_tms570_data {
#endif
<<<<<<< HEAD
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_XMC7) && MG_ENABLE_DRIVER_XMC7
@ -3233,6 +3257,8 @@ struct mg_tcpip_driver_xmc_data {
#endif
=======
>>>>>>> a0834330 (Add driver init to mg_mgr_init())
#ifdef __cplusplus
}
#endif

View File

@ -5,4 +5,13 @@
#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

View File

@ -249,6 +249,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

View File

@ -1197,4 +1197,22 @@ 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

View File

@ -29,6 +29,8 @@ bool mg_path_is_sane(const struct mg_str path);
#define MG_IPV4(a, b, c, d) mg_htonl(MG_U32(a, b, c, d))
#define MG_IPV4(a, b, c, d) mg_htonl(MG_U32(a, b, c, d))
// For printing IPv4 addresses: printf("%d.%d.%d.%d\n", MG_IPADDR_PARTS(&ip))
#define MG_U8P(ADDR) ((uint8_t *) (ADDR))
#define MG_IPADDR_PARTS(ADDR) \