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 #include "esp_event.h"
16 #include "esp_event_internal.h"
17 #include "esp_task.h"
18 
19 /* ------------------------- Static Variables ------------------------------- */
20 
21 static esp_event_loop_handle_t s_default_loop = NULL;
22 
23 /* ---------------------------- Public API ---------------------------------- */
24 
esp_event_handler_register(esp_event_base_t event_base,int32_t event_id,esp_event_handler_t event_handler,void * event_handler_arg)25 esp_err_t esp_event_handler_register(esp_event_base_t event_base, int32_t event_id,
26         esp_event_handler_t event_handler, void* event_handler_arg)
27 {
28     if (s_default_loop == NULL) {
29         return ESP_ERR_INVALID_STATE;
30     }
31 
32     return esp_event_handler_register_with(s_default_loop, event_base, event_id,
33             event_handler, event_handler_arg);
34 }
35 
esp_event_handler_instance_register(esp_event_base_t event_base,int32_t event_id,esp_event_handler_t event_handler,void * event_handler_arg,esp_event_handler_instance_t * context)36 esp_err_t esp_event_handler_instance_register(esp_event_base_t event_base,
37                                               int32_t event_id,
38                                               esp_event_handler_t event_handler,
39                                               void *event_handler_arg,
40                                               esp_event_handler_instance_t *context)
41 {
42     if (s_default_loop == NULL) {
43         return ESP_ERR_INVALID_STATE;
44     }
45 
46     return esp_event_handler_instance_register_with(s_default_loop,
47                                                     event_base,
48                                                     event_id,
49                                                     event_handler,
50                                                     event_handler_arg,
51                                                     context);
52 }
53 
esp_event_handler_unregister(esp_event_base_t event_base,int32_t event_id,esp_event_handler_t event_handler)54 esp_err_t esp_event_handler_unregister(esp_event_base_t event_base, int32_t event_id,
55         esp_event_handler_t event_handler)
56 {
57     if (s_default_loop == NULL) {
58         return ESP_ERR_INVALID_STATE;
59     }
60 
61     return esp_event_handler_unregister_with(s_default_loop, event_base, event_id,
62             event_handler);
63 }
64 
esp_event_handler_instance_unregister(esp_event_base_t event_base,int32_t event_id,esp_event_handler_instance_t context)65 esp_err_t esp_event_handler_instance_unregister(esp_event_base_t event_base,
66                                                 int32_t event_id,
67                                                 esp_event_handler_instance_t context)
68 {
69     if (s_default_loop == NULL) {
70         return ESP_ERR_INVALID_STATE;
71     }
72 
73     return esp_event_handler_instance_unregister_with(s_default_loop, event_base, event_id, context);
74 }
75 
esp_event_post(esp_event_base_t event_base,int32_t event_id,void * event_data,size_t event_data_size,TickType_t ticks_to_wait)76 esp_err_t esp_event_post(esp_event_base_t event_base, int32_t event_id,
77         void* event_data, size_t event_data_size, TickType_t ticks_to_wait)
78 {
79     if (s_default_loop == NULL) {
80         return ESP_ERR_INVALID_STATE;
81     }
82 
83     return esp_event_post_to(s_default_loop, event_base, event_id,
84             event_data, event_data_size, ticks_to_wait);
85 }
86 
87 
88 #if CONFIG_ESP_EVENT_POST_FROM_ISR
esp_event_isr_post(esp_event_base_t event_base,int32_t event_id,void * event_data,size_t event_data_size,BaseType_t * task_unblocked)89 esp_err_t esp_event_isr_post(esp_event_base_t event_base, int32_t event_id,
90         void* event_data, size_t event_data_size, BaseType_t* task_unblocked)
91 {
92     if (s_default_loop == NULL) {
93         return ESP_ERR_INVALID_STATE;
94     }
95 
96     return esp_event_isr_post_to(s_default_loop, event_base, event_id,
97             event_data, event_data_size, task_unblocked);
98 }
99 #endif
100 
101 
esp_event_loop_create_default(void)102 esp_err_t esp_event_loop_create_default(void)
103 {
104     if (s_default_loop) {
105         return ESP_ERR_INVALID_STATE;
106     }
107 
108     esp_event_loop_args_t loop_args = {
109         .queue_size = CONFIG_ESP_SYSTEM_EVENT_QUEUE_SIZE,
110         .task_name = "sys_evt",
111         .task_stack_size = ESP_TASKD_EVENT_STACK,
112         .task_priority = ESP_TASKD_EVENT_PRIO,
113         .task_core_id = 0
114     };
115 
116     esp_err_t err;
117 
118     err = esp_event_loop_create(&loop_args, &s_default_loop);
119     if (err != ESP_OK) {
120         return err;
121     }
122 
123     return ESP_OK;
124 }
125 
esp_event_loop_delete_default(void)126 esp_err_t esp_event_loop_delete_default(void)
127 {
128     if (!s_default_loop) {
129         return ESP_ERR_INVALID_STATE;
130     }
131 
132     esp_err_t err;
133 
134     err = esp_event_loop_delete(s_default_loop);
135 
136     if (err != ESP_OK) {
137         return err;
138     }
139 
140     s_default_loop = NULL;
141 
142     return ESP_OK;
143 }
144 
145 #if !CONFIG_IDF_TARGET_LINUX
146 /* Include the code to forward legacy system_event_t events to the this default
147  * event loop.
148  */
149 #include "event_send_compat.inc"
150 #endif
151