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
27/*
28 * Normally you don't want this, use lws_sul instead inside the event loop.
29 * But sometimes for drivers it makes sense, so there's an internal-only
30 * crossplatform api for it.
31 */
32
33void
34lws_msleep(unsigned int ms)
35{
36	vTaskDelay(portTICK_PERIOD_MS > ms ? 1 : ms / portTICK_PERIOD_MS);
37}
38
39lws_usec_t
40lws_now_usecs(void)
41{
42	struct timeval tv;
43	gettimeofday(&tv, NULL);
44	return ((unsigned long long)tv.tv_sec * 1000000LL) + tv.tv_usec;
45}
46
47size_t
48lws_get_random(struct lws_context *context, void *buf, size_t len)
49{
50#if defined(LWS_WITH_ESP32)
51	uint8_t *pb = buf;
52
53	while (len) {
54		uint32_t r = esp_random();
55		uint8_t *p = (uint8_t *)&r;
56		int b = 4;
57
58		if (len < (size_t)b)
59			b = len;
60
61		len -= b;
62
63		while (b--)
64			*pb++ = p[b];
65	}
66
67	return pb - (uint8_t *)buf;
68#else
69#if defined(LWS_WITH_MBEDTLS)
70	int n;
71
72	n = mbedtls_ctr_drbg_random(&context->mcdc, buf, len);
73	if (!n)
74		return len;
75
76	/* failed */
77
78	lwsl_err("%s: mbedtls_ctr_drbg_random returned 0x%x\n", __func__, n);
79#endif
80	return 0;
81#endif
82}
83
84
85void lwsl_emit_syslog(int level, const char *line)
86{
87	lwsl_emit_stderr(level, line);
88}
89
90int
91lws_plat_drop_app_privileges(struct lws_context *context, int actually_init)
92{
93	return 0;
94}
95
96int
97lws_plat_recommended_rsa_bits(void)
98{
99	/*
100	 * 2048-bit key generation takes up to a minute on ESP32, 4096
101	 * is like 15 minutes +
102	 */
103	return 2048;
104}
105