1 // Copyright 2015-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_err.h"
16 #include "esp_log.h"
17 #include "esp_event.h"
18 #include "esp_event_legacy.h"
19
20 #include "sdkconfig.h"
21
22 static const char* TAG = "event";
23
24 static system_event_cb_t s_event_handler_cb;
25 static void *s_event_ctx;
26 static bool s_initialized;
27
28 ESP_EVENT_DEFINE_BASE(SYSTEM_EVENT);
29
esp_event_post_to_user(void * arg,esp_event_base_t base,int32_t id,void * data)30 static void esp_event_post_to_user(void* arg, esp_event_base_t base, int32_t id, void* data)
31 {
32 if (s_event_handler_cb) {
33 system_event_t* event = (system_event_t*) data;
34 (*s_event_handler_cb)(s_event_ctx, event);
35 }
36 }
37
esp_event_loop_set_cb(system_event_cb_t cb,void * ctx)38 system_event_cb_t esp_event_loop_set_cb(system_event_cb_t cb, void *ctx)
39 {
40 system_event_cb_t old_cb = s_event_handler_cb;
41 s_event_handler_cb = cb;
42 s_event_ctx = ctx;
43 return old_cb;
44 }
45
esp_event_send_legacy(system_event_t * event)46 esp_err_t esp_event_send_legacy(system_event_t *event)
47 {
48 if (!s_initialized) {
49 ESP_LOGE(TAG, "system event loop not initialized via esp_event_loop_init");
50 return ESP_ERR_INVALID_STATE;
51 }
52
53 return esp_event_post(SYSTEM_EVENT, event->event_id, event, sizeof(*event), portMAX_DELAY);
54 }
55
esp_event_loop_init(system_event_cb_t cb,void * ctx)56 esp_err_t esp_event_loop_init(system_event_cb_t cb, void *ctx)
57 {
58 if (s_initialized) {
59 ESP_LOGE(TAG, "system event loop already initialized");
60 return ESP_ERR_INVALID_STATE;
61 }
62
63 esp_err_t err = esp_event_loop_create_default();
64 if (err != ESP_OK && err != ESP_ERR_INVALID_STATE) {
65 return err;
66 }
67
68 err = esp_event_handler_register(SYSTEM_EVENT, ESP_EVENT_ANY_ID, esp_event_post_to_user, NULL);
69 if (err != ESP_OK) {
70 return err;
71 }
72
73 s_initialized = true;
74 s_event_handler_cb = cb;
75 s_event_ctx = ctx;
76 return ESP_OK;
77 }
78
esp_event_loop_deinit(void)79 esp_err_t esp_event_loop_deinit(void)
80 {
81 if (!s_initialized) {
82 ESP_LOGE(TAG, "system event loop not initialized");
83 return ESP_ERR_INVALID_STATE;
84 }
85
86 esp_err_t err = esp_event_handler_unregister(SYSTEM_EVENT, ESP_EVENT_ANY_ID, esp_event_post_to_user);
87 if (err != ESP_OK) {
88 return err;
89 }
90
91 err = esp_event_loop_delete_default();
92 if (err != ESP_OK && err != ESP_ERR_INVALID_STATE) {
93 return err;
94 }
95
96 s_initialized = false;
97 s_event_handler_cb = NULL;
98 s_event_ctx = NULL;
99 return ESP_OK;
100 }
101