3#include "AudioTools/CoreAudio/AudioHttp/AbstractURLStream.h"
4#include "AudioTools/CoreAudio/AudioHttp/HttpRequest.h"
5#include "AudioTools/CoreAudio/AudioHttp/ICYStreamT.h"
6#include "AudioTools/CoreAudio/AudioHttp/URLStreamBufferedT.h"
7#include "esp_crt_bundle.h"
8#include "esp_http_client.h"
9#include "esp_idf_version.h"
10#include "esp_system.h"
25 bool begin(
const char* ssid,
const char* password) {
27 if (is_open)
return true;
28 if (!setupWIFI(ssid, password)) {
29 LOGE(
"setupWIFI failed");
48 bool isConnected() {
return is_open; }
51 volatile bool is_open =
false;
55 bool setupWIFI(
const char* ssid,
const char* password) {
56 assert(ssid !=
nullptr);
57 assert(password !=
nullptr);
58 LOGI(
"setupWIFI: %s", ssid);
67 LOGE(
"esp_netif_init");
72 LOGE(
"esp_event_loop_create_default");
78 LOGE(
"esp_netif_create_default_wifi_sta");
83 &wifi_sta_event_handler,
this,
NULL);
85 &wifi_sta_event_handler,
this,
NULL);
88 LOGE(
"esp_wifi_init");
105 LOGE(
"esp_wifi_start");
117 LOGI(
"WIFI_EVENT_STA_START");
121 LOGI(
"WIFI_EVENT_STA_DISCONNECTED");
129 self->ip =
event->ip_info.ip;
130 self->is_open =
true;
131 LOGI(
"==> Station connected with IP: " IPSTR ", GW: " IPSTR
132 ", Mask: " IPSTR ".",
174 virtual bool begin(
const char* urlStr,
const char*
acceptMime =
"",
179 if (ssid !=
nullptr) {
180 if (!IDF_WIFI.begin(ssid, password)) {
184 if (!IDF_WIFI.isConnected()) {
186 Serial.print(
"Waiting for connection ");
187 while (!IDF_WIFI.isConnected()) {
196 actualURLStreamESP32 =
this;
204 LOGI(
"wifi country: %s",
text);
216 if (pem_cert !=
nullptr) {
221 if (crt_bundle_attach !=
nullptr) {
239 LOGE(
"Unsupported action: %d",
action);
244 if (client_handle ==
nullptr) {
252 for (
auto it = lines.begin(); it != lines.end(); ++it) {
255 (*it)->value.c_str());
261 LOGE(
"esp_http_client_open");
268 LOGI(
"status_code: %d / content_length: %d", status_code,
content_length);
276 return status_code == 200;
279 virtual void end()
override {
288 virtual void setSSID(
const char* ssid) { this->ssid = ssid; }
291 virtual void setPassword(
const char* password) { this->password = password; }
298 size_t write(
const uint8_t* data,
size_t len)
override {
303 size_t readBytes(uint8_t* data,
size_t len)
override {
305 return esp_http_client_read(client_handle, (
char*)data, len);
315 return request.getReplyHeader(key);
328 crt_bundle_attach =
cb;
333 buffer_size = size; }
346 bool is_power_save =
false;
347 const char* ssid =
nullptr;
348 const char* password =
nullptr;
349 int buffer_size = DEFAULT_BUFFER_SIZE;
350 const uint8_t* pem_cert =
nullptr;
351 int pem_cert_len = 0;
352 esp_err_t (*crt_bundle_attach)(
void *conf) =
nullptr;
361 assert(
cert[len - 1] == 0);
365 switch (
evt->event_id) {
367 LOGI(
"HTTP_EVENT_ERROR");
370 LOGD(
"HTTP_EVENT_ON_CONNECTED");
373 LOGD(
"HTTP_EVENT_HEADER_SENT");
376 LOGI(
"HTTP_EVENT_ON_HEADER, key=%s, value=%s",
evt->header_key,
379 actualURLStreamESP32->request.reply().put(
evt->header_key,
383 LOGD(
"HTTP_EVENT_ON_DATA, len=%d",
evt->data_len);
386 LOGI(
"HTTP_EVENT_ON_FINISH");
389 LOGI(
"HTTP_EVENT_DISCONNECTED");
391#if ESP_IDF_VERSION > ESP_IDF_VERSION_VAL(5, 3, 7)
393 LOGI(
"HTTP_EVENT_REDIRECT");