12 #include <libtrap/trap.h>
26 uint8_t inputInterfaceID,
27 uint8_t outputInterfaceID)
29 , m_inputInterfaceID(inputInterfaceID)
30 , m_outputInterfaceID(outputInterfaceID)
31 , m_prioritizedDataPointer(nullptr)
32 , m_sendEoFonExit(true)
33 , m_EoFOnNextReceive(false)
40 const void* receivedData;
41 uint16_t dataSize = 0;
54 if (errorCode == TRAP_E_TIMEOUT) {
57 if (errorCode == TRAP_E_FORMAT_CHANGED) {
77 if (errorCode == TRAP_E_OK) {
80 if (errorCode == TRAP_E_NOT_INITIALIZED) {
81 throw std::runtime_error(
82 "UnirecBidirectionalInterface::receive() has failed. Trap interface is not "
85 if (errorCode == TRAP_E_TERMINATED) {
86 throw std::runtime_error(
87 "UnirecBidirectionalInterface::receive() has failed. Trap interface is terminated.");
89 if (errorCode == TRAP_E_NOT_SELECTED) {
90 throw std::runtime_error(
91 "UnirecBidirectionalInterface::receive() has failed. Interface ID out of range.");
93 throw std::runtime_error(
94 "UnirecBidirectionalInterface::receive() has failed. Return code: "
95 + std::to_string(errorCode) +
", msg: " + trap_last_error_msg);
101 = trap_set_required_fmt(
m_inputInterfaceID, TRAP_FMT_UNIREC, templateSpecification.c_str());
102 if (ret != TRAP_E_OK) {
103 throw std::runtime_error(
104 "UnirecBidirectionalInterface::setRequieredFormat() has failed. Unable to set required "
117 const char* spec =
nullptr;
120 if (ret != TRAP_E_OK) {
121 throw std::runtime_error(
122 "UnirecBidirectionalInterface::changeTemplate() has failed. Data format was not "
128 throw std::runtime_error(
129 "UnirecBidirectionalInterface::changeTemplate() has failed. Template could not be "
133 std::string specCopy = spec;
137 if (ret != TRAP_E_OK) {
138 throw std::runtime_error(
"UnirecBidirectionalInterface::changeTemplate() has failed.");
142 if (ret != TRAP_E_OK) {
143 throw std::runtime_error(
"UnirecBidirectionalInterface::changeTemplate() has failed.");
169 if (errorCode == TRAP_E_TIMEOUT) {
172 if (errorCode == TRAP_E_OK) {
175 if (errorCode == TRAP_E_NOT_INITIALIZED) {
176 throw std::runtime_error(
177 "UnirecBidirectionalInterface::send() has failed. Trap interface is not initialized.");
179 if (errorCode == TRAP_E_TERMINATED) {
180 throw std::runtime_error(
181 "UnirecBidirectionalInterface::send() has failed. Trap interface is terminated.");
183 if (errorCode == TRAP_E_BAD_IFC_INDEX) {
184 throw std::runtime_error(
185 "UnirecBidirectionalInterface::send() has failed. Interface ID out of range.");
187 throw std::runtime_error(
188 "UnirecBidirectionalInterface::send() has failed. Return code: " + std::to_string(errorCode)
189 +
", msg: " + trap_last_error_msg);
Defines a bidirectional interface for sending and receiving unirec records using the TRAP interface p...
An exception that is thrown when the end of the input stream is reached.
UnirecBidirectionalInterface(uint8_t inputInterfaceID, uint8_t outputInterfaceID)
void handleReceiveErrorCodes(int errorCode) const
bool handleSendErrorCodes(int errorCode) const
void sendFlush() const
Flushes any pending UniRec records in the Trap interface.
ur_template_t * m_template
UnirecRecord createUnirecRecord(size_t maxVariableFieldsSize=UR_MAX_SIZE)
Creates a new UniRec record with the specified maximum variable fields size.
void setSendAutoflushTimeout(int timeout)
Sets the autoflush timeout for the output Trap interface.
~UnirecBidirectionalInterface()
Destructor for the UnirecBidirectionalInterface class.
std::optional< UnirecRecordView > receive()
Receives data from the interface and returns an optional UnirecRecordView object.
bool isEoFReceived() const noexcept
void setRequieredFormat(const std::string &templateSpecification)
Sets the required Unirec format specification.
const void * m_prioritizedDataPointer
uint8_t m_inputInterfaceID
UnirecRecord m_unirecRecord
void doNotsendEoFOnExit()
Disables sending an end-of-file marker on exit.
bool send(UnirecRecord &unirecRecord) const
Sends a UniRec record through the Trap interface.
void setSendTimeout(int timeout)
Sets the send timeout for the Trap interface.
void setReceiveTimeout(int timeout)
Sets the receive timeout for the interface. This method sets the timeout for receiving UniRec records...
void changeTemplate()
Changes the Unirec template used by the bidirectional interface.
uint8_t m_outputInterfaceID
Provides a view into a UniRec record.
size_t size() const noexcept
Returns the size of the UniRec record.
const void * data() const noexcept
Returns a const pointer to the data of the UniRec record.
A class for working with UniRec records and their fields.
size_t size() const noexcept
Returns the size of the UniRec record.
const void * data() const noexcept
Returns a pointer to the data of the UniRec record.
ur_template_t * ur_define_fields_and_update_template(const char *ifc_data_fmt, ur_template_t *tmplt)
Defined new fields and expand an UniRec template Define new fields (function ur_define_set_of_fields)...
#define ur_set_output_template(ifc, tmplt)
Set UniRec template to ouput interface.
void ur_free_template(ur_template_t *tmplt)
Destroy UniRec template Free all memory allocated for a template created previously by ur_create_temp...
#define ur_set_input_template(ifc, tmplt)
Set UniRec template to input interface.