X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=server%2Fcontrol.c;h=2c0fd7927462e96e39cddc395cad9110f6291a77;hb=f5e2b07860f26bde42b9a9eb41ef68138ff2a379;hp=b12530f4cae4f2c36a34d6327d1af5f87d19ffa2;hpb=46c219c69676ea4e6f467692b7db6e48a708ab80;p=matches%2FMCTX3420.git diff --git a/server/control.c b/server/control.c index b12530f..2c0fd79 100644 --- a/server/control.c +++ b/server/control.c @@ -7,19 +7,22 @@ #include "sensor.h" #include "actuator.h" -typedef enum ControlState { - STATE_STOPPED, - STATE_PAUSED, - STATE_RUNNING -} ControlState; - 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 PathExists(const char *path) { + FILE *fp = fopen(path, "r"); + if (fp) { + fclose(fp); + return true; + } + return false; +} /** * System control handler. This covers high-level control, including @@ -30,6 +33,7 @@ ControlData g_controls = {STATE_STOPPED, PTHREAD_MUTEX_INITIALIZER, {0}}; void Control_Handler(FCGIContext *context, char *params) { const char *action, *key = "", *name = ""; bool force = false; + ControlModes desired_mode; FCGIValue values[4] = { {"action", &action, FCGI_REQUIRED(FCGI_STRING_T)}, @@ -43,68 +47,134 @@ 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 (!strcmp(action, "query")) { + FCGI_BeginJSON(context, STATUS_OK); + FCGI_JSONPair("state", Control_GetModeName(Control_GetMode())); + FCGI_EndJSON(); + return; } else if (FCGI_HasControl(context, key)) { if (!strcmp(action, "release")) { FCGI_ReleaseControl(context); } else if (!strcmp(action, "start")) { - FCGI_BeginJSON(context, STATUS_OK); - FCGI_JSONPair("description", "start"); - FCGI_EndJSON(); + desired_mode = CONTROL_START; } else if (!strcmp(action, "pause")) { - FCGI_BeginJSON(context, STATUS_OK); - FCGI_JSONPair("description", "stop"); - FCGI_EndJSON(); - } else if (!strcmp(action, "end")) { - FCGI_BeginJSON(context, STATUS_OK); - FCGI_JSONPair("description", "stop"); - FCGI_EndJSON(); + desired_mode = CONTROL_PAUSE; + } else if (!strcmp(action, "resume")) { + desired_mode = CONTROL_RESUME; + } else if (!strcmp(action, "stop")) { + desired_mode = CONTROL_STOP; } else { FCGI_RejectJSON(context, "Unknown action specified."); + return; } } else { FCGI_RejectJSONEx(context, STATUS_UNAUTHORIZED, "Invalid control key specified."); + return; + } + + void *arg = NULL; + if (desired_mode == CONTROL_START) { + int len = strlen(name); + if (len <= 0) { + FCGI_RejectJSON(context, "An experiment name must be specified."); + return; + } else if (PathExists(name) && !force) { + FCGI_RejectJSON(context, "An experiment with that name already exists."); + return; + } + + arg = (void*)name; + } + + 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(); } } -bool Control_Start(const char *experiment_name) { - bool ret = false; +/** + * 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; pthread_mutex_lock(&(g_controls.mutex)); - if (g_controls.state == STATE_STOPPED) { - gettimeofday(&(g_controls.start_time), NULL); - Sensor_StartAll(experiment_name); - Actuator_StartAll(experiment_name); - g_controls.state = STATE_RUNNING; - ret = true; + if (g_controls.current_mode == CONTROL_EMERGENCY && desired_mode != CONTROL_STOP) { + ret = "In emergency mode. Stop before doing anything else."; + } else if (g_controls.current_mode == desired_mode) { + ret = "Already in desired mode."; + } else if (desired_mode == CONTROL_START) { + if (g_controls.current_mode == CONTROL_STOP) { + //TODO Sanitise name (ensure it contains no special chars eg \ / .. . + 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."; + } + } else if (desired_mode == CONTROL_RESUME) { + if (g_controls.current_mode != CONTROL_PAUSE) + ret = "Cannot resume when not in a paused state."; + } + + 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; } - -void Control_Pause() { - pthread_mutex_lock(&(g_controls.mutex)); - pthread_mutex_unlock(&(g_controls.mutex)); +/** + * Gets the current mode. + * @return The current mode + */ +ControlModes Control_GetMode() { + return g_controls.current_mode; } -bool Control_Stop() { - bool ret = false; +/** + * Gets a string representation of a mode + * @param mode The mode to get a string representation of + * @return The string representation of the mode + */ +const char * Control_GetModeName(ControlModes mode) { + const char * ret = "Unknown"; - pthread_mutex_lock(&(g_controls.mutex)); - if (g_controls.state != STATE_STOPPED) { - Actuator_StopAll(); - Sensor_StopAll(); - g_controls.state = STATE_STOPPED; - ret = true; + switch (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) + if (g_controls.state == STATE_RUNNING || g_controls.state == STATE_PAUSED) return true; pthread_mutex_unlock(&(g_controls.mutex)); return false; @@ -112,4 +182,12 @@ bool Control_Lock() { 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