2023-08-07 06:11:55 -04:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2006-2016 ARM Limited
|
|
|
|
* SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
|
2017-01-11 05:20:01 -05:00
|
|
|
*
|
2023-08-07 06:11:55 -04:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2017-01-11 05:20:01 -05:00
|
|
|
*/
|
2023-08-07 06:11:55 -04:00
|
|
|
|
2017-01-11 05:20:01 -05:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "freertos/event_groups.h"
|
|
|
|
#include "esp_wifi.h"
|
2023-08-07 06:11:55 -04:00
|
|
|
#include "esp_eap_client.h"
|
2018-11-20 05:26:53 -05:00
|
|
|
#include "esp_event.h"
|
2017-01-11 05:20:01 -05:00
|
|
|
#include "esp_log.h"
|
|
|
|
#include "esp_system.h"
|
|
|
|
#include "nvs_flash.h"
|
2019-09-01 12:25:23 -04:00
|
|
|
#include "esp_netif.h"
|
2017-01-11 05:20:01 -05:00
|
|
|
|
|
|
|
/* The examples use simple WiFi configuration that you can set via
|
2019-06-22 21:54:31 -04:00
|
|
|
project configuration menu.
|
2017-01-11 05:20:01 -05:00
|
|
|
|
|
|
|
If you'd rather not, just change the below entries to strings with
|
|
|
|
the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
|
|
|
|
|
2019-06-22 21:54:31 -04:00
|
|
|
You can choose EAP method via project configuration according to the
|
2017-01-11 05:20:01 -05:00
|
|
|
configuration of AP.
|
|
|
|
*/
|
2019-05-09 10:43:06 -04:00
|
|
|
#define EXAMPLE_WIFI_SSID CONFIG_EXAMPLE_WIFI_SSID
|
|
|
|
#define EXAMPLE_EAP_METHOD CONFIG_EXAMPLE_EAP_METHOD
|
2017-01-11 05:20:01 -05:00
|
|
|
|
2019-05-09 10:43:06 -04:00
|
|
|
#define EXAMPLE_EAP_ID CONFIG_EXAMPLE_EAP_ID
|
|
|
|
#define EXAMPLE_EAP_USERNAME CONFIG_EXAMPLE_EAP_USERNAME
|
|
|
|
#define EXAMPLE_EAP_PASSWORD CONFIG_EXAMPLE_EAP_PASSWORD
|
2017-01-11 05:20:01 -05:00
|
|
|
|
|
|
|
/* FreeRTOS event group to signal when we are connected & ready to make a request */
|
|
|
|
static EventGroupHandle_t wifi_event_group;
|
|
|
|
|
2019-09-01 12:25:23 -04:00
|
|
|
/* esp netif object representing the WIFI station */
|
|
|
|
static esp_netif_t *sta_netif = NULL;
|
|
|
|
|
2017-01-11 05:20:01 -05:00
|
|
|
/* The event group allows multiple bits for each event,
|
|
|
|
but we only care about one event - are we connected
|
|
|
|
to the AP with an IP? */
|
|
|
|
const int CONNECTED_BIT = BIT0;
|
|
|
|
|
|
|
|
static const char *TAG = "example";
|
|
|
|
|
2021-09-06 08:23:30 -04:00
|
|
|
/* CA cert, taken from ca.pem
|
|
|
|
Client cert, taken from client.crt
|
|
|
|
Client key, taken from client.key
|
2017-01-11 05:20:01 -05:00
|
|
|
|
|
|
|
The PEM, CRT and KEY file were provided by the person or organization
|
2023-08-07 06:11:55 -04:00
|
|
|
who configured the AP with wifi enterprise.
|
2017-01-11 05:20:01 -05:00
|
|
|
|
|
|
|
To embed it in the app binary, the PEM, CRT and KEY file is named
|
|
|
|
in the component.mk COMPONENT_EMBED_TXTFILES variable.
|
|
|
|
*/
|
2019-10-14 11:37:51 -04:00
|
|
|
#ifdef CONFIG_EXAMPLE_VALIDATE_SERVER_CERT
|
2021-09-06 08:23:30 -04:00
|
|
|
extern uint8_t ca_pem_start[] asm("_binary_ca_pem_start");
|
|
|
|
extern uint8_t ca_pem_end[] asm("_binary_ca_pem_end");
|
2019-10-14 11:37:51 -04:00
|
|
|
#endif /* CONFIG_EXAMPLE_VALIDATE_SERVER_CERT */
|
|
|
|
|
|
|
|
#ifdef CONFIG_EXAMPLE_EAP_METHOD_TLS
|
2021-09-06 08:23:30 -04:00
|
|
|
extern uint8_t client_crt_start[] asm("_binary_client_crt_start");
|
|
|
|
extern uint8_t client_crt_end[] asm("_binary_client_crt_end");
|
|
|
|
extern uint8_t client_key_start[] asm("_binary_client_key_start");
|
|
|
|
extern uint8_t client_key_end[] asm("_binary_client_key_end");
|
2019-10-14 11:37:51 -04:00
|
|
|
#endif /* CONFIG_EXAMPLE_EAP_METHOD_TLS */
|
2017-01-11 05:20:01 -05:00
|
|
|
|
2020-06-23 04:34:23 -04:00
|
|
|
#if defined CONFIG_EXAMPLE_EAP_METHOD_TTLS
|
|
|
|
esp_eap_ttls_phase2_types TTLS_PHASE2_METHOD = CONFIG_EXAMPLE_EAP_METHOD_TTLS_PHASE_2;
|
|
|
|
#endif /* CONFIG_EXAMPLE_EAP_METHOD_TTLS */
|
|
|
|
|
2020-11-10 02:40:01 -05:00
|
|
|
static void event_handler(void* arg, esp_event_base_t event_base,
|
2018-11-20 05:26:53 -05:00
|
|
|
int32_t event_id, void* event_data)
|
2017-01-11 05:20:01 -05:00
|
|
|
{
|
2018-11-20 05:26:53 -05:00
|
|
|
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
|
2017-01-11 05:20:01 -05:00
|
|
|
esp_wifi_connect();
|
2018-11-20 05:26:53 -05:00
|
|
|
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
|
2017-01-11 05:20:01 -05:00
|
|
|
esp_wifi_connect();
|
|
|
|
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
|
2018-11-20 05:26:53 -05:00
|
|
|
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
|
|
|
|
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
|
2017-01-11 05:20:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void initialise_wifi(void)
|
|
|
|
{
|
2019-10-14 11:37:51 -04:00
|
|
|
#ifdef CONFIG_EXAMPLE_VALIDATE_SERVER_CERT
|
2017-01-11 05:20:01 -05:00
|
|
|
unsigned int ca_pem_bytes = ca_pem_end - ca_pem_start;
|
2019-10-14 11:37:51 -04:00
|
|
|
#endif /* CONFIG_EXAMPLE_VALIDATE_SERVER_CERT */
|
|
|
|
|
|
|
|
#ifdef CONFIG_EXAMPLE_EAP_METHOD_TLS
|
2017-01-11 05:20:01 -05:00
|
|
|
unsigned int client_crt_bytes = client_crt_end - client_crt_start;
|
|
|
|
unsigned int client_key_bytes = client_key_end - client_key_start;
|
2019-10-14 11:37:51 -04:00
|
|
|
#endif /* CONFIG_EXAMPLE_EAP_METHOD_TLS */
|
2017-01-11 05:20:01 -05:00
|
|
|
|
2019-11-29 04:54:02 -05:00
|
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
2017-01-11 05:20:01 -05:00
|
|
|
wifi_event_group = xEventGroupCreate();
|
2018-11-20 05:26:53 -05:00
|
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
2019-12-02 00:54:51 -05:00
|
|
|
sta_netif = esp_netif_create_default_wifi_sta();
|
2019-10-17 05:36:34 -04:00
|
|
|
assert(sta_netif);
|
2019-09-01 12:25:23 -04:00
|
|
|
|
2017-01-11 05:20:01 -05:00
|
|
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
2023-08-07 06:11:55 -04:00
|
|
|
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
|
|
|
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL));
|
|
|
|
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL));
|
|
|
|
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
|
2017-01-11 05:20:01 -05:00
|
|
|
wifi_config_t wifi_config = {
|
|
|
|
.sta = {
|
|
|
|
.ssid = EXAMPLE_WIFI_SSID,
|
2023-08-07 06:11:55 -04:00
|
|
|
#if defined (CONFIG_EXAMPLE_WPA3_192BIT_ENTERPRISE) || defined (CONFIG_EXAMPLE_WPA3_ENTERPRISE)
|
2021-09-06 08:23:30 -04:00
|
|
|
.pmf_cfg = {
|
|
|
|
.required = true
|
|
|
|
},
|
|
|
|
#endif
|
2017-01-11 05:20:01 -05:00
|
|
|
},
|
|
|
|
};
|
|
|
|
ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid);
|
2023-08-07 06:11:55 -04:00
|
|
|
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
|
|
|
|
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config) );
|
|
|
|
ESP_ERROR_CHECK(esp_eap_client_set_identity((uint8_t *)EXAMPLE_EAP_ID, strlen(EXAMPLE_EAP_ID)) );
|
2019-10-14 11:37:51 -04:00
|
|
|
|
2021-09-06 08:23:30 -04:00
|
|
|
#if defined(CONFIG_EXAMPLE_VALIDATE_SERVER_CERT) || \
|
|
|
|
defined(CONFIG_EXAMPLE_WPA3_ENTERPRISE) || \
|
|
|
|
defined(CONFIG_EXAMPLE_WPA3_192BIT_ENTERPRISE)
|
2023-08-07 06:11:55 -04:00
|
|
|
ESP_ERROR_CHECK(esp_eap_client_set_ca_cert(ca_pem_start, ca_pem_bytes) );
|
2021-09-06 08:23:30 -04:00
|
|
|
#endif /* CONFIG_EXAMPLE_VALIDATE_SERVER_CERT */ /* EXAMPLE_WPA3_ENTERPRISE */
|
2019-10-14 11:37:51 -04:00
|
|
|
|
|
|
|
#ifdef CONFIG_EXAMPLE_EAP_METHOD_TLS
|
2023-08-07 06:11:55 -04:00
|
|
|
ESP_ERROR_CHECK(esp_eap_client_set_certificate_and_key(client_crt_start, client_crt_bytes,
|
|
|
|
client_key_start, client_key_bytes, NULL, 0) );
|
2019-10-14 11:37:51 -04:00
|
|
|
#endif /* CONFIG_EXAMPLE_EAP_METHOD_TLS */
|
|
|
|
|
2023-08-07 06:11:55 -04:00
|
|
|
#if defined (CONFIG_EXAMPLE_EAP_METHOD_PEAP) || \
|
|
|
|
defined (CONFIG_EXAMPLE_EAP_METHOD_TTLS)
|
|
|
|
ESP_ERROR_CHECK(esp_eap_client_set_username((uint8_t *)EXAMPLE_EAP_USERNAME, strlen(EXAMPLE_EAP_USERNAME)) );
|
|
|
|
ESP_ERROR_CHECK(esp_eap_client_set_password((uint8_t *)EXAMPLE_EAP_PASSWORD, strlen(EXAMPLE_EAP_PASSWORD)) );
|
2019-10-14 11:37:51 -04:00
|
|
|
#endif /* CONFIG_EXAMPLE_EAP_METHOD_PEAP || CONFIG_EXAMPLE_EAP_METHOD_TTLS */
|
2020-11-10 02:40:01 -05:00
|
|
|
|
2020-06-23 04:34:23 -04:00
|
|
|
#if defined CONFIG_EXAMPLE_EAP_METHOD_TTLS
|
2023-08-07 06:11:55 -04:00
|
|
|
ESP_ERROR_CHECK(esp_eap_client_set_ttls_phase2_method(TTLS_PHASE2_METHOD) );
|
2020-06-23 04:34:23 -04:00
|
|
|
#endif /* CONFIG_EXAMPLE_EAP_METHOD_TTLS */
|
2023-08-07 06:11:55 -04:00
|
|
|
|
2021-09-06 08:23:30 -04:00
|
|
|
#if defined (CONFIG_EXAMPLE_WPA3_192BIT_ENTERPRISE)
|
|
|
|
ESP_LOGI(TAG, "Enabling 192 bit certification");
|
2023-08-07 06:11:55 -04:00
|
|
|
ESP_ERROR_CHECK(esp_eap_client_set_suiteb_192bit_certification(true));
|
2022-02-08 04:50:13 -05:00
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_EXAMPLE_USE_DEFAULT_CERT_BUNDLE
|
2023-08-07 06:11:55 -04:00
|
|
|
ESP_ERROR_CHECK(esp_eap_client_use_default_cert_bundle(true));
|
2021-09-06 08:23:30 -04:00
|
|
|
#endif
|
2023-08-07 06:11:55 -04:00
|
|
|
ESP_ERROR_CHECK(esp_wifi_sta_enterprise_enable());
|
|
|
|
ESP_ERROR_CHECK(esp_wifi_start());
|
2017-01-11 05:20:01 -05:00
|
|
|
}
|
|
|
|
|
2023-08-07 06:11:55 -04:00
|
|
|
static void wifi_enterprise_example_task(void *pvParameters)
|
2017-01-11 05:20:01 -05:00
|
|
|
{
|
2019-09-01 12:25:23 -04:00
|
|
|
esp_netif_ip_info_t ip;
|
|
|
|
memset(&ip, 0, sizeof(esp_netif_ip_info_t));
|
2017-01-11 05:20:01 -05:00
|
|
|
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
|
|
|
|
2019-09-01 12:25:23 -04:00
|
|
|
if (esp_netif_get_ip_info(sta_netif, &ip) == 0) {
|
2017-01-11 05:20:01 -05:00
|
|
|
ESP_LOGI(TAG, "~~~~~~~~~~~");
|
|
|
|
ESP_LOGI(TAG, "IP:"IPSTR, IP2STR(&ip.ip));
|
|
|
|
ESP_LOGI(TAG, "MASK:"IPSTR, IP2STR(&ip.netmask));
|
|
|
|
ESP_LOGI(TAG, "GW:"IPSTR, IP2STR(&ip.gw));
|
|
|
|
ESP_LOGI(TAG, "~~~~~~~~~~~");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
void app_main(void)
|
2017-01-11 05:20:01 -05:00
|
|
|
{
|
2023-08-07 06:11:55 -04:00
|
|
|
ESP_ERROR_CHECK(nvs_flash_init());
|
2017-01-11 05:20:01 -05:00
|
|
|
initialise_wifi();
|
2023-08-07 06:11:55 -04:00
|
|
|
xTaskCreate(&wifi_enterprise_example_task, "wifi_enterprise_example_task", 4096, NULL, 5, NULL);
|
2017-01-11 05:20:01 -05:00
|
|
|
}
|