Line data Source code
1 : /* SPDX-License-Identifier: Apache-2.0 */
2 : /**
3 : * @file mlops-agent-interface.c
4 : * @date 5 April 2023
5 : * @brief A set of exported ml-agent interfaces for managing pipelines, models, and other service.
6 : * @see https://github.com/nnstreamer/deviceMLOps.MLAgent
7 : * @author Wook Song <wook16.song@samsung.com>
8 : * @bug No known bugs except for NYI items
9 : */
10 :
11 : #include <errno.h>
12 : #include <glib.h>
13 : #include <stdint.h>
14 :
15 : #include "include/mlops-agent-interface.h"
16 : #include "dbus-interface.h"
17 : #include "model-dbus.h"
18 : #include "pipeline-dbus.h"
19 : #include "resource-dbus.h"
20 :
21 : #define STR_IS_VALID(s) ((s) && (s)[0] != '\0')
22 :
23 : typedef enum
24 : {
25 : ML_AGENT_SERVICE_PIPELINE = 0,
26 : ML_AGENT_SERVICE_MODEL,
27 : ML_AGENT_SERVICE_RESOURCE,
28 : ML_AGENT_SERVICE_END
29 : } ml_agent_service_type_e;
30 :
31 : typedef gpointer ml_agent_proxy_h;
32 :
33 : /**
34 : * @brief An internal helper to get the dbus proxy
35 : */
36 : static ml_agent_proxy_h
37 0 : _get_proxy_new_for_bus_sync (ml_agent_service_type_e type)
38 : {
39 : static const GBusType bus_types[] = { G_BUS_TYPE_SYSTEM, G_BUS_TYPE_SESSION };
40 : static const size_t num_bus_types =
41 : sizeof (bus_types) / sizeof (bus_types[0]);
42 0 : ml_agent_proxy_h proxy = NULL;
43 : size_t i;
44 :
45 0 : switch (type) {
46 0 : case ML_AGENT_SERVICE_PIPELINE:
47 : {
48 : MachinelearningServicePipeline *mlsp;
49 :
50 0 : for (i = 0; i < num_bus_types; ++i) {
51 0 : mlsp = machinelearning_service_pipeline_proxy_new_for_bus_sync
52 : (bus_types[i], G_DBUS_PROXY_FLAGS_NONE, DBUS_ML_BUS_NAME,
53 : DBUS_PIPELINE_PATH, NULL, NULL);
54 0 : if (mlsp) {
55 0 : break;
56 : }
57 : }
58 0 : proxy = (ml_agent_proxy_h) mlsp;
59 0 : break;
60 : }
61 0 : case ML_AGENT_SERVICE_MODEL:
62 : {
63 : MachinelearningServiceModel *mlsm;
64 :
65 0 : for (i = 0; i < num_bus_types; ++i) {
66 0 : mlsm = machinelearning_service_model_proxy_new_for_bus_sync
67 : (bus_types[i], G_DBUS_PROXY_FLAGS_NONE, DBUS_ML_BUS_NAME,
68 : DBUS_MODEL_PATH, NULL, NULL);
69 0 : if (mlsm)
70 0 : break;
71 : }
72 0 : proxy = (ml_agent_proxy_h) mlsm;
73 0 : break;
74 : }
75 0 : case ML_AGENT_SERVICE_RESOURCE:
76 : {
77 : MachinelearningServiceResource *mlsr;
78 :
79 0 : for (i = 0; i < num_bus_types; ++i) {
80 0 : mlsr = machinelearning_service_resource_proxy_new_for_bus_sync
81 : (bus_types[i], G_DBUS_PROXY_FLAGS_NONE, DBUS_ML_BUS_NAME,
82 : DBUS_RESOURCE_PATH, NULL, NULL);
83 0 : if (mlsr)
84 0 : break;
85 : }
86 0 : proxy = (ml_agent_proxy_h) mlsr;
87 0 : break;
88 : }
89 0 : default:
90 0 : break;
91 : }
92 :
93 0 : return proxy;
94 : }
95 :
96 : /**
97 : * @brief An interface exported for setting the description of a pipeline.
98 : */
99 : int
100 0 : ml_agent_pipeline_set_description (const char *name, const char *pipeline_desc)
101 : {
102 : MachinelearningServicePipeline *mlsp;
103 : gboolean result;
104 :
105 0 : if (!STR_IS_VALID (name) || !STR_IS_VALID (pipeline_desc)) {
106 0 : g_return_val_if_reached (-EINVAL);
107 : }
108 :
109 0 : mlsp = _get_proxy_new_for_bus_sync (ML_AGENT_SERVICE_PIPELINE);
110 0 : if (!mlsp) {
111 0 : g_return_val_if_reached (-EIO);
112 : }
113 :
114 0 : result = machinelearning_service_pipeline_call_set_pipeline_sync (mlsp,
115 : name, pipeline_desc, NULL, NULL, NULL);
116 0 : g_object_unref (mlsp);
117 :
118 0 : g_return_val_if_fail (result, -EIO);
119 0 : return 0;
120 : }
121 :
122 : /**
123 : * @brief An interface exported for getting the pipeline's description corresponding to the given @a name.
124 : */
125 : int
126 0 : ml_agent_pipeline_get_description (const char *name, char **pipeline_desc)
127 : {
128 : MachinelearningServicePipeline *mlsp;
129 : gboolean result;
130 : gint ret;
131 :
132 0 : if (!STR_IS_VALID (name) || !pipeline_desc) {
133 0 : g_return_val_if_reached (-EINVAL);
134 : }
135 :
136 0 : mlsp = _get_proxy_new_for_bus_sync (ML_AGENT_SERVICE_PIPELINE);
137 0 : if (!mlsp) {
138 0 : g_return_val_if_reached (-EIO);
139 : }
140 :
141 0 : result = machinelearning_service_pipeline_call_get_pipeline_sync (mlsp,
142 : name, &ret, pipeline_desc, NULL, NULL);
143 0 : g_object_unref (mlsp);
144 :
145 0 : g_return_val_if_fail (ret == 0 && result, ret);
146 0 : return 0;
147 : }
148 :
149 : /**
150 : * @brief An interface exported for deletion of the pipeline's description corresponding to the given @a name.
151 : */
152 : int
153 0 : ml_agent_pipeline_delete (const char *name)
154 : {
155 : MachinelearningServicePipeline *mlsp;
156 : gboolean result;
157 : gint ret;
158 :
159 0 : if (!STR_IS_VALID (name)) {
160 0 : g_return_val_if_reached (-EINVAL);
161 : }
162 :
163 0 : mlsp = _get_proxy_new_for_bus_sync (ML_AGENT_SERVICE_PIPELINE);
164 0 : if (!mlsp) {
165 0 : g_return_val_if_reached (-EIO);
166 : }
167 :
168 0 : result = machinelearning_service_pipeline_call_delete_pipeline_sync (mlsp,
169 : name, &ret, NULL, NULL);
170 0 : g_object_unref (mlsp);
171 :
172 0 : g_return_val_if_fail (ret == 0 && result, ret);
173 0 : return 0;
174 : }
175 :
176 : /**
177 : * @brief An interface exported for launching the pipeline's description corresponding to the given @a name.
178 : */
179 : int
180 0 : ml_agent_pipeline_launch (const char *name, int64_t * id)
181 : {
182 : MachinelearningServicePipeline *mlsp;
183 : gboolean result;
184 : gint ret;
185 :
186 0 : if (!STR_IS_VALID (name) || !id) {
187 0 : g_return_val_if_reached (-EINVAL);
188 : }
189 :
190 0 : mlsp = _get_proxy_new_for_bus_sync (ML_AGENT_SERVICE_PIPELINE);
191 0 : if (!mlsp) {
192 0 : g_return_val_if_reached (-EIO);
193 : }
194 :
195 0 : result = machinelearning_service_pipeline_call_launch_pipeline_sync (mlsp,
196 : name, &ret, id, NULL, NULL);
197 0 : g_object_unref (mlsp);
198 :
199 0 : g_return_val_if_fail (ret == 0 && result, ret);
200 0 : return 0;
201 : }
202 :
203 : /**
204 : * @brief An interface exported for changing the pipeline's state of the given @a id to start.
205 : */
206 : int
207 0 : ml_agent_pipeline_start (const int64_t id)
208 : {
209 : MachinelearningServicePipeline *mlsp;
210 : gboolean result;
211 : gint ret;
212 :
213 0 : mlsp = _get_proxy_new_for_bus_sync (ML_AGENT_SERVICE_PIPELINE);
214 0 : if (!mlsp) {
215 0 : g_return_val_if_reached (-EIO);
216 : }
217 :
218 0 : result = machinelearning_service_pipeline_call_start_pipeline_sync (mlsp,
219 : id, &ret, NULL, NULL);
220 0 : g_object_unref (mlsp);
221 :
222 0 : g_return_val_if_fail (ret == 0 && result, ret);
223 0 : return 0;
224 : }
225 :
226 : /**
227 : * @brief An interface exported for changing the pipeline's state of the given @a id to stop.
228 : */
229 : int
230 0 : ml_agent_pipeline_stop (const int64_t id)
231 : {
232 : MachinelearningServicePipeline *mlsp;
233 : gboolean result;
234 : gint ret;
235 :
236 0 : mlsp = _get_proxy_new_for_bus_sync (ML_AGENT_SERVICE_PIPELINE);
237 0 : if (!mlsp) {
238 0 : g_return_val_if_reached (-EIO);
239 : }
240 :
241 0 : result = machinelearning_service_pipeline_call_stop_pipeline_sync (mlsp,
242 : id, &ret, NULL, NULL);
243 0 : g_object_unref (mlsp);
244 :
245 0 : g_return_val_if_fail (ret == 0 && result, ret);
246 0 : return 0;
247 : }
248 :
249 : /**
250 : * @brief An interface exported for destroying a launched pipeline corresponding to the given @a id.
251 : */
252 : int
253 0 : ml_agent_pipeline_destroy (const int64_t id)
254 : {
255 : MachinelearningServicePipeline *mlsp;
256 : gboolean result;
257 : gint ret;
258 :
259 0 : mlsp = _get_proxy_new_for_bus_sync (ML_AGENT_SERVICE_PIPELINE);
260 0 : if (!mlsp) {
261 0 : g_return_val_if_reached (-EIO);
262 : }
263 :
264 0 : result = machinelearning_service_pipeline_call_destroy_pipeline_sync (mlsp,
265 : id, &ret, NULL, NULL);
266 0 : g_object_unref (mlsp);
267 :
268 0 : g_return_val_if_fail (ret == 0 && result, ret);
269 0 : return 0;
270 : }
271 :
272 : /**
273 : * @brief An interface exported for getting the pipeline's state of the given @a id.
274 : */
275 : int
276 0 : ml_agent_pipeline_get_state (const int64_t id, int *state)
277 : {
278 : MachinelearningServicePipeline *mlsp;
279 : gboolean result;
280 : gint ret;
281 :
282 0 : if (!state) {
283 0 : g_return_val_if_reached (-EINVAL);
284 : }
285 :
286 0 : mlsp = _get_proxy_new_for_bus_sync (ML_AGENT_SERVICE_PIPELINE);
287 0 : if (!mlsp) {
288 0 : g_return_val_if_reached (-EIO);
289 : }
290 :
291 0 : result = machinelearning_service_pipeline_call_get_state_sync (mlsp,
292 : id, &ret, state, NULL, NULL);
293 0 : g_object_unref (mlsp);
294 :
295 0 : g_return_val_if_fail (ret == 0 && result, ret);
296 0 : return 0;
297 : }
298 :
299 : /**
300 : * @brief An interface exported for registering a model.
301 : */
302 : int
303 0 : ml_agent_model_register (const char *name, const char *path,
304 : const int activate, const char *description, const char *app_info,
305 : uint32_t * version)
306 : {
307 : MachinelearningServiceModel *mlsm;
308 : gboolean result;
309 : gint ret;
310 :
311 0 : if (!STR_IS_VALID (name) || !STR_IS_VALID (path) || !version) {
312 0 : g_return_val_if_reached (-EINVAL);
313 : }
314 :
315 0 : mlsm = _get_proxy_new_for_bus_sync (ML_AGENT_SERVICE_MODEL);
316 0 : if (!mlsm) {
317 0 : g_return_val_if_reached (-EIO);
318 : }
319 :
320 0 : result = machinelearning_service_model_call_register_sync (mlsm, name, path,
321 : activate, description ? description : "", app_info ? app_info : "",
322 : version, &ret, NULL, NULL);
323 0 : g_object_unref (mlsm);
324 :
325 0 : g_return_val_if_fail (ret == 0 && result, ret);
326 0 : return 0;
327 : }
328 :
329 : /**
330 : * @brief An interface exported for updating the description of the model with @a name and @a version.
331 : */
332 : int
333 0 : ml_agent_model_update_description (const char *name,
334 : const uint32_t version, const char *description)
335 : {
336 : MachinelearningServiceModel *mlsm;
337 : gboolean result;
338 : gint ret;
339 :
340 0 : if (!STR_IS_VALID (name) || !STR_IS_VALID (description) || version == 0U) {
341 0 : g_return_val_if_reached (-EINVAL);
342 : }
343 :
344 0 : mlsm = _get_proxy_new_for_bus_sync (ML_AGENT_SERVICE_MODEL);
345 0 : if (!mlsm) {
346 0 : g_return_val_if_reached (-EIO);
347 : }
348 :
349 0 : result = machinelearning_service_model_call_update_description_sync (mlsm,
350 : name, version, description, &ret, NULL, NULL);
351 0 : g_object_unref (mlsm);
352 :
353 0 : g_return_val_if_fail (ret == 0 && result, ret);
354 0 : return 0;
355 : }
356 :
357 : /**
358 : * @brief An interface exported for activating the model with @a name and @a version.
359 : */
360 : int
361 0 : ml_agent_model_activate (const char *name, const uint32_t version)
362 : {
363 : MachinelearningServiceModel *mlsm;
364 : gboolean result;
365 : gint ret;
366 :
367 0 : if (!STR_IS_VALID (name) || version == 0U) {
368 0 : g_return_val_if_reached (-EINVAL);
369 : }
370 :
371 0 : mlsm = _get_proxy_new_for_bus_sync (ML_AGENT_SERVICE_MODEL);
372 0 : if (!mlsm) {
373 0 : g_return_val_if_reached (-EIO);
374 : }
375 :
376 0 : result = machinelearning_service_model_call_activate_sync (mlsm,
377 : name, version, &ret, NULL, NULL);
378 0 : g_object_unref (mlsm);
379 :
380 0 : g_return_val_if_fail (ret == 0 && result, ret);
381 0 : return 0;
382 : }
383 :
384 : /**
385 : * @brief An interface exported for getting the information of the model with @a name and @a version.
386 : */
387 : int
388 0 : ml_agent_model_get (const char *name, const uint32_t version, char **model_info)
389 : {
390 : MachinelearningServiceModel *mlsm;
391 : gboolean result;
392 : gint ret;
393 :
394 0 : if (!STR_IS_VALID (name) || !model_info || version == 0U) {
395 0 : g_return_val_if_reached (-EINVAL);
396 : }
397 :
398 0 : mlsm = _get_proxy_new_for_bus_sync (ML_AGENT_SERVICE_MODEL);
399 0 : if (!mlsm) {
400 0 : g_return_val_if_reached (-EIO);
401 : }
402 :
403 0 : result = machinelearning_service_model_call_get_sync (mlsm,
404 : name, version, model_info, &ret, NULL, NULL);
405 0 : g_object_unref (mlsm);
406 :
407 0 : g_return_val_if_fail (ret == 0 && result, ret);
408 0 : return 0;
409 : }
410 :
411 : /**
412 : * @brief An interface exported for getting the information of the activated model with @a name.
413 : */
414 : int
415 0 : ml_agent_model_get_activated (const char *name, char **model_info)
416 : {
417 : MachinelearningServiceModel *mlsm;
418 : gboolean result;
419 : gint ret;
420 :
421 0 : if (!STR_IS_VALID (name) || !model_info) {
422 0 : g_return_val_if_reached (-EINVAL);
423 : }
424 :
425 0 : mlsm = _get_proxy_new_for_bus_sync (ML_AGENT_SERVICE_MODEL);
426 0 : if (!mlsm) {
427 0 : g_return_val_if_reached (-EIO);
428 : }
429 :
430 0 : result = machinelearning_service_model_call_get_activated_sync (mlsm,
431 : name, model_info, &ret, NULL, NULL);
432 0 : g_object_unref (mlsm);
433 :
434 0 : g_return_val_if_fail (ret == 0 && result, ret);
435 0 : return 0;
436 : }
437 :
438 : /**
439 : * @brief An interface exported for getting the information of all the models corresponding to the given @a name.
440 : */
441 : int
442 0 : ml_agent_model_get_all (const char *name, char **model_info)
443 : {
444 : MachinelearningServiceModel *mlsm;
445 : gboolean result;
446 : gint ret;
447 :
448 0 : if (!STR_IS_VALID (name) || !model_info) {
449 0 : g_return_val_if_reached (-EINVAL);
450 : }
451 :
452 0 : mlsm = _get_proxy_new_for_bus_sync (ML_AGENT_SERVICE_MODEL);
453 0 : if (!mlsm) {
454 0 : g_return_val_if_reached (-EIO);
455 : }
456 :
457 0 : result = machinelearning_service_model_call_get_all_sync (mlsm,
458 : name, model_info, &ret, NULL, NULL);
459 0 : g_object_unref (mlsm);
460 :
461 0 : g_return_val_if_fail (ret == 0 && result, ret);
462 0 : return 0;
463 : }
464 :
465 : /**
466 : * @brief An interface exported for removing the model of @a name and @a version.
467 : * @details If @a force is true, this will delete the model even if it is activated.
468 : */
469 : int
470 0 : ml_agent_model_delete (const char *name, const uint32_t version,
471 : const int force)
472 : {
473 : MachinelearningServiceModel *mlsm;
474 : gboolean result;
475 : gint ret;
476 :
477 0 : if (!STR_IS_VALID (name)) {
478 0 : g_return_val_if_reached (-EINVAL);
479 : }
480 :
481 0 : mlsm = _get_proxy_new_for_bus_sync (ML_AGENT_SERVICE_MODEL);
482 0 : if (!mlsm) {
483 0 : g_return_val_if_reached (-EIO);
484 : }
485 :
486 0 : result = machinelearning_service_model_call_delete_sync (mlsm,
487 : name, version, force, &ret, NULL, NULL);
488 0 : g_object_unref (mlsm);
489 :
490 0 : g_return_val_if_fail (ret == 0 && result, ret);
491 0 : return 0;
492 : }
493 :
494 : /**
495 : * @brief An interface exported for adding the resource.
496 : */
497 : int
498 0 : ml_agent_resource_add (const char *name, const char *path,
499 : const char *description, const char *app_info)
500 : {
501 : MachinelearningServiceResource *mlsr;
502 : gboolean result;
503 : gint ret;
504 :
505 0 : if (!STR_IS_VALID (name) || !STR_IS_VALID (path)) {
506 0 : g_return_val_if_reached (-EINVAL);
507 : }
508 :
509 0 : mlsr = _get_proxy_new_for_bus_sync (ML_AGENT_SERVICE_RESOURCE);
510 0 : if (!mlsr) {
511 0 : g_return_val_if_reached (-EIO);
512 : }
513 :
514 0 : result = machinelearning_service_resource_call_add_sync (mlsr, name, path,
515 : description ? description : "", app_info ? app_info : "",
516 : &ret, NULL, NULL);
517 0 : g_object_unref (mlsr);
518 :
519 0 : g_return_val_if_fail (ret == 0 && result, ret);
520 0 : return 0;
521 : }
522 :
523 : /**
524 : * @brief An interface exported for removing the resource with @a name.
525 : */
526 : int
527 0 : ml_agent_resource_delete (const char *name)
528 : {
529 : MachinelearningServiceResource *mlsr;
530 : gboolean result;
531 : gint ret;
532 :
533 0 : if (!STR_IS_VALID (name)) {
534 0 : g_return_val_if_reached (-EINVAL);
535 : }
536 :
537 0 : mlsr = _get_proxy_new_for_bus_sync (ML_AGENT_SERVICE_RESOURCE);
538 0 : if (!mlsr) {
539 0 : g_return_val_if_reached (-EIO);
540 : }
541 :
542 0 : result = machinelearning_service_resource_call_delete_sync (mlsr,
543 : name, &ret, NULL, NULL);
544 0 : g_object_unref (mlsr);
545 :
546 0 : g_return_val_if_fail (ret == 0 && result, ret);
547 0 : return 0;
548 : }
549 :
550 : /**
551 : * @brief An interface exported for getting the description of the resource with @a name.
552 : */
553 : int
554 0 : ml_agent_resource_get (const char *name, char **res_info)
555 : {
556 : MachinelearningServiceResource *mlsr;
557 : gboolean result;
558 : gint ret;
559 :
560 0 : if (!STR_IS_VALID (name) || !res_info) {
561 0 : g_return_val_if_reached (-EINVAL);
562 : }
563 :
564 0 : mlsr = _get_proxy_new_for_bus_sync (ML_AGENT_SERVICE_RESOURCE);
565 0 : if (!mlsr) {
566 0 : g_return_val_if_reached (-EIO);
567 : }
568 :
569 0 : result = machinelearning_service_resource_call_get_sync (mlsr,
570 : name, res_info, &ret, NULL, NULL);
571 0 : g_object_unref (mlsr);
572 :
573 0 : g_return_val_if_fail (ret == 0 && result, ret);
574 0 : return 0;
575 : }
|