1 /*
2  * Copyright (c) 2021 Espressif Systems (Shanghai) Co., Ltd.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #define CONFIG_POSIX_FS
7 
8 #include "esp_wifi.h"
9 #include "stdlib.h"
10 #include "string.h"
11 #include "esp_private/wifi.h"
12 #include "soc/soc.h"
13 #include "soc/dport_access.h"
14 #include "soc/wdev_reg.h"
15 #include "soc/soc_caps.h"
16 #include "soc/rtc.h"
17 #include "soc/system_reg.h"
18 #include "esp_log.h"
19 #include "esp_attr.h"
20 #include "esp_private/wifi_os_adapter.h"
21 #include "esp_system.h"
22 #include "esp_timer.h"
23 #include "os.h"
24 #include "esp_wpa.h"
25 #include "esp_private/periph_ctrl.h"
26 #include "esp_phy_init.h"
27 #include <soc/syscon_reg.h>
28 #include <rom/efuse.h>
29 #include <rom/rtc.h>
30 #include <rom/ets_sys.h>
31 #include <riscv/interrupt.h>
32 #include "esp32c3/rom/ets_sys.h"
33 #include "esp_mac.h"
34 #include "wifi/wifi_event.h"
35 
36 #include <soc.h>
37 #include <zephyr/kernel.h>
38 #include <zephyr/sys/printk.h>
39 #include <zephyr/random/random.h>
40 #include <zephyr/drivers/interrupt_controller/intc_esp32c3.h>
41 #include "zephyr_compat.h"
42 
43 #include <zephyr/logging/log.h>
44 LOG_MODULE_REGISTER(esp32_wifi_adapter, CONFIG_WIFI_LOG_LEVEL);
45 
46 ESP_EVENT_DEFINE_BASE(WIFI_EVENT);
47 
48 static void esp_wifi_free(void *mem);
49 
50 static void *wifi_msgq_buffer;
51 
52 static struct k_thread wifi_task_handle;
53 
54 uint64_t g_wifi_feature_caps =
55 #if CONFIG_ESP_WIFI_ENABLE_WPA3_SAE
56 	CONFIG_FEATURE_WPA3_SAE_BIT |
57 #endif
58 #if CONFIG_SPIRAM
59 	CONFIG_FEATURE_CACHE_TX_BUF_BIT |
60 #endif
61 #if CONFIG_ESP_WIFI_FTM_INITIATOR_SUPPORT
62 	CONFIG_FEATURE_FTM_INITIATOR_BIT |
63 #endif
64 #if CONFIG_ESP_WIFI_FTM_RESPONDER_SUPPORT
65 	CONFIG_FEATURE_FTM_RESPONDER_BIT |
66 #endif
67 0;
68 
wifi_malloc(size_t size)69 IRAM_ATTR void *wifi_malloc(size_t size)
70 {
71 	void *ptr = k_malloc(size);
72 
73 	if (ptr == NULL) {
74 		LOG_ERR("memory allocation failed");
75 	}
76 
77 	return ptr;
78 }
79 
wifi_realloc(void * ptr,size_t size)80 IRAM_ATTR void *wifi_realloc(void *ptr, size_t size)
81 {
82 	ARG_UNUSED(ptr);
83 	ARG_UNUSED(size);
84 
85 	LOG_ERR("%s not yet supported", __func__);
86 	return NULL;
87 }
88 
wifi_calloc(size_t n,size_t size)89 IRAM_ATTR void *wifi_calloc(size_t n, size_t size)
90 {
91 	void *ptr = k_calloc(n, size);
92 
93 	if (ptr == NULL) {
94 		LOG_ERR("memory allocation failed");
95 	}
96 
97 	return ptr;
98 }
99 
wifi_zalloc_wrapper(size_t size)100 static void *IRAM_ATTR wifi_zalloc_wrapper(size_t size)
101 {
102 	return wifi_calloc(1, size);
103 }
104 
wifi_create_queue(int queue_len,int item_size)105 wifi_static_queue_t *wifi_create_queue(int queue_len, int item_size)
106 {
107 	wifi_static_queue_t *queue = NULL;
108 
109 	queue = (wifi_static_queue_t *) wifi_malloc(sizeof(wifi_static_queue_t));
110 	if (!queue) {
111 		LOG_ERR("msg buffer allocation failed");
112 		return NULL;
113 	}
114 
115 	wifi_msgq_buffer = wifi_malloc(queue_len * item_size);
116 	if (wifi_msgq_buffer == NULL) {
117 		LOG_ERR("msg buffer allocation failed");
118 		return NULL;
119 	}
120 
121 	queue->handle = wifi_malloc(sizeof(struct k_msgq));
122 	if (queue->handle == NULL) {
123 		esp_wifi_free(wifi_msgq_buffer);
124 		LOG_ERR("queue handle allocation failed");
125 		return NULL;
126 	}
127 
128 	k_msgq_init((struct k_msgq *)queue->handle, wifi_msgq_buffer, item_size, queue_len);
129 
130 	return queue;
131 }
132 
wifi_delete_queue(wifi_static_queue_t * queue)133 void wifi_delete_queue(wifi_static_queue_t *queue)
134 {
135 	if (queue) {
136 		esp_wifi_free(queue->handle);
137 		esp_wifi_free(queue);
138 	}
139 }
140 
wifi_create_queue_wrapper(int queue_len,int item_size)141 static void *wifi_create_queue_wrapper(int queue_len, int item_size)
142 {
143 	return wifi_create_queue(queue_len, item_size);
144 }
145 
wifi_delete_queue_wrapper(void * queue)146 static void wifi_delete_queue_wrapper(void *queue)
147 {
148 	wifi_delete_queue(queue);
149 }
150 
env_is_chip_wrapper(void)151 static bool IRAM_ATTR env_is_chip_wrapper(void)
152 {
153 #ifdef CONFIG_IDF_ENV_FPGA
154 	return false;
155 #else
156 	return true;
157 #endif
158 }
159 
spin_lock_create_wrapper(void)160 static void *spin_lock_create_wrapper(void)
161 {
162 	unsigned int *wifi_spin_lock = (unsigned int *) wifi_malloc(sizeof(unsigned int));
163 	if (wifi_spin_lock == NULL) {
164 		LOG_ERR("spin_lock_create_wrapper allocation failed");
165 	}
166 
167 	return (void *)wifi_spin_lock;
168 }
169 
wifi_int_disable_wrapper(void * wifi_int_mux)170 static uint32_t IRAM_ATTR wifi_int_disable_wrapper(void *wifi_int_mux)
171 {
172 	unsigned int *int_mux = (unsigned int *) wifi_int_mux;
173 
174 	*int_mux = irq_lock();
175 	return 0;
176 }
177 
wifi_int_restore_wrapper(void * wifi_int_mux,uint32_t tmp)178 static void IRAM_ATTR wifi_int_restore_wrapper(void *wifi_int_mux, uint32_t tmp)
179 {
180 	unsigned int *key = (unsigned int *) wifi_int_mux;
181 
182 	irq_unlock(*key);
183 }
184 
task_yield_from_isr_wrapper(void)185 static void IRAM_ATTR task_yield_from_isr_wrapper(void)
186 {
187 	k_yield();
188 }
189 
semphr_create_wrapper(uint32_t max,uint32_t init)190 static void *semphr_create_wrapper(uint32_t max, uint32_t init)
191 {
192 	struct k_sem *sem = (struct k_sem *) wifi_malloc(sizeof(struct k_sem));
193 
194 	if (sem == NULL) {
195 		LOG_ERR("semphr_create_wrapper allocation failed");
196 	}
197 
198 	k_sem_init(sem, init, max);
199 	return (void *) sem;
200 }
201 
semphr_delete_wrapper(void * semphr)202 static void semphr_delete_wrapper(void *semphr)
203 {
204 	esp_wifi_free(semphr);
205 }
206 
wifi_thread_semphr_get_wrapper(void)207 static void *wifi_thread_semphr_get_wrapper(void)
208 {
209 	struct k_sem *sem = NULL;
210 
211 	sem = k_thread_custom_data_get();
212 	if (!sem) {
213 		sem = (struct k_sem *) wifi_malloc(sizeof(struct k_sem));
214 		if (sem == NULL) {
215 			LOG_ERR("wifi_thread_semphr_get_wrapper allocation failed");
216 		}
217 		k_sem_init(sem, 0, 1);
218 		if (sem) {
219 			k_thread_custom_data_set(sem);
220 		}
221 	}
222 	return (void *)sem;
223 }
224 
semphr_take_wrapper(void * semphr,uint32_t block_time_tick)225 static int32_t semphr_take_wrapper(void *semphr, uint32_t block_time_tick)
226 {
227 	if (block_time_tick == OSI_FUNCS_TIME_BLOCKING) {
228 		int ret = k_sem_take((struct k_sem *)semphr, K_FOREVER);
229 
230 		if (ret == 0) {
231 			return 1;
232 		}
233 	} else {
234 		int ret = k_sem_take((struct k_sem *)semphr, K_TICKS(block_time_tick));
235 
236 		if (ret == 0) {
237 			return 1;
238 		}
239 	}
240 	return 0;
241 }
242 
semphr_give_wrapper(void * semphr)243 static int32_t semphr_give_wrapper(void *semphr)
244 {
245 	k_sem_give((struct k_sem *) semphr);
246 	return 1;
247 }
248 
recursive_mutex_create_wrapper(void)249 static void *recursive_mutex_create_wrapper(void)
250 {
251 	struct k_mutex *my_mutex = (struct k_mutex *) wifi_malloc(sizeof(struct k_mutex));
252 
253 	if (my_mutex == NULL) {
254 		LOG_ERR("recursive_mutex_create_wrapper allocation failed");
255 	}
256 
257 	k_mutex_init(my_mutex);
258 
259 	return (void *)my_mutex;
260 }
261 
mutex_create_wrapper(void)262 static void *mutex_create_wrapper(void)
263 {
264 	struct k_mutex *my_mutex = (struct k_mutex *) wifi_malloc(sizeof(struct k_mutex));
265 
266 	if (my_mutex == NULL) {
267 		LOG_ERR("mutex_create_wrapper allocation failed");
268 	}
269 
270 	k_mutex_init(my_mutex);
271 
272 	return (void *)my_mutex;
273 }
274 
mutex_delete_wrapper(void * mutex)275 static void mutex_delete_wrapper(void *mutex)
276 {
277 	esp_wifi_free(mutex);
278 }
279 
mutex_lock_wrapper(void * mutex)280 static int32_t IRAM_ATTR mutex_lock_wrapper(void *mutex)
281 {
282 	struct k_mutex *my_mutex = (struct k_mutex *) mutex;
283 
284 	k_mutex_lock(my_mutex, K_FOREVER);
285 	return 0;
286 }
287 
mutex_unlock_wrapper(void * mutex)288 static int32_t IRAM_ATTR mutex_unlock_wrapper(void *mutex)
289 {
290 	struct k_mutex *my_mutex = (struct k_mutex *) mutex;
291 
292 	k_mutex_unlock(my_mutex);
293 	return 0;
294 }
295 
queue_create_wrapper(uint32_t queue_len,uint32_t item_size)296 static void *queue_create_wrapper(uint32_t queue_len, uint32_t item_size)
297 {
298 	struct k_queue *queue = (struct k_queue *) wifi_malloc(sizeof(struct k_queue));
299 
300 	if (queue == NULL) {
301 		LOG_ERR("queue malloc failed");
302 		return NULL;
303 	}
304 
305 	k_msgq_init((struct k_msgq *)queue, wifi_msgq_buffer, item_size, queue_len);
306 
307 	return (void *)queue;
308 }
309 
queue_delete_wrapper(void * handle)310 static void queue_delete_wrapper(void *handle)
311 {
312 	if (handle != NULL) {
313 		esp_wifi_free(handle);
314 	}
315 }
316 
task_delete_wrapper(void * handle)317 static void task_delete_wrapper(void *handle)
318 {
319 	if (handle != NULL) {
320 		k_thread_abort((k_tid_t) handle);
321 	}
322 
323 	k_object_release(&wifi_task_handle);
324 }
325 
queue_send_wrapper(void * queue,void * item,uint32_t block_time_tick)326 static int32_t queue_send_wrapper(void *queue, void *item, uint32_t block_time_tick)
327 {
328 	if (block_time_tick == OSI_FUNCS_TIME_BLOCKING) {
329 		k_msgq_put((struct k_msgq *)queue, item, K_FOREVER);
330 	} else {
331 		k_msgq_put((struct k_msgq *)queue, item, K_TICKS(block_time_tick));
332 	}
333 	return 1;
334 }
335 
queue_send_from_isr_wrapper(void * queue,void * item,void * hptw)336 static int32_t IRAM_ATTR queue_send_from_isr_wrapper(void *queue, void *item, void *hptw)
337 {
338 	int *hpt = (int *) hptw;
339 
340 	k_msgq_put((struct k_msgq *)queue, item, K_NO_WAIT);
341 	*hpt = 0;
342 	return 1;
343 }
344 
queue_send_to_back_wrapper(void * queue,void * item,uint32_t block_time_tick)345 int32_t queue_send_to_back_wrapper(void *queue, void *item, uint32_t block_time_tick)
346 {
347 	ARG_UNUSED(queue);
348 	ARG_UNUSED(item);
349 	ARG_UNUSED(block_time_tick);
350 
351 	return 0;
352 }
353 
queue_send_to_front_wrapper(void * queue,void * item,uint32_t block_time_tick)354 int32_t queue_send_to_front_wrapper(void *queue, void *item, uint32_t block_time_tick)
355 {
356 	ARG_UNUSED(queue);
357 	ARG_UNUSED(item);
358 	ARG_UNUSED(block_time_tick);
359 
360 	return 0;
361 }
362 
queue_recv_wrapper(void * queue,void * item,uint32_t block_time_tick)363 static int32_t queue_recv_wrapper(void *queue, void *item, uint32_t block_time_tick)
364 {
365 	if (block_time_tick == OSI_FUNCS_TIME_BLOCKING) {
366 		k_msgq_get((struct k_msgq *)queue, item, K_FOREVER);
367 	} else {
368 		k_msgq_get((struct k_msgq *)queue, item, K_MSEC(block_time_tick));
369 	}
370 	return 1;
371 }
372 
event_group_wait_bits_wrapper(void * event,uint32_t bits_to_wait_for,int clear_on_exit,int wait_for_all_bits,uint32_t block_time_tick)373 static uint32_t event_group_wait_bits_wrapper(void *event, uint32_t bits_to_wait_for, int clear_on_exit, int wait_for_all_bits, uint32_t block_time_tick)
374 {
375 	ARG_UNUSED(event);
376 	ARG_UNUSED(bits_to_wait_for);
377 	ARG_UNUSED(clear_on_exit);
378 	ARG_UNUSED(wait_for_all_bits);
379 	ARG_UNUSED(block_time_tick);
380 
381 	return 0;
382 }
383 
task_create_pinned_to_core_wrapper(void * task_func,const char * name,uint32_t stack_depth,void * param,uint32_t prio,void * task_handle,uint32_t core_id)384 static int32_t task_create_pinned_to_core_wrapper(void *task_func, const char *name, uint32_t stack_depth, void *param, uint32_t prio, void *task_handle, uint32_t core_id)
385 {
386 	k_thread_stack_t *wifi_stack = k_thread_stack_alloc(stack_depth,
387 									IS_ENABLED(CONFIG_USERSPACE) ? K_USER : 0);
388 
389 	k_tid_t tid = k_thread_create(&wifi_task_handle, wifi_stack, stack_depth,
390 				      (k_thread_entry_t)task_func, param, NULL, NULL,
391 				      prio, K_INHERIT_PERMS, K_NO_WAIT);
392 
393 	k_thread_name_set(tid, name);
394 
395 	*(int32_t *)task_handle = (int32_t) tid;
396 	return 1;
397 }
398 
task_create_wrapper(void * task_func,const char * name,uint32_t stack_depth,void * param,uint32_t prio,void * task_handle)399 static int32_t task_create_wrapper(void *task_func, const char *name, uint32_t stack_depth, void *param, uint32_t prio, void *task_handle)
400 {
401 	k_thread_stack_t *wifi_stack = k_thread_stack_alloc(stack_depth,
402 									IS_ENABLED(CONFIG_USERSPACE) ? K_USER : 0);
403 
404 	k_tid_t tid = k_thread_create(&wifi_task_handle, wifi_stack, stack_depth,
405 				      (k_thread_entry_t)task_func, param, NULL, NULL,
406 				      prio, K_INHERIT_PERMS, K_NO_WAIT);
407 
408 	k_thread_name_set(tid, name);
409 
410 	*(int32_t *)task_handle = (int32_t) tid;
411 	return 1;
412 }
413 
task_ms_to_tick_wrapper(uint32_t ms)414 static int32_t IRAM_ATTR task_ms_to_tick_wrapper(uint32_t ms)
415 {
416 	return (int32_t)(k_ms_to_ticks_ceil32(ms));
417 }
418 
task_get_max_priority_wrapper(void)419 static int32_t task_get_max_priority_wrapper(void)
420 {
421 	return (int32_t)(4);
422 }
423 
esp_event_post_wrapper(const char * event_base,int32_t event_id,void * event_data,size_t event_data_size,uint32_t ticks_to_wait)424 static int32_t esp_event_post_wrapper(const char* event_base, int32_t event_id, void* event_data, size_t event_data_size, uint32_t ticks_to_wait)
425 {
426 	esp_wifi_event_handler(event_base, event_id, event_data, event_data_size, ticks_to_wait);
427 	return 0;
428 }
429 
wifi_apb80m_request_wrapper(void)430 static void IRAM_ATTR wifi_apb80m_request_wrapper(void)
431 {
432 #ifdef CONFIG_PM_ENABLE
433 	wifi_apb80m_request();
434 #endif
435 }
436 
wifi_apb80m_release_wrapper(void)437 static void IRAM_ATTR wifi_apb80m_release_wrapper(void)
438 {
439 #ifdef CONFIG_PM_ENABLE
440 	wifi_apb80m_release();
441 #endif
442 }
443 
timer_arm_wrapper(void * timer,uint32_t tmout,bool repeat)444 static void IRAM_ATTR timer_arm_wrapper(void *timer, uint32_t tmout, bool repeat)
445 {
446 	ets_timer_arm(timer, tmout, repeat);
447 }
448 
timer_disarm_wrapper(void * timer)449 static void IRAM_ATTR timer_disarm_wrapper(void *timer)
450 {
451 	ets_timer_disarm(timer);
452 }
453 
timer_done_wrapper(void * ptimer)454 static void timer_done_wrapper(void *ptimer)
455 {
456 	ets_timer_done(ptimer);
457 }
458 
timer_setfn_wrapper(void * ptimer,void * pfunction,void * parg)459 static void timer_setfn_wrapper(void *ptimer, void *pfunction, void *parg)
460 {
461 	ets_timer_setfn(ptimer, pfunction, parg);
462 }
463 
timer_arm_us_wrapper(void * ptimer,uint32_t us,bool repeat)464 static void IRAM_ATTR timer_arm_us_wrapper(void *ptimer, uint32_t us, bool repeat)
465 {
466 	ets_timer_arm_us(ptimer, us, repeat);
467 }
468 
get_time_wrapper(void * t)469 static int get_time_wrapper(void *t)
470 {
471 	return os_get_time(t);
472 }
473 
esp_coex_common_clk_slowclk_cal_get_wrapper(void)474 uint32_t esp_coex_common_clk_slowclk_cal_get_wrapper(void)
475 {
476     /* The bit width of WiFi light sleep clock calibration is 12 while the one of
477      * system is 19. It should shift 19 - 12 = 7.
478     */
479     if (GET_PERI_REG_MASK(SYSTEM_BT_LPCK_DIV_FRAC_REG, SYSTEM_LPCLK_SEL_XTAL)) {
480         uint64_t time_per_us = 1000000ULL;
481         return (((time_per_us << RTC_CLK_CAL_FRACT) / (MHZ(1))) >> (RTC_CLK_CAL_FRACT - SOC_WIFI_LIGHT_SLEEP_CLK_WIDTH));
482     }
483 
484 	return (esp_clk_slowclk_cal_get() >> (RTC_CLK_CAL_FRACT - SOC_WIFI_LIGHT_SLEEP_CLK_WIDTH));
485 
486 }
487 
malloc_internal_wrapper(size_t size)488 static void *IRAM_ATTR malloc_internal_wrapper(size_t size)
489 {
490 	return wifi_malloc(size);
491 }
492 
realloc_internal_wrapper(void * ptr,size_t size)493 static void *IRAM_ATTR realloc_internal_wrapper(void *ptr, size_t size)
494 {
495 	ARG_UNUSED(ptr);
496 	ARG_UNUSED(size);
497 
498 	LOG_ERR("%s not yet supported", __func__);
499 	return NULL;
500 }
501 
calloc_internal_wrapper(size_t n,size_t size)502 static void *IRAM_ATTR calloc_internal_wrapper(size_t n, size_t size)
503 {
504 	return wifi_calloc(n, size);
505 }
506 
zalloc_internal_wrapper(size_t size)507 static void *IRAM_ATTR zalloc_internal_wrapper(size_t size)
508 {
509 	return wifi_calloc(1, size);
510 }
511 
uxQueueMessagesWaiting(void * queue)512 uint32_t uxQueueMessagesWaiting(void *queue)
513 {
514 	ARG_UNUSED(queue);
515 
516 	return 0;
517 }
518 
xEventGroupCreate(void)519 void *xEventGroupCreate(void)
520 {
521 	LOG_ERR("EventGroup not supported!");
522 	return NULL;
523 }
524 
vEventGroupDelete(void * grp)525 void vEventGroupDelete(void *grp)
526 {
527 	ARG_UNUSED(grp);
528 }
529 
xEventGroupSetBits(void * ptr,uint32_t data)530 uint32_t xEventGroupSetBits(void *ptr, uint32_t data)
531 {
532 	ARG_UNUSED(ptr);
533 	ARG_UNUSED(data);
534 
535 	return 0;
536 }
537 
xEventGroupClearBits(void * ptr,uint32_t data)538 uint32_t xEventGroupClearBits(void *ptr, uint32_t data)
539 {
540 	ARG_UNUSED(ptr);
541 	ARG_UNUSED(data);
542 
543 	return 0;
544 }
545 
xTaskGetCurrentTaskHandle(void)546 void *xTaskGetCurrentTaskHandle(void)
547 {
548 	return (void *)k_current_get();
549 }
550 
task_delay(uint32_t ticks)551 void task_delay(uint32_t ticks)
552 {
553 	k_sleep(K_TICKS(ticks));
554 }
555 
set_intr_wrapper(int32_t cpu_no,uint32_t intr_source,uint32_t intr_num,int32_t intr_prio)556 static void set_intr_wrapper(int32_t cpu_no, uint32_t intr_source, uint32_t intr_num, int32_t intr_prio)
557 {
558 	ARG_UNUSED(cpu_no);
559 
560 	intr_matrix_route(intr_source, intr_num);
561 	esprv_intc_int_set_priority(intr_num, intr_prio);
562 	esprv_intc_int_set_type(intr_num, INTR_TYPE_LEVEL);
563 }
564 
clear_intr_wrapper(uint32_t intr_source,uint32_t intr_num)565 static void clear_intr_wrapper(uint32_t intr_source, uint32_t intr_num)
566 {
567 	ARG_UNUSED(intr_source);
568 	ARG_UNUSED(intr_num);
569 }
570 
set_isr_wrapper(int32_t n,void * f,void * arg)571 static void set_isr_wrapper(int32_t n, void *f, void *arg)
572 {
573 	ARG_UNUSED(n);
574 
575 	/* workaround to force allocating same handler for wifi interrupts */
576 	esp_intr_alloc(0, 0, (isr_handler_t)f, arg, NULL);
577 	esp_intr_alloc(2, 0, (isr_handler_t)f, arg,	NULL);
578 }
579 
enable_intr_wrapper(unsigned int mask)580 static void enable_intr_wrapper(unsigned int mask)
581 {
582 	esp_intr_enable(mask);
583 }
584 
disable_intr_wrapper(unsigned int mask)585 static void disable_intr_wrapper(unsigned int mask)
586 {
587 	esp_intr_disable(mask);
588 }
589 
esp_get_free_heap_size(void)590 uint32_t esp_get_free_heap_size(void)
591 {
592 	/* FIXME: API to get free heap size is not available in Zephyr. */
593 	/* It is only used by ESP-MESH feature (not supported yet) */
594 	return 10000;
595 }
596 
wifi_clock_enable_wrapper(void)597 static void wifi_clock_enable_wrapper(void)
598 {
599 	wifi_module_enable();
600 }
601 
wifi_clock_disable_wrapper(void)602 static void wifi_clock_disable_wrapper(void)
603 {
604 	wifi_module_disable();
605 }
606 
wifi_reset_mac_wrapper(void)607 static void wifi_reset_mac_wrapper(void)
608 {
609     periph_module_reset(PERIPH_WIFI_MODULE);
610 }
611 
nvs_set_i8(uint32_t handle,const char * key,int8_t value)612 int32_t nvs_set_i8(uint32_t handle, const char *key, int8_t value)
613 {
614 	ARG_UNUSED(handle);
615 	ARG_UNUSED(key);
616 	ARG_UNUSED(value);
617 
618 	return 0;
619 }
620 
nvs_get_i8(uint32_t handle,const char * key,int8_t * out_value)621 int32_t nvs_get_i8(uint32_t handle, const char *key, int8_t *out_value)
622 {
623 	ARG_UNUSED(handle);
624 	ARG_UNUSED(key);
625 	ARG_UNUSED(out_value);
626 
627 	return 0;
628 }
629 
nvs_set_u8(uint32_t handle,const char * key,uint8_t value)630 int32_t nvs_set_u8(uint32_t handle, const char *key, uint8_t value)
631 {
632 	ARG_UNUSED(handle);
633 	ARG_UNUSED(key);
634 	ARG_UNUSED(value);
635 
636 	return 0;
637 }
638 
nvs_get_u8(uint32_t handle,const char * key,uint8_t * out_value)639 int32_t nvs_get_u8(uint32_t handle, const char *key, uint8_t *out_value)
640 {
641 	ARG_UNUSED(handle);
642 	ARG_UNUSED(key);
643 	ARG_UNUSED(out_value);
644 
645 	return 0;
646 }
647 
nvs_set_u16(uint32_t handle,const char * key,uint16_t value)648 int32_t nvs_set_u16(uint32_t handle, const char *key, uint16_t value)
649 {
650 	ARG_UNUSED(handle);
651 	ARG_UNUSED(key);
652 	ARG_UNUSED(value);
653 
654 	return 0;
655 }
656 
nvs_get_u16(uint32_t handle,const char * key,uint16_t * out_value)657 int32_t nvs_get_u16(uint32_t handle, const char *key, uint16_t *out_value)
658 {
659 	ARG_UNUSED(handle);
660 	ARG_UNUSED(key);
661 	ARG_UNUSED(out_value);
662 
663 	return 0;
664 }
665 
nvs_open_wrapper(const char * name,uint32_t open_mode,uint32_t * out_handle)666 int32_t nvs_open_wrapper(const char *name, uint32_t open_mode, uint32_t *out_handle)
667 {
668 	ARG_UNUSED(name);
669 	ARG_UNUSED(open_mode);
670 	ARG_UNUSED(out_handle);
671 
672 	return 0;
673 }
674 
nvs_close(uint32_t handle)675 void nvs_close(uint32_t handle)
676 {
677 	ARG_UNUSED(handle);
678 
679 	return;
680 }
681 
nvs_commit(uint32_t handle)682 int32_t nvs_commit(uint32_t handle)
683 {
684 	ARG_UNUSED(handle);
685 
686 	return 0;
687 }
688 
nvs_set_blob(uint32_t handle,const char * key,const void * value,size_t length)689 int32_t nvs_set_blob(uint32_t handle, const char *key, const void *value,
690 		     size_t length)
691 {
692 	ARG_UNUSED(handle);
693 	ARG_UNUSED(key);
694 	ARG_UNUSED(value);
695 	ARG_UNUSED(length);
696 
697 	return 0;
698 }
699 
nvs_get_blob(uint32_t handle,const char * key,void * out_value,size_t * length)700 int32_t nvs_get_blob(uint32_t handle, const char *key, void *out_value,
701 		     size_t *length)
702 {
703 	ARG_UNUSED(handle);
704 	ARG_UNUSED(key);
705 	ARG_UNUSED(out_value);
706 	ARG_UNUSED(length);
707 
708 	return 0;
709 }
710 
nvs_erase_key(uint32_t handle,const char * key)711 int32_t nvs_erase_key(uint32_t handle, const char *key)
712 {
713 	ARG_UNUSED(handle);
714 	ARG_UNUSED(key);
715 
716 	return 0;
717 }
718 
coex_init_wrapper(void)719 static int coex_init_wrapper(void)
720 {
721 #if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE
722 	return coex_init();
723 #else
724 	return 0;
725 #endif
726 }
727 
coex_deinit_wrapper(void)728 static void coex_deinit_wrapper(void)
729 {
730 #if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE
731 	coex_deinit();
732 #endif
733 }
734 
coex_enable_wrapper(void)735 static int coex_enable_wrapper(void)
736 {
737 #if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE
738 	return coex_enable();
739 #else
740 	return 0;
741 #endif
742 }
743 
coex_disable_wrapper(void)744 static void coex_disable_wrapper(void)
745 {
746 #if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE
747 	coex_disable();
748 #endif
749 }
750 
coex_status_get_wrapper(void)751 static IRAM_ATTR uint32_t coex_status_get_wrapper(void)
752 {
753 #if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE
754 	return coex_status_get();
755 #else
756 	return 0;
757 #endif
758 }
759 
coex_wifi_request_wrapper(uint32_t event,uint32_t latency,uint32_t duration)760 static int coex_wifi_request_wrapper(uint32_t event, uint32_t latency, uint32_t duration)
761 {
762 #if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE
763 	return coex_wifi_request(event, latency, duration);
764 #else
765 	return 0;
766 #endif
767 }
768 
coex_wifi_release_wrapper(uint32_t event)769 static IRAM_ATTR int coex_wifi_release_wrapper(uint32_t event)
770 {
771 #if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE
772 	return coex_wifi_release(event);
773 #else
774 	return 0;
775 #endif
776 }
777 
coex_wifi_channel_set_wrapper(uint8_t primary,uint8_t secondary)778 static int coex_wifi_channel_set_wrapper(uint8_t primary, uint8_t secondary)
779 {
780 #if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE
781 	return coex_wifi_channel_set(primary, secondary);
782 #else
783 	return 0;
784 #endif
785 }
786 
coex_event_duration_get_wrapper(uint32_t event,uint32_t * duration)787 static IRAM_ATTR int coex_event_duration_get_wrapper(uint32_t event, uint32_t *duration)
788 {
789 #if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE
790 	return coex_event_duration_get(event, duration);
791 #else
792 	return 0;
793 #endif
794 }
795 
coex_pti_get_wrapper(uint32_t event,uint8_t * pti)796 static int coex_pti_get_wrapper(uint32_t event, uint8_t *pti)
797 {
798 	ARG_UNUSED(event);
799 	ARG_UNUSED(pti);
800 
801 	return 0;
802 }
803 
coex_schm_status_bit_clear_wrapper(uint32_t type,uint32_t status)804 static void coex_schm_status_bit_clear_wrapper(uint32_t type, uint32_t status)
805 {
806 #if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE
807 	coex_schm_status_bit_clear(type, status);
808 #endif
809 }
810 
coex_schm_status_bit_set_wrapper(uint32_t type,uint32_t status)811 static void coex_schm_status_bit_set_wrapper(uint32_t type, uint32_t status)
812 {
813 #if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE
814 	coex_schm_status_bit_set(type, status);
815 #endif
816 }
817 
coex_schm_interval_set_wrapper(uint32_t interval)818 static IRAM_ATTR int coex_schm_interval_set_wrapper(uint32_t interval)
819 {
820 #if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE
821 	return coex_schm_interval_set(interval);
822 #else
823 	return 0;
824 #endif
825 }
826 
coex_schm_interval_get_wrapper(void)827 static uint32_t coex_schm_interval_get_wrapper(void)
828 {
829 #if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE
830 	return coex_schm_interval_get();
831 #else
832 	return 0;
833 #endif
834 }
835 
coex_schm_curr_period_get_wrapper(void)836 static uint8_t coex_schm_curr_period_get_wrapper(void)
837 {
838 #if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE
839 	return coex_schm_curr_period_get();
840 #else
841 	return 0;
842 #endif
843 }
844 
coex_schm_curr_phase_get_wrapper(void)845 static void * coex_schm_curr_phase_get_wrapper(void)
846 {
847 #if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE
848 	return coex_schm_curr_phase_get();
849 #else
850 	return NULL;
851 #endif
852 }
853 
esp_empty_wrapper(void)854 static void IRAM_ATTR esp_empty_wrapper(void)
855 {
856 
857 }
858 
coex_is_in_isr_wrapper(void)859 int32_t IRAM_ATTR coex_is_in_isr_wrapper(void)
860 {
861 	return !k_is_in_isr();
862 }
863 
esp_log_writev_wrapper(uint32_t level,const char * tag,const char * format,va_list args)864 static void esp_log_writev_wrapper(uint32_t level, const char *tag, const char *format, va_list args)
865 {
866 #if CONFIG_WIFI_LOG_LEVEL >= LOG_LEVEL_DBG
867 	esp_log_writev((esp_log_level_t)level,tag,format,args);
868 #endif
869 }
870 
esp_log_write_wrapper(uint32_t level,const char * tag,const char * format,...)871 static void esp_log_write_wrapper(uint32_t level,const char *tag,const char *format, ...)
872 {
873 #if CONFIG_WIFI_LOG_LEVEL >= LOG_LEVEL_DBG
874 	va_list list;
875 	va_start(list, format);
876 	esp_log_writev((esp_log_level_t)level, tag, format, list);
877 	va_end(list);
878 #endif
879 }
880 
coex_register_start_cb_wrapper(int (* cb)(void))881 static int coex_register_start_cb_wrapper(int (* cb)(void))
882 {
883 #if CONFIG_SW_COEXIST_ENABLE
884     return coex_register_start_cb(cb);
885 #else
886     return 0;
887 #endif
888 }
889 
coex_schm_process_restart_wrapper(void)890 static int coex_schm_process_restart_wrapper(void)
891 {
892 #if CONFIG_SW_COEXIST_ENABLE
893     return coex_schm_process_restart();
894 #else
895     return 0;
896 #endif
897 }
898 
coex_schm_register_cb_wrapper(int type,int (* cb)(int))899 static int coex_schm_register_cb_wrapper(int type, int(*cb)(int))
900 {
901 #if CONFIG_SW_COEXIST_ENABLE
902     return coex_schm_register_callback(type, cb);
903 #else
904     return 0;
905 #endif
906 }
907 
908 wifi_osi_funcs_t g_wifi_osi_funcs = {
909 	._version = ESP_WIFI_OS_ADAPTER_VERSION,
910 	._env_is_chip = env_is_chip_wrapper,
911 	._set_intr = set_intr_wrapper,
912 	._clear_intr = clear_intr_wrapper,
913 	._set_isr = set_isr_wrapper,
914 	._ints_on = enable_intr_wrapper,
915 	._ints_off = disable_intr_wrapper,
916 	._is_from_isr = k_is_in_isr,
917 	._spin_lock_create = spin_lock_create_wrapper,
918 	._spin_lock_delete = esp_wifi_free,
919 	._wifi_int_disable = wifi_int_disable_wrapper,
920 	._wifi_int_restore = wifi_int_restore_wrapper,
921 	._task_yield_from_isr = task_yield_from_isr_wrapper,
922 	._semphr_create = semphr_create_wrapper,
923 	._semphr_delete = semphr_delete_wrapper,
924 	._semphr_take = semphr_take_wrapper,
925 	._semphr_give = semphr_give_wrapper,
926 	._wifi_thread_semphr_get = wifi_thread_semphr_get_wrapper,
927 	._mutex_create = mutex_create_wrapper,
928 	._recursive_mutex_create = recursive_mutex_create_wrapper,
929 	._mutex_delete = mutex_delete_wrapper,
930 	._mutex_lock = mutex_lock_wrapper,
931 	._mutex_unlock = mutex_unlock_wrapper,
932 	._queue_create = queue_create_wrapper,
933 	._queue_delete = queue_delete_wrapper,
934 	._queue_send = queue_send_wrapper,
935 	._queue_send_from_isr = queue_send_from_isr_wrapper,
936 	._queue_send_to_back = queue_send_to_back_wrapper,
937 	._queue_send_to_front = queue_send_to_front_wrapper,
938 	._queue_recv = queue_recv_wrapper,
939 	._queue_msg_waiting = (uint32_t(*)(void *))uxQueueMessagesWaiting,
940 	._event_group_create = (void *(*)(void))xEventGroupCreate,
941 	._event_group_delete = (void(*)(void *))vEventGroupDelete,
942 	._event_group_set_bits = (uint32_t(*)(void *,uint32_t))xEventGroupSetBits,
943 	._event_group_clear_bits = (uint32_t(*)(void *,uint32_t))xEventGroupClearBits,
944 	._event_group_wait_bits = event_group_wait_bits_wrapper,
945 	._task_create_pinned_to_core = task_create_pinned_to_core_wrapper,
946 	._task_create = task_create_wrapper,
947 	._task_delete = task_delete_wrapper,
948 	._task_delay = task_delay,
949 	._task_ms_to_tick = task_ms_to_tick_wrapper,
950 	._task_get_current_task = (void *(*)(void))k_current_get,
951 	._task_get_max_priority = task_get_max_priority_wrapper,
952 	._malloc = wifi_malloc,
953 	._free = esp_wifi_free,
954 	._event_post = esp_event_post_wrapper,
955 	._get_free_heap_size = esp_get_free_heap_size,
956 	._rand = sys_rand32_get,
957 	._dport_access_stall_other_cpu_start_wrap = esp_empty_wrapper,
958 	._dport_access_stall_other_cpu_end_wrap = esp_empty_wrapper,
959 	._wifi_apb80m_request = wifi_apb80m_request_wrapper,
960 	._wifi_apb80m_release = wifi_apb80m_release_wrapper,
961 	._phy_disable = esp_phy_disable,
962 	._phy_enable = esp_phy_enable,
963 	._phy_update_country_info = esp_phy_update_country_info,
964 	._read_mac = esp_read_mac,
965 	._timer_arm = timer_arm_wrapper,
966 	._timer_disarm = timer_disarm_wrapper,
967 	._timer_done = timer_done_wrapper,
968 	._timer_setfn = timer_setfn_wrapper,
969 	._timer_arm_us = timer_arm_us_wrapper,
970 	._wifi_reset_mac = wifi_reset_mac_wrapper,
971 	._wifi_clock_enable = wifi_clock_enable_wrapper,
972 	._wifi_clock_disable = wifi_clock_disable_wrapper,
973 	._wifi_rtc_enable_iso = esp_empty_wrapper,
974 	._wifi_rtc_disable_iso = esp_empty_wrapper,
975 	._esp_timer_get_time = esp_timer_get_time,
976 	._nvs_set_i8 = nvs_set_i8,
977 	._nvs_get_i8 = nvs_get_i8,
978 	._nvs_set_u8 = nvs_set_u8,
979 	._nvs_get_u8 = nvs_get_u8,
980 	._nvs_set_u16 = nvs_set_u16,
981 	._nvs_get_u16 = nvs_get_u16,
982 	._nvs_open = nvs_open_wrapper,
983 	._nvs_close = nvs_close,
984 	._nvs_commit = nvs_commit,
985 	._nvs_set_blob = nvs_set_blob,
986 	._nvs_get_blob = nvs_get_blob,
987 	._nvs_erase_key = nvs_erase_key,
988 	._get_random = os_get_random,
989 	._get_time = get_time_wrapper,
990 	._random = os_random,
991 	._slowclk_cal_get = esp_coex_common_clk_slowclk_cal_get_wrapper,
992 	._log_write = esp_log_write_wrapper,
993 	._log_writev = esp_log_writev_wrapper,
994 	._log_timestamp = k_uptime_get_32,
995 	._malloc_internal =  malloc_internal_wrapper,
996 	._realloc_internal = realloc_internal_wrapper,
997 	._calloc_internal = calloc_internal_wrapper,
998 	._zalloc_internal = zalloc_internal_wrapper,
999 	._wifi_malloc = wifi_malloc,
1000 	._wifi_realloc = wifi_realloc,
1001 	._wifi_calloc = wifi_calloc,
1002 	._wifi_zalloc = wifi_zalloc_wrapper,
1003 	._wifi_create_queue = wifi_create_queue_wrapper,
1004 	._wifi_delete_queue = wifi_delete_queue_wrapper,
1005 	._coex_init = coex_init_wrapper,
1006 	._coex_deinit = coex_deinit_wrapper,
1007 	._coex_enable = coex_enable_wrapper,
1008 	._coex_disable = coex_disable_wrapper,
1009 	._coex_status_get = coex_status_get_wrapper,
1010 	._coex_wifi_request = coex_wifi_request_wrapper,
1011 	._coex_wifi_release = coex_wifi_release_wrapper,
1012 	._coex_wifi_channel_set = coex_wifi_channel_set_wrapper,
1013 	._coex_event_duration_get = coex_event_duration_get_wrapper,
1014 	._coex_pti_get = coex_pti_get_wrapper,
1015 	._coex_schm_status_bit_clear = coex_schm_status_bit_clear_wrapper,
1016 	._coex_schm_status_bit_set = coex_schm_status_bit_set_wrapper,
1017 	._coex_schm_interval_set = coex_schm_interval_set_wrapper,
1018 	._coex_schm_interval_get = coex_schm_interval_get_wrapper,
1019 	._coex_schm_curr_period_get = coex_schm_curr_period_get_wrapper,
1020 	._coex_schm_curr_phase_get = coex_schm_curr_phase_get_wrapper,
1021 	._coex_register_start_cb = coex_register_start_cb_wrapper,
1022 	._coex_schm_process_restart = coex_schm_process_restart_wrapper,
1023 	._coex_schm_register_cb = coex_schm_register_cb_wrapper,
1024 	._magic = ESP_WIFI_OS_ADAPTER_MAGIC,
1025 };
1026 
esp_wifi_deinit(void)1027 esp_err_t esp_wifi_deinit(void)
1028 {
1029 	esp_err_t err = ESP_OK;
1030 
1031 	esp_supplicant_deinit();
1032 	err = esp_wifi_deinit_internal();
1033 
1034 	return err;
1035 }
1036 
esp_wifi_init(const wifi_init_config_t * config)1037 esp_err_t esp_wifi_init(const wifi_init_config_t *config)
1038 {
1039 	esp_wifi_power_domain_on();
1040 
1041 #if CONFIG_ESP32_WIFI_SW_COEXIST_ENABLE
1042 	coex_init();
1043 #endif
1044 
1045     esp_wifi_internal_set_log_level(CONFIG_WIFI_LOG_LEVEL);
1046 
1047 	esp_err_t result = esp_wifi_init_internal(config);
1048 	if (result == ESP_OK) {
1049 		result = esp_supplicant_init();
1050 		if (result != ESP_OK) {
1051 		LOG_ERR("Failed to init supplicant (0x%x)", result);
1052 			esp_wifi_deinit();
1053 		}
1054 	}
1055 
1056 	return result;
1057 }
1058 
esp_wifi_free(void * mem)1059 static void esp_wifi_free(void *mem)
1060 {
1061 	k_free(mem);
1062 }
1063