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>
diff --git a/dbus/README b/dbus/README
new file mode 100644
index 0000000..dd079d8
--- /dev/null
+++ b/dbus/README
@@ -0,0 +1,7 @@
+# DbusPassive
+
+These sensors wait for their updates.
+
+# DbusActive
+
+These sensors reach out for their value.
diff --git a/dbus/dbusactiveread.cpp b/dbus/dbusactiveread.cpp
new file mode 100644
index 0000000..5e046a4
--- /dev/null
+++ b/dbus/dbusactiveread.cpp
@@ -0,0 +1,45 @@
+/**
+ * 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 <cmath>
+#include <iostream>
+
+#include "dbusactiveread.hpp"
+#include "dbus/util.hpp"
+
+
+ReadReturn DbusActiveRead::read(void)
+{
+ struct SensorProperties settings;
+ double value;
+
+ GetProperties(_bus, _service, _path, &settings);
+
+ value = settings.value * pow(10, settings.scale);
+
+ /*
+ * Technically it might not be a value from now, but there's no timestamp
+ * on Sensor.Value yet.
+ */
+ struct ReadReturn r = {
+ value,
+ std::chrono::high_resolution_clock::now()
+ };
+
+ return r;
+}
+
diff --git a/dbus/dbusactiveread.hpp b/dbus/dbusactiveread.hpp
new file mode 100644
index 0000000..a9146b9
--- /dev/null
+++ b/dbus/dbusactiveread.hpp
@@ -0,0 +1,33 @@
+#pragma once
+
+#include <memory>
+#include <string>
+
+#include <sdbusplus/bus.hpp>
+
+#include "interfaces.hpp"
+
+
+/*
+ * This ReadInterface will actively reach out over dbus upon calling read to
+ * get the value from whomever owns the associated dbus path.
+ */
+class DbusActiveRead: public ReadInterface
+{
+ public:
+ DbusActiveRead(sdbusplus::bus::bus& bus,
+ const std::string& path,
+ const std::string& service)
+ : ReadInterface(),
+ _bus(bus),
+ _path(path),
+ _service(service)
+ { }
+
+ ReadReturn read(void) override;
+
+ private:
+ sdbusplus::bus::bus& _bus;
+ const std::string _path;
+ const std::string _service; // the sensor service.
+};
diff --git a/dbus/dbuspassive.cpp b/dbus/dbuspassive.cpp
new file mode 100644
index 0000000..25a0148
--- /dev/null
+++ b/dbus/dbuspassive.cpp
@@ -0,0 +1,107 @@
+/**
+ * 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 <cmath>
+#include <mutex>
+
+#include "dbuspassive.hpp"
+
+DbusPassive::DbusPassive(
+ sdbusplus::bus::bus& bus,
+ const std::string& type,
+ const std::string& id)
+ : ReadInterface(),
+ _bus(bus),
+ _signal(bus, GetMatch(type, id).c_str(), DbusHandleSignal, this),
+ _id(id)
+{
+ /* Need to get the scale and initial value */
+ auto tempBus = sdbusplus::bus::new_default();
+ /* service == busname */
+ std::string path = GetSensorPath(type, id);
+ std::string service = GetService(tempBus, sensorintf, path);
+
+ struct SensorProperties settings;
+ GetProperties(tempBus, service, path, &settings);
+
+ _scale = settings.scale;
+ _value = settings.value * pow(10, _scale);
+ _updated = std::chrono::high_resolution_clock::now();
+}
+
+ReadReturn DbusPassive::read(void)
+{
+ std::lock_guard<std::mutex> guard(_lock);
+
+ struct ReadReturn r = {
+ _value,
+ _updated
+ };
+
+ return r;
+}
+
+void DbusPassive::setValue(double value)
+{
+ std::lock_guard<std::mutex> guard(_lock);
+
+ _value = value;
+ _updated = std::chrono::high_resolution_clock::now();
+}
+
+int64_t DbusPassive::getScale(void)
+{
+ return _scale;
+}
+
+std::string DbusPassive::getId(void)
+{
+ return _id;
+}
+
+int DbusHandleSignal(sd_bus_message* msg, void* usrData, sd_bus_error* err)
+{
+ namespace sdm = sdbusplus::message;
+ auto sdbpMsg = sdm::message(msg);
+ DbusPassive* obj = static_cast<DbusPassive*>(usrData);
+
+ std::string msgSensor;
+ std::map<std::string, sdm::variant<int64_t>> msgData;
+ sdbpMsg.read(msgSensor, msgData);
+
+ if (msgSensor == "xyz.openbmc_project.Sensor.Value")
+ {
+ auto valPropMap = msgData.find("Value");
+ if (valPropMap != msgData.end())
+ {
+ int64_t rawValue = sdm::variant_ns::get<int64_t>
+ (valPropMap->second);
+
+ double value = rawValue * pow(10, obj->getScale());
+
+#if 0
+ std::cerr << "received update: " << value
+ << " for: " << obj->getId()
+ << std::endl;
+#endif
+
+ obj->setValue(value);
+ }
+ }
+
+ return 0;
+}
diff --git a/dbus/dbuspassive.hpp b/dbus/dbuspassive.hpp
new file mode 100644
index 0000000..36e0ac1
--- /dev/null
+++ b/dbus/dbuspassive.hpp
@@ -0,0 +1,58 @@
+#pragma once
+
+#include <chrono>
+#include <cmath>
+#include <iostream>
+#include <map>
+#include <mutex>
+#include <set>
+#include <string>
+#include <tuple>
+#include <vector>
+
+
+#include <sdbusplus/bus.hpp>
+#include <sdbusplus/message.hpp>
+#include <sdbusplus/server.hpp>
+
+#include "interfaces.hpp"
+#include "dbus/util.hpp"
+
+int DbusHandleSignal(sd_bus_message* msg, void* data, sd_bus_error* err);
+
+/*
+ * This ReadInterface will passively listen for Value updates from whomever
+ * owns the associated dbus object.
+ *
+ * This requires another modification in phosphor-dbus-interfaces that will
+ * signal a value update every time it's read instead of only when it changes
+ * to help us:
+ * - ensure we're still receiving data (since we don't control the reader)
+ * - simplify stale data detection
+ * - simplify error detection
+ */
+class DbusPassive : public ReadInterface
+{
+ public:
+ DbusPassive(sdbusplus::bus::bus& bus,
+ const std::string& type,
+ const std::string& id);
+
+ ReadReturn read(void) override;
+
+ void setValue(double value);
+ int64_t getScale(void);
+ std::string getId(void);
+
+ private:
+ sdbusplus::bus::bus& _bus;
+ sdbusplus::server::match::match _signal;
+ int64_t _scale;
+ std::string _id; // for debug identification
+
+ std::mutex _lock;
+ double _value = 0;
+ /* The last time the value was refreshed, not necessarily changed. */
+ std::chrono::high_resolution_clock::time_point _updated;
+};
+
diff --git a/dbus/util.cpp b/dbus/util.cpp
new file mode 100644
index 0000000..692e9c5
--- /dev/null
+++ b/dbus/util.cpp
@@ -0,0 +1,109 @@
+#include <iostream>
+
+#include "dbus/util.hpp"
+
+using Property = std::string;
+using Value = sdbusplus::message::variant<int64_t, std::string>;
+using PropertyMap = std::map<Property, Value>;
+
+/* TODO(venture): Basically all phosphor apps need this, maybe it should be a
+ * part of sdbusplus. There is an old version in libmapper.
+ */
+std::string GetService(sdbusplus::bus::bus& bus,
+ const std::string& intf,
+ const std::string& path)
+{
+ auto mapper = bus.new_method_call(
+ "xyz.openbmc_project.ObjectMapper",
+ "/xyz/openbmc_project/object_mapper",
+ "xyz.openbmc_project.ObjectMapper",
+ "GetObject");
+
+ mapper.append(path);
+ mapper.append(std::vector<std::string>({intf}));
+
+ auto responseMsg = bus.call(mapper);
+ if (responseMsg.is_method_error())
+ {
+ throw std::runtime_error("ObjectMapper Call Failure");
+ }
+
+ std::map<std::string, std::vector<std::string>> response;
+ responseMsg.read(response);
+
+ if (response.begin() == response.end())
+ {
+ throw std::runtime_error("Unable to find Object: " + path);
+ }
+
+ return response.begin()->first;
+}
+
+void GetProperties(sdbusplus::bus::bus& bus,
+ const std::string& service,
+ const std::string& path,
+ struct SensorProperties* prop)
+{
+
+ auto pimMsg = bus.new_method_call(service.c_str(),
+ path.c_str(),
+ propertiesintf.c_str(),
+ "GetAll");
+
+ pimMsg.append(sensorintf);
+ auto valueResponseMsg = bus.call(pimMsg);
+
+ if (valueResponseMsg.is_method_error())
+ {
+ std::cerr << "Error in value call\n";
+ throw std::runtime_error("ERROR in value call.");
+ }
+
+ // The PropertyMap returned will look like this because it's always
+ // reading a Sensor.Value interface.
+ // a{sv} 3:
+ // "Value" x 24875
+ // "Unit" s "xyz.openbmc_project.Sensor.Value.Unit.DegreesC"
+ // "Scale" x -3
+ PropertyMap propMap;
+ valueResponseMsg.read(propMap);
+
+ if (propMap.size() != 3)
+ {
+ throw std::runtime_error("ERROR in results, expected three properties");
+ }
+
+ prop->unit = sdbusplus::message::variant_ns::get<std::string>(propMap["Unit"]);
+ prop->scale = sdbusplus::message::variant_ns::get<int64_t>(propMap["Scale"]);
+ prop->value = sdbusplus::message::variant_ns::get<int64_t>(propMap["Value"]);
+
+ return;
+}
+
+std::string GetSensorPath(const std::string& type, const std::string& id)
+{
+ std::string layer = type;
+ if (type == "fan")
+ {
+ layer = "fan_tach";
+ }
+ else if (type == "temp")
+ {
+ layer = "temperature";
+ }
+ else
+ {
+ layer = "unknown"; // TODO(venture): Need to handle.
+ }
+
+ return std::string("/xyz/openbmc_project/sensors/" + layer + "/" + id);
+}
+
+std::string GetMatch(const std::string& type, const std::string& id)
+{
+ return std::string("type='signal',"
+ "interface='org.freedesktop.DBus.Properties',"
+ "member='PropertiesChanged',"
+ "path='" + GetSensorPath(type, id) + "'");
+}
+
diff --git a/dbus/util.hpp b/dbus/util.hpp
new file mode 100644
index 0000000..3ff2424
--- /dev/null
+++ b/dbus/util.hpp
@@ -0,0 +1,29 @@
+#pragma once
+
+#include <sdbusplus/bus.hpp>
+
+struct SensorProperties
+{
+ int64_t scale;
+ int64_t value;
+ std::string unit;
+};
+
+/*
+ * Retrieve the dbus bus (or service) for the given object and interface.
+ */
+std::string GetService(sdbusplus::bus::bus& bus,
+ const std::string& intf,
+ const std::string& path);
+
+void GetProperties(sdbusplus::bus::bus& bus,
+ const std::string& service,
+ const std::string& path,
+ struct SensorProperties* prop);
+
+std::string GetSensorPath(const std::string& type, const std::string& id);
+std::string GetMatch(const std::string& type, const std::string& id);
+
+const std::string sensorintf = "xyz.openbmc_project.Sensor.Value";
+const std::string propertiesintf = "org.freedesktop.DBus.Properties";
+