我试图在系统总线上创建一个带有复杂签名的属性,在我的示例中使用两个带符号的整数。源文件test.c:
#include <stdio.h>
#include <stdint.h>
#include <systemd/sd-bus.h>
#include <systemd/sd-bus-vtable.h>
#define DATA_INTERFACE "com.myexample.myproject"
#define DATA_PATH "/com/myexample/myproject"
#define DATA_PROPERTY "data"
#define DATA_SIGNATURE "ii"
static int get_property_cb(sd_bus*_bus,
const char* path,
const char* interface,
const char* property,
sd_bus_message* reply,
void* userdata,
sd_bus_error* ret_error)
{
int ret = 1;
sd_bus_message_open_container(reply, 'r', DATA_SIGNATURE);
sd_bus_message_append(reply, "i", 111);
sd_bus_message_append(reply, "i", 222);
sd_bus_message_close_container(reply);
return ret;
}
static const sd_bus_vtable prop_spec[3] = {
SD_BUS_VTABLE_START(0),
SD_BUS_PROPERTY(DATA_PROPERTY, DATA_SIGNATURE, get_property_cb, 0, SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
SD_BUS_VTABLE_END
};
int main ()
{
sd_bus* _bus = NULL;
sd_bus_slot* _slot = NULL;
int ret = 0;
ret = sd_bus_open_system(&_bus);
if(ret < 0) {
fprintf(stderr, "Error: sd_bus_open ret %d\n", ret);
return -1;
}
ret = sd_bus_request_name(_bus, DATA_INTERFACE, 0);
if(ret < 0) {
fprintf(stderr, "Error: sd_bus_request_name ret %d\n", ret);
return -1;
}
ret = sd_bus_add_object_vtable(_bus, &_slot, DATA_PATH, DATA_INTERFACE, prop_spec, NULL);
if(ret < 0) {
fprintf(stderr, "Error: sd_bus_add_object_vtable ret %d\n", ret);
return -1;
}
printf("!!!Success!!!\n");
sd_bus_slot_unref(_slot);
sd_bus_unref(_bus);
return 0;
}此外,我还创建了文件/usr/share/dbus-1/system.d/com.myexample.myproject.conf (还试图将其放到/etc/dbus-1/system.d/)
<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-Bus Bus Configuration 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>
<policy context="default">
<allow own="com.myexample.myproject"/>
<allow send_interface="*"/>
</policy>
</busconfig>接口文件/usr/share/dbus-1/interfaces/com.myexample.myproject.xml
<!DOCTYPE node PUBLIC
"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"
"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd" >
<node xmlns:doc="http://www.freedesktop.org/dbus/1.0/doc.dtd">
<interface name="com.myexample.myproject">
<property name="data" type="ii" access="read">
<annotation name="org.freedesktop.DBus.Property.EmitsChangedSignal" value="true"/>
</property>
</interface>
</node>和/usr/share/dbus-1/services/com.myexample.myproject.service
[D-BUS Service]
Name=com.myexample.myproject
Exec=/home/user/test我把它作为root运行:
sudo ./test折磨我的大脑为什么sd_bus_add_object_vtable回来-22 (-EINVAL)
有人知道怎么回事吗?
发布于 2020-03-23 14:10:14
签名必须写在括号内:
#define DATA_SIGNATURE "(ii)"另外,/usr/share/dbus-1/services/com.myexample.myproject.service和/usr/share/dbus-1/interfaces/com.myexample.myproject.xml文件也是不必要的
https://stackoverflow.com/questions/60756071
复制相似问题