#ifndef AD_BINARY_TRANSPORT_H
#define AD_BINARY_TRANSPORT_H

#include <stdint.h>

typedef enum
{
    AD_HOST_PROTOCOL_MODBUS = 0,
    AD_HOST_PROTOCOL_BINARY_TELEMETRY = 1
} AD_HostProtocol_t;

typedef enum
{
    AD_BINARY_TRANSPORT_USB = 0,
    AD_BINARY_TRANSPORT_CAN = 1,
    AD_BINARY_TRANSPORT_USB_AND_CAN = 2
} AD_BinaryTransportTarget_t;

typedef enum
{
    AD_BINARY_TRANSPORT_STATUS_INITIALIZED = (1UL << 0),
    AD_BINARY_TRANSPORT_STATUS_USB_READY = (1UL << 1),
    AD_BINARY_TRANSPORT_STATUS_CAN_READY = (1UL << 2),
    AD_BINARY_TRANSPORT_STATUS_USB_CONFIGURED = (1UL << 3),
    AD_BINARY_TRANSPORT_STATUS_USB_TX_BUSY = (1UL << 4),
    AD_BINARY_TRANSPORT_STATUS_CAN_TX_BUSY = (1UL << 5),
    AD_BINARY_TRANSPORT_STATUS_LAST_DROPPED = (1UL << 6)
} AD_BinaryTransportStatusFlag_t;

typedef struct
{
    uint8_t initialized;
    uint8_t host_protocol;
    uint8_t binary_transport;
    uint8_t usb_configured;
    uint8_t usb_tx_busy;
    uint8_t can_started;
    uint8_t can_tx_busy;
    uint8_t reserved;
    uint32_t status_flags;
    uint32_t period_ms;
    uint32_t tx_packets;
    uint32_t dropped_packets;
    uint32_t usb_packets;
    uint32_t can_packets;
    uint32_t last_tx_tick_ms;
    uint32_t last_size_bytes;
} AD_BinaryTransportState_t;

void AD_BinaryTransport_Init(void);
void AD_BinaryTransport_Loop(void);

uint8_t AD_BinaryTransport_IsProtocolValid(uint16_t protocol);
uint8_t AD_BinaryTransport_IsTransportValid(uint16_t transport);
uint8_t AD_BinaryTransport_SetProtocol(uint16_t protocol);
uint8_t AD_BinaryTransport_SetTransport(uint16_t transport);
uint8_t AD_BinaryTransport_GetProtocol(void);
uint8_t AD_BinaryTransport_GetTransport(void);
const AD_BinaryTransportState_t* AD_BinaryTransport_GetState(void);

#endif
