Skip to content

Commit d7b2b3f

Browse files
authored
Merge pull request #2648 from cesanta/autoinit
Refactor autoinit code
2 parents 15bd8b4 + fafc5c8 commit d7b2b3f

File tree

14 files changed

+111
-303
lines changed

14 files changed

+111
-303
lines changed

examples/stm32/nucleo-f746zg-make-baremetal-builtin/main.c

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,8 @@ void mg_random(void *buf, size_t len) { // Use on-board RNG
2424
}
2525

2626
static void timer_fn(void *arg) {
27-
gpio_toggle(LED); // Blink LED
28-
struct mg_tcpip_if *ifp = arg; // And show
29-
const char *names[] = {"down", "up", "req", "ready"}; // network stats
30-
MG_INFO(("Ethernet: %s, IP: %M, rx:%u, tx:%u, dr:%u, er:%u",
31-
names[ifp->state], mg_print_ip4, &ifp->ip, ifp->nrecv, ifp->nsent,
32-
ifp->ndrop, ifp->nerr));
27+
gpio_toggle(LED); // Blink LED
28+
(void) arg;
3329
}
3430

3531
int main(void) {
@@ -41,23 +37,7 @@ int main(void) {
4137
struct mg_mgr mgr; // Initialise
4238
mg_mgr_init(&mgr); // Mongoose event manager
4339
mg_log_set(MG_LL_DEBUG); // Set log level
44-
45-
// Initialise Mongoose network stack
46-
struct mg_tcpip_driver_stm32f_data driver_data = {.mdc_cr = 4};
47-
struct mg_tcpip_if mif = {.mac = GENERATE_LOCALLY_ADMINISTERED_MAC(),
48-
// Uncomment below for static configuration:
49-
// .ip = mg_htonl(MG_U32(192, 168, 0, 223)),
50-
// .mask = mg_htonl(MG_U32(255, 255, 255, 0)),
51-
// .gw = mg_htonl(MG_U32(192, 168, 0, 1)),
52-
.driver = &mg_tcpip_driver_stm32f,
53-
.driver_data = &driver_data};
54-
mg_tcpip_init(&mgr, &mif);
55-
mg_timer_add(&mgr, BLINK_PERIOD_MS, MG_TIMER_REPEAT, timer_fn, &mif);
56-
57-
MG_INFO(("MAC: %M. Waiting for IP...", mg_print_mac, mif.mac));
58-
while (mif.state != MG_TCPIP_STATE_READY) {
59-
mg_mgr_poll(&mgr, 0);
60-
}
40+
mg_timer_add(&mgr, BLINK_PERIOD_MS, MG_TIMER_REPEAT, timer_fn, NULL);
6141

6242
MG_INFO(("Initialising application..."));
6343
web_init(&mgr);

examples/stm32/nucleo-f746zg-make-baremetal-builtin/mongoose_custom.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,19 @@
99
#define MG_ENABLE_PACKED_FS 1
1010
#define MG_ENABLE_DRIVER_STM32F 1
1111
#define MG_ENABLE_LINES 1
12+
#define MG_ENABLE_TCPIP_PRINT_DEBUG_STATS 1
13+
14+
// For static IP configuration, define MG_TCPIP_{IP,MASK,GW}
15+
// By default, those are set to zero, meaning that DHCP is used
16+
//
17+
// #define MG_TCPIP_IP MG_IPV4(192, 168, 0, 10)
18+
// #define MG_TCPIP_GW MG_IPV4(192, 168, 0, 1)
19+
// #define MG_TCPIP_MASK MG_IPV4(255, 255, 255, 0)
20+
21+
// Set custom MAC address. By default, it is randomly generated
22+
// Using a build-time constant:
23+
// #define MG_SET_MAC_ADDRESS(mac) do { uint8_t buf_[6] = {2,3,4,5,6,7}; memmove(mac, buf_, sizeof(buf_)); } while (0)
24+
//
25+
// Using custom function:
26+
// extern void my_function(unsigned char *mac);
27+
// #define MG_SET_MAC_ADDRESS(mac) my_function(mac)

mongoose.c

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4851,11 +4851,6 @@ void mg_mgr_free(struct mg_mgr *mgr) {
48514851
mg_tls_ctx_free(mgr);
48524852
}
48534853

4854-
4855-
#if MG_ENABLE_TCPIP && MG_ENABLE_TCPIP_DRIVER_INIT
4856-
void mg_tcpip_auto_init(struct mg_mgr *);
4857-
#endif
4858-
48594854
void mg_mgr_init(struct mg_mgr *mgr) {
48604855
memset(mgr, 0, sizeof(*mgr));
48614856
#if MG_ENABLE_EPOLL
@@ -4874,8 +4869,8 @@ void mg_mgr_init(struct mg_mgr *mgr) {
48744869
// Ignore SIGPIPE signal, so if client cancels the request, it
48754870
// won't kill the whole process.
48764871
signal(SIGPIPE, SIG_IGN);
4877-
#elif MG_ENABLE_TCPIP && MG_ENABLE_TCPIP_DRIVER_INIT
4878-
mg_tcpip_auto_init(mgr);
4872+
#elif MG_ENABLE_TCPIP_DRIVER_INIT && defined(MG_TCPIP_DRIVER_INIT)
4873+
MG_TCPIP_DRIVER_INIT(mgr);
48794874
#endif
48804875
mgr->pipe = MG_INVALID_SOCKET;
48814876
mgr->dnstimeout = 3000;
@@ -5757,7 +5752,7 @@ static void mg_tcpip_poll(struct mg_tcpip_if *ifp, uint64_t now) {
57575752
#if MG_ENABLE_TCPIP_PRINT_DEBUG_STATS
57585753
if (expired_1000ms) {
57595754
const char *names[] = {"down", "up", "req", "ready"};
5760-
MG_INFO(("Ethernet: %s, IP: %M, rx:%u, tx:%u, dr:%u, er:%u",
5755+
MG_INFO(("Status: %s, IP: %M, rx:%u, tx:%u, dr:%u, er:%u",
57615756
names[ifp->state], mg_print_ip4, &ifp->ip, ifp->nrecv, ifp->nsent,
57625757
ifp->ndrop, ifp->nerr));
57635758
}
@@ -5917,7 +5912,7 @@ void mg_connect_resolved(struct mg_connection *c) {
59175912
if (c->is_udp && (rem_ip == 0xffffffff || rem_ip == (ifp->ip | ~ifp->mask))) {
59185913
struct connstate *s = (struct connstate *) (c + 1);
59195914
memset(s->mac, 0xFF, sizeof(s->mac)); // global or local broadcast
5920-
} else if (((rem_ip & ifp->mask) == (ifp->ip & ifp->mask))) {
5915+
} else if (ifp->ip && ((rem_ip & ifp->mask) == (ifp->ip & ifp->mask))) {
59215916
// If we're in the same LAN, fire an ARP lookup.
59225917
MG_DEBUG(("%lu ARP lookup...", c->id));
59235918
arp_ask(ifp, rem_ip);
@@ -5988,8 +5983,9 @@ void mg_mgr_poll(struct mg_mgr *mgr, int ms) {
59885983
struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) mgr->priv;
59895984
struct mg_connection *c, *tmp;
59905985
uint64_t now = mg_millis();
5991-
if (ifp != NULL && ifp->driver != NULL) mg_tcpip_poll(ifp, now);
59925986
mg_timer_poll(&mgr->timers, now);
5987+
if (ifp == NULL || ifp->driver == NULL) return;
5988+
mg_tcpip_poll(ifp, now);
59935989
for (c = mgr->conns; c != NULL; c = tmp) {
59945990
tmp = c->next;
59955991
struct connstate *s = (struct connstate *) (c + 1);
@@ -6022,24 +6018,6 @@ bool mg_send(struct mg_connection *c, const void *buf, size_t len) {
60226018
}
60236019
return res;
60246020
}
6025-
6026-
#if MG_ENABLE_TCPIP_DRIVER_INIT && defined(MG_TCPIP_DRIVER_DATA)
6027-
void mg_tcpip_auto_init(struct mg_mgr *mgr);
6028-
void mg_tcpip_auto_init(struct mg_mgr *mgr) {
6029-
MG_TCPIP_DRIVER_DATA // static ... driver_data
6030-
struct mg_tcpip_if i = {
6031-
// let the compiler solve the macros at run time
6032-
.mac = MG_MAC_ADDRESS, .ip = MG_TCPIP_IP,
6033-
.mask = MG_TCPIP_MASK, .gw = MG_TCPIP_GW,
6034-
.driver = MG_TCPIP_DRIVER_CODE, .driver_data = &driver_data,
6035-
};
6036-
static struct mg_tcpip_if mif;
6037-
6038-
mif = i; // copy the initialized structure to a static to be exported
6039-
mg_tcpip_init(mgr, &mif);
6040-
MG_INFO(("Driver: " MG_TCPIP_DRIVER_NAME ", MAC: %M", mg_print_mac, mif.mac));
6041-
}
6042-
#endif
60436021
#endif // MG_ENABLE_TCPIP
60446022

60456023
#ifdef MG_ENABLE_LINES

mongoose.h

Lines changed: 40 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -837,19 +837,21 @@ struct timeval {
837837
#define MG_ENABLE_TCPIP_DRIVER_INIT 1 // enabled built-in driver for
838838
#endif // Mongoose built-in network stack
839839

840-
#ifndef MG_TCPIP_IP // e.g. MG_IPV4(192, 168, 0, 223)
841-
#define MG_TCPIP_IP MG_IPV4(0, 0, 0, 0) // or leave as 0 for DHCP
840+
#ifndef MG_TCPIP_IP // e.g. MG_IPV4(192, 168, 0, 223)
841+
#define MG_TCPIP_IP MG_IPV4(0, 0, 0, 0) // Default is 0.0.0.0 (DHCP)
842842
#endif
843843

844844
#ifndef MG_TCPIP_MASK
845-
#define MG_TCPIP_MASK MG_IPV4(255, 255, 255, 0)
845+
#define MG_TCPIP_MASK MG_IPV4(0, 0, 0, 0) // Default is 0.0.0.0 (DHCP)
846846
#endif
847847

848848
#ifndef MG_TCPIP_GW
849-
#define MG_TCPIP_GW MG_IPV4(0, 0, 0, 1)
849+
#define MG_TCPIP_GW MG_IPV4(0, 0, 0, 0) // Default is 0.0.0.0 (DHCP)
850850
#endif
851851

852-
#define MG_MAC_ADDRESS_RANDOM { 0, 0, 0, 0, 0, 0 }
852+
#ifndef MG_SET_MAC_ADDRESS
853+
#define MG_SET_MAC_ADDRESS(mac)
854+
#endif
853855

854856
#ifndef MG_ENABLE_TCPIP_PRINT_DEBUG_STATS
855857
#define MG_ENABLE_TCPIP_PRINT_DEBUG_STATS 0
@@ -2882,15 +2884,6 @@ struct mg_profitem {
28822884
#include "Driver_ETH_MAC.h" // keep this include
28832885
#include "Driver_ETH_PHY.h" // keep this include
28842886

2885-
#ifndef MG_MAC_ADDRESS
2886-
#define MG_MAC_ADDRESS MG_MAC_ADDRESS_RANDOM
2887-
#endif
2888-
2889-
#define MG_TCPIP_DRIVER_DATA int driver_data;
2890-
2891-
#define MG_TCPIP_DRIVER_CODE &mg_tcpip_driver_cmsis
2892-
#define MG_TCPIP_DRIVER_NAME "cmsis"
2893-
28942887
#endif
28952888

28962889

@@ -2913,10 +2906,6 @@ struct mg_tcpip_driver_imxrt_data {
29132906
uint8_t phy_addr; // PHY address
29142907
};
29152908

2916-
#ifndef MG_MAC_ADDRESS
2917-
#define MG_MAC_ADDRESS MG_MAC_ADDRESS_RANDOM
2918-
#endif
2919-
29202909
#ifndef MG_TCPIP_PHY_ADDR
29212910
#define MG_TCPIP_PHY_ADDR 2
29222911
#endif
@@ -2925,15 +2914,6 @@ struct mg_tcpip_driver_imxrt_data {
29252914
#define MG_DRIVER_MDC_CR 24
29262915
#endif
29272916

2928-
#define MG_TCPIP_DRIVER_DATA \
2929-
static struct mg_tcpip_driver_imxrt_data driver_data = { \
2930-
.mdc_cr = MG_DRIVER_MDC_CR, \
2931-
.phy_addr = MG_TCPIP_PHY_ADDR, \
2932-
};
2933-
2934-
#define MG_TCPIP_DRIVER_CODE &mg_tcpip_driver_imxrt
2935-
#define MG_TCPIP_DRIVER_NAME "imxrt"
2936-
29372917
#endif
29382918

29392919

@@ -2968,35 +2948,6 @@ struct mg_tcpip_driver_ra_data {
29682948
uint8_t phy_addr; // PHY address
29692949
};
29702950

2971-
#undef MG_ENABLE_TCPIP_DRIVER_INIT
2972-
#define MG_ENABLE_TCPIP_DRIVER_INIT 0 // TODO(): needs SystemCoreClock
2973-
#if 0
2974-
#ifndef MG_MAC_ADDRESS
2975-
#define MG_MAC_ADDRESS MG_MAC_ADDRESS_RANDOM
2976-
#endif
2977-
2978-
#ifndef MG_TCPIP_PHY_ADDR
2979-
#define MG_TCPIP_PHY_ADDR 1
2980-
#endif
2981-
2982-
#ifndef MG_DRIVER_RA_CLOCK
2983-
#define MG_DRIVER_RA_CLOCK
2984-
#endif
2985-
2986-
#ifndef MG_DRIVER_RA_IRQNO
2987-
#define MG_DRIVER_RA_IRQNO 0
2988-
#endif
2989-
2990-
#define MG_TCPIP_DRIVER_DATA \
2991-
static struct mg_tcpip_driver_ra_data driver_data = { \
2992-
.clock = MG_DRIVER_RA_CLOCK, \
2993-
.irqno = MG_DRIVER_RA_CLOCK, \
2994-
.phy_addr = MG_TCPIP_PHY_ADDR, \
2995-
};
2996-
2997-
#define MG_TCPIP_DRIVER_CODE &mg_tcpip_driver_ra
2998-
#define MG_TCPIP_DRIVER_NAME "ra"
2999-
#endif
30002951
#endif
30012952

30022953

@@ -3006,22 +2957,10 @@ struct mg_tcpip_driver_same54_data {
30062957
int mdc_cr;
30072958
};
30082959

3009-
#ifndef MG_MAC_ADDRESS
3010-
#define MG_MAC_ADDRESS MG_MAC_ADDRESS_RANDOM
3011-
#endif
3012-
30132960
#ifndef MG_DRIVER_MDC_CR
30142961
#define MG_DRIVER_MDC_CR 5
30152962
#endif
30162963

3017-
#define MG_TCPIP_DRIVER_DATA \
3018-
static struct mg_tcpip_driver_same54_data driver_data = { \
3019-
.mdc_cr = MG_DRIVER_MDC_CR, \
3020-
};
3021-
3022-
#define MG_TCPIP_DRIVER_CODE &mg_tcpip_driver_same54
3023-
#define MG_TCPIP_DRIVER_NAME "same54"
3024-
30252964
#endif
30262965

30272966

@@ -3045,10 +2984,6 @@ struct mg_tcpip_driver_stm32f_data {
30452984
uint8_t phy_addr; // PHY address
30462985
};
30472986

3048-
#ifndef MG_MAC_ADDRESS
3049-
#define MG_MAC_ADDRESS MG_MAC_ADDRESS_RANDOM
3050-
#endif
3051-
30522987
#ifndef MG_TCPIP_PHY_ADDR
30532988
#define MG_TCPIP_PHY_ADDR 0
30542989
#endif
@@ -3057,14 +2992,21 @@ struct mg_tcpip_driver_stm32f_data {
30572992
#define MG_DRIVER_MDC_CR 4
30582993
#endif
30592994

3060-
#define MG_TCPIP_DRIVER_DATA \
3061-
static struct mg_tcpip_driver_stm32f_data driver_data = { \
3062-
.mdc_cr = MG_DRIVER_MDC_CR, \
3063-
.phy_addr = MG_TCPIP_PHY_ADDR, \
3064-
};
3065-
3066-
#define MG_TCPIP_DRIVER_CODE &mg_tcpip_driver_stm32f
3067-
#define MG_TCPIP_DRIVER_NAME "stm32f"
2995+
#define MG_TCPIP_DRIVER_INIT(mgr) \
2996+
do { \
2997+
static struct mg_tcpip_driver_stm32f_data driver_data_; \
2998+
static struct mg_tcpip_if mif_; \
2999+
driver_data_.mdc_cr = MG_DRIVER_MDC_CR; \
3000+
driver_data_.phy_addr = MG_TCPIP_PHY_ADDR; \
3001+
mif_.ip = MG_TCPIP_IP; \
3002+
mif_.mask = MG_TCPIP_MASK; \
3003+
mif_.gw = MG_TCPIP_GW; \
3004+
mif_.driver = &mg_tcpip_driver_stm32f; \
3005+
mif_.driver_data = &driver_data_; \
3006+
MG_SET_MAC_ADDRESS(mif_.mac); \
3007+
mg_tcpip_init(mgr, &mif_); \
3008+
MG_INFO(("Driver: stm32f, MAC: %M", mg_print_mac, mif_.mac)); \
3009+
} while (0)
30683010

30693011
#endif
30703012

@@ -3081,19 +3023,15 @@ struct mg_tcpip_driver_stm32h_data {
30813023
// 100-150 MHz HCLK/62 1
30823024
// 20-35 MHz HCLK/16 2
30833025
// 35-60 MHz HCLK/26 3
3084-
// 150-250 MHz HCLK/102 4 <-- value for Nucleo-H* on max speed
3085-
// driven by HSI 250-300 MHz HCLK/124 5 <-- value for Nucleo-H* on
3086-
// max speed driven by CSI 110, 111 Reserved
3026+
// 150-250 MHz HCLK/102 4 <-- value for max speed HSI
3027+
// 250-300 MHz HCLK/124 5 <-- value for Nucleo-H* on CSI
3028+
// 110, 111 Reserved
30873029
int mdc_cr; // Valid values: -1, 0, 1, 2, 3, 4, 5
30883030

30893031
uint8_t phy_addr; // PHY address
30903032
uint8_t phy_conf; // PHY config
30913033
};
30923034

3093-
#ifndef MG_MAC_ADDRESS
3094-
#define MG_MAC_ADDRESS MG_MAC_ADDRESS_RANDOM
3095-
#endif
3096-
30973035
#ifndef MG_TCPIP_PHY_ADDR
30983036
#define MG_TCPIP_PHY_ADDR 0
30993037
#endif
@@ -3102,14 +3040,21 @@ struct mg_tcpip_driver_stm32h_data {
31023040
#define MG_DRIVER_MDC_CR 4
31033041
#endif
31043042

3105-
#define MG_TCPIP_DRIVER_DATA \
3106-
static struct mg_tcpip_driver_stm32h_data driver_data = { \
3107-
.mdc_cr = MG_DRIVER_MDC_CR, \
3108-
.phy_addr = MG_TCPIP_PHY_ADDR, \
3109-
};
3110-
3111-
#define MG_TCPIP_DRIVER_CODE &mg_tcpip_driver_stm32h
3112-
#define MG_TCPIP_DRIVER_NAME "stm32h"
3043+
#define MG_TCPIP_DRIVER_INIT(mgr) \
3044+
do { \
3045+
static struct mg_tcpip_driver_stm32h_data driver_data_; \
3046+
static struct mg_tcpip_if mif_; \
3047+
driver_data_.mdc_cr = MG_DRIVER_MDC_CR; \
3048+
driver_data_.phy_addr = MG_TCPIP_PHY_ADDR; \
3049+
mif_.ip = MG_TCPIP_IP; \
3050+
mif_.mask = MG_TCPIP_MASK; \
3051+
mif_.gw = MG_TCPIP_GW; \
3052+
mif_.driver = &mg_tcpip_driver_stm32h; \
3053+
mif_.driver_data = &driver_data_; \
3054+
MG_SET_MAC_ADDRESS(mif_.mac); \
3055+
mg_tcpip_init(mgr, &mif_); \
3056+
MG_INFO(("Driver: stm32h, MAC: %M", mg_print_mac, mif_.mac)); \
3057+
} while (0)
31133058

31143059
#endif
31153060

@@ -3129,31 +3074,10 @@ struct mg_tcpip_driver_tm4c_data {
31293074
int mdc_cr; // Valid values: -1, 0, 1, 2, 3
31303075
};
31313076

3132-
#ifndef MG_MAC_ADDRESS
3133-
#define MG_MAC_ADDRESS MG_MAC_ADDRESS_RANDOM
3134-
#endif
3135-
31363077
#ifndef MG_DRIVER_MDC_CR
31373078
#define MG_DRIVER_MDC_CR 1
31383079
#endif
31393080

3140-
#define MG_TCPIP_DRIVER_DATA \
3141-
static struct mg_tcpip_driver_tm4c_data driver_data = { \
3142-
.mdc_cr = MG_DRIVER_MDC_CR, \
3143-
};
3144-
3145-
#define MG_TCPIP_DRIVER_CODE &mg_tcpip_driver_tm4c
3146-
#define MG_TCPIP_DRIVER_NAME "tm4c"
3147-
3148-
3149-
#endif
3150-
3151-
3152-
#if MG_ENABLE_TCPIP && defined(MG_ENABLE_DRIVER_W5500) && MG_ENABLE_DRIVER_W5500
3153-
3154-
#undef MG_ENABLE_TCPIP_DRIVER_INIT
3155-
#define MG_ENABLE_TCPIP_DRIVER_INIT 0
3156-
31573081
#endif
31583082

31593083
#ifdef __cplusplus

0 commit comments

Comments
 (0)