diff options
| author | Patrick Venture <venture@google.com> | 2018-03-08 08:29:23 -0800 |
|---|---|---|
| committer | Patrick Venture <venture@google.com> | 2018-03-08 08:29:59 -0800 |
| commit | 863b9246dd43464da91ec789deaa42d15c45f84d (patch) | |
| tree | 3865e012d91ad15f5b16590e6cdb1138cb271a1f /sysfs | |
| parent | d80121817bb771b305f1db6aacc3ad71611a3e58 (diff) | |
| download | phosphor-pid-control-863b9246dd43464da91ec789deaa42d15c45f84d.tar.gz phosphor-pid-control-863b9246dd43464da91ec789deaa42d15c45f84d.zip | |
Sensor Objects
This includes all the sensor objects including a few
implementations, including dbus and sysfs sensors.
Change-Id: I9897c79f9fd463f00f0e02aeb6c32ffa89635dbe
Signed-off-by: Patrick Venture <venture@google.com>
Diffstat (limited to 'sysfs')
| -rw-r--r-- | sysfs/sysfsread.cpp | 39 | ||||
| -rw-r--r-- | sysfs/sysfsread.hpp | 25 | ||||
| -rw-r--r-- | sysfs/sysfswrite.cpp | 48 | ||||
| -rw-r--r-- | sysfs/sysfswrite.hpp | 39 | ||||
| -rw-r--r-- | sysfs/util.cpp | 71 | ||||
| -rw-r--r-- | sysfs/util.hpp | 6 |
6 files changed, 228 insertions, 0 deletions
diff --git a/sysfs/sysfsread.cpp b/sysfs/sysfsread.cpp new file mode 100644 index 0000000..284aa55 --- /dev/null +++ b/sysfs/sysfsread.cpp @@ -0,0 +1,39 @@ +/** + * Copyright 2017 Google Inc. + * + * 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 <chrono> +#include <fstream> +#include <iostream> + +#include "sysfs/sysfsread.hpp" + + +ReadReturn SysFsRead::read(void) +{ + int64_t value; + std::ifstream ifs; + + ifs.open(_path); + ifs >> value; + ifs.close(); + + struct ReadReturn r = { + static_cast<double>(value), + std::chrono::high_resolution_clock::now() + }; + + return r; +} diff --git a/sysfs/sysfsread.hpp b/sysfs/sysfsread.hpp new file mode 100644 index 0000000..0dbc71b --- /dev/null +++ b/sysfs/sysfsread.hpp @@ -0,0 +1,25 @@ +#pragma once + +#include <string> + +#include "interfaces.hpp" +#include "sysfs/util.hpp" + + +/* + * A ReadInterface that is expecting a path that's sysfs, but really could be + * any filesystem path. + */ +class SysFsRead : public ReadInterface +{ + public: + SysFsRead(const std::string& path) + : ReadInterface(), + _path(FixupPath(path)) + { } + + ReadReturn read(void) override; + + private: + const std::string _path; +}; diff --git a/sysfs/sysfswrite.cpp b/sysfs/sysfswrite.cpp new file mode 100644 index 0000000..c3e1b03 --- /dev/null +++ b/sysfs/sysfswrite.cpp @@ -0,0 +1,48 @@ +/** + * Copyright 2017 Google Inc. + * + * 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 <fstream> +#include <iostream> + +#include "sysfswrite.hpp" + + +void SysFsWritePercent::write(double value) +{ + float minimum = getMin(); + float maximum = getMax(); + + float range = maximum - minimum; + float offset = range * value; + float ovalue = offset + minimum; + + std::ofstream ofs; + ofs.open(_writepath); + ofs << static_cast<int64_t>(ovalue); + ofs.close(); + + return; +} + +void SysFsWrite::write(double value) +{ + std::ofstream ofs; + ofs.open(_writepath); + ofs << static_cast<int64_t>(value); + ofs.close(); + + return; +} diff --git a/sysfs/sysfswrite.hpp b/sysfs/sysfswrite.hpp new file mode 100644 index 0000000..9f7083c --- /dev/null +++ b/sysfs/sysfswrite.hpp @@ -0,0 +1,39 @@ +#pragma once + +#include <string> + +#include "interfaces.hpp" +#include "sysfs/util.hpp" + + +/* + * A WriteInterface that is expecting a path that's sysfs, but really could be + * any filesystem path. + */ +class SysFsWritePercent : public WriteInterface +{ + public: + SysFsWritePercent(std::string& writepath, int64_t min, int64_t max) + : WriteInterface(min, max), + _writepath(FixupPath(writepath)) + { } + + void write(double value) override; + + private: + std::string _writepath; +}; + +class SysFsWrite : public WriteInterface +{ + public: + SysFsWrite(std::string& writepath, int64_t min, int64_t max) + : WriteInterface(min, max), + _writepath(FixupPath(writepath)) + { } + + void write(double value) override; + + private: + std::string _writepath; +}; diff --git a/sysfs/util.cpp b/sysfs/util.cpp new file mode 100644 index 0000000..c297412 --- /dev/null +++ b/sysfs/util.cpp @@ -0,0 +1,71 @@ +/** + * Copyright 2017 Google Inc. + * + * 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 <experimental/filesystem> +#include <iostream> +#include <string> + + +#include "sysfs/util.hpp" + +/* + * There are two basic paths I want to support: + * 1. /sys/class/hwmon/hwmon0/pwm1 + * 2. /sys/devices/platform/ahb/1e786000.pwm-tacho-controller/hwmon/<asterisk asterisk>/pwm1 + * + * In this latter case, I want to fill in that gap. Assuming because it's this + * path that it'll only have one directory there. + */ + +static constexpr auto platform = "/sys/devices/platform/"; +namespace fs = std::experimental::filesystem; + + +std::string FixupPath(std::string original) +{ + std::string::size_type n, x; + + /* TODO: Consider the merits of using regex for this. */ + n = original.find("**"); + x = original.find(platform); + + if ((n != std::string::npos) && (x != std::string::npos)) + { + /* This path has some missing pieces and we support it. */ + std::string base = original.substr(0, n); + std::string fldr; + std::string f = original.substr(n + 2, original.size() - (n + 2)); + + /* Equivalent to glob and grab 0th entry. */ + for (const auto& folder : fs::directory_iterator(base)) + { + fldr = folder.path(); + break; + } + + if (!fldr.length()) + { + return original; + } + + return fldr + f; + } + else + { + /* It'll throw an exception when we use it if it's still bad. */ + return original; + } +} diff --git a/sysfs/util.hpp b/sysfs/util.hpp new file mode 100644 index 0000000..2d4c274 --- /dev/null +++ b/sysfs/util.hpp @@ -0,0 +1,6 @@ +#include <string> + +/* + * Given a path that optionally has a glob portion, fill it out. + */ +std::string FixupPath(std::string original); |

