Skip to content

Commit

Permalink
Allowing to forge unhorodated frames' timestamp based on system time
Browse files Browse the repository at this point in the history
  • Loading branch information
lains committed May 15, 2024
1 parent 38fb43c commit 6d6fbae
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
17 changes: 14 additions & 3 deletions inc/domain/TicFrameParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ class TicFrameParser {
public:
/* Types */
typedef void(*FOnNewPowerDataFunc)(const TicEvaluatedPower& power, const TimeOfDay& timestamp, unsigned int frameId, void* context); /*!< The prototype of callbacks invoked on new power data */
typedef void(*FOnDayOverFunc)(void* context); /*!< The prototype of callbacks invoked we we switch to the next day */
typedef void(*FOnDayOverFunc)(void* context); /*!< The prototype of callbacks invoked when we switch to the next day */
typedef TimeOfDay(*FCurrentTimerGetterFunc)(void* context); /*!< The prototype of function to invoked to get the current TimeOfDay */

/* Methods */
/**
Expand All @@ -86,10 +87,18 @@ class TicFrameParser {
/**
* @brief Set the method to invoke when we detect a switch to the next day
*
* @param power The method to invoke
* @param dayOverFunc The method to invoke
* @param context A context provided to the method
*/
void onDayOverInvoke(FOnDayOverFunc dayOverFunc, void* context);
void invokeWhenDayOver(FOnDayOverFunc dayOverFunc, void* context);

/**
* @brief Set the method to invoke to get the current TimeOfDay
*
* @param currentTimeGetter The method to invoke
* @param context A context provided to the method
*/
void setCurrentTimeGetter(FCurrentTimerGetterFunc currentTimeGetter, void* context);

protected:
void onNewMeasurementAvailable();
Expand Down Expand Up @@ -215,6 +224,8 @@ class TicFrameParser {
void* onNewPowerDataContext; /*!< A context pointer passed to onNewFrameBytes() and onFrameComplete() at invokation */
FOnDayOverFunc onDayOverFunc; /*!< Pointer to a function invoked when we detect switching to the next day */
void* onDayOverFuncContext; /*!< A context pointer passed as argument to the above method */
FCurrentTimerGetterFunc currentTimeGetterFunc; /*!< Pointer to a function to invoke to get the current TimeOfDay */
void* currentTimeGetterFuncContext; /*!< A context pointer passed as argument to the above method */
unsigned int nbFramesParsed; /*!< Total number of complete frames parsed */
TIC::DatasetExtractor de; /*!< The encapsulated dataset extractor instance (programmed to call us back on newly decoded datasets) */
TicMeasurements lastFrameMeasurements; /*!< Gathers all interesting measurement of the last frame */
Expand Down
23 changes: 13 additions & 10 deletions src/domain/TicFrameParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,10 @@ void std::swap(TicMeasurements& first, TicMeasurements& second) {
TicFrameParser::TicFrameParser(FOnNewPowerDataFunc onNewPowerData, void* onNewPowerDataContext) :
onNewPowerData(onNewPowerData),
onNewPowerDataContext(onNewPowerDataContext),
onDayOverFunc([](void* context) { }),
onDayOverFunc(nullptr),
onDayOverFuncContext(nullptr),
currentTimeGetterFunc(nullptr),
currentTimeGetterFuncContext(nullptr),
nbFramesParsed(0),
de(ticFrameParserUnWrapDatasetExtractor, this),
lastFrameMeasurements(),
Expand Down Expand Up @@ -220,15 +222,11 @@ void TicFrameParser::guessFrameArrivalTime() {
Stm32DebugOutput::get().send("\n");
#endif
}
unsigned int emulatedSecond = (this->nbFramesParsed * 3) % 60; /* Assume 1 historical TIC frame every 3 seconds */
unsigned int emulatedHorodateRemainder = (this->nbFramesParsed / 20); /* Counts total remainder as minutes */
unsigned int emulatedMinute = emulatedHorodateRemainder % 60;
emulatedHorodateRemainder = emulatedHorodateRemainder / 60; /* Now count total remainder as hours */
unsigned int emulatedHour = emulatedHorodateRemainder % 24;
/* Note: we discard days and month for now */
this->lastFrameMeasurements.timestamp = TimeOfDay(emulatedHour, emulatedMinute, emulatedSecond);
if (this->currentTimeGetterFunc) {
this->lastFrameMeasurements.timestamp = this->currentTimeGetterFunc(this->currentTimeGetterFuncContext);
}
#ifdef EMBEDDED_DEBUG_CONSOLE
Stm32DebugOutput::get().send("Injecting timestamp in historical frame: ");
Stm32DebugOutput::get().send("Using the following systemtime instead of (missing) frame horodate: ");
Stm32DebugOutput::get().send(static_cast<unsigned int>(this->lastFrameMeasurements.timestamp.hour));
Stm32DebugOutput::get().send(":");
Stm32DebugOutput::get().send(static_cast<unsigned int>(this->lastFrameMeasurements.timestamp.minute));
Expand All @@ -238,11 +236,16 @@ void TicFrameParser::guessFrameArrivalTime() {
#endif
}

void TicFrameParser::onDayOverInvoke(FOnDayOverFunc dayOverFunc, void* context) {
void TicFrameParser::invokeWhenDayOver(FOnDayOverFunc dayOverFunc, void* context) {
this->onDayOverFunc = dayOverFunc;
this->onDayOverFuncContext = context;
}

void TicFrameParser::setCurrentTimeGetter(FCurrentTimerGetterFunc currentTimeGetter, void* context) {
this->currentTimeGetterFunc = currentTimeGetter;
this->currentTimeGetterFuncContext = context;
}

void TicFrameParser::onRefPowerInfo(uint32_t power) {
//FIXME: Todo
}
Expand Down
7 changes: 6 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,12 @@ int main(void) {
TicProcessingContext* ticContext = static_cast<TicProcessingContext*>(context);
ticContext->currentTime.startNewDayAtMidnight();
};
ticParser.onDayOverInvoke(performAtMidnight, static_cast<void*>(&ticContext));
ticParser.invokeWhenDayOver(performAtMidnight, static_cast<void*>(&ticContext));
auto currentTimeGetter = [](void* context) -> TimeOfDay {
TicProcessingContext* ticContext = static_cast<TicProcessingContext*>(context);
return ticContext->currentTime.time;
};
ticParser.setCurrentTimeGetter(currentTimeGetter, static_cast<void*>(&ticContext));

powerHistory.setContext(&ticContext);

Expand Down

0 comments on commit 6d6fbae

Please sign in to comment.