Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature "ClearTrajectories();" command #228

Merged
merged 11 commits into from
Mar 13, 2020
2 changes: 2 additions & 0 deletions core/inc/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,8 @@ void UtilGetConfDirectoryPath(char* path, size_t pathLen);
void UtilGetTrajDirectoryPath(char* path, size_t pathLen);
void UtilGetGeofenceDirectoryPath(char* path, size_t pathLen);

int UtilDeleteTrajectoryFiles();

// File parsing functions
int UtilCheckTrajectoryFileFormat(const char *path, size_t pathLen);
int UtilParseTrajectoryFileHeader(char *headerLine, TrajectoryFileHeader * header);
Expand Down
44 changes: 26 additions & 18 deletions core/src/systemcontrol.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,9 @@ typedef enum {
InitializeScenario_0,
ConnectObject_0, DisconnectObject_0, GetServerParameterList_0, SetServerParameter_2, GetServerParameter_1,
DownloadFile_1, UploadFile_3, CheckFileDirectoryExist_1, GetRootDirectoryContent_0, GetDirectoryContent_1,
DeleteFileDirectory_1, CreateDirectory_1, GetTestOrigin_0, replay_1, control_0, Exit_0, start_ext_trigg_1,
nocommand
ClearTrajectories_0, DeleteFileDirectory_1, CreateDirectory_1, GetTestOrigin_0, replay_1, control_0,
Exit_0,
start_ext_trigg_1, nocommand
} SystemControlCommand_t;

const char *SystemControlCommandsArr[] = {
Expand All @@ -138,8 +139,9 @@ const char *SystemControlCommandsArr[] = {
"ConnectObject_0", "DisconnectObject_0", "GetServerParameterList_0", "SetServerParameter_2",
"GetServerParameter_1", "DownloadFile_1", "UploadFile_3", "CheckFileDirectoryExist_1",
"GetRootDirectoryContent_0", "GetDirectoryContent_1",
"DeleteFileDirectory_1", "CreateDirectory_1", "GetTestOrigin_0", "replay_1", "control_0", "Exit_0",
"start_ext_trigg_1"
"ClearTrajectories_0", "DeleteFileDirectory_1", "CreateDirectory_1", "GetTestOrigin_0", "replay_1",
"control_0",
"Exit_0", "start_ext_trigg_1"
};

const char *SystemControlStatesArr[] =
Expand Down Expand Up @@ -185,6 +187,7 @@ I32 SystemControlCheckFileDirectoryExist(C8 * ParameterName, C8 * ReturnValue, U
I32 SystemControlUploadFile(C8 * Path, C8 * FileSize, C8 * PacketSize, C8 * ReturnValue, U8 Debug);
I32 SystemControlReceiveRxData(I32 * sockfd, C8 * Path, C8 * FileSize, C8 * PacketSize, C8 * ReturnValue,
U8 Debug);
C8 SystemControlClearTrajectories();
I32 SystemControlDeleteFileDirectory(C8 * Path, C8 * ReturnValue, U8 Debug);
I32 SystemControlBuildFileContentInfo(C8 * Path, U8 Debug);
I32 SystemControlDestroyFileContentInfo(C8 * path);
Expand Down Expand Up @@ -731,26 +734,12 @@ void systemcontrol_task(TimeType * GPSTime, GSDType * GSD, LOG_LEVEL logLevel) {

I32 file_len = SystemControlBuildFileContentInfo("dir.info", 0);

/*
if (file_len > 0) printf("file contred Created\n");
char *traversingPointer = SystemControlDirectoryInfo.info_buffer;
for (int i = 0; i < file_len; i++) {
printf("0x%X\n", *traversingPointer);
traversingPointer++;
}
*/
SystemControlSendControlResponse(SYSTEM_CONTROL_RESPONSE_CODE_OK,
"SubGetDirectoryContent:",
SystemControlDirectoryInfo.info_buffer, file_len,
&ClientSocket, 0);

SystemControlDestroyFileContentInfo("dir.info");
/*
SystemControlBuildFileContentInfo("dir.info", ControlResponseBuffer, 0);

SystemControlSendFileContent(&ClientSocket, "dir.info", STR_SYSTEM_CONTROL_TX_PACKET_SIZE,
ControlResponseBuffer, REMOVE_FILE, 0);
*/
}

}
Expand All @@ -761,6 +750,15 @@ void systemcontrol_task(TimeType * GPSTime, GSDType * GSD, LOG_LEVEL logLevel) {
SystemControlCommand = Idle_0;
}
break;
case ClearTrajectories_0:
if (CurrentInputArgCount == CommandArgCount) {
SystemControlCommand = Idle_0;
memset(ControlResponseBuffer, 0, sizeof (ControlResponseBuffer));
*ControlResponseBuffer = SystemControlClearTrajectories();
SystemControlSendControlResponse(SYSTEM_CONTROL_RESPONSE_CODE_OK, "ClearTrajectories:",
ControlResponseBuffer, 1, &ClientSocket, 0);
}
break;
case DownloadFile_1:
if (CurrentInputArgCount == CommandArgCount) {
SystemControlCommand = Idle_0;
Expand Down Expand Up @@ -2062,6 +2060,16 @@ I32 SystemControlCheckFileDirectoryExist(C8 * ParameterName, C8 * ReturnValue, U
return 0;
}

/*!
* \brief SystemControlClearTrajectories Clears the trajectory folder on the machine
* \return Returns ::SUCCEDED_DELETE upon successfully deleting a file, otherwise ::FAILED_DELETE.
*/
C8 SystemControlClearTrajectories() {
if (UtilDeleteTrajectoryFiles() != 0) {
return FAILED_DELETE;
}
return SUCCEDED_DELETE;
}

I32 SystemControlDeleteFileDirectory(C8 * Path, C8 * ReturnValue, U8 Debug) {

Expand Down
50 changes: 49 additions & 1 deletion core/src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ static void CopyHTTPHeaderField(char *request, char *targetContainer, size_t tar
const char *fieldName);
static char rayFromPointIntersectsLine(double pointX, double pointY, double polyPointAX, double polyPointAY,
double polyPointBX, double polyPointBY);

static int deleteDirectoryContents(char *path, size_t pathLen);

void CopyHTTPHeaderField(char *request, char *targetContainer, size_t targetContainerSize,
const char *fieldName) {
Expand Down Expand Up @@ -177,6 +177,42 @@ void CopyHTTPHeaderField(char *request, char *targetContainer, size_t targetCont

}

/*!
* \brief deleteDirectoryContents Deletes the directory given in the parameter ::path
* \param path The path to the directory on the machine.
* \param pathLen The length of ::the path string.
* \return 0 if it could successfully delete file, non-zero if it could not.
*/
int deleteDirectoryContents(char *path, size_t pathLen) {
if (path == NULL) {
LogMessage(LOG_LEVEL_ERROR, "Path is null-pointer.");
errno = EINVAL;
return -1;
}
if (pathLen > MAX_FILE_PATH) {
LogMessage(LOG_LEVEL_ERROR, "Path variable too large to handle");
errno = EINVAL;
return -1;
}
// These are data types defined in the "dirent" header
DIR *theFolder = opendir(path);

if (theFolder == NULL) {
LogMessage(LOG_LEVEL_ERROR, "Path: %s could not be opened", path);
errno = ENOENT;
return -1;
}
struct dirent *next_file;
char filepath[MAX_FILE_PATH];

while ((next_file = readdir(theFolder)) != NULL) {
// build the path for each file in the folder
sprintf(filepath, "%s/%s", path, next_file->d_name);
remove(filepath);
}
closedir(theFolder);
return 0;
}

/*---------------------------------------------s---------------
-- Public functions
Expand Down Expand Up @@ -2347,6 +2383,18 @@ void UtilGetGeofenceDirectoryPath(char *path, size_t pathLen) {
strcat(path, "/");
}

/*!
* \brief UtilDeleteTrajectoryFiles finds the trajectory folder and deletes its contents
* \return returns 0 if succesfull if the trajectory folder now is empty. Non-zero values otherwise.
*/
int UtilDeleteTrajectoryFiles() {
char filePath[MAX_FILE_PATH] = { '\0' };
UtilGetTrajDirectoryPath(filePath, MAX_FILE_PATH);
if (filePath[0] == '\0')
return -1;
return deleteDirectoryContents(filePath, MAX_FILE_PATH);
}

/*!
* \brief UtilParseTrajectoryFileHeader Attempts to parse a line into a trajectory header
* \param line Line to be parsed
Expand Down