10 namespace http_request {
14 #define UPDATE_RETURN vTaskDelete(nullptr) // Delete the current update task 16 #define UPDATE_RETURN return 19 static const char *
const TAG =
"http_request.update";
21 static const size_t MAX_READ_SIZE = 256;
51 if (container ==
nullptr || container->status_code !=
HTTP_STATUS_OK) {
58 uint8_t *data = allocator.
allocate(container->content_length);
59 if (data ==
nullptr) {
60 std::string msg =
str_sprintf(
"Failed to allocate %d bytes for manifest", container->content_length);
66 size_t read_index = 0;
67 while (container->get_bytes_read() < container->content_length) {
68 int read_bytes = container->read(data + read_index, MAX_READ_SIZE);
72 read_index += read_bytes;
77 std::string response((
char *) data, read_index);
78 allocator.
deallocate(data, container->content_length);
84 if (!root.containsKey(
"name") || !root.containsKey(
"version") || !root.containsKey(
"builds")) {
85 ESP_LOGE(TAG,
"Manifest does not contain required fields");
91 for (
auto build : root[
"builds"].as<JsonArray>()) {
92 if (!build.containsKey(
"chipFamily")) {
93 ESP_LOGE(TAG,
"Manifest does not contain required fields");
96 if (build[
"chipFamily"] == ESPHOME_VARIANT) {
97 if (!build.containsKey(
"ota")) {
98 ESP_LOGE(TAG,
"Manifest does not contain required fields");
101 auto ota = build[
"ota"];
102 if (!ota.containsKey(
"path") || !ota.containsKey(
"md5")) {
103 ESP_LOGE(TAG,
"Manifest does not contain required fields");
109 if (ota.containsKey(
"summary"))
111 if (ota.containsKey(
"release_url"))
130 if (path[0] ==
'/') {
140 std::string current_version;
141 #ifdef ESPHOME_PROJECT_VERSION 142 current_version = ESPHOME_PROJECT_VERSION;
144 current_version = ESPHOME_VERSION;
std::shared_ptr< HttpContainer > get(std::string url)
HttpRequestComponent * request_parent_
bool parse_json(const std::string &data, const json_parse_t &f)
Parse a JSON string and run the provided json parse function if it's valid.
const UpdateState & state
void defer(const std::string &name, std::function< void()> &&f)
Defer a callback to the next loop() call.
std::string latest_version
std::string current_version
static void update_task(void *params)
void set_md5(const std::string &md5)
void add_on_state_callback(std::function< void(ota::OTAState, float, uint8_t)> &&callback)
void status_set_error(const char *message="unspecified")
const UpdateInfo & update_info
std::string str_sprintf(const char *fmt,...)
void set_url(const std::string &url)
OtaHttpRequestComponent * ota_parent_
void deallocate(T *p, size_t n)
void status_clear_error()
void IRAM_ATTR HOT yield()
Implementation of SPI Controller mode.
An STL allocator that uses SPI or internal RAM.
TaskHandle_t update_task_handle_