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";
+
diff --git a/notimpl/README b/notimpl/README
new file mode 100644
index 0000000..268de39
--- /dev/null
+++ b/notimpl/README
@@ -0,0 +1,2 @@
+These are convenience objects that let one create a read-only sensor or a
+write-only object in whatever scenario.
\ No newline at end of file
diff --git a/notimpl/readonly.cpp b/notimpl/readonly.cpp
new file mode 100644
index 0000000..f6ddad3
--- /dev/null
+++ b/notimpl/readonly.cpp
@@ -0,0 +1,30 @@
+/**
+ * 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 <stdexcept>
+
+#include "readonly.hpp"
+
+
+void ReadOnly::write(double value)
+{
+    throw std::runtime_error("Not supported.");
+}
+
+void ReadOnlyNoExcept::write(double value)
+{
+    return;
+}
diff --git a/notimpl/readonly.hpp b/notimpl/readonly.hpp
new file mode 100644
index 0000000..a609e22
--- /dev/null
+++ b/notimpl/readonly.hpp
@@ -0,0 +1,26 @@
+#pragma once
+
+/* Interface that implements an exception throwing read method. */
+
+#include "interfaces.hpp"
+
+
+class ReadOnly: public WriteInterface
+{
+    public:
+        ReadOnly()
+            : WriteInterface(0, 0)
+        { }
+
+        void write(double value) override;
+};
+
+class ReadOnlyNoExcept: public WriteInterface
+{
+    public:
+        ReadOnlyNoExcept()
+            : WriteInterface(0, 0)
+        { }
+
+        void write(double value) override;
+};
diff --git a/notimpl/writeonly.cpp b/notimpl/writeonly.cpp
new file mode 100644
index 0000000..a465ecf
--- /dev/null
+++ b/notimpl/writeonly.cpp
@@ -0,0 +1,26 @@
+/**
+ * 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 <stdexcept>
+
+#include "writeonly.hpp"
+
+
+ReadReturn WriteOnly::read(void)
+{
+    throw std::runtime_error("Not supported.");
+}
+
diff --git a/notimpl/writeonly.hpp b/notimpl/writeonly.hpp
new file mode 100644
index 0000000..4c26fbd
--- /dev/null
+++ b/notimpl/writeonly.hpp
@@ -0,0 +1,15 @@
+/* Interface that implements an exception throwing write method. */
+#pragma once
+
+#include "interfaces.hpp"
+
+
+class WriteOnly: public ReadInterface
+{
+    public:
+        WriteOnly()
+            : ReadInterface()
+        { }
+
+        ReadReturn read(void) override;
+};
diff --git a/sensors/README b/sensors/README
new file mode 100644
index 0000000..d2c2f34
--- /dev/null
+++ b/sensors/README
@@ -0,0 +1,20 @@
+# HostSensor
+
+A HostSensor object is one whose value is received from the host over IPMI (or
+some other mechanism).  These sensors create dbus objects in the following
+namespace:
+        /xyz/openbmc_projects/extsensors/{namespace}/{sensorname}
+
+You can update them by setting the Value in Sensor.Value as a set or update
+property dbus call.
+
+# SensorManager
+
+There is a SensorManager object whose job is to hold all the sensors.
+
+# PluggableSensor
+
+The PluggableSensor is an object that receives a reader and writer and is
+therefore meant to be used for a variety of sensor types that aren't
+HostSensors.
+
diff --git a/sensors/host.cpp b/sensors/host.cpp
new file mode 100644
index 0000000..44e83d8
--- /dev/null
+++ b/sensors/host.cpp
@@ -0,0 +1,73 @@
+/**
+ * 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 <cmath>
+#include <iostream>
+#include <memory>
+#include <mutex>
+
+#include "host.hpp"
+
+std::unique_ptr<Sensor> HostSensor::CreateTemp(
+    const std::string& name,
+    int64_t timeout,
+    sdbusplus::bus::bus& bus,
+    const char* objPath,
+    bool defer)
+{
+    auto sensor = std::make_unique<HostSensor>(name, timeout, bus, objPath, defer);
+    sensor->value(0);
+
+    // TODO(venture): Need to not hard-code that this is DegreesC and scale
+    // 10x-3 unless it is! :D
+    sensor->unit(ValueInterface::Unit::DegreesC);
+    sensor->scale(-3);
+    sensor->emit_object_added();
+
+    /* TODO(venture): Need to set that _updated is set to epoch or something
+     * else.  what is the default value?
+     */
+    return sensor;
+}
+
+int64_t HostSensor::value(int64_t value)
+{
+    std::lock_guard<std::mutex> guard(_lock);
+
+    _updated = std::chrono::high_resolution_clock::now();
+    _value = value * pow(10, scale()); /* scale value */
+
+    return ValueObject::value(value);
+}
+
+ReadReturn HostSensor::read(void)
+{
+    std::lock_guard<std::mutex> guard(_lock);
+
+    /* This doesn't sanity check anything, that's the caller's job. */
+    struct ReadReturn r = {
+        _value,
+        _updated
+    };
+
+    return r;
+}
+
+void HostSensor::write(double value)
+{
+    throw std::runtime_error("Not Implemented.");
+}
+
diff --git a/sensors/host.hpp b/sensors/host.hpp
new file mode 100644
index 0000000..5d25aa0
--- /dev/null
+++ b/sensors/host.hpp
@@ -0,0 +1,56 @@
+#pragma once
+
+#include <memory>
+#include <mutex>
+
+#include <sdbusplus/bus.hpp>
+#include <sdbusplus/server.hpp>
+#include "xyz/openbmc_project/Sensor/Value/server.hpp"
+
+#include "sensor.hpp"
+
+template <typename... T>
+using ServerObject = typename sdbusplus::server::object::object<T...>;
+
+using ValueInterface = sdbusplus::xyz::openbmc_project::Sensor::server::Value;
+using ValueObject = ServerObject<ValueInterface>;
+
+/*
+ * HostSensor object is a Sensor derivative that also implements a ValueObject,
+ * which comes from the dbus as an object that implements Sensor.Value.
+ */
+class HostSensor : public Sensor, public ValueObject
+{
+    public:
+        static std::unique_ptr<Sensor> CreateTemp(
+            const std::string& name,
+            int64_t timeout,
+            sdbusplus::bus::bus& bus,
+            const char* objPath,
+            bool defer);
+
+        HostSensor(const std::string& name,
+                   int64_t timeout,
+                   sdbusplus::bus::bus& bus,
+                   const char* objPath,
+                   bool defer)
+            : Sensor(name, timeout),
+              ValueObject(bus, objPath, defer)
+        { }
+
+        /* Note: This must be int64_t because it's from ValueObject */
+        int64_t value(int64_t value) override;
+
+        ReadReturn read(void) override;
+        void write(double value) override;
+
+    private:
+        /*
+         * _lock will be used to make sure _updated & _value are updated
+         * together.
+         */
+        std::mutex _lock;
+        std::chrono::high_resolution_clock::time_point _updated;
+        double _value = 0;
+};
+
diff --git a/sensors/manager.cpp b/sensors/manager.cpp
new file mode 100644
index 0000000..590ca80
--- /dev/null
+++ b/sensors/manager.cpp
@@ -0,0 +1,248 @@
+/**
+ * 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 <cstring>
+#include <iostream>
+#include <libconfig.h++>
+#include <map>
+#include <memory>
+
+/* Configuration. */
+#include "conf.hpp"
+
+#include "interfaces.hpp"
+#include "manager.hpp"
+#include "util.hpp"
+
+#include "dbus/dbuspassive.hpp"
+#include "notimpl/readonly.hpp"
+#include "notimpl/writeonly.hpp"
+#include "sysfs/sysfsread.hpp"
+#include "sensors/manager.hpp"
+#include "sensors/host.hpp"
+#include "sensors/pluggable.hpp"
+#include "sysfs/sysfswrite.hpp"
+
+
+static constexpr bool deferSignals = true;
+
+std::shared_ptr<SensorManager> BuildSensors(
+    std::map<std::string, struct sensor>& Config)
+{
+    auto mgmr = std::make_shared<SensorManager>();
+    auto& HostSensorBus = mgmr->getHostBus();
+    auto& PassiveListeningBus = mgmr->getPassiveBus();
+
+    for (auto& it : Config)
+    {
+        std::unique_ptr<ReadInterface> ri;
+        std::unique_ptr<WriteInterface> wi;
+
+        std::string name = it.first;
+        struct sensor* info = &it.second;
+
+        std::cerr << "Sensor: " << name << " " << info->type << " ";
+        std::cerr << info->readpath << " " << info->writepath << "\n";
+
+        IOInterfaceType rtype = GetReadInterfaceType(info->readpath);
+        IOInterfaceType wtype = GetWriteInterfaceType(info->writepath);
+
+        // fan sensors can be ready any way and written others.
+        // fan sensors are the only sensors this is designed to write.
+        // Nothing here should be write-only, although, in theory a fan could be.
+        // I'm just not sure how that would fit together.
+        // TODO(venture): It should check with the ObjectMapper to check if
+        // that sensor exists on the Dbus.
+        switch (rtype)
+        {
+            case IOInterfaceType::DBUSPASSIVE:
+                ri = std::make_unique<DbusPassive>(
+                         PassiveListeningBus,
+                         info->type,
+                         name);
+                break;
+            case IOInterfaceType::EXTERNAL:
+                // These are a special case for read-only.
+                break;
+            case IOInterfaceType::SYSFS:
+                ri = std::make_unique<SysFsRead>(info->readpath);
+                break;
+            default:
+                ri = std::make_unique<WriteOnly>();
+                break;
+        }
+
+        if (info->type == "fan")
+        {
+            switch (wtype)
+            {
+                case IOInterfaceType::SYSFS:
+                    if (info->max > 0)
+                    {
+                        wi = std::make_unique<SysFsWritePercent>(
+                                 info->writepath,
+                                 info->min,
+                                 info->max);
+                    }
+                    else
+                    {
+                        wi = std::make_unique<SysFsWrite>(
+                                 info->writepath,
+                                 info->min,
+                                 info->max);
+                    }
+
+                    break;
+                default:
+                    wi = std::make_unique<ReadOnlyNoExcept>();
+                    break;
+            }
+
+            auto sensor = std::make_unique<PluggableSensor>(
+                              name,
+                              info->timeout,
+                              std::move(ri),
+                              std::move(wi));
+            mgmr->addSensor(info->type, name, std::move(sensor));
+        }
+        else if (info->type == "temp" || info->type == "margin")
+        {
+            // These sensors are read-only, but only for this application
+            // which only writes to fan sensors.
+            std::cerr << info->type << " readpath: " << info->readpath << "\n";
+
+            if (IOInterfaceType::EXTERNAL == rtype)
+            {
+                std::cerr << "Creating HostSensor: " << name
+                          << " path: " << info->readpath << "\n";
+
+                /*
+                 * The reason we handle this as a HostSensor is because it's
+                 * not quite pluggable; but maybe it could be.
+                 */
+                auto sensor = HostSensor::CreateTemp(
+                                  name,
+                                  info->timeout,
+                                  HostSensorBus,
+                                  info->readpath.c_str(),
+                                  deferSignals);
+                mgmr->addSensor(info->type, name, std::move(sensor));
+            }
+            else
+            {
+                wi = std::make_unique<ReadOnlyNoExcept>();
+                auto sensor = std::make_unique<PluggableSensor>(
+                                  name,
+                                  info->timeout,
+                                  std::move(ri),
+                                  std::move(wi));
+                mgmr->addSensor(info->type, name, std::move(sensor));
+            }
+        }
+    }
+
+    return mgmr;
+}
+
+/*
+ * If there's a configuration file, we build from that, and it requires special
+ * parsing.  I should just ditch the compile-time version to reduce the
+ * probability of sync bugs.
+ */
+std::shared_ptr<SensorManager> BuildSensorsFromConfig(std::string& path)
+{
+    using namespace libconfig;
+
+    std::map<std::string, struct sensor> config;
+    Config cfg;
+
+    std::cerr << "entered BuildSensorsFromConfig\n";
+
+    /* The load was modeled after the example source provided. */
+    try
+    {
+        cfg.readFile(path.c_str());
+    }
+    catch (const FileIOException& fioex)
+    {
+        std::cerr << "I/O error while reading file: " << fioex.what() << std::endl;
+        throw;
+    }
+    catch (const ParseException& pex)
+    {
+        std::cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine()
+                  << " - " << pex.getError() << std::endl;
+        throw;
+    }
+
+    try
+    {
+        const Setting& root = cfg.getRoot();
+
+        /* Grab the list of sensors and create them all */
+        const Setting& sensors = root["sensors"];
+        int count = sensors.getLength();
+
+        for (int i = 0; i < count; ++i)
+        {
+            const Setting& sensor = sensors[i];
+
+            std::string name;
+            struct sensor thisOne;
+
+            /* Not a super fan of using this library for run-time configuration. */
+            name = sensor.lookup("name").c_str();
+            thisOne.type = sensor.lookup("type").c_str();
+            thisOne.readpath = sensor.lookup("readpath").c_str();
+            thisOne.writepath = sensor.lookup("writepath").c_str();
+
+            /* TODO: Document why this is wonky.  The library probably doesn't
+             * like int64_t
+             */
+            int min = sensor.lookup("min");
+            thisOne.min = static_cast<int64_t>(min);
+            int max = sensor.lookup("max");
+            thisOne.max = static_cast<int64_t>(max);
+            int timeout = sensor.lookup("timeout");
+            thisOne.timeout = static_cast<int64_t>(timeout);
+
+            // leaving for verification for now.  and yea the above is
+            // necessary.
+            std::cerr << "min: " << min
+                    << " max: " << max
+                    << " savedmin: " << thisOne.min
+                    << " savedmax: " << thisOne.max
+                    << " timeout: " << thisOne.timeout
+                    << std::endl;
+
+            config[name] = thisOne;
+        }
+    }
+    catch (const SettingTypeException &setex)
+    {
+        std::cerr << "Setting '" << setex.getPath()
+                  << "' type exception!" << std::endl;
+        throw;
+    }
+    catch (const SettingNotFoundException& snex)
+    {
+        std::cerr << "Setting not found!" << std::endl;
+        throw;
+    }
+
+    return BuildSensors(config);
+}
+
diff --git a/sensors/manager.hpp b/sensors/manager.hpp
new file mode 100644
index 0000000..5de058e
--- /dev/null
+++ b/sensors/manager.hpp
@@ -0,0 +1,75 @@
+#pragma once
+
+#include <map>
+#include <memory>
+#include <string>
+#include <vector>
+
+#include <sdbusplus/bus.hpp>
+#include <sdbusplus/server.hpp>
+
+#include "sensors/sensor.hpp"
+
+
+/*
+ * The SensorManager holds all sensors across all zones.
+ */
+class SensorManager
+{
+    public:
+        SensorManager()
+            : _passiveListeningBus(std::move(sdbusplus::bus::new_default())),
+              _hostSensorBus(std::move(sdbusplus::bus::new_default()))
+        {
+            // Create a manger for the sensor root because we own it.
+            static constexpr auto SensorRoot = "/xyz/openbmc_project/extsensors";
+            sdbusplus::server::manager::manager(_hostSensorBus, SensorRoot);
+        }
+
+        /*
+         * Add a Sensor to the Manager.
+         */
+        void addSensor(
+            std::string type,
+            std::string name,
+            std::unique_ptr<Sensor> sensor)
+        {
+            _sensorMap[name] = std::move(sensor);
+
+            auto entry = _sensorTypeList.find(type);
+            if (entry == _sensorTypeList.end())
+            {
+                _sensorTypeList[type] = {};
+            }
+
+            _sensorTypeList[type].push_back(name);
+        }
+
+        // TODO(venture): Should implement read/write by name.
+        std::unique_ptr<Sensor>& getSensor(std::string name)
+        {
+            return _sensorMap.at(name);
+        }
+
+        sdbusplus::bus::bus& getPassiveBus(void)
+        {
+            return _passiveListeningBus;
+        }
+
+        sdbusplus::bus::bus& getHostBus(void)
+        {
+            return _hostSensorBus;
+        }
+
+    private:
+        std::map<std::string, std::unique_ptr<Sensor>> _sensorMap;
+        std::map<std::string, std::vector<std::string>> _sensorTypeList;
+
+        sdbusplus::bus::bus _passiveListeningBus;
+        sdbusplus::bus::bus _hostSensorBus;
+};
+
+std::shared_ptr<SensorManager> BuildSensors(
+    std::map<std::string, struct sensor>& Config);
+
+std::shared_ptr<SensorManager> BuildSensorsFromConfig(std::string& path);
diff --git a/sensors/pluggable.cpp b/sensors/pluggable.cpp
new file mode 100644
index 0000000..8039aea
--- /dev/null
+++ b/sensors/pluggable.cpp
@@ -0,0 +1,36 @@
+/**
+ * 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 "pluggable.hpp"
+
+#include <iostream>
+#include <memory>
+#include <string>
+
+#include "sysfs/sysfswrite.hpp"
+#include "dbus/dbuspassive.hpp"
+
+
+ReadReturn PluggableSensor::read(void)
+{
+    return _reader->read();
+}
+
+void PluggableSensor::write(double value)
+{
+    _writer->write(value);
+}
+
diff --git a/sensors/pluggable.hpp b/sensors/pluggable.hpp
new file mode 100644
index 0000000..1eb6571
--- /dev/null
+++ b/sensors/pluggable.hpp
@@ -0,0 +1,33 @@
+#pragma once
+
+#include <memory>
+#include <string>
+
+#include <sdbusplus/bus.hpp>
+
+#include "interfaces.hpp"
+#include "sensor.hpp"
+
+
+/*
+ * A Sensor that can use any reader or writer you provide.
+ */
+class PluggableSensor : public Sensor
+{
+    public:
+        PluggableSensor(const std::string& name,
+                        int64_t timeout,
+                        std::unique_ptr<ReadInterface> reader,
+                        std::unique_ptr<WriteInterface> writer)
+            : Sensor(name, timeout),
+              _reader(std::move(reader)),
+              _writer(std::move(writer))
+        { }
+
+        ReadReturn read(void) override;
+        void write(double value) override;
+
+    private:
+        std::unique_ptr<ReadInterface> _reader;
+        std::unique_ptr<WriteInterface> _writer;
+};
diff --git a/sensors/sensor.hpp b/sensors/sensor.hpp
new file mode 100644
index 0000000..dc5f3e0
--- /dev/null
+++ b/sensors/sensor.hpp
@@ -0,0 +1,41 @@
+#pragma once
+
+#include <chrono>
+#include <string>
+
+#include "interfaces.hpp"
+
+
+/**
+ * Abstract base class for all sensors.
+ */
+class Sensor
+{
+    public:
+        Sensor(std::string name, int64_t timeout)
+            : _name(name), _timeout(timeout)
+        { }
+
+        virtual ~Sensor() { }
+
+        virtual ReadReturn read(void) = 0;
+        virtual void write(double value) = 0;
+
+        std::string GetName(void) const
+        {
+            return _name;
+        }
+
+        /* Returns the configurable timeout period
+         * for this sensor in seconds (undecorated).
+         */
+        int64_t GetTimeout(void) const
+        {
+            return _timeout;
+        }
+
+    private:
+        std::string _name;
+        int64_t _timeout;
+};
+
diff --git a/sysfs/sysfsread.cpp b/sysfs/sysfsread.cpp
new file mode 100644
index 0000000..284aa55
--- /dev/null
+++ b/sysfs/sysfsread.cpp
@@ -0,0 +1,39 @@
+/**
+ * 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 <fstream>
+#include <iostream>
+
+#include "sysfs/sysfsread.hpp"
+
+
+ReadReturn SysFsRead::read(void)
+{
+    int64_t value;
+    std::ifstream ifs;
+
+    ifs.open(_path);
+    ifs >> value;
+    ifs.close();
+
+    struct ReadReturn r = {
+        static_cast<double>(value),
+        std::chrono::high_resolution_clock::now()
+    };
+
+    return r;
+}
diff --git a/sysfs/sysfsread.hpp b/sysfs/sysfsread.hpp
new file mode 100644
index 0000000..0dbc71b
--- /dev/null
+++ b/sysfs/sysfsread.hpp
@@ -0,0 +1,25 @@
+#pragma once
+
+#include <string>
+
+#include "interfaces.hpp"
+#include "sysfs/util.hpp"
+
+
+/*
+ * A ReadInterface that is expecting a path that's sysfs, but really could be
+ * any filesystem path.
+ */
+class SysFsRead : public ReadInterface
+{
+    public:
+        SysFsRead(const std::string& path)
+            : ReadInterface(),
+              _path(FixupPath(path))
+        { }
+
+        ReadReturn read(void) override;
+
+    private:
+        const std::string _path;
+};
diff --git a/sysfs/sysfswrite.cpp b/sysfs/sysfswrite.cpp
new file mode 100644
index 0000000..c3e1b03
--- /dev/null
+++ b/sysfs/sysfswrite.cpp
@@ -0,0 +1,48 @@
+/**
+ * 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 <fstream>
+#include <iostream>
+
+#include "sysfswrite.hpp"
+
+
+void SysFsWritePercent::write(double value)
+{
+    float minimum = getMin();
+    float maximum = getMax();
+
+    float range = maximum - minimum;
+    float offset = range * value;
+    float ovalue = offset + minimum;
+
+    std::ofstream ofs;
+    ofs.open(_writepath);
+    ofs << static_cast<int64_t>(ovalue);
+    ofs.close();
+
+    return;
+}
+
+void SysFsWrite::write(double value)
+{
+    std::ofstream ofs;
+    ofs.open(_writepath);
+    ofs << static_cast<int64_t>(value);
+    ofs.close();
+
+    return;
+}
diff --git a/sysfs/sysfswrite.hpp b/sysfs/sysfswrite.hpp
new file mode 100644
index 0000000..9f7083c
--- /dev/null
+++ b/sysfs/sysfswrite.hpp
@@ -0,0 +1,39 @@
+#pragma once
+
+#include <string>
+
+#include "interfaces.hpp"
+#include "sysfs/util.hpp"
+
+
+/*
+ * A WriteInterface that is expecting a path that's sysfs, but really could be
+ * any filesystem path.
+ */
+class SysFsWritePercent : public WriteInterface
+{
+    public:
+        SysFsWritePercent(std::string& writepath, int64_t min, int64_t max)
+            : WriteInterface(min, max),
+              _writepath(FixupPath(writepath))
+        { }
+
+        void write(double value) override;
+
+    private:
+        std::string _writepath;
+};
+
+class SysFsWrite : public WriteInterface
+{
+    public:
+        SysFsWrite(std::string& writepath, int64_t min, int64_t max)
+            : WriteInterface(min, max),
+              _writepath(FixupPath(writepath))
+        { }
+
+        void write(double value) override;
+
+    private:
+        std::string _writepath;
+};
diff --git a/sysfs/util.cpp b/sysfs/util.cpp
new file mode 100644
index 0000000..c297412
--- /dev/null
+++ b/sysfs/util.cpp
@@ -0,0 +1,71 @@
+/**
+ * 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 <experimental/filesystem>
+#include <iostream>
+#include <string>
+
+
+#include "sysfs/util.hpp"
+
+/*
+ * There are two basic paths I want to support:
+ * 1. /sys/class/hwmon/hwmon0/pwm1
+ * 2. /sys/devices/platform/ahb/1e786000.pwm-tacho-controller/hwmon/<asterisk asterisk>/pwm1
+ *
+ * In this latter case, I want to fill in that gap.  Assuming because it's this
+ * path that it'll only have one directory there.
+ */
+
+static constexpr auto platform = "/sys/devices/platform/";
+namespace fs = std::experimental::filesystem;
+
+
+std::string FixupPath(std::string original)
+{
+    std::string::size_type n, x;
+
+    /* TODO: Consider the merits of using regex for this. */
+    n = original.find("**");
+    x = original.find(platform);
+
+    if ((n != std::string::npos) && (x != std::string::npos))
+    {
+        /* This path has some missing pieces and we support it. */
+        std::string base = original.substr(0, n);
+        std::string fldr;
+        std::string f = original.substr(n + 2, original.size() - (n + 2));
+
+        /* Equivalent to glob and grab 0th entry. */
+        for (const auto& folder : fs::directory_iterator(base))
+        {
+            fldr = folder.path();
+            break;
+        }
+
+        if (!fldr.length())
+        {
+            return original;
+        }
+
+        return fldr + f;
+    }
+    else
+    {
+        /* It'll throw an exception when we use it if it's still bad. */
+        return original;
+    }
+}
diff --git a/sysfs/util.hpp b/sysfs/util.hpp
new file mode 100644
index 0000000..2d4c274
--- /dev/null
+++ b/sysfs/util.hpp
@@ -0,0 +1,6 @@
+#include <string>
+
+/*
+ * Given a path that optionally has a glob portion, fill it out.
+ */
+std::string FixupPath(std::string original);