3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * Licensed under the Apache License, Version 2.0 (the License);
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
23 #include <sys/types.h>
28 #include <haptic_plugin_intf.h>
29 #include "haptic_module_log.h"
30 #include "haptic_file.h"
33 #define EXTAPI __attribute__ ((visibility("default")))
36 #define DEFAULT_MOTOR_COUNT 1
37 #define DEFAULT_DEVICE_HANDLE 0x01
38 #define DEFAULT_EFFECT_HANDLE 0x02
40 #define HAPTIC_PLAY_FILE_EXT ".tht"
42 /* START of Static Function Section */
43 static int __feedback_to_tizen_type(int feedback)
49 status = GetHapticLevelMax(&max);
51 MODULE_ERROR("GetHapticLevelMax fail : %d", status);
56 MODULE_LOG("feedback value is changed : %d -> %d", feedback, (int)(feedback*max/HAPTIC_MODULE_FEEDBACK_MAX));
57 return (int)(feedback*max/HAPTIC_MODULE_FEEDBACK_MAX);
60 static char* __check_file (const char *name)
62 char *file_full_name = NULL;
65 char *file_ext = NULL;
70 file_full_name = strdup(name);
71 if (!file_full_name) {
72 MODULE_ERROR("strdup failed");
76 name_length = strlen(file_full_name) - 1;
77 while (file_full_name[name_length] == ' ')
79 file_full_name[name_length + 1] = '\0';
81 file_ext = strrchr(file_full_name, '.');
82 if (!(file_ext && !strcmp(file_ext, HAPTIC_PLAY_FILE_EXT))) {
84 MODULE_ERROR("Not supported file");
88 return file_full_name;
91 static unsigned char* __convert_file_to_vibe_buffer(const char *file_name)
96 unsigned char* pIVTData;
99 MODULE_ERROR("Wrowng file name");
103 file_full_name = __check_file(file_name);
104 if(!file_full_name) {
105 MODULE_ERROR("__check_file_faild");
109 /* Get File Stream Pointer */
110 pf = fopen(file_full_name, "rb");
111 free(file_full_name);
113 MODULE_ERROR("file open failed");
117 if (fseek(pf, 0, SEEK_END)) {
118 MODULE_ERROR("fseek failed");
123 file_size = ftell(pf);
125 MODULE_ERROR("ftell failed");
130 if (fseek(pf, 0, SEEK_SET)) {
131 MODULE_ERROR("fseek failed");
136 pIVTData = (unsigned char*)malloc(file_size);
142 if (fread(pIVTData, 1, file_size, pf) != file_size) {
145 MODULE_ERROR("fread failed");
152 /* END of Static Function Section */
154 int haptic_internal_get_device_count(int *count)
157 return HAPTIC_MODULE_INVALID_ARGUMENT;
159 *count = DEFAULT_MOTOR_COUNT;
160 return HAPTIC_MODULE_ERROR_NONE;
163 int haptic_internal_open_device(int device_index, int *device_handle)
165 if (device_index < HAPTIC_MODULE_DEVICE_0 || device_index > HAPTIC_MODULE_DEVICE_ALL)
166 return HAPTIC_MODULE_INVALID_ARGUMENT;
168 if (device_handle == NULL)
169 return HAPTIC_MODULE_INVALID_ARGUMENT;
171 *device_handle = DEFAULT_DEVICE_HANDLE;
172 return HAPTIC_MODULE_ERROR_NONE;
175 int haptic_internal_close_device(int device_handle)
179 if (device_handle < 0)
180 return HAPTIC_MODULE_INVALID_ARGUMENT;
182 status = CloseHapticDevice();
184 MODULE_ERROR("CloseHapticDevice fail : %d", status);
185 return HAPTIC_MODULE_OPERATION_FAILED;
188 status = SetHapticEnable(0);
190 MODULE_ERROR("SetHapticEnable fail : %d", status);
191 return HAPTIC_MODULE_OPERATION_FAILED;
194 return HAPTIC_MODULE_ERROR_NONE;
197 int haptic_internal_vibrate_monotone(int device_handle, int duration, int feedback, int priority, int *effect_handle)
200 int input_feedback = -1;
202 if (device_handle < 0)
203 return HAPTIC_MODULE_INVALID_ARGUMENT;
206 return HAPTIC_MODULE_INVALID_ARGUMENT;
208 if (feedback < HAPTIC_MODULE_FEEDBACK_MIN || feedback > HAPTIC_MODULE_FEEDBACK_MAX)
209 return HAPTIC_MODULE_INVALID_ARGUMENT;
211 if (priority < HAPTIC_MODULE_PRIORITY_MIN || priority > HAPTIC_MODULE_PRIORITY_HIGH)
212 return HAPTIC_MODULE_INVALID_ARGUMENT;
214 if (effect_handle == NULL)
215 return HAPTIC_MODULE_INVALID_ARGUMENT;
217 if (feedback == HAPTIC_MODULE_FEEDBACK_MIN)
218 return HAPTIC_MODULE_ERROR_NONE;
220 input_feedback = __feedback_to_tizen_type(feedback);
221 if (input_feedback < 0)
222 return HAPTIC_MODULE_OPERATION_FAILED;
224 status = SetHapticLevel(input_feedback);
226 MODULE_ERROR("SetHapticLevel fail : %d", status);
227 return HAPTIC_MODULE_OPERATION_FAILED;
230 status = SetHapticOneshot(duration);
232 MODULE_ERROR("SetHapticOneshot fail : %d", status);
233 return HAPTIC_MODULE_OPERATION_FAILED;
236 *effect_handle = DEFAULT_EFFECT_HANDLE;
237 return HAPTIC_MODULE_ERROR_NONE;
240 int haptic_internal_vibrate_file(int device_handle, const char *file_path, int iteration, int feedback, int priority, int *effect_handle)
243 unsigned char *vibe_buffer = NULL;
244 int input_feedback = -1;
248 if (device_handle < 0)
249 return HAPTIC_MODULE_INVALID_ARGUMENT;
251 if (file_path == NULL)
252 return HAPTIC_MODULE_INVALID_ARGUMENT;
254 if (iteration < HAPTIC_MODULE_ITERATION_ONCE || iteration > HAPTIC_MODULE_ITERATION_INFINITE)
255 return HAPTIC_MODULE_INVALID_ARGUMENT;
257 if (feedback < HAPTIC_MODULE_FEEDBACK_MIN || feedback > HAPTIC_MODULE_FEEDBACK_MAX)
258 return HAPTIC_MODULE_INVALID_ARGUMENT;
260 if (priority < HAPTIC_MODULE_PRIORITY_MIN || priority > HAPTIC_MODULE_PRIORITY_HIGH)
261 return HAPTIC_MODULE_INVALID_ARGUMENT;
263 if (effect_handle == NULL)
264 return HAPTIC_MODULE_INVALID_ARGUMENT;
266 if (feedback == HAPTIC_MODULE_FEEDBACK_MIN)
267 return HAPTIC_MODULE_ERROR_NONE;
269 input_feedback = __feedback_to_tizen_type(feedback);
270 if (input_feedback < 0)
271 return HAPTIC_MODULE_OPERATION_FAILED;
273 status = SetHapticLevel(input_feedback);
275 MODULE_ERROR("SetHapticLevel fail : %d", status);
276 return HAPTIC_MODULE_OPERATION_FAILED;
279 vibe_buffer = __convert_file_to_vibe_buffer(file_path);
281 MODULE_ERROR("File load filed");
282 return HAPTIC_MODULE_OPERATION_FAILED;
285 status = PlayHapticBuffer(vibe_buffer, iteration, &handle);
287 MODULE_ERROR("PlayHapticBuffer fail: %d", status);
289 return HAPTIC_MODULE_OPERATION_FAILED;
292 *effect_handle = handle;
294 return HAPTIC_MODULE_ERROR_NONE;
297 int haptic_internal_vibrate_buffer(int device_handle, const unsigned char *vibe_buffer, int iteration, int feedback, int priority, int *effect_handle)
300 int input_feedback = -1;
304 if (device_handle < 0)
305 return HAPTIC_MODULE_INVALID_ARGUMENT;
307 if (vibe_buffer == NULL)
308 return HAPTIC_MODULE_INVALID_ARGUMENT;
310 if (iteration < HAPTIC_MODULE_ITERATION_ONCE || iteration > HAPTIC_MODULE_ITERATION_INFINITE)
311 return HAPTIC_MODULE_INVALID_ARGUMENT;
313 if (feedback < HAPTIC_MODULE_FEEDBACK_MIN || feedback > HAPTIC_MODULE_FEEDBACK_MAX)
314 return HAPTIC_MODULE_INVALID_ARGUMENT;
316 if (priority < HAPTIC_MODULE_PRIORITY_MIN || priority > HAPTIC_MODULE_PRIORITY_HIGH)
317 return HAPTIC_MODULE_INVALID_ARGUMENT;
319 if (effect_handle == NULL)
320 return HAPTIC_MODULE_INVALID_ARGUMENT;
322 if (feedback == HAPTIC_MODULE_FEEDBACK_MIN)
323 return HAPTIC_MODULE_ERROR_NONE;
325 input_feedback = __feedback_to_tizen_type(feedback);
326 if (input_feedback < 0)
327 return HAPTIC_MODULE_OPERATION_FAILED;
329 status = SetHapticLevel(input_feedback);
331 MODULE_ERROR("SetHapticLevel fail : %d", status);
332 return HAPTIC_MODULE_OPERATION_FAILED;
335 status = PlayHapticBuffer(vibe_buffer, iteration, &handle);
337 MODULE_ERROR("PlayHapticBuffer fail: %d", status);
338 return HAPTIC_MODULE_OPERATION_FAILED;
341 *effect_handle = handle;
342 return HAPTIC_MODULE_ERROR_NONE;
345 int haptic_internal_stop_effect(int device_handle, int effect_handle)
349 if (device_handle < 0)
350 return HAPTIC_MODULE_INVALID_ARGUMENT;
352 if (effect_handle < 0)
353 return HAPTIC_MODULE_INVALID_ARGUMENT;
355 status = SetHapticEnable(0);
357 MODULE_ERROR("SetHapticEnable fail : %d", status);
358 return HAPTIC_MODULE_OPERATION_FAILED;
361 return HAPTIC_MODULE_ERROR_NONE;
364 int haptic_internal_stop_all_effects(int device_handle)
368 if (device_handle < 0)
369 return HAPTIC_MODULE_INVALID_ARGUMENT;
371 status = SetHapticEnable(0);
373 MODULE_ERROR("SetHapticEnable fail : %d", status);
374 return HAPTIC_MODULE_OPERATION_FAILED;
377 return HAPTIC_MODULE_ERROR_NONE;
380 int haptic_internal_pause_effect(int device_handle, int effect_handle)
382 MODULE_ERROR("This device is not supported this function(%s)", __func__);
383 return HAPTIC_MODULE_NOT_SUPPORTED;
386 int haptic_internal_resume_effect(int device_handle, int effect_handle)
388 MODULE_ERROR("This device is not supported this function(%s)", __func__);
389 return HAPTIC_MODULE_NOT_SUPPORTED;
392 int haptic_internal_get_effect_state(int device_handle, int effect_handle, int *state)
394 MODULE_ERROR("This device is not supported this function(%s)", __func__);
395 return HAPTIC_MODULE_NOT_SUPPORTED;
398 int haptic_internal_create_effect(unsigned char *vibe_buffer, int max_bufsize, haptic_module_effect_element *elem_arr, int max_element)
404 if (vibe_buffer == NULL)
405 return HAPTIC_MODULE_INVALID_ARGUMENT;
408 return HAPTIC_MODULE_INVALID_ARGUMENT;
410 if (elem_arr == NULL)
411 return HAPTIC_MODULE_INVALID_ARGUMENT;
414 return HAPTIC_MODULE_INVALID_ARGUMENT;
416 status = InitializeHapticBuffer(vibe_buffer, max_bufsize);
418 MODULE_ERROR("InitializeHapticBuffer fail: %d", status);
419 return HAPTIC_MODULE_OPERATION_FAILED;
422 MODULE_LOG("effect count : %d", max_element);
423 for (i = 0; i < max_element; ++i) {
424 elem.stime = elem_arr[i].haptic_stime;
425 elem.duration = elem_arr[i].haptic_duration;
426 MODULE_LOG("%d) time : %d, duration : %d", i, elem_arr[i].haptic_stime, elem_arr[i].haptic_duration);
428 status = InsertHapticElement(vibe_buffer, max_bufsize, &elem);
430 MODULE_ERROR("InsertHapticElement fail: %d", status);
431 return HAPTIC_MODULE_OPERATION_FAILED;
435 return HAPTIC_MODULE_ERROR_NONE;
438 int haptic_internal_save_effect(const unsigned char *vibe_buffer, int max_bufsize, const char *file_path)
446 if (vibe_buffer == NULL)
447 return HAPTIC_MODULE_INVALID_ARGUMENT;
450 return HAPTIC_MODULE_INVALID_ARGUMENT;
452 if (file_path == NULL)
453 return HAPTIC_MODULE_INVALID_ARGUMENT;
455 status = GetHapticBufferSize(vibe_buffer, &size);
457 MODULE_ERROR("GetHapticBufferSize fail: %d", status);
458 return HAPTIC_MODULE_OPERATION_FAILED;
461 file = fopen(file_path, "wb");
463 MODULE_ERROR("To open file is failed");
464 return HAPTIC_MODULE_OPERATION_FAILED;
467 ret = fwrite(vibe_buffer, 1, size, file);
469 MODULE_ERROR("To write file is failed");
471 return HAPTIC_MODULE_OPERATION_FAILED;
476 MODULE_ERROR("To get file descriptor is failed");
478 return HAPTIC_MODULE_OPERATION_FAILED;
482 MODULE_ERROR("To be synchronized with the disk is failed");
484 return HAPTIC_MODULE_OPERATION_FAILED;
488 return HAPTIC_MODULE_ERROR_NONE;
491 int haptic_internal_get_file_duration(int device_handle, const char *file_path, int *file_duration)
494 unsigned char *vibe_buffer = NULL;
497 if (device_handle < 0)
498 return HAPTIC_MODULE_INVALID_ARGUMENT;
500 if (file_path == NULL)
501 return HAPTIC_MODULE_INVALID_ARGUMENT;
503 if (file_duration == NULL)
504 return HAPTIC_MODULE_INVALID_ARGUMENT;
506 vibe_buffer = __convert_file_to_vibe_buffer(file_path);
508 MODULE_ERROR("File load filed");
509 return HAPTIC_MODULE_OPERATION_FAILED;
512 status = GetHapticBufferDuration(vibe_buffer, &duration);
514 MODULE_ERROR("GetHapticBufferDuration fail: %d", status);
516 return HAPTIC_MODULE_OPERATION_FAILED;
519 *file_duration = duration;
521 return HAPTIC_MODULE_ERROR_NONE;
524 int haptic_internal_get_buffer_duration(int device_handle, const unsigned char *vibe_buffer, int *buffer_duration)
529 if (device_handle < 0)
530 return HAPTIC_MODULE_INVALID_ARGUMENT;
532 if (vibe_buffer == NULL)
533 return HAPTIC_MODULE_INVALID_ARGUMENT;
535 if (buffer_duration == NULL)
536 return HAPTIC_MODULE_INVALID_ARGUMENT;
538 status = GetHapticBufferDuration(vibe_buffer, &duration);
540 MODULE_ERROR("GetHapticBufferDuration fail: %d", status);
541 return HAPTIC_MODULE_OPERATION_FAILED;
544 *buffer_duration = duration;
545 return HAPTIC_MODULE_ERROR_NONE;
548 int haptic_internal_convert_binary (void)
550 MODULE_ERROR("This device is not supported this function(%s)", __func__);
551 return HAPTIC_MODULE_NOT_SUPPORTED;
554 static haptic_plugin_interface haptic_plugin_tizen;
557 const haptic_plugin_interface *get_haptic_plugin_interface()
559 haptic_plugin_tizen.haptic_internal_get_device_count= &haptic_internal_get_device_count;
560 haptic_plugin_tizen.haptic_internal_open_device = &haptic_internal_open_device;
561 haptic_plugin_tizen.haptic_internal_close_device = &haptic_internal_close_device;
562 haptic_plugin_tizen.haptic_internal_vibrate_monotone= &haptic_internal_vibrate_monotone;
563 haptic_plugin_tizen.haptic_internal_vibrate_file = &haptic_internal_vibrate_file;
564 haptic_plugin_tizen.haptic_internal_vibrate_buffer = &haptic_internal_vibrate_buffer;
565 haptic_plugin_tizen.haptic_internal_stop_effect = &haptic_internal_stop_effect;
566 haptic_plugin_tizen.haptic_internal_stop_all_effects= &haptic_internal_stop_all_effects;
567 haptic_plugin_tizen.haptic_internal_pause_effect = &haptic_internal_pause_effect;
568 haptic_plugin_tizen.haptic_internal_resume_effect = &haptic_internal_resume_effect;
569 haptic_plugin_tizen.haptic_internal_get_effect_state= &haptic_internal_get_effect_state;
570 haptic_plugin_tizen.haptic_internal_create_effect = &haptic_internal_create_effect;
571 haptic_plugin_tizen.haptic_internal_save_effect = &haptic_internal_save_effect;
572 haptic_plugin_tizen.haptic_internal_get_file_duration = &haptic_internal_get_file_duration;
573 haptic_plugin_tizen.haptic_internal_get_buffer_duration = &haptic_internal_get_buffer_duration;
574 haptic_plugin_tizen.haptic_internal_convert_binary = &haptic_internal_convert_binary;
576 return &haptic_plugin_tizen;