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_hdi_connection.h"
16
17 #include "hitrace_meter.h"
18
19 #include "compatible_connection.h"
20 #include "hdi_connection.h"
21 #include "sensors_errors.h"
22
23 namespace OHOS {
24 namespace Sensors {
25 using namespace OHOS::HiviewDFX;
26
27 namespace {
28 constexpr HiLogLabel LABEL = { LOG_CORE, MISC_LOG_DOMAIN, "VibratorHdiConnection" };
29 } // namespace
30
ConnectHdi()31 int32_t VibratorHdiConnection::ConnectHdi()
32 {
33 iVibratorHdiConnection_ = std::make_unique<HdiConnection>();
34 int32_t ret = iVibratorHdiConnection_->ConnectHdi();
35 if (ret != ERR_OK) {
36 MISC_HILOGE("Hdi direct failed");
37 iVibratorHdiConnection_ = std::make_unique<CompatibleConnection>();
38 ret = iVibratorHdiConnection_->ConnectHdi();
39 }
40 if (ret != ERR_OK) {
41 MISC_HILOGE("Hdi connection failed");
42 return VIBRATOR_HDF_CONNECT_ERR;
43 }
44 return ERR_OK;
45 }
46
StartOnce(uint32_t duration)47 int32_t VibratorHdiConnection::StartOnce(uint32_t duration)
48 {
49 CHKPR(iVibratorHdiConnection_, VIBRATOR_HDF_CONNECT_ERR);
50 StartTrace(HITRACE_TAG_SENSORS, "StartOnce");
51 int32_t ret = iVibratorHdiConnection_->StartOnce(duration);
52 FinishTrace(HITRACE_TAG_SENSORS);
53 if (ret != 0) {
54 MISC_HILOGE("StartOnce failed");
55 return VIBRATOR_ON_ERR;
56 }
57 return ERR_OK;
58 }
59
Start(const std::string & effectType)60 int32_t VibratorHdiConnection::Start(const std::string &effectType)
61 {
62 CHKPR(iVibratorHdiConnection_, VIBRATOR_HDF_CONNECT_ERR);
63 StartTrace(HITRACE_TAG_SENSORS, "Start");
64 int32_t ret = iVibratorHdiConnection_->Start(effectType);
65 FinishTrace(HITRACE_TAG_SENSORS);
66 if (ret != 0) {
67 MISC_HILOGE("Start failed");
68 return VIBRATOR_ON_ERR;
69 }
70 return ERR_OK;
71 }
72
73 #ifdef OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM
EnableCompositeEffect(const HdfCompositeEffect & hdfCompositeEffect)74 int32_t VibratorHdiConnection::EnableCompositeEffect(const HdfCompositeEffect &hdfCompositeEffect)
75 {
76 CHKPR(iVibratorHdiConnection_, VIBRATOR_HDF_CONNECT_ERR);
77 StartTrace(HITRACE_TAG_SENSORS, "EnableCompositeEffect");
78 int32_t ret = iVibratorHdiConnection_->EnableCompositeEffect(hdfCompositeEffect);
79 FinishTrace(HITRACE_TAG_SENSORS);
80 if (ret != 0) {
81 MISC_HILOGE("EnableCompositeEffect failed");
82 return VIBRATOR_ON_ERR;
83 }
84 return ERR_OK;
85 }
86
IsVibratorRunning()87 bool VibratorHdiConnection::IsVibratorRunning()
88 {
89 CHKPR(iVibratorHdiConnection_, VIBRATOR_HDF_CONNECT_ERR);
90 return iVibratorHdiConnection_->IsVibratorRunning();
91 }
92 #endif // OHOS_BUILD_ENABLE_VIBRATOR_CUSTOM
93
GetEffectInfo(const std::string & effect)94 std::optional<HdfEffectInfo> VibratorHdiConnection::GetEffectInfo(const std::string &effect)
95 {
96 if (iVibratorHdiConnection_ == nullptr) {
97 MISC_HILOGE("Connect hdi failed");
98 return std::nullopt;
99 }
100 StartTrace(HITRACE_TAG_SENSORS, "GetEffectInfo");
101 std::optional<HdfEffectInfo> ret = iVibratorHdiConnection_->GetEffectInfo(effect);
102 FinishTrace(HITRACE_TAG_SENSORS);
103 return ret;
104 }
105
Stop(HdfVibratorMode mode)106 int32_t VibratorHdiConnection::Stop(HdfVibratorMode mode)
107 {
108 CHKPR(iVibratorHdiConnection_, VIBRATOR_HDF_CONNECT_ERR);
109 StartTrace(HITRACE_TAG_SENSORS, "Stop");
110 int32_t ret = iVibratorHdiConnection_->Stop(mode);
111 FinishTrace(HITRACE_TAG_SENSORS);
112 if (ret != 0) {
113 MISC_HILOGE("Stop failed");
114 return VIBRATOR_OFF_ERR;
115 }
116 return ERR_OK;
117 }
118
DestroyHdiConnection()119 int32_t VibratorHdiConnection::DestroyHdiConnection()
120 {
121 CHKPR(iVibratorHdiConnection_, VIBRATOR_HDF_CONNECT_ERR);
122 int32_t ret = iVibratorHdiConnection_->DestroyHdiConnection();
123 if (ret != 0) {
124 MISC_HILOGE("DestroyHdiConnection failed");
125 return VIBRATOR_HDF_CONNECT_ERR;
126 }
127 return ERR_OK;
128 }
129 } // namespace Sensors
130 } // namespace OHOS
131