4 * @brief Connman technology 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 "technology.hpp"
28 #include "connman_manager.hpp"
30 #include <settingsd/response_callback.hpp>
31 #include <settingsd/glib_traits.hpp>
32 #include <settingsd/json_glib_traits.hpp>
33 #include <settingsd/unique_ptr.hpp>
37 ivi::settings::technology::technology(char const * path,
38 GDBusConnection * connection,
39 connman_manager & manager)
40 : connman_("net.connman.Technology", // Interface
48 ivi::settings::technology::handle_request(char const * name,
50 response_callback & response)
52 if (name != nullptr) {
53 if (strcmp(name, "is_enabled") == 0)
54 get_powered(reader, response);
55 else if (strcmp(name, "enable") == 0)
56 set_powered(reader, response);
57 else if (strcmp(name, "scan") == 0)
58 scan(reader, response);
59 else if (strcmp(name, "tethering") == 0)
60 set_tethering(reader, response);
63 std::string("Unrecognized connman technology request name: ")
70 ivi::settings::technology::get_powered(JsonReader * reader,
71 response_callback & response)
74 // The value is the second array element.
75 if (json_reader_read_element(reader, 1)) {
76 null = json_reader_get_null_value(reader);
78 json_reader_end_element(reader);
82 "connman::technology is_enabled parameter is not null.");
86 unique_ptr<GVariant> const property(
87 get_property("Powered", G_VARIANT_TYPE_BOOLEAN, response));
89 if (property != nullptr) {
90 bool const powered = g_variant_get_boolean(property.get());
92 response.send_response(
93 [powered](JsonBuilder * builder)
95 json_builder_set_member_name(builder, "value");
96 json_builder_add_boolean_value(builder, powered);
100 // Error responses handled in get_property() method.
104 ivi::settings::technology::set_powered(JsonReader * reader,
105 response_callback & response)
108 // The value is the second array element.
109 if (json_reader_read_element(reader, 1)) {
110 enable = json_reader_get_boolean_value(reader);
112 json_reader_end_element(reader);
114 constexpr char const name[] = "Powered";
116 GError * error = nullptr;
118 unique_ptr<GVariant> ret(
119 connman_.set_property(name,
120 g_variant_new_boolean(enable),
123 unique_ptr<GError> safe_error(error);
125 if (ret != nullptr) {
126 // Nothing to add to successful response.
127 response.send_response(
128 [](JsonBuilder * /* builder */) {});
129 } else if (error != nullptr) {
131 std::string("Unable to set connman::technology powered state: ")
135 "Malformed connman::technology enable request value.");
140 ivi::settings::technology::set_tethering(JsonReader * reader,
141 response_callback & response)
143 char const * ssid = nullptr;
144 char const * password = nullptr;
145 bool enabled = false, have_password = true, have_ssid = true;
146 constexpr char const identifier[] = "TetheringIdentifier";
147 constexpr char const passphrase[] = "TetheringPassphrase";
148 constexpr char const tethering[] = "Tethering";
150 if (json_reader_read_member(reader, "enabled")) {
151 enabled = json_reader_get_boolean_value(reader);
152 json_reader_end_element(reader);
154 // In case that we are enabling tethering we need to setup also SSID and passphrase
155 // Otherwise those values are not required
157 have_ssid = json_reader_read_member(reader, "ssid");
159 ssid = json_reader_get_string_value(reader);
161 json_reader_end_element(reader);
163 have_password = json_reader_read_member(reader, "password");
165 password = json_reader_get_string_value(reader);
167 json_reader_end_element(reader);
170 if (have_ssid && have_password) {
171 GError * identifier_error = nullptr, *passphrase_error = nullptr, *enabled_error = nullptr;
172 bool identifier_res = true, passphrase_res = true, enabled_res;
174 enabled_res = this->set_property(tethering, g_variant_new_boolean(enabled), response, enabled_error);
176 if (enabled && enabled_res) {
177 passphrase_res = this->set_property(passphrase, g_variant_new_string(password), response, passphrase_error);
178 identifier_res = this->set_property(identifier, g_variant_new_string(ssid), response, identifier_error);
181 if (enabled_res && identifier_res && passphrase_res) {
182 response.send_response(
183 [enabled](JsonBuilder * builder)
185 json_builder_set_member_name(builder, "value");
186 json_builder_add_boolean_value(builder, enabled);
189 std::string failed_part;
193 error = enabled_error;
194 failed_part = std::string("status");
195 } else if (!identifier_res) {
196 error = identifier_error;
197 failed_part = std::string("identifier");
199 error = passphrase_error;
200 failed_part = std::string("passphrase");
203 if (error != nullptr) {
204 unique_ptr<GError> safe_error(error);
206 std::string("Unable to set connman::technology tethering ")
209 + enabled_error->message);
211 response.send_error(std::string("Malformed connman::technology tethering value ") + failed_part);
214 } else if (!have_ssid) {
215 response.send_error("connman::technology tether ssid parameter is null.");
217 response.send_error("connman::technology tether password parameter is null.");
220 response.send_error("connman::technology tether enabled parameter is null.");
225 ivi::settings::technology::set_property(char const * name,
227 response_callback /* response */,
230 unique_ptr<GVariant> const ret(
231 connman_.set_property(name, value, error));
233 return ret != nullptr;
237 ivi::settings::technology::scan(JsonReader * reader,
238 response_callback & response)
241 // The value is the second array element.
242 if (json_reader_read_element(reader, 1)) {
243 null = json_reader_get_null_value(reader);
245 json_reader_end_element(reader);
249 "connman::technology scan parameter is not null.");
253 // The scan could take a while.
254 constexpr gint const timeout = 10000; // milliseconds
255 GError * error = nullptr;
257 unique_ptr<GVariant> const ret(
258 g_dbus_proxy_call_sync(connman_.proxy(),
260 nullptr, // No parameters
261 G_DBUS_CALL_FLAGS_NONE,
263 nullptr, // Not cancellable
266 unique_ptr<GError> safe_error(error);
268 if (ret != nullptr) {
269 manager_.get_services(response);
270 } else if (error != nullptr) {
272 std::string("Unable to scan connman::technology: ")
276 "Malformed connman::technology scan request value.");
281 ivi::settings::technology::get_property(char const * name,
282 GVariantType const * type,
283 response_callback & response)
285 GError * error = nullptr;
287 std::string tech(connman_.object_path());
288 auto const n = tech.rfind('/');
289 if (n == std::string::npos) {
291 "Unable to determine Connman technology. Malformed object path.");
296 tech = tech.substr(n + 1);
298 unique_ptr<GVariant> const properties(
299 manager_.get_properties(tech.c_str(), error));
301 unique_ptr<GError> safe_error(error);
303 unique_ptr<GVariant> property;
304 if (properties != nullptr) {
306 g_variant_lookup_value(properties.get(), name, type));
308 if (property == nullptr) {
311 + "error: \"" + name + "\" property lookup failed.");
313 } else if (error != nullptr) {
315 "Unable to get " + tech + "properties: "
318 // This scenario will occur if the technology doesn't exist on
319 // the platform. For example, attempting to retrieve the wifi
320 // "Powered" property on a platform without supported hardware
321 // will result in this error.
323 "Connman technology properties are not available.");
326 return property.release();
333 // indent-tabs-mode: nil