diff --git a/CHANGELOG.md b/CHANGELOG.md index e83a132c8..118f6c8f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ All notable changes to this project will be documented in this file. - Berry `gc_heap` and `gc_time` to `tasmota.memory()` (#24054) - Scripter array transfer via UFS (#24060) - ESP8266 GPIOViewer memory map if enabled with `#define GV_USE_ESPINFO` +- Berry `tcp.write()` add `offset` and `len` ### Breaking Changed diff --git a/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_mqtt.ino b/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_mqtt.ino index 53e692975..2d0b3f736 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_mqtt.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_mqtt.ino @@ -38,7 +38,7 @@ extern "C" { if (top >= 5) { if (!is_binary) { be_raise(vm, "argument_error", "start and len are not allowed with string payloads"); } payload_start = be_toint(vm, 5); - if (payload_start < 0) payload_start = 0; + if (payload_start < 0) { payload_start = 0; } } if (top >= 6) { len = be_toint(vm, 6); } const char * topic = be_tostring(vm, 2); diff --git a/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_tcpclientasync.ino b/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_tcpclientasync.ino index 22aedd520..fa650e49a 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_tcpclientasync.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_tcpclientasync.ino @@ -511,10 +511,12 @@ extern "C" { be_return(vm); } - // tcp.write(bytes | string) -> int + // tcp.write(bytes | string[, offset:int, len:int]) -> int int32_t wc_tcpasync_write(struct bvm *vm); int32_t wc_tcpasync_write(struct bvm *vm) { int32_t argc = be_top(vm); + int32_t offset = 0; + int32_t len = -1; // send all of it if (argc >= 2 && (be_isstring(vm, 2) || be_isbytes(vm, 2))) { AsyncTCPClient * tcp = wc_gettcpclientasync_p(vm); const char * buf = nullptr; @@ -525,8 +527,22 @@ extern "C" { } else { // bytes buf = (const char*) be_tobytes(vm, 2, &buf_len); } - size_t bw = tcp->write(buf, buf_len); - be_pushint(vm, bw); + if (argc >= 3 && be_isint(vm, 3)) { offset = be_toint(vm, 3); } + if (argc >= 4 && be_isint(vm, 4)) { len = be_toint(vm, 4); } + + if (offset < 0) { offset = 0; } // default to the beginning of the buffer + if (offset >= buf_len) { len = 0; offset = 0; } // default to the end of the buffer (nothing to write) + else if (len < 0) { len = buf_len - offset; } // default to the end of the buffer + else if (offset + len > buf_len) { len = buf_len - offset; } // len is too long, adjust + + if (len > 0) { + // now adjust the buffer with offset + buf_len += offset; + size_t bw = tcp->write(buf, buf_len); + be_pushint(vm, bw); + } else { + be_pushint(vm, 0); // nothing to send + } be_return(vm); /* return code */ } be_raise(vm, kTypeError, nullptr);