blob: 7befa9296ab880f546a9cf577910dfbeb05c8568 [file] [log] [blame]
Yuan Li445efe32019-06-14 22:58:32 +08001/*
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 Tanous8a57ec02020-10-09 12:46:52 -070016#include <MCUTempSensor.hpp>
17#include <Utils.hpp>
18#include <VariantVisitors.hpp>
Yuan Li445efe32019-06-14 22:58:32 +080019#include <boost/algorithm/string/predicate.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070020#include <boost/container/flat_map.hpp>
James Feist38fb5982020-05-28 10:09:54 -070021#include <sdbusplus/asio/connection.hpp>
22#include <sdbusplus/asio/object_server.hpp>
23#include <sdbusplus/bus/match.hpp>
24
Yuan Li445efe32019-06-14 22:58:32 +080025#include <chrono>
Ed Tanous8a57ec02020-10-09 12:46:52 -070026#include <cmath>
Patrick Venture96e97db2019-10-31 13:44:38 -070027#include <functional>
Yuan Li445efe32019-06-14 22:58:32 +080028#include <iostream>
29#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070030#include <memory>
Yuan Li445efe32019-06-14 22:58:32 +080031#include <numeric>
Patrick Venture96e97db2019-10-31 13:44:38 -070032#include <string>
Yuan Li445efe32019-06-14 22:58:32 +080033#include <vector>
34
James Feist38fb5982020-05-28 10:09:54 -070035extern "C"
36{
Yuan Li445efe32019-06-14 22:58:32 +080037#include <i2c/smbus.h>
38#include <linux/i2c-dev.h>
39}
40
41constexpr const bool debug = false;
42
43constexpr const char* configInterface =
44 "xyz.openbmc_project.Configuration.MCUTempSensor";
45static constexpr double mcuTempMaxReading = 0xFF;
46static constexpr double mcuTempMinReading = 0;
47
48boost::container::flat_map<std::string, std::unique_ptr<MCUTempSensor>> sensors;
49
50MCUTempSensor::MCUTempSensor(std::shared_ptr<sdbusplus::asio::connection>& conn,
51 boost::asio::io_service& io,
52 const std::string& sensorName,
53 const std::string& sensorConfiguration,
54 sdbusplus::asio::object_server& objectServer,
55 std::vector<thresholds::Threshold>&& thresholdData,
56 uint8_t busId, uint8_t mcuAddress,
57 uint8_t tempReg) :
Zhikui Renda98f092021-11-01 09:41:08 -070058 Sensor(escapeName(sensorName), std::move(thresholdData),
59 sensorConfiguration, "xyz.openbmc_project.Configuration.ExitAirTemp",
60 false, false, mcuTempMaxReading, mcuTempMinReading, conn),
Brad Bishopfbb44ad2019-11-08 09:42:37 -050061 busId(busId), mcuAddress(mcuAddress), tempReg(tempReg),
James Feiste3338522020-09-15 15:40:30 -070062 objectServer(objectServer), waitTimer(io)
Yuan Li445efe32019-06-14 22:58:32 +080063{
64 sensorInterface = objectServer.add_interface(
65 "/xyz/openbmc_project/sensors/temperature/" + name,
66 "xyz.openbmc_project.Sensor.Value");
67
Jayashree Dhanapal56678082022-01-04 17:27:20 +053068 for (const auto& threshold : thresholds)
Yuan Li445efe32019-06-14 22:58:32 +080069 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +053070 std::string interface = thresholds::getInterface(threshold.level);
71 thresholdInterfaces[static_cast<size_t>(threshold.level)] =
72 objectServer.add_interface(
73 "/xyz/openbmc_project/sensors/temperature/" + name, interface);
Yuan Li445efe32019-06-14 22:58:32 +080074 }
75 association = objectServer.add_interface(
76 "/xyz/openbmc_project/sensors/temperature/" + name,
James Feist2adc95c2019-09-30 14:55:28 -070077 association::interface);
Yuan Li445efe32019-06-14 22:58:32 +080078}
79
80MCUTempSensor::~MCUTempSensor()
81{
82 waitTimer.cancel();
Jayashree Dhanapal56678082022-01-04 17:27:20 +053083 for (const auto& iface : thresholdInterfaces)
84 {
85 objectServer.remove_interface(iface);
86 }
Yuan Li445efe32019-06-14 22:58:32 +080087 objectServer.remove_interface(sensorInterface);
88 objectServer.remove_interface(association);
89}
90
91void MCUTempSensor::init(void)
92{
Andrei Kartashev39287412022-02-04 16:04:47 +030093 setInitialProperties(sensor_paths::unitDegreesC);
Yuan Li445efe32019-06-14 22:58:32 +080094 read();
95}
96
97void MCUTempSensor::checkThresholds(void)
98{
99 thresholds::checkThresholds(this);
100}
101
Ed Tanous2049bd22022-07-09 07:20:26 -0700102int MCUTempSensor::getMCURegsInfoWord(uint8_t regs, int16_t* pu16data) const
Yuan Li445efe32019-06-14 22:58:32 +0800103{
104 std::string i2cBus = "/dev/i2c-" + std::to_string(busId);
Yuan Li445efe32019-06-14 22:58:32 +0800105
Ed Tanous99c44092022-01-14 09:59:09 -0800106 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
107 int fd = open(i2cBus.c_str(), O_RDWR);
Yuan Li445efe32019-06-14 22:58:32 +0800108 if (fd < 0)
109 {
110 std::cerr << " unable to open i2c device" << i2cBus << " err=" << fd
111 << "\n";
112 return -1;
113 }
114
Ed Tanous99c44092022-01-14 09:59:09 -0800115 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
Yuan Li445efe32019-06-14 22:58:32 +0800116 if (ioctl(fd, I2C_SLAVE_FORCE, mcuAddress) < 0)
117 {
118 std::cerr << " unable to set device address\n";
119 close(fd);
120 return -1;
121 }
122
123 unsigned long funcs = 0;
Ed Tanous99c44092022-01-14 09:59:09 -0800124 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
Yuan Li445efe32019-06-14 22:58:32 +0800125 if (ioctl(fd, I2C_FUNCS, &funcs) < 0)
126 {
127 std::cerr << " not support I2C_FUNCS\n";
128 close(fd);
129 return -1;
130 }
131
Ed Tanous2049bd22022-07-09 07:20:26 -0700132 if ((funcs & I2C_FUNC_SMBUS_READ_WORD_DATA) == 0U)
Yuan Li445efe32019-06-14 22:58:32 +0800133 {
134 std::cerr << " not support I2C_FUNC_SMBUS_READ_WORD_DATA\n";
135 close(fd);
136 return -1;
137 }
138
139 *pu16data = i2c_smbus_read_word_data(fd, regs);
140 close(fd);
141
142 if (*pu16data < 0)
143 {
144 std::cerr << " read word data failed at " << static_cast<int>(regs)
145 << "\n";
146 return -1;
147 }
148
149 return 0;
150}
151
152void MCUTempSensor::read(void)
153{
154 static constexpr size_t pollTime = 1; // in seconds
155
156 waitTimer.expires_from_now(boost::posix_time::seconds(pollTime));
157 waitTimer.async_wait([this](const boost::system::error_code& ec) {
158 if (ec == boost::asio::error::operation_aborted)
159 {
160 return; // we're being cancelled
161 }
162 // read timer error
Ed Tanous8a57ec02020-10-09 12:46:52 -0700163 if (ec)
Yuan Li445efe32019-06-14 22:58:32 +0800164 {
165 std::cerr << "timer error\n";
166 return;
167 }
Ed Tanousa771f6a2022-01-14 09:36:51 -0800168 int16_t temp = 0;
Yuan Li445efe32019-06-14 22:58:32 +0800169 int ret = getMCURegsInfoWord(tempReg, &temp);
170 if (ret >= 0)
171 {
172 double v = static_cast<double>(temp) / 1000;
173 if constexpr (debug)
174 {
James Feistb6c0b912019-07-09 12:21:44 -0700175 std::cerr << "Value update to " << v << "raw reading "
Yuan Li445efe32019-06-14 22:58:32 +0800176 << static_cast<int>(temp) << "\n";
177 }
178 updateValue(v);
179 }
180 else
181 {
182 std::cerr << "Invalid read getMCURegsInfoWord\n";
James Feist961bf092020-07-01 16:38:12 -0700183 incrementError();
Yuan Li445efe32019-06-14 22:58:32 +0800184 }
185 read();
186 });
187}
188
189void createSensors(
190 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
191 boost::container::flat_map<std::string, std::unique_ptr<MCUTempSensor>>&
192 sensors,
193 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection)
194{
195 if (!dbusConnection)
196 {
197 std::cerr << "Connection not created\n";
198 return;
199 }
200
201 dbusConnection->async_method_call(
202 [&io, &objectServer, &dbusConnection, &sensors](
203 boost::system::error_code ec, const ManagedObjectType& resp) {
Ed Tanousbb679322022-05-16 16:10:00 -0700204 if (ec)
205 {
206 std::cerr << "Error contacting entity manager\n";
207 return;
208 }
Zev Weissd7c1f762022-08-12 18:21:02 -0700209 for (const auto& [path, interfaces] : resp)
Ed Tanousbb679322022-05-16 16:10:00 -0700210 {
Zev Weissd7c1f762022-08-12 18:21:02 -0700211 for (const auto& [intf, cfg] : interfaces)
Yuan Li445efe32019-06-14 22:58:32 +0800212 {
Zev Weissd7c1f762022-08-12 18:21:02 -0700213 if (intf != configInterface)
Yuan Li445efe32019-06-14 22:58:32 +0800214 {
Ed Tanousbb679322022-05-16 16:10:00 -0700215 continue;
Yuan Li445efe32019-06-14 22:58:32 +0800216 }
Zev Weissd7c1f762022-08-12 18:21:02 -0700217 std::string name = loadVariant<std::string>(cfg, "Name");
Ed Tanousbb679322022-05-16 16:10:00 -0700218
219 std::vector<thresholds::Threshold> sensorThresholds;
Zev Weissd7c1f762022-08-12 18:21:02 -0700220 if (!parseThresholdsFromConfig(interfaces, sensorThresholds))
Ed Tanousbb679322022-05-16 16:10:00 -0700221 {
222 std::cerr << "error populating thresholds for " << name
223 << "\n";
224 }
225
Zev Weissd7c1f762022-08-12 18:21:02 -0700226 uint8_t busId = loadVariant<uint8_t>(cfg, "Bus");
227 uint8_t mcuAddress = loadVariant<uint8_t>(cfg, "Address");
228 uint8_t tempReg = loadVariant<uint8_t>(cfg, "Reg");
Ed Tanousbb679322022-05-16 16:10:00 -0700229
230 std::string sensorClass =
Zev Weissd7c1f762022-08-12 18:21:02 -0700231 loadVariant<std::string>(cfg, "Class");
Ed Tanousbb679322022-05-16 16:10:00 -0700232
233 if constexpr (debug)
234 {
Zev Weissd7c1f762022-08-12 18:21:02 -0700235 std::cerr << "Configuration parsed for \n\t" << intf << "\n"
Ed Tanousbb679322022-05-16 16:10:00 -0700236 << "with\n"
237 << "\tName: " << name << "\n"
238 << "\tBus: " << static_cast<int>(busId) << "\n"
239 << "\tAddress: " << static_cast<int>(mcuAddress)
240 << "\n"
241 << "\tReg: " << static_cast<int>(tempReg) << "\n"
242 << "\tClass: " << sensorClass << "\n";
243 }
244
245 auto& sensor = sensors[name];
246
247 sensor = std::make_unique<MCUTempSensor>(
Zev Weissd7c1f762022-08-12 18:21:02 -0700248 dbusConnection, io, name, path, objectServer,
Ed Tanousbb679322022-05-16 16:10:00 -0700249 std::move(sensorThresholds), busId, mcuAddress, tempReg);
250
251 sensor->init();
Yuan Li445efe32019-06-14 22:58:32 +0800252 }
Ed Tanousbb679322022-05-16 16:10:00 -0700253 }
Yuan Li445efe32019-06-14 22:58:32 +0800254 },
255 entityManagerName, "/", "org.freedesktop.DBus.ObjectManager",
256 "GetManagedObjects");
257}
258
James Feistb6c0b912019-07-09 12:21:44 -0700259int main()
Yuan Li445efe32019-06-14 22:58:32 +0800260{
261 boost::asio::io_service io;
262 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
263 systemBus->request_name("xyz.openbmc_project.MCUTempSensor");
264 sdbusplus::asio::object_server objectServer(systemBus);
265
266 io.post([&]() { createSensors(io, objectServer, sensors, systemBus); });
267
268 boost::asio::deadline_timer configTimer(io);
269
Patrick Williams92f8f512022-07-22 19:26:55 -0500270 std::function<void(sdbusplus::message_t&)> eventHandler =
271 [&](sdbusplus::message_t&) {
Ed Tanousbb679322022-05-16 16:10:00 -0700272 configTimer.expires_from_now(boost::posix_time::seconds(1));
273 // create a timer because normally multiple properties change
274 configTimer.async_wait([&](const boost::system::error_code& ec) {
275 if (ec == boost::asio::error::operation_aborted)
276 {
277 return; // we're being canceled
278 }
279 // config timer error
280 if (ec)
281 {
282 std::cerr << "timer error\n";
283 return;
284 }
285 createSensors(io, objectServer, sensors, systemBus);
286 if (sensors.empty())
287 {
288 std::cout << "Configuration not detected\n";
289 }
290 });
291 };
Yuan Li445efe32019-06-14 22:58:32 +0800292
Zev Weiss214d9712022-08-12 12:54:31 -0700293 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
294 setupPropertiesChangedMatches(
295 *systemBus, std::to_array<const char*>({configInterface}),
296 eventHandler);
Bruce Lee1263c3d2021-06-04 15:16:33 +0800297 setupManufacturingModeMatch(*systemBus);
Yuan Li445efe32019-06-14 22:58:32 +0800298 io.run();
Zhikui Ren8685b172021-06-29 15:16:52 -0700299 return 0;
Yuan Li445efe32019-06-14 22:58:32 +0800300}