/**
 * @file ethernetif.h
 * @brief Ethernet interface header for CH390 + LwIP + FreeRTOS
 */

#ifndef __ETHERNETIF_H__
#define __ETHERNETIF_H__

#include <stdint.h>

#include "lwip/netif.h"
#include "lwip/err.h"
#include "lwip/ip4_addr.h"

#ifdef __cplusplus
extern "C" {
#endif

/* Ethernet interface state structure */
struct ethernetif {
    uint16_t rx_len;
    uint8_t  rx_status;
};

/* Global network interface */
extern struct netif ch390_netif;

/**
 * @brief Initialize the ethernet interface
 * @param netif Network interface structure
 * @return ERR_OK on success
 */
err_t ethernetif_init(struct netif *netif);

/**
 * @brief Process received ethernet packets
 * @param netif Network interface structure
 * @note Call this from the LwIP task when packets are available
 */
void ethernetif_input(struct netif *netif);

/**
 * @brief Initialize LwIP network interface with static IP
 * @param ipaddr IP address
 * @param netmask Network mask
 * @param gw Gateway address
 */
void lwip_netif_init(const ip4_addr_t *ipaddr, const ip4_addr_t *netmask, const ip4_addr_t *gw);

/**
 * @brief Initialize CH390 hardware without lwIP netif bring-up
 */
void ethernetif_diag_ch390_init(void);

/**
 * @brief Poll CH390 status and update diagnostic globals without lwIP RX handoff
 */
void ethernetif_diag_poll_status(void);

/**
 * @brief Check and handle CH390 interrupt status
 * @note Call this from the LwIP task periodically or on interrupt
 */
void ethernetif_check_link(void);

/**
 * @brief Query whether physical Ethernet link is currently up
 * @return 1 if link is up, 0 otherwise
 */
uint8_t ethernetif_link_is_up(void);

/**
 * @brief Process all pending RX packets
 * @note Call this from the LwIP task when RX interrupt occurs
 */
void ethernetif_poll(void);

#ifdef __cplusplus
}
#endif

#endif /* __ETHERNETIF_H__ */
