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

190 lines
6.2 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) {
2022-05-09 00:25:48 +02:00
FSFW_LOGE("{}", "initializeInterface: Invalid cookie\n");
2022-02-02 10:29:30 +01:00
return NULLPOINTER;
}
2022-05-08 21:45:51 +02:00
auto* i2cCookie = dynamic_cast<I2cCookie*>(cookie);
2022-02-02 10:29:30 +01:00
i2cAddress = i2cCookie->getAddress();
2021-07-13 19:19:25 +02:00
2022-02-02 10:29:30 +01:00
i2cDeviceMapIter = i2cDeviceMap.find(i2cAddress);
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) {
2022-05-09 00:25:48 +02:00
FSFW_LOGW("initializeInterface: Failed to insert device with address {} to I2C device map\n",
i2cAddress);
2022-02-02 10:29:30 +01:00
return HasReturnvaluesIF::RETURN_FAILED;
2021-07-13 19:19:25 +02:00
}
2022-02-02 10:29:30 +01:00
return HasReturnvaluesIF::RETURN_OK;
}
2021-07-13 19:19:25 +02:00
2022-05-09 00:25:48 +02:00
FSFW_LOGE("initializeInterface: Device with address {} already in use\n", i2cAddress);
2022-02-02 10:29:30 +01:00
return HasReturnvaluesIF::RETURN_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) {
2022-05-09 00:25:48 +02:00
FSFW_LOGW("{}", "sendMessage: Send Data is nullptr\n");
2022-02-02 10:29:30 +01:00
return HasReturnvaluesIF::RETURN_FAILED;
}
2021-07-13 19:19:25 +02:00
2022-02-02 10:29:30 +01:00
if (sendLen == 0) {
return HasReturnvaluesIF::RETURN_OK;
}
2021-07-13 19:19:25 +02:00
2022-05-08 21:45:51 +02:00
auto* i2cCookie = dynamic_cast<I2cCookie*>(cookie);
2022-02-02 10:29:30 +01:00
if (i2cCookie == nullptr) {
2022-05-09 00:25:48 +02:00
FSFW_LOGWT("{}", "sendMessage: Invalid I2C Cookie\n");
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();
i2cDeviceMapIter = i2cDeviceMap.find(i2cAddress);
if (i2cDeviceMapIter == i2cDeviceMap.end()) {
2022-05-09 00:25:48 +02:00
FSFW_LOGWT("{}", "sendMessage: I2C address of cookie not registered in I2C device map\n");
2022-02-02 10:29:30 +01:00
return HasReturnvaluesIF::RETURN_FAILED;
}
deviceFile = i2cCookie->getDeviceFile();
UnixFileGuard fileHelper(deviceFile, &fd, O_RDWR, "I2cComIF::sendMessage");
if (fileHelper.getOpenResult() != HasReturnvaluesIF::RETURN_OK) {
return fileHelper.getOpenResult();
}
result = openDevice(deviceFile, i2cAddress, &fd);
if (result != HasReturnvaluesIF::RETURN_OK) {
return result;
}
if (write(fd, sendData, sendLen) != static_cast<int>(sendLen)) {
2022-05-09 00:25:48 +02:00
FSFW_LOGE("sendMessage: Failed to send data to I2C device with error code {} | {}\n", errno,
strerror(errno));
2022-02-02 10:29:30 +01:00
return HasReturnvaluesIF::RETURN_FAILED;
}
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-02-02 10:29:30 +01:00
return HasReturnvaluesIF::RETURN_OK;
2021-07-13 19:19:25 +02:00
}
2022-02-02 10:29:30 +01:00
ReturnValue_t I2cComIF::getSendSuccess(CookieIF* cookie) { return HasReturnvaluesIF::RETURN_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) {
return HasReturnvaluesIF::RETURN_OK;
}
2021-07-13 19:19:25 +02:00
2022-05-08 21:45:51 +02:00
auto* i2cCookie = dynamic_cast<I2cCookie*>(cookie);
2022-02-02 10:29:30 +01:00
if (i2cCookie == nullptr) {
2022-05-09 00:25:48 +02:00
FSFW_LOGWT("{}", "requestReceiveMessage: Invalid I2C Cookie\n");
2022-02-02 10:29:30 +01:00
i2cDeviceMapIter->second.replyLen = 0;
return NULLPOINTER;
}
2021-07-13 19:19:25 +02:00
2022-02-02 10:29:30 +01:00
address_t i2cAddress = i2cCookie->getAddress();
i2cDeviceMapIter = i2cDeviceMap.find(i2cAddress);
if (i2cDeviceMapIter == i2cDeviceMap.end()) {
2022-05-09 00:25:48 +02:00
FSFW_LOGW("requestReceiveMessage: I2C address {} of Cookie not registered in i2cDeviceMap",
i2cAddress);
2022-02-02 10:29:30 +01:00
i2cDeviceMapIter->second.replyLen = 0;
return HasReturnvaluesIF::RETURN_FAILED;
}
deviceFile = i2cCookie->getDeviceFile();
UnixFileGuard fileHelper(deviceFile, &fd, O_RDWR, "I2cComIF::requestReceiveMessage");
if (fileHelper.getOpenResult() != HasReturnvaluesIF::RETURN_OK) {
return fileHelper.getOpenResult();
}
result = openDevice(deviceFile, i2cAddress, &fd);
if (result != HasReturnvaluesIF::RETURN_OK) {
i2cDeviceMapIter->second.replyLen = 0;
return result;
}
uint8_t* replyBuffer = i2cDeviceMapIter->second.replyBuffer.data();
2022-05-08 21:45:51 +02:00
ssize_t readLen = read(fd, replyBuffer, requestLen);
2022-02-02 10:29:30 +01:00
if (readLen != static_cast<int>(requestLen)) {
2022-05-09 00:25:48 +02:00
FSFW_LOGWT(
2022-05-08 21:45:51 +02:00
"requestReceiveMessage: Reading from I2C device failed with error code "
"{} | {}\nRead only {} from {} bytes\n",
errno, strerror(errno), readLen, requestLen);
2022-02-02 10:29:30 +01:00
i2cDeviceMapIter->second.replyLen = 0;
return HasReturnvaluesIF::RETURN_FAILED;
}
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;
return HasReturnvaluesIF::RETURN_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) {
2022-05-08 21:45:51 +02:00
auto* i2cCookie = dynamic_cast<I2cCookie*>(cookie);
2022-02-02 10:29:30 +01:00
if (i2cCookie == nullptr) {
2022-05-09 00:25:48 +02:00
FSFW_LOGW("{}", "readReceivedMessage: Invalid I2C Cookie\n");
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();
i2cDeviceMapIter = i2cDeviceMap.find(i2cAddress);
if (i2cDeviceMapIter == i2cDeviceMap.end()) {
2022-05-09 00:25:48 +02:00
FSFW_LOGE("readReceivedMessage: I2C address {} of cookie not found in I2C device map\n",
i2cAddress);
2022-02-02 10:29:30 +01:00
return HasReturnvaluesIF::RETURN_FAILED;
}
*buffer = i2cDeviceMapIter->second.replyBuffer.data();
*size = i2cDeviceMapIter->second.replyLen;
2021-07-13 19:19:25 +02:00
2022-02-02 10:29:30 +01:00
return HasReturnvaluesIF::RETURN_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) {
2022-05-09 00:25:48 +02:00
FSFW_LOGWT("openDevice: Specifying target device failed with error code {} | {}\n", errno,
strerror(errno));
2022-02-02 10:29:30 +01:00
return HasReturnvaluesIF::RETURN_FAILED;
}
return HasReturnvaluesIF::RETURN_OK;
2021-07-13 19:19:25 +02:00
}