ChaosDMX
Open-Source DMX-Interface
Loading...
Searching...
No Matches
wifi.c
Go to the documentation of this file.
1#define LOG_TAG "WIFI"
2
3#include <string.h>
4
5#include "esp_event.h"
6#include "esp_netif.h"
7#include "esp_wifi.h"
8#include "logger.h"
9#include "nvs_flash.h"
10#include "wifi.h"
11
15static bool s_wifi_started = false;
16
29esp_err_t wifi_start_ap(const char *ssid, const char *password, uint8_t channel,
30 uint8_t max_connections) {
31 if (s_wifi_started) {
32 return ESP_OK;
33 }
34
35 if (!ssid || strlen(ssid) == 0 || strlen(ssid) > 32) {
36 return ESP_ERR_INVALID_ARG;
37 }
38
39 const bool has_password = password && strlen(password) > 0;
40 if (has_password && strlen(password) < 8) {
41 return ESP_ERR_INVALID_ARG;
42 }
43
44 esp_err_t err = nvs_flash_init();
45 if (err == ESP_ERR_NVS_NO_FREE_PAGES ||
46 err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
47 ESP_ERROR_CHECK(nvs_flash_erase());
48 err = nvs_flash_init();
49 }
50 if (err != ESP_OK) {
51 return err;
52 }
53
54 ESP_ERROR_CHECK(esp_netif_init());
55 ESP_ERROR_CHECK(esp_event_loop_create_default());
56 esp_netif_create_default_wifi_ap();
57
58 wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
59 ESP_ERROR_CHECK(esp_wifi_init(&cfg));
60
61 wifi_config_t wifi_config = {
62 .ap =
63 {
64 .channel = channel,
65 .max_connection = max_connections,
66 .authmode = has_password ? WIFI_AUTH_WPA2_PSK : WIFI_AUTH_OPEN,
67 .pmf_cfg =
68 {
69 .required = false,
70 },
71 },
72 };
73
74 strlcpy((char *)wifi_config.ap.ssid, ssid, sizeof(wifi_config.ap.ssid));
75 wifi_config.ap.ssid_len = strlen(ssid);
76
77 if (has_password) {
78 strlcpy((char *)wifi_config.ap.password, password,
79 sizeof(wifi_config.ap.password));
80 }
81
82 ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
83 ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config));
84 ESP_ERROR_CHECK(esp_wifi_start());
85
86 s_wifi_started = true;
87 LOGI("WiFi AP started: SSID=%s channel=%u", ssid, channel);
88 return ESP_OK;
89}
90
96void wifi_stop_ap(void) {
97 if (!s_wifi_started) {
98 return;
99 }
100
101 esp_wifi_stop();
102 esp_wifi_deinit();
103 s_wifi_started = false;
104 LOGI("WiFi AP stopped");
105}
Project-wide logging macros based on ESP-IDF's logging library.
#define LOGI(...)
Log a message at Info level.
Definition logger.h:42
void wifi_stop_ap(void)
Stop WiFi Access Point (AP) mode.
Definition wifi.c:96
esp_err_t wifi_start_ap(const char *ssid, const char *password, uint8_t channel, uint8_t max_connections)
Start WiFi Access Point (AP) mode.
Definition wifi.c:29
static bool s_wifi_started
Indicates whether the WiFi AP is started.
Definition wifi.c:15