1 /*
2  * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/sys/printk.h>
9 #include <zephyr/random/random.h>
10 
11 #define CONFIG_POSIX_FS
12 
13 #include "esp_wifi.h"
14 #include "stdlib.h"
15 #include "string.h"
16 #include "esp_private/wifi.h"
17 #include "soc/dport_access.h"
18 #include "soc/syscon_reg.h"
19 #include "soc/soc_caps.h"
20 #include "esp_log.h"
21 #include "esp_attr.h"
22 #include "esp_private/wifi_os_adapter.h"
23 #include "private/esp_coexist_internal.h"
24 #include "private/esp_modem_wrapper.h"
25 #include "esp_system.h"
26 #include "esp_timer.h"
27 #include "os.h"
28 #include "esp_event.h"
29 #include "esp_wpa.h"
30 #include "esp_private/periph_ctrl.h"
31 #include "esp_private/adc_share_hw_ctrl.h"
32 #include "soc/rtc_cntl_reg.h"
33 #include "soc/rtc.h"
34 #include "esp_phy_init.h"
35 #include "soc/dport_reg.h"
36 #include "esp32s3/rom/ets_sys.h"
37 #include "esp_mac.h"
38 #include "wifi/wifi_event.h"
39 #include "esp_private/esp_clk.h"
40 #include "esp_heap_adapter.h"
41 #include <zephyr/drivers/interrupt_controller/intc_esp32.h>
42 
43 #include <zephyr/logging/log.h>
44 LOG_MODULE_REGISTER(esp32_wifi_adapter, CONFIG_WIFI_LOG_LEVEL);
45 
46 K_THREAD_STACK_DEFINE(wifi_stack, 8192);
47 
48 static void *wifi_msgq_buffer;
49 
50 static struct k_thread wifi_task_handle;
51 
52 static void esp_wifi_free(void *mem);
53 
wifi_malloc(size_t size)54 IRAM_ATTR void *wifi_malloc(size_t size)
55 {
56 	void *ptr = esp_wifi_malloc_func(size);
57 
58 	if (ptr == NULL) {
59 		LOG_ERR("memory allocation failed");
60 	}
61 
62 	return ptr;
63 }
64 
wifi_realloc(void * ptr,size_t size)65 IRAM_ATTR void *wifi_realloc(void *ptr, size_t size)
66 {
67 	LOG_ERR("%s not yet supported", __func__);
68 	return NULL;
69 }
70 
wifi_calloc(size_t n,size_t size)71 IRAM_ATTR void *wifi_calloc(size_t n, size_t size)
72 {
73 	void *ptr = esp_wifi_calloc_func(n, size);
74 
75 	if (ptr == NULL) {
76 		LOG_ERR("memory allocation failed");
77 	}
78 
79 	return ptr;
80 }
81 
wifi_zalloc_wrapper(size_t size)82 static void *IRAM_ATTR wifi_zalloc_wrapper(size_t size)
83 {
84 	void *ptr = wifi_malloc(size);
85 
86 	if (ptr) {
87 		memset(ptr, 0, size);
88 	}
89 
90 	return ptr;
91 }
92 
esp_wifi_free(void * mem)93 static void esp_wifi_free(void *mem)
94 {
95 	esp_wifi_free_func(mem);
96 }
97 
wifi_create_queue(int queue_len,int item_size)98 wifi_static_queue_t *wifi_create_queue(int queue_len, int item_size)
99 {
100 	wifi_static_queue_t *queue = NULL;
101 
102 	queue = (wifi_static_queue_t *) wifi_malloc(sizeof(wifi_static_queue_t));
103 	if (!queue) {
104 		LOG_ERR("msg buffer allocation failed");
105 		return NULL;
106 	}
107 
108 	wifi_msgq_buffer = wifi_malloc(queue_len * item_size);
109 	if (wifi_msgq_buffer == NULL) {
110 		LOG_ERR("msg buffer allocation failed");
111 		return NULL;
112 	}
113 
114 	queue->handle = wifi_malloc(sizeof(struct k_msgq));
115 	if (queue->handle == NULL) {
116 		esp_wifi_free(wifi_msgq_buffer);
117 		LOG_ERR("queue handle allocation failed");
118 		return NULL;
119 	}
120 
121 	k_msgq_init((struct k_msgq *)queue->handle, wifi_msgq_buffer, item_size, queue_len);
122 
123 	return queue;
124 }
125 
wifi_delete_queue(wifi_static_queue_t * queue)126 void wifi_delete_queue(wifi_static_queue_t *queue)
127 {
128 	if (queue) {
129 		esp_wifi_free(queue->handle);
130 		esp_wifi_free(queue);
131 	}
132 }
133 
wifi_create_queue_wrapper(int queue_len,int item_size)134 static void *wifi_create_queue_wrapper(int queue_len, int item_size)
135 {
136 	return wifi_create_queue(queue_len, item_size);
137 }
138 
wifi_delete_queue_wrapper(void * queue)139 static void wifi_delete_queue_wrapper(void *queue)
140 {
141 	wifi_delete_queue(queue);
142 }
143 
set_intr_wrapper(int32_t cpu_no,uint32_t intr_source,uint32_t intr_num,int32_t intr_prio)144 static void set_intr_wrapper(int32_t cpu_no, uint32_t intr_source, uint32_t intr_num, int32_t intr_prio)
145 {
146 	intr_matrix_set(cpu_no, intr_source, intr_num);
147 }
148 
clear_intr_wrapper(uint32_t intr_source,uint32_t intr_num)149 static void clear_intr_wrapper(uint32_t intr_source, uint32_t intr_num)
150 {
151 
152 }
153 
set_isr_wrapper(int32_t n,void * f,void * arg)154 static void set_isr_wrapper(int32_t n, void *f, void *arg)
155 {
156 	ARG_UNUSED(n);
157 
158 	esp_intr_alloc(0, 0, f, arg, NULL);
159 	esp_intr_alloc(2, 0, f, arg, NULL);
160 }
161 
intr_on(unsigned int mask)162 static void intr_on(unsigned int mask)
163 {
164 	irq_enable(0);
165 }
166 
intr_off(unsigned int mask)167 static void intr_off(unsigned int mask)
168 {
169 	irq_disable(0);
170 }
171 
wifi_thread_semphr_get_wrapper(void)172 static void *wifi_thread_semphr_get_wrapper(void)
173 {
174 	struct k_sem *sem = NULL;
175 
176 	sem = k_thread_custom_data_get();
177 	if (!sem) {
178 		sem = (struct k_sem *) wifi_malloc(sizeof(struct k_sem));
179 		if (sem == NULL) {
180 			LOG_ERR("wifi_thread_semphr_get_wrapper allocation failed");
181 		}
182 		k_sem_init(sem, 0, 1);
183 		if (sem) {
184 			k_thread_custom_data_set(sem);
185 		}
186 	}
187 	return (void *)sem;
188 }
189 
recursive_mutex_create_wrapper(void)190 static void *recursive_mutex_create_wrapper(void)
191 {
192 	struct k_mutex *my_mutex = (struct k_mutex *) wifi_malloc(sizeof(struct k_mutex));
193 
194 	if (my_mutex == NULL) {
195 		LOG_ERR("recursive_mutex_create_wrapper allocation failed");
196 	}
197 
198 	k_mutex_init(my_mutex);
199 
200 	return (void *)my_mutex;
201 }
202 
mutex_create_wrapper(void)203 static void *mutex_create_wrapper(void)
204 {
205 	struct k_mutex *my_mutex = (struct k_mutex *) wifi_malloc(sizeof(struct k_mutex));
206 
207 	if (my_mutex == NULL) {
208 		LOG_ERR("recursive_mutex_create_wrapper allocation failed");
209 	}
210 
211 	k_mutex_init(my_mutex);
212 
213 	return (void *)my_mutex;
214 }
215 
mutex_delete_wrapper(void * mutex)216 static void mutex_delete_wrapper(void *mutex)
217 {
218 	esp_wifi_free(mutex);
219 }
220 
mutex_lock_wrapper(void * mutex)221 static int32_t IRAM_ATTR mutex_lock_wrapper(void *mutex)
222 {
223 	struct k_mutex *my_mutex = (struct k_mutex *) mutex;
224 
225 	k_mutex_lock(my_mutex, K_FOREVER);
226 	return 0;
227 }
228 
mutex_unlock_wrapper(void * mutex)229 static int32_t IRAM_ATTR mutex_unlock_wrapper(void *mutex)
230 {
231 	struct k_mutex *my_mutex = (struct k_mutex *) mutex;
232 
233 	k_mutex_unlock(my_mutex);
234 	return 0;
235 }
236 
queue_create_wrapper(uint32_t queue_len,uint32_t item_size)237 static void *queue_create_wrapper(uint32_t queue_len, uint32_t item_size)
238 {
239 	struct k_queue *queue = (struct k_queue *) wifi_malloc(sizeof(struct k_queue));
240 
241 	if (queue == NULL) {
242 		LOG_ERR("queue malloc failed");
243 		return NULL;
244 	}
245 
246 	k_msgq_init((struct k_msgq *)queue, wifi_msgq_buffer, item_size, queue_len);
247 
248 	return (void *)queue;
249 }
250 
queue_delete_wrapper(void * handle)251 static void queue_delete_wrapper(void *handle)
252 {
253 	if (handle != NULL) {
254 		esp_wifi_free(handle);
255 	}
256 }
257 
task_delete_wrapper(void * handle)258 static void task_delete_wrapper(void *handle)
259 {
260 	if (handle != NULL) {
261 		k_thread_abort((k_tid_t) handle);
262 	}
263 
264 	k_object_release(&wifi_task_handle);
265 }
266 
queue_send_wrapper(void * queue,void * item,uint32_t block_time_tick)267 static int32_t queue_send_wrapper(void *queue, void *item, uint32_t block_time_tick)
268 {
269 	if (block_time_tick == OSI_FUNCS_TIME_BLOCKING) {
270 		k_msgq_put((struct k_msgq *)queue, item, K_FOREVER);
271 	} else {
272 		k_msgq_put((struct k_msgq *)queue, item, K_TICKS(block_time_tick));
273 	}
274 	return 1;
275 }
276 
queue_send_from_isr_wrapper(void * queue,void * item,void * hptw)277 static int32_t IRAM_ATTR queue_send_from_isr_wrapper(void *queue, void *item, void *hptw)
278 {
279 	int *hpt = (int *) hptw;
280 
281 	k_msgq_put((struct k_msgq *)queue, item, K_NO_WAIT);
282 	*hpt = 0;
283 	return 1;
284 }
285 
queue_send_to_back_wrapper(void * queue,void * item,uint32_t block_time_tick)286 int32_t queue_send_to_back_wrapper(void *queue, void *item, uint32_t block_time_tick)
287 {
288 	return 0;
289 }
290 
queue_send_to_front_wrapper(void * queue,void * item,uint32_t block_time_tick)291 int32_t queue_send_to_front_wrapper(void *queue, void *item, uint32_t block_time_tick)
292 {
293 	return 0;
294 }
295 
queue_recv_wrapper(void * queue,void * item,uint32_t block_time_tick)296 static int32_t queue_recv_wrapper(void *queue, void *item, uint32_t block_time_tick)
297 {
298 	if (block_time_tick == OSI_FUNCS_TIME_BLOCKING) {
299 		k_msgq_get((struct k_msgq *)queue, item, K_FOREVER);
300 	} else {
301 		k_msgq_get((struct k_msgq *)queue, item, K_TICKS(block_time_tick));
302 	}
303 	return 1;
304 }
305 
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)306 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)
307 {
308 	return 0;
309 }
310 
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)311 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)
312 {
313 	k_tid_t tid = k_thread_create(&wifi_task_handle, wifi_stack, stack_depth,
314 				      (k_thread_entry_t)task_func, param, NULL, NULL,
315 				      prio, K_INHERIT_PERMS, K_NO_WAIT);
316 
317 	k_thread_name_set(tid, name);
318 
319 	*(int32_t *)task_handle = (int32_t) tid;
320 	return 1;
321 }
322 
task_create_wrapper(void * task_func,const char * name,uint32_t stack_depth,void * param,uint32_t prio,void * task_handle)323 static int32_t task_create_wrapper(void *task_func, const char *name, uint32_t stack_depth, void *param, uint32_t prio, void *task_handle)
324 {
325 	k_tid_t tid = k_thread_create(&wifi_task_handle, wifi_stack, stack_depth,
326 				      (k_thread_entry_t)task_func, param, NULL, NULL,
327 				      prio, K_INHERIT_PERMS, K_NO_WAIT);
328 
329 	k_thread_name_set(tid, name);
330 
331 	*(int32_t *)task_handle = (int32_t) tid;
332 	return 1;
333 }
334 
task_ms_to_tick_wrapper(uint32_t ms)335 static int32_t IRAM_ATTR task_ms_to_tick_wrapper(uint32_t ms)
336 {
337 	return (int32_t)(k_ms_to_ticks_ceil32(ms));
338 }
339 
task_get_max_priority_wrapper(void)340 static int32_t task_get_max_priority_wrapper(void)
341 {
342 	return (int32_t)(CONFIG_ESP32_WIFI_MAX_THREAD_PRIORITY);
343 }
344 
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)345 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)
346 {
347 	esp_wifi_event_handler(event_base, event_id, event_data, event_data_size, ticks_to_wait);
348 	return 0;
349 }
350 
wifi_apb80m_request_wrapper(void)351 static void IRAM_ATTR wifi_apb80m_request_wrapper(void)
352 {
353 #ifdef CONFIG_PM_ENABLE
354 	wifi_apb80m_request();
355 #endif
356 }
357 
wifi_apb80m_release_wrapper(void)358 static void IRAM_ATTR wifi_apb80m_release_wrapper(void)
359 {
360 #ifdef CONFIG_PM_ENABLE
361 	wifi_apb80m_release();
362 #endif
363 }
364 
timer_arm_wrapper(void * timer,uint32_t tmout,bool repeat)365 static void IRAM_ATTR timer_arm_wrapper(void *timer, uint32_t tmout, bool repeat)
366 {
367 	ets_timer_arm(timer, tmout, repeat);
368 }
369 
wifi_reset_mac_wrapper(void)370 static void wifi_reset_mac_wrapper(void)
371 {
372 	periph_module_reset(PERIPH_WIFI_MODULE);
373 }
374 
wifi_rtc_enable_iso_wrapper(void)375 static void IRAM_ATTR wifi_rtc_enable_iso_wrapper(void)
376 {
377 #if CONFIG_MAC_BB_PD
378 	esp_mac_bb_power_down();
379 #endif
380 }
381 
wifi_rtc_disable_iso_wrapper(void)382 static void IRAM_ATTR wifi_rtc_disable_iso_wrapper(void)
383 {
384 #if CONFIG_MAC_BB_PD
385 	esp_mac_bb_power_up();
386 #endif
387 }
388 
wifi_clock_enable_wrapper(void)389 static void wifi_clock_enable_wrapper(void)
390 {
391 	wifi_module_enable();
392 }
393 
wifi_clock_disable_wrapper(void)394 static void wifi_clock_disable_wrapper(void)
395 {
396 	wifi_module_disable();
397 }
398 
get_time_wrapper(void * t)399 static int get_time_wrapper(void *t)
400 {
401 	return os_get_time(t);
402 }
403 
realloc_internal_wrapper(void * ptr,size_t size)404 static void *IRAM_ATTR realloc_internal_wrapper(void *ptr, size_t size)
405 {
406 	LOG_ERR("%s not yet supported", __func__);
407 	return NULL;
408 }
409 
calloc_internal_wrapper(size_t n,size_t size)410 static void *IRAM_ATTR calloc_internal_wrapper(size_t n, size_t size)
411 {
412 	return wifi_calloc(n, size);
413 }
414 
zalloc_internal_wrapper(size_t size)415 static void *IRAM_ATTR zalloc_internal_wrapper(size_t size)
416 {
417 	return wifi_calloc(1, size);
418 }
419 
uxQueueMessagesWaiting(void * queue)420 uint32_t uxQueueMessagesWaiting(void *queue)
421 {
422 	return 0;
423 }
424 
xEventGroupCreate(void)425 void *xEventGroupCreate(void)
426 {
427 	LOG_ERR("EventGroup not supported!");
428 	return NULL;
429 }
430 
vEventGroupDelete(void * grp)431 void vEventGroupDelete(void *grp)
432 {
433 }
434 
xEventGroupSetBits(void * ptr,uint32_t data)435 uint32_t xEventGroupSetBits(void *ptr, uint32_t data)
436 {
437 	return 0;
438 }
439 
xEventGroupClearBits(void * ptr,uint32_t data)440 uint32_t xEventGroupClearBits(void *ptr, uint32_t data)
441 {
442 	return 0;
443 }
444 
task_delay(uint32_t ticks)445 void task_delay(uint32_t ticks)
446 {
447 	k_sleep(K_TICKS(ticks));
448 }
449 
esp_get_free_heap_size(void)450 uint32_t esp_get_free_heap_size(void)
451 {
452 	/* FIXME: API to get free heap size is not available in Zephyr. */
453 	/* It is only used by ESP-MESH feature (not supported yet) */
454 	return 10000;
455 }
456 
random(void)457 unsigned long random(void)
458 {
459 	return sys_rand32_get();
460 }
461 
nvs_set_i8(uint32_t handle,const char * key,int8_t value)462 int32_t nvs_set_i8(uint32_t handle, const char *key, int8_t value)
463 {
464 	return 0;
465 }
466 
nvs_get_i8(uint32_t handle,const char * key,int8_t * out_value)467 int32_t nvs_get_i8(uint32_t handle, const char *key, int8_t *out_value)
468 {
469 	return 0;
470 }
471 
nvs_set_u8(uint32_t handle,const char * key,uint8_t value)472 int32_t nvs_set_u8(uint32_t handle, const char *key, uint8_t value)
473 {
474 	return 0;
475 }
476 
nvs_get_u8(uint32_t handle,const char * key,uint8_t * out_value)477 int32_t nvs_get_u8(uint32_t handle, const char *key, uint8_t *out_value)
478 {
479 	return 0;
480 }
481 
nvs_set_u16(uint32_t handle,const char * key,uint16_t value)482 int32_t nvs_set_u16(uint32_t handle, const char *key, uint16_t value)
483 {
484 	return 0;
485 }
486 
nvs_get_u16(uint32_t handle,const char * key,uint16_t * out_value)487 int32_t nvs_get_u16(uint32_t handle, const char *key, uint16_t *out_value)
488 {
489 	return 0;
490 }
491 
nvs_open(const char * name,uint32_t open_mode,uint32_t * out_handle)492 int32_t nvs_open(const char *name, uint32_t open_mode, uint32_t *out_handle)
493 {
494 	return 0;
495 }
496 
nvs_close(uint32_t handle)497 void nvs_close(uint32_t handle)
498 {
499 	return;
500 }
501 
nvs_commit(uint32_t handle)502 int32_t nvs_commit(uint32_t handle)
503 {
504 	return 0;
505 }
506 
nvs_set_blob(uint32_t handle,const char * key,const void * value,size_t length)507 int32_t nvs_set_blob(uint32_t handle, const char *key, const void *value,
508 		     size_t length)
509 {
510 	return 0;
511 }
512 
nvs_get_blob(uint32_t handle,const char * key,void * out_value,size_t * length)513 int32_t nvs_get_blob(uint32_t handle, const char *key, void *out_value,
514 		     size_t *length)
515 {
516 	return 0;
517 }
518 
nvs_erase_key(uint32_t handle,const char * key)519 int32_t nvs_erase_key(uint32_t handle, const char *key)
520 {
521 	return 0;
522 }
523 
coex_init_wrapper(void)524 static int coex_init_wrapper(void)
525 {
526 #if CONFIG_SW_COEXIST_ENABLE
527 	return coex_init();
528 #else
529 	return 0;
530 #endif
531 }
532 
coex_deinit_wrapper(void)533 static void coex_deinit_wrapper(void)
534 {
535 #if CONFIG_SW_COEXIST_ENABLE
536 	coex_deinit();
537 #endif
538 }
539 
coex_enable_wrapper(void)540 static int coex_enable_wrapper(void)
541 {
542 #if CONFIG_SW_COEXIST_ENABLE
543 	return coex_enable();
544 #else
545 	return 0;
546 #endif
547 }
548 
coex_disable_wrapper(void)549 static void coex_disable_wrapper(void)
550 {
551 #if CONFIG_SW_COEXIST_ENABLE
552 	coex_disable();
553 #endif
554 }
555 
coex_status_get_wrapper(void)556 static IRAM_ATTR uint32_t coex_status_get_wrapper(void)
557 {
558 #if CONFIG_SW_COEXIST_ENABLE
559 	return coex_status_get(COEX_STATUS_GET_WIFI_BITMAP);
560 #else
561 	return 0;
562 #endif
563 }
564 
565 
coex_wifi_request_wrapper(uint32_t event,uint32_t latency,uint32_t duration)566 static int coex_wifi_request_wrapper(uint32_t event, uint32_t latency, uint32_t duration)
567 {
568 #if CONFIG_SW_COEXIST_ENABLE
569 	return coex_wifi_request(event, latency, duration);
570 #else
571 	return 0;
572 #endif
573 }
574 
coex_wifi_release_wrapper(uint32_t event)575 static IRAM_ATTR int coex_wifi_release_wrapper(uint32_t event)
576 {
577 #if CONFIG_SW_COEXIST_ENABLE
578 	return coex_wifi_release(event);
579 #else
580 	return 0;
581 #endif
582 }
583 
coex_wifi_channel_set_wrapper(uint8_t primary,uint8_t secondary)584 static int coex_wifi_channel_set_wrapper(uint8_t primary, uint8_t secondary)
585 {
586 #if CONFIG_SW_COEXIST_ENABLE
587 	return coex_wifi_channel_set(primary, secondary);
588 #else
589 	return 0;
590 #endif
591 }
592 
coex_event_duration_get_wrapper(uint32_t event,uint32_t * duration)593 static IRAM_ATTR int coex_event_duration_get_wrapper(uint32_t event, uint32_t *duration)
594 {
595 #if CONFIG_SW_COEXIST_ENABLE
596 	return coex_event_duration_get(event, duration);
597 #else
598 	return 0;
599 #endif
600 }
601 
coex_pti_get_wrapper(uint32_t event,uint8_t * pti)602 static int coex_pti_get_wrapper(uint32_t event, uint8_t *pti)
603 {
604 #if CONFIG_SW_COEXIST_ENABLE
605 	return coex_pti_get(event, pti);
606 #else
607 	return 0;
608 #endif
609 }
610 
coex_schm_status_bit_clear_wrapper(uint32_t type,uint32_t status)611 static void coex_schm_status_bit_clear_wrapper(uint32_t type, uint32_t status)
612 {
613 #if CONFIG_SW_COEXIST_ENABLE
614 	coex_schm_status_bit_clear(type, status);
615 #endif
616 }
617 
coex_schm_status_bit_set_wrapper(uint32_t type,uint32_t status)618 static void coex_schm_status_bit_set_wrapper(uint32_t type, uint32_t status)
619 {
620 #if CONFIG_SW_COEXIST_ENABLE
621 	coex_schm_status_bit_set(type, status);
622 #endif
623 }
624 
coex_schm_interval_set_wrapper(uint32_t interval)625 static IRAM_ATTR int coex_schm_interval_set_wrapper(uint32_t interval)
626 {
627 #if CONFIG_SW_COEXIST_ENABLE
628 	return coex_schm_interval_set(interval);
629 #else
630 	return 0;
631 #endif
632 }
633 
coex_schm_interval_get_wrapper(void)634 static uint32_t coex_schm_interval_get_wrapper(void)
635 {
636 #if CONFIG_SW_COEXIST_ENABLE
637 	return coex_schm_interval_get();
638 #else
639 	return 0;
640 #endif
641 }
642 
coex_schm_curr_period_get_wrapper(void)643 static uint8_t coex_schm_curr_period_get_wrapper(void)
644 {
645 #if CONFIG_SW_COEXIST_ENABLE
646 	return coex_schm_curr_period_get();
647 #else
648 	return 0;
649 #endif
650 }
651 
coex_schm_curr_phase_get_wrapper(void)652 static void * coex_schm_curr_phase_get_wrapper(void)
653 {
654 #if CONFIG_SW_COEXIST_ENABLE
655 	return coex_schm_curr_phase_get();
656 #else
657 	return NULL;
658 #endif
659 }
660 
esp_empty_wrapper(void)661 static void IRAM_ATTR esp_empty_wrapper(void)
662 {
663 
664 }
665 
esp_phy_enable_wrapper(void)666 static void esp_phy_enable_wrapper(void)
667 {
668 	esp_phy_enable(PHY_MODEM_WIFI);
669 	phy_wifi_enable_set(1);
670 }
671 
esp_phy_disable_wrapper(void)672 static void esp_phy_disable_wrapper(void)
673 {
674 	phy_wifi_enable_set(0);
675 	esp_phy_disable(PHY_MODEM_WIFI);
676 }
677 
esp_log_writev_wrapper(uint32_t level,const char * tag,const char * format,va_list args)678 static void esp_log_writev_wrapper(uint32_t level, const char *tag, const char *format, va_list args)
679 {
680 #if CONFIG_WIFI_LOG_LEVEL >= LOG_LEVEL_DBG
681 	esp_log_writev((esp_log_level_t)level,tag,format,args);
682 #endif
683 }
684 
esp_log_write_wrapper(uint32_t level,const char * tag,const char * format,...)685 static void esp_log_write_wrapper(uint32_t level,const char *tag,const char *format, ...)
686 {
687 #if CONFIG_WIFI_LOG_LEVEL >= LOG_LEVEL_DBG
688 	va_list list;
689 	va_start(list, format);
690 	esp_log_writev((esp_log_level_t)level, tag, format, list);
691 	va_end(list);
692 #endif
693 }
694 
coex_register_start_cb_wrapper(int (* cb)(void))695 static int coex_register_start_cb_wrapper(int (* cb)(void))
696 {
697 #if CONFIG_SW_COEXIST_ENABLE
698 	return coex_register_start_cb(cb);
699 #else
700 	return 0;
701 #endif
702 }
703 
coex_schm_process_restart_wrapper(void)704 static int coex_schm_process_restart_wrapper(void)
705 {
706 #if CONFIG_SW_COEXIST_ENABLE
707 	return coex_schm_process_restart();
708 #else
709 	return 0;
710 #endif
711 }
712 
coex_schm_register_cb_wrapper(int type,int (* cb)(int))713 static int coex_schm_register_cb_wrapper(int type, int(*cb)(int))
714 {
715 #if CONFIG_SW_COEXIST_ENABLE
716 	return coex_schm_register_callback(type, cb);
717 #else
718 	return 0;
719 #endif
720 }
721 
coex_schm_flexible_period_set_wrapper(uint8_t period)722 static int coex_schm_flexible_period_set_wrapper(uint8_t period)
723 {
724 #if CONFIG_ESP_COEX_POWER_MANAGEMENT
725 	return coex_schm_flexible_period_set(period);
726 #else
727 	return 0;
728 #endif
729 }
730 
coex_schm_flexible_period_get_wrapper(void)731 static uint8_t coex_schm_flexible_period_get_wrapper(void)
732 {
733 #if CONFIG_ESP_COEX_POWER_MANAGEMENT
734 	return coex_schm_flexible_period_get();
735 #else
736 	return 1;
737 #endif
738 }
739 
740 wifi_osi_funcs_t g_wifi_osi_funcs = {
741 	._version = ESP_WIFI_OS_ADAPTER_VERSION,
742 	._env_is_chip = esp_coex_common_env_is_chip_wrapper,
743 	._set_intr = set_intr_wrapper,
744 	._clear_intr = clear_intr_wrapper,
745 	._set_isr = set_isr_wrapper,
746 	._ints_on = intr_on,
747 	._ints_off = intr_off,
748 	._is_from_isr = k_is_in_isr,
749 	._spin_lock_create = esp_coex_common_spin_lock_create_wrapper,
750 	._spin_lock_delete = esp_wifi_free,
751 	._wifi_int_disable = esp_coex_common_int_disable_wrapper,
752 	._wifi_int_restore = esp_coex_common_int_restore_wrapper,
753 	._task_yield_from_isr = esp_coex_common_task_yield_from_isr_wrapper,
754 	._semphr_create = esp_coex_common_semphr_create_wrapper,
755 	._semphr_delete = esp_coex_common_semphr_delete_wrapper,
756 	._semphr_take = esp_coex_common_semphr_take_wrapper,
757 	._semphr_give = esp_coex_common_semphr_give_wrapper,
758 	._wifi_thread_semphr_get = wifi_thread_semphr_get_wrapper,
759 	._mutex_create = mutex_create_wrapper,
760 	._recursive_mutex_create = recursive_mutex_create_wrapper,
761 	._mutex_delete = mutex_delete_wrapper,
762 	._mutex_lock = mutex_lock_wrapper,
763 	._mutex_unlock = mutex_unlock_wrapper,
764 	._queue_create = queue_create_wrapper,
765 	._queue_delete = queue_delete_wrapper,
766 	._queue_send = queue_send_wrapper,
767 	._queue_send_from_isr = queue_send_from_isr_wrapper,
768 	._queue_send_to_back = queue_send_to_back_wrapper,
769 	._queue_send_to_front = queue_send_to_front_wrapper,
770 	._queue_recv = queue_recv_wrapper,
771 	._queue_msg_waiting = uxQueueMessagesWaiting,
772 	._event_group_create = (void *(*)(void))xEventGroupCreate,
773 	._event_group_delete = (void (*)(void *))vEventGroupDelete,
774 	._event_group_set_bits = xEventGroupSetBits,
775 	._event_group_clear_bits = xEventGroupClearBits,
776 	._event_group_wait_bits = event_group_wait_bits_wrapper,
777 	._task_create_pinned_to_core = task_create_pinned_to_core_wrapper,
778 	._task_create = task_create_wrapper,
779 	._task_delete = task_delete_wrapper,
780 	._task_delay = task_delay,
781 	._task_ms_to_tick = task_ms_to_tick_wrapper,
782 	._task_get_current_task = (void *(*)(void))k_current_get,
783 	._task_get_max_priority = task_get_max_priority_wrapper,
784 	._malloc = wifi_malloc,
785 	._free = esp_wifi_free,
786 	._event_post = esp_event_post_wrapper,
787 	._get_free_heap_size = esp_get_free_heap_size,
788 	._rand = sys_rand32_get,
789 	._dport_access_stall_other_cpu_start_wrap = esp_empty_wrapper,
790 	._dport_access_stall_other_cpu_end_wrap = esp_empty_wrapper,
791 	._wifi_apb80m_request = wifi_apb80m_request_wrapper,
792 	._wifi_apb80m_release = wifi_apb80m_release_wrapper,
793 	._phy_disable = esp_phy_disable_wrapper,
794 	._phy_enable = esp_phy_enable_wrapper,
795 	._phy_update_country_info = esp_phy_update_country_info,
796 	._read_mac = esp_read_mac,
797 	._timer_arm = timer_arm_wrapper,
798 	._timer_disarm = esp_coex_common_timer_disarm_wrapper,
799 	._timer_done = esp_coex_common_timer_done_wrapper,
800 	._timer_setfn = esp_coex_common_timer_setfn_wrapper,
801 	._timer_arm_us = esp_coex_common_timer_arm_us_wrapper,
802 	._wifi_reset_mac = wifi_reset_mac_wrapper,
803 	._wifi_clock_enable = wifi_clock_enable_wrapper,
804 	._wifi_clock_disable = wifi_clock_disable_wrapper,
805 	._wifi_rtc_enable_iso = wifi_rtc_enable_iso_wrapper,
806 	._wifi_rtc_disable_iso = wifi_rtc_disable_iso_wrapper,
807 	._esp_timer_get_time = esp_timer_get_time,
808 	._nvs_set_i8 = nvs_set_i8,
809 	._nvs_get_i8 = nvs_get_i8,
810 	._nvs_set_u8 = nvs_set_u8,
811 	._nvs_get_u8 = nvs_get_u8,
812 	._nvs_set_u16 = nvs_set_u16,
813 	._nvs_get_u16 = nvs_get_u16,
814 	._nvs_open = nvs_open,
815 	._nvs_close = nvs_close,
816 	._nvs_commit = nvs_commit,
817 	._nvs_set_blob = nvs_set_blob,
818 	._nvs_get_blob = nvs_get_blob,
819 	._nvs_erase_key = nvs_erase_key,
820 	._get_random = os_get_random,
821 	._get_time = get_time_wrapper,
822 	._random = random,
823 	._slowclk_cal_get = esp_coex_common_clk_slowclk_cal_get_wrapper,
824 	._log_write = esp_log_write_wrapper,
825 	._log_writev = esp_log_writev_wrapper,
826 	._log_timestamp = k_uptime_get_32,
827 	._malloc_internal =  esp_coex_common_malloc_internal_wrapper,
828 	._realloc_internal = realloc_internal_wrapper,
829 	._calloc_internal = calloc_internal_wrapper,
830 	._zalloc_internal = zalloc_internal_wrapper,
831 	._wifi_malloc = wifi_malloc,
832 	._wifi_realloc = wifi_realloc,
833 	._wifi_calloc = wifi_calloc,
834 	._wifi_zalloc = wifi_zalloc_wrapper,
835 	._wifi_create_queue = wifi_create_queue_wrapper,
836 	._wifi_delete_queue = wifi_delete_queue_wrapper,
837 	._coex_init = coex_init_wrapper,
838 	._coex_deinit = coex_deinit_wrapper,
839 	._coex_enable = coex_enable_wrapper,
840 	._coex_disable = coex_disable_wrapper,
841 	._coex_status_get = coex_status_get_wrapper,
842 	._coex_wifi_request = coex_wifi_request_wrapper,
843 	._coex_wifi_release = coex_wifi_release_wrapper,
844 	._coex_wifi_channel_set = coex_wifi_channel_set_wrapper,
845 	._coex_event_duration_get = coex_event_duration_get_wrapper,
846 	._coex_pti_get = coex_pti_get_wrapper,
847 	._coex_schm_status_bit_clear = coex_schm_status_bit_clear_wrapper,
848 	._coex_schm_status_bit_set = coex_schm_status_bit_set_wrapper,
849 	._coex_schm_interval_set = coex_schm_interval_set_wrapper,
850 	._coex_schm_interval_get = coex_schm_interval_get_wrapper,
851 	._coex_schm_curr_period_get = coex_schm_curr_period_get_wrapper,
852 	._coex_schm_curr_phase_get = coex_schm_curr_phase_get_wrapper,
853 	._coex_register_start_cb = coex_register_start_cb_wrapper,
854 	._coex_schm_process_restart = coex_schm_process_restart_wrapper,
855 	._coex_schm_register_cb = coex_schm_register_cb_wrapper,
856 	._coex_schm_flexible_period_set = coex_schm_flexible_period_set_wrapper,
857 	._coex_schm_flexible_period_get = coex_schm_flexible_period_get_wrapper,
858 	._magic = ESP_WIFI_OS_ADAPTER_MAGIC,
859 };
860