diff options
author | Vishwanatha Subbanna <vishwa@linux.vnet.ibm.com> | 2017-04-14 18:12:14 +0530 |
---|---|---|
committer | Vishwanatha Subbanna <vishwa@linux.vnet.ibm.com> | 2017-04-26 16:15:07 +0530 |
commit | 38b08d791b159424c9c9f9a21876f73f6020062b (patch) | |
tree | 95b9007eff763921cdcd3e66436ffddd80a74516 | |
parent | afd21a650715bf7a0ecc9b71a93d60639706d980 (diff) | |
download | openpower-occ-control-38b08d791b159424c9c9f9a21876f73f6020062b.tar.gz openpower-occ-control-38b08d791b159424c9c9f9a21876f73f6020062b.zip |
Open device and populate file descriptor
Change-Id: I8159f71ff3dd087f10cdf3b013e1acb60d4758fb
Signed-off-by: Vishwanatha Subbanna <vishwa@linux.vnet.ibm.com>
-rw-r--r-- | file.hpp | 51 | ||||
-rw-r--r-- | occ_pass_through.cpp | 34 | ||||
-rw-r--r-- | occ_pass_through.hpp | 12 |
3 files changed, 77 insertions, 20 deletions
diff --git a/file.hpp b/file.hpp new file mode 100644 index 0000000..4d0a83c --- /dev/null +++ b/file.hpp @@ -0,0 +1,51 @@ +#pragma once + +#include <unistd.h> +namespace open_power +{ +namespace occ +{ +namespace pass_through +{ +/** @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 pass_through +} // namespace occ +} // namespace open-power diff --git a/occ_pass_through.cpp b/occ_pass_through.cpp index 05864c1..88a0d29 100644 --- a/occ_pass_through.cpp +++ b/occ_pass_through.cpp @@ -1,5 +1,6 @@ #include <memory> #include <algorithm> +#include <fcntl.h> #include <phosphor-logging/log.hpp> #include "occ_pass_through.hpp" #include "occ_finder.hpp" @@ -38,32 +39,29 @@ PassThrough::PassThrough( sdbusplus::bus::bus& bus, const char* path) : Iface(bus, path), - path(path) + path(path), + fd(openDevice()) { - devicePath.append(std::to_string((this->path.back() - '0') + 1)); + // Nothing to do. } -std::vector<int32_t> PassThrough::send(std::vector<int32_t> command) +int PassThrough::openDevice() { - std::string msg = "Pass through to OCC "; - msg += path; + // Device instance number starts from 1. + devicePath.append(std::to_string((this->path.back() - '0') + 1)); - std::string cmd; - std::for_each(command.cbegin(), command.cend(), - [&cmd](const auto& c) - { - cmd += std::to_string(c); - cmd += ','; - }); - if (!cmd.empty()) + int fd = open(devicePath.c_str(), O_RDWR | O_NONBLOCK); + if (fd < 0) { - // Remove trailing ',' - cmd.pop_back(); + // This is for completion. This is getting replaced by elog + // in the next commit + throw std::runtime_error("Error opening " + devicePath); } + return fd; +} - using namespace phosphor::logging; - log<level::INFO>(msg.c_str(), entry("COMMAND=%s", cmd.c_str())); - +std::vector<int32_t> PassThrough::send(std::vector<int32_t> command) +{ return {}; } diff --git a/occ_pass_through.hpp b/occ_pass_through.hpp index da8c617..673b68e 100644 --- a/occ_pass_through.hpp +++ b/occ_pass_through.hpp @@ -2,10 +2,12 @@ #include <string> #include <vector> +#include <unistd.h> #include <sdbusplus/bus.hpp> #include <sdbusplus/server/object.hpp> #include "org/open_power/OCC/PassThrough/server.hpp" #include "config.h" +#include "file.hpp" namespace open_power { @@ -39,11 +41,11 @@ class PassThrough : public Iface { public: PassThrough() = delete; + ~PassThrough() = default; PassThrough(const PassThrough&) = delete; PassThrough& operator=(const PassThrough&) = delete; PassThrough(PassThrough&&) = default; PassThrough& operator=(PassThrough&&) = default; - ~PassThrough() = default; /** @brief Ctor to put pass-through d-bus object on the bus * @param[in] bus - Bus to attach to @@ -65,12 +67,18 @@ class PassThrough : public Iface /** @brief OCC device path * For now, here is the hard-coded mapping until - * the udev rule is in + * the udev rule is in. * occ0 --> /dev/occfifo1 * occ1 --> /dev/occfifo2 * ... */ std::string devicePath = "/dev/occfifo"; + + /** @brief File descriptor manager */ + FileDescriptor fd; + + /** Opens devicePath and returns file descritor */ + int openDevice(); }; } // namespace pass_through |