summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVishwanatha Subbanna <vishwa@linux.vnet.ibm.com>2017-04-04 14:05:09 +0530
committerVishwanatha Subbanna <vishwa@linux.vnet.ibm.com>2017-04-26 12:02:26 +0530
commit4902a10c9fabcdf5e1f2868f28cba2d571d55f03 (patch)
tree9f0662ade186a796fec8bb9d69338c5e55f19b79
parentaffea8bfc77162d8631b48f1eb5e79c06b96302f (diff)
downloadphosphor-gpio-monitor-4902a10c9fabcdf5e1f2868f28cba2d571d55f03.tar.gz
phosphor-gpio-monitor-4902a10c9fabcdf5e1f2868f28cba2d571d55f03.zip
Populate the file descriptor for the device
On a GPIO chip, there could be many gpio lines and for each line, there would be a corresponding input device event file. To know the assertion state of a GPIO line, a descriptor is needed. This descriptor will later be plugged into sd_event so that the GPIO state changes can be caught and handled. Change-Id: Idc8c2b429688fea2a5124b96677085b1be48128b Signed-off-by: Vishwanatha Subbanna <vishwa@linux.vnet.ibm.com>
-rw-r--r--Makefile.am6
-rw-r--r--configure.ac3
-rw-r--r--file.hpp48
-rw-r--r--mainapp.cpp7
-rw-r--r--monitor.cpp41
-rw-r--r--monitor.hpp66
6 files changed, 169 insertions, 2 deletions
diff --git a/Makefile.am b/Makefile.am
index aa81c04..3fe7742 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,5 +1,11 @@
sbin_PROGRAMS = phosphor-gpio-monitor
+noinst_HEADERS = monitor.hpp
+
phosphor_gpio_monitor_SOURCES = \
argument.cpp \
+ monitor.cpp \
mainapp.cpp
+
+phosphor_gpio_monitor_LDFLAGS = $(PHOSPHOR_LOGGING_LIBS)
+phosphor_gpio_monitor_CFLAGS = $(PHOSPHOR_LOGGING_CFLAGS)
diff --git a/configure.ac b/configure.ac
index d7b4f75..40100c7 100644
--- a/configure.ac
+++ b/configure.ac
@@ -18,6 +18,9 @@ AX_APPEND_COMPILE_FLAGS([-Wall -Werror], [CXXFLAGS])
# For linking
LT_INIT
+# Checks for modules
+PKG_CHECK_MODULES([PHOSPHOR_LOGGING], [phosphor-logging],, [AC_MSG_ERROR([Could not find phosphor-logging...openbmc/phosphor-logging package required])])
+
# Create configured output
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
diff --git a/file.hpp b/file.hpp
new file mode 100644
index 0000000..9ba4cbb
--- /dev/null
+++ b/file.hpp
@@ -0,0 +1,48 @@
+#pragma once
+
+#include <unistd.h>
+namespace phosphor
+{
+namespace gpio
+{
+/** @class FileDescriptor
+ * @brief Responsible for handling file descriptor
+ */
+class FileDescriptor
+{
+ private:
+ /** @brief File descriptor for the gpio input device */
+ int fd = -1;
+
+ public:
+ FileDescriptor() = delete;
+ FileDescriptor(const FileDescriptor&) = delete;
+ FileDescriptor& operator=(const FileDescriptor&) = delete;
+ FileDescriptor(FileDescriptor&&) = delete;
+ FileDescriptor& operator=(FileDescriptor&&) = delete;
+
+ /** @brief Saves File descriptor and uses it to do file operation
+ *
+ * @param[in] fd - File descriptor
+ */
+ FileDescriptor(int fd) : fd(fd)
+ {
+ // Nothing
+ }
+
+ ~FileDescriptor()
+ {
+ if (fd >=0)
+ {
+ close(fd);
+ }
+ }
+
+ int operator()()
+ {
+ return fd;
+ }
+};
+
+} // namespace gpio
+} // namespace phosphor
diff --git a/mainapp.cpp b/mainapp.cpp
index 671b73c..18bc4ca 100644
--- a/mainapp.cpp
+++ b/mainapp.cpp
@@ -17,6 +17,7 @@
#include <iostream>
#include <string>
#include "argument.hpp"
+#include "monitor.hpp"
static void exitWithError(const char* err, char** argv)
{
@@ -44,7 +45,6 @@ int main(int argc, char** argv)
{
exitWithError("Key not specified.", argv);
}
- // TODO : Convert key to integer
// Parse out assertion polarity interested in
// Its either 1 or 0 for press / release
@@ -53,12 +53,15 @@ int main(int argc, char** argv)
{
exitWithError("Polarity not specified.", argv);
}
- // TODO : Convert polarity to integer
// Parse out target argument. It is fine if the caller does not
// pass this if they are not interested in calling into any target
// on meeting a condition.
auto target = (options)["target"];
+ // Create a GPIO monitor object and let it do all the rest
+ phosphor::gpio::Monitor monitor(path, std::stoi(key),
+ std::stoi(polarity),target);
+
return 0;
}
diff --git a/monitor.cpp b/monitor.cpp
new file mode 100644
index 0000000..29d9d6d
--- /dev/null
+++ b/monitor.cpp
@@ -0,0 +1,41 @@
+/**
+ * Copyright © 2016 IBM Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <fcntl.h>
+#include <phosphor-logging/log.hpp>
+#include "monitor.hpp"
+namespace phosphor
+{
+namespace gpio
+{
+
+// Populate the file descriptor for passed in device
+int Monitor::openDevice()
+{
+ using namespace phosphor::logging;
+
+ int fd = open(path.c_str(), O_RDONLY | O_NONBLOCK);
+ if (fd < 0)
+ {
+ log<level::ERR>("Failed to open device",
+ entry("PATH=%s", path.c_str()));
+ throw std::runtime_error("Failed to open device");
+ }
+ return fd;
+}
+
+} // namespace gpio
+} // namespace phosphor
diff --git a/monitor.hpp b/monitor.hpp
new file mode 100644
index 0000000..9e3992d
--- /dev/null
+++ b/monitor.hpp
@@ -0,0 +1,66 @@
+#pragma once
+
+#include <unistd.h>
+#include <string>
+#include <linux/input.h>
+#include "file.hpp"
+namespace phosphor
+{
+namespace gpio
+{
+/** @class Monitor
+ * @brief Responsible for catching GPIO state change
+ * condition and taking actions
+ */
+class Monitor
+{
+ public:
+ Monitor() = delete;
+ Monitor(const Monitor&) = delete;
+ Monitor& operator=(const Monitor&) = delete;
+ Monitor(Monitor&&) = delete;
+ Monitor& operator=(Monitor&&) = delete;
+
+ /** @brief Constructs Monitor object.
+ *
+ * @param[in] path - Path to gpio input device
+ * @param[in] key - GPIO key to monitor
+ * @param[in] polarity - GPIO assertion polarity to look for
+ * @param[in] target - systemd unit to be started on GPIO
+ * value change
+ */
+ Monitor(const std::string& path,
+ decltype(input_event::code) key,
+ decltype(input_event::value) polarity,
+ const std::string& target)
+ : path(path),
+ key(key),
+ polarity(polarity),
+ target(target),
+ fd(openDevice())
+ {
+ // Nothing
+ }
+
+ private:
+ /** @brief Absolute path of GPIO input device */
+ const std::string& path;
+
+ /** @brief GPIO key code that is of interest */
+ decltype(input_event::code) key;
+
+ /** @brief GPIO key value that is of interest */
+ decltype(input_event::value) polarity;
+
+ /** @brief Systemd unit to be started when the condition is met */
+ const std::string& target;
+
+ /** @brief Manages File descriptor */
+ FileDescriptor fd;
+
+ /** @brief Opens the device and populates the descriptor */
+ int openDevice();
+};
+
+} // namespace gpio
+} // namespace phosphor
OpenPOWER on IntegriCloud