From cc9b3beb1e82449e34dc3d7052395607fe12b47f Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 18 Jun 2024 19:29:05 +0200 Subject: WIP puzzle module <-> main controller <-> client communication --- client/i2c.cpp | 2 +- client/readme.md | 5 +++++ main/i2c.c | 24 +++++++++++++++++++++++- main/sock.c | 23 +++++++++++------------ main/sock.h | 3 --- test/CMakeLists.txt | 2 ++ test/pbdrv/pb-route.c | 2 ++ 7 files changed, 44 insertions(+), 17 deletions(-) create mode 100644 test/pbdrv/pb-route.c diff --git a/client/i2c.cpp b/client/i2c.cpp index 4dbc724..78e5585 100644 --- a/client/i2c.cpp +++ b/client/i2c.cpp @@ -10,7 +10,7 @@ // #include "pb/mod/main.h" -bool i2c_dump_send = false; +bool i2c_dump_send = true; bool i2c_dump_recv = true; void i2c_send(uint16_t addr, const char * data, size_t data_size) { diff --git a/client/readme.md b/client/readme.md index da48cf1..fcde40d 100644 --- a/client/readme.md +++ b/client/readme.md @@ -45,3 +45,8 @@ send 0x39 68:65:6c:6c:6f 44 0x20 'world' 33 The data is concatenated, and may contain mixed types of literals +## known bugs (TODO) + +- tab completion for `dump` seems to print garbage sometimes +- the send command with an address but no data causes a segmentation fault + diff --git a/main/i2c.c b/main/i2c.c index 612075b..2503560 100644 --- a/main/i2c.c +++ b/main/i2c.c @@ -10,13 +10,35 @@ #include "pb-mod.h" #include "pbdrv.h" #include "config.h" +#include "pb-buf.h" +#include "pb-send.h" i2c_addr_t modules[CFG_PB_MOD_MAX]; size_t modules_size = 0; +static void state_exchange() { + for (size_t i = 0; i < modules_size; i++) { + pb_buf_t buf = pb_send_state_req(); + + pb_buf_free(&buf); + } +} + void bus_task() { + // do a scan of the bus bus_scan(); - vTaskDelete(NULL); + + // FIXME: this should be removed (see handover: RP2040 I2C limitations) + // wait for 5 seconds until all handshake responses are received + pb_mod_blocking_delay_ms(5e3); + + while(1) { + // send my state to all puzzle modules + state_exchange(); + + // wait 1 second + pb_mod_blocking_delay_ms(1e3); + } } void pb_route_cmd_magic_res(pb_msg_t * msg) { diff --git a/main/sock.c b/main/sock.c index 6a5ff72..112e754 100644 --- a/main/sock.c +++ b/main/sock.c @@ -9,6 +9,7 @@ #include "config.h" #include "i2ctcpv1.h" #include "sock.h" +#include "pb-mod.h" struct netconn* current_connection = NULL; i2ctcp_msg_t recv_msg; @@ -34,16 +35,14 @@ void i2c_send(uint16_t addr, const char * data, size_t data_size) { free(buf); } -void i2c_recv(uint16_t addr, const char * data, size_t data_size) { - printf("address: 0x%02x\n", addr); - printf("data: \"%.*s\"\n", data_size, data); - - // send message back - char reply[] = "Test message back!"; - i2c_send(0x69, reply, strlen(reply)); - - // TODO: this function should forward the recieved message onto the puzzle - // bus instead of printing/replying +static void i2c_recv_fwd(uint16_t addr, const uint8_t * data, size_t data_size) { + if (addr == PB_MOD_ADDR) { + // addressed to me = act as recieved + pb_i2c_recv(data, data_size); + } else { + // addressed to other puzzle module = forward + pb_i2c_send(addr, data, data_size); + } } void recv_handler(struct netconn* conn, struct netbuf* buf) { @@ -55,10 +54,10 @@ void recv_handler(struct netconn* conn, struct netbuf* buf) { netbuf_data(buf, (void**)&data, &len); // continue early if more data is needed to complete message - if (!i2ctcp_read(&recv_msg, data, len)) continue; + if (i2ctcp_read(&recv_msg, data, len) != 0) continue; // forward received message to puzzle bus - i2c_recv(recv_msg.addr, recv_msg.data, recv_msg.length); + i2c_recv_fwd(recv_msg.addr, (uint8_t *) recv_msg.data, recv_msg.length); free(recv_msg.data); } while (netbuf_next(buf) >= 0); diff --git a/main/sock.h b/main/sock.h index 61828fb..38fca01 100644 --- a/main/sock.h +++ b/main/sock.h @@ -6,6 +6,3 @@ //! start listening for TCP socket requests void serve_task(); -void i2c_send(uint16_t addr, const char * data, size_t data_size); -void i2c_recv(uint16_t addr, const char * data, size_t data_size); - diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 30f3f77..1ec0cc6 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -10,12 +10,14 @@ project(pbtest C CXX ASM) add_executable(test # i2ctcp/main.cpp + pbdrv/pb-route.c pbdrv/msg.cpp pbdrv/mod.c ) add_subdirectory(lib/googletest) add_subdirectory(lib/pbdrv) +include(lib/pbdrv/ext/stdlib/include.cmake) add_subdirectory(lib/i2ctcp) target_link_libraries(test diff --git a/test/pbdrv/pb-route.c b/test/pbdrv/pb-route.c new file mode 100644 index 0000000..62a4b85 --- /dev/null +++ b/test/pbdrv/pb-route.c @@ -0,0 +1,2 @@ +void pb_mod_blocking_delay_ms(unsigned long ms) { } + -- cgit v1.2.3