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..
This commit is contained in:
Robin Müller 2023-03-04 11:19:19 +01:00
parent 33de15205b
commit fad9373a74
No known key found for this signature in database
GPG Key ID: 71B58F8A3CDFA9AC
2 changed files with 25 additions and 6 deletions

View File

@ -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;
}

View File

@ -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_ */