1 #ifndef ESP_EVENT_API_HPP_
2 #define ESP_EVENT_API_HPP_
3 
4 #include "esp_event.h"
5 
6 namespace idf {
7 
8 namespace event {
9 
10 /**
11  * Abstract interface for direct calls to esp_event C-API.
12  * This is generally not intended to be used directly.
13  * It's main purpose is to provide ESPEventLoop a unified API not dependent on whether the default event loop or a
14  * custom event loop is used.
15  * The interface resembles the C-API, have a look there for further documentation.
16  */
17 class ESPEventAPI {
18 public:
~ESPEventAPI()19     virtual ~ESPEventAPI() { }
20 
21     virtual esp_err_t handler_register(esp_event_base_t event_base,
22             int32_t event_id,
23             esp_event_handler_t event_handler,
24             void* event_handler_arg,
25             esp_event_handler_instance_t *instance) = 0;
26 
27     virtual esp_err_t handler_unregister(esp_event_base_t event_base,
28             int32_t event_id,
29             esp_event_handler_instance_t instance) = 0;
30 
31     virtual esp_err_t post(esp_event_base_t event_base,
32             int32_t event_id,
33             void* event_data,
34             size_t event_data_size,
35             TickType_t ticks_to_wait) = 0;
36 };
37 
38 /**
39  * @brief API version with default event loop.
40  *
41  * It will direct calls to the default event loop API.
42  */
43 class ESPEventAPIDefault : public ESPEventAPI {
44 public:
45     ESPEventAPIDefault();
46     virtual ~ESPEventAPIDefault();
47 
48     /**
49      * Copying would lead to deletion of event loop through destructor.
50      */
51     ESPEventAPIDefault(const ESPEventAPIDefault &o) = delete;
52     ESPEventAPIDefault& operator=(const ESPEventAPIDefault&) = delete;
53 
54     esp_err_t handler_register(esp_event_base_t event_base,
55             int32_t event_id,
56             esp_event_handler_t event_handler,
57             void* event_handler_arg,
58             esp_event_handler_instance_t *instance) override;
59 
60     esp_err_t handler_unregister(esp_event_base_t event_base,
61             int32_t event_id,
62             esp_event_handler_instance_t instance) override;
63 
64     esp_err_t post(esp_event_base_t event_base,
65             int32_t event_id,
66             void* event_data,
67             size_t event_data_size,
68             TickType_t ticks_to_wait) override;
69 };
70 
71 /**
72  * @brief API version with custom event loop.
73  *
74  * It will direct calls to the custom event loop API.
75  * The loop parameters are given in the constructor the same way it's done in esp_event_loop_create() in event.h.
76  * This class also provides a run method in case the custom event loop was created without its own task.
77  */
78 class ESPEventAPICustom : public ESPEventAPI {
79 public:
80     /**
81      * @param event_loop_args the event loop arguments, refer to esp_event_loop_create() in event.h.
82      */
83     ESPEventAPICustom(const esp_event_loop_args_t &event_loop_args);
84 
85     virtual ~ESPEventAPICustom();
86 
87     /**
88      * Copying would lead to deletion of event loop through destructor.
89      */
90     ESPEventAPICustom(const ESPEventAPICustom &o) = delete;
91     ESPEventAPICustom& operator=(const ESPEventAPICustom&) = delete;
92 
93     esp_err_t handler_register(esp_event_base_t event_base,
94             int32_t event_id,
95             esp_event_handler_t event_handler,
96             void* event_handler_arg,
97             esp_event_handler_instance_t *instance) override;
98 
99     esp_err_t handler_unregister(esp_event_base_t event_base,
100             int32_t event_id,
101             esp_event_handler_instance_t instance) override;
102 
103     esp_err_t post(esp_event_base_t event_base,
104             int32_t event_id,
105             void* event_data,
106             size_t event_data_size,
107             TickType_t ticks_to_wait) override;
108 
109     /**
110      * Run the event loop. The behavior is the same as esp_event_loop_run in esp_event.h.
111      */
112     esp_err_t run(TickType_t ticks_to_run);
113 
114 private:
115     esp_event_loop_handle_t event_loop;
116 };
117 
118 } // event
119 
120 } // idf
121 
122 #endif // ESP_EVENT_API_HPP_
123