/** * @file settings_test.cpp * * @brief Settings daemon test. * * @author Ossama Othman @ * * @copyright @par * Copyright 2013 Intel Corporation All Rights Reserved. * @par * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; * version 2.1 of the License. * @par * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * @par * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301 USA * * @note This header is internal. */ #include "../lib/config.hpp" #include #include #include namespace { // -------------------------------------------------------------------- // libwebsockets callbacks and related data. // -------------------------------------------------------------------- int send_request(libwebsocket_context * /* context */, libwebsocket * wsi) { static char const request[] = // "{" // " \"type\": \"test_setting\"," // " \"transactionid\": \"fnord\"," // " \"name\": \"some_arbitrary_method\"," // " \"value\": \"some_arbitrary_value\"" // "}"; //"{" //" \"type\": \"ethernet\"," //" \"transactionid\": \"fnord\"," //" \"name\": \"is_enabled\"," //" \"value\": null" //"}"; "{" " \"type\": \"connman::service\"," " \"transactionid\": \"fnord\"," " \"name\": \"autoconnect\"," " \"value\": { \"path\": \"/net/connman/service/ethernet_eca86bf61ad2_cable\", \"enable\": true }" "}"; // "{" // " \"type\":\"wifi\"," // " \"transactionid\":\"3b5c9ebe-23fa-6b58-3f50-1203d7641441\"," // " \"name\":\"scan\"," // " \"value\":null" // "}"; // "{" // " \"type\":\"wifi\"," // " \"transactionid\":\"3b5c9ebe-23fa-6b58-3f50-1203d7641441\"," // " \"name\":\"connect\"," // " \"value\":{" // " \"ssid\":\"/net/connman/service/wifi_0c8bfd22b497_4775657374_managed_none\"," // " \"security\":null," // " \"passcode\":null}" // "}"; // "{" // " \"type\": \"clock\"," // " \"transactionid\": \"fnord\"," // " \"name\": \"time_updates\"," // " \"value\": \"auto\"" // "}"; // libwebsockets wants a sequence of octets (unsigned char *) rather // than characters. typedef std::vector vector_type; static vector_type::size_type const request_len = sizeof(request) - 1; // Don't include null terminator. vector_type::size_type const buf_len = LWS_SEND_BUFFER_PRE_PADDING + request_len + LWS_SEND_BUFFER_POST_PADDING; vector_type buf(buf_len); unsigned char * const payload = buf.data() + LWS_SEND_BUFFER_PRE_PADDING; // Copy the string into the buffer after libwebsockets pre-padding. std::copy(std::begin(request), std::end(request) - 1, // Don't include null terminator. payload); int const count = libwebsocket_write(wsi, payload, request_len, LWS_WRITE_TEXT); std::cout << "WROTE " << count << " bytes of " << request_len << "\n"; if (count < 0 || count < static_cast(request_len)) return -1; return 0; } int callback_settings_test(libwebsocket_context * context, libwebsocket * wsi, enum libwebsocket_callback_reasons reason, void * /* user */, void * in, size_t /* len */) { // For LWS_CALLBACK_*_POLL_FD cases. // int const fd = static_cast(reinterpret_cast(in)); // short const events = static_cast(len); switch(reason) { case LWS_CALLBACK_CLIENT_RECEIVE: // Response has come in from Settings daemon. std::cout << "Response: " << static_cast(in) << std::endl; break; case LWS_CALLBACK_CLIENT_ESTABLISHED: libwebsocket_callback_on_writable(context, wsi); break; case LWS_CALLBACK_CLIENT_WRITEABLE: return send_request(context, wsi); default: break; } return 0; } struct ws_session_data_type { }; libwebsocket_protocols settings_protocols[] = { { "http-only", callback_settings_test, sizeof(ws_session_data_type), 0, nullptr, 0 }, { nullptr, nullptr, 0, 0, nullptr, 0 } }; } // ---------------------------------------------------------------------- /** * settings_test program entry point. */ int main() { lws_context_creation_info info; info.port = 0; info.iface = "lo"; info.protocols = settings_protocols; info.extensions = libwebsocket_get_internal_extensions(); info.ssl_cert_filepath = nullptr; info.ssl_private_key_filepath = nullptr; info.ssl_ca_filepath = nullptr; info.ssl_cipher_list = nullptr; info.gid = -1; info.uid = -1; info.options = 0; info.user = nullptr; info.ka_time = 0; info.ka_probes = 0; info.ka_interval = 0; // lws_set_log_level(LLL_INFO, lwsl_emit_syslog); libwebsocket_context * const context = libwebsocket_create_context(&info); if (context == nullptr) exit(EXIT_FAILURE); static char const address[] = "localhost"; static int const port = IVI_SETTINGS_DEFAULT_WEBSOCKET_PORT; static int ssl_connection = 0; // Unencrypted Websocket connection. static char const path[] = "/"; static char const host[] = "localhost"; static char const origin[] = "localhost"; static char const protocol[] = "http-only"; static int const ietf_version = -1; // Connect using latest // supported protocol. libwebsocket * const wsi = libwebsocket_client_connect (context, address, port, ssl_connection, path, host, origin, protocol, ietf_version); if (wsi == nullptr) { std::cerr << "Unable to connect to settings daemon.\n"; libwebsocket_context_destroy(context); exit(EXIT_FAILURE); } int n = 0; while (n >= 0) n = libwebsocket_service(context, 10); libwebsocket_context_destroy(context); return 0; } // Local Variables: // mode:c++ // c-basic-offset:2 // indent-tabs-mode: nil // End: