Create a new Dbus interface for LED

A new Dbus API method is created in the phosphor-led-sysfs repository
under xyz.openbmc_project.Led.Sysfs.Internal interface name to add or
remove the LED, which will be coming from each udev LED event to
create the LED dbus path.

xyz.openbmc_project.Led.Sysfs.Internal interface
.AddLED                                method
.RemoveLED                             method

This Dbus API method is added to support the multihost physical
LEDs design.
https://gerrit.openbmc.org/c/openbmc/docs/+/55230

Also support a executable for LED DBUS API method

Added a new executable for LED DBUS API method to communicate
between udev and application.

Executable will call Dbus API method to pass LED name as argument from
udev, after the primary service started.

Tested : Tested the dbus method is invoked for each LED udev event
in Facebook YosemiteV2 platform.

Signed-off-by: Jayashree Dhanapal <jayashree-d@hcl.com>
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
Change-Id: I3fa6c3caa130b2b71ebc9fe8d69541c029f516ab
Signed-off-by: Alexander Hansen <alexander.hansen@9elements.com>
diff --git a/controller.cpp b/controller.cpp
index 695f174..2c02aca 100644
--- a/controller.cpp
+++ b/controller.cpp
@@ -14,145 +14,22 @@
  * limitations under the License.
  */
 
-#include "physical.hpp"
-#include "sysfs.hpp"
+#include "interfaces/internal_interface.hpp"
 
-#include <CLI/CLI.hpp>
-#include <boost/algorithm/string.hpp>
-
-#include <algorithm>
-#include <iostream>
-#include <string>
-
-struct LedDescr
+int main()
 {
-    std::string devicename;
-    std::string color;
-    std::string function;
-};
-
-/** @brief parse LED name in sysfs
- *  Parse sysfs LED name in format "devicename:colour:function"
- *  or "devicename:colour" or "devicename" and sets corresponding
- *  fields in LedDescr struct.
- *
- *  @param[in] name      - LED name in sysfs
- *  @param[out] ledDescr - LED description
- */
-void getLedDescr(const std::string& name, LedDescr& ledDescr)
-{
-    std::vector<std::string> words;
-    // FIXME: https://bugs.llvm.org/show_bug.cgi?id=41141
-    // NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks)
-    boost::split(words, name, boost::is_any_of(":"));
-    // NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
-    try
-    {
-        ledDescr.devicename = words.at(0);
-        ledDescr.color = words.at(1);
-        ledDescr.function = words.at(2);
-    }
-    catch (const std::out_of_range&)
-    {
-        return;
-    }
-}
-
-/** @brief generates LED DBus name from LED description
- *
- *  @param[in] name      - LED description
- *  @return              - DBus LED name
- */
-std::string getDbusName(const LedDescr& ledDescr)
-{
-    std::vector<std::string> words;
-    words.emplace_back(ledDescr.devicename);
-    if (!ledDescr.function.empty())
-    {
-        words.emplace_back(ledDescr.function);
-    }
-    if (!ledDescr.color.empty())
-    {
-        words.emplace_back(ledDescr.color);
-    }
-    return boost::join(words, "_");
-}
-
-int main(int argc, char** argv)
-{
-    namespace fs = std::filesystem;
-    static constexpr auto busParent = "xyz.openbmc_project.LED.Controller";
-    static constexpr auto objParent = "/xyz/openbmc_project/led/physical";
-    static constexpr auto devParent = "/sys/class/leds/";
-
-    CLI::App app{"Control and Drive the physical LEDs"};
-
-    // Read arguments.
-    std::string path{};
-    app.add_option("-p,--path", path,
-                   "absolute path of LED in sysfs; like /sys/class/leds/<name>")
-        ->required();
-
-    // Parse out Path argument.
-    try
-    {
-        app.parse(argc, argv);
-    }
-    catch (const CLI::Error& e)
-    {
-        return app.exit(e);
-    }
-
-    // If the LED has a hyphen in the name like: "one-two", then it gets passed
-    // as /one/two/ as opposed to /one-two to the service file. There is a
-    // change needed in systemd to solve this issue and hence putting in this
-    // work-around.
-
-    // Since this application always gets invoked as part of a udev rule,
-    // it is always guaranteed to get /sys/class/leds/one/two
-    // and we can go beyond leds/ to get the actual LED name.
-    // Refer: systemd/systemd#5072
-
-    // On an error, this throws an exception and terminates.
-    auto name = path.substr(strlen(devParent));
-
-    // LED names may have a hyphen and that would be an issue for
-    // dbus paths and hence need to convert them to underscores.
-    std::replace(name.begin(), name.end(), '/', '-');
-    path = devParent + name;
-
-    // Convert to lowercase just in case some are not and that
-    // we follow lowercase all over
-    std::transform(name.begin(), name.end(), name.begin(), ::tolower);
-
-    // LED names may have a hyphen and that would be an issue for
-    // dbus paths and hence need to convert them to underscores.
-    std::replace(name.begin(), name.end(), '-', '_');
-
-    // Convert LED name in sysfs into DBus name
-    LedDescr ledDescr;
-    getLedDescr(name, ledDescr);
-
-    name = getDbusName(ledDescr);
-
-    // Unique bus name representing a single LED.
-    auto busName = std::string(busParent) + '.' + name;
-    auto objPath = std::string(objParent) + '/' + name;
-
-    // Get a handle to system dbus.
+    // Get a handle to system dbus
     auto bus = sdbusplus::bus::new_default();
 
-    sdbusplus::server::manager_t manager{bus, objPath.c_str()};
+    // Add the ObjectManager interface
+    sdbusplus::server::manager_t objManager(bus, ledPath);
 
-    // Create the Physical LED objects for directing actions.
-    // Need to save this else sdbusplus destructor will wipe this off.
-    auto sled = std::make_unique<phosphor::led::SysfsLed>(fs::path(path));
-    phosphor::led::Physical led(bus, objPath, std::move(sled), ledDescr.color);
+    // Create an led controller object
+    phosphor::led::sysfs::interface::InternalInterface internal(bus, ledPath);
 
-    /** @brief Claim the bus */
-    bus.request_name(busName.c_str());
+    // Request service bus name
+    bus.request_name(busName);
 
-    /** @brief Wait for client requests */
     while (true)
     {
         // Handle dbus message / signals discarding unhandled