Fix g_sensor_names (enum out of order) and add identification
authorJeremy Tan <[email protected]>
Fri, 6 Sep 2013 03:02:40 +0000 (11:02 +0800)
committerJeremy Tan <[email protected]>
Fri, 6 Sep 2013 03:02:40 +0000 (11:02 +0800)
server/control.c
server/control.h
server/fastcgi.c
server/sensor.c
server/sensor.h

index 59b5bf8..e883eb1 100644 (file)
@@ -5,14 +5,18 @@
 #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, int id, const char *set_value) {
+void ActuatorHandler(FCGIContext *context, ActuatorId id, const char *set_value) {
        char *ptr;
        
        switch(id) { //Add new actuators here
-               case ACT_PREG: //Suppose is pressure regulator. 0-700 input (kPa)
+               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) {
@@ -57,7 +61,7 @@ void Control_Handler(FCGIContext *context, char *params) {
        const char *action = NULL, *set_value = NULL;
        bool force = false;
        char *ptr;
-       int id = ACT_NONE;
+       int id = -1;
        
        while ((params = FCGI_KeyPair(params, &key, &value))) {
                if (!strcmp(key, "action"))
index 2878a88..87d62fc 100644 (file)
@@ -5,8 +5,19 @@
 #ifndef _CONTROL_H
 #define _CONTROL_H
 
-/**ID codes for all the actuators**/
-typedef enum Actuators {ACT_NONE = -1, ACT_PREG = 0, ACT_SOLENOID1} Actuators;
+/** Number of actuators **/
+#define NUMACTUATORS 2
+
+/** List of actuator ids (should be of size NUMACTUATORS) **/
+typedef enum ActuatorId {
+       ACT_PRESSURE = 0,
+       ACT_SOLENOID1
+} ActuatorId;
+
+/** Human readable names for the actuator ids **/
+extern const char * g_actuator_names[NUMACTUATORS];
+
+/** ID codes for all the actuators **/
 extern void Control_Handler(FCGIContext *context, char *params);
 
 #endif
index bddac21..4b7f137 100644 (file)
@@ -8,7 +8,6 @@
 
 #include <fcgi_stdio.h>
 #include <openssl/sha.h>
-#include <time.h>
 
 #include "common.h"
 #include "sensor.h"
@@ -35,9 +34,43 @@ struct FCGIContext {
  * TODO - Consider adding info about available sensors and actuators (eg capabilities)?
  */ 
 static void IdentifyHandler(FCGIContext *context, char *params) {
+       bool identSensors = false, identActuators = false;
+       const char *key, *value;
+       int i;
+
+       while ((params = FCGI_KeyPair(params, &key, &value))) {
+               if (!strcmp(key, "sensors")) {
+                       identSensors = !identSensors;
+               } else if (!strcmp(key, "actuators")) {
+                       identActuators = !identActuators;
+               }
+       }
+
        FCGI_BeginJSON(context, STATUS_OK);
        FCGI_JSONPair("description", "MCTX3420 Server API (2013)");
        FCGI_JSONPair("build_date", __DATE__ " " __TIME__);
+       if (identSensors) {
+               FCGI_JSONKey("sensors");
+               FCGI_JSONValue("{\n\t\t");
+               for (i = 0; i < NUMSENSORS; i++) {
+                       if (i > 0) {
+                               FCGI_JSONValue(",\n\t\t");
+                       }
+                       FCGI_JSONValue("\"%d\" : \"%s\"", i, g_sensor_names[i]); 
+               }
+               FCGI_JSONValue("\n\t}");
+       }
+       if (identActuators) {
+               FCGI_JSONKey("actuators");
+               FCGI_JSONValue("{\n\t\t");
+               for (i = 0; i < NUMACTUATORS; i++) {
+                       if (i > 0) {
+                               FCGI_JSONValue(",\n\t\t");
+                       }
+                       FCGI_JSONValue("\"%d\" : \"%s\"", i, g_actuator_names[i]); 
+               }
+               FCGI_JSONValue("\n\t}");
+       }
        FCGI_EndJSON();
 }
 
index f58daa5..79dd82e 100644 (file)
 
 /** Array of sensors, initialised by Sensor_Init **/
 static Sensor g_sensors[NUMSENSORS]; //global to this file
-static const char * g_sensor_names[] = {"analog_test0", "analog_test1", "digital_test0", "digital_test1"};
+const char * g_sensor_names[NUMSENSORS] = {    
+       "analog_test0", "analog_test1", 
+       "digital_test0", "digital_test1"
+};
+
 /**
  * Read a data value from a sensor; block until value is read
  * @param sensor_id - The ID of the sensor
  * @param d - DataPoint to set
  * @returns NULL for digital sensors when data is unchanged, otherwise d
  */
-DataPoint * GetData(int sensor_id, DataPoint * d)
+DataPoint * GetData(SensorId sensor_id, DataPoint * d)
 {
        // switch based on the sensor_id at the moment for testing;
        // might be able to just directly access ADC from sensor_id?
@@ -104,9 +108,6 @@ void Init(Sensor * s, int id)
 }
 
 
-
-
-
 /**
  * Run the main sensor polling loop
  * @param arg - Cast to Sensor* - Sensor that the thread will handle
@@ -278,7 +279,10 @@ void Sensor_Handler(FCGIContext *context, char * params)
        {
                case DUMP:
                {
-                       FCGI_PrintRaw("Content-type: text/plain\r\n\r\n");
+                       //Force download with content-disposition
+                       FCGI_PrintRaw("Content-type: text/plain\r\n"
+                               "Content-disposition: attachment;filename=%d.csv\r\n\r\n",
+                               sensor->id);
                        //CRITICAL SECTION
                        pthread_mutex_lock(&(sensor->mutex));
                                fseek(sensor->file, 0, SEEK_SET);
@@ -290,7 +294,7 @@ void Sensor_Handler(FCGIContext *context, char * params)
                                        {
                                                FCGI_PrintRaw("%f\t%f\n", buffer[i].time_stamp, buffer[i].value);
                                        }
-       
+
                                }
                                while (amount_read == SENSOR_QUERYBUFSIZ);
                        pthread_mutex_unlock(&(sensor->mutex));
index c6cfbe0..f6c9047 100644 (file)
@@ -3,7 +3,6 @@
  * @brief Declarations for sensor thread related stuff
  */
 
-
 #ifndef _SENSOR_H
 #define _SENSOR_H
 
 /** Number of sensors **/
 #define NUMSENSORS 4
 
+typedef enum SensorId{
+       ANALOG_TEST0,
+       ANALOG_TEST1,
+       DIGITAL_TEST0,
+       DIGITAL_TEST1
+} SensorId;
+
+extern const char * g_sensor_names[NUMSENSORS];
 
 /** Structure to represent data recorded by a sensor at an instant in time **/
 typedef struct
@@ -29,7 +36,7 @@ typedef struct
 typedef struct
 {
        /** ID number of the sensor **/
-       enum {ANALOG_TEST0=2, ANALOG_TEST1=0, DIGITAL_TEST0=1, DIGITAL_TEST1=3} id;
+       SensorId id;
        /** Buffer to store data from the sensor **/
        DataPoint buffer[SENSOR_DATABUFSIZ];
        /** Index of last point written in the data buffer **/

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