/* Creates a web service instance */ struct duda_service *duda_service_create(struct duda *d, char *root, char *log, char *data, char *html, char *service) { int ret; void *handle; struct duda_service *ds; struct duda_api_objects *api; if (!d) { return NULL; } /* Check access to root, log, data and html directories */ if (root) { ret = validate_dir(root); if (ret != 0) { return NULL; } } if (log) { ret = validate_dir(log); if (ret != 0) { return NULL; } } if (data) { ret = validate_dir(log); if (ret != 0) { return NULL; } } if (html) { ret = validate_dir(html); if (ret != 0) { return NULL; } } /* Validate the web service file */ handle = dlopen(service, RTLD_LAZY); if (!handle) { fprintf(stderr, "Error opening web service file '%s'\n", service); return NULL; } /* Create web service context */ ds = mk_mem_alloc_z(sizeof(struct duda_service)); if (!ds) { dlclose(handle); return NULL; } /* Root prefix path */ if (root) { ds->path_root = mk_string_dup(root); } /* * If root prefix path is set, for all incoming paths that are not * absolute, we prefix the path */ if (log) { ds->path_log = service_path(root, log); } if (data) { ds->path_data = service_path(root, data); } if (html) { ds->path_html = service_path(root, html); } if (service) { ds->path_service = service_path(root, service); } ds->dl_handle = handle; mk_list_add(&ds->_head, &d->services); /* Initialize references for API objects */ mk_list_init(&ds->router_list); api = duda_api_create(); map_internals(ds, api); return ds; }
DeviceDbus(GType type, const string& name, const string& desc, DeviceConf_t& conf) : DeviceBase(name, desc, conf) { guint result; DBusGProxy *busProxy; GError* error = NULL; string service_path(SMARTGATEWAY_SERVICE_PATH_PREFIX); service_path += name; busProxy = dbus_g_proxy_new_for_name(DEV_AGENT->GetDbusSession(), DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS); if (busProxy == NULL) { printf("Error: could not create dbus proxy for name registration\n"); return; } if (!dbus_g_proxy_call(busProxy, /* Method name to call. */ "RequestName", /* Where to store the GError. */ &error, /* Parameter type of argument 0. Note that since we're dealing with GLib/D-Bus wrappers, you will need to find a suitable GType instead of using the "native" D-Bus type codes. */ G_TYPE_STRING, /* Data of argument 0. In our case, this is the well-known name for our server example ("org.asu.smartgateway"). */ service_path.c_str(), /* Parameter type of argument 1. */ G_TYPE_UINT, /* Data of argument 0. This is the "flags" argument of the "RequestName" method which can be use to specify what the bus service should do when the name already exists on the bus. We'll go with defaults. */ 0, /* Input arguments are terminated with a special GType marker. */ G_TYPE_INVALID, /* Parameter type of return lighting 0. For "RequestName" it is UINT32 so we pick the GType that maps into UINT32 in the wrappers. */ G_TYPE_UINT, /* Data of return lighting 0. These will always be pointers to the locations where the proxy can copy the results. */ &result, /* Terminate list of return lightings. */ G_TYPE_INVALID)) { printf("D-Bus.RequestName RPC failed: %s\n", error->message); /* Note that the whole call failed, not "just" the name registration (we deal with that below). This means that something bad probably has happened and there's not much we can do (hence program termination). */ return; } /* Check the result code of the registration RPC. */ printf("RequestName returned %d for %s.\n", result, service_path.c_str()); m_obj = (DbusObjectType *)g_object_new(type, NULL); if (m_obj == NULL) { g_print("Failed to create one gobj instance.\n"); return; } /* * Store the back pointer here so that when the D-Bus "set_status" * is invoked, we can use the LightingDevice obj from the LightingObject obj. */ m_obj->m_dev_ptr = static_cast<void *>(this); g_print("Registering DbusObject on the D-Bus.\n"); /* * The function does not return any status, so can't check for * errors here. */ dbus_g_connection_register_g_object(DEV_AGENT->GetDbusSession(), (SMARTGATEWAY_SERVICE_OBJECT_PATH_PREFIX+name).c_str(), G_OBJECT(m_obj)); g_print("%s ready to serve requests\n", name.c_str()); }