5c3714d9c191e2e435b5fb042e90c30e1f4732c0
[profile/ivi/settings-daemon.git] / plugins / connman / connman_service.cpp
1 /**
2  * @file connman_service.cpp
3  *
4  * @brief Connman Service operations.
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 "connman_service.hpp"
28 #include "service.hpp"
29
30 #include <settingsd/response_callback.hpp>
31 #include <settingsd/glib_traits.hpp>
32 #include <settingsd/unique_ptr.hpp>
33
34
35 #include <cstring>
36
37 // ----------------------------------------------------------------------
38
39 namespace
40 {
41   std::string const service_name("connman::service");
42 }
43
44 // ----------------------------------------------------------------------
45
46 ivi::settings::connman_service::connman_service(
47   GDBusConnection * connection,
48   connman_manager & manager)
49   : connection_(connection)
50   , manager_(manager)
51 {
52 }
53
54 ivi::settings::connman_service::~connman_service()
55 {
56 }
57
58 std::string const &
59 ivi::settings::connman_service::id() const
60 {
61   return service_name;
62 }
63
64 void
65 ivi::settings::connman_service::handle_request(
66   std::string request,
67   response_callback response)
68 {
69   unique_ptr<JsonParser> const parser(json_parser_new());
70   json_parser_load_from_data(parser.get(), request.c_str(), -1, nullptr);
71
72   unique_ptr<JsonReader> const safe_reader(
73     json_reader_new(json_parser_get_root(parser.get())));
74   JsonReader * const reader = safe_reader.get();
75
76   char const * name = nullptr;
77   if (json_reader_read_member(reader, "name"))
78     name = json_reader_get_string_value(reader);
79   else
80     response.send_error(
81       "Malformed " + id() + " request: missing 'name' element");
82
83   json_reader_end_member(reader);
84
85   if (name != nullptr) {
86     if (json_reader_read_member(reader, "value")) {
87       // The service object path is the first array element.
88       json_reader_read_element(reader, 0);
89       char const * const path = json_reader_get_string_value(reader);
90       json_reader_end_element(reader);
91
92       if (path != nullptr) {
93         service s(path, connection_);
94         s.handle_request(name, reader, manager_, response);
95       }
96     }
97     json_reader_end_member(reader);
98   } else {
99     response.send_error(
100       "Operation name for " + id() + " request is not a string.");
101   }
102 }
103
104
105 // Local Variables:
106 // mode:c++
107 // c-basic-offset:2
108 // indent-tabs-mode: nil
109 // End: