common: i2c: Add non-coroutine sendReceive()
This commit introduces a non-coroutine version of `sendReceive()` in the
common I2C module. The new implementation avoids potential stack buffer
issues by ensuring synchronous execution.
Change-Id: I1d308f24fafa6e0d68ffcc67073e0a2e67a6b419
Signed-off-by: Daniel Hsu <Daniel-Hsu@quantatw.com>
diff --git a/common/i2c/i2c.cpp b/common/i2c/i2c.cpp
index fb8bee0..653f823 100644
--- a/common/i2c/i2c.cpp
+++ b/common/i2c/i2c.cpp
@@ -76,6 +76,51 @@
co_return result;
}
+bool I2C::sendReceive(const std::vector<uint8_t>& writeData,
+ std::vector<uint8_t>& readData) const
+{
+ bool result = true;
+
+ if (fd <= 0)
+ {
+ return false;
+ }
+ else
+ {
+ struct i2c_msg msg[2];
+ struct i2c_rdwr_ioctl_data readWriteData;
+ int msgIndex = 0;
+
+ if (!writeData.empty())
+ {
+ msg[msgIndex].addr = deviceNode;
+ msg[msgIndex].flags = 0;
+ msg[msgIndex].len = writeData.size();
+ msg[msgIndex].buf = const_cast<uint8_t*>(writeData.data());
+ msgIndex++;
+ }
+
+ if (!readData.empty())
+ {
+ msg[msgIndex].addr = deviceNode;
+ msg[msgIndex].flags = I2C_M_RD;
+ msg[msgIndex].len = readData.size();
+ msg[msgIndex].buf = readData.data();
+ msgIndex++;
+ }
+
+ readWriteData.msgs = msg;
+ readWriteData.nmsgs = msgIndex;
+
+ if (ioctl(fd, I2C_RDWR, &readWriteData) < 0)
+ {
+ result = false;
+ }
+ }
+
+ return result;
+}
+
int I2C::close() const
{
return ::close(fd);
diff --git a/common/include/i2c/i2c.hpp b/common/include/i2c/i2c.hpp
index e86d574..d230ef7 100644
--- a/common/include/i2c/i2c.hpp
+++ b/common/include/i2c/i2c.hpp
@@ -42,6 +42,8 @@
sdbusplus::async::task<bool> sendReceive(
uint8_t* writeData, uint8_t writeSize, uint8_t* readData,
uint8_t readSize) const;
+ bool sendReceive(const std::vector<uint8_t>& writeData,
+ std::vector<uint8_t>& readData) const;
bool isOpen() const
{