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 <zephyr/net/tls_credentials.h>
58 #include "../certificate.h"
59 
60 static const sec_tag_t sec_tag_list_verify_none[] = {
61 		SERVER_CERTIFICATE_TAG,
62 #if defined(CONFIG_MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
63 		PSK_TAG,
64 #endif
65 	};
66 
67 #define SEC_TAG_LIST sec_tag_list_verify_none
68 #define SEC_TAG_LIST_LEN sizeof(sec_tag_list_verify_none)
69 #else
70 #define SEC_TAG_LIST NULL
71 #define SEC_TAG_LIST_LEN 0
72 #endif /* CONFIG_NET_SAMPLE_HTTPS_SERVICE */
73 
74 WEBSOCKET_CONSOLE_DEFINE(ws_console_service, SEC_TAG_LIST,
75 			 SEC_TAG_LIST_LEN);
76 
77 HTTP_RESOURCE_DEFINE(root_resource, ws_console_service, "/",
78 		     &index_html_gz_resource_detail);
79 
80 HTTP_RESOURCE_DEFINE(index_html_gz_resource, ws_console_service, "/index.html",
81 		     &index_html_gz_resource_detail);
82 
83 HTTP_RESOURCE_DEFINE(style_css_gz_resource, ws_console_service, "/style.css",
84 		     &style_css_gz_resource_detail);
85 
86 HTTP_RESOURCE_DEFINE(favicon_16x16_png_gz_resource, ws_console_service,
87 		     "/favicon-16x16.png",
88 		     &favicon_16x16_png_gz_resource_detail);
89 
init_ws(void)90 int init_ws(void)
91 {
92 	int ret;
93 
94 	WEBSOCKET_CONSOLE_ENABLE(ws_console_service);
95 
96 	ret = http_server_start();
97 	if (ret < 0) {
98 		LOG_DBG("Cannot start websocket console (%d)", ret);
99 	} else {
100 		LOG_DBG("Starting websocket console");
101 	}
102 
103 	return 0;
104 }
105