summaryrefslogtreecommitdiff
path: root/src/tests/dlt-test-qnx-slogger.c
blob: 973fbe0f7c56031d7dff63ae6ee517645234cbe3 (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
/*
 * SPDX license identifier: MPL-2.0
 *
 * Copyright (C) 2021 Advanced Driver Information Technology.
 * This code is developed by Advanced Driver Information Technology.
 * Copyright of Advanced Driver Information Technology, Bosch and DENSO.
 *
 * 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/.
 */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/slog.h>
#include <sys/slogcodes.h>

#include "dlt.h"
#include "dlt_common.h" /* for dlt_get_version() */

#define COUNT 10
#define DELAY 500
#define LENGTH 100

void usage()
{
    char version[255];

    dlt_get_version(version, 255);

    printf("Usage: dlt-test-qnx-slogger [options]\n");
    printf("Generate messages and send them to slogger2\n");
    printf("%s\n", version);
    printf("Options:\n");
    printf("  -h          Usage\n");
    printf("  -n count    Number of messages to be generated (Default: %d)\n", COUNT);
    printf("  -d delay    Milliseconds to wait between sending messages (Default: %d)\n", DELAY);
    printf("  -l length   Message payload length (Default: %d bytes)\n", LENGTH);
}

int main(int argc, char *argv[]) {
    int i = 0;
    int count = COUNT;
    int delay = DELAY;
    int length = LENGTH;
    char *str = NULL;
    struct timespec ts;

    int c;

    while ((c = getopt(argc, argv, "hn:d:l:")) != -1)
    {
        switch(c)
        {
        case 'n':
        {
            count = atoi(optarg);
            break;
        }
        case 'd':
        {
            delay = atoi(optarg);
            break;
        }
        case 'l':
        {
            length = atoi(optarg);
            break;
        }
        case 'h':
        {
            usage();
            return -1;
        }
        case '?':
        {
            if ((optopt == 'n') || (optopt == 'd') || (optopt == 'l'))
                fprintf(stderr, "Option -%c requires an argument\n", optopt);
            else if (isprint(optopt))
                fprintf(stderr, "Unknown option `-%c`\n", optopt);
            else
                fprintf(stderr, "Unknown option character `\\x%x`\n", optopt);

            /* unknown or wrong option used, show usage information and terminate */
            usage();
            return -1;
        }
        default:
        {
            usage();
            return -1;
        }
        }
    }

    /* Generate string */
    if (length > 0)
    {
        str = (char *) malloc((size_t) length + 1);
        if (str == NULL)
        {
            fprintf(stderr, "Cannot allocate memory\n");
            return -1;
        }
        memset(str, 'X', (size_t) length);
        str[length] = '\n';
    }

    /* Calculate delay */
    if (delay > 0) {
        ts.tv_sec = delay / 1000;
        ts.tv_nsec = (delay % 1000) * 1000000;
    }


    for (i = 0; i < count; i++)
    {
        slogf(_SLOG_SETCODE(_SLOGC_TEST, 0), _SLOG_INFO, "%s", str);
        nanosleep(&ts, NULL);
    }

    if (str != NULL)
    {
        free(str);
        str = NULL;
    }

    return 0;
}