TIVI-1924: Initial commit of IVI settings daemon.
[profile/ivi/settings-daemon.git] / plugins / connman / connman.hpp
1 /**
2  * @file connman.hpp
3  *
4  * @brief Connman-based settings plugin header.
5  *
6  * @author Ossama Othman @<ossama.othman@@intel.com@>
7  *
8  * @copyright @par
9  * Copyright 2013 Intel Corporation All Rights Reserved.
10  * @par
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation;
14  * version 2.1 of the License.
15  * @par
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  * @par
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this library; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
24  * Boston, MA  02110-1301  USA
25  */
26
27
28 #ifndef IVI_SETTINGS_CONNMAN_HPP
29 #define IVI_SETTINGS_CONNMAN_HPP
30
31 #include <settingsd/glib_traits.hpp>
32 #include <settingsd/smart_ptr.hpp>
33
34 #include <gio/gio.h>
35
36 #include <future>
37 #include <utility>
38 #include <list>
39 #include <memory>
40
41
42 namespace ivi
43 {
44   namespace settings
45   {
46     /**
47      * @class connman
48      *
49      * @brief Common connman-based settings functionality.
50      *
51      * This class implements functionality common to all connman-based
52      * settings, such as bluetooth, wifi, date/time, etc.
53      */
54     class connman
55     {
56     public:
57
58       /**
59        * Constructor.
60        *
61        * @param[in] interface Connman D-Bus interface.
62        * @param[in] path      Connman D-Bus object path.
63        */
64       connman(char const * interface,
65               char const * path);
66
67       /// Destructor.
68       ~connman();
69
70       /// The type held by the @c future containing the async result.
71       typedef smart_ptr<GVariant> future_value_type;
72
73       /// The @c future type returned by the @c promise.
74       typedef std::future<future_value_type>  future_type;
75
76       /// The @c promise that provides the async result.
77       typedef std::promise<future_value_type> promise_type;
78
79       /**
80        * Smart pointer to the @c promise that provides the async
81        * result.
82        */
83       typedef std::shared_ptr<promise_type> shared_promise_type;
84
85       /**
86        * Property Name/value pair type held by the @c future
87        * containing the async result.  Both values will be populated
88        * when retrieving the value from the @c PropertyChanged
89        * signal.
90        *
91        * @note The property name is meant for internal use.
92        */
93       typedef std::pair<char const *,
94                         shared_promise_type> promise_value_type;
95
96       /// List type for promises to be updated with async result.
97       typedef std::list<promise_value_type> promise_list_type;
98
99       /**
100        * Set @a property to given @a value on the underlying connman
101        * object.
102        *
103        * @param[in] property The name of the property to be set.
104        * @param[in] value    The property value to be set.
105        * @param[inout] error The error object containing information
106        *                     on a failed connman @c SetProperty method
107        *                     call.
108        *
109        * @returns @c nullptr on a failed call to the connman
110        *          @c SetProperty method.
111        */
112       GVariant * set_property(char const * property,
113                               GVariant * value,
114                               GError *& error);
115
116       /**
117        * Get a promise that will contain the changed connman property
118        * when it becomes available.  The user must obtain the value
119        * from the corresponding future object.
120        */
121       shared_promise_type get_property_changed_promise(
122         char const * property);
123
124       /// Get pointer to underlying GDBusProxy.
125       GDBusProxy * proxy() const { return proxy_; }
126
127       /// Convenience function to get D-Bus interface name.
128       char const *
129       interface_name() const
130       {
131         return g_dbus_proxy_get_interface_name(proxy_);
132       }
133
134       /// Convenience function to get D-Bus object path.
135       char const *
136       object_path() const
137       {
138         return g_dbus_proxy_get_object_path(proxy_);
139       }
140
141       /**
142        * @struct user_data
143        *
144        * @brief Struct passed to @c ServicesChanged signal handler.
145        */
146       struct user_data
147       {
148         /// Constructor.
149         user_data(std::mutex & m, promise_list_type & p)
150           : mutex(m)
151           , promises(p)
152         {
153         }
154
155         /**
156          * References to mutex used to synchronize access to the list
157          * of promises.
158          */
159         std::mutex & mutex;
160
161         /// List of promises to be updated with the changed services.
162         promise_list_type & promises;
163       };
164
165     private:
166
167       /// The proxy used to access the Connman D-Bus API.
168       GDBusProxy * proxy_;
169
170       /// Mutex used to synchronize access to the promises list.
171       std::mutex mutex_;
172
173       /**
174        * List of promises that will be updated with the
175        * PropertyChanged signal results.
176        */
177       promise_list_type promises_;
178
179       /// User data passed to @c PropertyChanged signal handler.
180       user_data data_;
181
182       /// PropertyChanged signal subscription ID.
183       guint subscription_id_;
184
185     };
186
187   }
188 }
189
190
191 #endif  /* IVI_SETTINGS_CONNMAN_HPP */
192
193
194 // Local Variables:
195 // mode:c++
196 // c-basic-offset:2
197 // indent-tabs-mode: nil
198 // End: