4 * @brief Connman service request handling.
6 * @author Ossama Othman @<ossama.othman@@intel.com@>
9 * Copyright 2013 Intel Corporation All Rights Reserved.
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.
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.
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
27 #include "service.hpp"
29 #include <settingsd/glib_traits.hpp>
30 #include <settingsd/unique_ptr.hpp>
31 #include <settingsd/response_callback.hpp>
36 ivi::settings::service::service(std::string service_path,
37 GDBusConnection * connection)
38 : connman_("net.connman.Service", // Interface
39 service_path.c_str(), // Object path
45 ivi::settings::service::handle_request(char const * name,
47 connman_manager & manager,
48 response_callback & response)
50 if (name != nullptr) {
51 if (strcmp(name, "connect") == 0)
52 connect(reader, manager, response);
53 else if (strcmp(name, "disconnect") == 0)
54 disconnect(reader, manager, response);
57 std::string("Unrecognized connman service request name: ")
64 ivi::settings::service::connect(JsonReader * reader,
65 connman_manager & manager,
66 response_callback response)
68 bool successful_parse = false;
70 typedef agent::connect_info_map_type map_type;
73 // The service connection information is found in the second array
75 if (json_reader_read_element(reader, 1)
76 && json_reader_is_object(reader)) {
77 gint const count = json_reader_count_members(reader);
78 for (gint i = 0; i < count; ++i) {
79 if (json_reader_read_element(reader, i)) {
80 char const * const name = json_reader_get_member_name(reader);
81 char const * const value = json_reader_get_string_value(reader);
83 if (name != nullptr && value != nullptr)
84 info.insert(std::make_pair(name, value));
86 json_reader_end_element(reader);
90 if (static_cast<gint>(info.size()) == count) {
91 successful_parse = true;
94 json_reader_end_element(reader);
96 if (!successful_parse) {
97 response.send_error("Unable to parse service connection information.");
101 if (!manager.register_connect_data(connman_.object_path(),
104 response.send_error("Unable to register connection information.");
108 call_method("Connect", response);
112 ivi::settings::service::disconnect(JsonReader * reader,
113 connman_manager & manager,
114 response_callback response)
117 // The value is the second array element.
118 if (json_reader_read_element(reader, 1)) {
119 null = json_reader_get_null_value(reader);
121 json_reader_end_element(reader);
125 "connman::service::disconnect parameter is not null.");
129 call_method("Disconnect", response);
131 manager.deregister_connect_data(connman_.object_path());
135 ivi::settings::service::call_method(char const * name,
136 response_callback response)
138 constexpr gint const timeout = 15000; // milliseconds
139 GError * error = nullptr;
141 unique_ptr<GVariant> const ret(
142 g_dbus_proxy_call_sync(connman_.proxy(),
143 name, // "Connect", "Disconect", ...
144 nullptr, // No parameters
145 G_DBUS_CALL_FLAGS_NONE,
147 nullptr, // Not cancellable
150 unique_ptr<GError> safe_error(error);
152 if (ret != nullptr) {
153 // Nothing to add to successful response.
154 response.send_response(
155 [](JsonBuilder * /* builder */) {});
157 } else if (error != nullptr) {
158 response.send_error(std::string(name) + " failed: " + error->message);
166 // indent-tabs-mode: nil