summaryrefslogtreecommitdiff
path: root/src/dbus/dlt-dbus-options.c
blob: 88e13fa25427a77f556aba4502635135ae267e89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
 * SPDX license identifier: MPL-2.0
 *
 * Copyright (C) 2011-2015, BMW AG
 *
 * This file is part of GENIVI Project DLT - Diagnostic Log and Trace.
 *
 * This Source Code Form is subject to the terms of the
 * Mozilla Public License (MPL), v. 2.0.
 * If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * For further information see http://www.genivi.org/.
 */

/*!
 * \author Alexander Wenzel <alexander.aw.wenzel@bmw.de>
 *
 * \copyright Copyright © 2011-2015 BMW AG. \n
 * License MPL-2.0: Mozilla Public License version 2.0 http://mozilla.org/MPL/2.0/.
 *
 * \file dlt-dbus-options.c
 */



#include "dlt-dbus.h"

#include <stdlib.h>
#include <string.h>

/**
 * Print information how to use this program.
 */
void usage(char *prog_name)
{
    char version[255];
    dlt_get_version(version, 255);

    printf("Usage: %s [options]\n", prog_name);
    printf("Application to forward dbus messages to DLT.\n");
    printf("%s\n", version);
    printf("Options:\n");
    printf(" -d             Daemonize. Detach from terminal and run in background.\n");
    printf(" -c filename    Use configuration file. \n");
    printf(" -a apid        Used application id. \n");
    printf("                Default: %s\n", DEFAULT_CONF_FILE);
    printf(" -b type        Used bus type. \n");
    printf("                Session = 0, System = 1.\n");
    printf(" -h             This help message.\n");
}

/**
 * Initialize command line options with default values.
 */
void init_cli_options(DltDBusCliOptions *options)
{
    options->ConfigurationFileName = DEFAULT_CONF_FILE;
    options->ApplicationId = 0;
    options->BusType = 0;
    options->Daemonize = 0;
}

/**
 * Read command line options and set the values in provided structure
 */
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 uncritical 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 uncritical 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 uncritical 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;
}

/**
 * Initialize configuration to default values.
 */
void init_configuration(DltDBusConfiguration *config)
{
    /* Common */
    config->ApplicationId = "IPC0";

    /* DBus */
    config->DBus.ContextId = "ALL";
    config->DBus.BusType = 0;
    config->DBus.FilterCount = 0;

}

/**
 * Read options from the configuration file
 */
int read_configuration_file(DltDBusConfiguration *config, char *file_name)
{
    FILE *file;
    char *line, *token, *value, *filter, *pch;
    int ret = 0;
    char *filterBegin, *filterEnd;

    init_configuration(config);

    file = fopen(file_name, "r");

    if (file == NULL) {
        fprintf(stderr, "dlt-dbus-options, could not open configuration file.\n");
        return -1;
    }

    line = malloc(MAX_LINE);
    token = malloc(MAX_LINE);
    value = malloc(MAX_LINE);
    filter = malloc(MAX_LINE);

    MALLOC_ASSERT(line);
    MALLOC_ASSERT(token);
    MALLOC_ASSERT(value);
    MALLOC_ASSERT(filter);

    while (fgets(line, MAX_LINE, file) != NULL) {
        token[0] = 0;
        value[0] = 0;
        filter[0] = 0;

        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;
            }
            else {
                strcpy(filter, filterBegin + 1);
            }
        }

        pch = strtok (line, " =\r\n");

        while (pch != NULL) {
            if (pch[0] == '#')
                break;

            if (token[0] == 0) {
                strncpy(token, pch, MAX_LINE - 1);
                token[MAX_LINE - 1] = 0;
            }
            else {
                strncpy(value, pch, MAX_LINE);
                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);
                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)
            {
                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)
            {
                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)
            {
                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++;
                }
            }
        }
    }

    fclose(file);
    free(value);
    free(token);
    free(filter);
    free(line);
    return ret;
}