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.
19 #include <dbus/dbus.h>
20 #include <cynara-client.h>
21 #include <cynara-error.h>
22 #include <cynara-session.h>
23 #include <system_info.h>
26 #include "gesture_internal.h"
27 #include "gesture_common_internal.h"
28 #include "gesture_main.h"
29 #include "gesture_client_dbus.h"
35 #define LOG_TAG "GESTURE_CLIENT"
37 #define GESTURE_FIRST HAND_GESTURE_WRIST_UP
38 #define GESTURE_LAST HAND_GESTURE_WRIST_UP
39 //#define GESTURE_LAST GESTURE_RIGHT_HAND_MOVE
40 #define IS_VALID_GESTURE(X) (GESTURE_FIRST <= (X) && (X) <= GESTURE_LAST)
43 static cynara *p_cynara = NULL;
45 static int _cynara_initialize()
47 int ret = cynara_initialize(&p_cynara, NULL);
48 if (ret != CYNARA_API_SUCCESS)
49 LOGE("Failed to cynara initialize");
54 static char * _get_smack_label()
57 char label_path[1024] = "/proc/self/attr/current";
58 static char smack_label[1024] = {'\0',};
60 fp = fopen(label_path, "r");
62 int ret = fread(smack_label, 1, sizeof(smack_label), fp);
64 LOGE("Failed to fread");
72 static int _check_privilege(const char *uid, const char *privilege)
75 char smack_label[1024] = {'\0',};
80 char * sl = _get_smack_label();
81 memcpy(smack_label, sl, strlen(sl) + 1);
84 char *session = cynara_session_from_pid(pid);
85 ret = cynara_check(p_cynara, smack_label, session, uid, privilege);
89 if (ret != CYNARA_API_ACCESS_ALLOWED) {
90 LOGE("Access denied. The result of cynara_check() : %d.", ret);
97 static void _cynara_deinitialize()
100 cynara_finish(p_cynara);
105 static int _gesture_check_privilege()
108 int ret = HAND_GESTURE_ERROR_NONE;
110 if (_cynara_initialize() != CYNARA_API_SUCCESS)
111 return HAND_GESTURE_ERROR_PERMISSION_DENIED;
113 snprintf(uid, 16, "%d", getuid());
114 if (_check_privilege(uid, GESTURE_PRIVILEGE_APPLAUNCH) < 0) {
115 LOGE("Permission is denied");
116 ret = HAND_GESTURE_ERROR_PERMISSION_DENIED;
119 _cynara_deinitialize();
124 EXPORT_API int hand_gesture_create(hand_gesture_h *handle)
126 LOGD("hand_gesture_create");
128 CHECK_GESTURE_FEATURE();
129 ASSERT_NOT_NULL(handle);
133 return HAND_GESTURE_ERROR_INVALID_PARAMETER;
135 if (_gesture_check_privilege() != HAND_GESTURE_ERROR_NONE)
136 return HAND_GESTURE_ERROR_PERMISSION_DENIED;
138 struct hand_gesture_s *_struct = (hand_gesture_h)calloc(1, sizeof(struct hand_gesture_s));
141 return HAND_GESTURE_ERROR_OUT_OF_MEMORY;
143 /* get uid and smack label */
144 snprintf(_struct->uid, 16, "%d", getuid());
146 char *sl = _get_smack_label();
147 memcpy(_struct->smack_label, sl, strlen(sl) + 1);
149 LOGI("uid(%s), smack(%s)", _struct->uid, _struct->smack_label);
151 ret = gesture_client_dbus_init(&_struct->gdbus_connection, &_struct->server_watcher_id,
152 &_struct->monitor_id, &_struct->server_monitor_id, GESTURE_CLIENT_LIB_GESTURE, _struct->uid, _struct->smack_label, (void *)_struct);
153 if (ret != HAND_GESTURE_ERROR_NONE) {
154 LOGE("Failed to initialize dbus : %d", ret);
156 return HAND_GESTURE_ERROR_OPERATION_FAILED;
159 ret = gesture_client_dbus_initialize_engine(_struct->gdbus_connection);
160 if (ret != HAND_GESTURE_ERROR_NONE) {
161 LOGE("Failed to initialize engine dbus : %d", ret);
163 return HAND_GESTURE_ERROR_OPERATION_FAILED;
169 return HAND_GESTURE_ERROR_NONE;
172 EXPORT_API int hand_gesture_destroy(hand_gesture_h handle)
174 LOGD("hand_gesture_destroy");
176 CHECK_GESTURE_FEATURE();
177 ASSERT_NOT_NULL(handle);
181 return HAND_GESTURE_ERROR_INVALID_PARAMETER;
183 LOGD("handle : %p", handle);
184 ret = gesture_client_dbus_shutdown(handle->gdbus_connection, &handle->server_monitor_id, &handle->monitor_id);
185 if (ret != HAND_GESTURE_ERROR_NONE) {
186 LOGE("Failed to finalize dbus : %d", ret);
188 return HAND_GESTURE_ERROR_OPERATION_FAILED;
191 if (handle->gdbus_connection)
192 g_object_unref(handle->gdbus_connection);
196 return HAND_GESTURE_ERROR_NONE;
199 EXPORT_API int hand_gesture_is_supported_type(hand_gesture_h handle, hand_gesture_type_e gesture, bool* supported)
201 LOGD("hand_gesture_is_supported_type");
203 CHECK_GESTURE_FEATURE();
204 ASSERT_NOT_NULL(supported);
207 ret = gesture_client_dbus_is_support_gesture_type(handle->gdbus_connection, gesture, supported);
208 if (ret != HAND_GESTURE_ERROR_NONE) {
209 LOGE("Failed to get is_supported_type dbus : %d", ret);
211 return HAND_GESTURE_ERROR_OPERATION_FAILED;
214 return HAND_GESTURE_ERROR_NONE;
217 EXPORT_API int hand_gesture_set_handtype(hand_gesture_h handle, hand_gesture_handtype_e hand_type)
219 LOGD("hand_gesture_set_handtype");
221 CHECK_GESTURE_FEATURE();
222 ASSERT_NOT_NULL(handle);
224 int ret = HAND_GESTURE_ERROR_NONE;
226 ret = gesture_client_dbus_set_handtype(handle->gdbus_connection, hand_type);
227 if (ret != HAND_GESTURE_ERROR_NONE) {
228 LOGE("Failed to set handtype : %d", ret);
229 return HAND_GESTURE_ERROR_OPERATION_FAILED;
235 EXPORT_API int hand_gesture_set_workmode(hand_gesture_h handle, hand_gesture_workmode_e work_mode)
237 LOGD("hand_gesture_set_workmode");
239 CHECK_GESTURE_FEATURE();
240 ASSERT_NOT_NULL(handle);
242 int ret = HAND_GESTURE_ERROR_NONE;
244 ret = gesture_client_dbus_set_workmode(handle->gdbus_connection, work_mode);
245 if (ret != HAND_GESTURE_ERROR_NONE) {
246 LOGE("Failed to set work_mode : %d", ret);
247 return HAND_GESTURE_ERROR_OPERATION_FAILED;
253 EXPORT_API int hand_gesture_set_option(hand_gesture_h handle, hand_gesture_option_e option)
255 LOGD("hand_gesture_set_option");
257 CHECK_GESTURE_FEATURE();
258 ASSERT_NOT_NULL(handle);
260 int ret = HAND_GESTURE_ERROR_NONE;
262 ret = gesture_client_dbus_set_option(handle->gdbus_connection, option);
263 if (ret != HAND_GESTURE_ERROR_NONE) {
264 LOGE("Failed to set option : %d", ret);
265 return HAND_GESTURE_ERROR_OPERATION_FAILED;
271 EXPORT_API int hand_gesture_set_sensitivity(hand_gesture_h handle, int sensitivity)
273 LOGD("hand_gesture_set_sensitivity");
275 CHECK_GESTURE_FEATURE();
276 ASSERT_NOT_NULL(handle);
278 int ret = HAND_GESTURE_ERROR_NONE;
280 ret = gesture_client_dbus_set_sensitivity(handle->gdbus_connection, sensitivity);
281 if (ret != HAND_GESTURE_ERROR_NONE) {
282 LOGE("Failed to set sensitivity : %d", ret);
283 return HAND_GESTURE_ERROR_OPERATION_FAILED;
289 EXPORT_API int hand_gesture_start_recognition(hand_gesture_h handle, hand_gesture_type_e gesture_type, hand_gesture_recognition_cb callback, void *user_data)
291 LOGD("hand_gesture_start_recognition");
293 CHECK_GESTURE_FEATURE();
294 ASSERT_NOT_NULL(handle);
296 if (_gesture_check_privilege() != HAND_GESTURE_ERROR_NONE)
297 return HAND_GESTURE_ERROR_PERMISSION_DENIED;
299 ASSERT_NOT_NULL(callback);
300 IF_FAIL_RETURN(IS_VALID_GESTURE(gesture_type), HAND_GESTURE_ERROR_INVALID_PARAMETER);
302 int ret = HAND_GESTURE_ERROR_NONE;
304 /* Set recognition callback and userdata in the handle */
305 handle->recog_cb = callback;
306 handle->recog_user_data = user_data;
308 hand_gesture_data_h gesture_data = (hand_gesture_data_h)calloc(1, sizeof(struct hand_gesture_data_s));
310 return HAND_GESTURE_ERROR_OUT_OF_MEMORY;
313 ret = gesture_client_dbus_start_recognition(handle->gdbus_connection, gesture_type, gesture_data, callback);
314 if (ret != HAND_GESTURE_ERROR_NONE) {
315 LOGE("Failed to start recoginition : %d", ret);
316 return HAND_GESTURE_ERROR_OPERATION_FAILED;
322 EXPORT_API int hand_gesture_stop_recognition(hand_gesture_h handle)
324 CHECK_GESTURE_FEATURE();
326 ASSERT_NOT_NULL(handle);
328 int ret = HAND_GESTURE_ERROR_NONE;
329 ret = gesture_client_dbus_stop_recognition(handle->gdbus_connection);
330 if (ret != HAND_GESTURE_ERROR_NONE) {
331 LOGE("Failed to stop recoginition : %d", ret);
332 return HAND_GESTURE_ERROR_OPERATION_FAILED;
335 /* Unset recognition callback and userdata in the handle */
336 handle->recog_cb = NULL;
337 handle->recog_user_data = NULL;
339 return HAND_GESTURE_ERROR_NONE;
342 EXPORT_API int hand_gesture_get_event(const hand_gesture_data_h data, hand_gesture_event_e *event)
344 CHECK_GESTURE_FEATURE();
345 ASSERT_NOT_NULL(data);
346 ASSERT_NOT_NULL(event);
348 //*event = static_cast<gesture_event_e>(data->event);
350 return HAND_GESTURE_ERROR_NONE;
353 EXPORT_API int hand_gesture_get_engine_info(hand_gesture_h handle, char** engine_app_id, char** engine_name)
355 LOGD("[engineInfo] hand_gesture_get_engine_info");
357 CHECK_GESTURE_FEATURE();
358 ASSERT_NOT_NULL(handle);
360 int ret = HAND_GESTURE_ERROR_NONE;
362 ret = gesture_client_dbus_get_engine_info(handle->gdbus_connection, engine_app_id, engine_name);
363 if (ret != HAND_GESTURE_ERROR_NONE) {
364 LOGE("Failed to get engine info : %d", ret);
365 return HAND_GESTURE_ERROR_OPERATION_FAILED;
367 LOGD("[engineInfo] hand_gesture_get_engine_info : engine_app_id = %s, engine_name = %s", *engine_app_id, *engine_name);
371 EXPORT_API int hand_gesture_set_error_cb(hand_gesture_h handle, hand_gesture_error_cb callback, void *user_data)
373 CHECK_GESTURE_FEATURE();
374 ASSERT_NOT_NULL(handle);
376 ASSERT_NOT_NULL(callback);
378 LOGD("[DEBUG] Set error_cb");
380 int ret = HAND_GESTURE_ERROR_NONE;
382 handle->error_cb = callback;
383 handle->error_user_data = user_data;
389 EXPORT_API int hand_gesture_unset_error_cb(hand_gesture_h handle)
391 CHECK_GESTURE_FEATURE();
392 ASSERT_NOT_NULL(handle);
394 LOGD("[DEBUG] Unset error_cb");
396 int ret = HAND_GESTURE_ERROR_NONE;
398 handle->error_cb = NULL;
399 handle->error_user_data = NULL;