fsfw/hal/src/fsfw_hal/linux/gpio/LinuxLibgpioIF.cpp

448 lines
16 KiB
C++
Raw Normal View History

#include "LinuxLibgpioIF.h"
2022-02-02 10:29:30 +01:00
#include <gpiod.h>
#include <unistd.h>
2021-07-13 19:19:25 +02:00
#include <utility>
2022-02-02 10:29:30 +01:00
#include "fsfw/serviceinterface/ServiceInterface.h"
#include "fsfw_hal/common/gpio/GpioCookie.h"
#include "fsfw_hal/common/gpio/gpioDefinitions.h"
LinuxLibgpioIF::LinuxLibgpioIF(object_id_t objectId) : SystemObject(objectId) {}
2021-07-13 19:19:25 +02:00
LinuxLibgpioIF::~LinuxLibgpioIF() {
2022-02-02 10:29:30 +01:00
for (auto& config : gpioMap) {
delete (config.second);
}
2021-07-13 19:19:25 +02:00
}
ReturnValue_t LinuxLibgpioIF::addGpios(GpioCookie* gpioCookie) {
2022-02-02 10:29:30 +01:00
ReturnValue_t result;
if (gpioCookie == nullptr) {
sif::error << "LinuxLibgpioIF::addGpios: Invalid cookie" << std::endl;
return RETURN_FAILED;
}
2021-07-13 19:19:25 +02:00
2022-02-02 10:29:30 +01:00
GpioMap mapToAdd = gpioCookie->getGpioMap();
2021-07-13 19:19:25 +02:00
2022-02-02 10:29:30 +01:00
/* Check whether this ID already exists in the map and remove duplicates */
result = checkForConflicts(mapToAdd);
if (result != RETURN_OK) {
return result;
}
2021-07-13 19:19:25 +02:00
2022-02-02 10:29:30 +01:00
result = configureGpios(mapToAdd);
if (result != RETURN_OK) {
return RETURN_FAILED;
}
2021-07-13 19:19:25 +02:00
2022-02-02 10:29:30 +01:00
/* Register new GPIOs in gpioMap */
gpioMap.insert(mapToAdd.begin(), mapToAdd.end());
2021-07-13 19:19:25 +02:00
2022-02-02 10:29:30 +01:00
return RETURN_OK;
2021-07-13 19:19:25 +02:00
}
ReturnValue_t LinuxLibgpioIF::configureGpios(GpioMap& mapToAdd) {
2022-02-02 10:29:30 +01:00
for (auto& gpioConfig : mapToAdd) {
auto& gpioType = gpioConfig.second->gpioType;
switch (gpioType) {
case (gpio::GpioTypes::NONE): {
return GPIO_INVALID_INSTANCE;
}
case (gpio::GpioTypes::GPIO_REGULAR_BY_CHIP): {
auto regularGpio = dynamic_cast<GpiodRegularByChip*>(gpioConfig.second);
if (regularGpio == nullptr) {
return GPIO_INVALID_INSTANCE;
2021-07-13 19:19:25 +02:00
}
2022-02-02 10:29:30 +01:00
configureGpioByChip(gpioConfig.first, *regularGpio);
break;
}
case (gpio::GpioTypes::GPIO_REGULAR_BY_LABEL): {
auto regularGpio = dynamic_cast<GpiodRegularByLabel*>(gpioConfig.second);
if (regularGpio == nullptr) {
return GPIO_INVALID_INSTANCE;
}
2022-02-02 10:29:30 +01:00
configureGpioByLabel(gpioConfig.first, *regularGpio);
break;
}
case (gpio::GpioTypes::GPIO_REGULAR_BY_LINE_NAME): {
auto regularGpio = dynamic_cast<GpiodRegularByLineName*>(gpioConfig.second);
if (regularGpio == nullptr) {
return GPIO_INVALID_INSTANCE;
2021-07-13 19:19:25 +02:00
}
2022-02-02 10:29:30 +01:00
configureGpioByLineName(gpioConfig.first, *regularGpio);
break;
}
2022-02-28 14:56:37 +01:00
case (gpio::GpioTypes::CALLBACK): {
2022-02-02 10:29:30 +01:00
auto gpioCallback = dynamic_cast<GpioCallback*>(gpioConfig.second);
if (gpioCallback->callback == nullptr) {
return GPIO_INVALID_INSTANCE;
2021-07-13 19:19:25 +02:00
}
2022-02-02 10:29:30 +01:00
gpioCallback->callback(gpioConfig.first, gpio::GpioOperation::WRITE,
gpioCallback->initValue, gpioCallback->callbackArgs);
}
2021-07-13 19:19:25 +02:00
}
2022-02-02 10:29:30 +01:00
}
return RETURN_OK;
2021-07-13 19:19:25 +02:00
}
ReturnValue_t LinuxLibgpioIF::configureGpioByLabel(gpioId_t gpioId,
2022-02-02 10:29:30 +01:00
GpiodRegularByLabel& gpioByLabel) {
std::string& label = gpioByLabel.label;
struct gpiod_chip* chip = gpiod_chip_open_by_label(label.c_str());
if (chip == nullptr) {
sif::warning << "LinuxLibgpioIF::configureGpioByLabel: Failed to open gpio from gpio "
<< "group with label " << label << ". Gpio ID: " << gpioId << std::endl;
return RETURN_FAILED;
}
std::string failOutput = "label: " + label;
return configureRegularGpio(gpioId, chip, gpioByLabel, failOutput);
}
2022-02-02 10:29:30 +01:00
ReturnValue_t LinuxLibgpioIF::configureGpioByChip(gpioId_t gpioId, GpiodRegularByChip& gpioByChip) {
std::string& chipname = gpioByChip.chipname;
struct gpiod_chip* chip = gpiod_chip_open_by_name(chipname.c_str());
if (chip == nullptr) {
sif::warning << "LinuxLibgpioIF::configureGpioByChip: Failed to open chip " << chipname
<< ". Gpio ID: " << gpioId << std::endl;
return RETURN_FAILED;
}
std::string failOutput = "chipname: " + chipname;
return configureRegularGpio(gpioId, chip, gpioByChip, failOutput);
}
ReturnValue_t LinuxLibgpioIF::configureGpioByLineName(gpioId_t gpioId,
2022-02-02 10:29:30 +01:00
GpiodRegularByLineName& gpioByLineName) {
std::string& lineName = gpioByLineName.lineName;
char chipname[MAX_CHIPNAME_LENGTH];
unsigned int lineOffset;
int result =
gpiod_ctxless_find_line(lineName.c_str(), chipname, MAX_CHIPNAME_LENGTH, &lineOffset);
if (result != LINE_FOUND) {
parseFindeLineResult(result, lineName);
return RETURN_FAILED;
}
gpioByLineName.lineNum = static_cast<int>(lineOffset);
struct gpiod_chip* chip = gpiod_chip_open_by_name(chipname);
if (chip == nullptr) {
sif::warning << "LinuxLibgpioIF::configureGpioByLineName: Failed to open chip " << chipname
<< ". <Gpio ID: " << gpioId << std::endl;
return RETURN_FAILED;
}
std::string failOutput = "line name: " + lineName;
return configureRegularGpio(gpioId, chip, gpioByLineName, failOutput);
}
2021-07-13 19:19:25 +02:00
ReturnValue_t LinuxLibgpioIF::configureRegularGpio(gpioId_t gpioId, struct gpiod_chip* chip,
2022-02-02 10:29:30 +01:00
GpiodRegularBase& regularGpio,
std::string failOutput) {
unsigned int lineNum;
gpio::Direction direction;
std::string consumer;
struct gpiod_line* lineHandle;
int result = 0;
lineNum = regularGpio.lineNum;
lineHandle = gpiod_chip_get_line(chip, lineNum);
if (!lineHandle) {
sif::warning << "LinuxLibgpioIF::configureRegularGpio: Failed to open line " << std::endl;
sif::warning << "GPIO ID: " << gpioId << ", line number: " << lineNum << ", " << failOutput
<< std::endl;
sif::warning << "Check if Linux GPIO configuration has changed. " << std::endl;
gpiod_chip_close(chip);
return RETURN_FAILED;
}
direction = regularGpio.direction;
consumer = regularGpio.consumer;
/* Configure direction and add a description to the GPIO */
switch (direction) {
2022-02-28 14:56:37 +01:00
case (gpio::Direction::OUT): {
result = gpiod_line_request_output(lineHandle, consumer.c_str(),
static_cast<int>(regularGpio.initValue));
2022-02-02 10:29:30 +01:00
break;
}
2022-02-28 14:56:37 +01:00
case (gpio::Direction::IN): {
2022-02-02 10:29:30 +01:00
result = gpiod_line_request_input(lineHandle, consumer.c_str());
break;
2021-07-13 19:19:25 +02:00
}
default: {
2022-02-02 10:29:30 +01:00
sif::error << "LinuxLibgpioIF::configureGpios: Invalid direction specified" << std::endl;
return GPIO_INVALID_INSTANCE;
2021-07-13 19:19:25 +02:00
}
2022-02-02 10:29:30 +01:00
if (result < 0) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::error << "LinuxLibgpioIF::configureRegularGpio: Failed to request line " << lineNum
<< " from GPIO instance with ID: " << gpioId << std::endl;
#else
2022-02-02 10:29:30 +01:00
sif::printError(
"LinuxLibgpioIF::configureRegularGpio: "
"Failed to request line %d from GPIO instance with ID: %d\n",
lineNum, gpioId);
#endif
gpiod_line_release(lineHandle);
return RETURN_FAILED;
2022-02-02 10:29:30 +01:00
}
}
/**
* Write line handle to GPIO configuration instance so it can later be used to set or
* read states of GPIOs.
*/
regularGpio.lineHandle = lineHandle;
return RETURN_OK;
2021-07-13 19:19:25 +02:00
}
ReturnValue_t LinuxLibgpioIF::pullHigh(gpioId_t gpioId) {
2022-02-02 10:29:30 +01:00
gpioMapIter = gpioMap.find(gpioId);
if (gpioMapIter == gpioMap.end()) {
sif::warning << "LinuxLibgpioIF::pullHigh: Unknown GPIO ID " << gpioId << std::endl;
return UNKNOWN_GPIO_ID;
}
auto gpioType = gpioMapIter->second->gpioType;
if (gpioType == gpio::GpioTypes::GPIO_REGULAR_BY_CHIP or
gpioType == gpio::GpioTypes::GPIO_REGULAR_BY_LABEL or
gpioType == gpio::GpioTypes::GPIO_REGULAR_BY_LINE_NAME) {
auto regularGpio = dynamic_cast<GpiodRegularBase*>(gpioMapIter->second);
if (regularGpio == nullptr) {
return GPIO_TYPE_FAILURE;
}
return driveGpio(gpioId, *regularGpio, gpio::HIGH);
} else {
auto gpioCallback = dynamic_cast<GpioCallback*>(gpioMapIter->second);
if (gpioCallback->callback == nullptr) {
return GPIO_INVALID_INSTANCE;
}
gpioCallback->callback(gpioMapIter->first, gpio::GpioOperation::WRITE, gpio::Levels::HIGH,
gpioCallback->callbackArgs);
return RETURN_OK;
}
return GPIO_TYPE_FAILURE;
2021-07-13 19:19:25 +02:00
}
ReturnValue_t LinuxLibgpioIF::pullLow(gpioId_t gpioId) {
2022-02-02 10:29:30 +01:00
gpioMapIter = gpioMap.find(gpioId);
if (gpioMapIter == gpioMap.end()) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::warning << "LinuxLibgpioIF::pullLow: Unknown GPIO ID " << gpioId << std::endl;
#else
2022-02-02 10:29:30 +01:00
sif::printWarning("LinuxLibgpioIF::pullLow: Unknown GPIO ID %d\n", gpioId);
#endif
2022-02-02 10:29:30 +01:00
return UNKNOWN_GPIO_ID;
}
auto& gpioType = gpioMapIter->second->gpioType;
if (gpioType == gpio::GpioTypes::GPIO_REGULAR_BY_CHIP or
gpioType == gpio::GpioTypes::GPIO_REGULAR_BY_LABEL or
gpioType == gpio::GpioTypes::GPIO_REGULAR_BY_LINE_NAME) {
auto regularGpio = dynamic_cast<GpiodRegularBase*>(gpioMapIter->second);
if (regularGpio == nullptr) {
return GPIO_TYPE_FAILURE;
}
2022-02-28 14:56:37 +01:00
return driveGpio(gpioId, *regularGpio, gpio::Levels::LOW);
2022-02-02 10:29:30 +01:00
} else {
auto gpioCallback = dynamic_cast<GpioCallback*>(gpioMapIter->second);
if (gpioCallback->callback == nullptr) {
return GPIO_INVALID_INSTANCE;
}
gpioCallback->callback(gpioMapIter->first, gpio::GpioOperation::WRITE, gpio::Levels::LOW,
gpioCallback->callbackArgs);
return RETURN_OK;
}
return GPIO_TYPE_FAILURE;
2021-07-13 19:19:25 +02:00
}
2022-02-02 10:29:30 +01:00
ReturnValue_t LinuxLibgpioIF::driveGpio(gpioId_t gpioId, GpiodRegularBase& regularGpio,
gpio::Levels logicLevel) {
2022-02-28 14:56:37 +01:00
int result = gpiod_line_set_value(regularGpio.lineHandle, static_cast<int>(logicLevel));
2022-02-02 10:29:30 +01:00
if (result < 0) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::warning << "LinuxLibgpioIF::driveGpio: Failed to pull GPIO with ID " << gpioId
<< " to logic level " << logicLevel << std::endl;
#else
2022-02-02 10:29:30 +01:00
sif::printWarning(
"LinuxLibgpioIF::driveGpio: Failed to pull GPIO with ID %d to "
"logic level %d\n",
gpioId, logicLevel);
#endif
2022-02-02 10:29:30 +01:00
return DRIVE_GPIO_FAILURE;
}
2021-07-13 19:19:25 +02:00
2022-02-02 10:29:30 +01:00
return RETURN_OK;
2021-07-13 19:19:25 +02:00
}
ReturnValue_t LinuxLibgpioIF::readGpio(gpioId_t gpioId, int* gpioState) {
2022-02-02 10:29:30 +01:00
gpioMapIter = gpioMap.find(gpioId);
if (gpioMapIter == gpioMap.end()) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::warning << "LinuxLibgpioIF::readGpio: Unknown GPIOD ID " << gpioId << std::endl;
#else
2022-02-02 10:29:30 +01:00
sif::printWarning("LinuxLibgpioIF::readGpio: Unknown GPIOD ID %d\n", gpioId);
#endif
2022-02-02 10:29:30 +01:00
return UNKNOWN_GPIO_ID;
}
auto gpioType = gpioMapIter->second->gpioType;
if (gpioType == gpio::GpioTypes::GPIO_REGULAR_BY_CHIP or
gpioType == gpio::GpioTypes::GPIO_REGULAR_BY_LABEL or
gpioType == gpio::GpioTypes::GPIO_REGULAR_BY_LINE_NAME) {
auto regularGpio = dynamic_cast<GpiodRegularBase*>(gpioMapIter->second);
if (regularGpio == nullptr) {
return GPIO_TYPE_FAILURE;
}
*gpioState = gpiod_line_get_value(regularGpio->lineHandle);
} else {
auto gpioCallback = dynamic_cast<GpioCallback*>(gpioMapIter->second);
if (gpioCallback->callback == nullptr) {
return GPIO_INVALID_INSTANCE;
}
gpioCallback->callback(gpioMapIter->first, gpio::GpioOperation::READ, gpio::Levels::NONE,
gpioCallback->callbackArgs);
2021-07-13 19:19:25 +02:00
return RETURN_OK;
2022-02-02 10:29:30 +01:00
}
return RETURN_OK;
2021-07-13 19:19:25 +02:00
}
2022-02-02 10:29:30 +01:00
ReturnValue_t LinuxLibgpioIF::checkForConflicts(GpioMap& mapToAdd) {
ReturnValue_t status = HasReturnvaluesIF::RETURN_OK;
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
for (auto& gpioConfig : mapToAdd) {
switch (gpioConfig.second->gpioType) {
case (gpio::GpioTypes::GPIO_REGULAR_BY_CHIP):
case (gpio::GpioTypes::GPIO_REGULAR_BY_LABEL):
case (gpio::GpioTypes::GPIO_REGULAR_BY_LINE_NAME): {
auto regularGpio = dynamic_cast<GpiodRegularBase*>(gpioConfig.second);
if (regularGpio == nullptr) {
return GPIO_TYPE_FAILURE;
}
// Check for conflicts and remove duplicates if necessary
result = checkForConflictsById(gpioConfig.first, gpioConfig.second->gpioType, mapToAdd);
if (result != HasReturnvaluesIF::RETURN_OK) {
status = result;
2021-07-13 19:19:25 +02:00
}
2022-02-02 10:29:30 +01:00
break;
}
2022-02-22 11:00:39 +01:00
case (gpio::GpioTypes::TYPE_CALLBACK): {
2022-02-02 10:29:30 +01:00
auto callbackGpio = dynamic_cast<GpioCallback*>(gpioConfig.second);
if (callbackGpio == nullptr) {
return GPIO_TYPE_FAILURE;
}
// Check for conflicts and remove duplicates if necessary
result = checkForConflictsById(gpioConfig.first, gpioConfig.second->gpioType, mapToAdd);
if (result != HasReturnvaluesIF::RETURN_OK) {
status = result;
2021-07-13 19:19:25 +02:00
}
2022-02-02 10:29:30 +01:00
break;
}
default: {
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::warning << "Invalid GPIO type detected for GPIO ID " << gpioConfig.first << std::endl;
#else
2022-02-02 10:29:30 +01:00
sif::printWarning("Invalid GPIO type detected for GPIO ID %d\n", gpioConfig.first);
#endif
2022-02-02 10:29:30 +01:00
status = GPIO_TYPE_FAILURE;
}
2021-07-13 19:19:25 +02:00
}
2022-02-02 10:29:30 +01:00
}
return status;
2021-07-13 19:19:25 +02:00
}
ReturnValue_t LinuxLibgpioIF::checkForConflictsById(gpioId_t gpioIdToCheck,
2022-02-02 10:29:30 +01:00
gpio::GpioTypes expectedType,
GpioMap& mapToAdd) {
// Cross check with private map
gpioMapIter = gpioMap.find(gpioIdToCheck);
if (gpioMapIter != gpioMap.end()) {
auto& gpioType = gpioMapIter->second->gpioType;
bool eraseDuplicateDifferentType = false;
switch (expectedType) {
case (gpio::GpioTypes::NONE): {
break;
}
case (gpio::GpioTypes::GPIO_REGULAR_BY_CHIP):
case (gpio::GpioTypes::GPIO_REGULAR_BY_LABEL):
case (gpio::GpioTypes::GPIO_REGULAR_BY_LINE_NAME): {
2022-02-22 11:00:39 +01:00
if (gpioType == gpio::GpioTypes::NONE or gpioType == gpio::GpioTypes::TYPE_CALLBACK) {
2022-02-02 10:29:30 +01:00
eraseDuplicateDifferentType = true;
}
2022-02-02 10:29:30 +01:00
break;
}
2022-02-22 11:00:39 +01:00
case (gpio::GpioTypes::TYPE_CALLBACK): {
if (gpioType != gpio::GpioTypes::TYPE_CALLBACK) {
2022-02-02 10:29:30 +01:00
eraseDuplicateDifferentType = true;
}
2022-02-02 10:29:30 +01:00
}
}
if (eraseDuplicateDifferentType) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::warning << "LinuxLibgpioIF::checkForConflicts: ID already exists for "
"different GPIO type "
<< gpioIdToCheck << ". Removing duplicate from map to add" << std::endl;
#else
2022-02-02 10:29:30 +01:00
sif::printWarning(
"LinuxLibgpioIF::checkForConflicts: ID already exists for "
"different GPIO type %d. Removing duplicate from map to add\n",
gpioIdToCheck);
#endif
2022-02-02 10:29:30 +01:00
mapToAdd.erase(gpioIdToCheck);
return GPIO_DUPLICATE_DETECTED;
}
2021-07-13 19:19:25 +02:00
2022-02-02 10:29:30 +01:00
// Remove element from map to add because a entry for this GPIO already exists
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::warning << "LinuxLibgpioIF::checkForConflictsRegularGpio: Duplicate GPIO "
"definition with ID "
<< gpioIdToCheck << " detected. "
<< "Duplicate will be removed from map to add" << std::endl;
#else
2022-02-02 10:29:30 +01:00
sif::printWarning(
"LinuxLibgpioIF::checkForConflictsRegularGpio: Duplicate GPIO definition "
"with ID %d detected. Duplicate will be removed from map to add\n",
gpioIdToCheck);
#endif
2022-02-02 10:29:30 +01:00
mapToAdd.erase(gpioIdToCheck);
return GPIO_DUPLICATE_DETECTED;
}
return HasReturnvaluesIF::RETURN_OK;
2021-07-13 19:19:25 +02:00
}
void LinuxLibgpioIF::parseFindeLineResult(int result, std::string& lineName) {
2022-02-02 10:29:30 +01:00
switch (result) {
#if FSFW_CPP_OSTREAM_ENABLED == 1
case LINE_NOT_EXISTS:
case LINE_ERROR: {
2022-02-02 10:29:30 +01:00
sif::warning << "LinuxLibgpioIF::parseFindeLineResult: Line with name " << lineName
<< " does not exist" << std::endl;
break;
2021-07-13 19:19:25 +02:00
}
default: {
2022-02-02 10:29:30 +01:00
sif::warning << "LinuxLibgpioIF::parseFindeLineResult: Unknown return code for line "
"with name "
<< lineName << std::endl;
break;
}
#else
case LINE_NOT_EXISTS:
case LINE_ERROR: {
2022-02-02 10:29:30 +01:00
sif::printWarning(
"LinuxLibgpioIF::parseFindeLineResult: Line with name %s "
"does not exist\n",
lineName);
break;
}
default: {
2022-02-02 10:29:30 +01:00
sif::printWarning(
"LinuxLibgpioIF::parseFindeLineResult: Unknown return code for line "
"with name %s\n",
lineName);
break;
}
#endif
2022-02-02 10:29:30 +01:00
}
2021-07-13 19:19:25 +02:00
}