fsfw/src/fsfw_hal/linux/i2c/I2cComIF.cpp

237 lines
7.4 KiB
C++
Raw Normal View History

2022-02-22 11:19:49 +01:00
#include "I2cComIF.h"
#include "fsfw/FSFW.h"
#include "fsfw/serviceinterface.h"
#include "fsfw_hal/linux/UnixFileGuard.h"
#include "fsfw_hal/linux/utility.h"
#if FSFW_HAL_I2C_WIRETAPPING == 1
#include "fsfw/globalfunctions/arrayprinter.h"
#endif
2021-07-13 19:19:25 +02:00
2022-02-02 10:29:30 +01:00
#include <errno.h>
2021-07-13 19:19:25 +02:00
#include <fcntl.h>
#include <linux/i2c-dev.h>
2022-02-02 10:29:30 +01:00
#include <sys/ioctl.h>
#include <unistd.h>
2021-07-13 19:19:25 +02:00
#include <cstring>
2022-02-02 10:29:30 +01:00
I2cComIF::I2cComIF(object_id_t objectId) : SystemObject(objectId) {}
2021-07-13 19:19:25 +02:00
I2cComIF::~I2cComIF() {}
ReturnValue_t I2cComIF::initializeInterface(CookieIF* cookie) {
2022-02-02 10:29:30 +01:00
address_t i2cAddress;
std::string deviceFile;
2021-07-13 19:19:25 +02:00
2022-02-02 10:29:30 +01:00
if (cookie == nullptr) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::error << "I2cComIF::initializeInterface: Invalid cookie!" << std::endl;
#endif
2022-02-02 10:29:30 +01:00
return NULLPOINTER;
}
I2cCookie* i2cCookie = dynamic_cast<I2cCookie*>(cookie);
if (i2cCookie == nullptr) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::error << "I2cComIF::initializeInterface: Invalid I2C cookie!" << std::endl;
#endif
2022-02-02 10:29:30 +01:00
return NULLPOINTER;
}
2021-07-13 19:19:25 +02:00
2022-02-02 10:29:30 +01:00
i2cAddress = i2cCookie->getAddress();
2021-07-13 19:19:25 +02:00
auto i2cDeviceMapIter = i2cDeviceMap.find(i2cAddress);
2022-02-02 10:29:30 +01:00
if (i2cDeviceMapIter == i2cDeviceMap.end()) {
size_t maxReplyLen = i2cCookie->getMaxReplyLen();
I2cInstance i2cInstance = {std::vector<uint8_t>(maxReplyLen), 0};
auto statusPair = i2cDeviceMap.emplace(i2cAddress, i2cInstance);
if (not statusPair.second) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::error << "I2cComIF::initializeInterface: Failed to insert device with address "
<< i2cAddress << "to I2C device "
<< "map" << std::endl;
#endif
2022-08-15 20:28:16 +02:00
return returnvalue::FAILED;
2021-07-13 19:19:25 +02:00
}
2022-08-15 20:28:16 +02:00
return returnvalue::OK;
2022-02-02 10:29:30 +01:00
}
2021-07-13 19:19:25 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::error << "I2cComIF::initializeInterface: Device with address " << i2cAddress
<< "already in use" << std::endl;
#endif
2022-08-15 20:28:16 +02:00
return returnvalue::FAILED;
2021-07-13 19:19:25 +02:00
}
2022-02-02 10:29:30 +01:00
ReturnValue_t I2cComIF::sendMessage(CookieIF* cookie, const uint8_t* sendData, size_t sendLen) {
ReturnValue_t result;
int fd;
std::string deviceFile;
2021-07-13 19:19:25 +02:00
2022-02-02 10:29:30 +01:00
if (sendData == nullptr) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::error << "I2cComIF::sendMessage: Send Data is nullptr" << std::endl;
#endif
2022-08-15 20:28:16 +02:00
return returnvalue::FAILED;
2022-02-02 10:29:30 +01:00
}
2021-07-13 19:19:25 +02:00
2022-02-02 10:29:30 +01:00
if (sendLen == 0) {
2022-08-15 20:28:16 +02:00
return returnvalue::OK;
2022-02-02 10:29:30 +01:00
}
2021-07-13 19:19:25 +02:00
2022-02-02 10:29:30 +01:00
I2cCookie* i2cCookie = dynamic_cast<I2cCookie*>(cookie);
if (i2cCookie == nullptr) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::error << "I2cComIF::sendMessage: Invalid I2C Cookie!" << std::endl;
#endif
2022-02-02 10:29:30 +01:00
return NULLPOINTER;
}
2021-07-13 19:19:25 +02:00
2022-02-02 10:29:30 +01:00
address_t i2cAddress = i2cCookie->getAddress();
auto i2cDeviceMapIter = i2cDeviceMap.find(i2cAddress);
2022-02-02 10:29:30 +01:00
if (i2cDeviceMapIter == i2cDeviceMap.end()) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::error << "I2cComIF::sendMessage: i2cAddress of Cookie not "
<< "registered in i2cDeviceMap" << std::endl;
#endif
2022-08-15 20:28:16 +02:00
return returnvalue::FAILED;
2022-02-02 10:29:30 +01:00
}
deviceFile = i2cCookie->getDeviceFile();
UnixFileGuard fileHelper(deviceFile, &fd, O_RDWR, "I2cComIF::sendMessage");
2022-08-15 20:28:16 +02:00
if (fileHelper.getOpenResult() != returnvalue::OK) {
2022-02-02 10:29:30 +01:00
return fileHelper.getOpenResult();
}
result = openDevice(deviceFile, i2cAddress, &fd);
2022-08-15 20:28:16 +02:00
if (result != returnvalue::OK) {
2022-02-02 10:29:30 +01:00
return result;
}
if (write(fd, sendData, sendLen) != static_cast<int>(sendLen)) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::error << "I2cComIF::sendMessage: Failed to send data to I2C "
"device with error code "
<< errno << ". Error description: " << strerror(errno) << std::endl;
#endif
2022-08-15 20:28:16 +02:00
return returnvalue::FAILED;
2022-02-02 10:29:30 +01:00
}
2022-02-22 11:19:49 +01:00
#if FSFW_HAL_I2C_WIRETAPPING == 1
sif::info << "Sent I2C data to bus " << deviceFile << ":" << std::endl;
arrayprinter::print(sendData, sendLen);
#endif
2022-08-15 20:28:16 +02:00
return returnvalue::OK;
2021-07-13 19:19:25 +02:00
}
2022-08-15 20:28:16 +02:00
ReturnValue_t I2cComIF::getSendSuccess(CookieIF* cookie) { return returnvalue::OK; }
2021-07-13 19:19:25 +02:00
2022-02-02 10:29:30 +01:00
ReturnValue_t I2cComIF::requestReceiveMessage(CookieIF* cookie, size_t requestLen) {
ReturnValue_t result;
int fd;
std::string deviceFile;
2021-07-13 19:19:25 +02:00
2022-02-02 10:29:30 +01:00
if (requestLen == 0) {
2022-08-15 20:28:16 +02:00
return returnvalue::OK;
2022-02-02 10:29:30 +01:00
}
2021-07-13 19:19:25 +02:00
2022-02-02 10:29:30 +01:00
I2cCookie* i2cCookie = dynamic_cast<I2cCookie*>(cookie);
if (i2cCookie == nullptr) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::error << "I2cComIF::requestReceiveMessage: Invalid I2C Cookie!" << std::endl;
#endif
2022-02-02 10:29:30 +01:00
return NULLPOINTER;
}
2021-07-13 19:19:25 +02:00
2022-02-02 10:29:30 +01:00
address_t i2cAddress = i2cCookie->getAddress();
auto i2cDeviceMapIter = i2cDeviceMap.find(i2cAddress);
2022-02-02 10:29:30 +01:00
if (i2cDeviceMapIter == i2cDeviceMap.end()) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::error << "I2cComIF::requestReceiveMessage: i2cAddress of Cookie not "
<< "registered in i2cDeviceMap" << std::endl;
#endif
2022-08-15 20:28:16 +02:00
return returnvalue::FAILED;
2022-02-02 10:29:30 +01:00
}
i2cDeviceMapIter->second.replyLen = 0;
2022-02-02 10:29:30 +01:00
deviceFile = i2cCookie->getDeviceFile();
UnixFileGuard fileHelper(deviceFile, &fd, O_RDWR, "I2cComIF::requestReceiveMessage");
2022-08-15 20:28:16 +02:00
if (fileHelper.getOpenResult() != returnvalue::OK) {
2022-02-02 10:29:30 +01:00
return fileHelper.getOpenResult();
}
result = openDevice(deviceFile, i2cAddress, &fd);
2022-08-15 20:28:16 +02:00
if (result != returnvalue::OK) {
2022-02-02 10:29:30 +01:00
return result;
}
uint8_t* replyBuffer = i2cDeviceMapIter->second.replyBuffer.data();
int readLen = read(fd, replyBuffer, requestLen);
if (readLen != static_cast<int>(requestLen)) {
2022-11-28 08:42:08 +01:00
#if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1
if (readLen < 0) {
sif::warning << "I2cComIF::requestReceiveMessage: Reading from I2C "
<< "device failed with error code " << errno << " | " << strerror(errno)
<< std::endl;
} else {
sif::warning << "I2cComIF::requestReceiveMessage: Read only " << readLen << " from "
<< requestLen << " bytes" << std::endl;
}
#else
#endif
#endif
2022-08-15 20:28:16 +02:00
return returnvalue::FAILED;
2022-02-02 10:29:30 +01:00
}
2021-07-13 19:19:25 +02:00
2022-02-22 11:19:49 +01:00
#if FSFW_HAL_I2C_WIRETAPPING == 1
sif::info << "I2C read bytes from bus " << deviceFile << ":" << std::endl;
arrayprinter::print(replyBuffer, requestLen);
#endif
2022-02-02 10:29:30 +01:00
i2cDeviceMapIter->second.replyLen = requestLen;
2022-08-15 20:28:16 +02:00
return returnvalue::OK;
2021-07-13 19:19:25 +02:00
}
2022-02-02 10:29:30 +01:00
ReturnValue_t I2cComIF::readReceivedMessage(CookieIF* cookie, uint8_t** buffer, size_t* size) {
I2cCookie* i2cCookie = dynamic_cast<I2cCookie*>(cookie);
if (i2cCookie == nullptr) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::error << "I2cComIF::readReceivedMessage: Invalid I2C Cookie!" << std::endl;
#endif
2022-02-02 10:29:30 +01:00
return NULLPOINTER;
}
2021-07-13 19:19:25 +02:00
2022-02-02 10:29:30 +01:00
address_t i2cAddress = i2cCookie->getAddress();
auto i2cDeviceMapIter = i2cDeviceMap.find(i2cAddress);
2022-02-02 10:29:30 +01:00
if (i2cDeviceMapIter == i2cDeviceMap.end()) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::error << "I2cComIF::readReceivedMessage: i2cAddress of Cookie not "
<< "found in i2cDeviceMap" << std::endl;
#endif
2022-08-15 20:28:16 +02:00
return returnvalue::FAILED;
2022-02-02 10:29:30 +01:00
}
*buffer = i2cDeviceMapIter->second.replyBuffer.data();
*size = i2cDeviceMapIter->second.replyLen;
2022-10-20 14:24:03 +02:00
i2cDeviceMapIter->second.replyLen = 0;
2022-08-15 20:28:16 +02:00
return returnvalue::OK;
2021-07-13 19:19:25 +02:00
}
2022-02-02 10:29:30 +01:00
ReturnValue_t I2cComIF::openDevice(std::string deviceFile, address_t i2cAddress,
int* fileDescriptor) {
if (ioctl(*fileDescriptor, I2C_SLAVE, i2cAddress) < 0) {
2021-07-13 19:19:25 +02:00
#if FSFW_VERBOSE_LEVEL >= 1
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::warning << "I2cComIF: Specifying target device failed with error code " << errno << "."
<< std::endl;
sif::warning << "Error description " << strerror(errno) << std::endl;
2021-07-13 19:19:25 +02:00
#else
2022-02-02 10:29:30 +01:00
sif::printWarning("I2cComIF: Specifying target device failed with error code %d.\n");
sif::printWarning("Error description: %s\n", strerror(errno));
2021-07-13 19:19:25 +02:00
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
#endif /* FSFW_VERBOSE_LEVEL >= 1 */
2022-08-15 20:28:16 +02:00
return returnvalue::FAILED;
2022-02-02 10:29:30 +01:00
}
2022-08-15 20:28:16 +02:00
return returnvalue::OK;
2021-07-13 19:19:25 +02:00
}