X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=server%2Fcontrol.c;h=4b4a1e6e2f15473861b2a3629602982a2b399e13;hb=6a5de8e3b2870ca950f2116aeb9561ad7d707427;hp=4da1f7507718de6c90e7517efa1b9542a06a6f10;hpb=84282dc8bd6b84d23284742741fab5664ffb25a6;p=matches%2FMCTX3420.git diff --git a/server/control.c b/server/control.c index 4da1f75..4b4a1e6 100644 --- a/server/control.c +++ b/server/control.c @@ -1,107 +1,181 @@ /** * @file control.c - * @brief Handles all client control requests (admin/actuator related) + * @brief Handles all client control requests (admin related) */ #include "common.h" #include "control.h" +#include "sensor.h" +#include "actuator.h" -const char * g_actuator_names[NUMACTUATORS] = { - "Pressure regulator", "Solenoid 1" -}; +typedef struct ControlData { + ControlModes current_mode; + pthread_mutex_t mutex; + struct timeval start_time; +} ControlData; -/** - * Handles control of the actuators. - */ -void ActuatorHandler(FCGIContext *context, ActuatorId id, const char *set_value) { - char *ptr; - - switch(id) { //Add new actuators here - case ACT_PRESSURE: //Suppose is pressure regulator. 0-700 input (kPa) - { - int value = strtol(set_value, &ptr, 10); - if (*ptr == '\0' && value >= 0 && value <= 700) { - FCGI_BeginJSON(context, STATUS_OK); - FCGI_JSONKey("description"); - FCGI_JSONValue("\"Set pressure to %d kPa!\"", value); - FCGI_EndJSON(); - } else { - FCGI_RejectJSONEx(context, - STATUS_ERROR, "Invalid pressure specified."); - } - } break; - case ACT_SOLENOID1: - { - int value = strtol(set_value, &ptr, 10); - if (*ptr == '\0') { - const char *state = "off"; - if (value) - state = "on"; - FCGI_BeginJSON(context, STATUS_OK); - FCGI_JSONKey("description"); - FCGI_JSONValue("\"Solenoid 1 turned %s!\"", state); - FCGI_EndJSON(); - } else { - FCGI_RejectJSON(context, "Invalid actuator value specified"); - } - } break; - default: - FCGI_RejectJSONEx(context, - STATUS_ERROR, "Invalid actuator id specified."); +ControlData g_controls = {CONTROL_STOP, PTHREAD_MUTEX_INITIALIZER, {0}}; + +bool PathExists(const char *path) +{ + FILE *fp = fopen(path, "r"); + if (fp) { + fclose(fp); + return true; } + return false; } /** - * System control handler. This covers control over all aspects of the system. - * E.g: Actuators, system commands (start/stop experiment/recording) etc + * System control handler. This covers high-level control, including + * admin related functions and starting/stopping experiments.. * @param context The context to work in * @param params The input parameters */ void Control_Handler(FCGIContext *context, char *params) { - const char *key, *value, *control_key = NULL; - const char *action = NULL, *set_value = NULL; + const char *action, *key = "", *name = ""; bool force = false; - char *ptr; - int id = -1; + ControlModes desired_mode; + + FCGIValue values[4] = { + {"action", &action, FCGI_REQUIRED(FCGI_STRING_T)}, + {"key", &key, FCGI_STRING_T}, + {"force", &force, FCGI_BOOL_T}, + {"name", &name, FCGI_STRING_T} + }; + + if (!FCGI_ParseRequest(context, params, values, 4)) + return; - while ((params = FCGI_KeyPair(params, &key, &value))) { - if (!strcmp(key, "action")) - action = value; - else if (!strcmp(key, "key")) - control_key = value; - else if (!strcmp(key, "force")) - force = !force; - else if (!strcmp(key, "id") && *value) { //Ensure non-empty value - int parsed = strtol(value, &ptr, 10); - if (*ptr == '\0') { - id = parsed; - } - } else if (!strcmp(key, "value")) { - set_value = value; + 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")) { + desired_mode = CONTROL_START; + } else if (!strcmp(action, "pause")) { + 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; } - - if (action == NULL) { //Must have an action - FCGI_RejectJSON(context, "No action specified"); - } else if (!strcmp(action, "start")) { - FCGI_BeginControl(context, force); - } else if (!strcmp(action, "stop")) { //Don't require control key to stop... - //EMERGENCY STOP!! TODO - replace! + + void *arg = NULL; + if (desired_mode == CONTROL_START) { + 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", "stopped! (not)"); + FCGI_JSONPair("description", "ok"); FCGI_EndJSON(); - } else { //Under this section, the user must have the current control key. - if (!FCGI_HasControl(context, control_key)) { - FCGI_RejectJSONEx(context, - STATUS_UNAUTHORIZED, "Invalid control key specified."); - } else if (!strcmp(action, "end")) { - FCGI_EndControl(context); - } else if (!strcmp(action, "set")) { - if (set_value == NULL || *set_value == '\0') { - FCGI_RejectJSONEx(context, - STATUS_ERROR, "Set called but no value specified."); - } else { - ActuatorHandler(context, id, set_value); - } - } } } + +/** + * 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.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; + } + + 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; +} + +/** + * 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; + } + return ret; +} + +/** + * Gets the start time for the current experiment + * @return the start time + */ +const struct timeval* Control_GetStartTime() { + return &g_controls.start_time; +}