1 /*
2 * Copyright (c) 2022-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_infos.h"
16
17 #include <cerrno>
18 #include <cinttypes>
19
20 #include <sys/stat.h>
21 #include <unistd.h>
22
23 #include "sensors_errors.h"
24
25 #undef LOG_TAG
26 #define LOG_TAG "MiscdeviceVibratorInfos"
27
28 namespace OHOS {
29 namespace Sensors {
Dump() const30 void VibratePattern::Dump() const
31 {
32 int32_t size = static_cast<int32_t>(events.size());
33 MISC_HILOGD("Pattern startTime:%{public}d, eventSize:%{public}d", startTime, size);
34 for (int32_t i = 0; i < size; ++i) {
35 std::string tag = (events[i].tag == EVENT_TAG_CONTINUOUS) ? "continuous" : "transient";
36 int32_t pointSize = static_cast<int32_t>(events[i].points.size());
37 MISC_HILOGD("Event tag:%{public}s, time:%{public}d, duration:%{public}d,"
38 "intensity:%{public}d, frequency:%{public}d, index:%{public}d, curve pointSize:%{public}d",
39 tag.c_str(), events[i].time, events[i].duration, events[i].intensity,
40 events[i].frequency, events[i].index, pointSize);
41 for (int32_t j = 0; j < pointSize; ++j) {
42 MISC_HILOGD("Curve point time:%{public}d, intensity:%{public}d, frequency:%{public}d",
43 events[i].points[j].time, events[i].points[j].intensity, events[i].points[j].frequency);
44 }
45 }
46 }
47
Dump() const48 void VibratePackage::Dump() const
49 {
50 int32_t size = static_cast<int32_t>(patterns.size());
51 MISC_HILOGD("Vibrate package pattern size:%{public}d", size);
52 for (int32_t i = 0; i < size; ++i) {
53 patterns[i].Dump();
54 }
55 }
56
Dump() const57 void VibratorCapacity::Dump() const
58 {
59 std::string isSupportHdHapticStr = isSupportHdHaptic ? "true" : "false";
60 std::string isSupportPresetMappingStr = isSupportPresetMapping ? "true" : "false";
61 std::string isSupportTimeDelayStr = isSupportTimeDelay ? "true" : "false";
62 MISC_HILOGD("SupportHdHaptic:%{public}s, SupportPresetMapping:%{public}s, "
63 "SupportTimeDelayStr:%{public}s", isSupportHdHapticStr.c_str(),
64 isSupportPresetMappingStr.c_str(), isSupportTimeDelayStr.c_str());
65 }
66
GetVibrateMode()67 int32_t VibratorCapacity::GetVibrateMode()
68 {
69 if (isSupportHdHaptic) {
70 return VIBRATE_MODE_HD;
71 }
72 if (isSupportPresetMapping) {
73 return VIBRATE_MODE_MAPPING;
74 }
75 if (isSupportTimeDelay) {
76 return VIBRATE_MODE_TIMES;
77 }
78 return -1;
79 }
80
Marshalling(Parcel & parcel) const81 bool VibratePattern::Marshalling(Parcel &parcel) const
82 {
83 if (!parcel.WriteInt32(startTime)) {
84 MISC_HILOGE("Write pattern's startTime failed");
85 return false;
86 }
87 if (!parcel.WriteInt32(static_cast<int32_t>(events.size()))) {
88 MISC_HILOGE("Write events's size failed");
89 return false;
90 }
91 for (size_t i = 0; i < events.size(); ++i) {
92 if (!parcel.WriteInt32(static_cast<int32_t>(events[i].tag))) {
93 MISC_HILOGE("Write tag failed");
94 return false;
95 }
96 if (!parcel.WriteInt32(events[i].time)) {
97 MISC_HILOGE("Write events's time failed");
98 return false;
99 }
100 if (!parcel.WriteInt32(events[i].duration)) {
101 MISC_HILOGE("Write duration failed");
102 return false;
103 }
104 if (!parcel.WriteInt32(events[i].intensity)) {
105 MISC_HILOGE("Write intensity failed");
106 return false;
107 }
108 if (!parcel.WriteInt32(events[i].frequency)) {
109 MISC_HILOGE("Write frequency failed");
110 return false;
111 }
112 if (!parcel.WriteInt32(events[i].index)) {
113 MISC_HILOGE("Write index failed");
114 return false;
115 }
116 if (!parcel.WriteInt32(static_cast<int32_t>(events[i].points.size()))) {
117 MISC_HILOGE("Write points's size failed");
118 return false;
119 }
120 for (size_t j = 0; j < events[i].points.size(); ++j) {
121 if (!parcel.WriteInt32(events[i].points[j].time)) {
122 MISC_HILOGE("Write points's time failed");
123 return false;
124 }
125 if (!parcel.WriteInt32(events[i].points[j].intensity)) {
126 MISC_HILOGE("Write points's intensity failed");
127 return false;
128 }
129 if (!parcel.WriteInt32(events[i].points[j].frequency)) {
130 MISC_HILOGE("Write points's frequency failed");
131 return false;
132 }
133 }
134 }
135 return true;
136 }
137
Unmarshalling(Parcel & data)138 std::optional<VibratePattern> VibratePattern::Unmarshalling(Parcel &data)
139 {
140 VibratePattern pattern;
141 if (!(data.ReadInt32(pattern.startTime))) {
142 MISC_HILOGE("Read time failed");
143 return std::nullopt;
144 }
145 int32_t eventSize { 0 };
146 if (!(data.ReadInt32(eventSize))) {
147 MISC_HILOGE("Read eventSize failed");
148 return std::nullopt;
149 }
150 for (int32_t i = 0; i < eventSize; ++i) {
151 VibrateEvent event;
152 int32_t tag { -1 };
153 if (!data.ReadInt32(tag)) {
154 MISC_HILOGE("Read type failed");
155 return std::nullopt;
156 }
157 event.tag = static_cast<VibrateTag>(tag);
158 if (!data.ReadInt32(event.time)) {
159 MISC_HILOGE("Read events's time failed");
160 return std::nullopt;
161 }
162 if (!data.ReadInt32(event.duration)) {
163 MISC_HILOGE("Read duration failed");
164 return std::nullopt;
165 }
166 if (!data.ReadInt32(event.intensity)) {
167 MISC_HILOGE("Read intensity failed");
168 return std::nullopt;
169 }
170 if (!data.ReadInt32(event.frequency)) {
171 MISC_HILOGE("Read frequency failed");
172 return std::nullopt;
173 }
174 if (!data.ReadInt32(event.index)) {
175 MISC_HILOGE("Read index failed");
176 return std::nullopt;
177 }
178 int32_t pointSize { 0 };
179 if (!data.ReadInt32(pointSize)) {
180 MISC_HILOGE("Read pointSize failed");
181 return std::nullopt;
182 }
183 pattern.events.emplace_back(event);
184 for (int32_t j = 0; j < pointSize; ++j) {
185 VibrateCurvePoint point;
186 if (!data.ReadInt32(point.time)) {
187 MISC_HILOGE("Read points's time failed");
188 return std::nullopt;
189 }
190 if (!data.ReadInt32(point.intensity)) {
191 MISC_HILOGE("Read points's intensity failed");
192 return std::nullopt;
193 }
194 if (!data.ReadInt32(point.frequency)) {
195 MISC_HILOGE("Read points's frequency failed");
196 return std::nullopt;
197 }
198 pattern.events[i].points.emplace_back(point);
199 }
200 }
201 return pattern;
202 }
203
Dump() const204 void VibrateParameter::Dump() const
205 {
206 MISC_HILOGI("intensity:%{public}d, frequency:%{public}d", intensity, frequency);
207 }
208
Marshalling(Parcel & parcel) const209 bool VibrateParameter::Marshalling(Parcel &parcel) const
210 {
211 if (!parcel.WriteInt32(intensity)) {
212 MISC_HILOGE("Write parameter's intensity failed");
213 return false;
214 }
215 if (!parcel.WriteInt32(frequency)) {
216 MISC_HILOGE("Write parameter's frequency failed");
217 return false;
218 }
219 return true;
220 }
221
Unmarshalling(Parcel & data)222 std::optional<VibrateParameter> VibrateParameter::Unmarshalling(Parcel &data)
223 {
224 VibrateParameter parameter;
225 if (!(data.ReadInt32(parameter.intensity))) {
226 MISC_HILOGE("Read parameter's intensity failed");
227 return std::nullopt;
228 }
229 if (!(data.ReadInt32(parameter.frequency))) {
230 MISC_HILOGE("Read parameter's frequency failed");
231 return std::nullopt;
232 }
233 return parameter;
234 }
235 } // namespace Sensors
236 } // namespace OHOS
237