X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=server%2Fcontrol.c;h=4da1f7507718de6c90e7517efa1b9542a06a6f10;hb=a0c96fa8feb1938db84d95b14c320e6c7511c282;hp=7f5ea34c0509fc59eb1c5b5af96de9387fd12622;hpb=0a7664de84e25474991ead0d76dc9aea58e61aba;p=matches%2FMCTX3420.git diff --git a/server/control.c b/server/control.c index 7f5ea34..4da1f75 100644 --- a/server/control.c +++ b/server/control.c @@ -1,6 +1,55 @@ +/** + * @file control.c + * @brief Handles all client control requests (admin/actuator related) + */ #include "common.h" #include "control.h" +const char * g_actuator_names[NUMACTUATORS] = { + "Pressure regulator", "Solenoid 1" +}; + +/** + * 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."); + } +} + /** * System control handler. This covers control over all aspects of the system. * E.g: Actuators, system commands (start/stop experiment/recording) etc @@ -8,43 +57,51 @@ * @param params The input parameters */ void Control_Handler(FCGIContext *context, char *params) { - const char *key, *value, *loginkey = NULL, *action = NULL; + const char *key, *value, *control_key = NULL; + const char *action = NULL, *set_value = NULL; bool force = false; + char *ptr; + int id = -1; while ((params = FCGI_KeyPair(params, &key, &value))) { if (!strcmp(key, "action")) action = value; else if (!strcmp(key, "key")) - loginkey = value; + control_key = value; else if (!strcmp(key, "force")) force = !force; - else if (!strcmp(key, "id")) { - - } - else if (!strcmp(key, "value")) { - + 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, "start")) { - FCGI_Authorize(context, force); + 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!! + //EMERGENCY STOP!! TODO - replace! FCGI_BeginJSON(context, STATUS_OK); - FCGI_JSONPair("description", "stopped!"); //Not really + FCGI_JSONPair("description", "stopped! (not)"); FCGI_EndJSON(); - } else { - if (!FCGI_Authorized(context, loginkey)) { - FCGI_BeginJSON(context, STATUS_UNAUTHORIZED); - FCGI_JSONPair("description", "Invalid key specified."); - FCGI_EndJSON(); - return; + } 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_AuthorizeEnd(context); + FCGI_EndControl(context); } else if (!strcmp(action, "set")) { - FCGI_BeginJSON(context, STATUS_OK); - FCGI_JSONPair("description", "actuated!"); - FCGI_EndJSON(); + if (set_value == NULL || *set_value == '\0') { + FCGI_RejectJSONEx(context, + STATUS_ERROR, "Set called but no value specified."); + } else { + ActuatorHandler(context, id, set_value); + } } } }