summaryrefslogtreecommitdiff
path: root/src/console/dlt-passive-node-ctrl.c
blob: 88a03a8a628de6ae44066d82ce17a7043fc7d236 (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
/**
 * @licence app begin@
 * Copyright (C) 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 console apps.
 *
 *
 * \copyright
 * This Source Code Form is subject to the terms of the
 * Mozilla Public License, 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/.
 *
 *
 * \author Christoph Lipka <clipka@jp.adit-jv.com> ADIT 2015
 *
 * \file dlt-passive-node-ctrl.c
 * For further information see http://www.genivi.org/.
 * @licence end@
 */

/*******************************************************************************
**                                                                            **
**  SRC-MODULE: dlt-passive-node-ctrl.c                                       **
**                                                                            **
**  TARGET    : linux                                                         **
**                                                                            **
**  PROJECT   : DLT                                                           **
**                                                                            **
**  AUTHOR    : 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                            **
** --------     -------------------------  ---------------------------------- **
**  CL          Christoph Lipka             ADIT                              **
*******************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <pthread.h>
#include "dlt_protocol.h"
#include "dlt_client.h"
#include "dlt-control-common.h"
#include "dlt_daemon_connection_types.h"

#define MAX_RESPONSE_LENGTH     32

#define DLT_NODE_CONNECT        1
#define DLT_NODE_DISCONNECT     0
#define DLT_NODE_CONNECT_UNDEF  999

#define DLT_GATEWAY_CONNECTED   2
#define DLT_NODE_CONNECTED_STR    "Connected"
#define DLT_NODE_DISCONNECTED_STR "Disconnected"

#define UNDEFINED 999

static struct PassiveNodeOptions {
    unsigned int command;          /**< passive node control command */
    unsigned int connection_state; /**< connection state */
    char node_id[DLT_ID_SIZE];     /**< node identifier */
    long timeout;                  /**< Default timeout */
} g_options = {
    .command = UNDEFINED,
    .connection_state = UNDEFINED,
    .node_id = {'\0'},
};

unsigned int get_command(void)
{
    return g_options.command;
}

void set_command(unsigned int c)
{
    g_options.command = c;
}

unsigned int get_connection_state(void)
{
    return g_options.connection_state;
}

void set_connection_state(unsigned int s)
{
    if ((s == DLT_NODE_CONNECT) || (s == DLT_NODE_DISCONNECT))
    {
        g_options.connection_state = s;
        set_command(DLT_SERVICE_ID_PASSIVE_NODE_CONNECT);
    }
    else
    {
        pr_error("Connection status %u invalid\n", s);
        exit(-1);
    }
}

void set_node_id(char *id)
{
    if (id == 0)
    {
        pr_error("node identifier is NULL\n");
        exit(-1);
    }
    else
    {
        strncpy(g_options.node_id, id, DLT_ID_SIZE);
    }
}

char *get_node_id()
{
    return g_options.node_id;
}

/**
 * @brief Print passive node status information
 *
 * @param info DltServicePassiveNodeConnectionInfo
 */
static void dlt_print_passive_node_status(
                DltServicePassiveNodeConnectionInfo *info)
{
    unsigned int i = 0;
    char *status;

    if (info == NULL)
    {
        return;
    }

    printf("\nPassive Node connection status:\n"
           "---------------------------------\n");
    for(i = 0; i < info->num_connections; i++)
    {
        if (info->connection_status[i] == DLT_GATEWAY_CONNECTED)
        {
            status = DLT_NODE_CONNECTED_STR;
        }
        else
        {
            status = DLT_NODE_DISCONNECTED_STR;
        }

        printf("%.4s: %s\n", &info->node_id[i * DLT_ID_SIZE], status);
    }

    printf("\n");
}

/**
 * @brief Analyze received DLT Daemon response
 *
 * This function checks the received message. In particular, it checks the
 * answer string 'service(<ID>, {ok, error, perm_denied})'. In any case the
 * g_callback_return variable will be set as well which is evaluated in the
 * main function after the communication thread returned.
 *
 * @param message   Received DLT Message
 * @return 0 if daemon returns 'ok' message, -1 otherwise
 */
static int dlt_passive_node_analyze_response(char *answer,
                                             void *payload,
                                             int len)
{
    int ret = -1;
    char resp_ok[MAX_RESPONSE_LENGTH] = { 0 };

    if (answer == NULL || payload == NULL)
    {
        return -1;
    }

    snprintf(resp_ok,
             MAX_RESPONSE_LENGTH,
             "service(%u), ok",
             get_command());

    pr_verbose("Response received: '%s'\n", answer);
    pr_verbose("Response expected: '%s'\n", resp_ok);

    if (strncmp(answer, resp_ok, strlen(resp_ok)) == 0)
    {
        ret = 0;

        if (get_command() == DLT_SERVICE_ID_PASSIVE_NODE_CONNECTION_STATUS)
        {
            if ((int)sizeof(DltServicePassiveNodeConnectionInfo) > len)
            {
                pr_error("Received payload is smaller than expected\n");
                pr_verbose("Expected: %lu,\nreceived: %d",
                           sizeof(DltServicePassiveNodeConnectionInfo),
                           len);
                ret = -1;
            }
            else
            {
                DltServicePassiveNodeConnectionInfo *info =
                    (DltServicePassiveNodeConnectionInfo *) (payload);
                if (info == NULL)
                {
                    fprintf(stderr, "Received response is NULL\n");
                    return -1;
                }

                dlt_print_passive_node_status(info);
            }
        }
    }

    return ret;
}

/**
 * @brief Prepare message body to be send to DLT Daemon
 *
 * @return Pointer ot DltControlMsgBody, NULL otherwise
 */
DltControlMsgBody *dlt_passive_node_prepare_message_body()
{
    DltControlMsgBody *mb = calloc(1, sizeof(DltControlMsgBody));
    char *ecuid = get_node_id();
    if (mb == NULL)
    {
        return NULL;
    }

    if (get_command() == DLT_SERVICE_ID_PASSIVE_NODE_CONNECT)
    {
        mb->data = calloc(1, sizeof(DltServicePassiveNodeConnect));
        if (mb->data == NULL)
        {
            free(mb);
            return NULL;
        }
        mb->size = sizeof(DltServicePassiveNodeConnect);
        DltServicePassiveNodeConnect *serv = (DltServicePassiveNodeConnect *)
            mb->data;
        serv->service_id = DLT_SERVICE_ID_PASSIVE_NODE_CONNECT;
        serv->connection_status = get_connection_state();

        memcpy(serv->node_id, ecuid, DLT_ID_SIZE);
    }
    else /* DLT_SERVICE_ID_PASSIVE_NODE_CONNECTION_STATUS */
    {
        mb->data = calloc(1, sizeof(DltServicePassiveNodeConnectionInfo));
        if (mb->data == NULL)
        {
            free(mb);
            return NULL;
        }

        mb->size = sizeof(DltServicePassiveNodeConnectionInfo);
        DltServicePassiveNodeConnectionInfo *serv =
            (DltServicePassiveNodeConnectionInfo *) mb->data;
        serv->service_id = DLT_SERVICE_ID_PASSIVE_NODE_CONNECTION_STATUS;
    }

    return mb;
}

/**
 * @brief Destroy message body
 */
void dlt_passive_node_destroy_message_body(DltControlMsgBody *msg_body)
{
    if (msg_body == NULL)
    {
        return;
    }

    if (msg_body->data != NULL)
    {
        free(msg_body->data);
    }
    free(msg_body);
}

/**
 * @brief Send a single command to DLT daemon and wait for response
 *
 * @return 0 on success, -1 on error
 */
static int dlt_passive_node_ctrl_single_request()
{
    int ret = -1;

    /* Initializing the communication with the daemon */
    if (dlt_control_init(dlt_passive_node_analyze_response,
                         get_ecuid(),
                         get_verbosity()) != 0)
    {
        pr_error("Failed to initialize connection with the daemon.\n");
        return ret;
    }

    /* prepare message body */
    DltControlMsgBody *msg_body = NULL;
    msg_body = dlt_passive_node_prepare_message_body();

    if (msg_body == NULL)
    {
        pr_error("Data for Dlt Message body is NULL\n");
        return ret;
    }

    ret = dlt_control_send_message(msg_body, get_timeout());

    dlt_passive_node_destroy_message_body(msg_body);

    dlt_control_deinit();

    return ret;
}

static void usage()
{
    printf("Usage: dlt-passive-node-ctrl [options]\n");
    printf("Send a trigger to DLT daemon to (dis)connect a passive node "
           "or get current passive node status \n");
    printf("\n");
    printf("Options:\n");
    printf("  -c         Connection status (1 - connect, 0 - disconnect)\n");
    printf("  -h         Usage\n");
    printf("  -n         passive Node identifier (e.g. ECU2)\n");
    printf("  -s         Show passive node(s) connection status\n");
    printf("  -t         Specify connection timeout (Default: %ds)\n",
           DLT_CTRL_TIMEOUT);
    printf("  -v         Set verbose flag (Default:%d)\n", get_verbosity());
}

/**
 * @brief Parse application arguments
 *
 * The arguments are parsed and saved in static structure for future use.
 *
 * @param argc  amount of arguments
 * @param argv  argument table
 * @return 0 on success, -1 otherwise
 */
static int parse_args(int argc, char *argv[])
{
    int c = 0;
    int state = -1;

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

    while ((c = getopt(argc, argv, "c:hn:stv")) != -1)
    {
        switch(c)
        {
        case 'c':
            state = (int)strtol(optarg,NULL, 10);
            if (state == DLT_NODE_CONNECT || state == DLT_NODE_DISCONNECT)
            {
                set_connection_state(state);
                set_command(DLT_SERVICE_ID_PASSIVE_NODE_CONNECT);
            }
            else
            {
                pr_error("unknown connection state: %d\n", state);
                return -1;
            }
            break;
        case 'h':
            usage();
            return -1;
        case 'n':
            set_node_id(optarg);
            break;
        case 's':
            set_command(DLT_SERVICE_ID_PASSIVE_NODE_CONNECTION_STATUS);
            break;
        case 't':
            set_timeout(strtol(optarg, NULL, 10));
            break;
        case 'v':
            set_verbosity(1);
            pr_verbose("Now in verbose mode.\n");
            break;
        case '?':
            if (isprint(optopt))
            {
                pr_error("Unknown option -%c.\n", optopt);
            }
            else
            {
                pr_error("Unknown option character \\x%x.\n", optopt);
            }
            usage();
            return -1;
        default:
            pr_error("Try %s -h for more information.\n", argv[0]);
            return -1;
        }
    }

    return 0;
}

/**
 * @brief Entry point
 *
 * Execute the argument parser and call the main feature accordingly
 *
 * @param argc  amount of arguments
 * @param argv  argument table
 * @return 0 on success, -1 otherwise
 */
int main(int argc, char *argv[])
{
    int ret = 0;

    set_ecuid(NULL);
    set_timeout(DLT_CTRL_TIMEOUT);

    /* Get command line arguments */
    if (parse_args(argc, argv) != 0)
    {
        return -1;
    }

    if (get_command() == UNDEFINED ||
       (get_command() == DLT_SERVICE_ID_PASSIVE_NODE_CONNECT &&
       g_options.node_id[0] == '\0' &&
       g_options.connection_state == DLT_NODE_CONNECT_UNDEF))
    {
        pr_error("No valid parameter configuration given!\n");
        usage();
        return -1;
    }

    pr_verbose("Sending command to DLT daemon.\n");

    /* one shot request */
    ret = dlt_passive_node_ctrl_single_request();

    pr_verbose("Exiting.\n");

    return ret;
}