1 /*
2  * wpa_supplicant/hostapd / Internal implementation of OS specific functions
3  * Copyright (c) 2005-2006, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  *
14  * This file is an example of operating system specific  wrapper functions.
15  * This version implements many of the functions internally, so it can be used
16  * to fill in missing functions from the target system C libraries.
17  *
18  * Some of the functions are using standard C library calls in order to keep
19  * this file in working condition to allow the functions to be tested on a
20  * Linux target. Please note that OS_NO_C_LIB_DEFINES needs to be defined for
21  * this file to work correctly. Note that these implementations are only
22  * examples and are not optimized for speed.
23  */
24 
25 #include "os.h"
26 #include <stdlib.h>
27 #include <time.h>
28 #include <sys/time.h>
29 #include "esp_system.h"
30 #include "utils/common.h"
31 #include "mbedtls/platform_util.h"
32 
os_get_time(struct os_time * t)33 int os_get_time(struct os_time *t)
34 {
35     struct timeval tv;
36     int ret = gettimeofday(&tv, NULL);
37     t->sec = (os_time_t) tv.tv_sec;
38     t->usec = tv.tv_usec;
39     return ret;
40 }
41 
os_random(void)42 unsigned long os_random(void)
43 {
44     return esp_random();
45 }
46 
os_get_random(unsigned char * buf,size_t len)47 int os_get_random(unsigned char *buf, size_t len)
48 {
49     esp_fill_random(buf, len);
50     return 0;
51 }
52 
os_sleep(os_time_t sec,os_time_t usec)53 void os_sleep(os_time_t sec, os_time_t usec)
54 {
55     if (sec) {
56         sleep(sec);
57     }
58     if (usec) {
59         usleep(usec);
60     }
61 }
62 
63 #ifdef USE_MBEDTLS_CRYPTO
forced_memzero(void * ptr,size_t len)64 void forced_memzero(void *ptr, size_t len)
65 {
66     mbedtls_platform_zeroize(ptr, len);
67 }
68 #endif
69