-
Notifications
You must be signed in to change notification settings - Fork 5
/
Commands.cpp
138 lines (115 loc) · 2.83 KB
/
Commands.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Prevent Visual Studio Intellisense from defining _WIN32 and _MSC_VER when we use
// Visual Studio to edit Linux or Borland C++ code.
#ifdef __linux__
# undef _WIN32
#endif // __linux__
#if defined(__GNUC__) || defined(__BORLANDC__)
# undef _MSC_VER
#endif // defined(__GNUC__) || defined(__BORLANDC__)
#include "Commands.h"
THREAD_PROC_RETURN_VALUE CommandsThread(void* pParam)
{
char line[MAX_BUF_LEN];
UNREFERENCED_PARAMETER(pParam);
printf("Type a command (e.g. help) or exit to terminate the program.\n");
// Read and execute commands from stdin.
for (;;)
{
// fgets() should be a blocking call.
// How to cancel it properly?
memset(line, 0, sizeof(line));
if (fgets3(stdin, line, sizeof(line)) != NULL)
{
if (bExit) break;
Commands(line);
}
else
{
if (bExit) break;
printf("Invalid command or stdin failure.\n");
mSleep(100);
}
if (bExit) break;
}
return 0;
}
THREAD_PROC_RETURN_VALUE MissionThread(void* pParam)
{
char line[MAX_BUF_LEN];
UNREFERENCED_PARAMETER(pParam);
for (;;)
{
EnterCriticalSection(&MissionFilesCS);
if (bMissionRunning)
{
memset(line, 0, sizeof(line));
if (fgets3(missionfile, line, sizeof(line)) != NULL)
{
LeaveCriticalSection(&MissionFilesCS);
Commands(line);
}
else
{
if (ferror(missionfile))
{
LeaveCriticalSection(&MissionFilesCS);
printf("File error.\n");
AbortMission();
}
else if (feof(missionfile))
{
LeaveCriticalSection(&MissionFilesCS);
AbortMission();
unlink(LOG_FOLDER"CurLbl.txt");
unlink(LOG_FOLDER"CurWp.txt");
}
else
{
LeaveCriticalSection(&MissionFilesCS);
printf("Invalid command.\n");
mSleep(100);
}
}
}
else
{
LeaveCriticalSection(&MissionFilesCS);
mSleep(100);
}
if (bExit) break;
}
bMissionPaused = FALSE;
AbortMission();
return 0;
}
THREAD_PROC_RETURN_VALUE MissionLogThread(void* pParam)
{
double lathat = 0, longhat = 0, althat = 0, depthhat = 0;
UNREFERENCED_PARAMETER(pParam);
for (;;)
{
mSleep(100);
EnterCriticalSection(&MissionFilesCS);
if (bMissionRunning&&(logmissionfile != NULL))
{
EnterCriticalSection(&StateVariablesCS);
EnvCoordSystem2GPS(lat_env, long_env, alt_env, angle_env, Center(xhat), Center(yhat), Center(zhat), &lathat, &longhat, &althat);
depthhat = -Center(zhat);
LeaveCriticalSection(&StateVariablesCS);
// szAction is not protected and might be temporarily invalid when it is changing,
// but this should not cause a crash...
fprintf(logmissionfile, "%f;%f;%f;%f;%s;\n",
GetTimeElapsedChronoQuick(&chrono_mission), lathat, longhat, depthhat, szAction
);
fflush(logmissionfile);
}
LeaveCriticalSection(&MissionFilesCS);
if (bExit) break;
}
return 0;
}
THREAD_PROC_RETURN_VALUE MissionArgThread(void* pParam)
{
CallMission((char*)pParam);
return 0;
}