* [GAM-35] implement DBus for SESSION or SYSTEM bus
[profile/ivi/audiomanager.git] / include / shared / CAmDbusWrapper.h
1 /**
2  *  Copyright (C) 2012, BMW AG
3  *
4  *  \author Christian Mueller, christian.ei.mueller@bmw.de BMW 2011,2012
5  *
6  *  \copyright
7  *  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction,
8  *  including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
9  *  subject to the following conditions:
10  *  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
11  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
12  *  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
13  *  THE USE OR OTHER DEALINGS IN THE SOFTWARE.
14  *
15  *  \file CAmDbusWrapper.h
16  *  For further information see http://www.genivi.org/.
17  */
18
19 /**
20  * todo add removeCallback ...
21  */
22
23 #ifndef DBUSWRAPPER_H_
24 #define DBUSWRAPPER_H_
25
26 #include <dbus/dbus.h>
27 #include <string>
28 #include <list>
29 #include <map>
30 #include "config.h"
31 #include "CAmSocketHandler.h"
32
33 namespace am
34 {
35
36 /**
37  * This wraps dbus and provides everything needed to anyone who wants to use dbus (including plugins). Works on the basis of CAmSocketHandler
38  */
39 class CAmDbusWrapper
40 {
41 public:
42     CAmDbusWrapper(CAmSocketHandler* socketHandler,DBusBusType type=DBUS_BUS_SESSION);
43     virtual ~CAmDbusWrapper();
44
45     void registerCallback(const DBusObjectPathVTable* vtable, const std::string& path, void* userdata);
46     void getDBusConnection(DBusConnection*& connection) const;
47
48     static dbus_bool_t addWatch(DBusWatch *watch, void *userData);
49     static void removeWatch(DBusWatch *watch, void *userData);
50     static void toogleWatch(DBusWatch *watch, void *userData);
51
52     static dbus_bool_t addTimeout(DBusTimeout *timeout, void* userData);
53     static void removeTimeout(DBusTimeout *timeout, void* userData);
54     static void toggleTimeout(DBusTimeout *timeout, void* userData);
55
56     bool dbusDispatchCallback(const sh_pollHandle_t handle, void* userData);
57     TAmShPollDispatch<CAmDbusWrapper> pDbusDispatchCallback;
58
59     void dbusFireCallback(const pollfd pollfd, const sh_pollHandle_t handle, void* userData);
60     TAmShPollFired<CAmDbusWrapper> pDbusFireCallback;
61
62     bool dbusCheckCallback(const sh_pollHandle_t handle, void* userData);
63     TAmShPollCheck<CAmDbusWrapper> pDbusCheckCallback;
64
65     void dbusTimerCallback(sh_timerHandle_t handle, void* userData);
66     TAmShTimerCallBack<CAmDbusWrapper> pDbusTimerCallback;
67
68 private:
69     static CAmDbusWrapper* mpReference; //!< reference to the dbus instance
70     static DBusHandlerResult cbRootIntrospection(DBusConnection *conn, DBusMessage *msg, void *reference);
71     dbus_bool_t addWatchDelegate(DBusWatch * watch, void* userData);
72     void removeWatchDelegate(DBusWatch *watch, void *userData);
73     void toogleWatchDelegate(DBusWatch *watch, void *userData);
74     dbus_bool_t addTimeoutDelegate(DBusTimeout *timeout, void* userData);
75     void removeTimeoutDelegate(DBusTimeout *timeout, void* userData);
76     void toggleTimeoutDelegate(DBusTimeout *timeout, void* userData);
77     DBusObjectPathVTable mObjectPathVTable; //!< the vpathtable
78     DBusConnection* mpDbusConnection; //!< pointer to the dbus connection used
79     DBusError mDBusError; //!< dbuserror
80     std::list<std::string> mListNodes; //!< holds a list of all nodes of the dbus
81     std::vector<sh_timerHandle_t*> mpListTimerhandles; //!< pointer to the timer handles
82     CAmSocketHandler *mpSocketHandler; //!< pointer to the sockethandler
83     std::map<DBusWatch*, sh_pollHandle_t> mMapHandleWatch; //!< map to the handle watches
84 };
85
86 }
87
88 #endif /* DBUSWRAPPER_H_ */