DBus: Watch when name disappears from bus - c

I am implimenting the StatusNotifierWatcher service using the low-level DBus C library. The StatusNotifierWatcher specification requires that the watcher can know when "A StatusNotifierItem instance has disappeared from the bus" So that it can send the StatusNotifierItemUnregistered signal.
An example implimentation:
#include <stdio.h>
#include <stdlib.h>
#include <dbus/dbus.h>
DBusConnection *conn = NULL;
void item_unregistered_signal(const char *name) {
DBusMessage *signal = dbus_message_new_signal(
"/org/freedesktop/StatusNotifierWatcher",
"org.freedesktop.StatusNotifierWatcher",
"StatusNotifierItemUnregistered");
dbus_message_append_args(signal,
DBUS_TYPE_STRING, &name,
DBUS_TYPE_INVALID);
dbus_connection_send(conn, signal, NULL);
dbus_message_unref(signal);
}
void watch_name(const char *name, void(*cb)(const char *)) {
// Not sure how to impliment
}
dbus_bool_t register_item(DBusConnection *connection, DBusMessage *message, void *_data) {
DBusError error;
char *name;
if (!dbus_message_get_args(message, &error,
DBUS_TYPE_STRING, &name,
DBUS_TYPE_INVALID)) {
fprintf(stderr, "Error parsing method args: %s\n", error.message);
return FALSE;
}
watch_name(name, item_unregistered_signal);
return TRUE;
}
static void check_and_abort(DBusError *error) {
if (dbus_error_is_set(error)) {
fprintf(stderr, "dbus_err: %s\n", error->message);
exit(EXIT_FAILURE);
}
}
int main() {
DBusError error;
dbus_error_init(&error);
conn = dbus_bus_get(DBUS_BUS_SESSION, &error);
check_and_abort(&error);
dbus_bus_request_name(conn, "org.freedesktop.StatusNotifierWatcher",
DBUS_NAME_FLAG_REPLACE_EXISTING,
&error);
check_and_abort(&error);
dbus_connection_add_filter(conn, register_item, NULL, free);
while(1) {
dbus_connection_read_write_dispatch(conn, 1000);
}
}
If I have the well-known name to a DBus service, how do I know when the name disappears from the bus?

Well, I figured this out and I'll post an answer for any future poor souls who need to work with libdbus.
org.freedesktop.DBus sends the NameOwnerChanged signal whenever any DBus name changes. One can use this signal to track if an item has disappeared because the NewOwner argument is a null string.
This function can do that:
static DBusHandlerResult signal_handler(DBusConnection *connection,
DBusMessage *message, void *_usr_data) {
if (dbus_message_is_signal(message, "org.freedesktop.DBus",
"NameOwnerChanged")) {
const char *name;
const char *old_owner;
const char *new_owner;
if (!dbus_message_get_args(message, NULL,
DBUS_TYPE_STRING, &name,
DBUS_TYPE_STRING, &old_owner,
DBUS_TYPE_STRING, &new_owner,
DBUS_TYPE_INVALID)) {
fprintf(stderr, "Error getting OwnerChanged args");
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
if (strcmp(name, "") != 0) {
// Name not lost, just swapped owners
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
name_lost(name);
return DBUS_HANDLER_RESULT_HANDLED;
}
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
One also needs to add a match so that your program will be called with this signal. I added this in main()
dbus_bus_add_match(conn,
"type='signal',\
sender='org.freedesktop.DBus',\
interface='org.freedesktop.DBus',\
member='NameOwnerChanged'",
&error);
check_and_abort(&error);
dbus_connection_add_filter(conn, signal_handler, NULL, free);

For those using GDBus (recommended) instead of libdbus (discouraged), the equivalent is g_bus_watch_name().

Related

DBus: Connection was disconnected before a reply was received

I want to send notifications to my window manager on linux through the org.freedesktop.Notifications service. In d-feet this works, but in C, I get the error "Connection was disconnected before a reply was received"
#include <stdio.h>
#include <dbus/dbus.h>
int main() {
DBusError error;
DBusConnection *conn;
dbus_uint32_t serial = 0;
char *app_name = "My App";
dbus_uint32_t replaces_id = 0;
char *app_icon = "";
char *summary = "Notification from My App";
char *body = "This is a test notification from My App.";
char **actions = NULL;
dbus_int32_t timeout = -1;
dbus_uint32_t notification_id = 0;
dbus_error_init(&error);
conn = dbus_bus_get(DBUS_BUS_SESSION, &error);
if (dbus_error_is_set(&error)) {
fprintf(stderr, "Error connecting to the D-Bus session bus: %s\n", error.message);
dbus_error_free(&error);
return 1;
}
DBusMessage *message = dbus_message_new_method_call("org.freedesktop.Notifications",
"/org/freedesktop/Notifications",
"org.freedesktop.Notifications",
"Notify");
DBusMessageIter args, array;
dbus_message_iter_init_append(message, &args);
dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &app_name);
dbus_message_iter_append_basic(&args, DBUS_TYPE_UINT32, &notification_id);
dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &app_icon);
dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &summary);
dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &body);
dbus_message_iter_open_container(&args, DBUS_TYPE_ARRAY, DBUS_TYPE_STRING_AS_STRING, &array);
dbus_message_iter_close_container(&args, &array);
DBusMessageIter dict_entry;
const char *key = "";
dbus_uint32_t value = 0;
dbus_message_iter_open_container(&args, DBUS_TYPE_DICT_ENTRY, NULL, &dict_entry);
dbus_message_iter_append_basic(&dict_entry, DBUS_TYPE_STRING, &key);
DBusMessageIter variant;
dbus_message_iter_open_container(&dict_entry, DBUS_TYPE_VARIANT, DBUS_TYPE_UINT32_AS_STRING, &variant);
dbus_message_iter_append_basic(&variant, DBUS_TYPE_UINT32, &value);
dbus_message_iter_close_container(&dict_entry, &variant);
dbus_message_iter_close_container(&args, &dict_entry);
dbus_message_iter_append_basic(&args, DBUS_TYPE_INT32, &timeout);
DBusPendingCall* pending;
dbus_connection_send_with_reply(conn, message, &pending, DBUS_TIMEOUT_INFINITE);
dbus_connection_flush(conn);
dbus_message_unref(message);
dbus_pending_call_block(pending);
message = dbus_pending_call_steal_reply(pending);
dbus_pending_call_unref(pending);
DBusMessageIter args2;
int arg, id;
if(!dbus_message_iter_init(message, &args2))
fprintf(stderr, "message has no arugments!\n");
else if (DBUS_TYPE_UINT32 != (arg=dbus_message_iter_get_arg_type(&args2))) {
fprintf(stderr, "Argument is not int!\n");
switch(arg){
case DBUS_TYPE_STRING:
char *string;
dbus_message_iter_get_basic(&args2, &string);
fprintf(stderr, "ITS A STRING: %s\n", string);
break;
}}
else {
dbus_message_iter_get_basic(&args2, &id);
printf("ID: %d\n", id);
}
dbus_message_unref(message);
dbus_connection_close(conn);
return 0;
}
Edit: I fixed the segfaults, but now I get a even more obsucure reply from dbus when reading the reply arguments
While this doesn’t directly answer your question, it will help in the long run: You probably don’t want to use dbus-glib or libdbus to connect to D-Bus. Both are old and have poor API designs which make them hard to use. Using a D-Bus binding with a better-designed API will make your code easier to write and maintain in the long run.
See this answer for a comparison of the D-Bus bindings available to you which are easier to use. It is unfortunately quite confusing that there are so many bindings and that some of them are not recommended.

C Code, RPC Error "RPC: Can't encode arguments"

I saw this question but it doesn't seem to apply.
Heres my function which does most the heavy lifting in the client code:
void
database_1(char *host, char *action, char *message)
{
printf("Action: %s\n", action);
printf("Message: %s\n", message);
CLIENT *clnt;
rpc_args *result_1;
//struct rpc_args action_1_arg;
//rpc arguments struct to pass to server
struct rpc_args *args = malloc(sizeof(struct rpc_args));
char *id = generate_id();
if (strcmp(action, "GET") == 0) {
printf("Client: GET request\n");
strcpy(args->action, action);
strcpy(args->id, id);
} else if(strcmp(action, "PUT") == 0) {
printf("Client: PUT request\n");
strcpy(args->action, action);
strcpy(args->id, id);
strcpy(args->message.content, message);
}
#ifndef DEBUG
clnt = clnt_create (host, DATABASE, ASSIGNMENT_7, "udp");
if (clnt == NULL) {
clnt_pcreateerror (host);
exit (1);
}
#endif /* DEBUG */
result_1 = action_1(args, clnt);
if (result_1 == (rpc_args *) NULL) {
clnt_perror (clnt, "call failed");
}
free(args);
#ifndef DEBUG
clnt_destroy (clnt);
#endif /* DEBUG */
}
Here's my output:
./database_client eecslinab3.case.edu GET
running client, main
Action: GET
Message: (null)
hostname is eecslinab3
The process id is 24697
The unique id is eecslinab324697
Client: GET request
call failed: RPC: Can't encode arguments
Database.x
struct message {
char content[2000];
};
struct rpc_args {
char action[20];
char id[1024];
struct message message;
};
program DATABASE {
version ASSIGNMENT_7 {
rpc_args ACTION(struct rpc_args) = 1;
} = 1;
} = 0x20fff100;
A friend helped me solve the problem but didn't give me much details about why or how the fix works. Basically I reduced the char array sizes in my struct and it worked. Has something to do with the limit of data you can send over UDP.
struct rpc_args {
char action[20];
char id[80];
char message[80];
};
program DATABASE {
version ASSIGNMENT_7 {
rpc_args ACTION(struct rpc_args) = 1;
} = 1;
} = 0x20fff100;

dbus c program - send(with reply) and receive using method_call

I'm new to D-Bus. I want a c program to send and receive data using the dbus_message_new_method_call function. I have tried the following programs from the link How to reply a D-Bus message but I'm getting error in the server.c side like "the name client.signal.Object was not provided by any .service files"
"server.c"
/* server.c */
#include <dbus/dbus.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
static DBusHandlerResult
filter_func(DBusConnection *connection, DBusMessage *message, void *usr_data)
{
DBusMessage *reply;
dbus_bool_t handled = false;
char *word = NULL;
DBusError dberr;
dbus_error_init(&dberr);
dbus_message_get_args(message, &dberr, DBUS_TYPE_STRING, &word, DBUS_TYPE_INVALID);
printf("receive message: %s\n", word);
handled = true;
reply = dbus_message_new_method_return(message);
char * reply_content;
printf("\nEnter your Reply Msg : ");
scanf("%s",reply_content);
dbus_message_append_args(reply, DBUS_TYPE_STRING, &reply_content, DBUS_TYPE_INVALID);
dbus_connection_send(connection, reply, NULL);
dbus_connection_flush(connection);
dbus_message_unref(reply);
return (handled ? DBUS_HANDLER_RESULT_HANDLED : DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
}
int main(int argc, char *argv[])
{
DBusError dberr;
DBusConnection *dbconn;
dbus_error_init(&dberr);
dbconn = dbus_bus_get(DBUS_BUS_SESSION, &dberr);
if (!dbus_connection_add_filter(dbconn, filter_func, NULL, NULL))
{
return -1;
}
dbus_bus_add_match(dbconn, "type='method_call',interface='client.signal.Type'", &dberr);
while(dbus_connection_read_write_dispatch(dbconn, -1))
{
/* loop */
}
return 0;
}
here client.c
#include <dbus/dbus.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
static DBusHandlerResult
filter_func(DBusConnection *connection, DBusMessage *message, void *usr_data)
{
dbus_bool_t handled = false;
char *word = NULL;
DBusError dberr;
dbus_error_init(&dberr);
dbus_message_get_args(message, &dberr, DBUS_TYPE_STRING, &word, DBUS_TYPE_INVALID);
printf("receive message %s\n", word);
handled = true;
return (handled ? DBUS_HANDLER_RESULT_HANDLED : DBUS_HANDLER_RESULT_NOT_YET_HANDLED);
}
int db_send(DBusConnection *dbconn)
{
DBusMessage *dbmsg;
char *word = (char *)malloc(sizeof(char));
int i;
dbmsg = dbus_message_new_method_call("client.signal.Object","/client/signal/Object", "client.signal.Type", "Test");
scanf("%s", word);
if (!dbus_message_append_args(dbmsg, DBUS_TYPE_STRING, &word, DBUS_TYPE_INVALID))
{
return -1;
}
if (!dbus_connection_send(dbconn, dbmsg, NULL))
{
return -1;
}
dbus_connection_flush(dbconn);
printf("send message %s\n", word);
dbus_message_unref(dbmsg);
return 0;
}
int main(int argc, char *argv[])
{
DBusError dberr;
DBusConnection *dbconn;
dbus_error_init(&dberr);
dbconn = dbus_bus_get(DBUS_BUS_SESSION, &dberr);
if (!dbus_connection_add_filter(dbconn, filter_func, NULL, NULL))
{
return -1;
}
db_send(dbconn);
while(dbus_connection_read_write_dispatch(dbconn, -1))
{
db_send(dbconn);
}
dbus_connection_unref(dbconn);
return 0;
}
Please help me to fix.
Have a look here: http://www.freedesktop.org/wiki/IntroductionToDBus/
From what it looks like you need to write a file "*.service" that describes the service.
(Quoted from the website)
# (Lines starting with hash marks are comments)
# Fixed section header (do not change):
[D-BUS Service]
Names=com.bigmoneybank.Deposits;com.bigmoneybank.Withdrawals
Exec=/usr/local/bin/bankcounter
This is what I found within 5 minutes of Googleing.

Connecting to Bluetooth Device using bluetooth_client_connect_service() - gnome-bluetooth 3.8.2.1

I would be glad if I can be pointed in the right direction the community concerning my above topic.
I am interested in connecting to bluetooth devices using the gnome-bluetooth api in ubuntu 14.04 using the bluetooth_client_connect_service() function.
I have tried searching but could not find good results on how to use it so I decided to read the gnome-bluetooth's source code but due to insufficient commenting I am unable to understand.
Below is what I have done so far but I do not get any errors when I try running my application yet when I double-click on a device it does nothing.
#define AGENT_PATH "/org/bluez/agent/wizard"
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <signal.h>
#include <math.h>
#include <glib.h>
//#include <dbus/dbus.h>
#include <glib/gi18n.h>
#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>
#include <bluetooth-chooser.h>
#include <bluetooth-client.h>
#include <bluetooth-utils.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
#define CONNECT_TIMEOUT 3.0
#define AGENT_PATH "/org/bluez/agent/wizard"
typedef struct {
char *path;
GTimer *timer;
} ConnectData;
BluetoothClient *client;
GtkWidget *selector;
GtkWidget *vboxMainLayout;
GValue value = { 0, };
int find_conn(int s, int dev_id, long arg)
{
struct hci_conn_list_req *cl;
struct hci_conn_info *ci;
int i;
if (!(cl = malloc(10 * sizeof(*ci) + sizeof(*cl)))) {
perror("Can't allocate memory");
exit(1);
}
cl->dev_id = dev_id;
cl->conn_num = 10;
ci = cl->conn_info;
if (ioctl(s, HCIGETCONNLIST, (void *) cl)) {
perror("Can't get connection list");
exit(1);
}
for (i = 0; i < cl->conn_num; i++, ci++)
if (!bacmp((bdaddr_t *) arg, &ci->bdaddr)) {
free(cl);
return 1;
}
free(cl);
return 0;
}
void cmd_rssi(const char *bt_address)
{
struct hci_conn_info_req *cr;
bdaddr_t bdaddr;
int8_t rssi;
int dd, dev_id;
str2ba(bt_address, &bdaddr);
dev_id = hci_for_each_dev(HCI_UP, find_conn, (long) &bdaddr);
if (dev_id < 0) {
g_print("\tNot connected.\n");
return;
}
dd = hci_open_dev(dev_id);
if (dd < 0) {
perror("HCI device open failed");
exit(1);
}
cr = malloc(sizeof(*cr) + sizeof(struct hci_conn_info));
if (!cr) {
perror("Can't allocate memory");
exit(1);
}
bacpy(&cr->bdaddr, &bdaddr);
cr->type = ACL_LINK;
if (ioctl(dd, HCIGETCONNINFO, (unsigned long) cr) < 0) {
perror("Get connection info failed");
exit(1);
}
if (hci_read_rssi(dd, htobs(cr->conn_info->handle), &rssi, 1000) < 0) {
perror("Read RSSI failed");
exit(1);
}
g_print("\tRSSI return value: %d\n", rssi);
free(cr);
hci_close_dev(dd);
}
void connect_callback (GObject *source_object, GAsyncResult *res, gpointer user_data)
{
ConnectData *data = (ConnectData *) user_data;
gboolean success;
success = bluetooth_client_connect_service_finish (client, res, NULL);
if (success == FALSE && g_timer_elapsed (data->timer, NULL) < CONNECT_TIMEOUT) {
bluetooth_client_connect_service (client, data->path, TRUE, NULL, connect_callback, data);
return;
}
if (success == FALSE)
g_print ("\tFailed to connect to device %s", data->path);
else
g_print("\n\tConnection successfully.. ha.. I'm tired\n");
g_timer_destroy (data->timer);
g_free (data->path);
g_free (data);
}
void create_callback (BluetoothClient *_client, const char *path, const GError *error, gpointer user_data)
{
ConnectData *data;
//compiler throws "implicit declaration" warning here
//bluetooth_client_set_trusted(client, path, TRUE);
data = g_new0 (ConnectData, 1);
data->path = g_strdup (path);
data->timer = g_timer_new ();
bluetooth_client_connect_service (client, path, TRUE, NULL, connect_callback, data);
}
void get_device_info(BluetoothChooser *self)
{
const gchar* result;
g_print ("Info dumped:\n");
if (bluetooth_chooser_get_selected_device_info (self, "name", &value)) {
g_print ("\tName: '%s'\n", g_value_get_string (&value));
g_value_unset (&value);
}
if (bluetooth_chooser_get_selected_device_info (self, "address", &value)) {
g_print ("\tAddress: '%s'\n", g_value_get_string (&value));
g_value_unset (&value);
}
if (bluetooth_chooser_get_selected_device_info (self, "paired", &value)) {
result = g_value_get_boolean (&value)? "Paired":"Unpaired";
g_print ("\tPaired: '%s'\n", result);
g_value_unset (&value);
}
guint type = bluetooth_chooser_get_selected_device_type (self);
const gchar *device_type = bluetooth_type_to_string(type);
if(type)
{
g_print("\tType: '%s'\n", device_type);
}
if (bluetooth_chooser_get_selected_device_info (self, "connected", &value)) {
result = g_value_get_boolean (&value)? "Connected":"Not Connected";
g_print ("\tConnected: '%s'\n", result);
g_value_unset (&value);
}
}
/* My problem lies here.. how to connect to the detected device
* no error message is displayed when a device is double-clicked
*/
void connect_button_clicked(GtkWidget *widget, gpointer user_data)
{
const char *path = AGENT_PATH;
ConnectData *data = (ConnectData *) user_data;
GValue value = { 0, };
bluetooth_chooser_get_selected_device_info (widget, "address", &value);
bluetooth_client_connect_service (client, path, TRUE, NULL, connect_callback, data);
//function to get the rssi value of the remote device
cmd_rssi(g_value_get_string (&value));
}
void create_interface(GtkApplication *app, gpointer user_data)
{
GtkWidget *frmTopWindow;
frmTopWindow = gtk_application_window_new(app);
gtk_window_set_title(GTK_WINDOW(frmTopWindow), "Test");
gtk_window_set_position(GTK_WINDOW(frmTopWindow),GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(frmTopWindow),200,400);
selector = bluetooth_chooser_new();
g_object_set(selector,
"show-searching", TRUE,
"show-device-type", FALSE,
"show-pairing" , TRUE,
"show-device-category", FALSE,
NULL);
client = bluetooth_client_new();
vboxMainLayout = gtk_box_new(GTK_ORIENTATION_VERTICAL, 3);
g_object_set(vboxMainLayout,
"width-request", 190,
"height-request", 300, NULL);
gtk_container_add(GTK_CONTAINER(frmTopWindow),selector);
/*Events and Signals*/
/*------------------*/
// When user double-clicks on a detected device, try and connect to that device
// and display it's RSSI
g_signal_connect(G_OBJECT(selector),"notify::selected-device-activated",G_CALLBACK(connect_button_clicked),client);
//When user clicks on a detected device, display information about that device in the
// standard output
g_signal_connect(G_OBJECT(selector),"notify::device-selected",G_CALLBACK(get_device_info),vboxMainLayout);
gtk_widget_show_all(frmTopWindow);
}
int main(int argc, char** argv)
{
GtkApplication *app;
int status;
app = gtk_application_new ("rucst.project.test", G_APPLICATION_FLAGS_NONE);
g_signal_connect (app, "activate", G_CALLBACK (create_interface), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}
In my implementation code, I have a g_signal_connect function that calls it as shown below:
g_signal_connect(G_OBJECT(selector),"notify::selected-device-activated",G_CALLBACK(connect_button_clicked),client);
This code I expect to connect to the selected detected device when it receives a double-click signal but at the moment nothing happens when I double-click on it.
I would be very grateful to receive guidance from the experts.
Thank you in advance
g_signal_connect(G_OBJECT(selector),"notify::selected-device-activated",
G_CALLBACK(connect_button_clicked),client);
This signal signature would be used if there was a property "selected-device-activated" and you wanted to know when the property value changes. But in this case "selected-device-activated" is an actual signal so you should just do:
g_signal_connect(G_OBJECT(selector),"selected-device-activated",
G_CALLBACK(callback), client);
The single click version just happens to work because you've found a property that does what you want, so connecting to "notify::device-selected" works (I'd still connect to the "selected-device-changed" signal for consistency instead).
After that in connect_button_clicked() your connect call uses what seems like a an unrelated path as the device object path... Something like this might work instead (although I'm not 100% sure about the "proxy" field name, I've not used this API myself):
GValue value = { 0, };
if (bluetooth_chooser_get_selected_device_info (BLUETOOTH_CHOOSER (widget),
"proxy", &value)) {
GDBusProxy *proxy = g_value_get_object (&value);
g_print ("Connecting to %s\n", g_dbus_proxy_get_object_path(proxy));
bluetooth_client_connect_service (client, g_dbus_proxy_get_object_path(proxy),
TRUE, NULL, connect_callback, data);
}
Note also that the userdata pointers you give to the callbacks are wrong: e.g. connect_button_clicked() expects a ConnectData* but gets a BluetoothClient*.

Print out response of Dbus Method Call in C

The problem I am having is specifically printing out the response of a dbus method call in C using the low level API. I am new to C's libdbus, but have done some work in python-dbus.
I know how to write dbus methods and method calls in python as well as the CLI
I can find code on the internet to invoke dbus methods, but they don't return or print out the response
I have been looking at the libdbus doxygen api, but cannot determine how to pull out the response.
The way I have my code set up, a python dbus daemon runs with methods I want to call. Some of them return a string. I want a C program to connect to the session bus, call the method, print out the reply and exit.
This is what I have currently:
#include <stdio.h>
#include <dbus/dbus.h>
static void send_dbus_message (DBusConnection *connection, const char *msg)
{
DBusMessage *message;
//initialize the message
message = dbus_message_new_signal ("/org/example/foo/bar",
"org.example.foo.bar",
msg);
//send the message
dbus_connection_send (connection, message, NULL);
//deallocate the message
dbus_message_unref (message);
}
int main (int argc, char **argv)
{
DBusConnection *connection;
DBusError error;
//init error message
dbus_error_init (&error);
connection = dbus_bus_get (DBUS_BUS_SESSION, &error);
if (!connection)
{
printf ("Connection to D-BUS daemon failed: %s", error.message);
//deallocate error message
dbus_error_free (&error);
return 1;
}
send_dbus_message (connection, "HelloWorld");
return 0;
}
Can be synchronous or asynchronous.
You can use the method mentioned in http://www.matthew.ath.cx/misc/dbus to get a method reply message.
Once you have a dbus message you can use following method to extract the data.
To parse a dbus message, you need a argument iterator. Initalize it to read contents of the incoming message.
DBusMessageIter MsgIter;
dbus_message_iter_init(msg, &MsgIter);//msg is pointer to dbus message received
You have to validate the signature of the incoming message before reading it. Or you can also go for argument by argument verification. For example, if the argument type is string
if (DBUS_TYPE_STRING == dbus_message_iter_get_arg_type(&MsgIter)){
char* str = NULL;
dbus_message_iter_get_basic(&MsgIter, &str);//this function is used to read basic dbus types like int, string etc.
}
For complex types, like structures, arrays, variants and dict entries, you have to create corresponding child iterators to parse contents of each complex element. Say, for a dbus signature ofs(i{ii}i)u, the extraction is done as below
//Parsing a signature s(i{ii}i)u
DBusMessageIter rootIter;
dbus_message_iter_init(msg, &rootIter);
if (DBUS_TYPE_STRING == dbus_message_iter_get_arg_type(&rootIter))
{
char* str = NULL;
dbus_message_iter_get_basic(&rootIter, &str);//this function is used to read basic dbus types like int, string etc.
dbus_message_iter_next(&rootIter);//Go to next argument of root iter
//Block to enter and read structure
if (DBUS_TYPE_STRUCT == dbus_message_iter_get_arg_type(&rootIter))
{
DBusMessageIter structIter;
dbus_message_iter_recurse(&rootIter, &structIter);//Initialize iterator for struct
//Argument 1 is int32
if (DBUS_TYPE_INT32 == dbus_message_iter_get_arg_type(&structIter))
{
int a;
dbus_message_iter_get_basic(&structIter, &a);//Read integer
dbus_message_iter_next(&structIter);//Go to next argument of structiter
if (DDBUS_TYPE_DICT_ENTRY == dbus_message_iter_get_arg_type(&structIter))
{
DBusMessageIter dictIter;
dbus_message_iter_recurse(&structIter, &dictIter);//Initialize iterator for dictentry
if (DBUS_TYPE_INT32 == dbus_message_iter_get_arg_type(&dictIter))
{
dbus_message_iter_get_basic(&dictIter, &a);//Read integer
dbus_message_iter_next(&dictIter);//Go to next argument of dictentry
if (DBUS_TYPE_INT32 == dbus_message_iter_get_arg_type(&dictIter))
{
dbus_message_iter_get_basic(&dictIter, &a);//Read integer
}
}
}
dbus_message_iter_next(&structIter);//Go to next argument of structiter
if (DBUS_TYPE_INT32 == dbus_message_iter_get_arg_type(&structIter))
{
dbus_message_iter_get_basic(&structIter, &a);//Read integer
}
}
}
dbus_message_iter_next(&rootIter);//Go to next argument of root iterator
if (DBUS_TYPE_UINT32 == dbus_message_iter_get_arg_type(&rootIter))
{
uint32_t b;
dbus_message_iter_get_basic(&rootIter, &b);//Read integer
}
}
In above code, I used argument by argument signature check. Instead you can do a one time verfication using dbus_message_iter_get_signature. Refer to libdbus api for more info.
#
From your reply I understand that you have problems with connection setup ,
here is a full example, where a method call is invoked on a server and the result is printed
if the first argument is a string.
#
#include <stdio.h>
#include <stdlib.h>
#include <dbus/dbus.h>
#include <assert.h>
DBusConnection* conn = NULL;
//Helper function to setup connection
void vsetupconnection();
//Send method call, Returns NULL on failure, else pointer to reply
DBusMessage* sendMethodCall(const char* objectpath, \
const char* busname, \
const char* interfacename, \
const char* methodname);
#define TEST_BUS_NAME "org.freedesktop.DBus"
#define TEST_OBJ_PATH "/org/freedesktop/DBus"
#define TEST_INTERFACE_NAME "org.freedesktop.DBus.Introspectable"
#define TEST_METHOD_NAME "Introspect"
int main (int argc, char **argv)
{
vsetupconnection();
DBusMessage* reply = sendMethodCall(TEST_OBJ_PATH, TEST_BUS_NAME, TEST_INTERFACE_NAME, TEST_METHOD_NAME);
if(reply != NULL) {
DBusMessageIter MsgIter;
dbus_message_iter_init(reply, &MsgIter);//msg is pointer to dbus message received
if (DBUS_TYPE_STRING == dbus_message_iter_get_arg_type(&MsgIter)){
char* str = NULL;
dbus_message_iter_get_basic(&MsgIter, &str);
printf("Received string: \n %s \n",str);
}
dbus_message_unref(reply);//unref reply
}
dbus_connection_close(conn);
return 0;
}
void vsetupconnection()
{
DBusError err;
// initialise the errors
dbus_error_init(&err);
// connect to session bus
conn = dbus_bus_get(DBUS_BUS_SESSION, &err);
if (dbus_error_is_set(&err)) {
printf("Connection Error (%s)\n", err.message);
dbus_error_free(&err);
}
if (NULL == conn) {
exit(1);
}
else {
printf("Connected to session bus\n");
}
}
DBusMessage* sendMethodCall(const char* objectpath, const char* busname, const char* interfacename, const char* methodname)
{
assert(objectpath != NULL); assert(busname != NULL); assert(interfacename != NULL);
assert(methodname != NULL); assert(conn != NULL);
DBusMessage* methodcall = dbus_message_new_method_call(busname,objectpath, interfacename, methodname);
if (methodcall == NULL) {
printf("Cannot allocate DBus message!\n");
}
//Now do a sync call
DBusPendingCall* pending;
DBusMessage* reply;
if (!dbus_connection_send_with_reply(conn, methodcall, &pending, -1))//Send and expect reply using pending call object
{
printf("failed to send message!\n");
}
dbus_connection_flush(conn);
dbus_message_unref(methodcall);
methodcall = NULL;
dbus_pending_call_block(pending);//Now block on the pending call
reply = dbus_pending_call_steal_reply(pending);//Get the reply message from the queue
dbus_pending_call_unref(pending);//Free pending call handle
assert(reply != NULL);
if(dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
printf("Error : %s",dbus_message_get_error_name(reply));
dbus_message_unref(reply);
reply = NULL;
}
return reply;
}

Resources