"""
HART-IP protocol library for Python.

A pure-Python implementation of the HART-IP transport (TP10300 / HCF_SPEC-085)
and HART application layer, using the ``construct`` library for binary parsing.

Supports both HART-IP v1 (plaintext) and v2 (TLS/DTLS, Direct PDU, Audit Log).

Quickstart::

    from hartip import HARTIPClient

    with HARTIPClient("192.168.1.100") as client:
        resp = client.read_unique_id()
        info = resp.parsed  # auto-parses Command 0 payload
        print(info.manufacturer_name, info.device_id)

    # Friendly-name parsers also available:
    from hartip import parse_unique_id, parse_additional_device_status

    # Or the classic parse_cmdNN names still work:
    from hartip import parse_cmd0, parse_cmd48

    # v2 Direct PDU
    from hartip.v2 import DirectPDUCommand

    with HARTIPClient("192.168.1.100", version=2) as client:
        result = client.send_direct_pdu([
            DirectPDUCommand(command_number=0),
            DirectPDUCommand(command_number=48),
        ])
"""

from .ascii import pack_ascii, unpack_ascii
from .client import HARTIPClient, HARTIPResponse, probe_server_version
from .constants import (
    COMM_ERROR_MASK,
    DEFAULT_INACTIVITY_TIMER,
    HARTIP_HEADER_SIZE,
    HARTIP_TCP_PORT,
    HARTIP_UDP_PORT,
    HARTIP_V2_ALL_CIPHERS,
    HARTIP_V2_PSK_CIPHERS,
    HARTIP_V2_SRP_CIPHERS,
    MASTER_TYPE_PRIMARY,
    MASTER_TYPE_SECONDARY,
    HARTCommand,
    HARTCommErrorFlags,
    HARTDeviceStatus,
    HARTFrameType,
    HARTIPMessageID,
    HARTIPMessageType,
    HARTIPServerStatus,
    HARTIPSessionStatus,
    HARTIPStatus,
    HARTIPVersion,
    HARTResponseCode,
)
from .device import (
    # Registry and dispatch
    COMMAND_REGISTRY,
    PHYSICAL_SIGNALING_CODES,
    # Dataclasses
    DeviceInfo,
    DeviceVariable,
    Variable,
    # Utilities
    decode_comm_error_flags,
    get_command_name,
    get_device_type_name,
    get_parser,
    get_physical_signaling_name,
    is_comm_error,
    parse_additional_device_status,
    parse_burst_command_number,
    parse_burst_device_variables,
    parse_burst_mode_config,
    parse_burst_mode_control,
    parse_burst_period,
    parse_burst_trigger,
    # --- parse_cmdNN functions (backward compatible) ---
    parse_cmd0,
    parse_cmd1,
    parse_cmd2,
    parse_cmd3,
    parse_cmd6,
    parse_cmd7,
    parse_cmd8,
    parse_cmd9,
    parse_cmd11,
    parse_cmd12,
    parse_cmd13,
    parse_cmd14,
    parse_cmd15,
    parse_cmd16,
    parse_cmd17,
    parse_cmd18,
    parse_cmd19,
    parse_cmd20,
    parse_cmd21,
    parse_cmd22,
    parse_cmd33,
    parse_cmd35,
    parse_cmd38,
    parse_cmd44,
    parse_cmd48,
    parse_cmd52,
    parse_cmd53,
    parse_cmd54,
    parse_cmd71,
    parse_cmd76,
    parse_cmd79,
    parse_cmd90,
    parse_cmd95,
    parse_cmd103,
    parse_cmd104,
    parse_cmd105,
    parse_cmd107,
    parse_cmd108,
    parse_cmd109,
    parse_cmd512,
    parse_cmd513,
    parse_cmd534,
    parse_command,
    parse_country_code,
    parse_current_and_percent,
    parse_device_message_statistics,
    parse_device_message_timing,
    parse_device_variable_info,
    parse_device_variable_simulation_status,
    parse_device_variable_units,
    parse_device_variables,
    parse_device_variables_with_status,
    parse_dynamic_variable_classifications,
    parse_dynamic_variables,
    parse_final_assembly_number,
    parse_lock_device,
    parse_lock_device_state,
    parse_long_tag,
    parse_loop_configuration,
    parse_message,
    parse_output_info,
    parse_polling_address,
    parse_primary_variable,
    parse_pv_range_values,
    parse_pv_transducer_info,
    parse_pv_units,
    parse_reset_config_flag,
    parse_set_device_variable_zero,
    parse_tag_descriptor_date,
    # --- Friendly-name aliases ---
    parse_unique_id,
    parse_unique_id_by_tag,
    parse_write_device_variable,
)
from .exceptions import (
    HARTChecksumError,
    HARTCommunicationError,
    HARTError,
    HARTIPConnectionError,
    HARTIPError,
    HARTIPStatusError,
    HARTIPTimeoutError,
    HARTIPTLSError,
    HARTProtocolError,
    HARTResponseError,
)
from .high_level import Device
from .lookups import (
    ALARM_SELECTION_CODES,
    CLASSIFICATION_CODES,
    DEVICE_FAMILY_CODES,
    HART_TICKS_PER_SECOND,
    OPERATING_MODE_CODES,
    TRANSFER_FUNCTION_CODES,
    WRITE_PROTECT_CODES,
    decode_cmd0_flags,
    decode_device_status,
    decode_device_variable_status,
    decode_extended_device_status,
    decode_standardized_status_0,
    decode_standardized_status_1,
    decode_standardized_status_2,
    decode_standardized_status_3,
    get_alarm_selection_name,
    get_classification_name,
    get_device_family_name,
    get_operating_mode_name,
    get_transfer_function_name,
    get_write_protect_name,
    hart_ticks_to_seconds,
    hart_ticks_to_timedelta,
)
from .protocol import (
    HARTIPHeader,
    HARTPdu,
    PduContainer,
    build_keep_alive,
    build_pdu,
    build_request,
    build_session_close,
    build_session_init,
    parse_pdu,
    parse_response,
    xor_checksum,
)
from .types import (
    Cmd2Response,
    Cmd3Response,
    Cmd8Response,
    Cmd9Response,
    Cmd13Response,
    Cmd15Response,
    Cmd48Response,
    Cmd54Response,
)
from .units import UNITS, get_unit_name
from .v2 import (
    AuditLogResponse,
    DirectPDU,
    DirectPDUCommand,
    SessionLogRecord,
    build_audit_log_request,
    build_direct_pdu_request,
    parse_audit_log_response,
    parse_direct_pdu_request,
    parse_direct_pdu_response,
)
from .vendors import MANUFACTURERS, get_vendor_name

__version__ = "0.3.0"

__all__ = [
    # Client
    "HARTIPClient",
    "HARTIPResponse",
    "probe_server_version",
    # High-level
    "Device",
    # Typed response dicts
    "Cmd2Response",
    "Cmd3Response",
    "Cmd8Response",
    "Cmd9Response",
    "Cmd13Response",
    "Cmd15Response",
    "Cmd48Response",
    "Cmd54Response",
    # Protocol structs (v1)
    "HARTIPHeader",
    "HARTPdu",
    "PduContainer",
    "build_keep_alive",
    "build_pdu",
    "build_request",
    "build_session_close",
    "build_session_init",
    "parse_pdu",
    "parse_response",
    "xor_checksum",
    # Protocol structs (v2)
    "DirectPDU",
    "DirectPDUCommand",
    "AuditLogResponse",
    "SessionLogRecord",
    "build_direct_pdu_request",
    "parse_direct_pdu_request",
    "parse_direct_pdu_response",
    "build_audit_log_request",
    "parse_audit_log_response",
    # Constants / enums
    "HARTIPVersion",
    "HARTIPMessageID",
    "HARTIPMessageType",
    "HARTIPStatus",
    "HARTIPServerStatus",
    "HARTIPSessionStatus",
    "HARTFrameType",
    "HARTCommand",
    "HARTResponseCode",
    "HARTDeviceStatus",
    "HARTCommErrorFlags",
    "COMM_ERROR_MASK",
    "HARTIP_HEADER_SIZE",
    "HARTIP_UDP_PORT",
    "HARTIP_TCP_PORT",
    "HARTIP_V2_PSK_CIPHERS",
    "HARTIP_V2_SRP_CIPHERS",
    "HARTIP_V2_ALL_CIPHERS",
    "MASTER_TYPE_PRIMARY",
    "MASTER_TYPE_SECONDARY",
    "DEFAULT_INACTIVITY_TIMER",
    # Command registry and dispatch
    "COMMAND_REGISTRY",
    "get_parser",
    "get_command_name",
    "parse_command",
    # Device / parsing
    "DeviceInfo",
    "DeviceVariable",
    "Variable",
    "PHYSICAL_SIGNALING_CODES",
    "get_device_type_name",
    "get_physical_signaling_name",
    "is_comm_error",
    "decode_comm_error_flags",
    # parse_cmdNN (backward compatible)
    "parse_cmd0",
    "parse_cmd1",
    "parse_cmd2",
    "parse_cmd3",
    "parse_cmd6",
    "parse_cmd7",
    "parse_cmd8",
    "parse_cmd9",
    "parse_cmd11",
    "parse_cmd12",
    "parse_cmd13",
    "parse_cmd14",
    "parse_cmd15",
    "parse_cmd16",
    "parse_cmd17",
    "parse_cmd18",
    "parse_cmd19",
    "parse_cmd20",
    "parse_cmd21",
    "parse_cmd22",
    "parse_cmd33",
    "parse_cmd35",
    "parse_cmd38",
    "parse_cmd44",
    "parse_cmd48",
    "parse_cmd52",
    "parse_cmd53",
    "parse_cmd54",
    "parse_cmd71",
    "parse_cmd76",
    "parse_cmd79",
    "parse_cmd90",
    "parse_cmd95",
    "parse_cmd103",
    "parse_cmd104",
    "parse_cmd105",
    "parse_cmd107",
    "parse_cmd108",
    "parse_cmd109",
    "parse_cmd512",
    "parse_cmd513",
    "parse_cmd534",
    # Friendly-name parse aliases
    "parse_unique_id",
    "parse_primary_variable",
    "parse_current_and_percent",
    "parse_dynamic_variables",
    "parse_polling_address",
    "parse_loop_configuration",
    "parse_dynamic_variable_classifications",
    "parse_device_variables_with_status",
    "parse_unique_id_by_tag",
    "parse_message",
    "parse_tag_descriptor_date",
    "parse_pv_transducer_info",
    "parse_output_info",
    "parse_final_assembly_number",
    "parse_long_tag",
    "parse_device_variables",
    "parse_pv_range_values",
    "parse_reset_config_flag",
    "parse_pv_units",
    "parse_additional_device_status",
    "parse_set_device_variable_zero",
    "parse_device_variable_units",
    "parse_device_variable_info",
    "parse_lock_device",
    "parse_lock_device_state",
    "parse_write_device_variable",
    "parse_device_message_timing",
    "parse_device_message_statistics",
    "parse_burst_period",
    "parse_burst_trigger",
    "parse_burst_mode_config",
    "parse_burst_device_variables",
    "parse_burst_command_number",
    "parse_burst_mode_control",
    "parse_country_code",
    "parse_device_variable_simulation_status",
    # ASCII
    "pack_ascii",
    "unpack_ascii",
    # Lookup tables
    "MANUFACTURERS",
    "UNITS",
    "CLASSIFICATION_CODES",
    "TRANSFER_FUNCTION_CODES",
    "OPERATING_MODE_CODES",
    "ALARM_SELECTION_CODES",
    "WRITE_PROTECT_CODES",
    "DEVICE_FAMILY_CODES",
    "HART_TICKS_PER_SECOND",
    "get_vendor_name",
    "get_unit_name",
    "get_classification_name",
    "get_transfer_function_name",
    "get_operating_mode_name",
    "get_alarm_selection_name",
    "get_write_protect_name",
    "get_device_family_name",
    # Bitfield decoders
    "decode_device_status",
    "decode_cmd0_flags",
    "decode_extended_device_status",
    "decode_device_variable_status",
    "decode_standardized_status_0",
    "decode_standardized_status_1",
    "decode_standardized_status_2",
    "decode_standardized_status_3",
    # Timestamp helpers
    "hart_ticks_to_seconds",
    "hart_ticks_to_timedelta",
    # Exceptions
    "HARTError",
    "HARTIPError",
    "HARTIPTimeoutError",
    "HARTIPConnectionError",
    "HARTIPTLSError",
    "HARTIPStatusError",
    "HARTProtocolError",
    "HARTChecksumError",
    "HARTCommunicationError",
    "HARTResponseError",
]
