Skip to content

Commit

Permalink
!feat: working on errors
Browse files Browse the repository at this point in the history
  • Loading branch information
charliekush committed Aug 2, 2024
1 parent e42fc03 commit 22eddf5
Show file tree
Hide file tree
Showing 15 changed files with 192 additions and 151 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,18 @@ include_directories(../external/googletest/googletest/include src/ tests)
set(GTEST_SOURCE_FILES
tests/scheduler_test_system.cpp
tests/test_ensembles.cpp
src/antara_scheduler.cpp
src/charlie_scheduler.cpp
tests/fixed_google_tests.cpp
tests/file_google_tests.cpp
tests/overlap_google_tests.cpp
#tests/overlap_google_tests.cpp
tests/scheduler_test_flog.cpp
)
set(EXAMINE_BEHAVIOR_SOURCE_FILES
tests/scheduler_test_system.cpp
tests/test_ensembles.cpp
src/charlie_scheduler.cpp
src/antara_scheduler.cpp
tests/scheduler_test_flog.cpp
tests/examine_behavior.cpp
)
Expand Down
2 changes: 1 addition & 1 deletion src/abstractScheduler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class AbstractScheduler {
* @return 0 if successful, otherwise error code to be defined by
* implementation
*/
virtual int getNextTask(const DeploymentSchedule_t* p_next_task,
virtual int getNextTask(DeploymentSchedule_t** p_next_task,
std::uint32_t* p_next_runtime,
std::uint32_t current_time) = 0;
};
Expand Down
137 changes: 66 additions & 71 deletions src/antara_scheduler.cpp
Original file line number Diff line number Diff line change
@@ -1,98 +1,93 @@
/**
* @file scheduler.cpp
* @brief Contains definitions for functions defined in @ref scheduler.hpp
* @author Charlie Kushelevsky ([email protected])
* @version 1
*/

#ifndef TEST_VERSION
#include "Particle.h"
#else
#include "scheduler_test_system.hpp"
#endif
#include "cli/conio.hpp"

#include "ensembles.hpp"
#include "antara_scheduler.hpp"
#include "consts.hpp"
#include "product.hpp"
#include <cli/flog.hpp>
#include <string.h>


#if SCHEDULER_VERSION == ANTARA_VERSION
Scheduler:: Scheduler(DeploymentSchedule_t schedule[]){
Scheduler::Scheduler(DeploymentSchedule_t schedule[], int numTasks)
{
SF_OSAL_printf("Using Antara's Version\n");
tasks=schedule;

int i=0;
for(; tasks[i].measure; i++){
if(i=0){
tasks[i].startDelay=0;
}else {
if(i!=0){
tasks[i].startDelay=(tasks[i-1].startDelay)+(tasks[i-1].maxDuration);
tasks = schedule;
this->numTasks = numTasks;

for (int i = 0; i < numTasks; i++)
{
if (i == 0)
{
tasks[i].startDelay = 0;
}
else
{
tasks[i].startDelay = tasks[i - 1].startDelay + tasks[i - 1].maxDuration;
}

}
int numTasks=i-1;

}


int Scheduler:: getNextTask(const DeploymentSchedule_t* p_next_task, std::uint32_t* p_next_runtime, std::uint32_t current_time){

//check in reverse order, return if possible
int i=numTasks;

while(i>0){
bool canSet=true;
int runTime=tasks[i].nextRunTime;
int delay=current_time-tasks[i].nextRunTime;
if(delay>0){
runTime=current_time;
}else{
delay=0;
}
if(delay==tasks[i].maxDelay){
p_next_task=&(tasks[i]);
tasks[i].nextRunTime+=delay+tasks[i].ensembleInterval;
return runTime;
}
int completion=runTime+tasks[i].ensembleInterval;
int j=0;
while(j<i && canSet){
if(tasks[j].nextRunTime<completion){
canSet=false;
}
j++;

}
if(canSet){
p_next_task=&(tasks[i]);
tasks[i].nextRunTime+=delay+tasks[i].ensembleInterval;
return runTime;

int Scheduler::getNextTask(DeploymentSchedule_t **p_next_task, std::uint32_t *p_next_runtime, std::uint32_t current_time)
{
int i = numTasks - 1;

while (i >= 0)
{
bool canSet = true;
int runTime = tasks[i].nextRunTime;
int delay = current_time - tasks[i].nextRunTime;
if (delay > 0)
{
runTime = current_time;
}
else
{
delay = 0;
}
if (delay == tasks[i].maxDelay)
{
*p_next_task = &(tasks[i]);
tasks[i].nextRunTime += delay + tasks[i].ensembleInterval;
return runTime;
}
int completion = runTime + tasks[i].ensembleInterval;
int j = 0;
while (j < i && canSet)
{
if (tasks[j].nextRunTime < completion)
{
canSet = false;
}


i--;

j++;
}
if (canSet)
{
*p_next_task = &(tasks[i]);
tasks[i].nextRunTime += delay + tasks[i].ensembleInterval;
return runTime;
}

p_next_task=&(tasks[0]);
int runTime=tasks[0].nextRunTime;
int delay=current_time-(tasks[0].nextRunTime);
if(delay>0){
runTime=current_time;
}else{
delay=0;
}
tasks[0].nextRunTime+=delay+tasks[0].ensembleInterval;

return runTime;
i--;
}

*p_next_task = &(tasks[0]);
int runTime = tasks[0].nextRunTime;
int delay = current_time - (tasks[0].nextRunTime);
if (delay > 0)
{
runTime = current_time;
}
else
{
delay = 0;
}
tasks[0].nextRunTime += delay + tasks[0].ensembleInterval;

return runTime;
}

#endif
42 changes: 33 additions & 9 deletions src/antara_scheduler.hpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,37 @@
#include "product.hpp"
#if SCHEDULER_VERSION == ANTARA_VERSION
#ifndef __SCHEDULER__HPP__
#define __SCHEDULER__HPP__
#include "abstractScheduler.hpp"
#include "deploymentSchedule.hpp"
struct DeploymentSchedule_
{
// Ensemble properties
int priority;
void (*measure)(); //!< measurement function
void (*init)(); //!< initialization function
// EnsembleProccess process; //!< processing function
uint32_t startDelay; //!< delay after deployment start
uint32_t ensembleInterval; //!< time between ensembles
// void* pData; //!< Buffer to store measurements temporarily
uint32_t maxDuration; //!< store max running time of measurement
char taskName; //!< task name of ensemble
uint32_t maxDelay;

class Scheduler : public AbstractScheduler{
// State Information
uint32_t nextRunTime;
};

DeploymentSchedule_t * tasks;
int numTasks;
public:
Scheduler(DeploymentSchedule_t schedule[]);
int getNextTask(const DeploymentSchedule_t* p_next_task,
std::uint32_t* p_next_runtime,
std::uint32_t current_time);
class Scheduler : public AbstractScheduler
{
DeploymentSchedule_t *tasks;
int numTasks;

};
public:
Scheduler(DeploymentSchedule_t schedule[], int numTasks);
int getNextTask(DeploymentSchedule_t **p_next_task,
std::uint32_t *p_next_runtime,
std::uint32_t current_time);
};
#endif
#endif
8 changes: 3 additions & 5 deletions src/charlie_scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void Scheduler::initializeScheduler()
* TASK_SEARCH_FAIL otherwise.
*/

int Scheduler::getNextTask(const DeploymentSchedule_t* p_nextEvent,
int Scheduler::getNextTask(DeploymentSchedule_t** p_nextEvent,
system_tick_t* p_nextTime,
system_tick_t currentTime)
{
Expand Down Expand Up @@ -149,7 +149,7 @@ int Scheduler::getNextTask(const DeploymentSchedule_t* p_nextEvent,
nextStartTime = nextInterval;
}

if (willOverlap(idx, currentTime, nextStartTime))
if (!willOverlap(idx, currentTime, nextStartTime))
{
s.nextRunTime = nextStartTime;
if (nextStartTime < minNextTime)
Expand Down Expand Up @@ -186,11 +186,9 @@ int Scheduler::getNextTask(const DeploymentSchedule_t* p_nextEvent,
// Increments ensemble variable lastMeasurementTime measurementCount
nextEvent->state.measurementCount++;
// Sets next event pointer to the next event
p_nextEvent = nextEvent;
*p_nextEvent = nextEvent;
// Sets next time pointer to the next event time
*p_nextTime = minNextTime;
SF_OSAL_printf("next event: %s\n", nextEvent->taskName);
SF_OSAL_printf("p_nextEvent: %s\n", p_nextEvent->taskName);
return SUCCESS;
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/charlie_scheduler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
* @brief Header file for scheduler defined in @ref scheduler.cpp
* @version 1
*/
#ifndef __SCHEDULER__HPP_
#define __SCHEDULER__HPP_
#if SCHEDULER_VERSION == CHARLIE_VERSION
#ifndef __SCHEDULER__HPP__
#define __SCHEDULER__HPP__

#include <stddef.h>
#include <cstdint>
Expand Down Expand Up @@ -117,7 +118,7 @@ class Scheduler : public AbstractScheduler {
* @param nextStartTime The proposed start time of the current task.
* @return True if there is an overlap with another task; false otherwise.
*/
int getNextTask(const DeploymentSchedule_t* p_next_task,
int getNextTask(DeploymentSchedule_t** p_next_task,
uint32_t* p_next_runtime,
uint32_t current_time);

Expand All @@ -144,3 +145,4 @@ typedef enum error_

#endif //__SCHEDULER__HPP_

#endif
4 changes: 4 additions & 0 deletions src/ensembles.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
#ifndef __ENSEMBLES_HPP__
#define __ENSEMBLES_HPP__

#if SCHEDULER_VERSION == CHARLIE_VERSION
#include "charlie_scheduler.hpp"
#else
#include "antara_scheduler.hpp"
#endif

#endif
2 changes: 1 addition & 1 deletion src/product.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@
#define CHARLIE_VERSION 0
#define ANTARA_VERSION 1
#ifndef SCHEDULER_VERSION
#define SCHEDULER_VERSION CHARLIE_VERSION
#define SCHEDULER_VERSION ANTARA_VERSION
#endif


Expand Down
6 changes: 5 additions & 1 deletion src/rideTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@
#include "sleepTask.hpp"
#include "util.hpp"
#include "vers.hpp"
#include "charlie_scheduler.hpp"
#include "ensembles.hpp"
#include "cli/flog.hpp"
#if SCHEDULER_VERSION == CHARLIE_VERSION
#include "charlie_scheduler.hpp"
#else
#include "antara_scheduler.hpp"
#endif

/**
* @brief creates file name for log
Expand Down
13 changes: 9 additions & 4 deletions tests/examine_behavior.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class ExamineBehavior

while (millis() < input.end)
{
scheduler->getNextTask(nextEvent, &nextEventTime,
scheduler->getNextTask(&nextEvent, &nextEventTime,
millis());
uint32_t beforeDelay = 0;
uint32_t afterDelay = 0;
Expand Down Expand Up @@ -326,8 +326,12 @@ class ExamineBehavior
std::vector<std::string> filesInDir;
DIR* dirp = opendir(directory.c_str());
struct dirent* dp;
while ((dp = readdir(dirp)) != nullptr) {
if (dp->d_type == DT_REG) {
dp = readdir(dirp);
std::cout << directory << "\n";

while (dp != nullptr) {
if (dp->d_type == DT_REG) {
std::cout << directory + "/" + std::string(dp->d_name) << "\n";
filesInDir.push_back(directory + "/" + std::string(dp->d_name));
}
}
Expand All @@ -337,7 +341,8 @@ class ExamineBehavior
void runTests()
{
SetUpTestSuite();
std::vector<std::string> filesInDir = GetFilesInDirectory("no_check_inputs/");
std::vector<std::string> filesInDir = GetFilesInDirectory(
"tests/no_check_inputs/");
for (const auto& file : filesInDir)
{
SetUp(file);
Expand Down
5 changes: 3 additions & 2 deletions tests/file_google_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class SchedulerTestsFromFiles : public ::testing::TestWithParam<std::string>
void runNextEvent()
{

scheduler->getNextTask(nextEvent,
scheduler->getNextTask(&nextEvent,
&nextEventTime,
millis());
ASSERT_NE(nextEvent, nullptr) << "Scheduler returned nullptr.";
Expand Down Expand Up @@ -166,11 +166,12 @@ class SchedulerTestsFromFiles : public ::testing::TestWithParam<std::string>
}
e = { nullptr, nullptr, 0, 0, 0, 0, 0, "",{0} };
deploymentSchedule.emplace_back(e);
scheduler = std::make_unique<Scheduler>(deploymentSchedule.data());
scheduler->initializeScheduler();

while(millis() < input.end)
{
scheduler->getNextTask(nextEvent,&nextEventTime,
scheduler->getNextTask(&nextEvent,&nextEventTime,
millis());
uint32_t beforeDelay = 0;
uint32_t afterDelay = 0;
Expand Down
Loading

0 comments on commit 22eddf5

Please sign in to comment.