Skip to content

Commit

Permalink
Merge pull request #2648 from cesanta/autoinit
Browse files Browse the repository at this point in the history
Refactor autoinit code
  • Loading branch information
scaprile authored Mar 17, 2024
2 parents 15bd8b4 + fafc5c8 commit d7b2b3f
Show file tree
Hide file tree
Showing 14 changed files with 111 additions and 303 deletions.
26 changes: 3 additions & 23 deletions examples/stm32/nucleo-f746zg-make-baremetal-builtin/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,8 @@ void mg_random(void *buf, size_t len) { // Use on-board RNG
}

static void timer_fn(void *arg) {
gpio_toggle(LED); // Blink LED
struct mg_tcpip_if *ifp = arg; // And show
const char *names[] = {"down", "up", "req", "ready"}; // network stats
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));
gpio_toggle(LED); // Blink LED
(void) arg;
}

int main(void) {
Expand All @@ -41,23 +37,7 @@ int main(void) {
struct mg_mgr mgr; // Initialise
mg_mgr_init(&mgr); // Mongoose event manager
mg_log_set(MG_LL_DEBUG); // Set log level

// Initialise Mongoose network stack
struct mg_tcpip_driver_stm32f_data driver_data = {.mdc_cr = 4};
struct mg_tcpip_if mif = {.mac = GENERATE_LOCALLY_ADMINISTERED_MAC(),
// Uncomment below for static configuration:
// .ip = mg_htonl(MG_U32(192, 168, 0, 223)),
// .mask = mg_htonl(MG_U32(255, 255, 255, 0)),
// .gw = mg_htonl(MG_U32(192, 168, 0, 1)),
.driver = &mg_tcpip_driver_stm32f,
.driver_data = &driver_data};
mg_tcpip_init(&mgr, &mif);
mg_timer_add(&mgr, BLINK_PERIOD_MS, MG_TIMER_REPEAT, timer_fn, &mif);

MG_INFO(("MAC: %M. Waiting for IP...", mg_print_mac, mif.mac));
while (mif.state != MG_TCPIP_STATE_READY) {
mg_mgr_poll(&mgr, 0);
}
mg_timer_add(&mgr, BLINK_PERIOD_MS, MG_TIMER_REPEAT, timer_fn, NULL);

MG_INFO(("Initialising application..."));
web_init(&mgr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,19 @@
#define MG_ENABLE_PACKED_FS 1
#define MG_ENABLE_DRIVER_STM32F 1
#define MG_ENABLE_LINES 1
#define MG_ENABLE_TCPIP_PRINT_DEBUG_STATS 1

// For static IP configuration, define MG_TCPIP_{IP,MASK,GW}
// By default, those are set to zero, meaning that DHCP is used
//
// #define MG_TCPIP_IP MG_IPV4(192, 168, 0, 10)
// #define MG_TCPIP_GW MG_IPV4(192, 168, 0, 1)
// #define MG_TCPIP_MASK MG_IPV4(255, 255, 255, 0)

// Set custom MAC address. By default, it is randomly generated
// Using a build-time constant:
// #define MG_SET_MAC_ADDRESS(mac) do { uint8_t buf_[6] = {2,3,4,5,6,7}; memmove(mac, buf_, sizeof(buf_)); } while (0)
//
// Using custom function:
// extern void my_function(unsigned char *mac);
// #define MG_SET_MAC_ADDRESS(mac) my_function(mac)
34 changes: 6 additions & 28 deletions mongoose.c
Original file line number Diff line number Diff line change
Expand Up @@ -4851,11 +4851,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
Expand All @@ -4874,8 +4869,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;
Expand Down Expand Up @@ -5757,7 +5752,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));
}
Expand Down Expand Up @@ -5917,7 +5912,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);
Expand Down Expand Up @@ -5988,8 +5983,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);
Expand Down Expand Up @@ -6022,24 +6018,6 @@ 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
156 changes: 40 additions & 116 deletions mongoose.h
Original file line number Diff line number Diff line change
Expand Up @@ -837,19 +837,21 @@ 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) // 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
Expand Down Expand Up @@ -2882,15 +2884,6 @@ 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


Expand All @@ -2913,10 +2906,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
Expand All @@ -2925,15 +2914,6 @@ 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


Expand Down Expand Up @@ -2968,35 +2948,6 @@ 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


Expand All @@ -3006,22 +2957,10 @@ 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


Expand All @@ -3045,10 +2984,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
Expand All @@ -3057,14 +2992,21 @@ 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

Expand All @@ -3081,19 +3023,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
Expand All @@ -3102,14 +3040,21 @@ 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

Expand All @@ -3129,31 +3074,10 @@ 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


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

#undef MG_ENABLE_TCPIP_DRIVER_INIT
#define MG_ENABLE_TCPIP_DRIVER_INIT 0

#endif

#ifdef __cplusplus
Expand Down
Loading

0 comments on commit d7b2b3f

Please sign in to comment.