Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2016 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #include <cstdlib> |
Brad Bishop | 08379a3 | 2017-03-06 21:28:46 -0500 | [diff] [blame] | 17 | #include <experimental/filesystem> |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 18 | #include <memory> |
Matthew Barth | 048ac87 | 2017-03-09 14:36:08 -0600 | [diff] [blame] | 19 | #include <phosphor-logging/elog.hpp> |
| 20 | #include <phosphor-logging/elog-errors.hpp> |
| 21 | #include <xyz/openbmc_project/Control/Device/error.hpp> |
Matthew Barth | 4e1f30f | 2017-03-21 16:13:27 -0500 | [diff] [blame] | 22 | #include <xyz/openbmc_project/Sensor/Device/error.hpp> |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 23 | #include "sysfs.hpp" |
| 24 | #include "util.hpp" |
Brandon Wyman | 4eb9858 | 2017-05-24 14:24:00 -0500 | [diff] [blame^] | 25 | #include <fstream> |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 26 | |
Matthew Barth | 048ac87 | 2017-03-09 14:36:08 -0600 | [diff] [blame] | 27 | using namespace phosphor::logging; |
| 28 | |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 29 | std::string findHwmon(const std::string& ofNode) |
| 30 | { |
Brad Bishop | 08379a3 | 2017-03-06 21:28:46 -0500 | [diff] [blame] | 31 | namespace fs = std::experimental::filesystem; |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 32 | static constexpr auto hwmonRoot = "/sys/class/hwmon"; |
Brad Bishop | 08379a3 | 2017-03-06 21:28:46 -0500 | [diff] [blame] | 33 | static constexpr auto ofRoot = "/sys/firmware/devicetree/base"; |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 34 | |
Brad Bishop | 08379a3 | 2017-03-06 21:28:46 -0500 | [diff] [blame] | 35 | fs::path fullOfPath{ofRoot}; |
| 36 | fullOfPath /= ofNode; |
Brandon Wyman | 4eb9858 | 2017-05-24 14:24:00 -0500 | [diff] [blame^] | 37 | fs::path fullOfPathPhandle{fullOfPath}; |
| 38 | fullOfPathPhandle /= "phandle"; |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 39 | |
Brad Bishop | 08379a3 | 2017-03-06 21:28:46 -0500 | [diff] [blame] | 40 | for (const auto& hwmonInst : fs::directory_iterator(hwmonRoot)) |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 41 | { |
Brad Bishop | 08379a3 | 2017-03-06 21:28:46 -0500 | [diff] [blame] | 42 | auto path = hwmonInst.path(); |
| 43 | path /= "of_node"; |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 44 | |
Brad Bishop | 08379a3 | 2017-03-06 21:28:46 -0500 | [diff] [blame] | 45 | if (fs::canonical(path) != fullOfPath) |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 46 | { |
Brandon Wyman | 4eb9858 | 2017-05-24 14:24:00 -0500 | [diff] [blame^] | 47 | // Try to find HWMON instance via phandle values. |
| 48 | // Used for IIO device drivers. |
| 49 | path /= "io-channels"; |
| 50 | if (fs::exists(path) && fs::exists(fullOfPathPhandle)) |
| 51 | { |
| 52 | std::ifstream ioChannelsFile(path); |
| 53 | std::ifstream pHandleFile(fullOfPathPhandle); |
| 54 | |
| 55 | uint32_t ioChannelsValue; |
| 56 | uint32_t pHandleValue; |
| 57 | |
| 58 | try |
| 59 | { |
| 60 | ioChannelsFile.read(reinterpret_cast<char*>(&ioChannelsValue), |
| 61 | sizeof(ioChannelsValue)); |
| 62 | pHandleFile.read(reinterpret_cast<char*>(&pHandleValue), |
| 63 | sizeof(pHandleValue)); |
| 64 | |
| 65 | if (ioChannelsValue == pHandleValue) |
| 66 | { |
| 67 | return hwmonInst.path(); |
| 68 | } |
| 69 | } |
| 70 | catch (const std::exception& e) |
| 71 | { |
| 72 | log<level::INFO>(e.what()); |
| 73 | } |
| 74 | |
| 75 | |
| 76 | } |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 77 | continue; |
| 78 | } |
| 79 | |
Brad Bishop | 08379a3 | 2017-03-06 21:28:46 -0500 | [diff] [blame] | 80 | return hwmonInst.path(); |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | return std::string(); |
| 84 | } |
| 85 | |
Brad Bishop | 4db6442 | 2017-02-16 11:33:32 -0500 | [diff] [blame] | 86 | int readSysfsWithCallout(const std::string& root, |
| 87 | const std::string& instance, |
| 88 | const std::string& type, |
| 89 | const std::string& id, |
| 90 | const std::string& sensor) |
| 91 | { |
Brad Bishop | 5ec68ab | 2017-03-27 13:41:02 -0400 | [diff] [blame] | 92 | namespace fs = std::experimental::filesystem; |
| 93 | |
Brad Bishop | 4db6442 | 2017-02-16 11:33:32 -0500 | [diff] [blame] | 94 | int value = 0; |
Brad Bishop | 5ec68ab | 2017-03-27 13:41:02 -0400 | [diff] [blame] | 95 | std::ifstream ifs; |
| 96 | fs::path instancePath{root}; |
| 97 | instancePath /= instance; |
Brad Bishop | 4db6442 | 2017-02-16 11:33:32 -0500 | [diff] [blame] | 98 | std::string fullPath = make_sysfs_path(instancePath, |
| 99 | type, id, sensor); |
Brad Bishop | 4db6442 | 2017-02-16 11:33:32 -0500 | [diff] [blame] | 100 | |
| 101 | ifs.exceptions(std::ifstream::failbit |
| 102 | | std::ifstream::badbit |
| 103 | | std::ifstream::eofbit); |
| 104 | try |
| 105 | { |
| 106 | ifs.open(fullPath); |
| 107 | ifs >> value; |
| 108 | } |
| 109 | catch (const std::exception& e) |
| 110 | { |
| 111 | // Too many GCC bugs (53984, 66145) to do |
| 112 | // this the right way... |
Brad Bishop | 4db6442 | 2017-02-16 11:33:32 -0500 | [diff] [blame] | 113 | |
| 114 | // errno should still reflect the error from the failing open |
| 115 | // or read system calls that got us here. |
| 116 | auto rc = errno; |
Brad Bishop | 5ec68ab | 2017-03-27 13:41:02 -0400 | [diff] [blame] | 117 | instancePath /= "device"; |
Matthew Barth | 4e1f30f | 2017-03-21 16:13:27 -0500 | [diff] [blame] | 118 | using namespace sdbusplus::xyz::openbmc_project::Sensor::Device::Error; |
Marri Devender Rao | 05711eb | 2017-04-15 06:47:11 -0500 | [diff] [blame] | 119 | report<ReadFailure>( |
| 120 | xyz::openbmc_project::Sensor::Device:: |
| 121 | ReadFailure::CALLOUT_ERRNO(rc), |
| 122 | xyz::openbmc_project::Sensor::Device:: |
| 123 | ReadFailure::CALLOUT_DEVICE_PATH( |
| 124 | fs::canonical(instancePath).c_str())); |
| 125 | |
Brad Bishop | 4db6442 | 2017-02-16 11:33:32 -0500 | [diff] [blame] | 126 | exit(EXIT_FAILURE); |
| 127 | } |
| 128 | |
| 129 | return value; |
| 130 | } |
| 131 | |
Matthew Barth | 048ac87 | 2017-03-09 14:36:08 -0600 | [diff] [blame] | 132 | uint64_t writeSysfsWithCallout(const uint64_t& value, |
| 133 | const std::string& root, |
| 134 | const std::string& instance, |
| 135 | const std::string& type, |
| 136 | const std::string& id, |
| 137 | const std::string& sensor) |
| 138 | { |
| 139 | namespace fs = std::experimental::filesystem; |
| 140 | |
| 141 | std::string valueStr = std::to_string(value); |
| 142 | std::ofstream ofs; |
| 143 | fs::path instancePath{root}; |
| 144 | instancePath /= instance; |
| 145 | std::string fullPath = make_sysfs_path(instancePath, |
| 146 | type, id, sensor); |
| 147 | |
| 148 | ofs.exceptions(std::ofstream::failbit |
| 149 | | std::ofstream::badbit |
| 150 | | std::ofstream::eofbit); |
| 151 | try |
| 152 | { |
| 153 | ofs.open(fullPath); |
| 154 | ofs << valueStr; |
| 155 | } |
| 156 | catch (const std::exception& e) |
| 157 | { |
| 158 | // errno should still reflect the error from the failing open |
| 159 | // or write system calls that got us here. |
| 160 | auto rc = errno; |
| 161 | instancePath /= "device"; |
| 162 | using namespace sdbusplus::xyz::openbmc_project::Control::Device::Error; |
Marri Devender Rao | 05711eb | 2017-04-15 06:47:11 -0500 | [diff] [blame] | 163 | report<WriteFailure>( |
| 164 | xyz::openbmc_project::Control::Device:: |
| 165 | WriteFailure::CALLOUT_ERRNO(rc), |
| 166 | xyz::openbmc_project::Control::Device:: |
| 167 | WriteFailure::CALLOUT_DEVICE_PATH( |
| 168 | fs::canonical(instancePath).c_str())); |
| 169 | |
Matthew Barth | 048ac87 | 2017-03-09 14:36:08 -0600 | [diff] [blame] | 170 | exit(EXIT_FAILURE); |
| 171 | } |
| 172 | |
| 173 | return value; |
| 174 | } |
| 175 | |
Brad Bishop | 613a5b3 | 2017-01-05 20:58:13 -0500 | [diff] [blame] | 176 | // vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |