2 * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include "gesture_client_dbus.h"
25 #define LOG_TAG "GESTURE_CLIENT_DBUS"
27 static int is_server_started = 0;
28 static hand_gesture_recognition_cb g_callback;
29 static hand_gesture_data_h g_gesture_data;
31 static hand_gesture_handtype_e g_hand_type = HAND_GESTURE_LEFT_HAND;
32 static hand_gesture_workmode_e g_work_mode = HAND_GESTURE_WORK_MODE_ONE_WAY;
33 static hand_gesture_option_e g_option = HAND_GESTURE_OPTION_DEFAULT;
34 static int g_sensitivity = 1;
36 static char *g_engine_app_id;
37 static char *g_engine_name;
39 static void _free_gesture_data(hand_gesture_data_h gesture_data)
45 static void _server_appeared_cb(GDBusConnection *connection, const gchar *name, const gchar *name_owner, gpointer user_data)
47 LOGD("name : %s, name_owner : %s", name, name_owner);
50 static void _server_vanished_cb(GDBusConnection *connection, const gchar *name, gpointer user_data)
52 LOGD("name : %s", name);
55 static int _dbus_init(GDBusConnection **gdbus_connection, guint *server_watcher_id)
59 if (*gdbus_connection == NULL) {
60 GDBusConnection *conn = NULL;
61 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
64 LOGE("g_bus_get_sync error message = %s", error->message);
67 return HAND_GESTURE_ERROR_OPERATION_FAILED;
69 *gdbus_connection = conn;
72 LOGD("Connected bus name : %s", g_dbus_connection_get_unique_name(*gdbus_connection));
73 if (*server_watcher_id == 0) {
74 *server_watcher_id = g_bus_watch_name(G_BUS_TYPE_SYSTEM,
76 G_BUS_NAME_WATCHER_FLAGS_NONE,
82 LOGD("server_watcher_id : %d", *server_watcher_id);
83 if (*server_watcher_id == 0) {
84 LOGE("Failed to get identifier");
85 return HAND_GESTURE_ERROR_OPERATION_FAILED;
88 return HAND_GESTURE_ERROR_NONE;
92 static void _handle_gesture_cb(GDBusConnection *connection,
93 const gchar *sender_name,
94 const gchar *object_path,
95 const gchar *interface_name,
96 const gchar *signal_name,
100 LOGD("own_name : %s, signal_name : %s", g_dbus_connection_get_unique_name(connection), signal_name);
101 hand_gesture_h _handle = (hand_gesture_h)user_data;
103 if (_handle == NULL) {
104 LOGE("handle is not available");
108 if (parameters == NULL) {
109 LOGE("failed to get gesture info");
113 if (g_strcmp0(signal_name, GESTURE_CLIENT_SIGNAL_GET_RESULT) == 0) {
114 LOGD("[GESTURE_CLIENT_SIGNAL_GET_RESULT] called");
120 g_variant_get(parameters, "(iiiii)", &event, &gesture_type, <ype, &levent, &lcount);
122 char *printmsg = g_variant_print(parameters, true);
123 LOGD("start parameter print : %s", printmsg);
125 LOGD("[event = %d] [gesture_type = %d] [ltype = %d] [levent = %d] [lcount = %d]", event, gesture_type, ltype, levent, lcount);
127 if (g_gesture_data == NULL){
128 LOGD("Can't send the result to Client because g_gesture_data is NULL");
131 LOGD("address = %p", g_gesture_data);
132 g_gesture_data->gesture_type = ltype;
133 g_gesture_data->event = levent;
134 g_gesture_data->detected_Count = lcount;
136 g_callback(gesture_type, g_gesture_data, 0, HAND_GESTURE_ERROR_NONE, user_data);
138 else if (g_strcmp0(signal_name, GESTURE_CLIENT_SIGNAL_GET_ERROR) == 0) {
141 else if (g_strcmp0(signal_name, GESTURE_CLIENT_SIGNAL_GET_MOTION_STATUS) == 0) {
144 else if (g_strcmp0(signal_name, GESTURE_CLIENT_SIGNAL_GET_ENGINE_INFO) == 0) {
145 g_variant_get(parameters, "(ss)", &g_engine_app_id, &g_engine_name);
147 char *printmsg = g_variant_print(parameters, true);
148 LOGD("[engineInfo]start parameter print : %s", printmsg);
150 LOGE("[engineInfo][g_engine_app_id = %s] [g_engine_name = %s]", g_engine_app_id, g_engine_name);
156 static int _dbus_signal_init(GDBusConnection *gdbus_connection, int *monitor_id, CLIENT_LIB lib, void *data)
158 int ret = HAND_GESTURE_ERROR_NONE;
159 if (*monitor_id == 0) {
161 if (lib == GESTURE_CLIENT_LIB_GESTURE) {
162 id = g_dbus_connection_signal_subscribe(gdbus_connection,
164 GESTURE_CLIENT_INTERFACE_NAME,
168 G_DBUS_SIGNAL_FLAGS_NONE,
172 } else if (lib == GESTURE_CLIENT_LIB_ENGINE) {
173 LOGD("Fail to come for ENGINE lib");
175 LOGD("Fail to Not CLIENT lib");
180 ret = HAND_GESTURE_ERROR_OPERATION_FAILED;
181 LOGE("g_dbus_connection_signal_subscribe() failed");
190 static GDBusMessage *gdbus_make_message(GVariant *body, const char *cmd)
192 LOGD("gdbus_make_message : cmd = %s", cmd);
193 GDBusMessage *message = NULL;
194 message = g_dbus_message_new_method_call(
197 GESTURE_INTERFACE_NAME,
201 LOGE("Failed to create a new gdbus message");
203 g_variant_unref(body);
208 g_dbus_message_set_body(message, body);
213 static int _send_message_with_sync(GDBusConnection *gdbus_connection, GDBusMessage *msg, GDBusMessage **reply, const char *cmd)
215 LOGD("_send_message_with_sync : cmd = %s", cmd);
216 int ret = HAND_GESTURE_ERROR_NONE;
219 gchar *printmsg = g_dbus_message_print (msg, 1);
220 LOGD("[sync] before send to server, print : %s", printmsg);
223 *reply = g_dbus_connection_send_message_with_reply_sync(
226 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
233 ret = HAND_GESTURE_ERROR_OPERATION_FAILED;
235 LOGE("Error occurred when sending message(%s) : %s", cmd, err->message);
236 if (err->code == G_DBUS_ERROR_ACCESS_DENIED)
237 ret = HAND_GESTURE_ERROR_PERMISSION_DENIED;
243 if (g_dbus_message_to_gerror(*reply, &err)) {
244 LOGE("error message = %s, code = %d", err->message, err->code);
245 if (err->code == G_DBUS_ERROR_ACCESS_DENIED)
246 ret = HAND_GESTURE_ERROR_PERMISSION_DENIED;
253 printmsg = g_dbus_message_print (*reply, 1);
254 LOGD("[sync] reply from server, print : %s", printmsg);
257 GVariant *result = NULL;
258 result = g_dbus_message_get_body(*reply);
259 if (result != NULL) {
260 printmsg = g_variant_print(result, true);
261 LOGD("Result msg print : %s", printmsg);
265 return HAND_GESTURE_ERROR_NONE;
268 static int gdbus_send_message_with_sync(GDBusConnection *gdbus_connection, GVariant *body, GDBusMessage **reply, char *cmd)
270 LOGD("gdbus_send_message_with_sync start : cmd = %s", cmd);
272 int ret = HAND_GESTURE_ERROR_NONE;
273 GDBusMessage *msg = NULL;
275 msg = gdbus_make_message(body, cmd);
277 return HAND_GESTURE_ERROR_OPERATION_FAILED;
279 ret = _send_message_with_sync(gdbus_connection, msg, reply, cmd);
287 static void _async_cb(GDBusConnection *connection, GAsyncResult *res, gpointer user_data)
289 LOGD("_async_cb start");
290 GDBusMessage *reply = NULL;
292 // hand_gesture_data_h gesturedata = (hand_gesture_data_h)user_data;
294 reply = g_dbus_connection_send_message_with_reply_finish(connection, res, &err);
296 if (g_dbus_message_to_gerror(reply, &err)) {
297 LOGE("error message = %s, code = %d", err->message, err->code);
302 GVariant *result = g_dbus_message_get_body(reply);
303 gchar *printmsg = g_variant_print (result, true);
304 LOGD("[async] _async_cb, print : %s", printmsg);
309 LOGE("There is no reply");
314 g_object_unref(reply);
319 static int gdbus_send_message_with_async(GDBusConnection *gdbus_connection, GVariant *body, char *cmd, hand_gesture_data_h gesture_data)
321 LOGD("gdbus_send_message_with_async start : cmd = %s", cmd);
322 int ret = HAND_GESTURE_ERROR_NONE;
323 GDBusMessage *msg = NULL;
325 msg = gdbus_make_message(body, cmd);
327 return HAND_GESTURE_ERROR_OPERATION_FAILED;
329 g_dbus_connection_send_message_with_reply(
332 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
336 (GAsyncReadyCallback)_async_cb,
345 static int _monitor_register(GDBusConnection *gdbus_connection)
348 GDBusMessage *reply = NULL;
349 GVariant *client_body = NULL;
351 char appid[1024] = {0, };
352 ret = aul_app_get_appid_bypid(getpid(), appid, sizeof(appid));
354 LOGE("aul_app_get_appid_bypid() failed : %d", ret);
357 client_body = g_variant_new("(iis)", 11, GESTURE_CLIENT_LIB_GESTURE, appid);
359 ret = gdbus_send_message_with_sync(gdbus_connection, client_body, &reply, GESTURE_MSG_SERVICE_REGISTER);
361 g_object_unref(reply);
364 g_variant_unref(client_body);
366 if (ret != HAND_GESTURE_ERROR_NONE) {
367 LOGE("gdbus_send_message_with_sync() failed : %d", ret);
371 is_server_started = 1;
375 static void _on_name_appeared(GDBusConnection *connection,
377 const gchar *name_owner,
380 if (is_server_started == 0)
381 _monitor_register(connection);
384 static void _on_name_vanished(GDBusConnection *connection,
388 is_server_started = 0;
391 int gesture_client_dbus_init(GDBusConnection **gdbus_connection, guint *server_watcher_id,
392 int *monitor_id, int *server_monitor_id, CLIENT_LIB lib, void *data)
394 LOGD("gesture_client_dbus_init start");
398 ret = _dbus_init(gdbus_connection, server_watcher_id);
399 if (ret != HAND_GESTURE_ERROR_NONE) {
400 LOGE("_dbus_init() failed : %d", ret);
404 ret = _dbus_signal_init(*gdbus_connection, monitor_id, lib, data);
405 if (ret != HAND_GESTURE_ERROR_NONE) {
406 LOGE("_dbus_signal_init() failed : %d", ret);
410 ret = _monitor_register(*gdbus_connection);
411 if (ret != HAND_GESTURE_ERROR_NONE) {
412 LOGE("_monitor_register() failed : %d", ret);
416 if (*server_monitor_id == 0) {
417 *server_monitor_id = g_bus_watch_name_on_connection(
420 G_BUS_NAME_WATCHER_FLAGS_NONE,
425 if (*server_monitor_id == 0) {
426 g_dbus_connection_signal_unsubscribe(*gdbus_connection, *monitor_id);
428 LOGE("Failed to get identifier");
429 return HAND_GESTURE_ERROR_OPERATION_FAILED;
433 return HAND_GESTURE_ERROR_NONE;
436 int gesture_client_dbus_shutdown(GDBusConnection *gdbus_connection, int *server_monitor_id, int *monitor_id)
438 if (*server_monitor_id) {
439 g_bus_unwatch_name(*server_monitor_id);
440 *server_monitor_id = 0;
444 g_dbus_connection_signal_unsubscribe(gdbus_connection, *monitor_id);
448 return HAND_GESTURE_ERROR_NONE;
451 int gesture_client_dbus_initialize_engine(GDBusConnection *gdbus_connection)
454 GDBusMessage *reply = NULL;
455 GVariant *body = NULL;
457 body = g_variant_new("()");
458 ret = gdbus_send_message_with_sync(gdbus_connection, body, &reply, GESTURE_CLIENT_MSG_INITIALIZE_ENGINE);
459 if (ret != HAND_GESTURE_ERROR_NONE)
460 LOGE("failed to initialize");
463 g_variant_unref(body);
466 g_object_unref(reply);
472 int gesture_client_dbus_deinitialize_engine(GDBusConnection *gdbus_connection)
475 GDBusMessage *reply = NULL;
476 GVariant *body = NULL;
478 body = g_variant_new("()");
479 ret = gdbus_send_message_with_sync(gdbus_connection, body, &reply, GESTURE_CLIENT_MSG_DEINITIALIZE_ENGINE);
480 if (ret != HAND_GESTURE_ERROR_NONE)
481 LOGE("failed to deinitialize");
484 g_variant_unref(body);
487 g_object_unref(reply);
493 int gesture_client_dbus_set_handtype(GDBusConnection *gdbus_connection, hand_gesture_handtype_e hand_type)
495 int ret = HAND_GESTURE_ERROR_NONE;
496 if (hand_type > HAND_GESTURE_RIGHT_HAND || hand_type < HAND_GESTURE_NO_SELECTED_HAND) {
497 LOGE("Invalid Parmeter");
498 return HAND_GESTURE_ERROR_INVALID_PARAMETER;
500 g_hand_type = hand_type;
505 int gesture_client_dbus_set_workmode(GDBusConnection *gdbus_connection, hand_gesture_workmode_e work_mode)
507 int ret = HAND_GESTURE_ERROR_NONE;
508 if (work_mode > HAND_GESTURE_WORK_MODE_UNDEFINED || work_mode < HAND_GESTURE_WORK_MODE_ONE_WAY) {
509 LOGE("Invalid Parmeter");
510 return HAND_GESTURE_ERROR_INVALID_PARAMETER;
512 g_work_mode = work_mode;
517 int gesture_client_dbus_set_option(GDBusConnection *gdbus_connection, hand_gesture_option_e option)
519 int ret = HAND_GESTURE_ERROR_NONE;
520 if (option > HAND_GESTURE_OPTION_ALWAYS_ON || option < HAND_GESTURE_OPTION_DEFAULT) {
521 LOGE("Invalid Parmeter");
522 return HAND_GESTURE_ERROR_INVALID_PARAMETER;
529 int gesture_client_dbus_set_sensitivity(GDBusConnection *gdbus_connection, int sensitivity)
531 int ret = HAND_GESTURE_ERROR_NONE;
532 g_sensitivity = sensitivity;
537 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)
539 LOGD("gesture_client_dbus_start_recognition start");
540 LOGD("client busname: %s", g_dbus_connection_get_unique_name(gdbus_connection));
541 g_callback = callback;
542 g_gesture_data = gesture_data;
544 GVariant *body = NULL;
545 body = g_variant_new("(iiiii)", gesture_type, g_hand_type, g_work_mode, g_option, g_sensitivity);
547 gdbus_send_message_with_async(gdbus_connection, body, GESTURE_CLIENT_MSG_START_RECOGNITION, gesture_data);
550 g_variant_unref(body);
552 return HAND_GESTURE_ERROR_NONE;
555 int gesture_client_dbus_stop_recognition(GDBusConnection *gdbus_connection)
558 GDBusMessage *reply = NULL;
559 GVariant *body = NULL;
561 /* free for gesture data struct */
562 _free_gesture_data(g_gesture_data);
564 body = g_variant_new("()");
565 ret = gdbus_send_message_with_sync(gdbus_connection, body, &reply, GESTURE_CLIENT_MSG_STOP_RECOGNITION);
566 if (ret != HAND_GESTURE_ERROR_NONE)
567 LOGE("failed to stop gesture");
570 g_variant_unref(body);
573 g_object_unref(reply);
579 int gesture_client_dbus_foreach_result_time(GDBusConnection *gdbus_connection)
582 GDBusMessage *reply = NULL;
583 GVariant *body = NULL;
585 body = g_variant_new("()");
586 ret = gdbus_send_message_with_sync(gdbus_connection, body, &reply, GESTURE_CLIENT_MSG_FOREACH_RESULT_TIME);
587 if (ret != HAND_GESTURE_ERROR_NONE)
588 LOGE("failed to foreach_result_time");
591 g_variant_unref(body);
594 g_object_unref(reply);
600 int gesture_client_dbus_foreach_supported_type(GDBusConnection *gdbus_connection)
603 GDBusMessage *reply = NULL;
604 GVariant *body = NULL;
606 body = g_variant_new("()");
607 ret = gdbus_send_message_with_sync(gdbus_connection, body, &reply, GESTURE_CLIENT_MSG_FOREACH_SUPPORTED_TYPE);
608 if (ret != HAND_GESTURE_ERROR_NONE)
609 LOGE("failed to foreach_supported_type");
612 g_variant_unref(body);
615 g_object_unref(reply);
621 int gesture_client_dbus_is_support_gesture_type(GDBusConnection *gdbus_connection, hand_gesture_type_e gesture, bool* supported)
624 GDBusMessage *reply = NULL;
625 GVariant *body = NULL;
627 body = g_variant_new("()");
628 ret = gdbus_send_message_with_sync(gdbus_connection, body, &reply, GESTURE_CLIENT_MSG_IS_SUPPORT_GESTURE_TYPE);
629 if (ret != HAND_GESTURE_ERROR_NONE)
630 LOGE("failed to is_supported_gesture_type");
633 g_variant_unref(body);
636 g_object_unref(reply);
642 int gesture_client_dbus_engine_get_info(GDBusConnection *gdbus_connection, char** engine_app_id, char** engine_name)
645 GDBusMessage *reply = NULL;
646 GVariant *body = NULL;
647 g_engine_app_id = *engine_app_id;
648 g_engine_name = *engine_name;
650 body = g_variant_new("()");
651 ret = gdbus_send_message_with_sync(gdbus_connection, body, &reply, GESTURE_CLIENT_MSG_ENGINE_GET_INFO);
652 if (ret != HAND_GESTURE_ERROR_NONE)
653 LOGE("failed to engine_get_info");
656 g_variant_unref(body);
659 g_object_unref(reply);