Updated to new esp-idf

This commit is contained in:
2023-02-27 17:26:55 +01:00
parent 4719a27c5d
commit 3ca4171a13
3 changed files with 47 additions and 50 deletions

View File

@@ -11,7 +11,6 @@ set(dependencies
espchrono espchrono
espcpputils espcpputils
esp_http_client esp_http_client
expected
fmt fmt
) )

View File

@@ -48,20 +48,20 @@ AsyncHttpRequest::~AsyncHttpRequest()
endTask(); endTask();
} }
tl::expected<void, std::string> AsyncHttpRequest::startTask() std::expected<void, std::string> AsyncHttpRequest::startTask()
{ {
if (m_taskHandle) if (m_taskHandle)
{ {
constexpr auto msg = "http task handle is not null"; constexpr auto msg = "http task handle is not null";
ESP_LOGW(TAG, "%s", msg); ESP_LOGW(TAG, "%s", msg);
return tl::make_unexpected(msg); return std::unexpected(msg);
} }
if (const auto bits = m_eventGroup.getBits(); bits & TASK_RUNNING_BIT) if (const auto bits = m_eventGroup.getBits(); bits & TASK_RUNNING_BIT)
{ {
constexpr auto msg = "http task already running"; constexpr auto msg = "http task already running";
ESP_LOGW(TAG, "%s", msg); ESP_LOGW(TAG, "%s", msg);
return tl::make_unexpected(msg); return std::unexpected(msg);
} }
m_eventGroup.clearBits(TASK_RUNNING_BIT | START_REQUEST_BIT | REQUEST_RUNNING_BIT | REQUEST_FINISHED_BIT | END_TASK_BIT | TASK_ENDED_BIT | ABORT_REQUEST_BIT); m_eventGroup.clearBits(TASK_RUNNING_BIT | START_REQUEST_BIT | REQUEST_RUNNING_BIT | REQUEST_FINISHED_BIT | END_TASK_BIT | TASK_ENDED_BIT | ABORT_REQUEST_BIT);
@@ -71,14 +71,14 @@ tl::expected<void, std::string> AsyncHttpRequest::startTask()
{ {
auto msg = fmt::format("failed creating http task {}", result); auto msg = fmt::format("failed creating http task {}", result);
ESP_LOGE(TAG, "%.*s", msg.size(), msg.data()); ESP_LOGE(TAG, "%.*s", msg.size(), msg.data());
return tl::make_unexpected(std::move(msg)); return std::unexpected(std::move(msg));
} }
if (!m_taskHandle) if (!m_taskHandle)
{ {
constexpr auto msg = "http task handle is null"; constexpr auto msg = "http task handle is null";
ESP_LOGW(TAG, "%s", msg); ESP_LOGW(TAG, "%s", msg);
return tl::make_unexpected(msg); return std::unexpected(msg);
} }
ESP_LOGD(TAG, "created http task %s", m_taskName); ESP_LOGD(TAG, "created http task %s", m_taskName);
@@ -97,7 +97,7 @@ tl::expected<void, std::string> AsyncHttpRequest::startTask()
return {}; return {};
} }
tl::expected<void, std::string> AsyncHttpRequest::endTask() std::expected<void, std::string> AsyncHttpRequest::endTask()
{ {
if (const auto bits = m_eventGroup.getBits(); if (const auto bits = m_eventGroup.getBits();
!(bits & TASK_RUNNING_BIT)) !(bits & TASK_RUNNING_BIT))
@@ -106,7 +106,7 @@ tl::expected<void, std::string> AsyncHttpRequest::endTask()
{ {
constexpr auto msg = "Another end request is already pending"; constexpr auto msg = "Another end request is already pending";
ESP_LOGE(TAG, "%s", msg); ESP_LOGE(TAG, "%s", msg);
return tl::make_unexpected(msg); return std::unexpected(msg);
} }
m_eventGroup.setBits(END_TASK_BIT); m_eventGroup.setBits(END_TASK_BIT);
@@ -139,14 +139,14 @@ bool AsyncHttpRequest::taskRunning() const
return false; return false;
} }
tl::expected<void, std::string> AsyncHttpRequest::createClient(std::string_view url, esp_http_client_method_t method, std::expected<void, std::string> AsyncHttpRequest::createClient(std::string_view url, esp_http_client_method_t method,
int timeout_ms) int timeout_ms)
{ {
if (m_client) if (m_client)
{ {
constexpr auto msg = "m_client already created"; constexpr auto msg = "m_client already created";
ESP_LOGE(TAG, "%s", msg); ESP_LOGE(TAG, "%s", msg);
return tl::make_unexpected(msg); return std::unexpected(msg);
} }
esp_http_client_config_t config{}; esp_http_client_config_t config{};
@@ -164,7 +164,7 @@ tl::expected<void, std::string> AsyncHttpRequest::createClient(std::string_view
{ {
auto msg = fmt::format("http client could not be constructed (url={})", url); auto msg = fmt::format("http client could not be constructed (url={})", url);
ESP_LOGE(TAG, "%.*s", msg.size(), msg.data()); ESP_LOGE(TAG, "%.*s", msg.size(), msg.data());
return tl::make_unexpected(std::move(msg)); return std::unexpected(std::move(msg));
} }
ESP_LOGD(TAG, "created http client %s", m_taskName); ESP_LOGD(TAG, "created http client %s", m_taskName);
@@ -172,7 +172,7 @@ tl::expected<void, std::string> AsyncHttpRequest::createClient(std::string_view
return {}; return {};
} }
tl::expected<void, std::string> AsyncHttpRequest::deleteClient() std::expected<void, std::string> AsyncHttpRequest::deleteClient()
{ {
if (!m_client) if (!m_client)
return {}; return {};
@@ -181,7 +181,7 @@ tl::expected<void, std::string> AsyncHttpRequest::deleteClient()
{ {
constexpr auto msg = "request still in progress"; constexpr auto msg = "request still in progress";
ESP_LOGW(TAG, "%s", msg); ESP_LOGW(TAG, "%s", msg);
return tl::make_unexpected(msg); return std::unexpected(msg);
} }
m_client = {}; m_client = {};
@@ -194,7 +194,7 @@ bool AsyncHttpRequest::hasClient() const
return m_client; return m_client;
} }
tl::expected<void, std::string> AsyncHttpRequest::start(std::string_view url, std::expected<void, std::string> AsyncHttpRequest::start(std::string_view url,
esp_http_client_method_t method, esp_http_client_method_t method,
const std::map<std::string, std::string> &requestHeaders, const std::map<std::string, std::string> &requestHeaders,
std::string_view requestBody, int timeout_ms) std::string_view requestBody, int timeout_ms)
@@ -202,14 +202,14 @@ tl::expected<void, std::string> AsyncHttpRequest::start(std::string_view url,
if (!m_taskHandle) if (!m_taskHandle)
{ {
if (auto result = startTask(); !result) if (auto result = startTask(); !result)
return tl::make_unexpected(std::move(result).error()); return std::unexpected(std::move(result).error());
} }
if (inProgress()) if (inProgress())
{ {
constexpr auto msg = "another request still in progress"; constexpr auto msg = "another request still in progress";
ESP_LOGW(TAG, "%s", msg); ESP_LOGW(TAG, "%s", msg);
return tl::make_unexpected(msg); return std::unexpected(msg);
} }
if (m_client) if (m_client)
@@ -219,14 +219,14 @@ tl::expected<void, std::string> AsyncHttpRequest::start(std::string_view url,
} }
if (auto result = createClient(url, method, timeout_ms); !result) if (auto result = createClient(url, method, timeout_ms); !result)
return tl::make_unexpected(std::move(result).error()); return std::unexpected(std::move(result).error());
for (auto iter = std::cbegin(requestHeaders); iter != std::cend(requestHeaders); iter++) for (auto iter = std::cbegin(requestHeaders); iter != std::cend(requestHeaders); iter++)
if (const auto result = m_client.set_header(iter->first, iter->second); result != ESP_OK) if (const auto result = m_client.set_header(iter->first, iter->second); result != ESP_OK)
{ {
auto msg = fmt::format("m_client.set_header() failed: {} ({} {})", esp_err_to_name(result), iter->first, iter->second); auto msg = fmt::format("m_client.set_header() failed: {} ({} {})", esp_err_to_name(result), iter->first, iter->second);
ESP_LOGW(TAG, "%.*s", msg.size(), msg.data()); ESP_LOGW(TAG, "%.*s", msg.size(), msg.data());
return tl::make_unexpected(std::move(msg)); return std::unexpected(std::move(msg));
} }
if (!requestBody.empty()) if (!requestBody.empty())
@@ -235,19 +235,19 @@ tl::expected<void, std::string> AsyncHttpRequest::start(std::string_view url,
{ {
auto msg = fmt::format("m_client.open() failed: {} ({})", esp_err_to_name(result), requestBody.size()); auto msg = fmt::format("m_client.open() failed: {} ({})", esp_err_to_name(result), requestBody.size());
ESP_LOGW(TAG, "%.*s", msg.size(), msg.data()); ESP_LOGW(TAG, "%.*s", msg.size(), msg.data());
return tl::make_unexpected(std::move(msg)); return std::unexpected(std::move(msg));
} }
if (const auto written = m_client.write(requestBody); written < 0) if (const auto written = m_client.write(requestBody); written < 0)
{ {
auto msg = fmt::format("m_client.write() failed: {}", written); auto msg = fmt::format("m_client.write() failed: {}", written);
ESP_LOGW(TAG, "%.*s", msg.size(), msg.data()); ESP_LOGW(TAG, "%.*s", msg.size(), msg.data());
return tl::make_unexpected(std::move(msg)); return std::unexpected(std::move(msg));
} }
else if (written != requestBody.size()) else if (written != requestBody.size())
{ {
auto msg = fmt::format("m_client.write() written size mismatch: {} != {}", written, requestBody.size()); auto msg = fmt::format("m_client.write() written size mismatch: {} != {}", written, requestBody.size());
ESP_LOGW(TAG, "%.*s", msg.size(), msg.data()); ESP_LOGW(TAG, "%.*s", msg.size(), msg.data());
return tl::make_unexpected(std::move(msg)); return std::unexpected(std::move(msg));
} }
} }
@@ -259,7 +259,7 @@ tl::expected<void, std::string> AsyncHttpRequest::start(std::string_view url,
return {}; return {};
} }
tl::expected<void, std::string> AsyncHttpRequest::retry(std::optional<std::string_view> url, std::expected<void, std::string> AsyncHttpRequest::retry(std::optional<std::string_view> url,
std::optional<esp_http_client_method_t> method, std::optional<esp_http_client_method_t> method,
const std::map<std::string, std::string> &requestHeaders, const std::map<std::string, std::string> &requestHeaders,
std::string_view requestBody std::string_view requestBody
@@ -271,21 +271,21 @@ tl::expected<void, std::string> AsyncHttpRequest::retry(std::optional<std::strin
if (!m_taskHandle) if (!m_taskHandle)
{ {
if (auto result = startTask(); !result) if (auto result = startTask(); !result)
return tl::make_unexpected(std::move(result).error()); return std::unexpected(std::move(result).error());
} }
if (inProgress()) if (inProgress())
{ {
constexpr auto msg = "another request still in progress"; constexpr auto msg = "another request still in progress";
ESP_LOGW(TAG, "%s", msg); ESP_LOGW(TAG, "%s", msg);
return tl::make_unexpected(msg); return std::unexpected(msg);
} }
if (!m_client) if (!m_client)
{ {
constexpr auto msg = "http client is null"; constexpr auto msg = "http client is null";
ESP_LOGW(TAG, "%s", msg); ESP_LOGW(TAG, "%s", msg);
return tl::make_unexpected(msg); return std::unexpected(msg);
} }
if (url) if (url)
@@ -293,7 +293,7 @@ tl::expected<void, std::string> AsyncHttpRequest::retry(std::optional<std::strin
{ {
auto msg = fmt::format("m_client.set_url() failed: {} ({})", esp_err_to_name(result), *url); auto msg = fmt::format("m_client.set_url() failed: {} ({})", esp_err_to_name(result), *url);
ESP_LOGW(TAG, "%.*s", msg.size(), msg.data()); ESP_LOGW(TAG, "%.*s", msg.size(), msg.data());
return tl::make_unexpected(std::move(msg)); return std::unexpected(std::move(msg));
} }
if (method) if (method)
@@ -301,7 +301,7 @@ tl::expected<void, std::string> AsyncHttpRequest::retry(std::optional<std::strin
{ {
auto msg = fmt::format("m_client.set_method() failed: {}", esp_err_to_name(result)); auto msg = fmt::format("m_client.set_method() failed: {}", esp_err_to_name(result));
ESP_LOGW(TAG, "%.*s", msg.size(), msg.data()); ESP_LOGW(TAG, "%.*s", msg.size(), msg.data());
return tl::make_unexpected(std::move(msg)); return std::unexpected(std::move(msg));
} }
#ifndef OLD_IDF #ifndef OLD_IDF
@@ -310,7 +310,7 @@ tl::expected<void, std::string> AsyncHttpRequest::retry(std::optional<std::strin
{ {
auto msg = fmt::format("m_client.set_timeout_ms() failed: {}", esp_err_to_name(result)); auto msg = fmt::format("m_client.set_timeout_ms() failed: {}", esp_err_to_name(result));
ESP_LOGW(TAG, "%.*s", msg.size(), msg.data()); ESP_LOGW(TAG, "%.*s", msg.size(), msg.data());
return tl::make_unexpected(std::move(msg)); return std::unexpected(std::move(msg));
} }
#endif #endif
@@ -319,7 +319,7 @@ tl::expected<void, std::string> AsyncHttpRequest::retry(std::optional<std::strin
{ {
auto msg = fmt::format("m_client.set_header() failed: {} ({} {})", esp_err_to_name(result), iter->first, iter->second); auto msg = fmt::format("m_client.set_header() failed: {} ({} {})", esp_err_to_name(result), iter->first, iter->second);
ESP_LOGW(TAG, "%.*s", msg.size(), msg.data()); ESP_LOGW(TAG, "%.*s", msg.size(), msg.data());
return tl::make_unexpected(std::move(msg)); return std::unexpected(std::move(msg));
} }
if (!requestBody.empty()) if (!requestBody.empty())
@@ -328,19 +328,19 @@ tl::expected<void, std::string> AsyncHttpRequest::retry(std::optional<std::strin
{ {
auto msg = fmt::format("m_client.open() failed: {} ({})", esp_err_to_name(result), requestBody.size()); auto msg = fmt::format("m_client.open() failed: {} ({})", esp_err_to_name(result), requestBody.size());
ESP_LOGW(TAG, "%.*s", msg.size(), msg.data()); ESP_LOGW(TAG, "%.*s", msg.size(), msg.data());
return tl::make_unexpected(std::move(msg)); return std::unexpected(std::move(msg));
} }
if (const auto written = m_client.write(requestBody); written < 0) if (const auto written = m_client.write(requestBody); written < 0)
{ {
auto msg = fmt::format("m_client.write() failed: {}", written); auto msg = fmt::format("m_client.write() failed: {}", written);
ESP_LOGW(TAG, "%.*s", msg.size(), msg.data()); ESP_LOGW(TAG, "%.*s", msg.size(), msg.data());
return tl::make_unexpected(std::move(msg)); return std::unexpected(std::move(msg));
} }
else if (written != requestBody.size()) else if (written != requestBody.size())
{ {
auto msg = fmt::format("m_client.write() written size mismatch: {} != {}", written, requestBody.size()); auto msg = fmt::format("m_client.write() written size mismatch: {} != {}", written, requestBody.size());
ESP_LOGW(TAG, "%.*s", msg.size(), msg.data()); ESP_LOGW(TAG, "%.*s", msg.size(), msg.data());
return tl::make_unexpected(std::move(msg)); return std::unexpected(std::move(msg));
} }
} }
@@ -352,12 +352,12 @@ tl::expected<void, std::string> AsyncHttpRequest::retry(std::optional<std::strin
return {}; return {};
} }
tl::expected<void, std::string> AsyncHttpRequest::abort() std::expected<void, std::string> AsyncHttpRequest::abort()
{ {
if (const auto bits = m_eventGroup.getBits(); !(bits & (START_REQUEST_BIT | REQUEST_RUNNING_BIT))) if (const auto bits = m_eventGroup.getBits(); !(bits & (START_REQUEST_BIT | REQUEST_RUNNING_BIT)))
return tl::make_unexpected("no ota job is running!"); return std::unexpected("no ota job is running!");
else if (bits & ABORT_REQUEST_BIT) else if (bits & ABORT_REQUEST_BIT)
return tl::make_unexpected("an abort has already been requested!"); return std::unexpected("an abort has already been requested!");
m_eventGroup.setBits(ABORT_REQUEST_BIT); m_eventGroup.setBits(ABORT_REQUEST_BIT);
ESP_LOGI(TAG, "http request abort requested"); ESP_LOGI(TAG, "http request abort requested");
@@ -375,24 +375,24 @@ bool AsyncHttpRequest::finished() const
return m_eventGroup.getBits() & REQUEST_FINISHED_BIT; return m_eventGroup.getBits() & REQUEST_FINISHED_BIT;
} }
tl::expected<void, std::string> AsyncHttpRequest::result() const std::expected<void, std::string> AsyncHttpRequest::result() const
{ {
if (const auto bits = m_eventGroup.getBits(); if (const auto bits = m_eventGroup.getBits();
bits & REQUEST_RUNNING_BIT) bits & REQUEST_RUNNING_BIT)
{ {
constexpr auto msg = "request still running"; constexpr auto msg = "request still running";
ESP_LOGW(TAG, "%s", msg); ESP_LOGW(TAG, "%s", msg);
return tl::make_unexpected(msg); return std::unexpected(msg);
} }
else if (!(bits & REQUEST_FINISHED_BIT)) else if (!(bits & REQUEST_FINISHED_BIT))
{ {
constexpr auto msg = "request not finished"; constexpr auto msg = "request not finished";
ESP_LOGW(TAG, "%s", msg); ESP_LOGW(TAG, "%s", msg);
return tl::make_unexpected(msg); return std::unexpected(msg);
} }
if (m_result != ESP_OK) if (m_result != ESP_OK)
return tl::make_unexpected(fmt::format("http request failed: {}", esp_err_to_name(m_result))); return std::unexpected(fmt::format("http request failed: {}", esp_err_to_name(m_result)));
return {}; return {};
} }

View File

@@ -5,15 +5,13 @@
#include <string_view> #include <string_view>
#include <map> #include <map>
#include <optional> #include <optional>
#include <expected>
// esp-idf includes // esp-idf includes
#include <freertos/FreeRTOS.h> #include <freertos/FreeRTOS.h>
#include <freertos/task.h> #include <freertos/task.h>
#include <esp_err.h> #include <esp_err.h>
// 3rdparty lib includes
#include <tl/expected.hpp>
// local includes // local includes
#include "wrappers/http_client.h" #include "wrappers/http_client.h"
#include "wrappers/event_group.h" #include "wrappers/event_group.h"
@@ -25,21 +23,21 @@ public:
AsyncHttpRequest(const char *taskName="httpRequestTask", espcpputils::CoreAffinity coreAffinity=espcpputils::CoreAffinity::Core1); AsyncHttpRequest(const char *taskName="httpRequestTask", espcpputils::CoreAffinity coreAffinity=espcpputils::CoreAffinity::Core1);
~AsyncHttpRequest(); ~AsyncHttpRequest();
tl::expected<void, std::string> startTask(); std::expected<void, std::string> startTask();
tl::expected<void, std::string> endTask(); std::expected<void, std::string> endTask();
bool taskRunning() const; bool taskRunning() const;
tl::expected<void, std::string> createClient(std::string_view url, std::expected<void, std::string> createClient(std::string_view url,
esp_http_client_method_t method = HTTP_METHOD_GET, esp_http_client_method_t method = HTTP_METHOD_GET,
int timeout_ms = 0); int timeout_ms = 0);
tl::expected<void, std::string> deleteClient(); std::expected<void, std::string> deleteClient();
bool hasClient() const; bool hasClient() const;
tl::expected<void, std::string> start(std::string_view url, std::expected<void, std::string> start(std::string_view url,
esp_http_client_method_t method = HTTP_METHOD_GET, esp_http_client_method_t method = HTTP_METHOD_GET,
const std::map<std::string, std::string> &requestHeaders = {}, const std::map<std::string, std::string> &requestHeaders = {},
std::string_view requestBody = {}, int timeout_ms = 0); std::string_view requestBody = {}, int timeout_ms = 0);
tl::expected<void, std::string> retry(std::optional<std::string_view> url = std::nullopt, std::expected<void, std::string> retry(std::optional<std::string_view> url = std::nullopt,
std::optional<esp_http_client_method_t> method = std::nullopt, std::optional<esp_http_client_method_t> method = std::nullopt,
const std::map<std::string, std::string> &requestHeaders = {}, const std::map<std::string, std::string> &requestHeaders = {},
std::string_view requestBody = {} std::string_view requestBody = {}
@@ -47,12 +45,12 @@ public:
,std::optional<int> timeout_ms = {} ,std::optional<int> timeout_ms = {}
#endif #endif
); );
tl::expected<void, std::string> abort(); std::expected<void, std::string> abort();
bool inProgress() const; bool inProgress() const;
bool finished() const; bool finished() const;
tl::expected<void, std::string> result() const; std::expected<void, std::string> result() const;
int statusCode() const { return m_statusCode; } int statusCode() const { return m_statusCode; }