X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fconfig.c;h=6f23664b5ab2b86e1271253f1fc2f0f712910955;hb=631b0be76caa4ad4bbbbbfe1ca333dc9aa192ce0;hp=29d7241dc80aa48504ac957c8cb319b8e998928f;hpb=e7c246a1b0d30b8156c7033061a61ecb5d2bdfc8;p=src%2Fapp-framework-binder.git diff --git a/src/config.c b/src/config.c index 29d7241d..6f23664b 100644 --- a/src/config.c +++ b/src/config.c @@ -21,39 +21,13 @@ */ - #include "../include/local-def.h" #include #include #include - #define AFB_CONFIG_JTYPE "AFB_config" -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) { @@ -63,7 +37,8 @@ PUBLIC AFB_error configLoadFile (AFB_session * session, AFB_config *cliconfig) { // TBD integrate alias-dir array with config-file session->config->aliasdir = cliconfig->aliasdir; - + session->config->mode = cliconfig->mode; + // default HTTP port if (cliconfig->httpdPort == 0) session->config->httpdPort=1234; else session->config->httpdPort=cliconfig->httpdPort; @@ -71,6 +46,10 @@ PUBLIC AFB_error configLoadFile (AFB_session * session, AFB_config *cliconfig) { // default Plugin API timeout if (cliconfig->apiTimeout == 0) session->config->apiTimeout=DEFLT_API_TIMEOUT; else session->config->apiTimeout=cliconfig->apiTimeout; + + // default AUTH_TOKEN + if (cliconfig->token == NULL) session->config->token= DEFLT_AUTH_TOKEN; + else session->config->token=cliconfig->token; // cache timeout default one hour if (cliconfig->cacheTimeout == 0) session->config->cacheTimeout=DEFLT_CACHE_TIMEOUT; @@ -106,16 +85,10 @@ PUBLIC AFB_error configLoadFile (AFB_session * session, AFB_config *cliconfig) { session->config->rootapi= cliconfig->rootapi; } - if (cliconfig->smack == NULL) { - session->config->smack = "demo"; - } else { - session->config->smack= cliconfig->smack; - } - - if (cliconfig->smack == NULL) { - session->config->plugins = "all"; + if (cliconfig->ldpaths == NULL) { + session->config->ldpaths = PLUGIN_INSTALL_DIR; } else { - session->config->plugins= cliconfig->plugins; + session->config->ldpaths= cliconfig->ldpaths; } // if no session dir create a default path from rootdir @@ -184,12 +157,8 @@ PUBLIC AFB_error configLoadFile (AFB_session * session, AFB_config *cliconfig) { session->config->rootapi = strdup (json_object_get_string (value)); } - if (!cliconfig->smack && json_object_object_get_ex (AFBConfig, "smack", &value)) { - session->config->smack = strdup (json_object_get_string (value)); - } - - if (!cliconfig->plugins && json_object_object_get_ex (AFBConfig, "plugins", &value)) { - session->config->plugins = strdup (json_object_get_string (value)); + if (!cliconfig->ldpaths && json_object_object_get_ex (AFBConfig, "plugins", &value)) { + session->config->ldpaths = strdup (json_object_get_string (value)); } if (!cliconfig->setuid && json_object_object_get_ex (AFBConfig, "setuid", &value)) { @@ -236,133 +205,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, "smack" , json_object_new_string (session->config->smack)); - json_object_object_add (AFBConfig, "plugins" , json_object_new_string (session->config->plugins)); - 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; - - // Load Plugins - initPlugins (session); - - 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; }