利用系统的dbus进行通信
/*
* Example low-level D-Bus code.
* Written by Matthew Johnson <dbus@matthew.ath.cx>
*
* This code has been released into the Public Domain.
* You may do whatever you like with it.
*/
#include <dbus/dbus.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
/**
* Connect to the DBUS bus and send a broadcast signal
*/
void sendsignal(char* sigvalue)
{
DBusMessage* msg;
DBusMessageIter args;
DBusConnection* conn;
DBusError err;
int ret;
dbus_uint32_t serial = 0;
printf("Sending signal with value %s/n", sigvalue);
// initialise the error value
dbus_error_init(&err);
// connect to the DBUS system bus, and check for errors
conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
if (dbus_error_is_set(&err)) {
fprintf(stderr, "Connection Error (%s)/n", err.message);
dbus_error_free(&err);
}
if (NULL == conn) {
exit(1);
}
// create a signal & check for errors
msg = dbus_message_new_signal("/test/signal/Object", // object name of the signal
"test.signal.Type", // interface name of the signal
"Test"); // name of the signal
if (NULL == msg)
{
fprintf(stderr, "Message Null/n");
exit(1);
}
//以下是我们需要添加的==================================================================================
// append arguments onto signal
dbus_message_iter_init_append(msg, &args);
if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &sigvalue)) {
fprintf(stderr, "Out Of Memory!/n");
exit(1);
}
// send the message and flush the connection
if (!dbus_connection_send(conn, msg, &serial)) {
fprintf(stderr, "Out Of Memory!/n");
exit(1);
}
dbus_connection_flush(conn);
//以上部分多一个 dbus_connection_flush(conn) 和错误处理,这里我们都需要加上。=================================
printf("Signal Sent/n");
// free the message
dbus_message_unref(msg);
}
/**
* Listens for signals on the bus
*/
void receive()
{
DBusMessage* msg;
DBusMessageIter args;
DBusConnection* conn;
DBusError err;
int ret;
char* sigvalue;
printf("Listening for signals/n");
// initialise the errors
dbus_error_init(&err);
// connect to the bus and check for errors
conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
if (dbus_error_is_set(&err)) {
fprintf(stderr, "Connection Error (%s)/n", err.message);
dbus_error_free(&err);
}
if (NULL == conn) {
exit(1);
}
// add a rule for which messages we want to see
dbus_bus_add_match(conn, "type='signal',interface='test.signal.Type'", &err); // see signals from the given interface
dbus_connection_flush(conn); //flush 处理,最好加上
if (dbus_error_is_set(&err)) {
fprintf(stderr, "Match Error (%s)/n", err.message);
exit(1);
}
printf("Match rule sent/n");
// loop listening for signals being emmitted
while (true) {
//以下部分是接收部分,需要添加进main.c文件。===============================================
// non blocking read of the next available message
dbus_connection_read_write(conn, 0);
msg = dbus_connection_pop_message(conn);
// loop again if we haven't read a message
if (NULL == msg) {
sleep(1);
continue;
}
// check if the message is a signal from the correct interface and with the correct name
if (dbus_message_is_signal(msg, "test.signal.Type", "Test")) {
// read the parameters
if (!dbus_message_iter_init(msg, &args))
fprintf(stderr, "Message Has No Parameters/n");
else if (DBUS_TYPE_STRING != dbus_message_iter_get_arg_type(&args))
fprintf(stderr, "Argument is not string!/n");
else
dbus_message_iter_get_basic(&args, &sigvalue);
printf("Got Signal with value %s/n", sigvalue);
}
//以上部分读取字符,并且对message的类型进行了判断处理。===========================================
// free the message
dbus_message_unref(msg);
}
}
int main(int argc, char** argv)
{
if (2 > argc) {
printf ("Syntax: dbus-example [send|receive|listen|query] [<param>]/n");
return 1;
}
char* param = "no param";
if (3 >= argc && NULL != argv[2]) param = argv[2];
if (0 == strcmp(argv[1], "send"))
sendsignal(param);
else if (0 == strcmp(argv[1], "receive"))
receive();
else {
printf ("Syntax: dbus-example [send|receive|listen|query] [<param>]/n");
return 1;
}
return 0;
}