3 * @brief Handles all client control requests (admin related)
10 typedef struct ControlData {
11 ControlModes current_mode;
12 pthread_mutex_t mutex;
13 struct timeval start_time;
16 ControlData g_controls = {CONTROL_STOP, PTHREAD_MUTEX_INITIALIZER, {0}};
18 static bool PathExists(const char *path) {
19 FILE *fp = fopen(path, "r");
28 * System control handler. This covers high-level control, including
29 * admin related functions and starting/stopping experiments..
30 * @param context The context to work in
31 * @param params The input parameters
33 void Control_Handler(FCGIContext *context, char *params) {
34 const char *action, *key = "", *name = "";
36 ControlModes desired_mode;
38 FCGIValue values[4] = {
39 {"action", &action, FCGI_REQUIRED(FCGI_STRING_T)},
40 {"key", &key, FCGI_STRING_T},
41 {"force", &force, FCGI_BOOL_T},
42 {"name", &name, FCGI_STRING_T}
45 if (!FCGI_ParseRequest(context, params, values, 4))
48 if (!strcmp(action, "lock")) {
49 FCGI_LockControl(context, force);
51 } else if (!strcmp(action, "emergency")) {
52 desired_mode = CONTROL_EMERGENCY;
53 } else if (FCGI_HasControl(context, key)) {
54 if (!strcmp(action, "release")) {
55 FCGI_ReleaseControl(context);
56 } else if (!strcmp(action, "start")) {
57 desired_mode = CONTROL_START;
58 } else if (!strcmp(action, "pause")) {
59 desired_mode = CONTROL_PAUSE;
60 } else if (!strcmp(action, "resume")) {
61 desired_mode = CONTROL_RESUME;
62 } else if (!strcmp(action, "stop")) {
63 desired_mode = CONTROL_STOP;
65 FCGI_RejectJSON(context, "Unknown action specified.");
69 FCGI_RejectJSONEx(context, STATUS_UNAUTHORIZED,
70 "Invalid control key specified.");
75 if (desired_mode == CONTROL_START) {
76 if (PathExists(name) && !force) {
77 FCGI_RejectJSON(context, "An experiment with that name already exists.");
85 if ((ret = Control_SetMode(desired_mode, arg)) != NULL) {
86 FCGI_RejectJSON(context, ret);
88 FCGI_BeginJSON(context, STATUS_OK);
89 FCGI_JSONPair("description", "ok");
95 * Sets the mode to the desired mode, if possible.
96 * @param desired_mode The mode to be set to
97 * @param arg An argument specific to the mode to be set.
98 * @return NULL on success, an error message on failure.
100 const char* Control_SetMode(ControlModes desired_mode, void * arg)
102 const char *ret = NULL;
104 pthread_mutex_lock(&(g_controls.mutex));
105 if (g_controls.current_mode == desired_mode)
106 ret = "Already in the desired mode.";
107 else if (g_controls.current_mode == CONTROL_EMERGENCY && desired_mode != CONTROL_STOP)
108 ret = "In emergency mode. You must stop before continuing.";
109 else switch (desired_mode) {
111 if (g_controls.current_mode == CONTROL_STOP) {
112 const char * name = arg;
114 ret = "An experiment name must be specified";
115 else if (strpbrk(name, INVALID_CHARACTERS))
116 ret = "The experiment name must not contain: " INVALID_CHARACTERS_JSON;
118 FILE *fp = fopen((const char*) arg, "a");
121 gettimeofday(&(g_controls.start_time), NULL);
123 ret = "Cannot open experiment name marker";
126 ret = "Cannot start when not in a stopped state.";
129 if (g_controls.current_mode != CONTROL_START)
130 ret = "Cannot pause when not in a running state.";
133 if (g_controls.current_mode != CONTROL_PAUSE)
134 ret = "Cannot resume when not in a paused state.";
136 case CONTROL_EMERGENCY:
137 if (g_controls.current_mode != CONTROL_START) //pfft
138 ret = "Not running so how can there be an emergency.";
145 Actuator_SetModeAll(desired_mode, arg);
146 Sensor_SetModeAll(desired_mode, arg);
147 if (desired_mode != CONTROL_RESUME)
148 g_controls.current_mode = desired_mode;
150 g_controls.current_mode = CONTROL_START;
152 pthread_mutex_unlock(&(g_controls.mutex));
157 * Gets a string representation of the current mode
158 * @param mode The mode to get a string representation of
159 * @return The string representation of the mode
161 const char * Control_GetModeName() {
162 const char * ret = "Unknown";
164 switch (g_controls.current_mode) {
165 case CONTROL_START: ret = "Running"; break;
166 case CONTROL_PAUSE: ret = "Paused"; break;
167 case CONTROL_RESUME: ret = "Resumed"; break;
168 case CONTROL_STOP: ret = "Stopped"; break;
169 case CONTROL_EMERGENCY: ret = "Emergency mode"; break;
175 * Gets the start time for the current experiment
176 * @return the start time
178 const struct timeval* Control_GetStartTime() {
179 return &g_controls.start_time;