automotive-dlt
dlt-control-common.c File Reference
#include <errno.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "dlt_common.h"
#include "dlt_protocol.h"
#include "dlt_client.h"
#include "dlt-control-common.h"
Include dependency graph for dlt-control-common.c:

Go to the source code of this file.

Macros

#define pr_fmt(fmt)   "Common control: "fmt
 
#define DLT_CTRL_APID   "DLTC"
 
#define DLT_CTRL_CTID   "DLTC"
 
#define DLT_DAEMON_DEFAULT_CTRL_SOCK_PATH   "/tmp/dlt-ctrl.sock"
 
#define DLT_RECEIVE_TEXTBUFSIZE   1024
 

Functions

int get_verbosity (void)
 
void set_verbosity (int v)
 
char * get_ecuid (void)
 
void set_ecuid (char *ecuid)
 
long get_timeout (void)
 
void set_timeout (long t)
 
int dlt_parse_config_param (char *config_id, char **config_data)
 
static int dlt_control_send_message_to_socket (int sock, DltMessage *msg)
 Send a message to the daemon through the socket. More...
 
static int prepare_extra_headers (DltMessage *msg, uint8_t *header)
 Prepare the extra headers of a DLT message. More...
 
static int prepare_headers (DltMessage *msg, uint8_t *header)
 Prepare the headers of a DLT message. More...
 
static DltMessagedlt_control_prepare_message (DltControlMsgBody *data)
 Prepare a DLT message. More...
 
static int dlt_control_init_connection (DltClient *client, void *cb)
 Initialize the connection with the daemon. More...
 
static void * dlt_control_listen_to_daemon (void *data)
 Daemon listener function. More...
 
static int dlt_control_callback (DltMessage *message, void *data)
 Internal callback for DLT response. More...
 
int dlt_control_send_message (DltControlMsgBody *body, int timeout)
 Send a message to the daemon and wait for the asynchronous answer. More...
 
int dlt_control_init (int(*response_analyzer)(char *, void *, int), char *ecuid, int verbosity)
 Control communication initialization. More...
 
int dlt_control_deinit (void)
 Control communication clean-up. More...
 

Variables

static int(* response_analyzer_cb )(char *, void *, int)
 Analyze the daemon answer. More...
 
static pthread_t daemon_connect_thread
 
static DltClient client
 
static int callback_return = -1
 
static pthread_mutex_t answer_lock = PTHREAD_MUTEX_INITIALIZER
 
static pthread_cond_t answer_cond = PTHREAD_COND_INITIALIZER
 
static int local_verbose
 
static char local_ecuid [DLT_CTRL_ECUID_LEN]
 
static long local_timeout
 

Detailed Description

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.

Author
Christoph Lipka clipk.nosp@m.a@jp.nosp@m..adit.nosp@m.-jv..nosp@m.com ADIT 2015
Frederic Berat fbera.nosp@m.t@de.nosp@m..adit.nosp@m.-jv..nosp@m.com ADIT 2015

For further information see http://www.genivi.org/. end@

Definition in file dlt-control-common.c.

Macro Definition Documentation

#define DLT_CTRL_APID   "DLTC"

Definition at line 69 of file dlt-control-common.c.

Referenced by prepare_extra_headers().

#define DLT_CTRL_CTID   "DLTC"

Definition at line 70 of file dlt-control-common.c.

Referenced by prepare_extra_headers().

#define DLT_DAEMON_DEFAULT_CTRL_SOCK_PATH   "/tmp/dlt-ctrl.sock"

Definition at line 71 of file dlt-control-common.c.

Referenced by dlt_control_init_connection(), and option_file_parser().

#define DLT_RECEIVE_TEXTBUFSIZE   1024

Definition at line 72 of file dlt-control-common.c.

Referenced by dlt_control_callback(), and dlt_parse_config_param().

#define pr_fmt (   fmt)    "Common control: "fmt

Definition at line 52 of file dlt-control-common.c.

Function Documentation

static int dlt_control_callback ( DltMessage message,
void *  data 
)
static

Internal callback for DLT response.

This function is called by the dlt_client_main_loop once a response is read from the DLT socket. After some basic checks, the user's response analyzer is called. The return value of the analyzer is then provided back to the dlt_control_send_message function to be given back as a return value. As this function is called in a dedicated thread, the return value is provided using a global variable. Access to this variable is controlled through a dedicated mutex. New values are signaled using a dedicated condition variable.

Parameters
messageThe DLT answer Unused
Returns
The analyzer return value or -1 on early errors.

Definition at line 526 of file dlt-control-common.c.

References answer_cond, answer_lock, callback_return, sDltMessage::databuffer, sDltMessage::datasize, DLT_IS_HTYP_WEID, dlt_message_header(), dlt_message_payload(), DLT_OUTPUT_ASCII, DLT_RECEIVE_TEXTBUFSIZE, dlt_set_storageheader(), get_verbosity(), sDltMessage::headerextra, NULL, pr_error, response_analyzer_cb, sDltMessage::standardheader, and sDltMessage::storageheader.

Referenced by dlt_control_init().

int dlt_control_deinit ( void  )

Control communication clean-up.

Cancels the listener thread and clean=up the dlt client structure.

Returns
0 on success, -1 otherwise.

Definition at line 688 of file dlt-control-common.c.

References daemon_connect_thread, dlt_client_cleanup(), and get_verbosity().

Referenced by dlt_logstorage_ctrl_setup_event_loop(), dlt_logstorage_ctrl_single_request(), and dlt_passive_node_ctrl_single_request().

int dlt_control_init ( int(*)(char *, void *, int)  response_analyzer,
char *  ecuid,
int  verbosity 
)

Control communication initialization.

This will prepare the DLT connection and the thread dedicated to the response listening.

Parameters
response_analyzerUser defined function used to analyze the response
ecuidThe ECUID to provide to the daemon
verbosityThe verbosity level
Returns
0 on success, -1 otherwise.

Definition at line 648 of file dlt-control-common.c.

References daemon_connect_thread, dlt_client_cleanup(), dlt_control_callback(), dlt_control_init_connection(), dlt_control_listen_to_daemon(), get_verbosity(), NULL, pr_error, response_analyzer_cb, set_ecuid(), and set_verbosity().

Referenced by dlt_logstorage_ctrl_setup_event_loop(), dlt_logstorage_ctrl_single_request(), and dlt_passive_node_ctrl_single_request().

static int dlt_control_init_connection ( DltClient client,
void *  cb 
)
static

Initialize the connection with the daemon.

The connection is initialized using an internal callback. The user's response analyzer will be finally executed by this callback. The client pointer is used to established the connection.

Parameters
clientA pointer to a valid client structure
cbThe internal callback to be executed while receiving a new message
Returns
0 on success, -1 otherwise.

Definition at line 459 of file dlt-control-common.c.

References data, dlt_client_connect(), dlt_client_init(), DLT_CLIENT_MODE_UNIX, dlt_client_register_message_callback(), dlt_client_set_socket_path(), DLT_DAEMON_DEFAULT_CTRL_SOCK_PATH, dlt_parse_config_param(), get_verbosity(), DltClient::mode, NULL, pr_error, pr_verbose, and DltClient::socketPath.

Referenced by dlt_control_init().

static void* dlt_control_listen_to_daemon ( void *  data)
static

Daemon listener function.

This function will continuously read on the DLT socket, until an error occurs or the thread executing this function is canceled.

Parameters
dataThread parameter
Returns
The thread parameter given as argument.

Definition at line 502 of file dlt-control-common.c.

References data, dlt_client_main_loop(), get_verbosity(), NULL, and pr_verbose.

Referenced by dlt_control_init().

static DltMessage* dlt_control_prepare_message ( DltControlMsgBody data)
static

Prepare a DLT message.

The DLT message is built using the data given by the user. The data is basically composed of a buffer and a size.

Parameters
dataThe message body to be used to build the DLT message.
Returns
0 on success, -1 otherwise.

Definition at line 386 of file dlt-control-common.c.

References DltControlMsgBody::data, sDltMessage::databuffer, sDltMessage::databuffersize, sDltMessage::datasize, dlt_message_free(), dlt_message_init(), get_verbosity(), sDltMessage::headerbuffer, NULL, pr_error, pr_verbose, prepare_extra_headers(), prepare_headers(), and DltControlMsgBody::size.

Referenced by dlt_control_send_message().

int dlt_control_send_message ( DltControlMsgBody body,
int  timeout 
)

Send a message to the daemon and wait for the asynchronous answer.

The answer is received and analyzed by a dedicated thread. Thus we need to wait for the signal from this thread and then read the return value to be provided to the user. In case of timeout, this function fails. The message provided by the user is formated in DLT format before sending.

Parameters
bodyThe message provided by the user
timeoutThe time to wait before considering that no answer will come
Returns
The user response analyzer return value, -1 in case of early error.

Definition at line 581 of file dlt-control-common.c.

References answer_cond, answer_lock, callback_return, dlt_control_prepare_message(), dlt_control_send_message_to_socket(), dlt_message_free(), get_verbosity(), NULL, pr_error, and DltClient::sock.

Referenced by dlt_logstorage_send_event(), and dlt_passive_node_ctrl_single_request().

static int dlt_control_send_message_to_socket ( int  sock,
DltMessage msg 
)
static

Send a message to the daemon through the socket.

The socket as to be opened and active before sending.

Parameters
sockThe socket to send the message through
msgThe message to be send in DLT format.
Returns
0 on success, -1 otherwise.

Definition at line 248 of file dlt-control-common.c.

References sDltMessage::databuffer, sDltMessage::datasize, sDltMessage::headerbuffer, sDltMessage::headersize, and pr_error.

Referenced by dlt_control_send_message().

int dlt_parse_config_param ( char *  config_id,
char **  config_data 
)
static int prepare_extra_headers ( DltMessage msg,
uint8_t *  header 
)
static

Prepare the extra headers of a DLT message.

Modifies the extra headers of the message so that it can be sent.

Parameters
msgThe message to be prepared
headerThe base header to be used.
Returns
0 on success, -1 otherwise.

Definition at line 276 of file dlt-control-common.c.

References DLT_CTRL_APID, DLT_CTRL_CTID, dlt_message_set_extraparameters(), DLT_MSIN_CONTROL_REQUEST, dlt_set_id(), DLT_STANDARD_HEADER_EXTRA_SIZE, dlt_uptime(), sDltMessage::extendedheader, get_ecuid(), get_verbosity(), sDltMessage::headerextra, pr_error, pr_verbose, and sDltMessage::standardheader.

Referenced by dlt_control_prepare_message().

static int prepare_headers ( DltMessage msg,
uint8_t *  header 
)
static

Prepare the headers of a DLT message.

Modifies the headers of the message so that it can be sent.

Parameters
msgThe message to be prepared
headerThe base header to be used.
Returns
0 on success, -1 otherwise.

Definition at line 326 of file dlt-control-common.c.

References sDltMessage::datasize, DLT_HTOBE_16, DLT_HTYP_MSBF, DLT_HTYP_PROTOCOL_VERSION1, DLT_HTYP_UEH, DLT_HTYP_WEID, DLT_HTYP_WTMS, dlt_set_storageheader(), DLT_STANDARD_HEADER_EXTRA_SIZE, sDltMessage::headersize, pr_error, pr_verbose, sDltMessage::standardheader, and sDltMessage::storageheader.

Referenced by dlt_control_prepare_message().

void set_ecuid ( char *  ecuid)
void set_timeout ( long  t)

Definition at line 146 of file dlt-control-common.c.

References DLT_CTRL_TIMEOUT, local_timeout, and pr_error.

Referenced by main(), and parse_args().

void set_verbosity ( int  v)

Definition at line 101 of file dlt-control-common.c.

References local_verbose.

Referenced by dlt_control_init(), and parse_args().

Variable Documentation

pthread_cond_t answer_cond = PTHREAD_COND_INITIALIZER
static

Definition at line 90 of file dlt-control-common.c.

Referenced by dlt_control_callback(), and dlt_control_send_message().

pthread_mutex_t answer_lock = PTHREAD_MUTEX_INITIALIZER
static

Definition at line 89 of file dlt-control-common.c.

Referenced by dlt_control_callback(), and dlt_control_send_message().

int callback_return = -1
static

Definition at line 88 of file dlt-control-common.c.

Referenced by dlt_control_callback(), and dlt_control_send_message().

DltClient client
static

Definition at line 87 of file dlt-control-common.c.

Referenced by main().

pthread_t daemon_connect_thread
static

Definition at line 86 of file dlt-control-common.c.

Referenced by dlt_control_deinit(), and dlt_control_init().

char local_ecuid[DLT_CTRL_ECUID_LEN]
static

Definition at line 93 of file dlt-control-common.c.

Referenced by get_ecuid(), and set_ecuid().

long local_timeout
static

Definition at line 94 of file dlt-control-common.c.

Referenced by get_timeout(), and set_timeout().

int local_verbose
static

Definition at line 92 of file dlt-control-common.c.

Referenced by get_verbosity(), and set_verbosity().

int(* response_analyzer_cb) (char *, void *, int)
static

Analyze the daemon answer.

This function as to be provided by the user of the connection.

Parameters
answerThe textual answer of the daemon
payloadThe daemons answer payload
lengthThe daemons answer payload length
Returns
User defined.

Definition at line 84 of file dlt-control-common.c.

Referenced by dlt_control_callback(), and dlt_control_init().