11bbe3d1eSWilliam A. Kennington III #include "system_configuration.hpp"
21bbe3d1eSWilliam A. Kennington III
31bbe3d1eSWilliam A. Kennington III #include <phosphor-logging/elog-errors.hpp>
4a2947b4bSJagpal Singh Gill #include <phosphor-logging/lg2.hpp>
59ede1b74SWilliam A. Kennington III #include <stdplus/pinned.hpp>
61bbe3d1eSWilliam A. Kennington III #include <xyz/openbmc_project/Common/error.hpp>
71bbe3d1eSWilliam A. Kennington III
81bbe3d1eSWilliam A. Kennington III namespace phosphor
91bbe3d1eSWilliam A. Kennington III {
101bbe3d1eSWilliam A. Kennington III namespace network
111bbe3d1eSWilliam A. Kennington III {
121bbe3d1eSWilliam A. Kennington III
1340c29b54SWilliam A. Kennington III static constexpr char HOSTNAMED_SVC[] = "org.freedesktop.hostname1";
1440c29b54SWilliam A. Kennington III static constexpr char HOSTNAMED_OBJ[] = "/org/freedesktop/hostname1";
1540c29b54SWilliam A. Kennington III static constexpr char HOSTNAMED_INTF[] = "org.freedesktop.hostname1";
161bbe3d1eSWilliam A. Kennington III
1746dda597SRavi Teja using namespace phosphor::logging;
181bbe3d1eSWilliam A. Kennington III using namespace sdbusplus::xyz::openbmc_project::Common::Error;
1946dda597SRavi Teja using Argument = xyz::openbmc_project::Common::InvalidArgument;
201bbe3d1eSWilliam A. Kennington III
2140c29b54SWilliam A. Kennington III static constexpr char propMatch[] =
2240c29b54SWilliam A. Kennington III "type='signal',sender='org.freedesktop.hostname1',"
2340c29b54SWilliam A. Kennington III "path='/org/freedesktop/hostname1',"
2440c29b54SWilliam A. Kennington III "interface='org.freedesktop.DBus.Properties',member='PropertiesChanged',"
2540c29b54SWilliam A. Kennington III "arg0='org.freedesktop.hostname1'";
261bbe3d1eSWilliam A. Kennington III
SystemConfiguration(stdplus::PinnedRef<sdbusplus::bus_t> bus,stdplus::const_zstring objPath)279ede1b74SWilliam A. Kennington III SystemConfiguration::SystemConfiguration(
289ede1b74SWilliam A. Kennington III stdplus::PinnedRef<sdbusplus::bus_t> bus, stdplus::const_zstring objPath) :
29*ad205028SPatrick Williams Iface(bus, objPath.c_str(), Iface::action::defer_emit), bus(bus),
30*ad205028SPatrick Williams hostnamePropMatch(
319ede1b74SWilliam A. Kennington III bus, propMatch,
329ede1b74SWilliam A. Kennington III [sc = stdplus::PinnedRef(*this)](sdbusplus::message_t& m) {
3340c29b54SWilliam A. Kennington III std::string intf;
3440c29b54SWilliam A. Kennington III std::unordered_map<std::string, std::variant<std::string>> values;
3540c29b54SWilliam A. Kennington III try
361bbe3d1eSWilliam A. Kennington III {
3740c29b54SWilliam A. Kennington III m.read(intf, values);
3840c29b54SWilliam A. Kennington III auto it = values.find("Hostname");
3940c29b54SWilliam A. Kennington III if (it == values.end())
4040c29b54SWilliam A. Kennington III {
4140c29b54SWilliam A. Kennington III return;
4240c29b54SWilliam A. Kennington III }
439ede1b74SWilliam A. Kennington III sc.get().Iface::hostName(std::get<std::string>(it->second));
4440c29b54SWilliam A. Kennington III }
4540c29b54SWilliam A. Kennington III catch (const std::exception& e)
4640c29b54SWilliam A. Kennington III {
47*ad205028SPatrick Williams lg2::error("Hostname match parsing failed: {ERROR}", "ERROR",
48*ad205028SPatrick Williams e);
4940c29b54SWilliam A. Kennington III }
5040c29b54SWilliam A. Kennington III })
5140c29b54SWilliam A. Kennington III {
5240c29b54SWilliam A. Kennington III try
5340c29b54SWilliam A. Kennington III {
5440c29b54SWilliam A. Kennington III std::variant<std::string> name;
55*ad205028SPatrick Williams auto req =
56*ad205028SPatrick Williams bus.get().new_method_call(HOSTNAMED_SVC, HOSTNAMED_OBJ,
57*ad205028SPatrick Williams "org.freedesktop.DBus.Properties", "Get");
5840c29b54SWilliam A. Kennington III
5940c29b54SWilliam A. Kennington III req.append(HOSTNAMED_INTF, "Hostname");
609ede1b74SWilliam A. Kennington III auto reply = req.call();
6140c29b54SWilliam A. Kennington III reply.read(name);
6240c29b54SWilliam A. Kennington III SystemConfigIntf::hostName(std::get<std::string>(name), true);
6340c29b54SWilliam A. Kennington III }
6440c29b54SWilliam A. Kennington III catch (const std::exception& e)
6540c29b54SWilliam A. Kennington III {
66a2947b4bSJagpal Singh Gill lg2::error("Failed to get hostname: {ERROR}", "ERROR", e);
6740c29b54SWilliam A. Kennington III }
681bbe3d1eSWilliam A. Kennington III
69d99e6dbaSWilliam A. Kennington III emit_object_added();
701bbe3d1eSWilliam A. Kennington III }
711bbe3d1eSWilliam A. Kennington III
hostName(std::string name)721bbe3d1eSWilliam A. Kennington III std::string SystemConfiguration::hostName(std::string name)
731bbe3d1eSWilliam A. Kennington III {
741bbe3d1eSWilliam A. Kennington III if (SystemConfigIntf::hostName() == name)
751bbe3d1eSWilliam A. Kennington III {
761bbe3d1eSWilliam A. Kennington III return name;
771bbe3d1eSWilliam A. Kennington III }
781bbe3d1eSWilliam A. Kennington III try
791bbe3d1eSWilliam A. Kennington III {
809ede1b74SWilliam A. Kennington III auto method = bus.get().new_method_call(
819ede1b74SWilliam A. Kennington III HOSTNAMED_SVC, HOSTNAMED_OBJ, HOSTNAMED_INTF, "SetStaticHostname");
8240c29b54SWilliam A. Kennington III method.append(name, /*interactive=*/false);
8346dda597SRavi Teja method.call();
8440c29b54SWilliam A. Kennington III return SystemConfigIntf::hostName(std::move(name));
851bbe3d1eSWilliam A. Kennington III }
8646dda597SRavi Teja catch (const sdbusplus::exception::SdBusError& e)
871bbe3d1eSWilliam A. Kennington III {
8846dda597SRavi Teja lg2::error("Failed to set hostname {HOSTNAME}: {ERROR} ", "HOSTNAME",
8946dda597SRavi Teja name, "ERROR", e);
9046dda597SRavi Teja auto dbusError = e.get_error();
9146dda597SRavi Teja if ((dbusError != nullptr) &&
9246dda597SRavi Teja (strcmp(dbusError->name,
9346dda597SRavi Teja "org.freedesktop.DBus.Error.InvalidArgs") == 0))
9446dda597SRavi Teja {
9546dda597SRavi Teja elog<InvalidArgument>(Argument::ARGUMENT_NAME("Hostname"),
9646dda597SRavi Teja Argument::ARGUMENT_VALUE(name.c_str()));
9746dda597SRavi Teja }
981bbe3d1eSWilliam A. Kennington III }
9940c29b54SWilliam A. Kennington III return SystemConfigIntf::hostName();
1001bbe3d1eSWilliam A. Kennington III }
1011bbe3d1eSWilliam A. Kennington III
1021bbe3d1eSWilliam A. Kennington III } // namespace network
1031bbe3d1eSWilliam A. Kennington III } // namespace phosphor
104