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 +++++++++++++++++++++++++++++++++++++-------------- main/sock.c | 8 +++---- main/sock.h | 6 +++++ proto/include.cmake | 3 +++ proto/puzbusv1.c | 21 +++++++++++++++++- proto/puzbusv1.h | 2 +- 8 files changed, 88 insertions(+), 24 deletions(-) create mode 120000 client/lib 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; } diff --git a/main/sock.c b/main/sock.c index 7064de7..dac62af 100644 --- a/main/sock.c +++ b/main/sock.c @@ -5,9 +5,10 @@ #include #include "init.h" - #include "config.h" +struct netconn* current_connection = NULL; + void recv_handler(struct netconn* conn, struct netbuf* buf) { void *data; uint16_t len; @@ -21,17 +22,16 @@ void recv_handler(struct netconn* conn, struct netbuf* buf) { } void accept_handler(struct netconn* conn) { - printf("new connection!\n"); + current_connection = conn; struct netbuf* buf; - while (netconn_recv(conn, &buf) == ERR_OK) recv_handler(conn, buf); netconn_close(conn); netconn_delete(conn); - printf("connection closed!\n"); + current_connection = NULL; } void serve_task() { diff --git a/main/sock.h b/main/sock.h index dd7fc61..2a73418 100644 --- a/main/sock.h +++ b/main/sock.h @@ -1,5 +1,11 @@ #pragma once +#include +#include + /** \brief start listening for TCP socket requests */ void serve_task(); +void i2c_send(uint16_t addr, char* data, size_t data_size); +void i2c_recv(uint16_t addr, char* data, size_t data_size); + diff --git a/proto/include.cmake b/proto/include.cmake index c8a90b6..ac1305e 100644 --- a/proto/include.cmake +++ b/proto/include.cmake @@ -1,4 +1,7 @@ include_directories(${CMAKE_CURRENT_LIST_DIR}) +add_library(puzbus STATIC + ${CMAKE_CURRENT_LIST_DIR}/puzbusv1.c + ) # mpack include_directories(${CMAKE_CURRENT_LIST_DIR}/lib/mpack/src/mpack) diff --git a/proto/puzbusv1.c b/proto/puzbusv1.c index 9d1335e..3ff7c63 100644 --- a/proto/puzbusv1.c +++ b/proto/puzbusv1.c @@ -1,13 +1,32 @@ #include +#include #include "puzbusv1.h" int pb_read(struct pb_msg* target, char* buf, size_t buf_sz) { mpack_reader_t reader; + printf("read %lu bytes...\n", buf_sz); + + mpack_reader_init_data(&reader, buf, buf_sz); + + uint16_t address = mpack_expect_u16(&reader); + char data_buf[80]; + size_t data_size = mpack_expect_bin_buf(&reader, data_buf, sizeof(data_buf)); + + printf("0x%02x\n", address); + printf("\"%.*s\"\n", data_size, data_buf); return 0; } -void pb_free(struct pb_msg* msg); +int pb_write(struct pb_msg* target, char** buf, size_t* buf_sz) { + mpack_writer_t writer; + mpack_writer_init_growable(&writer, buf, buf_sz); + mpack_write_u16(&writer, target->addr); + mpack_write_bin(&writer, target->data, target->length); + + // finish writing + return mpack_writer_destroy(&writer) == mpack_ok; +} diff --git a/proto/puzbusv1.h b/proto/puzbusv1.h index 071c887..116dbf9 100644 --- a/proto/puzbusv1.h +++ b/proto/puzbusv1.h @@ -14,7 +14,7 @@ struct pb_msg { }; int pb_read(struct pb_msg* target, char* buf, size_t buf_sz); -void pb_free(struct pb_msg* msg); +int pb_write(struct pb_msg* target, char** buf, size_t* buf_sz); #ifdef __cplusplus } -- cgit v1.2.3