diff options
| author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-06-26 11:35:43 +0200 | 
|---|---|---|
| committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-06-26 11:35:43 +0200 | 
| commit | c3fee7eacd55a3f660f801b6be16bfe67baf4bfa (patch) | |
| tree | 0a0ca7323b4bcf3ad7020f91563464a6ca03a278 /client/i2c.cpp | |
| parent | a81cbaf61183fe2c471a48042b37f28ce3a70297 (diff) | |
fix send/recv detection in client dump
Diffstat (limited to 'client/i2c.cpp')
| -rw-r--r-- | client/i2c.cpp | 53 | 
1 files changed, 42 insertions, 11 deletions
diff --git a/client/i2c.cpp b/client/i2c.cpp index c8b2dc8..18be4bc 100644 --- a/client/i2c.cpp +++ b/client/i2c.cpp @@ -7,10 +7,18 @@  #include "xxd.h"  #include "pb.h" +#include "pb-buf.h" +#include "pb-msg.h"  #include "pb-types.h" +#include "pb-mod.h" +#ifdef DEBUG  bool i2c_dump_send = true;  bool i2c_dump_recv = true; +#else +bool i2c_dump_send = false; +bool i2c_dump_recv = false; +#endif  void i2c_send(uint16_t addr, const char * data, size_t data_size) {  	i2ctcp_msg_t msg = { @@ -24,20 +32,43 @@ void i2c_send(uint16_t addr, const char * data, size_t data_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);  } -void i2c_recv(const char * data, size_t data_size) { -	if (i2c_dump_recv) { -		_rl_printf_start(); -		printf("[%s] data(0x%02lx):\n", __FUNCTION__, data_size); -		xxd(data, data_size); -		_rl_printf_stop(); -	} +void i2c_dump(const i2ctcp_msg_t * msg) { +	pb_buf_t buf = { +		.data = (char *) msg->data, +		.size = msg->length, +	}; +	pb_msg_t * pb_msg = pb_msg_read(&buf); + +	// ignore invalid messages +	if (msg == NULL) return; +	// NOTE: I feel like this check is OK to do because the main controller isn't +	// supposed to be written to by I2C controllers that aren't puzzle modules. +	// I2C doesn't tell the receiver *who's* currently addressing it, which means +	// we have to assume all messages written to the main controller are puzzle +	// bus messages, and use the puzzle bus message to get the sender's address. +	i2c_addr_t sender, receiver; +	sender = receiver = pb_msg->sender; + +	bool send = sender == PB_MOD_ADDR; // = PB_ADDR_MOD_MAIN + +	if (send) receiver = msg->addr; +	else receiver = PB_MOD_ADDR; + +	pb_msg_free(pb_msg); + +	if (send && !i2c_dump_send) return; +	if (!send && !i2c_dump_recv) return; + +	_rl_printf_start(); + +	const char * direction = send ? "send" : "recv"; +	printf("[I2C %s] 0x%02x -> 0x%02x data(0x%02lx):\n", direction, sender, receiver, msg->length); +	xxd(msg->data, msg->length); + +	_rl_printf_stop();  }  |