//! Darra EtherCAT Master SDK - Rust 绑定
//!
//! 封装 `Darra.Core.dll` / `libDarraCore.so`, 提供安全的 Rust API。
//!
//! # 快速开始
//!
//! ## 方式一: 直接初始化
//!
//! ```no_run
//! use ethercat::{EtherCATMaster, EcState};
//!
//! let mut master = EtherCATMaster::new().unwrap();
//! master.set_network("\\Device\\NPF_{...}", "").unwrap();
//! master.set_state_sequence(EcState::Operational, 10000).unwrap();
//! master.start();
//!
//! // PDO 循环中读写从站数据
//! let slave = master.slave(1);
//! // ...
//!
//! master.stop();
//! // master 析构时自动释放资源
//! ```
//!
//! ## 方式二: 构建器模式 (推荐)
//!
//! ```no_run
//! use ethercat::{EtherCATMaster, EcState, MasterBuilder, BuildResult};
//!
//! let result = EtherCATMaster::builder()
//!     .set_eni("config.xml")
//!     .enable_auto_startup()
//!     .build()
//!     .unwrap();
//!
//! // BuildResult 实现 Deref<Target = EtherCATMaster>
//! println!("从站数量: {}", result.slave_count());
//! ```
//!
//! # 模块组织
//!
//! - [`master`] - 主站核心 (初始化、状态、PDO循环、IO映射)
//! - [`slave`] - 从站操作 (CoE/SoE/FoE/EoE/AoE/VoE/FSoE)
//! - [`data`] - 数据类型 (错误、枚举、结构体)
//! - [`statics`] - 静态功能 (网络扫描、版本信息、授权)
//! - [`utils`] - 工具模块 (FFI绑定、数据类型处理)
//! - [`logging`] - 日志系统 (DLL回调日志)

/// 主站模块
pub mod master;

/// 从站模块
pub mod slave;

/// 数据类型模块
pub mod data;

/// 静态功能模块
pub mod statics;

/// 工具模块
pub mod utils;

/// 日志模块
pub mod logging;

/// 邮箱抽象层 (对齐 C# Abstractions)
pub mod abstractions;

// ===================== 核心类型重新导出 =====================

// 邮箱抽象层 (对齐 C# IMailboxProtocol / MailboxStatistics / MailboxStatus)
pub use abstractions::{MailboxStatus, MailboxStatistics, MailboxProtocol, MailboxError, MailboxResult};

// 数据类型
pub use data::error::{
    CiA402Mode, CiA402State, DarraError, EcState, FoEErrorCode,
    LinkState, RedundancyState, SoEErrorCode, SyncWindowStatus, Result,
};
pub use data::structures::{
    constants, PdoStats, PdoMappingEntry, CommunicationStatsLocal,
    SlaveInfo, RealtimeStats, FoEOptionsLocal, EcGroupConfigHelper,
};
pub use data::types::*;

// FFI 类型
pub use utils::ffi::{
    EmcyRecord, TopologyNode, SlaveIdentity, EsmTimeouts,
    FoEOptions, CommunicationStats, RedundancyStatus,
    WatchdogConfig, WatchdogStatus,
    MasterIdentity, MasterDiagData,
    AOENotificationCallback,
    FsoeState, FsoeError, FsoeConfig, FsoeStatus, SafeMdpConfig,
};
pub use master::core::{EtherCATMaster, IomapGuard, BuildResult, MasterBuilder};
pub use slave::core::Slave;

// 重新导出 FFI 常量 (启动参数时序)
pub use utils::ffi::{
    TIMING_AFTER, TIMING_BEFORE,
    TRANS_IP, TRANS_OS, TRANS_PI, TRANS_PS, TRANS_SO, TRANS_SP,
};

// 回调类型
pub use utils::ffi::{
    LogCallback, CrashNotifyCallback, ProcessDataCyclicCallback,
    SlaveStateChangeCallback, EmergencyEventCallback, SlaveDiscoveryCallback,
    InputDataChangedCallback, RedundancyModeChangedCallback,
    PDOFrameLossCallback, SlavePreOpReconfigCallback,
    FoEProgressCallback, DCSyncLostCallback,
};

// 主站子模块类型
pub use master::{
    MasterEvents, SlaveEvents, SlaveIdentityMismatch,
    EmergencyMessage, EmcyRecorder,
    MasterDiagnosticsInfo, MasterPDODiagnostics, BreakPointInfo, PDOFrameLossStats,
    MasterConfig, save_master_settings, load_master_settings,
    MasterObjectDictionary,
    MailboxGatewayService,
    RedundancyManager, RedundancyInfo,
    HotConnect, HotConnectGroup, HotConnectStatus, HotPlugIdentityMismatch,
    MasterOdEtg1510,
};

// 从站子模块类型
pub use slave::{
    EcDataType, ObjAccess, ObjectEntry, OdObject, OdList,
    SoEInstance, SoEParameter, SoEAttributes, SoEDataType,
    EoEInstance, EoEPingResult, eoe_ping,
    FoEInstance,
    AoEInstance, AoeTransMode, AoeSubscription, AoeSubscriptionManager,
    VoEInstance, VoEResponse,
    FsoeCrc16, SafetyManager, SafeMdp, FsoeConnectionInfo,
    SlaveDC,
    PdoMonitor, PdoManager,
    SlavePdo,
    CiA401, CiA401ErrorMode,
    CiA402Instance, StateCiA402, ModeCiA402,
    PdoMapEntry, Cia402PdoMap,
    MdpModule, MdpModuleClass, MdpModuleProfile,
    EsiLoader, EsiStartupParam, extract_startup_parameters,
    StartupParameter, StartupParameterList,
    SlaveTopology,
};

// CiA 402 常量
pub use slave::cia402::{
    OD_CONTROLWORD, OD_STATUSWORD, OD_MODES_OF_OPERATION, OD_MODES_OF_OPERATION_DISPLAY,
    OD_TARGET_POSITION, OD_POSITION_ACTUAL, OD_TARGET_VELOCITY, OD_VELOCITY_ACTUAL,
    OD_TARGET_TORQUE, OD_TORQUE_ACTUAL, OD_PROFILE_VELOCITY, OD_PROFILE_ACCELERATION,
    OD_PROFILE_DECELERATION, OD_QUICK_STOP_DECELERATION, OD_HOMING_METHOD, OD_HOMING_SPEEDS,
    OD_HOME_OFFSET, OD_SUPPORTED_DRIVE_MODES, OD_DIGITAL_INPUTS, OD_DIGITAL_OUTPUTS,
    CW_SHUTDOWN, CW_SWITCH_ON, CW_ENABLE_OPERATION, CW_DISABLE_VOLTAGE,
    CW_QUICK_STOP, CW_FAULT_RESET, CW_HALT,
    SW_FAULT, SW_TARGET_REACHED, SW_WARNING, SW_REMOTE,
};

// 静态功能
pub use statics::{
    ScannedSlaveInfo, NetworkInfo, get_scanned_slaves, get_network_adapters,
    DllVersion, dll_version,
    LicenseStatus, Authorization,
};

// 物理 NIC 枚举 (含从站数, 与 C# DarraEtherCAT.GetNetworkInfo 对齐)
pub use statics::network::{EnumeratedNetworkInfo, enumerate_network_info};

// 工具
pub use utils::{
    BaseData, EcValue, type_size, convert_from_bytes, convert_to_bytes, default_value,
    ExpectedSlaveConfig, ActualSlaveInfo, SlaveVerifyDetail,
    VerificationResult, verify_configuration,
};

// 内核驱动可用性门控 (对齐 C# `DarraEtherCAT_Master::KernelGuard`)
pub use utils::kernel_guard;
pub use utils::kernel_guard::{DarraKernelStatus, DarraKernelNotAvailableError};

// ESI 工具
pub use utils::esi::{
    EsiPhysicsPortType, EsiRevisionCheckStrategy,
    EsiBootstrapInfo, EsiCoEDetails, EsiDcOpMode, EsiDcConfiguration,
    EsiEepromConfiguration, EsiElectricalInfo, EsiIdentification, EsiMailboxTimeout,
    EsiPdoEntry, EsiPdoInfo, EsiPdoConfiguration, EsiPhysicsPort, EsiPhysicsInfo,
    EsiPortInfo, EsiStartupSdo, EsiStatistics, EsiSyncManagerInfo, EsiDeviceInfo,
    calculate_eeprom_crc, validate_eeprom_crc, match_revision,
};

// 日志
pub use logging::{LogManager, LogCategory, LogEntry, LogView, LogUpdatedCallback};
