Fix coverity issue
[platform/core/api/gesture.git] / client / gesture_client_dbus.c
index 0683bf0..0c58dad 100644 (file)
@@ -25,8 +25,6 @@
 #define LOG_TAG "GESTURE_CLIENT_DBUS"
 
 static int is_server_started = 0;
-static hand_gesture_recognition_cb g_callback;
-static hand_gesture_data_h g_gesture_data;
 
 static hand_gesture_handtype_e g_hand_type = HAND_GESTURE_LEFT_HAND;
 static hand_gesture_workmode_e g_work_mode = HAND_GESTURE_WORK_MODE_ONE_WAY;
@@ -36,11 +34,6 @@ static int g_sensitivity = 1;
 static char *g_engine_app_id;
 static char *g_engine_name;
 
-static void _free_gesture_data(hand_gesture_data_h gesture_data)
-{
-       free(gesture_data);
-       gesture_data = NULL;
-}
 
 static void _server_appeared_cb(GDBusConnection *connection, const gchar *name, const gchar *name_owner, gpointer user_data)
 {
@@ -124,19 +117,23 @@ static void _handle_gesture_cb(GDBusConnection *connection,
                g_free(printmsg);
                LOGD("[event = %d] [gesture_type = %d] [ltype = %d] [levent = %d] [lcount = %d]", event, gesture_type, ltype, levent, lcount);
 #endif
-               if (g_gesture_data == NULL){
-                       LOGD("Can't send the result to Client because g_gesture_data is NULL");
-                       return;
+               if (_handle->recog_cb) {
+                       LOGD("[CLIENT DBUS] Send recognition result. gesture_type(%d)", gesture_type);
+                       _handle->recog_cb(_handle, gesture_type, 0, HAND_GESTURE_ERROR_NONE, _handle->recog_user_data);
                }
-               LOGD("address = %p", g_gesture_data);
-               g_gesture_data->gesture_type = ltype;
-               g_gesture_data->event = levent;
-               g_gesture_data->detected_Count = lcount;
-
-               g_callback(gesture_type, g_gesture_data, 0, HAND_GESTURE_ERROR_NONE, user_data);
        }
        else if (g_strcmp0(signal_name, GESTURE_CLIENT_SIGNAL_GET_ERROR) == 0) {
-
+               int error;
+               char* err_msg = NULL;
+               g_variant_get(parameters, "(is)", &error, err_msg);
+               LOGD("[CLIENT DBUS] error(%d)", error);
+
+               if (_handle->error_cb) {
+                       LOGD("[CLIENT DBUS] Send error");
+                       _handle->error_cb(_handle, (hand_gesture_error_e)error, err_msg, _handle->error_user_data);
+               } else {
+                       LOGW("[CLIENT DBUS] There is no error callback");
+               }
        }
        else if (g_strcmp0(signal_name, GESTURE_CLIENT_SIGNAL_GET_MOTION_STATUS) == 0) {
 
@@ -342,19 +339,22 @@ static int gdbus_send_message_with_async(GDBusConnection *gdbus_connection, GVar
        return ret;
 }
 
-static int _monitor_register(GDBusConnection *gdbus_connection)
+static int _monitor_register(GDBusConnection *gdbus_connection, const char *uid, const char *smack_label)
 {
        int ret;
        GDBusMessage *reply = NULL;
        GVariant *client_body = NULL;
 
        char appid[1024] = {0, };
-       ret = aul_app_get_appid_bypid(getpid(), appid, sizeof(appid));
+       int pid = getpid();
+       ret = aul_app_get_appid_bypid(pid, appid, sizeof(appid));
        if (ret != 0) {
                LOGE("aul_app_get_appid_bypid() failed : %d", ret);
        }
 
-       client_body = g_variant_new("(iis)", 11, GESTURE_CLIENT_LIB_GESTURE, appid);
+       LOGI("[INFO] appid(%s), pid(%d), uid(%s), smack_label(%s)", appid, pid, uid, smack_label);
+
+       client_body = g_variant_new("(iisiss)", 11, GESTURE_CLIENT_LIB_GESTURE, appid, pid, uid, smack_label);
 
        ret = gdbus_send_message_with_sync(gdbus_connection, client_body, &reply, GESTURE_MSG_SERVICE_REGISTER);
        if (reply)
@@ -377,8 +377,12 @@ static void _on_name_appeared(GDBusConnection *connection,
                const gchar     *name_owner,
                gpointer         user_data)
 {
-       if (is_server_started == 0)
-       _monitor_register(connection);
+       struct hand_gesture_s *_struct = user_data;
+
+       if (is_server_started == 0) {
+               LOGI("uid(%s), smack_label(%s)", _struct->uid, _struct->smack_label);
+               _monitor_register(connection, _struct->uid, _struct->smack_label);
+       }
 }
 
 static void _on_name_vanished(GDBusConnection *connection,
@@ -389,7 +393,7 @@ static void _on_name_vanished(GDBusConnection *connection,
 }
 
 int gesture_client_dbus_init(GDBusConnection **gdbus_connection, guint *server_watcher_id,
-                               int *monitor_id, int *server_monitor_id, CLIENT_LIB lib, void *data)
+                               int *monitor_id, int *server_monitor_id, CLIENT_LIB lib, const char *uid, const char *smack_label, void *data)
 {
        LOGD("gesture_client_dbus_init start");
 
@@ -407,7 +411,7 @@ int gesture_client_dbus_init(GDBusConnection **gdbus_connection, guint *server_w
                return ret;
        }
 
-       ret = _monitor_register(*gdbus_connection);
+       ret = _monitor_register(*gdbus_connection, uid, smack_label);
        if (ret != HAND_GESTURE_ERROR_NONE) {
                LOGE("_monitor_register() failed : %d", ret);
                return ret;
@@ -420,7 +424,7 @@ int gesture_client_dbus_init(GDBusConnection **gdbus_connection, guint *server_w
                                G_BUS_NAME_WATCHER_FLAGS_NONE,
                                _on_name_appeared,
                                _on_name_vanished,
-                               NULL,
+                               data,
                                NULL);
                if (*server_monitor_id == 0) {
                        g_dbus_connection_signal_unsubscribe(*gdbus_connection, *monitor_id);
@@ -538,8 +542,6 @@ int gesture_client_dbus_start_recognition(GDBusConnection *gdbus_connection, han
 {
        LOGD("gesture_client_dbus_start_recognition start");
        LOGD("client busname: %s", g_dbus_connection_get_unique_name(gdbus_connection));
-       g_callback = callback;
-       g_gesture_data = gesture_data;
 
        GVariant *body = NULL;
        body = g_variant_new("(iiiii)", gesture_type, g_hand_type, g_work_mode, g_option, g_sensitivity);
@@ -558,9 +560,6 @@ int gesture_client_dbus_stop_recognition(GDBusConnection *gdbus_connection)
        GDBusMessage *reply = NULL;
        GVariant *body = NULL;
 
-       /* free for gesture data struct */
-       _free_gesture_data(g_gesture_data);
-
        body = g_variant_new("()");
        ret = gdbus_send_message_with_sync(gdbus_connection, body, &reply, GESTURE_CLIENT_MSG_STOP_RECOGNITION);
        if (ret != HAND_GESTURE_ERROR_NONE)
@@ -624,10 +623,21 @@ int gesture_client_dbus_is_support_gesture_type(GDBusConnection *gdbus_connectio
        GDBusMessage *reply = NULL;
        GVariant *body = NULL;
 
-       body = g_variant_new("()");
+       body = g_variant_new("(i)", gesture);
        ret = gdbus_send_message_with_sync(gdbus_connection, body, &reply, GESTURE_CLIENT_MSG_IS_SUPPORT_GESTURE_TYPE);
-       if (ret != HAND_GESTURE_ERROR_NONE)
+       if (ret != HAND_GESTURE_ERROR_NONE || NULL == reply) {
                LOGE("failed to is_supported_gesture_type");
+       } else {
+               LOGD("Check reply from server");
+               GVariant *reply_body = g_dbus_message_get_body(reply);
+               if (NULL == reply_body) {
+                       LOGE("There is no return value");
+                       ret = HAND_GESTURE_ERROR_OPERATION_FAILED;
+               } else {
+                       g_variant_get(reply_body, "(b)", supported);
+                       LOGD("supported(%d)", *supported);
+               }
+       }
 
        if (body)
                g_variant_unref(body);
@@ -639,18 +649,40 @@ int gesture_client_dbus_is_support_gesture_type(GDBusConnection *gdbus_connectio
 
 }
 
-int gesture_client_dbus_engine_get_info(GDBusConnection *gdbus_connection, char** engine_app_id, char** engine_name)
+int gesture_client_dbus_get_engine_info(GDBusConnection *gdbus_connection, char** engine_app_id, char** engine_name)
 {
        int ret;
        GDBusMessage *reply = NULL;
        GVariant *body = NULL;
-       g_engine_app_id = *engine_app_id;
-       g_engine_name = *engine_name;
 
        body = g_variant_new("()");
        ret = gdbus_send_message_with_sync(gdbus_connection, body, &reply, GESTURE_CLIENT_MSG_ENGINE_GET_INFO);
        if (ret != HAND_GESTURE_ERROR_NONE)
-               LOGE("failed to engine_get_info");
+               LOGE("Fail to send message");
+
+       GVariant *reply_body = g_dbus_message_get_body(reply);
+       if (NULL == reply_body) {
+               LOGE("There is no return value");
+               ret = HAND_GESTURE_ERROR_OPERATION_FAILED;
+       } else {
+               g_variant_get(reply_body, "(ss)", engine_app_id, engine_name);
+               if (NULL == engine_app_id || NULL == engine_name) {
+                       LOGE("Fail to get engine info");
+                       ret = HAND_GESTURE_ERROR_OPERATION_FAILED;
+               } else {
+                       if (NULL != g_engine_app_id) {
+                               free(g_engine_app_id);
+                       }
+
+                       if (NULL != g_engine_name) {
+                               free(g_engine_name);
+                       }
+
+                       g_engine_app_id = *engine_app_id;
+                       g_engine_name = *engine_name;
+                       LOGD("APP ID(%s), Name(%s)", *engine_app_id, *engine_name);
+               }
+       }
 
        if (body)
                g_variant_unref(body);