ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
download_buffer.cpp
Go to the documentation of this file.
1#include "download_buffer.h"
2#include "esphome/core/log.h"
3#include <algorithm>
4#include <cstring>
5
7
8ESPHOME_LOG_TAG(TAG, "online_image.download_buffer");
9
11 RAMAllocator<uint8_t> allocator;
12 this->buffer_ = allocator.allocate(size);
13 this->reset();
14 if (!this->buffer_) {
15 ESP_LOGE(TAG, "Initial allocation of download buffer failed!");
16 this->size_ = 0;
17 }
18}
19
20uint8_t *DownloadBuffer::data(size_t offset) {
21 if (offset > this->size_) {
22 ESP_LOGE(TAG, "Tried to access beyond download buffer bounds!!!");
23 return this->buffer_;
24 }
25 return this->buffer_ + offset;
26}
27
28size_t DownloadBuffer::read(size_t len) {
29 if (len >= this->unread_) {
30 this->unread_ = 0;
31 return 0;
32 }
33 this->unread_ -= len;
34 memmove(this->data(), this->data(len), this->unread_);
35 return this->unread_;
36}
37
39 if (this->keep_read_ || this->pos_ == this->start_)
40 return;
41 this->buffer_.read(this->pos_ - this->start_);
42 this->start_ = this->pos_;
43}
44
45int32_t DownloadSource::read(uint8_t *buf, uint32_t len) {
46 const size_t offset = this->pos_ - this->start_;
47 const size_t n = std::min<size_t>(len, this->buffer_.unread() - offset);
48 memcpy(buf, this->buffer_.data(offset), n);
49 this->pos_ += n;
50 return static_cast<int32_t>(n);
51}
52
54 // Only what is still in the buffer can be read again; a decoder never skips ahead of the download
55 if (position < this->start_ || position > this->start_ + this->buffer_.unread())
56 return false;
57 this->pos_ = position;
58 return true;
59}
60
62 if (this->size_ >= size)
63 return true;
64 RAMAllocator<uint8_t> allocator;
65 uint8_t *buffer = allocator.allocate(size);
66 if (buffer == nullptr) {
67 ESP_LOGE(TAG, "allocation of %zu bytes failed. Biggest block in heap: %zu Bytes", size,
68 allocator.get_max_free_block_size());
69 return false;
70 }
71 memcpy(buffer, this->buffer_, this->unread_);
72 allocator.deallocate(this->buffer_, this->size_);
73 this->buffer_ = buffer;
74 this->size_ = size;
75 return true;
76}
77
79 if (this->size_ >= size) {
80 // Avoid useless reallocations; if the buffer is big enough, don't reallocate.
81 return this->size_;
82 }
83 RAMAllocator<uint8_t> allocator;
84 allocator.deallocate(this->buffer_, this->size_);
85 this->buffer_ = allocator.allocate(size);
86 this->reset();
87 if (this->buffer_) {
88 this->size_ = size;
89 return size;
90 } else {
91 ESP_LOGE(TAG, "allocation of %zu bytes failed. Biggest block in heap: %zu Bytes", size,
92 allocator.get_max_free_block_size());
93 this->size_ = 0;
94 return 0;
95 }
96}
97
98} // namespace esphome::online_image
An STL allocator that uses SPI or internal RAM.
Definition helpers.h:2396
void deallocate(T *p, size_t n)
Definition helpers.h:2453
size_t get_max_free_block_size() const
Return the maximum size block this allocator could allocate.
Definition helpers.h:2501
T * allocate(size_t n)
Definition helpers.h:2423
size_t unread_
Total number of downloaded bytes not yet read.
bool grow(size_t size)
Make the buffer at least size bytes, keeping the unread data. Returns false if the allocation failed.
void compact()
Drop the bytes the decoder has read, to make room for more of the download.
int32_t read(uint8_t *buf, uint32_t len) override
bool seek(uint32_t position) override
float position
Definition cover.h:0
uint32_t buffer
Definition gif.cpp:50
ESPHOME_LOG_TAG(TAG, "online_image.download_buffer")
const void size_t len
Definition hal.h:68
uint16_t size
Definition helpers.cpp:34
static void uint32_t