MIP_STATE_* -> MG_TCPIP_STATE_*

This commit is contained in:
Sergio R. Caprile 2023-03-09 11:25:02 -03:00
parent 3f760e1875
commit 2ece3a8b4c
16 changed files with 75 additions and 75 deletions

View File

@ -83,7 +83,7 @@ int main(void) {
mg_timer_add(&mgr, BLINK_PERIOD_MS, MG_TIMER_REPEAT, timer_fn, &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)); MG_INFO(("MAC: %M. Waiting for IP...", mg_print_mac, mif.mac));
while (mif.state != MIP_STATE_READY) { while (mif.state != MG_TCPIP_STATE_READY) {
mg_mgr_poll(&mgr, 0); mg_mgr_poll(&mgr, 0);
} }

View File

@ -73,7 +73,7 @@ int main(void) {
mg_timer_add(&mgr, BLINK_PERIOD_MS, MG_TIMER_REPEAT, timer_fn, &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)); MG_INFO(("MAC: %M. Waiting for IP...", mg_print_mac, mif.mac));
while (mif.state != MIP_STATE_READY) { while (mif.state != MG_TCPIP_STATE_READY) {
mg_mgr_poll(&mgr, 0); mg_mgr_poll(&mgr, 0);
} }

View File

@ -60,7 +60,7 @@ static void server(void *args) {
mg_timer_add(&mgr, BLINK_PERIOD_MS, MG_TIMER_REPEAT, timer_fn, &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)); MG_INFO(("MAC: %M. Waiting for IP...", mg_print_mac, mif.mac));
while (mif.state != MIP_STATE_READY) { while (mif.state != MG_TCPIP_STATE_READY) {
mg_mgr_poll(&mgr, 0); mg_mgr_poll(&mgr, 0);
} }

View File

@ -73,7 +73,7 @@ int main(void) {
mg_timer_add(&mgr, BLINK_PERIOD_MS, MG_TIMER_REPEAT, timer_fn, &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)); MG_INFO(("MAC: %M. Waiting for IP...", mg_print_mac, mif.mac));
while (mif.state != MIP_STATE_READY) { while (mif.state != MG_TCPIP_STATE_READY) {
mg_mgr_poll(&mgr, 0); mg_mgr_poll(&mgr, 0);
} }

View File

@ -60,7 +60,7 @@ static void server(void *args) {
mg_timer_add(&mgr, BLINK_PERIOD_MS, MG_TIMER_REPEAT, timer_fn, &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)); MG_INFO(("MAC: %M. Waiting for IP...", mg_print_mac, mif.mac));
while (mif.state != MIP_STATE_READY) { while (mif.state != MG_TCPIP_STATE_READY) {
mg_mgr_poll(&mgr, 0); mg_mgr_poll(&mgr, 0);
} }

View File

@ -75,7 +75,7 @@ int main(void) {
mg_timer_add(&mgr, BLINK_PERIOD_MS, MG_TIMER_REPEAT, timer_fn, &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)); MG_INFO(("MAC: %M. Waiting for IP...", mg_print_mac, mif.mac));
while (mif.state != MIP_STATE_READY) { while (mif.state != MG_TCPIP_STATE_READY) {
mg_mgr_poll(&mgr, 0); mg_mgr_poll(&mgr, 0);
} }

View File

@ -84,7 +84,7 @@ int main(void) {
mg_timer_add(&mgr, BLINK_PERIOD_MS, MG_TIMER_REPEAT, timer_fn, &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)); MG_INFO(("MAC: %M. Waiting for IP...", mg_print_mac, mif.mac));
while (mif.state != MIP_STATE_READY) { while (mif.state != MG_TCPIP_STATE_READY) {
mg_mgr_poll(&mgr, 0); mg_mgr_poll(&mgr, 0);
} }

View File

@ -69,7 +69,7 @@ static void server(void *args) {
mg_timer_add(&mgr, BLINK_PERIOD_MS, MG_TIMER_REPEAT, timer_fn, &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)); MG_INFO(("MAC: %M. Waiting for IP...", mg_print_mac, mif.mac));
while (mif.state != MIP_STATE_READY) { while (mif.state != MG_TCPIP_STATE_READY) {
mg_mgr_poll(&mgr, 0); mg_mgr_poll(&mgr, 0);
} }

View File

@ -6511,7 +6511,7 @@ static size_t mg_tcpip_driver_stm32_tx(const void *buf, size_t len,
static bool mg_tcpip_driver_stm32_up(struct mg_tcpip_if *ifp) { static bool mg_tcpip_driver_stm32_up(struct mg_tcpip_if *ifp) {
uint32_t bsr = eth_read_phy(PHY_ADDR, PHY_BSR); uint32_t bsr = eth_read_phy(PHY_ADDR, PHY_BSR);
bool up = bsr & BIT(2) ? 1 : 0; bool up = bsr & BIT(2) ? 1 : 0;
if ((ifp->state == MIP_STATE_DOWN) && up) { // link state just went up if ((ifp->state == MG_TCPIP_STATE_DOWN) && up) { // link state just went up
uint32_t scsr = eth_read_phy(PHY_ADDR, PHY_CSCR); uint32_t scsr = eth_read_phy(PHY_ADDR, PHY_CSCR);
uint32_t maccr = ETH->MACCR | BIT(14) | BIT(11); // 100M, Full-duplex uint32_t maccr = ETH->MACCR | BIT(14) | BIT(11); // 100M, Full-duplex
if ((scsr & BIT(3)) == 0) maccr &= ~BIT(14); // 10M if ((scsr & BIT(3)) == 0) maccr &= ~BIT(14); // 10M
@ -6787,7 +6787,7 @@ static size_t mg_tcpip_driver_stm32h_tx(const void *buf, size_t len,
static bool mg_tcpip_driver_stm32h_up(struct mg_tcpip_if *ifp) { static bool mg_tcpip_driver_stm32h_up(struct mg_tcpip_if *ifp) {
uint32_t bsr = eth_read_phy(PHY_ADDR, PHY_BSR); uint32_t bsr = eth_read_phy(PHY_ADDR, PHY_BSR);
bool up = bsr & BIT(2) ? 1 : 0; bool up = bsr & BIT(2) ? 1 : 0;
if ((ifp->state == MIP_STATE_DOWN) && up) { // link state just went up if ((ifp->state == MG_TCPIP_STATE_DOWN) && up) { // link state just went up
uint32_t scsr = eth_read_phy(PHY_ADDR, PHY_CSCR); uint32_t scsr = eth_read_phy(PHY_ADDR, PHY_CSCR);
uint32_t maccr = ETH->MACCR | BIT(14) | BIT(13); // 100M, Full-duplex uint32_t maccr = ETH->MACCR | BIT(14) | BIT(13); // 100M, Full-duplex
if ((scsr & BIT(3)) == 0) maccr &= ~BIT(14); // 10M if ((scsr & BIT(3)) == 0) maccr &= ~BIT(14); // 10M
@ -7042,7 +7042,7 @@ static size_t mg_tcpip_driver_tm4c_tx(const void *buf, size_t len,
static bool mg_tcpip_driver_tm4c_up(struct mg_tcpip_if *ifp) { static bool mg_tcpip_driver_tm4c_up(struct mg_tcpip_if *ifp) {
uint32_t bmsr = emac_read_phy(EPHY_ADDR, EPHYBMSR); uint32_t bmsr = emac_read_phy(EPHY_ADDR, EPHYBMSR);
bool up = (bmsr & BIT(2)) ? 1 : 0; bool up = (bmsr & BIT(2)) ? 1 : 0;
if ((ifp->state == MIP_STATE_DOWN) && up) { // link state just went up if ((ifp->state == MG_TCPIP_STATE_DOWN) && up) { // link state just went up
uint32_t sts = emac_read_phy(EPHY_ADDR, EPHYSTS); uint32_t sts = emac_read_phy(EPHY_ADDR, EPHYSTS);
uint32_t emaccfg = EMAC->EMACCFG | BIT(14) | BIT(11); // 100M, Full-duplex uint32_t emaccfg = EMAC->EMACCFG | BIT(14) | BIT(11); // 100M, Full-duplex
if (sts & BIT(1)) emaccfg &= ~BIT(14); // 10M if (sts & BIT(1)) emaccfg &= ~BIT(14); // 10M
@ -7352,7 +7352,7 @@ static void arp_ask(struct mg_tcpip_if *ifp, uint32_t ip) {
} }
static void onstatechange(struct mg_tcpip_if *ifp) { static void onstatechange(struct mg_tcpip_if *ifp) {
if (ifp->state == MIP_STATE_READY) { if (ifp->state == MG_TCPIP_STATE_READY) {
MG_INFO(("READY, IP: %M", mg_print_ip4, &ifp->ip)); MG_INFO(("READY, IP: %M", mg_print_ip4, &ifp->ip));
MG_INFO((" GW: %M", mg_print_ip4, &ifp->gw)); MG_INFO((" GW: %M", mg_print_ip4, &ifp->gw));
if (ifp->lease_expire > ifp->now) { if (ifp->lease_expire > ifp->now) {
@ -7360,10 +7360,10 @@ static void onstatechange(struct mg_tcpip_if *ifp) {
(" Lease: %lld sec", (ifp->lease_expire - ifp->now) / 1000)); (" Lease: %lld sec", (ifp->lease_expire - ifp->now) / 1000));
} }
arp_ask(ifp, ifp->gw); arp_ask(ifp, ifp->gw);
} else if (ifp->state == MIP_STATE_UP) { } else if (ifp->state == MG_TCPIP_STATE_UP) {
MG_ERROR(("Link up")); MG_ERROR(("Link up"));
srand((unsigned int) mg_millis()); srand((unsigned int) mg_millis());
} else if (ifp->state == MIP_STATE_DOWN) { } else if (ifp->state == MG_TCPIP_STATE_DOWN) {
MG_ERROR(("Link down")); MG_ERROR(("Link down"));
} }
} }
@ -7533,7 +7533,7 @@ static void rx_dhcp_client(struct mg_tcpip_if *ifp, struct pkt *pkt) {
if (ip && mask && gw && ifp->ip == 0) { if (ip && mask && gw && ifp->ip == 0) {
memcpy(ifp->gwmac, pkt->eth->src, sizeof(ifp->gwmac)); memcpy(ifp->gwmac, pkt->eth->src, sizeof(ifp->gwmac));
ifp->ip = ip, ifp->gw = gw, ifp->mask = mask; ifp->ip = ip, ifp->gw = gw, ifp->mask = mask;
ifp->state = MIP_STATE_READY; ifp->state = MG_TCPIP_STATE_READY;
onstatechange(ifp); onstatechange(ifp);
tx_dhcp_request(ifp, pkt->eth->src, ip, pkt->dhcp->siaddr); tx_dhcp_request(ifp, pkt->eth->src, ip, pkt->dhcp->siaddr);
uint64_t rand; uint64_t rand;
@ -7916,16 +7916,16 @@ static void mg_tcpip_poll(struct mg_tcpip_if *ifp, uint64_t uptime_ms) {
// Handle physical interface up/down status // Handle physical interface up/down status
if (expired_1000ms && ifp->driver->up) { if (expired_1000ms && ifp->driver->up) {
bool up = ifp->driver->up(ifp); bool up = ifp->driver->up(ifp);
bool current = ifp->state != MIP_STATE_DOWN; bool current = ifp->state != MG_TCPIP_STATE_DOWN;
if (up != current) { if (up != current) {
ifp->state = up == false ? MIP_STATE_DOWN ifp->state = up == false ? MG_TCPIP_STATE_DOWN
: ifp->enable_dhcp_client ? MIP_STATE_UP : ifp->enable_dhcp_client ? MG_TCPIP_STATE_UP
: MIP_STATE_READY; : MG_TCPIP_STATE_READY;
if (!up && ifp->enable_dhcp_client) ifp->ip = 0; if (!up && ifp->enable_dhcp_client) ifp->ip = 0;
onstatechange(ifp); onstatechange(ifp);
} }
} }
if (ifp->state == MIP_STATE_DOWN) return; if (ifp->state == MG_TCPIP_STATE_DOWN) return;
// If IP not configured, send DHCP // If IP not configured, send DHCP
if (ifp->ip == 0 && expired_1000ms) tx_dhcp_discover(ifp); if (ifp->ip == 0 && expired_1000ms) tx_dhcp_discover(ifp);
@ -8109,7 +8109,7 @@ void mg_mgr_poll(struct mg_mgr *mgr, int ms) {
bool mg_send(struct mg_connection *c, const void *buf, size_t len) { bool mg_send(struct mg_connection *c, const void *buf, size_t len) {
struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) c->mgr->priv; struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) c->mgr->priv;
bool res = false; bool res = false;
if (ifp->ip == 0 || ifp->state != MIP_STATE_READY) { if (ifp->ip == 0 || ifp->state != MG_TCPIP_STATE_READY) {
mg_error(c, "net down"); mg_error(c, "net down");
} else if (c->is_udp) { } else if (c->is_udp) {
struct connstate *s = (struct connstate *) (c + 1); struct connstate *s = (struct connstate *) (c + 1);

View File

@ -1514,32 +1514,32 @@ struct mg_tcpip_driver {
// Network interface // Network interface
struct mg_tcpip_if { struct mg_tcpip_if {
uint8_t mac[6]; // MAC address. Must be set to a valid MAC uint8_t mac[6]; // MAC address. Must be set to a valid MAC
uint32_t ip, mask, gw; // IP address, mask, default gateway uint32_t ip, mask, gw; // IP address, mask, default gateway
struct mg_str tx; // Output (TX) buffer struct mg_str tx; // Output (TX) buffer
bool enable_dhcp_client; // Enable DCHP client bool enable_dhcp_client; // Enable DCHP client
bool enable_dhcp_server; // Enable DCHP server bool enable_dhcp_server; // Enable DCHP server
bool enable_crc32_check; // Do a CRC check on rx frames and strip it bool enable_crc32_check; // Do a CRC check on rx frames and strip it
bool enable_mac_check; // Do a MAC check on rx frames bool enable_mac_check; // Do a MAC check on rx frames
struct mg_tcpip_driver *driver; // Low level driver struct mg_tcpip_driver *driver; // Low level driver
void *driver_data; // Driver-specific data void *driver_data; // Driver-specific data
struct mg_mgr *mgr; // Mongoose event manager struct mg_mgr *mgr; // Mongoose event manager
struct mg_queue recv_queue; // Receive queue struct mg_queue recv_queue; // Receive queue
// Internal state, user can use it but should not change it // Internal state, user can use it but should not change it
uint8_t gwmac[6]; // Router's MAC uint8_t gwmac[6]; // Router's MAC
uint64_t now; // Current time uint64_t now; // Current time
uint64_t timer_1000ms; // 1000 ms timer: for DHCP and link state uint64_t timer_1000ms; // 1000 ms timer: for DHCP and link state
uint64_t lease_expire; // Lease expiration time uint64_t lease_expire; // Lease expiration time
uint16_t eport; // Next ephemeral port uint16_t eport; // Next ephemeral port
volatile uint32_t ndrop; // Number of received, but dropped frames volatile uint32_t ndrop; // Number of received, but dropped frames
volatile uint32_t nrecv; // Number of received frames volatile uint32_t nrecv; // Number of received frames
volatile uint32_t nsent; // Number of transmitted frames volatile uint32_t nsent; // Number of transmitted frames
volatile uint32_t nerr; // Number of driver errors volatile uint32_t nerr; // Number of driver errors
uint8_t state; // Current state uint8_t state; // Current state
#define MIP_STATE_DOWN 0 // Interface is down #define MG_TCPIP_STATE_DOWN 0 // Interface is down
#define MIP_STATE_UP 1 // Interface is up #define MG_TCPIP_STATE_UP 1 // Interface is up
#define MIP_STATE_READY 2 // Interface is up and has IP #define MG_TCPIP_STATE_READY 2 // Interface is up and has IP
}; };
void mg_tcpip_init(struct mg_mgr *, struct mg_tcpip_if *); void mg_tcpip_init(struct mg_mgr *, struct mg_tcpip_if *);

View File

@ -178,7 +178,7 @@ static size_t mg_tcpip_driver_stm32_tx(const void *buf, size_t len,
static bool mg_tcpip_driver_stm32_up(struct mg_tcpip_if *ifp) { static bool mg_tcpip_driver_stm32_up(struct mg_tcpip_if *ifp) {
uint32_t bsr = eth_read_phy(PHY_ADDR, PHY_BSR); uint32_t bsr = eth_read_phy(PHY_ADDR, PHY_BSR);
bool up = bsr & BIT(2) ? 1 : 0; bool up = bsr & BIT(2) ? 1 : 0;
if ((ifp->state == MIP_STATE_DOWN) && up) { // link state just went up if ((ifp->state == MG_TCPIP_STATE_DOWN) && up) { // link state just went up
uint32_t scsr = eth_read_phy(PHY_ADDR, PHY_CSCR); uint32_t scsr = eth_read_phy(PHY_ADDR, PHY_CSCR);
uint32_t maccr = ETH->MACCR | BIT(14) | BIT(11); // 100M, Full-duplex uint32_t maccr = ETH->MACCR | BIT(14) | BIT(11); // 100M, Full-duplex
if ((scsr & BIT(3)) == 0) maccr &= ~BIT(14); // 10M if ((scsr & BIT(3)) == 0) maccr &= ~BIT(14); // 10M

View File

@ -233,7 +233,7 @@ static size_t mg_tcpip_driver_stm32h_tx(const void *buf, size_t len,
static bool mg_tcpip_driver_stm32h_up(struct mg_tcpip_if *ifp) { static bool mg_tcpip_driver_stm32h_up(struct mg_tcpip_if *ifp) {
uint32_t bsr = eth_read_phy(PHY_ADDR, PHY_BSR); uint32_t bsr = eth_read_phy(PHY_ADDR, PHY_BSR);
bool up = bsr & BIT(2) ? 1 : 0; bool up = bsr & BIT(2) ? 1 : 0;
if ((ifp->state == MIP_STATE_DOWN) && up) { // link state just went up if ((ifp->state == MG_TCPIP_STATE_DOWN) && up) { // link state just went up
uint32_t scsr = eth_read_phy(PHY_ADDR, PHY_CSCR); uint32_t scsr = eth_read_phy(PHY_ADDR, PHY_CSCR);
uint32_t maccr = ETH->MACCR | BIT(14) | BIT(13); // 100M, Full-duplex uint32_t maccr = ETH->MACCR | BIT(14) | BIT(13); // 100M, Full-duplex
if ((scsr & BIT(3)) == 0) maccr &= ~BIT(14); // 10M if ((scsr & BIT(3)) == 0) maccr &= ~BIT(14); // 10M

View File

@ -209,7 +209,7 @@ static size_t mg_tcpip_driver_tm4c_tx(const void *buf, size_t len,
static bool mg_tcpip_driver_tm4c_up(struct mg_tcpip_if *ifp) { static bool mg_tcpip_driver_tm4c_up(struct mg_tcpip_if *ifp) {
uint32_t bmsr = emac_read_phy(EPHY_ADDR, EPHYBMSR); uint32_t bmsr = emac_read_phy(EPHY_ADDR, EPHYBMSR);
bool up = (bmsr & BIT(2)) ? 1 : 0; bool up = (bmsr & BIT(2)) ? 1 : 0;
if ((ifp->state == MIP_STATE_DOWN) && up) { // link state just went up if ((ifp->state == MG_TCPIP_STATE_DOWN) && up) { // link state just went up
uint32_t sts = emac_read_phy(EPHY_ADDR, EPHYSTS); uint32_t sts = emac_read_phy(EPHY_ADDR, EPHYSTS);
uint32_t emaccfg = EMAC->EMACCFG | BIT(14) | BIT(11); // 100M, Full-duplex uint32_t emaccfg = EMAC->EMACCFG | BIT(14) | BIT(11); // 100M, Full-duplex
if (sts & BIT(1)) emaccfg &= ~BIT(14); // 10M if (sts & BIT(1)) emaccfg &= ~BIT(14); // 10M

View File

@ -173,7 +173,7 @@ static void arp_ask(struct mg_tcpip_if *ifp, uint32_t ip) {
} }
static void onstatechange(struct mg_tcpip_if *ifp) { static void onstatechange(struct mg_tcpip_if *ifp) {
if (ifp->state == MIP_STATE_READY) { if (ifp->state == MG_TCPIP_STATE_READY) {
MG_INFO(("READY, IP: %M", mg_print_ip4, &ifp->ip)); MG_INFO(("READY, IP: %M", mg_print_ip4, &ifp->ip));
MG_INFO((" GW: %M", mg_print_ip4, &ifp->gw)); MG_INFO((" GW: %M", mg_print_ip4, &ifp->gw));
if (ifp->lease_expire > ifp->now) { if (ifp->lease_expire > ifp->now) {
@ -181,10 +181,10 @@ static void onstatechange(struct mg_tcpip_if *ifp) {
(" Lease: %lld sec", (ifp->lease_expire - ifp->now) / 1000)); (" Lease: %lld sec", (ifp->lease_expire - ifp->now) / 1000));
} }
arp_ask(ifp, ifp->gw); arp_ask(ifp, ifp->gw);
} else if (ifp->state == MIP_STATE_UP) { } else if (ifp->state == MG_TCPIP_STATE_UP) {
MG_ERROR(("Link up")); MG_ERROR(("Link up"));
srand((unsigned int) mg_millis()); srand((unsigned int) mg_millis());
} else if (ifp->state == MIP_STATE_DOWN) { } else if (ifp->state == MG_TCPIP_STATE_DOWN) {
MG_ERROR(("Link down")); MG_ERROR(("Link down"));
} }
} }
@ -354,7 +354,7 @@ static void rx_dhcp_client(struct mg_tcpip_if *ifp, struct pkt *pkt) {
if (ip && mask && gw && ifp->ip == 0) { if (ip && mask && gw && ifp->ip == 0) {
memcpy(ifp->gwmac, pkt->eth->src, sizeof(ifp->gwmac)); memcpy(ifp->gwmac, pkt->eth->src, sizeof(ifp->gwmac));
ifp->ip = ip, ifp->gw = gw, ifp->mask = mask; ifp->ip = ip, ifp->gw = gw, ifp->mask = mask;
ifp->state = MIP_STATE_READY; ifp->state = MG_TCPIP_STATE_READY;
onstatechange(ifp); onstatechange(ifp);
tx_dhcp_request(ifp, pkt->eth->src, ip, pkt->dhcp->siaddr); tx_dhcp_request(ifp, pkt->eth->src, ip, pkt->dhcp->siaddr);
uint64_t rand; uint64_t rand;
@ -737,16 +737,16 @@ static void mg_tcpip_poll(struct mg_tcpip_if *ifp, uint64_t uptime_ms) {
// Handle physical interface up/down status // Handle physical interface up/down status
if (expired_1000ms && ifp->driver->up) { if (expired_1000ms && ifp->driver->up) {
bool up = ifp->driver->up(ifp); bool up = ifp->driver->up(ifp);
bool current = ifp->state != MIP_STATE_DOWN; bool current = ifp->state != MG_TCPIP_STATE_DOWN;
if (up != current) { if (up != current) {
ifp->state = up == false ? MIP_STATE_DOWN ifp->state = up == false ? MG_TCPIP_STATE_DOWN
: ifp->enable_dhcp_client ? MIP_STATE_UP : ifp->enable_dhcp_client ? MG_TCPIP_STATE_UP
: MIP_STATE_READY; : MG_TCPIP_STATE_READY;
if (!up && ifp->enable_dhcp_client) ifp->ip = 0; if (!up && ifp->enable_dhcp_client) ifp->ip = 0;
onstatechange(ifp); onstatechange(ifp);
} }
} }
if (ifp->state == MIP_STATE_DOWN) return; if (ifp->state == MG_TCPIP_STATE_DOWN) return;
// If IP not configured, send DHCP // If IP not configured, send DHCP
if (ifp->ip == 0 && expired_1000ms) tx_dhcp_discover(ifp); if (ifp->ip == 0 && expired_1000ms) tx_dhcp_discover(ifp);
@ -930,7 +930,7 @@ void mg_mgr_poll(struct mg_mgr *mgr, int ms) {
bool mg_send(struct mg_connection *c, const void *buf, size_t len) { bool mg_send(struct mg_connection *c, const void *buf, size_t len) {
struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) c->mgr->priv; struct mg_tcpip_if *ifp = (struct mg_tcpip_if *) c->mgr->priv;
bool res = false; bool res = false;
if (ifp->ip == 0 || ifp->state != MIP_STATE_READY) { if (ifp->ip == 0 || ifp->state != MG_TCPIP_STATE_READY) {
mg_error(c, "net down"); mg_error(c, "net down");
} else if (c->is_udp) { } else if (c->is_udp) {
struct connstate *s = (struct connstate *) (c + 1); struct connstate *s = (struct connstate *) (c + 1);

View File

@ -15,32 +15,32 @@ struct mg_tcpip_driver {
// Network interface // Network interface
struct mg_tcpip_if { struct mg_tcpip_if {
uint8_t mac[6]; // MAC address. Must be set to a valid MAC uint8_t mac[6]; // MAC address. Must be set to a valid MAC
uint32_t ip, mask, gw; // IP address, mask, default gateway uint32_t ip, mask, gw; // IP address, mask, default gateway
struct mg_str tx; // Output (TX) buffer struct mg_str tx; // Output (TX) buffer
bool enable_dhcp_client; // Enable DCHP client bool enable_dhcp_client; // Enable DCHP client
bool enable_dhcp_server; // Enable DCHP server bool enable_dhcp_server; // Enable DCHP server
bool enable_crc32_check; // Do a CRC check on rx frames and strip it bool enable_crc32_check; // Do a CRC check on rx frames and strip it
bool enable_mac_check; // Do a MAC check on rx frames bool enable_mac_check; // Do a MAC check on rx frames
struct mg_tcpip_driver *driver; // Low level driver struct mg_tcpip_driver *driver; // Low level driver
void *driver_data; // Driver-specific data void *driver_data; // Driver-specific data
struct mg_mgr *mgr; // Mongoose event manager struct mg_mgr *mgr; // Mongoose event manager
struct mg_queue recv_queue; // Receive queue struct mg_queue recv_queue; // Receive queue
// Internal state, user can use it but should not change it // Internal state, user can use it but should not change it
uint8_t gwmac[6]; // Router's MAC uint8_t gwmac[6]; // Router's MAC
uint64_t now; // Current time uint64_t now; // Current time
uint64_t timer_1000ms; // 1000 ms timer: for DHCP and link state uint64_t timer_1000ms; // 1000 ms timer: for DHCP and link state
uint64_t lease_expire; // Lease expiration time uint64_t lease_expire; // Lease expiration time
uint16_t eport; // Next ephemeral port uint16_t eport; // Next ephemeral port
volatile uint32_t ndrop; // Number of received, but dropped frames volatile uint32_t ndrop; // Number of received, but dropped frames
volatile uint32_t nrecv; // Number of received frames volatile uint32_t nrecv; // Number of received frames
volatile uint32_t nsent; // Number of transmitted frames volatile uint32_t nsent; // Number of transmitted frames
volatile uint32_t nerr; // Number of driver errors volatile uint32_t nerr; // Number of driver errors
uint8_t state; // Current state uint8_t state; // Current state
#define MIP_STATE_DOWN 0 // Interface is down #define MG_TCPIP_STATE_DOWN 0 // Interface is down
#define MIP_STATE_UP 1 // Interface is up #define MG_TCPIP_STATE_UP 1 // Interface is up
#define MIP_STATE_READY 2 // Interface is up and has IP #define MG_TCPIP_STATE_READY 2 // Interface is up and has IP
}; };
void mg_tcpip_init(struct mg_mgr *, struct mg_tcpip_if *); void mg_tcpip_init(struct mg_mgr *, struct mg_tcpip_if *);

View File

@ -22,7 +22,7 @@ static void test_statechange(void) {
struct mg_tcpip_if iface; struct mg_tcpip_if iface;
memset(&iface, 0, sizeof(iface)); memset(&iface, 0, sizeof(iface));
iface.ip = mg_htonl(0x01020304); iface.ip = mg_htonl(0x01020304);
iface.state = MIP_STATE_READY; iface.state = MG_TCPIP_STATE_READY;
iface.tx.ptr = tx, iface.tx.len = sizeof(tx); iface.tx.ptr = tx, iface.tx.len = sizeof(tx);
iface.driver = &mg_tcpip_driver_mock; iface.driver = &mg_tcpip_driver_mock;
onstatechange(&iface); onstatechange(&iface);