Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
scaprile committed Mar 11, 2024
1 parent 39f3a38 commit 62f8199
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 84 deletions.
31 changes: 27 additions & 4 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,16 +4874,14 @@ 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;
mgr->dns4.url = "udp://8.8.8.8:53";
mgr->dns6.url = "udp://[2001:4860:4860::8888]:53";
mg_tls_ctx_init(mgr);

#if (MG_ENABLE_DRIVER_INIT == 1) && defined(MG_TCPIP_DRIVER_INIT)
MG_TCPIP_DRIVER_INIT(mgr);
#endif
}

#ifdef MG_ENABLE_LINES
Expand Down Expand Up @@ -6011,6 +6014,26 @@ bool mg_send(struct mg_connection *c, const void *buf, size_t len) {
}
return res;
}

#if MG_ENABLE_TCPIP_DRIVER_INIT
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 = {
.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;
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
53 changes: 16 additions & 37 deletions mongoose.h
Original file line number Diff line number Diff line change
Expand Up @@ -833,35 +833,23 @@ struct timeval {
#define MG_ENABLE_PROFILE 0
#endif

#ifndef MG_ENABLE_DRIVER_INIT
#define MG_ENABLE_DRIVER_INIT 1
#endif
#ifndef MG_ENABLE_TCPIP_DRIVER_INIT // mg_mgr_init() will also initialize
#define MG_ENABLE_TCPIP_DRIVER_INIT 0 // enabled built-in driver for
#endif // Mongoose built-in network stack

#ifndef MG_TCPIP_IP
#define MG_TCPIP_IP 0
#ifndef MG_TCPIP_IP // e.g. mg_htonl(MG_U32(192, 168, 0, 223))
#define MG_TCPIP_IP mg_htonl(MG_U32(0, 0, 0, 0)) // or leave as 0 for DHCP
#endif

#ifndef MG_TCPIP_MASK
#define MG_TCPIP_MASK 0
#define MG_TCPIP_MASK mg_htonl(MG_U32(255, 255, 255, 0))
#endif

#ifndef MG_TCPIP_GW
#define MG_TCPIP_GW 0
#define MG_TCPIP_GW mg_htonl(MG_U32(0, 0, 0, 1))
#endif

#define MG_MAC_ADDRESS_RANDOM \
{ 0, 0, 0, 0, 0, 0 }

#define MG_STM32_UID_TO_MAC_ADDRESS(BASE) \
{ \
2, ((uint8_t *) (BASE))[0] ^ ((uint8_t *) (BASE))[1], \
((uint8_t *) (BASE))[2] ^ ((uint8_t *) (BASE))[3], \
((uint8_t *) (BASE))[4] ^ ((uint8_t *) (BASE))[5], \
((uint8_t *) (BASE))[6] ^ ((uint8_t *) (BASE))[7] ^ \
((uint8_t *) (BASE))[8], \
((uint8_t *) (BASE))[9] ^ ((uint8_t *) (BASE))[10] ^ \
((uint8_t *) (BASE))[11] \
}
#define MG_MAC_ADDRESS_RANDOM { 0, 0, 0, 0, 0, 0 }



Expand Down Expand Up @@ -2951,23 +2939,14 @@ struct mg_tcpip_driver_stm32f_data {
#define MG_TCPIP_MDC_CR 4
#endif

#define MG_TCPIP_DRIVER_INIT(mgr) \
do { \
static struct mg_tcpip_driver_stm32f_data driver_data = { \
.mdc_cr = MG_TCPIP_MDC_CR, \
.phy_addr = MG_TCPIP_PHY_ADDR, \
}; \
static struct mg_tcpip_if mif = { \
.mac = MG_MAC_ADDRESS, \
.ip = MG_TCPIP_IP, \
.mask = MG_TCPIP_MASK, \
.gw = MG_TCPIP_GW, \
.driver = &mg_tcpip_driver_stm32f, \
.driver_data = &driver_data, \
}; \
mg_tcpip_init((mgr), &mif); \
MG_INFO(("Driver: stm32fxx, 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_TCPIP_MDC_CR, \
.phy_addr = MG_TCPIP_PHY_ADDR, \
};

#define MG_TCPIP_DRIVER_CODE &mg_tcpip_driver_stm32f
#define MG_TCPIP_DRIVER_NAME "stm32fxx"


struct mg_tcpip_driver_stm32h_data {
Expand Down
32 changes: 10 additions & 22 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,32 +158,20 @@
#define MG_ENABLE_PROFILE 0
#endif

#ifndef MG_ENABLE_DRIVER_INIT
#define MG_ENABLE_DRIVER_INIT 1
#endif
#ifndef MG_ENABLE_TCPIP_DRIVER_INIT // mg_mgr_init() will also initialize
#define MG_ENABLE_TCPIP_DRIVER_INIT 0 // enabled built-in driver for
#endif // Mongoose built-in network stack

#ifndef MG_TCPIP_IP
#define MG_TCPIP_IP 0
#ifndef MG_TCPIP_IP // e.g. mg_htonl(MG_U32(192, 168, 0, 223))
#define MG_TCPIP_IP mg_htonl(MG_U32(0, 0, 0, 0)) // or leave as 0 for DHCP
#endif

#ifndef MG_TCPIP_MASK
#define MG_TCPIP_MASK 0
#define MG_TCPIP_MASK mg_htonl(MG_U32(255, 255, 255, 0))
#endif

#ifndef MG_TCPIP_GW
#define MG_TCPIP_GW 0
#endif

#define MG_MAC_ADDRESS_RANDOM \
{ 0, 0, 0, 0, 0, 0 }

#define MG_STM32_UID_TO_MAC_ADDRESS(BASE) \
{ \
2, ((uint8_t *) (BASE))[0] ^ ((uint8_t *) (BASE))[1], \
((uint8_t *) (BASE))[2] ^ ((uint8_t *) (BASE))[3], \
((uint8_t *) (BASE))[4] ^ ((uint8_t *) (BASE))[5], \
((uint8_t *) (BASE))[6] ^ ((uint8_t *) (BASE))[7] ^ \
((uint8_t *) (BASE))[8], \
((uint8_t *) (BASE))[9] ^ ((uint8_t *) (BASE))[10] ^ \
((uint8_t *) (BASE))[11] \
}
#define MG_TCPIP_GW mg_htonl(MG_U32(0, 0, 0, 1))
#endif

#define MG_MAC_ADDRESS_RANDOM { 0, 0, 0, 0, 0, 0 }
25 changes: 8 additions & 17 deletions src/drivers/stm32f.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,11 @@ struct mg_tcpip_driver_stm32f_data {
#define MG_TCPIP_MDC_CR 4
#endif

#define MG_TCPIP_DRIVER_INIT(mgr) \
do { \
static struct mg_tcpip_driver_stm32f_data driver_data = { \
.mdc_cr = MG_TCPIP_MDC_CR, \
.phy_addr = MG_TCPIP_PHY_ADDR, \
}; \
static struct mg_tcpip_if mif = { \
.mac = MG_MAC_ADDRESS, \
.ip = MG_TCPIP_IP, \
.mask = MG_TCPIP_MASK, \
.gw = MG_TCPIP_GW, \
.driver = &mg_tcpip_driver_stm32f, \
.driver_data = &driver_data, \
}; \
mg_tcpip_init((mgr), &mif); \
MG_INFO(("Driver: stm32fxx, 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_TCPIP_MDC_CR, \
.phy_addr = MG_TCPIP_PHY_ADDR, \
};

#define MG_TCPIP_DRIVER_CODE &mg_tcpip_driver_stm32f
#define MG_TCPIP_DRIVER_NAME "stm32fxx"
11 changes: 7 additions & 4 deletions src/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,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 @@ -270,14 +275,12 @@ 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;
mgr->dns4.url = "udp://8.8.8.8:53";
mgr->dns6.url = "udp://[2001:4860:4860::8888]:53";
mg_tls_ctx_init(mgr);

#if (MG_ENABLE_DRIVER_INIT == 1) && defined(MG_TCPIP_DRIVER_INIT)
MG_TCPIP_DRIVER_INIT(mgr);
#endif
}
20 changes: 20 additions & 0 deletions src/net_builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -1125,4 +1125,24 @@ bool mg_send(struct mg_connection *c, const void *buf, size_t len) {
}
return res;
}

#if MG_ENABLE_TCPIP_DRIVER_INIT
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 = {
.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;
mg_tcpip_init(mgr, &mif);
MG_INFO(("Driver: " MG_TCPIP_DRIVER_NAME ", MAC: %M", mg_print_mac, mif.mac));
}
#endif
#endif // MG_ENABLE_TCPIP

0 comments on commit 62f8199

Please sign in to comment.