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_private.h"
16 #include "esp_event_internal.h"
17 
18 #include "esp_log.h"
19 
esp_event_is_handler_registered(esp_event_loop_handle_t event_loop,esp_event_base_t event_base,int32_t event_id,esp_event_handler_t event_handler)20 bool esp_event_is_handler_registered(esp_event_loop_handle_t event_loop, esp_event_base_t event_base, int32_t event_id, esp_event_handler_t event_handler)
21 {
22     esp_event_loop_instance_t* loop = (esp_event_loop_instance_t*) event_loop;
23 
24     bool result = false;
25 
26     esp_event_loop_node_t* loop_node;
27     esp_event_base_node_t* base_node;
28     esp_event_id_node_t* id_node;
29     esp_event_handler_node_t* handler;
30 
31     SLIST_FOREACH(loop_node, &(loop->loop_nodes), next) {
32         SLIST_FOREACH(handler, &(loop_node->handlers), next) {
33             if(event_base == ESP_EVENT_ANY_BASE && event_id == ESP_EVENT_ANY_ID && handler->handler_ctx->handler == event_handler)
34             {
35                 result = true;
36                 goto out;
37             }
38         }
39 
40         SLIST_FOREACH(base_node, &(loop_node->base_nodes), next) {
41             if (base_node->base == event_base) {
42                 SLIST_FOREACH(handler, &(base_node->handlers), next) {
43                     if(event_id == ESP_EVENT_ANY_ID && handler->handler_ctx->handler == event_handler)
44                     {
45                         result = true;
46                         goto out;
47                     }
48                 }
49 
50                 SLIST_FOREACH(id_node, &(base_node->id_nodes), next) {
51                     if(id_node->id == event_id) {
52                         SLIST_FOREACH(handler, &(id_node->handlers), next) {
53                             if(handler->handler_ctx->handler == event_handler)
54                             {
55                                 result = true;
56                                 goto out;
57                             }
58                         }
59                     }
60                 }
61             }
62         }
63     }
64 
65 out:
66     xSemaphoreGive(loop->mutex);
67     return result;
68 }
69