From 1152ec32b6086c153dc41da59c9c451aa4465995 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Sat, 18 May 2024 20:11:44 +0200 Subject: WIP send/receive w/ msgpack --- client/CMakeLists.txt | 10 +++++++-- client/lib | 1 + client/main.cpp | 61 +++++++++++++++++++++++++++++++++++++-------------- 3 files changed, 54 insertions(+), 18 deletions(-) create mode 120000 client/lib (limited to 'client') diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index 9e433b1..bcef4c0 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -4,11 +4,17 @@ set(CMAKE_C_STANDARD 11) set(CMAKE_CXX_STANDARD 17) set(CMAKE_EXPORT_COMPILE_COMMANDS 1) -include(../proto/include.cmake) - project(puzzlebox_client C CXX) +include(../proto/include.cmake) + add_executable(main main.cpp ) +target_link_libraries(main + puzbus + mpack + ) + + diff --git a/client/lib b/client/lib new file mode 120000 index 0000000..dc598c5 --- /dev/null +++ b/client/lib @@ -0,0 +1 @@ +../lib \ No newline at end of file diff --git a/client/main.cpp b/client/main.cpp index 7a05049..30d7045 100644 --- a/client/main.cpp +++ b/client/main.cpp @@ -1,25 +1,54 @@ #include -#include - -#include "puzbusv1.pb.h" - -int main() { - GOOGLE_PROTOBUF_VERIFY_VERSION; - - puzbus::I2CMsg test_msg; +#include +#include +#include + +#include "puzbusv1.h" + +int send_message() { + const char* data = "Test message data!"; + struct pb_msg output = { + .addr = 0x39, + .data = (char*) data, + .length = strlen(data), + }; + + char* packed; + size_t size; + if (!pb_write(&output, &packed, &size)) { + printf("error writing!\n"); + return 1; + } - test_msg.set_address(0x39); - test_msg.set_data("Test message data!"); + fwrite(packed, sizeof(packed[0]), size, stdout); + fflush(stdout); - std::string output; - test_msg.SerializeToString(&output); + return 0; +} - printf("output[%lu]:\n", output.size()); - for (size_t i = 0; i < output.size(); i++) { - printf("%02x ", output[i]); +int read_message() { + freopen(NULL, "rb", stdin); // allow binary on stdin + struct pb_msg input; + + char buf[8]; // extremely small buffer to test chunked message parsing + size_t bytes = 0; + while ((bytes = fread(buf, sizeof(buf[0]), sizeof(buf), stdin)) > 0) { + if (!pb_read(&input, buf, bytes)) continue; + + printf("address: 0x%02x\n", input.addr); + printf("data: \"%.*s\"\n", input.length, input.data); + free(input.data); + return 0; } - printf("\n"); + return 1; +} + +int main() { + if (!isatty(fileno(stdout))) return send_message(); + if (!isatty(fileno(stdin))) return read_message(); + + printf("please pipe some data in or out to use this program\n"); return 0; } -- cgit v1.2.3