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
16 #include "battery_vibrate.h"
17
18 #include <unistd.h>
19 #include "file_ex.h"
20 #include "battery_log.h"
21
22 namespace OHOS {
23 namespace HDI {
24 namespace Battery {
25 namespace V1_1 {
26 namespace {
27 const std::string VIBRATOR_PLAYMODE_PATH = "/sys/class/leds/vibrator/play_mode";
28 const std::string VIBRATOR_DURATIONMODE_PATH = "/sys/class/leds/vibrator/duration";
29 const std::string VIBRATOR_ACTIVATEMODE_PATH = "/sys/class/leds/vibrator/activate";
30 const std::string DURATION_MODE_DERECT = "direct";
31 const std::string DURATION_MODE_AUDIO = "audio";
32 constexpr int32_t VIBRATION_PLAYMODE = 0;
33 constexpr int32_t VIBRATION_DURATIONMODE = 1;
34 constexpr int32_t ACTIVE = 1;
35 constexpr int32_t DEACTIVE = 0;
36 constexpr int32_t VIBRATE_DELAY_MS = 5;
37 constexpr int32_t USEC_TO_MSEC = 1000;
38 }
39
InitVibration()40 bool BatteryVibrate::InitVibration()
41 {
42 BATTERY_HILOGD(FEATURE_CHARGING, "start init vibrate");
43
44 if (FileExists(VIBRATOR_PLAYMODE_PATH)) {
45 BATTERY_HILOGI(FEATURE_CHARGING, "vibrate path is play mode path");
46 vibrateMode_ = VIBRATION_PLAYMODE;
47 return true;
48 }
49
50 if (FileExists(VIBRATOR_DURATIONMODE_PATH)) {
51 BATTERY_HILOGI(FEATURE_CHARGING, "vibrate path is duration path");
52 vibrateMode_ = VIBRATION_DURATIONMODE;
53 return true;
54 }
55
56 BATTERY_HILOGW(FEATURE_CHARGING, "not support vibrate path");
57 return false;
58 }
59
HandlePlayMode(int32_t time)60 void BatteryVibrate::HandlePlayMode(int32_t time)
61 {
62 SetPlayMode(DURATION_MODE_DERECT);
63 SetDuration(time);
64 ActivateVibration(true);
65 usleep((time + VIBRATE_DELAY_MS) * USEC_TO_MSEC);
66 SetPlayMode(DURATION_MODE_AUDIO);
67 }
68
HandleDurationMode(int32_t time)69 void BatteryVibrate::HandleDurationMode(int32_t time)
70 {
71 SetDuration(time);
72 ActivateVibration(true);
73 usleep((time + VIBRATE_DELAY_MS) * USEC_TO_MSEC);
74 ActivateVibration(false);
75 }
76
HandleVibration(int32_t time) const77 void BatteryVibrate::HandleVibration(int32_t time) const
78 {
79 switch (vibrateMode_) {
80 case VIBRATION_PLAYMODE: {
81 BATTERY_HILOGD(FEATURE_CHARGING, "vibrate play mode");
82 HandlePlayMode(time);
83 break;
84 }
85 case VIBRATION_DURATIONMODE: {
86 BATTERY_HILOGD(FEATURE_CHARGING, "vibrate duration mode");
87 HandleDurationMode(time);
88 break;
89 }
90 default: {
91 BATTERY_HILOGD(FEATURE_CHARGING, "vibrate unknown mode");
92 break;
93 }
94 }
95 }
96
SetPlayMode(const std::string & modeName)97 void BatteryVibrate::SetPlayMode(const std::string& modeName)
98 {
99 FILE* file = fopen(VIBRATOR_PLAYMODE_PATH.c_str(), "w");
100 if (file == nullptr) {
101 BATTERY_HILOGW(FEATURE_CHARGING, "play mode path open failed.");
102 return;
103 }
104 if (fprintf(file, "%s\n", modeName.c_str()) < 0) {
105 BATTERY_HILOGW(FEATURE_CHARGING, "play mode fprintf direct failed.");
106 }
107 if (fclose(file) < 0) {
108 BATTERY_HILOGW(FEATURE_CHARGING, "play mode fclose failed.");
109 }
110 }
111
ActivateVibration(bool isActive)112 void BatteryVibrate::ActivateVibration(bool isActive)
113 {
114 int32_t value = isActive ? ACTIVE : DEACTIVE;
115 FILE* file = fopen(VIBRATOR_ACTIVATEMODE_PATH.c_str(), "w");
116 if (file == nullptr) {
117 BATTERY_HILOGW(FEATURE_CHARGING, "activate mode path open failed");
118 return;
119 }
120 if (fprintf(file, "%d\n", value) < 0) {
121 BATTERY_HILOGW(FEATURE_CHARGING, "activate mode fprintf failed, value=%{public}d", value);
122 }
123 if (fclose(file) < 0) {
124 BATTERY_HILOGW(FEATURE_CHARGING, "activate mode fclose failed");
125 }
126 }
127
SetDuration(int32_t time)128 void BatteryVibrate::SetDuration(int32_t time)
129 {
130 FILE* file = fopen(VIBRATOR_DURATIONMODE_PATH.c_str(), "w");
131 if (file == nullptr) {
132 BATTERY_HILOGW(FEATURE_CHARGING, "duration mode path open failed");
133 return;
134 }
135 if (fprintf(file, "%d\n", time) < 0) {
136 BATTERY_HILOGW(FEATURE_CHARGING, "duration mode fprintf time failed, time=%{public}d", time);
137 }
138 if (fclose(file) < 0) {
139 BATTERY_HILOGW(FEATURE_CHARGING, "duration mode fclose failed.");
140 }
141 }
142 } // namespace V1_1
143 } // namespace Battery
144 } // namespace HDI
145 } // namespace OHOS
146