/**
 * @file lwipopts.h
 * @brief LwIP configuration for STM32F103RCT6 + FreeRTOS + CH390 Ethernet
 * 
 * Path A: NO_SYS=0, netconn API, multi-task TCP architecture.
 * Optimized for STM32F103RCT6 (48KB SRAM) with pin-to-pin backup STM32F103RDT6 (64KB SRAM).
 * 
 * Key design decisions:
 * - netconn API for thread-safe multi-connection TCP
 * - tcpip_thread handles netconn API and timers
 * - core locking lets the poll task process RX packets synchronously
 * - Conservative memory footprint: target ~16KB for lwIP
 */

#ifndef LWIP_LWIPOPTS_H
#define LWIP_LWIPOPTS_H

/*-----------------------------------------------------------------------------
 * Platform and OS Options
 *---------------------------------------------------------------------------*/

/* Use FreeRTOS - this enables the sequential API (netconn, sockets) */
#define NO_SYS                          0

/* Enable netconn API (primary), disable socket API to save RAM */
#define LWIP_SOCKET                     0
#define LWIP_NETCONN                    1
#define LWIP_NETIF_API                  1

/* Core locking: process netif RX synchronously instead of consuming TCPIP_MSG_INPKT slots. */
#define LWIP_TCPIP_CORE_LOCKING         1
#define LWIP_TCPIP_CORE_LOCKING_INPUT   1

/* Critical section protection */
#define SYS_LIGHTWEIGHT_PROT            1

/* Use FreeRTOS memory allocation */
#define MEM_LIBC_MALLOC                 0
#define MEMP_MEM_MALLOC                 0

/* Let lwIP provide the errno values used by sockets/netconn. */
#define LWIP_PROVIDE_ERRNO              1

/*-----------------------------------------------------------------------------
 * Memory Configuration (optimized for STM32F103RCT6 with 48KB SRAM)
 *---------------------------------------------------------------------------*/

/* Memory alignment (ARM Cortex-M3 = 4 byte alignment) */
#define MEM_ALIGNMENT                   4

/* Heap size for dynamic memory allocation.
 * With netconn: larger heap needed for netbuf allocation and connection management.
 * 8KB provides headroom for 4 concurrent TCP connections. */
#define MEM_SIZE                        (7 * 1024)

/* Number of pbufs in pool.
 * RX is processed synchronously under the core lock, so a small pool is sufficient. */
#define PBUF_POOL_SIZE                  8

/* Size of each pbuf in pool (must hold one Ethernet frame) */
#define PBUF_POOL_BUFSIZE               LWIP_MEM_ALIGN_SIZE(TCP_MSS + 40 + PBUF_LINK_ENCAPSULATION_HLEN + PBUF_LINK_HLEN)

/* Number of memp struct pbufs */
#define MEMP_NUM_PBUF                   8

/* Number of raw PCBs */
#define MEMP_NUM_RAW_PCB                2

/* Number of UDP PCBs */
#define MEMP_NUM_UDP_PCB                4

/* Number of simultaneously active TCP connections */
#define MEMP_NUM_TCP_PCB                4

/* Number of listening TCP connections */
#define MEMP_NUM_TCP_PCB_LISTEN         2

/* Number of simultaneously queued TCP segments
 * Increased for 4 concurrent connections */
#define MEMP_NUM_TCP_SEG                12

/* Number of simultaneously active timeouts */
#define MEMP_NUM_SYS_TIMEOUT            12

/* Number of netbufs (for netconn API, one per pending recv) */
#define MEMP_NUM_NETBUF                 8

/* Number of netconns: 2 listeners + 2 accepted + 2 clients + 2 margin = 8 */
#define MEMP_NUM_NETCONN                8

/* TCPIP message queue size (must be >= max simultaneous API calls) */
#define MEMP_NUM_TCPIP_MSG_API          8
#define MEMP_NUM_TCPIP_MSG_INPKT        8

/*-----------------------------------------------------------------------------
 * IP Configuration
 *---------------------------------------------------------------------------*/

#define LWIP_IPV4                       1
#define LWIP_IPV6                       0

/* No IP forwarding (single interface device) */
#define IP_FORWARD                      0

/* IP fragment reassembly */
#define IP_REASSEMBLY                   0
#define IP_FRAG                         0

/* IP options processing */
#define IP_OPTIONS_ALLOWED              1

/*-----------------------------------------------------------------------------
 * ICMP Configuration
 *---------------------------------------------------------------------------*/

#define LWIP_ICMP                       1
#define ICMP_TTL                        255

/*-----------------------------------------------------------------------------
 * ARP Configuration
 *---------------------------------------------------------------------------*/

#define LWIP_ARP                        1
#define ARP_TABLE_SIZE                  10
#define ARP_QUEUEING                    1
#define ETHARP_SUPPORT_STATIC_ENTRIES   1

/*-----------------------------------------------------------------------------
 * DHCP Configuration
 *---------------------------------------------------------------------------*/

#define LWIP_DHCP                       0  /* Static IP only */
#define DHCP_DOES_ARP_CHECK             0

/*-----------------------------------------------------------------------------
 * UDP Configuration
 *---------------------------------------------------------------------------*/

#define LWIP_UDP                        0  /* UDP not used in this project */

/*-----------------------------------------------------------------------------
 * TCP Configuration (optimized for transparent transmission)
 *---------------------------------------------------------------------------*/

#define LWIP_TCP                        1
#define TCP_TTL                         255

/* TCP Maximum Segment Size */
#define TCP_MSS                         536  /* Conservative value for compatibility */

/* TCP sender buffer space - increased for bridge throughput */
#define TCP_SND_BUF                     (2 * TCP_MSS)

/* TCP sender buffer space (pbufs) */
#define TCP_SND_QUEUELEN                ((4 * (TCP_SND_BUF) + (TCP_MSS - 1)) / (TCP_MSS))

/*
 * Temporary phase-1 exception: current TCP queue sizing trips lwIP's compile-time
 * sanity guard on this memory-constrained target. Keep the bypass in project
 * configuration instead of patching lwIP core source logic.
 */
#define LWIP_DISABLE_TCP_SANITY_CHECKS  1

/* TCP receive window - increased for bridge throughput */
#define TCP_WND                         (2 * TCP_MSS)

/* TCP writable space threshold */
#define TCP_SNDLOWAT                    LWIP_MIN(LWIP_MAX(((TCP_SND_BUF)/2), (2 * TCP_MSS) + 1), (TCP_SND_BUF) - 1)

/* Enable TCP keepalive */
#define LWIP_TCP_KEEPALIVE              1

/* TCP segment queue handling */
#define TCP_QUEUE_OOSEQ                 0  /* Disable out-of-order segment queuing to save RAM */

/* Maximum number of retransmissions */
#define TCP_MAXRTX                      12
#define TCP_SYNMAXRTX                   6

/* TCP listen backlog */
#define TCP_LISTEN_BACKLOG              1

/* TCP timestamp option */
#define LWIP_TCP_TIMESTAMPS             0

/*-----------------------------------------------------------------------------
 * RAW API (used for ping, etc.)
 *---------------------------------------------------------------------------*/

#define LWIP_RAW                        1

/*-----------------------------------------------------------------------------
 * DNS Configuration
 *---------------------------------------------------------------------------*/

#define LWIP_DNS                        0  /* Disable DNS to save RAM */

/*-----------------------------------------------------------------------------
 * IGMP Configuration
 *---------------------------------------------------------------------------*/

#define LWIP_IGMP                       0  /* Disable IGMP to save RAM */

/*-----------------------------------------------------------------------------
 * Callback Configuration
 *---------------------------------------------------------------------------*/

#define LWIP_NETIF_STATUS_CALLBACK      0
#define LWIP_NETIF_LINK_CALLBACK        0

/*-----------------------------------------------------------------------------
 * Checksum Configuration
 *---------------------------------------------------------------------------*/

/* Use software checksums (CH390 doesn't have checksum offload) */
#define CHECKSUM_GEN_IP                 1
#define CHECKSUM_GEN_UDP                1
#define CHECKSUM_GEN_TCP                1
#define CHECKSUM_GEN_ICMP               1
#define CHECKSUM_CHECK_IP               1
#define CHECKSUM_CHECK_UDP              1
#define CHECKSUM_CHECK_TCP              1
#define CHECKSUM_CHECK_ICMP             1

/*-----------------------------------------------------------------------------
 * Statistics (disabled to save RAM)
 *---------------------------------------------------------------------------*/

#define LWIP_STATS                      0
#define LWIP_STATS_DISPLAY              0

/*-----------------------------------------------------------------------------
 * Debug Options (disabled for production)
 *---------------------------------------------------------------------------*/

#define LWIP_DEBUG                      0

#if LWIP_DEBUG
#define LWIP_DBG_MIN_LEVEL              LWIP_DBG_LEVEL_ALL
#define LWIP_DBG_TYPES_ON               LWIP_DBG_ON

#define ETHARP_DEBUG                    LWIP_DBG_OFF
#define NETIF_DEBUG                     LWIP_DBG_OFF
#define PBUF_DEBUG                      LWIP_DBG_OFF
#define API_LIB_DEBUG                   LWIP_DBG_OFF
#define API_MSG_DEBUG                   LWIP_DBG_OFF
#define SOCKETS_DEBUG                   LWIP_DBG_OFF
#define ICMP_DEBUG                      LWIP_DBG_OFF
#define IGMP_DEBUG                      LWIP_DBG_OFF
#define INET_DEBUG                      LWIP_DBG_OFF
#define IP_DEBUG                        LWIP_DBG_OFF
#define IP_REASS_DEBUG                  LWIP_DBG_OFF
#define RAW_DEBUG                       LWIP_DBG_OFF
#define MEM_DEBUG                       LWIP_DBG_OFF
#define MEMP_DEBUG                      LWIP_DBG_OFF
#define SYS_DEBUG                       LWIP_DBG_OFF
#define TIMERS_DEBUG                    LWIP_DBG_OFF
#define TCP_DEBUG                       LWIP_DBG_OFF
#define TCP_INPUT_DEBUG                 LWIP_DBG_OFF
#define TCP_FR_DEBUG                    LWIP_DBG_OFF
#define TCP_RTO_DEBUG                   LWIP_DBG_OFF
#define TCP_CWND_DEBUG                  LWIP_DBG_OFF
#define TCP_WND_DEBUG                   LWIP_DBG_OFF
#define TCP_OUTPUT_DEBUG                LWIP_DBG_OFF
#define TCP_RST_DEBUG                   LWIP_DBG_OFF
#define TCP_QLEN_DEBUG                  LWIP_DBG_OFF
#define UDP_DEBUG                       LWIP_DBG_OFF
#define TCPIP_DEBUG                     LWIP_DBG_OFF
#define DHCP_DEBUG                      LWIP_DBG_OFF
#endif /* LWIP_DEBUG */

/*-----------------------------------------------------------------------------
 * FreeRTOS Specific Options
 *---------------------------------------------------------------------------*/

/* Task stack sizes */
#define TCPIP_THREAD_STACKSIZE          512
#define TCPIP_THREAD_PRIO               (configMAX_PRIORITIES - 2)

#define DEFAULT_THREAD_STACKSIZE        256
#define DEFAULT_THREAD_PRIO             (configMAX_PRIORITIES - 3)

/* Mailbox sizes */
#define TCPIP_MBOX_SIZE                 12
#define DEFAULT_RAW_RECVMBOX_SIZE       4
#define DEFAULT_UDP_RECVMBOX_SIZE       4
#define DEFAULT_TCP_RECVMBOX_SIZE       6
#define DEFAULT_ACCEPTMBOX_SIZE         6

/* Thread name length */
#define LWIP_NETCONN_SEM_PER_THREAD     1

/*-----------------------------------------------------------------------------
 * Ethernet Specific
 *---------------------------------------------------------------------------*/

/* Ethernet MTU */
#define LWIP_ETHERNET                   1

/* Link layer header overhead */
#define PBUF_LINK_HLEN                  14  /* Ethernet header size */
#define PBUF_LINK_ENCAPSULATION_HLEN    0

/* Use static Ethernet address (configured at runtime) */
#define LWIP_NETIF_HOSTNAME             1

/*-----------------------------------------------------------------------------
 * Socket Options
 *---------------------------------------------------------------------------*/

#define LWIP_SO_SNDTIMEO                1
#define LWIP_SO_RCVTIMEO                1
#define LWIP_SO_RCVBUF                  0
#define SO_REUSE                        1

/*-----------------------------------------------------------------------------
 * Additional Options
 *---------------------------------------------------------------------------*/

/* Enable loop interface for testing */
#define LWIP_HAVE_LOOPIF                0
#define LWIP_NETIF_LOOPBACK             0

/* Random number generator (required for some TCP operations) */
#define LWIP_RAND()                     ((uint32_t)rand())

#endif /* LWIP_LWIPOPTS_H */
