1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
|
/**
* @file settings_test.cpp
*
* @brief Settings daemon test.
*
* @author Ossama Othman @<ossama.othman@@intel.com@>
*
* @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 <libwebsockets.h>
#include <iostream>
#include <vector>
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<unsigned char> 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<int>(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<int>(reinterpret_cast<intptr_t>(in));
// short const events = static_cast<short>(len);
switch(reason) {
case LWS_CALLBACK_CLIENT_RECEIVE:
// Response has come in from Settings daemon.
std::cout << "Response: " << static_cast<char const *>(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:
|