presence: Hard code JSON config file locations
Instead of using locations provided at configure time to retrieve the
JSON config file, hard code the directory paths and the dbus
interface/property used. This simplifies what's required to be given at
configure time and eliminates complexities in the possible locations the
JSON config files could be stored in.
The expected value retrieved from dbus is a relative location to the
base path /usr/share/phosphor-fan-presence/presence/[DBUS_VALUE]
Tested:
Config file is able to loaded from each location
Dbus property value used relative to base path location
Change-Id: I831647f78887b3948ce35be4015fd7762748838d
Signed-off-by: Matthew Barth <msbarth@us.ibm.com>
diff --git a/presence/json_config.cpp b/presence/json_config.cpp
index 59d452a..deccbf1 100644
--- a/presence/json_config.cpp
+++ b/presence/json_config.cpp
@@ -18,12 +18,15 @@
#include <fstream>
#include <nlohmann/json.hpp>
#include <phosphor-logging/log.hpp>
+#include <sdbusplus/bus.hpp>
#include "json_config.hpp"
+#include "config.h"
#include "tach.hpp"
#include "gpio.hpp"
#include "anyof.hpp"
#include "fallback.hpp"
+#include "sdbusplus.hpp"
namespace phosphor
{
@@ -48,19 +51,13 @@
{"fallback", rpolicy::getFallback}
};
-JsonConfig::JsonConfig(const std::string& jsonConfigPath) :
- _defaultFile(fs::path(fs::path{jsonConfigPath} / jsonFileName))
+JsonConfig::JsonConfig(sdbusplus::bus::bus& bus) :
+ _bus(bus)
{
- if (!_defaultFile.empty())
- {
- // Load and process the json configuration
- load();
- }
- else
- {
- log<level::ERR>("No JSON config file provided");
- throw std::runtime_error("No JSON config file provided");
- }
+ // Determine the configuration file to use
+ _confFile = getConfFile();
+ // Load and process the json configuration
+ load();
}
const policies& JsonConfig::get()
@@ -73,6 +70,10 @@
{
try
{
+ // Determine the configuration file to use
+ _confFile = getConfFile();
+ log<level::INFO>("Loading configuration",
+ entry("JSON_FILE=%s", _confFile.c_str()));
// Load and process the json configuration
load();
for (auto& p: _policies)
@@ -88,24 +89,69 @@
}
}
-void JsonConfig::load()
+const fs::path JsonConfig::getConfFile()
{
- fs::path confFile{fs::path{jsonOverridePath} / jsonFileName};
- if (!fs::exists(confFile))
+ // Check override location
+ fs::path confFile = fs::path{confOverridePath} / confFileName;
+ if (fs::exists(confFile))
{
- confFile = _defaultFile;
+ return confFile;
+ }
+
+ // Check base path location
+ confFile = fs::path{confBasePath} / confFileName;
+ if (fs::exists(confFile))
+ {
+ return confFile;
+ }
+
+ // Check dbus interface & property
+ // Use first object returned in the subtree
+ // (Should really be only one object with the config interface)
+ auto objects = util::SDBusPlus::getSubTreeRaw(_bus, "/", confDbusIntf, 0);
+ auto itObj = objects.begin();
+ if (itObj != objects.end())
+ {
+ auto itServ = itObj->second.begin();
+ if (itServ != itObj->second.end())
+ {
+ // Retrieve json config relative path location from dbus
+ auto relPathLoc = util::SDBusPlus::getProperty<std::string>(
+ _bus, itServ->first, itObj->first,
+ confDbusIntf, confDbusProp);
+ confFile = fs::path{confBasePath} / relPathLoc / confFileName;
+ if (!fs::exists(confFile))
+ {
+ log<level::ERR>("No JSON config file found",
+ entry("NO_FILE=%s", confFile.c_str()));
+ throw std::runtime_error("No JSON config file found");
+ }
+ }
+ else
+ {
+ log<level::ERR>("No JSON config file found",
+ entry("NO_SERVICE=%s", itObj->first.c_str()));
+ throw std::runtime_error("No JSON config file found");
+ }
}
else
{
- log<level::INFO>("Loading alternate configuration",
- entry("JSON_FILE=%s", confFile.c_str()));
+ log<level::ERR>("No JSON config file found",
+ entry("NO_INTERFACE=%s", confDbusIntf));
+ throw std::runtime_error("No JSON config file found");
}
+
+ return confFile;
+}
+
+void JsonConfig::load()
+{
std::ifstream file;
json jsonConf;
- if (fs::exists(confFile))
+ if (fs::exists(_confFile))
{
- file.open(confFile);
+ file.open(_confFile);
try
{
jsonConf = json::parse(file);
@@ -113,7 +159,7 @@
catch (std::exception& e)
{
log<level::ERR>("Failed to parse JSON config file",
- entry("JSON_FILE=%s", confFile.c_str()),
+ entry("JSON_FILE=%s", _confFile.c_str()),
entry("JSON_ERROR=%s", e.what()));
throw std::runtime_error("Failed to parse JSON config file");
}
@@ -121,7 +167,7 @@
else
{
log<level::ERR>("Unable to open JSON config file",
- entry("JSON_FILE=%s", confFile.c_str()));
+ entry("JSON_FILE=%s", _confFile.c_str()));
throw std::runtime_error("Unable to open JSON config file");
}