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/lg2.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 sdbusplus::xyz::openbmc_project::Common::Error;
19 using InvalidArgumentMetadata = xyz::openbmc_project::Common::InvalidArgument;
20 
21 using SysConfigIntf =
22     sdbusplus::xyz::openbmc_project::Network::server::SystemConfiguration;
23 
setHostName()24 void HypSysConfig::setHostName()
25 {
26     auto name = getHostNameFromBios();
27 
28     SysConfigIntf::hostName(std::move(name));
29 }
30 
hostName(std::string name)31 std::string HypSysConfig::hostName(std::string name)
32 {
33     if (SysConfigIntf::hostName() == name)
34     {
35         return name;
36     }
37 
38     name = SysConfigIntf::hostName(name);
39     setHostNameInBios(name);
40     return name;
41 }
42 
getHostNameFromBios() const43 std::string HypSysConfig::getHostNameFromBios() const
44 {
45     try
46     {
47         using getAttrRetType =
48             std::tuple<std::string, std::variant<std::string, int64_t>,
49                        std::variant<std::string, int64_t>>;
50         getAttrRetType name;
51         auto method = bus.new_method_call(BIOS_SERVICE, BIOS_OBJPATH,
52                                           BIOS_MGR_INTF, "GetAttribute");
53 
54         method.append("vmi_hostname");
55 
56         auto reply = bus.call(method);
57 
58         std::string type;
59         std::variant<std::string, int64_t> currValue;
60         std::variant<std::string, int64_t> defValue;
61         reply.read(type, currValue, defValue);
62         return std::get<std::string>(currValue);
63     }
64     catch (const sdbusplus::exception::SdBusError& ex)
65     {
66         lg2::error("Failed to get the hostname from bios table: {ERROR}",
67                    "ERROR", ex);
68     }
69     return std::string();
70 }
71 
setHostNameInBios(const std::string & name)72 void HypSysConfig::setHostNameInBios(const std::string& name)
73 {
74     auto properties = bus.new_method_call(BIOS_SERVICE, BIOS_OBJPATH,
75                                           BIOS_MGR_INTF, "SetAttribute");
76     properties.append("vmi_hostname");
77     properties.append(std::variant<std::string>(name));
78     auto result = bus.call(properties);
79 
80     if (result.is_method_error())
81     {
82         throw std::runtime_error("Set attribute api failed");
83     }
84 }
85 
86 } // namespace network
87 } // namespace phosphor
88