30#ifndef DW_FRAMEWORK_SENSOR_DRAINER_TEMPLATE_HPP_
31#define DW_FRAMEWORK_SENSOR_DRAINER_TEMPLATE_HPP_
33#include <dw/sensors/Sensors.h>
34#include <dw/sensors/radar/Radar.h>
35#include <dw/sensors/canbus/CAN.h>
36#include <dw/core/base/Types.h>
38#if defined(DW_SDK_BUILD_EXPERIMENTAL) && defined(LINUX)
39#include <dwexperimental/sensors/lockstep/Lockstep.h>
40#include <dwexperimental/sensors/Sensors.h>
66template <
typename ProcessedDataType,
typename ReadProcessedDataFunc,
typename DataSourceType>
70 static constexpr char LOG_TAG[] =
"dwBaseDrainerTemplate";
109 virtual dwStatus
getNextData(ProcessedDataType* outFrame, dwTime_t timeout)
117 FRWK_LOGE <<
"getNextData: readNextData error!" << Logger::State::endl;
125 virtual dwStatus
tryRead(ProcessedDataType* outFrame,
126 dwTime_t& latestTimestamp,
128 bool isDroppingData =
false)
146 FRWK_LOGE <<
"tryRead: getNextData error!" << Logger::State::endl;
159 latestTimestamp = nextTime;
199 if (timeToNextData >= timeout)
207 FRWK_LOGD <<
"isVirtualDataReady: timeToNextData is larger than m_blockingTimeout, must read some data. "
208 <<
"timeToNextData: " << timeToNextData <<
", dataTime: " << dataTime << Logger::State::endl;
215 if (timeToNextData <= 0)
234 dwTime_t& timestampOutput,
235 dwTime_t& nextTimestampOutput,
236 dwTime_t virtualSyncTime)
240 dwStatus
status = DW_SUCCESS;
245 int32_t drainCount = 0;
261#if defined(DW_SDK_BUILD_EXPERIMENTAL) && defined(LINUX)
268 FRWK_LOGE <<
m_name <<
" blockingTimeout not sufficient to successfully waitForNewData. No data available!" << Logger::State::endl;
279 nextTimestampOutput = 0;
281 if (nextTimeStatus != DW_SUCCESS)
283 FRWK_LOGD <<
m_name <<
" Failed to get next timestamp: " << dwGetStatusName(nextTimeStatus);
290 virtual dwStatus
readProcessedData(ProcessedDataType* outFrame, dwTime_t& latestTimestamp, dwTime_t timeout,
bool isDroppingData =
false) = 0;
315template <
typename ProcessedDataType,
typename ReadProcessedDataFunc>
319 static constexpr char LOG_TAG[] =
"dwSensorDrainerTemplate";
323 :
dwBaseDrainerTemplate<ProcessedDataType, ReadProcessedDataFunc, dwSensorHandle_t>(params, std::move(readProcessedDataFunc), hsensor)
331 dwTime_t timeout)
override
333#if defined(DW_SDK_BUILD_EXPERIMENTAL) && defined(LINUX)
337 if (nextStatus == DW_SUCCESS || nextStatus == DW_END_OF_STREAM)
350 if (res != DW_SUCCESS)
366 dwTime_t& latestTimestamp,
368 bool isDroppingData =
false)
override
371 if (
status == DW_END_OF_STREAM)
397 if (!readCb(dataEvent))
399 FRWK_LOGW <<
"replayDroppedFrames: Trace cannot be read." << Logger::State::endl;
400 return DW_END_OF_STREAM;
407 dwTime_t timestampOutput{};
409 if (
status == DW_END_OF_STREAM)
413 else if (
status == DW_SUCCESS)
417 if (timestampOutput != dataEvent.
timestamp)
419 FRWK_LOGE <<
"replayDroppedFrames: Data/trace mismatch: current: " << timestampOutput <<
" recorded: " << dataEvent.
timestamp << Logger::State::endl;
420 throw Exception(DW_FAILURE,
"dwSensorDrainerTemplate: replayDroppedFrames: data/trace mismatch.");
425 FRWK_LOGE <<
"replayDroppedFrames: Cannot read next data." << Logger::State::endl;
426 throw Exception(DW_FAILURE,
"dwSensorDrainerTemplate: replayDroppedFrames: cannot read next data.");
429 }
while (isDropEvent);
440 dwTime_t& timestampOutput,
449 if (
status == DW_END_OF_STREAM)
473 FRWK_LOGE <<
"replayProcessedData: Cannot read next data." << Logger::State::endl;
474 throw Exception(DW_FAILURE,
"dwSensorDrainerTemplate: replayProcessedData: cannot read next data.");
478 if (timestampOutput != de.timestamp)
480 FRWK_LOGE <<
"replayProcessedData: Data/trace mismatch." << Logger::State::endl;
481 throw Exception(DW_FAILURE,
"dwSensorDrainerTemplate: replayProcessedData: data/trace mismatch.");
486 FRWK_LOGE <<
"replayProcessedData: UnhandledEventType." << Logger::State::endl;
487 throw Exception(DW_FAILURE,
"dwSensorDrainerTemplate: unhandled event type");
493#if defined(DW_SDK_BUILD_EXPERIMENTAL) && defined(LINUX)
498 bool isDataAvailable =
false;
501 char8_t const* protocol;
503 throw Exception(DW_FAILURE,
"dwSensorDrainerTemplate: can't get the data available flag from:", protocol,
" sensor");
506 if (!isDataAvailable &&
status != DW_SUCCESS)
523template <
typename ProcessedDataType,
typename ReadProcessedDataFunc,
typename DataSourceType>
526template <
typename ProcessedDataType,
typename ReadProcessedDataFunc>
DataEventType dataEventType
FixedString< MAX_NAME_LEN > Name_t
ISensorNode::DataEventReadCallback DataEventReadCallback
virtual dwStatus tryRead(ProcessedDataType *outFrame, dwTime_t &latestTimestamp, dwTime_t timeout, bool isDroppingData=false)
static constexpr int32_t DRAIN_SENSOR_DATA_COUNT_MAX
void setOnDataDropped(OnDataDropped onDataDropped)
dwTime_t m_virtualSyncTime
dw::core::Function< void(dwTime_t const)> OnDataDropped
virtual dwStatus getNextTimestamp(dwTime_t ×tamp, dwTime_t timeout)
virtual dwTime_t getReadTimeout() const
virtual dwStatus getNextData(ProcessedDataType *outFrame, dwTime_t timeout)
dwTime_t m_nonBlockingTimeout
dwBaseDrainerTemplate(dwSensorDrainerParams params, std::unique_ptr< ReadProcessedDataFunc > readProcessedDataFunc, DataSourceType dataSource)
virtual dwStatus drainProcessedData(ProcessedDataType *processedOutput, dwTime_t ×tampOutput, dwTime_t &nextTimestampOutput, dwTime_t virtualSyncTime)
DataSourceType m_dataSource
static constexpr char LOG_TAG[]
virtual void setVirtualSyncTime(dwTime_t virtualSyncTime)
virtual bool isVirtualDataReady(dwTime_t dataTime, dwTime_t timeout)
dwTime_t m_blockingTimeout
virtual void isLockstepDataAvailable(dwStatus &status, dwTime_t &readTimeout)
std::unique_ptr< ReadProcessedDataFunc > m_readProcessedDataFunc
virtual dwStatus readProcessedData(ProcessedDataType *outFrame, dwTime_t &latestTimestamp, dwTime_t timeout, bool isDroppingData=false)=0
static constexpr dwTime_t REPLAY_SENSOR_READ_TIMEOUT
OnDataDropped m_onDataDropped
dwStatus readProcessedData(ProcessedDataType *outFrame, dwTime_t &latestTimestamp, dwTime_t timeout, bool isDroppingData=false) override
dwSensorDrainerTemplate(dwSensorDrainerParams params, std::unique_ptr< ReadProcessedDataFunc > readProcessedDataFunc, dwSensorHandle_t hsensor)
virtual dwStatus replayProcessedData(ProcessedDataType *processedOutput, dwTime_t ×tampOutput, SensorNode::DataEventReadCallback readCb)
static constexpr char LOG_TAG[]
virtual ~dwSensorDrainerTemplate()=default
virtual dwStatus reset() override
dwStatus getNextTimestamp(dwTime_t ×tamp, dwTime_t timeout) override
dwStatus replayDroppedFrames(ProcessedDataType *processedOutput, SensorNode::DataEvent &dataEvent, SensorNode::DataEventReadCallback readCb)
dwCalibrationStatus status
dwTime_t nonBlockingTimeout