757e1d752b0aa4e2593252742dc12dccbc6477d0
[profile/ivi/settings-daemon.git] / plugins / connman / service.cpp
1 /**
2  * @file service.cpp
3  *
4  * @brief Connman service request handling.
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 "service.hpp"
28
29 #include <settingsd/glib_traits.hpp>
30 #include <settingsd/unique_ptr.hpp>
31 #include <settingsd/response_callback.hpp>
32
33 #include <chrono>
34
35
36 ivi::settings::service::service(std::string service_path,
37                                 GDBusConnection * connection,
38                                 event_callback const & e)
39   : connman_("net.connman.Service",     // Interface
40              service_path.c_str(),      // Object path
41              connection,
42              e)
43 {
44 }
45
46 void
47 ivi::settings::service::connect(response_callback response)
48 {
49   call_method("Connect", response);
50 }
51
52 void
53 ivi::settings::service::disconnect(response_callback response)
54 {
55   call_method("Disconnect", response);
56 }
57
58 void
59 ivi::settings::service::call_method(char const * name,
60                                     response_callback response)
61 {
62   constexpr gint const timeout = 10000;  // milliseconds
63   GError * error = nullptr;
64
65   unique_ptr<GVariant> const ret(
66     g_dbus_proxy_call_sync(connman_.proxy(),
67                            name,     // "Connect", "Disconect", ...
68                            nullptr,  // No parameters
69                            G_DBUS_CALL_FLAGS_NONE,
70                            timeout,
71                            nullptr,  // Not cancellable
72                            &error));
73
74   unique_ptr<GError> safe_error;
75
76   if (ret != nullptr) {
77     // Nothing to add to successful response.
78     response.send_response(
79       [](JsonBuilder * /* builder */) {});
80
81   } else if (error != nullptr) {
82     response.send_error(std::string(name) + " failed: " + error->message);
83   }
84 }
85
86
87 // Local Variables:
88 // mode:c++
89 // c-basic-offset:2
90 // indent-tabs-mode: nil
91 // End: