Merge "Remove unused code" into tizen
[platform/core/api/gesture.git] / client / gesture_client_dbus.c
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <dlog.h>
18 #include <aul.h>
19
20 #include "gesture_client_dbus.h"
21
22 #ifdef LOG_TAG
23 #undef LOG_TAG
24 #endif
25 #define LOG_TAG "GESTURE_CLIENT_DBUS"
26
27 static int is_server_started = 0;
28
29 static hand_gesture_handtype_e g_hand_type = HAND_GESTURE_LEFT_HAND;
30 static hand_gesture_workmode_e g_work_mode = HAND_GESTURE_WORK_MODE_ONE_WAY;
31 static hand_gesture_option_e g_option = HAND_GESTURE_OPTION_DEFAULT;
32 static int g_sensitivity = 1;
33
34 static char *g_engine_app_id;
35 static char *g_engine_name;
36
37 static void _server_appeared_cb(GDBusConnection *connection, const gchar *name, const gchar *name_owner, gpointer user_data)
38 {
39         LOGD("name : %s, name_owner : %s", name, name_owner);
40 }
41
42 static void _server_vanished_cb(GDBusConnection *connection, const gchar *name, gpointer user_data)
43 {
44         LOGD("name : %s", name);
45 }
46
47 static int _dbus_init(GDBusConnection **gdbus_connection, guint *server_watcher_id)
48 {
49         GError *error = NULL;
50
51         if (*gdbus_connection == NULL) {
52                 GDBusConnection *conn = NULL;
53                 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
54                 if (conn == NULL) {
55                         if (error != NULL) {
56                                 LOGE("g_bus_get_sync error message = %s", error->message);
57                                 g_error_free(error);
58                         }
59                         return HAND_GESTURE_ERROR_OPERATION_FAILED;
60                 }
61                 *gdbus_connection = conn;
62         }
63
64         LOGD("Connected bus name : %s", g_dbus_connection_get_unique_name(*gdbus_connection));
65         if (*server_watcher_id == 0) {
66                 *server_watcher_id = g_bus_watch_name(G_BUS_TYPE_SYSTEM,
67                         GESTURE_DBUS_NAME,
68                         G_BUS_NAME_WATCHER_FLAGS_NONE,
69                         _server_appeared_cb,
70                         _server_vanished_cb,
71                         NULL, NULL);
72         }
73
74         LOGD("server_watcher_id : %d", *server_watcher_id);
75         if (*server_watcher_id == 0) {
76                 LOGE("Failed to get identifier");
77                 return HAND_GESTURE_ERROR_OPERATION_FAILED;
78         }
79
80         return HAND_GESTURE_ERROR_NONE;
81 }
82
83
84 static void _handle_gesture_cb(GDBusConnection *connection,
85                                         const gchar *sender_name,
86                                         const gchar *object_path,
87                                         const gchar *interface_name,
88                                         const gchar *signal_name,
89                                         GVariant *parameters,
90                                         gpointer user_data)
91 {
92         LOGD("own_name : %s, signal_name : %s", g_dbus_connection_get_unique_name(connection), signal_name);
93         hand_gesture_h _handle = (hand_gesture_h)user_data;
94
95         if (_handle == NULL) {
96                 LOGE("handle is not available");
97                 return;
98         }
99
100         if (parameters == NULL) {
101                 LOGE("failed to get gesture info");
102                 return;
103         }
104
105         if (g_strcmp0(signal_name, GESTURE_CLIENT_SIGNAL_GET_RESULT) == 0) {
106                 LOGD("[GESTURE_CLIENT_SIGNAL_GET_RESULT] called");
107                 int event;
108                 int gesture_type;
109                 int ltype;
110                 int levent;
111                 int lcount;
112                 g_variant_get(parameters, "(iiiii)", &event, &gesture_type, &ltype, &levent, &lcount);
113 #if 0
114                 char *printmsg = g_variant_print(parameters, true);
115                 LOGD("start parameter print : %s", printmsg);
116                 g_free(printmsg);
117                 LOGD("[event = %d] [gesture_type = %d] [ltype = %d] [levent = %d] [lcount = %d]", event, gesture_type, ltype, levent, lcount);
118 #endif
119                 if (_handle->recog_cb) {
120                         LOGD("[CLIENT DBUS] Send recognition result. gesture_type(%d)", gesture_type);
121                         _handle->recog_cb(_handle, gesture_type, 0, HAND_GESTURE_ERROR_NONE, _handle->recog_user_data);
122                 }
123         }
124         else if (g_strcmp0(signal_name, GESTURE_CLIENT_SIGNAL_GET_ERROR) == 0) {
125                 int error;
126                 char* err_msg = NULL;
127                 g_variant_get(parameters, "(is)", &error, err_msg);
128                 LOGD("[CLIENT DBUS] error(%d)", error);
129
130                 if (_handle->error_cb) {
131                         LOGD("[CLIENT DBUS] Send error");
132                         _handle->error_cb(_handle, (hand_gesture_error_e)error, err_msg, _handle->error_user_data);
133                 } else {
134                         LOGW("[CLIENT DBUS] There is no error callback");
135                 }
136         }
137         else if (g_strcmp0(signal_name, GESTURE_CLIENT_SIGNAL_GET_MOTION_STATUS) == 0) {
138
139         }
140         else if (g_strcmp0(signal_name, GESTURE_CLIENT_SIGNAL_GET_ENGINE_INFO) == 0) {
141                 g_variant_get(parameters, "(ss)", &g_engine_app_id, &g_engine_name);
142 #if 1
143                 char *printmsg = g_variant_print(parameters, true);
144                 LOGD("[engineInfo]start parameter print : %s", printmsg);
145                 g_free(printmsg);
146                 LOGE("[engineInfo][g_engine_app_id = %s] [g_engine_name = %s]", g_engine_app_id, g_engine_name);
147 #endif
148
149         }
150 }
151
152 static int _dbus_signal_init(GDBusConnection *gdbus_connection, int *monitor_id, CLIENT_LIB lib, void *data)
153 {
154         int ret = HAND_GESTURE_ERROR_NONE;
155         if (*monitor_id == 0) {
156                 int id = 0;
157                 if (lib == GESTURE_CLIENT_LIB_GESTURE) {
158                         id = g_dbus_connection_signal_subscribe(gdbus_connection,
159                                                         GESTURE_DBUS_NAME,
160                                                         GESTURE_CLIENT_INTERFACE_NAME,
161                                                         NULL,
162                                                         GESTURE_OBJECT_PATH,
163                                                         NULL,
164                                                         G_DBUS_SIGNAL_FLAGS_NONE,
165                                                         _handle_gesture_cb,
166                                                         data,
167                                                         NULL);
168                 } else if (lib == GESTURE_CLIENT_LIB_ENGINE) {
169                         LOGD("Fail to come for ENGINE lib");
170                 } else {
171                         LOGD("Fail to Not CLIENT lib");
172                 }
173
174                 LOGD("id : %d", id);
175                 if (id == 0) {
176                         ret = HAND_GESTURE_ERROR_OPERATION_FAILED;
177                         LOGE("g_dbus_connection_signal_subscribe() failed");
178                 } else {
179                         *monitor_id = id;
180                 }
181         }
182
183         return ret;
184 }
185
186 static GDBusMessage *gdbus_make_message(GVariant *body, const char *cmd)
187 {
188         LOGD("gdbus_make_message : cmd = %s", cmd);
189         GDBusMessage *message = NULL;
190         message = g_dbus_message_new_method_call(
191         GESTURE_DBUS_NAME,
192         GESTURE_OBJECT_PATH,
193         GESTURE_INTERFACE_NAME,
194         cmd);
195
196         if (!message) {
197                 LOGE("Failed to create a new gdbus message");
198                 if (body)
199                         g_variant_unref(body);
200                 return NULL;
201         }
202
203         if (body != NULL)
204                 g_dbus_message_set_body(message, body);
205
206         return message;
207 }
208
209 static int _send_message_with_sync(GDBusConnection *gdbus_connection, GDBusMessage *msg, GDBusMessage **reply, const char *cmd)
210 {
211         LOGD("_send_message_with_sync : cmd = %s", cmd);
212         int ret = HAND_GESTURE_ERROR_NONE;
213         GError *err = NULL;
214
215         gchar *printmsg = g_dbus_message_print (msg, 1);
216         LOGD("[sync] before send to server, print : %s", printmsg);
217         g_free(printmsg);
218
219         *reply = g_dbus_connection_send_message_with_reply_sync(
220                                         gdbus_connection,
221                                         msg,
222                                         G_DBUS_SEND_MESSAGE_FLAGS_NONE,
223                                         -1,
224                                         NULL,
225                                         NULL,
226                                         &err);
227
228         if (!*reply) {
229                 ret = HAND_GESTURE_ERROR_OPERATION_FAILED;
230                 if (err != NULL) {
231                         LOGE("Error occurred when sending message(%s) : %s", cmd, err->message);
232                         if (err->code == G_DBUS_ERROR_ACCESS_DENIED)
233                                 ret = HAND_GESTURE_ERROR_PERMISSION_DENIED;
234                         g_error_free(err);
235                 }
236                 return ret;
237         }
238
239         if (g_dbus_message_to_gerror(*reply, &err)) {
240                 LOGE("error message = %s, code = %d", err->message, err->code);
241                 if (err->code == G_DBUS_ERROR_ACCESS_DENIED)
242                         ret = HAND_GESTURE_ERROR_PERMISSION_DENIED;
243                 else
244                         ret = err->code;
245                 g_error_free(err);
246                 return ret;
247         }
248 #if 0
249         printmsg = g_dbus_message_print (*reply, 1);
250         LOGD("[sync] reply from server, print : %s", printmsg);
251         g_free(printmsg);
252
253         GVariant *result = NULL;
254         result = g_dbus_message_get_body(*reply);
255         if (result != NULL) {
256                 printmsg = g_variant_print(result, true);
257                 LOGD("Result msg print : %s", printmsg);
258                 g_free(printmsg);
259         }
260 #endif
261         return HAND_GESTURE_ERROR_NONE;
262 }
263
264 static int gdbus_send_message_with_sync(GDBusConnection *gdbus_connection, GVariant *body, GDBusMessage **reply, char *cmd)
265 {
266         LOGD("gdbus_send_message_with_sync start : cmd = %s", cmd);
267
268         int ret = HAND_GESTURE_ERROR_NONE;
269         GDBusMessage *msg = NULL;
270
271         msg = gdbus_make_message(body, cmd);
272         if (msg == NULL)
273                 return HAND_GESTURE_ERROR_OPERATION_FAILED;
274
275         ret = _send_message_with_sync(gdbus_connection, msg, reply, cmd);
276
277         if (msg)
278                 g_object_unref(msg);
279
280         return ret;
281 }
282
283 static void _async_cb(GDBusConnection *connection, GAsyncResult *res, gpointer user_data)
284 {
285         LOGD("_async_cb start");
286         GDBusMessage *reply = NULL;
287         GError *err = NULL;
288 //      hand_gesture_data_h gesturedata = (hand_gesture_data_h)user_data;
289
290         reply = g_dbus_connection_send_message_with_reply_finish(connection, res, &err);
291         if (reply) {
292                 if (g_dbus_message_to_gerror(reply, &err)) {
293                         LOGE("error message = %s, code = %d", err->message, err->code);
294                         g_error_free(err);
295                         return;
296                 }
297 #if 0
298                 GVariant *result = g_dbus_message_get_body(reply);
299                 gchar *printmsg = g_variant_print (result, true);
300                 LOGD("[async] _async_cb, print : %s", printmsg);
301                 g_free(printmsg);
302 #endif
303
304         } else {
305                 LOGE("There is no reply");
306                 return;
307         }
308
309         if (reply)
310                 g_object_unref(reply);
311
312         return;
313 }
314
315 static int gdbus_send_message_with_async(GDBusConnection *gdbus_connection, GVariant *body, char *cmd, hand_gesture_data_h gesture_data)
316 {
317         LOGD("gdbus_send_message_with_async start : cmd = %s", cmd);
318         int ret = HAND_GESTURE_ERROR_NONE;
319         GDBusMessage *msg = NULL;
320
321         msg = gdbus_make_message(body, cmd);
322         if (msg == NULL)
323                 return HAND_GESTURE_ERROR_OPERATION_FAILED;
324
325         g_dbus_connection_send_message_with_reply(
326                 gdbus_connection,
327                 msg,
328                 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
329                 -1,
330                 NULL,
331                 NULL,
332                 (GAsyncReadyCallback)_async_cb,
333                 gesture_data);
334
335         if (msg)
336                 g_object_unref(msg);
337
338         return ret;
339 }
340
341 static int _monitor_register(GDBusConnection *gdbus_connection)
342 {
343         int ret;
344         GDBusMessage *reply = NULL;
345         GVariant *client_body = NULL;
346
347         char appid[1024] = {0, };
348         ret = aul_app_get_appid_bypid(getpid(), appid, sizeof(appid));
349         if (ret != 0) {
350                 LOGE("aul_app_get_appid_bypid() failed : %d", ret);
351         }
352
353         client_body = g_variant_new("(iis)", 11, GESTURE_CLIENT_LIB_GESTURE, appid);
354
355         ret = gdbus_send_message_with_sync(gdbus_connection, client_body, &reply, GESTURE_MSG_SERVICE_REGISTER);
356         if (reply)
357                 g_object_unref(reply);
358
359         if (client_body)
360                 g_variant_unref(client_body);
361
362         if (ret != HAND_GESTURE_ERROR_NONE) {
363                 LOGE("gdbus_send_message_with_sync() failed : %d", ret);
364                 return ret;
365         }
366
367         is_server_started = 1;
368         return ret;
369 }
370
371 static void _on_name_appeared(GDBusConnection *connection,
372                 const gchar     *name,
373                 const gchar     *name_owner,
374                 gpointer         user_data)
375 {
376         if (is_server_started == 0)
377         _monitor_register(connection);
378 }
379
380 static void _on_name_vanished(GDBusConnection *connection,
381                 const gchar     *name,
382                 gpointer         user_data)
383 {
384         is_server_started = 0;
385 }
386
387 int gesture_client_dbus_init(GDBusConnection **gdbus_connection, guint *server_watcher_id,
388                                 int *monitor_id, int *server_monitor_id, CLIENT_LIB lib, void *data)
389 {
390         LOGD("gesture_client_dbus_init start");
391
392         int ret;
393
394         ret = _dbus_init(gdbus_connection, server_watcher_id);
395         if (ret != HAND_GESTURE_ERROR_NONE) {
396                 LOGE("_dbus_init() failed : %d", ret);
397                 return ret;
398         }
399
400         ret = _dbus_signal_init(*gdbus_connection, monitor_id, lib, data);
401         if (ret != HAND_GESTURE_ERROR_NONE) {
402                 LOGE("_dbus_signal_init() failed : %d", ret);
403                 return ret;
404         }
405
406         ret = _monitor_register(*gdbus_connection);
407         if (ret != HAND_GESTURE_ERROR_NONE) {
408                 LOGE("_monitor_register() failed : %d", ret);
409                 return ret;
410         }
411
412         if (*server_monitor_id == 0) {
413                 *server_monitor_id = g_bus_watch_name_on_connection(
414                                 *gdbus_connection,
415                                 GESTURE_DBUS_NAME,
416                                 G_BUS_NAME_WATCHER_FLAGS_NONE,
417                                 _on_name_appeared,
418                                 _on_name_vanished,
419                                 NULL,
420                                 NULL);
421                 if (*server_monitor_id == 0) {
422                         g_dbus_connection_signal_unsubscribe(*gdbus_connection, *monitor_id);
423                         *monitor_id = 0;
424                         LOGE("Failed to get identifier");
425                         return HAND_GESTURE_ERROR_OPERATION_FAILED;
426                 }
427         }
428
429         return HAND_GESTURE_ERROR_NONE;
430 }
431
432 int gesture_client_dbus_shutdown(GDBusConnection *gdbus_connection, int *server_monitor_id, int *monitor_id)
433 {
434         if (*server_monitor_id) {
435                 g_bus_unwatch_name(*server_monitor_id);
436                 *server_monitor_id = 0;
437         }
438
439         if (*monitor_id) {
440                 g_dbus_connection_signal_unsubscribe(gdbus_connection, *monitor_id);
441                 *monitor_id = 0;
442         }
443
444         return HAND_GESTURE_ERROR_NONE;
445 }
446
447 int gesture_client_dbus_initialize_engine(GDBusConnection *gdbus_connection)
448 {
449         int ret;
450         GDBusMessage *reply = NULL;
451         GVariant *body = NULL;
452
453         body = g_variant_new("()");
454         ret = gdbus_send_message_with_sync(gdbus_connection, body, &reply, GESTURE_CLIENT_MSG_INITIALIZE_ENGINE);
455         if (ret != HAND_GESTURE_ERROR_NONE)
456                 LOGE("failed to initialize");
457
458         if (body)
459                 g_variant_unref(body);
460
461         if (reply)
462                 g_object_unref(reply);
463
464         return ret;
465
466 }
467
468 int gesture_client_dbus_deinitialize_engine(GDBusConnection *gdbus_connection)
469 {
470         int ret;
471         GDBusMessage *reply = NULL;
472         GVariant *body = NULL;
473
474         body = g_variant_new("()");
475         ret = gdbus_send_message_with_sync(gdbus_connection, body, &reply, GESTURE_CLIENT_MSG_DEINITIALIZE_ENGINE);
476         if (ret != HAND_GESTURE_ERROR_NONE)
477                 LOGE("failed to deinitialize");
478
479         if (body)
480                 g_variant_unref(body);
481
482         if (reply)
483                 g_object_unref(reply);
484
485         return ret;
486
487 }
488
489 int gesture_client_dbus_set_handtype(GDBusConnection *gdbus_connection, hand_gesture_handtype_e hand_type)
490 {
491         int ret = HAND_GESTURE_ERROR_NONE;
492         if (hand_type > HAND_GESTURE_RIGHT_HAND || hand_type < HAND_GESTURE_NO_SELECTED_HAND) {
493                 LOGE("Invalid Parmeter");
494                 return HAND_GESTURE_ERROR_INVALID_PARAMETER;
495         }
496         g_hand_type = hand_type;
497
498         return ret;
499 }
500
501 int gesture_client_dbus_set_workmode(GDBusConnection *gdbus_connection, hand_gesture_workmode_e work_mode)
502 {
503         int ret = HAND_GESTURE_ERROR_NONE;
504         if (work_mode > HAND_GESTURE_WORK_MODE_UNDEFINED || work_mode < HAND_GESTURE_WORK_MODE_ONE_WAY) {
505                 LOGE("Invalid Parmeter");
506                 return HAND_GESTURE_ERROR_INVALID_PARAMETER;
507         }
508         g_work_mode = work_mode;
509
510         return ret;
511 }
512
513 int gesture_client_dbus_set_option(GDBusConnection *gdbus_connection, hand_gesture_option_e option)
514 {
515         int ret = HAND_GESTURE_ERROR_NONE;
516         if (option > HAND_GESTURE_OPTION_ALWAYS_ON || option < HAND_GESTURE_OPTION_DEFAULT) {
517                 LOGE("Invalid Parmeter");
518                 return HAND_GESTURE_ERROR_INVALID_PARAMETER;
519         }
520         g_option = option;
521
522         return ret;
523 }
524
525 int gesture_client_dbus_set_sensitivity(GDBusConnection *gdbus_connection, int sensitivity)
526 {
527         int ret = HAND_GESTURE_ERROR_NONE;
528         g_sensitivity = sensitivity;
529
530         return ret;
531 }
532
533 int gesture_client_dbus_start_recognition(GDBusConnection *gdbus_connection, hand_gesture_type_e gesture_type, hand_gesture_data_h gesture_data, hand_gesture_recognition_cb callback)
534 {
535         LOGD("gesture_client_dbus_start_recognition start");
536         LOGD("client busname: %s", g_dbus_connection_get_unique_name(gdbus_connection));
537
538         GVariant *body = NULL;
539         body = g_variant_new("(iiiii)", gesture_type, g_hand_type, g_work_mode, g_option, g_sensitivity);
540
541         gdbus_send_message_with_async(gdbus_connection, body, GESTURE_CLIENT_MSG_START_RECOGNITION, gesture_data);
542
543         if (body)
544                 g_variant_unref(body);
545
546         return HAND_GESTURE_ERROR_NONE;
547 }
548
549 int gesture_client_dbus_stop_recognition(GDBusConnection *gdbus_connection)
550 {
551         int ret;
552         GDBusMessage *reply = NULL;
553         GVariant *body = NULL;
554
555         body = g_variant_new("()");
556         ret = gdbus_send_message_with_sync(gdbus_connection, body, &reply, GESTURE_CLIENT_MSG_STOP_RECOGNITION);
557         if (ret != HAND_GESTURE_ERROR_NONE)
558                 LOGE("failed to stop gesture");
559
560         if (body)
561                 g_variant_unref(body);
562
563         if (reply)
564                 g_object_unref(reply);
565
566         return ret;
567
568 }
569
570 int gesture_client_dbus_foreach_result_time(GDBusConnection *gdbus_connection)
571 {
572         int ret;
573         GDBusMessage *reply = NULL;
574         GVariant *body = NULL;
575
576         body = g_variant_new("()");
577         ret = gdbus_send_message_with_sync(gdbus_connection, body, &reply, GESTURE_CLIENT_MSG_FOREACH_RESULT_TIME);
578         if (ret != HAND_GESTURE_ERROR_NONE)
579                 LOGE("failed to foreach_result_time");
580
581         if (body)
582                 g_variant_unref(body);
583
584         if (reply)
585                 g_object_unref(reply);
586
587         return ret;
588
589 }
590
591 int gesture_client_dbus_foreach_supported_type(GDBusConnection *gdbus_connection)
592 {
593         int ret;
594         GDBusMessage *reply = NULL;
595         GVariant *body = NULL;
596
597         body = g_variant_new("()");
598         ret = gdbus_send_message_with_sync(gdbus_connection, body, &reply, GESTURE_CLIENT_MSG_FOREACH_SUPPORTED_TYPE);
599         if (ret != HAND_GESTURE_ERROR_NONE)
600                 LOGE("failed to foreach_supported_type");
601
602         if (body)
603                 g_variant_unref(body);
604
605         if (reply)
606                 g_object_unref(reply);
607
608         return ret;
609
610 }
611
612 int gesture_client_dbus_is_support_gesture_type(GDBusConnection *gdbus_connection, hand_gesture_type_e gesture, bool* supported)
613 {
614         int ret;
615         GDBusMessage *reply = NULL;
616         GVariant *body = NULL;
617
618         body = g_variant_new("()");
619         ret = gdbus_send_message_with_sync(gdbus_connection, body, &reply, GESTURE_CLIENT_MSG_IS_SUPPORT_GESTURE_TYPE);
620         if (ret != HAND_GESTURE_ERROR_NONE)
621                 LOGE("failed to is_supported_gesture_type");
622
623         if (body)
624                 g_variant_unref(body);
625
626         if (reply)
627                 g_object_unref(reply);
628
629         return ret;
630
631 }
632
633 int gesture_client_dbus_get_engine_info(GDBusConnection *gdbus_connection, char** engine_app_id, char** engine_name)
634 {
635         int ret;
636         GDBusMessage *reply = NULL;
637         GVariant *body = NULL;
638         g_engine_app_id = *engine_app_id;
639         g_engine_name = *engine_name;
640
641         body = g_variant_new("()");
642         ret = gdbus_send_message_with_sync(gdbus_connection, body, &reply, GESTURE_CLIENT_MSG_ENGINE_GET_INFO);
643         if (ret != HAND_GESTURE_ERROR_NONE)
644                 LOGE("failed to engine_get_info");
645
646         if (body)
647                 g_variant_unref(body);
648
649         if (reply)
650                 g_object_unref(reply);
651
652         return ret;
653 }
654