• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "compatible_connection.h"
18 #include "hdi_connection.h"
19 #include "hitrace_meter.h"
20 #include "sensors_errors.h"
21 
22 namespace OHOS {
23 namespace Sensors {
24 using namespace OHOS::HiviewDFX;
25 
26 namespace {
27 constexpr HiLogLabel LABEL = { LOG_CORE, MISC_LOG_DOMAIN, "VibratorHdiConnection" };
28 }
29 
ConnectHdi()30 int32_t VibratorHdiConnection::ConnectHdi()
31 {
32     iVibratorHdiConnection_ = std::make_unique<HdiConnection>();
33     int32_t ret = iVibratorHdiConnection_->ConnectHdi();
34     if (ret != 0) {
35         MISC_HILOGE("hdi direct failed");
36         iVibratorHdiConnection_ = std::make_unique<CompatibleConnection>();
37         ret = iVibratorHdiConnection_->ConnectHdi();
38     }
39     if (ret != 0) {
40         MISC_HILOGE("hdi connection failed");
41         return VIBRATOR_HDF_CONNECT_ERR;
42     }
43     return ERR_OK;
44 }
45 
StartOnce(uint32_t duration)46 int32_t VibratorHdiConnection::StartOnce(uint32_t duration)
47 {
48     StartTrace(HITRACE_TAG_SENSORS, "StartOnce");
49     int32_t ret = iVibratorHdiConnection_->StartOnce(duration);
50     FinishTrace(HITRACE_TAG_SENSORS);
51     if (ret != 0) {
52         MISC_HILOGE("StartOnce failed");
53         return VIBRATOR_ON_ERR;
54     }
55     return ERR_OK;
56 }
57 
Start(const std::string & effectType)58 int32_t VibratorHdiConnection::Start(const std::string &effectType)
59 {
60     StartTrace(HITRACE_TAG_SENSORS, "Start");
61     int32_t ret = iVibratorHdiConnection_->Start(effectType);
62     FinishTrace(HITRACE_TAG_SENSORS);
63     if (ret != 0) {
64         MISC_HILOGE("Start failed");
65         return VIBRATOR_ON_ERR;
66     }
67     return ERR_OK;
68 }
69 
Stop(VibratorStopMode mode)70 int32_t VibratorHdiConnection::Stop(VibratorStopMode mode)
71 {
72     StartTrace(HITRACE_TAG_SENSORS, "Stop");
73     int32_t ret = iVibratorHdiConnection_->Stop(mode);
74     FinishTrace(HITRACE_TAG_SENSORS);
75     if (ret != 0) {
76         MISC_HILOGE("Stop failed");
77         return VIBRATOR_OFF_ERR;
78     }
79     return ERR_OK;
80 }
81 
DestroyHdiConnection()82 int32_t VibratorHdiConnection::DestroyHdiConnection()
83 {
84     int32_t ret = iVibratorHdiConnection_->DestroyHdiConnection();
85     if (ret != 0) {
86         MISC_HILOGE("DestroyHdiConnection failed");
87         return VIBRATOR_HDF_CONNECT_ERR;
88     }
89     return ERR_OK;
90 }
91 }  // namespace Sensors
92 }  // namespace OHOS
93