1 /*
2 * Copyright (c) 2024 Nordic Semiconductor
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/logging/log.h>
8 LOG_MODULE_DECLARE(net_echo_server_sample, LOG_LEVEL_DBG);
9
10 #include <zephyr/kernel.h>
11 #include <zephyr/net/tls_credentials.h>
12 #include <zephyr/shell/shell_websocket.h>
13
14 static const char index_html_gz[] = {
15 #include "index.html.gz.inc"
16 };
17
18 struct http_resource_detail_static index_html_gz_resource_detail = {
19 .common = {
20 .type = HTTP_RESOURCE_TYPE_STATIC,
21 .bitmask_of_supported_http_methods = BIT(HTTP_GET),
22 .content_encoding = "gzip",
23 },
24 .static_data = index_html_gz,
25 .static_data_len = sizeof(index_html_gz),
26 };
27
28 static const char style_css_gz[] = {
29 #include "style.css.gz.inc"
30 };
31
32 struct http_resource_detail_static style_css_gz_resource_detail = {
33 .common = {
34 .type = HTTP_RESOURCE_TYPE_STATIC,
35 .bitmask_of_supported_http_methods = BIT(HTTP_GET),
36 .content_encoding = "gzip",
37 },
38 .static_data = style_css_gz,
39 .static_data_len = sizeof(style_css_gz),
40 };
41
42 static const char favicon_16x16_png_gz[] = {
43 #include "favicon-16x16.png.gz.inc"
44 };
45
46 struct http_resource_detail_static favicon_16x16_png_gz_resource_detail = {
47 .common = {
48 .type = HTTP_RESOURCE_TYPE_STATIC,
49 .bitmask_of_supported_http_methods = BIT(HTTP_GET),
50 .content_encoding = "gzip",
51 },
52 .static_data = favicon_16x16_png_gz,
53 .static_data_len = sizeof(favicon_16x16_png_gz),
54 };
55
56 #if defined(CONFIG_NET_SAMPLE_HTTPS_SERVICE)
57 #include "../certificate.h"
58
59 static const sec_tag_t sec_tag_list_verify_none[] = {
60 SERVER_CERTIFICATE_TAG,
61 #if defined(CONFIG_MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
62 PSK_TAG,
63 #endif
64 };
65
66 #define SEC_TAG_LIST sec_tag_list_verify_none
67 #define SEC_TAG_LIST_LEN sizeof(sec_tag_list_verify_none)
68 #else
69 #define SEC_TAG_LIST NULL
70 #define SEC_TAG_LIST_LEN 0
71 #endif /* CONFIG_NET_SAMPLE_HTTPS_SERVICE */
72
73 WEBSOCKET_CONSOLE_DEFINE(ws_console_service, SEC_TAG_LIST,
74 SEC_TAG_LIST_LEN);
75
76 HTTP_RESOURCE_DEFINE(root_resource, ws_console_service, "/",
77 &index_html_gz_resource_detail);
78
79 HTTP_RESOURCE_DEFINE(index_html_gz_resource, ws_console_service, "/index.html",
80 &index_html_gz_resource_detail);
81
82 HTTP_RESOURCE_DEFINE(style_css_gz_resource, ws_console_service, "/style.css",
83 &style_css_gz_resource_detail);
84
85 HTTP_RESOURCE_DEFINE(favicon_16x16_png_gz_resource, ws_console_service,
86 "/favicon-16x16.png",
87 &favicon_16x16_png_gz_resource_detail);
88
init_ws(void)89 int init_ws(void)
90 {
91 int ret;
92
93 WEBSOCKET_CONSOLE_ENABLE(ws_console_service);
94
95 ret = http_server_start();
96 if (ret < 0) {
97 LOG_DBG("Cannot start websocket console (%d)", ret);
98 } else {
99 LOG_DBG("Starting websocket console");
100 }
101
102 return 0;
103 }
104