diff options
author | ThomasintAnker <thomasintanker1@gmail.com> | 2024-05-31 15:06:06 +0200 |
---|---|---|
committer | ThomasintAnker <thomasintanker1@gmail.com> | 2024-05-31 15:06:06 +0200 |
commit | 0278037aaf3fd497aae57d90f2638ceda3b12a6d (patch) | |
tree | 3354a14d4551b9524108d02971bfa42fc1ad4993 /client/i2c.cpp | |
parent | b865921e5dcf2ae2d6532b88eba1a0a49998eb27 (diff) | |
parent | 18d06c79b9f6a625eb218a15c8216556fb99dc02 (diff) |
Merge branch 'wip/client' into wip/i2c-communication
Diffstat (limited to 'client/i2c.cpp')
-rw-r--r-- | client/i2c.cpp | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/client/i2c.cpp b/client/i2c.cpp new file mode 100644 index 0000000..ee57e20 --- /dev/null +++ b/client/i2c.cpp @@ -0,0 +1,67 @@ +#include <stdio.h> +#include <stdlib.h> + +#include "i2ctcpv1.h" +#include "sock.h" +#include "xxd.h" + +#include "pb/bus.h" +#include "pb/types.h" + +#include "pb/mod/main.h" + +bool i2c_dump_send = false; +bool i2c_dump_recv = true; + +void i2c_send(uint16_t addr, const char * data, size_t data_size) { + i2ctcp_msg_t msg = { + .addr = addr, + .data = (char *) data, + .length = data_size, + }; + + char* packed; + size_t size; + if (!i2ctcp_write(&msg, &packed, &size)) return; + + sock->send(packed, size); + if (i2c_dump_send) { + printf("[%s] addr(0x%02x) data(0x%02lx):\n", __FUNCTION__, addr, data_size); + xxd(data, data_size); + } + + free(packed); +} + +static void i2c_handle_cmd_read(uint16_t, const char *, size_t); + +void i2c_recv(uint16_t addr, const char * data, size_t data_size) { + if (i2c_dump_recv) { + printf("[%s] addr(0x%02x) data(0x%02lx):\n", __FUNCTION__, addr, data_size); + xxd(data, data_size); + } + + if (data_size == 0) return; + enum pb_cmd cmd = (enum pb_cmd) data[0]; + data++; data_size--; + + switch (cmd) { + case PB_CMD_READ: return i2c_handle_cmd_read(addr, data, data_size); + default: return; + } +} + +static void i2c_handle_cmd_read(uint16_t i2c_addr, const char * buf, size_t sz) { + if (sz < 2) return; // require data address + 1 byte of data + pb_cmd_read_t * cmd = (pb_cmd_read_t *) buf; + sz--; // sz now represents size of cmd->data + + if (i2c_addr == BUSADDR_MAIN && cmd->address == 0x01) { + if (sz % 2 != 0) return; // invalid data + for (size_t offset = 0; offset < sz; offset += sizeof(pb_mod_main_mod_t)) { + pb_mod_main_mod_t * mod = (pb_mod_main_mod_t *) (cmd->data + offset); + printf("module at addr 0x%02x with state %d\n", mod->addr, mod->state); + } + } +} + |