From fc92dc0995b9f430725f8d41babf590de4b20975 Mon Sep 17 00:00:00 2001 From: btsimonh Date: Fri, 9 Jun 2023 07:56:05 +0100 Subject: [PATCH] Add wcclient.get_bytes() to Berry (#18829) (cherry picked from commit 5903b21448a3f088b12bd47a04996ff84e467a3a) --- .../berry_tasmota/src/be_webclient_lib.c | 2 + .../xdrv_52_3_berry_webclient.ino | 52 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/lib/libesp32/berry_tasmota/src/be_webclient_lib.c b/lib/libesp32/berry_tasmota/src/be_webclient_lib.c index 6458169a5..c49d189ec 100644 --- a/lib/libesp32/berry_tasmota/src/be_webclient_lib.c +++ b/lib/libesp32/berry_tasmota/src/be_webclient_lib.c @@ -30,6 +30,7 @@ extern int wc_getstring(bvm *vm); extern int wc_writefile(bvm *vm); extern int wc_writeflash(bvm *vm); extern int wc_getsize(bvm *vm); +extern int wc_getbytes(bvm *vm); #include "be_fixed_be_class_webclient.h" @@ -68,6 +69,7 @@ class be_class_webclient (scope: global, name: webclient) { write_file, func(wc_writefile) write_flash, func(wc_writeflash) get_size, func(wc_getsize) + get_bytes, func(wc_getbytes) } @const_object_info_end */ diff --git a/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_webclient.ino b/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_webclient.ino index f81326570..4902ec3a4 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_webclient.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_webclient.ino @@ -654,5 +654,57 @@ extern "C" { } } +class StreamBytes: public Stream +{ +public: + StreamBytes(uint8_t * buf_in, int len_in) : buf(buf_in), offset(0), len(len_in) {}; + + size_t write(const uint8_t *buffer, size_t size) override { + // AddLog(LOG_LEVEL_INFO, "FLASH: addr=%p hex=%*_H size=%i", addr_start + offset, 32, buffer, size); + if (size > 0) { + if (offset + size > len){ + AddLog(LOG_LEVEL_ERROR, "BERRYWC: bufer overrun"); + return size; + } + memcpy(buf+offset, buffer, size); + offset += size; + } + return size; + } + size_t write(uint8_t data) override { + write(&data, 1); + return 1; + } + + int available() override { return 0; } + int read() override { return -1; } + int peek() override { return -1; } + void flush() override { } + +protected: + uint8_t *buf; // start address + uint32_t offset; // how many bytes have already been written + uint32_t len; // allocated len +}; + +extern "C" { + int32_t wc_getbytes(struct bvm *vm); + int32_t wc_getbytes(struct bvm *vm) { + HTTPClientLight * cl = wc_getclient(vm); + int32_t sz = cl->getSize(); + // abort if we exceed 32KB size, things will not go well otherwise + if (sz >= 32767 || sz < 0) { + be_raise(vm, "value_error", "response size -1 or too big (>32KB)"); + } + uint8_t * buf = (uint8_t*) be_pushbuffer(vm, sz); + StreamBytes memory_writer(buf, sz); + int32_t written = cl->writeToStream(&memory_writer); + + be_pushbytes(vm, buf, sz); + cl->end(); // free allocated memory ~16KB + be_return(vm); /* return code */ + } +} + #endif // USE_WEBCLIENT #endif // USE_BERRY