[TIVI-2848] Fix tethering configuration.
[profile/ivi/settings-daemon.git] / lib / send_callback.cpp
1 /**
2  * @file send_callback.cpp
3  *
4  * @brief Core send functionality for responses and events.
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 <settingsd/send_callback.hpp>
28 #include <settingsd/glib_traits.hpp>
29 #include <settingsd/json_glib_traits.hpp>
30 #include <settingsd/unique_ptr.hpp>
31
32 #include <cstring>
33 #include <vector>
34 #include <algorithm>
35
36
37 ivi::settings::send_callback::send_callback(libwebsocket * wsi)
38   : wsi_(wsi)
39 {
40 }
41
42 ivi::settings::send_callback::send_callback(
43   send_callback const & other)
44   : wsi_(other.wsi_)
45 {
46 }
47
48 ivi::settings::send_callback &
49 ivi::settings::send_callback::operator=(send_callback const & other)
50 {
51   wsi_ = other.wsi_;
52
53   return *this;
54 }
55
56 bool
57 ivi::settings::send_callback::send_payload(
58   char const * send_type,
59   unique_ptr<JsonBuilder> const & builder) const
60 {
61   unique_ptr<JsonGenerator> const generator(json_generator_new());
62   unique_ptr<JsonNode> const root(json_builder_get_root(builder.get()));
63   json_generator_set_root(generator.get(), root.get());
64
65   gchar * const data =
66     json_generator_to_data(generator.get(), nullptr);
67
68   unique_ptr<gchar> safe_data(data);
69
70   if (data == nullptr) {
71     g_critical("Unable to generate JSON formatted %s payload:\n",
72                send_type);
73
74     return false;
75   }
76
77   g_debug("Sending %s: %s\n", send_type, data);
78
79   size_t const payload_len = strlen(data);
80
81   // libwebsockets wants a sequence of octets (unsigned char *) rather
82   // than characters.
83   typedef std::vector<unsigned char> vector_type;
84
85   vector_type::size_type const buf_len =
86     LWS_SEND_BUFFER_PRE_PADDING
87     + payload_len
88     + LWS_SEND_BUFFER_POST_PADDING;
89
90   vector_type buf(buf_len);
91   unsigned char * const payload =
92     buf.data() + LWS_SEND_BUFFER_PRE_PADDING;
93
94   // Copy the string into the buffer after libwebsockets pre-padding.
95   std::copy(data,
96             data + payload_len,
97             payload);
98
99   int const count = libwebsocket_write(wsi_,
100                                        payload,
101                                        payload_len,
102                                        LWS_WRITE_TEXT);
103
104   if (count < 0)
105     g_critical("Unable to send the following data to client: %s\n",
106                data);
107
108   return count >= 0;
109 }
110
111
112 // Local Variables:
113 // mode:c++
114 // c-basic-offset:2
115 // indent-tabs-mode: nil
116 // End: