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.
23 * THIS CODE HAS BEEN GENERATED BY ENTERPRISE ARCHITECT GENIVI MODEL. PLEASE CHANGE ONLY IN ENTERPRISE ARCHITECT AND GENERATE AGAIN
26 #include "DBusWrapper.h"
32 DLT_IMPORT_CONTEXT(AudioManager)
35 #define ROOT_INTROSPECT_XML \
36 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
38 "<interface name='org.freedesktop.DBus.Introspectable'>" \
39 "<method name='Introspect'>" \
40 " <arg name='xml_data' type='s' direction='out'/>" \
44 DBusWrapper* DBusWrapper::mReference = NULL;
47 DBusWrapper::DBusWrapper()
52 dbus_error_init(&mDBusError);
54 DLT_LOG(AudioManager,DLT_LOG_INFO, DLT_STRING("DBusWrapper::DBusWrapper Opening DBus connection"));
56 mDbusConnection=dbus_bus_get(DBUS_BUS_SESSION, &mDBusError);
57 if (dbus_error_is_set(&mDBusError))
59 DLT_LOG(AudioManager,DLT_LOG_ERROR, DLT_STRING("DBusWrapper::DBusWrapper Error while getting the DBus"));
60 dbus_error_free(&mDBusError);
62 if (NULL == mDbusConnection)
64 DLT_LOG(AudioManager,DLT_LOG_ERROR, DLT_STRING("DBusWrapper::DBusWrapper DBus Connection is null"));
67 mObjectPathVTable.message_function=DBusWrapper::cbRootIntrospection;
68 dbus_connection_register_object_path(mDbusConnection,DBUS_SERVICE_OBJECT_PATH , &mObjectPathVTable, this);
69 int ret = dbus_bus_request_name(mDbusConnection,DBUS_SERVICE_PREFIX,DBUS_NAME_FLAG_DO_NOT_QUEUE, &mDBusError);
70 if (dbus_error_is_set(&mDBusError))
72 DLT_LOG(AudioManager,DLT_LOG_ERROR, DLT_STRING("DBusWrapper::DBusWrapper Name Error"),DLT_STRING(mDBusError.message));
73 dbus_error_free(&mDBusError);
75 if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret)
77 DLT_LOG(AudioManager,DLT_LOG_ERROR, DLT_STRING("DBusWrapper::DBusWrapper Wrapper is not the Primary Owner"), DLT_INT(ret));
81 DBusWrapper::~DBusWrapper()
83 //close the connection again
84 DLT_LOG(AudioManager,DLT_LOG_INFO, DLT_STRING("DBusWrapper::~DBusWrapper Closing DBus connection"));
85 dbus_connection_close(mDbusConnection);
88 void DBusWrapper::registerCallback(const DBusObjectPathVTable* vtable, const std::string& path, void* userdata)
90 DLT_LOG(AudioManager,DLT_LOG_INFO, DLT_STRING("DBusWrapper::~registerCallback register callback:"),DLT_STRING(path.c_str()));
92 std::string completePath=std::string(DBUS_SERVICE_OBJECT_PATH)+"/"+path;
93 dbus_error_init(&mDBusError);
94 mDbusConnection=dbus_bus_get(DBUS_BUS_SESSION, &mDBusError);
95 dbus_connection_register_object_path(mDbusConnection,completePath.c_str(), vtable, userdata);
96 if (dbus_error_is_set(&mDBusError))
98 DLT_LOG(AudioManager,DLT_LOG_ERROR, DLT_STRING("DBusWrapper::registerCallack error: "),DLT_STRING(mDBusError.message));
99 dbus_error_free(&mDBusError);
101 mNodesList.push_back(path);
104 DBusHandlerResult DBusWrapper::cbRootIntrospection(DBusConnection *conn, DBusMessage *msg, void *reference)
106 DLT_LOG(AudioManager,DLT_LOG_INFO, DLT_STRING("DBusWrapper::~cbRootIntrospection called:"));
108 mReference=(DBusWrapper*)reference;
109 std::list<std::string>nodesList=mReference->mNodesList;
111 DBusMessageIter args;
112 dbus_uint32_t serial = 0;
113 if (dbus_message_is_method_call(msg, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
115 std::list<std::string>::iterator nodeIter=nodesList.begin();
116 const char *xml=ROOT_INTROSPECT_XML;
117 std::stringstream introspect;
118 introspect << std::string(xml);
119 for(;nodeIter!=nodesList.end();++nodeIter)
121 introspect<<"<node name='"<<nodeIter->c_str()<<"'/>";
123 introspect<<"</node>";
125 reply = dbus_message_new_method_return(msg);
126 std::string s = introspect.str();
127 const char* string=s.c_str();
129 // add the arguments to the reply
130 dbus_message_iter_init_append(reply, &args);
131 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &string))
133 DLT_LOG(AudioManager,DLT_LOG_ERROR, DLT_STRING("DBusWrapper::~cbRootIntrospection DBUS Out Of Memory!"));
136 // send the reply && flush the connection
137 if (!dbus_connection_send(conn, reply, &serial))
139 DLT_LOG(AudioManager,DLT_LOG_ERROR, DLT_STRING("DBusWrapper::~cbRootIntrospection DBUS Out Of Memory!"));
141 dbus_connection_flush(conn);
143 dbus_message_unref(reply);
145 return DBUS_HANDLER_RESULT_HANDLED;
149 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
153 void DBusWrapper::dbusMainLoop()
155 DLT_LOG(AudioManager,DLT_LOG_INFO, DLT_STRING("DBusWrapper::~dbusMainLoop Entering MainLoop"));
157 while (dbus_connection_read_write_dispatch(mDbusConnection, -1))
163 void DBusWrapper::getDBusConnection(DBusConnection *& connection) const
165 connection=mDbusConnection;