2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 * @author Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
20 #ifndef WRTDEVICEAPIS_DBUS_CONNECTION_H_
21 #define WRTDEVICEAPIS_DBUS_CONNECTION_H_
26 #include <dbus/dbus.h>
27 #include <dpl/shared_ptr.h>
28 #include <dpl/generic_event.h>
29 #include <dpl/event/event_support.h>
30 #include <dpl/event/event_listener.h>
31 #include <dpl/waitable_event.h>
32 #include <dpl/thread.h>
33 #include <dpl/noncopyable.h>
34 #include <dpl/event/controller.h>
35 #include <dpl/type_list.h>
36 #include <dpl/mutex.h>
37 #include "MessageEvent.h"
39 namespace WrtDeviceApis {
41 class Connection : public DPL::Event::EventSupport<MessageEvent>
44 typedef DPL::Event::EventListener<MessageEvent> EventListener;
50 void open(DBusBusType bus);
52 void addFilter(const std::string& rule);
53 void removeFilter(const std::string& rule);
54 void setWorkerThread(DPL::Thread* thread);
57 DECLARE_GENERIC_EVENT_2(AddDescriptorEvent,
61 DECLARE_GENERIC_EVENT_2(RemoveDescriptorEvent,
65 class MessageDispatcher :
66 public DPL::Event::Controller<DPL::TypeListDecl<
68 RemoveDescriptorEvent>::Type>,
69 private DPL::WaitableHandleWatchSupport::WaitableHandleListener
72 explicit MessageDispatcher(Connection* connection);
74 void addDescriptor(DPL::WaitableHandle handle,
75 DPL::WaitMode::Type mode);
76 void removeDescriptor(DPL::WaitableHandle handle,
77 DPL::WaitMode::Type mode);
80 void OnEventReceived(const AddDescriptorEvent& event);
81 void OnEventReceived(const RemoveDescriptorEvent& event);
82 void OnWaitableHandleEvent(DPL::WaitableHandle waitableHandle,
83 DPL::WaitMode::Type mode);
86 Connection* m_connection;
89 class Watch : private DPL::Noncopyable
92 explicit Watch(DBusWatch* watch);
94 inline DPL::WaitableHandle getHandle() const;
95 inline DPL::WaitMode::Type getType() const;
96 inline bool isEnabled() const;
97 inline DBusWatch* get() const;
102 typedef DPL::SharedPtr<Watch> WatchPtr;
104 typedef std::map<DPL::WaitableHandle, WatchPtr> WatchSet;
105 typedef WatchSet::iterator WatchSetIterator;
107 typedef std::set<std::string> FilterSet;
108 typedef FilterSet::iterator FilterSetIterator;
111 static dbus_bool_t addWatch(DBusWatch* watch,
113 static void removeWatch(DBusWatch* watch,
115 static void toggleWatch(DBusWatch* watch,
117 static DBusHandlerResult filterMessage(DBusConnection* connection,
118 DBusMessage* message,
122 void addFilterInternal(const std::string& rule);
123 void removeFilterInternal(const std::string& rule);
126 DBusConnection* m_connection;
129 MessageDispatcher* m_messageDispatcher;
133 DPL::Mutex m_watchesMutex;
136 typedef DPL::SharedPtr<Connection> ConnectionPtr;
140 #endif // WRTDEVICEAPIS_DBUS_CONNECTION_H_