Implemented event reporting to settings daemon clients.
[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 #include "signal_callback.hpp"
29
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_connman_signal,
52         &event_callback_,
53         nullptr))
54 {
55 }
56
57 ivi::settings::connman_manager::~connman_manager()
58 {
59   g_dbus_connection_signal_unsubscribe(
60     g_dbus_proxy_get_connection(G_DBUS_PROXY(connman_.proxy())),
61     subscription_id_);
62 }
63
64 GVariant *
65 ivi::settings::connman_manager::get_properties(
66   std::string const & technology,
67   GError *& error) const
68 {
69   constexpr gint const timeout = 5000;  // milliseconds
70
71   smart_ptr<GVariant> const dictionary(
72     g_dbus_proxy_call_sync(connman_.proxy(),
73                            "GetTechnologies",
74                            nullptr,  // No parameters
75                            G_DBUS_CALL_FLAGS_NONE,
76                            timeout,
77                            nullptr,  // Not cancellable
78                            &error));
79
80   if (dictionary != nullptr) {
81     GVariantIter * i = nullptr;
82     g_variant_get(dictionary.get(), "(a(oa{sv}))", &i);
83     smart_ptr<GVariantIter> const safe_i(i);
84
85     for (smart_ptr<GVariant> child(g_variant_iter_next_value(i));
86          child != nullptr;
87          child.reset(g_variant_iter_next_value(i))) {
88
89       // The object path is the first tuple element.
90       smart_ptr<GVariant> const tmp(
91         g_variant_get_child_value(child.get(), 0));
92
93       char const * object =
94         g_variant_get_string(tmp.get(), nullptr);
95
96       // The technology is found at end the object path,
97       // e.g. "/net/connman/technology/wifi" for the wifi.
98       char const * tech = strrchr(object, '/');
99
100       if (tech != nullptr) {
101         ++tech;  // Skip the found '/'.
102
103         if (technology == tech) {
104           // The property dictionary is the second tuple element.
105           return g_variant_get_child_value(child.get(), 1);
106         }
107       }
108     }
109   }
110
111   return nullptr;
112 }
113
114 GVariant *
115 ivi::settings::connman_manager::get_services(GError *& error) const
116 {
117   constexpr gint const timeout = 5000;  // milliseconds
118
119   return g_dbus_proxy_call_sync(connman_.proxy(),
120                                 "GetServices",
121                                 nullptr,  // No parameters
122                                 G_DBUS_CALL_FLAGS_NONE,
123                                 timeout,
124                                 nullptr,  // Not cancellable
125                                 &error);
126 }
127
128
129 // Local Variables:
130 // mode:c++
131 // c-basic-offset:2
132 // indent-tabs-mode: nil
133 // End: