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