diff options
author | John Klug <john.klug@multitech.com> | 2023-09-14 10:27:07 -0500 |
---|---|---|
committer | John Klug <john.klug@multitech.com> | 2023-09-14 10:27:07 -0500 |
commit | 3ce7b597de0df365ce4d66b05b45e3701dd80501 (patch) | |
tree | 86359de52edaa405f2746c2159694c54266eddec /src/Device | |
parent | 37863013ab0b0b09a97f8cbffeab2a2f703f1b2c (diff) | |
download | mts-io-sysfs-3ce7b597de0df365ce4d66b05b45e3701dd80501.tar.gz mts-io-sysfs-3ce7b597de0df365ce4d66b05b45e3701dd80501.tar.bz2 mts-io-sysfs-3ce7b597de0df365ce4d66b05b45e3701dd80501.zip |
Add libgpiod feature to mts-io-sysfs
Diffstat (limited to 'src/Device')
-rw-r--r-- | src/Device/Device.cpp | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/Device/Device.cpp b/src/Device/Device.cpp index 1a2aca5..c99e252 100644 --- a/src/Device/Device.cpp +++ b/src/Device/Device.cpp @@ -15,6 +15,7 @@ * ***********************************************************************/ +#include <gpiod.h> #include "Device.h" const std::vector<std::string> Device::apIdentifiers = { @@ -494,8 +495,34 @@ void Device::printUsage(std::string program) { exitHandler(0); } +/* If the name starts in upper case, assume + * the name is a GPIO line name from gpio-hog + * in device tree. If it starts in lower case + * assume the name is an attribute of the mts-io + * driver. GPIOs can only be zero + * or one, never a text string. + */ void Device::show(std::string name) { std::string fileData; + char chipname[64]; + unsigned int offset; + int retval; + + if(isupper(name[0])) { + retval = gpiod_ctxless_find_line(name.c_str(), chipname, + sizeof chipname,&offset); + if(retval != -1) + retval = gpiod_ctxless_get_value(chipname, offset, + false, CMDNAME); + if(retval != -1) { + if(retval == 0) + printf("0\n"); + else + printf("1\n"); + exitHandler(0); + } + } + int32_t code = MTS::System::readFile(SYSFS_PLATFORM + name, fileData); if (code == 0) { printf("%s", fileData.c_str()); @@ -519,12 +546,38 @@ void Device::showTrigger(std::string name) { exitHandler(99); } +// Prefer gpiod if name starts with upper case and is value 0 or 1 void Device::store(std::string name, std::string value) { + int intvalue = -1; + int prefergpiod = 0; + char chipname[64]; + unsigned int offset; + int retval; + if (!isRoot) { printError("Must be root to store to %s", name.c_str()); exitHandler(99); } printDebug("Setting %s to %s", name.c_str(), value.c_str()); + if(isupper(name[0])) { + if (value.compare("0") == 0) + intvalue = 0; + if (value.compare("1") == 0) + intvalue = 1; + if ((intvalue != -1)) + prefergpiod = 1; + } + + if(prefergpiod) { + retval = gpiod_ctxless_find_line(name.c_str(), chipname, + sizeof chipname,&offset); + if(retval != -1) + retval = gpiod_ctxless_set_value(chipname, offset, intvalue, + false, CMDNAME, NULL, NULL); + if(retval != -1) + return; + } + std::ofstream fileToWrite(SYSFS_PLATFORM + name); if (fileToWrite.is_open()) { fileToWrite << value; |