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_RECEIVE:
57 // Request has come in from Settings app. Pass it on to the
59 mgr->dispatch(static_cast<char const *>(in), wsi);
69 struct ws_session_data_type
73 libwebsocket_protocols settings_protocols[] = {
75 "http-only",//"ivi-settings-protocol",
76 callback_ivi_settings,
77 sizeof(ws_session_data_type),
94 // ----------------------------------------------------------------------
96 ivi::settings::websocket_server::websocket_server(
97 ivi::settings::configurator const & config,
98 ivi::settings::manager & mgr)
101 lws_context_creation_info info;
102 info.port = config.websocket_port();
103 info.iface = "lo"; // Only allow communication over the loopback
106 info.protocols = settings_protocols;
107 info.extensions = libwebsocket_get_internal_extensions();
109 info.ssl_cert_filepath = config.ssl_cert_file();
110 info.ssl_private_key_filepath = config.ssl_private_key_file();
111 info.ssl_ca_filepath = config.ssl_ca_file();
112 info.ssl_cipher_list = nullptr;
123 info.ka_interval = 0;
125 lws_set_log_level(LLL_INFO, lwsl_emit_syslog);
127 context_ = libwebsocket_create_context(&info);
128 if (context_ == nullptr)
129 throw std::runtime_error("Unable to initialize websocket.\n");
131 std::cout << "\n" PACKAGE_NAME " listening for requests on port "
132 << info.port << ".\n";
135 ivi::settings::websocket_server::~websocket_server()
137 libwebsocket_context_destroy(context_);
141 ivi::settings::websocket_server::run()
143 // Run the websocket event loop in its own thread.
145 [](libwebsocket_context * context){
147 // Run the libwebsockets event loop with an infinite timeout.
148 // The negative timeout causes the underlying call poll() to
149 // block indefinitely until an event occurs.
150 constexpr int const timeout = -1;
151 while (libwebsocket_service(context, timeout) >= 0) {
154 std::cerr << "WebSocket event loop ended.\n";
155 } catch(std::exception & e) {
156 std::cerr << e.what() << std::endl;
164 * @todo Add 'state' event do determine whether technology is
171 // indent-tabs-mode: nil