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