• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 #include "sensors_errors.h"
17 #include "vibrator_service_client.h"
18 
19 namespace OHOS {
20 namespace Sensors {
21 using OHOS::HiviewDFX::HiLog;
22 using OHOS::HiviewDFX::HiLogLabel;
23 using OHOS::Sensors::VibratorServiceClient;
24 
25 static const HiLogLabel LABEL = { LOG_CORE, MISC_LOG_DOMAIN, "VibratorNDK" };
26 static const int32_t DEFAULT_VIBRATOR_ID = 123;
27 static int32_t g_loopCount = 1;
28 static int32_t g_usage = USAGE_UNKNOWN;
29 
NormalizeErrCode(int32_t code)30 static int32_t NormalizeErrCode(int32_t code)
31 {
32     switch (code) {
33         case PERMISSION_DENIED: {
34             return PERMISSION_DENIED;
35         }
36         case PARAMETER_ERROR: {
37             return PARAMETER_ERROR;
38         }
39         default: {
40             return DEVICE_OPERATION_FAILED;
41         }
42     }
43 }
44 
SetLoopCount(int32_t count)45 bool SetLoopCount(int32_t count)
46 {
47     if (count <= 0) {
48         MISC_HILOGE("input invalid, count is %{public}d", count);
49         return false;
50     }
51     g_loopCount = count;
52     return true;
53 }
54 
StartVibrator(const char * effectId)55 int32_t StartVibrator(const char *effectId)
56 {
57     CHKPR(effectId, PARAMETER_ERROR);
58     auto &client = VibratorServiceClient::GetInstance();
59     int32_t ret = client.Vibrate(DEFAULT_VIBRATOR_ID, effectId, g_loopCount, g_usage);
60     if (ret != ERR_OK) {
61         MISC_HILOGE("vibrator effectId failed, ret: %{public}d", ret);
62         return NormalizeErrCode(ret);
63     }
64     g_loopCount = 1;
65     g_usage = USAGE_UNKNOWN;
66     return SUCCESS;
67 }
68 
StartVibratorOnce(int32_t duration)69 int32_t StartVibratorOnce(int32_t duration)
70 {
71     if (duration <= 0) {
72         MISC_HILOGE("duration is invalid");
73         return PARAMETER_ERROR;
74     }
75     auto &client = VibratorServiceClient::GetInstance();
76     int32_t ret = client.Vibrate(DEFAULT_VIBRATOR_ID, duration, g_usage);
77     if (ret != ERR_OK) {
78         MISC_HILOGE("vibrator duration failed, ret: %{public}d", ret);
79         return NormalizeErrCode(ret);
80     }
81     g_usage = USAGE_UNKNOWN;
82     return SUCCESS;
83 }
84 
StopVibrator(const char * mode)85 int32_t StopVibrator(const char *mode)
86 {
87     CHKPR(mode, PARAMETER_ERROR);
88     if (strcmp(mode, "time") != 0 && strcmp(mode, "preset") != 0) {
89         MISC_HILOGE("mode is invalid, mode is %{public}s", mode);
90         return PARAMETER_ERROR;
91     }
92     auto &client = VibratorServiceClient::GetInstance();
93     int32_t ret = client.Stop(DEFAULT_VIBRATOR_ID, mode);
94     if (ret != ERR_OK) {
95         MISC_HILOGE("client is failed, ret: %{public}d", ret);
96         return NormalizeErrCode(ret);
97     }
98     return SUCCESS;
99 }
100 
SetUsage(int32_t usage)101 bool SetUsage(int32_t usage)
102 {
103     if ((usage < 0) || (usage >= USAGE_MAX)) {
104         MISC_HILOGE("input invalid, usage is %{public}d", usage);
105         return false;
106     }
107     g_usage = usage;
108     return true;
109 }
110 }  // namespace Sensors
111 }  // namespace OHOS