From b7d26d1ab53e0465668be90c94f8b06a5e701359 Mon Sep 17 00:00:00 2001
From: Dimitrios Stoupis <dimitris.apple@gmail.com>
Date: Wed, 13 Mar 2019 21:21:36 +0000
Subject: [PATCH] Almost done with the timeshift function

---
 src/Services/TimeBasedSchedulingService.cpp | 30 ++++++++++++---------
 1 file changed, 18 insertions(+), 12 deletions(-)

diff --git a/src/Services/TimeBasedSchedulingService.cpp b/src/Services/TimeBasedSchedulingService.cpp
index f487d64d..ffa1e794 100644
--- a/src/Services/TimeBasedSchedulingService.cpp
+++ b/src/Services/TimeBasedSchedulingService.cpp
@@ -44,7 +44,7 @@ void TimeBasedSchedulingService::insertActivities(Message &request) {
 		// Temporary definitions until the Time helper is ready
 		uint32_t releaseTime = 0; // Temporary release time
 		uint32_t currentTime = 50; // Temporary current time
-		// Message receivedPacket; // Temporary message field
+		Message receivedTCPacket; // Temporary message field
 
 		if ((currentNumberOfActivities >= MAX_NUMBER_OF_ACTIVITIES) || (releaseTime <
 		                                                                (currentTime +
@@ -52,7 +52,7 @@ void TimeBasedSchedulingService::insertActivities(Message &request) {
 			// todo: Send a failed start of execution
 		} else {
 			ScheduledActivity newActivity;
-			// newActivity.request = receivedTCPacket;
+			newActivity.request = receivedTCPacket;
 			newActivity.requestReleaseTime = releaseTime;
 
 			scheduledActivities.push_back(newActivity); // Insert the new activity into the schedule
@@ -71,15 +71,20 @@ void TimeBasedSchedulingService::timeShiftAllActivities(Message &request) {
 	assert(request.serviceType == 11);
 	assert(request.messageType == 15);
 
-	uint16_t iterationCount = request.readUint16(); // Get the iteration count, (N)
-	for (std::size_t i = 0; i < iterationCount; i++) {
-		uint32_t relativeOffset = request.readUint32();
-		// todo: define the enumeration types for the source ID and application process ID
-		uint16_t receivedSequenceCount = request.readUint16(); // Get the sequence count
-		/*
-		* Perform a search in the vector containing the activity definitions to find a match for
-		* the received parameters
-		*/
+	// Temporary variables
+	uint32_t current_time = 0;
+
+	const auto[earliestRelease, latestRelease] = etl::minmax_element(scheduledActivities.front()
+		                                                                 .requestReleaseTime,
+	                                                                 scheduledActivities.back().requestReleaseTime);
+
+	uint32_t relativeOffset = request.readUint32(); // Get the relative offset
+	if ((earliestRelease + relativeOffset) < (current_time + TIME_MARGIN_FOR_ACTIVATION)) {
+		// todo: generate a failed start of execution error
+	} else {
+		for (auto &activity : scheduledActivities) {
+			activity.requestReleaseTime += relativeOffset; // Time shift each activity
+		}
 	}
 }
 
@@ -122,12 +127,13 @@ void TimeBasedSchedulingService::detailReportAllActivities(Message &request) {
 
 	// todo: add an extra check for the vector size to make sure it matches with
 	//  currentNumberOfActivities?
-	for (auto const& activity : scheduledActivities) {
+	for (auto const &activity : scheduledActivities) {
 		// Create the report message object of telemetry message subtype 10 for each activity
 		Message report = createTM(10);
 		// todo: append sub-schedule and group ID if they are defined
 		// todo: append the release time of the current activity "activity.requestReleaseTime;"
 		// todo: append the request contained in the activity "activity.request;"
+		// todo: important todo, implement append TC packet in the Message header
 		storeMessage(report); // Save the report
 		request.resetRead(); // todo: define if this statement is required
 	}
-- 
GitLab