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(handle);
205 ASSERT_NOT_NULL(supported);
208 ret = gesture_client_dbus_is_support_gesture_type(handle->gdbus_connection, gesture, supported);
209 if (ret != HAND_GESTURE_ERROR_NONE) {
210 LOGE("Failed to get is_supported_type dbus : %d", ret);
212 return HAND_GESTURE_ERROR_OPERATION_FAILED;
215 return HAND_GESTURE_ERROR_NONE;
218 EXPORT_API int hand_gesture_set_handtype(hand_gesture_h handle, hand_gesture_handtype_e hand_type)
220 LOGD("hand_gesture_set_handtype");
222 CHECK_GESTURE_FEATURE();
223 ASSERT_NOT_NULL(handle);
225 int ret = HAND_GESTURE_ERROR_NONE;
227 ret = gesture_client_dbus_set_handtype(handle->gdbus_connection, hand_type);
228 if (ret != HAND_GESTURE_ERROR_NONE) {
229 LOGE("Failed to set handtype : %d", ret);
230 return HAND_GESTURE_ERROR_OPERATION_FAILED;
236 EXPORT_API int hand_gesture_set_workmode(hand_gesture_h handle, hand_gesture_workmode_e work_mode)
238 LOGD("hand_gesture_set_workmode");
240 CHECK_GESTURE_FEATURE();
241 ASSERT_NOT_NULL(handle);
243 int ret = HAND_GESTURE_ERROR_NONE;
245 ret = gesture_client_dbus_set_workmode(handle->gdbus_connection, work_mode);
246 if (ret != HAND_GESTURE_ERROR_NONE) {
247 LOGE("Failed to set work_mode : %d", ret);
248 return HAND_GESTURE_ERROR_OPERATION_FAILED;
254 EXPORT_API int hand_gesture_set_option(hand_gesture_h handle, hand_gesture_option_e option)
256 LOGD("hand_gesture_set_option");
258 CHECK_GESTURE_FEATURE();
259 ASSERT_NOT_NULL(handle);
261 int ret = HAND_GESTURE_ERROR_NONE;
263 ret = gesture_client_dbus_set_option(handle->gdbus_connection, option);
264 if (ret != HAND_GESTURE_ERROR_NONE) {
265 LOGE("Failed to set option : %d", ret);
266 return HAND_GESTURE_ERROR_OPERATION_FAILED;
272 EXPORT_API int hand_gesture_set_sensitivity(hand_gesture_h handle, int sensitivity)
274 LOGD("hand_gesture_set_sensitivity");
276 CHECK_GESTURE_FEATURE();
277 ASSERT_NOT_NULL(handle);
279 int ret = HAND_GESTURE_ERROR_NONE;
281 ret = gesture_client_dbus_set_sensitivity(handle->gdbus_connection, sensitivity);
282 if (ret != HAND_GESTURE_ERROR_NONE) {
283 LOGE("Failed to set sensitivity : %d", ret);
284 return HAND_GESTURE_ERROR_OPERATION_FAILED;
290 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)
292 LOGD("hand_gesture_start_recognition");
294 CHECK_GESTURE_FEATURE();
295 ASSERT_NOT_NULL(handle);
297 if (_gesture_check_privilege() != HAND_GESTURE_ERROR_NONE)
298 return HAND_GESTURE_ERROR_PERMISSION_DENIED;
300 ASSERT_NOT_NULL(callback);
301 IF_FAIL_RETURN(IS_VALID_GESTURE(gesture_type), HAND_GESTURE_ERROR_INVALID_PARAMETER);
303 int ret = HAND_GESTURE_ERROR_NONE;
305 /* Set recognition callback and userdata in the handle */
306 handle->recog_cb = callback;
307 handle->recog_user_data = user_data;
309 hand_gesture_data_h gesture_data = (hand_gesture_data_h)calloc(1, sizeof(struct hand_gesture_data_s));
311 return HAND_GESTURE_ERROR_OUT_OF_MEMORY;
314 ret = gesture_client_dbus_start_recognition(handle->gdbus_connection, gesture_type, gesture_data, callback);
315 if (ret != HAND_GESTURE_ERROR_NONE) {
316 LOGE("Failed to start recoginition : %d", ret);
317 return HAND_GESTURE_ERROR_OPERATION_FAILED;
323 EXPORT_API int hand_gesture_stop_recognition(hand_gesture_h handle)
325 CHECK_GESTURE_FEATURE();
327 ASSERT_NOT_NULL(handle);
329 int ret = HAND_GESTURE_ERROR_NONE;
330 ret = gesture_client_dbus_stop_recognition(handle->gdbus_connection);
331 if (ret != HAND_GESTURE_ERROR_NONE) {
332 LOGE("Failed to stop recoginition : %d", ret);
333 return HAND_GESTURE_ERROR_OPERATION_FAILED;
336 /* Unset recognition callback and userdata in the handle */
337 handle->recog_cb = NULL;
338 handle->recog_user_data = NULL;
340 return HAND_GESTURE_ERROR_NONE;
343 EXPORT_API int hand_gesture_get_event(const hand_gesture_data_h data, hand_gesture_event_e *event)
345 CHECK_GESTURE_FEATURE();
346 ASSERT_NOT_NULL(data);
347 ASSERT_NOT_NULL(event);
349 //*event = static_cast<gesture_event_e>(data->event);
351 return HAND_GESTURE_ERROR_NONE;
354 EXPORT_API int hand_gesture_get_engine_info(hand_gesture_h handle, char** engine_app_id, char** engine_name)
356 LOGD("[engineInfo] hand_gesture_get_engine_info");
358 CHECK_GESTURE_FEATURE();
359 ASSERT_NOT_NULL(handle);
361 int ret = HAND_GESTURE_ERROR_NONE;
363 ret = gesture_client_dbus_get_engine_info(handle->gdbus_connection, engine_app_id, engine_name);
364 if (ret != HAND_GESTURE_ERROR_NONE) {
365 LOGE("Failed to get engine info : %d", ret);
366 return HAND_GESTURE_ERROR_OPERATION_FAILED;
368 LOGD("[engineInfo] hand_gesture_get_engine_info : engine_app_id = %s, engine_name = %s", *engine_app_id, *engine_name);
372 EXPORT_API int hand_gesture_set_error_cb(hand_gesture_h handle, hand_gesture_error_cb callback, void *user_data)
374 CHECK_GESTURE_FEATURE();
375 ASSERT_NOT_NULL(handle);
377 ASSERT_NOT_NULL(callback);
379 LOGD("[DEBUG] Set error_cb");
381 int ret = HAND_GESTURE_ERROR_NONE;
383 handle->error_cb = callback;
384 handle->error_user_data = user_data;
390 EXPORT_API int hand_gesture_unset_error_cb(hand_gesture_h handle)
392 CHECK_GESTURE_FEATURE();
393 ASSERT_NOT_NULL(handle);
395 LOGD("[DEBUG] Unset error_cb");
397 int ret = HAND_GESTURE_ERROR_NONE;
399 handle->error_cb = NULL;
400 handle->error_user_data = NULL;