From fad9373a74ac03dd3d129f99df26793e66ef13f9 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Sat, 4 Mar 2023 11:19:19 +0100 Subject: [PATCH] no close call in I2C API anymore - Not done in any examples or in official docs. This should not be an issue, but let's do it like it's recommended officially.. --- src/fsfw_hal/linux/i2c/I2cComIF.cpp | 29 +++++++++++++++++++++++------ src/fsfw_hal/linux/i2c/I2cComIF.h | 2 ++ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/src/fsfw_hal/linux/i2c/I2cComIF.cpp b/src/fsfw_hal/linux/i2c/I2cComIF.cpp index b1d54701..0f4a5c42 100644 --- a/src/fsfw_hal/linux/i2c/I2cComIF.cpp +++ b/src/fsfw_hal/linux/i2c/I2cComIF.cpp @@ -98,9 +98,9 @@ ReturnValue_t I2cComIF::sendMessage(CookieIF* cookie, const uint8_t* sendData, s } const auto& deviceFile = i2cCookie->getDeviceFile(); - UnixFileGuard fileHelper(deviceFile, fd, O_RDWR, "I2cComIF::sendMessage"); - if (fileHelper.getOpenResult() != returnvalue::OK) { - return fileHelper.getOpenResult(); + result = openI2cDev(deviceFile, fd); + if(result != returnvalue::OK) { + return result; } result = openI2cSlave(deviceFile, i2cAddress, fd); if (result != returnvalue::OK) { @@ -156,9 +156,9 @@ ReturnValue_t I2cComIF::requestReceiveMessage(CookieIF* cookie, size_t requestLe i2cDeviceMapIter->second.replyLen = 0; auto& deviceFile = i2cCookie->getDeviceFile(); - UnixFileGuard fileHelper(deviceFile, fd, O_RDWR, "I2cComIF::requestReceiveMessage"); - if (fileHelper.getOpenResult() != returnvalue::OK) { - return fileHelper.getOpenResult(); + result = openI2cDev(deviceFile, fd); + if(result != returnvalue::OK) { + return result; } result = openI2cSlave(deviceFile, i2cAddress, fd); if (result != returnvalue::OK) { @@ -235,3 +235,20 @@ ReturnValue_t I2cComIF::openI2cSlave(const std::string& deviceFile, address_t i2 } return returnvalue::OK; } + +ReturnValue_t I2cComIF::openI2cDev(const std::string &dev, int &fd) { + fd = open(dev.c_str(), O_RDWR); + if(fd < 0) { +#if FSFW_VERBOSE_LEVEL >= 1 +#if FSFW_CPP_OSTREAM_ENABLED == 1 + sif::warning << "I2cComIF: Opening device failed with error code " << errno << ": " + << strerror(errno) << std::endl; +#else + sif::printWarning("I2cComIF: Opening device failed with error code %d: %s\n", errno, + strerror(errno)); +#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */ +#endif /* FSFW_VERBOSE_LEVEL >= 1 */ + return returnvalue::FAILED; + } + return returnvalue::OK; +} diff --git a/src/fsfw_hal/linux/i2c/I2cComIF.h b/src/fsfw_hal/linux/i2c/I2cComIF.h index 983ce734..e0a823d2 100644 --- a/src/fsfw_hal/linux/i2c/I2cComIF.h +++ b/src/fsfw_hal/linux/i2c/I2cComIF.h @@ -51,6 +51,8 @@ class I2cComIF : public DeviceCommunicationIF, public SystemObject { */ ReturnValue_t openI2cSlave(const std::string &deviceFile, address_t i2cAddress, int &fileDescriptor); + ReturnValue_t openI2cDev(const std::string& dev, int&fd); + }; #endif /* LINUX_I2C_I2COMIF_H_ */