blob: 853ee11ade77c73969d43dece3f3ded54ff00afe [file] [log] [blame]
Asmitha Karunanithieb40f082021-07-22 06:13:04 -05001#include "hyp_sys_config.hpp"
2
3#include "hyp_network_manager.hpp"
4
5#include <phosphor-logging/elog-errors.hpp>
Jagpal Singh Gill7eb07c22023-04-17 12:10:32 -07006#include <phosphor-logging/lg2.hpp>
Asmitha Karunanithieb40f082021-07-22 06:13:04 -05007#include <xyz/openbmc_project/Common/error.hpp>
8
9namespace phosphor
10{
11namespace network
12{
13
14constexpr auto BIOS_SERVICE = "xyz.openbmc_project.BIOSConfigManager";
15constexpr auto BIOS_OBJPATH = "/xyz/openbmc_project/bios_config/manager";
16constexpr auto BIOS_MGR_INTF = "xyz.openbmc_project.BIOSConfig.Manager";
17
Asmitha Karunanithieb40f082021-07-22 06:13:04 -050018using namespace sdbusplus::xyz::openbmc_project::Common::Error;
19using InvalidArgumentMetadata = xyz::openbmc_project::Common::InvalidArgument;
20
21using SysConfigIntf =
22 sdbusplus::xyz::openbmc_project::Network::server::SystemConfiguration;
23
24void HypSysConfig::setHostName()
25{
26 auto name = getHostNameFromBios();
27
28 SysConfigIntf::hostName(std::move(name));
29}
30
31std::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
43std::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 {
Jagpal Singh Gill7eb07c22023-04-17 12:10:32 -070066 lg2::error("Failed to get the hostname from bios table: {ERROR}",
67 "ERROR", ex);
Asmitha Karunanithieb40f082021-07-22 06:13:04 -050068 }
69 return std::string();
70}
71
72void 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