Line data Source code
1 : /* SPDX-License-Identifier: Apache-2.0 */
2 : /**
3 : * NNStreamer API / Machine Learning Agent Daemon
4 : * Copyright (C) 2022 Samsung Electronics Co., Ltd. All Rights Reserved.
5 : */
6 :
7 : /**
8 : * @file pipeline-dbus-impl.cc
9 : * @date 20 Jul 2022
10 : * @brief Implementation of pipeline dbus interface.
11 : * @see https://github.com/nnstreamer/deviceMLOps.MLAgent
12 : * @author Yongjoo Ahn <yongjoo1.ahn@samsung.com>
13 : * @bug No known bugs except for NYI items
14 : * @details This implements the pipeline dbus interface.
15 : */
16 :
17 : #include <glib.h>
18 : #include <gst/gst.h>
19 : #include <stdbool.h>
20 : #include <stdio.h>
21 : #include <stdlib.h>
22 :
23 : #include "common.h"
24 : #include "dbus-interface.h"
25 : #include "gdbus-util.h"
26 : #include "log.h"
27 : #include "mlops-agent-node.h"
28 : #include "modules.h"
29 : #include "pipeline-dbus.h"
30 : #include "service-db-util.h"
31 :
32 : static MachinelearningServicePipeline *g_gdbus_instance = NULL;
33 :
34 : /**
35 : * @brief Get the skeleton object of the DBus interface.
36 : */
37 : static MachinelearningServicePipeline *
38 19 : gdbus_get_pipeline_instance (void)
39 : {
40 19 : return machinelearning_service_pipeline_skeleton_new ();
41 : }
42 :
43 : /**
44 : * @brief Put the obtained skeleton object and release the resource.
45 : */
46 : static void
47 19 : gdbus_put_pipeline_instance (MachinelearningServicePipeline **instance)
48 : {
49 19 : g_clear_object (instance);
50 19 : }
51 :
52 : /**
53 : * @brief Set the service with given description. Return the call result.
54 : */
55 : static gboolean
56 4 : dbus_cb_core_set_pipeline (MachinelearningServicePipeline *obj, GDBusMethodInvocation *invoc,
57 : const gchar *service_name, const gchar *pipeline_desc, gpointer user_data)
58 : {
59 4 : gint result = 0;
60 :
61 4 : result = svcdb_pipeline_set (service_name, pipeline_desc);
62 4 : machinelearning_service_pipeline_complete_set_pipeline (obj, invoc, result);
63 :
64 4 : return TRUE;
65 : }
66 :
67 : /**
68 : * @brief Get the pipeline description of the given service. Return the call result and the pipeline description.
69 : */
70 : static gboolean
71 2 : dbus_cb_core_get_pipeline (MachinelearningServicePipeline *obj,
72 : GDBusMethodInvocation *invoc, const gchar *service_name, gpointer user_data)
73 : {
74 2 : gint result = 0;
75 2 : g_autofree gchar *desc = NULL;
76 :
77 2 : result = svcdb_pipeline_get (service_name, &desc);
78 2 : machinelearning_service_pipeline_complete_get_pipeline (obj, invoc, result, desc);
79 :
80 2 : return TRUE;
81 2 : }
82 :
83 : /**
84 : * @brief Delete the pipeline description of the given service. Return the call result.
85 : */
86 : static gboolean
87 5 : dbus_cb_core_delete_pipeline (MachinelearningServicePipeline *obj,
88 : GDBusMethodInvocation *invoc, const gchar *service_name, gpointer user_data)
89 : {
90 5 : gint result = 0;
91 :
92 5 : result = svcdb_pipeline_delete (service_name);
93 5 : machinelearning_service_pipeline_complete_delete_pipeline (obj, invoc, result);
94 :
95 5 : return TRUE;
96 : }
97 :
98 : /**
99 : * @brief Launch the pipeline with given description. Return the call result and its id.
100 : */
101 : static gboolean
102 3 : dbus_cb_core_launch_pipeline (MachinelearningServicePipeline *obj,
103 : GDBusMethodInvocation *invoc, const gchar *service_name, gpointer user_data)
104 : {
105 3 : gint result = 0;
106 3 : gint64 id = -1;
107 :
108 3 : result = mlops_node_create (service_name, MLOPS_NODE_TYPE_PIPELINE, &id);
109 3 : machinelearning_service_pipeline_complete_launch_pipeline (obj, invoc, result, id);
110 :
111 3 : return TRUE;
112 : }
113 :
114 : /**
115 : * @brief Start the pipeline with given id. Return the call result.
116 : */
117 : static gboolean
118 2 : dbus_cb_core_start_pipeline (MachinelearningServicePipeline *obj,
119 : GDBusMethodInvocation *invoc, gint64 id, gpointer user_data)
120 : {
121 2 : gint result = 0;
122 :
123 2 : result = mlops_node_start (id);
124 2 : machinelearning_service_pipeline_complete_start_pipeline (obj, invoc, result);
125 :
126 2 : return TRUE;
127 : }
128 :
129 : /**
130 : * @brief Stop the pipeline with given id. Return the call result.
131 : */
132 : static gboolean
133 2 : dbus_cb_core_stop_pipeline (MachinelearningServicePipeline *obj,
134 : GDBusMethodInvocation *invoc, gint64 id, gpointer user_data)
135 : {
136 2 : gint result = 0;
137 :
138 2 : result = mlops_node_stop (id);
139 2 : machinelearning_service_pipeline_complete_stop_pipeline (obj, invoc, result);
140 :
141 2 : return TRUE;
142 : }
143 :
144 : /**
145 : * @brief Destroy the pipeline with given id. Return the call result.
146 : */
147 : static gboolean
148 3 : dbus_cb_core_destroy_pipeline (MachinelearningServicePipeline *obj,
149 : GDBusMethodInvocation *invoc, gint64 id, gpointer user_data)
150 : {
151 3 : gint result = 0;
152 :
153 3 : result = mlops_node_destroy (id);
154 3 : machinelearning_service_pipeline_complete_destroy_pipeline (obj, invoc, result);
155 :
156 3 : return TRUE;
157 : }
158 :
159 : /**
160 : * @brief Get the state of pipeline with given id. Return the call result and its state.
161 : */
162 : static gboolean
163 3 : dbus_cb_core_get_state (MachinelearningServicePipeline *obj,
164 : GDBusMethodInvocation *invoc, gint64 id, gpointer user_data)
165 : {
166 3 : gint result = 0;
167 3 : GstState state = GST_STATE_NULL;
168 :
169 3 : result = mlops_node_get_state (id, &state);
170 3 : machinelearning_service_pipeline_complete_get_state (obj, invoc, result, (gint) state);
171 :
172 3 : return TRUE;
173 : }
174 :
175 : static struct gdbus_signal_info handler_infos[] = {
176 : {
177 : .signal_name = DBUS_PIPELINE_I_SET_HANDLER,
178 : .cb = G_CALLBACK (dbus_cb_core_set_pipeline),
179 : .cb_data = NULL,
180 : .handler_id = 0,
181 : },
182 : {
183 : .signal_name = DBUS_PIPELINE_I_GET_HANDLER,
184 : .cb = G_CALLBACK (dbus_cb_core_get_pipeline),
185 : .cb_data = NULL,
186 : .handler_id = 0,
187 : },
188 : {
189 : .signal_name = DBUS_PIPELINE_I_DELETE_HANDLER,
190 : .cb = G_CALLBACK (dbus_cb_core_delete_pipeline),
191 : .cb_data = NULL,
192 : .handler_id = 0,
193 : },
194 : {
195 : .signal_name = DBUS_PIPELINE_I_LAUNCH_HANDLER,
196 : .cb = G_CALLBACK (dbus_cb_core_launch_pipeline),
197 : .cb_data = NULL,
198 : .handler_id = 0,
199 : },
200 : {
201 : .signal_name = DBUS_PIPELINE_I_START_HANDLER,
202 : .cb = G_CALLBACK (dbus_cb_core_start_pipeline),
203 : .cb_data = NULL,
204 : .handler_id = 0,
205 : },
206 : {
207 : .signal_name = DBUS_PIPELINE_I_STOP_HANDLER,
208 : .cb = G_CALLBACK (dbus_cb_core_stop_pipeline),
209 : .cb_data = NULL,
210 : .handler_id = 0,
211 : },
212 : {
213 : .signal_name = DBUS_PIPELINE_I_DESTROY_HANDLER,
214 : .cb = G_CALLBACK (dbus_cb_core_destroy_pipeline),
215 : .cb_data = NULL,
216 : .handler_id = 0,
217 : },
218 : {
219 : .signal_name = DBUS_PIPELINE_I_GET_STATE_HANDLER,
220 : .cb = G_CALLBACK (dbus_cb_core_get_state),
221 : .cb_data = NULL,
222 : .handler_id = 0,
223 : },
224 : };
225 :
226 : /**
227 : * @brief Probe the D-BUS and connect this module.
228 : */
229 : static int
230 19 : probe_pipeline_module (void *data)
231 : {
232 19 : int ret = 0;
233 :
234 19 : g_gdbus_instance = gdbus_get_pipeline_instance ();
235 19 : if (g_gdbus_instance == NULL) {
236 0 : ml_loge ("cannot get a dbus instance for the %s interface\n", DBUS_PIPELINE_INTERFACE);
237 0 : return -ENOSYS;
238 : }
239 :
240 19 : ret = gdbus_connect_signal (g_gdbus_instance, ARRAY_SIZE (handler_infos), handler_infos);
241 19 : if (ret < 0) {
242 0 : ml_loge ("cannot register callbacks as the dbus method invocation handlers\n ret: %d", ret);
243 0 : ret = -ENOSYS;
244 0 : goto out;
245 : }
246 :
247 19 : ret = gdbus_export_interface (g_gdbus_instance, DBUS_PIPELINE_PATH);
248 19 : if (ret < 0) {
249 0 : ml_loge ("cannot export the dbus interface '%s' at the object path '%s'\n",
250 : DBUS_PIPELINE_INTERFACE, DBUS_PIPELINE_PATH);
251 0 : ret = -ENOSYS;
252 0 : goto out_disconnect;
253 : }
254 :
255 19 : return 0;
256 :
257 0 : out_disconnect:
258 0 : gdbus_disconnect_signal (g_gdbus_instance, ARRAY_SIZE (handler_infos), handler_infos);
259 0 : out:
260 0 : gdbus_put_pipeline_instance (&g_gdbus_instance);
261 :
262 0 : return ret;
263 : }
264 :
265 : /**
266 : * @brief Initialize this module.
267 : */
268 : static void
269 19 : init_pipeline_module (void *data)
270 : {
271 19 : gdbus_initialize ();
272 19 : }
273 :
274 : /**
275 : * @brief Finalize this module.
276 : */
277 : static void
278 19 : exit_pipeline_module (void *data)
279 : {
280 19 : gdbus_disconnect_signal (g_gdbus_instance, ARRAY_SIZE (handler_infos), handler_infos);
281 19 : gdbus_put_pipeline_instance (&g_gdbus_instance);
282 19 : }
283 :
284 : static const struct module_ops pipeline_ops = {
285 : .name = "pipeline",
286 : .probe = probe_pipeline_module,
287 : .init = init_pipeline_module,
288 : .exit = exit_pipeline_module,
289 : };
290 :
291 48 : MODULE_OPS_REGISTER (&pipeline_ops)
|