some updates, new tests
[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 * THIS CODE HAS BEEN GENERATED BY ENTERPRISE ARCHITECT GENIVI MODEL. PLEASE CHANGE ONLY IN ENTERPRISE ARCHITECT AND GENERATE AGAIN
24 */
25
26 #include "DBusWrapper.h"
27 #include <dlt/dlt.h>
28 #include <fstream>
29 #include <sstream>
30 #include <string>
31
32 DLT_IMPORT_CONTEXT(AudioManager)
33
34
35 #define ROOT_INTROSPECT_XML                                                                                             \
36 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE                                                               \
37 "<node>"                                                                                                                                \
38 "<interface name='org.freedesktop.DBus.Introspectable'>"                                \
39 "<method name='Introspect'>"                                                                                    \
40 "       <arg name='xml_data' type='s' direction='out'/>"                                        \
41 "</method>"                                                                                                                     \
42 "</interface>"                                                                                                                  \
43
44 DBusWrapper* DBusWrapper::mReference = NULL;
45
46
47 DBusWrapper::DBusWrapper()
48         : mDbusConnection(0),
49           mDBusError(),
50           mNodesList()
51 {
52     dbus_error_init(&mDBusError);
53
54     DLT_LOG(AudioManager,DLT_LOG_INFO, DLT_STRING("DBusWrapper::DBusWrapper Opening DBus connection"));
55
56     mDbusConnection=dbus_bus_get(DBUS_BUS_SESSION, &mDBusError);
57     if (dbus_error_is_set(&mDBusError))
58     {
59         DLT_LOG(AudioManager,DLT_LOG_ERROR, DLT_STRING("DBusWrapper::DBusWrapper Error while getting the DBus"));
60         dbus_error_free(&mDBusError);
61     }
62     if (NULL == mDbusConnection)
63     {
64         DLT_LOG(AudioManager,DLT_LOG_ERROR, DLT_STRING("DBusWrapper::DBusWrapper DBus Connection is null"));
65     }
66
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))
71     {
72         DLT_LOG(AudioManager,DLT_LOG_ERROR, DLT_STRING("DBusWrapper::DBusWrapper Name Error"),DLT_STRING(mDBusError.message));
73         dbus_error_free(&mDBusError);
74     }
75     if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret)
76     {
77         DLT_LOG(AudioManager,DLT_LOG_ERROR, DLT_STRING("DBusWrapper::DBusWrapper Wrapper is not the Primary Owner"), DLT_INT(ret));
78     }
79 }
80
81 DBusWrapper::~DBusWrapper()
82 {
83         //close the connection again
84         DLT_LOG(AudioManager,DLT_LOG_INFO, DLT_STRING("DBusWrapper::~DBusWrapper Closing DBus connection"));
85         dbus_connection_close(mDbusConnection);
86 }
87
88 void DBusWrapper::registerCallback(const DBusObjectPathVTable* vtable, const std::string& path, void* userdata)
89 {
90         DLT_LOG(AudioManager,DLT_LOG_INFO, DLT_STRING("DBusWrapper::~registerCallback register callback:"),DLT_STRING(path.c_str()));
91
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))
97     {
98         DLT_LOG(AudioManager,DLT_LOG_ERROR, DLT_STRING("DBusWrapper::registerCallack error: "),DLT_STRING(mDBusError.message));
99         dbus_error_free(&mDBusError);
100     }
101         mNodesList.push_back(path);
102 }
103
104 DBusHandlerResult DBusWrapper::cbRootIntrospection(DBusConnection *conn, DBusMessage *msg, void *reference)
105 {
106         DLT_LOG(AudioManager,DLT_LOG_INFO, DLT_STRING("DBusWrapper::~cbRootIntrospection called:"));
107
108         mReference=(DBusWrapper*)reference;
109         std::list<std::string>nodesList=mReference->mNodesList;
110         DBusMessage * reply;
111     DBusMessageIter args;
112     dbus_uint32_t serial = 0;
113         if (dbus_message_is_method_call(msg, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
114         {
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)
120                 {
121                         introspect<<"<node name='"<<nodeIter->c_str()<<"'/>";
122                 }
123                 introspect<<"</node>";
124
125                 reply = dbus_message_new_method_return(msg);
126             std::string s = introspect.str();
127             const char* string=s.c_str();
128
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))
132             {
133                 DLT_LOG(AudioManager,DLT_LOG_ERROR, DLT_STRING("DBusWrapper::~cbRootIntrospection DBUS Out Of Memory!"));
134             }
135
136             // send the reply && flush the connection
137             if (!dbus_connection_send(conn, reply, &serial))
138             {
139                 DLT_LOG(AudioManager,DLT_LOG_ERROR, DLT_STRING("DBusWrapper::~cbRootIntrospection DBUS Out Of Memory!"));
140             }
141             dbus_connection_flush(conn);
142             // free the reply
143             dbus_message_unref(reply);
144
145                 return DBUS_HANDLER_RESULT_HANDLED;
146         }
147         else
148         {
149                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
150         }
151 }
152
153 void DBusWrapper::dbusMainLoop()
154 {
155         DLT_LOG(AudioManager,DLT_LOG_INFO, DLT_STRING("DBusWrapper::~dbusMainLoop Entering MainLoop"));
156
157         while (dbus_connection_read_write_dispatch(mDbusConnection, -1))
158         {
159
160         }
161 }
162
163 void DBusWrapper::getDBusConnection(DBusConnection *& connection) const
164 {
165         connection=mDbusConnection;
166 }
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182