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