X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fhelper-api.c;h=7684469b0c198880c72d400a41bef783d758ad4d;hb=fc0da145ff8e9b8cea1c40fa3025596fb8ffaae8;hp=12539978f5eb87c077e0d0474b4e99daa565a701;hpb=b2247ca634420ed0d5ecc1743834ea4e0666aa2a;p=src%2Fapp-framework-binder.git diff --git a/src/helper-api.c b/src/helper-api.c index 12539978..7684469b 100644 --- a/src/helper-api.c +++ b/src/helper-api.c @@ -19,6 +19,8 @@ #include "../include/local-def.h" #include +#include +#include // handle to hold queryAll values @@ -28,9 +30,33 @@ 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) { - static pingcount = 0; +json_object* getPingTest(AFB_request *request) { + static int pingcount = 0; json_object *response; char query [256]; char session[256]; @@ -51,39 +77,40 @@ 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); + return MHD_YES; /* continue to iterate */ } // 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); @@ -93,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; @@ -105,12 +132,12 @@ 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; char filepath[512]; - int len; + ssize_t len; // This is called after PostForm and then after DonePostForm if (item == NULL) { @@ -124,11 +151,9 @@ PUBLIC json_object* getPostFile (AFB_request *request, AFB_PostItem *item, char* } // We have a context but last Xform iteration fail or application set a message - if (postFileCtx->jresp != NULL) { - jresp = postFileCtx->jresp; // retrieve previous error from postCtx - if (postFileCtx->errcode != 0) request->errcode=postFileCtx->errcode; - } - else jresp = jsonNewMessage(AFB_FAIL,"getPostFile Post Request done"); + if (request->jresp != NULL) { + jresp = request->jresp; // retrieve previous error from postCtx + } else jresp = jsonNewMessage(AFB_SUCCESS,"getPostFile Post Request done"); // Error or not let's free all resources close(postFileCtx->fd); @@ -136,7 +161,7 @@ PUBLIC json_object* getPostFile (AFB_request *request, AFB_PostItem *item, char* free (postFileCtx); return (jresp); } - +#if defined(PLEASE_FIX_ME_THE_ERROR_IS_postFileCtx_NOT_INITIALIZED) // Make sure it's a valid PostForm request if (!request->post && request->post->type != AFB_POST_FORM) { postFileCtx->jresp= jsonNewMessage(AFB_FAIL,"This is not a valid PostForm request\n"); @@ -154,7 +179,7 @@ PUBLIC json_object* getPostFile (AFB_request *request, AFB_PostItem *item, char* postFileCtx->jresp= jsonNewMessage(AFB_FAIL,"Buffer size NULL key=%s]\n", item->key); goto ExitOnError; } - +#endif // Extract Application Context from posthandle [NULL == 1st iteration] postFileCtx = (AFB_PostCtx*) postHandle->ctx; @@ -164,7 +189,6 @@ PUBLIC json_object* getPostFile (AFB_request *request, AFB_PostItem *item, char* // Create an application specific context postFileCtx = calloc (1, sizeof(AFB_PostCtx)); // May place anything here until post->completeCB handle resources liberation - postFileCtx->path = strdup (filepath); // attach application to postHandle postHandle->ctx = (void*) postFileCtx; // May place anything here until post->completeCB handle resources liberation @@ -175,12 +199,12 @@ PUBLIC json_object* getPostFile (AFB_request *request, AFB_PostItem *item, char* strncat (filepath, "/", sizeof(filepath)); strncat (filepath, destination, sizeof(filepath)); } else strncpy (filepath, destination, sizeof(filepath)); - + // make sure destination directory exist destDir = opendir (filepath); if (destDir == NULL) { - if ( 0 <= mkdir(filepath,O_RDWR | S_IRWXU | S_IRGRP)) { + if (mkdir(filepath,O_RDWR | S_IRWXU | S_IRGRP) < 0) { postFileCtx->jresp= jsonNewMessage(AFB_FAIL,"Fail to Create destination directory=[%s] error=%s\n", filepath, strerror(errno)); goto ExitOnError; } @@ -189,6 +213,9 @@ PUBLIC json_object* getPostFile (AFB_request *request, AFB_PostItem *item, char* strncat (filepath, "/", sizeof(filepath)); strncat (filepath, item->filename, sizeof(filepath)); + postFileCtx->path = strdup (filepath); + if (verbose) fprintf(stderr, "getPostFile path=%s\n", filepath); + if((postFileCtx->fd = open(filepath, O_RDWR |O_CREAT, S_IRWXU|S_IRGRP)) <= 0) { postFileCtx->jresp= jsonNewMessage(AFB_FAIL,"Fail to Create destination File=[%s] error=%s\n", filepath, strerror(errno)); goto ExitOnError; @@ -200,7 +227,7 @@ PUBLIC json_object* getPostFile (AFB_request *request, AFB_PostItem *item, char* // Check we successfully wrote full buffer len = write (postFileCtx->fd, item->data, item->len); - if (item->len != len) { + if ((ssize_t)item->len != len) { postFileCtx->jresp= jsonNewMessage(AFB_FAIL,"Fail to write file [%s] at [%s] error=\n", item->filename, strerror(errno)); goto ExitOnError; } @@ -213,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)); + } +} +