moves helpers from config to helper-api
authorJosé Bollo <jose.bollo@iot.bzh>
Wed, 23 Mar 2016 10:57:49 +0000 (11:57 +0100)
committerJosé Bollo <jose.bollo@iot.bzh>
Wed, 23 Mar 2016 12:18:24 +0000 (13:18 +0100)
Change-Id: Iecba4a22718b76a3188986eae814c359d21bbd78
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
include/proto-def.h
src/config.c
src/helper-api.c

index a030a27..89b6e3d 100644 (file)
 */
 
 // helper-api
-PUBLIC json_object* getPingTest(AFB_request *request);
-PUBLIC const char* getQueryValue (const AFB_request * request, const char *name);
-PUBLIC int getQueryAll(AFB_request * request, char *query, size_t len);
-PUBLIC AFB_PostHandle* getPostHandle (AFB_request *request);
-PUBLIC json_object* getPostFile (AFB_request *request, AFB_PostItem *item, char* destination) ;
-PUBLIC AFB_PostCtx* getPostContext (AFB_request *request);
-PUBLIC char* getPostPath (AFB_request *request);
+extern json_object* getPingTest(AFB_request *request);
+extern const char* getQueryValue (const AFB_request * request, const char *name);
+extern int getQueryAll(AFB_request * request, char *query, size_t len);
+extern AFB_PostHandle* getPostHandle (AFB_request *request);
+extern json_object* getPostFile (AFB_request *request, AFB_PostItem *item, char* destination) ;
+extern AFB_PostCtx* getPostContext (AFB_request *request);
+extern char* getPostPath (AFB_request *request);
+
+extern json_object *jsonNewMessage (AFB_error level, char* format, ...);
+extern json_object *jsonNewStatus (AFB_error level);
+extern json_object *jsonNewjtype (void);
+extern json_object *jsonNewMessage (AFB_error level, char* format, ...);
+extern void jsonDumpObject (json_object * jObject);
+
+extern char *configTime        (void);
 
 // rest-api
-PUBLIC void endPostRequest(AFB_PostHandle *posthandle); 
-PUBLIC int doRestApi(struct MHD_Connection *connection, AFB_session *session, const char* url, const char *method
+extern void endPostRequest(AFB_PostHandle *posthandle); 
+extern int doRestApi(struct MHD_Connection *connection, AFB_session *session, const char* url, const char *method
     , const char *upload_data, size_t *upload_data_size, void **con_cls);
 
-void initPlugins (AFB_session *session);
+extern void initPlugins (AFB_session *session);
 
-typedef AFB_plugin* (*AFB_pluginCB)();
-PUBLIC  AFB_plugin* pluginRegister ();
+extern  AFB_plugin* pluginRegister ();
 
 // Session handling
-PUBLIC AFB_error sessionCheckdir     (AFB_session *session);
-PUBLIC json_object *sessionList      (AFB_session *session, AFB_request *request);
-PUBLIC json_object *sessionToDisk    (AFB_session *session, AFB_request *request, char *name,json_object *jsonSession);
-PUBLIC json_object *sessionFromDisk  (AFB_session *session, AFB_request *request, char *name);
+extern AFB_error sessionCheckdir     (AFB_session *session);
+extern json_object *sessionList      (AFB_session *session, AFB_request *request);
+extern json_object *sessionToDisk    (AFB_session *session, AFB_request *request, char *name,json_object *jsonSession);
+extern json_object *sessionFromDisk  (AFB_session *session, AFB_request *request, char *name);
 
-PUBLIC AFB_error ctxTokenRefresh (AFB_clientCtx *clientCtx, AFB_request *request);
-PUBLIC AFB_error ctxTokenCreate (AFB_clientCtx *clientCtx, AFB_request *request);
-PUBLIC AFB_error ctxTokenCheck (AFB_clientCtx *clientCtx, AFB_request *request);
-PUBLIC AFB_error ctxTokenReset (AFB_clientCtx *clientCtx, AFB_request *request);
-PUBLIC AFB_clientCtx *ctxClientGet (AFB_request *request, int idx);
-PUBLIC void      ctxStoreInit (int);
+extern AFB_error ctxTokenRefresh (AFB_clientCtx *clientCtx, AFB_request *request);
+extern AFB_error ctxTokenCreate (AFB_clientCtx *clientCtx, AFB_request *request);
+extern AFB_error ctxTokenCheck (AFB_clientCtx *clientCtx, AFB_request *request);
+extern AFB_error ctxTokenReset (AFB_clientCtx *clientCtx, AFB_request *request);
+extern AFB_clientCtx *ctxClientGet (AFB_request *request, int idx);
+extern void      ctxStoreInit (int);
 
 
 
 // Httpd server
-PUBLIC AFB_error httpdStart          (AFB_session *session);
-PUBLIC AFB_error httpdLoop           (AFB_session *session);
-PUBLIC void  httpdStop               (AFB_session *session);
+extern AFB_error httpdStart          (AFB_session *session);
+extern AFB_error httpdLoop           (AFB_session *session);
+extern void  httpdStop               (AFB_session *session);
 
 
 // config management
-PUBLIC char *configTime        (void);
-PUBLIC AFB_session *configInit (void);
-PUBLIC json_object *jsonNewMessage (AFB_error level, char* format, ...);
-PUBLIC json_object *jsonNewStatus (AFB_error level);
-PUBLIC json_object *jsonNewjtype (void);
-PUBLIC json_object *jsonNewMessage (AFB_error level, char* format, ...);
-PUBLIC void jsonDumpObject (json_object * jObject);
-PUBLIC AFB_error configLoadFile (AFB_session * session, AFB_config *cliconfig);
-PUBLIC void configStoreFile (AFB_session * session);
+extern AFB_session *configInit (void);
+extern AFB_error configLoadFile (AFB_session * session, AFB_config *cliconfig);
 
 
index a64e131..32336a7 100644 (file)
 
 PUBLIC  char *ERROR_LABEL[]=ERROR_LABEL_DEF;
 
-PUBLIC int verbose;
-STATIC AFB_errorT   AFBerr [AFB_SUCCESS+1];
-STATIC json_object *jTypeStatic;
-
-/* ------------------------------------------------------------------------------
- * Get localtime and return in a string
- * ------------------------------------------------------------------------------ */
-
-PUBLIC char * configTime (void) {
-  static char reqTime [26];
-  time_t tt;
-  struct tm *rt;
-
-  /* Get actual Date and Time */
-  time (&tt);
-  rt = localtime (&tt);
-
-  strftime (reqTime, sizeof (reqTime), "(%d-%b %H:%M)",rt);
-
-  // return pointer on static data
-  return (reqTime);
-}
-
 // load config from disk and merge with CLI option
 PUBLIC AFB_error configLoadFile (AFB_session * session, AFB_config *cliconfig) {
    static char cacheTimeout [10];
@@ -229,129 +206,11 @@ PUBLIC AFB_error configLoadFile (AFB_session * session, AFB_config *cliconfig) {
    return AFB_SUCCESS;
 }
 
-// Save the config on disk
-PUBLIC void configStoreFile (AFB_session * session) {
-   json_object * AFBConfig;
-   time_t rawtime;
-   struct tm * timeinfo;
-   int err;
-
-   AFBConfig =  json_object_new_object();
-
-   // add a timestamp and store session on disk
-   time ( &rawtime );  timeinfo = localtime ( &rawtime );
-   // A copy of the string is made and the memory is managed by the json_object
-   json_object_object_add (AFBConfig, "jtype"         , json_object_new_string (AFB_CONFIG_JTYPE));
-   json_object_object_add (AFBConfig, "timestamp"     , json_object_new_string (asctime (timeinfo)));
-   json_object_object_add (AFBConfig, "rootdir"       , json_object_new_string (session->config->rootdir));
-   json_object_object_add (AFBConfig, "rootapi"       , json_object_new_string (session->config->rootapi));
-   json_object_object_add (AFBConfig, "rootbase"      , json_object_new_string (session->config->rootbase));
-   json_object_object_add (AFBConfig, "plugins"       , json_object_new_string (session->config->ldpaths));
-   json_object_object_add (AFBConfig, "sessiondir"    , json_object_new_string (session->config->sessiondir));
-   json_object_object_add (AFBConfig, "pidfile"       , json_object_new_string (session->config->pidfile));
-   json_object_object_add (AFBConfig, "setuid"        , json_object_new_string (session->config->setuid));
-   json_object_object_add (AFBConfig, "httpdPort"     , json_object_new_int (session->config->httpdPort));
-   json_object_object_add (AFBConfig, "localhostonly" , json_object_new_int (session->config->localhostOnly));
-   json_object_object_add (AFBConfig, "cachetimeout"  , json_object_new_int (session->config->cacheTimeout));
-   json_object_object_add (AFBConfig, "apitimeout"    , json_object_new_int (session->config->apiTimeout));
-   json_object_object_add (AFBConfig, "cntxtimeout"   , json_object_new_int (session->config->cntxTimeout));
-
-   err = json_object_to_file (session->config->configfile, AFBConfig);
-   json_object_put   (AFBConfig);    // decrease reference count to free the json object
-   if (err < 0) {
-      fprintf(stderr, "AFB: Fail to save config on disk [%s]\n ", session->config->configfile);
-   }
-}
-
-
-PUBLIC AFB_session *configInit () {
-
-  AFB_session *session;
-  AFB_config  *config;
-  int idx, verbosesav;
-
-
-  session = malloc (sizeof (AFB_session));
-  memset (session,0, sizeof (AFB_session));
-
-  // create config handle
-  config = malloc (sizeof (AFB_config));
-  memset (config,0, sizeof (AFB_config));
-
-  // stack config handle into session
-  session->config = config;
-
-  jTypeStatic = json_object_new_string ("AFB_message");
-
-  // initialise JSON constant messages and increase reference count to make them permanent
-  verbosesav = verbose;
-  verbose = 0;  // run initialisation in silent mode
-
-
-  for (idx = 0; idx <= AFB_SUCCESS; idx++) {
-     AFBerr[idx].level = idx;
-     AFBerr[idx].label = ERROR_LABEL [idx];
-     AFBerr[idx].json  = jsonNewMessage (idx, NULL);
-  }
-  verbose = verbosesav;
-  
-  return (session);
-}
-
-
-// get JSON object from error level and increase its reference count
-PUBLIC json_object *jsonNewStatus (AFB_error level) {
-
-  json_object *target =  AFBerr[level].json;
-  json_object_get (target);
-
-  return (target);
-}
-
-// get AFB object type with adequate usage count
-PUBLIC json_object *jsonNewjtype (void) {
-  json_object_get (jTypeStatic); // increase reference count
-  return (jTypeStatic);
-}
 
-// build an ERROR message and return it as a valid json object
-PUBLIC  json_object *jsonNewMessage (AFB_error level, char* format, ...) {
-   static int count = 0;
-   json_object * AFBResponse;
-   va_list args;
-   char message [512];
-
-   // format message
-   if (format != NULL) {
-       va_start(args, format);
-       vsnprintf (message, sizeof (message), format, args);
-       va_end(args);
-   }
-
-   AFBResponse = json_object_new_object();
-   json_object_object_add (AFBResponse, "jtype", jsonNewjtype ());
-   json_object_object_add (AFBResponse, "status" , json_object_new_string (ERROR_LABEL[level]));
-   if (format != NULL) {
-        json_object_object_add (AFBResponse, "info"   , json_object_new_string (message));
-   }
-   if (verbose) {
-        fprintf (stderr, "AFB:%-6s [%3d]: ", AFBerr [level].label, count++);
-        if (format != NULL) {
-            fprintf (stderr, "%s", message);
-        } else {
-            fprintf (stderr, "No Message");
-        }
-        fprintf (stderr, "\n");
-   }
-
-   return (AFBResponse);
-}
-
-// Dump a message on stderr
-PUBLIC void jsonDumpObject (json_object * jObject) {
-
-   if (verbose) {
-        fprintf (stderr, "AFB:dump [%s]\n", json_object_to_json_string(jObject));
-   }
+PUBLIC AFB_session *configInit ()
+{
+  AFB_session *session = calloc (1, sizeof (AFB_session));
+  session->config = calloc (1, sizeof (AFB_config));
+  return session;
 }
 
index b376350..7684469 100644 (file)
@@ -30,8 +30,32 @@ typedef struct {
      size_t  len;
 } queryHandleT;
 
+static AFB_errorT   AFBerr [AFB_SUCCESS+1];
+static json_object *jTypeStatic;
+PUBLIC int verbose;
+
+/* ------------------------------------------------------------------------------
+ * Get localtime and return in a string
+ * ------------------------------------------------------------------------------ */
+
+PUBLIC char * configTime (void) {
+  static char reqTime [26];
+  time_t tt;
+  struct tm *rt;
+
+  /* Get actual Date and Time */
+  time (&tt);
+  rt = localtime (&tt);
+
+  strftime (reqTime, sizeof (reqTime), "(%d-%b %H:%M)",rt);
+
+  // return pointer on static data
+  return (reqTime);
+}
+
+
 // Sample Generic Ping Debug API
-PUBLIC json_object* getPingTest(AFB_request *request) {
+json_object* getPingTest(AFB_request *request) {
     static int pingcount = 0;
     json_object *response;
     char query  [256];
@@ -53,14 +77,14 @@ PUBLIC json_object* getPingTest(AFB_request *request) {
 
 
 // Helper to retrieve argument from  connection
-PUBLIC const char* getQueryValue(const AFB_request * request, const char *name) {
+const char* getQueryValue(const AFB_request * request, const char *name) {
     const char *value;
 
     value = MHD_lookup_connection_value(request->connection, MHD_GET_ARGUMENT_KIND, name);
     return (value);
 }
 
-STATIC int getQueryCB (void*handle, enum MHD_ValueKind kind, const char *key, const char *value) {
+static int getQueryCB (void*handle, enum MHD_ValueKind kind, const char *key, const char *value) {
     queryHandleT *query = (queryHandleT*)handle;
         
     query->idx += snprintf (&query->msg[query->idx],query->len," %s: \'%s\',", key, value);
@@ -68,25 +92,25 @@ STATIC int getQueryCB (void*handle, enum MHD_ValueKind kind, const char *key, co
 }
 
 // Helper to retrieve argument from  connection
-PUBLIC int getQueryAll(AFB_request * request, char *buffer, size_t len) {
+int getQueryAll(AFB_request * request, char *buffer, size_t len) {
     queryHandleT query;
     buffer[0] = '\0'; // start with an empty string
-    query.msg= buffer;
-    query.len= len;
-    query.idx= 0;
+    query.msg = buffer;
+    query.len = len;
+    query.idx = 0;
 
     MHD_get_connection_values (request->connection, MHD_GET_ARGUMENT_KIND, getQueryCB, &query);
     return (len);
 }
 
 // Helper to retrieve POST handle
-PUBLIC AFB_PostHandle* getPostHandle (AFB_request *request) {
+AFB_PostHandle* getPostHandle (AFB_request *request) {
     if (request->post == NULL) return (NULL);
     return ((AFB_PostHandle*) request->post->data);
 }
 
 // Helper to retrieve POST file context
-PUBLIC AFB_PostCtx* getPostContext (AFB_request *request) {
+AFB_PostCtx* getPostContext (AFB_request *request) {
     AFB_PostHandle* postHandle;
     if (request->post == NULL) return (NULL);
     
@@ -96,7 +120,7 @@ PUBLIC AFB_PostCtx* getPostContext (AFB_request *request) {
     return ((AFB_PostCtx*) postHandle->ctx);
 }
 
-PUBLIC char* getPostPath (AFB_request *request) {
+char* getPostPath (AFB_request *request) {
     AFB_PostHandle *postHandle = getPostHandle(request);
     AFB_PostCtx *postFileCtx;
     
@@ -108,7 +132,7 @@ PUBLIC char* getPostPath (AFB_request *request) {
     return (postFileCtx->path);
 }
 
-PUBLIC json_object* getPostFile (AFB_request *request, AFB_PostItem *item, char* destination) {
+json_object* getPostFile (AFB_request *request, AFB_PostItem *item, char* destination) {
 
     AFB_PostHandle *postHandle = getPostHandle(request);
     AFB_PostCtx *postFileCtx;
@@ -216,3 +240,88 @@ ExitOnError:
     request->errcode = MHD_HTTP_EXPECTATION_FAILED;
     return NULL;
 }
+
+
+
+static void jsoninit()
+{
+  int idx, verbosesav;
+
+  if (jTypeStatic)
+       return;
+
+  // initialise JSON constant messages and increase reference count to make them permanent
+  verbosesav = verbose;
+  verbose = 0;  // run initialisation in silent mode
+  jTypeStatic = json_object_new_string ("AFB_message");
+  for (idx = 0; idx <= AFB_SUCCESS; idx++) {
+     AFBerr[idx].level = idx;
+     AFBerr[idx].label = ERROR_LABEL [idx];
+     AFBerr[idx].json  = jsonNewMessage (idx, NULL);
+  }
+  verbose = verbosesav;
+}
+
+
+
+// get JSON object from error level and increase its reference count
+struct json_object *jsonNewStatus (AFB_error level)
+{
+  jsoninit();
+  json_object *target =  AFBerr[level].json;
+  json_object_get (target);
+
+  return (target);
+}
+
+// get AFB object type with adequate usage count
+struct json_object *jsonNewjtype (void)
+{
+  jsoninit();
+  json_object_get (jTypeStatic); // increase reference count
+  return (jTypeStatic);
+}
+
+// build an ERROR message and return it as a valid json object
+struct json_object *jsonNewMessage (AFB_error level, char* format, ...) {
+   static int count = 0;
+   json_object * AFBResponse;
+   va_list args;
+   char message [512];
+
+  jsoninit();
+
+   // format message
+   if (format != NULL) {
+       va_start(args, format);
+       vsnprintf (message, sizeof (message), format, args);
+       va_end(args);
+   }
+
+   AFBResponse = json_object_new_object();
+   json_object_object_add (AFBResponse, "jtype", jsonNewjtype ());
+   json_object_object_add (AFBResponse, "status" , json_object_new_string (ERROR_LABEL[level]));
+   if (format != NULL) {
+        json_object_object_add (AFBResponse, "info"   , json_object_new_string (message));
+   }
+   if (verbose) {
+        fprintf (stderr, "AFB:%-6s [%3d]: ", AFBerr [level].label, count++);
+        if (format != NULL) {
+            fprintf (stderr, "%s", message);
+        } else {
+            fprintf (stderr, "No Message");
+        }
+        fprintf (stderr, "\n");
+   }
+
+   return (AFBResponse);
+}
+
+// Dump a message on stderr
+void jsonDumpObject (struct json_object * jObject) {
+
+   if (verbose) {
+        fprintf (stderr, "AFB:dump [%s]\n", json_object_to_json_string(jObject));
+   }
+}
+