From 84282dc8bd6b84d23284742741fab5664ffb25a6 Mon Sep 17 00:00:00 2001 From: Jeremy Tan Date: Fri, 6 Sep 2013 19:40:47 +0800 Subject: [PATCH] Add API version and change FCGI_RejectJSON FCGI_RejectJSON now requires a description message. --- server/common.h | 3 ++ server/control.c | 6 ++-- server/control.h | 2 +- server/fastcgi.c | 28 +++++++--------- server/fastcgi.h | 13 ++++++-- server/index.html | 82 ----------------------------------------------- server/sensor.c | 4 +-- server/sensor.h | 6 ++-- 8 files changed, 34 insertions(+), 110 deletions(-) delete mode 100644 server/index.html diff --git a/server/common.h b/server/common.h index 67ea195..1275032 100644 --- a/server/common.h +++ b/server/common.h @@ -10,6 +10,9 @@ #define _BSD_SOURCE #define _XOPEN_SOURCE 600 +/** The current API version **/ +#define API_VERSION 0 + #include #include #include diff --git a/server/control.c b/server/control.c index e883eb1..4da1f75 100644 --- a/server/control.c +++ b/server/control.c @@ -7,7 +7,7 @@ const char * g_actuator_names[NUMACTUATORS] = { "Pressure regulator", "Solenoid 1" -}; +}; /** * Handles control of the actuators. @@ -41,7 +41,7 @@ void ActuatorHandler(FCGIContext *context, ActuatorId id, const char *set_value) FCGI_JSONValue("\"Solenoid 1 turned %s!\"", state); FCGI_EndJSON(); } else { - FCGI_RejectJSON(context); + FCGI_RejectJSON(context, "Invalid actuator value specified"); } } break; default: @@ -81,7 +81,7 @@ void Control_Handler(FCGIContext *context, char *params) { } if (action == NULL) { //Must have an action - FCGI_RejectJSON(context); + 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... diff --git a/server/control.h b/server/control.h index 87d62fc..a1aa5c7 100644 --- a/server/control.h +++ b/server/control.h @@ -10,7 +10,7 @@ /** List of actuator ids (should be of size NUMACTUATORS) **/ typedef enum ActuatorId { - ACT_PRESSURE = 0, + ACT_PRESSURE, ACT_SOLENOID1 } ActuatorId; diff --git a/server/fastcgi.c b/server/fastcgi.c index 4b7f137..1bd520b 100644 --- a/server/fastcgi.c +++ b/server/fastcgi.c @@ -30,8 +30,11 @@ struct FCGIContext { }; /** - * Identifies current version info. Useful for testing that the API is running. - * TODO - Consider adding info about available sensors and actuators (eg capabilities)? + * Identifies build information and the current API version to the user. + * Also useful for testing that the API is running and identifying the + * sensors and actuators present. + * @param context The context to work in + * @param params User specified paramters: [actuators, sensors] */ static void IdentifyHandler(FCGIContext *context, char *params) { bool identSensors = false, identActuators = false; @@ -49,6 +52,7 @@ static void IdentifyHandler(FCGIContext *context, char *params) { FCGI_BeginJSON(context, STATUS_OK); FCGI_JSONPair("description", "MCTX3420 Server API (2013)"); FCGI_JSONPair("build_date", __DATE__ " " __TIME__); + FCGI_JSONLong("api_version", API_VERSION); if (identSensors) { FCGI_JSONKey("sensors"); FCGI_JSONValue("{\n\t\t"); @@ -205,7 +209,6 @@ void FCGI_BeginJSON(FCGIContext *context, StatusCodes status_code) FCGI_JSONDouble("start_time", start_time); FCGI_JSONDouble("current_time", current_time); FCGI_JSONDouble("running_time", current_time - start_time); - } /** @@ -267,16 +270,6 @@ void FCGI_EndJSON() printf("\r\n}\r\n"); } -/** - * To be used when the input parameters are invalid. The return data will - * have a status of STATUS_ERROR, along with other debugging information. - * @param context The context to work in - */ -void FCGI_RejectJSON(FCGIContext *context) -{ - FCGI_RejectJSONEx(context, STATUS_ERROR, "Invalid request"); -} - /** * To be used when the input parameters are rejected. The return data * will also have debugging information provided. @@ -352,8 +345,12 @@ void * FCGI_RequestLoop (void *data) size_t lastchar = strlen(module) - 1; if (lastchar > 0 && module[lastchar] == '/') module[lastchar] = 0; + + //Default to the 'identify' module if none specified + if (!*module) + strcpy(module, "identify"); - if (!*module || !strcmp("identify", module)) { + if (!strcmp("identify", module)) { module_handler = IdentifyHandler; } else if (!strcmp("control", module)) { module_handler = Control_Handler; @@ -365,8 +362,7 @@ void * FCGI_RequestLoop (void *data) if (module_handler) { module_handler(&context, params); } else { - strncat(module, " (unhandled)", BUFSIZ); - FCGI_RejectJSON(&context); + FCGI_RejectJSON(&context, "Unhandled module"); } context.response_number++; diff --git a/server/fastcgi.h b/server/fastcgi.h index 2003fd0..f52a20a 100644 --- a/server/fastcgi.h +++ b/server/fastcgi.h @@ -34,15 +34,22 @@ 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 void FCGI_RejectJSON(FCGIContext *context); extern void FCGI_RejectJSONEx(FCGIContext *context, StatusCodes status, const char *description); -extern void * FCGI_RequestLoop (void *data); +extern void *FCGI_RequestLoop (void *data); + +/** + * Shortcut to calling FCGI_RejectJSONEx. Sets the error code + * to STATUS_ERROR. + * @param context The context to work in + * @param description A short description of why the request was rejected. + * @see FCGI_RejectJSONEx + */ +#define FCGI_RejectJSON(context, description) FCGI_RejectJSONEx(context, STATUS_ERROR, description) /** * Custom formatting function for the JSON value. To be used in * conjunction with FCGI_JSONKey. Care should be taken to ensure * that valid JSON is produced. - * * @see FCGI_PrintRaw for calling syntax */ #define FCGI_JSONValue FCGI_PrintRaw diff --git a/server/index.html b/server/index.html deleted file mode 100644 index a3ff3ec..0000000 --- a/server/index.html +++ /dev/null @@ -1,82 +0,0 @@ - - - - - - FastCGI API Test - - - - - - -

FastCGI API Test

- The API is located at: http://mctx.us.to:8080/api/
-

Input

- Place a query string here. Examples include:
-
    -
  • sensors?key=value&key2
  • -
  • doesntexist?f
  • -
- Response times are inaccurate via JavaScript. Use the web console of - your browser to determine how long the query takes.
- Hopefully this doesn't break! -
-
- Query string:
- -
-
- -

Output

-
-
- - diff --git a/server/sensor.c b/server/sensor.c index 79dd82e..ecbc584 100644 --- a/server/sensor.c +++ b/server/sensor.c @@ -271,7 +271,7 @@ void Sensor_Handler(FCGIContext *context, char * params) if (status == STATUS_ERROR) { - FCGI_RejectJSON(context); + FCGI_RejectJSON(context, "Invalid input parameters"); return; } @@ -296,7 +296,7 @@ void Sensor_Handler(FCGIContext *context, char * params) } } - while (amount_read == SENSOR_QUERYBUFSIZ); + while (amount_read > 0); pthread_mutex_unlock(&(sensor->mutex)); // end critical section break; diff --git a/server/sensor.h b/server/sensor.h index f6c9047..8ae5660 100644 --- a/server/sensor.h +++ b/server/sensor.h @@ -8,19 +8,20 @@ /** Number of data points to keep in sensor buffers **/ #define SENSOR_DATABUFSIZ 10 - +/** Size of the query buffer. @see Sensor_Handler **/ #define SENSOR_QUERYBUFSIZ 10 /** Number of sensors **/ #define NUMSENSORS 4 -typedef enum SensorId{ +typedef enum SensorId { ANALOG_TEST0, ANALOG_TEST1, DIGITAL_TEST0, DIGITAL_TEST1 } SensorId; +/** Human readable names for the sensors **/ extern const char * g_sensor_names[NUMSENSORS]; /** Structure to represent data recorded by a sensor at an instant in time **/ @@ -64,6 +65,5 @@ extern int Sensor_Query(Sensor * s, DataPoint * buffer, int bufsiz); // fill buf extern void Sensor_Handler(FCGIContext *context, char * params); - #endif //_SENSOR_H -- 2.20.1