2 * Copyright (C) 2011, BMW AG
4 * GeniviAudioMananger AudioManagerDaemon
6 * \file SocketHandler.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 <dbus/DBusWrapper.h>
26 #include <SocketHandler.h>
33 #include "DLTWrapper.h"
37 #define ROOT_INTROSPECT_XML \
38 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
40 "<interface name='org.AudioManager.freedesktop.DBus.Introspectable'>" \
41 "<method name='Introspect'>" \
42 " <arg name='xml_data' type='s' direction='out'/>" \
46 DBusWrapper* DBusWrapper::mReference = NULL;
48 DBusWrapper::DBusWrapper(SocketHandler* socketHandler) :
49 pDbusDispatchCallback(this, &DBusWrapper::dbusDispatchCallback), //
50 pDbusFireCallback(this, &DBusWrapper::dbusFireCallback), //
51 pDbusCheckCallback(this, &DBusWrapper::dbusCheckCallback), //
52 pDbusTimerCallback(this, &DBusWrapper::dbusTimerCallback), //
53 mDbusConnection(0), //
56 mListTimerhandlePointer(), //
57 mSocketHandler(socketHandler)
59 assert(mSocketHandler!=0);
61 dbus_error_init(&mDBusError);
62 logInfo("DBusWrapper::DBusWrapper Opening DBus connection");
63 mDbusConnection = dbus_bus_get(DBUS_BUS_SESSION, &mDBusError);
64 if (dbus_error_is_set(&mDBusError))
66 logError("DBusWrapper::DBusWrapper Error while getting the DBus");
67 dbus_error_free(&mDBusError);
69 if (NULL == mDbusConnection)
71 logError("DBusWrapper::DBusWrapper DBus Connection is null");
74 //then we need to adopt the dbus to our mainloop:
75 //first, we are old enought to live longer then the connection:
76 dbus_connection_set_exit_on_disconnect(mDbusConnection, FALSE);
78 //we do not need the manual dispatching, since it is not allowed to call from a different thread. So leave it uncommented:
79 //dbus_connection_set_dispatch_status_function
81 //add watch functions:
82 dbus_bool_t watch = dbus_connection_set_watch_functions(mDbusConnection, addWatch, removeWatch, toogleWatch, this, NULL);
85 logError("DBusWrapper::DBusWrapper Registering of watch functions failed");
88 //add timer functions:
89 dbus_bool_t timer = dbus_connection_set_timeout_functions(mDbusConnection, addTimeout, removeTimeout, toggleTimeout, this, NULL);
92 logError("DBusWrapper::DBusWrapper Registering of timer functions failed");
95 //register callback for Introspectio
96 mObjectPathVTable.message_function = DBusWrapper::cbRootIntrospection;
97 dbus_connection_register_object_path(mDbusConnection, DBUS_SERVICE_OBJECT_PATH, &mObjectPathVTable, this);
98 int ret = dbus_bus_request_name(mDbusConnection, DBUS_SERVICE_PREFIX, DBUS_NAME_FLAG_DO_NOT_QUEUE, &mDBusError);
99 if (dbus_error_is_set(&mDBusError))
101 logError("DBusWrapper::DBusWrapper Name Error",mDBusError.message);
102 dbus_error_free(&mDBusError);
104 if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret)
106 logError("DBusWrapper::DBusWrapper Wrapper is not the Primary Owner",ret);
111 DBusWrapper::~DBusWrapper()
113 //close the connection again
114 logInfo("DBusWrapper::~DBusWrapper Closing DBus connection");
115 dbus_connection_unref(mDbusConnection);
117 //clean up all timerhandles we created but did not delete before
118 std::vector<sh_timerHandle_t*>::iterator it = mListTimerhandlePointer.begin();
119 for (; it != mListTimerhandlePointer.end(); ++it)
125 void DBusWrapper::registerCallback(const DBusObjectPathVTable* vtable, const std::string& path, void* userdata)
127 logInfo("DBusWrapper::~registerCallback register callback:",path);
129 std::string completePath = std::string(DBUS_SERVICE_OBJECT_PATH) + "/" + path;
130 dbus_error_init(&mDBusError);
131 mDbusConnection = dbus_bus_get(DBUS_BUS_SESSION, &mDBusError);
132 dbus_connection_register_object_path(mDbusConnection, completePath.c_str(), vtable, userdata);
133 if (dbus_error_is_set(&mDBusError))
135 logError("DBusWrapper::registerCallack error: ",mDBusError.message);
136 dbus_error_free(&mDBusError);
138 mNodesList.push_back(path);
141 DBusHandlerResult DBusWrapper::cbRootIntrospection(DBusConnection *conn, DBusMessage *msg, void *reference)
143 logInfo("DBusWrapper::~cbRootIntrospection called:");
145 mReference = (DBusWrapper*) reference;
146 std::list<std::string> nodesList = mReference->mNodesList;
148 DBusMessageIter args;
149 dbus_uint32_t serial = 0;
150 if (dbus_message_is_method_call(msg, DBUS_INTERFACE_INTROSPECTABLE, "Introspect"))
152 std::list<std::string>::iterator nodeIter = nodesList.begin();
153 const char *xml = ROOT_INTROSPECT_XML;
154 std::stringstream introspect;
155 introspect << std::string(xml);
156 for (; nodeIter != nodesList.end(); ++nodeIter)
158 introspect << "<node name='" << nodeIter->c_str() << "'/>";
160 introspect << "</node>";
162 reply = dbus_message_new_method_return(msg);
163 std::string s = introspect.str();
164 const char* string = s.c_str();
166 // add the arguments to the reply
167 dbus_message_iter_init_append(reply, &args);
168 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &string))
170 logError("DBusWrapper::~cbRootIntrospection DBUS Out Of Memory!");
173 // send the reply && flush the connection
174 if (!dbus_connection_send(conn, reply, &serial))
176 logError("DBusWrapper::~cbRootIntrospection DBUS Out Of Memory!");
178 dbus_connection_flush(conn);
180 dbus_message_unref(reply);
182 return DBUS_HANDLER_RESULT_HANDLED;
186 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
190 void DBusWrapper::dbusMainLoop()
192 logInfo("DBusWrapper::dbusMainLoop Entering MainLoop");
194 while (dbus_connection_read_write_dispatch(mDbusConnection, -1))
200 void DBusWrapper::getDBusConnection(DBusConnection *& connection) const
202 connection = mDbusConnection;
205 dbus_bool_t DBusWrapper::addWatch(DBusWatch *watch, void *userData)
207 mReference = (DBusWrapper*) userData;
208 assert(mReference!=0);
209 return mReference->addWatchDelegate(watch, userData);
212 dbus_bool_t DBusWrapper::addWatchDelegate(DBusWatch * watch, void* userData)
216 sh_pollHandle_t handle = 0;
217 uint flags = dbus_watch_get_flags(watch);
219 /* no watch flags for disabled watches */
220 if (dbus_watch_get_enabled(watch))
222 if (flags & DBUS_WATCH_READABLE)
224 if (flags & DBUS_WATCH_WRITABLE)
228 logInfo("DBusWrapper::addWatchDelegate entered new watch, fd=",dbus_watch_get_unix_fd(watch),"event flag=",event);
229 am_Error_e error = mSocketHandler->addFDPoll(dbus_watch_get_unix_fd(watch), event, NULL, &pDbusFireCallback, &pDbusCheckCallback, &pDbusDispatchCallback, watch, handle);
231 //if everything is alright, add the watch and the handle to our map so we know this relationship
232 if (error == E_OK && handle != 0)
234 mMapHandleWatch.insert(std::make_pair(watch, handle));
237 logError("DBusWrapper::addWatchDelegate entering watch failed");
241 void DBusWrapper::removeWatch(DBusWatch *watch, void *userData)
243 mReference = (DBusWrapper*) userData;
244 assert(mReference!=0);
245 mReference->removeWatchDelegate(watch, userData);
248 void DBusWrapper::removeWatchDelegate(DBusWatch *watch, void *userData)
251 std::map<DBusWatch*, sh_pollHandle_t>::iterator iterator = mMapHandleWatch.begin();
252 iterator = mMapHandleWatch.find(watch);
253 if (iterator != mMapHandleWatch.end())
254 mSocketHandler->removeFDPoll(iterator->second);
255 logInfo("DBusWrapper::removeWatch removed watch with handle",iterator->second);
256 mMapHandleWatch.erase(iterator);
259 void DBusWrapper::toogleWatch(DBusWatch *watch, void *userData)
261 mReference = (DBusWrapper*) userData;
262 assert(mReference!=0);
263 mReference->toogleWatchDelegate(watch, userData);
266 void DBusWrapper::toogleWatchDelegate(DBusWatch *watch, void *userData)
270 dbus_watch_get_unix_fd(watch);
271 uint flags = dbus_watch_get_flags(watch);
272 /* no watch flags for disabled watches */
273 if (dbus_watch_get_enabled(watch))
275 if (flags & DBUS_WATCH_READABLE)
277 if (flags & DBUS_WATCH_WRITABLE)
280 std::map<DBusWatch*, sh_pollHandle_t>::iterator iterator = mMapHandleWatch.begin();
281 iterator = mMapHandleWatch.find(watch);
282 if (iterator != mMapHandleWatch.end())
283 mSocketHandler->updateEventFlags(iterator->second, event);
284 logInfo("DBusWrapper::toogleWatchDelegate watch was toggeled");
287 dbus_bool_t DBusWrapper::addTimeout(DBusTimeout *timeout, void* userData)
289 mReference = (DBusWrapper*) userData;
290 assert(mReference!=0);
291 return mReference->addTimeoutDelegate(timeout, userData);
294 dbus_bool_t DBusWrapper::addTimeoutDelegate(DBusTimeout *timeout, void* userData)
296 if (!dbus_timeout_get_enabled(timeout))
299 //calculate the timeout in timeval
300 timespec pollTimeout;
301 int localTimeout = dbus_timeout_get_interval(timeout);
302 pollTimeout.tv_sec = localTimeout / 1000;
303 pollTimeout.tv_nsec = (localTimeout % 1000) * 1000000;
305 //prepare handle and callback. new is eval, but there is no other choice because we need the pointer!
306 sh_timerHandle_t* handle = new sh_timerHandle_t;
307 mListTimerhandlePointer.push_back(handle);
308 shTimerCallBack* buffer = &pDbusTimerCallback;
310 //add the timer to the pollLoop
311 mSocketHandler->addTimer(pollTimeout, buffer, *handle, timeout);
313 //save the handle with dbus context
314 dbus_timeout_set_data(timeout, handle, NULL);
316 //save timeout in Socket context
318 logInfo("DBusWrapper::addTimeoutDelegate a timeout was added");
322 void DBusWrapper::removeTimeout(DBusTimeout *timeout, void* userData)
324 mReference = (DBusWrapper*) userData;
325 assert(mReference!=0);
326 mReference->removeTimeoutDelegate(timeout, userData);
329 void DBusWrapper::removeTimeoutDelegate(DBusTimeout *timeout, void* userData)
332 //get the pointer to the handle and remove the timer
333 sh_timerHandle_t* handle = (sh_timerHandle_t*) dbus_timeout_get_data(timeout);
334 mSocketHandler->removeTimer(*handle);
336 //now go throught the timerlist and remove the pointer, free memory
337 std::vector<sh_timerHandle_t*>::iterator it = mListTimerhandlePointer.begin();
338 for (; it != mListTimerhandlePointer.end(); ++it)
342 mListTimerhandlePointer.erase(it);
347 logInfo("DBusWrapper::removeTimeoutDelegate a timeout was removed");
350 void DBusWrapper::toggleTimeout(DBusTimeout *timeout, void* userData)
352 mReference = (DBusWrapper*) userData;
353 assert(mReference!=0);
354 mReference->toggleTimeoutDelegate(timeout, userData);
357 bool am::DBusWrapper::dbusDispatchCallback(const sh_pollHandle_t handle, void *userData)
361 bool returnVal = true;
362 dbus_connection_ref(mDbusConnection);
363 if (dbus_connection_dispatch(mDbusConnection) == DBUS_DISPATCH_COMPLETE)
365 dbus_connection_unref(mDbusConnection);
366 // logInfo("DBusWrapper::dbusDispatchCallback was called");
370 bool am::DBusWrapper::dbusCheckCallback(const sh_pollHandle_t handle, void *userData)
374 bool returnVal = false;
375 dbus_connection_ref(mDbusConnection);
376 if (dbus_connection_get_dispatch_status(mDbusConnection) == DBUS_DISPATCH_DATA_REMAINS)
378 dbus_connection_unref(mDbusConnection);
379 // logInfo("DBusWrapper::dbusCheckCallback was called");
383 void am::DBusWrapper::dbusFireCallback(const pollfd pollfd, const sh_pollHandle_t handle, void *userData)
387 assert(userData!=NULL);
390 if (pollfd.revents & POLLIN)
391 flags |= DBUS_WATCH_READABLE;
392 if (pollfd.revents & POLLOUT)
393 flags |= DBUS_WATCH_WRITABLE;
394 if (pollfd.revents & POLLHUP)
395 flags |= DBUS_WATCH_HANGUP;
396 if (pollfd.revents & POLLERR)
397 flags |= DBUS_WATCH_ERROR;
399 DBusWatch *watch = (DBusWatch*) userData;
401 dbus_connection_ref(mDbusConnection);
402 dbus_watch_handle(watch, flags);
403 dbus_connection_unref(mDbusConnection);
404 // logInfo("DBusWrapper::dbusFireCallback was called");
407 void DBusWrapper::toggleTimeoutDelegate(DBusTimeout *timeout, void* userData)
410 //get the pointer to the handle and remove the timer
411 sh_timerHandle_t* handle = (sh_timerHandle_t*) dbus_timeout_get_data(timeout);
414 if (dbus_timeout_get_enabled(timeout))
416 //calculate the timeout in timeval
417 timespec pollTimeout;
418 int localTimeout = dbus_timeout_get_interval(timeout);
419 pollTimeout.tv_sec = localTimeout / 1000;
420 pollTimeout.tv_nsec = (localTimeout % 1000) * 1000000;
421 mSocketHandler->restartTimer(*handle, pollTimeout);
425 mSocketHandler->stopTimer(*handle);
426 }logInfo("DBusWrapper::toggleTimeoutDelegate was called");
429 void DBusWrapper::dbusTimerCallback(sh_timerHandle_t handle, void *userData)
431 assert(userData!=NULL);
432 if (dbus_timeout_get_enabled((DBusTimeout*) userData))
437 mSocketHandler->restartTimer(handle, ts);
439 dbus_timeout_handle((DBusTimeout*) userData);
440 logInfo("DBusWrapper::dbusTimerCallback was called");