Line data Source code
1 : /* SPDX-License-Identifier: Apache-2.0 */
2 : /**
3 : * Copyright (c) 2022 Samsung Electronics Co., Ltd. All Rights Reserved.
4 : *
5 : * @file model-dbus-impl.cc
6 : * @date 29 Jul 2022
7 : * @brief DBus implementation for Model 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 "model-dbus.h"
21 : #include "modules.h"
22 : #include "service-db-util.h"
23 :
24 : static MachinelearningServiceModel *g_gdbus_instance = NULL;
25 :
26 : /**
27 : * @brief Utility function to get the DBus proxy of Model interface.
28 : */
29 : static MachinelearningServiceModel *
30 19 : gdbus_get_model_instance (void)
31 : {
32 19 : return machinelearning_service_model_skeleton_new ();
33 : }
34 :
35 : /**
36 : * @brief Utility function to release DBus proxy of Model interface.
37 : */
38 : static void
39 19 : gdbus_put_model_instance (MachinelearningServiceModel **instance)
40 : {
41 19 : g_clear_object (instance);
42 19 : }
43 :
44 : /**
45 : * @brief The callback function of Register method
46 : *
47 : * @param obj Proxy instance.
48 : * @param invoc Method invocation handle.
49 : * @param name The name of target model.
50 : * @param path The file path of target.
51 : * @param is_active The active status of target model.
52 : * @param description The description of target model.
53 : * @return @c TRUE if the request is handled. FALSE if the service is not available.
54 : */
55 : static gboolean
56 9 : gdbus_cb_model_register (MachinelearningServiceModel *obj,
57 : GDBusMethodInvocation *invoc, const gchar *name, const gchar *path,
58 : const bool is_active, const gchar *description, const gchar *app_info)
59 : {
60 9 : gint ret = 0;
61 9 : guint version = 0U;
62 :
63 9 : ret = svcdb_model_add (name, path, is_active, description, app_info, &version);
64 9 : machinelearning_service_model_complete_register (obj, invoc, version, ret);
65 :
66 9 : return TRUE;
67 : }
68 :
69 : /**
70 : * @brief The callback function of update description method
71 : *
72 : * @param obj Proxy instance.
73 : * @param invoc Method invocation handle.
74 : * @param name The name of target model.
75 : * @param version The version of target model.
76 : * @param description The description of target model.
77 : * @return @c TRUE if the request is handled. FALSE if the service is not available.
78 : */
79 : static gboolean
80 3 : gdbus_cb_model_update_description (MachinelearningServiceModel *obj,
81 : GDBusMethodInvocation *invoc, const gchar *name, const guint version,
82 : const gchar *description)
83 : {
84 3 : gint ret = 0;
85 :
86 3 : ret = svcdb_model_update_description (name, version, description);
87 3 : machinelearning_service_model_complete_update_description (obj, invoc, ret);
88 :
89 3 : return TRUE;
90 : }
91 :
92 : /**
93 : * @brief The callback function of activate method
94 : *
95 : * @param obj Proxy instance.
96 : * @param invoc Method invocation handle.
97 : * @param name The name of target model.
98 : * @param version The version of target model.
99 : * @return @c TRUE if the request is handled. FALSE if the service is not available.
100 : */
101 : static gboolean
102 3 : gdbus_cb_model_activate (MachinelearningServiceModel *obj,
103 : GDBusMethodInvocation *invoc, const gchar *name, const guint version)
104 : {
105 3 : gint ret = 0;
106 :
107 3 : ret = svcdb_model_activate (name, version);
108 3 : machinelearning_service_model_complete_activate (obj, invoc, ret);
109 :
110 3 : return TRUE;
111 : }
112 :
113 : /**
114 : * @brief The callback function of get method
115 : *
116 : * @param obj Proxy instance.
117 : * @param invoc Method invocation handle.
118 : * @param name The name of target model.
119 : * @return @c TRUE if the request is handled. FALSE if the service is not available.
120 : */
121 : static gboolean
122 3 : gdbus_cb_model_get (MachinelearningServiceModel *obj,
123 : GDBusMethodInvocation *invoc, const gchar *name, const guint version)
124 : {
125 3 : gint ret = 0;
126 3 : g_autofree gchar *model_info = NULL;
127 :
128 3 : ret = svcdb_model_get (name, version, &model_info);
129 3 : machinelearning_service_model_complete_get (obj, invoc, model_info, ret);
130 :
131 3 : return TRUE;
132 3 : }
133 :
134 : /**
135 : * @brief The callback function of get activated method
136 : *
137 : * @param obj Proxy instance.
138 : * @param invoc Method invocation handle.
139 : * @param name The name of target model.
140 : * @return @c TRUE if the request is handled. FALSE if the service is not available.
141 : */
142 : static gboolean
143 3 : gdbus_cb_model_get_activated (MachinelearningServiceModel *obj,
144 : GDBusMethodInvocation *invoc, const gchar *name)
145 : {
146 3 : gint ret = 0;
147 3 : g_autofree gchar *model_info = NULL;
148 :
149 3 : ret = svcdb_model_get_activated (name, &model_info);
150 3 : machinelearning_service_model_complete_get_activated (obj, invoc, model_info, ret);
151 :
152 3 : return TRUE;
153 3 : }
154 :
155 : /**
156 : * @brief The callback function of get all method
157 : *
158 : * @param obj Proxy instance.
159 : * @param invoc Method invocation handle.
160 : * @param name The name of target model.
161 : * @return @c TRUE if the request is handled. FALSE if the service is not available.
162 : */
163 : static gboolean
164 4 : gdbus_cb_model_get_all (MachinelearningServiceModel *obj,
165 : GDBusMethodInvocation *invoc, const gchar *name)
166 : {
167 4 : gint ret = 0;
168 4 : g_autofree gchar *model_info = NULL;
169 :
170 4 : ret = svcdb_model_get_all (name, &model_info);
171 4 : machinelearning_service_model_complete_get_all (obj, invoc, model_info, ret);
172 :
173 4 : return TRUE;
174 4 : }
175 :
176 : /**
177 : * @brief The callback function of delete method
178 : *
179 : * @param obj Proxy instance.
180 : * @param invoc Method invocation handle.
181 : * @param name The name of target model.
182 : * @param version The version of target model.
183 : * @param force If the force is set to @c TRUE, the target model will be forced to delete.
184 : * @return @c TRUE if the request is handled. FALSE if the service is not available.
185 : */
186 : static gboolean
187 10 : gdbus_cb_model_delete (MachinelearningServiceModel *obj,
188 : GDBusMethodInvocation *invoc, const gchar *name, const guint version, const gboolean force)
189 : {
190 10 : gint ret = 0;
191 :
192 10 : ret = svcdb_model_delete (name, version, force);
193 10 : machinelearning_service_model_complete_delete (obj, invoc, ret);
194 :
195 10 : return TRUE;
196 : }
197 :
198 : /**
199 : * @brief Event handler list of Model interface
200 : */
201 : static struct gdbus_signal_info handler_infos[] = {
202 : {
203 : .signal_name = DBUS_MODEL_I_HANDLER_REGISTER,
204 : .cb = G_CALLBACK (gdbus_cb_model_register),
205 : .cb_data = NULL,
206 : .handler_id = 0,
207 : },
208 : {
209 : .signal_name = DBUS_MODEL_I_HANDLER_UPDATE_DESCRIPTION,
210 : .cb = G_CALLBACK (gdbus_cb_model_update_description),
211 : .cb_data = NULL,
212 : .handler_id = 0,
213 : },
214 : {
215 : .signal_name = DBUS_MODEL_I_HANDLER_ACTIVATE,
216 : .cb = G_CALLBACK (gdbus_cb_model_activate),
217 : .cb_data = NULL,
218 : .handler_id = 0,
219 : },
220 : {
221 : .signal_name = DBUS_MODEL_I_HANDLER_GET,
222 : .cb = G_CALLBACK (gdbus_cb_model_get),
223 : .cb_data = NULL,
224 : .handler_id = 0,
225 : },
226 : {
227 : .signal_name = DBUS_MODEL_I_HANDLER_GET_ACTIVATED,
228 : .cb = G_CALLBACK (gdbus_cb_model_get_activated),
229 : .cb_data = NULL,
230 : .handler_id = 0,
231 : },
232 : {
233 : .signal_name = DBUS_MODEL_I_HANDLER_GET_ALL,
234 : .cb = G_CALLBACK (gdbus_cb_model_get_all),
235 : .cb_data = NULL,
236 : .handler_id = 0,
237 : },
238 : {
239 : .signal_name = DBUS_MODEL_I_HANDLER_DELETE,
240 : .cb = G_CALLBACK (gdbus_cb_model_delete),
241 : .cb_data = NULL,
242 : .handler_id = 0,
243 : },
244 : };
245 :
246 : /**
247 : * @brief The callback function for probing Model Interface module.
248 : */
249 : static int
250 19 : probe_model_module (void *data)
251 : {
252 19 : int ret = 0;
253 :
254 19 : ml_logd ("probe_model_module");
255 :
256 19 : g_gdbus_instance = gdbus_get_model_instance ();
257 19 : if (NULL == g_gdbus_instance) {
258 0 : ml_loge ("cannot get a dbus instance for the %s interface\n", DBUS_MODEL_INTERFACE);
259 0 : return -ENOSYS;
260 : }
261 :
262 19 : ret = gdbus_connect_signal (g_gdbus_instance, ARRAY_SIZE (handler_infos), handler_infos);
263 19 : if (ret < 0) {
264 0 : ml_loge ("cannot register callbacks as the dbus method invocation handlers\n ret: %d", ret);
265 0 : ret = -ENOSYS;
266 0 : goto out;
267 : }
268 :
269 19 : ret = gdbus_export_interface (g_gdbus_instance, DBUS_MODEL_PATH);
270 19 : if (ret < 0) {
271 0 : ml_loge ("cannot export the dbus interface '%s' at the object path '%s'\n",
272 : DBUS_MODEL_INTERFACE, DBUS_MODEL_PATH);
273 0 : ret = -ENOSYS;
274 0 : goto out_disconnect;
275 : }
276 :
277 19 : return 0;
278 :
279 0 : out_disconnect:
280 0 : gdbus_disconnect_signal (g_gdbus_instance, ARRAY_SIZE (handler_infos), handler_infos);
281 :
282 0 : out:
283 0 : gdbus_put_model_instance (&g_gdbus_instance);
284 :
285 0 : return ret;
286 : }
287 :
288 : /**
289 : * @brief The callback function for initializing Model Interface module.
290 : */
291 : static void
292 19 : init_model_module (void *data)
293 : {
294 19 : gdbus_initialize ();
295 19 : }
296 :
297 : /**
298 : * @brief The callback function for exiting Model Interface module.
299 : */
300 : static void
301 19 : exit_model_module (void *data)
302 : {
303 19 : gdbus_disconnect_signal (g_gdbus_instance, ARRAY_SIZE (handler_infos), handler_infos);
304 19 : gdbus_put_model_instance (&g_gdbus_instance);
305 19 : }
306 :
307 : static const struct module_ops model_ops = {
308 : .name = "model-interface",
309 : .probe = probe_model_module,
310 : .init = init_model_module,
311 : .exit = exit_model_module,
312 : };
313 :
314 48 : MODULE_OPS_REGISTER (&model_ops)
|