diff --git a/.github/workflows/espidf.yaml b/.github/workflows/espidf.yaml index e0685732d..07097ec93 100644 --- a/.github/workflows/espidf.yaml +++ b/.github/workflows/espidf.yaml @@ -51,7 +51,7 @@ jobs: mkdir -p $ESPIDF_BASE cd $ESPIDF_BASE - platformio init -b az-delivery-devkit-v4 --project-option="framework=espidf" --project-option="build_flags=-DZENOH_ESPIDF -DZENOH_DEBUG=3" + platformio init -b az-delivery-devkit-v4 -O "board_build.cmake_extra_args=-DZ_FEATURE_LINK_SERIAL=1" --project-option="framework=espidf" --project-option="build_flags=-DZENOH_ESPIDF -DZENOH_DEBUG=3" cd $ESPIDF_BASE/lib ln -s $ZENOH_PICO_BASE diff --git a/include/zenoh-pico/system/platform/arduino/esp32.h b/include/zenoh-pico/system/platform/arduino/esp32.h index 4eee5f543..9e8df7a07 100644 --- a/include/zenoh-pico/system/platform/arduino/esp32.h +++ b/include/zenoh-pico/system/platform/arduino/esp32.h @@ -47,11 +47,7 @@ typedef struct { BluetoothSerial *_bts; // As pointer to cross the boundary between C and C++ #endif #if Z_FEATURE_LINK_SERIAL == 1 - struct { - HardwareSerial *_serial; // As pointer to cross the boundary between C and C++ - uint8_t *tmp_buf; - uint8_t *raw_buf; - }; + HardwareSerial *_serial; // As pointer to cross the boundary between C and C++ #endif }; } _z_sys_net_socket_t; diff --git a/include/zenoh-pico/system/platform/espidf.h b/include/zenoh-pico/system/platform/espidf.h index ccaf99ce6..eaca83742 100644 --- a/include/zenoh-pico/system/platform/espidf.h +++ b/include/zenoh-pico/system/platform/espidf.h @@ -56,11 +56,7 @@ typedef struct { int _fd; #endif #if Z_FEATURE_LINK_SERIAL == 1 - struct { - uart_port_t _serial; - uint8_t *tmp_buf; - uint8_t *raw_buf; - }; + uart_port_t _serial; #endif }; } _z_sys_net_socket_t; diff --git a/include/zenoh-pico/system/platform/flipper.h b/include/zenoh-pico/system/platform/flipper.h index 27b3962b1..8eb94b4c7 100644 --- a/include/zenoh-pico/system/platform/flipper.h +++ b/include/zenoh-pico/system/platform/flipper.h @@ -44,8 +44,6 @@ typedef struct { #if Z_FEATURE_LINK_SERIAL == 1 FuriStreamBuffer* _rx_stream; FuriHalSerialHandle* _serial; - uint8_t* tmp_buf; - uint8_t* raw_buf; #endif } _z_sys_net_socket_t; diff --git a/include/zenoh-pico/system/platform/mbed.h b/include/zenoh-pico/system/platform/mbed.h index b2911acd2..9d9d91aeb 100644 --- a/include/zenoh-pico/system/platform/mbed.h +++ b/include/zenoh-pico/system/platform/mbed.h @@ -51,11 +51,7 @@ typedef struct { UDPSocket *_udp; // As pointer to cross the boundary between C and C++ #endif #if Z_FEATURE_LINK_SERIAL == 1 - struct { - BufferedSerial *_serial; // As pointer to cross the boundary between C and C++ - uint8_t *tmp_buf; - uint8_t *raw_buf; - }; + BufferedSerial *_serial; // As pointer to cross the boundary between C and C++ #endif }; } _z_sys_net_socket_t; diff --git a/include/zenoh-pico/system/platform/rpi_pico.h b/include/zenoh-pico/system/platform/rpi_pico.h index 9ef61f9a3..b5480548c 100644 --- a/include/zenoh-pico/system/platform/rpi_pico.h +++ b/include/zenoh-pico/system/platform/rpi_pico.h @@ -61,11 +61,7 @@ typedef struct { int _fd; #endif #if Z_FEATURE_LINK_SERIAL == 1 - struct { - uart_inst_t *_serial; - uint8_t *tmp_buf; - uint8_t *raw_buf; - }; + uart_inst_t *_serial; #endif }; } _z_sys_net_socket_t; diff --git a/include/zenoh-pico/system/platform/zephyr.h b/include/zenoh-pico/system/platform/zephyr.h index 2fbc42a6d..addf67f31 100644 --- a/include/zenoh-pico/system/platform/zephyr.h +++ b/include/zenoh-pico/system/platform/zephyr.h @@ -50,11 +50,7 @@ typedef struct { int _fd; #endif #if Z_FEATURE_LINK_SERIAL == 1 - struct { - const struct device *_serial; - uint8_t *tmp_buf; - uint8_t *raw_buf; - }; + const struct device *_serial; #endif }; } _z_sys_net_socket_t; diff --git a/src/system/arduino/esp32/network.cpp b/src/system/arduino/esp32/network.cpp index 7eb2e1f02..9cb4c5bd1 100644 --- a/src/system/arduino/esp32/network.cpp +++ b/src/system/arduino/esp32/network.cpp @@ -685,9 +685,6 @@ z_result_t _z_open_serial_from_pins(_z_sys_net_socket_t *sock, uint32_t txpin, u } } - sock->tmp_buf = (uint8_t *)z_malloc(_Z_SERIAL_MFS_SIZE); - sock->raw_buf = (uint8_t *)z_malloc(_Z_SERIAL_MAX_COBS_BUF_SIZE); - return ret; } @@ -727,9 +724,6 @@ z_result_t _z_open_serial_from_dev(_z_sys_net_socket_t *sock, char *dev, uint32_ return _Z_ERR_GENERIC; } - sock->tmp_buf = (uint8_t *)z_malloc(_Z_SERIAL_MFS_SIZE); - sock->raw_buf = (uint8_t *)z_malloc(_Z_SERIAL_MAX_COBS_BUF_SIZE); - return _z_connect_serial(*sock); } @@ -761,38 +755,49 @@ z_result_t _z_listen_serial_from_dev(_z_sys_net_socket_t *sock, char *dev, uint3 void _z_close_serial(_z_sys_net_socket_t *sock) { sock->_serial->end(); delete sock->_serial; - z_free(sock->tmp_buf); - z_free(sock->raw_buf); } size_t _z_read_serial_internal(const _z_sys_net_socket_t sock, uint8_t *header, uint8_t *ptr, size_t len) { + uint8_t *raw_buf = z_malloc(_Z_SERIAL_MAX_COBS_BUF_SIZE); size_t rb = 0; for (size_t i = 0; i < _Z_SERIAL_MAX_COBS_BUF_SIZE; i++) { while (sock._serial->available() < 1) { z_sleep_ms(1); // FIXME: Yield by sleeping. } - sock.raw_buf[i] = sock._serial->read(); + raw_buf[i] = sock._serial->read(); rb = rb + (size_t)1; - if (sock.raw_buf[i] == (uint8_t)0x00) { + if (raw_buf[i] == (uint8_t)0x00) { break; } } - return _z_serial_msg_deserialize(sock.raw_buf, rb, ptr, len, header, sock.tmp_buf, _Z_SERIAL_MFS_SIZE); + uint8_t *tmp_buf = z_malloc(_Z_SERIAL_MFS_SIZE); + size_t ret = _z_serial_msg_deserialize(raw_buf, rb, ptr, len, header, tmp_buf, _Z_SERIAL_MFS_SIZE); + + z_free(raw_buf); + z_free(tmp_buf); + + return ret; } size_t _z_send_serial_internal(const _z_sys_net_socket_t sock, uint8_t header, const uint8_t *ptr, size_t len) { - size_t ret = _z_serial_msg_serialize(sock.raw_buf, _Z_SERIAL_MAX_COBS_BUF_SIZE, ptr, len, header, sock.tmp_buf, - _Z_SERIAL_MFS_SIZE); + uint8_t *tmp_buf = (uint8_t *)z_malloc(_Z_SERIAL_MFS_SIZE); + uint8_t *raw_buf = (uint8_t *)z_malloc(_Z_SERIAL_MAX_COBS_BUF_SIZE); + size_t ret = + _z_serial_msg_serialize(raw_buf, _Z_SERIAL_MAX_COBS_BUF_SIZE, ptr, len, header, tmp_buf, _Z_SERIAL_MFS_SIZE); + if (ret == SIZE_MAX) { return ret; } - ssize_t wb = sock._serial->write(sock.raw_buf, ret); + ssize_t wb = sock._serial->write(raw_buf, ret); if (wb != (ssize_t)ret) { ret = SIZE_MAX; } + z_free(raw_buf); + z_free(tmp_buf); + return len; } #endif diff --git a/src/system/espidf/network.c b/src/system/espidf/network.c index 16c1d7397..7aa41e49a 100644 --- a/src/system/espidf/network.c +++ b/src/system/espidf/network.c @@ -601,8 +601,6 @@ z_result_t _z_open_serial_from_dev(_z_sys_net_socket_t *sock, char *dev, uint32_ QueueHandle_t uart_queue; uart_driver_install(sock->_serial, uart_buffer_size, 0, 100, &uart_queue, 0); uart_flush_input(sock->_serial); - sock->tmp_buf = (uint8_t *)z_malloc(_Z_SERIAL_MFS_SIZE); - sock->raw_buf = (uint8_t *)z_malloc(_Z_SERIAL_MAX_COBS_BUF_SIZE); return _z_connect_serial(*sock); } @@ -636,14 +634,13 @@ void _z_close_serial(_z_sys_net_socket_t *sock) { uart_wait_tx_done(sock->_serial, 1000); uart_flush(sock->_serial); uart_driver_delete(sock->_serial); - z_free(sock->tmp_buf); - z_free(sock->raw_buf); } size_t _z_read_serial_internal(const _z_sys_net_socket_t sock, uint8_t *header, uint8_t *ptr, size_t len) { + uint8_t *raw_buf = z_malloc(_Z_SERIAL_MAX_COBS_BUF_SIZE); size_t rb = 0; while (rb < _Z_SERIAL_MAX_COBS_BUF_SIZE) { - int r = uart_read_bytes(sock._serial, &sock.raw_buf[rb], 1, 1000); + int r = uart_read_bytes(sock._serial, &raw_buf[rb], 1, 1000); if (r == 0) { _Z_DEBUG("Timeout reading from serial"); if (rb == 0) { @@ -651,7 +648,7 @@ size_t _z_read_serial_internal(const _z_sys_net_socket_t sock, uint8_t *header, } } else if (r == 1) { rb = rb + (size_t)1; - if (sock.raw_buf[rb - 1] == (uint8_t)0x00) { + if (raw_buf[rb - 1] == (uint8_t)0x00) { break; } } else { @@ -660,22 +657,33 @@ size_t _z_read_serial_internal(const _z_sys_net_socket_t sock, uint8_t *header, } } - return _z_serial_msg_deserialize(sock.raw_buf, rb, ptr, len, header, sock.tmp_buf, _Z_SERIAL_MFS_SIZE); + uint8_t *tmp_buf = z_malloc(_Z_SERIAL_MFS_SIZE); + size_t ret = _z_serial_msg_deserialize(raw_buf, rb, ptr, len, header, tmp_buf, _Z_SERIAL_MFS_SIZE); + + z_free(raw_buf); + z_free(tmp_buf); + + return ret; } size_t _z_send_serial_internal(const _z_sys_net_socket_t sock, uint8_t header, const uint8_t *ptr, size_t len) { - size_t ret = _z_serial_msg_serialize(sock.raw_buf, _Z_SERIAL_MAX_COBS_BUF_SIZE, ptr, len, header, sock.tmp_buf, - _Z_SERIAL_MFS_SIZE); + uint8_t *tmp_buf = (uint8_t *)z_malloc(_Z_SERIAL_MFS_SIZE); + uint8_t *raw_buf = (uint8_t *)z_malloc(_Z_SERIAL_MAX_COBS_BUF_SIZE); + size_t ret = + _z_serial_msg_serialize(raw_buf, _Z_SERIAL_MAX_COBS_BUF_SIZE, ptr, len, header, tmp_buf, _Z_SERIAL_MFS_SIZE); if (ret == SIZE_MAX) { return ret; } - ssize_t wb = uart_write_bytes(sock._serial, sock.raw_buf, ret); + ssize_t wb = uart_write_bytes(sock._serial, raw_buf, ret); if (wb != (ssize_t)ret) { ret = SIZE_MAX; } + z_free(raw_buf); + z_free(tmp_buf); + return len; } #endif diff --git a/src/system/flipper/network.c b/src/system/flipper/network.c index 208e327db..2286cfa8b 100644 --- a/src/system/flipper/network.c +++ b/src/system/flipper/network.c @@ -78,9 +78,6 @@ z_result_t _z_open_serial_from_dev(_z_sys_net_socket_t* sock, char* dev, uint32_ _Z_DEBUG("Serial port opened: %s (%li)", dev, baudrate); - sock->tmp_buf = (uint8_t*)z_malloc(_Z_SERIAL_MFS_SIZE); - sock->raw_buf = (uint8_t*)z_malloc(_Z_SERIAL_MAX_COBS_BUF_SIZE); - return _z_connect_serial(*sock); } @@ -116,42 +113,52 @@ void _z_close_serial(_z_sys_net_socket_t* sock) { sock->_serial = 0; sock->_rx_stream = 0; - - z_free(sock->tmp_buf); - z_free(sock->raw_buf); } _Z_DEBUG("Serial port closed"); } size_t _z_read_serial_internal(const _z_sys_net_socket_t sock, uint8_t* header, uint8_t* ptr, size_t len) { + uint8_t* raw_buf = z_malloc(_Z_SERIAL_MAX_COBS_BUF_SIZE); size_t rb = 0; for (size_t i = 0; i < _Z_SERIAL_MAX_COBS_BUF_SIZE; i++) { size_t len = 0; - len = furi_stream_buffer_receive(sock._rx_stream, &sock.raw_buf[i], 1, FLIPPER_SERIAL_TIMEOUT_MS); + len = furi_stream_buffer_receive(sock._rx_stream, &raw_buf[i], 1, FLIPPER_SERIAL_TIMEOUT_MS); if (!len) { return SIZE_MAX; } rb++; - if (sock.raw_buf[i] == (uint8_t)0x00) { + if (raw_buf[i] == (uint8_t)0x00) { break; } } - return _z_serial_msg_deserialize(sock.raw_buf, rb, ptr, len, header, sock.tmp_buf, _Z_SERIAL_MFS_SIZE); + uint8_t* tmp_buf = z_malloc(_Z_SERIAL_MFS_SIZE); + size_t ret = _z_serial_msg_deserialize(raw_buf, rb, ptr, len, header, tmp_buf, _Z_SERIAL_MFS_SIZE); + + z_free(raw_buf); + z_free(tmp_buf); + + return ret; } size_t _z_send_serial_internal(const _z_sys_net_socket_t sock, uint8_t header, const uint8_t* ptr, size_t len) { - size_t ret = _z_serial_msg_serialize(sock.raw_buf, _Z_SERIAL_MAX_COBS_BUF_SIZE, ptr, len, header, sock.tmp_buf, - _Z_SERIAL_MFS_SIZE); + uint8_t* tmp_buf = (uint8_t*)z_malloc(_Z_SERIAL_MFS_SIZE); + uint8_t* raw_buf = (uint8_t*)z_malloc(_Z_SERIAL_MAX_COBS_BUF_SIZE); + size_t ret = + _z_serial_msg_serialize(raw_buf, _Z_SERIAL_MAX_COBS_BUF_SIZE, ptr, len, header, tmp_buf, _Z_SERIAL_MFS_SIZE); if (ret == SIZE_MAX) { return ret; } - furi_hal_serial_tx(sock._serial, sock.raw_buf, ret); + furi_hal_serial_tx(sock._serial, raw_buf, ret); furi_hal_serial_tx_wait_complete(sock._serial); + + z_free(raw_buf); + z_free(tmp_buf); + return len; } #endif diff --git a/src/system/mbed/network.cpp b/src/system/mbed/network.cpp index 5a6c6c6df..39de6204f 100644 --- a/src/system/mbed/network.cpp +++ b/src/system/mbed/network.cpp @@ -332,9 +332,6 @@ z_result_t _z_open_serial_from_pins(_z_sys_net_socket_t *sock, uint32_t txpin, u ret = _Z_ERR_GENERIC; } - sock->tmp_buf = (uint8_t *)z_malloc(_Z_SERIAL_MFS_SIZE); - sock->raw_buf = (uint8_t *)z_malloc(_Z_SERIAL_MAX_COBS_BUF_SIZE); - return _z_connect_serial(*sock); } @@ -373,38 +370,46 @@ z_result_t _z_listen_serial_from_dev(_z_sys_net_socket_t *sock, char *device, ui return ret; } -void _z_close_serial(_z_sys_net_socket_t *sock) { - delete sock->_serial; - z_free(sock->tmp_buf); - z_free(sock->raw_buf); -} +void _z_close_serial(_z_sys_net_socket_t *sock) { delete sock->_serial; } size_t _z_read_serial(const _z_sys_net_socket_t sock, uint8_t *ptr, size_t len) { + uint8_t *raw_buf = z_malloc(_Z_SERIAL_MAX_COBS_BUF_SIZE); size_t rb = 0; for (size_t i = 0; i < _Z_SERIAL_MAX_COBS_BUF_SIZE; i++) { - sock._serial->read(&sock.raw_buf[i], 1); + sock._serial->read(&raw_buf[i], 1); rb++; if (before_cobs[i] == (uint8_t)0x00) { break; } } - return _z_serial_msg_deserialize(sock.raw_buf, rb, ptr, len, header, sock.tmp_buf, _Z_SERIAL_MFS_SIZE); + uint8_t *tmp_buf = z_malloc(_Z_SERIAL_MFS_SIZE); + size_t ret = _z_serial_msg_deserialize(raw_buf, rb, ptr, len, header, tmp_buf, _Z_SERIAL_MFS_SIZE); + + z_free(raw_buf); + z_free(tmp_buf); + + return ret; } size_t _z_send_serial_internal(const _z_sys_net_socket_t sock, uint8_t header, const uint8_t *ptr, size_t len) { - size_t ret = _z_serial_msg_serialize(sock.raw_buf, _Z_SERIAL_MAX_COBS_BUF_SIZE, ptr, len, header, sock.tmp_buf, - _Z_SERIAL_MFS_SIZE); + uint8_t *tmp_buf = (uint8_t *)z_malloc(_Z_SERIAL_MFS_SIZE); + uint8_t *raw_buf = (uint8_t *)z_malloc(_Z_SERIAL_MAX_COBS_BUF_SIZE); + size_t ret = + _z_serial_msg_serialize(raw_buf, _Z_SERIAL_MAX_COBS_BUF_SIZE, ptr, len, header, tmp_buf, _Z_SERIAL_MFS_SIZE); if (ret == SIZE_MAX) { return ret; } - ssize_t wb = sock._serial->write(after_cobs, sock.raw_buf, ret); + ssize_t wb = sock._serial->write(after_cobs, raw_buf, ret); if (wb != (ssize_t)ret) { ret = SIZE_MAX; } + z_free(raw_buf); + z_free(tmp_buf); + return len; } #endif diff --git a/src/system/rpi_pico/network.c b/src/system/rpi_pico/network.c index b658e97ec..322e82230 100644 --- a/src/system/rpi_pico/network.c +++ b/src/system/rpi_pico/network.c @@ -530,9 +530,6 @@ z_result_t _z_open_serial_from_pins(_z_sys_net_socket_t *sock, uint32_t txpin, u _z_open_serial_impl(sock->_serial, txpin, rxpin, baudrate); - sock->tmp_buf = (uint8_t *)z_malloc(_Z_SERIAL_MFS_SIZE); - sock->raw_buf = (uint8_t *)z_malloc(_Z_SERIAL_MAX_COBS_BUF_SIZE); - return _z_connect_serial(*sock); } @@ -540,8 +537,6 @@ z_result_t _z_open_serial_from_dev(_z_sys_net_socket_t *sock, char *dev, uint32_ z_result_t ret = _Z_RES_OK; sock->_serial = NULL; - sock->tmp_buf = (uint8_t *)z_malloc(_Z_SERIAL_MFS_SIZE); - sock->raw_buf = (uint8_t *)z_malloc(_Z_SERIAL_MAX_COBS_BUF_SIZE); #if Z_FEATURE_LINK_SERIAL_USB == 1 if (strcmp("usb", dev) == 0) { @@ -564,8 +559,6 @@ z_result_t _z_open_serial_from_dev(_z_sys_net_socket_t *sock, char *dev, uint32_ if (sock->_serial == NULL) { _Z_ERROR("invalid device name"); - z_free(sock->tmp_buf); - z_free(sock->raw_buf); return _Z_ERR_INVALID; } @@ -607,42 +600,52 @@ void _z_close_serial(_z_sys_net_socket_t *sock) { _z_usb_uart_deinit(); #endif } - z_free(sock->tmp_buf); - z_free(sock->raw_buf); } size_t _z_read_serial_internal(const _z_sys_net_socket_t sock, uint8_t *header, uint8_t *ptr, size_t len) { + uint8_t *raw_buf = z_malloc(_Z_SERIAL_MAX_COBS_BUF_SIZE); size_t rb = 0; for (size_t i = 0; i < _Z_SERIAL_MAX_COBS_BUF_SIZE; i++) { #if Z_FEATURE_LINK_SERIAL_USB == 1 - sock.raw_buf[i] = (sock._serial == NULL) ? _z_usb_uart_getc() : uart_getc(sock._serial); + raw_buf[i] = (sock._serial == NULL) ? _z_usb_uart_getc() : uart_getc(sock._serial); #else - sock.raw_buf[i] = uart_getc(sock._serial); + raw_buf[i] = uart_getc(sock._serial); #endif rb++; - if (sock.raw_buf[i] == 0x00) { + if (raw_buf[i] == 0x00) { break; } } - return _z_serial_msg_deserialize(sock.raw_buf, rb, ptr, len, header, sock.tmp_buf, _Z_SERIAL_MFS_SIZE); + uint8_t *tmp_buf = z_malloc(_Z_SERIAL_MFS_SIZE); + size_t ret = _z_serial_msg_deserialize(raw_buf, rb, ptr, len, header, tmp_buf, _Z_SERIAL_MFS_SIZE); + + z_free(raw_buf); + z_free(tmp_buf); + + return ret; } size_t _z_send_serial_internal(const _z_sys_net_socket_t sock, uint8_t header, const uint8_t *ptr, size_t len) { - size_t ret = _z_serial_msg_serialize(sock.raw_buf, _Z_SERIAL_MAX_COBS_BUF_SIZE, ptr, len, header, sock.tmp_buf, - _Z_SERIAL_MFS_SIZE); + uint8_t *tmp_buf = (uint8_t *)z_malloc(_Z_SERIAL_MFS_SIZE); + uint8_t *raw_buf = (uint8_t *)z_malloc(_Z_SERIAL_MAX_COBS_BUF_SIZE); + size_t ret = + _z_serial_msg_serialize(raw_buf, _Z_SERIAL_MAX_COBS_BUF_SIZE, ptr, len, header, tmp_buf, _Z_SERIAL_MFS_SIZE); if (ret == SIZE_MAX) { return ret; } if (sock._serial == NULL) { #if Z_FEATURE_LINK_SERIAL_USB == 1 - _z_usb_uart_write(sock.raw_buf, ret); + _z_usb_uart_write(raw_buf, ret); #endif } else { - uart_write_blocking(sock._serial, sock.raw_buf, ret); + uart_write_blocking(sock._serial, raw_buf, ret); } + z_free(raw_buf); + z_free(tmp_buf); + return len; } #endif diff --git a/src/system/zephyr/network.c b/src/system/zephyr/network.c index f85d47db5..cce1a59cc 100644 --- a/src/system/zephyr/network.c +++ b/src/system/zephyr/network.c @@ -598,9 +598,6 @@ z_result_t _z_open_serial_from_dev(_z_sys_net_socket_t *sock, char *dev, uint32_ ret = _Z_ERR_GENERIC; } - sock->tmp_buf = (uint8_t *)z_malloc(_Z_SERIAL_MFS_SIZE); - sock->raw_buf = (uint8_t *)z_malloc(_Z_SERIAL_MAX_COBS_BUF_SIZE); - return _z_connect_serial(*sock); } @@ -629,40 +626,49 @@ z_result_t _z_listen_serial_from_dev(_z_sys_net_socket_t *sock, char *dev, uint3 return ret; } -void _z_close_serial(_z_sys_net_socket_t *sock) { - z_free(sock->tmp_buf); - z_free(sock->raw_buf); -} +void _z_close_serial(_z_sys_net_socket_t *sock) {} size_t _z_read_serial_internal(const _z_sys_net_socket_t sock, uint8_t *header, uint8_t *ptr, size_t len) { + uint8_t *raw_buf = z_malloc(_Z_SERIAL_MAX_COBS_BUF_SIZE); size_t rb = 0; for (size_t i = 0; i < _Z_SERIAL_MAX_COBS_BUF_SIZE; i++) { int res = -1; while (res != 0) { - res = uart_poll_in(sock._serial, &sock.raw_buf[i]); + res = uart_poll_in(sock._serial, &raw_buf[i]); } rb++; - if (sock.raw_buf[i] == (uint8_t)0x00) { + if (raw_buf[i] == (uint8_t)0x00) { break; } } - return _z_serial_msg_deserialize(sock.raw_buf, rb, ptr, len, header, sock.tmp_buf, _Z_SERIAL_MFS_SIZE); + uint8_t *tmp_buf = z_malloc(_Z_SERIAL_MFS_SIZE); + size_t ret = _z_serial_msg_deserialize(raw_buf, rb, ptr, len, header, tmp_buf, _Z_SERIAL_MFS_SIZE); + + z_free(raw_buf); + z_free(tmp_buf); + + return ret; } size_t _z_send_serial_internal(const _z_sys_net_socket_t sock, uint8_t header, const uint8_t *ptr, size_t len) { - size_t ret = _z_serial_msg_serialize(sock.raw_buf, _Z_SERIAL_MAX_COBS_BUF_SIZE, ptr, len, header, sock.tmp_buf, - _Z_SERIAL_MFS_SIZE); + uint8_t *tmp_buf = (uint8_t *)z_malloc(_Z_SERIAL_MFS_SIZE); + uint8_t *raw_buf = (uint8_t *)z_malloc(_Z_SERIAL_MAX_COBS_BUF_SIZE); + size_t ret = + _z_serial_msg_serialize(raw_buf, _Z_SERIAL_MAX_COBS_BUF_SIZE, ptr, len, header, tmp_buf, _Z_SERIAL_MFS_SIZE); if (ret == SIZE_MAX) { return ret; } for (size_t i = 0; i < ret; i++) { - uart_poll_out(sock._serial, sock.raw_buf[i]); + uart_poll_out(sock._serial, raw_buf[i]); } + z_free(raw_buf); + z_free(tmp_buf); + return len; } #endif