I'm using an XMEGA A3BU dev board, which has 2 hardware I2C interfaces. I'm trying to achieve a full, bi-directional man-in-the-middle "attack" between an I2C Master and an I2C Slave device. I tried using ASF, but I quickly realised it was using blocking operations, and it left me little to no time to send the received packets through. So I rolled my own implementation, which just checks if any of the flags are set, and acts appropriately. This approach works perfectly on normal "write" operations. I receive the data on my slave side, then store it in a buffer. My master side notices that there's data to send, and it sends it through. This is relatively easy.
My main issue is with "read" operations. The external master device wants to access some data from the external slave device, but my board is sitting in the middle. The "read" request hits my board's slave, which doesn't have the data to pass back. So it signals the on-board master to get 1 byte of data from the external slave. When it gets it, it cannot ack or nack the transaction, because we don't know if the external master would respond with an ack or nack to that single byte. So now the internal slave sends the byte to the external master, and waits for an ack or nack. Once that's done, the internal master can finally ack or nack the external slave. This goes on until the transaction is complete. To my understanding, this approach requires lots of signalling to tell which side has to do what, and whether it's done it already, or not. It got to a level of complexity, where my little brain can no longer reliably tell what's going on. I'm not using any interrupts. Am I going down the right route, or just completely missing something? Does this really require lots of signalling between the two sides to achieve this, or is there an easier approach?
Thank you very much for all the answers in advance!