From 9b4f19fc557b7b3176c60d5e2a52a490dd3bf825 Mon Sep 17 00:00:00 2001 From: Marco Randazzo Date: Fri, 4 Aug 2023 13:54:53 +0200 Subject: [PATCH] complete removal of libYARP_wire_rep_utils --- .github/CODEOWNERS | 1 - doc/Doxyfile.in | 1 - src/CMakeLists.txt | 1 - src/libYARP_wire_rep_utils/CMakeLists.txt | 16 - src/libYARP_wire_rep_utils/src/CMakeLists.txt | 83 -- .../yarp/wire_rep_utils/BlobNetworkHeader.h | 36 - .../src/yarp/wire_rep_utils/WireBottle.cpp | 130 -- .../src/yarp/wire_rep_utils/WireBottle.h | 82 -- .../src/yarp/wire_rep_utils/WireImage.cpp | 53 - .../src/yarp/wire_rep_utils/WireImage.h | 182 --- .../src/yarp/wire_rep_utils/WireTwiddler.cpp | 1076 ----------------- .../src/yarp/wire_rep_utils/WireTwiddler.h | 365 ------ .../src/yarp/wire_rep_utils/api.h | 28 - .../tests/CMakeLists.txt | 31 - src/libYARP_wire_rep_utils/tests/WireTest.cpp | 165 --- 15 files changed, 2250 deletions(-) delete mode 100644 src/libYARP_wire_rep_utils/CMakeLists.txt delete mode 100644 src/libYARP_wire_rep_utils/src/CMakeLists.txt delete mode 100644 src/libYARP_wire_rep_utils/src/yarp/wire_rep_utils/BlobNetworkHeader.h delete mode 100644 src/libYARP_wire_rep_utils/src/yarp/wire_rep_utils/WireBottle.cpp delete mode 100644 src/libYARP_wire_rep_utils/src/yarp/wire_rep_utils/WireBottle.h delete mode 100644 src/libYARP_wire_rep_utils/src/yarp/wire_rep_utils/WireImage.cpp delete mode 100644 src/libYARP_wire_rep_utils/src/yarp/wire_rep_utils/WireImage.h delete mode 100644 src/libYARP_wire_rep_utils/src/yarp/wire_rep_utils/WireTwiddler.cpp delete mode 100644 src/libYARP_wire_rep_utils/src/yarp/wire_rep_utils/WireTwiddler.h delete mode 100644 src/libYARP_wire_rep_utils/src/yarp/wire_rep_utils/api.h delete mode 100644 src/libYARP_wire_rep_utils/tests/CMakeLists.txt delete mode 100644 src/libYARP_wire_rep_utils/tests/WireTest.cpp diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 9ca56f75f6d..5cdfb641e25 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -59,7 +59,6 @@ /src/libYARP_run/** @drdanz /src/libYARP_serversql/** @drdanz /src/libYARP_sig/** @drdanz -/src/libYARP_wire_rep_utils/** @drdanz /src/portmonitors/depthimage_compression_zfp/** @Nicogene /src/portmonitors/depthimage_to_mono/** @randaz81 /src/portmonitors/depthimage_to_rgb/** @randaz81 diff --git a/doc/Doxyfile.in b/doc/Doxyfile.in index 342573d7a5f..46db72d75d3 100644 --- a/doc/Doxyfile.in +++ b/doc/Doxyfile.in @@ -189,7 +189,6 @@ STRIP_FROM_INC_PATH = @CMAKE_SOURCE_DIR@/src/libYARP_conf/src/ \ @CMAKE_SOURCE_DIR@/src/libYARP_run/src/ \ @CMAKE_SOURCE_DIR@/src/libYARP_manager/src/ \ @CMAKE_SOURCE_DIR@/src/libYARP_logger/src/ \ - @CMAKE_SOURCE_DIR@/src/libYARP_wire_rep_utils/src/ \ @CMAKE_SOURCE_DIR@/src/libYARP_robotinterface/src/ \ @CMAKE_SOURCE_DIR@/src/libYARP_robottestingframework/src/ \ @CMAKE_BINARY_DIR@/src/libYARP_companion/src/ \ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 53566ebc052..ff36e4d2738 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -23,7 +23,6 @@ add_subdirectory(libYARP_manager) add_subdirectory(libYARP_profiler) add_subdirectory(libYARP_logger) add_subdirectory(libYARP_dataplayer) -add_subdirectory(libYARP_wire_rep_utils) add_subdirectory(libYARP_robottestingframework) # plugins diff --git a/src/libYARP_wire_rep_utils/CMakeLists.txt b/src/libYARP_wire_rep_utils/CMakeLists.txt deleted file mode 100644 index ecf1bc2a6bb..00000000000 --- a/src/libYARP_wire_rep_utils/CMakeLists.txt +++ /dev/null @@ -1,16 +0,0 @@ -# SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT) -# SPDX-License-Identifier: BSD-3-Clause - -project(YARP_wire_rep_utils) - -add_subdirectory(src) - -include(YarpInstallBasicPackageFiles) -yarp_install_basic_package_files(YARP_wire_rep_utils - DEPENDENCIES ${YARP_wire_rep_utils_PUBLIC_DEPS} - PRIVATE_DEPENDENCIES ${YARP_wire_rep_utils_PRIVATE_DEPS} -) - -if(YARP_COMPILE_TESTS) - add_subdirectory(tests) -endif() diff --git a/src/libYARP_wire_rep_utils/src/CMakeLists.txt b/src/libYARP_wire_rep_utils/src/CMakeLists.txt deleted file mode 100644 index 0cb71cc28fc..00000000000 --- a/src/libYARP_wire_rep_utils/src/CMakeLists.txt +++ /dev/null @@ -1,83 +0,0 @@ -# SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT) -# SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium -# SPDX-License-Identifier: BSD-3-Clause - -add_library(YARP_wire_rep_utils STATIC) -add_library(YARP::YARP_wire_rep_utils ALIAS YARP_wire_rep_utils) - -set(YARP_wire_rep_utils_HDRS - yarp/wire_rep_utils/api.h - yarp/wire_rep_utils/BlobNetworkHeader.h - yarp/wire_rep_utils/WireImage.h - yarp/wire_rep_utils/WireBottle.h - yarp/wire_rep_utils/WireTwiddler.h -) - -set(YARP_wire_rep_utils_SRCS - yarp/wire_rep_utils/WireImage.cpp - yarp/wire_rep_utils/WireBottle.cpp - yarp/wire_rep_utils/WireTwiddler.cpp -) - -target_sources(YARP_wire_rep_utils - PRIVATE - ${YARP_wire_rep_utils_HDRS} - ${YARP_wire_rep_utils_SRCS} -) - -target_include_directories(YARP_wire_rep_utils - PUBLIC - $ - $ -) -target_compile_features(YARP_wire_rep_utils PUBLIC cxx_std_17) - -target_link_libraries(YARP_wire_rep_utils - PUBLIC - YARP::YARP_conf - YARP::YARP_os - YARP::YARP_sig -) -list(APPEND YARP_wire_rep_utils_PUBLIC_DEPS - YARP_conf - YARP_os - YARP_sig -) - -if(YARP_HAS_ACE) - target_compile_definitions(YARP_wire_rep_utils PRIVATE YARP_HAS_ACE) - target_link_libraries(YARP_wire_rep_utils PRIVATE ACE::ACE_INLINE) - list(APPEND YARP_wire_rep_utils_PRIVATE_DEPS ACE) -endif() - -set_property(TARGET YARP_wire_rep_utils PROPERTY PUBLIC_HEADER ${YARP_wire_rep_utils_HDRS}) -set_property(TARGET YARP_wire_rep_utils PROPERTY PRIVATE_HEADER ${YARP_wire_rep_utils_IMPL_HDRS}) -set_property(TARGET YARP_wire_rep_utils PROPERTY VERSION ${YARP_VERSION_SHORT}) -set_property(TARGET YARP_wire_rep_utils PROPERTY SOVERSION ${YARP_SOVERSION}) -set_property(TARGET YARP_wire_rep_utils PROPERTY FOLDER "Libraries/Private") - - -install( - TARGETS YARP_wire_rep_utils - EXPORT YARP_wire_rep_utils - RUNTIME - DESTINATION "${CMAKE_INSTALL_BINDIR}" - COMPONENT YARP_wire_rep_utils - LIBRARY - DESTINATION "${CMAKE_INSTALL_LIBDIR}" - COMPONENT YARP_wire_rep_utils - NAMELINK_COMPONENT YARP_wire_rep_utils-dev - ARCHIVE - DESTINATION "${CMAKE_INSTALL_LIBDIR}" - COMPONENT YARP_wire_rep_utils-dev - PUBLIC_HEADER - DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/yarp/wire_rep_utils" - COMPONENT YARP_wire_rep_utils-dev - PRIVATE_HEADER - DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/yarp/wire_rep_utils/impl" - COMPONENT YARP_wire_rep_utils-priv-dev -) - - -set(YARP_wire_rep_utils_PUBLIC_DEPS ${YARP_wire_rep_utils_PUBLIC_DEPS} PARENT_SCOPE) -set(YARP_wire_rep_utils_PRIVATE_DEPS ${YARP_wire_rep_utils_PRIVATE_DEPS} PARENT_SCOPE) diff --git a/src/libYARP_wire_rep_utils/src/yarp/wire_rep_utils/BlobNetworkHeader.h b/src/libYARP_wire_rep_utils/src/yarp/wire_rep_utils/BlobNetworkHeader.h deleted file mode 100644 index f3b7d145e15..00000000000 --- a/src/libYARP_wire_rep_utils/src/yarp/wire_rep_utils/BlobNetworkHeader.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT) - * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium - * SPDX-License-Identifier: BSD-3-Clause - */ - -#ifndef BLOBHEADER_INC -#define BLOBHEADER_INC - -#include -#include -#include - -namespace yarp::wire_rep_utils { - -// translate to blobs for now; better translation requires type system -YARP_BEGIN_PACK -class BlobNetworkHeader { -public: - yarp::os::NetInt32 listTag; - yarp::os::NetInt32 listLen; - yarp::os::NetInt32 blobLen; - - void init(int len) { - // state that the following data is a list containing 1 blob - listTag = BOTTLE_TAG_LIST + BOTTLE_TAG_BLOB; - listLen = 1; - blobLen = len; - } - -}; -YARP_END_PACK - -} // namespace yarp::wire_rep_utils - -#endif diff --git a/src/libYARP_wire_rep_utils/src/yarp/wire_rep_utils/WireBottle.cpp b/src/libYARP_wire_rep_utils/src/yarp/wire_rep_utils/WireBottle.cpp deleted file mode 100644 index 3a6dbb5be28..00000000000 --- a/src/libYARP_wire_rep_utils/src/yarp/wire_rep_utils/WireBottle.cpp +++ /dev/null @@ -1,130 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT) - * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium - * SPDX-License-Identifier: BSD-3-Clause - */ - -#include -#include "WireBottle.h" - -#include -#include -#include - -using namespace yarp::os; -using namespace yarp::wire_rep_utils; - -static NetInt32 getInt(char *cursor) { - auto* icursor = reinterpret_cast (cursor); - return *icursor; -} - -static char *checkBottle(char *cursor, int& remaining, int ct, int list_tag) { - while (remaining>0 && ct>0) { - ct--; - //printf("at %ld : %d : %d\n", (long int) cursor, remaining, list_tag); - int tag = 0; - if (list_tag!=0) { - tag = list_tag; - } else { - if (remaining<4) { - return nullptr; - } - tag = getInt(cursor); - cursor += 4; - remaining -= 4; - } - //printf("tag is %d\n", tag); - switch (tag) { - case BOTTLE_TAG_INT32: - case BOTTLE_TAG_VOCAB32: - if (remaining<4) { return nullptr; } - cursor += 4; - remaining -= 4; - break; - case BOTTLE_TAG_FLOAT64: - if (remaining<8) { return nullptr; } - cursor += 8; - remaining -= 8; - break; - case BOTTLE_TAG_STRING: - case BOTTLE_TAG_BLOB: - { - if (remaining<4) { - return nullptr; - } - NetInt32 len = getInt(cursor); - cursor += 4; - remaining -= 4; - if (len<0||len>remaining) { - return nullptr; - } - cursor += len; - remaining -= len; - } - break; - default: - if (tag&BOTTLE_TAG_LIST) { - if (remaining<4) { - return nullptr; - } - NetInt32 len = getInt(cursor); - cursor += 4; - remaining -= 4; - cursor = checkBottle(cursor,remaining,len,tag&0xff); - if (cursor == nullptr) { - return nullptr; - } - } else { - return nullptr; - } - break; - } - } - if (remaining!=0) { return nullptr; } - if (ct!=0) { return nullptr; } - return cursor; -} - -bool WireBottle::checkBottle(void *cursor, int len) { - int rem = len; - return ::checkBottle((char *)cursor,rem,1,0) != nullptr; -} - -bool WireBottle::extractBlobFromBottle(yarp::os::SizedWriter& src, - SizedWriterTail& dest) { - size_t total_len = 0; - for (size_t i=0; i=12) { - // could this be a Bottle compatible blob? - char *base = (char*)src.data(0); - Bytes b1(base,4); - Bytes b2(base+4,4); - Bytes b3(base+8,4); - int i1 = NetType::netInt(b1); - int i2 = NetType::netInt(b2); - int i3 = NetType::netInt(b3); - //dbg_printf(">>> %d %d %d\n", i1, i2, i3); - if (i1==BOTTLE_TAG_LIST+BOTTLE_TAG_BLOB) { - if (i2==1) { - if ((size_t)i3==total_len - 12) { - // good enough - //dbg_printf("Header detected\n"); - has_header = true; - payload_index = 0; - payload_offset = 12; - } - } - } - } - if (has_header) { - dest.setDelegate(&src,payload_index,payload_offset); - return true; - } - return false; -} diff --git a/src/libYARP_wire_rep_utils/src/yarp/wire_rep_utils/WireBottle.h b/src/libYARP_wire_rep_utils/src/yarp/wire_rep_utils/WireBottle.h deleted file mode 100644 index 3316dc844a8..00000000000 --- a/src/libYARP_wire_rep_utils/src/yarp/wire_rep_utils/WireBottle.h +++ /dev/null @@ -1,82 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT) - * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium - * SPDX-License-Identifier: BSD-3-Clause - */ - -#ifndef YARP2_WIREBOTTLE -#define YARP2_WIREBOTTLE - -#include - -#include - -namespace yarp::wire_rep_utils { - -class YARP_wire_rep_utils_API SizedWriterTail : - public yarp::os::SizedWriter -{ -private: - yarp::os::SizedWriter *delegate; - size_t payload_index, payload_offset; -public: - void setDelegate(yarp::os::SizedWriter *delegate, int index, - int offset) { - this->delegate = delegate; - payload_index = index; - payload_offset = offset; - } - - size_t length() const override { - return delegate->length()-payload_index; - } - - size_t headerLength() const override { - return 0; // not supported - } - - size_t length(size_t index) const override { - index += payload_index; - if (index == payload_index) { - return delegate->length(index) - payload_offset; - } - return delegate->length(index); - } - - const char *data(size_t index) const override { - index += payload_index; - if (index == payload_index) { - return delegate->data(index) + payload_offset; - } - return delegate->data(index); - } - - yarp::os::PortReader *getReplyHandler() override { - return delegate->getReplyHandler(); - } - - yarp::os::Portable *getReference() override { - return delegate->getReference(); - } - - bool dropRequested() override { return false; } - - - void startWrite() const override { - } - - void stopWrite() const override { - } -}; - -class YARP_wire_rep_utils_API WireBottle -{ -public: - static bool checkBottle(void *cursor, int len); - static bool extractBlobFromBottle(yarp::os::SizedWriter& src, - SizedWriterTail& dest); -}; - -} // namespace yarp::wire_rep_utils - -#endif diff --git a/src/libYARP_wire_rep_utils/src/yarp/wire_rep_utils/WireImage.cpp b/src/libYARP_wire_rep_utils/src/yarp/wire_rep_utils/WireImage.cpp deleted file mode 100644 index 234bcee60e8..00000000000 --- a/src/libYARP_wire_rep_utils/src/yarp/wire_rep_utils/WireImage.cpp +++ /dev/null @@ -1,53 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT) - * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium - * SPDX-License-Identifier: BSD-3-Clause - */ - -#include "WireImage.h" - -#include - -using namespace yarp::os; -using namespace yarp::sig; -using namespace yarp::wire_rep_utils; - -FlexImage *WireImage::checkForImage(SizedWriter& writer) { - ImageNetworkHeader hdr; - char *header_buf = (char*)(&hdr); - size_t header_len = sizeof(hdr); - const char *img_buf = nullptr; - int img_len = 0; - hdr.imgSize = -1; - for (size_t i=0; i0) { - memcpy(header_buf,data,len); - header_len -= len; - header_buf += len; - } - if (header_len == 0) { - img_buf = data+len; - img_len = writer.length(i)-len; - } - } - if (hdr.imgSize==-1) { - return nullptr; - } - if (hdr.imgSize!=img_len) { - return nullptr; - } - //printf("Passing on a %dx%d image\n", hdr.width, hdr.height); - int w = hdr.width; - int h = hdr.height; - //int row_stride = hdr.imgSize/hdr.height; - img.setPixelCode(hdr.id); - img.setQuantum(hdr.quantum); - img.setExternal((char*)img_buf,w,h); - return &img; -} diff --git a/src/libYARP_wire_rep_utils/src/yarp/wire_rep_utils/WireImage.h b/src/libYARP_wire_rep_utils/src/yarp/wire_rep_utils/WireImage.h deleted file mode 100644 index a4f87dd184b..00000000000 --- a/src/libYARP_wire_rep_utils/src/yarp/wire_rep_utils/WireImage.h +++ /dev/null @@ -1,182 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT) - * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium - * SPDX-License-Identifier: BSD-3-Clause - */ - -#ifndef YARP2_WIREIMAGE -#define YARP2_WIREIMAGE - -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -namespace yarp::wire_rep_utils { - -/* -// layout for ROS: -// Header header -// uint32 seq --> +1 -// time stamp --> int32 secs, int32 nsecs (sync by time) -// string frame_id --> string! argh / just pass it along -// uint32 height -// uint32 width -// string encoding --> string! argh -// uint8 is_bigendian -// uint32 step -// uint8[] data --> real payload -*/ - -YARP_BEGIN_PACK -class RosImageStamp { -public: - yarp::os::NetInt32 seq; - yarp::os::NetInt32 sec; - yarp::os::NetInt32 nsec; -}; -YARP_END_PACK - -/** - * - * As far as YARP is concerned, on the wire to/from ROS a raw image has: - * + a variable 12 byte header: seq secs nsecs - * + a constant byte sequence with information about the image source - * + a 4 byte header with the length of the binary image payload - * + the binary image payload - * The "constant" sequence should change if image size or other details change - * but we optimize for the truly constant case. - * - */ -class RosWireImage : public yarp::os::SizedWriter { -private: - RosImageStamp ros_seq_stamp; - yarp::os::ManagedBytes ros_const_header; - const yarp::sig::FlexImage *image; -public: - RosWireImage() : - ros_seq_stamp({0,0,0}), - image(nullptr) - {} - - void init(const yarp::sig::FlexImage& img, - const std::string& frame) { - image = &img; - yarp::os::ConnectionWriter *pbuf = - yarp::os::ConnectionWriter::createBufferedConnectionWriter(); - if (!pbuf) { - ::exit(1); - } - yarp::os::ConnectionWriter& buf = *pbuf; - yarp::os::StringOutputStream ss; - // probably need to translate encoding format better, but at - // a guess "rgb" and "bgr" will work ok. - std::string encoding = - yarp::os::Vocab32::decode(image->getPixelCode()).c_str(); - switch (image->getPixelCode()) { - case VOCAB_PIXEL_BGR: - encoding = "bgr8"; - break; - case VOCAB_PIXEL_RGB: - encoding = "rgb8"; - break; - case VOCAB_PIXEL_MONO: - encoding = "mono8"; - break; - case VOCAB_PIXEL_MONO16: - encoding = "mono16"; - break; - case VOCAB_PIXEL_MONO_FLOAT: - encoding = "32FC1"; - break; - } - buf.appendString(frame); - buf.appendInt32(image->height()); - buf.appendInt32(image->width()); - buf.appendString(encoding); - char is_bigendian = 0; - buf.appendBlock(&is_bigendian,1); - buf.appendInt32((image->width()*image->getPixelSize())+image->getPadding()); - buf.appendInt32(image->getRawImageSize()); - buf.getBuffer()->write(ss); - std::string hdr = ss.toString(); - yarp::os::Bytes hdr_wrap((char*)hdr.c_str(),hdr.length()); - ros_const_header = yarp::os::ManagedBytes(hdr_wrap); - ros_const_header.copy(); - delete pbuf; - pbuf = 0 /*NULL*/; - } - - void update(const yarp::sig::FlexImage *img, int seq, double t) { - // We should check if img properties have changed. But we don't. - ros_seq_stamp.seq = seq; - ros_seq_stamp.sec = (int)(t); - ros_seq_stamp.nsec = (int)((t-(int)t)*1e9); - } - - size_t length() const override { return 3; } - - size_t headerLength() const override { return 0; } - - size_t length(size_t index) const override { - size_t result = 0; - switch (index) { - case 0: - result = sizeof(ros_seq_stamp); - break; - case 1: - result = ros_const_header.length(); - break; - case 2: - result = image->getRawImageSize(); - break; - default: - result = 0; - break; - } - return result; - } - - const char *data(size_t index) const override { - const char *result = 0 /*NULL*/; - switch (index) { - case 0: - result = (const char *)(&ros_seq_stamp); - break; - case 1: - result = ros_const_header.get(); - break; - case 2: - result = (const char *)(image->getRawImage()); - break; - } - return result; - } - - yarp::os::PortReader *getReplyHandler() override { return NULL; } - - yarp::os::Portable *getReference() override { return NULL; } - - bool dropRequested() override { return false; } - - void startWrite() const override {} - - void stopWrite() const override {} -}; - -class YARP_wire_rep_utils_API WireImage { -private: - yarp::sig::FlexImage img; -public: - yarp::sig::FlexImage *checkForImage(yarp::os::SizedWriter& writer); -}; - -} // namespace yarp::wire_rep_utils - -#endif diff --git a/src/libYARP_wire_rep_utils/src/yarp/wire_rep_utils/WireTwiddler.cpp b/src/libYARP_wire_rep_utils/src/yarp/wire_rep_utils/WireTwiddler.cpp deleted file mode 100644 index f394ba4800c..00000000000 --- a/src/libYARP_wire_rep_utils/src/yarp/wire_rep_utils/WireTwiddler.cpp +++ /dev/null @@ -1,1076 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT) - * SPDX-License-Identifier: BSD-3-Clause - */ - -#include "WireTwiddler.h" - -#include - -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -using namespace yarp::os; -using namespace yarp::wire_rep_utils; - -#define dbg_flag 0 -#define dbg_printf if (dbg_flag) printf - -WireTwiddlerWriter::~WireTwiddlerWriter() = default; - - -int WireTwiddler::configure(Bottle& desc, int offset, bool& ignored, - const std::string& vtag) { - int start = offset; - // example: list 4 int32 * float64 * vector int32 * vector int32 3 * - bool is_vector = false; - bool is_list = false; - bool ignore = false; - bool saving = false; - bool loading = false; - bool computing = false; - std::string kind = desc.get(offset).asString(); - std::string var; - offset++; - if (kind=="---") { - offset = desc.size(); - return offset; - } - if (kind=="skip"||kind=="compute") { - if (kind == "compute") { - computing = true; - } - ignore = true; - var = kind = desc.get(offset).asString(); - offset++; - } - if (kind.length()>0 && (kind[0]=='>'||kind[0]=='<')) { - saving = (kind[0]=='>'); - loading = (kind[0]=='<'); - ignore = saving; - var = kind.substr(1,kind.length()); - kind = desc.get(offset).asString(); - offset++; - } - - is_vector = (kind=="vector"); - is_list = (kind=="list"); - if (kind=="item_vector") { - is_vector = true; - is_list = true; - } - if (is_vector) { - kind = desc.get(offset).asString(); - offset++; - } - int len = 1; - bool data = false; - if (computing) { - data = true; - } - if (is_vector||is_list) { - Value v = desc.get(offset); - offset++; - if (v.isInt32()) { - len = v.asInt32(); - } else { - if (v.asString()!="*") { - fprintf(stderr,"Does not look like length: %s\n", - v.toString().c_str()); - } - len = -1; - if (is_list) { - fprintf(stderr,"List should have fixed length\n"); - len = 0; - } - data = true; - } - } - if (!is_list) { - if (!data) { - Value v = desc.get(offset); - offset++; - if (v.asString()!="*") { - fprintf(stderr,"Does not look like data: %s\n", - v.toString().c_str()); - } - data = true; - } - } - - int tag = 0; - int unit_length = 0; - int wire_unit_length = -1; - bool item = false; - if (kind=="item") { - kind = vtag; - item = true; - } - if (kind=="int8"||kind=="uint8"||kind=="bool") { - tag = BOTTLE_TAG_INT8; - unit_length = 1; - } else if (kind=="int16"||kind=="uint16") { - tag = BOTTLE_TAG_INT16; - unit_length = 2; - } else if (kind=="int32"||kind=="uint32") { - tag = BOTTLE_TAG_INT32; - unit_length = 4; - } else if (kind=="int64"||kind=="uint64") { - tag = BOTTLE_TAG_INT64; - unit_length = 8; - } else if (kind=="float32") { - tag = BOTTLE_TAG_FLOAT32; - unit_length = 4; - } else if (kind=="float64") { - tag = BOTTLE_TAG_FLOAT64; - unit_length = 8; - } else if (kind=="vocab") { - tag = BOTTLE_TAG_VOCAB32; - unit_length = 4; - } else if (kind=="string") { - tag = BOTTLE_TAG_STRING; - unit_length = -1; - //len = -1; - } else if (kind=="blob") { - tag = BOTTLE_TAG_BLOB; - unit_length = -1; - //len = -1; - } else if (kind=="list"||kind=="vector"||computing) { - //pass - } else { - fprintf(stderr,"%s does not know about %s\n", __FILE__, kind.c_str()); - std::exit(1); - } - - dbg_printf("Type %s (%s) len %d unit %d %s\n", - kind.c_str(), - is_list?"LIST":(is_vector?"VECTOR":"PRIMITIVE"), len, - unit_length, - ignore?"SKIP":""); - - if (!ignore) { - if (is_vector) { - buffer.push_back(BOTTLE_TAG_LIST+tag); - if (len!=-1) { - buffer.push_back(len); - } - } else if (is_list) { - buffer.push_back(BOTTLE_TAG_LIST); - buffer.push_back(len); - } else { - if (!item) { - buffer.push_back(tag); - } - } - } - - if (data) { - WireTwiddlerGap gap; - if (!ignore) { - gap.buffer_start = buffer_start; - gap.buffer_length = (int)buffer.size()-buffer_start; - buffer_start = (int)buffer.size(); - } else { - gap.buffer_start = 0; - gap.buffer_length = 0; - } - gap.unit_length = unit_length; - gap.wire_unit_length = (wire_unit_length!=-1)?wire_unit_length:unit_length; - gap.length = len; - gap.ignore_external = ignore; - gap.save_external = saving; - gap.load_external = loading; - gap.computing = computing; - gap.var_name = var; - Bottle tmp; - tmp.copy(desc,start,offset-start-1); - gap.origin = tmp.toString(); - gap.flavor = tag; - gaps.push_back(gap); - } - ignored = ignore; - - if (is_list) { - int i=0; - while (iprompt = prompt; - clear(); - std::string str(txt); - char *cstr = (char *)str.c_str(); - for (size_t i=0; iat&&next instances of 4-byte-length bytes followed by specified number of bytes]"); - } else if (gap.length==-1) { - sprintf(buf," [4-byte length] [*%d bytes]", gap.unit_length); - } else if (gap.length==1) { - sprintf(buf," [%d bytes]", gap.unit_length); - } else { - sprintf(buf," [%d*%d bytes]", gap.length, gap.unit_length); - } - item += buf; - } - if (result != "") { - result += " "; - } - result += item; - } - return result; -} - - -bool WireTwiddler::read(Bottle& bot, const Bytes& data) { - StringInputStream sis; - sis.add(data); - WireTwiddlerReader twiddled_input(sis,*this); - return ConnectionReader::readFromStream(bot,twiddled_input); -} - - -bool WireTwiddler::write(yarp::os::Bottle& bot, - yarp::os::ManagedBytes& data) { - StringOutputStream sos; - if (!writer) { - writer = ConnectionWriter::createBufferedConnectionWriter(); - } - if (!writer) { - return false; - } - SizedWriter *buf = writer->getBuffer(); - if (!buf) { - return false; - } - buf->clear(); - bot.write(*writer); - WireTwiddlerWriter twiddled_output(*buf,*this); - twiddled_output.write(sos); - std::string result = sos.toString(); - data = ManagedBytes(Bytes((char*)result.c_str(),result.length()),false); - data.copy(); - return true; -} - - -void WireTwiddlerReader::compute(const WireTwiddlerGap& gap) { - if (gap.var_name == "image_params") { - int w = prop.find("width").asInt32(); - int h = prop.find("height").asInt32(); - int step = prop.find("step").asInt32(); - bool bigendian = prop.find("is_bigendian").asInt32()==1; - if (bigendian) { - fprintf(stderr,"Sorry, cannot handle bigendian images yet.\n"); - std::exit(1); - } - std::string encoding = prop.find("encoding").asString(); - int bpp = 1; - int translated_encoding = 0; - switch (Vocab32::encode(encoding)) { - case yarp::os::createVocab32('b','g','r','8'): - bpp = 3; - translated_encoding = VOCAB_PIXEL_BGR; - break; - case yarp::os::createVocab32('r','g','b','8'): - bpp = 3; - translated_encoding = VOCAB_PIXEL_RGB; - break; - case yarp::os::createVocab32('3','2','F','C'): - yAssert(encoding=="32FC1"); - bpp = 4; - translated_encoding = VOCAB_PIXEL_MONO_FLOAT; - break; - case yarp::os::createVocab32('1','6','U','C'): - yAssert(encoding=="16UC1"); - bpp = 2; - translated_encoding = VOCAB_PIXEL_MONO16; - break; - case yarp::os::createVocab32('m','o','n','o'): - yAssert(encoding=="mono8"||encoding=="mono16"); - if (encoding == "mono8") { - bpp = 1; - translated_encoding = VOCAB_PIXEL_MONO; - } else { - bpp = 2; - translated_encoding = VOCAB_PIXEL_MONO16; - } - break; - case yarp::os::createVocab32('b', 'a', 'y', 'e'): - bpp = 1; - translated_encoding = VOCAB_PIXEL_MONO; - if (encoding == "bayer_grbg8") { - translated_encoding = VOCAB_PIXEL_ENCODING_BAYER_GRBG8; - } else { - fprintf(stderr, "Warning automatic debayering not yet supported, keeping raw format.\n"); - } - - break; - default: - fprintf(stderr, "Sorry, cannot handle [%s] images yet.\n", - encoding.c_str()); - std::exit(1); - break; - } - int quantum = 1; - if (step!=w*bpp) { - bool ok = false; - while (quantum<=256) { - quantum *= 2; - if (((w*bpp+quantum)/quantum)*quantum == step) { - ok = true; - break; - } - } - if (!ok) { - quantum = step; - } - } - int img_size = step*h; - prop.put("depth",3); - prop.put("img_size",img_size); - prop.put("quantum",quantum); - prop.put("translated_encoding",translated_encoding); - } -} - -yarp::conf::ssize_t WireTwiddlerReader::read(Bytes& b) { - dbg_printf("Want %zu bytes\n", b.length()); - if (index==-1) { - dbg_printf("WireTwidderReader::read getting started\n"); - } - int ct = twiddler.getGapCount(); - if (ct<=0) { - fprintf(stderr,"WireTwidderReader, nothing available\n"); - return -1; - } - if (index==-1) { - index = 0; - sent = 0; - consumed = 0; - } - if (index>=ct) { - fprintf(stderr,"WireTwidderReader, nothing left\n"); - return -1; - } - bool more = false; - do { - const WireTwiddlerGap& gap = twiddler.getGap(index); - if (gap.computing) { - compute(gap); - } - dbg_printf("*** index %d sent %d consumed %d / len %d unit %d/%d\n", index, sent, consumed, gap.length, gap.unit_length, gap.wire_unit_length); - char *byte_start = gap.getStart(); - int byte_length = gap.getLength(); - if (byte_start != nullptr) { - byte_start += consumed; - byte_length -= consumed; - } - if (byte_start != nullptr && byte_length > 0) { - int len = b.length(); - if (len>byte_length) { - len = byte_length; - } - memcpy(b.get(),byte_start,len); - sent += len; - consumed += len; - auto* nn = reinterpret_cast (b.get()); - dbg_printf("WireTwidderReader sending %d boilerplate bytes:\n",len); - dbg_printf(" [[[%d]]]\n", (int)(*nn)); - return len; - } - if ((gap.length==-1||gap.unit_length==-1) && override_length==-1) { - dbg_printf("LOOKING TO EXTERNAL\n"); - Bytes bytes(reinterpret_cast(&lengthBuffer), sizeof(NetInt32)); - int r = is.readFull(bytes); - if (r != sizeof(NetInt32)) { - return -1; - } - dbg_printf("Read length %d\n", lengthBuffer); - pending_length = sizeof(lengthBuffer); - if (gap.length==-1) { - dbg_printf("Expect 4 + %d x %d\n", lengthBuffer, gap.unit_length); - if (gap.unit_length<0) { - pending_strings = lengthBuffer; - pending_string_length = 0; - pending_string_data = 0; - override_length = 0; - } else { - override_length = lengthBuffer*gap.unit_length; - } - } else { - override_length = lengthBuffer; - dbg_printf("Expect 1 x %d\n", lengthBuffer); - } - } - if (gap.shouldIgnoreExternal()) { - pending_length = 0; - } - if (pending_length) { - int len = b.length(); - if (len>pending_length) { - len = pending_length; - } - Bytes b2(b.get(),len); - memcpy(b.get(), - (char*)(&lengthBuffer)+sizeof(lengthBuffer)-pending_length, - len); - pending_length -= len; - dbg_printf("WireTwidderReader sending %d length bytes:\n",len); - dbg_printf(" (((%d)))\n", lengthBuffer); - return len; - } - while (pending_strings) { - dbg_printf("### %d pending strings\n", pending_strings); - if (pending_string_length==0&&pending_string_data==0) { - dbg_printf("Checking string length\n"); - Bytes bytes(reinterpret_cast(&lengthBuffer), sizeof(NetInt32)); - int r = is.readFull(bytes); - if (r != sizeof(NetInt32)) { - return -1; - } - dbg_printf("Read length %d\n", lengthBuffer); - pending_string_length = sizeof(lengthBuffer); - pending_string_data = lengthBuffer; - } - if (pending_string_length) { - int len = b.length(); - if (len>pending_string_length) { - len = pending_string_length; - } - Bytes b2(b.get(),len); - memcpy(b.get(), - (char*)(&lengthBuffer)+sizeof(lengthBuffer)-pending_string_length, - len); - pending_string_length -= len; - dbg_printf("WireTwidderReader sending %d string length bytes:\n",len); - dbg_printf(" (((%d)))\n", lengthBuffer); - if (pending_string_length==0&&pending_string_data==0) { pending_strings--; } - return len; - } - if (pending_string_data) { - int len = b.length(); - if (len>pending_string_data) { - len = pending_string_data; - } - Bytes b2(b.get(),len); - int r = is.readFull(b2); - if (r<0) { - fprintf(stderr,"No string payload bytes available\n"); - return r; - } - pending_string_data -= r; - dbg_printf("WireTwidderReader sending %d string payload bytes\n",r); - if (pending_string_data==0) { pending_strings--; } - return r; - } - } - int extern_length = gap.length * gap.unit_length; - if (gap.unit_length < 0 || gap.length < 0) { - extern_length = override_length; - } - dbg_printf("extern_length %d\n", extern_length); - - if (extern_length>sent-consumed) { - int len = b.length(); - if (len>extern_length) { - len = extern_length; - } - Bytes b2(b.get(),len); - int r = 0; - if (!gap.shouldIgnoreExternal()) { - r = readMapped(is,b2,gap); - auto* nn = reinterpret_cast (b.get()); - dbg_printf("WireTwidderReader sending %d payload bytes:\n",r); - dbg_printf(" [[[%d]]]\n", (int)(*nn)); - if (r>0) { - sent += r; - } - if (r<0) { - fprintf(stderr,"No payload bytes available\n"); - return r; - } - return r; - } else { - int len2 = extern_length; - if (gap.wire_unit_length>=0 && gap.wire_unit_length!=len2) { - len2 = gap.wire_unit_length; - } - dump.allocateOnNeed(len2,len2); - Bytes b3(dump.get(),len2); - r = is.readFull(b3); - auto* nn = reinterpret_cast (dump.get()); - if (gap.save_external) { - if (override_length>=0) { - prop.put(gap.var_name, - std::string((char *)(dump.get()), - len2)); - dbg_printf("Saved %s: %s\n", - gap.var_name.c_str(), - prop.find(gap.var_name).asString().c_str()); - } else if (len2>=4) { - prop.put(gap.var_name,(int)(*nn)); - dbg_printf("Saved %s: is %d\n", - gap.var_name.c_str(), - prop.find(gap.var_name).asInt32()); - } - } - dbg_printf("WireTwidderReader sending %d payload bytes:\n",r); - dbg_printf(" [[[%d]]]\n", (int)(*nn)); - dbg_printf(" (ignoring %d payload bytes)\n",r); - if (r>0) { - sent += r; - } - } - } - more = false; - if (indexheaderLength(); - lastBlock = parent->length()-block-1; - activeEmit = nullptr; - activeEmitLength = 0; - activeEmitOffset = -1; - - dbg_printf("Parent headers %zu blocks %zu\n", parent->headerLength(), parent->length()); - - for (int i=0; igetGapCount(); i++) { - if (errorState) { - return false; - } - std::string item; - const WireTwiddlerGap& gap = twiddler->getGap(i); - if (gap.buffer_length!=0) { - dbg_printf("Skip %d bytes\n", gap.byte_length); - skip(gap.byte_start,gap.byte_length); - if (gap.unit_length!=0) { - dbg_printf("Length %d unit_length %d\n", gap.length, - gap.unit_length); - if (gap.length==-1 && gap.unit_length==-1) { - dbg_printf("Pass [4-byte length] [ instances of 4-byte-length bytes followed by specified number of bytes]\n"); - readLengthAndPass(-1); - } else if (gap.length==1&&gap.unit_length==1) { - dbg_printf("Pass [%d bytes]\n", gap.unit_length); - pass(gap.unit_length); - } else if (gap.length==1&&gap.unit_length==-1) { - dbg_printf("Pass [4-byte length] [ bytes]\n"); - readLengthAndPass(1); - } else if (gap.length==-1) { - dbg_printf("Pass [4-byte length] [*%d bytes]\n", gap.unit_length); - readLengthAndPass(gap.unit_length,&gap); - } else { - dbg_printf("Pass [%d*%d bytes]\n", gap.length, gap.unit_length); - if (gap.unit_length!=gap.wire_unit_length) { - dbg_printf("Need to tweak length (not correct for neg numbers yet)...\n"); - for (int i=0; iwire_unit_length == unitLength) { - advance(unitLength*len,true); - } else { - for (int i=0; igetPrompt().c_str()); - if (evidence>=4) { - if (expected!=received) { - yError("Expected '%s', got '%s'\n", - Bottle::describeBottleCode(expected).c_str(), - Bottle::describeBottleCode(received).c_str()); - } - } - } - errorState = true; -} - - -int WireTwiddlerWriter::readLength() { - advance(4,true,true); - if (accumOffset==4) { - if (codeExpected!=codeReceived) { - if (lengthBuffer!=0) { - showBrokenExpectation(codeExpected,codeReceived,4); - } - codeExpected = codeReceived = 0; - } - return lengthBuffer; - } - return 0; -} - -bool WireTwiddlerWriter::advance(int length, bool shouldEmit, - bool shouldAccum, bool shouldCheck) { - accumOffset = 0; - if (length == 0) { - return true; - } - if (length < 0) { - return false; - } - while (length>0) { - if (blockPtr == nullptr) { - blockPtr = parent->data(block); - blockLen = parent->length(block); - dbg_printf(" block %d is at %ld, length %d\n",block, - (long int)blockPtr, blockLen); - offset = 0; - } - int rem = blockLen-offset; - if (rem==0) { - block++; - blockPtr = nullptr; - if (block>lastBlock) { - return false; - } - dbg_printf(" moved on to block %d\n",block); - continue; - } - if (rem > length) { - rem = length; - } - if (shouldCheck) { - dbg_printf("Type check against %ld\n", (long int)activeCheck); - int result = memcmp(activeCheck,blockPtr+offset,rem); - activeCheck += rem; - if (result!=0) { - NetInt32 t1 = 0; - NetInt32 t2 = 0; - if (rem>=4) { - t1 = *(reinterpret_cast(blockPtr+offset)); - t2 = *(reinterpret_cast(activeCheck-rem)); - if (t1!=t2 && (t1&BOTTLE_TAG_LIST) && (t2&BOTTLE_TAG_LIST)) { - // delay checking of codes until list length is read - // since list may be empty - codeExpected = t2; - codeReceived = t1; - result = 0; - } - } - if (result!=0) { - dbg_printf("Type check failed! >>>\n"); - showBrokenExpectation(t2,t1,rem); - return false; - } - } - } - if (shouldEmit) { - emit(blockPtr+offset, rem); - } - if (shouldAccum) { - if (accumOffset+rem>4) { - fprintf(stderr,"ACCUMULATION GONE WRONG %d %d\n", - accumOffset, rem); - ::exit(1); - } - memcpy(lengthBytes.get()+accumOffset,blockPtr+offset,rem); - accumOffset += rem; - } - offset += rem; - length -= rem; - } - return true; -} - -bool WireTwiddlerWriter::emit(const char *src, int len) { - int noffset = -1; - if (src != nullptr) { - if (activeGap) { - const WireTwiddlerGap& gap = *activeGap; - if (gap.wire_unit_length==4 && gap.unit_length==8 && - len == 8 && - gap.flavor == BOTTLE_TAG_FLOAT64) { - const auto* x = reinterpret_cast(src); - if (scratchOffset+4>scratch.length()) { - scratch.allocateOnNeed(scratchOffset+4,scratchOffset+4); - } - auto* y = reinterpret_cast(scratch.get()+scratchOffset); - *y = (NetFloat32) *x; - src = nullptr; - noffset = scratchOffset; - len = 4; - scratchOffset += 4; - } else { - fprintf(stderr,"WireTwidder::emit needs to be extended to deal with this case\n"); - ::exit(1); - } - activeGap = nullptr; - } - } - dbg_printf(" cache %ld len %d offset %d /// activeEmit %ld %d %d\n", (long int)src, len, noffset, (long int) activeEmit, activeEmitLength, activeEmitOffset); - if (activeEmit != nullptr || activeEmitOffset >= 0) { - bool push = false; - if (activeEmitOffset>=0 && noffset<0) { - push = true; - } else if (activeEmitOffset<0 && noffset>=0) { - push = true; - } else if (noffset==-1 && activeEmit+activeEmitLength!=src) { - push = true; - } - if (push) { - dbg_printf(" ** emit %ld len %d offset %d\n", (long int)activeEmit, - activeEmitLength, activeEmitOffset); - srcs.emplace_back((char*)activeEmit,activeEmitLength,activeEmitOffset); - activeEmit = nullptr; - activeEmitLength = 0; - activeEmitOffset = -1; - } else { - activeEmitLength += len; - dbg_printf(" ** extend %ld len %d offset %d\n", (long int)activeEmit, - activeEmitLength, activeEmitOffset); - return true; - } - } - if (src != nullptr || noffset >= 0) { - activeEmit = src; - activeEmitLength = len; - activeEmitOffset = noffset; - } - return true; -} - -bool WireTwiddlerWriter::transform(const WireTwiddlerGap& gap) { - // So far, very crude, does not cover all cases - if (gap.wire_unit_length>gap.unit_length) { - pass(gap.unit_length); - pad(gap.wire_unit_length-gap.unit_length); - return true; - } - if (gap.wire_unit_length==4 && gap.unit_length==8 && - gap.flavor == BOTTLE_TAG_FLOAT64) { - activeGap = ⪆ - pass(gap.unit_length); - return true; - } - fprintf(stderr,"WireTwidder::transform needs to be extended to deal with this case\n"); - ::exit(1); - return false; -} - -yarp::conf::ssize_t WireTwiddlerReader::readMapped(yarp::os::InputStream& is, - yarp::os::Bytes& b, - const WireTwiddlerGap& gap) { - if (gap.load_external) { - int v = 0; - if (gap.var_name[0]=='=') { - Bottle bot; - bot.fromString(gap.var_name.substr(1,gap.var_name.length())); - v = bot.get(0).asInt32(); - } else { - v = prop.find(gap.var_name).asInt32(); - } - dbg_printf("Read %s: %d\n", gap.var_name.c_str(), v); - for (size_t i=0; i (b.get()); - if (b.length()>=4) { - *nn = v; - } - return gap.unit_length; - } - if (gap.wire_unit_length==gap.unit_length) { - return is.read(b); - } - for (size_t i=0; ib.length()) { - len = b.length(); - } - Bytes b2(b.get(),len); - yarp::conf::ssize_t r = is.readFull(b2); - if (r==(yarp::conf::ssize_t)len) { - if (gap.flavor==BOTTLE_TAG_FLOAT64) { - if (gap.wire_unit_length==4 && - gap.unit_length==8) { - NetFloat32 x = *(reinterpret_cast (b2.get())); - auto* y = reinterpret_cast (b2.get()); - *y = x; - } - } - int len2 = gap.wire_unit_length-b.length(); - if (len2>0) { - dump.allocateOnNeed(len2,len2); - Bytes b3(dump.get(),len2); - is.readFull(b3); - } - return gap.unit_length; - } - return -1; -} - - - -//void write(OutputStream& os) { -//} diff --git a/src/libYARP_wire_rep_utils/src/yarp/wire_rep_utils/WireTwiddler.h b/src/libYARP_wire_rep_utils/src/yarp/wire_rep_utils/WireTwiddler.h deleted file mode 100644 index 9090dcb5c2d..00000000000 --- a/src/libYARP_wire_rep_utils/src/yarp/wire_rep_utils/WireTwiddler.h +++ /dev/null @@ -1,365 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT) - * SPDX-License-Identifier: BSD-3-Clause - */ - -#ifndef YARP2_WIRETWIDDLER -#define YARP2_WIRETWIDDLER - -#include -#include -#include -#include - -#include -#include -#include -#include - -#include - -#include -#include - -namespace yarp::wire_rep_utils { - -class WireTwiddlerGap -{ -public: - int buffer_start; - int buffer_length; - int length; - int unit_length; - int wire_unit_length; - char *byte_start; - int byte_length; - bool ignore_external; - bool save_external; - bool load_external; - bool computing; - std::string origin; - std::string var_name; - int flavor; - WireTwiddlerGap() { - buffer_start = 0; - buffer_length = 0; - length = 0; - unit_length = 0; - byte_start = nullptr; - byte_length = 0; - ignore_external = false; - wire_unit_length = -1; - save_external = false; - load_external = false; - computing = false; - flavor = 0; - } - - char *getStart() const { return byte_start; } - int getLength() const { return byte_length; } - int getUnitLength() const { return unit_length; } - int getExternalLength() const { return unit_length; } - bool shouldIgnoreExternal() const { return ignore_external; } - const std::string& getOrigin() const { return origin; } -}; - - -class YARP_wire_rep_utils_API WireTwiddler -{ -public: - WireTwiddler() { - buffer_start = 0; - writer = nullptr; - } - - virtual ~WireTwiddler() { - if (writer) { - delete writer; - } - writer = nullptr; - } - - bool configure(const char *txt, const char *prompt); - - - std::string fromTemplate(const yarp::os::Bottle& msg); - - /* - bool setHeaderFormat(const char *txt) { - headerFormat = txt; - } - */ - -private: - - int buffer_start; - std::vector buffer; - std::vector gaps; - yarp::os::ConnectionWriter *writer; - std::string prompt; - -public: - void show(); - int configure(yarp::os::Bottle& desc, int offset, bool& ignored, - const std::string& vtag); - - int getGapCount() { - return (int)gaps.size(); - } - - void clear() { - buffer_start = 0; - buffer.clear(); - gaps.clear(); - } - - const WireTwiddlerGap& getGap(int index) { - return gaps[index]; - } - - std::string toString() const; - - const std::string& getPrompt() const { - return prompt; - } - - bool read(yarp::os::Bottle& bot, const yarp::os::Bytes& data); - - bool write(yarp::os::Bottle& bot, yarp::os::ManagedBytes& data); -}; - - -/* - - State: - index, sent - - */ - -class YARP_wire_rep_utils_API WireTwiddlerReader : - public yarp::os::InputStream -{ -private: - yarp::os::InputStream& is; - WireTwiddler& twiddler; - char *cursor; - int sent; - int consumed; - int override_length; - int index; - bool recite; - yarp::os::NetInt32 lengthBuffer; - int pending_length; - int pending_strings; - int pending_string_length; - int pending_string_data; - yarp::os::ManagedBytes dump; - yarp::os::Property prop; -public: - WireTwiddlerReader(yarp::os::InputStream& is, - WireTwiddler& twiddler) : is(is), - twiddler(twiddler) { - reset(); - } - - void reset() { - recite = false; - index = -1; - sent = 0; - consumed = 0; - cursor = nullptr; - pending_length = 0; - pending_strings = 0; - pending_string_length = 0; - pending_string_data = 0; - override_length = -1; - lengthBuffer = 0; - } - - virtual ~WireTwiddlerReader() {} - - using yarp::os::InputStream::read; - yarp::conf::ssize_t read(yarp::os::Bytes& b) override; - - void close() override { is.close(); } - - bool isOk() const override { return is.isOk(); } - - yarp::conf::ssize_t readMapped(yarp::os::InputStream& is, - yarp::os::Bytes& b, - const WireTwiddlerGap& gap); - - void compute(const WireTwiddlerGap& gap); -}; - - -class YARP_wire_rep_utils_API WireTwiddlerSrc -{ -public: - char *src; - int len; - int offset; - - WireTwiddlerSrc(char *src, int len, int offset) { - this->src = src; - this->len = len; - this->offset = offset; - } -}; - -class YARP_wire_rep_utils_API WireTwiddlerWriter : - public yarp::os::SizedWriter -{ -private: - yarp::os::SizedWriter *parent; - WireTwiddler *twiddler; - std::vector srcs; - int block; - int lastBlock; - int offset; - const char *blockPtr; - int blockLen; - yarp::os::NetInt32 lengthBuffer; - yarp::os::Bytes lengthBytes; - yarp::os::ManagedBytes zeros; - yarp::os::ManagedBytes scratch; - int accumOffset; - const char *activeEmit; - const WireTwiddlerGap *activeGap; - int activeEmitLength; - int activeEmitOffset; - const char *activeCheck; - bool errorState; - size_t scratchOffset; - yarp::os::NetInt32 codeExpected; - yarp::os::NetInt32 codeReceived; - - void showBrokenExpectation(const yarp::os::NetInt32& expected, - const yarp::os::NetInt32& received, - int evidence); -public: - WireTwiddlerWriter(yarp::os::SizedWriter& parent, - WireTwiddler& twiddler) : - parent(&parent), - twiddler(&twiddler), - srcs(), - block(0), - lastBlock(0), - offset(0), - blockPtr(nullptr), - blockLen(0), - lengthBuffer(0), - lengthBytes(), - zeros(0), - scratch(0), - accumOffset(0), - activeEmit(nullptr), - activeGap(nullptr), - activeEmitLength(0), - activeEmitOffset(0), - activeCheck(nullptr), - errorState(false), - scratchOffset(0), - codeExpected(), - codeReceived() - { - update(); - } - - WireTwiddlerWriter() : - parent(nullptr), - twiddler(nullptr), - srcs(), - block(0), - lastBlock(0), - offset(0), - blockPtr(nullptr), - blockLen(0), - lengthBuffer(0), - lengthBytes(), - zeros(0), - scratch(0), - accumOffset(0), - activeEmit(nullptr), - activeGap(nullptr), - activeEmitLength(0), - activeEmitOffset(0), - activeCheck(nullptr), - errorState(false), - scratchOffset(0), - codeExpected(), - codeReceived() - {} - - virtual ~WireTwiddlerWriter(); - - void attach(yarp::os::SizedWriter& parent, - WireTwiddler& twiddler) { - this->parent = &parent; - this->twiddler = &twiddler; - update(); - } - - bool update(); - - size_t length() const override { - return srcs.size(); - } - - size_t headerLength() const override { - return 0; - } - - size_t length(size_t index) const override { - return srcs[index].len; - } - - const char *data(size_t index) const override { - if (srcs[index].offset < 0) { - return srcs[index].src; - } - return scratch.get()+srcs[index].offset; - } - - yarp::os::PortReader *getReplyHandler() override { - return parent->getReplyHandler(); - } - - yarp::os::Portable *getReference() override { - return parent->getReference(); - } - - bool skip(const char *start, int len); - - bool transform(const WireTwiddlerGap& gap); - - bool pass(int len); - - bool pad(size_t len); - - bool readLengthAndPass(int unitLength, const WireTwiddlerGap *gap = nullptr); - - bool advance(int length, bool shouldEmit, bool shouldAccum=false, - bool shouldCheck=false); - - bool emit(const char *src, int len); - - int readLength(); - - //virtual void write(OutputStream& os); - bool dropRequested() override { - return false; - } - - void startWrite() const override { - parent->startWrite(); - } - - void stopWrite() const override { - parent->stopWrite(); - } -}; - -} // namespace yarp::wire_rep_utils - -#endif diff --git a/src/libYARP_wire_rep_utils/src/yarp/wire_rep_utils/api.h b/src/libYARP_wire_rep_utils/src/yarp/wire_rep_utils/api.h deleted file mode 100644 index 343e6757500..00000000000 --- a/src/libYARP_wire_rep_utils/src/yarp/wire_rep_utils/api.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT) - * SPDX-License-Identifier: BSD-3-Clause - */ - -#ifndef YARP_WIRE_REP_UTILS_WIRE_REP_UTILS_API_H -#define YARP_WIRE_REP_UTILS_WIRE_REP_UTILS_API_H - -#include - -// avoid dll export until YARP_wire_rep_utils becomes dynamic again -#define YARP_wire_rep_utils_API - -#ifndef YARP_wire_rep_utils_API -# ifdef wire_rep_utils_EXPORTS -# define YARP_wire_rep_utils_API YARP_EXPORT -# define YARP_wire_rep_utils_EXTERN YARP_EXPORT_EXTERN -# define YARP_wire_rep_utils_DEPRECATED_API YARP_DEPRECATED_EXPORT -# define YARP_wire_rep_utils_DEPRECATED_API_MSG(X) YARP_DEPRECATED_MSG_EXPORT(X) -# else -# define YARP_wire_rep_utils_API YARP_IMPORT -# define YARP_wire_rep_utils_EXTERN YARP_IMPORT_EXTERN -# define YARP_wire_rep_utils_DEPRECATED_API YARP_DEPRECATED_IMPORT -# define YARP_wire_rep_utils_DEPRECATED_API_MSG(X) YARP_DEPRECATED_MSG_IMPORT(X) -# endif -#endif - -#endif // YARP_WIRE_REP_UTILS_API_WIRE_REP_UTILS_H diff --git a/src/libYARP_wire_rep_utils/tests/CMakeLists.txt b/src/libYARP_wire_rep_utils/tests/CMakeLists.txt deleted file mode 100644 index 129ae788e14..00000000000 --- a/src/libYARP_wire_rep_utils/tests/CMakeLists.txt +++ /dev/null @@ -1,31 +0,0 @@ -# SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT) -# SPDX-License-Identifier: BSD-3-Clause - -if(NOT TARGET YARP::YARP_wire_rep_utils) - return() -endif() - -include(YarpCatchUtils) - -add_executable(harness_wire_rep_utils) - -target_sources(harness_wire_rep_utils - PRIVATE - WireTest.cpp -) - -target_include_directories(harness_wire_rep_utils - PRIVATE - ${hmac_INCLUDE_DIRS} -) - -target_link_libraries(harness_wire_rep_utils - PRIVATE - YARP_harness - YARP::YARP_os - YARP::YARP_wire_rep_utils -) - -set_property(TARGET harness_wire_rep_utils PROPERTY FOLDER "Test") - -yarp_catch_discover_tests(harness_wire_rep_utils) diff --git a/src/libYARP_wire_rep_utils/tests/WireTest.cpp b/src/libYARP_wire_rep_utils/tests/WireTest.cpp deleted file mode 100644 index ed87a793462..00000000000 --- a/src/libYARP_wire_rep_utils/tests/WireTest.cpp +++ /dev/null @@ -1,165 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT) - * SPDX-License-Identifier: BSD-3-Clause - */ - - -#include - -#include -#include -#include -#include - -#include -#include - -#include -#include - -using namespace yarp::os; -using namespace yarp::os::impl; -using namespace yarp::wire_rep_utils; - - -void testSequence(char *seq, - size_t len, - const char *fmt, - Bottle ref, - bool testWrite = true) -{ - char err[1024]; - printf("\n"); - printf("================================================\n"); - printf(" READ %s\n", fmt); - Bytes b1(seq, len); - WireTwiddler tt; - tt.configure(fmt, fmt); - printf(">>> %s\n", tt.toString().c_str()); - Bottle bot; - - CHECK(tt.read(bot, b1)); // Read failed - std::snprintf(err, 1024, "%s: read %s, expected %s", fmt, bot.toString().c_str(), ref.toString().c_str()); - CHECK(bot == ref); - INFO(err); - printf("[1] %s: read %s as expected\n", fmt, bot.toString().c_str()); - - StringInputStream sis; - sis.add(b1); - sis.add(b1); - WireTwiddlerReader twiddled_input(sis, tt); - Route route; - bot.clear(); - twiddled_input.reset(); - ConnectionReader::readFromStream(bot, twiddled_input); - - std::snprintf(err, 1024, "%s: read %s, expected %s", fmt, bot.toString().c_str(), ref.toString().c_str()); - CHECK(bot == ref); - INFO(err); - printf("[2] %s: read %s as expected\n", fmt, bot.toString().c_str()); - - bot.clear(); - twiddled_input.reset(); - ConnectionReader::readFromStream(bot, twiddled_input); - - std::snprintf(err, 1024, "%s: read %s, expected %s", fmt, bot.toString().c_str(), ref.toString().c_str()); - CHECK(bot == ref); - INFO(err); - printf("[3] %s: read %s as expected\n", fmt, bot.toString().c_str()); - - if (testWrite) { - - printf("\n"); - printf("================================================\n"); - printf(" WRITE %s\n", fmt); - ManagedBytes output; - CHECK(tt.write(ref, output)); // WRITE FAILED - std::snprintf(err, 1024, "WRITE MISMATCH, length %zd, expected %zd", output.length(), len); - CHECK(output.length() == len); - INFO(err); - - for (size_t i = 0; i < output.length(); i++) { - std::snprintf(err, 1024, "WRITE MISMATCH, at %zd, have [%d:%c] expected [%d:%c]\n", i, output.get()[i], output.get()[i], seq[i], seq[i]); - CHECK(output.get()[i] == seq[i]); - INFO(err); - } - printf("[4] %s: wrote %s as expected\n", fmt, bot.toString().c_str()); - } -} - - -TEST_CASE("wire_rep_utils::WireTest", "[yarp::wire_rep_utils]") -{ - SECTION("check wire") - { - NetworkBase::setLocalMode(true); - { - INFO("checking vector int32 1 *"); - char seq[] = {42, 0, 0, 0}; - testSequence(seq, sizeof(seq),"vector int32 1 *", Bottle("42")); - } - { - INFO("checking vector string 1 *"); - char seq[] = {5, 0, 0, 0, 'h', 'e', 'l', 'l', 'o'}; - testSequence(seq, sizeof(seq),"vector string 1 *", Bottle("hello")); - } - { - INFO("checking vector int32 *"); - char seq[] = {2, 0, 0, 0, 42, 0, 0, 0, 55, 0, 0, 0}; - testSequence(seq, sizeof(seq),"vector int32 *", Bottle("42 55")); - } - { - INFO("checking vector string *"); - char seq[] = {2, 0, 0, 0, 3, 0, 0, 0, 'f', 'o', 'o', 3, 0, 0, 0, 'b', 'a', 'r'}; - testSequence(seq, sizeof(seq),"vector string *", Bottle("foo bar")); - } - { - INFO("checking list 2 vector string * int32 *"); - char seq[] = {2, 0, 0, 0, 3, 0, 0, 0, 'f', 'o', 'o', 3, 0, 0, 0, 'b', 'a', 'r', 12, 0, 0, 0}; - testSequence(seq, sizeof(seq),"list 2 vector string * int32 *", Bottle("(foo bar) 12")); - } - { - INFO("checking list 3 vector string * int32 * vector int32 *"); - char seq[] = {2, 0, 0, 0, 3, 0, 0, 0, 'f', 'o', 'o', 3, 0, 0, 0, 'b', 'a', 'r', 12, 0, 0, 0, 2, 0, 0, 0, 42, 0, 0, 0, 24, 0, 0, 0}; - testSequence(seq, sizeof(seq),"list 3 vector string * int32 * vector int32 *", Bottle("(foo bar) 12 (42 24)")); - } - { - INFO("checking vector int32 2 *"); - char seq[] = {42, 0, 0, 0, 24, 0, 0, 0}; - testSequence(seq, sizeof(seq),"vector int32 2 *", Bottle("42 24")); - } - { - INFO("checking list 2 string * int32 *"); - char seq[] = {5, 0, 0, 0, 'h', 'e', 'l', 'l', 'o', 12, 0, 0, 0}; - testSequence(seq, sizeof(seq),"list 2 string * int32 *", Bottle("hello 12")); - } - { - INFO("checking list 3 string * int32 * int32 *"); - char seq[] = {5, 0, 0, 0, 'h', 'e', 'l', 'l', 'o', 12, 0, 0, 0, 42, 0, 0, 0}; - testSequence(seq, sizeof(seq),"list 3 string * int32 * int32 *", Bottle("hello 12 42")); - } - { - INFO("checking list 2 string * skip int32 * int32 *"); - char seq[] = {5, 0, 0, 0, 'h', 'e', 'l', 'l', 'o', 12, 0, 0, 0, 42, 0, 0, 0}; - testSequence(seq, sizeof(seq),"list 2 string * skip int32 * int32 *", Bottle("hello 42"), false); - } - { - INFO("checking list 1 skip int32 * int32 *"); - char seq[] = {33, 0, 0, 0, 42, 0, 0, 0}; - testSequence(seq, sizeof(seq),"list 1 skip int32 * int32 *", Bottle("42"), false); - } - { - INFO("checking skip int32 * list 1 int32 *"); - char seq[] = {99, 0, 0, 0, 42, 0, 0, 0}; - testSequence(seq, sizeof(seq),"skip int32 * list 1 int32 *", Bottle("42"), false); - } - { - INFO("checking skip int32 *"); - char seq[] = {99, 0, 0, 0}; - testSequence(seq, sizeof(seq),"skip int32 *", Bottle(), false); - } - NetworkBase::setLocalMode(false); - - } - -}