summaryrefslogtreecommitdiff
path: root/plugins/connman/technology.cpp
blob: e8e3d1416c8c2aed37d088c0f9a515c53c95bba8 (plain)
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
/**
 * @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 "connman_manager.hpp"

#include <settingsd/response_callback.hpp>
#include <settingsd/glib_traits.hpp>
#include <settingsd/json_glib_traits.hpp>
#include <settingsd/unique_ptr.hpp>

#include <cstring>


ivi::settings::technology::technology(char const * path,
                                      GDBusConnection * connection,
                                      connman_manager & manager)
  : connman_("net.connman.Technology",     // Interface
             path,                         // Object path
             connection)
  , manager_(manager)
{
}

void
ivi::settings::technology::handle_request(char const * name,
                                          JsonReader * reader,
                                          response_callback & response)
{
  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 {
      response.send_error(
        std::string("Unrecognized connman technology request name: ")
        + name);
    }
  }
}

void
ivi::settings::technology::get_powered(JsonReader * reader,
                                       response_callback & response)
{
  bool null = false;
  // The value is the second array element.
  if (json_reader_read_element(reader, 1)) {
    null = json_reader_get_null_value(reader);
  }
  json_reader_end_element(reader);

  if (!null) {
    response.send_error(
      "connman::technology is_enabled parameter is not null.");
    return;
  }

  unique_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;
  // The value is the second array element.
  if (json_reader_read_element(reader, 1)) {
    enable = json_reader_get_boolean_value(reader);
  }
  json_reader_end_element(reader);

  constexpr char const name[] = "Powered";

  GError * error = nullptr;

  unique_ptr<GVariant> ret(
    connman_.set_property(name,
                          g_variant_new_boolean(enable),
                          error));

  unique_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(
      std::string("Unable to set connman::technology powered state: ")
      + error->message);
  } else {
    response.send_error(
      "Malformed connman::technology enable request value.");
  }
}

void
ivi::settings::technology::scan(JsonReader * reader,
                                response_callback & response)
{
  bool null = false;
  // The value is the second array element.
  if (json_reader_read_element(reader, 1)) {
    null = json_reader_get_null_value(reader);
  }
  json_reader_end_element(reader);

  if (!null) {
    response.send_error(
      "connman::technology scan parameter is not null.");
    return;
  }

  // The scan could take a while.
  constexpr gint const timeout = 10000;  // milliseconds
  GError * error = nullptr;

  unique_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));

  unique_ptr<GError> safe_error(error);

  if (ret != nullptr) {
    manager_.get_services(response);
  } else if (error != nullptr) {
    response.send_error(
      std::string("Unable to scan connman::technology: ")
      + error->message);
  } else {
    response.send_error(
      "Malformed connman::technology scan request value.");
  }
}

GVariant *
ivi::settings::technology::get_property(char const * name,
                                        GVariantType const * type,
                                        response_callback & response)
{
  GError * error = nullptr;

  std::string tech(connman_.object_path());
  auto const n = tech.rfind('/');
  if (n == std::string::npos) {
    response.send_error(
      "Unable to determine Connman technology.  Malformed object path.");

    return nullptr;
  }

  tech = tech.substr(n);

  unique_ptr<GVariant> const properties(
    manager_.get_properties(tech.c_str(), error));

  unique_ptr<GError> safe_error(error);

  unique_ptr<GVariant> property;
  if (properties != nullptr) {
    property.reset(
      g_variant_lookup_value(properties.get(), name, type));

    if (property == nullptr) {
      response.send_error(
        "Internal " + tech
        + "error: \"" + name + "\" property lookup failed.");
    }
  } else if (error != nullptr) {
    response.send_error(
      "Unable to get " + tech + "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(
      "Connman technology properties are not available.");
  }

  return property.release();
}


// Local Variables:
// mode:c++
// c-basic-offset:2
// indent-tabs-mode: nil
// End: