summaryrefslogtreecommitdiff
path: root/src/console/dlt-logstorage-ctrl.c
blob: 24742f0f1162041e870581a46b6630c466292e6c (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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*
 * @licence app begin@
 * SPDX license identifier: MPL-2.0
 *
 * Copyright (C) 2013 - 2015  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/.
 * @licence end@
 */

/*!
 * \author Syed Hameed <shameed@jp.adit-jv.com> ADIT 2013 - 2015
 * \author Christoph Lipka <clipka@jp.adit-jv.com> ADIT 2015
 *
 * \copyright Copyright © 2013-2015 Advanced Driver Information Technology. \n
 * License MPL-2.0: Mozilla Public License version 2.0 http://mozilla.org/MPL/2.0/.
 *
 * \file dlt-logstorage-ctrl.c
*/

/*******************************************************************************
**                                                                            **
**  SRC-MODULE: dlt-logstorage-ctrl.c                                         **
**                                                                            **
**  TARGET    : linux                                                         **
**                                                                            **
**  PROJECT   : DLT                                                           **
**                                                                            **
**  AUTHOR    : Syed Hameed shameed@jp.adit-jv.com                            **
**              Christoph Lipka clipka@jp.adit-jv.com                         **
**  PURPOSE   :                                                               **
**                                                                            **
**  REMARKS   :                                                               **
**                                                                            **
**  PLATFORM DEPENDANT [yes/no]: yes                                          **
**                                                                            **
**  TO BE CHANGED BY USER [yes/no]: no                                        **
**                                                                            **
*******************************************************************************/

/*******************************************************************************
**                      Author Identity                                       **
********************************************************************************
**                                                                            **
** Initials     Name                       Company                            **
** --------     -------------------------  ---------------------------------- **
**  sh          Syed Hameed                ADIT                               **
**  cl          Christoph Lipka            ADIT                               **
*******************************************************************************/

#include <netdb.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/uio.h>
#include <stdint.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>
#include <pthread.h>

#include "dlt_common.h"
#include "dlt_client.h"
#include "dlt_protocol.h"

#define DLT_RECEIVE_TEXTBUFSIZE 1024
#define DLT_DAEMON_ECUID_DEFAULT "ECU1"
#define DLT_DAEMON_ECUID_LEN 10
#define DLT_LOGSTORAGE_CTRL_TIMEOUT 10

DltClient dltclient;

/* Result of callback function */
int g_logstorage_callback_return = -1;

const char *serverIP = "localhost";

typedef struct
{
    int connection_type;              /* Conntection Type: 1 (connect), 0 (disconnect) */
    int device_number;                /* Number of Logstorage device [1 .. MAX DEVICES] */
    char ecuid[DLT_DAEMON_ECUID_LEN]; /* Name of ECU */
} DltLogstorageCtrlData;

void usage()
{
    printf("Usage: dlt-logstorage-ctrl [options]\n");
    printf("Send a trigger to DLT daemon to connect/disconnect a certain logstorage device\n");
    printf("\n");
    printf("Options:\n");
    printf("  -c         Connection type: connect = 1, disconnect = 0\n");
    printf("  -d         Device: [1 .. DLT_OFFLINE_LOGSTORAGE_MAX_DEVICES]\n");
    printf("  -e         Set ECU ID (Default: %s\n", DLT_DAEMON_ECUID_DEFAULT);
    printf("  -h         Usage\n");
    printf("  -t         Specify connection timeout (Default: %ds)\n", DLT_LOGSTORAGE_CTRL_TIMEOUT);
}

int dlt_logstorage_message_callback(DltMessage *message,void *data);
int dlt_logstorage_analyze_response(char *data);

/*
 * Prepare request message and send to Dlt Daemon. Since Logstorage control application acts
 * as Dlt client, we use the same functionality found in
 * dlt_daemon_client.c: dlt_daemon_client_send_control_message()
 */
int dlt_send_logstorage_request(int sockid, int device_number, int connection_type, char *ecuid)
{
    DltMessage msg;
    DltServiceOfflineLogstorage *req = NULL;
    int32_t len = 0;
    int ret = 0;

    /* Initialise new message */
    if (dlt_message_init(&msg, 0) == -1)
    {
        fprintf( stderr, "Cannot initialize DltMessage\n");
        return -1;
    }

    msg.datasize = sizeof(DltServiceOfflineLogstorage);

    /* prepare payload of data */
    if (msg.databuffer && (msg.databuffersize < msg.datasize))
    {
        free(msg.databuffer);
        msg.databuffer = 0;
    }
    if (msg.databuffer == 0)
    {
        /* malloc databuffer for DltMessage */
        msg.databuffer = (uint8_t *) malloc(msg.datasize);
        msg.databuffersize = msg.datasize;
    }
    if(msg.databuffer == 0)
    {
        fprintf( stderr, "Cannot allocate memory for data buffer\n");
        dlt_message_free(&msg, 0);
        return -1;
    }

    req = (DltServiceOfflineLogstorage*) msg.databuffer;

    req->service_id = DLT_SERVICE_ID_OFFLINE_LOGSTORAGE;
    req->dev_num = device_number;
    req->connection_type = connection_type;
    dlt_set_id(req->comid,"remo");

    /* prepare storage header */
    msg.storageheader = (DltStorageHeader*) msg.headerbuffer;
    if (dlt_set_storageheader(msg.storageheader, "") == -1)
    {
        fprintf( stderr, "Init storage header failed\n");
        dlt_message_free(&msg, 0);
        return -1;
    }

    /* prepare standard header */
    msg.standardheader = (DltStandardHeader*)(msg.headerbuffer + sizeof(DltStorageHeader));
    msg.standardheader->htyp = DLT_HTYP_WEID | DLT_HTYP_WTMS | DLT_HTYP_UEH | DLT_HTYP_PROTOCOL_VERSION1;

#if (BYTE_ORDER==BIG_ENDIAN)
    msg.standardheader->htyp = (msg.standardheader->htyp | DLT_HTYP_MSBF);
#endif

    msg.standardheader->mcnt = 0;

    /* Set header extra parameters */
    if (ecuid == NULL)
    {
        dlt_set_id(msg.headerextra.ecu,DLT_DAEMON_ECUID_DEFAULT);
    }
    else
    {
        dlt_set_id(msg.headerextra.ecu,ecuid);
    }

    msg.headerextra.tmsp = dlt_uptime();

    /* Copy header extra parameters to headerbuffer */
    if (dlt_message_set_extraparameters(&msg, 0) == -1)
    {
        fprintf( stderr, "Cannot copy header extra parameter\n");
        dlt_message_free(&msg,0);
        return -1;
    }

    /* prepare extended header */
    msg.extendedheader = (DltExtendedHeader*)(msg.headerbuffer +
                         sizeof(DltStorageHeader) +
                         sizeof(DltStandardHeader) +
                         DLT_STANDARD_HEADER_EXTRA_SIZE(msg.standardheader->htyp) );

    msg.extendedheader->msin = DLT_MSIN_CONTROL_REQUEST;

    msg.extendedheader->noar = 1; /* one payload packet */

    /* Dummy values have to be set: Offline_Log_Storage_Control, Context1 */
    dlt_set_id(msg.extendedheader->apid, "OLSC");
    dlt_set_id(msg.extendedheader->ctid, "CON1");

    /* prepare length information */
    msg.headersize = sizeof(DltStorageHeader) +
                     sizeof(DltStandardHeader) +
                     sizeof(DltExtendedHeader) +
                     DLT_STANDARD_HEADER_EXTRA_SIZE(msg.standardheader->htyp);

    len = msg.headersize - sizeof(DltStorageHeader) + msg.datasize;
    if (len > UINT16_MAX)
    {
        fprintf( stderr, "Message header too long\n");
        dlt_message_free(&msg, 0);
        return -1;
    }

    msg.standardheader->len = DLT_HTOBE_16(len);

    /* Send via Socket */
    if (send(sockid, (const char *)(msg.headerbuffer+sizeof(DltStorageHeader)), msg.headersize-sizeof(DltStorageHeader), 0) == -1)
    {
        fprintf( stderr, "Sending message to socket failed: %s\n", strerror(errno));
        ret = -1;
    }

    if (send(sockid, (const char *) msg.databuffer,msg.datasize, 0) == -1)
    {
        fprintf( stderr, "Sending message to socket failed: %s\n", strerror(errno));
        ret = -1;
    }

    /* free message */
    if (dlt_message_free(&msg, 0) == -1)
    {
        fprintf( stderr, "free DltMessage failed\n");
        return -1;
    }

    return ret;
}

int dlt_logstorage_message_callback(DltMessage *message, void *data)
{
    char text[DLT_RECEIVE_TEXTBUFSIZE] = {0};
    int ret = 0;

    (void)data; /* ignore data */

    if (message == 0)
    {
        fprintf( stderr, "Received message is null\n");
        g_logstorage_callback_return = -1;
        return -1;
    }

    /* prepare storage header */
    if (DLT_IS_HTYP_WEID(message->standardheader->htyp))
    {
        dlt_set_storageheader(message->storageheader, message->headerextra.ecu);
    }
    else
    {
        dlt_set_storageheader(message->storageheader, "LCTL");
    }

    dlt_message_header(message, text, DLT_RECEIVE_TEXTBUFSIZE, 1);

    /* payload contains "DLT_SERVICE_ID_OFFLINE_LOGSTORAGE, DLT_SERVICE_RESPONSE" as string
     * that has to be checked */
    dlt_message_payload(message, text, DLT_RECEIVE_TEXTBUFSIZE,DLT_OUTPUT_ASCII, 1);

    ret = dlt_logstorage_analyze_response(text);
    dlt_client_cleanup(&dltclient, 1);

    g_logstorage_callback_return = ret;
    return ret;
}

/*
 * Communicate with daemon in a thread to detect communication errors (timeout).
 */
void *communicate_with_daemon (void *data)
{
    DltLogstorageCtrlData *ctrl = (DltLogstorageCtrlData *) data;
    /* Initialize DLT Client */
    dlt_client_init(&dltclient, 1);

    /* Register callback to be called when message was received */
    dlt_client_register_message_callback(dlt_logstorage_message_callback);

    dltclient.servIP = "localhost";

    /* Connect to TCP socket or open serial device */
    dlt_client_connect(&dltclient, 1);

    if (dlt_send_logstorage_request(dltclient.sock, ctrl->device_number, ctrl->connection_type, ctrl->ecuid) != 0)
    {
        fprintf(stderr, "dlt_send_logstorage_request failed. Exit.\n");
    }
    else
    {
        /* Dlt Client Main Loop */
        dlt_client_main_loop(&dltclient, NULL, 1);
    }

    return (void *)0;
}

/*
 * Resonse received in form of "service(<id>), <ok, error>
 * check if id is DLT_SERVICE_ID_OFFLINE_LOGSTORAGE and return value either ok or error.
 */
int dlt_logstorage_analyze_response(char *data)
{
    int ret = 0;
    char resp_ok[20] = {0};
    char resp_nok[20] = {0};

    if (data == NULL)
        return -1;

    snprintf(resp_ok, 20, "service(%u), ok", DLT_SERVICE_ID_OFFLINE_LOGSTORAGE);
    snprintf(resp_nok, 20, "service(%u), error", DLT_SERVICE_ID_OFFLINE_LOGSTORAGE);

    if (strncmp(data, resp_ok, strlen(resp_ok)) == 0)
    {
        dlt_client_cleanup(&dltclient, 1);
        ret = 0;
    }
    else if (strncmp(data, resp_nok, strlen(resp_nok)) == 0)
    {
        dlt_client_cleanup(&dltclient, 1);
        ret = -1;
    }
    else
    {
        /* fall through */
    }

    g_logstorage_callback_return = ret;
    return ret;
}

int main(int argc, char *argv[])
{
    DltLogstorageCtrlData ctrl_data;
    pthread_t daemon_connect_thread;
    int c = 0;
    int ret = 0;
    int timeout = DLT_LOGSTORAGE_CTRL_TIMEOUT;
    struct timespec t;

    /* Initialize control data */
    memset(&ctrl_data, 0, sizeof(DltLogstorageCtrlData));

    /* Get command line arguments */
    opterr = 0;

    while ((c = getopt(argc, argv, "t:he:d:c:")) != -1)
    {
        switch(c)
        {
        case 't':
            timeout = (int) strtol(optarg,NULL, 10);
            if (timeout <= 1)
            {
                fprintf(stderr, "Timeout to small. Set to default: %d", DLT_LOGSTORAGE_CTRL_TIMEOUT);
                timeout = DLT_LOGSTORAGE_CTRL_TIMEOUT;
            }
            break;
        case 'h':
            usage();
            return -1;
        case 'e':
            strncpy(ctrl_data.ecuid, optarg, DLT_DAEMON_ECUID_LEN);
            break;
        case 'd':
            ctrl_data.device_number = (int) strtol(optarg, NULL, 10);
            break;
        case 'c':
            ctrl_data.connection_type = (int) strtol(optarg, NULL, 10);
            break;
        case '?':
            if (optopt == 'c' || optopt == 'd' || optopt == 'e')
            {
                 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);
            }

            usage();
            return -1;
        default:
            fprintf(stdout, "Try %s -h for more information.\n", argv[0]);
            return -1;
        }
    }

    /* Check if device number and connection type configured correclty */
    if (ctrl_data.connection_type < 0 || ctrl_data.connection_type > 1)
    {
        fprintf(stderr, "Please check connection type parameter. Try %s -h for more information.\n", argv[0]);
        return -1;
    }

    if (ctrl_data.device_number <= 0)
    {
        fprintf(stderr, "Please check device parameter. Try %s -h for more information.\n", argv[0]);
        return -1;
    }

    /* Inside DLT Logstorage, devices are stored in an array. This starts with device 0
     * Therefore, we decrease given device number by 1 */
    ctrl_data.device_number -= 1;

    if (clock_gettime(CLOCK_REALTIME, &t) == -1)
    {
        fprintf(stderr, "Cannot read system time.\n");
        return -1;
    }

    /* set timeout */
    t.tv_sec += timeout;

    /* Contact DLT daemon */
    if (pthread_create(&daemon_connect_thread, NULL, communicate_with_daemon, &ctrl_data) != 0) {
        fprintf(stderr, "Cannot create thread to communicate with DLT daemon.\n");
        return -1;
    }

    /* Wait for thread */
    ret = pthread_timedjoin_np(daemon_connect_thread, NULL, &t);

    if (ret != 0)
    {
        fprintf( stderr, "Connection to Dlt Daemon timed out.\n");
        fprintf( stderr, "Sending command to DLT daemon was not successful.\n");
    }
    else
    {
        /* Check callback return value */
        if (g_logstorage_callback_return != 0) {
            fprintf( stderr, "Sending command to DLT daemon was not successful.\n");
            ret = -1;
        }
    }

    return ret;
}