// === Início de: components/rest_api/src/dashboard_api.c === #include "dashboard_api.h" #include "esp_log.h" #include "cJSON.h" #include "evse_api.h" #include "evse_error.h" static const char *TAG = "dashboard_api"; static esp_err_t dashboard_get_handler(httpd_req_t *req) { httpd_resp_set_type(req, "application/json"); // Cria o objeto JSON principal do dashboard cJSON *dashboard = cJSON_CreateObject(); // Status do sistema evse_state_t state = evse_get_state(); cJSON_AddStringToObject(dashboard, "status", evse_state_to_str(state)); // Carregador - informação do carregador 1 (adapte conforme necessário) cJSON *chargers = cJSON_CreateArray(); cJSON *charger1 = cJSON_CreateObject(); cJSON_AddNumberToObject(charger1, "id", 1); cJSON_AddStringToObject(charger1, "status", evse_state_to_str(state)); cJSON_AddNumberToObject(charger1, "current", evse_get_charging_current() / 10); cJSON_AddNumberToObject(charger1, "maxCurrent", evse_get_max_charging_current()); // Calcular a potência com base na corrente (considerando 230V) int power = (evse_get_charging_current() / 10) * 230; cJSON_AddNumberToObject(charger1, "power", power); cJSON_AddItemToArray(chargers, charger1); cJSON_AddItemToObject(dashboard, "chargers", chargers); // Consumo e tempo de carregamento cJSON_AddNumberToObject(dashboard, "energyConsumed", evse_get_consumption_limit()); cJSON_AddNumberToObject(dashboard, "chargingTime", evse_get_charging_time_limit()); // Alertas cJSON *alerts = cJSON_CreateArray(); if (evse_is_limit_reached()) { cJSON_AddItemToArray(alerts, cJSON_CreateString("Limite de consumo atingido.")); } if (!evse_is_available()) { cJSON_AddItemToArray(alerts, cJSON_CreateString("Estação indisponível.")); } if (!evse_is_enabled()) { cJSON_AddItemToArray(alerts, cJSON_CreateString("EVSE desativado.")); } cJSON_AddItemToObject(dashboard, "alerts", alerts); // Erros uint32_t error_bits = evse_get_error(); cJSON *errors = cJSON_CreateArray(); if (error_bits & EVSE_ERR_DIODE_SHORT_BIT) cJSON_AddItemToArray(errors, cJSON_CreateString("Diodo curto-circuitado")); if (error_bits & EVSE_ERR_LOCK_FAULT_BIT) cJSON_AddItemToArray(errors, cJSON_CreateString("Falha no travamento")); if (error_bits & EVSE_ERR_UNLOCK_FAULT_BIT) cJSON_AddItemToArray(errors, cJSON_CreateString("Falha no destravamento")); if (error_bits & EVSE_ERR_RCM_SELFTEST_FAULT_BIT) cJSON_AddItemToArray(errors, cJSON_CreateString("Falha no autoteste do RCM")); if (error_bits & EVSE_ERR_RCM_TRIGGERED_BIT) cJSON_AddItemToArray(errors, cJSON_CreateString("RCM disparado")); if (error_bits & EVSE_ERR_TEMPERATURE_HIGH_BIT) cJSON_AddItemToArray(errors, cJSON_CreateString("Temperatura elevada")); if (error_bits & EVSE_ERR_PILOT_FAULT_BIT) cJSON_AddItemToArray(errors, cJSON_CreateString("Erro no sinal piloto")); if (error_bits & EVSE_ERR_TEMPERATURE_FAULT_BIT) cJSON_AddItemToArray(errors, cJSON_CreateString("Falha no sensor de temperatura")); cJSON_AddItemToObject(dashboard, "errors", errors); // Enviar resposta JSON const char *json_str = cJSON_Print(dashboard); httpd_resp_sendstr(req, json_str); // Liberar memória free((void *)json_str); cJSON_Delete(dashboard); return ESP_OK; } void register_dashboard_handlers(httpd_handle_t server, void *ctx) { httpd_uri_t uri = { .uri = "/api/v1/dashboard", .method = HTTP_GET, .handler = dashboard_get_handler, .user_ctx = ctx }; httpd_register_uri_handler(server, &uri); } // === Fim de: components/rest_api/src/dashboard_api.c === // === Início de: components/rest_api/src/auth_api.c === // ========================= // auth_api.c // ========================= #include "auth_api.h" #include "auth.h" #include "esp_log.h" #include "cJSON.h" static const char *TAG = "auth_api"; static struct { char username[128]; } users[10] = { /*{"admin"}, {"user1"}*/ }; static int num_users = 2; static esp_err_t auth_methods_get_handler(httpd_req_t *req) { httpd_resp_set_type(req, "application/json"); cJSON *json = cJSON_CreateObject(); cJSON_AddBoolToObject(json, "RFID", auth_is_enabled() ); char *str = cJSON_PrintUnformatted(json); httpd_resp_sendstr(req, str); free(str); cJSON_Delete(json); return ESP_OK; } static esp_err_t auth_methods_post_handler(httpd_req_t *req) { char buf[256]; int len = httpd_req_recv(req, buf, sizeof(buf) - 1); if (len <= 0) { httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Erro ao receber dados"); return ESP_FAIL; } buf[len] = '\0'; cJSON *json = cJSON_Parse(buf); if (!json) { httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "JSON inválido"); return ESP_FAIL; } cJSON *rfid = cJSON_GetObjectItem(json, "RFID"); if (rfid && cJSON_IsBool(rfid)) { auth_set_enabled(cJSON_IsTrue(rfid)); } else { cJSON_Delete(json); httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Campo 'RFID' inválido ou ausente"); return ESP_FAIL; } cJSON_Delete(json); httpd_resp_sendstr(req, "Métodos de autenticação atualizados"); return ESP_OK; } static esp_err_t users_get_handler(httpd_req_t *req) { httpd_resp_set_type(req, "application/json"); cJSON *root = cJSON_CreateObject(); cJSON *list = cJSON_CreateArray(); for (int i = 0; i < num_users; ++i) { cJSON *u = cJSON_CreateObject(); cJSON_AddStringToObject(u, "username", users[i].username); cJSON_AddItemToArray(list, u); } cJSON_AddItemToObject(root, "users", list); char *str = cJSON_Print(root); httpd_resp_sendstr(req, str); free(str); cJSON_Delete(root); return ESP_OK; } static esp_err_t users_post_handler(httpd_req_t *req) { char buf[128]; int len = httpd_req_recv(req, buf, sizeof(buf) - 1); if (len <= 0) return ESP_FAIL; buf[len] = '\0'; if (num_users < 10) { strlcpy(users[num_users].username, buf, sizeof(users[num_users].username)); num_users++; httpd_resp_sendstr(req, "Usuário adicionado com sucesso"); } else { httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Limite de usuários atingido"); } return ESP_OK; } static esp_err_t users_delete_handler(httpd_req_t *req) { char query[128]; if (httpd_req_get_url_query_str(req, query, sizeof(query)) == ESP_OK) { char username[128]; if (httpd_query_key_value(query, "username", username, sizeof(username)) == ESP_OK) { for (int i = 0; i < num_users; i++) { if (strcmp(users[i].username, username) == 0) { for (int j = i; j < num_users - 1; j++) { users[j] = users[j + 1]; } num_users--; httpd_resp_sendstr(req, "Usuário removido com sucesso"); return ESP_OK; } } } } httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Usuário não encontrado"); return ESP_FAIL; } void register_auth_handlers(httpd_handle_t server, void *ctx) { httpd_register_uri_handler(server, &(httpd_uri_t){ .uri = "/api/v1/config/auth-methods", .method = HTTP_GET, .handler = auth_methods_get_handler, .user_ctx = ctx }); httpd_register_uri_handler(server, &(httpd_uri_t){ .uri = "/api/v1/config/auth-methods", .method = HTTP_POST, .handler = auth_methods_post_handler, .user_ctx = ctx }); httpd_register_uri_handler(server, &(httpd_uri_t){ .uri = "/api/v1/config/users", .method = HTTP_GET, .handler = users_get_handler, .user_ctx = ctx }); httpd_register_uri_handler(server, &(httpd_uri_t){ .uri = "/api/v1/config/users", .method = HTTP_POST, .handler = users_post_handler, .user_ctx = ctx }); httpd_register_uri_handler(server, &(httpd_uri_t){ .uri = "/api/v1/config/users", .method = HTTP_DELETE, .handler = users_delete_handler, .user_ctx = ctx }); } // === Fim de: components/rest_api/src/auth_api.c === // === Início de: components/rest_api/src/loadbalancing_settings_api.c === #include "loadbalancing_settings_api.h" #include "loadbalancer.h" #include "esp_log.h" #include "cJSON.h" static const char *TAG = "loadbalancing_settings_api"; // GET Handler: Retorna configurações atuais de load balancing static esp_err_t loadbalancing_config_get_handler(httpd_req_t *req) { bool enabled = loadbalancer_is_enabled(); uint8_t currentLimit = load_balancing_get_max_grid_current(); ESP_LOGI(TAG, "Fetching load balancing settings: enabled = %d, currentLimit = %u", enabled, currentLimit); httpd_resp_set_type(req, "application/json"); cJSON *config = cJSON_CreateObject(); cJSON_AddBoolToObject(config, "loadBalancingEnabled", enabled); cJSON_AddNumberToObject(config, "loadBalancingCurrentLimit", currentLimit); const char *json_str = cJSON_Print(config); httpd_resp_sendstr(req, json_str); ESP_LOGI(TAG, "Returned config: %s", json_str); free((void *)json_str); cJSON_Delete(config); return ESP_OK; } // POST Handler: Atualiza configurações de load balancing static esp_err_t loadbalancing_config_post_handler(httpd_req_t *req) { char buf[512]; int len = httpd_req_recv(req, buf, sizeof(buf) - 1); if (len <= 0) { ESP_LOGE(TAG, "Received empty POST body"); httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Empty body"); return ESP_FAIL; } buf[len] = '\0'; ESP_LOGI(TAG, "Received POST data: %s", buf); cJSON *json = cJSON_Parse(buf); if (!json) { ESP_LOGE(TAG, "Invalid JSON"); httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid JSON"); return ESP_FAIL; } // Atualizar estado habilitado cJSON *enabled_item = cJSON_GetObjectItem(json, "loadBalancingEnabled"); if (enabled_item && cJSON_IsBool(enabled_item)) { bool isEnabled = cJSON_IsTrue(enabled_item); loadbalancer_set_enabled(isEnabled); ESP_LOGI(TAG, "Updated loadBalancingEnabled to: %d", isEnabled); } // Atualizar limite de corrente cJSON *limit_item = cJSON_GetObjectItem(json, "loadBalancingCurrentLimit"); if (limit_item && cJSON_IsNumber(limit_item)) { uint8_t currentLimit = (uint8_t)limit_item->valuedouble; // Validar intervalo if (currentLimit < 6 || currentLimit > 100) { ESP_LOGW(TAG, "Rejected invalid currentLimit: %d", currentLimit); cJSON_Delete(json); httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid currentLimit (must be 6-100)"); return ESP_FAIL; } esp_err_t err = load_balancing_set_max_grid_current(currentLimit); if (err != ESP_OK) { ESP_LOGE(TAG, "Failed to save currentLimit: %s", esp_err_to_name(err)); cJSON_Delete(json); httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Failed to save setting"); return ESP_FAIL; } ESP_LOGI(TAG, "Updated loadBalancingCurrentLimit to: %d", currentLimit); } cJSON_Delete(json); httpd_resp_sendstr(req, "Load balancing settings updated successfully"); return ESP_OK; } // Registro dos handlers na API HTTP void register_loadbalancing_settings_handlers(httpd_handle_t server, void *ctx) { // GET httpd_uri_t get_uri = { .uri = "/api/v1/config/loadbalancing", .method = HTTP_GET, .handler = loadbalancing_config_get_handler, .user_ctx = ctx }; httpd_register_uri_handler(server, &get_uri); // POST httpd_uri_t post_uri = { .uri = "/api/v1/config/loadbalancing", .method = HTTP_POST, .handler = loadbalancing_config_post_handler, .user_ctx = ctx }; httpd_register_uri_handler(server, &post_uri); } // === Fim de: components/rest_api/src/loadbalancing_settings_api.c === // === Início de: components/rest_api/src/evse_settings_api.c === // ========================= // evse_settings_api.c // ========================= #include "evse_settings_api.h" #include "evse_api.h" #include "esp_log.h" #include "cJSON.h" static const char *TAG = "evse_settings_api"; static esp_err_t config_settings_get_handler(httpd_req_t *req) { httpd_resp_set_type(req, "application/json"); cJSON *config = cJSON_CreateObject(); cJSON_AddNumberToObject(config, "currentLimit", evse_get_max_charging_current()); cJSON_AddNumberToObject(config, "temperatureLimit", evse_get_temp_threshold()); const char *json_str = cJSON_Print(config); httpd_resp_sendstr(req, json_str); free((void *)json_str); cJSON_Delete(config); return ESP_OK; } static esp_err_t config_settings_post_handler(httpd_req_t *req) { char buf[512]; int len = httpd_req_recv(req, buf, sizeof(buf) - 1); if (len <= 0) { httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Empty body"); return ESP_FAIL; } buf[len] = '\0'; cJSON *json = cJSON_Parse(buf); if (!json) { httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "Invalid JSON"); return ESP_FAIL; } cJSON *current = cJSON_GetObjectItem(json, "currentLimit"); if (current) evse_set_max_charging_current(current->valueint); cJSON *temp = cJSON_GetObjectItem(json, "temperatureLimit"); if (temp) evse_set_temp_threshold(temp->valueint); cJSON_Delete(json); httpd_resp_sendstr(req, "Configurações atualizadas com sucesso"); return ESP_OK; } void register_evse_settings_handlers(httpd_handle_t server, void *ctx) { httpd_uri_t get_uri = { .uri = "/api/v1/config/settings", .method = HTTP_GET, .handler = config_settings_get_handler, .user_ctx = ctx }; httpd_register_uri_handler(server, &get_uri); httpd_uri_t post_uri = { .uri = "/api/v1/config/settings", .method = HTTP_POST, .handler = config_settings_post_handler, .user_ctx = ctx }; httpd_register_uri_handler(server, &post_uri); } // === Fim de: components/rest_api/src/evse_settings_api.c === // === Início de: components/rest_api/include/dashboard_api.h === #pragma once #ifdef __cplusplus extern "C" { #endif #include "esp_http_server.h" /** * @brief Registra o handler da dashboard (status geral do sistema) */ void register_dashboard_handlers(httpd_handle_t server, void *ctx); #ifdef __cplusplus } #endif // === Fim de: components/rest_api/include/dashboard_api.h === // === Início de: components/rest_api/include/static_file_api.h === #pragma once #ifdef __cplusplus extern "C" { #endif #include "esp_http_server.h" /** * @brief Registra o handler para servir arquivos estáticos da web (SPA) */ void register_static_file_handlers(httpd_handle_t server, void *ctx); #ifdef __cplusplus } #endif // === Fim de: components/rest_api/include/static_file_api.h === // === Início de: components/rest_api/include/network_api.h === #pragma once #ifdef __cplusplus extern "C" { #endif #include "esp_http_server.h" /** * @brief Registra os handlers de configuração Wi-Fi e MQTT */ void register_network_handlers(httpd_handle_t server, void *ctx); #ifdef __cplusplus } #endif // === Fim de: components/rest_api/include/network_api.h === // === Início de: components/rest_api/include/auth_api.h === #pragma once #ifdef __cplusplus extern "C" { #endif #include "esp_http_server.h" /** * @brief Registra os handlers de autenticação e gerenciamento de usuários */ void register_auth_handlers(httpd_handle_t server, void *ctx); #ifdef __cplusplus } #endif // === Fim de: components/rest_api/include/auth_api.h === // === Início de: components/rest_api/include/loadbalancing_settings_api.h === // ========================= // loadbalancing_settings_api.h // ========================= #ifndef LOADBALANCING_SETTINGS_API_H #define LOADBALANCING_SETTINGS_API_H #include "esp_err.h" #include "esp_http_server.h" // Função para registrar os manipuladores de URI para as configurações de load balancing e solar void register_loadbalancing_settings_handlers(httpd_handle_t server, void *ctx); #endif // LOADBALANCING_SETTINGS_API_H // === Fim de: components/rest_api/include/loadbalancing_settings_api.h === // === Início de: components/rest_api/include/rest_main.h === #pragma once #include #include #define SCRATCH_BUFSIZE (10240) typedef struct rest_server_context { char base_path[ESP_VFS_PATH_MAX + 1]; char scratch[SCRATCH_BUFSIZE]; } rest_server_context_t; esp_err_t rest_server_init(const char *base_path); // === Fim de: components/rest_api/include/rest_main.h === // === Início de: components/rest_api/include/meters_settings_api.h === // ========================= // meters_settings_api.h // ========================= #ifndef METERS_SETTINGS_API_H #define METERS_SETTINGS_API_H #include "esp_err.h" #include "esp_http_server.h" // Função para registrar os manipuladores de URI para as configurações dos contadores void register_meters_settings_handlers(httpd_handle_t server, void *ctx); #endif // METERS_SETTINGS_API_H // === Fim de: components/rest_api/include/meters_settings_api.h === // === Início de: components/rest_api/include/ocpp_api.h === #pragma once #ifdef __cplusplus extern "C" { #endif #include "esp_http_server.h" /** * @brief Registra os handlers da configuração e status do OCPP */ void register_ocpp_handlers(httpd_handle_t server, void *ctx); #ifdef __cplusplus } #endif // === Fim de: components/rest_api/include/ocpp_api.h === // === Início de: components/rest_api/include/evse_settings_api.h === #pragma once #ifdef __cplusplus extern "C" { #endif #include "esp_http_server.h" /** * @brief Registra os handlers de configuração elétrica e limites de carregamento */ void register_evse_settings_handlers(httpd_handle_t server, void *ctx); #ifdef __cplusplus } #endif // === Fim de: components/rest_api/include/evse_settings_api.h === // === Início de: components/network/src/wifi_2.c === #include #include "freertos/FreeRTOS.h" #include "freertos/event_groups.h" #include "esp_log.h" #include "esp_wifi.h" #include "esp_event.h" #include "esp_netif.h" #include "esp_mac.h" #include "nvs.h" #include "mdns.h" #include "wifi.h" #include "nvs_flash.h" #include #define WIFI_STORAGE_NAMESPACE "wifi_config" #define TAG "wifi" #define AP_SSID "plx-%02x%02x%02x" #define MDNS_HOSTNAME "plx%02x" #define NVS_NAMESPACE "wifi" static nvs_handle_t nvs; static esp_netif_t *ap_netif; EventGroupHandle_t wifi_event_group; // // Event handler para modo AP // static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { if (event_base == WIFI_EVENT) { switch (event_id) { case WIFI_EVENT_AP_STACONNECTED: { wifi_event_ap_staconnected_t *event = event_data; ESP_LOGI(TAG, "STA " MACSTR " conectou, AID=%d", MAC2STR(event->mac), event->aid); xEventGroupSetBits(wifi_event_group, WIFI_AP_CONNECTED_BIT); break; } case WIFI_EVENT_AP_STADISCONNECTED: { wifi_event_ap_stadisconnected_t *event = event_data; ESP_LOGI(TAG, "STA " MACSTR " desconectou, AID=%d", MAC2STR(event->mac), event->aid); xEventGroupClearBits(wifi_event_group, WIFI_AP_CONNECTED_BIT); break; } } } } // // Iniciar o AP com SSID baseado no MAC // void wifi_ap_start(void) { ESP_LOGI(TAG, "Iniciando AP"); ESP_ERROR_CHECK(esp_wifi_stop()); wifi_config_t ap_config = { .ap = { .ssid = "", .ssid_len = 0, .channel = 1, .password = "", .max_connection = 4, .authmode = WIFI_AUTH_OPEN } }; uint8_t mac[6]; ESP_ERROR_CHECK(esp_read_mac(mac, ESP_MAC_WIFI_SOFTAP)); snprintf((char *)ap_config.ap.ssid, sizeof(ap_config.ap.ssid), AP_SSID, mac[3], mac[4], mac[5]); ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP)); ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &ap_config)); ESP_ERROR_CHECK(esp_wifi_start()); xEventGroupSetBits(wifi_event_group, WIFI_AP_MODE_BIT); } // // Inicializar Wi-Fi em modo AP // void wifi_ini(void) { ESP_LOGI(TAG, "Inicializando Wi-Fi (modo AP)"); ESP_ERROR_CHECK(nvs_open(NVS_NAMESPACE, NVS_READWRITE, &nvs)); wifi_event_group = xEventGroupCreate(); ESP_ERROR_CHECK(esp_netif_init()); /* if (!esp_event_loop_is_running()) { ESP_ERROR_CHECK(esp_event_loop_create_default()); }*/ ap_netif = esp_netif_create_default_wifi_ap(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); uint8_t mac[6]; ESP_ERROR_CHECK(esp_read_mac(mac, ESP_MAC_WIFI_SOFTAP)); char hostname[16]; snprintf(hostname, sizeof(hostname), MDNS_HOSTNAME, mac[5]); ESP_ERROR_CHECK(mdns_init()); ESP_ERROR_CHECK(mdns_hostname_set(hostname)); ESP_ERROR_CHECK(mdns_instance_name_set("EVSE Controller")); wifi_ap_start(); } esp_netif_t *wifi_get_ap_netif(void) { return ap_netif; } esp_err_t wifi_set_config(bool enabled, const char *ssid, const char *password) { return ESP_OK; } void wifi_get_ssid(char *value) { // Your implementation here } void wifi_get_password(char *value) { // Your implementation here } bool wifi_get_enabled(void) { return true; } // === Fim de: components/network/src/wifi_2.c === // === Início de: components/network/src/wifi.c === #include #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/event_groups.h" #include "esp_log.h" #include "esp_wifi.h" #include "esp_event.h" #include "esp_netif.h" #include "esp_mac.h" #include "nvs.h" #include "mdns.h" #include "wifi.h" #define AP_SSID "plx-%02x%02x%02x" #define MDNS_SSID "plx%02x" #define NVS_NAMESPACE "wifi" #define NVS_ENABLED "enabled" #define NVS_SSID "ssid" #define NVS_PASSWORD "password" static const char *TAG = "wifi"; static nvs_handle_t nvs; static esp_netif_t *sta_netif; static esp_netif_t *ap_netif; EventGroupHandle_t wifi_event_group; static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { ESP_LOGI(TAG, "event_handler"); if (event_base == WIFI_EVENT) { if (event_id == WIFI_EVENT_AP_STACONNECTED) { ESP_LOGI(TAG, "STA connected"); wifi_event_ap_staconnected_t *event = (wifi_event_ap_staconnected_t *)event_data; ESP_LOGI(TAG, "WiFi AP " MACSTR " join, AID=%d", MAC2STR(event->mac), event->aid); xEventGroupClearBits(wifi_event_group, WIFI_AP_DISCONNECTED_BIT); xEventGroupSetBits(wifi_event_group, WIFI_AP_CONNECTED_BIT); } if (event_id == WIFI_EVENT_AP_STADISCONNECTED) { ESP_LOGI(TAG, "AP STA disconnected"); wifi_event_ap_stadisconnected_t *event = (wifi_event_ap_stadisconnected_t *)event_data; ESP_LOGI(TAG, "WiFi AP " MACSTR " leave, AID=%d", MAC2STR(event->mac), event->aid); xEventGroupClearBits(wifi_event_group, WIFI_AP_CONNECTED_BIT); xEventGroupSetBits(wifi_event_group, WIFI_AP_DISCONNECTED_BIT); } if (event_id == WIFI_EVENT_STA_DISCONNECTED) { ESP_LOGI(TAG, "STA disconnected"); xEventGroupClearBits(wifi_event_group, WIFI_STA_CONNECTED_BIT); xEventGroupSetBits(wifi_event_group, WIFI_STA_DISCONNECTED_BIT); esp_wifi_connect(); } if (event_id == WIFI_EVENT_STA_START) { ESP_LOGI(TAG, "STA start"); esp_wifi_connect(); } } else if (event_base == IP_EVENT) { ESP_LOGI(TAG, "event_base == IP_EVENT"); if (event_id == IP_EVENT_STA_GOT_IP || event_id == IP_EVENT_GOT_IP6) { if (event_id == IP_EVENT_STA_GOT_IP) { ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data; ESP_LOGI(TAG, "WiFi STA got ip: " IPSTR, IP2STR(&event->ip_info.ip)); } else { ip_event_got_ip6_t *event = (ip_event_got_ip6_t *)event_data; ESP_LOGI(TAG, "WiFi STA got ip6: " IPV6STR, IPV62STR(event->ip6_info.ip)); } xEventGroupClearBits(wifi_event_group, WIFI_STA_DISCONNECTED_BIT); xEventGroupSetBits(wifi_event_group, WIFI_STA_CONNECTED_BIT); } } } static void sta_set_config(void) { ESP_LOGI(TAG, "sta_set_config"); if (wifi_get_enabled()) { wifi_config_t wifi_config = { .sta = { .pmf_cfg = { .capable = true, .required = false}}}; wifi_get_ssid((char *)wifi_config.sta.ssid); wifi_get_password((char *)wifi_config.sta.password); esp_wifi_set_mode(WIFI_MODE_STA); esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config); } } static void ap_set_config(void) { ESP_LOGI(TAG, "ap_set_config"); wifi_config_t wifi_ap_config = { .ap = { .max_connection = 1, .authmode = WIFI_AUTH_OPEN}}; uint8_t mac[6]; esp_wifi_get_mac(ESP_IF_WIFI_AP, mac); sprintf((char *)wifi_ap_config.ap.ssid, AP_SSID, mac[3], mac[4], mac[5]); wifi_config_t wifi_sta_config = {0}; esp_wifi_set_mode(WIFI_MODE_APSTA); esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_ap_config); esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_sta_config); } static void sta_try_start(void) { ESP_LOGI(TAG, "sta_try_start"); sta_set_config(); if (wifi_get_enabled()) { ESP_LOGI(TAG, "Starting STA"); esp_wifi_start(); xEventGroupSetBits(wifi_event_group, WIFI_STA_MODE_BIT); } } void wifi_ini(void) { ESP_LOGI(TAG, "Wifi init"); ESP_ERROR_CHECK(nvs_open(NVS_NAMESPACE, NVS_READWRITE, &nvs)); wifi_event_group = xEventGroupCreate(); wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ap_netif = esp_netif_create_default_wifi_ap(); sta_netif = esp_netif_create_default_wifi_sta(); 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, ESP_EVENT_ANY_ID, &event_handler, NULL)); char chargeid[6]; uint8_t mac[6]; esp_wifi_get_mac(ESP_IF_WIFI_AP, mac); sprintf((char *)chargeid, MDNS_SSID, mac[5]); ESP_ERROR_CHECK(mdns_init()); ESP_ERROR_CHECK(mdns_hostname_set(chargeid)); ESP_ERROR_CHECK(mdns_instance_name_set("EVSE controller")); sta_try_start(); } esp_netif_t *wifi_get_sta_netif(void) { return sta_netif; } esp_netif_t *wifi_get_ap_netif(void) { return ap_netif; } esp_err_t wifi_set_config(bool enabled, const char *ssid, const char *password) { ESP_LOGI(TAG, "Wifi set config"); if (enabled) { if (ssid == NULL || strlen(ssid) == 0) { size_t len = 0; nvs_get_str(nvs, NVS_SSID, NULL, &len); if (len <= 1) { ESP_LOGE(TAG, "Required SSID"); return ESP_ERR_INVALID_ARG; } } } if (ssid != NULL && strlen(ssid) > 32) { ESP_LOGE(TAG, "SSID out of range"); return ESP_ERR_INVALID_ARG; } if (password != NULL && strlen(password) > 32) { ESP_LOGE(TAG, "Password out of range"); return ESP_ERR_INVALID_ARG; } nvs_set_u8(nvs, NVS_ENABLED, enabled); if (ssid != NULL) { nvs_set_str(nvs, NVS_SSID, ssid); } if (password != NULL) { nvs_set_str(nvs, NVS_PASSWORD, password); } nvs_commit(nvs); ESP_LOGI(TAG, "Stopping AP/STA"); xEventGroupClearBits(wifi_event_group, WIFI_AP_MODE_BIT | WIFI_STA_MODE_BIT); esp_wifi_stop(); sta_try_start(); return ESP_OK; } uint16_t wifi_scan(wifi_scan_ap_t *scan_aps) { ESP_LOGI(TAG, "wifi_scan"); uint16_t number = WIFI_SCAN_SCAN_LIST_SIZE; wifi_ap_record_t ap_info[WIFI_SCAN_SCAN_LIST_SIZE]; uint16_t ap_count = 0; memset(ap_info, 0, sizeof(ap_info)); esp_wifi_scan_start(NULL, true); esp_wifi_scan_get_ap_records(&number, ap_info); esp_wifi_scan_get_ap_num(&ap_count); ESP_LOGI(TAG, "wifi_scan --- %d", ap_count); for (int i = 0; (i < WIFI_SCAN_SCAN_LIST_SIZE) && (i < ap_count); i++) { ESP_LOGI(TAG, "wifi_scan ---"); strcpy(scan_aps[i].ssid, (const char *)ap_info[i].ssid); scan_aps[i].rssi = ap_info[i].rssi; scan_aps[i].auth = ap_info[i].authmode != WIFI_AUTH_OPEN; } return ap_count; } bool wifi_get_enabled(void) { uint8_t value = false; nvs_get_u8(nvs, NVS_ENABLED, &value); return value; } void wifi_get_ssid(char *value) { size_t len = 32; value[0] = '\0'; nvs_get_str(nvs, NVS_SSID, value, &len); } void wifi_get_password(char *value) { size_t len = 64; value[0] = '\0'; nvs_get_str(nvs, NVS_PASSWORD, value, &len); } void wifi_ap_start(void) { ESP_LOGI(TAG, "Starting AP"); xEventGroupClearBits(wifi_event_group, WIFI_STA_MODE_BIT); esp_wifi_stop(); ap_set_config(); esp_wifi_start(); xEventGroupSetBits(wifi_event_group, WIFI_AP_MODE_BIT); } void wifi_ap_stop(void) { ESP_LOGI(TAG, "Stopping AP"); xEventGroupClearBits(wifi_event_group, WIFI_AP_MODE_BIT); esp_wifi_stop(); sta_try_start(); } bool wifi_is_ap(void) { wifi_mode_t mode; esp_wifi_get_mode(&mode); return mode == WIFI_MODE_APSTA; } // === Fim de: components/network/src/wifi.c ===