/* * Copyright (C) 2023-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * Description: supply a helper to write/read common cast engine structures through ipc * Author: zhangge * Create: 2022-06-15 */ #include "cast_engine_common_helper.h" #include #include "cast_engine_log.h" #include "securec.h" namespace OHOS { namespace CastEngine { DEFINE_CAST_ENGINE_LABEL("Cast-Engine-helper"); namespace { constexpr int SESSION_KEY_LENGTH = 16; bool WriteVideoSize(Parcel &parcel, const VideoSize &videoSize) { return parcel.WriteInt32(videoSize.width) && parcel.WriteInt32(videoSize.height); } const VideoSize ReadVideoSize(Parcel &parcel) { return { static_cast(parcel.ReadInt32()), static_cast(parcel.ReadInt32()) }; } bool WriteWindowProperty(Parcel &parcel, const WindowProperty &property) { return parcel.WriteInt32(property.startX) && parcel.WriteInt32(property.startY) && parcel.WriteInt32(property.width) && parcel.WriteInt32(property.height); } const WindowProperty ReadWindowProperty(Parcel &parcel) { WindowProperty property; property.startX = static_cast(parcel.ReadInt32()); property.startY = static_cast(parcel.ReadInt32()); property.width = static_cast(parcel.ReadInt32()); property.height = static_cast(parcel.ReadInt32()); return property; } bool WriteVideoProperty(Parcel &parcel, const VideoProperty &property) { return parcel.WriteInt32(static_cast(property.videoWidth)) && parcel.WriteInt32(static_cast(property.videoHeight)) && parcel.WriteInt32(static_cast(property.fps)) && parcel.WriteInt32(static_cast(property.codecType)) && parcel.WriteInt32(static_cast(property.gop)) && parcel.WriteInt32(static_cast(property.bitrate)) && parcel.WriteInt32(static_cast(property.minBitrate)) && parcel.WriteInt32(static_cast(property.maxBitrate)) && parcel.WriteInt32(static_cast(property.dpi)) && parcel.WriteInt32(static_cast(property.colorStandard)) && parcel.WriteInt32(static_cast(property.screenWidth)) && parcel.WriteInt32(static_cast(property.screenHeight)) && parcel.WriteInt32(static_cast(property.profile)) && parcel.WriteInt32(static_cast(property.level)); } std::optional ReadVideoProperty(Parcel &parcel) { VideoProperty property; property.videoWidth = static_cast(parcel.ReadInt32()); property.videoHeight = static_cast(parcel.ReadInt32()); property.fps = static_cast(parcel.ReadInt32()); auto codecType = parcel.ReadInt32(); property.gop = static_cast(parcel.ReadInt32()); property.bitrate = static_cast(parcel.ReadInt32()); property.minBitrate = static_cast(parcel.ReadInt32()); property.maxBitrate = static_cast(parcel.ReadInt32()); property.dpi = static_cast(parcel.ReadInt32()); auto colorStandard = parcel.ReadInt32(); property.screenWidth = static_cast(parcel.ReadInt32()); property.screenHeight = static_cast(parcel.ReadInt32()); property.profile = static_cast(parcel.ReadInt32()); property.level = static_cast(parcel.ReadInt32()); if (!IsColorStandard(colorStandard) || !IsVideoCodecType(codecType)) { return std::nullopt; } property.codecType = static_cast(codecType); property.colorStandard = static_cast(colorStandard); return property; } bool WriteAudioProperty(Parcel &parcel, const AudioProperty &property) { return parcel.WriteInt32(static_cast(property.sampleRate)) && parcel.WriteInt32(static_cast(property.sampleBitWidth)) && parcel.WriteInt32(static_cast(property.channelConfig)) && parcel.WriteInt32(static_cast(property.bitrate)) && parcel.WriteInt32(static_cast(property.codec)); } const AudioProperty ReadAudioProperty(Parcel &parcel) { AudioProperty property; property.sampleRate = static_cast(parcel.ReadInt32()); property.sampleBitWidth = static_cast(parcel.ReadInt32()); property.channelConfig = static_cast(parcel.ReadInt32()); property.bitrate = static_cast(parcel.ReadInt32()); property.codec = static_cast(parcel.ReadInt32()); return property; } int GetLocalFd(const std::string &url) { char *nextPtr = nullptr; int fd = static_cast(std::strtol(url.c_str(), &nextPtr, DECIMALISM)); if (errno == ERANGE || *nextPtr != '\0') { return INVALID_VALUE; } return fd; } } // namespace bool WriteCastRemoteDevice(Parcel &parcel, const CastRemoteDevice &device) { bool res = parcel.WriteInt32(static_cast(device.deviceType)) && parcel.WriteInt32(static_cast(device.subDeviceType)) && parcel.WriteInt32(static_cast(device.channelType)) && parcel.WriteString(device.deviceId) && parcel.WriteString(device.deviceName) && parcel.WriteString(device.ipAddress) && parcel.WriteString(device.networkId) && parcel.WriteString(device.localIpAddress); if (device.sessionKeyLength == SESSION_KEY_LENGTH && device.sessionKey) { res = res && parcel.WriteUint32(device.sessionKeyLength); res = res && parcel.WriteBuffer(device.sessionKey, device.sessionKeyLength); } else { parcel.WriteUint32(0); } return res; } bool ReadCastRemoteDevice(Parcel &parcel, CastRemoteDevice &device) { auto remote = ReadCastRemoteDevice(parcel); if (remote == nullptr) { CLOGE("ReadCastRemoteDevice failed"); return false; } device = *remote; return true; } std::unique_ptr ReadCastRemoteDevice(Parcel &parcel) { auto device = std::make_unique(); auto deviceType = parcel.ReadInt32(); auto subDeviceType = parcel.ReadInt32(); auto channelType = parcel.ReadInt32(); device->deviceType = static_cast(deviceType); device->subDeviceType = static_cast(subDeviceType); device->channelType = static_cast(channelType); device->deviceId = parcel.ReadString(); device->deviceName = parcel.ReadString(); device->ipAddress = parcel.ReadString(); device->networkId = parcel.ReadString(); device->localIpAddress = parcel.ReadString(); device->sessionKeyLength = parcel.ReadUint32(); if (device->sessionKeyLength == SESSION_KEY_LENGTH) { device->sessionKey = parcel.ReadBuffer(static_cast(device->sessionKeyLength)); } else { device->sessionKeyLength = 0; device->sessionKey = nullptr; } if (!IsDeviceType(deviceType) || !IsSubDeviceType(subDeviceType) || !IsChannelType(channelType)) { CLOGE("ReadCastRemoteDevice error"); return nullptr; } return device; } bool WriteMediaInfo(MessageParcel &parcel, const MediaInfo &mediaInfo) { if (mediaInfo.mediaUrl.empty()) { CLOGE("mediaUrl is empty"); return false; } int fd = GetLocalFd(mediaInfo.mediaUrl); if (fd != INVALID_VALUE) { if (!parcel.WriteString("localFd") || !parcel.WriteFileDescriptor(fd)) { CLOGE("Write local fd failed, fd = %{public}d", fd); return false; } } else if (!parcel.WriteString("path") || !parcel.WriteString(mediaInfo.mediaUrl)) { CLOGE("Write path failed"); return false; } return parcel.WriteString(mediaInfo.mediaId) && parcel.WriteString(mediaInfo.mediaName) && parcel.WriteString(mediaInfo.mediaType) && parcel.WriteUint32(mediaInfo.mediaSize) && parcel.WriteString(mediaInfo.albumCoverUrl) && parcel.WriteString(mediaInfo.albumTitle) && parcel.WriteString(mediaInfo.mediaArtist) && parcel.WriteString(mediaInfo.lrcUrl) && parcel.WriteString(mediaInfo.lrcContent) && parcel.WriteString(mediaInfo.appIconUrl) && parcel.WriteString(mediaInfo.appName) && parcel.WriteUint32(mediaInfo.startPosition) && parcel.WriteUint32(mediaInfo.duration) && parcel.WriteUint32(mediaInfo.closingCreditsPosition); } std::unique_ptr ReadMediaInfo(MessageParcel &parcel) { auto mediaInfo = std::make_unique(); if (mediaInfo == nullptr) { CLOGE("Failed to malloc mediaInfo"); return nullptr; } std::string urlType = parcel.ReadString(); if (urlType == "localFd") { CLOGD("localFd"); mediaInfo->mediaUrl = std::to_string(parcel.ReadFileDescriptor()); } else { CLOGD("online or localPath"); mediaInfo->mediaUrl = parcel.ReadString(); } mediaInfo->mediaId = parcel.ReadString(); mediaInfo->mediaName = parcel.ReadString(); mediaInfo->mediaType = parcel.ReadString(); mediaInfo->mediaSize = parcel.ReadUint32(); mediaInfo->albumCoverUrl = parcel.ReadString(); mediaInfo->albumTitle = parcel.ReadString(); mediaInfo->mediaArtist = parcel.ReadString(); mediaInfo->lrcUrl = parcel.ReadString(); mediaInfo->lrcContent = parcel.ReadString(); mediaInfo->appIconUrl = parcel.ReadString(); mediaInfo->appName = parcel.ReadString(); mediaInfo->startPosition = parcel.ReadUint32(); mediaInfo->duration = parcel.ReadUint32(); mediaInfo->closingCreditsPosition = parcel.ReadUint32(); return mediaInfo; } bool WriteMediaInfoHolder(MessageParcel &parcel, const MediaInfoHolder &mediaInfoHolder) { bool ret = parcel.WriteUint32(mediaInfoHolder.currentIndex); ret = ret && parcel.WriteUint32(mediaInfoHolder.progressRefreshInterval); ret = ret && parcel.WriteUint32(static_cast(mediaInfoHolder.mediaInfoList.size())); for (auto iter = mediaInfoHolder.mediaInfoList.begin(); iter != mediaInfoHolder.mediaInfoList.end(); iter++) { ret = ret && WriteMediaInfo(parcel, *iter); } return ret; } std::unique_ptr ReadMediaInfoHolder(MessageParcel &parcel) { auto mediaInfoHolder = std::make_unique(); if (mediaInfoHolder == nullptr) { CLOGE("Failed to malloc mediaInfoHolder"); return nullptr; } mediaInfoHolder->currentIndex = parcel.ReadUint32(); mediaInfoHolder->progressRefreshInterval = parcel.ReadUint32(); uint32_t infoListSize = parcel.ReadUint32(); if (infoListSize > MAX_FILE_NUM) { CLOGE("The number of list exceeds the upper limit. infoListSize: %{public}u", infoListSize); return nullptr; } for (uint32_t i = 0; i < infoListSize; i++) { auto mediaInfo = ReadMediaInfo(parcel); if (mediaInfo == nullptr) { return nullptr; } mediaInfoHolder->mediaInfoList.push_back(*mediaInfo); } return mediaInfoHolder; } bool WriteCastLocalDevice(Parcel &parcel, const CastLocalDevice &device) { return parcel.WriteString(device.deviceId) && parcel.WriteString(device.deviceName) && parcel.WriteInt32(static_cast(device.deviceType)) && parcel.WriteInt32(static_cast(device.subDeviceType)) && parcel.WriteString(device.ipAddress) && parcel.WriteInt32(static_cast(device.triggerType)) && parcel.WriteString(device.authData); } std::unique_ptr ReadCastLocalDevice(Parcel &parcel) { auto device = std::make_unique(); device->deviceId = parcel.ReadString(); device->deviceName = parcel.ReadString(); auto deviceType = parcel.ReadInt32(); auto subDeviceType = parcel.ReadInt32(); device->ipAddress = parcel.ReadString(); auto triggerType = parcel.ReadInt32(); device->authData = parcel.ReadString(); if (!IsDeviceType(deviceType) || !IsSubDeviceType(subDeviceType) || !IsTriggerType(triggerType)) { CLOGE("ReadCastLocalDevice error"); return nullptr; } device->deviceType = static_cast(deviceType); device->subDeviceType = static_cast(subDeviceType); device->triggerType = static_cast(triggerType); return device; } bool WriteCastSessionProperty(Parcel &parcel, const CastSessionProperty &property) { return parcel.WriteInt32(static_cast(property.protocolType)) && parcel.WriteInt32(static_cast(property.endType)) && WriteAudioProperty(parcel, property.audioProperty) && WriteVideoProperty(parcel, property.videoProperty) && WriteWindowProperty(parcel, property.windowProperty); } std::unique_ptr ReadCastSessionProperty(Parcel &parcel) { auto property = std::make_unique(); if (property == nullptr) { CLOGE("Failed to malloc cast session property"); return nullptr; } auto protocolType = parcel.ReadInt32(); if (!IsProtocolType(protocolType)) { return nullptr; } auto endType = parcel.ReadInt32(); if (!IsEndType(endType)) { return nullptr; } property->protocolType = static_cast(protocolType); property->endType = static_cast(endType); property->audioProperty = ReadAudioProperty(parcel); auto videoProperty = ReadVideoProperty(parcel); if (videoProperty == std::nullopt) { return nullptr; } property->videoProperty = videoProperty.value(); property->windowProperty = ReadWindowProperty(parcel); return property; } bool WritePropertyContainer(Parcel &parcel, const PropertyContainer &container) { return parcel.WriteInt32(static_cast(container.type)) && (((container.type == PropertyType::VIDEO_SIZE) && WriteVideoSize(parcel, container.videoSize)) || ((container.type == PropertyType::VIDEO_FPS) && parcel.WriteInt32(container.videoFps)) || ((container.type == PropertyType::WINDOW_SIZE) && WriteWindowProperty(parcel, container.windowProperty))); } std::unique_ptr ReadPropertyContainer(Parcel &parcel) { auto container = std::make_unique(); if (container == nullptr) { CLOGE("Failed to malloc property container"); return nullptr; } int32_t type = parcel.ReadInt32(); if (!IsPropertyType(type)) { return nullptr; } container->type = static_cast(type); if (container->type == PropertyType::VIDEO_SIZE) { container->videoSize = ReadVideoSize(parcel); } else if (container->type == PropertyType::VIDEO_FPS) { container->videoFps = static_cast(parcel.ReadInt32()); } else { container->windowProperty = ReadWindowProperty(parcel); } return container; } bool WriteAuthInfo(Parcel &parcel, const AuthInfo &authInfo) { return parcel.WriteInt32(static_cast(authInfo.authMode)) && parcel.WriteInt32(static_cast(authInfo.authCode)) && parcel.WriteString(authInfo.deviceId); } std::unique_ptr ReadAuthInfo(Parcel &parcel) { auto authInfo = std::make_unique(); if (authInfo == nullptr) { CLOGE("Failed to malloc auth info"); return nullptr; } authInfo->authMode = static_cast(parcel.ReadInt32()); authInfo->authCode = static_cast(parcel.ReadInt32()); authInfo->deviceId = parcel.ReadString(); return authInfo; } bool WriteTouchPoint(Parcel &parcel, const OHNativeXcomponentTouchPoint &touchPoint) { return parcel.WriteInt32(touchPoint.id) && parcel.WriteFloat(touchPoint.screenX) && parcel.WriteFloat(touchPoint.screenY) && parcel.WriteFloat(touchPoint.x) && parcel.WriteFloat(touchPoint.y) && parcel.WriteUint32(static_cast(touchPoint.type)) && parcel.WriteDouble(touchPoint.size) && parcel.WriteFloat(touchPoint.force) && parcel.WriteInt64(touchPoint.timeStamp) && parcel.WriteBool(touchPoint.isPressed); } bool WriteTouchPoints(Parcel &parcel, const OHNativeXcomponentTouchEvent touchEvent) { for (uint32_t i = 0; i < touchEvent.numPoints; i++) { if (!WriteTouchPoint(parcel, touchEvent.touchPoints[i])) { return false; } } return true; } bool WriteTouchEvent(Parcel &parcel, const OHNativeXcomponentTouchEvent &touchEvent) { return parcel.WriteInt32(touchEvent.id) && parcel.WriteFloat(touchEvent.screenX) && parcel.WriteFloat(touchEvent.screenY) && parcel.WriteFloat(touchEvent.x) && parcel.WriteFloat(touchEvent.y) && parcel.WriteUint32(static_cast(touchEvent.type)) && parcel.WriteDouble(touchEvent.size) && parcel.WriteFloat(touchEvent.force) && parcel.WriteInt64(touchEvent.deviceId) && parcel.WriteInt64(touchEvent.timeStamp) && parcel.WriteUint32(touchEvent.numPoints) && WriteTouchPoints(parcel, touchEvent); } bool WriteMouseEvent(Parcel &parcel, const OHNativeXcomponentMouseEvent &mouseEvent) { return parcel.WriteFloat(mouseEvent.x) && parcel.WriteFloat(mouseEvent.y) && parcel.WriteFloat(mouseEvent.screenX) && parcel.WriteFloat(mouseEvent.screenY) && parcel.WriteInt64(mouseEvent.timestamp) && parcel.WriteUint32(static_cast(mouseEvent.action)) && parcel.WriteUint32(static_cast(mouseEvent.button)); } bool WriteWhellEvent(Parcel &parcel, const OHNativeXcomponentWheelEvent &wheelEvent) { return parcel.WriteUint32(static_cast(wheelEvent.direction)) && parcel.WriteUint8(wheelEvent.indication) && parcel.WriteUint8(wheelEvent.scrollUnit) && parcel.WriteUint16(wheelEvent.wheelDis) && parcel.WriteFloat(wheelEvent.x) && parcel.WriteFloat(wheelEvent.y); } bool WriteKeyEvent(Parcel &parcel, const OHNativeXcomponentKeyEvent &keyEvent) { return parcel.WriteUint8(keyEvent.reserved) && parcel.WriteUint16(keyEvent.keyCode1) && parcel.WriteUint16(keyEvent.keyCode2) && parcel.WriteUint32(keyEvent.metaState) && parcel.WriteUint32(static_cast(keyEvent.type)); } bool WriteContentEvent(Parcel &parcel, const OHNativeXcomponentContentEvent &contentEvent) { return parcel.WriteUint16(contentEvent.msgLen) && parcel.WriteCString(contentEvent.inputText); } bool WriteFocusEvent(Parcel &parcel, const OHNativeXcomponentFocusEvent &focusEvent) { return parcel.WriteUint8(focusEvent.focusStat) && parcel.WriteDouble(focusEvent.cursorX1) && parcel.WriteDouble(focusEvent.cursorY1) && parcel.WriteDouble(focusEvent.cursorX2) && parcel.WriteDouble(focusEvent.cursorY2); } bool WriteInputMethodEvent(Parcel &parcel, const OHNativeXcomponentInputMethodEvent &inputMethodEvent) { return parcel.WriteUint32(static_cast(inputMethodEvent.type)) && (((inputMethodEvent.type == OHNativeXcomponentInputMethodEventType::OH_NATIVEXCOMPONENT_INPUT_CONTENT) && WriteContentEvent(parcel, inputMethodEvent.contentEvent)) || ((inputMethodEvent.type == OHNativeXcomponentInputMethodEventType::OH_NATIVEXCOMPONENT_INPUT_FOCUS) && WriteFocusEvent(parcel, inputMethodEvent.focusEvent))); } bool WriteVirtualKeyEvent(Parcel &parcel, const OHNativeXcomponentVirtualKeyEvent &virtualKeyEvent) { return parcel.WriteInt32(static_cast(virtualKeyEvent.type)) && parcel.WriteFloat(virtualKeyEvent.x) && parcel.WriteFloat(virtualKeyEvent.y); } bool WriteRemoteControlEvent(Parcel &parcel, const OHRemoteControlEvent &event) { return parcel.WriteInt32(static_cast(event.eventType)) && (((event.eventType == XcomponentEventType::REMOTECONTROL_TOUCH) && WriteTouchEvent(parcel, event.touchEvent)) || ((event.eventType == XcomponentEventType::REMOTECONTROL_MOUSE) && WriteMouseEvent(parcel, event.mouseEvent)) || ((event.eventType == XcomponentEventType::REMOTECONTROL_WHEEL) && WriteWhellEvent(parcel, event.wheelEvent)) || ((event.eventType == XcomponentEventType::REMOTECONTROL_KEY) && WriteKeyEvent(parcel, event.keyEvent)) || ((event.eventType == XcomponentEventType::REMOTECONTROL_INPUT_METHOD) && WriteInputMethodEvent(parcel, event.inputMethodEvent)) || ((event.eventType == XcomponentEventType::REMOTECONTROL_VIRTUAL_KEY) && WriteVirtualKeyEvent(parcel, event.virtualKeyEvent))); } const OHNativeXcomponentTouchPoint ReadTouchPoint(Parcel &parcel) { return { parcel.ReadInt32(), parcel.ReadFloat(), parcel.ReadFloat(), parcel.ReadFloat(), parcel.ReadFloat(), static_cast(parcel.ReadUint32()), parcel.ReadDouble(), parcel.ReadFloat(), parcel.ReadInt64(), parcel.ReadBool() }; } void ReadTouchPoints(Parcel &parcel, uint32_t numPoints, OHNativeXcomponentTouchPoint points[]) { for (uint32_t i = 0; i < numPoints; i++) { points[i] = ReadTouchPoint(parcel); } } void ReadTouchEvent(Parcel &parcel, OHNativeXcomponentTouchEvent &touchEvent) { touchEvent.id = parcel.ReadInt32(); touchEvent.screenX = parcel.ReadFloat(); touchEvent.screenY = parcel.ReadFloat(); touchEvent.x = parcel.ReadFloat(); touchEvent.y = parcel.ReadFloat(); touchEvent.type = static_cast(parcel.ReadUint32()); touchEvent.size = parcel.ReadDouble(); touchEvent.force = parcel.ReadFloat(); touchEvent.deviceId = parcel.ReadInt64(); touchEvent.timeStamp = parcel.ReadInt64(); touchEvent.numPoints = parcel.ReadUint32(); ReadTouchPoints(parcel, touchEvent.numPoints, touchEvent.touchPoints); } const OHNativeXcomponentMouseEvent ReadMouseEvent(Parcel &parcel) { return { parcel.ReadFloat(), parcel.ReadFloat(), parcel.ReadFloat(), parcel.ReadFloat(), parcel.ReadInt64(), static_cast(parcel.ReadUint32()), static_cast(parcel.ReadUint32()) }; } const OHNativeXcomponentWheelEvent ReadWhellEvent(Parcel &parcel) { return { static_cast(parcel.ReadUint32()), parcel.ReadUint8(), parcel.ReadUint8(), parcel.ReadUint16(), parcel.ReadFloat(), parcel.ReadFloat() }; } const OHNativeXcomponentKeyEvent ReadKeyEvent(Parcel &parcel) { return { parcel.ReadUint8(), parcel.ReadUint16(), parcel.ReadUint16(), parcel.ReadUint32(), static_cast(parcel.ReadUint32()) }; } void ReadContentEvent(Parcel &parcel, OHNativeXcomponentContentEvent &contentEvent) { contentEvent.msgLen = parcel.ReadUint16(); if (strcpy_s(contentEvent.inputText, OH_MAX_CONTENT_LEN, parcel.ReadCString()) != EOK) { CLOGE("Failed copy content to array"); } } const OHNativeXcomponentFocusEvent ReadFocusEvent(Parcel &parcel) { return { parcel.ReadUint8(), parcel.ReadDouble(), parcel.ReadDouble(), parcel.ReadDouble(), parcel.ReadDouble() }; } void ReadInputMethodEvent(Parcel &parcel, OHNativeXcomponentInputMethodEvent &inputMethodEvent) { inputMethodEvent.type = static_cast(parcel.ReadUint16()); if (inputMethodEvent.type == OHNativeXcomponentInputMethodEventType::OH_NATIVEXCOMPONENT_INPUT_CONTENT) { return ReadContentEvent(parcel, inputMethodEvent.contentEvent); } inputMethodEvent.focusEvent = ReadFocusEvent(parcel); } const OHNativeXcomponentVirtualKeyEvent ReadVirtualKeyEvent(Parcel &parcel) { return { static_cast(parcel.ReadInt32()), parcel.ReadFloat(), parcel.ReadFloat() }; } std::unique_ptr ReadRemoteControlEvent(Parcel &parcel) { auto remoteControlEvent = std::make_unique(); if (remoteControlEvent == nullptr) { CLOGE("Failed to malloc remote control event"); return nullptr; } remoteControlEvent->eventType = static_cast(parcel.ReadUint32()); switch (remoteControlEvent->eventType) { case XcomponentEventType::REMOTECONTROL_TOUCH: ReadTouchEvent(parcel, remoteControlEvent->touchEvent); break; case XcomponentEventType::REMOTECONTROL_MOUSE: remoteControlEvent->mouseEvent = ReadMouseEvent(parcel); break; case XcomponentEventType::REMOTECONTROL_WHEEL: remoteControlEvent->wheelEvent = ReadWhellEvent(parcel); break; case XcomponentEventType::REMOTECONTROL_KEY: remoteControlEvent->keyEvent = ReadKeyEvent(parcel); break; case XcomponentEventType::REMOTECONTROL_INPUT_METHOD: ReadInputMethodEvent(parcel, remoteControlEvent->inputMethodEvent); break; case XcomponentEventType::REMOTECONTROL_VIRTUAL_KEY: remoteControlEvent->virtualKeyEvent = ReadVirtualKeyEvent(parcel); break; default: CLOGE("Parameter error, event:(%d) not support", static_cast(remoteControlEvent->eventType)); remoteControlEvent = nullptr; break; } return remoteControlEvent; } bool WriteDeviceStateInfo(Parcel &parcel, const DeviceStateInfo &stateInfo) { return parcel.WriteInt32(static_cast(stateInfo.deviceState)) && parcel.WriteString(stateInfo.deviceId) && parcel.WriteInt32(static_cast(stateInfo.eventCode)); } std::unique_ptr ReadDeviceStateInfo(Parcel &parcel) { auto stateInfo = std::make_unique(); // Parse device state int32_t state = parcel.ReadInt32(); if (!IsDeviceState(state)) { CLOGE("incorrect device state"); return nullptr; } stateInfo->deviceState = static_cast(state); // Parse deviceId std::string deviceId = parcel.ReadString(); if (deviceId.empty()) { CLOGE("device id is empty"); return nullptr; } stateInfo->deviceId = deviceId; // Parse event code int32_t eventCode = parcel.ReadInt32(); stateInfo->eventCode = static_cast(eventCode); return stateInfo; } bool WriteEvent(Parcel &parcel, const EventId &eventId, const std::string &jsonParam) { return parcel.WriteInt32(static_cast(eventId)) && parcel.WriteString(jsonParam); } bool ReadEvent(Parcel &parcel, int32_t &eventId, std::string &jsonParam) { eventId = parcel.ReadInt32(); if (!IsEventId(eventId)) { CLOGE("incorrect event id"); return false; } jsonParam = parcel.ReadString(); return true; } void SetDataCapacity(MessageParcel &parcel, const FileFdMap &fileList, uint32_t tokenSize) { uint32_t totalSize = tokenSize; totalSize += sizeof(uint32_t); for (const auto &[srcPath, fdPair] : fileList) { totalSize += srcPath.length(); totalSize += sizeof(fdPair.first); totalSize += fdPair.second.length(); } if (totalSize > parcel.GetDataCapacity()) { CLOGD("SetDataCapacity totalSize: %d", totalSize); parcel.SetMaxCapacity(totalSize + totalSize); parcel.SetDataCapacity(totalSize); } } bool WriteFileList(MessageParcel &parcel, const FileFdMap &fileList) { bool ret = parcel.WriteUint32(fileList.size()); for (const auto &[srcPath, fdPair] : fileList) { ret = ret && parcel.WriteString(srcPath); ret = ret && parcel.WriteFileDescriptor(fdPair.first); ret = ret && parcel.WriteString(fdPair.second); } return ret; } bool ReadFileList(MessageParcel &parcel, FileFdMap &fileList) { uint32_t fileListSize = parcel.ReadUint32(); if (fileListSize > MAX_FILE_NUM) { CLOGE("The number of files exceeds the upper limit. fileListSize: %{public}u", fileListSize); return false; } for (uint32_t i = 0; i < fileListSize; i++) { std::string src = parcel.ReadString(); int32_t fd = parcel.ReadFileDescriptor(); std::string dst = parcel.ReadString(); fileList[src] = std::make_pair(fd, dst); } return true; } bool WriteRcvFdFileMap(MessageParcel &parcel, const RcvFdFileMap &rcvFdFileMap) { bool ret = parcel.WriteUint32(rcvFdFileMap.size()); for (const auto &[fd, path] : rcvFdFileMap) { ret = ret && parcel.WriteFileDescriptor(fd); ret = ret && parcel.WriteString(path); } return ret; } bool ReadRcvFdFileMap(MessageParcel &parcel, RcvFdFileMap &rcvFdFileMap) { uint32_t fileListSize = parcel.ReadUint32(); if (fileListSize > MAX_FILE_NUM) { CLOGE("The number of files exceeds the upper limit. fileListSize: %{public}u", fileListSize); return false; } for (uint32_t i = 0; i < fileListSize; i++) { int32_t fd = parcel.ReadFileDescriptor(); std::string path = parcel.ReadString(); rcvFdFileMap[fd] = path; } return true; } } // namespace CastEngine } // namespace OHOS