1 
2 #include <stdbool.h>
3 #include <string.h>
4 
5 #include "esp_event.h"
6 #include "sdkconfig.h"
7 
8 #include "freertos/FreeRTOS.h"
9 #include "freertos/task.h"
10 #include "esp_log.h"
11 #include "driver/periph_ctrl.h"
12 #include "driver/timer.h"
13 
14 #include "esp_event.h"
15 #include "esp_event_private.h"
16 #include "esp_event_internal.h"
17 
18 #include "esp_heap_caps.h"
19 
20 #include "sdkconfig.h"
21 #include "unity.h"
22 
23 #include "test_utils.h"
24 
25 typedef struct {
26     void* data;
27     SemaphoreHandle_t mutex;
28 } simple_arg_t;
29 
30 static const char* TAG = "test_event";
31 
32 ESP_EVENT_DECLARE_BASE(s_default_test_base1);
33 ESP_EVENT_DECLARE_BASE(s_default_test_base2);
34 
35 ESP_EVENT_DEFINE_BASE(s_default_test_base1);
36 ESP_EVENT_DEFINE_BASE(s_default_test_base2);
37 
38 enum {
39     TEST_EVENT_BASE1_EV1,
40     TEST_EVENT_BASE1_EV2,
41     TEST_EVENT_BASE1_MAX
42 };
43 
44 enum {
45     TEST_EVENT_BASE2_EV1,
46     TEST_EVENT_BASE2_EV2,
47     TEST_EVENT_BASE2_MAX
48 };
49 
50 // The initial logging "initializing test" is to ensure mutex allocation is not counted against memory not being freed
51 // during teardown.
52 #define TEST_SETUP() \
53         ESP_LOGI(TAG, "initializing test");
54 
test_event_simple_handler(void * event_handler_arg,esp_event_base_t event_base,int32_t event_id,void * event_data)55 static void test_event_simple_handler(void* event_handler_arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
56 {
57     if (!event_handler_arg) {
58         return;
59     }
60     simple_arg_t* arg = (simple_arg_t*) event_handler_arg;
61     xSemaphoreTake(arg->mutex, portMAX_DELAY);
62 
63     int* count = (int*) arg->data;
64 
65     if (event_data == NULL) {
66         (*count)++;
67     } else {
68         (*count) += *((int*) event_data);
69     }
70 
71     xSemaphoreGive(arg->mutex);
72 }
73 
74 TEST_CASE("default loop: can create and delete loop", "[event]")
75 {
76     TEST_SETUP();
77 
78     TEST_ESP_OK(esp_event_loop_create_default());
79 
80     TEST_ESP_OK(esp_event_loop_delete_default());
81 }
82 
83 TEST_CASE("default loop: registering fails on uninitialized default loop", "[event]")
84 {
85     TEST_SETUP();
86 
87     esp_event_handler_instance_t instance;
88     TEST_ASSERT_EQUAL(ESP_ERR_INVALID_STATE, esp_event_handler_instance_register(s_default_test_base1,
89             TEST_EVENT_BASE1_EV1,
90             test_event_simple_handler,
91             NULL,
92             &instance));
93 }
94 
95 TEST_CASE("default loop: registering/unregistering event works", "[event]")
96 {
97     TEST_SETUP();
98 
99     int count = 0;
100 
101     simple_arg_t arg = {
102         .data = &count,
103         .mutex = xSemaphoreCreateMutex()
104     };
105 
106     TEST_ESP_OK(esp_event_loop_create_default());
107 
108     esp_event_handler_instance_t instance;
109     TEST_ESP_OK(esp_event_handler_instance_register(s_default_test_base1,
110             TEST_EVENT_BASE1_EV1,
111             test_event_simple_handler,
112             &arg,
113             &instance));
114     TEST_ASSERT(instance);
115     TEST_ESP_OK(esp_event_post(s_default_test_base1, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
116 
117     vTaskDelay(10);
118 
119     TEST_ASSERT_EQUAL(1, count);
120 
121     TEST_ESP_OK(esp_event_handler_instance_unregister(s_default_test_base1,
122             TEST_EVENT_BASE1_EV1,
123             &instance));
124 
125     vTaskDelay(10);
126 
127     TEST_ASSERT_EQUAL(1, count);
128 
129     TEST_ESP_OK(esp_event_loop_delete_default());
130 
131     vSemaphoreDelete(arg.mutex);
132 }
133 
134 TEST_CASE("default event loop: registering event handler instance without instance context works", "[event]") {
135     TEST_SETUP();
136 
137     int count_1 = 0;
138 
139     simple_arg_t arg_1 = {
140         .data = &count_1,
141         .mutex = xSemaphoreCreateMutex()
142     };
143 
144     TEST_ESP_OK(esp_event_loop_create_default());
145 
146     TEST_ESP_OK(esp_event_handler_instance_register(ESP_EVENT_ANY_BASE, ESP_EVENT_ANY_ID, test_event_simple_handler, &arg_1, NULL));
147 
148     TEST_ESP_OK(esp_event_post(s_default_test_base1, TEST_EVENT_BASE1_EV1, NULL, 0, portMAX_DELAY));
149 
150     vTaskDelay(10);
151 
152     TEST_ASSERT_EQUAL(1, count_1);
153 
154     TEST_ESP_OK(esp_event_loop_delete_default());
155 
156     vSemaphoreDelete(arg_1.mutex);
157 }
158