summaryrefslogtreecommitdiff
path: root/src/d_bus.c
diff options
context:
space:
mode:
authorEvgeniy Alexeev <e.alekseev@k-soft-spb.ru>2022-10-12 11:16:49 +0300
committerEvgeniy Alexeev <e.alekseev@k-soft-spb.ru>2022-10-12 11:16:49 +0300
commit4330d39c35cc44ab1fa5b1e6e1a35207c0635217 (patch)
treef0ba53fe12d3e25acf9a505c05e96c16b4f2cb72 /src/d_bus.c
parentec32cc9ceb8d8f5cb1003360417e5d26f885b8a5 (diff)
downloadsila-shell-master.tar.xz
Add server commandsHEADmaster
Squashed commit of the following: commit 4ca788c95231709813bbe99220f93b4ef3e9c4ef Author: Evgeniy Alexeev <e.alekseev@k-soft-spb.ru> Date: Wed Oct 12 12:19:45 2022 +0500 Add host boot commands, pooulate variant functions commit 21c36beefd0304a600a2f781a60559f647d569fb Author: Evgeniy Alexeev <e.alekseev@k-soft-spb.ru> Date: Mon Oct 10 19:07:45 2022 +0500 Begin to add variant type commit b6f6416fa4dfbff9c677c4491fb42c5d3adb9d44 Author: Evgeniy Alexeev <e.alekseev@k-soft-spb.ru> Date: Mon Oct 10 14:04:30 2022 +0500 Modify status commands in server commit 72405cee84fbddb8866a8a3d96350010266fc137 Author: Evgeniy Alexeev <e.alekseev@k-soft-spb.ru> Date: Mon Oct 10 13:35:37 2022 +0500 Add power&policy status, get sdbus property func commit 81f88de144d6803bf4d53986193bf342772367eb Author: ironcaterpillar <ironcaterpillar@yandex.ru> Date: Sat Oct 8 13:42:22 2022 +0500 Refactor d_bus_commands to separate file commit 224e5b8317e7e3ffbb04ada342447bf678297002 Author: Evgeniy Alexeev <e.alekseev@k-soft-spb.ru> Date: Fri Oct 7 19:18:14 2022 +0500 Add power policy commands, refactor commit 94427ebcf778a2c0cc148c0344002491abdb052c Author: Evgeniy Alexeev <e.alekseev@k-soft-spb.ru> Date: Thu Oct 6 18:23:42 2022 +0500 Add server power commands help commit 6888ffd32af65cb984792a47f65983207c00467e Author: Evgeniy Alexeev <e.alekseev@k-soft-spb.ru> Date: Thu Oct 6 17:31:26 2022 +0500 Add unknown power command error message commit 5d1b4a2000820de903f10959b499c758eba383e0 Author: Evgeniy Alexeev <e.alekseev@k-soft-spb.ru> Date: Thu Oct 6 15:54:37 2022 +0500 Fix host power dbus property name commit 49f8a15abdfb749427bd5769725b3ad54dc0583b Author: Evgeniy Alexeev <e.alekseev@k-soft-spb.ru> Date: Thu Oct 6 13:27:37 2022 +0500 Add .gitignore commit 96569d5f9c7f245fd82545919a1910828c1058d9 Author: Evgeniy Alexeev <e.alekseev@k-soft-spb.ru> Date: Thu Oct 6 13:27:10 2022 +0500 Add sdbus, server power commands
Diffstat (limited to 'src/d_bus.c')
-rw-r--r--src/d_bus.c129
1 files changed, 129 insertions, 0 deletions
diff --git a/src/d_bus.c b/src/d_bus.c
new file mode 100644
index 0000000..76a84fc
--- /dev/null
+++ b/src/d_bus.c
@@ -0,0 +1,129 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <errno.h>
+
+#include <systemd/sd-bus.h>
+
+#include "utils.h"
+#include "d_bus_variant.h"
+#include "d_bus.h"
+
+
+int dbus_set_property_string(const string_quadruple_t* path, const char* value)
+{
+ _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+ _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
+ _cleanup_bus_unref_ sd_bus *bus = NULL;
+
+ int r;
+
+ /* Connect to the system bus */
+ r = sd_bus_open_system(&bus);
+ if (r >= 0)
+ {
+
+ r = sd_bus_set_property(
+ bus,
+ path->first,
+ path->second,
+ path->third,
+ path->fourth,
+ &error,
+ "s",
+ value);
+
+ }
+ if (r < 0)
+ {
+ fprintf(stderr, "Failed to issue dbus call: %d %s\n", r, error.message);
+ }
+
+ return (r>0?(0):(r));
+}
+
+int dbus_set_property(const string_quadruple_t* path, dbus_value_variant_t value)
+{
+ _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+ _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
+ _cleanup_bus_unref_ sd_bus *bus = NULL;
+
+ int r;
+
+ /* Connect to the system bus */
+ r = sd_bus_open_system(&bus);
+ if (r >= 0)
+ {
+
+ if(strcmp(value.type, "s") ==0 )
+ {
+
+ r = sd_bus_set_property(
+ bus,
+ path->first,
+ path->second,
+ path->third,
+ path->fourth,
+ &error,
+ value.type,
+ value.string);
+ }
+ else if(strcmp(value.type, "b") ==0 )
+ {
+
+ r = sd_bus_set_property(
+ bus,
+ path->first,
+ path->second,
+ path->third,
+ path->fourth,
+ &error,
+ value.type,
+ value.boolean);
+ }
+ else {
+ fprintf(stderr, "Failed to find type: %s\n", value.type);
+ return -EOPNOTSUPP;
+ }
+ }
+ if (r < 0)
+ {
+ fprintf(stderr, "Failed to issue dbus call: %d %s\n", r, error.message);
+ }
+
+ return (r>0?(0):(r));
+}
+
+
+char* dbus_get_property_string(const string_quadruple_t* path )
+{
+ _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
+ _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
+ _cleanup_bus_unref_ sd_bus *bus = NULL;
+ char *state = NULL;
+ int r;
+
+ /* Connect to the system bus */
+ r = sd_bus_open_system(&bus);
+ if (r >= 0)
+ {
+
+ r = sd_bus_get_property_string(
+ bus,
+ path->first,
+ path->second,
+ path->third,
+ path->fourth,
+ &error,
+ &state);
+ /* Issue the method call and store the respons message in m */
+ }
+ if (r < 0)
+ {
+ fprintf(stderr, "Failed to issue dbus call: %d %s\n", r, error.message);
+ }
+
+ return (r>=0?(state):(NULL));
+}
+