2 * @file websocket_server.cpp
4 * @brief Settings daemon WebSocket server source.
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
27 #include "websocket_server.hpp"
28 #include "configurator.hpp"
29 #include "../lib/config.hpp"
30 #include "../lib/manager.hpp"
38 // --------------------------------------------------------------------
39 // libwebsockets callbacks and related data.
40 // --------------------------------------------------------------------
43 callback_ivi_settings(libwebsocket_context * context,
45 enum libwebsocket_callback_reasons reason,
50 using namespace ivi::settings;
52 manager * const mgr = static_cast<manager *>(
53 libwebsocket_context_user(context));
56 case LWS_CALLBACK_ESTABLISHED:
57 // Enable event dispatching to client.
58 mgr->subscribe_client(wsi);
61 case LWS_CALLBACK_CLOSED:
62 // Disable event dispatching to client.
63 mgr->unsubscribe_client(wsi);
66 case LWS_CALLBACK_RECEIVE:
67 // Request has come in from Settings app. Pass it on to the
69 mgr->dispatch(static_cast<char const *>(in), wsi);
79 struct ws_session_data_type
83 libwebsocket_protocols settings_protocols[] = {
85 "http-only",//"ivi-settings-protocol",
86 callback_ivi_settings,
87 sizeof(ws_session_data_type),
104 // ----------------------------------------------------------------------
106 ivi::settings::websocket_server::websocket_server(
107 ivi::settings::configurator const & config,
108 ivi::settings::manager & mgr)
111 lws_context_creation_info info;
112 info.port = config.websocket_port();
113 info.iface = "lo"; // Only allow communication over the loopback
116 info.protocols = settings_protocols;
117 info.extensions = libwebsocket_get_internal_extensions();
119 info.ssl_cert_filepath = config.ssl_cert_file();
120 info.ssl_private_key_filepath = config.ssl_private_key_file();
121 info.ssl_ca_filepath = config.ssl_ca_file();
122 info.ssl_cipher_list = nullptr;
133 info.ka_interval = 0;
135 lws_set_log_level(LLL_INFO, lwsl_emit_syslog);
137 context_ = libwebsocket_create_context(&info);
138 if (context_ == nullptr)
139 throw std::runtime_error("Unable to initialize websocket.");
141 std::cout << "\n" PACKAGE_NAME " listening for requests on port "
142 << info.port << ".\n";
145 ivi::settings::websocket_server::~websocket_server()
147 libwebsocket_context_destroy(context_);
151 ivi::settings::websocket_server::run()
153 // Run the libwebsockets event loop with an infinite timeout. The
154 // negative timeout causes the underlying call poll() to block
155 // indefinitely until an event occurs.
156 constexpr int const timeout = -1;
158 while (libwebsocket_service(context_, timeout) == 0) {
166 // indent-tabs-mode: nil