10 namespace http_request {
12 static const char *
const TAG =
"http_request.update";
14 static const size_t MAX_READ_SIZE = 256;
34 if (container ==
nullptr || container->status_code !=
HTTP_STATUS_OK) {
41 uint8_t *data = allocator.
allocate(container->content_length);
42 if (data ==
nullptr) {
43 std::string msg =
str_sprintf(
"Failed to allocate %d bytes for manifest", container->content_length);
49 size_t read_index = 0;
50 while (container->get_bytes_read() < container->content_length) {
51 int read_bytes = container->read(data + read_index, MAX_READ_SIZE);
56 read_index += read_bytes;
59 std::string response((
char *) data, read_index);
60 allocator.
deallocate(data, container->content_length);
65 if (!root.containsKey(
"name") || !root.containsKey(
"version") || !root.containsKey(
"builds")) {
66 ESP_LOGE(TAG,
"Manifest does not contain required fields");
72 for (
auto build : root[
"builds"].as<JsonArray>()) {
73 if (!build.containsKey(
"chipFamily")) {
74 ESP_LOGE(TAG,
"Manifest does not contain required fields");
77 if (build[
"chipFamily"] == ESPHOME_VARIANT) {
78 if (!build.containsKey(
"ota")) {
79 ESP_LOGE(TAG,
"Manifest does not contain required fields");
82 auto ota = build[
"ota"];
83 if (!ota.containsKey(
"path") || !ota.containsKey(
"md5")) {
84 ESP_LOGE(TAG,
"Manifest does not contain required fields");
90 if (ota.containsKey(
"summary"))
92 if (ota.containsKey(
"release_url"))
110 if (path[0] ==
'/') {
119 std::string current_version;
120 #ifdef ESPHOME_PROJECT_VERSION 121 current_version = ESPHOME_PROJECT_VERSION;
123 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
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)
Application App
Global storage of Application pointer - only one Application can exist.
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.