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::technology\","
63 // " \"transactionid\": \"3b5c9ebe-23fa-6b58-3f50-1203d7641441\","
64 // " \"name\": \"tethering\","
65 // " \"value\": { \"path\": \"/net/connman/technology/wifi\", \"ssid\" : \"ivi-poc\", \"password\": \"ivipoc-123\", \"enabled\": true }"
69 " \"type\": \"connman::service\","
70 " \"transactionid\": \"fnord\","
71 " \"name\": \"autoconnect\","
72 " \"value\": { \"path\": \"/net/connman/service/ethernet_eca86bf61ad2_cable\", \"enable\": true }"
75 // " \"type\":\"wifi\","
76 // " \"transactionid\":\"3b5c9ebe-23fa-6b58-3f50-1203d7641441\","
77 // " \"name\":\"scan\","
82 // " \"type\":\"wifi\","
83 // " \"transactionid\":\"3b5c9ebe-23fa-6b58-3f50-1203d7641441\","
84 // " \"name\":\"connect\","
86 // " \"ssid\":\"/net/connman/service/wifi_0c8bfd22b497_4775657374_managed_none\","
87 // " \"security\":null,"
88 // " \"passcode\":null}"
92 // " \"type\": \"clock\","
93 // " \"transactionid\": \"fnord\","
94 // " \"name\": \"time_updates\","
95 // " \"value\": \"auto\""
99 // libwebsockets wants a sequence of octets (unsigned char *) rather
101 typedef std::vector<unsigned char> vector_type;
103 static vector_type::size_type const request_len =
104 sizeof(request) - 1; // Don't include null terminator.
106 vector_type::size_type const buf_len =
107 LWS_SEND_BUFFER_PRE_PADDING
109 + LWS_SEND_BUFFER_POST_PADDING;
111 vector_type buf(buf_len);
112 unsigned char * const payload =
113 buf.data() + LWS_SEND_BUFFER_PRE_PADDING;
115 // Copy the string into the buffer after libwebsockets pre-padding.
116 std::copy(std::begin(request),
117 std::end(request) - 1, // Don't include null terminator.
120 int const count = libwebsocket_write(wsi,
125 std::cout << "WROTE " << count << " bytes of " << request_len << "\n";
127 if (count < 0 || count < static_cast<int>(request_len))
134 callback_settings_test(libwebsocket_context * context,
136 enum libwebsocket_callback_reasons reason,
141 // For LWS_CALLBACK_*_POLL_FD cases.
142 // int const fd = static_cast<int>(reinterpret_cast<intptr_t>(in));
143 // short const events = static_cast<short>(len);
146 case LWS_CALLBACK_CLIENT_RECEIVE:
147 // Response has come in from Settings daemon.
148 std::cout << "Response: " << static_cast<char const *>(in) << std::endl;
150 case LWS_CALLBACK_CLIENT_ESTABLISHED:
151 libwebsocket_callback_on_writable(context, wsi);
153 case LWS_CALLBACK_CLIENT_WRITEABLE:
154 return send_request(context, wsi);
162 struct ws_session_data_type
166 libwebsocket_protocols settings_protocols[] = {
169 callback_settings_test,
170 sizeof(ws_session_data_type),
186 // ----------------------------------------------------------------------
189 * settings_test program entry point.
193 lws_context_creation_info info;
198 info.protocols = settings_protocols;
199 info.extensions = libwebsocket_get_internal_extensions();
201 info.ssl_cert_filepath = nullptr;
202 info.ssl_private_key_filepath = nullptr;
203 info.ssl_ca_filepath = nullptr;
204 info.ssl_cipher_list = nullptr;
215 info.ka_interval = 0;
217 // lws_set_log_level(LLL_INFO, lwsl_emit_syslog);
219 libwebsocket_context * const context =
220 libwebsocket_create_context(&info);
221 if (context == nullptr)
224 static char const address[] = "localhost";
225 static int const port = IVI_SETTINGS_DEFAULT_WEBSOCKET_PORT;
226 static int ssl_connection = 0; // Unencrypted Websocket connection.
227 static char const path[] = "/";
228 static char const host[] = "localhost";
229 static char const origin[] = "localhost";
230 static char const protocol[] = "http-only";
231 static int const ietf_version = -1; // Connect using latest
232 // supported protocol.
234 libwebsocket * const wsi =
235 libwebsocket_client_connect (context,
237 port, ssl_connection,
244 if (wsi == nullptr) {
245 std::cerr << "Unable to connect to settings daemon.\n";
246 libwebsocket_context_destroy(context);
252 n = libwebsocket_service(context, 10);
254 libwebsocket_context_destroy(context);
262 // indent-tabs-mode: nil