X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=server%2Fcontrol.c;h=5c8a4103feceb3672c79aa0cde7b1298973854f4;hb=0ef0945e8d83258400fabc61fa81725c5e8e533f;hp=29d9ef6ced8e1dede815729b936ae84740204c7d;hpb=9e7cfbbc6137056bba8ed8644fc6ecfe398553fe;p=matches%2FMCTX3420.git diff --git a/server/control.c b/server/control.c index 29d9ef6..5c8a410 100644 --- a/server/control.c +++ b/server/control.c @@ -7,33 +7,21 @@ #include "sensor.h" #include "actuator.h" -typedef enum ControlState { - STATE_STOPPED, - STATE_PAUSED, - STATE_RUNNING -} ControlState; - -typedef enum Mode { - START, - PAUSE, - RESUME, - STOP -} Mode; - typedef struct ControlData { - ControlState state; + ControlModes current_mode; pthread_mutex_t mutex; struct timeval start_time; } ControlData; -ControlData g_controls = {STATE_STOPPED, PTHREAD_MUTEX_INITIALIZER, {0}}; +ControlData g_controls = {CONTROL_STOP, PTHREAD_MUTEX_INITIALIZER, {0}}; -static bool ExperimentExists(const char *experiment_name) { - FILE *fp = fopen(experiment_name, "r"); - if (!fp) - return false; - fclose(fp); - return true; +static bool PathExists(const char *path) { + FILE *fp = fopen(path, "r"); + if (fp) { + fclose(fp); + return true; + } + return false; } /** @@ -45,7 +33,7 @@ static bool ExperimentExists(const char *experiment_name) { void Control_Handler(FCGIContext *context, char *params) { const char *action, *key = "", *name = ""; bool force = false; - Mode mode; + ControlModes desired_mode; FCGIValue values[4] = { {"action", &action, FCGI_REQUIRED(FCGI_STRING_T)}, @@ -60,17 +48,19 @@ void Control_Handler(FCGIContext *context, char *params) { if (!strcmp(action, "lock")) { FCGI_LockControl(context, force); return; + } else if (!strcmp(action, "emergency")) { + desired_mode = CONTROL_EMERGENCY; } else if (FCGI_HasControl(context, key)) { if (!strcmp(action, "release")) { FCGI_ReleaseControl(context); } else if (!strcmp(action, "start")) { - mode = START; + desired_mode = CONTROL_START; } else if (!strcmp(action, "pause")) { - mode = PAUSE; + desired_mode = CONTROL_PAUSE; } else if (!strcmp(action, "resume")) { - mode = RESUME; + desired_mode = CONTROL_RESUME; } else if (!strcmp(action, "stop")) { - mode = STOP; + desired_mode = CONTROL_STOP; } else { FCGI_RejectJSON(context, "Unknown action specified."); return; @@ -81,119 +71,110 @@ void Control_Handler(FCGIContext *context, char *params) { return; } - switch(mode) { - case START: - if (!*name) { - FCGI_RejectJSON(context, "An experiment name must be provided"); - } else if (ExperimentExists(name) && !force) { - FCGI_RejectJSONEx(context, STATUS_ALREADYEXISTS, - "An experiment with the specified name already exists."); - } else if (!Control_Start(name)) { - FCGI_RejectJSON(context, "An experiment is already running."); - } else { - FCGI_BeginJSON(context, STATUS_OK); - FCGI_EndJSON(); - } - break; - case PAUSE: - if (!Control_Pause()) { - FCGI_RejectJSON(context, "No experiment to pause."); - } else { - FCGI_BeginJSON(context, STATUS_OK); - FCGI_EndJSON(); - } - break; - case RESUME: - if (!Control_Resume()) { - FCGI_RejectJSON(context, "No experiment to resume."); - } else { - FCGI_BeginJSON(context, STATUS_OK); - FCGI_EndJSON(); - } - break; - case STOP: - if (!Control_Stop()) { - FCGI_RejectJSON(context, "No experiment to stop."); - } else { - FCGI_BeginJSON(context, STATUS_OK); - FCGI_EndJSON(); - } - break; - } -} + void *arg = NULL; + if (desired_mode == CONTROL_START) { + if (PathExists(name) && !force) { + FCGI_RejectJSON(context, "An experiment with that name already exists."); + return; + } -bool Control_Start(const char *experiment_name) { - bool ret = false; + arg = (void*)name; + } - pthread_mutex_lock(&(g_controls.mutex)); - if (g_controls.state == STATE_STOPPED) { - FILE *fp = fopen(experiment_name, "a"); - if (fp) { - fclose(fp); - gettimeofday(&(g_controls.start_time), NULL); - Sensor_StartAll(experiment_name); - Actuator_StartAll(experiment_name); - g_controls.state = STATE_RUNNING; - ret = true; - } + const char *ret; + if ((ret = Control_SetMode(desired_mode, arg)) != NULL) { + FCGI_RejectJSON(context, ret); + } else { + FCGI_BeginJSON(context, STATUS_OK); + FCGI_JSONPair("description", "ok"); + FCGI_EndJSON(); } - pthread_mutex_unlock(&(g_controls.mutex)); - return ret; } +/** + * Sets the mode to the desired mode, if possible. + * @param desired_mode The mode to be set to + * @param arg An argument specific to the mode to be set. + * @return NULL on success, an error message on failure. + */ +const char* Control_SetMode(ControlModes desired_mode, void * arg) +{ + const char *ret = NULL; -bool Control_Pause() { - bool ret = false; pthread_mutex_lock(&(g_controls.mutex)); - if (g_controls.state == STATE_RUNNING) { - Actuator_PauseAll(); - Sensor_PauseAll(); - g_controls.state = STATE_PAUSED; - ret = true; + if (g_controls.current_mode == desired_mode) + ret = "Already in the desired mode."; + else if (g_controls.current_mode == CONTROL_EMERGENCY && desired_mode != CONTROL_STOP) + ret = "In emergency mode. You must stop before continuing."; + else switch (desired_mode) { + case CONTROL_START: + if (g_controls.current_mode == CONTROL_STOP) { + const char * name = arg; + if (!*name) + ret = "An experiment name must be specified"; + else if (strpbrk(name, INVALID_CHARACTERS)) + ret = "The experiment name must not contain: " INVALID_CHARACTERS_JSON; + else { + FILE *fp = fopen((const char*) arg, "a"); + if (fp) { + fclose(fp); + gettimeofday(&(g_controls.start_time), NULL); + } else + ret = "Cannot open experiment name marker"; + } + } else + ret = "Cannot start when not in a stopped state."; + break; + case CONTROL_PAUSE: + if (g_controls.current_mode != CONTROL_START) + ret = "Cannot pause when not in a running state."; + break; + case CONTROL_RESUME: + if (g_controls.current_mode != CONTROL_PAUSE) + ret = "Cannot resume when not in a paused state."; + break; + case CONTROL_EMERGENCY: + if (g_controls.current_mode != CONTROL_START) //pfft + ret = "Not running so how can there be an emergency."; + break; + default: + break; } - pthread_mutex_unlock(&(g_controls.mutex)); - return ret; -} - -bool Control_Resume() { - bool ret = false; - pthread_mutex_lock(&(g_controls.mutex)); - if (g_controls.state == STATE_PAUSED) { - Actuator_ResumeAll(); - Sensor_ResumeAll(); - g_controls.state = STATE_RUNNING; - ret = true; + + if (ret == NULL) { + Actuator_SetModeAll(desired_mode, arg); + Sensor_SetModeAll(desired_mode, arg); + if (desired_mode != CONTROL_RESUME) + g_controls.current_mode = desired_mode; + else + g_controls.current_mode = CONTROL_START; } pthread_mutex_unlock(&(g_controls.mutex)); return ret; } -bool Control_Stop() { - bool ret = false; - - pthread_mutex_lock(&(g_controls.mutex)); - if (g_controls.state != STATE_STOPPED) { - Actuator_StopAll(); - Sensor_StopAll(); - g_controls.state = STATE_STOPPED; - ret = true; +/** + * Gets a string representation of the current mode + * @param mode The mode to get a string representation of + * @return The string representation of the mode + */ +const char * Control_GetModeName() { + const char * ret = "Unknown"; + + switch (g_controls.current_mode) { + case CONTROL_START: ret = "Running"; break; + case CONTROL_PAUSE: ret = "Paused"; break; + case CONTROL_RESUME: ret = "Resumed"; break; + case CONTROL_STOP: ret = "Stopped"; break; + case CONTROL_EMERGENCY: ret = "Emergency mode"; break; } - pthread_mutex_unlock(&(g_controls.mutex)); return ret; } -bool Control_Lock() { - pthread_mutex_lock(&(g_controls.mutex)); - if (g_controls.state == STATE_RUNNING || g_controls.state == STATE_PAUSED) - return true; - pthread_mutex_unlock(&(g_controls.mutex)); - return false; -} - -void Control_Unlock() { - pthread_mutex_unlock(&(g_controls.mutex)); -} - +/** + * Gets the start time for the current experiment + * @return the start time + */ const struct timeval* Control_GetStartTime() { return &g_controls.start_time; } \ No newline at end of file