1/*
2 * libwebsockets - small server side websockets and web server implementation
3 *
4 * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25#include "private-lib-core.h"
26
27int
28lws_plat_context_early_init(void)
29{
30	return 0;
31}
32
33void
34lws_plat_context_early_destroy(struct lws_context *context)
35{
36#if defined(LWS_AMAZON_RTOS) && defined(LWS_WITH_MBEDTLS)
37	mbedtls_ctr_drbg_free(&context->mcdc);
38	mbedtls_entropy_free(&context->mec);
39#endif
40}
41
42void
43lws_plat_context_late_destroy(struct lws_context *context)
44{
45#ifdef LWS_WITH_PLUGINS
46	if (context->plugin_list)
47		lws_plat_plugins_destroy(context);
48#endif
49
50	if (context->lws_lookup)
51		lws_free(context->lws_lookup);
52}
53
54#if defined(LWS_WITH_HTTP2)
55/*
56 * These are the default SETTINGS used on this platform.  The user
57 * can selectively modify them for a vhost during vhost creation.
58 */
59const struct http2_settings lws_h2_defaults_esp32 = { {
60	1,
61	/* H2SET_HEADER_TABLE_SIZE */			 512,
62	/* H2SET_ENABLE_PUSH */				   0,
63	/* H2SET_MAX_CONCURRENT_STREAMS */		   8,
64	/* H2SET_INITIAL_WINDOW_SIZE */		           0,
65	/* H2SET_MAX_FRAME_SIZE */		       16384,
66	/* H2SET_MAX_HEADER_LIST_SIZE */	 	 512,
67	/* H2SET_RESERVED7 */				   0,
68	/* H2SET_ENABLE_CONNECT_PROTOCOL */		   1,
69}};
70#endif
71
72int
73lws_plat_init(struct lws_context *context,
74	      const struct lws_context_creation_info *info)
75{
76#if defined(LWS_AMAZON_RTOS) && defined(LWS_WITH_MBEDTLS)
77	int n;
78
79	/* initialize platform random through mbedtls */
80	mbedtls_entropy_init(&context->mec);
81	mbedtls_ctr_drbg_init(&context->mcdc);
82
83	n = mbedtls_ctr_drbg_seed(&context->mcdc, mbedtls_entropy_func,
84				  &context->mec, NULL, 0);
85	if (n) {
86		lwsl_err("%s: mbedtls_ctr_drbg_seed() returned 0x%x\n",
87			 __func__, n);
88
89		return 1;
90	}
91#endif
92
93	/* context has the global fd lookup array */
94	context->lws_lookup = lws_zalloc(sizeof(struct lws *) *
95					 context->max_fds, "esp32 lws_lookup");
96	if (context->lws_lookup == NULL) {
97		lwsl_err("OOM on lws_lookup array for %d connections\n",
98			 context->max_fds);
99		return 1;
100	}
101
102	lwsl_notice(" mem: platform fd map: %5lu bytes\n",
103		    (unsigned long)(sizeof(struct lws *) * context->max_fds));
104
105#ifdef LWS_WITH_PLUGINS
106	if (info->plugin_dirs)
107		lws_plat_plugins_init(context, info->plugin_dirs);
108#endif
109#if defined(LWS_WITH_HTTP2)
110	/* override settings */
111	context->set = lws_h2_defaults_esp32;
112#endif
113
114#if defined(LWS_ESP_PLATFORM)
115	gpio_install_isr_service(0);
116#endif
117
118	return 0;
119}
120