static bool _changed(pxNetworkModule *s) { pxNetworkManagerNetworkModule *self = (pxNetworkManagerNetworkModule *) s; // Make sure we have a valid connection with a proper match DBusConnection *conn = self->conn; if (!conn || !dbus_connection_get_is_connected(conn)) { // If the connection was disconnected, // close it an clear the queue if (conn) { dbus_connection_close(conn); dbus_connection_read_write(conn, 0); for (DBusMessage *msg=NULL ; (msg = dbus_connection_pop_message(conn)) ; dbus_message_unref(msg)); } // Create a new connections conn = dbus_bus_get_private(DBUS_BUS_SYSTEM, NULL); self->conn = conn; if (!conn) return false; // If connection was successful, set it up dbus_connection_set_exit_on_disconnect(conn, false); dbus_bus_add_match(conn, "type='signal',interface='" NM_DBUS_INTERFACE "',member='StateChange'", NULL); dbus_connection_flush(conn); } // We are guaranteed a connection, // so check for incoming messages bool changed = false; while (true) { DBusMessage *msg = NULL; uint32_t state; // Pull messages off the queue dbus_connection_read_write(conn, 0); if (!(msg = dbus_connection_pop_message(conn))) break; // If a message is the right type and value, // we'll reset the network if (dbus_message_get_args(msg, NULL, DBUS_TYPE_UINT32, &state, DBUS_TYPE_INVALID)) if (state == NM_STATE_CONNECTED) changed = true; dbus_message_unref(msg); } return changed; }
static int dbus_read(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user) { struct pcap_dbus *handlep = handle->priv; struct pcap_pkthdr pkth; DBusMessage *message; char *raw_msg; int raw_msg_len; int count = 0; message = dbus_connection_pop_message(handlep->conn); while (!message) { /* XXX handle->opt.timeout = timeout_ms; */ if (!dbus_connection_read_write(handlep->conn, 100)) { pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Connection closed"); return -1; } if (handle->break_loop) { handle->break_loop = 0; return -2; } message = dbus_connection_pop_message(handlep->conn); } if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected")) { pcap_snprintf(handle->errbuf, PCAP_ERRBUF_SIZE, "Disconnected"); return -1; } if (dbus_message_marshal(message, &raw_msg, &raw_msg_len)) { pkth.caplen = pkth.len = raw_msg_len; /* pkth.caplen = min (payload_len, handle->snapshot); */ gettimeofday(&pkth.ts, NULL); if (handle->fcode.bf_insns == NULL || bpf_filter(handle->fcode.bf_insns, (u_char *)raw_msg, pkth.len, pkth.caplen)) { handlep->packets_read++; callback(user, &pkth, (u_char *)raw_msg); count++; } dbus_free(raw_msg); } return count; }
/* This constructor sets up a private connection to the DBus daemon, connects * to the hal service and installs a filter which sets the mTriggered flag in * the Context structure when a device (not necessarily USB) is added or * removed. */ VBoxMainHotplugWaiter::VBoxMainHotplugWaiter () { #if defined RT_OS_LINUX && defined VBOX_WITH_DBUS int rc = VINF_SUCCESS; mContext = new Context; if (RT_SUCCESS(RTDBusLoadLib())) { for (unsigned i = 0; RT_SUCCESS(rc) && i < 5 && !mContext->mConnection; ++i) { rc = halInitPrivate (&mContext->mConnection); } if (!mContext->mConnection) rc = VERR_NOT_SUPPORTED; DBusMessage *pMessage; while ( RT_SUCCESS(rc) && (pMessage = dbus_connection_pop_message (mContext->mConnection.get())) != NULL) dbus_message_unref (pMessage); /* empty the message queue. */ if ( RT_SUCCESS(rc) && !dbus_connection_add_filter (mContext->mConnection.get(), dbusFilterFunction, (void *) &mContext->mTriggered, NULL)) rc = VERR_NO_MEMORY; if (RT_FAILURE(rc)) mContext->mConnection.reset(); } #endif /* defined RT_OS_LINUX && defined VBOX_WITH_DBUS */ }
bool CConsoleUPowerSyscall::PumpPowerEvents(IPowerEventsCallback *callback) { bool result = false; if (m_connection) { dbus_connection_read_write(m_connection, 0); DBusMessage *msg = dbus_connection_pop_message(m_connection); if (msg) { result = true; if (dbus_message_is_signal(msg, "org.freedesktop.UPower", "Sleeping")) callback->OnSleep(); else if (dbus_message_is_signal(msg, "org.freedesktop.UPower", "Resuming")) callback->OnWake(); else if (dbus_message_is_signal(msg, "org.freedesktop.UPower", "Changed")) { bool lowBattery = m_lowBattery; UpdateUPower(); if (m_lowBattery && !lowBattery) callback->OnLowBattery(); } else CLog::Log(LOGDEBUG, "UPower: Recieved an unknown signal %s", dbus_message_get_member(msg)); dbus_message_unref(msg); } } return result; }
bool CUDisks2Provider::PumpDriveChangeEvents(IStorageEventsCallback *callback) { if (m_connection) { dbus_connection_read_write(m_connection, 0); DBusMessagePtr msg(dbus_connection_pop_message(m_connection)); if (msg) { CLog::Log(LOGDEBUG, LOGDBUS, "UDisks2: Message received (interface: %s, member: %s)", dbus_message_get_interface(msg.get()), dbus_message_get_member(msg.get())); if (dbus_message_is_signal(msg.get(), DBUS_INTERFACE_OBJECT_MANAGER, "InterfacesAdded")) { HandleInterfacesAdded(msg.get()); return false; } else if (dbus_message_is_signal(msg.get(), DBUS_INTERFACE_OBJECT_MANAGER, "InterfacesRemoved")) { return HandleInterfacesRemoved(msg.get(), callback); } else if (dbus_message_is_signal(msg.get(), DBUS_INTERFACE_PROPERTIES, "PropertiesChanged")) { return HandlePropertiesChanged(msg.get(), callback); } } } return false; }
static void check_bus() { DBusMessage* msg; DBusMessageIter args; char *player, *oldowner, *newowner; while(1) { dbus_connection_read_write(dbus.session.connection, 0); if(!(msg=dbus_connection_pop_message(dbus.session.connection))) break; if(dbus_message_is_signal(msg, dbus.interface, "NameOwnerChanged")) { if(!(dbus_message_iter_init(msg, &args)&&dbus_message_iter_get_arg_type(&args)==DBUS_TYPE_STRING)) continue; dbus_message_iter_get_basic(&args, &player); dbus_message_iter_next(&args); dbus_message_iter_get_basic(&args, &oldowner); dbus_message_iter_next(&args); dbus_message_iter_get_basic(&args, &newowner); if(!strncmp(player, mpris.base, strlen(mpris.base))) { if(!strlen(oldowner)&&strlen(newowner)) mediaplayer_register(player+strlen(mpris.base)); if(!strlen(newowner)) mediaplayer_deregister(player+strlen(mpris.base)); } } dbus_message_unref(msg); } }
/** * Server that exposes a method call and waits for it to be called */ void listen() { DBusMessage* msg; DBusMessage* reply; DBusMessageIter args; DBusConnection* conn; DBusError err; int ret; char* param; printf("Listening for method calls\n"); // initialise the error 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) { fprintf(stderr, "Connection Null\n"); exit(1); } // request our name on the bus and check for errors ret = dbus_bus_request_name(conn, "task.method.server", DBUS_NAME_FLAG_REPLACE_EXISTING , &err); if (dbus_error_is_set(&err)) { fprintf(stderr, "Name Error (%s)\n", err.message); dbus_error_free(&err); } if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) { fprintf(stderr, "Not Primary Owner (%d)\n", ret); exit(1); } // loop, testing for new messages while (true) { // 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 got a message if (NULL == msg) { sleep(1); continue; } printf("hmm\n"); // check this is a method call for the right interface & method if (dbus_message_is_method_call(msg, "task.method.Type", "Method")) reply_to_method_call(msg, conn); // free the message dbus_message_unref(msg); } // close the connection dbus_connection_close(conn); }
void listen_signal() { DBusMessage * msg; DBusMessageIter arg; DBusConnection * connection; DBusError err; int ret; char * sigvalue; //步骤1:建立与D-Bus后台的连接 dbus_error_init(&err); connection = 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); } if(connection == NULL) return; //步骤2:给连接名分配一个可记忆名字test.singal.dest作为Bus name,这个步骤不是必须的,但推荐这样处理 ret = dbus_bus_request_name(connection,"test.singal.dest",DBUS_NAME_FLAG_REPLACE_EXISTING,&err); if(dbus_error_is_set(&err)){ fprintf(stderr,"Name Error %s/n",err.message); dbus_error_free(&err); } if(ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) return; //步骤3:通知D-Bus daemon,希望监听来行接口test.signal.Type的信号 dbus_bus_add_match(connection,"type='signal',interface='test.signal.Type'",&err); //实际需要发送东西给daemon来通知希望监听的内容,所以需要flush dbus_connection_flush(connection); if(dbus_error_is_set(&err)){ fprintf(stderr,"Match Error %s/n",err.message); dbus_error_free(&err); } //步骤4:在循环中监听,每隔开1秒,就去试图自己的连接中获取这个信号。这里给出的是中连接中获取任何消息的方式,所以获取后去检查一下这个消息是否我们期望的信号,并获取内容。我们也可以通过这个方式来获取method call消息。 while(1){ dbus_connection_read_write(connection,0); msg = dbus_connection_pop_message (connection); if(msg == NULL) { printf("Sleeping\n"); sleep(10); continue; } printf("received something\n"); if(dbus_message_is_signal(msg,"test.signal.Type","Test") ){ if(!dbus_message_iter_init(msg,&arg) ) fprintf(stderr,"Message Has no Param"); else if(dbus_message_iter_get_arg_type(&arg) != DBUS_TYPE_STRING) fprintf(stderr,"Param is not string"); else dbus_message_iter_get_basic(&arg,&sigvalue); printf("Got Singal with value : %s\n\n",sigvalue); } dbus_message_unref(msg); }//End of while }
bool CDeviceKitDisksProvider::PumpDriveChangeEvents(IStorageEventsCallback *callback) { bool result = false; if (m_connection) { dbus_connection_read_write(m_connection, 0); DBusMessage *msg = dbus_connection_pop_message(m_connection); if (msg) { char *object; if (dbus_message_get_args (msg, NULL, DBUS_TYPE_OBJECT_PATH, &object, DBUS_TYPE_INVALID)) { result = true; if (dbus_message_is_signal(msg, "org.freedesktop.DeviceKit.Disks", "DeviceAdded")) DeviceAdded(object, callback); else if (dbus_message_is_signal(msg, "org.freedesktop.DeviceKit.Disks", "DeviceRemoved")) DeviceRemoved(object, callback); else if (dbus_message_is_signal(msg, "org.freedesktop.DeviceKit.Disks", "DeviceChanged")) DeviceChanged(object, callback); } dbus_message_unref(msg); } } return result; }
void cdbus_loop(session_t *ps) { dbus_connection_read_write(ps->dbus_conn, 0); DBusMessage *msg = NULL; while ((msg = dbus_connection_pop_message(ps->dbus_conn))) cdbus_process(ps, msg); }
void listen_signal() { DBusMessage* msg; DBusMessageIter arg; DBusConnection* connection; DBusError err; int ret; char* sigvalue; dbus_error_init(&err); connection = dbus_bus_get(DBUS_BUS_SESSION, &err); if(dbus_error_is_set(&err)) { fprintf(stderr, "ConnectionError %s\n", err.message); dbus_error_free(&err); } if(connection == NULL) { return; } ret = dbus_bus_request_name(connection, "test.singal.dest", DBUS_NAME_FLAG_REPLACE_EXISTING, &err); if(dbus_error_is_set(&err)) { fprintf(stderr, "Name Error: %s\n", err.message); dbus_error_free(&err); } if(ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) { return; } dbus_bus_add_match(connection, "type='signal',interface='test.signal.Type'", &err); dbus_connection_flush(connection); if(dbus_error_is_set(&err)) { fprintf(stderr, "Match Error: %s\n", err.message); dbus_error_free(&err); } while(1) { dbus_connection_read_write(connection, 0); msg = dbus_connection_pop_message(connection); if(msg == NULL) { sleep(1); continue; } if(dbus_message_is_signal(msg, "test.signal.Type", "Test")) { if (!dbus_message_iter_init(msg, &arg)) { fprintf(stderr, "Message has no params"); } else if (dbus_message_iter_get_arg_type(&arg) != DBUS_TYPE_STRING) { printf("Param is not string"); } else { dbus_message_iter_get_basic(&arg, &sigvalue); printf("Got Singal with value: %s\n", sigvalue); } dbus_message_unref(msg); } } }
/** * Returns -1 upon error, 0 when there are no more messages */ static int handle_messages(void) { DBusMessage *message; if (!dbus_connection_read_write(connection, 250)) { fprintf(stderr, "FAIL: Connecion is closed\n"); return -1; } for (;;) { message = dbus_connection_pop_message(connection); if (message == NULL) return 0; log_message(log_fd, "received ", message); if (dbus_message_is_signal(message, interface, "Signal")) { dbus_message_unref(message); continue; } else if (dbus_message_is_method_call (message, interface, "Method")) { DBusMessage *reply; reply = dbus_message_new_method_return(message); dbus_message_unref(message); log_message(log_fd, "sent ", reply); dbus_connection_send(connection, reply, NULL); dbus_connection_flush(connection); dbus_message_unref(reply); continue; } else if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL) { DBusMessage *reply; reply = dbus_message_new_error(message, DBUS_ERROR_UNKNOWN_METHOD, NULL); dbus_message_unref(message); log_message(log_fd, "sent ", reply); dbus_connection_send(connection, reply, NULL); dbus_connection_flush(connection); dbus_message_unref(reply); continue; } else { dbus_message_unref(message); continue; } } return 0; }
static dbus_bool_t match_acquired_or_lost_signal (DBusConnection *conn, const char *member, const char *name) { int tries; DBusMessage *msg; const char *interface = "org.freedesktop.DBus"; for (tries = 0; tries < NUM_TRIES_TIL_FAIL; tries++) { _dbus_connection_lock (conn); _dbus_connection_do_iteration_unlocked (conn, DBUS_ITERATION_DO_READING | DBUS_ITERATION_DO_WRITING | DBUS_ITERATION_BLOCK, 0); _dbus_connection_unlock (conn); msg = dbus_connection_pop_message (conn); if (msg != NULL) { if (dbus_message_is_signal (msg, interface, member)) { const char *n; DBusError error; dbus_error_init (&error); dbus_message_get_args (msg, &error, DBUS_TYPE_STRING, &n, DBUS_TYPE_INVALID); if (dbus_error_is_set (&error)) { fprintf (stderr, "Error getting args: %s\n", error.message); dbus_error_free (&error); dbus_message_unref (msg); return FALSE; } if (strcmp (n, name) == 0) { dbus_message_unref (msg); break; } } dbus_message_unref (msg); } } if (tries == NUM_TRIES_TIL_FAIL) { fprintf (stderr, "Did not recive the expected %s.%s signal!!!\n", interface, member); return FALSE; } return TRUE; }
static int ldbus_connection_pop_message(lua_State *L) { DBusConnection *connection = check_DBusConnection(L, 1); DBusMessage *message = dbus_connection_pop_message(connection); if (message == NULL) { lua_pushnil(L); } else { push_DBusMessage(L, message); } return 1; }
bool CLogindUPowerSyscall::PumpPowerEvents(IPowerEventsCallback *callback) { bool result = false; bool releaseLock = false; if (m_connection) { dbus_connection_read_write(m_connection, 0); DBusMessage *msg = dbus_connection_pop_message(m_connection); if (msg) { if (dbus_message_is_signal(msg, "org.freedesktop.login1.Manager", "PrepareForSleep")) { dbus_bool_t arg; // the boolean argument defines whether we are going to sleep (true) or just woke up (false) dbus_message_get_args(msg, NULL, DBUS_TYPE_BOOLEAN, &arg, DBUS_TYPE_INVALID); CLog::Log(LOGDEBUG, "LogindUPowerSyscall: Received PrepareForSleep with arg %i", (int)arg); if (arg) { callback->OnSleep(); releaseLock = true; } else { callback->OnWake(); InhibitDelayLock(); } result = true; } else if (dbus_message_is_signal(msg, "org.freedesktop.UPower", "DeviceChanged")) { bool lowBattery = m_lowBattery; UpdateBatteryLevel(); if (m_lowBattery && !lowBattery) callback->OnLowBattery(); result = true; } else CLog::Log(LOGDEBUG, "LogindUPowerSyscall - Received unknown signal %s", dbus_message_get_member(msg)); dbus_message_unref(msg); } } if (releaseLock) ReleaseDelayLock(); return result; }
void dbus_poll(int timeout) { DBusMessage *dbus_msg; dbus_connection_read_write(dbus_conn, timeout); dbus_msg = dbus_connection_pop_message(dbus_conn); while (dbus_msg) { if (dbus_message_is_method_call (dbus_msg, "org.freedesktop.DBus.Introspectable", "Introspect")) { dbus_introspect(dbus_msg); } if (dbus_message_is_method_call(dbus_msg, "org.freedesktop.Notifications", "Notify")) { notify(dbus_msg); } if (dbus_message_is_method_call(dbus_msg, "org.freedesktop.Notifications", "GetCapabilities")) { getCapabilities(dbus_msg); } if (dbus_message_is_method_call(dbus_msg, "org.freedesktop.Notifications", "GetServerInformation")) { getServerInformation(dbus_msg); } if (dbus_message_is_method_call(dbus_msg, "org.freedesktop.Notifications", "CloseNotification")) { closeNotification(dbus_msg); } dbus_message_unref(dbus_msg); dbus_msg = dbus_connection_pop_message(dbus_conn); } }
int main() { DBusError err; DBusConnection* conn; DBusMessage* msg; int ret; // initialise the errors dbus_error_init( &err ); // connect to the bus 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 ); } if ( NULL == conn ) { _exit( 1 ); } // request a name on the bus ret = dbus_bus_request_name(conn, "dbuscxx.example.calculator.server", DBUS_NAME_FLAG_REPLACE_EXISTING , &err); if (dbus_error_is_set(&err)) { fprintf(stderr, "Name Error (%s)\n", err.message); dbus_error_free(&err); } if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) { _exit(1); } // loop, testing for new messages while ( true ) { // 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 got a message if ( NULL == msg ) { sleep( 1 ); continue; } if ( dbus_message_has_interface(msg, "Calculator.Basic") ) reply_to_method_call( msg, conn ); // free the message dbus_message_unref( msg ); } }
void onDbusMessage(DBusConnection *conn) { if ( !m_bus ) return; DBusMessage* msg; DBusMessageIter args; DBusError err; int ret; char* sigvalue; // non blocking read of the next available message dbus_connection_read_write_dispatch(conn, 0); msg = dbus_connection_pop_message(conn); // loop again if we haven't read a message if (NULL == msg) { return; } //check if the message is a signal from the correct interface and with the correct name if (dbus_message_is_signal (msg, "com.burtonini.dbus.Signal", "Ping")) { //read the parameters if (!dbus_message_iter_init(msg, &args)) CRLog::error("dbus: Message Has No Parameters\n"); else if (DBUS_TYPE_STRING != dbus_message_iter_get_arg_type(&args)) CRLog::error("dbus: Argument is not string!\n"); else { dbus_message_iter_get_basic(&args, &sigvalue); CRLog::info("dbus: Got Signal with value %s\n", sigvalue); } } else if (dbus_message_is_signal (msg, "com.burtonini.dbus.Signal", "Exit")) { //read the parameters if (!dbus_message_iter_init(msg, &args)) CRLog::error("dbus: Message Has No Parameters\n"); else if (DBUS_TYPE_STRING != dbus_message_iter_get_arg_type(&args)) CRLog::error("dbus: Argument is not string!\n"); else { dbus_message_iter_get_basic(&args, &sigvalue); CRLog::info("dbus: Got Signal with value %s\n", sigvalue); } } //free the message dbus_message_unref(msg); }
void serve(char *method_name) { DBusMessage* msg; DBusConnection* conn; DBusError err; int ret; if (!method_name) return; log("Listening for method calls [%s]", method_name); dbus_error_init(&err); conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err); if (dbus_error_is_set(&err)) { log("Connection Error (%s)", err.message); dbus_error_free(&err); } if (NULL == conn) { log_err("Connection Null"); exit(1); } ret = dbus_bus_request_name(conn, SERVER, DBUS_NAME_FLAG_REPLACE_EXISTING , &err); if (dbus_error_is_set(&err)) { log_err("Name Error (%s)", err.message); dbus_error_free(&err); } if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) { log_err("Not Primary Owner (%d)", ret); exit(1); } while (true) { dbus_connection_read_write(conn, 0); msg = dbus_connection_pop_message(conn); if (NULL == msg) { sleep(1); continue; } log_dbg("Received\n"); if (dbus_message_is_method_call(msg, INTERFCE, method_name)) reply_to_method_call(msg, conn); dbus_message_unref(msg); } }
int main(int argc, char **argv) { DBusMessage *msg; DBusConnection *conn; DBusError err; #ifndef DEBUG setenv("DBUS_SESSION_BUS_ADDRESS", "unix:path=/var/run/dbus/system_bus_socket", 1); #endif dbus_error_init(&err); conn = dbus_bus_get(DBUS_BUS_SESSION, &err); if (dbus_error_is_set(&err)) { fprintf(stderr, ERR_CONN, err.message); dbus_error_free(&err); } if (conn == NULL) { exit(1); } int ret = dbus_bus_request_name(conn, DBUS_NAMESPACE, DBUS_NAME_FLAG_REPLACE_EXISTING, &err); if (dbus_error_is_set(&err)) { fprintf(stderr, "%s\n", err.message); dbus_error_free(&err); } if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) { fprintf(stderr, ERR_OWNER, ret); exit(1); } while (true) { dbus_connection_read_write(conn, 0); msg = dbus_connection_pop_message(conn); if (msg == NULL) { sleep(1); continue; } introspect(msg, conn); reply(msg, conn); dbus_message_unref(msg); } return 0; }
/** * This function runs in a separate thread. * * It is started in initSignalWatcher() * * @param user_data (Ignored) */ static void* signalWatch(void* user_data) { Signals* signals = user_data; DBusMessage* m; pthread_mutex_lock(signals->startLock); pthread_mutex_unlock(signals->startLock); while(signals->work) { pthread_mutex_lock(&signals->dbus_mutex); dbus_connection_read_write(signals->con, 100); m = dbus_connection_pop_message(signals->con); pthread_mutex_unlock(&signals->dbus_mutex); if(m != NULL) { char* str = PrintDBusMessage(m); if(dbus_message_get_type(m) == DBUS_MESSAGE_TYPE_SIGNAL) { char* marshalled; int size; dbus_message_marshal(m, &marshalled, &size); writeAllPort(signals->service, marshalled, size); free(marshalled); } dbus_message_unref(m); } else { /* * pthread's mutexes are not fair (it does not prevent starvation), * the signalWatch() thread might hog the mutex all for itself. * * Other threads might require the dbus connection (and the mutex) * for adding/removing watches. One way to minimize this starvation * is to yield control to another thread when the signalWatch() * thread released the mutex. * * This is a sub-optimal solution. The best solution would be a fair mutex. */ sched_yield(); } } fprintf(stderr, "signalWatch thread stopped\n"); return NULL; }
int main() { DBusError error; DBusConnection *connection; int ret; dbus_bool_t flag; dbus_error_init(&error); connection=dbus_bus_get(DBUS_BUS_SESSION,&error); if (dbus_error_is_set(&error)) { printf("error\n"); exit(1); } flag=dbus_bus_register(connection,&error); ret=dbus_bus_request_name(connection,"org.jinhui.dbus",DBUS_NAME_FLAG_ALLOW_REPLACEMENT,&error); printf("request name successfully\n"); if (dbus_error_is_set(&error)) { printf("error\n"); exit(1); } dbus_bus_add_match(connection, "type='signal',interface='org.jinhui.iface'",&error); dbus_connection_flush(connection); DBusMessage *msg; while ( 1 ) { flag=dbus_connection_read_write(connection,1); msg=dbus_connection_pop_message(connection); if (!flag) { printf("Invalid message\n"); continue; } //printf("Message received\n"); if (msg!=NULL && dbus_message_is_method_call(msg,"org.jinhui.iface","hello")) { printf("send to hello method\n"); reply_to_hello_method(connection,msg); } } dbus_connection_unref(connection); }
int main(int argc, char** argv) { DBusConnection* conn; DBusMessage* msg; DBusMessage* reply; DBusMessageIter args; DBusError err; dbus_uint32_t serial = 0; int ret; const char * lib_file; const char * init_fn; repl_result repl_result; dbus_error_init(&err); repl_init(); conn = dbus_bus_get(DBUS_BUS_SESSION, &err); CHECK_FOR_ERRORS("Connection error: %s\n"); ASSERT(conn != NULL); ret = dbus_bus_request_name(conn, "cljc.repl.runtime", DBUS_NAME_FLAG_REPLACE_EXISTING , &err); CHECK_FOR_ERRORS("Name error: %s\n"); ASSERT(ret == DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER); while (true) { dbus_connection_read_write(conn, 0); msg = dbus_connection_pop_message(conn); if (msg == NULL) { usleep(10000); continue; } if (dbus_message_is_method_call(msg, "cljc.repl.runtime", "eval")) { ASSERT(dbus_message_iter_init(msg, &args)); ASSERT(dbus_message_iter_get_arg_type(&args) == DBUS_TYPE_STRING); dbus_message_iter_get_basic(&args, &lib_file); ASSERT(dbus_message_iter_next(&args)); ASSERT(dbus_message_iter_get_arg_type(&args) == DBUS_TYPE_STRING); dbus_message_iter_get_basic(&args, &init_fn); repl_result = repl_eval(lib_file, init_fn); fflush(stdout); reply = dbus_message_new_method_return(msg); dbus_message_iter_init_append(reply, &args); ASSERT(dbus_message_iter_append_basic(&args, DBUS_TYPE_UINT32, &repl_result.status)); ASSERT(dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &repl_result.buffer)); ASSERT(dbus_connection_send(conn, reply, &serial)); dbus_connection_flush(conn); dbus_message_unref(reply); } dbus_message_unref(msg); } }
void listen(DBusConnection* conn) { DBusMessage* msg; //DBusMessage* reply; while (1) { dbus_connection_read_write(conn, 0); msg = dbus_connection_pop_message(conn); if (msg == NULL) { sleep(1); continue; } if (dbus_message_is_method_call(msg, TEST_METHOD_TYPE, TEST_METHOD)) reply_method_call(msg, conn); dbus_message_unref(msg); } }
/** * Server that exposes a method call and waits for it to be called */ void listen_to_clients() { DBusMessage* msg; DBusMessage* reply; DBusMessageIter args; int ret; char* param; printf("Listening for method calls & signals\n"); // loop, testing for new messages while (1) { // 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 got a message if (NULL == msg) { sleep(1); continue; } // check this is a method call for the right interface & method if (dbus_message_is_method_call(msg, SERVER_METHOD_INTERFACE, METHOD1)) handle_methods(msg); if (dbus_message_is_signal(msg, SERVER_SIGNAL_INTERFACE, SIGNAL1) || dbus_message_is_signal(msg, SERVER_SIGNAL_INTERFACE, SIGNAL2) || dbus_message_is_signal(msg, SERVER_SIGNAL_INTERFACE, SIGNAL3) || dbus_message_is_signal(msg, SERVER_SIGNAL_INTERFACE, SIGNAL4)) handle_signals(msg); // free the message dbus_message_unref(msg); } }
void dbus_poll(void) { DBusMessage *dbus_msg; /* make timeout smaller if we are displaying a message * to improve responsivness for mouse clicks */ if(msgqueue == NULL) { dbus_connection_read_write(dbus_conn, DBUS_POLL_TIMEOUT); } else { dbus_connection_read_write(dbus_conn, 100); } dbus_msg = dbus_connection_pop_message(dbus_conn); /* we don't have a new message */ if(dbus_msg == NULL) { return; } if(dbus_message_is_method_call(dbus_msg, "org.freedesktop.Notifications","Notify")) { notify(dbus_msg); } if(dbus_message_is_method_call(dbus_msg, "org.freedesktop.Notifications", "GetCapabilities")) { getCapabilities(dbus_msg); } if(dbus_message_is_method_call(dbus_msg, "org.freedesktop.Notifications", "GetServerInformation")) { getServerInformation(dbus_msg); } if(dbus_message_is_method_call(dbus_msg, "org.freedesktop.Notifications", "CloseNotification")) { closeNotification(dbus_msg); } dbus_message_unref(dbus_msg); }
OMXControlResult OMXControl::getEvent() { if (!bus) return KeyConfig::ACTION_BLANK; dispatch(); DBusMessage *m = dbus_connection_pop_message(bus); if (m == NULL) return KeyConfig::ACTION_BLANK; CLog::Log(LOGDEBUG, "Popped message member: %s interface: %s type: %d path: %s", dbus_message_get_member(m), dbus_message_get_interface(m), dbus_message_get_type(m), dbus_message_get_path(m) ); if (dbus_message_is_method_call(m, OMXPLAYER_DBUS_INTERFACE_ROOT, "Quit")) { dbus_respond_ok(m); return KeyConfig::ACTION_EXIT; } else if (dbus_message_is_method_call(m, OMXPLAYER_DBUS_INTERFACE_PROPERTIES, "CanQuit") || dbus_message_is_method_call(m, OMXPLAYER_DBUS_INTERFACE_PROPERTIES, "Fullscreen")) { dbus_respond_boolean(m, 1); return KeyConfig::ACTION_BLANK; } else if (dbus_message_is_method_call(m, OMXPLAYER_DBUS_INTERFACE_PROPERTIES, "CanSetFullscreen") || dbus_message_is_method_call(m, OMXPLAYER_DBUS_INTERFACE_PROPERTIES, "CanRaise") || dbus_message_is_method_call(m, OMXPLAYER_DBUS_INTERFACE_PROPERTIES, "HasTrackList")) { dbus_respond_boolean(m, 0); return KeyConfig::ACTION_BLANK; } else if (dbus_message_is_method_call(m, OMXPLAYER_DBUS_INTERFACE_PROPERTIES, "Identity")) { dbus_respond_string(m, "OMXPlayer"); return KeyConfig::ACTION_BLANK; } else if (dbus_message_is_method_call(m, OMXPLAYER_DBUS_INTERFACE_PROPERTIES, "SupportedUriSchemes")) { const char *UriSchemes[] = {"file", "http"}; dbus_respond_array(m, UriSchemes, 2); // Array is of length 2 return KeyConfig::ACTION_BLANK; } else if (dbus_message_is_method_call(m, OMXPLAYER_DBUS_INTERFACE_PROPERTIES, "SupportedMimeTypes")) { const char *MimeTypes[] = {}; // Needs supplying dbus_respond_array(m, MimeTypes, 0); return KeyConfig::ACTION_BLANK; } else if (dbus_message_is_method_call(m, OMXPLAYER_DBUS_INTERFACE_PROPERTIES, "CanGoNext") || dbus_message_is_method_call(m, OMXPLAYER_DBUS_INTERFACE_PROPERTIES, "CanGoPrevious")) { dbus_respond_boolean(m, 0); return KeyConfig::ACTION_BLANK; } else if (dbus_message_is_method_call(m, OMXPLAYER_DBUS_INTERFACE_PROPERTIES, "CanSeek")) { dbus_respond_boolean(m, reader->CanSeek()); return KeyConfig::ACTION_BLANK; } else if (dbus_message_is_method_call(m, OMXPLAYER_DBUS_INTERFACE_PROPERTIES, "CanControl") || dbus_message_is_method_call(m, OMXPLAYER_DBUS_INTERFACE_PROPERTIES, "CanPlay") || dbus_message_is_method_call(m, OMXPLAYER_DBUS_INTERFACE_PROPERTIES, "CanPause")) { dbus_respond_boolean(m, 1); } else if (dbus_message_is_method_call(m, OMXPLAYER_DBUS_INTERFACE_PLAYER, "Next")) { dbus_respond_ok(m); return KeyConfig::ACTION_NEXT_CHAPTER; } else if (dbus_message_is_method_call(m, OMXPLAYER_DBUS_INTERFACE_PLAYER, "Previous")) { dbus_respond_ok(m); return KeyConfig::ACTION_PREVIOUS_CHAPTER; } else if (dbus_message_is_method_call(m, OMXPLAYER_DBUS_INTERFACE_PLAYER, "Pause") || dbus_message_is_method_call(m, OMXPLAYER_DBUS_INTERFACE_PLAYER, "PlayPause")) { dbus_respond_ok(m); return KeyConfig::ACTION_PAUSE; } else if (dbus_message_is_method_call(m, OMXPLAYER_DBUS_INTERFACE_PLAYER, "Stop")) { dbus_respond_ok(m); return KeyConfig::ACTION_EXIT; } else if (dbus_message_is_method_call(m, OMXPLAYER_DBUS_INTERFACE_PLAYER, "Seek")) { DBusError error; dbus_error_init(&error); int64_t offset; dbus_message_get_args(m, &error, DBUS_TYPE_INT64, &offset, DBUS_TYPE_INVALID); // Make sure a value is sent for seeking if (dbus_error_is_set(&error)) { CLog::Log(LOGWARNING, "Seek D-Bus Error: %s", error.message ); dbus_error_free(&error); dbus_respond_ok(m); return KeyConfig::ACTION_BLANK; } else { dbus_respond_int64(m, offset); return OMXControlResult(KeyConfig::ACTION_SEEK_RELATIVE, offset); } } else if (dbus_message_is_method_call(m, OMXPLAYER_DBUS_INTERFACE_PLAYER, "SetPosition")) { DBusError error; dbus_error_init(&error); int64_t position; dbus_message_get_args(m, &error, DBUS_TYPE_INT64, &position, DBUS_TYPE_INVALID); // Make sure a value is sent for setting position if (dbus_error_is_set(&error)) { CLog::Log(LOGWARNING, "SetPosition D-Bus Error: %s", error.message ); dbus_error_free(&error); dbus_respond_ok(m); return KeyConfig::ACTION_BLANK; } else { dbus_respond_int64(m, position); return OMXControlResult(KeyConfig::ACTION_SEEK_ABSOLUTE, position); } } else if (dbus_message_is_method_call(m, OMXPLAYER_DBUS_INTERFACE_PROPERTIES, "PlaybackStatus")) { const char *status; if (clock->OMXIsPaused()) { status = "Paused"; } else { status = "Playing"; } dbus_respond_string(m, status); return KeyConfig::ACTION_BLANK; } else if (dbus_message_is_method_call(m, OMXPLAYER_DBUS_INTERFACE_PROPERTIES, "Volume")) { DBusError error; dbus_error_init(&error); double volume; dbus_message_get_args(m, &error, DBUS_TYPE_DOUBLE, &volume, DBUS_TYPE_INVALID); if (dbus_error_is_set(&error)) { // i.e. Get current volume dbus_error_free(&error); dbus_respond_double(m, audio->GetVolume()); return KeyConfig::ACTION_BLANK; } else { audio->SetVolume(volume); dbus_respond_double(m, volume); return KeyConfig::ACTION_BLANK; } } else if (dbus_message_is_method_call(m, OMXPLAYER_DBUS_INTERFACE_PROPERTIES, "Mute")) { audio->SetMute(true); dbus_respond_ok(m); return KeyConfig::ACTION_BLANK; } else if (dbus_message_is_method_call(m, OMXPLAYER_DBUS_INTERFACE_PROPERTIES, "Unmute")) { audio->SetMute(false); dbus_respond_ok(m); return KeyConfig::ACTION_BLANK; } else if (dbus_message_is_method_call(m, OMXPLAYER_DBUS_INTERFACE_PROPERTIES, "Position")) { // Returns the current position in microseconds int64_t pos = clock->OMXMediaTime(); dbus_respond_int64(m, pos); return KeyConfig::ACTION_BLANK; } else if (dbus_message_is_method_call(m, OMXPLAYER_DBUS_INTERFACE_PROPERTIES, "Duration")) { // Returns the duration in microseconds int64_t dur = reader->GetStreamLength(); dur *= 1000; // ms -> us dbus_respond_int64(m, dur); return KeyConfig::ACTION_BLANK; } else if (dbus_message_is_method_call(m, OMXPLAYER_DBUS_INTERFACE_PROPERTIES, "MinimumRate")) { dbus_respond_double(m, 0.0); return KeyConfig::ACTION_BLANK; } else if (dbus_message_is_method_call(m, OMXPLAYER_DBUS_INTERFACE_PROPERTIES, "MaximumRate")) { dbus_respond_double(m, 1.125); return KeyConfig::ACTION_BLANK; // Implement extra OMXPlayer controls } else if (dbus_message_is_method_call(m, OMXPLAYER_DBUS_INTERFACE_PLAYER, "ListSubtitles")) { int count = reader->SubtitleStreamCount(); char** values = new char*[count]; for (int i=0; i < count; i++) { asprintf(&values[i], "%d:%s:%s:%s:%s", i, reader->GetStreamLanguage(OMXSTREAM_SUBTITLE, i).c_str(), reader->GetStreamName(OMXSTREAM_SUBTITLE, i).c_str(), reader->GetCodecName(OMXSTREAM_SUBTITLE, i).c_str(), ((int)subtitles->GetActiveStream() == i) ? "active" : ""); } dbus_respond_array(m, (const char**)values, count); // Cleanup for (int i=0; i < count; i++) { delete[] values[i]; } return KeyConfig::ACTION_BLANK; } else if (dbus_message_is_method_call(m, OMXPLAYER_DBUS_INTERFACE_PLAYER, "VideoPos")) { DBusError error; dbus_error_init(&error); const char *win; const char *oPath; // ignoring path right now because we don't have a playlist dbus_message_get_args(m, &error, DBUS_TYPE_OBJECT_PATH, &oPath, DBUS_TYPE_STRING, &win, DBUS_TYPE_INVALID); // Make sure a value is sent for setting VideoPos if (dbus_error_is_set(&error)) { CLog::Log(LOGWARNING, "VideoPos D-Bus Error: %s", error.message ); dbus_error_free(&error); dbus_respond_ok(m); return KeyConfig::ACTION_BLANK; } else { dbus_respond_string(m, win); return OMXControlResult(KeyConfig::ACTION_MOVE_VIDEO, win); } } else if (dbus_message_is_method_call(m, OMXPLAYER_DBUS_INTERFACE_PLAYER, "HideVideo")) { dbus_respond_ok(m); return KeyConfig::ACTION_HIDE_VIDEO; } else if (dbus_message_is_method_call(m, OMXPLAYER_DBUS_INTERFACE_PLAYER, "UnHideVideo")) { dbus_respond_ok(m); return KeyConfig::ACTION_UNHIDE_VIDEO; } else if (dbus_message_is_method_call(m, OMXPLAYER_DBUS_INTERFACE_PLAYER, "ListAudio")) { int count = reader->AudioStreamCount(); char** values = new char*[count]; for (int i=0; i < count; i++) { asprintf(&values[i], "%d:%s:%s:%s:%s", i, reader->GetStreamLanguage(OMXSTREAM_AUDIO, i).c_str(), reader->GetStreamName(OMXSTREAM_AUDIO, i).c_str(), reader->GetCodecName(OMXSTREAM_AUDIO, i).c_str(), (reader->GetAudioIndex() == i) ? "active" : ""); } dbus_respond_array(m, (const char**)values, count); // Cleanup for (int i=0; i < count; i++) { delete[] values[i]; } return KeyConfig::ACTION_BLANK; } else if (dbus_message_is_method_call(m, OMXPLAYER_DBUS_INTERFACE_PLAYER, "ListVideo")) { int count = reader->AudioStreamCount(); char** values = new char*[count]; for (int i=0; i < count; i++) { asprintf(&values[i], "%d:%s:%s:%s:%s", i, reader->GetStreamLanguage(OMXSTREAM_VIDEO, i).c_str(), reader->GetStreamName(OMXSTREAM_VIDEO, i).c_str(), reader->GetCodecName(OMXSTREAM_VIDEO, i).c_str(), (reader->GetVideoIndex() == i) ? "active" : ""); } dbus_respond_array(m, (const char**)values, count); // Cleanup for (int i=0; i < count; i++) { delete[] values[i]; } return KeyConfig::ACTION_BLANK; } else if (dbus_message_is_method_call(m, OMXPLAYER_DBUS_INTERFACE_PLAYER, "SelectSubtitle")) { DBusError error; dbus_error_init(&error); int index; dbus_message_get_args(m, &error, DBUS_TYPE_INT32, &index, DBUS_TYPE_INVALID); if (dbus_error_is_set(&error)) { dbus_error_free(&error); dbus_respond_boolean(m, 0); } else { if (reader->SetActiveStream(OMXSTREAM_SUBTITLE, index)) { subtitles->SetActiveStream(reader->GetSubtitleIndex()); dbus_respond_boolean(m, 1); } else { dbus_respond_boolean(m, 0); } } return KeyConfig::ACTION_BLANK; } else if (dbus_message_is_method_call(m, OMXPLAYER_DBUS_INTERFACE_PLAYER, "SelectAudio")) { DBusError error; dbus_error_init(&error); int index; dbus_message_get_args(m, &error, DBUS_TYPE_INT32, &index, DBUS_TYPE_INVALID); if (dbus_error_is_set(&error)) { dbus_error_free(&error); dbus_respond_boolean(m, 0); } else { if (reader->SetActiveStream(OMXSTREAM_AUDIO, index)) { dbus_respond_boolean(m, 1); } else { dbus_respond_boolean(m, 0); } } return KeyConfig::ACTION_BLANK; } // TODO: SelectVideo ??? else if (dbus_message_is_method_call(m, OMXPLAYER_DBUS_INTERFACE_PLAYER, "ShowSubtitles")) { subtitles->SetVisible(true); dbus_respond_ok(m); return KeyConfig::ACTION_BLANK; } else if (dbus_message_is_method_call(m, OMXPLAYER_DBUS_INTERFACE_PLAYER, "HideSubtitles")) { subtitles->SetVisible(false); dbus_respond_ok(m); return KeyConfig::ACTION_BLANK; } else if (dbus_message_is_method_call(m, OMXPLAYER_DBUS_INTERFACE_PLAYER, "Action")) { DBusError error; dbus_error_init(&error); int action; dbus_message_get_args(m, &error, DBUS_TYPE_INT32, &action, DBUS_TYPE_INVALID); if (dbus_error_is_set(&error)) { dbus_error_free(&error); dbus_respond_ok(m); return KeyConfig::ACTION_BLANK; } else { dbus_respond_ok(m); return action; // Directly return enum } } else { CLog::Log(LOGWARNING, "Unhandled dbus message, member: %s interface: %s type: %d path: %s", dbus_message_get_member(m), dbus_message_get_interface(m), dbus_message_get_type(m), dbus_message_get_path(m) ); } return KeyConfig::ACTION_BLANK; }
void* serverthread(void*) { DBusMessage* msg; DBusError err; int ret; printf("Listening for method calls\n"); if (pipe(quitpipe) == -1) { fprintf(stderr, "Could not create pipe.\n"); return 0; } // initialise the error dbus_error_init(&err); // connect to the bus and check for errors 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 0; } if (NULL == conn) { fprintf(stderr, "Connection Null\n"); return 0; } // request our name on the bus and check for errors ret = dbus_bus_request_name(conn, "vandenoever.strigi", DBUS_NAME_FLAG_REPLACE_EXISTING , &err); if (dbus_error_is_set(&err)) { fprintf(stderr, "Name Error (%s)\n", err.message); dbus_error_free(&err); } if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) { fprintf(stderr, "Not Primary Owner (%d)\n", ret); return 0; } for (int i=0; i<10; ++i) { printf("server\n"); } int fd; dbus_connection_get_unix_fd(conn, &fd); bool run; do { // block until there's activit on the port fd_set rfds; struct timeval tv; FD_ZERO(&rfds); FD_SET(fd, &rfds); FD_SET(quitpipe[0], &rfds); printf("server\n"); tv.tv_sec = 50000; tv.tv_usec = 0; int retval = select(fd+1, &rfds, 0, 0, &tv); if (retval == -1 || FD_ISSET(quitpipe[0], &rfds)) break; // non blocking read of the next available message dbus_connection_read_write_dispatch(conn, 0); // dbus_connection_read_write(conn, 0); msg = dbus_connection_pop_message(conn); dbus_connection_flush(conn); dbus_message_unref(msg); STRIGI_MUTEX_LOCK(&lock); run = keeprunning; STRIGI_MUTEX_UNLOCK(&lock); } while (run); dbus_connection_unref(conn); dbus_connection_unref(conn); dbus_shutdown(); printf("after dbus_shutdown\n"); return 0; }
/** Dbus provider loop. waits for messages and call handlers on them. Just receive messages, doesn't do initialization or finalization To stop the loop, set the settings->connection to NULL. * * @param data pointer to initialized dbus_state_holder * @return NULL */ static void *dbus_provider_loop(void *data) { message_handle_state_e state = ZFSD_MESSAGE_HANDLED; dbus_state_holder settings = (dbus_state_holder) data; DBusMessage *msg = NULL; int listener_index = 0; #ifdef ENABLE_CHECKING if (data == NULL) { message(LOG_ERROR, FACILITY_DBUS, "NULL settings struct in dbus_provider_loop\n"); return NULL; } #endif while (state == ZFSD_MESSAGE_HANDLED) { pthread_mutex_lock(&(settings->mutex)); if (settings->connection == NULL) { state = ZFSD_NO_MESSAGE; goto NEXT; } // non blocking read of the next available message dbus_connection_read_write(settings->connection, DBUS_CONNECTION_TIMEOUT); msg = dbus_connection_pop_message(settings->connection); // loop again if we haven't got a message if (msg == NULL) { goto NEXT; } message(LOG_DEBUG, FACILITY_DBUS, "received message '%s' on iface '%s'\n", dbus_message_get_member(msg), dbus_message_get_interface(msg)); for (listener_index = 0; listener_index < settings->listener_count; listener_index++) { message(LOG_LOOPS, FACILITY_DBUS, "trying listener %d\n", listener_index); state = settings->listeners[listener_index]. handle_message(settings->connection, &(settings->error), msg); if (state == ZFSD_MESSAGE_HANDLED) { break; } } // ignore messages from org.freedesktop.DBus interface (e.g. // NameAcquired signal) if ((state != ZFSD_MESSAGE_HANDLED) && !strcmp(dbus_message_get_interface(msg), "org.freedesktop.DBus")) state = ZFSD_MESSAGE_HANDLED; if (state != ZFSD_MESSAGE_HANDLED) { message(LOG_WARNING, FACILITY_DBUS, "Can't handle message (%d)\n", state); state = ZFSD_MESSAGE_HANDLED; } // free the message dbus_message_unref(msg); NEXT: pthread_mutex_unlock(&(settings->mutex)); zfs_pthread_yield(); } return NULL; }
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_SESSION, &err); if (dbus_error_is_set(&err)) { fprintf(stderr, "Connection Error (%s)\n", err.message); dbus_error_free(&err); } if (NULL == conn) { exit(1); } // request our name on the bus and check for errors ret = dbus_bus_request_name(conn, "test.signal.sink", DBUS_NAME_FLAG_REPLACE_EXISTING, &err); if (dbus_error_is_set(&err)) { fprintf(stderr, "Name Error (%s)\n", err.message); dbus_error_free(&err); } if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret) { 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); 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) { // 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) { usleep(10000); 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); } // free the message dbus_message_unref(msg); } }