Login
[x]
Log in using an account from:
Fedora Account System
Red Hat Associate
Red Hat Customer
Or login using a Red Hat Bugzilla account
Forgot Password
Login:
Hide Forgot
Create an Account
Red Hat Bugzilla – Attachment 912572 Details for
Bug 1113758
dbus-API-call test fail
[?]
New
Simple Search
Advanced Search
My Links
Browse
Requests
Reports
Current State
Search
Tabular reports
Graphical reports
Duplicates
Other Reports
User Changes
Plotly Reports
Bug Status
Bug Severity
Non-Defaults
|
Product Dashboard
Help
Page Help!
Bug Writing Guidelines
What's new
Browser Support Policy
5.0.4.rh83 Release notes
FAQ
Guides index
User guide
Web Services
Contact
Legal
This site requires JavaScript to be enabled to function correctly, please enable it.
testing code
dbus-API.c (text/x-c), 10.27 KB, created by
Matěj Cepl
on 2014-06-26 20:33:45 UTC
(
hide
)
Description:
testing code
Filename:
MIME Type:
Creator:
Matěj Cepl
Created:
2014-06-26 20:33:45 UTC
Size:
10.27 KB
patch
obsolete
>/* > * 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> > > >void die(char* msg) >{ > fprintf(stderr, "%s\n", msg); > exit(1); >} > >/** > * 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_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); > } > > // register our name on the bus, and check for errors > ret = dbus_bus_request_name(conn, "test.signal.source", 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); > } > > // 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) > { > die("Message Null"); > } > > // append arguments onto signal > dbus_message_iter_init_append(msg, &args); > if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &sigvalue)) { > die("Out Of Memory!"); > } > > // send the message and flush the connection > if (!dbus_connection_send(conn, msg, &serial)) { > die("Out Of Memory!"); > } > dbus_connection_flush(conn); > > printf("Signal Sent\n"); > > // free the message > dbus_message_unref(msg); >} > >/** > * Call a method on a remote object > */ >void query(char* param) >{ > DBusMessage* msg; > DBusMessageIter args; > DBusConnection* conn; > DBusError err; > DBusPendingCall* pending; > int ret; > bool stat; > dbus_uint32_t level; > > printf("Calling remote method with %s\n", param); > > // initialiset the errors > dbus_error_init(&err); > > // connect to the system 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 > ret = dbus_bus_request_name(conn, "test.method.caller", 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); > } > > // create a new method call and check for errors > msg = dbus_message_new_method_call("test.method.server", // target for the method call > "/test/method/Object", // object to call on > "test.method.Type", // interface to call on > "Method"); // method name > if (NULL == msg) { > die("Message Null"); > } > > // append arguments > dbus_message_iter_init_append(msg, &args); > if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, ¶m)) { > die("Out Of Memory!"); > } > > // send message and get a handle for a reply > if (!dbus_connection_send_with_reply (conn, msg, &pending, -1)) { // -1 is default timeout > die("Out Of Memory!"); > } > if (NULL == pending) { > die("Pending Call Null"); > } > dbus_connection_flush(conn); > > printf("Request Sent\n"); > > // free message > dbus_message_unref(msg); > > // block until we recieve a reply > dbus_pending_call_block(pending); > > // get the reply message > msg = dbus_pending_call_steal_reply(pending); > if (NULL == msg) { > die("Reply Null"); > } > // free the pending message handle > dbus_pending_call_unref(pending); > > // read the parameters > if (!dbus_message_iter_init(msg, &args)) > die("Message has no arguments!"); > else if (DBUS_TYPE_BOOLEAN != dbus_message_iter_get_arg_type(&args)) > die("Argument is not boolean!"); > else > dbus_message_iter_get_basic(&args, &stat); > > if (!dbus_message_iter_next(&args)) > die("Message has too few arguments!"); > else if (DBUS_TYPE_UINT32 != dbus_message_iter_get_arg_type(&args)) > die("Argument is not int!"); > else > dbus_message_iter_get_basic(&args, &level); > > printf("Got Reply: %d, %d\n", stat, level); > > // free reply > dbus_message_unref(msg); >} > >void reply_to_method_call(DBusMessage* msg, DBusConnection* conn) >{ > DBusMessage* reply; > DBusMessageIter args; > dbus_bool_t stat = true; > dbus_uint32_t level = 21614; > dbus_uint32_t serial = 0; > char* param = ""; > > // read the arguments > if (!dbus_message_iter_init(msg, &args)) > die("Message has no arguments!"); > else if (DBUS_TYPE_STRING != dbus_message_iter_get_arg_type(&args)) > die("Argument is not string!"); > else > dbus_message_iter_get_basic(&args, ¶m); > > printf("Method called with %s\n", param); > > // create a reply from the message > reply = dbus_message_new_method_return(msg); > > // add the arguments to the reply > dbus_message_iter_init_append(reply, &args); > if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_BOOLEAN, &stat)) { > die("Out Of Memory!"); > } > if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_UINT32, &level)) { > die("Out Of Memory!"); > } > > // send the reply && flush the connection > if (!dbus_connection_send(conn, reply, &serial)) { > die("Out Of Memory!"); > } > dbus_connection_flush(conn); > > // free the reply > dbus_message_unref(reply); >} > >/** > * 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_SESSION, &err); > if (dbus_error_is_set(&err)) { > fprintf(stderr, "Connection Error (%s)\n", err.message); > dbus_error_free(&err); > } > if (NULL == conn) { > die("Connection Null"); > } > > // request our name on the bus and check for errors > ret = dbus_bus_request_name(conn, "test.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; > } > > // check this is a method call for the right interface & method > if (dbus_message_is_method_call(msg, "test.method.Type", "Method")) > reply_to_method_call(msg, conn); > > // 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_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) { > 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)) > die("Message Has No Parameters"); > else if (DBUS_TYPE_STRING != dbus_message_iter_get_arg_type(&args)) > die("Argument is not string!"); > else > dbus_message_iter_get_basic(&args, &sigvalue); > > printf("Got Signal with value %s\n", sigvalue); > } > > // 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 if (0 == strcmp(argv[1], "listen")) > listen(); > else if (0 == strcmp(argv[1], "query")) > query(param); > else { > printf ("Syntax: dbus-example [send|receive|listen|query] [<param>]\n"); > return 1; > } > return 0; >}
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Raw
Actions:
View
Attachments on
bug 1113758
: 912572