1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
/**
* @file technology.cpp
*
* @brief Connman technology request handling.
*
* @author Ossama Othman @<ossama.othman@@intel.com@>
*
* @copyright @par
* Copyright 2013 Intel Corporation All Rights Reserved.
* @par
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License.
* @par
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* @par
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
#include "technology.hpp"
#include "service.hpp"
#include <settingsd/response_callback.hpp>
#include <settingsd/glib_traits.hpp>
#include <settingsd/json_glib_traits.hpp>
#include <settingsd/smart_ptr.hpp>
#include <cstring>
#include <chrono>
ivi::settings::technology::technology(std::string tech,
event_callback const & e)
: connman_("net.connman.Technology", // Interface
("/net/connman/technology/"
+ tech).c_str(), // Object path
e)
, manager_(e)
, technology_(tech)
, event_callback_(e)
{
}
void
ivi::settings::technology::handle_request(std::string request,
response_callback response)
{
smart_ptr<JsonParser> const parser(json_parser_new());
json_parser_load_from_data(parser.get(), request.c_str(), -1, nullptr);
smart_ptr<JsonReader> const safe_reader(
json_reader_new(json_parser_get_root(parser.get())));
JsonReader * const reader = safe_reader.get();
char const * name = nullptr;
if (json_reader_read_member(reader, "name"))
name = json_reader_get_string_value(reader);
else
response.send_error(
"Malformed " + technology_ + " request: missing 'name' element");
json_reader_end_member(reader);
if (name != nullptr) {
if (strcmp(name, "is_enabled") == 0)
get_powered(reader, response);
else if (strcmp(name, "enable") == 0)
set_powered(reader, response);
else if (strcmp(name, "scan") == 0)
scan(reader, response);
else if (strcmp(name, "connect") == 0)
connect(reader, response);
else if (strcmp(name, "disconnect") == 0)
disconnect(reader, response);
else {
response.send_error(
"Unrecognized " + technology_ + " request name: "
+ name);
}
} else {
response.send_error(
"Operation name for " + technology_ + " request is not a string.");
}
}
void
ivi::settings::technology::get_powered(JsonReader * reader,
response_callback response)
{
bool null = false;
if (json_reader_read_member(reader, "value")) {
null = json_reader_get_null_value(reader);
}
json_reader_end_member(reader);
if (!null) {
response.send_error(
technology_ + " is_enabled parameter is not null.");
return;
}
smart_ptr<GVariant> const property(
get_property("Powered", G_VARIANT_TYPE_BOOLEAN, response));
if (property != nullptr) {
bool const powered = g_variant_get_boolean(property.get());
response.send_response(
[powered](JsonBuilder * builder)
{
json_builder_set_member_name(builder, "value");
json_builder_add_boolean_value(builder, powered);
});
}
// Error responses handled in get_property() method.
}
void
ivi::settings::technology::set_powered(JsonReader * reader,
response_callback response)
{
bool enable = false;
if (json_reader_read_member(reader, "value")) {
enable = json_reader_get_boolean_value(reader);
}
json_reader_end_member(reader);
constexpr char const name[] = "Powered";
GError * error = nullptr;
smart_ptr<GVariant> ret(
connman_.set_property(name,
g_variant_new_boolean(enable),
error));
smart_ptr<GError> safe_error(error);
if (ret != nullptr) {
// Nothing to add to successful response.
response.send_response(
[](JsonBuilder * /* builder */) {});
} else if (error != nullptr) {
response.send_error(
"Unable to set " + technology_ + " powered state: "
+ error->message);
} else {
response.send_error(
"Malformed " + technology_ + " enable request value.");
}
}
void
ivi::settings::technology::scan(JsonReader * reader,
response_callback response)
{
bool null = false;
if (json_reader_read_member(reader, "value")) {
null = json_reader_get_null_value(reader);
}
json_reader_end_member(reader);
if (!null) {
response.send_error(technology_ + " scan parameter is not null.");
return;
}
// The scan could take a while.
constexpr gint const timeout = 10000; // milliseconds
GError * error = nullptr;
smart_ptr<GVariant> const ret(
g_dbus_proxy_call_sync(connman_.proxy(),
"Scan",
nullptr, // No parameters
G_DBUS_CALL_FLAGS_NONE,
timeout,
nullptr, // Not cancellable
&error));
smart_ptr<GError> safe_error(error);
if (ret != nullptr) {
send_services(response, error);
} else if (error != nullptr) {
response.send_error(
"Unable to scan " + technology_ + ": "
+ error->message);
} else {
response.send_error(
"Malformed " + technology_ + " scan request value.");
}
}
void
ivi::settings::technology::connect(JsonReader * reader,
response_callback response)
{
char const * service_path = nullptr;
if (json_reader_read_member(reader, "value")) {
service_path = json_reader_get_string_value(reader);
}
json_reader_end_member(reader);
/// @todo Refactor malformed JSON request handling code.
if (service_path != nullptr) {
service s(service_path, event_callback_);
s.connect(response);
} else {
response.send_error(
"Malformed " + technology_ + " connect request value.");
}
}
void
ivi::settings::technology::disconnect(JsonReader * reader,
response_callback response)
{
char const * service_path = nullptr;
if (json_reader_read_member(reader, "value")) {
service_path = json_reader_get_string_value(reader);
}
json_reader_end_member(reader);
if (service_path != nullptr) {
service s(service_path, event_callback_);
s.disconnect(response);
} else {
response.send_error(
"Malformed " + technology_ + " disconnect request value.");
}
}
void
ivi::settings::technology::send_services(response_callback response,
GError *& error)
{
smart_ptr<GVariant> const services(manager_.get_services(error));
if (services != nullptr) {
response.send_response(
[&services](JsonBuilder * builder)
{
/**
* @todo Can @c json_gvariant_serialize() ever return a nullptr?
*/
JsonNode * const value =
json_gvariant_serialize(services.get());
json_builder_set_member_name(builder, "value");
json_builder_add_value(builder, value);
// No need to free the JsonNode. The builder will take
// ownership of it.
});
}
}
GVariant *
ivi::settings::technology::get_property(char const * name,
GVariantType const * type,
response_callback response)
{
GError * error = nullptr;
smart_ptr<GVariant> const properties(
manager_.get_properties(technology_, error));
smart_ptr<GError> safe_error(error);
smart_ptr<GVariant> property;
if (properties != nullptr) {
property.reset(
g_variant_lookup_value(properties.get(), name, type));
if (property == nullptr) {
response.send_error(
"Internal " + technology_
+ "error: \"" + name + "\" property lookup failed.");
}
} else if (error != nullptr) {
response.send_error(
"Unable to get " + technology_ + "properties: "
+ error->message);
} else {
// This scenario will occur if the technology doesn't exist on
// the platform. For example, attempting to retrieve the wifi
// "Powered" property on a platform without supported hardware
// will result in this error.
response.send_error(technology_ + " properties are not available.");
}
return property.release();
}
// Local Variables:
// mode:c++
// c-basic-offset:2
// indent-tabs-mode: nil
// End:
|