Brad Bishop | e345f70 | 2015-09-21 17:19:05 -0400 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <errno.h> |
| 4 | #include <systemd/sd-bus.h> |
| 5 | |
| 6 | static int method_echo(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { |
| 7 | char *str; |
| 8 | const char *intf = sd_bus_message_get_interface(m), |
| 9 | *path = sd_bus_message_get_path(m); |
| 10 | sd_bus *bus = sd_bus_message_get_bus(m); |
| 11 | |
| 12 | char response[512] = {0}; |
| 13 | int r; |
| 14 | |
| 15 | /* Read the parameters */ |
| 16 | r = sd_bus_message_read(m, "s", &str); |
| 17 | if (r < 0) { |
| 18 | fprintf(stderr, "Failed to parse parameters: %s\n", strerror(-r)); |
| 19 | return r; |
| 20 | } |
| 21 | |
| 22 | r = sd_bus_emit_signal(bus, path, intf, "MethodInvoked", "ss", |
| 23 | "Echo method was invoked", path); |
| 24 | if (r < 0) { |
| 25 | fprintf(stderr, "Failed to emit signal: %s\n", strerror(-r)); |
| 26 | return r; |
| 27 | } |
| 28 | |
| 29 | strncat(response, path, 128); |
| 30 | strcat(response, " says "); |
| 31 | strncat(response, str, 128); |
| 32 | |
| 33 | /* Reply with the response */ |
| 34 | return sd_bus_reply_method_return(m, "s", &response); |
| 35 | } |
| 36 | |
| 37 | static const sd_bus_vtable echo_vtable[] = { |
| 38 | SD_BUS_VTABLE_START(0), |
| 39 | SD_BUS_METHOD("Echo", "s", "s", method_echo, SD_BUS_VTABLE_UNPRIVILEGED), |
| 40 | SD_BUS_SIGNAL("MethodInvoked", "s", 0), |
| 41 | SD_BUS_VTABLE_END |
| 42 | }; |
| 43 | |
| 44 | int main(int argc, char *argv[]) { |
| 45 | sd_bus_slot *slot = NULL; |
| 46 | sd_bus *bus = NULL; |
| 47 | int r; |
| 48 | char **acquired = NULL, **activatable = NULL, **i; |
| 49 | |
| 50 | /* Connect to the user bus this time */ |
| 51 | r = sd_bus_open_system(&bus); |
| 52 | if (r < 0) { |
| 53 | fprintf(stderr, "Failed to connect to system bus: %s\n", strerror(-r)); |
| 54 | goto finish; |
| 55 | } |
| 56 | |
| 57 | /* Install an object */ |
| 58 | r = sd_bus_add_object_vtable(bus, |
| 59 | &slot, |
| 60 | "/org/openbmc/examples/path0/SDBusObj", /* object path */ |
| 61 | "org.openbmc.examples.Echo", /* interface name */ |
| 62 | echo_vtable, |
| 63 | NULL); |
| 64 | if (r < 0) { |
| 65 | fprintf(stderr, "Failed to issue method call: %s\n", strerror(-r)); |
| 66 | goto finish; |
| 67 | } |
| 68 | |
| 69 | /* Install an object */ |
| 70 | r = sd_bus_add_object_vtable(bus, |
| 71 | &slot, |
| 72 | "/org/openbmc/examples/path1/SDBusObj", /* object path */ |
| 73 | "org.openbmc.examples.Echo", /* interface name */ |
| 74 | echo_vtable, |
| 75 | NULL); |
| 76 | if (r < 0) { |
| 77 | fprintf(stderr, "Failed to issue method call: %s\n", strerror(-r)); |
| 78 | goto finish; |
| 79 | } |
| 80 | |
| 81 | /* Take a well-known service name so that clients can find us */ |
| 82 | r = sd_bus_request_name(bus, "org.openbmc.examples.SDBusService0", 0); |
| 83 | if (r < 0) { |
| 84 | fprintf(stderr, "Failed to acquire service name: %s\n", strerror(-r)); |
| 85 | goto finish; |
| 86 | } |
| 87 | |
| 88 | r = sd_bus_request_name(bus, "org.openbmc.examples.SDBusService1", 0); |
| 89 | if (r < 0) { |
| 90 | fprintf(stderr, "Failed to acquire service name: %s\n", strerror(-r)); |
| 91 | goto finish; |
| 92 | } |
| 93 | |
| 94 | for (;;) { |
| 95 | /* Process requests */ |
| 96 | r = sd_bus_process(bus, NULL); |
| 97 | if (r < 0) { |
| 98 | fprintf(stderr, "Failed to process bus: %s\n", strerror(-r)); |
| 99 | goto finish; |
| 100 | } |
| 101 | if (r > 0) /* we processed a request, try to process another one, right-away */ |
| 102 | continue; |
| 103 | |
| 104 | /* Wait for the next request to process */ |
| 105 | r = sd_bus_wait(bus, (uint64_t) -1); |
| 106 | if (r < 0) { |
| 107 | fprintf(stderr, "Failed to wait on bus: %s\n", strerror(-r)); |
| 108 | goto finish; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | finish: |
| 113 | sd_bus_slot_unref(slot); |
| 114 | sd_bus_unref(bus); |
| 115 | |
| 116 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; |
| 117 | } |