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 <string.h>
16 #include "esp_log.h"
17 #include "esp_event.h"
18 #include "esp_event_legacy.h"
19
20 #define TAG "event_send"
21
22 esp_err_t esp_event_send_noop(system_event_t *event);
23 extern esp_err_t esp_event_send_legacy(system_event_t *event) __attribute__((weak, alias("esp_event_send_noop")));
24 extern esp_err_t esp_event_send_to_default_loop(system_event_t *event) __attribute((weak, alias("esp_event_send_noop")));
25
esp_event_send_noop(system_event_t * event)26 esp_err_t esp_event_send_noop(system_event_t *event)
27 {
28 return ESP_OK;
29 }
30
esp_event_legacy_wifi_event_id(int32_t event_id)31 static system_event_id_t esp_event_legacy_wifi_event_id(int32_t event_id)
32 {
33 switch (event_id) {
34 case WIFI_EVENT_WIFI_READY:
35 return SYSTEM_EVENT_WIFI_READY;
36
37 case WIFI_EVENT_SCAN_DONE:
38 return SYSTEM_EVENT_SCAN_DONE;
39
40 case WIFI_EVENT_STA_START:
41 return SYSTEM_EVENT_STA_START;
42
43 case WIFI_EVENT_STA_STOP:
44 return SYSTEM_EVENT_STA_STOP;
45
46 case WIFI_EVENT_STA_CONNECTED:
47 return SYSTEM_EVENT_STA_CONNECTED;
48
49 case WIFI_EVENT_STA_DISCONNECTED:
50 return SYSTEM_EVENT_STA_DISCONNECTED;
51
52 case WIFI_EVENT_STA_AUTHMODE_CHANGE:
53 return SYSTEM_EVENT_STA_AUTHMODE_CHANGE;
54
55 case WIFI_EVENT_STA_BSS_RSSI_LOW:
56 return SYSTEM_EVENT_STA_BSS_RSSI_LOW;
57
58 case WIFI_EVENT_STA_WPS_ER_SUCCESS:
59 return SYSTEM_EVENT_STA_WPS_ER_SUCCESS;
60
61 case WIFI_EVENT_STA_WPS_ER_FAILED:
62 return SYSTEM_EVENT_STA_WPS_ER_FAILED;
63
64 case WIFI_EVENT_STA_WPS_ER_TIMEOUT:
65 return SYSTEM_EVENT_STA_WPS_ER_TIMEOUT;
66
67 case WIFI_EVENT_STA_WPS_ER_PIN:
68 return SYSTEM_EVENT_STA_WPS_ER_PIN;
69
70 case WIFI_EVENT_STA_WPS_ER_PBC_OVERLAP:
71 return SYSTEM_EVENT_STA_WPS_ER_PBC_OVERLAP;
72
73 case WIFI_EVENT_AP_START:
74 return SYSTEM_EVENT_AP_START;
75
76 case WIFI_EVENT_AP_STOP:
77 return SYSTEM_EVENT_AP_STOP;
78
79 case WIFI_EVENT_AP_STACONNECTED:
80 return SYSTEM_EVENT_AP_STACONNECTED;
81
82 case WIFI_EVENT_AP_STADISCONNECTED:
83 return SYSTEM_EVENT_AP_STADISCONNECTED;
84
85 case WIFI_EVENT_AP_PROBEREQRECVED:
86 return SYSTEM_EVENT_AP_PROBEREQRECVED;
87
88 case WIFI_EVENT_ACTION_TX_STATUS:
89 return SYSTEM_EVENT_ACTION_TX_STATUS;
90
91 case WIFI_EVENT_ROC_DONE:
92 return SYSTEM_EVENT_ROC_DONE;
93
94 case WIFI_EVENT_FTM_REPORT:
95 return SYSTEM_EVENT_FTM_REPORT;
96
97 case WIFI_EVENT_STA_BEACON_TIMEOUT:
98 return SYSTEM_EVENT_STA_BEACON_TIMEOUT;
99
100 default:
101 ESP_LOGE(TAG, "invalid wifi event id %d", event_id);
102 return SYSTEM_EVENT_MAX;
103 }
104 }
105
esp_event_legacy_ip_event_id(int32_t event_id)106 static system_event_id_t esp_event_legacy_ip_event_id(int32_t event_id)
107 {
108 switch (event_id) {
109 case IP_EVENT_STA_GOT_IP:
110 return SYSTEM_EVENT_STA_GOT_IP;
111
112 case IP_EVENT_STA_LOST_IP:
113 return SYSTEM_EVENT_STA_LOST_IP;
114
115 case IP_EVENT_AP_STAIPASSIGNED:
116 return SYSTEM_EVENT_AP_STAIPASSIGNED;
117
118 case IP_EVENT_GOT_IP6:
119 return SYSTEM_EVENT_GOT_IP6;
120
121 case IP_EVENT_ETH_GOT_IP:
122 return SYSTEM_EVENT_ETH_GOT_IP;
123
124 default:
125 ESP_LOGE(TAG, "invalid ip event id %d", event_id);
126 return SYSTEM_EVENT_MAX;
127 }
128 }
129
130
esp_event_legacy_event_id(esp_event_base_t event_base,int32_t event_id)131 static system_event_id_t esp_event_legacy_event_id(esp_event_base_t event_base, int32_t event_id)
132 {
133 if (event_base == WIFI_EVENT) {
134 return esp_event_legacy_wifi_event_id(event_id);
135 } else if (event_base == IP_EVENT) {
136 return esp_event_legacy_ip_event_id(event_id);
137 } else {
138 ESP_LOGE(TAG, "invalid event base %s", event_base);
139 return SYSTEM_EVENT_MAX;
140 }
141 }
142
esp_event_send(system_event_t * event)143 esp_err_t esp_event_send(system_event_t *event)
144 {
145 // send the event to the new style event loop
146 esp_err_t err = esp_event_send_to_default_loop(event);
147 if (err != ESP_OK) {
148 return err;
149 }
150
151 // send the event to the legacy event loop
152 err = esp_event_send_legacy(event);
153 if (err != ESP_OK) {
154 return err;
155 }
156
157 return ESP_OK;
158 }
159
esp_event_send_internal(esp_event_base_t event_base,int32_t event_id,void * event_data,size_t event_data_size,TickType_t ticks_to_wait)160 esp_err_t esp_event_send_internal(esp_event_base_t event_base,
161 int32_t event_id,
162 void* event_data,
163 size_t event_data_size,
164 TickType_t ticks_to_wait)
165 {
166 system_event_t event;
167
168 // send the event to the new style event loop
169 esp_err_t err = esp_event_post(event_base, event_id, event_data, event_data_size, ticks_to_wait);
170 if (err != ESP_OK) {
171 return err;
172 }
173
174 event.event_id = esp_event_legacy_event_id(event_base, event_id);
175
176 if (event_data) {
177 memcpy(&event.event_info, event_data, event_data_size);
178 }
179
180 return esp_event_send_legacy(&event);
181 }
182