Created
August 14, 2023 12:36
-
-
Save sumantro93/107dfa371b1ed06000326172fbc0d2e1 to your computer and use it in GitHub Desktop.
dbus_listen_recv.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <dbus/dbus.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
void send_signal(DBusConnection *conn) { | |
DBusMessage *msg; | |
DBusMessageIter args; | |
msg = dbus_message_new_signal("/com/example/TestObject", // object name | |
"com.example.TestSignal.Type", // interface name | |
"Test"); // name of the signal | |
if (NULL == msg) { | |
fprintf(stderr, "Message Null\n"); | |
exit(1); | |
} | |
dbus_connection_send(conn, msg, NULL); | |
dbus_connection_flush(conn); | |
printf("Signal Sent!\n"); | |
dbus_message_unref(msg); | |
} | |
void receive_signal(DBusConnection *conn) { | |
dbus_bus_add_match(conn, "type='signal',interface='com.example.TestSignal.Type'", NULL); | |
dbus_connection_flush(conn); | |
while (1) { | |
dbus_connection_read_write(conn, 0); | |
DBusMessage *msg = dbus_connection_pop_message(conn); | |
if (msg == NULL) { | |
sleep(1); | |
continue; | |
} | |
if (dbus_message_is_signal(msg, "com.example.TestSignal.Type", "Test")) { | |
printf("Received signal!\n"); | |
} | |
dbus_message_unref(msg); | |
} | |
} | |
int main() { | |
DBusError err; | |
DBusConnection *conn; | |
dbus_error_init(&err); | |
conn = dbus_bus_get(DBUS_BUS_SESSION, &err); | |
if (dbus_error_is_set(&err)) { | |
fprintf(stderr, "Connection Error: %s\n", err.message); | |
dbus_error_free(&err); | |
return 1; | |
} | |
send_signal(conn); | |
receive_signal(conn); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment