1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3 
4 #include "sd-bus.h"
5 
6 #include "macro.h"
7 
8 int bus_property_get_bool(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *error);
9 int bus_property_set_bool(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *value, void *userdata, sd_bus_error *error);
10 int bus_property_get_id128(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *error);
11 
12 #define bus_property_get_usec ((sd_bus_property_get_t) NULL)
13 #define bus_property_set_usec ((sd_bus_property_set_t) NULL)
14 
15 assert_cc(sizeof(int) == sizeof(int32_t));
16 #define bus_property_get_int ((sd_bus_property_get_t) NULL)
17 
18 assert_cc(sizeof(unsigned) == sizeof(uint32_t));
19 #define bus_property_get_unsigned ((sd_bus_property_get_t) NULL)
20 
21 /* On 64bit machines we can use the default serializer for size_t and
22  * friends, otherwise we need to cast this manually */
23 #if __SIZEOF_SIZE_T__ == 8
24 #define bus_property_get_size ((sd_bus_property_get_t) NULL)
25 #else
26 int bus_property_get_size(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *error);
27 #endif
28 
29 #if __SIZEOF_LONG__ == 8
30 #define bus_property_get_long ((sd_bus_property_get_t) NULL)
31 #define bus_property_get_ulong ((sd_bus_property_get_t) NULL)
32 #else
33 int bus_property_get_long(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *error);
34 int bus_property_get_ulong(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *error);
35 #endif
36 
37 /* uid_t and friends on Linux 32 bit. This means we can just use the
38  * default serializer for 32bit unsigned, for serializing it, and map
39  * it to NULL here */
40 assert_cc(sizeof(uid_t) == sizeof(uint32_t));
41 #define bus_property_get_uid ((sd_bus_property_get_t) NULL)
42 
43 assert_cc(sizeof(gid_t) == sizeof(uint32_t));
44 #define bus_property_get_gid ((sd_bus_property_get_t) NULL)
45 
46 assert_cc(sizeof(pid_t) == sizeof(uint32_t));
47 #define bus_property_get_pid ((sd_bus_property_get_t) NULL)
48 
49 assert_cc(sizeof(mode_t) == sizeof(uint32_t));
50 #define bus_property_get_mode ((sd_bus_property_get_t) NULL)
51 
52 int bus_property_get_rlimit(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *error);
53 
54 #define BUS_DEFINE_PROPERTY_GET_GLOBAL(function, bus_type, val)         \
55         int function(sd_bus *bus,                                       \
56                      const char *path,                                  \
57                      const char *interface,                             \
58                      const char *property,                              \
59                      sd_bus_message *reply,                             \
60                      void *userdata,                                    \
61                      sd_bus_error *error) {                             \
62                                                                         \
63                 assert(bus);                                            \
64                 assert(reply);                                          \
65                                                                         \
66                 return sd_bus_message_append(reply, bus_type, val);     \
67         }
68 
69 #define BUS_DEFINE_PROPERTY_GET2(function, bus_type, data_type, get1, get2) \
70         int function(sd_bus *bus,                                       \
71                      const char *path,                                  \
72                      const char *interface,                             \
73                      const char *property,                              \
74                      sd_bus_message *reply,                             \
75                      void *userdata,                                    \
76                      sd_bus_error *error) {                             \
77                                                                         \
78                 data_type *data = userdata;                             \
79                                                                         \
80                 assert(bus);                                            \
81                 assert(reply);                                          \
82                 assert(data);                                           \
83                                                                         \
84                 return sd_bus_message_append(reply, bus_type,           \
85                                              get2(get1(data)));         \
86         }
87 
88 #define ident(x) (x)
89 #define BUS_DEFINE_PROPERTY_GET(function, bus_type, data_type, get1) \
90         BUS_DEFINE_PROPERTY_GET2(function, bus_type, data_type, get1, ident)
91 
92 #define ref(x) (*(x))
93 #define BUS_DEFINE_PROPERTY_GET_REF(function, bus_type, data_type, get) \
94         BUS_DEFINE_PROPERTY_GET2(function, bus_type, data_type, ref, get)
95 
96 #define BUS_DEFINE_PROPERTY_GET_ENUM(function, name, type)              \
97         BUS_DEFINE_PROPERTY_GET_REF(function, "s", type, name##_to_string)
98 
99 #define BUS_PROPERTY_DUAL_TIMESTAMP(name, offset, flags) \
100         SD_BUS_PROPERTY(name, "t", bus_property_get_usec, (offset) + offsetof(struct dual_timestamp, realtime), (flags)), \
101         SD_BUS_PROPERTY(name "Monotonic", "t", bus_property_get_usec, (offset) + offsetof(struct dual_timestamp, monotonic), (flags))
102