1 /*
2  * OS specific functions
3  * Copyright (c) 2005-2009, 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 
15 #ifndef OS_H
16 #define OS_H
17 #include <zephyr/kernel.h>
18 #include <sys/_timeval.h>
19 #include <strings.h>
20 #include "esp_types.h"
21 #include <string.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include "esp_err.h"
25 #include "supplicant_opt.h"
26 #include "esp_wifi.h"
27 #include "esp_heap_adapter.h"
28 
29 // Declare strlcpy here to avoid warnings
30 size_t strlcpy(char *dst, const char *src, size_t dsize);
31 
32 char *z_strdup(const char *s);
33 int z_strcasecmp(const char *string1, const char *string2);
34 int z_strncasecmp(const char *string1, const char *string2, size_t num);
35 
36 typedef time_t os_time_t;
37 
38 /**
39  * os_sleep - Sleep (sec, usec)
40  * @sec: Number of seconds to sleep
41  * @usec: Number of microseconds to sleep
42  */
43 void os_sleep(os_time_t sec, os_time_t usec);
44 
45 struct os_time {
46 	os_time_t sec;
47 	suseconds_t usec;
48 };
49 
50 #define os_reltime os_time
51 
52 struct os_tm {
53     int sec; /* 0..59 or 60 for leap seconds */
54     int min; /* 0..59 */
55     int hour; /* 0..23 */
56     int day; /* 1..31 */
57     int month; /* 1..12 */
58     int year; /* Four digit year */
59 };
60 
61 /**
62  * os_get_time - Get current time (sec, usec)
63  * @t: Pointer to buffer for the time
64  * Returns: 0 on success, -1 on failure
65  */
66 int os_get_time(struct os_time *t);
67 #define os_get_reltime os_get_time
68 
69 /* Helper macros for handling struct os_time */
70 
71 #define os_time_before(a, b) \
72 	((a)->sec < (b)->sec || \
73 	 ((a)->sec == (b)->sec && (a)->usec < (b)->usec))
74 
75 #define os_reltime_before os_time_before
76 #define os_time_sub(a, b, res) do { \
77 	(res)->sec = (a)->sec - (b)->sec; \
78 	(res)->usec = (a)->usec - (b)->usec; \
79 	if ((res)->usec < 0) { \
80 		(res)->sec--; \
81 		(res)->usec += 1000000; \
82 	} \
83 } while (0)
84 #define os_reltime_sub os_time_sub
85 
86 /**
87  * os_mktime - Convert broken-down time into seconds since 1970-01-01
88  * @year: Four digit year
89  * @month: Month (1 .. 12)
90  * @day: Day of month (1 .. 31)
91  * @hour: Hour (0 .. 23)
92  * @min: Minute (0 .. 59)
93  * @sec: Second (0 .. 60)
94  * @t: Buffer for returning calendar time representation (seconds since
95  * 1970-01-01 00:00:00)
96  * Returns: 0 on success, -1 on failure
97  *
98  * Note: The result is in seconds from Epoch, i.e., in UTC, not in local time
99  * which is used by POSIX mktime().
100  */
101 int os_mktime(int year, int month, int day, int hour, int min, int sec,
102 	      os_time_t *t);
103 
104 int os_gmtime(os_time_t t, struct os_tm *tm);
105 
106 /**
107  * os_daemonize - Run in the background (detach from the controlling terminal)
108  * @pid_file: File name to write the process ID to or %NULL to skip this
109  * Returns: 0 on success, -1 on failure
110  */
111 int os_daemonize(const char *pid_file);
112 
113 /**
114  * os_daemonize_terminate - Stop running in the background (remove pid file)
115  * @pid_file: File name to write the process ID to or %NULL to skip this
116  */
117 void os_daemonize_terminate(const char *pid_file);
118 
119 /**
120  * os_get_random - Get cryptographically strong pseudo random data
121  * @buf: Buffer for pseudo random data
122  * @len: Length of the buffer
123  * Returns: 0 on success, -1 on failure
124  */
125 int os_get_random(unsigned char *buf, size_t len);
126 
127 /**
128  * os_random - Get pseudo random value (not necessarily very strong)
129  * Returns: Pseudo random value
130  */
131 unsigned long os_random(void);
132 
133 /**
134  * os_rel2abs_path - Get an absolute path for a file
135  * @rel_path: Relative path to a file
136  * Returns: Absolute path for the file or %NULL on failure
137  *
138  * This function tries to convert a relative path of a file to an absolute path
139  * in order for the file to be found even if current working directory has
140  * changed. The returned value is allocated and caller is responsible for
141  * freeing it. It is acceptable to just return the same path in an allocated
142  * buffer, e.g., return strdup(rel_path). This function is only used to find
143  * configuration files when os_daemonize() may have changed the current working
144  * directory and relative path would be pointing to a different location.
145  */
146 char * os_rel2abs_path(const char *rel_path);
147 
148 /**
149  * os_program_init - Program initialization (called at start)
150  * Returns: 0 on success, -1 on failure
151  *
152  * This function is called when a programs starts. If there are any OS specific
153  * processing that is needed, it can be placed here. It is also acceptable to
154  * just return 0 if not special processing is needed.
155  */
156 int os_program_init(void);
157 
158 /**
159  * os_program_deinit - Program deinitialization (called just before exit)
160  *
161  * This function is called just before a program exists. If there are any OS
162  * specific processing, e.g., freeing resourced allocated in os_program_init(),
163  * it should be done here. It is also acceptable for this function to do
164  * nothing.
165  */
166 void os_program_deinit(void);
167 
168 /**
169  * os_setenv - Set environment variable
170  * @name: Name of the variable
171  * @value: Value to set to the variable
172  * @overwrite: Whether existing variable should be overwritten
173  * Returns: 0 on success, -1 on error
174  *
175  * This function is only used for wpa_cli action scripts. OS wrapper does not
176  * need to implement this if such functionality is not needed.
177  */
178 int os_setenv(const char *name, const char *value, int overwrite);
179 
180 /**
181  * os_unsetenv - Delete environent variable
182  * @name: Name of the variable
183  * Returns: 0 on success, -1 on error
184  *
185  * This function is only used for wpa_cli action scripts. OS wrapper does not
186  * need to implement this if such functionality is not needed.
187  */
188 int os_unsetenv(const char *name);
189 
190 /**
191  * os_readfile - Read a file to an allocated memory buffer
192  * @name: Name of the file to read
193  * @len: For returning the length of the allocated buffer
194  * Returns: Pointer to the allocated buffer or %NULL on failure
195  *
196  * This function allocates memory and reads the given file to this buffer. Both
197  * binary and text files can be read with this function. The caller is
198  * responsible for freeing the returned buffer with os_free().
199  */
200 /* We don't support file reading support */
os_readfile(const char * name,size_t * len)201 static inline char *os_readfile(const char *name, size_t *len)
202 {
203 	return NULL;
204 }
205 
206 /*
207  * The following functions are wrapper for standard ANSI C or POSIX functions.
208  * By default, they are just defined to use the standard function name and no
209  * os_*.c implementation is needed for them. This avoids extra function calls
210  * by allowing the C pre-processor take care of the function name mapping.
211  *
212  * If the target system uses a C library that does not provide these functions,
213  * build_config.h can be used to define the wrappers to use a different
214  * function name. This can be done on function-by-function basis since the
215  * defines here are only used if build_config.h does not define the os_* name.
216  * If needed, os_*.c file can be used to implement the functions that are not
217  * included in the C library on the target system. Alternatively,
218  * OS_NO_C_LIB_DEFINES can be defined to skip all defines here in which case
219  * these functions need to be implemented in os_*.c file for the target system.
220  */
221 
222 #ifndef os_malloc
223 #define os_malloc(s) os_wpa_malloc_func((s))
224 #endif
225 #ifndef os_realloc
226 #define os_realloc(p, s) os_wpa_realloc_func((p), (s))
227 #endif
228 #ifndef os_zalloc
229 #define os_zalloc(s) os_wpa_calloc_func(1, (s))
230 #endif
231 #ifndef os_calloc
232 #define os_calloc(p, s) os_wpa_calloc_func((p), (s))
233 #endif
234 
235 #ifndef os_free
236 #define os_free(p) os_wpa_free_func((p))
237 #endif
238 
239 #ifndef os_bzero
240 #define os_bzero(s, n) memset(s, 0, n)
241 #endif
242 
243 
244 #ifndef os_strdup
245 #ifdef _MSC_VER
246 #define os_strdup(s) _strdup(s)
247 #else
248 #define os_strdup(s) z_strdup(s)
249 #endif
250 #endif
251 char * ets_strdup(const char *s);
252 
253 #ifndef os_memcpy
254 #define os_memcpy(d, s, n) memcpy((d), (s), (n))
255 #endif
256 #ifndef os_memmove
257 #define os_memmove(d, s, n) memmove((d), (s), (n))
258 #endif
259 #ifndef os_memset
260 #define os_memset(s, c, n) memset(s, c, n)
261 #endif
262 #ifndef os_memcmp
263 #define os_memcmp(s1, s2, n) memcmp((s1), (s2), (n))
264 #endif
265 #ifndef os_memcmp_const
266 #define os_memcmp_const(s1, s2, n) memcmp((s1), (s2), (n))
267 #endif
268 
269 #ifndef os_strlen
270 #define os_strlen(s) strlen(s)
271 #endif
272 #ifndef os_strcasecmp
273 #ifdef _MSC_VER
274 #define os_strcasecmp(s1, s2) _stricmp((s1), (s2))
275 #else
276 #define os_strcasecmp(s1, s2) z_strcasecmp((s1), (s2))
277 #endif
278 #endif
279 #ifndef os_strncasecmp
280 #ifdef _MSC_VER
281 #define os_strncasecmp(s1, s2, n) _strnicmp((s1), (s2), (n))
282 #else
283 #define os_strncasecmp(s1, s2, n) z_strncasecmp((s1), (s2), (n))
284 #endif
285 #endif
286 #ifndef os_strchr
287 #define os_strchr(s, c) strchr((s), (c))
288 #endif
289 #ifndef os_strcmp
290 #define os_strcmp(s1, s2) strcmp((s1), (s2))
291 #endif
292 #ifndef os_strncmp
293 #define os_strncmp(s1, s2, n) strncmp((s1), (s2), (n))
294 #endif
295 #ifndef os_strrchr
296 #define os_strrchr(s, c)  strrchr((s), (c))
297 #endif
298 #ifndef os_strstr
299 #define os_strstr(h, n) strstr((h), (n))
300 #endif
301 #ifndef os_strlcpy
302 #define os_strlcpy(d, s, n) strlcpy((d), (s), (n))
303 #endif
304 #ifndef os_strcat
305 #define os_strcat(d, s) strcat((d), (s))
306 #endif
307 
308 #ifndef os_snprintf
309 #ifdef _MSC_VER
310 #define os_snprintf _snprintf
311 #else
312 #define os_snprintf snprintf
313 #endif
314 #endif
315 #ifndef os_sprintf
316 #define os_sprintf sprintf
317 #endif
318 
os_snprintf_error(size_t size,int res)319 static inline int os_snprintf_error(size_t size, int res)
320 {
321         return res < 0 || (unsigned int) res >= size;
322 }
323 
os_realloc_array(void * ptr,size_t nmemb,size_t size)324 static inline void * os_realloc_array(void *ptr, size_t nmemb, size_t size)
325 {
326 	if (size && nmemb > (~(size_t) 0) / size)
327 		return NULL;
328 	return os_realloc(ptr, nmemb * size);
329 }
330 
331 #ifdef CONFIG_CRYPTO_MBEDTLS
332 void forced_memzero(void *ptr, size_t len);
333 #else
334 /* Try to prevent most compilers from optimizing out clearing of memory that
335  * becomes unaccessible after this function is called. This is mostly the case
336  * for clearing local stack variables at the end of a function. This is not
337  * exactly perfect, i.e., someone could come up with a compiler that figures out
338  * the pointer is pointing to memset and then end up optimizing the call out, so
339  * try go a bit further by storing the first octet (now zero) to make this even
340  * a bit more difficult to optimize out. Once memset_s() is available, that
341  * could be used here instead. */
342 static void * (* const volatile memset_func)(void *, int, size_t) = memset;
343 static uint8_t forced_memzero_val;
344 
forced_memzero(void * ptr,size_t len)345 static inline void forced_memzero(void *ptr, size_t len)
346 {
347 	memset_func(ptr, 0, len);
348 	if (len) {
349 		forced_memzero_val = ((uint8_t *) ptr)[0];
350 	}
351 }
352 #endif
353 
354 extern const wifi_osi_funcs_t *wifi_funcs;
355 #define OS_BLOCK OSI_FUNCS_TIME_BLOCKING
356 
357 #define os_mutex_lock(a) wifi_funcs->_mutex_lock((a))
358 #define os_mutex_unlock(a) wifi_funcs->_mutex_unlock((a))
359 #define os_recursive_mutex_create() wifi_funcs->_recursive_mutex_create()
360 #define os_mutex_create() wifi_funcs->_mutex_create();
361 #define os_mutex_delete(a) wifi_funcs->_mutex_delete(a)
362 
363 #define os_queue_create(a, b) wifi_funcs->_queue_create((a), (b))
364 #define os_queue_delete(a) wifi_funcs->_queue_delete(a)
365 #define os_queue_send(a, b, c) wifi_funcs->_queue_send((a), (b), (c))
366 #define os_queue_send_to_front(a, b, c) wifi_funcs->_queue_send_to_front((a), (b), (c))
367 #define os_queue_recv(a, b, c) wifi_funcs->_queue_recv((a), (b), (c))
368 #define os_queue_msg_waiting(a) wifi_funcs->_queue_msg_waiting((a))
369 
370 #define os_task_create(a,b,c,d,e,f) wifi_funcs->_task_create((a), (b), (c), (d), (e), (f))
371 #define os_task_delete(a) wifi_funcs->_task_delete((a))
372 #define os_task_get_current_task() wifi_funcs->_task_get_current_task()
373 
374 #define os_semphr_create(a, b) wifi_funcs->_semphr_create((a), (b))
375 #define os_semphr_delete(a) wifi_funcs->_semphr_delete((a))
376 #define os_semphr_give(a) wifi_funcs->_semphr_give((a))
377 #define os_semphr_take(a, b) wifi_funcs->_semphr_take((a), (b))
378 
379 #define os_task_ms_to_tick(a) wifi_funcs->_task_ms_to_tick((a))
380 #define os_timer_get_time(void) wifi_funcs->_esp_timer_get_time(void)
381 
382 #define os_event_group_create(void) wifi_funcs->_event_group_create(void)
383 #define os_event_group_delete(void) wifi_funcs->_event_group_delete(void)
384 #define os_event_group_wait_bits(a, b, c, d, e) wifi_funcs->_event_group_wait_bits((a), (b), (c), (d), (e))
385 #define os_event_group_clear_bits(a, b) wifi_funcs->_event_group_clear_bits((a), (b))
386 #define os_event_group_set_bits(a, b) wifi_funcs->_event_group_set_bits((a), (b))
387 
os_timer_setfn(void * ptimer,void * pfunction,void * parg)388 static inline void os_timer_setfn(void *ptimer, void *pfunction, void *parg)
389 {
390        return wifi_funcs->_timer_setfn(ptimer, pfunction, parg);
391 }
os_timer_disarm(void * ptimer)392 static inline void os_timer_disarm(void *ptimer)
393 {
394        return wifi_funcs->_timer_disarm(ptimer);
395 }
os_timer_arm_us(void * ptimer,uint32_t u_seconds,bool repeat_flag)396 static inline void os_timer_arm_us(void *ptimer,uint32_t u_seconds,bool repeat_flag)
397 {
398        return wifi_funcs->_timer_arm_us(ptimer, u_seconds, repeat_flag);
399 }
os_timer_arm(void * ptimer,uint32_t milliseconds,bool repeat_flag)400 static inline void os_timer_arm(void *ptimer,uint32_t milliseconds,bool repeat_flag)
401 {
402        return wifi_funcs->_timer_arm(ptimer, milliseconds, repeat_flag);
403 }
os_timer_done(void * ptimer)404 static inline void os_timer_done(void *ptimer)
405 {
406        return wifi_funcs->_timer_done(ptimer);
407 }
408 
409 #endif /* OS_H */
410