1 #include "esp_event.h"
2 #include "esp_event_cxx.hpp"
3 #include "esp_event_api.hpp"
4 
5 #ifdef __cpp_exceptions
6 
7 namespace idf {
8 
9 namespace event {
10 
ESPEventAPIDefault()11 ESPEventAPIDefault::ESPEventAPIDefault()
12 {
13     esp_err_t res = esp_event_loop_create_default();
14     if (res != ESP_OK) {
15         throw idf::event::EventException(res);
16     }
17 }
18 
~ESPEventAPIDefault()19 ESPEventAPIDefault::~ESPEventAPIDefault()
20 {
21     esp_event_loop_delete_default();
22 }
23 
handler_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 * instance)24 esp_err_t ESPEventAPIDefault::handler_register(esp_event_base_t event_base,
25         int32_t event_id,
26         esp_event_handler_t event_handler,
27         void *event_handler_arg,
28         esp_event_handler_instance_t *instance)
29 {
30     return esp_event_handler_instance_register(event_base,
31                                                event_id,
32                                                event_handler,
33                                                event_handler_arg,
34                                                instance);
35 }
36 
handler_unregister(esp_event_base_t event_base,int32_t event_id,esp_event_handler_instance_t instance)37 esp_err_t ESPEventAPIDefault::handler_unregister(esp_event_base_t event_base,
38         int32_t event_id,
39         esp_event_handler_instance_t instance)
40 {
41     return esp_event_handler_instance_unregister(event_base, event_id, instance);
42 }
43 
post(esp_event_base_t event_base,int32_t event_id,void * event_data,size_t event_data_size,TickType_t ticks_to_wait)44 esp_err_t ESPEventAPIDefault::post(esp_event_base_t event_base,
45         int32_t event_id,
46         void* event_data,
47         size_t event_data_size,
48         TickType_t ticks_to_wait)
49 {
50     return esp_event_post(event_base,
51                           event_id,
52                           event_data,
53                           event_data_size,
54                           ticks_to_wait);
55 
56 }
57 
ESPEventAPICustom(const esp_event_loop_args_t & event_loop_args)58 ESPEventAPICustom::ESPEventAPICustom(const esp_event_loop_args_t &event_loop_args)
59 {
60     esp_err_t res = esp_event_loop_create(&event_loop_args, &event_loop);
61     if (res != ESP_OK) {
62         throw idf::event::EventException(res);
63     }
64 }
65 
~ESPEventAPICustom()66 ESPEventAPICustom::~ESPEventAPICustom()
67 {
68     esp_event_loop_delete(event_loop);
69 }
70 
handler_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 * instance)71 esp_err_t ESPEventAPICustom::handler_register(esp_event_base_t event_base,
72         int32_t event_id,
73         esp_event_handler_t event_handler,
74         void *event_handler_arg,
75         esp_event_handler_instance_t *instance)
76 {
77     return esp_event_handler_instance_register_with(event_loop,
78                                                     event_base,
79                                                     event_id,
80                                                     event_handler,
81                                                     event_handler_arg,
82                                                     instance);
83 }
84 
handler_unregister(esp_event_base_t event_base,int32_t event_id,esp_event_handler_instance_t instance)85 esp_err_t ESPEventAPICustom::handler_unregister(esp_event_base_t event_base,
86         int32_t event_id,
87         esp_event_handler_instance_t instance)
88 {
89     return esp_event_handler_instance_unregister_with(event_loop, event_base, event_id, instance);
90 }
91 
post(esp_event_base_t event_base,int32_t event_id,void * event_data,size_t event_data_size,TickType_t ticks_to_wait)92 esp_err_t ESPEventAPICustom::post(esp_event_base_t event_base,
93         int32_t event_id,
94         void* event_data,
95         size_t event_data_size,
96         TickType_t ticks_to_wait)
97 {
98     return esp_event_post_to(event_loop,
99                              event_base,
100                              event_id,
101                              event_data,
102                              event_data_size,
103                              ticks_to_wait);
104 }
105 
run(TickType_t ticks_to_run)106 esp_err_t ESPEventAPICustom::run(TickType_t ticks_to_run)
107 {
108     return esp_event_loop_run(event_loop, ticks_to_run);
109 }
110 
111 } // event
112 
113 } // idf
114 
115 #endif // __cpp_exceptions
116