3 * Copyright (C) 2012 BMW AG
5 * This file is part of GENIVI Project Dlt - Diagnostic Log and Trace console apps.
7 * Contributions are licensed to the GENIVI Alliance under one or more
8 * Contribution License Agreements.
11 * This Source Code Form is subject to the terms of the
12 * Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with
13 * this file, You can obtain one at http://mozilla.org/MPL/2.0/.
16 * \author Lassi Marttala <Lassi.LM.Marttala@partner.bmw.de> BMW 2011-2012
18 * \file dlt-test-multi-process-client.c
19 * For further information see http://www.genivi.org/.
23 /*******************************************************************************
25 ** SRC-MODULE: dlt-test-multi-process-client.c **
31 ** AUTHOR : Lassi Marttala <Lassi.LM.Marttala@partner.bmw.de> **
33 ** PURPOSE : Receive, validate and measure data from multi process tester **
37 ** PLATFORM DEPENDANT [yes/no]: yes **
39 ** TO BE CHANGED BY USER [yes/no]: no **
41 *******************************************************************************/
53 // DLT Library includes
54 #include "dlt_client.h"
55 #include "dlt_protocol.h"
58 #include "dlt-test-multi-process.h"
60 // Local data structures
69 DltClient *client_ref;
73 int messages_received;
74 int broken_messages_received;
76 int first_message_time;
80 // Forward declarations
81 int receive(DltMessage *msg, void *data);
84 * Print usage information
86 void usage(char *name) {
88 dlt_get_version(version,255);
90 printf("Usage: %s [options] <remote address|serial device>\n", name);
91 printf("Receive messages from dlt-test-multi-process.\n");
92 printf("%s", version);
94 printf(" -m Total messages to receive. (Default: 10000)\n");
95 printf(" -y Serial device mode.\n");
96 printf(" -b baudrate Serial device baudrate. (Default: 115200)\n");
97 printf(" -v Verbose. Increases the verbosity level of dlt client library.\n");
98 printf(" -o filename Output messages in new DLT file.\n");
102 * Initialize reasonable default parameters.
104 void init_params(s_parameters *params) {
105 params->max_messages = 10000;
108 params->output = NULL;
109 params->output_handle = -1;
110 params->baudrate = 115200;
114 * Read the command line parameters
116 int read_params(s_parameters *params, int argc, char *argv[]) {
120 while ((c = getopt(argc, argv, "m:yb:vo:")) != -1) {
123 params->max_messages = atoi(optarg);
129 params->baudrate = atoi(optarg);
135 params->output = optarg;
138 if(optopt == 'm' || optopt == 'b' || optopt == 'o')
140 fprintf(stderr, "Option -%c requires an argument.\n", optopt);
142 if (isprint(optopt)) {
143 fprintf(stderr, "Unknown option '-%c'.\n", optopt);
145 fprintf(stderr, "Unknown option character '\\x%x'.\n", optopt);
157 * Set the connection parameters for dlt client
159 int init_dlt_connect(DltClient *client, const s_parameters *params, int argc, char *argv[]) {
163 if(params->serial > 0)
165 client->serial_mode = 1;
166 client->serialDevice = argv[argc - 1];
167 dlt_client_setbaudrate(client, params->baudrate);
171 client->servIP = argv[argc - 1];
173 dlt_set_id(id, ECUID);
180 int main(int argc, char *argv[]) {
183 params.client_ref = &client;
184 int err = read_params(¶ms, argc, argv);
191 dlt_client_init(&client, params.verbose);
192 dlt_client_register_message_callback(receive);
194 err = init_dlt_connect(&client, ¶ms, argc, argv);
200 err = dlt_client_connect(&client, params.verbose);
202 printf("Failed to connect %s.\n", client.serial_mode > 0 ? client.serialDevice : client.servIP);
208 params.output_handle = open(params.output,O_WRONLY|O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
210 if (params.output_handle == -1)
212 fprintf(stderr,"Failed to open %s for writing.\n",params.output);
217 params.messages_left = params.max_messages;
219 dlt_client_main_loop(&client, ¶ms, params.verbose);
221 if(params.output_handle > 0)
223 close(params.output_handle);
229 * Print current test statistics
231 void print_stats(s_statistics stats, s_parameters params)
233 static int last_print_time;
234 if(last_print_time >= time(NULL) && // Only print once a second
235 (stats.messages_received+stats.broken_messages_received) % 1000 != 0 &&
236 params.messages_left != 0) // Print also every 1000th message
240 printf("\033[2J\033[1;1H"); // Clear screen.
241 printf("Statistics:\n");
242 printf(" Messages received : %d\n", stats.messages_received);
243 printf(" Broken messages received : %d\n", stats.broken_messages_received);
244 printf(" Bytes received : %d\n", stats.bytes_received);
245 printf(" Time running (seconds) : %ld\n", time(NULL)-stats.first_message_time);
246 printf(" Throughput (msgs/sec)/(B/sec) : %ld/%ld\n",
247 stats.messages_received/((time(NULL)-stats.first_message_time)+1),
248 (stats.bytes_received)/((time(NULL)-stats.first_message_time)+1));
249 if(params.messages_left == 0)
251 if(stats.broken_messages_received == 0)
252 printf("All messages received succesfully!\n");
254 printf("Test failure! There were %d broken messages.", stats.broken_messages_received);
258 last_print_time = time(NULL);
261 * Callback for dlt client
263 int receive(DltMessage *msg, void *data) {
264 static s_statistics stats;
267 s_parameters *params = (s_parameters *)data;
270 memcpy(apid, msg->extendedheader->apid, 4);
272 if(apid[0] != 'M' || apid[1] != 'T') // TODO: Check through the app description
275 params->messages_left--;
277 if(stats.first_message_time == 0)
279 stats.first_message_time = time(NULL);
282 int buflen = msg->datasize + 1;
283 char *buf = malloc(buflen);
286 printf("Out of memory\n");
289 memset(buf, 0, buflen);
291 dlt_message_payload(msg,buf,buflen-1,DLT_OUTPUT_ASCII,0);
293 if(strcmp(buf, PAYLOAD_DATA) == 0)
295 stats.messages_received++;
299 stats.broken_messages_received++;
301 stats.bytes_received += msg->datasize+msg->headersize-sizeof(DltStorageHeader);;
305 print_stats(stats, *params);
307 if (params->output_handle > 0)
309 iov[0].iov_base = msg->headerbuffer;
310 iov[0].iov_len = msg->headersize;
311 iov[1].iov_base = msg->databuffer;
312 iov[1].iov_len = msg->datasize;
314 stats.output_bytes += writev(params->output_handle, iov, 2);
316 if(params->messages_left < 1)
318 dlt_client_cleanup(params->client_ref, params->verbose);