1 // Copyright 2018 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef _OSAL_H_
16 #define _OSAL_H_
17 
18 #include <freertos/FreeRTOS.h>
19 #include <freertos/task.h>
20 #include <unistd.h>
21 #include <stdint.h>
22 #include <esp_timer.h>
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 #define OS_SUCCESS ESP_OK
29 #define OS_FAIL    ESP_FAIL
30 
31 typedef TaskHandle_t othread_t;
32 
httpd_os_thread_create(othread_t * thread,const char * name,uint16_t stacksize,int prio,void (* thread_routine)(void * arg),void * arg,BaseType_t core_id)33 static inline int httpd_os_thread_create(othread_t *thread,
34                                  const char *name, uint16_t stacksize, int prio,
35                                  void (*thread_routine)(void *arg), void *arg,
36                                  BaseType_t core_id)
37 {
38     int ret = xTaskCreatePinnedToCore(thread_routine, name, stacksize, arg, prio, thread, core_id);
39     if (ret == pdPASS) {
40         return OS_SUCCESS;
41     }
42     return OS_FAIL;
43 }
44 
45 /* Only self delete is supported */
httpd_os_thread_delete(void)46 static inline void httpd_os_thread_delete(void)
47 {
48     vTaskDelete(xTaskGetCurrentTaskHandle());
49 }
50 
httpd_os_thread_sleep(int msecs)51 static inline void httpd_os_thread_sleep(int msecs)
52 {
53     vTaskDelay(msecs / portTICK_RATE_MS);
54 }
55 
httpd_os_thread_handle(void)56 static inline othread_t httpd_os_thread_handle(void)
57 {
58     return xTaskGetCurrentTaskHandle();
59 }
60 
61 #ifdef __cplusplus
62 }
63 #endif
64 
65 #endif /* ! _OSAL_H_ */
66