changes to make controlSender compile & loadable
[profile/ivi/audiomanager.git] / AudioManagerDaemon / src / DBusWrapper.cpp
1 /**
2 * Copyright (C) 2011, BMW AG
3 *
4 * GeniviAudioMananger AudioManagerDaemon
5 *
6 * \file DBusWrapper.cpp
7 *
8 * \date 20-Oct-2011 3:42:04 PM
9 * \author Christian Mueller (christian.ei.mueller@bmw.de)
10 *
11 * \section License
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
14 *
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.
22 *
23 */
24
25 #include "DBusWrapper.h"
26 #include <dlt/dlt.h>
27 #include <fstream>
28 #include <sstream>
29 #include <string>
30
31 DLT_IMPORT_CONTEXT(AudioManager)
32
33
34 #define ROOT_INTROSPECT_XML                                                                                             \
35 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE                                                               \
36 "<node>"                                                                                                                                \
37 "<interface name='org.freedesktop.DBus.Introspectable'>"                                \
38 "<method name='Introspect'>"                                                                                    \
39 "       <arg name='xml_data' type='s' direction='out'/>"                                        \
40 "</method>"                                                                                                                     \
41 "</interface>"                                                                                                                  \
42
43 DBusWrapper* DBusWrapper::mReference = NULL;
44
45
46 DBusWrapper::DBusWrapper()
47         : mDbusConnection(0),
48           mDBusError(),
49           mNodesList()
50 {
51     dbus_error_init(&mDBusError);
52
53     DLT_LOG(AudioManager,DLT_LOG_INFO, DLT_STRING("DBusWrapper::DBusWrapper Opening DBus connection"));
54
55     mDbusConnection=dbus_bus_get(DBUS_BUS_SESSION, &mDBusError);
56     if (dbus_error_is_set(&mDBusError))
57     {
58         DLT_LOG(AudioManager,DLT_LOG_ERROR, DLT_STRING("DBusWrapper::DBusWrapper Error while getting the DBus"));
59         dbus_error_free(&mDBusError);
60     }
61     if (NULL == mDbusConnection)
62     {
63         DLT_LOG(AudioManager,DLT_LOG_ERROR, DLT_STRING("DBusWrapper::DBusWrapper DBus Connection is null"));
64     }
65
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))
70     {
71         DLT_LOG(AudioManager,DLT_LOG_ERROR, DLT_STRING("DBusWrapper::DBusWrapper Name Error"),DLT_STRING(mDBusError.message));
72         dbus_error_free(&mDBusError);
73     }
74     if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret)
75     {
76         DLT_LOG(AudioManager,DLT_LOG_ERROR, DLT_STRING("DBusWrapper::DBusWrapper Wrapper is not the Primary Owner"), DLT_INT(ret));
77     }
78 }
79
80 DBusWrapper::~DBusWrapper()
81 {
82         //close the connection again
83         DLT_LOG(AudioManager,DLT_LOG_INFO, DLT_STRING("DBusWrapper::~DBusWrapper Closing DBus connection"));
84         dbus_connection_unref(mDbusConnection);
85 }
86
87 void DBusWrapper::registerCallback(const DBusObjectPathVTable* vtable, const std::string& path, void* userdata)
88 {
89         DLT_LOG(AudioManager,DLT_LOG_INFO, DLT_STRING("DBusWrapper::~registerCallback register callback:"),DLT_STRING(path.c_str()));
90
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))
96     {
97         DLT_LOG(AudioManager,DLT_LOG_ERROR, DLT_STRING("DBusWrapper::registerCallack error: "),DLT_STRING(mDBusError.message));
98         dbus_error_free(&mDBusError);
99     }
100         mNodesList.push_back(path);
101 }
102
103 DBusHandlerResult DBusWrapper::cbRootIntrospection(DBusConnection *conn, DBusMessage *msg, void *reference)
104 {
105         DLT_LOG(AudioManager,DLT_LOG_INFO, DLT_STRING("DBusWrapper::~cbRootIntrospection called:"));
106
107         mReference=(DBusWrapper*)reference;
108         std::list<std::string>nodesList=mReference->mNodesList;
109         DBusMessage * reply;
110     DBusMessageIter args;
111     dbus_uint32_t serial = 0;
112         if (dbus_message_is_method_call(msg, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
113         {
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)
119                 {
120                         introspect<<"<node name='"<<nodeIter->c_str()<<"'/>";
121                 }
122                 introspect<<"</node>";
123
124                 reply = dbus_message_new_method_return(msg);
125             std::string s = introspect.str();
126             const char* string=s.c_str();
127
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))
131             {
132                 DLT_LOG(AudioManager,DLT_LOG_ERROR, DLT_STRING("DBusWrapper::~cbRootIntrospection DBUS Out Of Memory!"));
133             }
134
135             // send the reply && flush the connection
136             if (!dbus_connection_send(conn, reply, &serial))
137             {
138                 DLT_LOG(AudioManager,DLT_LOG_ERROR, DLT_STRING("DBusWrapper::~cbRootIntrospection DBUS Out Of Memory!"));
139             }
140             dbus_connection_flush(conn);
141             // free the reply
142             dbus_message_unref(reply);
143
144                 return DBUS_HANDLER_RESULT_HANDLED;
145         }
146         else
147         {
148                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
149         }
150 }
151
152 void DBusWrapper::dbusMainLoop()
153 {
154         DLT_LOG(AudioManager,DLT_LOG_INFO, DLT_STRING("DBusWrapper::~dbusMainLoop Entering MainLoop"));
155
156         while (dbus_connection_read_write_dispatch(mDbusConnection, -1))
157         {
158
159         }
160 }
161
162 void DBusWrapper::getDBusConnection(DBusConnection *& connection) const
163 {
164         connection=mDbusConnection;
165 }
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181