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\": \"connman::service\","
63 " \"transactionid\": \"fnord\","
64 " \"name\": \"autoconnect\","
65 " \"value\": { \"path\": \"/net/connman/service/ethernet_eca86bf61ad2_cable\", \"enable\": true }"
68 // " \"type\":\"wifi\","
69 // " \"transactionid\":\"3b5c9ebe-23fa-6b58-3f50-1203d7641441\","
70 // " \"name\":\"scan\","
75 // " \"type\":\"wifi\","
76 // " \"transactionid\":\"3b5c9ebe-23fa-6b58-3f50-1203d7641441\","
77 // " \"name\":\"connect\","
79 // " \"ssid\":\"/net/connman/service/wifi_0c8bfd22b497_4775657374_managed_none\","
80 // " \"security\":null,"
81 // " \"passcode\":null}"
85 // " \"type\": \"clock\","
86 // " \"transactionid\": \"fnord\","
87 // " \"name\": \"time_updates\","
88 // " \"value\": \"auto\""
92 // libwebsockets wants a sequence of octets (unsigned char *) rather
94 typedef std::vector<unsigned char> vector_type;
96 static vector_type::size_type const request_len =
97 sizeof(request) - 1; // Don't include null terminator.
99 vector_type::size_type const buf_len =
100 LWS_SEND_BUFFER_PRE_PADDING
102 + LWS_SEND_BUFFER_POST_PADDING;
104 vector_type buf(buf_len);
105 unsigned char * const payload =
106 buf.data() + LWS_SEND_BUFFER_PRE_PADDING;
108 // Copy the string into the buffer after libwebsockets pre-padding.
109 std::copy(std::begin(request),
110 std::end(request) - 1, // Don't include null terminator.
113 int const count = libwebsocket_write(wsi,
118 std::cout << "WROTE " << count << " bytes of " << request_len << "\n";
120 if (count < 0 || count < static_cast<int>(request_len))
127 callback_settings_test(libwebsocket_context * context,
129 enum libwebsocket_callback_reasons reason,
134 // For LWS_CALLBACK_*_POLL_FD cases.
135 // int const fd = static_cast<int>(reinterpret_cast<intptr_t>(in));
136 // short const events = static_cast<short>(len);
139 case LWS_CALLBACK_CLIENT_RECEIVE:
140 // Response has come in from Settings daemon.
141 std::cout << "Response: " << static_cast<char const *>(in) << std::endl;
143 case LWS_CALLBACK_CLIENT_ESTABLISHED:
144 libwebsocket_callback_on_writable(context, wsi);
146 case LWS_CALLBACK_CLIENT_WRITEABLE:
147 return send_request(context, wsi);
155 struct ws_session_data_type
159 libwebsocket_protocols settings_protocols[] = {
162 callback_settings_test,
163 sizeof(ws_session_data_type),
179 // ----------------------------------------------------------------------
182 * settings_test program entry point.
186 lws_context_creation_info info;
191 info.protocols = settings_protocols;
192 info.extensions = libwebsocket_get_internal_extensions();
194 info.ssl_cert_filepath = nullptr;
195 info.ssl_private_key_filepath = nullptr;
196 info.ssl_ca_filepath = nullptr;
197 info.ssl_cipher_list = nullptr;
208 info.ka_interval = 0;
210 // lws_set_log_level(LLL_INFO, lwsl_emit_syslog);
212 libwebsocket_context * const context =
213 libwebsocket_create_context(&info);
214 if (context == nullptr)
217 static char const address[] = "localhost";
218 static int const port = IVI_SETTINGS_DEFAULT_WEBSOCKET_PORT;
219 static int ssl_connection = 0; // Unencrypted Websocket connection.
220 static char const path[] = "/";
221 static char const host[] = "localhost";
222 static char const origin[] = "localhost";
223 static char const protocol[] = "http-only";
224 static int const ietf_version = -1; // Connect using latest
225 // supported protocol.
227 libwebsocket * const wsi =
228 libwebsocket_client_connect (context,
230 port, ssl_connection,
237 if (wsi == nullptr) {
238 std::cerr << "Unable to connect to settings daemon.\n";
239 libwebsocket_context_destroy(context);
245 n = libwebsocket_service(context, 10);
247 libwebsocket_context_destroy(context);
255 // indent-tabs-mode: nil