automotive-dlt
dlt-dbus.c
Go to the documentation of this file.
1 /*
2  * @licence app begin@
3  * SPDX license identifier: MPL-2.0
4  *
5  * Copyright (C) 2011-2015, BMW AG
6  *
7  * This file is part of GENIVI Project DLT - Diagnostic Log and Trace.
8  *
9  * This Source Code Form is subject to the terms of the
10  * Mozilla Public License (MPL), v. 2.0.
11  * If a copy of the MPL was not distributed with this file,
12  * You can obtain one at http://mozilla.org/MPL/2.0/.
13  *
14  * For further information see http://www.genivi.org/.
15  * @licence end@
16  */
17 
27 #include "dlt-dbus.h"
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <dbus/dbus.h>
33 #include <sys/time.h>
34 #include <dlt.h>
35 
36 #include <time.h>
37 
39 DLT_DECLARE_CONTEXT(dbusContext)
40 
41 static char dbus_message_buffer[DBUS_MAXIMUM_MESSAGE_LENGTH];
42 
43 static DBusHandlerResult
44 filter_func (DBusConnection *con,
45  DBusMessage *message,
46  void *data)
47 {
48  char **buf;
49  int len_p;
50  UNUSED(con);
51  UNUSED(data);
52 
53  buf = (char**)&dbus_message_buffer;
54  if (!dbus_message_marshal(message,
55  buf,
56  &len_p))
57  {
58  fprintf (stderr, "Failed to serialize DBus message!\n");
59  return DBUS_HANDLER_RESULT_HANDLED;
60  }
61 
62  DLT_TRACE_NETWORK_SEGMENTED(dbusContext,DLT_NW_TRACE_IPC,0,0,len_p,(void *)*buf);
63 
64  free(*buf);
65  *buf = NULL;
66 
67  if (dbus_message_is_signal (message,
68  DBUS_INTERFACE_LOCAL,
69  "Disconnected"))
70  {
71  DLT_UNREGISTER_CONTEXT (dbusContext);
73  exit (0);
74  }
75 
76 
77  /* Conceptually we want this to be
78  * DBUS_HANDLER_RESULT_NOT_YET_HANDLED, but this raises
79  * some problems. See bug 1719.
80  */
81  return DBUS_HANDLER_RESULT_HANDLED;
82 }
83 
84 int main (int argc, char *argv[])
85 {
86  DltDBusCliOptions options;
88 
89  DBusConnection *connection;
90  DBusError error;
91  DBusBusType type;
92 
93  int num;
94 
95  if(read_command_line(&options, argc, argv) < 0)
96  {
97  fprintf(stderr, "Failed to read command line!\n");
98  return -1;
99  }
100 
101  if(read_configuration_file(&config, options.ConfigurationFileName) < 0)
102  {
103  fprintf(stderr, "Failed to read configuration file!\n");
104  return -1;
105  }
106 
107  // register application
108  if(options.ApplicationId)
109  DLT_REGISTER_APP (options.ApplicationId, "DBus Logging");
110  else
111  DLT_REGISTER_APP (config.ApplicationId, "DBus Logging");
112 
113  // register context
114  DLT_REGISTER_CONTEXT_LL_TS(dbusContext, config.DBus.ContextId, "DBus Context for Logging",DLT_LOG_INFO,DLT_TRACE_STATUS_ON);
115  DLT_REGISTER_CONTEXT(dbusLog, "Log", "DBus Context for Logging Generic information");
116 
117  // initialise error handler
118  dbus_error_init (&error);
119 
120  // set DBus bus type
121  if(options.BusType)
122  type = (DBusBusType) atoi(options.BusType);
123  else
124  type = (DBusBusType) atoi(config.DBus.BusType);
125 
126  // get connection
127  connection = dbus_bus_get (type, &error);
128 
129  if(type==0)
130  DLT_LOG(dbusLog,DLT_LOG_INFO,DLT_STRING("BusType"),DLT_STRING("Session Bus"));
131  else if(type==1)
132  DLT_LOG(dbusLog,DLT_LOG_INFO,DLT_STRING("BusType"),DLT_STRING("System Bus"));
133  else
134  DLT_LOG(dbusLog,DLT_LOG_INFO,DLT_STRING("BusType"),DLT_INT(type));
135 
136  if (NULL == connection)
137  {
138  fprintf (stderr, "Failed to open connection to %d: %s\n",
139  DBUS_BUS_SYSTEM,
140  error.message);
141  dbus_error_free (&error);
142  exit (1);
143  }
144 
145  for(num=0;num<config.DBus.FilterCount;num++)
146  {
147  dbus_bus_add_match (connection,
148  config.DBus.FilterMatch[num],
149  &error);
150  printf("Added FilterMatch: %s\n",config.DBus.FilterMatch[num]);
151  DLT_LOG(dbusLog,DLT_LOG_INFO,DLT_STRING("FilterMatch"),DLT_UINT(num+1),DLT_STRING(config.DBus.FilterMatch[num]));
152  if (dbus_error_is_set (&error))
153  goto fail;
154  }
155 
156  if (!dbus_connection_add_filter (connection, filter_func, NULL, NULL)) {
157  fprintf (stderr, "Couldn't add filter!\n");
158  exit (1);
159  }
160 
161  while (dbus_connection_read_write_dispatch(connection, -1))
162  ;
163 
164  DLT_UNREGISTER_CONTEXT (dbusContext);
165  DLT_UNREGISTER_CONTEXT (dbusLog);
167  exit(1);
168 
169 fail:
170 
171  /* fail */
172  fprintf (stderr, "Error: %s\n", error.message);
173  DLT_UNREGISTER_CONTEXT (dbusContext);
174  DLT_UNREGISTER_CONTEXT (dbusLog);
176  exit(1);
177 
178 }
#define DLT_UNREGISTER_APP()
DBusOptions DBus
Definition: dlt-dbus.h:64
char * ApplicationId
Definition: dlt-dbus.h:63
DltKpiConfig config
Definition: dlt-kpi.c:37
int FilterCount
Definition: dlt-dbus.h:58
char * FilterMatch[DLT_DBUS_FILTER_MAX]
Definition: dlt-dbus.h:59
DLT_DECLARE_CONTEXT(dbusLog)
Definition: dlt-dbus.c:38
#define DLT_INT(INT_VAR)
#define UNUSED(x)
Definition: dlt-dbus.h:39
char * ContextId
Definition: dlt-dbus.h:56
char * ConfigurationFileName
Definition: dlt-dbus.h:48
static char data[kDataSize]
Definition: city-test.cc:40
int read_configuration_file(DltDBusConfiguration *config, char *file_name)
#define DLT_REGISTER_APP(APPID, DESCRIPTION)
#define DLT_STRING(TEXT)
#define DLT_REGISTER_CONTEXT(CONTEXT, CONTEXTID, DESCRIPTION)
#define DLT_TRACE_NETWORK_SEGMENTED(CONTEXT, TYPE, HEADERLEN, HEADER, PAYLOADLEN, PAYLOAD)
char * BusType
Definition: dlt-dbus.h:57
char * BusType
Definition: dlt-dbus.h:50
#define DLT_UNREGISTER_CONTEXT(CONTEXT)
#define DLT_REGISTER_CONTEXT_LL_TS(CONTEXT, CONTEXTID, DESCRIPTION, LOGLEVEL, TRACESTATUS)
#define DLT_LOG(CONTEXT, LOGLEVEL, ARGS...)
#define DLT_UINT(UINT_VAR)
int read_command_line(DltDBusCliOptions *options, int argc, char *argv[])
char * ApplicationId
Definition: dlt-dbus.h:49
int main(int argc, char *argv[])
Definition: dlt-dbus.c:84
#define NULL
Definition: dlt_common.h:232