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 bool PathExists(const char *path)
20 FILE *fp = fopen(path, "r");
29 * System control handler. This covers high-level control, including
30 * admin related functions and starting/stopping experiments..
31 * @param context The context to work in
32 * @param params The input parameters
34 void Control_Handler(FCGIContext *context, char *params) {
35 const char *action = "";
36 const char *name = "";
38 ControlModes desired_mode;
41 // Login/auth now handled entirely in fastcgi.c and login.c
42 //TODO: Need to not have the ability for any user to stop someone else' experiment...
43 // (achieve by storing the username of the person running the current experiment, even when they log out?)
44 // (Our program should only realisitically support a single experiment at a time, so that should be sufficient)
45 FCGIValue values[3] = {
46 {"action", &action, FCGI_REQUIRED(FCGI_STRING_T)},
47 {"force", &force, FCGI_BOOL_T},
48 {"name", &name, FCGI_STRING_T}
51 if (!FCGI_ParseRequest(context, params, values, 3))
54 if (!strcmp(action, "emergency")) {
55 desired_mode = CONTROL_EMERGENCY;
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.");
70 if (desired_mode == CONTROL_START) {
71 if (PathExists(name) && !force) {
72 FCGI_RejectJSON(context, "An experiment with that name already exists.");
80 if ((ret = Control_SetMode(desired_mode, arg)) != NULL) {
81 FCGI_RejectJSON(context, ret);
83 FCGI_BeginJSON(context, STATUS_OK);
84 FCGI_JSONPair("description", "ok");
90 * Sets the mode to the desired mode, if possible.
91 * @param desired_mode The mode to be set to
92 * @param arg An argument specific to the mode to be set.
93 * @return NULL on success, an error message on failure.
95 const char* Control_SetMode(ControlModes desired_mode, void * arg)
97 const char *ret = NULL;
99 pthread_mutex_lock(&(g_controls.mutex));
100 if (g_controls.current_mode == desired_mode)
101 ret = "Already in the desired mode.";
102 else if (g_controls.current_mode == CONTROL_EMERGENCY && desired_mode != CONTROL_STOP)
103 ret = "In emergency mode. You must stop before continuing.";
104 else switch (desired_mode) {
106 if (g_controls.current_mode == CONTROL_STOP) {
107 const char * name = arg;
109 ret = "An experiment name must be specified";
110 else if (strpbrk(name, INVALID_CHARACTERS))
111 ret = "The experiment name must not contain: " INVALID_CHARACTERS_JSON;
113 FILE *fp = fopen((const char*) arg, "a");
116 gettimeofday(&(g_controls.start_time), NULL);
118 ret = "Cannot open experiment name marker";
121 ret = "Cannot start when not in a stopped state.";
124 if (g_controls.current_mode != CONTROL_START)
125 ret = "Cannot pause when not in a running state.";
128 if (g_controls.current_mode != CONTROL_PAUSE)
129 ret = "Cannot resume when not in a paused state.";
131 case CONTROL_EMERGENCY:
132 if (g_controls.current_mode != CONTROL_START) //pfft
133 ret = "Not running so how can there be an emergency.";
140 Actuator_SetModeAll(desired_mode, arg);
141 Sensor_SetModeAll(desired_mode, arg);
142 if (desired_mode != CONTROL_RESUME)
143 g_controls.current_mode = desired_mode;
145 g_controls.current_mode = CONTROL_START;
147 pthread_mutex_unlock(&(g_controls.mutex));
152 * Gets a string representation of the current mode
153 * @param mode The mode to get a string representation of
154 * @return The string representation of the mode
156 const char * Control_GetModeName() {
157 const char * ret = "Unknown";
159 switch (g_controls.current_mode) {
160 case CONTROL_START: ret = "Running"; break;
161 case CONTROL_PAUSE: ret = "Paused"; break;
162 case CONTROL_RESUME: ret = "Resumed"; break;
163 case CONTROL_STOP: ret = "Stopped"; break;
164 case CONTROL_EMERGENCY: ret = "Emergency mode"; break;
170 * Gets the start time for the current experiment
171 * @return the start time
173 const struct timeval* Control_GetStartTime() {
174 return &g_controls.start_time;