2 * Copyright (C) 2011, BMW AG
4 * GeniviAudioMananger AudioManagerDaemon
6 * \file DBusWrapper.cpp
8 * \date 20-Oct-2011 3:42:04 PM
9 * \author Christian Mueller (christian.ei.mueller@bmw.de)
12 * GNU Lesser General Public License, version 2.1, with special exception (GENIVI clause)
13 * Copyright (C) 2011, BMW AG Christian Mueller Christian.ei.mueller@bmw.de
15 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License, version 2.1, as published by the Free Software Foundation.
16 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License, version 2.1, for more details.
17 * You should have received a copy of the GNU Lesser General Public License, version 2.1, along with this program; if not, see <http://www.gnu.org/licenses/lgpl-2.1.html>.
18 * Note that the copyright holders assume that the GNU Lesser General Public License, version 2.1, may also be applicable to programs even in cases in which the program is not a library in the technical sense.
19 * Linking AudioManager statically or dynamically with other modules is making a combined work based on AudioManager. You may license such other modules under the GNU Lesser General Public License, version 2.1. If you do not want to license your linked modules under the GNU Lesser General Public License, version 2.1, you may use the program under the following exception.
20 * As a special exception, the copyright holders of AudioManager give you permission to combine AudioManager with software programs or libraries that are released under any license unless such a combination is not permitted by the license of such a software program or library. You may copy and distribute such a system following the terms of the GNU Lesser General Public License, version 2.1, including this special exception, for AudioManager and the licenses of the other code concerned.
21 * Note that people who make modified versions of AudioManager are not obligated to grant this special exception for their modified versions; it is their choice whether to do so. The GNU Lesser General Public License, version 2.1, gives permission to release a modified version without this exception; this exception also makes it possible to release a modified version which carries forward this exception.
25 #include "DBusWrapper.h"
31 DLT_IMPORT_CONTEXT(DLT_CONTEXT)
34 #define ROOT_INTROSPECT_XML \
35 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
37 "<interface name='orAudioManagerg.freedesktop.DBus.Introspectable'>" \
38 "<method name='Introspect'>" \
39 " <arg name='xml_data' type='s' direction='out'/>" \
43 DBusWrapper* DBusWrapper::mReference = NULL;
46 DBusWrapper::DBusWrapper()
51 dbus_error_init(&mDBusError);
53 DLT_LOG(DLT_CONTEXT,DLT_LOG_INFO, DLT_STRING("DBusWrapper::DBusWrapper Opening DBus connection"));
55 mDbusConnection=dbus_bus_get(DBUS_BUS_SESSION, &mDBusError);
56 if (dbus_error_is_set(&mDBusError))
58 DLT_LOG(DLT_CONTEXT,DLT_LOG_ERROR, DLT_STRING("DBusWrapper::DBusWrapper Error while getting the DBus"));
59 dbus_error_free(&mDBusError);
61 if (NULL == mDbusConnection)
63 DLT_LOG(DLT_CONTEXT,DLT_LOG_ERROR, DLT_STRING("DBusWrapper::DBusWrapper DBus Connection is null"));
66 mObjectPathVTable.message_function=DBusWrapper::cbRootIntrospection;
67 dbus_connection_register_object_path(mDbusConnection,DBUS_SERVICE_OBJECT_PATH , &mObjectPathVTable, this);
68 int ret = dbus_bus_request_name(mDbusConnection,DBUS_SERVICE_PREFIX,DBUS_NAME_FLAG_DO_NOT_QUEUE, &mDBusError);
69 if (dbus_error_is_set(&mDBusError))
71 DLT_LOG(DLT_CONTEXT,DLT_LOG_ERROR, DLT_STRING("DBusWrapper::DBusWrapper Name Error"),DLT_STRING(mDBusError.message));
72 dbus_error_free(&mDBusError);
74 if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret)
76 DLT_LOG(DLT_CONTEXT,DLT_LOG_ERROR, DLT_STRING("DBusWrapper::DBusWrapper Wrapper is not the Primary Owner"), DLT_INT(ret));
80 DBusWrapper::~DBusWrapper()
82 //close the connection again
83 DLT_LOG(DLT_CONTEXT,DLT_LOG_INFO, DLT_STRING("DBusWrapper::~DBusWrapper Closing DBus connection"));
84 dbus_connection_unref(mDbusConnection);
87 void DBusWrapper::registerCallback(const DBusObjectPathVTable* vtable, const std::string& path, void* userdata)
89 DLT_LOG(DLT_CONTEXT,DLT_LOG_INFO, DLT_STRING("DBusWrapper::~registerCallback register callback:"),DLT_STRING(path.c_str()));
91 std::string completePath=std::string(DBUS_SERVICE_OBJECT_PATH)+"/"+path;
92 dbus_error_init(&mDBusError);
93 mDbusConnection=dbus_bus_get(DBUS_BUS_SESSION, &mDBusError);
94 dbus_connection_register_object_path(mDbusConnection,completePath.c_str(), vtable, userdata);
95 if (dbus_error_is_set(&mDBusError))
97 DLT_LOG(DLT_CONTEXT,DLT_LOG_ERROR, DLT_STRING("DBusWrapper::registerCallack error: "),DLT_STRING(mDBusError.message));
98 dbus_error_free(&mDBusError);
100 mNodesList.push_back(path);
103 DBusHandlerResult DBusWrapper::cbRootIntrospection(DBusConnection *conn, DBusMessage *msg, void *reference)
105 DLT_LOG(DLT_CONTEXT,DLT_LOG_INFO, DLT_STRING("DBusWrapper::~cbRootIntrospection called:"));
107 mReference=(DBusWrapper*)reference;
108 std::list<std::string>nodesList=mReference->mNodesList;
110 DBusMessageIter args;
111 dbus_uint32_t serial = 0;
112 if (dbus_message_is_method_call(msg, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
114 std::list<std::string>::iterator nodeIter=nodesList.begin();
115 const char *xml=ROOT_INTROSPECT_XML;
116 std::stringstream introspect;
117 introspect << std::string(xml);
118 for(;nodeIter!=nodesList.end();++nodeIter)
120 introspect<<"<node name='"<<nodeIter->c_str()<<"'/>";
122 introspect<<"</node>";
124 reply = dbus_message_new_method_return(msg);
125 std::string s = introspect.str();
126 const char* string=s.c_str();
128 // add the arguments to the reply
129 dbus_message_iter_init_append(reply, &args);
130 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &string))
132 DLT_LOG(DLT_CONTEXT,DLT_LOG_ERROR, DLT_STRING("DBusWrapper::~cbRootIntrospection DBUS Out Of Memory!"));
135 // send the reply && flush the connection
136 if (!dbus_connection_send(conn, reply, &serial))
138 DLT_LOG(DLT_CONTEXT,DLT_LOG_ERROR, DLT_STRING("DBusWrapper::~cbRootIntrospection DBUS Out Of Memory!"));
140 dbus_connection_flush(conn);
142 dbus_message_unref(reply);
144 return DBUS_HANDLER_RESULT_HANDLED;
148 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
152 void DBusWrapper::dbusMainLoop()
154 DLT_LOG(DLT_CONTEXT,DLT_LOG_INFO, DLT_STRING("DBusWrapper::~dbusMainLoop Entering MainLoop"));
156 while (dbus_connection_read_write_dispatch(mDbusConnection, -1))
162 void DBusWrapper::getDBusConnection(DBusConnection *& connection) const
164 connection=mDbusConnection;