Imported Upstream version 3.2
[platform/upstream/libwebsockets.git] / lib / plat / esp32 / esp32-misc.c
1 /*
2  * libwebsockets - lib/plat/lws-plat-esp32.c
3  *
4  * Copyright (C) 2010-2017 Andy Green <andy@warmcat.com>
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Lesser General Public
8  *  License as published by the Free Software Foundation:
9  *  version 2.1 of the License.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this library; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  *  MA  02110-1301  USA
20  */
21
22 #include "core/private.h"
23
24 lws_usec_t
25 lws_now_usecs(void)
26 {
27         struct timeval tv;
28         gettimeofday(&tv, NULL);
29         return ((unsigned long long)tv.tv_sec * 1000000LL) + tv.tv_usec;
30 }
31
32 LWS_VISIBLE int
33 lws_get_random(struct lws_context *context, void *buf, int len)
34 {
35 #if defined(LWS_AMAZON_RTOS)
36         int n;
37
38         n = mbedtls_ctr_drbg_random(&context->mcdc, buf, len);
39         if (!n)
40                 return len;
41
42         /* failed */
43
44         lwsl_err("%s: mbedtls_ctr_drbg_random returned 0x%x\n", __func__, n);
45
46         return 0;
47 #else
48         uint8_t *pb = buf;
49
50         while (len) {
51                 uint32_t r = esp_random();
52                 uint8_t *p = (uint8_t *)&r;
53                 int b = 4;
54
55                 if (len < b)
56                         b = len;
57
58                 len -= b;
59
60                 while (b--)
61                         *pb++ = p[b];
62         }
63
64         return pb - (uint8_t *)buf;
65 #endif
66 }
67
68
69 LWS_VISIBLE void lwsl_emit_syslog(int level, const char *line)
70 {
71         lwsl_emit_stderr(level, line);
72 }
73
74 int
75 lws_plat_drop_app_privileges(struct lws_context *context, int actually_init)
76 {
77         return 0;
78 }
79
80 int
81 lws_plat_recommended_rsa_bits(void)
82 {
83         /*
84          * 2048-bit key generation takes up to a minute on ESP32, 4096
85          * is like 15 minutes +
86          */
87         return 2048;
88 }
89
90 void esp32_uvtimer_cb(TimerHandle_t t)
91 {
92         struct timer_mapping *p = pvTimerGetTimerID(t);
93
94         p->cb(p->t);
95 }
96