// SPDX-License-Identifier: LSL-1.0
// Copyright (c) 2026 Markus Maiwald
// Stewardship: Self Sovereign Society Foundation
//
// This file is part of the Nexus Sovereign Core.
// See legal/LICENSE_SOVEREIGN.md for license terms.

/**
 * @file cc.h
 * @brief lwIP Compiler/Platform Compatibility Layer
 * 
 * This file defines compiler-specific macros and types required by lwIP.
 * Tailored for GCC/Clang in a freestanding environment.
 */

#ifndef LWIP_ARCH_CC_H
#define LWIP_ARCH_CC_H

#include <stdint.h>
#include <stddef.h>

// =========================================================
// Basic Types (Fixed-width integers)
// =========================================================

typedef uint8_t     u8_t;
typedef int8_t      s8_t;
typedef uint16_t    u16_t;
typedef int16_t     s16_t;
typedef uint32_t    u32_t;
typedef int32_t     s32_t;

// Memory pointer types
typedef uintptr_t   mem_ptr_t;

// Protection type (required for SYS_LIGHTWEIGHT_PROT even in NO_SYS mode)
typedef uint32_t    sys_prot_t;

// =========================================================
// Endianness (RISC-V 64 is Little Endian)
// =========================================================
#undef LITTLE_ENDIAN
#define LITTLE_ENDIAN 1234
#undef BIG_ENDIAN
#define BIG_ENDIAN 4321
#undef BYTE_ORDER
#define BYTE_ORDER LITTLE_ENDIAN

// =========================================================
// Compiler Hints
// =========================================================

// Packing structures (for protocol headers)
#define PACK_STRUCT_FIELD(x)    x
#define PACK_STRUCT_STRUCT      __attribute__((packed))
#define PACK_STRUCT_BEGIN
#define PACK_STRUCT_END

// Alignment
#define LWIP_PLATFORM_BYTESWAP  0  // We'll use lwIP's portable functions

// =========================================================
// Diagnostics and Assertions
// =========================================================

// Platform diagnostics
extern void lwip_platform_diag(const char *fmt, ...);
#ifndef LWIP_PLATFORM_DIAG
#define LWIP_PLATFORM_DIAG(x)   lwip_platform_diag x
#endif

// Platform assertions
extern void nexus_lwip_panic(const char* msg);
#ifndef LWIP_PLATFORM_ASSERT
#define LWIP_PLATFORM_ASSERT(x) nexus_lwip_panic(x)
#endif

// =========================================================
// Random Number Generation
// =========================================================

// lwIP needs a random number source for things like TCP ISN
// We'll use the kernel's crypto subsystem
extern uint32_t syscall_get_random(void);
#define LWIP_RAND()     syscall_get_random()

// =========================================================
// Printf Format Specifiers
// =========================================================

// For 64-bit architectures
// For 64-bit architectures
#define X8_F    "02x"
#define U16_F   "hu"
#define S16_F   "hd"
#define X16_F   "hx"
#define U32_F   "u"
#define S32_F   "d"
#define X32_F   "x"
#define SZT_F   "lu"

#endif /* LWIP_ARCH_CC_H */
