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

466 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) {
2022-05-02 15:12:38 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::error << "LinuxLibgpioIF::addGpios: Invalid cookie" << std::endl;
2022-05-02 15:12:38 +02:00
#endif
2022-02-02 10:29:30 +01:00
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) {
ReturnValue_t result = RETURN_OK;
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-04-25 14:19:03 +02:00
result = configureGpioByChip(gpioConfig.first, *regularGpio);
2022-02-02 10:29:30 +01:00
break;
}
case (gpio::GpioTypes::GPIO_REGULAR_BY_LABEL): {
auto regularGpio = dynamic_cast<GpiodRegularByLabel*>(gpioConfig.second);
if (regularGpio == nullptr) {
return GPIO_INVALID_INSTANCE;
}
result = configureGpioByLabel(gpioConfig.first, *regularGpio);
2022-02-02 10:29:30 +01:00
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
}
result = configureGpioByLineName(gpioConfig.first, *regularGpio);
2022-02-02 10:29:30 +01:00
break;
}
case (gpio::GpioTypes::CALLBACK): {
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
}
if (result != RETURN_OK) {
return GPIO_INIT_FAILED;
2022-04-25 14:19:03 +02:00
}
2022-02-02 10:29:30 +01:00
}
return result;
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) {
2022-05-02 15:12:38 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::warning << "LinuxLibgpioIF::configureGpioByLabel: Failed to open gpio from gpio "
<< "group with label " << label << ". Gpio ID: " << gpioId << std::endl;
2022-05-02 15:12:38 +02:00
#endif
2022-02-02 10:29:30 +01:00
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) {
2022-05-02 15:12:38 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::warning << "LinuxLibgpioIF::configureGpioByChip: Failed to open chip " << chipname
<< ". Gpio ID: " << gpioId << std::endl;
2022-05-02 15:12:38 +02:00
#endif
2022-02-02 10:29:30 +01:00
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) {
2022-05-02 15:12:38 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::warning << "LinuxLibgpioIF::configureGpioByLineName: Failed to open chip " << chipname
<< ". <Gpio ID: " << gpioId << std::endl;
2022-05-02 15:12:38 +02:00
#endif
2022-02-02 10:29:30 +01:00
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) {
2022-05-02 15:12:38 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
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;
2022-05-02 15:12:38 +02:00
#endif
2022-02-02 10:29:30 +01:00
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-03-07 16:07:01 +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-03-07 16:07:01 +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-05-02 15:12:38 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::error << "LinuxLibgpioIF::configureGpios: Invalid direction specified" << std::endl;
2022-05-02 15:12:38 +02:00
#endif
2022-02-02 10:29:30 +01:00
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()) {
2022-05-13 00:25:52 +02:00
#if FSFW_CPP_OSTREAM_ENABLED == 1
2022-02-02 10:29:30 +01:00
sif::warning << "LinuxLibgpioIF::pullHigh: Unknown GPIO ID " << gpioId << std::endl;
2022-05-13 00:25:52 +02:00
#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-03-07 16:07:01 +01:00
return driveGpio(gpioId, *regularGpio, gpio::Levels::HIGH);
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::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-03-07 16:07:01 +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-03-07 16:07:01 +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
2022-03-07 16:07:01 +01:00
<< " to logic level " << static_cast<int>(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;
}
case (gpio::GpioTypes::CALLBACK): {
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): {
if (gpioType == gpio::GpioTypes::NONE or gpioType == gpio::GpioTypes::CALLBACK) {
eraseDuplicateDifferentType = true;
}
2022-02-02 10:29:30 +01:00
break;
}
case (gpio::GpioTypes::CALLBACK): {
if (gpioType != gpio::GpioTypes::CALLBACK) {
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
}