Skip to content

Commit 4dee04c

Browse files
committed
feat(esp_event): examples use esp_event instead of old event process
1 parent 562e183 commit 4dee04c

File tree

172 files changed

+1341
-3437
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

172 files changed

+1341
-3437
lines changed

examples/common_components/protocol_examples_common/connect.c

Lines changed: 68 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99

1010
#include <string.h>
11+
1112
#include "protocol_examples_common.h"
1213
#include "sdkconfig.h"
1314
#include "esp_event.h"
@@ -32,73 +33,79 @@
3233

3334
static EventGroupHandle_t s_connect_event_group;
3435
static ip4_addr_t s_ip_addr;
35-
static const char *s_connection_name;
36+
static char s_connection_name[32] = CONFIG_EXAMPLE_WIFI_SSID;
37+
static char s_connection_passwd[32] = CONFIG_EXAMPLE_WIFI_PASSWORD;
3638

3739
#ifdef CONFIG_EXAMPLE_CONNECT_IPV6
3840
static ip6_addr_t s_ipv6_addr;
3941
#endif
4042

4143
static const char *TAG = "example_connect";
4244

43-
static esp_err_t event_handler(void *ctx, system_event_t *event)
45+
static void on_wifi_disconnect(void *arg, esp_event_base_t event_base,
46+
int32_t event_id, void *event_data)
4447
{
45-
/* For accessing reason codes in case of disconnection */
46-
system_event_info_t *info = &event->event_info;
47-
48-
switch (event->event_id) {
49-
case SYSTEM_EVENT_STA_START:
50-
esp_wifi_connect();
51-
break;
52-
case SYSTEM_EVENT_STA_GOT_IP:
53-
memcpy(&s_ip_addr, &event->event_info.got_ip.ip_info.ip, sizeof(s_ip_addr));
54-
xEventGroupSetBits(s_connect_event_group, GOT_IPV4_BIT);;
55-
break;
48+
system_event_sta_disconnected_t *event = (system_event_sta_disconnected_t *)event_data;
49+
50+
ESP_LOGI(TAG, "Wi-Fi disconnected, trying to reconnect...");
51+
if (event->reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) {
52+
/*Switch to 802.11 bgn mode */
53+
esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N);
54+
}
55+
ESP_ERROR_CHECK(esp_wifi_connect());
56+
}
57+
5658
#ifdef CONFIG_EXAMPLE_CONNECT_IPV6
57-
case SYSTEM_EVENT_STA_CONNECTED:
58-
tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_STA);
59-
break;
60-
case SYSTEM_EVENT_AP_STA_GOT_IP6:
61-
memcpy(&s_ipv6_addr, &event->event_info.got_ip6.ip6_info, sizeof(s_ipv6_addr));
62-
xEventGroupSetBits(s_connect_event_group, GOT_IPV6_BIT);
63-
break;
59+
static void on_wifi_connect(void *arg, esp_event_base_t event_base,
60+
int32_t event_id, void *event_data)
61+
{
62+
tcpip_adapter_create_ip6_linklocal(TCPIP_ADAPTER_IF_STA);
63+
}
6464
#endif
65-
case SYSTEM_EVENT_STA_DISCONNECTED:
66-
ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason);
67-
if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) {
68-
/*Switch to 802.11 bgn mode */
69-
esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N);
70-
}
71-
esp_wifi_connect();
72-
xEventGroupClearBits(s_connect_event_group, GOT_IPV4_BIT);
65+
66+
static void on_got_ip(void *arg, esp_event_base_t event_base,
67+
int32_t event_id, void *event_data)
68+
{
69+
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
70+
memcpy(&s_ip_addr, &event->ip_info.ip, sizeof(s_ip_addr));
71+
xEventGroupSetBits(s_connect_event_group, GOT_IPV4_BIT);
72+
}
73+
7374
#ifdef CONFIG_EXAMPLE_CONNECT_IPV6
74-
xEventGroupClearBits(s_connect_event_group, GOT_IPV6_BIT);
75-
#endif
76-
break;
77-
default:
78-
break;
79-
}
80-
return ESP_OK;
75+
76+
static void on_got_ipv6(void *arg, esp_event_base_t event_base,
77+
int32_t event_id, void *event_data)
78+
{
79+
ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data;
80+
memcpy(&s_ipv6_addr, &event->ip6_info.ip, sizeof(s_ipv6_addr));
81+
xEventGroupSetBits(s_connect_event_group, GOT_IPV6_BIT);
8182
}
8283

84+
#endif // CONFIG_EXAMPLE_CONNECT_IPV6
85+
8386
static void start(void)
8487
{
85-
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
86-
8788
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
8889
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
8990

91+
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &on_wifi_disconnect, NULL));
92+
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &on_got_ip, NULL));
93+
#ifdef CONFIG_EXAMPLE_CONNECT_IPV6
94+
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &on_wifi_connect, NULL));
95+
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6, NULL));
96+
#endif
97+
9098
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
91-
wifi_config_t wifi_config = {
92-
.sta = {
93-
.ssid = CONFIG_EXAMPLE_WIFI_SSID,
94-
.password = CONFIG_EXAMPLE_WIFI_PASSWORD,
95-
},
96-
};
99+
wifi_config_t wifi_config = { 0 };
100+
101+
strncpy((char *)&wifi_config.sta.ssid, s_connection_name, 32);
102+
strncpy((char *)&wifi_config.sta.password, s_connection_passwd, 32);
103+
97104
ESP_LOGI(TAG, "Connecting to %s...", wifi_config.sta.ssid);
98105
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
99106
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
100107
ESP_ERROR_CHECK(esp_wifi_start());
101-
s_connection_name = CONFIG_EXAMPLE_WIFI_SSID;
108+
ESP_ERROR_CHECK(esp_wifi_connect());
102109
}
103110

104111
static void stop(void)
@@ -108,6 +115,14 @@ static void stop(void)
108115
return;
109116
}
110117
ESP_ERROR_CHECK(err);
118+
119+
ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &on_wifi_disconnect));
120+
ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &on_got_ip));
121+
#ifdef CONFIG_EXAMPLE_CONNECT_IPV6
122+
ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_GOT_IP6, &on_got_ipv6));
123+
ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &on_wifi_connect));
124+
#endif
125+
111126
ESP_ERROR_CHECK(esp_wifi_deinit());
112127
}
113128

@@ -137,6 +152,14 @@ esp_err_t example_disconnect(void)
137152
s_connect_event_group = NULL;
138153
stop();
139154
ESP_LOGI(TAG, "Disconnected from %s", s_connection_name);
140-
s_connection_name = NULL;
155+
s_connection_name[0] = '\0';
156+
return ESP_OK;
157+
}
158+
159+
esp_err_t example_set_connection_info(const char *ssid, const char *passwd)
160+
{
161+
strncpy(s_connection_name, ssid, sizeof(s_connection_name));
162+
strncpy(s_connection_passwd, passwd, sizeof(s_connection_passwd));
163+
141164
return ESP_OK;
142165
}

examples/common_components/protocol_examples_common/include/protocol_examples_common.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ esp_err_t example_disconnect(void);
4848
*/
4949
esp_err_t example_configure_stdin_stdout(void);
5050

51+
/**
52+
* @brief Configure SSID and password
53+
*/
54+
esp_err_t example_set_connection_info(const char *ssid, const char *passwd);
55+
5156
#ifdef __cplusplus
5257
}
5358
#endif

examples/protocols/coap_client/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,9 @@
22
# in this exact order for cmake to work correctly
33
cmake_minimum_required(VERSION 3.5)
44

5+
# (Not part of the boilerplate)
6+
# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection.
7+
set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common)
8+
59
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
610
project(coap_client)

examples/protocols/coap_client/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@
55

66
PROJECT_NAME := coap_client
77

8+
EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common
9+
810
include $(IDF_PATH)/make/project.mk
911

examples/protocols/coap_client/main/Kconfig.projbuild

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,4 @@ config TARGET_DOMAIN_URI
66
help
77
Target uri for the example to use.
88

9-
config WIFI_SSID
10-
string "WiFi SSID"
11-
default "myssid"
12-
help
13-
SSID (network name) for the example to connect to.
14-
15-
config WIFI_PASSWORD
16-
string "WiFi Password"
17-
default "mypassword"
18-
help
19-
WiFi password (WPA or WPA2) for the example to use.
20-
219
endmenu

examples/protocols/coap_client/main/coap_client_example_main.c

Lines changed: 10 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,16 @@
1313

1414
#include "freertos/FreeRTOS.h"
1515
#include "freertos/task.h"
16-
#include "freertos/event_groups.h"
17-
16+
#include "esp_system.h"
1817
#include "esp_log.h"
19-
#include "esp_wifi.h"
20-
#include "esp_event_loop.h"
18+
#include "esp_netif.h"
19+
#include "esp_event.h"
20+
#include "protocol_examples_common.h"
2121

2222
#include "nvs_flash.h"
2323

2424
#include "coap.h"
2525

26-
/* The examples use simple WiFi configuration that you can set via
27-
'make menuconfig'.
28-
29-
If you'd rather not, just change the below entries to strings with
30-
the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
31-
*/
32-
#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID
33-
#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD
34-
3526
#define COAP_DEFAULT_TIME_SEC 5
3627
#define COAP_DEFAULT_TIME_USEC 0
3728

@@ -43,13 +34,6 @@
4334
*/
4435
#define COAP_DEFAULT_DEMO_URI CONFIG_TARGET_DOMAIN_URI
4536

46-
static EventGroupHandle_t wifi_event_group;
47-
48-
/* The event group allows multiple bits for each event,
49-
but we only care about one event - are we connected
50-
to the AP with an IP? */
51-
const static int CONNECTED_BIT = BIT0;
52-
5337
const static char *TAG = "CoAP_client";
5438

5539
static void message_handler(struct coap_context_t *ctx, const coap_endpoint_t *local_interface, const coap_address_t *remote,
@@ -81,13 +65,6 @@ static void coap_example_task(void *p)
8165
uint8_t get_method = 1;
8266

8367
while (1) {
84-
/* Wait for the callback to set the CONNECTED_BIT in the
85-
event group.
86-
*/
87-
xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
88-
false, true, portMAX_DELAY);
89-
ESP_LOGI(TAG, "Connected to AP");
90-
9168
if (coap_split_uri((const uint8_t *)server_uri, strlen(server_uri), &uri) == -1) {
9269
ESP_LOGE(TAG, "CoAP server uri error");
9370
break;
@@ -157,55 +134,14 @@ static void coap_example_task(void *p)
157134
vTaskDelete(NULL);
158135
}
159136

160-
static esp_err_t wifi_event_handler(void *ctx, system_event_t *event)
137+
void app_main(void)
161138
{
162-
/* For accessing reason codes in case of disconnection */
163-
system_event_info_t *info = &event->event_info;
164-
165-
switch(event->event_id) {
166-
case SYSTEM_EVENT_STA_START:
167-
esp_wifi_connect();
168-
break;
169-
case SYSTEM_EVENT_STA_GOT_IP:
170-
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
171-
break;
172-
case SYSTEM_EVENT_STA_DISCONNECTED:
173-
ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason);
174-
if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) {
175-
/*Switch to 802.11 bgn mode */
176-
esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N);
177-
}
178-
esp_wifi_connect();
179-
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
180-
break;
181-
default:
182-
break;
183-
}
184-
return ESP_OK;
185-
}
139+
ESP_ERROR_CHECK(nvs_flash_init());
140+
ESP_ERROR_CHECK(esp_netif_init());
141+
ESP_ERROR_CHECK(esp_event_loop_create_default());
186142

187-
static void wifi_conn_init(void)
188-
{
189-
tcpip_adapter_init();
190-
wifi_event_group = xEventGroupCreate();
191-
ESP_ERROR_CHECK( esp_event_loop_init(wifi_event_handler, NULL) );
192-
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
193-
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
194-
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
195-
wifi_config_t wifi_config = {
196-
.sta = {
197-
.ssid = EXAMPLE_WIFI_SSID,
198-
.password = EXAMPLE_WIFI_PASS,
199-
},
200-
};
201-
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
202-
ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
203-
ESP_ERROR_CHECK( esp_wifi_start() );
204-
}
205143

206-
void app_main(void)
207-
{
208-
ESP_ERROR_CHECK( nvs_flash_init() );
209-
wifi_conn_init();
144+
ESP_ERROR_CHECK(example_connect());
145+
210146
xTaskCreate(coap_example_task, "coap", 2048, NULL, 5, NULL);
211147
}

examples/protocols/coap_server/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,9 @@
22
# in this exact order for cmake to work correctly
33
cmake_minimum_required(VERSION 3.5)
44

5+
# (Not part of the boilerplate)
6+
# This example uses an extra component for common functions such as Wi-Fi and Ethernet connection.
7+
set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common)
8+
59
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
610
project(coap_server)

examples/protocols/coap_server/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@
55

66
PROJECT_NAME := coap_server
77

8+
EXTRA_COMPONENT_DIRS = $(IDF_PATH)/examples/common_components/protocol_examples_common
9+
810
include $(IDF_PATH)/make/project.mk
911

examples/protocols/coap_server/main/Kconfig.projbuild

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)