1 /*
2 * Copyright (c) 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
16 #include "vibrator_thread.h"
17
18 #include "sensors_errors.h"
19 #include "vibrator_hdi_connection.h"
20
21 namespace OHOS {
22 namespace Sensors {
23 namespace {
24 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, MISC_LOG_DOMAIN, "VibratorThread" };
25 } // namespace
26
Run()27 bool VibratorThread::Run()
28 {
29 VibrateInfo info = GetCurrentVibrateInfo();
30 std::unique_lock<std::mutex> vibrateLck(vibrateMutex_);
31 if (currentVibration_.mode == "time") {
32 int32_t ret = VibratorDevice.StartOnce(static_cast<uint32_t>(info.duration));
33 if (ret != SUCCESS) {
34 MISC_HILOGE("StartOnce fail, duration: %{public}d, pid: %{public}d",
35 info.duration, info.pid);
36 return false;
37 }
38 cv_.wait_for(vibrateLck, std::chrono::milliseconds(info.duration));
39 VibratorDevice.Stop(IVibratorHdiConnection::VIBRATOR_STOP_MODE_TIME);
40 std::unique_lock<std::mutex> readyLck(readyMutex_);
41 if (ready_) {
42 MISC_HILOGI("Stop duration: %{public}d, pid: %{public}d",
43 info.duration, info.pid);
44 SetReadyStatus(false);
45 }
46 } else if (info.mode == "preset") {
47 for (int32_t i = 0; i < info.count; ++i) {
48 std::string effect = info.effect;
49 int32_t ret = VibratorDevice.Start(effect);
50 if (ret != SUCCESS) {
51 MISC_HILOGE("Vibrate effect %{public}s failed, pid: %{public}d", effect.c_str(), info.pid);
52 return false;
53 }
54 cv_.wait_for(vibrateLck, std::chrono::milliseconds(info.duration));
55 VibratorDevice.Stop(IVibratorHdiConnection::VIBRATOR_STOP_MODE_PRESET);
56 std::unique_lock<std::mutex> readyLck(readyMutex_);
57 if (ready_) {
58 MISC_HILOGI("Stop effect %{public}s failed, pid: %{public}d", effect.c_str(), currentVibration_.pid);
59 SetReadyStatus(false);
60 break;
61 }
62 }
63 }
64 return false;
65 }
66
UpdateVibratorEffect(VibrateInfo info)67 void VibratorThread::UpdateVibratorEffect(VibrateInfo info)
68 {
69 std::unique_lock<std::mutex> lck(currentVibrationMutex_);
70 currentVibration_ = info;
71 }
72
GetCurrentVibrateInfo()73 VibrateInfo VibratorThread::GetCurrentVibrateInfo()
74 {
75 std::unique_lock<std::mutex> lck(currentVibrationMutex_);
76 return currentVibration_;
77 }
78
SetReadyStatus(bool status)79 void VibratorThread::SetReadyStatus(bool status)
80 {
81 ready_ = status;
82 }
83
NotifyExit()84 void VibratorThread::NotifyExit()
85 {
86 std::unique_lock<std::mutex> readyLck(readyMutex_);
87 SetReadyStatus(true);
88 cv_.notify_one();
89 }
90 } // namespace Sensors
91 } // namespace OHOS
92