Add API version and change FCGI_RejectJSON
authorJeremy Tan <[email protected]>
Fri, 6 Sep 2013 11:40:47 +0000 (19:40 +0800)
committerJeremy Tan <[email protected]>
Fri, 6 Sep 2013 11:40:47 +0000 (19:40 +0800)
FCGI_RejectJSON now requires a description message.

server/common.h
server/control.c
server/control.h
server/fastcgi.c
server/fastcgi.h
server/index.html [deleted file]
server/sensor.c
server/sensor.h

index 67ea195..1275032 100644 (file)
@@ -10,6 +10,9 @@
 #define _BSD_SOURCE
 #define _XOPEN_SOURCE 600
 
+/** The current API version **/
+#define API_VERSION 0
+
 #include <stdlib.h>
 #include <stdio.h>
 #include <stdbool.h>
index e883eb1..4da1f75 100644 (file)
@@ -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...
index 87d62fc..a1aa5c7 100644 (file)
@@ -10,7 +10,7 @@
 
 /** List of actuator ids (should be of size NUMACTUATORS) **/
 typedef enum ActuatorId {
-       ACT_PRESSURE = 0,
+       ACT_PRESSURE,
        ACT_SOLENOID1
 } ActuatorId;
 
index 4b7f137..1bd520b 100644 (file)
@@ -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++;
 
index 2003fd0..f52a20a 100644 (file)
@@ -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 (file)
index a3ff3ec..0000000
+++ /dev/null
@@ -1,82 +0,0 @@
-<!DOCTYPE html>
-<html>
-       <head>
-               <meta charset="utf-8">
-               <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
-               <title>FastCGI API Test</title>
-               <style>
-                       body {
-                               font-family: "Trebuchet MS", "Verdana", "Arial", "Sans";
-                               font-size: 12px;
-                               margin: 1em;
-                       }
-                       h2 {
-                               border-bottom: 1px solid gray;
-                       }
-                       .io {
-                               border: 1px solid gray;
-                               padding: 0.5em;
-                               margin: 1em;
-                               min-height: 5em;
-                               background-color: GhostWhite;
-                       }
-               </style>
-               
-               <script>
-               $(document).ready(function()
-               {
-                       $('#inputquery').submit(function () 
-                       {
-                               $('#output').text("Submitting query...");
-                               var query = $('#inputquery').find('input[name="query"]').val();
-                               
-                               var d = new Date();
-                               var start = d.getMilliseconds();
-                               var domain = document.domain == "mctx.us.to" ? "mctx.us.to:8080" : document.domain;
-                               $.getJSON('http://'+domain+'/api/'+query, function(data) {
-                                       var items = [];
-                                       var timeDiff = d.getMilliseconds() - start; //Not precise at all, use web console
-                                       
-                                       $.each(data, function(key, val) {
-                                               items.push('<li>"' + key + '" : "' + val + '"</li>'); 
-                                       });
-                                       
-                                       
-                                       $('#output').html("Response ok (" + timeDiff + "ms)! Output:<br>");
-                                       $('<ul/>', {
-                                               html: items.join("\n")
-                                       }).appendTo('#output');
-               
-                               }).fail(function(jqXHR) {
-                                       $('#output').text("Query failed with response code: " + jqXHR.status);
-                               });
-                               return false;
-                       });
-               });
-               </script>
-       </head>
-       
-       <body>
-               <h1>FastCGI API Test</h1>
-               The API is located at: <a href="http://mctx.us.to:8080/api/">http://mctx.us.to:8080/api/</a><br>
-               <h2>Input</h2>
-               Place a query string here. Examples include:<br>
-               <ul>
-                       <li><pre>sensors?key=value&amp;key2</pre></li>
-                       <li><pre>doesntexist?f</pre></li>
-               </ul>
-               Response times are inaccurate via JavaScript. Use the web console of
-               your browser to determine how long the query takes.<br>
-               Hopefully this doesn't break!
-               <div class="io">
-                       <form id="inputquery" name="input" action="#">
-                               Query string: <input type="text" name="query"><br>
-                               <input type="submit" value="Submit">
-                       </form>
-               </div>
-               
-               <h2>Output</h2>
-               <div id="output" class="io">
-               </div>
-       </body>
-</html>
index 79dd82e..ecbc584 100644 (file)
@@ -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;
index f6c9047..8ae5660 100644 (file)
@@ -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
 

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