1 /*
2  * Copyright (c) 2021-2024 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_hdi_connection.h"
16 
17 #include "hitrace_meter.h"
18 
19 #ifdef BUILD_VARIANT_ENG
20 #include "compatible_connection.h"
21 #endif // BUILD_VARIANT_ENG
22 #include "hdi_connection.h"
23 #include "sensors_errors.h"
24 
25 #undef LOG_TAG
26 #define LOG_TAG "VibratorHdiConnection"
27 
28 namespace OHOS {
29 namespace Sensors {
30 
ConnectHdi()31 int32_t VibratorHdiConnection::ConnectHdi()
32 {
33     iVibratorHdiConnection_ = std::make_unique<HdiConnection>();
34     int32_t ret = iVibratorHdiConnection_->ConnectHdi();
35 #ifdef BUILD_VARIANT_ENG
36     if (ret != ERR_OK) {
37         MISC_HILOGE("Hdi direct failed");
38         iVibratorHdiConnection_ = std::make_unique<CompatibleConnection>();
39         ret = iVibratorHdiConnection_->ConnectHdi();
40     }
41     if (ret != ERR_OK) {
42         MISC_HILOGE("Hdi connection failed");
43         return VIBRATOR_HDF_CONNECT_ERR;
44     }
45 #endif // BUILD_VARIANT_ENG
46     return ret;
47 }
48 
StartOnce(uint32_t duration)49 int32_t VibratorHdiConnection::StartOnce(uint32_t duration)
50 {
51     CHKPR(iVibratorHdiConnection_, VIBRATOR_HDF_CONNECT_ERR);
52     StartTrace(HITRACE_TAG_SENSORS, "StartOnce");
53     int32_t ret = iVibratorHdiConnection_->StartOnce(duration);
54     FinishTrace(HITRACE_TAG_SENSORS);
55     if (ret != 0) {
56         MISC_HILOGE("StartOnce failed");
57         return VIBRATOR_ON_ERR;
58     }
59     return ERR_OK;
60 }
61 
Start(const std::string & effectType)62 int32_t VibratorHdiConnection::Start(const std::string &effectType)
63 {
64     CHKPR(iVibratorHdiConnection_, VIBRATOR_HDF_CONNECT_ERR);
65     StartTrace(HITRACE_TAG_SENSORS, "Start");
66     int32_t ret = iVibratorHdiConnection_->Start(effectType);
67     FinishTrace(HITRACE_TAG_SENSORS);
68     if (ret != 0) {
69         MISC_HILOGE("Start failed");
70         return VIBRATOR_ON_ERR;
71     }
72     return ERR_OK;
73 }
74 
75 #ifdef OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM
EnableCompositeEffect(const HdfCompositeEffect & hdfCompositeEffect)76 int32_t VibratorHdiConnection::EnableCompositeEffect(const HdfCompositeEffect &hdfCompositeEffect)
77 {
78     CHKPR(iVibratorHdiConnection_, VIBRATOR_HDF_CONNECT_ERR);
79     StartTrace(HITRACE_TAG_SENSORS, "EnableCompositeEffect");
80     int32_t ret = iVibratorHdiConnection_->EnableCompositeEffect(hdfCompositeEffect);
81     FinishTrace(HITRACE_TAG_SENSORS);
82     if (ret != 0) {
83         MISC_HILOGE("EnableCompositeEffect failed");
84         return VIBRATOR_ON_ERR;
85     }
86     return ERR_OK;
87 }
88 
IsVibratorRunning()89 bool VibratorHdiConnection::IsVibratorRunning()
90 {
91     CHKPR(iVibratorHdiConnection_, false);
92     return iVibratorHdiConnection_->IsVibratorRunning();
93 }
94 #endif // OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM
95 
GetEffectInfo(const std::string & effect)96 std::optional<HdfEffectInfo> VibratorHdiConnection::GetEffectInfo(const std::string &effect)
97 {
98     if (iVibratorHdiConnection_ == nullptr) {
99         MISC_HILOGE("Connect hdi failed");
100         return std::nullopt;
101     }
102     StartTrace(HITRACE_TAG_SENSORS, "GetEffectInfo");
103     std::optional<HdfEffectInfo> ret = iVibratorHdiConnection_->GetEffectInfo(effect);
104     FinishTrace(HITRACE_TAG_SENSORS);
105     return ret;
106 }
107 
Stop(HdfVibratorModeV1_2 mode)108 int32_t VibratorHdiConnection::Stop(HdfVibratorModeV1_2 mode)
109 {
110     CHKPR(iVibratorHdiConnection_, VIBRATOR_HDF_CONNECT_ERR);
111     StartTrace(HITRACE_TAG_SENSORS, "Stop");
112     int32_t ret = iVibratorHdiConnection_->Stop(mode);
113     FinishTrace(HITRACE_TAG_SENSORS);
114     if (ret != 0) {
115         MISC_HILOGE("Stop failed");
116         return VIBRATOR_OFF_ERR;
117     }
118     return ERR_OK;
119 }
120 
GetDelayTime(int32_t mode,int32_t & delayTime)121 int32_t VibratorHdiConnection::GetDelayTime(int32_t mode, int32_t &delayTime)
122 {
123     CHKPR(iVibratorHdiConnection_, VIBRATOR_HDF_CONNECT_ERR);
124     return iVibratorHdiConnection_->GetDelayTime(mode, delayTime);
125 }
126 
GetVibratorCapacity(VibratorCapacity & capacity)127 int32_t VibratorHdiConnection::GetVibratorCapacity(VibratorCapacity &capacity)
128 {
129     CHKPR(iVibratorHdiConnection_, VIBRATOR_HDF_CONNECT_ERR);
130     return iVibratorHdiConnection_->GetVibratorCapacity(capacity);
131 }
132 
PlayPattern(const VibratePattern & pattern)133 int32_t VibratorHdiConnection::PlayPattern(const VibratePattern &pattern)
134 {
135     CHKPR(iVibratorHdiConnection_, VIBRATOR_HDF_CONNECT_ERR);
136     return iVibratorHdiConnection_->PlayPattern(pattern);
137 }
138 
DestroyHdiConnection()139 int32_t VibratorHdiConnection::DestroyHdiConnection()
140 {
141     CHKPR(iVibratorHdiConnection_, VIBRATOR_HDF_CONNECT_ERR);
142     int32_t ret = iVibratorHdiConnection_->DestroyHdiConnection();
143     if (ret != 0) {
144         MISC_HILOGE("DestroyHdiConnection failed");
145         return VIBRATOR_HDF_CONNECT_ERR;
146     }
147     return ERR_OK;
148 }
149 
StartByIntensity(const std::string & effect,int32_t intensity)150 int32_t VibratorHdiConnection::StartByIntensity(const std::string &effect, int32_t intensity)
151 {
152     CHKPR(iVibratorHdiConnection_, VIBRATOR_HDF_CONNECT_ERR);
153     StartTrace(HITRACE_TAG_SENSORS, "StartByIntensity");
154     int32_t ret = iVibratorHdiConnection_->StartByIntensity(effect, intensity);
155     FinishTrace(HITRACE_TAG_SENSORS);
156     if (ret != 0) {
157         MISC_HILOGE("StartByIntensity failed");
158         return VIBRATOR_ON_ERR;
159     }
160     return ERR_OK;
161 }
162 
GetAllWaveInfo(std::vector<HdfWaveInformation> & waveInfos)163 int32_t VibratorHdiConnection::GetAllWaveInfo(std::vector<HdfWaveInformation> &waveInfos)
164 {
165     CHKPR(iVibratorHdiConnection_, VIBRATOR_HDF_CONNECT_ERR);
166     return iVibratorHdiConnection_->GetAllWaveInfo(waveInfos);
167 }
168 }  // namespace Sensors
169 }  // namespace OHOS
170