Refactored connman signal callback to generic D-Bus signal callback.
[profile/ivi/settings-daemon.git] / plugins / connman / connman_manager.cpp
1 /**
2  * @file connman_manager.cpp
3  *
4  * @brief Connman Manager operations.
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_manager.hpp"
28
29 #include <settingsd/dbus_signal_callback.hpp>
30 #include <settingsd/glib_traits.hpp>
31 #include <settingsd/smart_ptr.hpp>
32
33 #include <cstring>
34
35
36 ivi::settings::connman_manager::connman_manager(
37   event_callback const & e)
38   : connman_("net.connman.Manager",     // Interface
39              "/",                       // Object path
40              e)
41   , event_callback_(e)
42   , subscription_id_(
43       g_dbus_connection_signal_subscribe(
44         g_dbus_proxy_get_connection(G_DBUS_PROXY(connman_.proxy())),
45         nullptr,
46         connman_.interface_name(),
47         "ServicesChanged",
48         connman_.object_path(),
49         nullptr,
50         G_DBUS_SIGNAL_FLAGS_NONE,
51         on_dbus_signal,
52         &event_callback_,
53         nullptr))
54 {
55   // The ServicesChanged signal parameters are:
56   //
57   //   array{object, dict}, array{object})
58   //
59   // where "object" is the D-Bus object path, and "dict" is a
60   // dictionary of object-specific properties.  The first parameter is
61   // list of changed services.  The second is a list of removed
62   // services.
63 }
64
65 ivi::settings::connman_manager::~connman_manager()
66 {
67   g_dbus_connection_signal_unsubscribe(
68     g_dbus_proxy_get_connection(G_DBUS_PROXY(connman_.proxy())),
69     subscription_id_);
70 }
71
72 GVariant *
73 ivi::settings::connman_manager::get_properties(
74   std::string const & technology,
75   GError *& error) const
76 {
77   constexpr gint const timeout = 5000;  // milliseconds
78
79   smart_ptr<GVariant> const dictionary(
80     g_dbus_proxy_call_sync(connman_.proxy(),
81                            "GetTechnologies",
82                            nullptr,  // No parameters
83                            G_DBUS_CALL_FLAGS_NONE,
84                            timeout,
85                            nullptr,  // Not cancellable
86                            &error));
87
88   if (dictionary != nullptr) {
89     GVariantIter * i = nullptr;
90     g_variant_get(dictionary.get(), "(a(oa{sv}))", &i);
91     smart_ptr<GVariantIter> const safe_i(i);
92
93     for (smart_ptr<GVariant> child(g_variant_iter_next_value(i));
94          child != nullptr;
95          child.reset(g_variant_iter_next_value(i))) {
96
97       // The object path is the first tuple element.
98       smart_ptr<GVariant> const tmp(
99         g_variant_get_child_value(child.get(), 0));
100
101       char const * object =
102         g_variant_get_string(tmp.get(), nullptr);
103
104       // The technology is found at end the object path,
105       // e.g. "/net/connman/technology/wifi" for the wifi.
106       char const * tech = strrchr(object, '/');
107
108       if (tech != nullptr) {
109         ++tech;  // Skip the found '/'.
110
111         if (technology == tech) {
112           // The property dictionary is the second tuple element.
113           return g_variant_get_child_value(child.get(), 1);
114         }
115       }
116     }
117   }
118
119   return nullptr;
120 }
121
122 GVariant *
123 ivi::settings::connman_manager::get_services(GError *& error) const
124 {
125   constexpr gint const timeout = 5000;  // milliseconds
126
127   return g_dbus_proxy_call_sync(connman_.proxy(),
128                                 "GetServices",
129                                 nullptr,  // No parameters
130                                 G_DBUS_CALL_FLAGS_NONE,
131                                 timeout,
132                                 nullptr,  // Not cancellable
133                                 &error);
134 }
135
136
137 // Local Variables:
138 // mode:c++
139 // c-basic-offset:2
140 // indent-tabs-mode: nil
141 // End: