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