eive-obsw/linux/gpio/LinuxLibgpioIF.cpp

305 lines
11 KiB
C++
Raw Normal View History

2021-02-14 12:35:08 +01:00
#include "LinuxLibgpioIF.h"
#include "GpioCookie.h"
2021-02-14 12:35:08 +01:00
#include <fsfw/serviceinterface/ServiceInterface.h>
2021-02-23 13:24:05 +01:00
#include <linux/gpio/gpioDefinitions.h>
#include <utility>
2021-01-23 17:22:40 +01:00
#include <unistd.h>
#include <gpiod.h>
2021-01-23 17:22:40 +01:00
2021-02-23 13:24:05 +01:00
2021-01-23 17:22:40 +01:00
LinuxLibgpioIF::LinuxLibgpioIF(object_id_t objectId) : SystemObject(objectId) {
}
LinuxLibgpioIF::~LinuxLibgpioIF() {
}
2021-02-23 13:24:05 +01:00
ReturnValue_t LinuxLibgpioIF::addGpios(GpioCookie* gpioCookie) {
2021-02-14 12:57:09 +01:00
ReturnValue_t result;
if(gpioCookie == nullptr) {
sif::error << "LinuxLibgpioIF::initialize: Invalid cookie" << std::endl;
2021-02-14 12:57:09 +01:00
return RETURN_FAILED;
}
GpioMap mapToAdd = gpioCookie->getGpioMap();
2021-02-14 12:57:09 +01:00
2021-02-23 16:47:34 +01:00
/* Check whether this ID already exists in the map and remove duplicates */
2021-02-14 12:57:09 +01:00
result = checkForConflicts(mapToAdd);
if (result != RETURN_OK){
return result;
}
2021-02-14 18:21:59 +01:00
result = configureGpios(mapToAdd);
2021-02-14 12:57:09 +01:00
if (result != RETURN_OK) {
return RETURN_FAILED;
}
2021-02-23 13:24:05 +01:00
/* Register new GPIOs in gpioMap */
2021-02-14 12:57:09 +01:00
gpioMap.insert(mapToAdd.begin(), mapToAdd.end());
return RETURN_OK;
2021-02-12 21:23:35 +01:00
}
2021-02-14 18:21:59 +01:00
ReturnValue_t LinuxLibgpioIF::configureGpios(GpioMap& mapToAdd) {
2021-02-23 13:24:05 +01:00
for(auto& gpioConfig: mapToAdd) {
switch(gpioConfig.second->gpioType) {
case(gpio::GpioTypes::NONE): {
return GPIO_INVALID_INSTANCE;
}
case(gpio::GpioTypes::GPIOD_REGULAR): {
GpiodRegular* regularGpio = dynamic_cast<GpiodRegular*>(gpioConfig.second);
2021-02-23 16:47:34 +01:00
if(regularGpio == nullptr) {
return GPIO_INVALID_INSTANCE;
}
2021-02-23 13:24:05 +01:00
configureRegularGpio(gpioConfig.first, regularGpio);
break;
}
case(gpio::GpioTypes::CALLBACK): {
auto gpioCallback = dynamic_cast<GpioCallback*>(gpioMapIter->second);
if(gpioCallback->callback == nullptr) {
return GPIO_INVALID_INSTANCE;
}
gpioCallback->callback(gpioMapIter->first, gpio::GpioOperation::READ,
2021-02-23 16:47:34 +01:00
gpioCallback->initValue, gpioCallback->callbackArgs);
2021-02-23 13:24:05 +01:00
}
}
}
return RETURN_OK;
}
ReturnValue_t LinuxLibgpioIF::configureRegularGpio(gpioId_t gpioId, GpiodRegular *regularGpio) {
2021-02-12 21:23:35 +01:00
std::string chipname;
unsigned int lineNum;
struct gpiod_chip *chip;
2021-02-14 12:35:08 +01:00
gpio::Direction direction;
2021-02-12 21:23:35 +01:00
std::string consumer;
struct gpiod_line *lineHandle;
2021-02-23 13:24:05 +01:00
int result = 0;
2021-02-12 21:23:35 +01:00
2021-02-23 13:24:05 +01:00
chipname = regularGpio->chipname;
chip = gpiod_chip_open_by_name(chipname.c_str());
if (!chip) {
sif::error << "LinuxLibgpioIF::configureGpios: Failed to open chip "
<< chipname << ". Gpio ID: " << gpioId << std::endl;
return RETURN_FAILED;
}
lineNum = regularGpio->lineNum;
lineHandle = gpiod_chip_get_line(chip, lineNum);
if (!lineHandle) {
sif::error << "LinuxLibgpioIF::configureGpios: Failed to open line" << std::endl;
gpiod_chip_close(chip);
return RETURN_FAILED;
}
2021-02-12 21:23:35 +01:00
2021-02-23 13:24:05 +01:00
direction = regularGpio->direction;
consumer = regularGpio->consumer;
/* Configure direction and add a description to the GPIO */
switch (direction) {
case(gpio::OUT): {
result = gpiod_line_request_output(lineHandle, consumer.c_str(),
regularGpio->initValue);
if (result < 0) {
sif::error << "LinuxLibgpioIF::configureGpios: Failed to request line " << lineNum <<
" from GPIO instance with ID: " << gpioId << std::endl;
gpiod_line_release(lineHandle);
2021-02-12 21:23:35 +01:00
return RETURN_FAILED;
}
2021-02-23 13:24:05 +01:00
break;
}
case(gpio::IN): {
result = gpiod_line_request_input(lineHandle, consumer.c_str());
if (result < 0) {
sif::error << "LinuxLibgpioIF::configureGpios: Failed to request line "
<< lineNum << " from GPIO instance with ID: " << gpioId << std::endl;
gpiod_line_release(lineHandle);
2021-02-12 21:23:35 +01:00
return RETURN_FAILED;
}
2021-02-23 13:24:05 +01:00
break;
}
default: {
sif::error << "LinuxLibgpioIF::configureGpios: Invalid direction specified"
<< std::endl;
return GPIO_INVALID_INSTANCE;
2021-02-12 21:23:35 +01:00
}
2021-02-23 13:24:05 +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;
2021-02-12 21:23:35 +01:00
return RETURN_OK;
2021-01-23 17:22:40 +01:00
}
2021-02-23 13:24:05 +01:00
ReturnValue_t LinuxLibgpioIF::pullHigh(gpioId_t gpioId) {
gpioMapIter = gpioMap.find(gpioId);
if (gpioMapIter == gpioMap.end()) {
sif::warning << "LinuxLibgpioIF::driveGpio: Unknown GPIOD ID " << gpioId << std::endl;
return UNKNOWN_GPIO_ID;
}
if(gpioMapIter->second->gpioType == gpio::GpioTypes::GPIOD_REGULAR) {
return driveGpio(gpioId, dynamic_cast<GpiodRegular*>(gpioMapIter->second), 1);
}
else {
auto gpioCallback = dynamic_cast<GpioCallback*>(gpioMapIter->second);
if(gpioCallback->callback == nullptr) {
return GPIO_INVALID_INSTANCE;
}
gpioCallback->callback(gpioMapIter->first, gpio::GpioOperation::WRITE,
1, gpioCallback->callbackArgs);
}
return GPIO_TYPE_FAILURE;
2021-01-23 17:22:40 +01:00
}
2021-02-23 13:24:05 +01:00
ReturnValue_t LinuxLibgpioIF::pullLow(gpioId_t gpioId) {
gpioMapIter = gpioMap.find(gpioId);
if (gpioMapIter == gpioMap.end()) {
sif::warning << "LinuxLibgpioIF::driveGpio: Unknown GPIOD ID " << gpioId << std::endl;
return UNKNOWN_GPIO_ID;
}
if(gpioMapIter->second->gpioType == gpio::GpioTypes::GPIOD_REGULAR) {
return driveGpio(gpioId, dynamic_cast<GpiodRegular*>(gpioMapIter->second), 0);
}
else {
auto gpioCallback = dynamic_cast<GpioCallback*>(gpioMapIter->second);
if(gpioCallback->callback == nullptr) {
return GPIO_INVALID_INSTANCE;
}
gpioCallback->callback(gpioMapIter->first, gpio::GpioOperation::WRITE,
0, gpioCallback->callbackArgs);
}
return GPIO_TYPE_FAILURE;
2021-01-23 17:22:40 +01:00
}
ReturnValue_t LinuxLibgpioIF::driveGpio(gpioId_t gpioId,
2021-02-23 13:24:05 +01:00
GpiodRegular* regularGpio, unsigned int logicLevel) {
if(regularGpio == nullptr) {
return GPIO_TYPE_FAILURE;
2021-02-12 21:23:35 +01:00
}
2021-01-23 17:22:40 +01:00
2021-02-23 13:24:05 +01:00
int result = gpiod_line_set_value(regularGpio->lineHandle, logicLevel);
2021-02-14 12:57:09 +01:00
if (result < 0) {
2021-02-23 13:24:05 +01:00
sif::warning << "LinuxLibgpioIF::driveGpio: Failed to pull GPIO with ID " << gpioId <<
" to logic level " << logicLevel << std::endl;
2021-02-14 12:57:09 +01:00
return DRIVE_GPIO_FAILURE;
}
return RETURN_OK;
2021-02-12 21:23:35 +01:00
}
2021-01-23 17:22:40 +01:00
2021-02-12 21:23:35 +01:00
ReturnValue_t LinuxLibgpioIF::readGpio(gpioId_t gpioId, int* gpioState) {
2021-02-14 18:52:40 +01:00
gpioMapIter = gpioMap.find(gpioId);
2021-02-12 21:23:35 +01:00
if (gpioMapIter == gpioMap.end()){
2021-02-23 13:24:05 +01:00
sif::warning << "LinuxLibgpioIF::readGpio: Unknown GPIOD ID " << gpioId << std::endl;
return UNKNOWN_GPIO_ID;
}
if(gpioMapIter->second->gpioType == gpio::GpioTypes::GPIOD_REGULAR) {
GpiodRegular* regularGpio = dynamic_cast<GpiodRegular*>(gpioMapIter->second);
if(regularGpio == nullptr) {
return GPIO_TYPE_FAILURE;
}
*gpioState = gpiod_line_get_value(regularGpio->lineHandle);
}
else {
2021-02-12 21:23:35 +01:00
}
2021-01-23 17:22:40 +01:00
2021-02-12 21:23:35 +01:00
return RETURN_OK;
2021-01-23 17:22:40 +01:00
}
2021-02-14 18:21:59 +01:00
ReturnValue_t LinuxLibgpioIF::checkForConflicts(GpioMap& mapToAdd){
2021-02-23 13:24:05 +01:00
ReturnValue_t status = HasReturnvaluesIF::RETURN_OK;
ReturnValue_t result = HasReturnvaluesIF::RETURN_OK;
2021-02-14 18:21:59 +01:00
for(auto& gpioConfig: mapToAdd) {
2021-02-23 13:24:05 +01:00
switch(gpioConfig.second->gpioType) {
case(gpio::GpioTypes::GPIOD_REGULAR): {
auto regularGpio = dynamic_cast<GpiodRegular*>(gpioConfig.second);
if(regularGpio == nullptr) {
return GPIO_TYPE_FAILURE;
}
2021-02-23 16:47:34 +01:00
/* Check for conflicts and remove duplicates if necessary */
result = checkForConflictsRegularGpio(gpioConfig.first, regularGpio, mapToAdd);
if(result != HasReturnvaluesIF::RETURN_OK) {
status = result;
}
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 = checkForConflictsCallbackGpio(gpioConfig.first, callbackGpio, mapToAdd);
2021-02-23 13:24:05 +01:00
if(result != HasReturnvaluesIF::RETURN_OK) {
status = result;
}
break;
}
default: {
}
}
}
return status;
}
ReturnValue_t LinuxLibgpioIF::checkForConflictsRegularGpio(gpioId_t gpioIdToCheck,
2021-02-23 16:47:34 +01:00
GpiodRegular* gpioToCheck, GpioMap& mapToAdd) {
2021-02-23 13:24:05 +01:00
/* Cross check with private map */
gpioMapIter = gpioMap.find(gpioIdToCheck);
if(gpioMapIter != gpioMap.end()) {
2021-02-23 16:47:34 +01:00
if(gpioMapIter->second->gpioType != gpio::GpioTypes::GPIOD_REGULAR) {
sif::warning << "LinuxLibgpioIF::checkForConflicts: ID already exists for different "
"GPIO type" << gpioIdToCheck << ". Removing duplicate." << std::endl;
mapToAdd.erase(gpioIdToCheck);
return HasReturnvaluesIF::RETURN_OK;
}
auto ownRegularGpio = dynamic_cast<GpiodRegular*>(gpioMapIter->second);
if(ownRegularGpio == nullptr) {
return GPIO_TYPE_FAILURE;
}
/* An entry for this GPIO already exists. Check if configuration
* of direction is equivalent */
if (gpioToCheck->direction != ownRegularGpio->direction) {
sif::warning << "LinuxLibgpioIF::checkForConflicts: Detected conflict for GPIO " <<
gpioIdToCheck << std::endl;
2021-02-14 12:57:09 +01:00
}
2021-02-23 13:24:05 +01:00
/* Remove element from map to add because a entry for this GPIO
2021-02-23 16:47:34 +01:00
already exists */
sif::warning << "LinuxLibgpioIF::checkForConflictsRegularGpio: Duplicate GPIO definition"
<< " detected. Duplicate will be removed from map to add." << std::endl;
mapToAdd.erase(gpioIdToCheck);
}
return HasReturnvaluesIF::RETURN_OK;
}
2021-02-23 13:24:05 +01:00
2021-02-23 16:47:34 +01:00
ReturnValue_t LinuxLibgpioIF::checkForConflictsCallbackGpio(gpioId_t gpioIdToCheck,
GpioCallback *callbackGpio, GpioMap& mapToAdd) {
/* Cross check with private map */
gpioMapIter = gpioMap.find(gpioIdToCheck);
if(gpioMapIter != gpioMap.end()) {
if(gpioMapIter->second->gpioType != gpio::GpioTypes::CALLBACK) {
sif::warning << "LinuxLibgpioIF::checkForConflicts: ID already exists for different "
"GPIO type" << gpioIdToCheck << ". Removing duplicate." << std::endl;
mapToAdd.erase(gpioIdToCheck);
return HasReturnvaluesIF::RETURN_OK;
}
/* Remove element from map to add because a entry for this GPIO
already exists */
sif::warning << "LinuxLibgpioIF::checkForConflictsRegularGpio: Duplicate GPIO definition"
<< " detected. Duplicate will be removed from map to add." << std::endl;
mapToAdd.erase(gpioIdToCheck);
2021-02-14 12:57:09 +01:00
}
2021-02-23 13:24:05 +01:00
return HasReturnvaluesIF::RETURN_OK;
2021-01-23 17:22:40 +01:00
}