2 // Open Service Platform
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.
19 * @file FSys_VibratorImpl.cpp
20 * @brief This is the implementation file for _VibratorImpl class.
24 #include <unique_ptr.h>
26 #include <FBaseSysLog.h>
27 #include <FBaseResult.h>
28 #include <FSysVibrator.h>
30 #include <FSys_VibratorImpl.h>
32 namespace Tizen { namespace System
34 static const int MAX_SIZE_OF_VIBRATION_BUFFER = 64 * 1024; // Immersion lib guide value. 64kbytes
36 _VibratorImpl::_VibratorImpl(void)
38 , __effectHandle(null)
43 _VibratorImpl::~_VibratorImpl(void)
48 int deviceResult = haptic_close(__handle);
50 if (deviceResult != HAPTIC_ERROR_NONE)
52 SysLogException(NID_SYS, E_SYSTEM,
53 "[E_SYSTEM] Vibrator finalize operation has failed. [handle:%d, result:%d]", __handle, deviceResult);
59 _VibratorImpl::Construct(void)
61 SysTryReturnResult(NID_SYS, __handle == null, E_DEVICE_FAILED, "_VibratorImpl was already constructed.");
63 int ret = haptic_open(HAPTIC_DEVICE_ALL, &__handle);
65 SysTryReturnResult(NID_SYS, ret == HAPTIC_ERROR_NONE,E_SYSTEM,
66 "[E_SYSTEM] Vibrator initialize operation has failed. [result:%d]", ret);
67 SysLog(NID_SYS, "vibrator created handle = %d", __handle);
73 _VibratorImpl::Start(IntensityDurationVibrationPattern* patterns, int length, int repeatCount)
75 SysTryReturnResult(NID_SYS, __handle != null, E_DEVICE_FAILED, "_VibratorImpl was not constructed.");
80 errorCode = haptic_get_effect_state(__handle, __effectHandle, &state);
82 if ((haptic_error_e)errorCode == HAPTIC_ERROR_NONE)
84 if (state == HAPTIC_STATE_PLAYING)
87 SysTryReturnResult(NID_SYS, r == E_SUCCESS, r, "It failed to stop previous vibration.");
91 int numberOfElement = length / sizeof(IntensityDurationVibrationPattern);
92 std::unique_ptr<haptic_effect_element_s[]> pPatterns(new (std::nothrow) haptic_effect_element_s[numberOfElement * sizeof(haptic_effect_element_s)]);
93 SysTryReturnResult(NID_SYS, pPatterns != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] It failed to allocate memory");
95 for (int i = 0; i < numberOfElement; i++)
97 SysTryReturnResult(NID_SYS, patterns[i].duration >= 0, E_INVALID_ARG,
98 "[E_INVALID_ARG] The %dth pattern is the invalid duration[%d].", i, patterns[i].duration);
99 pPatterns[i].haptic_duration = patterns[i].duration;
101 SysTryReturnResult(NID_SYS, patterns[i].intensity >= -1 && patterns[i].intensity <= 100, E_INVALID_ARG,
102 "[E_INVALID_ARG] The %dth pattern is the invalid intensity[%d].", i, patterns[i].intensity);
103 pPatterns[i].haptic_level = (patterns[i].intensity == -1) ? HAPTIC_FEEDBACK_AUTO : (haptic_feedback_e)patterns[i].intensity;
106 int sizeOfBuf = MAX_SIZE_OF_VIBRATION_BUFFER;
107 std::unique_ptr<unsigned char[]> pEffectBuffer(new (std::nothrow) unsigned char[sizeOfBuf]);
108 SysTryReturnResult(NID_SYS, pEffectBuffer != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] It failed to allocate memory");
110 errorCode = haptic_create_effect(pEffectBuffer.get(), sizeOfBuf, pPatterns.get(), numberOfElement);
111 r = ConvertErrorCode(errorCode);
112 SysTryReturnResult(NID_SYS, r == E_SUCCESS, r,"[%s] It failed to start vibrator.",GetErrorMessage(r));
114 SysLog(NID_SYS, "The number of patterns %d, repeate count %d", numberOfElement, repeatCount);
115 errorCode = haptic_vibrate_buffer_with_detail(__handle, pEffectBuffer.get(), (haptic_iteration_e)repeatCount, HAPTIC_FEEDBACK_5, HAPTIC_PRIORITY_MIN, &__effectHandle);
116 r = ConvertErrorCode(errorCode);
117 SysTryReturnResult(NID_SYS, r == E_SUCCESS, r,"[%s] It failed to start vibrator.",GetErrorMessage(r));
123 _VibratorImpl::Start(long onPeriod, long offPeriod, int count, int level)
125 SysTryReturnResult(NID_SYS, __handle != null, E_DEVICE_FAILED, "_VibratorImpl was not constructed.");
127 result r = E_SUCCESS;
129 haptic_state_e state;
130 errorCode = haptic_get_effect_state(__handle, __effectHandle, &state);
132 if ((haptic_error_e)errorCode == HAPTIC_ERROR_NONE)
134 if (state == HAPTIC_STATE_PLAYING)
137 SysTryReturnResult(NID_SYS, r == E_SUCCESS, r, "It failed to stop previous vibration.");
141 haptic_effect_element_s pattern[2];
143 pattern[0].haptic_duration = onPeriod;
144 pattern[0].haptic_level = (level == 0) ? HAPTIC_FEEDBACK_AUTO : (haptic_feedback_e)level;
146 pattern[1].haptic_duration = offPeriod;
147 pattern[1].haptic_level = HAPTIC_FEEDBACK_0;
149 int sizeOfBuf = MAX_SIZE_OF_VIBRATION_BUFFER;
150 std::unique_ptr<unsigned char[]> pEffectBuffer(new (std::nothrow) unsigned char[sizeOfBuf]);
151 SysTryReturnResult(NID_SYS, pEffectBuffer != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] It failed to allocate memory");
153 errorCode = haptic_create_effect(pEffectBuffer.get(), sizeOfBuf, pattern, sizeof(pattern) / sizeof(haptic_effect_element_s));
154 r = ConvertErrorCode(errorCode);
155 SysTryReturnResult(NID_SYS, r == E_SUCCESS, r,"[%s] It failed to start vibrator.",GetErrorMessage(r));
157 SysLog(NID_SYS, "onPeriod %d, offPeriod %d, count %d, level = %d", onPeriod, offPeriod, count, level);
158 errorCode = haptic_vibrate_buffer_with_detail(__handle, pEffectBuffer.get(), (haptic_iteration_e)count, HAPTIC_FEEDBACK_5, HAPTIC_PRIORITY_MIN, &__effectHandle);
159 r = ConvertErrorCode(errorCode);
160 SysTryReturnResult(NID_SYS, r == E_SUCCESS, r,"[%s] It failed to start vibrator.",GetErrorMessage(r));
166 _VibratorImpl::Stop(void)
168 SysTryReturnResult(NID_SYS, __handle != null, E_FAILURE, "_VibratorImpl was not constructed.");
170 int deviceResult = haptic_stop_all_effects(__handle);
171 SysTryReturnResult(NID_SYS, deviceResult == HAPTIC_ERROR_NONE,E_SYSTEM,
172 "[E_SYSTEM] Vibration stop operation has failed. [result:%d]", deviceResult);
173 SysLog(NID_SYS, "vibrator stopped handle = %d", __handle);
174 __effectHandle = null;
180 _VibratorImpl::ConvertErrorCode(int errorCode)
182 switch ((haptic_error_e)errorCode)
184 case HAPTIC_ERROR_NONE: return E_SUCCESS;
185 case HAPTIC_ERROR_INVALID_PARAMETER: return E_INVALID_ARG;
186 default: return E_DEVICE_FAILED;
192 _VibratorImpl::GetInstance(Vibrator& vibrator)
194 return vibrator.__pVibratorImpl;
198 _VibratorImpl::GetInstance(const Vibrator& vibrator)
200 return vibrator.__pVibratorImpl;