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