
#ifndef ETHERCAT_MASTER_DARRA_GATEWAY_H
#define ETHERCAT_MASTER_DARRA_GATEWAY_H

#pragma once

#include "../ethercat_types.h"
#include <stdint.h>
#include <stdbool.h>

#ifdef __cplusplus
extern "C" {
#endif

#define GW_BASE_PORT  0x88A4

typedef struct {
    uint64_t rxPackets;
    uint64_t txPackets;
    uint64_t rxBytes;
    uint64_t txBytes;
    uint64_t masterOdRequests;
    uint64_t slaveRequests;
    uint64_t dropMalformed;
    uint64_t dropTimeout;
} DarraGatewayStats;

typedef int (*DarraGatewayMasterOdCallback)(uint8_t mbxType,
                                             const uint8_t* request, int requestLen,
                                             uint8_t* responseBuf, int responseBufSize,
                                             void* user);

typedef int (*DarraGatewaySlaveCallback)(uint16_t slaveAddr, uint8_t mbxType,
                                          const uint8_t* request, int requestLen,
                                          uint8_t* responseBuf, int responseBufSize,
                                          void* user);

typedef struct DarraGateway DarraGateway;

DarraGateway* gateway_create(uint16_t masterIndex);

void gateway_destroy(DarraGateway* gw);

int gateway_set_port(DarraGateway* gw, uint16_t port);

uint16_t gateway_get_port(const DarraGateway* gw);

void gateway_set_master_od_callback(DarraGateway* gw,
                                           DarraGatewayMasterOdCallback cb, void* user);

void gateway_set_slave_callback(DarraGateway* gw,
                                       DarraGatewaySlaveCallback cb, void* user);

int gateway_start(DarraGateway* gw);

void gateway_stop(DarraGateway* gw);

bool gateway_is_running(const DarraGateway* gw);

void gateway_get_statistics(const DarraGateway* gw, DarraGatewayStats* outStats);

void gateway_reset_statistics(DarraGateway* gw);

#ifdef __cplusplus
}
#endif

#endif
