1 #include "hyp_sys_config.hpp"
2 
3 #include "hyp_network_manager.hpp"
4 
5 #include <phosphor-logging/elog-errors.hpp>
6 #include <phosphor-logging/log.hpp>
7 #include <xyz/openbmc_project/Common/error.hpp>
8 
9 namespace phosphor
10 {
11 namespace network
12 {
13 
14 constexpr auto BIOS_SERVICE = "xyz.openbmc_project.BIOSConfigManager";
15 constexpr auto BIOS_OBJPATH = "/xyz/openbmc_project/bios_config/manager";
16 constexpr auto BIOS_MGR_INTF = "xyz.openbmc_project.BIOSConfig.Manager";
17 
18 using namespace phosphor::logging;
19 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
20 using InvalidArgumentMetadata = xyz::openbmc_project::Common::InvalidArgument;
21 
22 using SysConfigIntf =
23     sdbusplus::xyz::openbmc_project::Network::server::SystemConfiguration;
24 
25 void HypSysConfig::setHostName()
26 {
27     auto name = getHostNameFromBios();
28 
29     SysConfigIntf::hostName(std::move(name));
30 }
31 
32 std::string HypSysConfig::hostName(std::string name)
33 {
34     if (SysConfigIntf::hostName() == name)
35     {
36         return name;
37     }
38 
39     name = SysConfigIntf::hostName(name);
40     setHostNameInBios(name);
41     return name;
42 }
43 
44 std::string HypSysConfig::getHostNameFromBios() const
45 {
46     try
47     {
48         using getAttrRetType =
49             std::tuple<std::string, std::variant<std::string, int64_t>,
50                        std::variant<std::string, int64_t>>;
51         getAttrRetType name;
52         auto method = bus.new_method_call(BIOS_SERVICE, BIOS_OBJPATH,
53                                           BIOS_MGR_INTF, "GetAttribute");
54 
55         method.append("vmi_hostname");
56 
57         auto reply = bus.call(method);
58 
59         std::string type;
60         std::variant<std::string, int64_t> currValue;
61         std::variant<std::string, int64_t> defValue;
62         reply.read(type, currValue, defValue);
63         return std::get<std::string>(currValue);
64     }
65     catch (const sdbusplus::exception::SdBusError& ex)
66     {
67         log<level::ERR>("Failed to get the hostname from bios table",
68                         entry("ERR=%s", ex.what()));
69     }
70     return std::string();
71 }
72 
73 void HypSysConfig::setHostNameInBios(const std::string& name)
74 {
75     auto properties = bus.new_method_call(BIOS_SERVICE, BIOS_OBJPATH,
76                                           BIOS_MGR_INTF, "SetAttribute");
77     properties.append("vmi_hostname");
78     properties.append(std::variant<std::string>(name));
79     auto result = bus.call(properties);
80 
81     if (result.is_method_error())
82     {
83         throw std::runtime_error("Set attribute api failed");
84     }
85 }
86 
87 } // namespace network
88 } // namespace phosphor
89