sitara_depot/components/free_rtos/ethernet_industry/eth_ecat_telegram.hpp

101 lines
2.3 KiB
C++

/*
* eth_ecat_telegram.hpp
*
* Created on: Jun 5, 2023
* Author: algin
*/
#ifndef FREE_RTOS_ETHERNET_INDUSTRY_ETH_ECAT_TELEGRAM_HPP_
#define FREE_RTOS_ETHERNET_INDUSTRY_ETH_ECAT_TELEGRAM_HPP_
#include "free_rtos/ethernet/eth.hpp"
#include "free_rtos/ethernet_industry/eth_ecat_datagram.hpp"
namespace free_rtos {
namespace telegram {
enum class EcatTelegramResult : uint16_t {
SUCCESS = 0,
BUSY,
WARNING_TRANSFER_ERROR,
WARNING_WKC_ERROR,
FATAL_ERROR
};
struct EcatDescription {
const char* string;
const size_t length;
};
struct EcatTelegramStatus {
uint16_t transfer_errors;
uint16_t expected_wkc_errors;
uint16_t attempts_exceeded_errors;
EcatTelegramResult result = EcatTelegramResult::SUCCESS;
const EcatDescription& get_description() {
return descriptions[static_cast<size_t>(result)];
}
private:
static const char SuccessString[];
static const char BusyString[];
static const char WarningTransferErrorString[];
static const char WarningWkcErrorString[];
static const char FatalErrorString[];
static const EcatDescription descriptions[];
};
class EcatTelegram : public Handler {
public:
EcatTelegram(Eth& eth)
: eth_{eth}
, tx_flow_{*eth.getTxFlowPtr()}
, eth_stack_{*eth.getEthStackPtr()} { }
const EcatTelegramStatus& get_status() {
return status_;
}
virtual int32_t Process(uint8_t *p_data, uint32_t len) override;
virtual uint32_t Sender(uint8_t *p_data, size_t scatter_segment) override;
void init(TEthMacPorts port_id) {
port_id_ = port_id;
eth_.getEthStackPtr()->Register(ETH_PROT_ECAT_LE, this);
}
bool transfer(datagram::IEcatDatagram& next);
bool transfer(queue::Queue<datagram::IEcatDatagram>& next);
private:
static constexpr uint32_t max_transfer_attempts_ = 3;
Eth& eth_;
EthTxFlowIface& tx_flow_;
EthStackIface& eth_stack_;
TEthMacPorts port_id_;
free_rtos::Semaphore rx_sem_;
queue::Queue<datagram::IEcatDatagram> datagram_queue_;
//datagram::IEcatDatagram *datagram_queue_{nullptr};
EcatTelegramStatus status_;
bool transfer();
uint8_t* pack(uint8_t *raw);
uint8_t* unpack(uint8_t *raw);
};
}
}
#endif /* FREE_RTOS_ETHERNET_INDUSTRY_ETH_ECAT_TELEGRAM_HPP_ */