sd_bus_message_enter_container() "a{sv}" 实例代码
时间: 2024-05-13 16:17:38 浏览: 114
以下是一个使用`sd_bus_message_enter_container()`函数解析DBus消息中类型为"a{sv}"(即字典)的示例代码:
```c
#include <systemd/sd-bus.h>
#include <stdio.h>
int main() {
sd_bus *bus = NULL;
sd_bus_message *msg = NULL;
sd_bus_error error = SD_BUS_ERROR_NULL;
int r;
// Connect to the system bus
r = sd_bus_open_system(&bus);
if (r < 0) {
fprintf(stderr, "Failed to connect to system bus: %s\n", strerror(-r));
goto finish;
}
// Call a method on a remote object
r = sd_bus_call_method(bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"ListUnits",
&error,
&msg,
"");
if (r < 0) {
fprintf(stderr, "Failed to call ListUnits: %s\n", error.message);
goto finish;
}
// Parse the response message
r = sd_bus_message_enter_container(msg, 'a', "{sv}");
if (r < 0) {
fprintf(stderr, "Failed to enter dictionary container: %s\n", strerror(-r));
goto finish;
}
const char *name;
sd_bus_message *dict_msg;
r = sd_bus_message_read(msg, "{&s^v}", &name, &dict_msg);
while (r > 0) {
printf("Unit: %s\n", name);
// Parse the dictionary message
r = sd_bus_message_enter_container(dict_msg, 'e', NULL);
if (r < 0) {
fprintf(stderr, "Failed to enter dictionary entry container: %s\n", strerror(-r));
goto finish;
}
const char *key;
sd_bus_message *variant_msg;
r = sd_bus_message_read(dict_msg, "{&s^v}", &key, &variant_msg);
while (r > 0) {
printf(" %s: ", key);
// Parse the variant message
r = sd_bus_message_enter_container(variant_msg, 'v', NULL);
if (r < 0) {
fprintf(stderr, "Failed to enter variant container: %s\n", strerror(-r));
goto finish;
}
uint64_t value;
r = sd_bus_message_read(variant_msg, "t", &value);
if (r < 0) {
fprintf(stderr, "Failed to read uint64_t: %s\n", strerror(-r));
goto finish;
}
printf("%llu\n", value);
r = sd_bus_message_exit_container(variant_msg);
if (r < 0) {
fprintf(stderr, "Failed to exit variant container: %s\n", strerror(-r));
goto finish;
}
r = sd_bus_message_read(dict_msg, "{&s^v}", &key, &variant_msg);
}
if (r < 0) {
fprintf(stderr, "Failed to read dictionary entry: %s\n", strerror(-r));
goto finish;
}
r = sd_bus_message_exit_container(dict_msg);
if (r < 0) {
fprintf(stderr, "Failed to exit dictionary entry container: %s\n", strerror(-r));
goto finish;
}
r = sd_bus_message_read(msg, "{&s^v}", &name, &dict_msg);
}
if (r < 0) {
fprintf(stderr, "Failed to read dictionary: %s\n", strerror(-r));
goto finish;
}
r = sd_bus_message_exit_container(msg);
if (r < 0) {
fprintf(stderr, "Failed to exit dictionary container: %s\n", strerror(-r));
goto finish;
}
finish:
sd_bus_error_free(&error);
sd_bus_message_unref(msg);
sd_bus_unref(bus);
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}
```
此代码连接到系统DBus总线,调用`org.freedesktop.systemd1.Manager.ListUnits`方法,并解析返回的字典数据。在解析字典时,它首先使用`sd_bus_message_enter_container()`进入字典容器,然后使用`sd_bus_message_read()`读取字典键值对。对于每个字典键值对,它使用`sd_bus_message_enter_container()`进入变体容器,读取值,然后使用`sd_bus_message_exit_container()`退出变体容器。最后,它使用`sd_bus_message_exit_container()`退出字典容器。
阅读全文