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;
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;
34 static char *g_engine_app_id;
35 static char *g_engine_name;
37 static void _server_appeared_cb(GDBusConnection *connection, const gchar *name, const gchar *name_owner, gpointer user_data)
39 LOGD("name : %s, name_owner : %s", name, name_owner);
42 static void _server_vanished_cb(GDBusConnection *connection, const gchar *name, gpointer user_data)
44 LOGD("name : %s", name);
47 static int _dbus_init(GDBusConnection **gdbus_connection, guint *server_watcher_id)
51 if (*gdbus_connection == NULL) {
52 GDBusConnection *conn = NULL;
53 conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
56 LOGE("g_bus_get_sync error message = %s", error->message);
59 return HAND_GESTURE_ERROR_OPERATION_FAILED;
61 *gdbus_connection = conn;
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,
68 G_BUS_NAME_WATCHER_FLAGS_NONE,
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;
80 return HAND_GESTURE_ERROR_NONE;
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,
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;
95 if (_handle == NULL) {
96 LOGE("handle is not available");
100 if (parameters == NULL) {
101 LOGE("failed to get gesture info");
105 if (g_strcmp0(signal_name, GESTURE_CLIENT_SIGNAL_GET_RESULT) == 0) {
106 LOGD("[GESTURE_CLIENT_SIGNAL_GET_RESULT] called");
112 g_variant_get(parameters, "(iiiii)", &event, &gesture_type, <ype, &levent, &lcount);
114 char *printmsg = g_variant_print(parameters, true);
115 LOGD("start parameter print : %s", printmsg);
117 LOGD("[event = %d] [gesture_type = %d] [ltype = %d] [levent = %d] [lcount = %d]", event, gesture_type, ltype, levent, lcount);
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);
124 else if (g_strcmp0(signal_name, GESTURE_CLIENT_SIGNAL_GET_ERROR) == 0) {
126 char* err_msg = NULL;
127 g_variant_get(parameters, "(is)", &error, err_msg);
128 LOGD("[CLIENT DBUS] error(%d)", error);
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);
134 LOGW("[CLIENT DBUS] There is no error callback");
137 else if (g_strcmp0(signal_name, GESTURE_CLIENT_SIGNAL_GET_MOTION_STATUS) == 0) {
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);
143 char *printmsg = g_variant_print(parameters, true);
144 LOGD("[engineInfo]start parameter print : %s", printmsg);
146 LOGE("[engineInfo][g_engine_app_id = %s] [g_engine_name = %s]", g_engine_app_id, g_engine_name);
152 static int _dbus_signal_init(GDBusConnection *gdbus_connection, int *monitor_id, CLIENT_LIB lib, void *data)
154 int ret = HAND_GESTURE_ERROR_NONE;
155 if (*monitor_id == 0) {
157 if (lib == GESTURE_CLIENT_LIB_GESTURE) {
158 id = g_dbus_connection_signal_subscribe(gdbus_connection,
160 GESTURE_CLIENT_INTERFACE_NAME,
164 G_DBUS_SIGNAL_FLAGS_NONE,
168 } else if (lib == GESTURE_CLIENT_LIB_ENGINE) {
169 LOGD("Fail to come for ENGINE lib");
171 LOGD("Fail to Not CLIENT lib");
176 ret = HAND_GESTURE_ERROR_OPERATION_FAILED;
177 LOGE("g_dbus_connection_signal_subscribe() failed");
186 static GDBusMessage *gdbus_make_message(GVariant *body, const char *cmd)
188 LOGD("gdbus_make_message : cmd = %s", cmd);
189 GDBusMessage *message = NULL;
190 message = g_dbus_message_new_method_call(
193 GESTURE_INTERFACE_NAME,
197 LOGE("Failed to create a new gdbus message");
199 g_variant_unref(body);
204 g_dbus_message_set_body(message, body);
209 static int _send_message_with_sync(GDBusConnection *gdbus_connection, GDBusMessage *msg, GDBusMessage **reply, const char *cmd)
211 LOGD("_send_message_with_sync : cmd = %s", cmd);
212 int ret = HAND_GESTURE_ERROR_NONE;
215 gchar *printmsg = g_dbus_message_print (msg, 1);
216 LOGD("[sync] before send to server, print : %s", printmsg);
219 *reply = g_dbus_connection_send_message_with_reply_sync(
222 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
229 ret = HAND_GESTURE_ERROR_OPERATION_FAILED;
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;
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;
249 printmsg = g_dbus_message_print (*reply, 1);
250 LOGD("[sync] reply from server, print : %s", printmsg);
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);
261 return HAND_GESTURE_ERROR_NONE;
264 static int gdbus_send_message_with_sync(GDBusConnection *gdbus_connection, GVariant *body, GDBusMessage **reply, char *cmd)
266 LOGD("gdbus_send_message_with_sync start : cmd = %s", cmd);
268 int ret = HAND_GESTURE_ERROR_NONE;
269 GDBusMessage *msg = NULL;
271 msg = gdbus_make_message(body, cmd);
273 return HAND_GESTURE_ERROR_OPERATION_FAILED;
275 ret = _send_message_with_sync(gdbus_connection, msg, reply, cmd);
283 static void _async_cb(GDBusConnection *connection, GAsyncResult *res, gpointer user_data)
285 LOGD("_async_cb start");
286 GDBusMessage *reply = NULL;
288 // hand_gesture_data_h gesturedata = (hand_gesture_data_h)user_data;
290 reply = g_dbus_connection_send_message_with_reply_finish(connection, res, &err);
292 if (g_dbus_message_to_gerror(reply, &err)) {
293 LOGE("error message = %s, code = %d", err->message, err->code);
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);
305 LOGE("There is no reply");
310 g_object_unref(reply);
315 static int gdbus_send_message_with_async(GDBusConnection *gdbus_connection, GVariant *body, char *cmd, hand_gesture_data_h gesture_data)
317 LOGD("gdbus_send_message_with_async start : cmd = %s", cmd);
318 int ret = HAND_GESTURE_ERROR_NONE;
319 GDBusMessage *msg = NULL;
321 msg = gdbus_make_message(body, cmd);
323 return HAND_GESTURE_ERROR_OPERATION_FAILED;
325 g_dbus_connection_send_message_with_reply(
328 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
332 (GAsyncReadyCallback)_async_cb,
341 static int _monitor_register(GDBusConnection *gdbus_connection)
344 GDBusMessage *reply = NULL;
345 GVariant *client_body = NULL;
347 char appid[1024] = {0, };
348 ret = aul_app_get_appid_bypid(getpid(), appid, sizeof(appid));
350 LOGE("aul_app_get_appid_bypid() failed : %d", ret);
353 client_body = g_variant_new("(iis)", 11, GESTURE_CLIENT_LIB_GESTURE, appid);
355 ret = gdbus_send_message_with_sync(gdbus_connection, client_body, &reply, GESTURE_MSG_SERVICE_REGISTER);
357 g_object_unref(reply);
360 g_variant_unref(client_body);
362 if (ret != HAND_GESTURE_ERROR_NONE) {
363 LOGE("gdbus_send_message_with_sync() failed : %d", ret);
367 is_server_started = 1;
371 static void _on_name_appeared(GDBusConnection *connection,
373 const gchar *name_owner,
376 if (is_server_started == 0)
377 _monitor_register(connection);
380 static void _on_name_vanished(GDBusConnection *connection,
384 is_server_started = 0;
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)
390 LOGD("gesture_client_dbus_init start");
394 ret = _dbus_init(gdbus_connection, server_watcher_id);
395 if (ret != HAND_GESTURE_ERROR_NONE) {
396 LOGE("_dbus_init() failed : %d", ret);
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);
406 ret = _monitor_register(*gdbus_connection);
407 if (ret != HAND_GESTURE_ERROR_NONE) {
408 LOGE("_monitor_register() failed : %d", ret);
412 if (*server_monitor_id == 0) {
413 *server_monitor_id = g_bus_watch_name_on_connection(
416 G_BUS_NAME_WATCHER_FLAGS_NONE,
421 if (*server_monitor_id == 0) {
422 g_dbus_connection_signal_unsubscribe(*gdbus_connection, *monitor_id);
424 LOGE("Failed to get identifier");
425 return HAND_GESTURE_ERROR_OPERATION_FAILED;
429 return HAND_GESTURE_ERROR_NONE;
432 int gesture_client_dbus_shutdown(GDBusConnection *gdbus_connection, int *server_monitor_id, int *monitor_id)
434 if (*server_monitor_id) {
435 g_bus_unwatch_name(*server_monitor_id);
436 *server_monitor_id = 0;
440 g_dbus_connection_signal_unsubscribe(gdbus_connection, *monitor_id);
444 return HAND_GESTURE_ERROR_NONE;
447 int gesture_client_dbus_initialize_engine(GDBusConnection *gdbus_connection)
450 GDBusMessage *reply = NULL;
451 GVariant *body = NULL;
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");
459 g_variant_unref(body);
462 g_object_unref(reply);
468 int gesture_client_dbus_deinitialize_engine(GDBusConnection *gdbus_connection)
471 GDBusMessage *reply = NULL;
472 GVariant *body = NULL;
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");
480 g_variant_unref(body);
483 g_object_unref(reply);
489 int gesture_client_dbus_set_handtype(GDBusConnection *gdbus_connection, hand_gesture_handtype_e hand_type)
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;
496 g_hand_type = hand_type;
501 int gesture_client_dbus_set_workmode(GDBusConnection *gdbus_connection, hand_gesture_workmode_e work_mode)
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;
508 g_work_mode = work_mode;
513 int gesture_client_dbus_set_option(GDBusConnection *gdbus_connection, hand_gesture_option_e option)
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;
525 int gesture_client_dbus_set_sensitivity(GDBusConnection *gdbus_connection, int sensitivity)
527 int ret = HAND_GESTURE_ERROR_NONE;
528 g_sensitivity = sensitivity;
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)
535 LOGD("gesture_client_dbus_start_recognition start");
536 LOGD("client busname: %s", g_dbus_connection_get_unique_name(gdbus_connection));
538 GVariant *body = NULL;
539 body = g_variant_new("(iiiii)", gesture_type, g_hand_type, g_work_mode, g_option, g_sensitivity);
541 gdbus_send_message_with_async(gdbus_connection, body, GESTURE_CLIENT_MSG_START_RECOGNITION, gesture_data);
544 g_variant_unref(body);
546 return HAND_GESTURE_ERROR_NONE;
549 int gesture_client_dbus_stop_recognition(GDBusConnection *gdbus_connection)
552 GDBusMessage *reply = NULL;
553 GVariant *body = NULL;
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");
561 g_variant_unref(body);
564 g_object_unref(reply);
570 int gesture_client_dbus_foreach_result_time(GDBusConnection *gdbus_connection)
573 GDBusMessage *reply = NULL;
574 GVariant *body = NULL;
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");
582 g_variant_unref(body);
585 g_object_unref(reply);
591 int gesture_client_dbus_foreach_supported_type(GDBusConnection *gdbus_connection)
594 GDBusMessage *reply = NULL;
595 GVariant *body = NULL;
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");
603 g_variant_unref(body);
606 g_object_unref(reply);
612 int gesture_client_dbus_is_support_gesture_type(GDBusConnection *gdbus_connection, hand_gesture_type_e gesture, bool* supported)
615 GDBusMessage *reply = NULL;
616 GVariant *body = NULL;
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");
624 g_variant_unref(body);
627 g_object_unref(reply);
633 int gesture_client_dbus_get_engine_info(GDBusConnection *gdbus_connection, char** engine_app_id, char** engine_name)
636 GDBusMessage *reply = NULL;
637 GVariant *body = NULL;
638 g_engine_app_id = *engine_app_id;
639 g_engine_name = *engine_name;
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");
647 g_variant_unref(body);
650 g_object_unref(reply);