# SPDX-License-Identifier: MIT

config NET_L2_CUSTOM
	bool "Enable custom Layer 2 protocol support"
	depends on NETWORKING
	help
	  Enable support for custom Layer 2 protocols.
	  This allows implementing custom MAC protocols that don't fit
	  the standard Ethernet/IEEE 802.11/etc. model.

if NET_L2_CUSTOM

config NET_L2_HEYMAC
	bool "HeyMac Layer 2 protocol support"
	depends on NET_L2_CUSTOM
	depends on LORA
	help
	  Enable HeyMac Layer 2 protocol for LoRa mesh networking.
	  HeyMac provides the MAC layer for Æther mesh networks.

if NET_L2_HEYMAC

config NET_L2_HEYMAC_LOG_LEVEL
	int "HeyMac log level"
	depends on LOG
	default 3
	range 0 4
	help
	  Set log level for HeyMac protocol.

config HEYMAC_DEFAULT_NET_ID
	hex "Default HeyMac network ID"
	default 0x915E
	help
	  Default network ID for HeyMac frames.
	  0x915E = "915E" for 915MHz networks.

config HEYMAC_USE_SHORT_ADDR
	bool "Use short addressing by default"
	default y
	help
	  Use 2-byte short addresses instead of 6-byte long addresses.

config HEYMAC_MAX_FRAME_SIZE
	int "Maximum frame size"
	default 255
	range 64 255
	help
	  Maximum HeyMac frame size in bytes.

endif # NET_L2_HEYMAC

config HEYMAC_BEACON_INTERVAL_MS
	int "Beacon interval (milliseconds)"
	depends on NET_L2_HEYMAC
	default 30000
	range 5000 120000
	help
	  Interval between beacon transmissions in milliseconds.

rsource "subsys/net/l2/uart/Kconfig"

config AETHER_MESH
	bool "Æther mesh networking support"
	depends on NET_L2_HEYMAC || AETHER_UART
	help
	  Enable Æther mesh networking protocol for multi-hop networks.
	  Works over HeyMac (LoRa) or UART transports.

if AETHER_MESH

config AETHER_MESH_LOG_LEVEL
	int "Æther mesh log level"
	depends on LOG
	default 3
	range 0 4
	help
	  Set log level for Æther mesh protocol.

config AETHER_MAX_ROUTES
	int "Maximum number of routes in routing table"
	default 16
	range 4 64
	help
	  Maximum number of destination routes to maintain.

config AETHER_MAX_NEIGHBORS
	int "Maximum number of neighbors"
	default 8
	range 4 32
	help
	  Maximum number of direct neighbors to track.

config AETHER_DEFAULT_TTL
	int "Default time-to-live for mesh packets"
	default 6
	range 1 15
	help
	  Default TTL value for originated packets.

config AETHER_HELLO_INTERVAL
	int "Hello message interval (seconds)"
	default 30
	range 10 300
	help
	  Interval between hello messages for neighbor discovery.

config AETHER_ROUTE_TIMEOUT
	int "Route timeout (seconds)"
	default 300
	range 60 3600
	help
	  Time before inactive routes are expired.

config AETHER_NEIGHBOR_TIMEOUT
	int "Neighbor timeout (seconds)"
	default 90
	range 30 600
	help
	  Time before inactive neighbors are considered lost.

config AETHER_CONDITIONAL_NET_ID
	bool "Use conditional mesh network ID optimization"
	default y
	help
	  Enable conditional mesh network ID to save 2 bytes when mesh
	  network ID matches MAC network ID (12.5% header reduction).

config AETHER_MAX_PAYLOAD
	int "Maximum payload size"
	default 200
	range 32 255
	help
	  Maximum payload size for mesh packets. SX1262 supports up to
	  255 bytes per LoRa packet. Larger payloads reduce fragmentation
	  overhead for 9P transport.

# --- Reliable datagram (DELIV_RELIABLE) engine, see docs §4.6 ---

config AETHER_RELIABLE_WINDOW
	int "Reliable in-flight window (segments)"
	default 8
	range 1 32
	help
	  Maximum reliable DATA segments in flight per peer. Must stay < 64 so
	  selective repeat is unambiguous in the 1-byte sequence space. LoRa
	  effectively uses one message's worth; higher-rate PHYs use more.

config AETHER_RELIABLE_PEERS
	int "Reliable per-peer state slots"
	default 4
	range 1 32
	help
	  Number of concurrent peers the reliable engine tracks. Least-recently
	  used soft state is evicted when exceeded.

config AETHER_RELIABLE_MTU
	int "Reliable segment buffer size (bytes)"
	default 200
	range 32 255
	help
	  Per-slot TX/RX buffer size. Should match AETHER_MAX_PAYLOAD. Drives
	  the reliable engine's static memory: ~2 * WINDOW * PEERS * MTU.

config AETHER_RELIABLE_RTO_MS
	int "Reliable base retransmit timeout (ms)"
	default 4000
	help
	  Initial RTO before exponential backoff. LoRa SF10/BW125 wants seconds;
	  high-rate PHYs (DECT NR+) want tens of ms.

config AETHER_RELIABLE_ACK_DELAY_MS
	int "Reliable delayed-ACK window (ms)"
	default 300
	help
	  Coalesce/piggyback window for ACKs. 0 = acknowledge immediately. LoRa
	  coalesces hard (200-500); high-rate PHYs use ~0.

config AETHER_RELIABLE_MAX_RETRIES
	int "Reliable max retransmissions before failure"
	default 5
	help
	  After this many retransmits of a segment, delivery is declared failed
	  and the per-peer state is reset (the upper-layer session times out).

config AETHER_SHELL
	bool "Æther shell commands"
	depends on SHELL
	help
	  Enable shell commands for Æther mesh debugging and control.

config AETHER_MESH_WORKQ_STACK_SIZE
	int "Mesh workqueue stack size (bytes)"
	default 1536
	help
	  Stack for the mesh workqueue, which ticks the reliable engine and
	  delivers received datagrams.  The default suits mesh/HeyMac work and
	  best-effort consumers (e.g. chat).  Raise it on a node that consumes a
	  reliable conversation in-kernel on this workqueue — e.g. the 9P client
	  transport's per-conversation callback, which wakes the dialer (a
	  scheduler operation) from delivery context.  4096 is comfortable for
	  that; only raise it where the static DRAM is available.

config AETHER_SVCDISC
	bool "Æther service discovery (beacon advertisements + peer table)"
	default y
	help
	  Piggyback a service advertisement (node name + symbolic service
	  names) on the HELLO beacon and keep a bounded table of heard
	  peers.  Discovery metadata only: resolution is a real 9P attach.

config AETHER_SVCDISC_MAX_PEERS
	int "Known-peer table size"
	depends on AETHER_SVCDISC
	default 32
	help
	  Entries in the static known-peer table (~100 B each, allocated
	  from PSRAM when available).  Oldest entries are evicted when
	  full, except within the recent-heard guard window.

config AETHER_SVCDISC_MAX_AGE
	int "Peer age-out (seconds)"
	depends on AETHER_SVCDISC
	default 600
	help
	  Drop peers not heard from within this window.  Advisory aging
	  only — liveness is discovered at attach time, and HELLO
	  intervals stretch under stable-neighbor backoff.

config AETHER_NET_FS
	bool "9P /net/aether protocol directory (needs app srv framework)"
	help
	  The canonical Plan 9-style /net/aether file server
	  (clone/<N>/{ctl,data,...}, self/, peers/) over the reliable
	  datagram engine.  The consuming app must provide FRST's srv
	  framework and mem allocator ("srv.h" and "mem.h" under
	  <app>/src) plus the 9p4z module; see aether_net_fs.c for the
	  full contract.  Enabled by FRST decks and Æther gateways.

endif # AETHER_MESH

config AETHER_LORA
	bool "Æther LoRa driver integration"
	depends on LORA_SX126X
	depends on NET_L2_HEYMAC
	help
	  LoRa driver integration for Æther mesh networking.

if AETHER_LORA

config AETHER_LORA_LOG_LEVEL
	int "Æther LoRa log level"
	depends on LOG
	default 3
	range 0 4

config AETHER_RX_QUEUE_SIZE
	int "RX message queue size"
	default 8
	range 4 32
	help
	  Number of messages that can be queued for RX processing.

config AETHER_TX_QUEUE_SIZE
	int "TX message queue size"
	default 4
	range 2 16
	help
	  Number of messages that can be queued for transmission.

config AETHER_RX_STACK_SIZE
	int "RX thread stack size"
	default 2048 if !SOC_ESP32
	default 3072 if SOC_ESP32
	range 1024 8192
	help
	  Stack size for the RX processing thread.

config AETHER_TX_STACK_SIZE
	int "TX thread stack size"
	default 1536 if !SOC_ESP32
	default 2048 if SOC_ESP32
	range 1024 8192
	help
	  Stack size for the TX processing thread.

config AETHER_RX_THREAD_PRIO
	int "RX thread priority"
	default 2
	range 0 16
	help
	  Priority for the RX processing thread.

config AETHER_TX_THREAD_PRIO
	int "TX thread priority"
	default 3
	range 0 16
	help
	  Priority for the TX processing thread.

config AETHER_LORA_SF
	int "Default spreading factor"
	default 10
	range 7 12
	help
	  Default LoRa spreading factor (SF7-SF12).  Lower = higher data rate /
	  shorter air time / shorter range.  SF10 is the long-range default;
	  drop to 7 (e.g. via a build overlay) for high-throughput short-range
	  use such as in-lab testing.

config AETHER_LORA_BW
	int "Default bandwidth"
	default 0
	range 0 2
	help
	  Default LoRa bandwidth (0=125kHz, 1=250kHz, 2=500kHz).

config AETHER_LORA_CR
	int "Default coding rate"
	default 1
	range 1 4
	help
	  Default LoRa coding rate (1=4/5, 2=4/6, 3=4/7, 4=4/8).

config AETHER_LORA_TX_POWER
	int "Default TX power (dBm)"
	default 14
	range 0 22
	help
	  Default transmit power in dBm.

config AETHER_LORA_CAD_ENABLED
	bool "Default state of CAD listen-before-talk"
	default n
	help
	  Boot default for Channel Activity Detection (CAD) before each
	  transmission.  CAD is collision *avoidance* (the SX1262 detects LoRa
	  preambles in ~16ms and backs off if the channel is busy); the reliable
	  datagram engine already provides collision *recovery* via retransmit,
	  so CAD is an optimization for contended/long-range meshes, not a
	  correctness requirement.  Default off — best for short-range / few
	  nodes (and avoids close-range self-deference).  This only seeds the
	  runtime flag; flip it live via /net/aether/phy.  The CAD code is always
	  compiled in so it can be enabled at runtime.

config AETHER_LORA_CAD_MAX_RETRIES
	int "Maximum CAD retry attempts"
	default 5
	range 1 10
	help
	  Number of times to retry CAD if the channel is busy before
	  transmitting anyway.

config AETHER_LORA_CAD_BACKOFF_MAX_MS
	int "Maximum CAD backoff (ms)"
	default 100
	range 20 500
	help
	  Maximum random backoff time in milliseconds when CAD detects
	  channel activity.

endif # AETHER_LORA

endif # NET_L2_CUSTOM