c956cb68345d250815a29df73779513af23852a6
[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/unique_ptr.hpp>
31 #include <settingsd/response_callback.hpp>
32
33 #include <cstring>
34
35
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
40              connection)
41 {
42 }
43
44 void
45 ivi::settings::service::handle_request(char const * name,
46                                        JsonReader * reader,
47                                        connman_manager & manager,
48                                        response_callback & response)
49 {
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);
55     else {
56       response.send_error(
57         std::string("Unrecognized connman service request name: ")
58         + name);
59     }
60   }
61 }
62
63 void
64 ivi::settings::service::connect(JsonReader * reader,
65                                 connman_manager & manager,
66                                 response_callback response)
67 {
68   bool successful_parse = false;
69
70   typedef agent::connect_info_map_type map_type;
71   map_type info;
72
73   // The service connection information is found in the second array
74   // element.
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);
82
83         if (name != nullptr && value != nullptr)
84           info.insert(std::make_pair(name, value));
85       }
86       json_reader_end_element(reader);
87     }
88
89     // This cast is safe.
90     if (static_cast<gint>(info.size()) == count) {
91       successful_parse = true;
92     }
93   }
94   json_reader_end_element(reader);
95
96   if (!successful_parse) {
97     response.send_error("Unable to parse service connection information.");
98     return;
99   }
100
101   if (!manager.register_connect_data(connman_.object_path(),
102                                      std::move(info),
103                                      response)) {
104     response.send_error("Unable to register connection information.");
105     return;
106   }
107
108   call_method("Connect", response);
109 }
110
111 void
112 ivi::settings::service::disconnect(JsonReader * reader,
113                                    connman_manager & manager,
114                                    response_callback response)
115 {
116   bool null = false;
117   // The value is the second array element.
118   if (json_reader_read_element(reader, 1)) {
119     null = json_reader_get_null_value(reader);
120   }
121   json_reader_end_element(reader);
122
123   if (!null) {
124     response.send_error(
125       "connman::service::disconnect parameter is not null.");
126     return;
127   }
128
129   call_method("Disconnect", response);
130
131   manager.deregister_connect_data(connman_.object_path());
132 }
133
134 void
135 ivi::settings::service::call_method(char const * name,
136                                     response_callback response)
137 {
138   constexpr gint const timeout = 15000;  // milliseconds
139   GError * error = nullptr;
140
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,
146                            timeout,
147                            nullptr,  // Not cancellable
148                            &error));
149
150   unique_ptr<GError> safe_error(error);
151
152   if (ret != nullptr) {
153     // Nothing to add to successful response.
154     response.send_response(
155       [](JsonBuilder * /* builder */) {});
156
157   } else if (error != nullptr) {
158     response.send_error(std::string(name) + " failed: " + error->message);
159   }
160 }
161
162
163 // Local Variables:
164 // mode:c++
165 // c-basic-offset:2
166 // indent-tabs-mode: nil
167 // End: