Manual merge of semi-updated control code
authorJeremy Tan <[email protected]>
Sat, 14 Sep 2013 03:19:49 +0000 (11:19 +0800)
committerJeremy Tan <[email protected]>
Sat, 14 Sep 2013 03:19:49 +0000 (11:19 +0800)
server/control.c
server/control.h
server/fastcgi.c
server/fastcgi.h
server/sensor.h

index 70fc2ee..297cdd0 100644 (file)
@@ -1,17 +1,16 @@
 /**
  * @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;
        
@@ -48,56 +47,96 @@ void ActuatorHandler(FCGIContext *context, ActuatorId id, const char *set_value)
                        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;
 }
index 2de9b7f..950aeaa 100644 (file)
@@ -5,9 +5,10 @@
 #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
index 8714db2..b4b3587 100644 (file)
@@ -16,7 +16,7 @@
 #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*/
@@ -88,7 +88,7 @@ static void IdentifyHandler(FCGIContext *context, char *params) {
  * @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;
        
@@ -144,7 +144,7 @@ bool FCGI_HasControl(FCGIContext *context, const char *key) {
  * 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();
@@ -232,7 +232,7 @@ bool FCGI_ParseRequest(FCGIContext *context, char *params, FCGIValue values[], s
                                                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;
                                                }
 
@@ -245,7 +245,7 @@ bool FCGI_ParseRequest(FCGIContext *context, char *params, FCGIValue values[], s
                                                *((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;
@@ -260,7 +260,7 @@ bool FCGI_ParseRequest(FCGIContext *context, char *params, FCGIValue values[], s
                } //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;
                }
        }
@@ -354,36 +354,6 @@ void FCGI_EndJSON()
        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.
@@ -400,7 +370,7 @@ void FCGI_RejectJSONEx(FCGIContext *context, StatusCodes status, const char *des
        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"));
@@ -423,6 +393,37 @@ void FCGI_PrintRaw(const char *format, ...)
        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.
@@ -448,6 +449,9 @@ void * FCGI_RequestLoop (void *data)
                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");
index f0b8bd9..c1e0b3e 100644 (file)
@@ -41,8 +41,8 @@ typedef struct FCGIValue {
 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);
@@ -54,8 +54,8 @@ extern void FCGI_JSONBool(const char *key, bool value);
 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);
 
 /**
index 3f833d1..1b44673 100644 (file)
@@ -11,6 +11,7 @@
 /** Number of sensors **/
 #define NUMSENSORS 6 
 
+/** Sensor ids - there should be correspondence with the names in g_sensor_names **/
 typedef enum SensorId 
 {
        ANALOG_TEST0,
@@ -21,12 +22,9 @@ typedef enum SensorId
        DIGITAL_FAIL0
 } SensorId;
 
-
-
 /** Human readable names for the sensors **/
 extern const char * g_sensor_names[NUMSENSORS];
 
-
 /** Structure to represent a sensor **/
 typedef struct
 {
@@ -40,7 +38,6 @@ typedef struct
        pthread_t thread;
        /** Most recently recorded data **/
        DataPoint newest_data;
-
 } Sensor;
 
 // Structure to define the warning and error thresholds of the sensors

UCC git Repository :: git.ucc.asn.au