diff options
Diffstat (limited to 'src/dbus')
-rw-r--r-- | src/dbus/dlt-dbus-options.c | 182 | ||||
-rw-r--r-- | src/dbus/dlt-dbus.c | 134 | ||||
-rw-r--r-- | src/dbus/dlt-dbus.h | 30 |
3 files changed, 168 insertions, 178 deletions
diff --git a/src/dbus/dlt-dbus-options.c b/src/dbus/dlt-dbus-options.c index 27bf4f4..fc5b584 100644 --- a/src/dbus/dlt-dbus-options.c +++ b/src/dbus/dlt-dbus-options.c @@ -37,7 +37,7 @@ void usage(char *prog_name) { char version[255]; - dlt_get_version(version,255); + dlt_get_version(version, 255); printf("Usage: %s [options]\n", prog_name); printf("Application to forward dbus messages to DLT.\n"); @@ -57,10 +57,10 @@ void usage(char *prog_name) */ void init_cli_options(DltDBusCliOptions *options) { - options->ConfigurationFileName = DEFAULT_CONF_FILE; - options->ApplicationId = 0; - options->BusType = 0; - options->Daemonize = 0; + options->ConfigurationFileName = DEFAULT_CONF_FILE; + options->ApplicationId = 0; + options->BusType = 0; + options->Daemonize = 0; } /** @@ -71,49 +71,48 @@ int read_command_line(DltDBusCliOptions *options, int argc, char *argv[]) init_cli_options(options); int opt; - while((opt = getopt(argc, argv, "c:b:a:hd")) != -1) - { - switch(opt) { - case 'd': - { - options->Daemonize = 1; - break; - } - case 'b': - { - options->BusType = malloc(strlen(optarg)+1); - MALLOC_ASSERT(options->BusType); - strcpy(options->BusType, optarg); /* strcpy unritical here, because size matches exactly the size to be copied */ - break; - } - case 'a': - { - options->ApplicationId = malloc(strlen(optarg)+1); - MALLOC_ASSERT(options->ApplicationId); - strcpy(options->ApplicationId, optarg); /* strcpy unritical here, because size matches exactly the size to be copied */ - break; - } - case 'c': - { - options->ConfigurationFileName = malloc(strlen(optarg)+1); - MALLOC_ASSERT(options->ConfigurationFileName); - strcpy(options->ConfigurationFileName, optarg); /* strcpy unritical here, because size matches exactly the size to be copied */ - break; - } - case 'h': - { - usage(argv[0]); - exit(0); - return -1;//for parasoft - } - default: - { - fprintf(stderr, "Unknown option '%c'\n", optopt); - usage(argv[0]); - return -1; - } + while ((opt = getopt(argc, argv, "c:b:a:hd")) != -1) + switch (opt) { + case 'd': + { + options->Daemonize = 1; + break; } - } + case 'b': + { + options->BusType = malloc(strlen(optarg) + 1); + MALLOC_ASSERT(options->BusType); + strcpy(options->BusType, optarg); /* strcpy unritical here, because size matches exactly the size to be copied */ + break; + } + case 'a': + { + options->ApplicationId = malloc(strlen(optarg) + 1); + MALLOC_ASSERT(options->ApplicationId); + strcpy(options->ApplicationId, optarg); /* strcpy unritical here, because size matches exactly the size to be copied */ + break; + } + case 'c': + { + options->ConfigurationFileName = malloc(strlen(optarg) + 1); + MALLOC_ASSERT(options->ConfigurationFileName); + strcpy(options->ConfigurationFileName, optarg); /* strcpy unritical here, because size matches exactly the size to be copied */ + break; + } + case 'h': + { + usage(argv[0]); + exit(0); + return -1; /*for parasoft */ + } + default: + { + fprintf(stderr, "Unknown option '%c'\n", optopt); + usage(argv[0]); + return -1; + } + } + return 0; } @@ -122,13 +121,13 @@ int read_command_line(DltDBusCliOptions *options, int argc, char *argv[]) */ void init_configuration(DltDBusConfiguration *config) { - // Common - config->ApplicationId = "IPC0"; + /* Common */ + config->ApplicationId = "IPC0"; - // DBus - config->DBus.ContextId = "ALL"; - config->DBus.BusType = 0; - config->DBus.FilterCount = 0; + /* DBus */ + config->DBus.ContextId = "ALL"; + config->DBus.BusType = 0; + config->DBus.FilterCount = 0; } @@ -140,14 +139,13 @@ int read_configuration_file(DltDBusConfiguration *config, char *file_name) FILE *file; char *line, *token, *value, *filter, *pch; int ret = 0; - char *filterBegin,*filterEnd; + char *filterBegin, *filterEnd; init_configuration(config); file = fopen(file_name, "r"); - if(file == NULL) - { + if (file == NULL) { fprintf(stderr, "dlt-dbus-options, could not open configuration file.\n"); return -1; } @@ -162,78 +160,69 @@ int read_configuration_file(DltDBusConfiguration *config, char *file_name) MALLOC_ASSERT(value); MALLOC_ASSERT(filter); - while(fgets(line, MAX_LINE, file) != NULL) - { + while (fgets(line, MAX_LINE, file) != NULL) { token[0] = 0; value[0] = 0; filter[0] = 0; - filterBegin = strchr(line,'='); - filterEnd = strpbrk (line,"\r\n"); + filterBegin = strchr(line, '='); + filterEnd = strpbrk (line, "\r\n"); - if(filterBegin) - { - if(filterEnd && (filterEnd>filterBegin)) - { - strncpy(filter,filterBegin+1,filterEnd-filterBegin-1); - filter[filterEnd-filterBegin-1]=0; + if (filterBegin) { + if (filterEnd && (filterEnd > filterBegin)) { + strncpy(filter, filterBegin + 1, filterEnd - filterBegin - 1); + filter[filterEnd - filterBegin - 1] = 0; } - else - { - strcpy(filter,filterBegin+1); + else { + strcpy(filter, filterBegin + 1); } } pch = strtok (line, " =\r\n"); - while(pch != NULL) - { - if(pch[0] == '#') + + while (pch != NULL) { + if (pch[0] == '#') break; - if(token[0] == 0) - { - strncpy(token, pch, MAX_LINE-1); - token[MAX_LINE-1]=0; + if (token[0] == 0) { + strncpy(token, pch, MAX_LINE - 1); + token[MAX_LINE - 1] = 0; } - else - { + else { strncpy(value, pch, MAX_LINE); - value[MAX_LINE-1]=0; + value[MAX_LINE - 1] = 0; break; } pch = strtok (NULL, " =\r\n"); } - if(token[0] && value[0]) - { - // Common - if(strcmp(token, "ApplicationId") == 0) - { - config->ApplicationId = malloc(strlen(value)+1); + if (token[0] && value[0]) { + /* Common */ + if (strcmp(token, "ApplicationId") == 0) { + config->ApplicationId = malloc(strlen(value) + 1); MALLOC_ASSERT(config->ApplicationId); strcpy(config->ApplicationId, value); /* strcpy unritical here, because size matches exactly the size to be copied */ } - // ContextId - else if(strcmp(token, "ContextId") == 0) + /* ContextId */ + else if (strcmp(token, "ContextId") == 0) { - config->DBus.ContextId = malloc(strlen(value)+1); + config->DBus.ContextId = malloc(strlen(value) + 1); MALLOC_ASSERT(config->DBus.ContextId); strcpy(config->DBus.ContextId, value); /* strcpy unritical here, because size matches exactly the size to be copied */ } - // BusType - else if(strcmp(token, "BusType") == 0) + /* BusType */ + else if (strcmp(token, "BusType") == 0) { - config->DBus.BusType = malloc(strlen(value)+1); + config->DBus.BusType = malloc(strlen(value) + 1); MALLOC_ASSERT(config->DBus.BusType); strcpy(config->DBus.BusType, value); /* strcpy unritical here, because size matches exactly the size to be copied */ } - // BusType - else if(strcmp(token, "FilterMatch") == 0) + /* BusType */ + else if (strcmp(token, "FilterMatch") == 0) { - if(config->DBus.FilterCount<DLT_DBUS_FILTER_MAX) - { - config->DBus.FilterMatch[config->DBus.FilterCount]= malloc(strlen(filter)+1); + if (config->DBus.FilterCount < DLT_DBUS_FILTER_MAX) { + config->DBus.FilterMatch[config->DBus.FilterCount] = malloc(strlen(filter) + 1); MALLOC_ASSERT(config->DBus.FilterMatch[config->DBus.FilterCount]); strcpy(config->DBus.FilterMatch[config->DBus.FilterCount], filter); config->DBus.FilterCount++; @@ -241,6 +230,7 @@ int read_configuration_file(DltDBusConfiguration *config, char *file_name) } } } + fclose(file); free(value); free(token); diff --git a/src/dbus/dlt-dbus.c b/src/dbus/dlt-dbus.c index a42d02c..54dce33 100644 --- a/src/dbus/dlt-dbus.c +++ b/src/dbus/dlt-dbus.c @@ -45,40 +45,38 @@ filter_func (DBusConnection *con, DBusMessage *message, void *data) { - char **buf; - int len_p; - UNUSED(con); - UNUSED(data); - - buf = (char**)&dbus_message_buffer; - if (!dbus_message_marshal(message, - buf, - &len_p)) - { - fprintf (stderr, "Failed to serialize DBus message!\n"); - return DBUS_HANDLER_RESULT_HANDLED; - } - - DLT_TRACE_NETWORK_SEGMENTED(dbusContext,DLT_NW_TRACE_IPC,0,0,len_p,(void *)*buf); + char **buf; + int len_p; + UNUSED(con); + UNUSED(data); + + buf = (char **)&dbus_message_buffer; + + if (!dbus_message_marshal(message, + buf, + &len_p)) { + fprintf (stderr, "Failed to serialize DBus message!\n"); + return DBUS_HANDLER_RESULT_HANDLED; + } - free(*buf); - *buf = NULL; + DLT_TRACE_NETWORK_SEGMENTED(dbusContext, DLT_NW_TRACE_IPC, 0, 0, len_p, (void *)*buf); - if (dbus_message_is_signal (message, - DBUS_INTERFACE_LOCAL, - "Disconnected")) - { - DLT_UNREGISTER_CONTEXT (dbusContext); - DLT_UNREGISTER_APP (); - exit (0); - } + free(*buf); + *buf = NULL; + if (dbus_message_is_signal (message, + DBUS_INTERFACE_LOCAL, + "Disconnected")) { + DLT_UNREGISTER_CONTEXT (dbusContext); + DLT_UNREGISTER_APP (); + exit (0); + } - /* Conceptually we want this to be - * DBUS_HANDLER_RESULT_NOT_YET_HANDLED, but this raises - * some problems. See bug 1719. - */ - return DBUS_HANDLER_RESULT_HANDLED; + /* Conceptually we want this to be + * DBUS_HANDLER_RESULT_NOT_YET_HANDLED, but this raises + * some problems. See bug 1719. + */ + return DBUS_HANDLER_RESULT_HANDLED; } int main (int argc, char *argv[]) @@ -92,74 +90,76 @@ int main (int argc, char *argv[]) int num; - if(read_command_line(&options, argc, argv) < 0) - { + if (read_command_line(&options, argc, argv) < 0) { fprintf(stderr, "Failed to read command line!\n"); return -1; } - if(read_configuration_file(&config, options.ConfigurationFileName) < 0) - { + if (read_configuration_file(&config, options.ConfigurationFileName) < 0) { fprintf(stderr, "Failed to read configuration file!\n"); return -1; } - // register application - if(options.ApplicationId) + /* register application */ + if (options.ApplicationId) DLT_REGISTER_APP (options.ApplicationId, "DBus Logging"); else DLT_REGISTER_APP (config.ApplicationId, "DBus Logging"); - // register context - DLT_REGISTER_CONTEXT_LL_TS(dbusContext, config.DBus.ContextId, "DBus Context for Logging",DLT_LOG_INFO,DLT_TRACE_STATUS_ON); + /* register context */ + DLT_REGISTER_CONTEXT_LL_TS(dbusContext, + config.DBus.ContextId, + "DBus Context for Logging", + DLT_LOG_INFO, + DLT_TRACE_STATUS_ON); DLT_REGISTER_CONTEXT(dbusLog, "Log", "DBus Context for Logging Generic information"); - // initialise error handler + /* initialise error handler */ dbus_error_init (&error); - // set DBus bus type - if(options.BusType) - type = (DBusBusType) atoi(options.BusType); + /* set DBus bus type */ + if (options.BusType) + type = (DBusBusType)atoi(options.BusType); else - type = (DBusBusType) atoi(config.DBus.BusType); + type = (DBusBusType)atoi(config.DBus.BusType); - // get connection + /* get connection */ connection = dbus_bus_get (type, &error); - if(type==0) - DLT_LOG(dbusLog,DLT_LOG_INFO,DLT_STRING("BusType"),DLT_STRING("Session Bus")); - else if(type==1) - DLT_LOG(dbusLog,DLT_LOG_INFO,DLT_STRING("BusType"),DLT_STRING("System Bus")); + if (type == 0) + DLT_LOG(dbusLog, DLT_LOG_INFO, DLT_STRING("BusType"), DLT_STRING("Session Bus")); + else if (type == 1) + DLT_LOG(dbusLog, DLT_LOG_INFO, DLT_STRING("BusType"), DLT_STRING("System Bus")); else - DLT_LOG(dbusLog,DLT_LOG_INFO,DLT_STRING("BusType"),DLT_INT(type)); - - if (NULL == connection) - { - fprintf (stderr, "Failed to open connection to %d: %s\n", - DBUS_BUS_SYSTEM, - error.message); - dbus_error_free (&error); - exit (1); + DLT_LOG(dbusLog, DLT_LOG_INFO, DLT_STRING("BusType"), DLT_INT(type)); + + if (NULL == connection) { + fprintf (stderr, "Failed to open connection to %d: %s\n", + DBUS_BUS_SYSTEM, + error.message); + dbus_error_free (&error); + exit (1); } - for(num=0;num<config.DBus.FilterCount;num++) - { + for (num = 0; num < config.DBus.FilterCount; num++) { dbus_bus_add_match (connection, - config.DBus.FilterMatch[num], - &error); - printf("Added FilterMatch: %s\n",config.DBus.FilterMatch[num]); - DLT_LOG(dbusLog,DLT_LOG_INFO,DLT_STRING("FilterMatch"),DLT_UINT(num+1),DLT_STRING(config.DBus.FilterMatch[num])); + config.DBus.FilterMatch[num], + &error); + printf("Added FilterMatch: %s\n", config.DBus.FilterMatch[num]); + DLT_LOG(dbusLog, DLT_LOG_INFO, DLT_STRING("FilterMatch"), DLT_UINT(num + 1), + DLT_STRING(config.DBus.FilterMatch[num])); + if (dbus_error_is_set (&error)) - goto fail; + goto fail; } if (!dbus_connection_add_filter (connection, filter_func, NULL, NULL)) { - fprintf (stderr, "Couldn't add filter!\n"); - exit (1); + fprintf (stderr, "Couldn't add filter!\n"); + exit (1); } while (dbus_connection_read_write_dispatch(connection, -1)) - ; + ; DLT_UNREGISTER_CONTEXT (dbusContext); DLT_UNREGISTER_CONTEXT (dbusLog); diff --git a/src/dbus/dlt-dbus.h b/src/dbus/dlt-dbus.h index 50a26e8..6f553d9 100644 --- a/src/dbus/dlt-dbus.h +++ b/src/dbus/dlt-dbus.h @@ -27,7 +27,7 @@ #ifndef DLT_DBUS_H_ #define DLT_DBUS_H_ -// DLT related includes. +/* DLT related includes. */ #include "dlt.h" #include "dlt_common.h" @@ -35,33 +35,33 @@ #define DLT_DBUS_FILTER_MAX 32 -// Macros +/* Macros */ #define UNUSED(x) (void)(x) -#define MALLOC_ASSERT(x) if(x == NULL) {\ - fprintf(stderr, "Out of memory\n");\ - abort();} +#define MALLOC_ASSERT(x) if (x == NULL) { \ + fprintf(stderr, "Out of memory\n"); \ + abort(); } #define MAX_LINE 1024 -// Command line options +/* Command line options */ typedef struct { - char *ConfigurationFileName; - char *ApplicationId; - char *BusType; - int Daemonize; + char *ConfigurationFileName; + char *ApplicationId; + char *BusType; + int Daemonize; } DltDBusCliOptions; -// Configuration dbus options +/* Configuration dbus options */ typedef struct { - char *ContextId; - char *BusType; - int FilterCount; + char *ContextId; + char *BusType; + int FilterCount; char *FilterMatch[DLT_DBUS_FILTER_MAX]; } DBusOptions; typedef struct { char *ApplicationId; - DBusOptions DBus; + DBusOptions DBus; } DltDBusConfiguration; |