-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
532 lines (494 loc) · 21.4 KB
/
main.cpp
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/*
Copyright (c) 2020 Patrick Moffitt
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
//
// WebSocketServer.cpp
//
// This sample demonstrates the WebSocket class.
//
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/Net/HTTPServer.h"
#include "Poco/Net/HTTPRequestHandler.h"
#include "Poco/Net/HTTPRequestHandlerFactory.h"
#include "Poco/Net/HTTPServerParams.h"
#include "Poco/Net/HTTPServerRequest.h"
#include "Poco/Net/HTTPServerResponse.h"
#include "Poco/Net/HTTPServerParams.h"
#include "Poco/Net/ServerSocket.h"
#include "Poco/Net/WebSocket.h"
#include "Poco/Net/NetException.h"
#include "Poco/Util/ServerApplication.h"
#include "Poco/Util/Option.h"
#include "Poco/Util/OptionSet.h"
#include "Poco/Util/HelpFormatter.h"
#include "Poco/Format.h"
#include "motion_camera_settings_generated.h"
#include "html_css_js_util.hpp"
#include <iostream>
#include <fstream>
#include <unordered_set>
using namespace OV5642_Settings;
using html = html_css_js_util;
using Poco::Net::ServerSocket;
using Poco::Net::WebSocket;
using Poco::Net::WebSocketException;
using Poco::Net::HTTPRequestHandler;
using Poco::Net::HTTPRequestHandlerFactory;
using Poco::Net::HTTPServer;
using Poco::Net::HTTPServerRequest;
using Poco::Net::HTTPResponse;
using Poco::Net::HTTPServerResponse;
using Poco::Net::HTTPServerParams;
using Poco::Timestamp;
using Poco::ThreadPool;
using Poco::Util::ServerApplication;
using Poco::Util::Application;
using Poco::Util::Option;
using Poco::Util::OptionSet;
using Poco::Util::HelpFormatter;
class PageRequestHandler: public HTTPRequestHandler
/// Return a HTML document with some JavaScript creating
/// a WebSocket connection.
{
public:
void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
{
response.setChunkedTransferEncoding(true);
response.setContentType("text/html");
std::ostream& ostr = response.send();
ostr << html::render(request.serverAddress().toString());
/*
ostr << "<!doctype html>";
ostr << "<html lang=\"en-US\">";
ostr << "<head>";
ostr << "<meta charset=\"utf-8\">";
ostr << "<title>WebSocketServer</title>";
ostr << flatbuffers_js;
ostr << motion_camera_settings_generated_js;
ostr << "<script type=\"text/javascript\">";
ostr << "function WebSocketTest()";
ostr << "{";
ostr << " if (\"WebSocket\" in window)";
ostr << " {";
ostr << " var ws = new WebSocket(\"ws://" << request.serverAddress().toString() << "/ws\");";
ostr << " ws.onopen = function()";
ostr << " {";
ostr << " ws.send(\"Hello, world!\");";
ostr << " };";
ostr << " ws.onmessage = function(evt)";
ostr << " { ";
ostr << " evt.data.arrayBuffer().then(function(result) { ";
ostr << " const bytes = new Uint8Array(result);";
ostr << " var buf = new flatbuffers.ByteBuffer(bytes);";
ostr << " settings = OV5642_Settings.Settings.getRootAsSettings(buf);";
ostr << " console.log(settings.event"
ostr << " });";
//ostr << " var msg = evt.data;";
//ostr << " alert(\"Message received: \" + msg);";
ostr << " alert(\"Message received: \");";
ostr << " ws.close();";
ostr << " };";
ostr << " ws.onclose = function()";
ostr << " { ";
ostr << " alert(\"WebSocket closed.\");";
ostr << " };";
ostr << " }";
ostr << " else";
ostr << " {";
ostr << " alert(\"This browser does not support WebSockets.\");";
ostr << " }";
ostr << "}";
ostr << "</script>";
ostr << "</head>";
ostr << "<body>";
ostr << " <h1>WebSocket Server</h1>";
ostr << " <p><a href=\"javascript:WebSocketTest()\">Run WebSocket Script</a></p>";
ostr << "</body>";
ostr << "</html>";
*/
ostr.flush();
}
};
class FlatBufferHandler {
/// A utility class for loading, saving and comparing flatBuffer files.
public:
/// Compare to flatBuffers stored in fixed-name files.
static void compareBuffers(Poco::Util::LayeredConfiguration *config, Poco::Logger *logger){
using namespace OV5642_Settings;
using namespace flatbuffers;
size_t buf_size{0};
auto default_filename = config->getString("WebSocketServer.default_buffer",
"/tmp/default_buffer.bin");
auto new_filename = config->getString("WebSocketServer.new_buffer",
"/tmp/new_buffer.bin");
logger->information(Poco::format("Comparing: %s, %s", default_filename, new_filename));
std::ifstream ifs;
ifs.open(default_filename, std::ios_base::binary | std::ios_base::in | std::ios_base::ate);
buf_size = ifs.tellg();
ifs.seekg(std::ios_base::beg);
if (not ifs.is_open()) {
logger->error(Poco::format("\nCannot open: %s", default_filename));
Poco::Util::ServerApplication::terminate();
}
uint8_t default_buf[buf_size];
for (unsigned char & i : default_buf) {
i = ifs.get();
}
ifs.close();
auto def_v = Verifier(default_buf, buf_size);
bool def_ok = VerifySettingsBuffer(def_v);
if(def_ok) {
logger->information("\nflatbuffers::VerifySettingsBuffer PASSED for default_buffer.bin");
} else {
logger->error("\nflatbuffers::VerifySettingsBuffer FAILED for default_buffer.bin");
Poco::Util::ServerApplication::terminate();
}
auto default_settings = GetSettings(default_buf);
ifs.open(new_filename, std::ios_base::binary | std::ios_base::in | std::ios_base::ate);
buf_size = ifs.tellg();
ifs.seekg(std::ios_base::beg);
if (not ifs.is_open()) {
logger->error(Poco::format("\nCannot open: %s", new_filename));
Poco::Util::ServerApplication::terminate();
}
uint8_t new_buf[buf_size];
for (unsigned char & i : new_buf) {
i = ifs.get();
}
ifs.close();
auto new_v = Verifier(new_buf, buf_size);
bool new_ok = VerifySettingsBuffer(new_v);
if(new_ok) {
logger->information("\nflatbuffers::VerifySettingsBuffer PASSED for new_buffer.bin");
} else {
logger->error("\nflatbuffers::VerifySettingsBuffer FAILED for new_buffer.");
Poco::Util::ServerApplication::terminate();
}
auto new_settings = GetSettings(new_buf);
std::map<std::string, uint8_t> map_d {
{"jpeg_size", default_settings->jpeg_size()},
{"quality", default_settings->quality()},
{"frames", default_settings->frames()},
{"light_mode", default_settings->light_mode()},
{"color_saturation", default_settings->color_saturation()},
{"brightness", default_settings->brightness()},
{"contrast", default_settings->contrast()},
{"hue", default_settings->hue()},
{"effect", default_settings->effect()},
{"exposure", default_settings->exposure()},
{"sharpness", default_settings->sharpness()},
{"mirror", default_settings->mirror()},
{"test_pattern", default_settings->test_pattern()}
};
std::map<std::string, uint8_t> map_n {
{"jpeg_size", new_settings->jpeg_size()},
{"quality", new_settings->quality()},
{"frames", new_settings->frames()},
{"light_mode", new_settings->light_mode()},
{"color_saturation", new_settings->color_saturation()},
{"brightness", new_settings->brightness()},
{"contrast", new_settings->contrast()},
{"hue", new_settings->hue()},
{"effect", new_settings->effect()},
{"exposure", new_settings->exposure()},
{"sharpness", new_settings->sharpness()},
{"mirror", new_settings->mirror()},
{"test_pattern", new_settings->test_pattern()}
};
std::unordered_set<std::string> keys{ // Order is reversed at compile time.
"test_pattern", "mirror", "sharpness", "exposure", "effect", "hue",
"contrast", "brightness", "color_saturation", "light_mode", "frames",
"quality", "jpeg_size"
};
std::string h1{"key"};
std::string h2{"default"};
std::string h3{"new"};
std::string h4{"match"};
logger->information(Poco::format("\n%-18s%-10s%-10s%-10s", h1, h2, h3, h4));
logger->information("-------------------------------------------");
std::string result_en{"true"};
int matches{0};
for (auto &k: keys) {
if (map_d[k] != map_n[k]) {
result_en = "false";
} else {
result_en = "true";
matches++;
}
logger->information(
Poco::format("%-18s%-10d%-10d%-10s", k, int(map_d[k]), int(map_n[k]), result_en));
}
if (matches < keys.size()) {
logger->error("\nCompare result: buffers DO NOT match.");
} else {
logger->information("\nCompare result: buffers match.");
}
Poco::Util::ServerApplication::terminate();
}
/// Load a flatBuffer from a file.
static void loadBuffer(uint8_t *buf, int &buf_size, std::string &filename, Poco::Logger *logger) {
std::ifstream ifs;
ifs.open(filename, std::ios_base::binary | std::ios_base::in | std::ios_base::ate);
if (not ifs.is_open()) {
logger->error(Poco::format("\nCannot open: %s", filename));
buf_size = 0;
}
auto size = ifs.tellg();
ifs.seekg(0);
for (int i=0; i<size; i++) {
buf[i] = ifs.get();
}
ifs.close();
buf_size = size;
}
/// Save a flatBuffer to a file.
static void saveBuffer(uint8_t *buf, size_t size, std::string &filename) {
std::ofstream ofs;
ofs.open(filename, std::ios_base::binary | std::ios_base::out | std::ios_base::trunc);
for (int i=0; i<size; i++) {
ofs << buf[i];
}
ofs.flush();
ofs.close();
}
};
class WebSocketRequestHandler: public HTTPRequestHandler
/// Handle a WebSocket connection.
{
public:
void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
{
Application& app = Application::instance();
try
{
WebSocket ws(request, response);
app.logger().information("WebSocket connection established.");
uint8_t buffer[1024];
int flags;
int bytes_received;
do
{
bytes_received = ws.receiveFrame(buffer, sizeof(buffer), flags);
app.logger().information(
Poco::format("Frame received (length=%d, flags=0x%x).",
bytes_received, unsigned(flags))
);
bool request_handled{false};
/// Send default settings.
if (not request_handled
and std::string(reinterpret_cast<char*>(buffer)) == "getDefault") {
memset(buffer, '\0', sizeof(buffer));
flatbuffers::FlatBufferBuilder builder(64);
uint8_t jpeg_size = 0; // OV5642_320x240
uint8_t quality = 1; // default_quality
uint8_t frames = 0; // one frame
uint8_t light_mode = 0; // Advanced_AWB
uint8_t color_saturation = 4; // Saturation0
uint8_t brightness = 4; // Brightness0
uint8_t contrast = 4; // Contrast0
uint8_t hue = 6; // degree_0
uint8_t effect = 7; // Normal
uint8_t exposure = 5; // Exposure_default
uint8_t sharpness = 0; // Auto_Sharpness_default
uint8_t mirror = 7; // Normal
uint8_t test_pattern = 7; // Normal
auto build_buffer = CreateSettings(builder,
jpeg_size, quality, frames, light_mode,
color_saturation, brightness, contrast,
hue, effect, exposure, sharpness,
mirror, test_pattern);
builder.Finish(build_buffer);
uint8_t *buf = builder.GetBufferPointer();
int size = builder.GetSize();
if (not app.config().getBool("application.runAsDaemon", false)) {
auto filename = app.config().getString("WebSocketServer.default_buffer",
"/tmp/default_buffer.bin");
FlatBufferHandler::saveBuffer(buf, size, filename);
}
flags = WebSocket::FRAME_BINARY;
ws.sendFrame(buf, size, flags);
request_handled = true;
app.logger().information("Default settings sent.");
}
/// Send new settings.
if (not request_handled
and std::string(reinterpret_cast<char*>(buffer)) == "getNew") {
auto filename = app.config().getString("WebSocketServer.new_buffer",
"/tmp/new_buffer.bin");
int buf_size{0};
FlatBufferHandler::loadBuffer(buffer, buf_size, filename, &app.logger());
flags = WebSocket::FRAME_BINARY;
ws.sendFrame(buffer, buf_size, flags);
request_handled = true;
app.logger().information("New settings sent.");
}
/// Save a new settings buffer.
if (not request_handled and bytes_received > 0 ) {
app.logger().information("Received Flatbuffer size: " + std::to_string(bytes_received));
// We received a byteArray, save it.
auto filename = app.config().getString("WebSocketServer.new_buffer",
"/tmp/new_buffer.bin");
FlatBufferHandler::saveBuffer(buffer, bytes_received, filename);
request_handled = true;
app.logger().information("New settings buffer saved.");
}
}
while (bytes_received > 0 && (flags & WebSocket::FRAME_OP_BITMASK) != WebSocket::FRAME_OP_CLOSE);
app.logger().information("WebSocket connection closed.");
}
catch (WebSocketException& exc)
{
app.logger().log(exc);
switch (exc.code())
{
case WebSocket::WS_ERR_HANDSHAKE_UNSUPPORTED_VERSION:
response.set("Sec-WebSocket-Version", WebSocket::WEBSOCKET_VERSION);
// fallthrough
case WebSocket::WS_ERR_NO_HANDSHAKE:
case WebSocket::WS_ERR_HANDSHAKE_NO_VERSION:
case WebSocket::WS_ERR_HANDSHAKE_NO_KEY:
response.setStatusAndReason(HTTPResponse::HTTP_BAD_REQUEST);
response.setContentLength(0);
response.send();
break;
}
}
}
};
class RequestHandlerFactory: public HTTPRequestHandlerFactory
{
public:
HTTPRequestHandler* createRequestHandler(const HTTPServerRequest& request)
{
Application& app = Application::instance();
app.logger().information("Request from "
+ request.clientAddress().toString()
+ ": "
+ request.getMethod()
+ " "
+ request.getURI()
+ " "
+ request.getVersion());
for (HTTPServerRequest::ConstIterator it = request.begin(); it != request.end(); ++it)
{
app.logger().information(it->first + ": " + it->second);
}
if(request.find("Upgrade") != request.end() && Poco::icompare(request["Upgrade"], "websocket") == 0)
return new WebSocketRequestHandler;
else
return new PageRequestHandler;
}
};
class WebSocketServer: public Poco::Util::ServerApplication
/// The main application class.
///
/// This class handles command-line arguments and
/// configuration files.
/// Start the WebSocketServer executable with the help
/// option (/help on Windows, --help on Unix) for
/// the available command line options.
///
/// To use the sample configuration file (WebSocketServer.properties),
/// copy the file to the directory where the WebSocketServer executable
/// resides. If you start the debug version of the WebSocketServer
/// (WebSocketServerd[.exe]), you must also create a copy of the configuration
/// file named WebSocketServerd.properties. In the configuration file, you
/// can specify the port on which the server is listening (default
/// 9980) and the format of the date/time string sent back to the client.
///
/// To test the WebSocketServer you can use any web browser (http://localhost:9980/).
{
public:
WebSocketServer(): _helpRequested(false)
{
}
~WebSocketServer()
{
}
protected:
void initialize(Application& self)
{
loadConfiguration(); // load default configuration files, if present
ServerApplication::initialize(self);
}
void uninitialize()
{
ServerApplication::uninitialize();
}
void defineOptions(OptionSet& options)
{
ServerApplication::defineOptions(options);
options.addOption(
Option("help", "h", "display help information on command line arguments")
.required(false)
.repeatable(false));
options.addOption(
Option("compare", "c", "compare default_buffer.bin to new_buffer.bin")
.required(false)
.repeatable(false));
}
void handleOption(const std::string& name, const std::string& value)
{
ServerApplication::handleOption(name, value);
if (name == "help")
_helpRequested = true;
/// Implement FlatBufferHandler::compareBuffers
if (name == "compare") {
loadConfiguration();
FlatBufferHandler::compareBuffers(&config(), &logger());
}
}
void displayHelp()
{
HelpFormatter helpFormatter(options());
helpFormatter.setCommand(commandName());
helpFormatter.setUsage("OPTIONS");
helpFormatter.setHeader("A 5MP Camera Settings server supporting the WebSocket protocol.");
helpFormatter.format(std::cout);
}
int main(const std::vector<std::string>& args)
{
if (_helpRequested)
{
displayHelp();
}
else
{
// get parameters from configuration file
unsigned short port = (unsigned short) config().getInt("WebSocketServer.port", 9980);
// set-up a server socket
ServerSocket svs(port);
// set-up a HTTPServer instance
HTTPServer srv(new RequestHandlerFactory, svs, new HTTPServerParams);
// start the HTTPServer
srv.start();
// wait for CTRL-C or kill
waitForTerminationRequest();
// Stop the HTTPServer
srv.stop();
}
return Application::EXIT_OK;
}
private:
bool _helpRequested;
};
POCO_SERVER_MAIN(WebSocketServer)