static int method_echo(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { char *str; const char *intf = sd_bus_message_get_interface(m), *path = sd_bus_message_get_path(m); sd_bus *bus = sd_bus_message_get_bus(m); char response[512] = {0}; int r; /* Read the parameters */ r = sd_bus_message_read(m, "s", &str); if (r < 0) { fprintf(stderr, "Failed to parse parameters: %s\n", strerror(-r)); return r; } r = sd_bus_emit_signal(bus, path, intf, "MethodInvoked", "ss", "Echo method was invoked", path); if (r < 0) { fprintf(stderr, "Failed to emit signal: %s\n", strerror(-r)); return r; } strncat(response, path, 128); strcat(response, " says "); strncat(response, str, 128); /* Reply with the response */ return sd_bus_reply_method_return(m, "s", &response); }
static int on_properties_changed(sd_bus_message *m, void *userdata, sd_bus_error *error) { static const struct bus_properties_map map[] = { { "ActiveState", "s", NULL, offsetof(RunContext, active_state) }, { "InactiveExitTimestampMonotonic", "t", NULL, offsetof(RunContext, inactive_exit_usec) }, { "InactiveEnterTimestampMonotonic", "t", NULL, offsetof(RunContext, inactive_enter_usec) }, { "Result", "s", NULL, offsetof(RunContext, result) }, { "ExecMainCode", "i", NULL, offsetof(RunContext, exit_code) }, { "ExecMainStatus", "i", NULL, offsetof(RunContext, exit_status) }, { "CPUUsageNSec", "t", NULL, offsetof(RunContext, cpu_usage_nsec) }, {} }; RunContext *c = userdata; int r; r = bus_map_all_properties(c->bus, "org.freedesktop.systemd1", sd_bus_message_get_path(m), map, c); if (r < 0) { sd_event_exit(c->event, EXIT_FAILURE); return log_error_errno(r, "Failed to query unit state: %m"); } run_context_check_done(c); return 0; }
static int object_callback(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { int r; if (sd_bus_message_is_method_error(m, NULL)) return 0; if (sd_bus_message_is_method_call(m, "org.object.test", "Foobar")) { log_info("Invoked Foobar() on %s", sd_bus_message_get_path(m)); r = sd_bus_reply_method_return(m, NULL); if (r < 0) return log_error_errno(r, "Failed to send reply: %m"); return 1; } return 0; }
static int cc_Smartie_hangup_thunk( CC_IGNORE_BUS_ARG sd_bus_message *m, void *userdata, sd_bus_error *error) { int result = 0; struct cc_server_Smartie *ii = (struct cc_server_Smartie *) userdata; int32_t status; CC_LOG_DEBUG("invoked cc_Smartie_hangup_thunk()\n"); assert(m); assert(ii && ii->impl); CC_LOG_DEBUG("with path='%s'\n", sd_bus_message_get_path(m)); result = sd_bus_message_read(m, ""); if (result < 0) { CC_LOG_ERROR("unable to read method parameters: %s\n", strerror(-result)); return result; } if (!ii->impl->hangup) { CC_LOG_ERROR("unsupported method invoked: %s\n", "Smartie.hangup"); sd_bus_error_set( error, SD_BUS_ERROR_NOT_SUPPORTED, "instance does not support method Smartie.hangup"); sd_bus_reply_method_error(m, error); return -ENOTSUP; } result = ii->impl->hangup(ii, &status); if (result < 0) { CC_LOG_ERROR("failed to execute method: %s\n", strerror(-result)); sd_bus_error_setf( error, SD_BUS_ERROR_FAILED, "method implementation failed with error=%d", result); sd_bus_reply_method_error(m, error); return result; } result = sd_bus_reply_method_return(m, "i", status); if (result < 0) { CC_LOG_ERROR("unable to send method reply: %s\n", strerror(-result)); return result; } /* Successful method invocation must return >0 */ return 1; }
/* * ---------------------------------------------------------------- * Router function for any LED operations that come via dbus *---------------------------------------------------------------- */ static int led_function_router(sd_bus_message *msg, void *user_data, sd_bus_error *ret_error) { /* Generic error reporter. */ int rc = -1; /* Extract the led name from the full dbus path */ const char *led_path = sd_bus_message_get_path(msg); if(led_path == NULL) { fprintf(stderr, "Error. LED path is empty"); return sd_bus_reply_method_return(msg, "i", rc); } char *led_name = get_led_name(led_path); if(led_name == NULL) { fprintf(stderr, "Invalid LED name for path :[%s]\n",led_path); return sd_bus_reply_method_return(msg, "i", rc); } /* Now that we have the LED name, get the Operation. */ const char *led_function = sd_bus_message_get_member(msg); if(led_function == NULL) { fprintf(stderr, "Null LED function specificed for : [%s]\n",led_name); return sd_bus_reply_method_return(msg, "i", rc); } /* Route the user action to appropriate handlers. */ if( (strcmp(led_function, "setOn") == 0) || (strcmp(led_function, "setOff") == 0)) { rc = led_stable_state_function(led_name, led_function); return sd_bus_reply_method_return(msg, "i", rc); } else if( (strcmp(led_function, "setBlinkFast") == 0) || (strcmp(led_function, "setBlinkSlow") == 0)) { rc = led_default_blink(led_name, led_function); return sd_bus_reply_method_return(msg, "i", rc); } else if(strcmp(led_function, "GetLedState") == 0) { char value_str[10] = {0}; const char *led_state = NULL; rc = read_led(led_name, power_ctrl, value_str, sizeof(value_str)); if(rc >= 0) { /* LED is active low */ led_state = strtoul(value_str, NULL, 0) ? "Off" : "On"; } return sd_bus_reply_method_return(msg, "is", rc, led_state); } else { fprintf(stderr,"Invalid LED function:[%s]\n",led_function); } return sd_bus_reply_method_return(msg, "i", rc); }