2021-02-14 14:55:33 +01:00
|
|
|
#include "I2cComIF.h"
|
|
|
|
#include <fsfw/serviceinterface/ServiceInterface.h>
|
|
|
|
|
2021-01-08 09:34:43 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <linux/i2c-dev.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
|
|
I2cComIF::I2cComIF(object_id_t objectId): SystemObject(objectId){
|
|
|
|
}
|
|
|
|
|
|
|
|
I2cComIF::~I2cComIF() {}
|
|
|
|
|
|
|
|
ReturnValue_t I2cComIF::initializeInterface(CookieIF * cookie) {
|
2021-01-12 10:39:24 +01:00
|
|
|
|
|
|
|
address_t i2cAddress;
|
|
|
|
std::string deviceFile;
|
|
|
|
|
2021-01-08 09:34:43 +01:00
|
|
|
if(cookie == nullptr) {
|
|
|
|
return NULLPOINTER;
|
|
|
|
}
|
|
|
|
I2cCookie* i2cCookie = dynamic_cast<I2cCookie*>(cookie);
|
|
|
|
if(i2cCookie == nullptr) {
|
|
|
|
sif::error << "I2cComIF: Invalid I2C Cookie!"
|
|
|
|
<< std::endl;
|
|
|
|
return NULLPOINTER;
|
|
|
|
}
|
|
|
|
|
2021-01-12 10:39:24 +01:00
|
|
|
i2cAddress = i2cCookie->getAddress();
|
2021-01-08 09:34:43 +01:00
|
|
|
|
|
|
|
i2cDeviceMapIter = i2cDeviceMap.find(i2cAddress);
|
|
|
|
if(i2cDeviceMapIter == i2cDeviceMap.end()) {
|
|
|
|
size_t maxReplyLen = i2cCookie->getMaxReplyLen();
|
2021-01-12 10:39:24 +01:00
|
|
|
I2cInstance_t i2cInstance = {std::vector<uint8_t>(maxReplyLen), 0};
|
|
|
|
std::pair status = i2cDeviceMap.emplace(i2cAddress, i2cInstance);
|
|
|
|
if (status.second == false) {
|
|
|
|
sif::error << "I2cComIF::initializeInterface: Failed to insert "
|
|
|
|
<< "device with address " << i2cAddress << "to I2C device "
|
|
|
|
<< "map" << std::endl;
|
2021-02-22 09:24:42 +01:00
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
2021-01-12 10:39:24 +01:00
|
|
|
}
|
2021-01-08 09:34:43 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
sif::error << "I2cComIF: Device with address " << i2cAddress
|
|
|
|
<< "already in use" << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t I2cComIF::sendMessage(CookieIF *cookie,
|
|
|
|
const uint8_t *sendData, size_t sendLen) {
|
|
|
|
|
2021-01-12 10:39:24 +01:00
|
|
|
ReturnValue_t result;
|
|
|
|
int fd;
|
|
|
|
std::string deviceFile;
|
|
|
|
|
2021-01-08 09:34:43 +01:00
|
|
|
if(sendData == nullptr) {
|
|
|
|
sif::error << "I2cComIF::sendMessage: Send Data is nullptr"
|
|
|
|
<< std::endl;
|
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(sendLen == 0) {
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
I2cCookie* i2cCookie = dynamic_cast<I2cCookie*>(cookie);
|
|
|
|
if(i2cCookie == nullptr) {
|
|
|
|
sif::error << "I2cComIF::sendMessasge: Invalid I2C Cookie!"
|
|
|
|
<< std::endl;
|
|
|
|
return NULLPOINTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
address_t i2cAddress = i2cCookie->getAddress();
|
|
|
|
i2cDeviceMapIter = i2cDeviceMap.find(i2cAddress);
|
|
|
|
if (i2cDeviceMapIter == i2cDeviceMap.end()) {
|
|
|
|
sif::error << "I2cComIF::sendMessage: i2cAddress of Cookie not "
|
|
|
|
<< "registered in i2cDeviceMap" << std::endl;
|
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
2021-01-12 10:39:24 +01:00
|
|
|
|
|
|
|
deviceFile = i2cCookie->getDeviceFile();
|
|
|
|
result = openDevice(deviceFile, i2cAddress, &fd);
|
|
|
|
if (result != HasReturnvaluesIF::RETURN_OK){
|
|
|
|
return result;
|
|
|
|
}
|
2021-01-08 09:34:43 +01:00
|
|
|
|
|
|
|
if (write(fd, sendData, sendLen) != (int)sendLen) {
|
|
|
|
sif::error << "I2cComIF::sendMessage: Failed to send data to I2C "
|
|
|
|
"device with error code " << errno << ". Error description: "
|
|
|
|
<< strerror(errno) << std::endl;
|
2021-01-12 10:39:24 +01:00
|
|
|
close(fd);
|
2021-01-08 09:34:43 +01:00
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
2021-01-12 10:39:24 +01:00
|
|
|
close(fd);
|
2021-01-08 09:34:43 +01:00
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t I2cComIF::getSendSuccess(CookieIF *cookie) {
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t I2cComIF::requestReceiveMessage(CookieIF *cookie,
|
|
|
|
size_t requestLen) {
|
|
|
|
|
2021-01-12 10:39:24 +01:00
|
|
|
ReturnValue_t result;
|
|
|
|
int fd;
|
|
|
|
std::string deviceFile;
|
|
|
|
|
2021-01-08 09:34:43 +01:00
|
|
|
if (requestLen == 0) {
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
I2cCookie* i2cCookie = dynamic_cast<I2cCookie*>(cookie);
|
|
|
|
if(i2cCookie == nullptr) {
|
2021-01-16 12:22:17 +01:00
|
|
|
sif::error << "I2cComIF::requestReceiveMessage: Invalid I2C Cookie!"
|
2021-01-08 09:34:43 +01:00
|
|
|
<< std::endl;
|
2021-01-12 10:39:24 +01:00
|
|
|
i2cDeviceMapIter->second.replyLen = 0;
|
2021-01-08 09:34:43 +01:00
|
|
|
return NULLPOINTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
address_t i2cAddress = i2cCookie->getAddress();
|
|
|
|
i2cDeviceMapIter = i2cDeviceMap.find(i2cAddress);
|
|
|
|
if (i2cDeviceMapIter == i2cDeviceMap.end()) {
|
|
|
|
sif::error << "I2cComIF::requestReceiveMessage: i2cAddress of Cookie not "
|
|
|
|
<< "registered in i2cDeviceMap" << std::endl;
|
2021-01-12 10:39:24 +01:00
|
|
|
i2cDeviceMapIter->second.replyLen = 0;
|
2021-01-08 09:34:43 +01:00
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
2021-01-12 10:39:24 +01:00
|
|
|
|
|
|
|
deviceFile = i2cCookie->getDeviceFile();
|
|
|
|
result = openDevice(deviceFile, i2cAddress, &fd);
|
|
|
|
if (result != HasReturnvaluesIF::RETURN_OK){
|
|
|
|
i2cDeviceMapIter->second.replyLen = 0;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-01-08 09:34:43 +01:00
|
|
|
uint8_t* replyBuffer = i2cDeviceMapIter->second.replyBuffer.data();
|
|
|
|
|
|
|
|
if (read(fd, replyBuffer, requestLen) != (int)requestLen) {
|
|
|
|
sif::error << "I2cComIF::requestReceiveMessage: Reading from I2C "
|
|
|
|
<< "device failed with error code " << errno <<". Description"
|
|
|
|
<< " of error: " << strerror(errno) << std::endl;
|
2021-01-12 10:39:24 +01:00
|
|
|
close(fd);
|
|
|
|
i2cDeviceMapIter->second.replyLen = 0;
|
2021-01-08 09:34:43 +01:00
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
i2cDeviceMapIter->second.replyLen = requestLen;
|
|
|
|
|
2021-01-12 10:39:24 +01:00
|
|
|
close(fd);
|
|
|
|
|
2021-01-08 09:34:43 +01:00
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
ReturnValue_t I2cComIF::readReceivedMessage(CookieIF *cookie,
|
|
|
|
uint8_t **buffer, size_t* size) {
|
|
|
|
I2cCookie* i2cCookie = dynamic_cast<I2cCookie*>(cookie);
|
|
|
|
if(i2cCookie == nullptr) {
|
|
|
|
sif::error << "I2cComIF::readReceivedMessage: Invalid I2C Cookie!"
|
|
|
|
<< std::endl;
|
|
|
|
return NULLPOINTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
address_t i2cAddress = i2cCookie->getAddress();
|
|
|
|
i2cDeviceMapIter = i2cDeviceMap.find(i2cAddress);
|
|
|
|
if (i2cDeviceMapIter == i2cDeviceMap.end()) {
|
2021-01-12 10:39:24 +01:00
|
|
|
sif::error << "I2cComIF::readReceivedMessage: i2cAddress of Cookie not "
|
|
|
|
<< "found in i2cDeviceMap" << std::endl;
|
2021-01-08 09:34:43 +01:00
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
|
|
|
*buffer = i2cDeviceMapIter->second.replyBuffer.data();
|
|
|
|
*size = i2cDeviceMapIter->second.replyLen;
|
|
|
|
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
}
|
2021-01-12 10:39:24 +01:00
|
|
|
|
|
|
|
ReturnValue_t I2cComIF::openDevice(std::string deviceFile,
|
|
|
|
address_t i2cAddress, int* fileDescriptor) {
|
|
|
|
*fileDescriptor = open(deviceFile.c_str(), O_RDWR);
|
|
|
|
if (*fileDescriptor < 0) {
|
|
|
|
sif::error << "I2cComIF: Opening i2c device failed with error code "
|
|
|
|
<< errno << ". Error description: " << strerror(errno)
|
|
|
|
<< std::endl;
|
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ioctl(*fileDescriptor, I2C_SLAVE, i2cAddress) < 0) {
|
|
|
|
sif::error << "I2cComIF: Specifying target device failed with error "
|
|
|
|
<< "code " << errno << ". Error description "
|
|
|
|
<< strerror(errno) << std::endl;
|
|
|
|
return HasReturnvaluesIF::RETURN_FAILED;
|
|
|
|
}
|
|
|
|
return HasReturnvaluesIF::RETURN_OK;
|
|
|
|
}
|