• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 namespace OHOS {
23 namespace Sensors {
24 using OHOS::HiviewDFX::HiLog;
25 using OHOS::HiviewDFX::HiLogLabel;
26 using OHOS::Sensors::VibratorServiceClient;
27 
28 namespace {
29 constexpr HiLogLabel LABEL = { LOG_CORE, MISC_LOG_DOMAIN, "VibratorNDK" };
30 constexpr int32_t DEFAULT_VIBRATOR_ID = 123;
31 int32_t g_loopCount = 1;
32 int32_t g_usage = USAGE_UNKNOWN;
33 const std::string PHONE_TYPE = "phone";
34 } // namespace
35 
NormalizeErrCode(int32_t code)36 static int32_t NormalizeErrCode(int32_t code)
37 {
38     switch (code) {
39         case PERMISSION_DENIED: {
40             return PERMISSION_DENIED;
41         }
42         case PARAMETER_ERROR: {
43             return PARAMETER_ERROR;
44         }
45         case IS_NOT_SUPPORTED: {
46             return IS_NOT_SUPPORTED;
47         }
48         default: {
49             MISC_HILOGW("Operating the device fail");
50             return DEVICE_OPERATION_FAILED;
51         }
52     }
53 }
54 
SetLoopCount(int32_t count)55 bool SetLoopCount(int32_t count)
56 {
57     if (count <= 0) {
58         MISC_HILOGE("Input invalid, count is %{public}d", count);
59         return false;
60     }
61     g_loopCount = count;
62     return true;
63 }
64 
StartVibrator(const char * effectId)65 int32_t StartVibrator(const char *effectId)
66 {
67     CHKPR(effectId, PARAMETER_ERROR);
68     auto &client = VibratorServiceClient::GetInstance();
69     int32_t ret = client.Vibrate(DEFAULT_VIBRATOR_ID, effectId, g_loopCount, g_usage);
70     if (ret != ERR_OK) {
71         MISC_HILOGE("Vibrate effectId failed, ret:%{public}d", ret);
72         return NormalizeErrCode(ret);
73     }
74     g_loopCount = 1;
75     g_usage = USAGE_UNKNOWN;
76     return SUCCESS;
77 }
78 
StartVibratorOnce(int32_t duration)79 int32_t StartVibratorOnce(int32_t duration)
80 {
81     if (duration <= 0) {
82         MISC_HILOGE("duration is invalid");
83         return PARAMETER_ERROR;
84     }
85     auto &client = VibratorServiceClient::GetInstance();
86     int32_t ret = client.Vibrate(DEFAULT_VIBRATOR_ID, duration, g_usage);
87     if (ret != ERR_OK) {
88         MISC_HILOGE("Vibrate duration failed, ret:%{public}d", ret);
89         return NormalizeErrCode(ret);
90     }
91     g_usage = USAGE_UNKNOWN;
92     return SUCCESS;
93 }
94 
IsSupportVibratorCustom()95 bool IsSupportVibratorCustom()
96 {
97     return (OHOS::system::GetDeviceType() == PHONE_TYPE);
98 }
99 
PlayVibratorCustom(int32_t fd,int64_t offset,int64_t length)100 int32_t PlayVibratorCustom(int32_t fd, int64_t offset, int64_t length)
101 {
102 #ifdef OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM
103     if (fd < 0 || offset < 0 || length <= 0) {
104         MISC_HILOGE("Input parameter invalid, fd:%{public}d, offset:%{public}lld, length:%{public}lld",
105             fd, static_cast<long long>(offset), static_cast<long long>(length));
106         return PARAMETER_ERROR;
107     }
108     auto &client = VibratorServiceClient::GetInstance();
109     RawFileDescriptor rawFd = {
110         .fd = fd,
111         .offset = offset,
112         .length = length
113     };
114     int32_t ret = client.PlayVibratorCustom(DEFAULT_VIBRATOR_ID, rawFd, g_usage);
115     if (ret != ERR_OK) {
116         MISC_HILOGE("PlayVibratorCustom failed, ret:%{public}d", ret);
117         return NormalizeErrCode(ret);
118     }
119     g_usage = USAGE_UNKNOWN;
120     return SUCCESS;
121 #else
122     MISC_HILOGE("The device does not support this operation");
123     return IS_NOT_SUPPORTED;
124 #endif // OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM
125 }
126 
StopVibrator(const char * mode)127 int32_t StopVibrator(const char *mode)
128 {
129     CHKPR(mode, PARAMETER_ERROR);
130     if (strcmp(mode, "time") != 0 && strcmp(mode, "preset") != 0) {
131         MISC_HILOGE("Input parameter invalid, mode is %{public}s", mode);
132         return PARAMETER_ERROR;
133     }
134     auto &client = VibratorServiceClient::GetInstance();
135     int32_t ret = client.StopVibrator(DEFAULT_VIBRATOR_ID, mode);
136     if (ret != ERR_OK) {
137         MISC_HILOGE("StopVibrator failed, ret:%{public}d", ret);
138         return NormalizeErrCode(ret);
139     }
140     return SUCCESS;
141 }
142 
Cancel()143 int32_t Cancel()
144 {
145     auto &client = VibratorServiceClient::GetInstance();
146     int32_t ret = client.StopVibrator(DEFAULT_VIBRATOR_ID);
147     if (ret != ERR_OK) {
148         MISC_HILOGE("StopVibrator failed, ret:%{public}d", ret);
149         return NormalizeErrCode(ret);
150     }
151     return SUCCESS;
152 }
153 
SetUsage(int32_t usage)154 bool SetUsage(int32_t usage)
155 {
156     if ((usage < 0) || (usage >= USAGE_MAX)) {
157         MISC_HILOGE("Input invalid, usage is %{public}d", usage);
158         return false;
159     }
160     g_usage = usage;
161     return true;
162 }
163 
IsSupportEffect(const char * effectId,bool * state)164 int32_t IsSupportEffect(const char *effectId, bool *state)
165 {
166     CHKPR(effectId, PARAMETER_ERROR);
167     auto &client = VibratorServiceClient::GetInstance();
168     int32_t ret = client.IsSupportEffect(effectId, *state);
169     if (ret != ERR_OK) {
170         MISC_HILOGE("Query effect support failed, ret:%{public}d", ret);
171         return NormalizeErrCode(ret);
172     }
173     return SUCCESS;
174 }
175 }  // namespace Sensors
176 }  // namespace OHOS