2025-12-18 22:08:07 -06:00
|
|
|
#include "bmHTTP.hpp"
|
|
|
|
|
#include "esp_http_client.h"
|
2026-01-02 13:16:43 -06:00
|
|
|
#include "nvs_flash.h"
|
|
|
|
|
#include "defines.h"
|
2026-01-07 16:53:50 -06:00
|
|
|
#include "esp_crt_bundle.h"
|
2025-12-18 22:08:07 -06:00
|
|
|
|
|
|
|
|
std::string webToken;
|
2026-01-07 16:53:50 -06:00
|
|
|
const std::string urlBase = std::string("http") + (secureSrv ? "s" : "") + "://" + srvAddr + "/";
|
|
|
|
|
|
|
|
|
|
esp_err_t _http_event_handler(esp_http_client_event_t *evt) {
|
|
|
|
|
switch(evt->event_id) {
|
|
|
|
|
case HTTP_EVENT_ON_DATA: {
|
|
|
|
|
// Append received data to buffer (handles both chunked and non-chunked)
|
|
|
|
|
if (evt->data_len > 0 && evt->user_data != NULL) {
|
|
|
|
|
std::string* rxBuffer = (std::string*)evt->user_data;
|
|
|
|
|
rxBuffer->append((char*)evt->data, evt->data_len);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return ESP_OK;
|
|
|
|
|
}
|
2025-12-18 22:08:07 -06:00
|
|
|
|
|
|
|
|
bool httpGET(std::string endpoint, std::string token, cJSON* &JSONresponse) {
|
2026-01-07 16:53:50 -06:00
|
|
|
std::string url = urlBase + endpoint;
|
|
|
|
|
std::string responseBuffer = "";
|
2025-12-18 22:08:07 -06:00
|
|
|
|
|
|
|
|
esp_http_client_config_t config = {};
|
|
|
|
|
config.url = url.c_str();
|
2026-01-07 16:53:50 -06:00
|
|
|
config.event_handler = _http_event_handler; // Attach the bucket
|
|
|
|
|
config.user_data = &responseBuffer; // Pass pointer to our string so the handler can write to it
|
|
|
|
|
if (secureSrv) {
|
|
|
|
|
config.transport_type = HTTP_TRANSPORT_OVER_SSL;
|
|
|
|
|
config.crt_bundle_attach = esp_crt_bundle_attach;
|
|
|
|
|
}
|
2025-12-18 22:08:07 -06:00
|
|
|
|
|
|
|
|
esp_http_client_handle_t client = esp_http_client_init(&config);
|
|
|
|
|
|
|
|
|
|
// Add authorization header
|
|
|
|
|
std::string authHeader = "Bearer " + token;
|
|
|
|
|
esp_http_client_set_header(client, "Authorization", authHeader.c_str());
|
|
|
|
|
|
2025-12-23 17:21:44 -06:00
|
|
|
// Open connection and fetch headers
|
2026-01-07 16:53:50 -06:00
|
|
|
esp_err_t err = esp_http_client_perform(client);
|
2025-12-18 22:08:07 -06:00
|
|
|
bool success = false;
|
|
|
|
|
|
|
|
|
|
if (err == ESP_OK) {
|
2026-01-07 16:53:50 -06:00
|
|
|
int status_code = esp_http_client_get_status_code(client);
|
|
|
|
|
printf("Status = %d, Content Length = %d\n", status_code, esp_http_client_get_content_length(client));
|
2025-12-23 17:21:44 -06:00
|
|
|
|
2026-01-07 16:53:50 -06:00
|
|
|
if (status_code == 200) {
|
|
|
|
|
printf("Response: %s\n", responseBuffer.c_str());
|
|
|
|
|
JSONresponse = cJSON_Parse(responseBuffer.c_str());
|
|
|
|
|
if (JSONresponse) success = true;
|
2025-12-23 17:21:44 -06:00
|
|
|
}
|
2026-01-07 16:53:50 -06:00
|
|
|
} else {
|
|
|
|
|
printf("HTTP GET failed: %s\n", esp_err_to_name(err));
|
2025-12-18 22:08:07 -06:00
|
|
|
}
|
2026-01-07 16:53:50 -06:00
|
|
|
|
|
|
|
|
esp_http_client_cleanup(client);
|
|
|
|
|
return success;
|
2026-01-02 13:16:43 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void deleteWiFiAndTokenDetails() {
|
|
|
|
|
nvs_handle_t wifiHandle;
|
|
|
|
|
if (nvs_open(nvsWiFi, NVS_READWRITE, &wifiHandle) == ESP_OK) {
|
|
|
|
|
if (nvs_erase_all(wifiHandle) == ESP_OK) {
|
|
|
|
|
printf("Successfully erased WiFi details\n");
|
|
|
|
|
nvs_commit(wifiHandle);
|
|
|
|
|
}
|
|
|
|
|
else printf("ERROR: Erase wifi failed\n");
|
|
|
|
|
nvs_close(wifiHandle);
|
|
|
|
|
}
|
|
|
|
|
else printf("ERROR: Failed to open WiFi section for deletion\n");
|
|
|
|
|
|
|
|
|
|
nvs_handle_t authHandle;
|
|
|
|
|
if (nvs_open(nvsAuth, NVS_READWRITE, &authHandle) == ESP_OK) {
|
|
|
|
|
if (nvs_erase_all(authHandle) == ESP_OK) {
|
|
|
|
|
printf("Successfully erased Auth details\n");
|
|
|
|
|
nvs_commit(authHandle);
|
|
|
|
|
}
|
|
|
|
|
else printf("ERROR: Erase auth failed\n");
|
|
|
|
|
nvs_close(authHandle);
|
|
|
|
|
}
|
|
|
|
|
else printf("ERROR: Failed to open Auth section for deletion\n");
|
2025-12-18 22:08:07 -06:00
|
|
|
}
|