Skip to content

Commit b3948c5

Browse files
committed
hostname1 + describe + glaze
-add pattern to use glaze for de-serializing describe contents Signed-off-by: Joel Winarske <joel.winarske@gmail.com>
1 parent 70a1b5f commit b3948c5

File tree

3 files changed

+185
-64
lines changed

3 files changed

+185
-64
lines changed

src/hostname1/hostname1_client.cc

Lines changed: 130 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ Hostname1Client::Hostname1Client(sdbus::IConnection& connection)
2222
: ProxyInterfaces{connection, sdbus::ServiceName(INTERFACE_NAME),
2323
sdbus::ObjectPath(OBJECT_PATH)} {
2424
registerProxy();
25-
auto description = Describe();
26-
// Parse description as JSON and print key: value lines
27-
std::string parsed = Utils::parseDescriptionJson(description);
28-
spdlog::info("Hostname1 description:\n{}\nParsed description:\n{}",
29-
description, parsed);
25+
26+
if (const std::string description = Describe(); !description.empty()) {
27+
if (const auto s = glz::read_json<Hostname1>(description)) {
28+
printHostname1(s.value());
29+
}
30+
}
3031
}
3132

3233
Hostname1Client::~Hostname1Client() {
@@ -73,8 +74,8 @@ void Hostname1Client::updateHostname1(
7374
hostname1_.OperatingSystemCPEName = value.get<std::string>();
7475
} else if (key == "OperatingSystemSupportEnd") {
7576
hostname1_.OperatingSystemSupportEnd = value.get<uint64_t>();
76-
} else if (key == "HomeURL") {
77-
hostname1_.HomeURL = value.get<std::string>();
77+
} else if (key == "OperatingSystemHomeURL") {
78+
hostname1_.OperatingSystemHomeURL = value.get<std::string>();
7879
} else if (key == "HardwareVendor") {
7980
hostname1_.HardwareVendor = value.get<std::string>();
8081
} else if (key == "HardwareModel") {
@@ -86,9 +87,9 @@ void Hostname1Client::updateHostname1(
8687
} else if (key == "FirmwareDate") {
8788
hostname1_.FirmwareDate = value.get<uint64_t>();
8889
} else if (key == "MachineID") {
89-
hostname1_.MachineID = value.get<std::vector<uint8_t>>();
90+
MachineID_ = value.get<std::vector<uint8_t>>();
9091
} else if (key == "BootID") {
91-
hostname1_.BootID = value.get<std::vector<uint8_t>>();
92+
BootID_ = value.get<std::vector<uint8_t>>();
9293
}
9394
}
9495
}
@@ -98,29 +99,32 @@ void Hostname1Client::printHostname1() const {
9899
if (!hostname1_.Hostname.empty()) {
99100
os << "\tHostname: " << hostname1_.Hostname << std::endl;
100101
}
101-
if (!hostname1_.StaticHostname.empty()) {
102+
if (hostname1_.StaticHostname.empty()) {
102103
os << "\tStaticHostname: " << hostname1_.StaticHostname << std::endl;
103104
}
104-
if (!hostname1_.PrettyHostname.empty()) {
105-
os << "\tPrettyHostname: " << hostname1_.PrettyHostname << std::endl;
105+
if (hostname1_.PrettyHostname.has_value()) {
106+
os << "\tPrettyHostname: " << hostname1_.PrettyHostname.value()
107+
<< std::endl;
106108
}
107-
if (!hostname1_.DefaultHostname.empty()) {
108-
os << "\tDefaultHostname: " << hostname1_.DefaultHostname << std::endl;
109+
if (hostname1_.DefaultHostname.has_value()) {
110+
os << "\tDefaultHostname: " << hostname1_.DefaultHostname.value()
111+
<< std::endl;
109112
}
110-
if (!hostname1_.HostnameSource.empty()) {
111-
os << "\tHostnameSource: " << hostname1_.HostnameSource << std::endl;
113+
if (hostname1_.HostnameSource.has_value()) {
114+
os << "\tHostnameSource: " << hostname1_.HostnameSource.value()
115+
<< std::endl;
112116
}
113-
if (!hostname1_.IconName.empty()) {
114-
os << "\tIconName: " << hostname1_.IconName << std::endl;
117+
if (hostname1_.IconName.has_value()) {
118+
os << "\tIconName: " << hostname1_.IconName.value() << std::endl;
115119
}
116-
if (!hostname1_.Chassis.empty()) {
117-
os << "\tChassis: " << hostname1_.Chassis << std::endl;
120+
if (hostname1_.Chassis.has_value()) {
121+
os << "\tChassis: " << hostname1_.Chassis.value() << std::endl;
118122
}
119-
if (!hostname1_.Deployment.empty()) {
120-
os << "\tDeployment: " << hostname1_.Deployment << std::endl;
123+
if (hostname1_.Deployment.has_value()) {
124+
os << "\tDeployment: " << hostname1_.Deployment.value() << std::endl;
121125
}
122-
if (!hostname1_.Location.empty()) {
123-
os << "\tLocation: " << hostname1_.Location << std::endl;
126+
if (hostname1_.Location.has_value()) {
127+
os << "\tLocation: " << hostname1_.Location.value() << std::endl;
124128
}
125129
if (!hostname1_.KernelName.empty()) {
126130
os << "\tKernelName: " << hostname1_.KernelName << std::endl;
@@ -143,39 +147,127 @@ void Hostname1Client::printHostname1() const {
143147
os << "\tOperatingSystemSupportEnd: "
144148
<< hostname1_.OperatingSystemSupportEnd.value() << std::endl;
145149
}
146-
if (!hostname1_.HomeURL.empty()) {
147-
os << "\tHomeURL: " << hostname1_.HomeURL << std::endl;
150+
if (hostname1_.OperatingSystemHomeURL.has_value()) {
151+
os << "\tOperatingSystemHomeUrl: "
152+
<< hostname1_.OperatingSystemHomeURL.value() << std::endl;
148153
}
149-
if (!hostname1_.HardwareVendor.empty()) {
150-
os << "\tHardwareVendor: " << hostname1_.HardwareVendor << std::endl;
154+
if (hostname1_.HardwareVendor.has_value()) {
155+
os << "\tHardwareVendor: " << hostname1_.HardwareVendor.value()
156+
<< std::endl;
151157
}
152-
if (!hostname1_.HardwareModel.empty()) {
153-
os << "\tHardwareModel: " << hostname1_.HardwareModel << std::endl;
158+
if (hostname1_.HardwareModel.has_value()) {
159+
os << "\tHardwareModel: " << hostname1_.HardwareModel.value() << std::endl;
154160
}
155-
if (!hostname1_.FirmwareVersion.empty()) {
156-
os << "\tFirmwareVersion: " << hostname1_.FirmwareVersion << std::endl;
161+
if (hostname1_.FirmwareVersion.has_value()) {
162+
os << "\tFirmwareVersion: " << hostname1_.FirmwareVersion.value()
163+
<< std::endl;
157164
}
158-
if (!hostname1_.FirmwareVendor.empty()) {
159-
os << "\tFirmwareVendor: " << hostname1_.FirmwareVendor << std::endl;
165+
if (hostname1_.FirmwareVendor.has_value()) {
166+
os << "\tFirmwareVendor: " << hostname1_.FirmwareVendor.value()
167+
<< std::endl;
160168
}
161169
if (hostname1_.FirmwareDate.has_value()) {
162170
os << "\tFirmwareDate: " << hostname1_.FirmwareDate.value() << std::endl;
163171
}
164-
if (hostname1_.MachineID.has_value()) {
172+
if (MachineID_.has_value()) {
165173
os << "\tMachineID: ";
166-
for (const auto& b : hostname1_.MachineID.value()) {
174+
for (const auto& b : MachineID_.value()) {
167175
os << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(b)
168176
<< " ";
169177
}
170178
os << std::endl;
171179
}
172-
if (hostname1_.BootID.has_value()) {
180+
if (BootID_.has_value()) {
173181
os << "\tBootID: ";
174-
for (const auto& b : hostname1_.BootID.value()) {
182+
for (const auto& b : BootID_.value()) {
175183
os << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(b)
176184
<< " ";
177185
}
178186
os << std::endl;
179187
}
180188
spdlog::info("\n{}", os.str());
189+
}
190+
191+
void Hostname1Client::printHostname1(const Hostname1& val) {
192+
std::ostringstream os;
193+
if (!val.Hostname.empty()) {
194+
os << "\tHostname: " << val.Hostname << std::endl;
195+
}
196+
if (val.StaticHostname.empty()) {
197+
os << "\tStaticHostname: " << val.StaticHostname << std::endl;
198+
}
199+
if (val.PrettyHostname.has_value()) {
200+
os << "\tPrettyHostname: " << val.PrettyHostname.value() << std::endl;
201+
}
202+
if (val.DefaultHostname.has_value()) {
203+
os << "\tDefaultHostname: " << val.DefaultHostname.value() << std::endl;
204+
}
205+
if (val.HostnameSource.has_value()) {
206+
os << "\tHostnameSource: " << val.HostnameSource.value() << std::endl;
207+
}
208+
if (val.IconName.has_value()) {
209+
os << "\tIconName: " << val.IconName.value() << std::endl;
210+
}
211+
if (val.Chassis.has_value()) {
212+
os << "\tChassis: " << val.Chassis.value() << std::endl;
213+
}
214+
if (val.Deployment.has_value()) {
215+
os << "\tDeployment: " << val.Deployment.value() << std::endl;
216+
}
217+
if (val.Location.has_value()) {
218+
os << "\tLocation: " << val.Location.value() << std::endl;
219+
}
220+
if (!val.KernelName.empty()) {
221+
os << "\tKernelName: " << val.KernelName << std::endl;
222+
}
223+
if (!val.KernelRelease.empty()) {
224+
os << "\tKernelRelease: " << val.KernelRelease << std::endl;
225+
}
226+
if (!val.KernelVersion.empty()) {
227+
os << "\tKernelVersion: " << val.KernelVersion << std::endl;
228+
}
229+
if (!val.OperatingSystemPrettyName.empty()) {
230+
os << "\tOperatingSystemPrettyName: " << val.OperatingSystemPrettyName
231+
<< std::endl;
232+
}
233+
if (val.OperatingSystemReleaseData.has_value()) {
234+
os << "\tOperatingSystemReleaseData: " << std::endl;
235+
for (const auto& item : val.OperatingSystemReleaseData.value()) {
236+
os << "\t\t" << item << std::endl;
237+
}
238+
}
239+
if (!val.OperatingSystemCPEName.empty()) {
240+
os << "\tOperatingSystemCPEName: " << val.OperatingSystemCPEName
241+
<< std::endl;
242+
}
243+
if (val.OperatingSystemSupportEnd.has_value()) {
244+
os << "\tOperatingSystemSupportEnd: "
245+
<< val.OperatingSystemSupportEnd.value() << std::endl;
246+
}
247+
if (val.OperatingSystemHomeURL.has_value()) {
248+
os << "\tOperatingSystemHomeURL: " << val.OperatingSystemHomeURL.value()
249+
<< std::endl;
250+
}
251+
if (val.HardwareVendor.has_value()) {
252+
os << "\tHardwareVendor: " << val.HardwareVendor.value() << std::endl;
253+
}
254+
if (val.HardwareModel.has_value()) {
255+
os << "\tHardwareModel: " << val.HardwareModel.value() << std::endl;
256+
}
257+
if (val.FirmwareVersion.has_value()) {
258+
os << "\tFirmwareVersion: " << val.FirmwareVersion.value() << std::endl;
259+
}
260+
if (val.FirmwareVendor.has_value()) {
261+
os << "\tFirmwareVendor: " << val.FirmwareVendor.value() << std::endl;
262+
}
263+
if (val.FirmwareDate.has_value()) {
264+
os << "\tFirmwareDate: " << val.FirmwareDate.value() << std::endl;
265+
}
266+
if (val.MachineID.has_value()) {
267+
os << "\tMachineID: " << val.MachineID.value() << std::endl;
268+
}
269+
if (val.BootID.has_value()) {
270+
os << "\tBootID: " << val.BootID.value() << std::endl;
271+
}
272+
spdlog::info("\n{}", os.str());
181273
}

src/hostname1/hostname1_client.h

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
//{"Hostname":"air","StaticHostname":"air","PrettyHostname":null,"DefaultHostname":"fedora","HostnameSource":"static","IconName":"computer-desktop","Chassis":"desktop","ChassisAssetTag":null,"Deployment":null,"Location":null,"KernelName":"Linux","KernelRelease":"6.17.7-300.fc43.x86_64","KernelVersion":"#1
2+
//SMP PREEMPT_DYNAMIC Sun Nov 2 15:30:09 UTC
3+
//2025","OperatingSystemPrettyName":"Fedora Linux 43 (Workstation
4+
//Edition)","OperatingSystemCPEName":"cpe:/o:fedoraproject:fedora:43","OperatingSystemHomeURL":"https://fedoraproject.org/","OperatingSystemSupportEnd":1796169600000000,"OperatingSystemReleaseData":["NAME=Fedora
5+
//Linux","VERSION=43 (Workstation
6+
//Edition)","RELEASE_TYPE=stable","ID=fedora","VERSION_ID=43","VERSION_CODENAME=","PRETTY_NAME=Fedora
7+
//Linux 43 (Workstation
8+
//Edition)","ANSI_COLOR=0;38;2;60;110;180","LOGO=fedora-logo-icon","CPE_NAME=cpe:/o:fedoraproject:fedora:43","DEFAULT_HOSTNAME=fedora","HOME_URL=https://fedoraproject.org/","DOCUMENTATION_URL=https://docs.fedoraproject.org/en-US/fedora/f43/","SUPPORT_URL=https://ask.fedoraproject.org/","BUG_REPORT_URL=https://bugzilla.redhat.com/","REDHAT_BUGZILLA_PRODUCT=Fedora","REDHAT_BUGZILLA_PRODUCT_VERSION=43","REDHAT_SUPPORT_PRODUCT=Fedora","REDHAT_SUPPORT_PRODUCT_VERSION=43","SUPPORT_END=2026-12-02","VARIANT=Workstation
9+
//Edition","VARIANT_ID=workstation"],"OperatingSystemImageID":null,"OperatingSystemImageVersion":null,"MachineInformationData":[],"HardwareVendor":"ASUS","HardwareModel":"ROG
10+
//STRIX X670E-E GAMING
11+
//WIFI","HardwareSerial":null,"HardwareSKU":"SKU","HardwareVersion":"System
12+
//Version","FirmwareVersion":"2308","FirmwareVendor":"American Megatrends
13+
//Inc.","FirmwareDate":1725494400000000,"MachineID":"e4ae95483e814b74855a3a74bf347d61","BootID":"f049c41b9e5f4adcb8dd06008adfa47b","ProductUUID":null,"VSockCID":null}
114
// Copyright (c) 2025 Joel Winarske
215
//
316
// Licensed under the Apache License, Version 2.0 (the "License");
@@ -26,45 +39,59 @@ class Hostname1Client final
2639
static constexpr auto INTERFACE_NAME = "org.freedesktop.hostname1";
2740
static constexpr auto OBJECT_PATH = "/org/freedesktop/hostname1";
2841

29-
explicit Hostname1Client(sdbus::IConnection& connection);
30-
31-
virtual ~Hostname1Client();
32-
33-
void updateHostname1(
34-
const std::map<sdbus::PropertyName, sdbus::Variant>& changedProperties);
35-
36-
void printHostname1() const;
37-
38-
struct hostname1 {
42+
struct Hostname1 {
3943
std::string Hostname;
4044
std::string StaticHostname;
41-
std::string PrettyHostname;
42-
std::string DefaultHostname;
43-
std::string HostnameSource;
44-
std::string IconName;
45-
std::string Chassis;
46-
std::string Deployment;
47-
std::string Location;
45+
std::optional<std::string> PrettyHostname;
46+
std::optional<std::string> DefaultHostname;
47+
std::optional<std::string> HostnameSource;
48+
std::optional<std::string> IconName;
49+
std::optional<std::string> Chassis;
50+
std::optional<std::string> ChassisAssetTag;
51+
std::optional<std::string> Deployment;
52+
std::optional<std::string> Location;
4853
std::string KernelName;
4954
std::string KernelRelease;
5055
std::string KernelVersion;
5156
std::string OperatingSystemPrettyName;
5257
std::string OperatingSystemCPEName;
58+
std::optional<std::string> OperatingSystemHomeURL;
5359
std::optional<uint64_t> OperatingSystemSupportEnd;
54-
std::string HomeURL;
55-
std::string HardwareVendor;
56-
std::string HardwareModel;
57-
std::string FirmwareVersion;
58-
std::string FirmwareVendor;
60+
std::optional<std::vector<std::string>> OperatingSystemReleaseData;
61+
std::optional<std::string> OperatingSystemImageID;
62+
std::optional<std::string> OperatingSystemImageVersion;
63+
std::optional<std::vector<std::string>> MachineInformationData;
64+
std::optional<std::string> HardwareVendor;
65+
std::optional<std::string> HardwareModel;
66+
std::optional<std::string> HardwareSerial;
67+
std::optional<std::string> HardwareSKU;
68+
std::optional<std::string> HardwareVersion;
69+
std::optional<std::string> FirmwareVersion;
70+
std::optional<std::string> FirmwareVendor;
5971
std::optional<uint64_t> FirmwareDate;
60-
std::optional<std::vector<uint8_t>> MachineID;
61-
std::optional<std::vector<uint8_t>> BootID;
72+
std::optional<std::string> MachineID;
73+
std::optional<std::string> BootID;
74+
std::optional<std::string> ProductUUID;
75+
std::optional<std::string> VSockCID;
6276
};
6377

64-
[[nodiscard]] const hostname1& getHostname1() const { return hostname1_; }
78+
explicit Hostname1Client(sdbus::IConnection& connection);
79+
80+
virtual ~Hostname1Client();
81+
82+
void updateHostname1(
83+
const std::map<sdbus::PropertyName, sdbus::Variant>& changedProperties);
84+
85+
void printHostname1() const;
86+
static void printHostname1(const Hostname1& val);
87+
88+
[[nodiscard]] const Hostname1& getHostname1() const { return hostname1_; }
6589

6690
private:
67-
hostname1 hostname1_{};
91+
Hostname1 hostname1_{};
92+
93+
std::optional<std::vector<uint8_t>> MachineID_;
94+
std::optional<std::vector<uint8_t>> BootID_;
6895

6996
void onPropertiesChanged(
7097
const sdbus::InterfaceName& interfaceName,

src/hostname1/main.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#include "hostname1_client.h"
1616

17+
#include "../utils/utils.h"
18+
1719
int main() {
1820
const auto connection = sdbus::createSystemBusConnection();
1921
connection->enterEventLoopAsync();

0 commit comments

Comments
 (0)