automotive-dlt
dlt-dbus-options.c
Go to the documentation of this file.
1 /*
2  * @licence app begin@
3  * SPDX license identifier: MPL-2.0
4  *
5  * Copyright (C) 2011-2015, BMW AG
6  *
7  * This file is part of GENIVI Project DLT - Diagnostic Log and Trace.
8  *
9  * This Source Code Form is subject to the terms of the
10  * Mozilla Public License (MPL), v. 2.0.
11  * If a copy of the MPL was not distributed with this file,
12  * You can obtain one at http://mozilla.org/MPL/2.0/.
13  *
14  * For further information see http://www.genivi.org/.
15  * @licence end@
16  */
17 
29 #include "dlt-dbus.h"
30 
31 #include <stdlib.h>
32 #include <string.h>
33 
37 void usage(char *prog_name)
38 {
39  char version[255];
40  dlt_get_version(version,255);
41 
42  printf("Usage: %s [options]\n", prog_name);
43  printf("Application to forward dbus messages to DLT.\n");
44  printf("%s\n", version);
45  printf("Options:\n");
46  printf(" -d Daemonize. Detach from terminal and run in background.\n");
47  printf(" -c filename Use configuration file. \n");
48  printf(" -a appid Used application id. \n");
49  printf(" Default: %s\n", DEFAULT_CONF_FILE);
50  printf(" -b type Used bus type. \n");
51  printf(" Session = 0, System = 1.\n");
52  printf(" -h This help message.\n");
53 }
54 
59 {
61  options->ApplicationId = 0;
62  options->BusType = 0;
63  options->Daemonize = 0;
64 }
65 
69 int read_command_line(DltDBusCliOptions *options, int argc, char *argv[])
70 {
71  init_cli_options(options);
72  int opt;
73 
74  while((opt = getopt(argc, argv, "c:b:a:hd")) != -1)
75  {
76  switch(opt) {
77  case 'd':
78  {
79  options->Daemonize = 1;
80  break;
81  }
82  case 'b':
83  {
84  options->BusType = malloc(strlen(optarg)+1);
85  MALLOC_ASSERT(options->BusType);
86  strcpy(options->BusType, optarg); /* strcpy unritical here, because size matches exactly the size to be copied */
87  break;
88  }
89  case 'a':
90  {
91  options->ApplicationId = malloc(strlen(optarg)+1);
92  MALLOC_ASSERT(options->ApplicationId);
93  strcpy(options->ApplicationId, optarg); /* strcpy unritical here, because size matches exactly the size to be copied */
94  break;
95  }
96  case 'c':
97  {
98  options->ConfigurationFileName = malloc(strlen(optarg)+1);
100  strcpy(options->ConfigurationFileName, optarg); /* strcpy unritical here, because size matches exactly the size to be copied */
101  break;
102  }
103  case 'h':
104  {
105  usage(argv[0]);
106  exit(0);
107  return -1;//for parasoft
108  }
109  default:
110  {
111  fprintf(stderr, "Unknown option '%c'\n", optopt);
112  usage(argv[0]);
113  return -1;
114  }
115  }
116  }
117  return 0;
118 }
119 
124 {
125  // Common
126  config->ApplicationId = "IPC0";
127 
128  // DBus
129  config->DBus.ContextId = "ALL";
130  config->DBus.BusType = 0;
131  config->DBus.FilterCount = 0;
132 
133 }
134 
139 {
140  FILE *file;
141  char *line, *token, *value, *filter, *pch;
142  int ret = 0;
143  char *filterBegin,*filterEnd;
144 
145  init_configuration(config);
146 
147  file = fopen(file_name, "r");
148 
149  if(file == NULL)
150  {
151  fprintf(stderr, "dlt-dbus-options, could not open configuration file.\n");
152  return -1;
153  }
154 
155  line = malloc(MAX_LINE);
156  token = malloc(MAX_LINE);
157  value = malloc(MAX_LINE);
158  filter = malloc(MAX_LINE);
159 
160  MALLOC_ASSERT(line);
161  MALLOC_ASSERT(token);
162  MALLOC_ASSERT(value);
163  MALLOC_ASSERT(filter);
164 
165  while(fgets(line, MAX_LINE, file) != NULL)
166  {
167  token[0] = 0;
168  value[0] = 0;
169  filter[0] = 0;
170 
171  filterBegin = strchr(line,'=');
172  filterEnd = strpbrk (line,"\r\n");
173 
174  if(filterBegin)
175  {
176  if(filterEnd && (filterEnd>filterBegin))
177  {
178  strncpy(filter,filterBegin+1,filterEnd-filterBegin-1);
179  filter[filterEnd-filterBegin-1]=0;
180  }
181  else
182  {
183  strcpy(filter,filterBegin+1);
184  }
185  }
186 
187  pch = strtok (line, " =\r\n");
188  while(pch != NULL)
189  {
190  if(pch[0] == '#')
191  break;
192 
193  if(token[0] == 0)
194  {
195  strncpy(token, pch, MAX_LINE-1);
196  token[MAX_LINE-1]=0;
197  }
198  else
199  {
200  strncpy(value, pch, MAX_LINE);
201  value[MAX_LINE-1]=0;
202  break;
203  }
204 
205  pch = strtok (NULL, " =\r\n");
206  }
207 
208  if(token[0] && value[0])
209  {
210  // Common
211  if(strcmp(token, "ApplicationId") == 0)
212  {
213  config->ApplicationId = malloc(strlen(value)+1);
214  MALLOC_ASSERT(config->ApplicationId);
215  strcpy(config->ApplicationId, value); /* strcpy unritical here, because size matches exactly the size to be copied */
216  }
217  // ContextId
218  else if(strcmp(token, "ContextId") == 0)
219  {
220  config->DBus.ContextId = malloc(strlen(value)+1);
221  MALLOC_ASSERT(config->DBus.ContextId);
222  strcpy(config->DBus.ContextId, value); /* strcpy unritical here, because size matches exactly the size to be copied */
223  }
224  // BusType
225  else if(strcmp(token, "BusType") == 0)
226  {
227  config->DBus.BusType = malloc(strlen(value)+1);
228  MALLOC_ASSERT(config->DBus.BusType);
229  strcpy(config->DBus.BusType, value); /* strcpy unritical here, because size matches exactly the size to be copied */
230  }
231  // BusType
232  else if(strcmp(token, "FilterMatch") == 0)
233  {
235  {
236  config->DBus.FilterMatch[config->DBus.FilterCount]= malloc(strlen(filter)+1);
237  MALLOC_ASSERT(config->DBus.FilterMatch[config->DBus.FilterCount]);
238  strcpy(config->DBus.FilterMatch[config->DBus.FilterCount], filter);
239  config->DBus.FilterCount++;
240  }
241  }
242  }
243  }
244  fclose(file);
245  free(value);
246  free(token);
247  free(filter);
248  free(line);
249  return ret;
250 }
#define DLT_DBUS_FILTER_MAX
Definition: dlt-dbus.h:36
#define DEFAULT_CONF_FILE
Definition: dlt-dbus.h:34
DBusOptions DBus
Definition: dlt-dbus.h:64
char * ApplicationId
Definition: dlt-dbus.h:63
DltKpiConfig config
Definition: dlt-kpi.c:37
int FilterCount
Definition: dlt-dbus.h:58
char * FilterMatch[DLT_DBUS_FILTER_MAX]
Definition: dlt-dbus.h:59
char * ContextId
Definition: dlt-dbus.h:56
void usage(char *prog_name)
char * ConfigurationFileName
Definition: dlt-dbus.h:48
void init_cli_options(DltDBusCliOptions *options)
int read_configuration_file(DltDBusConfiguration *config, char *file_name)
void init_configuration(DltDBusConfiguration *config)
char * BusType
Definition: dlt-dbus.h:57
char * BusType
Definition: dlt-dbus.h:50
int read_command_line(DltDBusCliOptions *options, int argc, char *argv[])
char * ApplicationId
Definition: dlt-dbus.h:49
#define MALLOC_ASSERT(x)
Definition: dlt-dbus.h:40
void dlt_get_version(char *buf, size_t size)
Definition: dlt_common.c:3239
#define MAX_LINE
Definition: dlt-dbus.h:44
#define NULL
Definition: dlt_common.h:232