2 * @file settings_test.cpp
4 * @brief Settings daemon test.
6 * @author Ossama Othman @<ossama.othman@@intel.com@>
9 * Copyright 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
26 * @note This header is internal.
29 #include "../lib/config.hpp"
31 #include <libwebsockets.h>
39 // --------------------------------------------------------------------
40 // libwebsockets callbacks and related data.
41 // --------------------------------------------------------------------
44 send_request(libwebsocket_context * /* context */, libwebsocket * wsi)
46 static char const request[] =
48 // " \"type\": \"test_setting\","
49 // " \"transactionid\": \"fnord\","
50 // " \"name\": \"some_arbitrary_method\","
51 // " \"value\": \"some_arbitrary_value\""
55 " \"type\": \"ethernet\","
56 " \"transactionid\": \"fnord\","
57 " \"name\": \"is_enabled\","
62 // " \"type\":\"wifi\","
63 // " \"transactionid\":\"3b5c9ebe-23fa-6b58-3f50-1203d7641441\","
64 // " \"name\":\"scan\","
69 // " \"type\":\"wifi\","
70 // " \"transactionid\":\"3b5c9ebe-23fa-6b58-3f50-1203d7641441\","
71 // " \"name\":\"connect\","
73 // " \"ssid\":\"/net/connman/service/wifi_0c8bfd22b497_4775657374_managed_none\","
74 // " \"security\":null,"
75 // " \"passcode\":null}"
79 // " \"type\": \"clock\","
80 // " \"transactionid\": \"fnord\","
81 // " \"name\": \"time_updates\","
82 // " \"value\": \"auto\""
86 // libwebsockets wants a sequence of octets (unsigned char *) rather
88 typedef std::vector<unsigned char> vector_type;
90 static vector_type::size_type const request_len =
91 sizeof(request) - 1; // Don't include null terminator.
93 vector_type::size_type const buf_len =
94 LWS_SEND_BUFFER_PRE_PADDING
96 + LWS_SEND_BUFFER_POST_PADDING;
98 vector_type buf(buf_len);
99 unsigned char * const payload =
100 buf.data() + LWS_SEND_BUFFER_PRE_PADDING;
102 // Copy the string into the buffer after libwebsockets pre-padding.
103 std::copy(std::begin(request),
104 std::end(request) - 1, // Don't include null terminator.
107 int const count = libwebsocket_write(wsi,
112 std::cout << "WROTE " << count << " bytes of " << request_len << "\n";
114 if (count < 0 || count < static_cast<int>(request_len))
121 callback_settings_test(libwebsocket_context * context,
123 enum libwebsocket_callback_reasons reason,
128 // For LWS_CALLBACK_*_POLL_FD cases.
129 // int const fd = static_cast<int>(reinterpret_cast<intptr_t>(in));
130 // short const events = static_cast<short>(len);
133 case LWS_CALLBACK_CLIENT_RECEIVE:
134 // Response has come in from Settings daemon.
135 std::cout << "Response: " << static_cast<char const *>(in) << std::endl;
137 case LWS_CALLBACK_CLIENT_ESTABLISHED:
138 libwebsocket_callback_on_writable(context, wsi);
140 case LWS_CALLBACK_CLIENT_WRITEABLE:
141 return send_request(context, wsi);
149 struct ws_session_data_type
153 libwebsocket_protocols settings_protocols[] = {
156 callback_settings_test,
157 sizeof(ws_session_data_type),
173 // ----------------------------------------------------------------------
176 * settings_test program entry point.
180 lws_context_creation_info info;
185 info.protocols = settings_protocols;
186 info.extensions = libwebsocket_get_internal_extensions();
188 info.ssl_cert_filepath = nullptr;
189 info.ssl_private_key_filepath = nullptr;
190 info.ssl_ca_filepath = nullptr;
191 info.ssl_cipher_list = nullptr;
202 info.ka_interval = 0;
204 // lws_set_log_level(LLL_INFO, lwsl_emit_syslog);
206 libwebsocket_context * const context =
207 libwebsocket_create_context(&info);
208 if (context == nullptr)
211 static char const address[] = "localhost";
212 static int const port = IVI_SETTINGS_DEFAULT_WEBSOCKET_PORT;
213 static int ssl_connection = 0; // Unencrypted Websocket connection.
214 static char const path[] = "/";
215 static char const host[] = "localhost";
216 static char const origin[] = "localhost";
217 static char const protocol[] = "http-only";
218 static int const ietf_version = -1; // Connect using latest
219 // supported protocol.
221 libwebsocket * const wsi =
222 libwebsocket_client_connect (context,
224 port, ssl_connection,
231 if (wsi == nullptr) {
232 std::cerr << "Unable to connect to settings daemon.\n";
233 libwebsocket_context_destroy(context);
239 n = libwebsocket_service(context, 10);
241 libwebsocket_context_destroy(context);
249 // indent-tabs-mode: nil