1 /*
2 * Copyright (c) 2021-2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "vibrator_agent.h"
16
17 #include "parameters.h"
18
19 #include "sensors_errors.h"
20 #include "vibrator_service_client.h"
21
22 #undef LOG_TAG
23 #define LOG_TAG "VibratorNDK"
24
25 namespace OHOS {
26 namespace Sensors {
27 using OHOS::Sensors::VibratorServiceClient;
28
29 namespace {
30 constexpr int32_t DEFAULT_VIBRATOR_ID = 123;
31 int32_t g_loopCount = 1;
32 int32_t g_usage = USAGE_UNKNOWN;
33 VibratorParameter g_vibratorParameter;
34 const std::string PHONE_TYPE = "phone";
35 const int32_t INTENSITY_ADJUST_MIN = 0;
36 const int32_t INTENSITY_ADJUST_MAX = 100;
37 const int32_t FREQUENCY_ADJUST_MIN = -100;
38 const int32_t FREQUENCY_ADJUST_MAX = 100;
39 } // namespace
40
NormalizeErrCode(int32_t code)41 static int32_t NormalizeErrCode(int32_t code)
42 {
43 switch (code) {
44 case PERMISSION_DENIED: {
45 return PERMISSION_DENIED;
46 }
47 case PARAMETER_ERROR: {
48 return PARAMETER_ERROR;
49 }
50 case IS_NOT_SUPPORTED: {
51 return IS_NOT_SUPPORTED;
52 }
53 default: {
54 return DEVICE_OPERATION_FAILED;
55 }
56 }
57 }
58
SetLoopCount(int32_t count)59 bool SetLoopCount(int32_t count)
60 {
61 if (count <= 0) {
62 MISC_HILOGE("Input invalid, count is %{public}d", count);
63 return false;
64 }
65 g_loopCount = count;
66 return true;
67 }
68
StartVibrator(const char * effectId)69 int32_t StartVibrator(const char *effectId)
70 {
71 MISC_HILOGD("Time delay measurement:start time");
72 CHKPR(effectId, PARAMETER_ERROR);
73 auto &client = VibratorServiceClient::GetInstance();
74 int32_t ret = client.Vibrate(DEFAULT_VIBRATOR_ID, effectId, g_loopCount, g_usage);
75 g_loopCount = 1;
76 g_usage = USAGE_UNKNOWN;
77 if (ret != ERR_OK) {
78 MISC_HILOGE("Vibrate effectId failed, ret:%{public}d", ret);
79 return NormalizeErrCode(ret);
80 }
81 return SUCCESS;
82 }
83
StartVibratorOnce(int32_t duration)84 int32_t StartVibratorOnce(int32_t duration)
85 {
86 if (duration <= 0) {
87 MISC_HILOGE("duration is invalid");
88 return PARAMETER_ERROR;
89 }
90 auto &client = VibratorServiceClient::GetInstance();
91 int32_t ret = client.Vibrate(DEFAULT_VIBRATOR_ID, duration, g_usage);
92 g_usage = USAGE_UNKNOWN;
93 if (ret != ERR_OK) {
94 MISC_HILOGE("Vibrate duration failed, ret:%{public}d", ret);
95 return NormalizeErrCode(ret);
96 }
97 return SUCCESS;
98 }
99
IsSupportVibratorCustom()100 bool IsSupportVibratorCustom()
101 {
102 return (OHOS::system::GetDeviceType() == PHONE_TYPE);
103 }
104
PlayVibratorCustom(int32_t fd,int64_t offset,int64_t length)105 int32_t PlayVibratorCustom(int32_t fd, int64_t offset, int64_t length)
106 {
107 #ifdef OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM
108 MISC_HILOGD("Time delay measurement:start time");
109 if (fd < 0 || offset < 0 || length <= 0) {
110 MISC_HILOGE("Input parameter invalid, fd:%{public}d, offset:%{public}lld, length:%{public}lld",
111 fd, static_cast<long long>(offset), static_cast<long long>(length));
112 return PARAMETER_ERROR;
113 }
114 auto &client = VibratorServiceClient::GetInstance();
115 RawFileDescriptor rawFd = {
116 .fd = fd,
117 .offset = offset,
118 .length = length
119 };
120 int32_t ret = client.PlayVibratorCustom(DEFAULT_VIBRATOR_ID, rawFd, g_usage, g_vibratorParameter);
121 g_usage = USAGE_UNKNOWN;
122 g_vibratorParameter.intensity = INTENSITY_ADJUST_MAX;
123 g_vibratorParameter.frequency = 0;
124 if (ret != ERR_OK) {
125 MISC_HILOGE("PlayVibratorCustom failed, ret:%{public}d", ret);
126 return NormalizeErrCode(ret);
127 }
128 return SUCCESS;
129 #else
130 MISC_HILOGE("The device does not support this operation");
131 return IS_NOT_SUPPORTED;
132 #endif // OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM
133 }
134
StopVibrator(const char * mode)135 int32_t StopVibrator(const char *mode)
136 {
137 CHKPR(mode, PARAMETER_ERROR);
138 if (strcmp(mode, "time") != 0 && strcmp(mode, "preset") != 0) {
139 MISC_HILOGE("Input parameter invalid, mode is %{public}s", mode);
140 return PARAMETER_ERROR;
141 }
142 auto &client = VibratorServiceClient::GetInstance();
143 int32_t ret = client.StopVibrator(DEFAULT_VIBRATOR_ID, mode);
144 if (ret != ERR_OK) {
145 MISC_HILOGD("StopVibrator by mode failed, ret:%{public}d, mode:%{public}s", ret, mode);
146 return NormalizeErrCode(ret);
147 }
148 return SUCCESS;
149 }
150
Cancel()151 int32_t Cancel()
152 {
153 auto &client = VibratorServiceClient::GetInstance();
154 int32_t ret = client.StopVibrator(DEFAULT_VIBRATOR_ID);
155 if (ret != ERR_OK) {
156 MISC_HILOGD("StopVibrator failed, ret:%{public}d", ret);
157 return NormalizeErrCode(ret);
158 }
159 return SUCCESS;
160 }
161
SetUsage(int32_t usage)162 bool SetUsage(int32_t usage)
163 {
164 if ((usage < 0) || (usage >= USAGE_MAX)) {
165 MISC_HILOGE("Input invalid, usage is %{public}d", usage);
166 return false;
167 }
168 g_usage = usage;
169 return true;
170 }
171
IsSupportEffect(const char * effectId,bool * state)172 int32_t IsSupportEffect(const char *effectId, bool *state)
173 {
174 CHKPR(effectId, PARAMETER_ERROR);
175 auto &client = VibratorServiceClient::GetInstance();
176 int32_t ret = client.IsSupportEffect(effectId, *state);
177 if (ret != ERR_OK) {
178 MISC_HILOGE("Query effect support failed, ret:%{public}d, effectId:%{public}s", ret, effectId);
179 return NormalizeErrCode(ret);
180 }
181 return SUCCESS;
182 }
183
PreProcess(const VibratorFileDescription & fd,VibratorPackage & package)184 int32_t PreProcess(const VibratorFileDescription &fd, VibratorPackage &package)
185 {
186 auto &client = VibratorServiceClient::GetInstance();
187 int32_t ret = client.PreProcess(fd, package);
188 if (ret != ERR_OK) {
189 MISC_HILOGE("DecodeVibratorFile failed, ret:%{public}d", ret);
190 return NormalizeErrCode(ret);
191 }
192 return SUCCESS;
193 }
194
GetDelayTime(int32_t & delayTime)195 int32_t GetDelayTime(int32_t &delayTime)
196 {
197 auto &client = VibratorServiceClient::GetInstance();
198 int32_t ret = client.GetDelayTime(delayTime);
199 if (ret != ERR_OK) {
200 MISC_HILOGE("GetDelayTime failed, ret:%{public}d", ret);
201 return NormalizeErrCode(ret);
202 }
203 return SUCCESS;
204 }
205
PlayPattern(const VibratorPattern & pattern)206 int32_t PlayPattern(const VibratorPattern &pattern)
207 {
208 auto &client = VibratorServiceClient::GetInstance();
209 int32_t ret = client.PlayPattern(pattern, g_usage, g_vibratorParameter);
210 g_usage = USAGE_UNKNOWN;
211 g_vibratorParameter.intensity = INTENSITY_ADJUST_MAX;
212 g_vibratorParameter.frequency = 0;
213 if (ret != ERR_OK) {
214 MISC_HILOGE("PlayPattern failed, ret:%{public}d", ret);
215 return NormalizeErrCode(ret);
216 }
217 return SUCCESS;
218 }
219
FreeVibratorPackage(VibratorPackage & package)220 int32_t FreeVibratorPackage(VibratorPackage &package)
221 {
222 auto &client = VibratorServiceClient::GetInstance();
223 int32_t ret = client.FreeVibratorPackage(package);
224 if (ret != ERR_OK) {
225 MISC_HILOGE("FreeVibratorPackage failed, ret:%{public}d", ret);
226 return NormalizeErrCode(ret);
227 }
228 return SUCCESS;
229 }
230
SetParameters(const VibratorParameter & parameter)231 bool SetParameters(const VibratorParameter ¶meter)
232 {
233 if ((parameter.intensity < INTENSITY_ADJUST_MIN) || (parameter.intensity > INTENSITY_ADJUST_MAX) ||
234 (parameter.frequency < FREQUENCY_ADJUST_MIN) || (parameter.frequency > FREQUENCY_ADJUST_MAX)) {
235 MISC_HILOGE("Input invalid, intensity parameter is %{public}d, frequency parameter is %{public}d",
236 parameter.intensity, parameter.frequency);
237 return false;
238 }
239 g_vibratorParameter = parameter;
240 return true;
241 }
242 } // namespace Sensors
243 } // namespace OHOS