Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2019 Intel 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 | */ |
Andrew Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame] | 16 | |
| 17 | #include "MCUTempSensor.hpp" |
| 18 | |
| 19 | #include "Utils.hpp" |
| 20 | #include "VariantVisitors.hpp" |
| 21 | |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 22 | #include <boost/container/flat_map.hpp> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 23 | #include <sdbusplus/asio/connection.hpp> |
| 24 | #include <sdbusplus/asio/object_server.hpp> |
| 25 | #include <sdbusplus/bus/match.hpp> |
| 26 | |
Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 27 | #include <chrono> |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 28 | #include <cmath> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 29 | #include <functional> |
Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 30 | #include <iostream> |
| 31 | #include <limits> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 32 | #include <memory> |
Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 33 | #include <numeric> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 34 | #include <string> |
Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 35 | #include <vector> |
| 36 | |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 37 | extern "C" |
| 38 | { |
Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 39 | #include <i2c/smbus.h> |
| 40 | #include <linux/i2c-dev.h> |
| 41 | } |
| 42 | |
| 43 | constexpr const bool debug = false; |
| 44 | |
Zev Weiss | 054aad8 | 2022-08-18 01:37:34 -0700 | [diff] [blame] | 45 | constexpr const char* sensorType = "MCUTempSensor"; |
Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 46 | static constexpr double mcuTempMaxReading = 0xFF; |
| 47 | static constexpr double mcuTempMinReading = 0; |
| 48 | |
| 49 | boost::container::flat_map<std::string, std::unique_ptr<MCUTempSensor>> sensors; |
| 50 | |
| 51 | MCUTempSensor::MCUTempSensor(std::shared_ptr<sdbusplus::asio::connection>& conn, |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 52 | boost::asio::io_context& io, |
Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 53 | const std::string& sensorName, |
| 54 | const std::string& sensorConfiguration, |
| 55 | sdbusplus::asio::object_server& objectServer, |
| 56 | std::vector<thresholds::Threshold>&& thresholdData, |
| 57 | uint8_t busId, uint8_t mcuAddress, |
| 58 | uint8_t tempReg) : |
Zhikui Ren | da98f09 | 2021-11-01 09:41:08 -0700 | [diff] [blame] | 59 | Sensor(escapeName(sensorName), std::move(thresholdData), |
Zev Weiss | 054aad8 | 2022-08-18 01:37:34 -0700 | [diff] [blame] | 60 | sensorConfiguration, "MCUTempSensor", false, false, |
Zev Weiss | 34d7b97 | 2022-08-17 19:38:59 -0700 | [diff] [blame] | 61 | mcuTempMaxReading, mcuTempMinReading, conn), |
Brad Bishop | fbb44ad | 2019-11-08 09:42:37 -0500 | [diff] [blame] | 62 | busId(busId), mcuAddress(mcuAddress), tempReg(tempReg), |
James Feist | e333852 | 2020-09-15 15:40:30 -0700 | [diff] [blame] | 63 | objectServer(objectServer), waitTimer(io) |
Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 64 | { |
| 65 | sensorInterface = objectServer.add_interface( |
| 66 | "/xyz/openbmc_project/sensors/temperature/" + name, |
| 67 | "xyz.openbmc_project.Sensor.Value"); |
| 68 | |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 69 | for (const auto& threshold : thresholds) |
Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 70 | { |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 71 | std::string interface = thresholds::getInterface(threshold.level); |
| 72 | thresholdInterfaces[static_cast<size_t>(threshold.level)] = |
| 73 | objectServer.add_interface( |
| 74 | "/xyz/openbmc_project/sensors/temperature/" + name, interface); |
Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 75 | } |
| 76 | association = objectServer.add_interface( |
| 77 | "/xyz/openbmc_project/sensors/temperature/" + name, |
James Feist | 2adc95c | 2019-09-30 14:55:28 -0700 | [diff] [blame] | 78 | association::interface); |
Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | MCUTempSensor::~MCUTempSensor() |
| 82 | { |
| 83 | waitTimer.cancel(); |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 84 | for (const auto& iface : thresholdInterfaces) |
| 85 | { |
| 86 | objectServer.remove_interface(iface); |
| 87 | } |
Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 88 | objectServer.remove_interface(sensorInterface); |
| 89 | objectServer.remove_interface(association); |
| 90 | } |
| 91 | |
| 92 | void MCUTempSensor::init(void) |
| 93 | { |
Andrei Kartashev | 3928741 | 2022-02-04 16:04:47 +0300 | [diff] [blame] | 94 | setInitialProperties(sensor_paths::unitDegreesC); |
Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 95 | read(); |
| 96 | } |
| 97 | |
| 98 | void MCUTempSensor::checkThresholds(void) |
| 99 | { |
| 100 | thresholds::checkThresholds(this); |
| 101 | } |
| 102 | |
Saitwal, Meghan | f1169f7 | 2023-04-23 05:14:14 +0000 | [diff] [blame^] | 103 | int MCUTempSensor::getMCURegsInfoWord(uint8_t regs, int32_t* pu32data) const |
Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 104 | { |
| 105 | std::string i2cBus = "/dev/i2c-" + std::to_string(busId); |
Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 106 | |
Ed Tanous | 99c4409 | 2022-01-14 09:59:09 -0800 | [diff] [blame] | 107 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) |
| 108 | int fd = open(i2cBus.c_str(), O_RDWR); |
Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 109 | if (fd < 0) |
| 110 | { |
| 111 | std::cerr << " unable to open i2c device" << i2cBus << " err=" << fd |
| 112 | << "\n"; |
| 113 | return -1; |
| 114 | } |
| 115 | |
Ed Tanous | 99c4409 | 2022-01-14 09:59:09 -0800 | [diff] [blame] | 116 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) |
Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 117 | if (ioctl(fd, I2C_SLAVE_FORCE, mcuAddress) < 0) |
| 118 | { |
| 119 | std::cerr << " unable to set device address\n"; |
| 120 | close(fd); |
| 121 | return -1; |
| 122 | } |
| 123 | |
| 124 | unsigned long funcs = 0; |
Ed Tanous | 99c4409 | 2022-01-14 09:59:09 -0800 | [diff] [blame] | 125 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg) |
Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 126 | if (ioctl(fd, I2C_FUNCS, &funcs) < 0) |
| 127 | { |
| 128 | std::cerr << " not support I2C_FUNCS\n"; |
| 129 | close(fd); |
| 130 | return -1; |
| 131 | } |
| 132 | |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 133 | if ((funcs & I2C_FUNC_SMBUS_READ_WORD_DATA) == 0U) |
Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 134 | { |
| 135 | std::cerr << " not support I2C_FUNC_SMBUS_READ_WORD_DATA\n"; |
| 136 | close(fd); |
| 137 | return -1; |
| 138 | } |
| 139 | |
Saitwal, Meghan | f1169f7 | 2023-04-23 05:14:14 +0000 | [diff] [blame^] | 140 | *pu32data = i2c_smbus_read_word_data(fd, regs); |
Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 141 | close(fd); |
| 142 | |
Saitwal, Meghan | f1169f7 | 2023-04-23 05:14:14 +0000 | [diff] [blame^] | 143 | if (*pu32data < 0) |
Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 144 | { |
| 145 | std::cerr << " read word data failed at " << static_cast<int>(regs) |
| 146 | << "\n"; |
| 147 | return -1; |
| 148 | } |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | void MCUTempSensor::read(void) |
| 154 | { |
| 155 | static constexpr size_t pollTime = 1; // in seconds |
| 156 | |
Ed Tanous | 83db50c | 2023-03-01 10:20:24 -0800 | [diff] [blame] | 157 | waitTimer.expires_after(std::chrono::seconds(pollTime)); |
Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 158 | waitTimer.async_wait([this](const boost::system::error_code& ec) { |
| 159 | if (ec == boost::asio::error::operation_aborted) |
| 160 | { |
| 161 | return; // we're being cancelled |
| 162 | } |
| 163 | // read timer error |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 164 | if (ec) |
Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 165 | { |
| 166 | std::cerr << "timer error\n"; |
| 167 | return; |
| 168 | } |
Saitwal, Meghan | f1169f7 | 2023-04-23 05:14:14 +0000 | [diff] [blame^] | 169 | int32_t temp = 0; |
Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 170 | int ret = getMCURegsInfoWord(tempReg, &temp); |
| 171 | if (ret >= 0) |
| 172 | { |
| 173 | double v = static_cast<double>(temp) / 1000; |
| 174 | if constexpr (debug) |
| 175 | { |
James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 176 | std::cerr << "Value update to " << v << "raw reading " |
Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 177 | << static_cast<int>(temp) << "\n"; |
| 178 | } |
| 179 | updateValue(v); |
| 180 | } |
| 181 | else |
| 182 | { |
| 183 | std::cerr << "Invalid read getMCURegsInfoWord\n"; |
James Feist | 961bf09 | 2020-07-01 16:38:12 -0700 | [diff] [blame] | 184 | incrementError(); |
Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 185 | } |
| 186 | read(); |
| 187 | }); |
| 188 | } |
| 189 | |
| 190 | void createSensors( |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 191 | boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer, |
Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 192 | boost::container::flat_map<std::string, std::unique_ptr<MCUTempSensor>>& |
| 193 | sensors, |
| 194 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection) |
| 195 | { |
| 196 | if (!dbusConnection) |
| 197 | { |
| 198 | std::cerr << "Connection not created\n"; |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | dbusConnection->async_method_call( |
| 203 | [&io, &objectServer, &dbusConnection, &sensors]( |
| 204 | boost::system::error_code ec, const ManagedObjectType& resp) { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 205 | if (ec) |
| 206 | { |
| 207 | std::cerr << "Error contacting entity manager\n"; |
| 208 | return; |
| 209 | } |
Zev Weiss | d7c1f76 | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 210 | for (const auto& [path, interfaces] : resp) |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 211 | { |
Zev Weiss | d7c1f76 | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 212 | for (const auto& [intf, cfg] : interfaces) |
Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 213 | { |
Zev Weiss | 054aad8 | 2022-08-18 01:37:34 -0700 | [diff] [blame] | 214 | if (intf != configInterfaceName(sensorType)) |
Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 215 | { |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 216 | continue; |
Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 217 | } |
Zev Weiss | d7c1f76 | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 218 | std::string name = loadVariant<std::string>(cfg, "Name"); |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 219 | |
| 220 | std::vector<thresholds::Threshold> sensorThresholds; |
Zev Weiss | d7c1f76 | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 221 | if (!parseThresholdsFromConfig(interfaces, sensorThresholds)) |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 222 | { |
| 223 | std::cerr << "error populating thresholds for " << name |
| 224 | << "\n"; |
| 225 | } |
| 226 | |
Zev Weiss | d7c1f76 | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 227 | uint8_t busId = loadVariant<uint8_t>(cfg, "Bus"); |
| 228 | uint8_t mcuAddress = loadVariant<uint8_t>(cfg, "Address"); |
| 229 | uint8_t tempReg = loadVariant<uint8_t>(cfg, "Reg"); |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 230 | |
| 231 | std::string sensorClass = |
Zev Weiss | d7c1f76 | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 232 | loadVariant<std::string>(cfg, "Class"); |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 233 | |
| 234 | if constexpr (debug) |
| 235 | { |
Zev Weiss | d7c1f76 | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 236 | std::cerr << "Configuration parsed for \n\t" << intf << "\n" |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 237 | << "with\n" |
| 238 | << "\tName: " << name << "\n" |
| 239 | << "\tBus: " << static_cast<int>(busId) << "\n" |
| 240 | << "\tAddress: " << static_cast<int>(mcuAddress) |
| 241 | << "\n" |
| 242 | << "\tReg: " << static_cast<int>(tempReg) << "\n" |
| 243 | << "\tClass: " << sensorClass << "\n"; |
| 244 | } |
| 245 | |
| 246 | auto& sensor = sensors[name]; |
| 247 | |
| 248 | sensor = std::make_unique<MCUTempSensor>( |
Zev Weiss | d7c1f76 | 2022-08-12 18:21:02 -0700 | [diff] [blame] | 249 | dbusConnection, io, name, path, objectServer, |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 250 | std::move(sensorThresholds), busId, mcuAddress, tempReg); |
| 251 | |
| 252 | sensor->init(); |
Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 253 | } |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 254 | } |
Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 255 | }, |
JeffLin | 2c5a1f2 | 2022-10-05 15:19:09 +0800 | [diff] [blame] | 256 | entityManagerName, "/xyz/openbmc_project/inventory", |
| 257 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); |
Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 258 | } |
| 259 | |
James Feist | b6c0b91 | 2019-07-09 12:21:44 -0700 | [diff] [blame] | 260 | int main() |
Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 261 | { |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 262 | boost::asio::io_context io; |
Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 263 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
Ed Tanous | 14ed5e9 | 2022-07-12 15:50:23 -0700 | [diff] [blame] | 264 | sdbusplus::asio::object_server objectServer(systemBus, true); |
| 265 | objectServer.add_manager("/xyz/openbmc_project/sensors"); |
| 266 | |
Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 267 | systemBus->request_name("xyz.openbmc_project.MCUTempSensor"); |
Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 268 | |
Ed Tanous | 83db50c | 2023-03-01 10:20:24 -0800 | [diff] [blame] | 269 | boost::asio::post( |
| 270 | io, [&]() { createSensors(io, objectServer, sensors, systemBus); }); |
Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 271 | |
Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 272 | boost::asio::steady_timer configTimer(io); |
Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 273 | |
Patrick Williams | 92f8f51 | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 274 | std::function<void(sdbusplus::message_t&)> eventHandler = |
| 275 | [&](sdbusplus::message_t&) { |
Ed Tanous | 83db50c | 2023-03-01 10:20:24 -0800 | [diff] [blame] | 276 | configTimer.expires_after(std::chrono::seconds(1)); |
Ed Tanous | bb67932 | 2022-05-16 16:10:00 -0700 | [diff] [blame] | 277 | // create a timer because normally multiple properties change |
| 278 | configTimer.async_wait([&](const boost::system::error_code& ec) { |
| 279 | if (ec == boost::asio::error::operation_aborted) |
| 280 | { |
| 281 | return; // we're being canceled |
| 282 | } |
| 283 | // config timer error |
| 284 | if (ec) |
| 285 | { |
| 286 | std::cerr << "timer error\n"; |
| 287 | return; |
| 288 | } |
| 289 | createSensors(io, objectServer, sensors, systemBus); |
| 290 | if (sensors.empty()) |
| 291 | { |
| 292 | std::cout << "Configuration not detected\n"; |
| 293 | } |
| 294 | }); |
| 295 | }; |
Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 296 | |
Zev Weiss | 214d971 | 2022-08-12 12:54:31 -0700 | [diff] [blame] | 297 | std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches = |
| 298 | setupPropertiesChangedMatches( |
Zev Weiss | 054aad8 | 2022-08-18 01:37:34 -0700 | [diff] [blame] | 299 | *systemBus, std::to_array<const char*>({sensorType}), eventHandler); |
Bruce Lee | 1263c3d | 2021-06-04 15:16:33 +0800 | [diff] [blame] | 300 | setupManufacturingModeMatch(*systemBus); |
Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 301 | io.run(); |
Zhikui Ren | 8685b17 | 2021-06-29 15:16:52 -0700 | [diff] [blame] | 302 | return 0; |
Yuan Li | 445efe3 | 2019-06-14 22:58:32 +0800 | [diff] [blame] | 303 | } |