2023-02-07 21:16:42 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "arch.h"
|
|
|
|
#include "net.h"
|
2023-02-19 09:48:11 +00:00
|
|
|
#include "queue.h"
|
2023-02-07 21:16:42 +00:00
|
|
|
|
2023-08-18 19:47:39 +01:00
|
|
|
#if defined(MG_ENABLE_TCPIP) && MG_ENABLE_TCPIP
|
2023-07-20 14:36:10 +01:00
|
|
|
struct mg_tcpip_if; // Mongoose TCP/IP network interface
|
2023-02-07 21:16:42 +00:00
|
|
|
|
|
|
|
struct mg_tcpip_driver {
|
|
|
|
bool (*init)(struct mg_tcpip_if *); // Init driver
|
|
|
|
size_t (*tx)(const void *, size_t, struct mg_tcpip_if *); // Transmit frame
|
|
|
|
size_t (*rx)(void *buf, size_t len, struct mg_tcpip_if *); // Receive frame
|
|
|
|
bool (*up)(struct mg_tcpip_if *); // Up/down status
|
|
|
|
};
|
|
|
|
|
|
|
|
// Network interface
|
|
|
|
struct mg_tcpip_if {
|
2023-03-09 11:25:02 -03:00
|
|
|
uint8_t mac[6]; // MAC address. Must be set to a valid MAC
|
|
|
|
uint32_t ip, mask, gw; // IP address, mask, default gateway
|
|
|
|
struct mg_str tx; // Output (TX) buffer
|
|
|
|
bool enable_dhcp_client; // Enable DCHP client
|
|
|
|
bool enable_dhcp_server; // Enable DCHP server
|
2023-12-08 17:21:06 -03:00
|
|
|
bool enable_get_gateway; // DCHP server sets client as gateway
|
2023-07-20 14:36:10 +01:00
|
|
|
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
|
2023-02-07 21:16:42 +00:00
|
|
|
struct mg_tcpip_driver *driver; // Low level driver
|
|
|
|
void *driver_data; // Driver-specific data
|
|
|
|
struct mg_mgr *mgr; // Mongoose event manager
|
2023-02-19 09:48:11 +00:00
|
|
|
struct mg_queue recv_queue; // Receive queue
|
2023-09-07 02:14:09 -04:00
|
|
|
uint16_t mtu; // Interface MTU
|
|
|
|
#define MG_TCPIP_MTU_DEFAULT 1500
|
2023-02-07 21:16:42 +00:00
|
|
|
|
|
|
|
// Internal state, user can use it but should not change it
|
2023-03-09 11:25:02 -03:00
|
|
|
uint8_t gwmac[6]; // Router's MAC
|
|
|
|
uint64_t now; // Current time
|
|
|
|
uint64_t timer_1000ms; // 1000 ms timer: for DHCP and link state
|
2023-06-22 18:55:42 -03:00
|
|
|
uint64_t lease_expire; // Lease expiration time, in ms
|
2023-03-09 11:25:02 -03:00
|
|
|
uint16_t eport; // Next ephemeral port
|
|
|
|
volatile uint32_t ndrop; // Number of received, but dropped frames
|
|
|
|
volatile uint32_t nrecv; // Number of received frames
|
|
|
|
volatile uint32_t nsent; // Number of transmitted frames
|
|
|
|
volatile uint32_t nerr; // Number of driver errors
|
|
|
|
uint8_t state; // Current state
|
|
|
|
#define MG_TCPIP_STATE_DOWN 0 // Interface is down
|
|
|
|
#define MG_TCPIP_STATE_UP 1 // Interface is up
|
2023-06-22 18:55:42 -03:00
|
|
|
#define MG_TCPIP_STATE_REQ 2 // Interface is up and has requested an IP
|
|
|
|
#define MG_TCPIP_STATE_READY 3 // Interface is up and has an IP assigned
|
2023-02-07 21:16:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void mg_tcpip_init(struct mg_mgr *, struct mg_tcpip_if *);
|
|
|
|
void mg_tcpip_free(struct mg_tcpip_if *);
|
|
|
|
void mg_tcpip_qwrite(void *buf, size_t len, struct mg_tcpip_if *ifp);
|
|
|
|
|
2023-12-15 18:38:02 -03:00
|
|
|
extern struct mg_tcpip_driver mg_tcpip_driver_stm32f;
|
2023-02-07 21:16:42 +00:00
|
|
|
extern struct mg_tcpip_driver mg_tcpip_driver_w5500;
|
|
|
|
extern struct mg_tcpip_driver mg_tcpip_driver_tm4c;
|
|
|
|
extern struct mg_tcpip_driver mg_tcpip_driver_stm32h;
|
2023-11-15 17:13:59 -03:00
|
|
|
extern struct mg_tcpip_driver mg_tcpip_driver_imxrt;
|
2023-08-10 18:59:23 -03:00
|
|
|
extern struct mg_tcpip_driver mg_tcpip_driver_same54;
|
2023-10-12 19:52:18 -03:00
|
|
|
extern struct mg_tcpip_driver mg_tcpip_driver_cmsis;
|
2024-02-01 10:15:32 -03:00
|
|
|
extern struct mg_tcpip_driver mg_tcpip_driver_ra;
|
2024-03-22 04:52:07 -04:00
|
|
|
extern struct mg_tcpip_driver mg_tcpip_driver_xmc;
|
2023-02-07 21:16:42 +00:00
|
|
|
|
|
|
|
// Drivers that require SPI, can use this SPI abstraction
|
|
|
|
struct mg_tcpip_spi {
|
|
|
|
void *spi; // Opaque SPI bus descriptor
|
|
|
|
void (*begin)(void *); // SPI begin: slave select low
|
|
|
|
void (*end)(void *); // SPI end: slave select high
|
|
|
|
uint8_t (*txn)(void *, uint8_t); // SPI transaction: write 1 byte, read reply
|
|
|
|
};
|
|
|
|
#endif
|