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