#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) {
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"))
#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
#include <fcgi_stdio.h>
#include <openssl/sha.h>
-#include <time.h>
#include "common.h"
#include "sensor.h"
* 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();
}
/** 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?
}
-
-
-
/**
* Run the main sensor polling loop
* @param arg - Cast to Sensor* - Sensor that the thread will handle
{
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);
{
FCGI_PrintRaw("%f\t%f\n", buffer[i].time_stamp, buffer[i].value);
}
-
+
}
while (amount_read == SENSOR_QUERYBUFSIZ);
pthread_mutex_unlock(&(sensor->mutex));
* @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
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 **/