Line data Source code
1 : /* SPDX-License-Identifier: Apache-2.0 */
2 : /**
3 : * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved.
4 : *
5 : * @file resource-dbus-impl.cc
6 : * @date 17 July 2023
7 : * @brief DBus implementation for Resource Interface
8 : * @see https://github.com/nnstreamer/deviceMLOps.MLAgent
9 : * @author Sangjung Woo <sangjung.woo@samsung.com>
10 : * @bug No known bugs except for NYI items
11 : */
12 :
13 : #include <errno.h>
14 : #include <glib.h>
15 :
16 : #include "common.h"
17 : #include "dbus-interface.h"
18 : #include "gdbus-util.h"
19 : #include "log.h"
20 : #include "modules.h"
21 : #include "resource-dbus.h"
22 : #include "service-db-util.h"
23 :
24 : static MachinelearningServiceResource *g_gdbus_res_instance = NULL;
25 :
26 : /**
27 : * @brief Utility function to get the DBus proxy.
28 : */
29 : static MachinelearningServiceResource *
30 19 : gdbus_get_resource_instance (void)
31 : {
32 19 : return machinelearning_service_resource_skeleton_new ();
33 : }
34 :
35 : /**
36 : * @brief Utility function to release DBus proxy.
37 : */
38 : static void
39 19 : gdbus_put_resource_instance (MachinelearningServiceResource **instance)
40 : {
41 19 : g_clear_object (instance);
42 19 : }
43 :
44 : /**
45 : * @brief The callback function of Add method
46 : * @param obj Proxy instance.
47 : * @param invoc Method invocation handle.
48 : * @param name The name of target resource.
49 : * @param path The file path of target.
50 : * @param description The description of the resource.
51 : * @return @c TRUE if the request is handled. FALSE if the service is not available.
52 : */
53 : static gboolean
54 6 : gdbus_cb_resource_add (MachinelearningServiceResource *obj, GDBusMethodInvocation *invoc,
55 : const gchar *name, const gchar *path, const gchar *description, const gchar *app_info)
56 : {
57 6 : gint ret = 0;
58 :
59 6 : ret = svcdb_resource_add (name, path, description, app_info);
60 6 : machinelearning_service_resource_complete_add (obj, invoc, ret);
61 :
62 6 : return TRUE;
63 : }
64 :
65 : /**
66 : * @brief The callback function of get method
67 : * @param obj Proxy instance.
68 : * @param invoc Method invocation handle.
69 : * @param name The name of target resource.
70 : * @return @c TRUE if the request is handled. FALSE if the service is not available.
71 : */
72 : static gboolean
73 3 : gdbus_cb_resource_get (MachinelearningServiceResource *obj,
74 : GDBusMethodInvocation *invoc, const gchar *name)
75 : {
76 3 : gint ret = 0;
77 3 : g_autofree gchar *res_info = NULL;
78 :
79 3 : ret = svcdb_resource_get (name, &res_info);
80 3 : machinelearning_service_resource_complete_get (obj, invoc, res_info, ret);
81 :
82 3 : return TRUE;
83 3 : }
84 :
85 : /**
86 : * @brief The callback function of delete method
87 : * @param obj Proxy instance.
88 : * @param invoc Method invocation handle.
89 : * @param name The name of target resource.
90 : * @return @c TRUE if the request is handled. FALSE if the service is not available.
91 : */
92 : static gboolean
93 4 : gdbus_cb_resource_delete (MachinelearningServiceResource *obj,
94 : GDBusMethodInvocation *invoc, const gchar *name)
95 : {
96 4 : gint ret = 0;
97 :
98 4 : ret = svcdb_resource_delete (name);
99 4 : machinelearning_service_resource_complete_delete (obj, invoc, ret);
100 :
101 4 : return TRUE;
102 : }
103 :
104 : /**
105 : * @brief Event handler list of resource interface
106 : */
107 : static struct gdbus_signal_info res_handler_infos[] = {
108 : {
109 : .signal_name = DBUS_RESOURCE_I_HANDLER_ADD,
110 : .cb = G_CALLBACK (gdbus_cb_resource_add),
111 : .cb_data = NULL,
112 : .handler_id = 0,
113 : },
114 : {
115 : .signal_name = DBUS_RESOURCE_I_HANDLER_GET,
116 : .cb = G_CALLBACK (gdbus_cb_resource_get),
117 : .cb_data = NULL,
118 : .handler_id = 0,
119 : },
120 : {
121 : .signal_name = DBUS_RESOURCE_I_HANDLER_DELETE,
122 : .cb = G_CALLBACK (gdbus_cb_resource_delete),
123 : .cb_data = NULL,
124 : .handler_id = 0,
125 : },
126 : };
127 :
128 : /**
129 : * @brief The callback function for probing resource Interface module.
130 : */
131 : static int
132 19 : probe_resource_module (void *data)
133 : {
134 19 : int ret = 0;
135 :
136 19 : ml_logd ("probe_resource_module");
137 :
138 19 : g_gdbus_res_instance = gdbus_get_resource_instance ();
139 19 : if (NULL == g_gdbus_res_instance) {
140 0 : ml_loge ("cannot get a dbus instance for the %s interface\n", DBUS_RESOURCE_INTERFACE);
141 0 : return -ENOSYS;
142 : }
143 :
144 19 : ret = gdbus_connect_signal (
145 : g_gdbus_res_instance, ARRAY_SIZE (res_handler_infos), res_handler_infos);
146 19 : if (ret < 0) {
147 0 : ml_loge ("cannot register callbacks as the dbus method invocation handlers\n ret: %d", ret);
148 0 : ret = -ENOSYS;
149 0 : goto out;
150 : }
151 :
152 19 : ret = gdbus_export_interface (g_gdbus_res_instance, DBUS_RESOURCE_PATH);
153 19 : if (ret < 0) {
154 0 : ml_loge ("cannot export the dbus interface '%s' at the object path '%s'\n",
155 : DBUS_RESOURCE_INTERFACE, DBUS_RESOURCE_PATH);
156 0 : ret = -ENOSYS;
157 0 : goto out_disconnect;
158 : }
159 :
160 19 : return 0;
161 :
162 0 : out_disconnect:
163 0 : gdbus_disconnect_signal (
164 : g_gdbus_res_instance, ARRAY_SIZE (res_handler_infos), res_handler_infos);
165 :
166 0 : out:
167 0 : gdbus_put_resource_instance (&g_gdbus_res_instance);
168 :
169 0 : return ret;
170 : }
171 :
172 : /**
173 : * @brief The callback function for initializing resource interface module.
174 : */
175 : static void
176 19 : init_resource_module (void *data)
177 : {
178 19 : gdbus_initialize ();
179 19 : }
180 :
181 : /**
182 : * @brief The callback function for exiting resource interface module.
183 : */
184 : static void
185 19 : exit_resource_module (void *data)
186 : {
187 19 : gdbus_disconnect_signal (
188 : g_gdbus_res_instance, ARRAY_SIZE (res_handler_infos), res_handler_infos);
189 19 : gdbus_put_resource_instance (&g_gdbus_res_instance);
190 19 : }
191 :
192 : static const struct module_ops resource_ops = {
193 : .name = "resource-interface",
194 : .probe = probe_resource_module,
195 : .init = init_resource_module,
196 : .exit = exit_resource_module,
197 : };
198 :
199 48 : MODULE_OPS_REGISTER (&resource_ops)
|