automotive-dlt
dlt-kpi-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 
27 #include "dlt-kpi.h"
28 
32 void usage(char *prog_name)
33 {
34  char version[255];
35  dlt_get_version(version,255);
36 
37  printf("Usage: %s [options]\n", prog_name);
38  printf("Application to forward information from the /proc/ file system to DLT.\n");
39  printf("%s\n", version);
40  printf("Options:\n");
41  //printf(" -d Daemonize. Detach from terminal and run in background.\n");
42  printf(" -c filename Use configuration file. \n");
43  printf(" Default: %s\n", DEFAULT_CONF_FILE);
44  printf(" -h This help message.\n");
45 }
46 
51 {
53  options->customConfigFile = 0;
54 }
55 
57 {
58  if(options->customConfigFile)
59  free(options->configurationFileName);
60 }
61 
62 DltReturnValue dlt_kpi_read_command_line(DltKpiOptions *options, int argc, char **argv)
63 {
64  if(options == NULL)
65  {
66  fprintf(stderr, "%s: Nullpointer parameter\n", __func__);
68  }
69  dlt_kpi_init_cli_options(options);
70  int opt;
71 
72  while((opt = getopt(argc, argv, "c:h")) != -1)
73  {
74  switch(opt) {
75  case 'c':
76  {
77  if((options->configurationFileName = malloc(strlen(optarg)+1)) == 0)
78  {
79  fprintf(stderr, "Out of memory!\n");
80  return DLT_RETURN_ERROR;
81  }
82  strcpy(options->configurationFileName, optarg); /* strcpy unritical here, because size matches exactly the size to be copied */
83  options->customConfigFile = 1;
84  break;
85  }
86  case 'h':
87  {
88  usage(argv[0]);
89  exit(0);
90  return -1;//for parasoft
91  }
92  default:
93  {
94  fprintf(stderr, "Unknown option: %c\n", optopt);
95  usage(argv[0]);
96  return DLT_RETURN_ERROR;
97  }
98  }
99  }
100 
101  return DLT_RETURN_OK;
102 }
103 
108 {
109  config->process_log_interval = 1000;
110  config->irq_log_interval = 1000;
111  config->log_level = DLT_LOG_DEFAULT;
112 }
113 
118 {
119  FILE *file;
120  char *line = NULL;
121  char *token = NULL;
122  char *value = NULL;
123  char *pch, *strchk;
124  int tmp;
125 
126  if(config == NULL || file_name == NULL)
127  {
128  fprintf(stderr, "%s: Nullpointer parameter!\n", __func__);
130  }
131 
133 
134  file = fopen(file_name, "r");
135 
136  if(file == NULL)
137  {
138  fprintf(stderr, "%s: Could not open configuration file!\n", __func__);
139  return DLT_RETURN_ERROR;
140  }
141 
142  if(((line = malloc(COMMAND_LINE_SIZE)) == 0) ||
143  ((token = malloc(COMMAND_LINE_SIZE)) == 0) ||
144  ((value = malloc(COMMAND_LINE_SIZE)) == 0))
145 
146  {
147  fclose(file);
148  free(line);
149  free(token);
150  free(value);
151 
152  fprintf(stderr, "%s: Out of memory!\n", __func__);
153  return DLT_RETURN_ERROR;
154  }
155 
156  while(fgets(line, COMMAND_LINE_SIZE, file) != NULL)
157  {
158  token[0] = '\0';
159  value[0] = '\0';
160 
161  pch = strtok (line, " =\r\n");
162  while(pch != NULL)
163  {
164  if(pch[0] == '#')
165  break;
166 
167  if(token[0] == '\0')
168  {
169  strncpy(token, pch, COMMAND_LINE_SIZE-1);
170  token[COMMAND_LINE_SIZE-1] = '\0';
171  }
172  else
173  {
174  strncpy(value, pch, COMMAND_LINE_SIZE-1);
175  value[COMMAND_LINE_SIZE-1] = '\0';
176  break;
177  }
178 
179  pch = strtok(NULL, " =\r\n");
180  }
181 
182  if(token[0] != '\0' && value[0] != '\0')
183  {
184  if(strcmp(token, "process_interval") == '\0')
185  {
186  tmp = strtol(value, &strchk, 10);
187 
188  if(strchk[0] == '\0' && tmp > 0)
189  config->process_log_interval = tmp;
190  else
191  fprintf(stderr, "Error reading configuration file: %s is not a valid value for %s\n", value, token);
192  }
193  else if(strcmp(token, "irq_interval") == '\0')
194  {
195  tmp = strtol(value, &strchk, 10);
196 
197  if(strchk[0] == '\0' && tmp > 0)
198  config->irq_log_interval = tmp;
199  else
200  fprintf(stderr, "Error reading configuration file: %s is not a valid value for %s\n", value, token);
201  }
202  else if(strcmp(token, "check_interval") == '\0')
203  {
204  tmp = strtol(value, &strchk, 10);
205 
206  if(strchk[0] == '\0' && tmp > 0)
207  config->check_log_interval = tmp;
208  else
209  fprintf(stderr, "Error reading configuration file: %s is not a valid value for %s\n", value, token);
210  }
211  else if(strcmp(token, "log_level") == '\0')
212  {
213  tmp = strtol(value, &strchk, 10);
214 
215  if(strchk[0] == '\0' && tmp >= -1 && tmp <= 6)
216  config->log_level = tmp;
217  else
218  fprintf(stderr, "Error reading configuration file: %s is not a valid value for %s\n", value, token);
219  }
220  }
221  }
222 
223  fclose(file);
224  free(value);
225  free(token);
226  free(line);
227 
228  return DLT_RETURN_OK;
229 }
230 
232 {
233  DltKpiOptions options;
234 
235  DltReturnValue ret;
236 
237  if(config == NULL)
238  {
239  fprintf(stderr, "%s: Invalid Parameter!", __func__);
241  }
242 
243  if((ret = dlt_kpi_read_command_line(&options, argc, argv)) < DLT_RETURN_OK)
244  {
245  fprintf(stderr, "Failed to read command line!");
246  return ret;
247  }
248 
250  {
251  fprintf(stderr, "Failed to read configuration file!");
252  return ret;
253  }
254 
255  dlt_kpi_free_cli_options(&options);
256 
257  return DLT_RETURN_OK;
258 }
DltReturnValue dlt_kpi_init(int argc, char **argv, DltKpiConfig *config)
#define DEFAULT_CONF_FILE
Definition: dlt-dbus.h:34
DltReturnValue
Definition: dlt_types.h:86
DltKpiConfig config
Definition: dlt-kpi.c:37
#define COMMAND_LINE_SIZE
Definition: dlt-kpi.h:41
int check_log_interval
Definition: dlt-kpi.h:52
DltReturnValue dlt_kpi_read_command_line(DltKpiOptions *options, int argc, char **argv)
void usage(char *prog_name)
int irq_log_interval
Definition: dlt-kpi.h:52
int customConfigFile
Definition: dlt-kpi.h:47
void dlt_kpi_init_configuration(DltKpiConfig *config)
DltLogLevelType log_level
Definition: dlt-kpi.h:53
int process_log_interval
Definition: dlt-kpi.h:52
void dlt_kpi_free_cli_options(DltKpiOptions *options)
void dlt_get_version(char *buf, size_t size)
Definition: dlt_common.c:3239
void dlt_kpi_init_cli_options(DltKpiOptions *options)
char * configurationFileName
Definition: dlt-kpi.h:46
DltReturnValue dlt_kpi_read_configuration_file(DltKpiConfig *config, char *file_name)
#define NULL
Definition: dlt_common.h:232