2 * @file response_callback.cpp
4 * @brief Settings plugin response_callback implementation.
6 * @author Ossama Othman @<ossama.othman@@intel.com@>
9 * Copyright 2012, 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 <settingsd/response_callback.hpp>
28 #include <settingsd/glib_traits.hpp>
29 #include <settingsd/json_glib_traits.hpp>
30 #include <settingsd/smart_ptr.hpp>
37 ivi::settings::response_callback::response_callback(
38 struct libwebsocket * wsi,
40 std::string transaction_id)
43 , transaction_id_(transaction_id)
47 ivi::settings::response_callback::~response_callback()
52 ivi::settings::response_callback::send_response(
53 std::function<void(JsonBuilder *)> response_builder)
55 smart_ptr<JsonBuilder> const builder = begin_response("succeeded");
57 // Append successful Settings app results to the JSON formatted
59 response_builder(builder.get());
61 end_response(builder);
63 return send_payload(builder);
67 ivi::settings::response_callback::send_error(std::string error_message)
69 smart_ptr<JsonBuilder> builder = begin_response("failed");
71 json_builder_set_member_name(builder.get(), "reason");;
72 json_builder_add_string_value(builder.get(), error_message.c_str());
74 end_response(builder);
76 return send_payload(builder);
80 ivi::settings::response_callback::add_string_value(
81 JsonBuilder * builder,
82 std::string const & name,
83 std::string const & value)
85 json_builder_set_member_name(builder, name.c_str());
87 json_builder_add_null_value(builder);
89 json_builder_add_string_value(builder, value.c_str());
92 ivi::settings::smart_ptr<JsonBuilder>
93 ivi::settings::response_callback::begin_response(char const * result)
95 // Construct JSON response.
96 smart_ptr<JsonBuilder> safe_builder(json_builder_new());
97 JsonBuilder * const builder = safe_builder.get();
99 json_builder_begin_object(builder);
101 add_string_value(builder, "type", type_);
102 add_string_value(builder, "transactionid", transaction_id_);
104 json_builder_set_member_name(builder, "result");
105 json_builder_add_string_value(builder, result);
111 ivi::settings::response_callback::end_response(
112 ivi::settings::smart_ptr<JsonBuilder> const & builder)
114 json_builder_end_object(builder.get());
118 ivi::settings::response_callback::send_payload(
119 ivi::settings::smart_ptr<JsonBuilder> const & builder)
121 smart_ptr<JsonGenerator> const generator(json_generator_new());
122 smart_ptr<JsonNode> const root(json_builder_get_root(builder.get()));
123 json_generator_set_root(generator.get(), root.get());
125 gchar * const response =
126 json_generator_to_data(generator.get(), nullptr);
128 smart_ptr<gchar> safe_response(response);
130 if (response == nullptr) {
131 g_critical("Unable to generate JSON response for:\n"
133 " transactionid: %s\n",
135 transaction_id_.c_str());
140 g_debug("Sending response: %s\n", response);
142 size_t const payload_len = strlen(response);
144 // libwebsockets wants a sequence of octets (unsigned char *) rather
146 typedef std::vector<unsigned char> vector_type;
148 vector_type::size_type const buf_len =
149 LWS_SEND_BUFFER_PRE_PADDING
151 + LWS_SEND_BUFFER_POST_PADDING;
153 vector_type buf(buf_len);
154 unsigned char * const payload =
155 buf.data() + LWS_SEND_BUFFER_PRE_PADDING;
157 // Copy the string into the buffer after libwebsockets pre-padding.
159 response + payload_len,
162 int const count = libwebsocket_write(wsi_,
175 // indent-tabs-mode: nil