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