707111b2ebe4075150fb2bda656c19b81b63034a
[profile/ivi/settings-daemon.git] / plugins / connman / connman.cpp
1 /**
2  * @file connman.cpp
3  *
4  * @brief Connman-based settings plugin.
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 #include "connman.hpp"
28
29 #include <settingsd/glib_traits.hpp>
30 #include <settingsd/unique_ptr.hpp>
31 #include <settingsd/dbus_signal_callback.hpp>
32
33 #include <cstring>
34 #include <string>
35 #include <stdexcept>
36
37
38 ivi::settings::connman::connman(char const * interface,
39                                 char const * path,
40                                 GDBusConnection * connection,
41                                 event_callback const & e)
42   : proxy_(nullptr)
43   , event_callback_(e)
44   , subscription_id_(0)
45 {
46   static char const name[] = "net.connman";  // Service
47
48   GError * error = nullptr;
49
50   proxy_ =
51     g_dbus_proxy_new_sync (connection,
52                            G_DBUS_PROXY_FLAGS_NONE,
53                            nullptr, // GDBusInterfaceInfo
54                            name,
55                            path,
56                            interface,
57                            nullptr, // GCancellable
58                            &error);
59
60   unique_ptr<GError> safe_error(error);
61
62   if (proxy_ == nullptr) {
63     g_printerr("Unable to create D-Bus proxy for (\"%s\", \"%s\"): %s\n",
64                interface,
65                path,
66                error->message);
67
68     throw std::runtime_error(error->message);
69   }
70
71   // Listen for changes to properties.
72   subscription_id_ =
73       g_dbus_connection_signal_subscribe(
74         connection,
75         nullptr,
76         interface,
77         "PropertyChanged",
78         path,
79         nullptr,
80         G_DBUS_SIGNAL_FLAGS_NONE,
81         on_dbus_signal,
82         &event_callback_,
83         nullptr);
84 }
85
86 ivi::settings::connman::~connman()
87 {
88   if (proxy_ != nullptr) {
89     g_dbus_connection_signal_unsubscribe(
90       g_dbus_proxy_get_connection(G_DBUS_PROXY(proxy_)),
91       subscription_id_);
92
93     g_object_unref(proxy_);
94   }
95 }
96
97 GVariant *
98 ivi::settings::connman::set_property(char const * property,
99                                      GVariant * value,
100                                      GError *& error)
101 {
102   static gint const timeout = 5000;  // milliseconds
103
104   return g_dbus_proxy_call_sync(proxy_,
105                                 "SetProperty",     // Method name
106                                 g_variant_new("(sv)",
107                                               property,
108                                               value),
109                                 G_DBUS_CALL_FLAGS_NONE,
110                                 timeout,
111                                 nullptr,  // Not cancellable
112                                 &error);
113 }
114
115
116 // Local Variables:
117 // mode:c++
118 // c-basic-offset:2
119 // indent-tabs-mode: nil
120 // End: