/**
* @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"
+
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;
FCGI_RejectJSONEx(context,
STATUS_ERROR, "Invalid actuator id specified.");
}
-}
+}*/
+
+typedef enum ControlState {
+ STATE_STOPPED,
+ STATE_PAUSED,
+ STATE_RUNNING
+} ControlState;
+
+typedef struct ControlData {
+ ControlState state;
+ pthread_mutex_t mutex;
+ struct timeval start_time;
+} ControlData;
+
+ControlData g_controls = {STATE_STOPPED, PTHREAD_MUTEX_INITIALIZER, {0}};
/**
- * 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 *action, *key = "", *mode = "", *name = "";
+ const char *action, *key = "", *name = "";
bool force = false;
- FCGIValue values[5] = {
+ FCGIValue values[4] = {
{"action", &action, FCGI_REQUIRED(FCGI_STRING_T)},
{"key", &key, FCGI_STRING_T},
{"force", &force, FCGI_BOOL_T},
- {"mode", &mode, FCGI_STRING_T},
{"name", &name, FCGI_STRING_T}
};
- if (!FCGI_ParseRequest(context, params, values, 5))
+ if (!FCGI_ParseRequest(context, params, values, 4))
return;
-
- if (!strcmp(action, "gain")) {
- FCGI_BeginControl(context, force);
- } else {
- if (!FCGI_HasControl(context, key)) {
- FCGI_RejectJSONEx(context,
- STATUS_UNAUTHORIZED, "Invalid control key specified.");
-
- } else if (!strcmp(action, "release")) {
- FCGI_EndControl(context);
- } else if (!strcmp(action, "experiment")) {
- if (!strcmp(mode, "start")) {
- FCGI_BeginJSON(context, STATUS_OK);
- FCGI_JSONPair("description", mode);
- FCGI_EndJSON();
- } else if (!strcmp(mode, "pause")) {
- FCGI_BeginJSON(context, STATUS_OK);
- FCGI_JSONPair("description", mode);
- FCGI_EndJSON();
- } else if (!strcmp(mode, "stop")) {
- FCGI_BeginJSON(context, STATUS_OK);
- FCGI_JSONPair("description", mode);
- FCGI_EndJSON();
- } else {
- FCGI_RejectJSON(context, "Unknown experiment mode specified");
- }
+
+ if (!strcmp(action, "lock")) {
+ FCGI_LockControl(context, force);
+ } 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();
+ } 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();
} else {
- FCGI_RejectJSON(context, "Unknown action specified");
+ FCGI_RejectJSON(context, "Unknown action specified.");
}
+ } else {
+ FCGI_RejectJSONEx(context, STATUS_UNAUTHORIZED,
+ "Invalid control key specified.");
+ }
+}
+
+bool Control_Start(const char *experiment_name) {
+ pthread_mutex_lock(&(g_controls.mutex));
+ if (g_controls.state == STATE_STOPPED) {
+ gettimeofday(&(g_controls.start_time), NULL);
+ Sensor_StartAll(experiment_name);
+ g_controls.state = STATE_RUNNING;
+
+ pthread_mutex_unlock(&(g_controls.mutex));
+ return true;
+ }
+ return false;
+ pthread_mutex_unlock(&(g_controls.mutex));
+}
+
+void Control_Pause() {
+ pthread_mutex_lock(&(g_controls.mutex));
+ pthread_mutex_unlock(&(g_controls.mutex));
+}
+
+bool Control_End() {
+ pthread_mutex_lock(&(g_controls.mutex));
+ if (g_controls.state != STATE_STOPPED) {
+ Sensor_StopAll();
+ g_controls.state = STATE_STOPPED;
+
+ pthread_mutex_unlock(&(g_controls.mutex));
+ return true;
}
+ pthread_mutex_unlock(&(g_controls.mutex));
+ return false;
}
#ifndef _CONTROL_H
#define _CONTROL_H
-
-
/** ID codes for all the actuators **/
extern void Control_Handler(FCGIContext *context, char *params);
+extern bool Control_Start(const char *experiment_name);
+extern void Control_Pause();
+extern bool Control_End();
#endif
#include "control.h"
#include "options.h"
-/**The time period (in seconds) before the control key expires @ */
+/**The time period (in seconds) before the control key expires */
#define CONTROL_TIMEOUT 180
/**Contextual information related to FCGI requests*/
* @param context The context to work in
* @param force Whether to force key generation or not.
*/
-void FCGI_BeginControl(FCGIContext *context, bool force) {
+void FCGI_LockControl(FCGIContext *context, bool force) {
time_t now = time(NULL);
bool expired = now - context->control_timestamp > CONTROL_TIMEOUT;
* Revokes the current control key, if present.
* @param context The context to work in
*/
-void FCGI_EndControl(FCGIContext *context) {
+void FCGI_ReleaseControl(FCGIContext *context) {
*(context->control_key) = 0;
FCGI_BeginJSON(context, STATUS_OK);
FCGI_EndJSON();
long parsed = strtol(value, &ptr, 10);
if (!*value || *ptr) {
snprintf(buf, BUFSIZ, "Expected int for '%s' but got '%s'", key, value);
- FCGI_RejectJSON(context, FCGI_EscapeJSON(buf));
+ FCGI_RejectJSON(context, buf);
return false;
}
*((double*) val->value) = strtod(value, &ptr);
if (!*value || *ptr) {
snprintf(buf, BUFSIZ, "Expected float for '%s' but got '%s'", key, value);
- FCGI_RejectJSON(context, FCGI_EscapeJSON(buf));
+ FCGI_RejectJSON(context, buf);
return false;
}
break;
} //End for loop
if (i == count) {
snprintf(buf, BUFSIZ, "Unknown key '%s' specified", key);
- FCGI_RejectJSON(context, FCGI_EscapeJSON(buf));
+ FCGI_RejectJSON(context, buf);
return false;
}
}
printf("\r\n}\r\n");
}
-/**
- * Escapes a string so it can be used as a JSON string value.
- * Does not support unicode specifiers in the form of \uXXXX.
- * @param buf The string to be escaped
- * @return The escaped string (return value == buf)
- */
-char *FCGI_EscapeJSON(char *buf)
-{
- int length, i;
- length = strlen(buf);
-
- //Escape special characters. Must count down to escape properly
- for (i = length - 1; i >= 0; i--) {
- if (buf[i] < 0x20) { //Control characters
- buf[i] = ' ';
- } else if (buf[i] == '"') {
- if (i-1 >= 0 && buf[i-1] == '\\')
- i--;
- else
- buf[i] = '\'';
- } else if (buf[i] == '\\') {
- if (i-1 >= 0 && buf[i-1] == '\'')
- i--;
- else
- buf[i] = ' ';
- }
- }
- return buf;
-}
-
/**
* To be used when the input parameters are rejected. The return data
* will also have debugging information provided.
FCGI_BeginJSON(context, status);
FCGI_JSONPair("description", description);
FCGI_JSONLong("responsenumber", context->response_number);
- FCGI_JSONPair("params", getenv("QUERY_STRING"));
+ //FCGI_JSONPair("params", getenv("QUERY_STRING"));
FCGI_JSONPair("host", getenv("SERVER_HOSTNAME"));
FCGI_JSONPair("user", getenv("REMOTE_USER"));
FCGI_JSONPair("ip", getenv("REMOTE_ADDR"));
va_end(list);
}
+/**
+ * Escapes a string so it can be used safely.
+ * Currently escapes to ensure the validity for use as a JSON string
+ * Does not support unicode specifiers in the form of \uXXXX.
+ * @param buf The string to be escaped
+ * @return The escaped string (return value == buf)
+ */
+char *FCGI_EscapeText(char *buf)
+{
+ int length, i;
+ length = strlen(buf);
+
+ //Escape special characters. Must count down to escape properly
+ for (i = length - 1; i >= 0; i--) {
+ if (buf[i] < 0x20) { //Control characters
+ buf[i] = ' ';
+ } else if (buf[i] == '"') {
+ if (i-1 >= 0 && buf[i-1] == '\\')
+ i--;
+ else
+ buf[i] = '\'';
+ } else if (buf[i] == '\\') {
+ if (i-1 >= 0 && buf[i-1] == '\'')
+ i--;
+ else
+ buf[i] = ' ';
+ }
+ }
+ return buf;
+}
+
/**
* Main FCGI request loop that receives/responds to client requests.
* @param data Reserved.
if (lastchar > 0 && module[lastchar] == '/')
module[lastchar] = 0;
+ //Escape all special characters
+ FCGI_EscapeText(params);
+
//Default to the 'identify' module if none specified
if (!*module)
strcpy(module, "identify");
typedef struct FCGIContext FCGIContext;
typedef void (*ModuleHandler) (FCGIContext *context, char *params);
-extern void FCGI_BeginControl(FCGIContext *context, bool force);
-extern void FCGI_EndControl(FCGIContext *context);
+extern void FCGI_LockControl(FCGIContext *context, bool force);
+extern void FCGI_ReleaseControl(FCGIContext *context);
extern bool FCGI_HasControl(FCGIContext *context, const char *key);
extern char *FCGI_KeyPair(char *in, const char **key, const char **value);
extern bool FCGI_ParseRequest(FCGIContext *context, char *params, FCGIValue values[], size_t count);
extern void FCGI_JSONKey(const char *key);
extern void FCGI_PrintRaw(const char *format, ...);
extern void FCGI_EndJSON();
-extern char *FCGI_EscapeJSON(char *buf);
extern void FCGI_RejectJSONEx(FCGIContext *context, StatusCodes status, const char *description);
+extern char *FCGI_EscapeText(char *buf);
extern void *FCGI_RequestLoop (void *data);
/**
/** Number of sensors **/
#define NUMSENSORS 6
+/** Sensor ids - there should be correspondence with the names in g_sensor_names **/
typedef enum SensorId
{
ANALOG_TEST0,
DIGITAL_FAIL0
} SensorId;
-
-
/** Human readable names for the sensors **/
extern const char * g_sensor_names[NUMSENSORS];
-
/** Structure to represent a sensor **/
typedef struct
{
pthread_t thread;
/** Most recently recorded data **/
DataPoint newest_data;
-
} Sensor;
// Structure to define the warning and error thresholds of the sensors