2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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.
18 * @file FSys_VibratorImpl.cpp
19 * @brief This is the implementation file for _VibratorImpl class.
23 #include <unique_ptr.h>
25 #include <FBaseSysLog.h>
26 #include <FBaseResult.h>
27 #include <FSysVibrator.h>
29 #include <FSys_VibratorImpl.h>
31 namespace Tizen { namespace System
33 static const int MAX_SIZE_OF_VIBRATION_BUFFER = 64 * 1024; // Immersion lib guide value. 64kbytes
35 _VibratorImpl::_VibratorImpl(void)
37 , __effectHandle(null)
42 _VibratorImpl::~_VibratorImpl(void)
47 int deviceResult = haptic_close(__handle);
49 if (deviceResult != HAPTIC_ERROR_NONE)
51 SysLogException(NID_SYS, E_SYSTEM,
52 "[E_SYSTEM] Vibrator finalize operation has failed. [handle:%d, result:%d]", __handle, deviceResult);
58 _VibratorImpl::Construct(void)
60 SysTryReturnResult(NID_SYS, __handle == null, E_DEVICE_FAILED, "_VibratorImpl was already constructed.");
62 int ret = haptic_open(HAPTIC_DEVICE_ALL, &__handle);
64 SysTryReturnResult(NID_SYS, ret == HAPTIC_ERROR_NONE,E_SYSTEM,
65 "[E_SYSTEM] Vibrator initialize operation has failed. [result:%d]", ret);
66 SysLog(NID_SYS, "vibrator created handle = %d", __handle);
72 _VibratorImpl::Start(IntensityDurationVibrationPattern* patterns, int length, int repeatCount)
74 SysTryReturnResult(NID_SYS, __handle != null, E_DEVICE_FAILED, "_VibratorImpl was not constructed.");
79 errorCode = haptic_get_effect_state(__handle, __effectHandle, &state);
81 if ((haptic_error_e)errorCode == HAPTIC_ERROR_NONE)
83 if (state == HAPTIC_STATE_PLAYING)
86 SysTryReturnResult(NID_SYS, r == E_SUCCESS, r, "It failed to stop previous vibration.");
90 int numberOfElement = length / sizeof(IntensityDurationVibrationPattern);
91 std::unique_ptr<haptic_effect_element_s[]> pPatterns(new (std::nothrow) haptic_effect_element_s[numberOfElement * sizeof(haptic_effect_element_s)]);
92 SysTryReturnResult(NID_SYS, pPatterns != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] It failed to allocate memory");
94 for (int i = 0; i < numberOfElement; i++)
96 SysTryReturnResult(NID_SYS, patterns[i].duration >= 0, E_INVALID_ARG,
97 "[E_INVALID_ARG] The %dth pattern is the invalid duration[%d].", i, patterns[i].duration);
98 pPatterns[i].haptic_duration = patterns[i].duration;
100 SysTryReturnResult(NID_SYS, patterns[i].intensity >= -1 && patterns[i].intensity <= 100, E_INVALID_ARG,
101 "[E_INVALID_ARG] The %dth pattern is the invalid intensity[%d].", i, patterns[i].intensity);
102 pPatterns[i].haptic_level = (patterns[i].intensity == -1) ? HAPTIC_FEEDBACK_AUTO : (haptic_feedback_e)patterns[i].intensity;
105 int sizeOfBuf = MAX_SIZE_OF_VIBRATION_BUFFER;
106 std::unique_ptr<unsigned char[]> pEffectBuffer(new (std::nothrow) unsigned char[sizeOfBuf]);
107 SysTryReturnResult(NID_SYS, pEffectBuffer != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] It failed to allocate memory");
109 errorCode = haptic_create_effect(pEffectBuffer.get(), sizeOfBuf, pPatterns.get(), numberOfElement);
110 r = ConvertErrorCode(errorCode);
111 SysTryReturnResult(NID_SYS, r == E_SUCCESS, r,"[%s] It failed to start vibrator.",GetErrorMessage(r));
113 SysLog(NID_SYS, "The number of patterns %d, repeate count %d", numberOfElement, repeatCount);
114 errorCode = haptic_vibrate_buffer_with_detail(__handle, pEffectBuffer.get(), (haptic_iteration_e)repeatCount, HAPTIC_FEEDBACK_5, HAPTIC_PRIORITY_MIN, &__effectHandle);
115 r = ConvertErrorCode(errorCode);
116 SysTryReturnResult(NID_SYS, r == E_SUCCESS, r,"[%s] It failed to start vibrator.",GetErrorMessage(r));
122 _VibratorImpl::Start(long onPeriod, long offPeriod, int count, int level)
124 SysTryReturnResult(NID_SYS, __handle != null, E_DEVICE_FAILED, "_VibratorImpl was not constructed.");
126 result r = E_SUCCESS;
128 haptic_state_e state;
129 errorCode = haptic_get_effect_state(__handle, __effectHandle, &state);
131 if ((haptic_error_e)errorCode == HAPTIC_ERROR_NONE)
133 if (state == HAPTIC_STATE_PLAYING)
136 SysTryReturnResult(NID_SYS, r == E_SUCCESS, r, "It failed to stop previous vibration.");
140 haptic_effect_element_s pattern[2];
142 pattern[0].haptic_duration = onPeriod;
143 pattern[0].haptic_level = (level == 0) ? HAPTIC_FEEDBACK_AUTO : (haptic_feedback_e)level;
145 pattern[1].haptic_duration = offPeriod;
146 pattern[1].haptic_level = HAPTIC_FEEDBACK_0;
148 int sizeOfBuf = MAX_SIZE_OF_VIBRATION_BUFFER;
149 std::unique_ptr<unsigned char[]> pEffectBuffer(new (std::nothrow) unsigned char[sizeOfBuf]);
150 SysTryReturnResult(NID_SYS, pEffectBuffer != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] It failed to allocate memory");
152 errorCode = haptic_create_effect(pEffectBuffer.get(), sizeOfBuf, pattern, sizeof(pattern) / sizeof(haptic_effect_element_s));
153 r = ConvertErrorCode(errorCode);
154 SysTryReturnResult(NID_SYS, r == E_SUCCESS, r,"[%s] It failed to start vibrator.",GetErrorMessage(r));
156 SysLog(NID_SYS, "onPeriod %d, offPeriod %d, count %d, level = %d", onPeriod, offPeriod, count, level);
157 errorCode = haptic_vibrate_buffer_with_detail(__handle, pEffectBuffer.get(), (haptic_iteration_e)count, HAPTIC_FEEDBACK_5, HAPTIC_PRIORITY_MIN, &__effectHandle);
158 r = ConvertErrorCode(errorCode);
159 SysTryReturnResult(NID_SYS, r == E_SUCCESS, r,"[%s] It failed to start vibrator.",GetErrorMessage(r));
165 _VibratorImpl::Stop(void)
167 SysTryReturnResult(NID_SYS, __handle != null, E_FAILURE, "_VibratorImpl was not constructed.");
169 int deviceResult = haptic_stop_all_effects(__handle);
170 SysTryReturnResult(NID_SYS, deviceResult == HAPTIC_ERROR_NONE,E_DEVICE_FAILED,
171 "[E_SYSTEM] Vibration stop operation has failed. [result:%d]", deviceResult);
172 SysLog(NID_SYS, "vibrator stopped handle = %d", __handle);
173 __effectHandle = null;
179 _VibratorImpl::ConvertErrorCode(int errorCode)
181 switch ((haptic_error_e)errorCode)
183 case HAPTIC_ERROR_NONE: return E_SUCCESS;
184 case HAPTIC_ERROR_INVALID_PARAMETER: return E_INVALID_ARG;
185 default: return E_DEVICE_FAILED;
191 _VibratorImpl::GetInstance(Vibrator& vibrator)
193 return vibrator.__pVibratorImpl;
197 _VibratorImpl::GetInstance(const Vibrator& vibrator)
199 return vibrator.__pVibratorImpl;