X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;ds=inline;f=src%2Fhelper-api.c;h=303fd57d1fd48960ed183f3439b10753f9d33c1e;hb=20f5ff02e455580c6e7129479f3328787b3333ff;hp=b3763505e761d27d9b4d68b1663ab141b797ad8d;hpb=2f5957984835ffc7d4f03bb0094a93c575e3eb17;p=src%2Fapp-framework-binder.git diff --git a/src/helper-api.c b/src/helper-api.c index b3763505..303fd57d 100644 --- a/src/helper-api.c +++ b/src/helper-api.c @@ -21,6 +21,7 @@ #include #include #include +#include // handle to hold queryAll values @@ -30,37 +31,31 @@ typedef struct { size_t len; } queryHandleT; -// Sample Generic Ping Debug API -PUBLIC json_object* getPingTest(AFB_request *request) { - static int pingcount = 0; - json_object *response; - char query [256]; - char session[256]; - int len; - - // request all query key/value - len = getQueryAll (request, query, sizeof(query)); - if (len == 0) strncpy (query, "NoSearchQueryList", sizeof(query)); - - // check if we have some post data - if (request->post == NULL) request->post->data="NoData"; - - // return response to caller - response = jsonNewMessage(AFB_SUCCESS, "Ping Binder Daemon count=%d uuid=%s query={%s} session={0x%x} PostData: [%s] " - , pingcount++, request->uuid, query, session, request->post->data); - return (response); -} +// Error code are requested through function to manage json usage count +typedef struct { + int level; + const char* label; + json_object *json; +} AFB_errorT; + +static AFB_errorT AFBerr [AFB_UNAUTH+1]; +static json_object *jTypeStatic; + +PUBLIC int verbose; + +static const char *ERROR_LABEL[] = {"false", "true", "fatal", "fail", "warning", "empty", "success", "done", "unauth"}; + // 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 +63,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 +91,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 +103,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 +211,61 @@ 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_UNAUTH; idx++) { + AFBerr[idx].level = idx; + AFBerr[idx].label = ERROR_LABEL [idx]; + AFBerr[idx].json = jsonNewMessage (idx, NULL); + } + verbose = verbosesav; +} + + +// 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", json_object_get (jTypeStatic)); + 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); +} +