1 /*
2 * Copyright (C) 2023-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 * Description: supply a helper to write/read common cast engine structures through ipc
15 * Author: zhangge
16 * Create: 2022-06-15
17 */
18
19 #include "cast_engine_common_helper.h"
20
21 #include <optional>
22 #include "cast_engine_log.h"
23 #include "securec.h"
24
25 namespace OHOS {
26 namespace CastEngine {
27 DEFINE_CAST_ENGINE_LABEL("Cast-Engine-helper");
28 constexpr uint32_t MAX_CYCLES_NUM = 10;
29
30 namespace {
31 constexpr int SESSION_KEY_LENGTH = 16;
WriteVideoSize(Parcel & parcel,const VideoSize & videoSize)32 bool WriteVideoSize(Parcel &parcel, const VideoSize &videoSize)
33 {
34 return parcel.WriteInt32(videoSize.width) && parcel.WriteInt32(videoSize.height);
35 }
36
ReadVideoSize(Parcel & parcel)37 const VideoSize ReadVideoSize(Parcel &parcel)
38 {
39 return { static_cast<uint32_t>(parcel.ReadInt32()), static_cast<uint32_t>(parcel.ReadInt32()) };
40 }
41
WriteWindowProperty(Parcel & parcel,const WindowProperty & property)42 bool WriteWindowProperty(Parcel &parcel, const WindowProperty &property)
43 {
44 return parcel.WriteInt32(property.startX) && parcel.WriteInt32(property.startY) &&
45 parcel.WriteInt32(property.width) && parcel.WriteInt32(property.height);
46 }
47
ReadWindowProperty(Parcel & parcel)48 const WindowProperty ReadWindowProperty(Parcel &parcel)
49 {
50 WindowProperty property;
51 property.startX = static_cast<uint32_t>(parcel.ReadInt32());
52 property.startY = static_cast<uint32_t>(parcel.ReadInt32());
53 property.width = static_cast<uint32_t>(parcel.ReadInt32());
54 property.height = static_cast<uint32_t>(parcel.ReadInt32());
55
56 return property;
57 }
58
WriteVideoProperty(Parcel & parcel,const VideoProperty & property)59 bool WriteVideoProperty(Parcel &parcel, const VideoProperty &property)
60 {
61 return parcel.WriteInt32(static_cast<int32_t>(property.videoWidth)) &&
62 parcel.WriteInt32(static_cast<int32_t>(property.videoHeight)) &&
63 parcel.WriteInt32(static_cast<int32_t>(property.fps)) &&
64 parcel.WriteInt32(static_cast<int32_t>(property.codecType)) &&
65 parcel.WriteInt32(static_cast<int32_t>(property.gop)) &&
66 parcel.WriteInt32(static_cast<int32_t>(property.bitrate)) &&
67 parcel.WriteInt32(static_cast<int32_t>(property.minBitrate)) &&
68 parcel.WriteInt32(static_cast<int32_t>(property.maxBitrate)) &&
69 parcel.WriteInt32(static_cast<int32_t>(property.dpi)) &&
70 parcel.WriteInt32(static_cast<int32_t>(property.colorStandard)) &&
71 parcel.WriteInt32(static_cast<int32_t>(property.screenWidth)) &&
72 parcel.WriteInt32(static_cast<int32_t>(property.screenHeight)) &&
73 parcel.WriteInt32(static_cast<int32_t>(property.profile)) &&
74 parcel.WriteInt32(static_cast<int32_t>(property.level));
75 }
76
ReadVideoProperty(Parcel & parcel)77 std::optional<VideoProperty> ReadVideoProperty(Parcel &parcel)
78 {
79 VideoProperty property;
80 property.videoWidth = static_cast<uint32_t>(parcel.ReadInt32());
81 property.videoHeight = static_cast<uint32_t>(parcel.ReadInt32());
82 property.fps = static_cast<uint32_t>(parcel.ReadInt32());
83 auto codecType = parcel.ReadInt32();
84 property.gop = parcel.ReadInt32();
85 property.bitrate = static_cast<uint32_t>(parcel.ReadInt32());
86 property.minBitrate = static_cast<uint32_t>(parcel.ReadInt32());
87 property.maxBitrate = static_cast<uint32_t>(parcel.ReadInt32());
88 property.dpi = static_cast<uint32_t>(parcel.ReadInt32());
89 auto colorStandard = parcel.ReadInt32();
90 property.screenWidth = static_cast<uint32_t>(parcel.ReadInt32());
91 property.screenHeight = static_cast<uint32_t>(parcel.ReadInt32());
92 property.profile = static_cast<uint32_t>(parcel.ReadInt32());
93 property.level = static_cast<uint32_t>(parcel.ReadInt32());
94
95 if (!IsColorStandard(colorStandard) || !IsVideoCodecType(codecType)) {
96 return std::nullopt;
97 }
98
99 property.codecType = static_cast<VideoCodecType>(codecType);
100 property.colorStandard = static_cast<ColorStandard>(colorStandard);
101 return property;
102 }
103
104
WriteAudioProperty(Parcel & parcel,const AudioProperty & property)105 bool WriteAudioProperty(Parcel &parcel, const AudioProperty &property)
106 {
107 return parcel.WriteInt32(static_cast<int32_t>(property.sampleRate)) &&
108 parcel.WriteInt32(static_cast<int32_t>(property.sampleBitWidth)) &&
109 parcel.WriteInt32(static_cast<int32_t>(property.channelConfig)) &&
110 parcel.WriteInt32(static_cast<int32_t>(property.bitrate)) &&
111 parcel.WriteInt32(static_cast<int32_t>(property.codec));
112 }
113
ReadAudioProperty(Parcel & parcel)114 const AudioProperty ReadAudioProperty(Parcel &parcel)
115 {
116 AudioProperty property;
117 property.sampleRate = static_cast<uint32_t>(parcel.ReadInt32());
118 property.sampleBitWidth = static_cast<uint8_t>(parcel.ReadInt32());
119 property.channelConfig = static_cast<uint32_t>(parcel.ReadInt32());
120 property.bitrate = static_cast<uint32_t>(parcel.ReadInt32());
121 property.codec = static_cast<uint32_t>(parcel.ReadInt32());
122 return property;
123 }
124
GetLocalFd(const std::string & url)125 int GetLocalFd(const std::string &url)
126 {
127 if (url.empty()) {
128 CLOGE("url is empty.");
129 return INVALID_VALUE;
130 }
131 char *nextPtr = nullptr;
132 int fd = static_cast<int>(std::strtol(url.c_str(), &nextPtr, DECIMALISM));
133 if (errno == ERANGE || *nextPtr != '\0') {
134 return INVALID_VALUE;
135 }
136 return fd;
137 }
138 } // namespace
139
WriteCastRemoteDevice(Parcel & parcel,const CastRemoteDevice & device)140 bool WriteCastRemoteDevice(Parcel &parcel, const CastRemoteDevice &device)
141 {
142 bool res = parcel.WriteInt32(static_cast<int32_t>(device.deviceType)) &&
143 parcel.WriteInt32(static_cast<int32_t>(device.capability)) &&
144 parcel.WriteInt32(static_cast<int32_t>(device.subDeviceType)) &&
145 parcel.WriteInt32(static_cast<int32_t>(device.channelType)) && parcel.WriteString(device.deviceId) &&
146 parcel.WriteString(device.deviceName) && parcel.WriteString(device.ipAddress) &&
147 parcel.WriteString(device.networkId) && parcel.WriteString(device.localIpAddress) &&
148 parcel.WriteBool(device.isLeagacy) && parcel.WriteInt32(device.sessionId) &&
149 parcel.WriteUint32(device.mediumTypes) && parcel.WriteUint32(device.protocolCapabilities) &&
150 parcel.WriteString(device.modelName) && parcel.WriteString(device.manufacturerName) &&
151 parcel.WriteBool(device.isTrushed);
152 if (device.sessionKeyLength == SESSION_KEY_LENGTH && device.sessionKey) {
153 res = res && parcel.WriteUint32(device.sessionKeyLength);
154 res = res && parcel.WriteBuffer(device.sessionKey, device.sessionKeyLength);
155 } else {
156 parcel.WriteUint32(0);
157 }
158 uint32_t drmCapabilitySize = device.drmCapabilities.size();
159 res = res && parcel.WriteUint32(drmCapabilitySize);
160 for (auto iter = device.drmCapabilities.begin(); iter != device.drmCapabilities.end(); iter++) {
161 res = res && parcel.WriteString(*iter);
162 }
163 return res;
164 }
165
ReadCastRemoteDevice(Parcel & parcel,CastRemoteDevice & device)166 bool ReadCastRemoteDevice(Parcel &parcel, CastRemoteDevice &device)
167 {
168 auto remote = ReadCastRemoteDevice(parcel);
169 if (remote == nullptr) {
170 CLOGE("ReadCastRemoteDevice failed");
171 return false;
172 }
173
174 device = *remote;
175 return true;
176 }
177
ReadCastRemoteDevice(Parcel & parcel)178 std::unique_ptr<CastRemoteDevice> ReadCastRemoteDevice(Parcel &parcel)
179 {
180 auto device = std::make_unique<CastRemoteDevice>();
181 auto deviceType = parcel.ReadInt32();
182 auto capability = parcel.ReadInt32();
183 auto subDeviceType = parcel.ReadInt32();
184 auto channelType = parcel.ReadInt32();
185 if (!IsDeviceType(deviceType) || !IsSubDeviceType(subDeviceType) ||
186 !IsChannelType(channelType) || !IsCapabilityType(capability)) {
187 CLOGE("ReadCastRemoteDevice error");
188 return nullptr;
189 }
190 device->deviceType = static_cast<DeviceType>(deviceType);
191 device->capability = static_cast<CapabilityType>(capability);
192 device->subDeviceType = static_cast<SubDeviceType>(subDeviceType);
193 device->channelType = static_cast<ChannelType>(channelType);
194 device->deviceId = parcel.ReadString();
195 device->deviceName = parcel.ReadString();
196 device->ipAddress = parcel.ReadString();
197 device->networkId = parcel.ReadString();
198 device->localIpAddress = parcel.ReadString();
199 device->isLeagacy = parcel.ReadBool();
200 device->sessionId = parcel.ReadInt32();
201 device->mediumTypes = parcel.ReadUint32();
202 device->protocolCapabilities = parcel.ReadUint32();
203 device->modelName = parcel.ReadString();
204 device->manufacturerName = parcel.ReadString();
205 device->isTrushed = parcel.ReadBool();
206 device->sessionKeyLength = parcel.ReadUint32();
207 if (device->sessionKeyLength == SESSION_KEY_LENGTH) {
208 device->sessionKey = parcel.ReadBuffer(static_cast<size_t>(device->sessionKeyLength));
209 } else {
210 device->sessionKeyLength = 0;
211 device->sessionKey = nullptr;
212 }
213 uint32_t drmCapabilitySize = parcel.ReadUint32();
214 for (uint32_t i = 0; (i < drmCapabilitySize) && (i < MAX_CYCLES_NUM); i++) {
215 device->drmCapabilities.push_back(parcel.ReadString());
216 }
217
218 return device;
219 }
220
WriteStreamCapability(MessageParcel & parcel,const StreamCapability & streamCapability)221 bool WriteStreamCapability(MessageParcel &parcel, const StreamCapability &streamCapability)
222 {
223 return parcel.WriteBool(streamCapability.isPlaySupported) &&
224 parcel.WriteBool(streamCapability.isPauseSupported) &&
225 parcel.WriteBool(streamCapability.isStopSupported) &&
226 parcel.WriteBool(streamCapability.isNextSupported) &&
227 parcel.WriteBool(streamCapability.isPreviousSupported) &&
228 parcel.WriteBool(streamCapability.isSeekSupported) &&
229 parcel.WriteBool(streamCapability.isFastForwardSupported) &&
230 parcel.WriteBool(streamCapability.isFastRewindSupported) &&
231 parcel.WriteBool(streamCapability.isLoopModeSupported) &&
232 parcel.WriteBool(streamCapability.isToggleFavoriteSupported) &&
233 parcel.WriteBool(streamCapability.isSetVolumeSupported);
234 }
235
ReadStreamCapability(MessageParcel & parcel)236 StreamCapability ReadStreamCapability(MessageParcel &parcel)
237 {
238 StreamCapability streamCapability;
239 streamCapability.isPlaySupported = parcel.ReadBool();
240 streamCapability.isPauseSupported = parcel.ReadBool();
241 streamCapability.isStopSupported = parcel.ReadBool();
242 streamCapability.isNextSupported = parcel.ReadBool();
243 streamCapability.isPreviousSupported = parcel.ReadBool();
244 streamCapability.isSeekSupported = parcel.ReadBool();
245 streamCapability.isFastForwardSupported = parcel.ReadBool();
246 streamCapability.isFastRewindSupported = parcel.ReadBool();
247 streamCapability.isLoopModeSupported = parcel.ReadBool();
248 streamCapability.isToggleFavoriteSupported = parcel.ReadBool();
249 streamCapability.isSetVolumeSupported = parcel.ReadBool();
250 return streamCapability;
251 }
252
WriteMediaInfo(MessageParcel & parcel,const MediaInfo & mediaInfo)253 bool WriteMediaInfo(MessageParcel &parcel, const MediaInfo &mediaInfo)
254 {
255 if (mediaInfo.mediaUrl.empty()) {
256 CLOGE("mediaUrl is empty");
257 return false;
258 }
259 int fd = GetLocalFd(mediaInfo.mediaUrl);
260 if (fd != INVALID_VALUE) {
261 if (!parcel.WriteString("localFd") || !parcel.WriteFileDescriptor(fd)) {
262 CLOGE("Write local fd failed, fd = %{public}d", fd);
263 return false;
264 }
265 } else if (!parcel.WriteString("path") || !parcel.WriteString(mediaInfo.mediaUrl)) {
266 CLOGE("Write path failed");
267 return false;
268 }
269 return parcel.WriteString(mediaInfo.mediaId) && parcel.WriteString(mediaInfo.mediaName) &&
270 parcel.WriteString(mediaInfo.mediaType) && parcel.WriteUint32(mediaInfo.mediaSize) &&
271 parcel.WriteString(mediaInfo.albumCoverUrl) && parcel.WriteString(mediaInfo.albumTitle) &&
272 parcel.WriteString(mediaInfo.mediaArtist) && parcel.WriteString(mediaInfo.lrcUrl) &&
273 parcel.WriteString(mediaInfo.lrcContent) && parcel.WriteString(mediaInfo.appIconUrl) &&
274 parcel.WriteString(mediaInfo.appName) && parcel.WriteUint32(mediaInfo.startPosition) &&
275 parcel.WriteUint32(mediaInfo.duration) && parcel.WriteUint32(mediaInfo.closingCreditsPosition) &&
276 parcel.WriteString(mediaInfo.drmType);
277 }
278
ReadMediaInfo(MessageParcel & parcel)279 std::unique_ptr<MediaInfo> ReadMediaInfo(MessageParcel &parcel)
280 {
281 auto mediaInfo = std::make_unique<MediaInfo>();
282 if (mediaInfo == nullptr) {
283 CLOGE("Failed to malloc mediaInfo");
284 return nullptr;
285 }
286 std::string urlType = parcel.ReadString();
287 if (urlType == "localFd") {
288 CLOGD("localFd");
289 mediaInfo->mediaUrl = std::to_string(parcel.ReadFileDescriptor());
290 } else {
291 CLOGD("online or localPath");
292 mediaInfo->mediaUrl = parcel.ReadString();
293 }
294 mediaInfo->mediaId = parcel.ReadString();
295 mediaInfo->mediaName = parcel.ReadString();
296 mediaInfo->mediaType = parcel.ReadString();
297 mediaInfo->mediaSize = parcel.ReadUint32();
298 mediaInfo->albumCoverUrl = parcel.ReadString();
299 mediaInfo->albumTitle = parcel.ReadString();
300 mediaInfo->mediaArtist = parcel.ReadString();
301 mediaInfo->lrcUrl = parcel.ReadString();
302 mediaInfo->lrcContent = parcel.ReadString();
303 mediaInfo->appIconUrl = parcel.ReadString();
304 mediaInfo->appName = parcel.ReadString();
305 mediaInfo->startPosition = parcel.ReadUint32();
306 mediaInfo->duration = parcel.ReadUint32();
307 mediaInfo->closingCreditsPosition = parcel.ReadUint32();
308 mediaInfo->drmType = parcel.ReadString();
309
310 return mediaInfo;
311 }
312
WriteMediaInfoHolder(MessageParcel & parcel,const MediaInfoHolder & mediaInfoHolder)313 bool WriteMediaInfoHolder(MessageParcel &parcel, const MediaInfoHolder &mediaInfoHolder)
314 {
315 bool ret = parcel.WriteUint32(mediaInfoHolder.currentIndex);
316 ret = ret && parcel.WriteUint32(mediaInfoHolder.progressRefreshInterval);
317 ret = ret && parcel.WriteUint32(static_cast<uint32_t>(mediaInfoHolder.mediaInfoList.size()));
318 for (auto iter = mediaInfoHolder.mediaInfoList.begin(); iter != mediaInfoHolder.mediaInfoList.end(); iter++) {
319 ret = ret && WriteMediaInfo(parcel, *iter);
320 }
321 return ret;
322 }
323
ReadMediaInfoHolder(MessageParcel & parcel)324 std::unique_ptr<MediaInfoHolder> ReadMediaInfoHolder(MessageParcel &parcel)
325 {
326 auto mediaInfoHolder = std::make_unique<MediaInfoHolder>();
327 if (mediaInfoHolder == nullptr) {
328 CLOGE("Failed to malloc mediaInfoHolder");
329 return nullptr;
330 }
331 mediaInfoHolder->currentIndex = parcel.ReadUint32();
332 mediaInfoHolder->progressRefreshInterval = parcel.ReadUint32();
333 uint32_t infoListSize = parcel.ReadUint32();
334 if (infoListSize > MAX_FILE_NUM) {
335 CLOGE("The number of list exceeds the upper limit. infoListSize: %{public}u", infoListSize);
336 return nullptr;
337 }
338 for (uint32_t i = 0; i < infoListSize; i++) {
339 auto mediaInfo = ReadMediaInfo(parcel);
340 if (mediaInfo == nullptr) {
341 return nullptr;
342 }
343 mediaInfoHolder->mediaInfoList.push_back(*mediaInfo);
344 }
345 return mediaInfoHolder;
346 }
347
WriteCastLocalDevice(Parcel & parcel,const CastLocalDevice & device)348 bool WriteCastLocalDevice(Parcel &parcel, const CastLocalDevice &device)
349 {
350 return parcel.WriteString(device.deviceId) && parcel.WriteString(device.deviceName) &&
351 parcel.WriteInt32(static_cast<int32_t>(device.deviceType)) &&
352 parcel.WriteInt32(static_cast<int32_t>(device.subDeviceType)) && parcel.WriteString(device.ipAddress) &&
353 parcel.WriteInt32(static_cast<int32_t>(device.triggerType)) && parcel.WriteString(device.authData);
354 }
355
ReadCastLocalDevice(Parcel & parcel)356 std::unique_ptr<CastLocalDevice> ReadCastLocalDevice(Parcel &parcel)
357 {
358 auto device = std::make_unique<CastLocalDevice>();
359 device->deviceId = parcel.ReadString();
360 device->deviceName = parcel.ReadString();
361 auto deviceType = parcel.ReadInt32();
362 auto subDeviceType = parcel.ReadInt32();
363 device->ipAddress = parcel.ReadString();
364 auto triggerType = parcel.ReadInt32();
365 device->authData = parcel.ReadString();
366
367 if (!IsDeviceType(deviceType) || !IsSubDeviceType(subDeviceType) || !IsTriggerType(triggerType)) {
368 CLOGE("ReadCastLocalDevice error");
369 return nullptr;
370 }
371 device->deviceType = static_cast<DeviceType>(deviceType);
372 device->subDeviceType = static_cast<SubDeviceType>(subDeviceType);
373 device->triggerType = static_cast<TriggerType>(triggerType);
374
375 return device;
376 }
377
WriteCastSessionProperty(Parcel & parcel,const CastSessionProperty & property)378 bool WriteCastSessionProperty(Parcel &parcel, const CastSessionProperty &property)
379 {
380 return parcel.WriteInt32(static_cast<int32_t>(property.protocolType)) &&
381 parcel.WriteInt32(static_cast<int32_t>(property.endType)) &&
382 WriteAudioProperty(parcel, property.audioProperty) && WriteVideoProperty(parcel, property.videoProperty) &&
383 WriteWindowProperty(parcel, property.windowProperty);
384 }
385
ReadCastSessionProperty(Parcel & parcel)386 std::unique_ptr<CastSessionProperty> ReadCastSessionProperty(Parcel &parcel)
387 {
388 auto property = std::make_unique<CastSessionProperty>();
389 if (property == nullptr) {
390 CLOGE("Failed to malloc cast session property");
391 return nullptr;
392 }
393 auto protocolType = parcel.ReadInt32();
394 if (!IsProtocolType(protocolType)) {
395 return nullptr;
396 }
397 auto endType = parcel.ReadInt32();
398 if (!IsEndType(endType)) {
399 return nullptr;
400 }
401
402 property->protocolType = static_cast<ProtocolType>(protocolType);
403 property->endType = static_cast<EndType>(endType);
404 property->audioProperty = ReadAudioProperty(parcel);
405 auto videoProperty = ReadVideoProperty(parcel);
406 if (videoProperty == std::nullopt) {
407 return nullptr;
408 }
409 property->videoProperty = videoProperty.value();
410 property->windowProperty = ReadWindowProperty(parcel);
411 return property;
412 }
413
WritePropertyContainer(Parcel & parcel,const PropertyContainer & container)414 bool WritePropertyContainer(Parcel &parcel, const PropertyContainer &container)
415 {
416 return parcel.WriteInt32(static_cast<int32_t>(container.type)) &&
417 (((container.type == PropertyType::VIDEO_SIZE) && WriteVideoSize(parcel, container.videoSize)) ||
418 ((container.type == PropertyType::VIDEO_FPS) && parcel.WriteInt32(container.videoFps)) ||
419 ((container.type == PropertyType::WINDOW_SIZE) && WriteWindowProperty(parcel, container.windowProperty)));
420 }
421
ReadPropertyContainer(Parcel & parcel)422 std::unique_ptr<PropertyContainer> ReadPropertyContainer(Parcel &parcel)
423 {
424 auto container = std::make_unique<PropertyContainer>();
425 if (container == nullptr) {
426 CLOGE("Failed to malloc property container");
427 return nullptr;
428 }
429 int32_t type = parcel.ReadInt32();
430 if (!IsPropertyType(type)) {
431 return nullptr;
432 }
433 container->type = static_cast<PropertyType>(type);
434 if (container->type == PropertyType::VIDEO_SIZE) {
435 container->videoSize = ReadVideoSize(parcel);
436 } else if (container->type == PropertyType::VIDEO_FPS) {
437 container->videoFps = static_cast<unsigned int>(parcel.ReadInt32());
438 } else {
439 container->windowProperty = ReadWindowProperty(parcel);
440 }
441
442 return container;
443 }
444
WriteAuthInfo(Parcel & parcel,const AuthInfo & authInfo)445 bool WriteAuthInfo(Parcel &parcel, const AuthInfo &authInfo)
446 {
447 return parcel.WriteInt32(static_cast<int32_t>(authInfo.authMode)) &&
448 parcel.WriteInt32(static_cast<int32_t>(authInfo.authCode)) && parcel.WriteString(authInfo.deviceId);
449 }
450
ReadAuthInfo(Parcel & parcel)451 std::unique_ptr<AuthInfo> ReadAuthInfo(Parcel &parcel)
452 {
453 auto authInfo = std::make_unique<AuthInfo>();
454 if (authInfo == nullptr) {
455 CLOGE("Failed to malloc auth info");
456 return nullptr;
457 }
458
459 authInfo->authMode = static_cast<int>(parcel.ReadInt32());
460 authInfo->authCode = static_cast<uint32_t>(parcel.ReadInt32());
461 authInfo->deviceId = parcel.ReadString();
462 return authInfo;
463 }
464
WriteTouchPoint(Parcel & parcel,const OHNativeXcomponentTouchPoint & touchPoint)465 bool WriteTouchPoint(Parcel &parcel, const OHNativeXcomponentTouchPoint &touchPoint)
466 {
467 return parcel.WriteInt32(touchPoint.id) && parcel.WriteFloat(touchPoint.screenX) &&
468 parcel.WriteFloat(touchPoint.screenY) && parcel.WriteFloat(touchPoint.x) && parcel.WriteFloat(touchPoint.y) &&
469 parcel.WriteUint32(static_cast<uint32_t>(touchPoint.type)) && parcel.WriteDouble(touchPoint.size) &&
470 parcel.WriteFloat(touchPoint.force) && parcel.WriteInt64(touchPoint.timeStamp) &&
471 parcel.WriteBool(touchPoint.isPressed);
472 }
473
WriteTouchPoints(Parcel & parcel,const OHNativeXcomponentTouchEvent touchEvent)474 bool WriteTouchPoints(Parcel &parcel, const OHNativeXcomponentTouchEvent touchEvent)
475 {
476 for (uint32_t i = 0; i < touchEvent.numPoints; i++) {
477 if (!WriteTouchPoint(parcel, touchEvent.touchPoints[i])) {
478 return false;
479 }
480 }
481 return true;
482 }
483
WriteTouchEvent(Parcel & parcel,const OHNativeXcomponentTouchEvent & touchEvent)484 bool WriteTouchEvent(Parcel &parcel, const OHNativeXcomponentTouchEvent &touchEvent)
485 {
486 return parcel.WriteInt32(touchEvent.id) && parcel.WriteFloat(touchEvent.screenX) &&
487 parcel.WriteFloat(touchEvent.screenY) && parcel.WriteFloat(touchEvent.x) && parcel.WriteFloat(touchEvent.y) &&
488 parcel.WriteUint32(static_cast<uint32_t>(touchEvent.type)) && parcel.WriteDouble(touchEvent.size) &&
489 parcel.WriteFloat(touchEvent.force) && parcel.WriteInt64(touchEvent.deviceId) &&
490 parcel.WriteInt64(touchEvent.timeStamp) && parcel.WriteUint32(touchEvent.numPoints) &&
491 WriteTouchPoints(parcel, touchEvent);
492 }
493
494
WriteMouseEvent(Parcel & parcel,const OHNativeXcomponentMouseEvent & mouseEvent)495 bool WriteMouseEvent(Parcel &parcel, const OHNativeXcomponentMouseEvent &mouseEvent)
496 {
497 return parcel.WriteFloat(mouseEvent.x) && parcel.WriteFloat(mouseEvent.y) &&
498 parcel.WriteFloat(mouseEvent.screenX) && parcel.WriteFloat(mouseEvent.screenY) &&
499 parcel.WriteInt64(mouseEvent.timestamp) && parcel.WriteUint32(static_cast<uint32_t>(mouseEvent.action)) &&
500 parcel.WriteUint32(static_cast<uint32_t>(mouseEvent.button));
501 }
502
WriteWhellEvent(Parcel & parcel,const OHNativeXcomponentWheelEvent & wheelEvent)503 bool WriteWhellEvent(Parcel &parcel, const OHNativeXcomponentWheelEvent &wheelEvent)
504 {
505 return parcel.WriteUint32(static_cast<uint32_t>(wheelEvent.direction)) &&
506 parcel.WriteUint8(wheelEvent.indication) && parcel.WriteUint8(wheelEvent.scrollUnit) &&
507 parcel.WriteUint16(wheelEvent.wheelDis) && parcel.WriteFloat(wheelEvent.x) && parcel.WriteFloat(wheelEvent.y);
508 }
509
WriteKeyEvent(Parcel & parcel,const OHNativeXcomponentKeyEvent & keyEvent)510 bool WriteKeyEvent(Parcel &parcel, const OHNativeXcomponentKeyEvent &keyEvent)
511 {
512 return parcel.WriteUint8(keyEvent.reserved) && parcel.WriteUint16(keyEvent.keyCode1) &&
513 parcel.WriteUint16(keyEvent.keyCode2) && parcel.WriteUint32(keyEvent.metaState) &&
514 parcel.WriteUint32(static_cast<uint32_t>(keyEvent.type));
515 }
516
WriteContentEvent(Parcel & parcel,const OHNativeXcomponentContentEvent & contentEvent)517 bool WriteContentEvent(Parcel &parcel, const OHNativeXcomponentContentEvent &contentEvent)
518 {
519 return parcel.WriteUint16(contentEvent.msgLen) && parcel.WriteBuffer(contentEvent.inputText, contentEvent.msgLen);
520 }
521
WriteFocusEvent(Parcel & parcel,const OHNativeXcomponentFocusEvent & focusEvent)522 bool WriteFocusEvent(Parcel &parcel, const OHNativeXcomponentFocusEvent &focusEvent)
523 {
524 return parcel.WriteUint8(focusEvent.focusStat) && parcel.WriteDouble(focusEvent.cursorX1) &&
525 parcel.WriteDouble(focusEvent.cursorY1) && parcel.WriteDouble(focusEvent.cursorX2) &&
526 parcel.WriteDouble(focusEvent.cursorY2);
527 }
528
WriteInputMethodEvent(Parcel & parcel,const OHNativeXcomponentInputMethodEvent & inputMethodEvent)529 bool WriteInputMethodEvent(Parcel &parcel, const OHNativeXcomponentInputMethodEvent &inputMethodEvent)
530 {
531 return parcel.WriteUint32(static_cast<uint32_t>(inputMethodEvent.type)) &&
532 (((inputMethodEvent.type == OHNativeXcomponentInputMethodEventType::OH_NATIVEXCOMPONENT_INPUT_CONTENT) &&
533 WriteContentEvent(parcel, inputMethodEvent.contentEvent)) ||
534 ((inputMethodEvent.type == OHNativeXcomponentInputMethodEventType::OH_NATIVEXCOMPONENT_INPUT_FOCUS) &&
535 WriteFocusEvent(parcel, inputMethodEvent.focusEvent)));
536 }
537
WriteVirtualKeyEvent(Parcel & parcel,const OHNativeXcomponentVirtualKeyEvent & virtualKeyEvent)538 bool WriteVirtualKeyEvent(Parcel &parcel, const OHNativeXcomponentVirtualKeyEvent &virtualKeyEvent)
539 {
540 return parcel.WriteInt32(static_cast<int32_t>(virtualKeyEvent.type)) && parcel.WriteFloat(virtualKeyEvent.x) &&
541 parcel.WriteFloat(virtualKeyEvent.y);
542 }
543
WriteRemoteControlEvent(Parcel & parcel,const OHRemoteControlEvent & event)544 bool WriteRemoteControlEvent(Parcel &parcel, const OHRemoteControlEvent &event)
545 {
546 return parcel.WriteInt32(static_cast<int32_t>(event.eventType)) &&
547 (((event.eventType == XcomponentEventType::REMOTECONTROL_TOUCH) && WriteTouchEvent(parcel, event.touchEvent)) ||
548 ((event.eventType == XcomponentEventType::REMOTECONTROL_MOUSE) && WriteMouseEvent(parcel, event.mouseEvent)) ||
549 ((event.eventType == XcomponentEventType::REMOTECONTROL_WHEEL) && WriteWhellEvent(parcel, event.wheelEvent)) ||
550 ((event.eventType == XcomponentEventType::REMOTECONTROL_KEY) && WriteKeyEvent(parcel, event.keyEvent)) ||
551 ((event.eventType == XcomponentEventType::REMOTECONTROL_INPUT_METHOD) &&
552 WriteInputMethodEvent(parcel, event.inputMethodEvent)) ||
553 ((event.eventType == XcomponentEventType::REMOTECONTROL_VIRTUAL_KEY) &&
554 WriteVirtualKeyEvent(parcel, event.virtualKeyEvent)));
555 }
556
ReadTouchPoint(Parcel & parcel)557 const OHNativeXcomponentTouchPoint ReadTouchPoint(Parcel &parcel)
558 {
559 return { parcel.ReadInt32(), parcel.ReadFloat(),
560 parcel.ReadFloat(), parcel.ReadFloat(),
561 parcel.ReadFloat(), static_cast<OHNativeXcomponentTouchEventType>(parcel.ReadUint32()),
562 parcel.ReadDouble(), parcel.ReadFloat(),
563 parcel.ReadInt64(), parcel.ReadBool() };
564 }
565
ReadTouchPoints(Parcel & parcel,uint32_t numPoints,OHNativeXcomponentTouchPoint points[])566 void ReadTouchPoints(Parcel &parcel, uint32_t numPoints, OHNativeXcomponentTouchPoint points[])
567 {
568 if (numPoints > OH_MAX_TOUCH_POINTS_NUMBER) {
569 CLOGE("numPoints is invalid, numPoints:%{public}d", numPoints);
570 return;
571 }
572 for (uint32_t i = 0; i < numPoints; i++) {
573 points[i] = ReadTouchPoint(parcel);
574 }
575 }
576
ReadTouchEvent(Parcel & parcel,OHNativeXcomponentTouchEvent & touchEvent)577 void ReadTouchEvent(Parcel &parcel, OHNativeXcomponentTouchEvent &touchEvent)
578 {
579 touchEvent.id = parcel.ReadInt32();
580 touchEvent.screenX = parcel.ReadFloat();
581 touchEvent.screenY = parcel.ReadFloat();
582 touchEvent.x = parcel.ReadFloat();
583 touchEvent.y = parcel.ReadFloat();
584 touchEvent.type = static_cast<OHNativeXcomponentTouchEventType>(parcel.ReadUint32());
585 touchEvent.size = parcel.ReadDouble();
586 touchEvent.force = parcel.ReadFloat();
587 touchEvent.deviceId = parcel.ReadInt64();
588 touchEvent.timeStamp = parcel.ReadInt64();
589 touchEvent.numPoints = parcel.ReadUint32();
590 ReadTouchPoints(parcel, touchEvent.numPoints, touchEvent.touchPoints);
591 }
592
ReadMouseEvent(Parcel & parcel)593 const OHNativeXcomponentMouseEvent ReadMouseEvent(Parcel &parcel)
594 {
595 return { parcel.ReadFloat(),
596 parcel.ReadFloat(),
597 parcel.ReadFloat(),
598 parcel.ReadFloat(),
599 parcel.ReadInt64(),
600 static_cast<OHNativeXcomponentMouseEventAction>(parcel.ReadUint32()),
601 static_cast<OHNativeXcomponentMouseEventButton>(parcel.ReadUint32()) };
602 }
603
ReadWhellEvent(Parcel & parcel)604 const OHNativeXcomponentWheelEvent ReadWhellEvent(Parcel &parcel)
605 {
606 return { static_cast<OHNativeXcomponentWheelEventDirection>(parcel.ReadUint32()),
607 parcel.ReadUint8(),
608 parcel.ReadUint8(),
609 parcel.ReadUint16(),
610 parcel.ReadFloat(),
611 parcel.ReadFloat() };
612 }
613
ReadKeyEvent(Parcel & parcel)614 const OHNativeXcomponentKeyEvent ReadKeyEvent(Parcel &parcel)
615 {
616 return { parcel.ReadUint8(), parcel.ReadUint16(), parcel.ReadUint16(), parcel.ReadUint32(),
617 static_cast<OHNativeXcomponentKeyEventType>(parcel.ReadUint32()) };
618 }
619
ReadContentEvent(Parcel & parcel,OHNativeXcomponentContentEvent & contentEvent)620 void ReadContentEvent(Parcel &parcel, OHNativeXcomponentContentEvent &contentEvent)
621 {
622 contentEvent.msgLen = parcel.ReadUint16();
623 int32_t err = memcpy_s(contentEvent.inputText,
624 OH_MAX_CONTENT_LEN, parcel.ReadBuffer(contentEvent.msgLen), contentEvent.msgLen);
625 if (err != 0) {
626 CLOGE("memcpy_s inputText failed, err = %{public}d.", err);
627 }
628 }
629
ReadFocusEvent(Parcel & parcel)630 const OHNativeXcomponentFocusEvent ReadFocusEvent(Parcel &parcel)
631 {
632 return { parcel.ReadUint8(), parcel.ReadDouble(), parcel.ReadDouble(), parcel.ReadDouble(), parcel.ReadDouble() };
633 }
634
635
ReadInputMethodEvent(Parcel & parcel,OHNativeXcomponentInputMethodEvent & inputMethodEvent)636 void ReadInputMethodEvent(Parcel &parcel, OHNativeXcomponentInputMethodEvent &inputMethodEvent)
637 {
638 inputMethodEvent.type = static_cast<OHNativeXcomponentInputMethodEventType>(parcel.ReadUint16());
639 if (inputMethodEvent.type == OHNativeXcomponentInputMethodEventType::OH_NATIVEXCOMPONENT_INPUT_CONTENT) {
640 return ReadContentEvent(parcel, inputMethodEvent.contentEvent);
641 }
642 inputMethodEvent.focusEvent = ReadFocusEvent(parcel);
643 }
644
ReadVirtualKeyEvent(Parcel & parcel)645 const OHNativeXcomponentVirtualKeyEvent ReadVirtualKeyEvent(Parcel &parcel)
646 {
647 return { static_cast<OHNativeXcomponentVirtualKeyEventType>(parcel.ReadInt32()), parcel.ReadFloat(),
648 parcel.ReadFloat() };
649 }
650
ReadRemoteControlEvent(Parcel & parcel)651 std::unique_ptr<OHRemoteControlEvent> ReadRemoteControlEvent(Parcel &parcel)
652 {
653 auto remoteControlEvent = std::make_unique<OHRemoteControlEvent>();
654 if (remoteControlEvent == nullptr) {
655 CLOGE("Failed to malloc remote control event");
656 return nullptr;
657 }
658
659 remoteControlEvent->eventType = static_cast<XcomponentEventType>(parcel.ReadUint32());
660 switch (remoteControlEvent->eventType) {
661 case XcomponentEventType::REMOTECONTROL_TOUCH:
662 ReadTouchEvent(parcel, remoteControlEvent->touchEvent);
663 break;
664 case XcomponentEventType::REMOTECONTROL_MOUSE:
665 remoteControlEvent->mouseEvent = ReadMouseEvent(parcel);
666 break;
667 case XcomponentEventType::REMOTECONTROL_WHEEL:
668 remoteControlEvent->wheelEvent = ReadWhellEvent(parcel);
669 break;
670 case XcomponentEventType::REMOTECONTROL_KEY:
671 remoteControlEvent->keyEvent = ReadKeyEvent(parcel);
672 break;
673 case XcomponentEventType::REMOTECONTROL_INPUT_METHOD:
674 ReadInputMethodEvent(parcel, remoteControlEvent->inputMethodEvent);
675 break;
676 case XcomponentEventType::REMOTECONTROL_VIRTUAL_KEY:
677 remoteControlEvent->virtualKeyEvent = ReadVirtualKeyEvent(parcel);
678 break;
679 default:
680 CLOGE("Parameter error, event:(%d) not support", static_cast<uint32_t>(remoteControlEvent->eventType));
681 remoteControlEvent = nullptr;
682 break;
683 }
684 return remoteControlEvent;
685 }
686
WriteDeviceStateInfo(Parcel & parcel,const DeviceStateInfo & stateInfo)687 bool WriteDeviceStateInfo(Parcel &parcel, const DeviceStateInfo &stateInfo)
688 {
689 return parcel.WriteInt32(static_cast<int32_t>(stateInfo.deviceState)) &&
690 parcel.WriteString(stateInfo.deviceId) &&
691 parcel.WriteInt32(static_cast<int32_t>(stateInfo.reasonCode));
692 }
693
ReadDeviceStateInfo(Parcel & parcel)694 std::unique_ptr<DeviceStateInfo> ReadDeviceStateInfo(Parcel &parcel)
695 {
696 auto stateInfo = std::make_unique<DeviceStateInfo>();
697
698 // Parse device state
699 int32_t state = parcel.ReadInt32();
700 if (!IsDeviceState(state)) {
701 CLOGE("incorrect device state");
702 return nullptr;
703 }
704 stateInfo->deviceState = static_cast<DeviceState>(state);
705
706 // Parse deviceId
707 std::string deviceId = parcel.ReadString();
708 if (deviceId.empty()) {
709 CLOGE("device id is empty");
710 return nullptr;
711 }
712 stateInfo->deviceId = deviceId;
713
714 // Parse event code
715 int32_t reasonCode = parcel.ReadInt32();
716 stateInfo->reasonCode = static_cast<ReasonCode>(reasonCode);
717
718 return stateInfo;
719 }
720
WriteEvent(Parcel & parcel,const EventId & eventId,const std::string & jsonParam)721 bool WriteEvent(Parcel &parcel, const EventId &eventId, const std::string &jsonParam)
722 {
723 return parcel.WriteInt32(static_cast<int32_t>(eventId)) && parcel.WriteString(jsonParam);
724 }
725
ReadEvent(Parcel & parcel,int32_t & eventId,std::string & jsonParam)726 bool ReadEvent(Parcel &parcel, int32_t &eventId, std::string &jsonParam)
727 {
728 eventId = parcel.ReadInt32();
729 if (!IsEventId(eventId)) {
730 CLOGE("incorrect event id");
731 return false;
732 }
733 jsonParam = parcel.ReadString();
734 return true;
735 }
736
SetDataCapacity(MessageParcel & parcel,const FileFdMap & fileList,uint32_t tokenSize)737 void SetDataCapacity(MessageParcel &parcel, const FileFdMap &fileList, uint32_t tokenSize)
738 {
739 uint32_t totalSize = tokenSize;
740 totalSize += sizeof(uint32_t);
741 for (const auto &[srcPath, fdPair] : fileList) {
742 totalSize += srcPath.length();
743 totalSize += sizeof(fdPair.first);
744 totalSize += fdPair.second.length();
745 }
746 if (totalSize > parcel.GetDataCapacity()) {
747 CLOGD("SetDataCapacity totalSize: %d", totalSize);
748 parcel.SetMaxCapacity(totalSize + totalSize);
749 parcel.SetDataCapacity(totalSize);
750 }
751 }
752
WriteFileList(MessageParcel & parcel,const FileFdMap & fileList)753 bool WriteFileList(MessageParcel &parcel, const FileFdMap &fileList)
754 {
755 bool ret = parcel.WriteUint32(fileList.size());
756 for (const auto &[srcPath, fdPair] : fileList) {
757 ret = ret && parcel.WriteString(srcPath);
758 ret = ret && parcel.WriteFileDescriptor(fdPair.first);
759 ret = ret && parcel.WriteString(fdPair.second);
760 }
761 return ret;
762 }
763
ReadFileList(MessageParcel & parcel,FileFdMap & fileList)764 bool ReadFileList(MessageParcel &parcel, FileFdMap &fileList)
765 {
766 uint32_t fileListSize = parcel.ReadUint32();
767 if (fileListSize > MAX_FILE_NUM) {
768 CLOGE("The number of files exceeds the upper limit. fileListSize: %{public}u", fileListSize);
769 return false;
770 }
771 for (uint32_t i = 0; i < fileListSize; i++) {
772 std::string src = parcel.ReadString();
773 int32_t fd = parcel.ReadFileDescriptor();
774 std::string dst = parcel.ReadString();
775 fileList[src] = std::make_pair(fd, dst);
776 }
777 return true;
778 }
779
WriteRcvFdFileMap(MessageParcel & parcel,const RcvFdFileMap & rcvFdFileMap)780 bool WriteRcvFdFileMap(MessageParcel &parcel, const RcvFdFileMap &rcvFdFileMap)
781 {
782 bool ret = parcel.WriteUint32(rcvFdFileMap.size());
783 for (const auto &[fd, path] : rcvFdFileMap) {
784 ret = ret && parcel.WriteFileDescriptor(fd);
785 ret = ret && parcel.WriteString(path);
786 }
787 return ret;
788 }
789
ReadRcvFdFileMap(MessageParcel & parcel,RcvFdFileMap & rcvFdFileMap)790 bool ReadRcvFdFileMap(MessageParcel &parcel, RcvFdFileMap &rcvFdFileMap)
791 {
792 uint32_t fileListSize = parcel.ReadUint32();
793 if (fileListSize > MAX_FILE_NUM) {
794 CLOGE("The number of files exceeds the upper limit. fileListSize: %{public}u", fileListSize);
795 return false;
796 }
797 for (uint32_t i = 0; i < fileListSize; i++) {
798 int32_t fd = parcel.ReadFileDescriptor();
799 std::string path = parcel.ReadString();
800 rcvFdFileMap[fd] = path;
801 }
802 return true;
803 }
804 } // namespace CastEngine
805 } // namespace OHOS
806