From a9f86befa59aedac0e2177e2a1ebf1bf74fabeef Mon Sep 17 00:00:00 2001 From: kangpeng9771 Date: Thu, 11 Sep 2025 15:10:46 +0800 Subject: [PATCH 1/4] IDL Signed-off-by: kangpeng9771 --- ipc/native/src/taihe/idl/ohos.rpc.rpc.taihe | 166 ++- ipc/native/src/taihe/inc/taihe_ashmem.h | 21 + ipc/native/src/taihe/inc/taihe_ipc_skeleton.h | 6 + .../src/taihe/inc/taihe_message_option.h | 8 + .../src/taihe/inc/taihe_message_sequence.h | 86 ++ .../src/taihe/src/ohos.rpc.rpc.impl.cpp | 860 ++++++++++++ ipc/native/src/taihe/test/test_main.ets | 1189 ++++++++++++++++- 7 files changed, 2261 insertions(+), 75 deletions(-) diff --git a/ipc/native/src/taihe/idl/ohos.rpc.rpc.taihe b/ipc/native/src/taihe/idl/ohos.rpc.rpc.taihe index 2e433a6c..3ace6966 100644 --- a/ipc/native/src/taihe/idl/ohos.rpc.rpc.taihe +++ b/ipc/native/src/taihe/idl/ohos.rpc.rpc.taihe @@ -46,11 +46,48 @@ struct RequestResult { reply: MessageSequence; } +enum ErrorCode { + CHECK_PARAM_ERROR = 401, + OS_MMAP_ERROR = 1900001, + OS_IOCTL_ERROR = 1900002, + WRITE_TO_ASHMEM_ERROR = 1900003, + READ_FROM_ASHMEM_ERROR = 1900004, + ONLY_PROXY_OBJECT_PERMITTED_ERROR = 1900005, + ONLY_REMOTE_OBJECT_PERMITTED_ERROR = 1900006, + COMMUNICATION_ERROR = 1900007, + PROXY_OR_REMOTE_OBJECT_INVALID_ERROR = 1900008, + WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR = 1900009, + READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR = 1900010, + PARCEL_MEMORY_ALLOC_ERROR = 1900011, + CALL_JS_METHOD_ERROR = 1900012, + OS_DUP_ERROR = 1900013 +} + +enum TypeCode : i32{ + INT8_ARRAY = 0, + UINT8_ARRAY = 1, + INT16_ARRAY = 2, + UINT16_ARRAY = 3, + INT32_ARRAY = 4, + UINT32_ARRAY = 5, + FLOAT32_ARRAY = 6, + FLOAT64_ARRAY = 7, + BIGINT64_ARRAY = 8, + BIGUINT64_ARRAY = 9, +} + @class interface Ashmem { MapReadWriteAshmem(): void; GetNativePtr(): i64; GetAshmemSize(): i32; + SetProtectionType(protectionType: i32): void; + MapReadonlyAshmem(): void; + MapTypedAshmem(mapType: i32): void; + CloseAshmem(): void; + UnmapAshmem(): void; + ReadDataFromAshmem(size: i32, offset: i32): @arraybuffer Array; + WriteDataToAshmem(buf: @arraybuffer Array, size: i32, offset: i32): void; } @static("Ashmem") @@ -91,16 +128,23 @@ interface MessageSequence { WriteInt(val: i32): void; WriteLong(val: i64): void; + WriteDouble(val: f64): void; WriteBoolean(val: bool): void; + WriteChar(val: u8): void; WriteString(val: String): void; WriteParcelable(val: Parcelable): void; WriteByteArray(byteArray: Array): void; + WriteShortArray(shortArray: Array): void; WriteIntArray(intArray: Array): void; + WriteLongArray(longArray: Array): void; + WriteFloatArray(floatArray: Array): void; WriteDoubleArray(doubleArray: Array): void; WriteBooleanArray(booleanArray: Array): void; + WriteCharArray(charArray: Array): void; WriteStringArray(stringArray: Array): void; WriteParcelableArray(parcelableArray: Array): void; + WriteRemoteObjectArray(objectArray: Array): void; ReadInt(): i32; ReadLong(): i64; @@ -152,6 +196,75 @@ interface MessageSequence { } """) + @overload("readRemoteObjectArray") + ReadRemoteObjectArrayImpl(): Array; + @!sts_inject_into_class(""" + readRemoteObjectArray(dataIn: Array): void { + let res: Array = this.readRemoteObjectArray(); + for (let i = 0; i < res.length; i++) { + dataIn[i] = res[i]; + } + } + """) + + @overload("readCharArray") + ReadCharArrayImpl(): Array; + @!sts_inject_into_class(""" + readCharArray(dataIn: Array): void { + let res: Array = this.readCharArray(); + for (let i = 0; i < res.length; i++) { + dataIn[i] = res[i]; + } + } + """) + + @overload("readFloatArray") + ReadFloatArrayImpl(): Array; + @!sts_inject_into_class(""" + readFloatArray(dataIn: Array): void { + let res: Array = this.readFloatArray(); + for (let i = 0; i < res.length; i++) { + dataIn[i] = res[i]; + } + } + """) + + @overload("readLongArray") + ReadLongArrayImpl(): Array; + @!sts_inject_into_class(""" + readLongArray(dataIn: Array): void { + let res: Array = this.readLongArray(); + for (let i = 0; i < res.length; i++) { + dataIn[i] = res[i]; + } + } + """) + + @overload("readShortArray") + ReadShortArrayImpl(): Array; + @!sts_inject_into_class(""" + readShortArray(dataIn: Array): void { + let res: Array = this.readShortArray(); + for (let i = 0; i < res.length; i++) { + dataIn[i] = res[i]; + } + } + """) + + @overload("readByteArray") + ReadByteArrayGet(): Array; + @overload("readByteArray") + ReadByteArrayIn(dataIn: Array); + + ReadChar(): i16; + ReadFloat(): f64; + ReadDouble(): f64; + ReadShort(): i16; + ReadByte(): i8; + RewindRead(pos: i64): void; + RewindWrite(pos: i64): void; + SetSize(size: i64): void; + ReadParcelableArray(parcelableArray: Array): void; WriteFileDescriptor(fd: i32): void; @@ -165,6 +278,30 @@ interface MessageSequence { GetNativePtr(): i64; AddJsObjWeakRef(obj: MessageSequence): void; + + WriteArrayBuffer(buf: @arraybuffer Array, typeCode: TypeCode): void; + + ReadArrayBuffer(typeCode: TypeCode): @arraybuffer Array; + + GetSize(): i64; + + GetWritableBytes(): i64; + + GetReadableBytes(): i64; + + GetReadPosition(): i64; + + GetWritePosition(): i64; + + ContainFileDescriptors(): bool; + + GetRawDataCapacity(): i64; + + WriteByte(val: i8): void; + + WriteShort(val: i16): void; + + WriteFloat(val: f64): void; } @static("MessageSequence") @@ -180,6 +317,9 @@ function CreateMessageSequence(): MessageSequence; @static("MessageSequence") function CloseFileDescriptor(fd: i32): void; +@static("MessageSequence") +function DupFileDescriptor(fd: i32): i32; + function unwrapRemoteObject(obj: IRemoteObjectUnion): i64; function wrapRemoteObject(nativePtr: i64): IRemoteObjectUnion; @@ -191,7 +331,7 @@ interface IRemoteObject { @gen_async("sendMessageRequest") SendMessageRequestSync(code: i32, data: MessageSequence, reply: MessageSequence, options: MessageOption): RequestResult; - + RegisterDeathRecipient(recipient: DeathRecipient, flags: i32): void; UnregisterDeathRecipient(recipient: DeathRecipient, flags: i32): void; @@ -240,6 +380,10 @@ interface MessageOption { IsAsync(): bool; SetAsync(isAsync: bool): void; GetNativePtr(): i64; + GetFlags(): i32; + SetFlags(flags: i32): void; + GetWaitTime(): i32; + SetWaitTime(waitTime: i32): void; } @ctor("MessageOption") @@ -266,4 +410,22 @@ function GetCallingUid(): i32; function GetCallingTokenId(): i64; @static("IPCSkeleton") -function GetContextObject(): IRemoteObjectUnion; \ No newline at end of file +function GetContextObject(): IRemoteObjectUnion; + +@static("IPCSkeleton") +function ResetCallingIdentity(): String; + +@static("IPCSkeleton") +function RestoreCallingIdentity(identity: String): void; + +@static("IPCSkeleton") +function FlushCmdBuffer(object: IRemoteObjectUnion): void; + +@static("IPCSkeleton") +function GetCallingDeviceID(): String; + +@static("IPCSkeleton") +function GetLocalDeviceID(): String; + +@static("IPCSkeleton") +function IsLocalCalling(): bool; \ No newline at end of file diff --git a/ipc/native/src/taihe/inc/taihe_ashmem.h b/ipc/native/src/taihe/inc/taihe_ashmem.h index 851f3645..d4eeabca 100644 --- a/ipc/native/src/taihe/inc/taihe_ashmem.h +++ b/ipc/native/src/taihe/inc/taihe_ashmem.h @@ -34,6 +34,13 @@ namespace OHOS { class AshmemImpl { public: // only be used for returning invalid Ashmem. + enum { + PROT_EXEC = 4, + PROT_NONE = 0, + PROT_READ = 1, + PROT_WRITE = 2, + }; + explicit AshmemImpl(); explicit AshmemImpl(const char *name, int32_t size); @@ -48,6 +55,20 @@ public: OHOS::sptr GetAshmem(); + void SetProtectionType(int32_t protectionType); + + void MapReadonlyAshmem(); + + void MapTypedAshmem(int32_t mapType); + + void CloseAshmem(); + + void UnmapAshmem(); + + ::taihe::array ReadDataFromAshmem(int32_t size, int32_t offset); + + void WriteDataToAshmem(::taihe::array_view buf, int32_t size, int32_t offset); + static ::ohos::rpc::rpc::Ashmem CreateAshmem_WithTwoParam(::taihe::string_view name, int32_t size); static ::ohos::rpc::rpc::Ashmem CreateAshmem_WithOneParam(::ohos::rpc::rpc::weak::Ashmem ashmem); diff --git a/ipc/native/src/taihe/inc/taihe_ipc_skeleton.h b/ipc/native/src/taihe/inc/taihe_ipc_skeleton.h index 3327368c..c8718506 100644 --- a/ipc/native/src/taihe/inc/taihe_ipc_skeleton.h +++ b/ipc/native/src/taihe/inc/taihe_ipc_skeleton.h @@ -36,6 +36,12 @@ public: static int32_t GetCallingPid(); static int32_t GetCallingUid(); static int64_t GetCallingTokenId(); + static ::taihe::string ResetCallingIdentity(); + static void RestoreCallingIdentity(::taihe::string_view identity); + static void FlushCmdBuffer(::ohos::rpc::rpc::IRemoteObjectUnion const& object); + static ::taihe::string GetCallingDeviceID(); + static ::taihe::string GetLocalDeviceID(); + static bool IsLocalCalling(); static ::ohos::rpc::rpc::IRemoteObjectUnion GetContextObject(); }; diff --git a/ipc/native/src/taihe/inc/taihe_message_option.h b/ipc/native/src/taihe/inc/taihe_message_option.h index 0d204266..db50d5c4 100644 --- a/ipc/native/src/taihe/inc/taihe_message_option.h +++ b/ipc/native/src/taihe/inc/taihe_message_option.h @@ -39,6 +39,14 @@ public: void SetAsync(bool isAsync); + int32_t GetFlags(); + + void SetFlags(int32_t flags); + + int32_t GetWaitTime(); + + void SetWaitTime(int32_t waitTime); + int64_t GetNativePtr(); void AddJsObjWeakRef(::ohos::rpc::rpc::weak::MessageOption obj); diff --git a/ipc/native/src/taihe/inc/taihe_message_sequence.h b/ipc/native/src/taihe/inc/taihe_message_sequence.h index 3fe66341..bc56c5a8 100644 --- a/ipc/native/src/taihe/inc/taihe_message_sequence.h +++ b/ipc/native/src/taihe/inc/taihe_message_sequence.h @@ -64,6 +64,10 @@ public: void WriteLong(int64_t val); + void WriteDouble(double val); + + void WriteChar(uint8_t val); + void WriteBoolean(bool val); void WriteString(::taihe::string_view val); @@ -72,16 +76,26 @@ public: void WriteByteArray(::taihe::array_view byteArray); + void WriteShortArray(::taihe::array_view shortArray); + void WriteIntArray(::taihe::array_view intArray); + void WriteLongArray(::taihe::array_view longArray); + + void WriteFloatArray(::taihe::array_view floatArray); + void WriteDoubleArray(::taihe::array_view doubleArray); void WriteBooleanArray(::taihe::array_view booleanArray); + void WriteCharArray(::taihe::array_view charArray); + void WriteStringArray(::taihe::array_view<::taihe::string> stringArray); void WriteParcelableArray(::taihe::array_view<::ohos::rpc::rpc::Parcelable> parcelableArray); + void WriteRemoteObjectArray(::taihe::array_view<::ohos::rpc::rpc::IRemoteObjectUnion> objectArray); + int32_t ReadInt(); int64_t ReadLong(); @@ -100,6 +114,24 @@ public: ::taihe::array<::taihe::string> ReadStringArrayImpl(); + ::taihe::array ReadCharArrayImpl(); + + ::taihe::array ReadFloatArrayImpl(); + + ::taihe::array ReadLongArrayImpl(); + + ::taihe::array ReadShortArrayImpl(); + + int16_t ReadChar(); + + double ReadFloat(); + + double ReadDouble(); + + int16_t ReadShort(); + + int8_t ReadByte(); + void ReadParcelableArray(::taihe::array_view<::ohos::rpc::rpc::Parcelable> parcelableArray); void WriteFileDescriptor(int32_t fd); @@ -122,6 +154,60 @@ public: static void CloseFileDescriptor(int32_t fd); int64_t GetMessageSequenceImpl(); + + void WriteArrayBuffer(::taihe::array_view buf, ::ohos::rpc::rpc::TypeCode typeCode); + + bool WriteVectorByTypeCode(void *data, ::ohos::rpc::rpc::TypeCode typeCode, int32_t byteLength); + + ::taihe::array ReadArrayBuffer(::ohos::rpc::rpc::TypeCode typeCode); + + ::taihe::array ReadInt8ArrayBuffer(); + + ::taihe::array ReadUInt8ArrayBuffer(); + + ::taihe::array ReadInt16ArrayBuffer(); + + ::taihe::array ReadUInt16ArrayBuffer(); + + ::taihe::array ReadInt32ArrayBuffer(); + + ::taihe::array ReadUInt32ArrayBuffer(); + + ::taihe::array ReadFloatArrayBuffer(); + + ::taihe::array ReadDoubleArrayBuffer(); + + ::taihe::array ReadInt64ArrayBuffer(); + + ::taihe::array ReadUInt64ArrayBuffer(); + + int64_t GetSize(); + + int64_t GetWritableBytes(); + + int64_t GetReadableBytes(); + + int64_t GetReadPosition(); + + int64_t GetWritePosition(); + + bool ContainFileDescriptors(); + + int64_t GetRawDataCapacity(); + void RewindRead(int64_t pos); + void RewindWrite(int64_t pos); + void SetSize(int64_t size); + ::taihe::array<::ohos::rpc::rpc::IRemoteObjectUnion> ReadRemoteObjectArrayImpl(); + ::taihe::array ReadByteArrayGet(); + void ReadByteArrayIn(::taihe::array_view dataIn); + + void WriteByte(int8_t val); + + void WriteShort(int16_t val); + + void WriteFloat(double val); + + static int32_t DupFileDescriptor(int32_t fd); static ::ohos::rpc::rpc::MessageSequence RpcTransferStaicImpl(uintptr_t input); static uintptr_t RpcTransferDynamicImpl(::ohos::rpc::rpc::MessageSequence obj); static void CreateJsMessageSequence(napi_env jsenv, napi_status status, diff --git a/ipc/native/src/taihe/src/ohos.rpc.rpc.impl.cpp b/ipc/native/src/taihe/src/ohos.rpc.rpc.impl.cpp index f03f9753..f17b9a42 100644 --- a/ipc/native/src/taihe/src/ohos.rpc.rpc.impl.cpp +++ b/ipc/native/src/taihe/src/ohos.rpc.rpc.impl.cpp @@ -16,6 +16,7 @@ #include "ohos.rpc.rpc.proj.hpp" #include "ohos.rpc.rpc.impl.hpp" #include "taihe/runtime.hpp" +#include "ohos.rpc.rpc.TypeCode.proj.1.hpp" #include "stdexcept" #include @@ -49,7 +50,9 @@ namespace OHOS { static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, OHOS::LOG_ID_IPC_OTHER, "RpcTaiheImpl" }; +static constexpr int MAP_PROT_MAX = AshmemImpl::PROT_EXEC | AshmemImpl::PROT_READ | AshmemImpl::PROT_WRITE; constexpr size_t MAX_BYTES_LENGTH = 40960; +constexpr size_t BYTE_SIZE_64 = 8; constexpr size_t BYTE_SIZE_32 = 4; constexpr size_t BYTE_SIZE_16 = 2; constexpr size_t BYTE_SIZE_8 = 1; @@ -358,6 +361,81 @@ int32_t AshmemImpl::GetAshmemSize() return ashmem_->GetAshmemSize(); } +void AshmemImpl::SetProtectionType(int32_t protectionType) +{ + CHECK_NATIVE_OBJECT(ashmem_, OHOS::RpcTaiheErrorCode::TAIHE_OS_MMAP_ERROR); + ashmem_->SetProtection(protectionType); +} + +void AshmemImpl::MapReadonlyAshmem() +{ + CHECK_NATIVE_OBJECT(ashmem_, OHOS::RpcTaiheErrorCode::TAIHE_OS_MMAP_ERROR); + ashmem_->MapReadOnlyAshmem(); +} + +void AshmemImpl::MapTypedAshmem(int32_t mapType) +{ + CHECK_NATIVE_OBJECT(ashmem_, OHOS::RpcTaiheErrorCode::TAIHE_OS_MMAP_ERROR); + if (mapType > MAP_PROT_MAX) { + ZLOGE(LOG_LABEL, "napiAshmem mapType error"); + return; + } + ashmem_->MapAshmem(mapType); +} + +void AshmemImpl::CloseAshmem() +{ + CHECK_NATIVE_OBJECT(ashmem_, OHOS::RpcTaiheErrorCode::TAIHE_OS_MMAP_ERROR); + ashmem_->CloseAshmem(); +} + +void AshmemImpl::UnmapAshmem() +{ + CHECK_NATIVE_OBJECT(ashmem_, OHOS::RpcTaiheErrorCode::TAIHE_OS_MMAP_ERROR); + ashmem_->UnmapAshmem(); +} + +::taihe::array AshmemImpl::ReadDataFromAshmem(int32_t size, int32_t offset) +{ + CHECK_NATIVE_OBJECT_WITH_RETVAL(ashmem_, + OHOS::RpcTaiheErrorCode::TAIHE_READ_FROM_ASHMEM_ERROR, ::taihe::array(nullptr, 0)); + uint32_t ashmemSize = (uint32_t)GetAshmemSize(); + if (size <= 0 || size > std::numeric_limits::max() || + offset < 0 || offset > std::numeric_limits::max() || + (size + offset) > ashmemSize) { + ZLOGE(LOG_LABEL, "invalid parameter, size:%{public}d offset:%{public}d", size, offset); + RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::TAIHE_CHECK_PARAM_ERROR, + ::taihe::array(nullptr, 0)); + } + const void *rawData = ashmem_->ReadFromAshmem(size, offset); + if (rawData == nullptr) { + ZLOGE(LOG_LABEL, "rawData is null"); + RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::TAIHE_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, + ::taihe::array(nullptr, 0)); + } + const uint8_t* bytePtr = static_cast(rawData); + std::vector res(size); + std::copy(bytePtr, bytePtr + size, res.begin()); + return ::taihe::array(res); +} + +void AshmemImpl::WriteDataToAshmem(::taihe::array_view buf, int32_t size, int32_t offset) +{ + CHECK_NATIVE_OBJECT(ashmem_, OHOS::RpcTaiheErrorCode::TAIHE_OS_MMAP_ERROR); + uint32_t ashmemSize = (uint32_t)GetAshmemSize(); + if (size <= 0 || size > std::numeric_limits::max() || + offset < 0 || offset > std::numeric_limits::max() || + (size + offset) > ashmemSize) { + ZLOGE(LOG_LABEL, "invalid parameter, size:%{public}d offset:%{public}d", size, offset); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::TAIHE_CHECK_PARAM_ERROR); + return; + } + if (!ashmem_->WriteToAshmem(static_cast(buf.data()), size, offset)) { + ZLOGE(LOG_LABEL, "write data failed"); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::TAIHE_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + } +} + OHOS::sptr AshmemImpl::GetAshmem() { return ashmem_; @@ -844,6 +922,16 @@ void MessageSequenceImpl::WriteBoolean(bool val) } } +void MessageSequenceImpl::WriteChar(uint8_t val) +{ + CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::TAIHE_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + bool result = nativeParcel_->WriteUint8(val); + if (!result) { + ZLOGE(LOG_LABEL, "write uint8 failed"); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::TAIHE_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + } +} + void MessageSequenceImpl::WriteString(::taihe::string_view val) { if (val.size() > MAX_BYTES_LENGTH) { @@ -895,6 +983,28 @@ void MessageSequenceImpl::WriteByteArray(::taihe::array_view byteArray) } } +void MessageSequenceImpl::WriteShortArray(::taihe::array_view shortArray) +{ + CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::TAIHE_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + uint32_t arrayLength = shortArray.size(); + if (arrayLength == 0) { + ZLOGE(LOG_LABEL, "arrayLength is 0"); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::TAIHE_CHECK_PARAM_ERROR); + } + CHECK_WRITE_CAPACITY(BYTE_SIZE_32 + sizeof(int16_t) * arrayLength, nativeParcel_, nativeParcel_->GetMaxCapacity()); + size_t pos = nativeParcel_->GetWritePosition(); + nativeParcel_->WriteUint32(arrayLength); + bool result = false; + for (size_t i = 0; i < arrayLength; i++) { + result = nativeParcel_->WriteInt16(shortArray[i]); + if (!result) { + nativeParcel_->RewindWrite(pos); + ZLOGE(LOG_LABEL, "write int16 failed"); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::TAIHE_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + } + } +} + void MessageSequenceImpl::WriteIntArray(::taihe::array_view intArray) { CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::TAIHE_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); @@ -917,6 +1027,50 @@ void MessageSequenceImpl::WriteIntArray(::taihe::array_view intArray) } } +void MessageSequenceImpl::WriteLongArray(::taihe::array_view longArray) +{ + CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::TAIHE_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + uint32_t arrayLength = longArray.size(); + if (arrayLength == 0) { + ZLOGE(LOG_LABEL, "arrayLength is 0"); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::TAIHE_CHECK_PARAM_ERROR); + } + CHECK_WRITE_CAPACITY(BYTE_SIZE_32 + sizeof(int64_t) * arrayLength, nativeParcel_, nativeParcel_->GetMaxCapacity()); + size_t pos = nativeParcel_->GetWritePosition(); + nativeParcel_->WriteUint32(arrayLength); + bool result = false; + for (size_t i = 0; i < arrayLength; i++) { + result = nativeParcel_->WriteInt64(longArray[i]); + if (!result) { + nativeParcel_->RewindWrite(pos); + ZLOGE(LOG_LABEL, "write int64 failed"); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::TAIHE_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + } + } +} + +void MessageSequenceImpl::WriteFloatArray(::taihe::array_view floatArray) +{ + CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::TAIHE_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + uint32_t arrayLength = floatArray.size(); + if (arrayLength == 0) { + ZLOGE(LOG_LABEL, "arrayLength is 0"); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::TAIHE_CHECK_PARAM_ERROR); + } + CHECK_WRITE_CAPACITY(BYTE_SIZE_32 + sizeof(float) * arrayLength, nativeParcel_, nativeParcel_->GetMaxCapacity()); + size_t pos = nativeParcel_->GetWritePosition(); + nativeParcel_->WriteUint32(arrayLength); + bool result = false; + for (size_t i = 0; i < arrayLength; i++) { + result = nativeParcel_->WriteDouble(static_cast(floatArray[i])); + if (!result) { + nativeParcel_->RewindWrite(pos); + ZLOGE(LOG_LABEL, "write float failed"); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::TAIHE_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + } + } +} + void MessageSequenceImpl::WriteDoubleArray(::taihe::array_view doubleArray) { CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::TAIHE_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); @@ -961,6 +1115,28 @@ void MessageSequenceImpl::WriteBooleanArray(::taihe::array_view booleanArr } } +void MessageSequenceImpl::WriteCharArray(::taihe::array_view charArray) +{ + CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::TAIHE_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + uint32_t arrayLength = charArray.size(); + if (arrayLength == 0) { + ZLOGE(LOG_LABEL, "arrayLength is 0"); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::TAIHE_CHECK_PARAM_ERROR); + } + CHECK_WRITE_CAPACITY(BYTE_SIZE_32 + sizeof(uint8_t) * arrayLength, nativeParcel_, nativeParcel_->GetMaxCapacity()); + size_t pos = nativeParcel_->GetWritePosition(); + nativeParcel_->WriteUint32(arrayLength); + bool result = false; + for (size_t i = 0; i < arrayLength; i++) { + result = nativeParcel_->WriteUint8(static_cast(charArray[i])); + if (!result) { + nativeParcel_->RewindWrite(pos); + ZLOGE(LOG_LABEL, "write uint8 failed"); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::TAIHE_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + } + } +} + void MessageSequenceImpl::WriteStringArray(::taihe::array_view<::taihe::string> stringArray) { CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::TAIHE_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); @@ -1014,6 +1190,52 @@ void MessageSequenceImpl::WriteParcelableArray(::taihe::array_view<::ohos::rpc:: } } +void MessageSequenceImpl::WriteRemoteObjectArray(::taihe::array_view<::ohos::rpc::rpc::IRemoteObjectUnion> objectArray) +{ + CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::TAIHE_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + uint32_t arrayLength = objectArray.size(); + if (arrayLength == 0) { + ZLOGE(LOG_LABEL, "arrayLength is 0"); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::TAIHE_CHECK_PARAM_ERROR); + } + CHECK_WRITE_CAPACITY(BYTE_SIZE_32 + sizeof(::ohos::rpc::rpc::IRemoteObjectUnion) * arrayLength, + nativeParcel_, nativeParcel_->GetMaxCapacity()); + size_t pos = nativeParcel_->GetWritePosition(); + if (!(nativeParcel_->WriteUint32(arrayLength))) { + ZLOGE(LOG_LABEL, "write array length failed"); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::TAIHE_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + } + for (size_t i = 0; i < arrayLength; i++) { + if (objectArray[i].get_tag() == ::ohos::rpc::rpc::IRemoteObjectUnion::tag_t::remoteObject) { + auto &remoteStub = objectArray[i].get_remoteObject_ref(); + OHOS::sptr nativeStub = + reinterpret_cast(remoteStub->GetNativePtr()); + if (nativeStub == nullptr) { + ZLOGE(LOG_LABEL, "reinterpret_cast to IRemoteObject failed"); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::TAIHE_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + } + bool result = nativeParcel_->WriteRemoteObject(nativeStub); + if (!result) { + ZLOGE(LOG_LABEL, "write RemoteObject failed"); + nativeParcel_->RewindWrite(pos); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::TAIHE_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + } + } else if (objectArray[i].get_tag() == ::ohos::rpc::rpc::IRemoteObjectUnion::tag_t::remoteProxy) { + auto &remoteProxy = objectArray[i].get_remoteProxy_ref(); + auto nativeProxy = reinterpret_cast(remoteProxy->GetNativePtr()); + bool result = nativeParcel_->WriteRemoteObject(nativeProxy); + if (!result) { + ZLOGE(LOG_LABEL, "write RemoteProxy failed"); + nativeParcel_->RewindWrite(pos); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::TAIHE_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + } + } else { + ZLOGE(LOG_LABEL, "unknown tag: %{public}d", objectArray[i].get_tag()); + TH_THROW(std::runtime_error, "unknown tag"); + } + } +} + int32_t MessageSequenceImpl::ReadInt() { CHECK_NATIVE_OBJECT_WITH_RETVAL(nativeParcel_, @@ -1154,6 +1376,92 @@ void MessageSequenceImpl::ReadParcelable(::ohos::rpc::rpc::weak::Parcelable data return ::taihe::array<::taihe::string>(taihe::copy_data_t{}, res.data(), res.size()); } +::taihe::array MessageSequenceImpl::ReadCharArrayImpl() +{ + CHECK_NATIVE_OBJECT_WITH_RETVAL(nativeParcel_, OHOS::RpcTaiheErrorCode::TAIHE_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, + ::taihe::array(nullptr, 0)); + uint32_t arrayLength = nativeParcel_->ReadUint32(); + std::vector res; + for (uint32_t i = 0; i < arrayLength; i++) { + uint8_t val = nativeParcel_->ReadUint8(); + res.push_back(static_cast(val)); + } + return ::taihe::array(res); +} + +::taihe::array MessageSequenceImpl::ReadFloatArrayImpl() +{ + CHECK_NATIVE_OBJECT_WITH_RETVAL(nativeParcel_, OHOS::RpcTaiheErrorCode::TAIHE_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, + ::taihe::array(nullptr, 0)); + int32_t arrayLength = nativeParcel_->ReadInt32(); + std::vector res; + for (uint32_t i = 0; i < static_cast(arrayLength); i++) { + res.push_back(nativeParcel_->ReadDouble()); + } + return ::taihe::array(res); +} + +::taihe::array MessageSequenceImpl::ReadLongArrayImpl() +{ + CHECK_NATIVE_OBJECT_WITH_RETVAL(nativeParcel_, OHOS::RpcTaiheErrorCode::TAIHE_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, + ::taihe::array(nullptr, 0)); + int32_t arrayLength = nativeParcel_->ReadInt32(); + std::vector res; + for (uint32_t i = 0; i < static_cast(arrayLength); i++) { + res.push_back(nativeParcel_->ReadInt64()); + } + return ::taihe::array(res); +} + +::taihe::array MessageSequenceImpl::ReadShortArrayImpl() +{ + CHECK_NATIVE_OBJECT_WITH_RETVAL(nativeParcel_, OHOS::RpcTaiheErrorCode::TAIHE_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, + ::taihe::array(nullptr, 0)); + int32_t arrayLength = nativeParcel_->ReadInt32(); + std::vector res; + for (uint32_t i = 0; i < static_cast(arrayLength); i++) { + res.push_back(nativeParcel_->ReadInt16()); + } + return ::taihe::array(res); +} + +int16_t MessageSequenceImpl::ReadChar() +{ + CHECK_NATIVE_OBJECT_WITH_RETVAL(nativeParcel_, + OHOS::RpcTaiheErrorCode::TAIHE_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, 0); + uint8_t value = nativeParcel_->ReadUint8(); + return static_cast(value); +} + +double MessageSequenceImpl::ReadFloat() +{ + CHECK_NATIVE_OBJECT_WITH_RETVAL(nativeParcel_, + OHOS::RpcTaiheErrorCode::TAIHE_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, 0); + double value = nativeParcel_->ReadDouble(); + return value; +} + +double MessageSequenceImpl::ReadDouble() +{ + return ReadFloat(); +} + +int16_t MessageSequenceImpl::ReadShort() +{ + CHECK_NATIVE_OBJECT_WITH_RETVAL(nativeParcel_, + OHOS::RpcTaiheErrorCode::TAIHE_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, 0); + int16_t value = nativeParcel_->ReadInt16(); + return value; +} + +int8_t MessageSequenceImpl::ReadByte() +{ + CHECK_NATIVE_OBJECT_WITH_RETVAL(nativeParcel_, + OHOS::RpcTaiheErrorCode::TAIHE_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, 0); + int8_t value = nativeParcel_->ReadInt8(); + return value; +} + void MessageSequenceImpl::ReadParcelableArray(::taihe::array_view<::ohos::rpc::rpc::Parcelable> parcelableArray) { CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::TAIHE_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR); @@ -1270,6 +1578,15 @@ void MessageSequenceImpl::AddJsObjWeakRef(::ohos::rpc::rpc::weak::MessageSequenc return obj; } +int32_t MessageSequenceImpl::DupFileDescriptor(int32_t fd) +{ + if (fd < 0) { + ZLOGE(LOG_LABEL, "invalid fd:%{public}d", fd); + RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::TAIHE_CHECK_PARAM_ERROR, -1); + } + return dup(fd); +} + void MessageSequenceImpl::CloseFileDescriptor(int32_t fd) { if (fd < 0) { @@ -1279,6 +1596,366 @@ void MessageSequenceImpl::CloseFileDescriptor(int32_t fd) close(fd); } +void MessageSequenceImpl::WriteArrayBuffer(::taihe::array_view buf, ::ohos::rpc::rpc::TypeCode typeCode) +{ + CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::TAIHE_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + if (typeCode.get_key() < ohos::rpc::rpc::TypeCode::key_t::INT8_ARRAY + || typeCode.get_key() > ohos::rpc::rpc::TypeCode::key_t::BIGUINT64_ARRAY) { + ZLOGE(LOG_LABEL, "typeCode is out of range. typeCode:%{public}d", typeCode.get_value()); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::TAIHE_CHECK_PARAM_ERROR); + } + int32_t byteLength = buf.size(); + void *data = nullptr; + data = static_cast(buf.data()); + if (!WriteVectorByTypeCode(data, typeCode, byteLength)) { + ZLOGE(LOG_LABEL, "write array buffer failed"); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::TAIHE_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + } +} + +template +static std::vector BufferToVector(void *data, size_t byteLength) +{ + const T* dataPtr = reinterpret_cast(data); + std::vector vec; + std::copy(dataPtr, dataPtr + byteLength / sizeof(T), std::back_inserter(vec)); + return vec; +} + +bool MessageSequenceImpl::WriteVectorByTypeCode(void *data, ::ohos::rpc::rpc::TypeCode typeCode, int32_t byteLength) +{ + switch (typeCode.get_key()) { + case ohos::rpc::rpc::TypeCode::key_t::INT8_ARRAY: { + return nativeParcel_->WriteInt8Vector(BufferToVector(data, byteLength)); + } + case ohos::rpc::rpc::TypeCode::key_t::UINT8_ARRAY: { + return nativeParcel_->WriteUInt8Vector(BufferToVector(data, byteLength)); + } + case ohos::rpc::rpc::TypeCode::key_t::INT16_ARRAY: { + return nativeParcel_->WriteInt16Vector(BufferToVector(data, byteLength)); + } + case ohos::rpc::rpc::TypeCode::key_t::UINT16_ARRAY: { + return nativeParcel_->WriteUInt16Vector(BufferToVector(data, byteLength)); + } + case ohos::rpc::rpc::TypeCode::key_t::INT32_ARRAY: { + return nativeParcel_->WriteInt32Vector(BufferToVector(data, byteLength)); + } + case ohos::rpc::rpc::TypeCode::key_t::UINT32_ARRAY: { + return nativeParcel_->WriteUInt32Vector(BufferToVector(data, byteLength)); + } + case ohos::rpc::rpc::TypeCode::key_t::FLOAT32_ARRAY: { + return nativeParcel_->WriteFloatVector(BufferToVector(data, byteLength)); + } + case ohos::rpc::rpc::TypeCode::key_t::FLOAT64_ARRAY: { + return nativeParcel_->WriteDoubleVector(BufferToVector(data, byteLength)); + } + case ohos::rpc::rpc::TypeCode::key_t::BIGINT64_ARRAY: { + return nativeParcel_->WriteInt64Vector(BufferToVector(data, byteLength)); + } + case ohos::rpc::rpc::TypeCode::key_t::BIGUINT64_ARRAY: { + return nativeParcel_->WriteUInt64Vector(BufferToVector(data, byteLength)); + } + default: + ZLOGE(LOG_LABEL, "unsupported typeCode:%{public}d", typeCode.get_value()); + return false; + } +} + +::taihe::array MessageSequenceImpl::ReadArrayBuffer(::ohos::rpc::rpc::TypeCode typeCode) +{ + CHECK_NATIVE_OBJECT_WITH_RETVAL(nativeParcel_, + OHOS::RpcTaiheErrorCode::TAIHE_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, ::taihe::array(nullptr, 0)); + if (typeCode.get_key() < ohos::rpc::rpc::TypeCode::key_t::INT8_ARRAY + || typeCode.get_key() > ohos::rpc::rpc::TypeCode::key_t::BIGUINT64_ARRAY) { + ZLOGE(LOG_LABEL, "typeCode is out of range. typeCode:%{public}d", typeCode.get_value()); + RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::TAIHE_CHECK_PARAM_ERROR, + ::taihe::array(nullptr, 0)); + } + switch (typeCode.get_key()) { + case ohos::rpc::rpc::TypeCode::key_t::INT8_ARRAY: { + return ReadInt8ArrayBuffer(); + } + case ohos::rpc::rpc::TypeCode::key_t::UINT8_ARRAY: { + return ReadUInt8ArrayBuffer(); + } + case ohos::rpc::rpc::TypeCode::key_t::INT16_ARRAY: { + return ReadInt16ArrayBuffer(); + } + case ohos::rpc::rpc::TypeCode::key_t::UINT16_ARRAY: { + return ReadUInt16ArrayBuffer(); + } + case ohos::rpc::rpc::TypeCode::key_t::INT32_ARRAY: { + return ReadInt32ArrayBuffer(); + } + case ohos::rpc::rpc::TypeCode::key_t::UINT32_ARRAY: { + return ReadUInt32ArrayBuffer(); + } + case ohos::rpc::rpc::TypeCode::key_t::FLOAT32_ARRAY: { + return ReadFloatArrayBuffer(); + } + case ohos::rpc::rpc::TypeCode::key_t::FLOAT64_ARRAY: { + return ReadDoubleArrayBuffer(); + } + case ohos::rpc::rpc::TypeCode::key_t::BIGINT64_ARRAY: { + return ReadInt64ArrayBuffer(); + } + case ohos::rpc::rpc::TypeCode::key_t::BIGUINT64_ARRAY: { + return ReadUInt64ArrayBuffer(); + } + default: + RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::TAIHE_CHECK_PARAM_ERROR, + ::taihe::array(nullptr, 0)); + } +} + +::taihe::array MessageSequenceImpl::ReadInt8ArrayBuffer() +{ + std::vector int8Vector; + if (!nativeParcel_->ReadInt8Vector(&int8Vector)) { + ZLOGE(LOG_LABEL, "read Int8Vector failed"); + RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::TAIHE_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, + ::taihe::array(nullptr, 0)); + } + void *vec = nullptr; + vec = static_cast(int8Vector.data()); + int32_t byteLength = int8Vector.size(); + + std::vector ret; + ret = BufferToVector(vec, byteLength); + return ::taihe::array(ret); +} + +::taihe::array MessageSequenceImpl::ReadUInt8ArrayBuffer() +{ + std::vector uint8Vector; + if (!nativeParcel_->ReadUInt8Vector(&uint8Vector)) { + ZLOGE(LOG_LABEL, "read int16Vector failed"); + RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::TAIHE_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, + ::taihe::array(nullptr, 0)); + } + return ::taihe::array(uint8Vector); +} + +::taihe::array MessageSequenceImpl::ReadInt16ArrayBuffer() +{ + std::vector int16Vector; + if (!nativeParcel_->ReadInt16Vector(&int16Vector)) { + ZLOGE(LOG_LABEL, "read int16Vector failed"); + RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::TAIHE_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, + ::taihe::array(nullptr, 0)); + } + void *vec = nullptr; + vec = static_cast(int16Vector.data()); + int32_t byteLength = int16Vector.size() * BYTE_SIZE_16; + std::vector ret; + ret = BufferToVector(vec, byteLength); + return ::taihe::array(ret); +} + +::taihe::array MessageSequenceImpl::ReadUInt16ArrayBuffer() +{ + std::vector uint16Vector; + if (!nativeParcel_->ReadUInt16Vector(&uint16Vector)) { + ZLOGE(LOG_LABEL, "read uint16Vector failed"); + RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::TAIHE_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, + ::taihe::array(nullptr, 0)); + } + void *vec = nullptr; + vec = static_cast(uint16Vector.data()); + int32_t byteLength = uint16Vector.size() * BYTE_SIZE_16; + std::vector ret; + ret = BufferToVector(vec, byteLength); + return ::taihe::array(ret); +} + +::taihe::array MessageSequenceImpl::ReadInt32ArrayBuffer() +{ + std::vector int32Vector; + if (!nativeParcel_->ReadInt32Vector(&int32Vector)) { + ZLOGE(LOG_LABEL, "read int32Vector failed"); + RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::TAIHE_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, + ::taihe::array(nullptr, 0)); + } + void *vec = nullptr; + vec = static_cast(int32Vector.data()); + int32_t byteLength = int32Vector.size() * BYTE_SIZE_32; + std::vector ret; + ret = BufferToVector(vec, byteLength); + return ::taihe::array(ret); +} + +::taihe::array MessageSequenceImpl::ReadUInt32ArrayBuffer() +{ + std::vector uint32Vector; + if (!nativeParcel_->ReadUInt32Vector(&uint32Vector)) { + ZLOGE(LOG_LABEL, "read uint32Vector failed"); + RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::TAIHE_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, + ::taihe::array(nullptr, 0)); + } + void *vec = nullptr; + vec = static_cast(uint32Vector.data()); + int32_t byteLength = uint32Vector.size() * BYTE_SIZE_32; + std::vector ret; + ret = BufferToVector(vec, byteLength); + return ::taihe::array(ret); +} + +::taihe::array MessageSequenceImpl::ReadFloatArrayBuffer() +{ + std::vector floatVector; + if (!nativeParcel_->ReadFloatVector(&floatVector)) { + ZLOGE(LOG_LABEL, "read floatVector failed"); + RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::TAIHE_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, + ::taihe::array(nullptr, 0)); + } + void *vec = nullptr; + vec = static_cast(floatVector.data()); + int32_t byteLength = floatVector.size() * BYTE_SIZE_32; + std::vector ret; + ret = BufferToVector(vec, byteLength); + return ::taihe::array(ret); +} + +::taihe::array MessageSequenceImpl::ReadDoubleArrayBuffer() +{ + std::vector doubleVector; + if (!nativeParcel_->ReadDoubleVector(&doubleVector)) { + ZLOGE(LOG_LABEL, "read doubleVector failed"); + RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::TAIHE_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, + ::taihe::array(nullptr, 0)); + } + void *vec = nullptr; + vec = static_cast(doubleVector.data()); + int32_t byteLength = doubleVector.size() * BYTE_SIZE_64; + std::vector ret; + ret = BufferToVector(vec, byteLength); + return ::taihe::array(ret); +} + +::taihe::array MessageSequenceImpl::ReadInt64ArrayBuffer() +{ + std::vector int64Vector; + if (!nativeParcel_->ReadInt64Vector(&int64Vector)) { + ZLOGE(LOG_LABEL, "read int64Vector failed"); + RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::TAIHE_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, + ::taihe::array(nullptr, 0)); + } + void *vec = nullptr; + vec = static_cast(int64Vector.data()); + int32_t byteLength = int64Vector.size() * BYTE_SIZE_64; + std::vector ret; + ret = BufferToVector(vec, byteLength); + return ::taihe::array(ret); +} + +::taihe::array MessageSequenceImpl::ReadUInt64ArrayBuffer() +{ + std::vector uint64Vector; + if (!nativeParcel_->ReadUInt64Vector(&uint64Vector)) { + ZLOGE(LOG_LABEL, "read uint64Vector failed"); + RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::TAIHE_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, + ::taihe::array(nullptr, 0)); + } + void *vec = nullptr; + vec = static_cast(uint64Vector.data()); + int32_t byteLength = uint64Vector.size() * BYTE_SIZE_64; + std::vector ret; + ret = BufferToVector(vec, byteLength); + return ::taihe::array(ret); +} + +int64_t MessageSequenceImpl::GetSize() +{ + CHECK_NATIVE_OBJECT_WITH_RETVAL(nativeParcel_, + OHOS::RpcTaiheErrorCode::TAIHE_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, 0); + int64_t result = static_cast(nativeParcel_->GetDataSize()); + return result; +} + +int64_t MessageSequenceImpl::GetWritableBytes() +{ + CHECK_NATIVE_OBJECT_WITH_RETVAL(nativeParcel_, + OHOS::RpcTaiheErrorCode::TAIHE_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, 0); + int64_t result = static_cast(nativeParcel_->GetWritableBytes()); + return result; +} + +int64_t MessageSequenceImpl::GetReadableBytes() +{ + CHECK_NATIVE_OBJECT_WITH_RETVAL(nativeParcel_, + OHOS::RpcTaiheErrorCode::TAIHE_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, 0); + int64_t result = static_cast(nativeParcel_->GetReadableBytes()); + return result; +} + +int64_t MessageSequenceImpl::GetReadPosition() +{ + CHECK_NATIVE_OBJECT_WITH_RETVAL(nativeParcel_, + OHOS::RpcTaiheErrorCode::TAIHE_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, 0); + int64_t result = static_cast(nativeParcel_->GetReadPosition()); + return result; +} + +int64_t MessageSequenceImpl::GetWritePosition() +{ + CHECK_NATIVE_OBJECT_WITH_RETVAL(nativeParcel_, + OHOS::RpcTaiheErrorCode::TAIHE_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, 0); + int64_t result = static_cast(nativeParcel_->GetWritePosition()); + return result; +} + +bool MessageSequenceImpl::ContainFileDescriptors() +{ + bool result = nativeParcel_->GetWritePosition(); + return result; +} + +int64_t MessageSequenceImpl::GetRawDataCapacity() +{ + int64_t result = static_cast(nativeParcel_->GetRawDataCapacity()); + return result; +} + +void MessageSequenceImpl::WriteByte(int8_t val) +{ + CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::TAIHE_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + bool result = nativeParcel_->WriteInt8(val); + if (!result) { + ZLOGE(LOG_LABEL, "write int8 failed"); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::TAIHE_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + } +} + +void MessageSequenceImpl::WriteShort(int16_t val) +{ + CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::TAIHE_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + bool result = nativeParcel_->WriteInt16(val); + if (!result) { + ZLOGE(LOG_LABEL, "write int16 failed"); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::TAIHE_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + } +} + +void MessageSequenceImpl::WriteFloat(double val) +{ + CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::TAIHE_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + bool result = nativeParcel_->WriteDouble(val); + if (!result) { + ZLOGE(LOG_LABEL, "write float failed"); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::TAIHE_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + } +} + +void MessageSequenceImpl::WriteDouble(double val) +{ + CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::TAIHE_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + bool result = nativeParcel_->WriteDouble(val); + if (!result) { + ZLOGE(LOG_LABEL, "write double failed"); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::TAIHE_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + } +} + // MessageOptionImpl MessageOptionImpl::MessageOptionImpl(int32_t syncFlags, int32_t waitTime) { @@ -1311,6 +1988,34 @@ int64_t MessageOptionImpl::GetNativePtr() return reinterpret_cast(messageOption_.get()); } +int32_t MessageOptionImpl::GetFlags() +{ + CHECK_NATIVE_OBJECT_WITH_RETVAL(messageOption_, + OHOS::RpcTaiheErrorCode::TAIHE_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, 0); + return messageOption_->GetFlags(); +} + +void MessageOptionImpl::SetFlags(int32_t flags) +{ + CHECK_NATIVE_OBJECT(messageOption_, + OHOS::RpcTaiheErrorCode::TAIHE_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR); + messageOption_->SetFlags(flags); +} + +int32_t MessageOptionImpl::GetWaitTime() +{ + CHECK_NATIVE_OBJECT_WITH_RETVAL(messageOption_, + OHOS::RpcTaiheErrorCode::TAIHE_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, 0); + return messageOption_->GetWaitTime(); +} + +void MessageOptionImpl::SetWaitTime(int32_t waitTime) +{ + CHECK_NATIVE_OBJECT(messageOption_, + OHOS::RpcTaiheErrorCode::TAIHE_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR); + messageOption_->SetWaitTime(waitTime); +} + void MessageOptionImpl::AddJsObjWeakRef(::ohos::rpc::rpc::weak::MessageOption obj) { jsObjRef_ = std::optional<::ohos::rpc::rpc::MessageOption>(std::in_place, obj); @@ -1352,6 +2057,109 @@ int64_t IPCSkeletonImpl::GetCallingTokenId() return static_cast(OHOS::IPCSkeleton::GetCallingTokenID()); } +::taihe::string IPCSkeletonImpl::ResetCallingIdentity() +{ + return static_cast<::taihe::string>(OHOS::IPCSkeleton::ResetCallingIdentity()); +} + +void IPCSkeletonImpl::RestoreCallingIdentity(::taihe::string_view identity) +{ + std::string temp = std::string(identity); + size_t maxLen = 40960; + if (temp.size() >= maxLen) { + ZLOGE(LOG_LABEL, "string length too large"); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::TAIHE_CHECK_PARAM_ERROR); + } + OHOS::IPCSkeleton::SetCallingIdentity(temp); +} + +void IPCSkeletonImpl::FlushCmdBuffer(::ohos::rpc::rpc::IRemoteObjectUnion const& object) +{ + if (object.get_tag() == ::ohos::rpc::rpc::IRemoteObjectUnion::tag_t::remoteObject) { + auto &remoteStub = object.get_remoteObject_ref(); + OHOS::sptr nativeStub = + reinterpret_cast(remoteStub->GetNativePtr()); + if (nativeStub == nullptr) { + ZLOGE(LOG_LABEL, "reinterpret_cast to IRemoteObject failed"); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::TAIHE_CHECK_PARAM_ERROR); + } + OHOS::IPCSkeleton::FlushCommands(nativeStub); + } else if (object.get_tag() == ::ohos::rpc::rpc::IRemoteObjectUnion::tag_t::remoteProxy) { + auto &remoteProxy = object.get_remoteProxy_ref(); + auto nativeProxy = reinterpret_cast(remoteProxy->GetNativePtr()); + if (nativeProxy == nullptr) { + ZLOGE(LOG_LABEL, "reinterpret_cast to IPCObjectProxy failed"); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::TAIHE_CHECK_PARAM_ERROR); + } + OHOS::IPCSkeleton::FlushCommands(nativeProxy); + } else { + ZLOGE(LOG_LABEL, "unknown tag: %{public}d", object.get_tag()); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::TAIHE_CHECK_PARAM_ERROR); + } +} + +::taihe::string IPCSkeletonImpl::GetCallingDeviceID() +{ + return static_cast<::taihe::string>(OHOS::IPCSkeleton::GetCallingDeviceID()); +} + +::taihe::string IPCSkeletonImpl::GetLocalDeviceID() +{ + return static_cast<::taihe::string>(OHOS::IPCSkeleton::GetLocalDeviceID()); +} + +bool IPCSkeletonImpl::IsLocalCalling() +{ + return OHOS::IPCSkeleton::IsLocalCalling(); +} + +::taihe::array<::ohos::rpc::rpc::IRemoteObjectUnion> MessageSequenceImpl::ReadRemoteObjectArrayImpl() +{ + CHECK_NATIVE_OBJECT_WITH_RETVAL(nativeParcel_, + OHOS::RpcTaiheErrorCode::TAIHE_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, + (::taihe::array<::ohos::rpc::rpc::IRemoteObjectUnion>(nullptr, 0))); + int32_t arrayLength = nativeParcel_->ReadInt32(); + if (arrayLength <= 0) { + ZLOGE(LOG_LABEL, "arrayLength:%{public}d <= 0", arrayLength); + RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::TAIHE_CHECK_PARAM_ERROR, + (::taihe::array<::ohos::rpc::rpc::IRemoteObjectUnion>(nullptr, 0))); + } + if (!(nativeParcel_->WriteUint32(arrayLength))) { + ZLOGE(LOG_LABEL, "write array length failed"); + RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::TAIHE_CHECK_PARAM_ERROR, + (::taihe::array<::ohos::rpc::rpc::IRemoteObjectUnion>(nullptr, 0))); + } + std::vector<::ohos::rpc::rpc::IRemoteObjectUnion> res; + for (uint32_t i = 0; i < static_cast(arrayLength); i++) { + ::ohos::rpc::rpc::IRemoteObjectUnion temp = ReadRemoteObject(); + if (temp.get_tag() == ::ohos::rpc::rpc::IRemoteObjectUnion::tag_t::errRet) { + ZLOGE(LOG_LABEL, "read RemoteObject failed"); + RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::TAIHE_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, + (::taihe::array<::ohos::rpc::rpc::IRemoteObjectUnion>(nullptr, 0))); + } + res.push_back(temp); + } + return ::taihe::array<::ohos::rpc::rpc::IRemoteObjectUnion>(res); +} + +void MessageSequenceImpl::RewindRead(int64_t pos) +{ + CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::TAIHE_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR); + nativeParcel_->RewindRead(static_cast(pos)); +} + +void MessageSequenceImpl::RewindWrite(int64_t pos) +{ + CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::TAIHE_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR); + nativeParcel_->RewindWrite(static_cast(pos)); +} + +void MessageSequenceImpl::SetSize(int64_t size) +{ + CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::TAIHE_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); + nativeParcel_->SetDataSize(static_cast(size)); +} + ::ohos::rpc::rpc::IRemoteObjectUnion IPCSkeletonImpl::GetContextObject() { auto object = OHOS::IPCSkeleton::GetContextObject(); @@ -1359,6 +2167,51 @@ int64_t IPCSkeletonImpl::GetCallingTokenId() auto jsProxy = RemoteProxyImpl::CreateRemoteProxyFromNative(addr); return ::ohos::rpc::rpc::IRemoteObjectUnion::make_remoteProxy(jsProxy); } + +::taihe::array MessageSequenceImpl::ReadByteArrayGet() +{ + CHECK_NATIVE_OBJECT_WITH_RETVAL(nativeParcel_, + OHOS::RpcTaiheErrorCode::TAIHE_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, ::taihe::array(nullptr, 0)); + int32_t arrayLength = nativeParcel_->ReadInt32(); + if (arrayLength <= 0) { + ZLOGE(LOG_LABEL, "arrayLength:%{public}d <= 0", arrayLength); + RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::TAIHE_CHECK_PARAM_ERROR, + (::taihe::array(nullptr, 0))); + } + CHECK_READ_LENGTH_RETVAL(static_cast(arrayLength), sizeof(int8_t), + nativeParcel_, (::taihe::array(nullptr, 0))); + ::taihe::array res(arrayLength); + int8_t value = 0; + for (uint32_t i = 0; i < static_cast(arrayLength); i++) { + if (!nativeParcel_->ReadInt8(value)) { + ZLOGE(LOG_LABEL, "read int8 failed"); + RPC_TAIHE_ERROR_WITH_RETVAL(OHOS::RpcTaiheErrorCode::TAIHE_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR, + (::taihe::array(nullptr, 0))); + } + res[i] = value; + } + return res; +} + +void MessageSequenceImpl::ReadByteArrayIn(::taihe::array_view dataIn) +{ + CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::TAIHE_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR); + int32_t arrayLength = nativeParcel_->ReadInt32(); + if (arrayLength <= 0) { + ZLOGE(LOG_LABEL, "arrayLength:%{public}d <= 0", arrayLength); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::TAIHE_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR); + } + CHECK_READ_LENGTH(static_cast(arrayLength), sizeof(int8_t), nativeParcel_); + int8_t value = 0; + for (uint32_t i = 0; i < static_cast(arrayLength); i++) { + if (!nativeParcel_->ReadInt8(value)) { + ZLOGE(LOG_LABEL, "read int8 failed"); + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::TAIHE_READ_DATA_FROM_MESSAGE_SEQUENCE_ERROR); + } + dataIn[i] = value; + } + return; +} } // namespace // Since these macros are auto-generate, lint will cause false positive. @@ -1372,6 +2225,7 @@ TH_EXPORT_CPP_API_RpcTransferStaicImpl(OHOS::MessageSequenceImpl::RpcTransferSta TH_EXPORT_CPP_API_RpcTransferDynamicImpl(OHOS::MessageSequenceImpl::RpcTransferDynamicImpl); TH_EXPORT_CPP_API_CreateMessageOption_WithTwoParam(OHOS::MessageOptionImpl::CreateMessageOption_WithTwoParam); TH_EXPORT_CPP_API_CreateMessageOption_WithOneParam(OHOS::MessageOptionImpl::CreateMessageOption_WithOneParam); +TH_EXPORT_CPP_API_DupFileDescriptor(OHOS::MessageSequenceImpl::DupFileDescriptor); TH_EXPORT_CPP_API_CreateMessageOption(OHOS::MessageOptionImpl::CreateMessageOption); TH_EXPORT_CPP_API_CreateAshmem_WithTwoParam(OHOS::AshmemImpl::CreateAshmem_WithTwoParam); TH_EXPORT_CPP_API_CreateAshmem_WithOneParam(OHOS::AshmemImpl::CreateAshmem_WithOneParam); @@ -1379,6 +2233,12 @@ TH_EXPORT_CPP_API_GetCallingPid(OHOS::IPCSkeletonImpl::GetCallingPid); TH_EXPORT_CPP_API_GetCallingUid(OHOS::IPCSkeletonImpl::GetCallingUid); TH_EXPORT_CPP_API_GetCallingTokenId(OHOS::IPCSkeletonImpl::GetCallingTokenId); TH_EXPORT_CPP_API_GetContextObject(OHOS::IPCSkeletonImpl::GetContextObject); +TH_EXPORT_CPP_API_ResetCallingIdentity(OHOS::IPCSkeletonImpl::ResetCallingIdentity); +TH_EXPORT_CPP_API_RestoreCallingIdentity(OHOS::IPCSkeletonImpl::RestoreCallingIdentity); +TH_EXPORT_CPP_API_FlushCmdBuffer(OHOS::IPCSkeletonImpl::FlushCmdBuffer); +TH_EXPORT_CPP_API_GetCallingDeviceID(OHOS::IPCSkeletonImpl::GetCallingDeviceID); +TH_EXPORT_CPP_API_GetLocalDeviceID(OHOS::IPCSkeletonImpl::GetLocalDeviceID); +TH_EXPORT_CPP_API_IsLocalCalling(OHOS::IPCSkeletonImpl::IsLocalCalling); TH_EXPORT_CPP_API_unwrapRemoteObject(OHOS::unwrapRemoteObject); TH_EXPORT_CPP_API_wrapRemoteObject(OHOS::wrapRemoteObject); // NOLINTEND \ No newline at end of file diff --git a/ipc/native/src/taihe/test/test_main.ets b/ipc/native/src/taihe/test/test_main.ets index ea0c1829..cbba3493 100644 --- a/ipc/native/src/taihe/test/test_main.ets +++ b/ipc/native/src/taihe/test/test_main.ets @@ -298,26 +298,27 @@ function testMessageSequence_WriteAndRead_BooleanArray2() { console.log(`out func testMessageSequence_WriteAndRead_BooleanArray2`); } +class MyParcelable implements rpc.Parcelable { + num: int = 0; + str: string = ""; + constructor(num: int, str: string) { + this.num = num; + this.str = str; + } + marshalling(dataOut: rpc.MessageSequence) { + dataOut.writeInt(this.num); + dataOut.writeString(this.str); + return true; + } + unmarshalling(dataIn: rpc.MessageSequence) { + this.num = dataIn.readInt(); + this.str = dataIn.readString(); + return true; + } +} + function testMessageSequence_WriteAndRead_Parcelable() { console.log(`into func testMessageSequence_WriteAndRead_Parcelable`); - class MyParcelable implements rpc.Parcelable { - num: int = 0; - str: string = ""; - constructor(num: int, str: string) { - this.num = num; - this.str = str; - } - marshalling(dataOut: rpc.MessageSequence) { - dataOut.writeInt(this.num); - dataOut.writeString(this.str); - return true; - } - unmarshalling(dataIn: rpc.MessageSequence) { - this.num = dataIn.readInt(); - this.str = dataIn.readString(); - return true; - } - } let parcelable = new MyParcelable(1, "this is a test parcelable"); console.log(`parcelable gen`); @@ -339,24 +340,6 @@ function testMessageSequence_WriteAndRead_Parcelable() { function testMessageSequence_WriteAndRead_ParcelableArray() { console.log(`into func testMessageSequence_WriteAndRead_ParcelableArray`); - class MyParcelable implements rpc.Parcelable { - num: int = 0; - str: string = ""; - constructor(num: int, str: string) { - this.num = num; - this.str = str; - } - marshalling(dataOut: rpc.MessageSequence) { - dataOut.writeInt(this.num); - dataOut.writeString(this.str); - return true; - } - unmarshalling(dataIn: rpc.MessageSequence) { - this.num = dataIn.readInt(); - this.str = dataIn.readString(); - return true; - } - } let parcelable1 = new MyParcelable(1, "this is a test parcelable and this is 1"); let parcelable2 = new MyParcelable(2, "this is a test parcelable and this is 2"); @@ -687,14 +670,15 @@ function testMessageSequence_closeFileDescriptor() { console.log(`out func testMessageSequence_closeFileDescriptor`); } -function testMessageSequence_WriteAndRead_RemoteObject() { - console.log(`into func testMessageSequence_WriteAndRead_RemoteObject`); - - class TestRemoteObject extends rpc.RemoteObject { +class TestRemoteObject extends rpc.RemoteObject { constructor(descriptor: string) { super(descriptor); } } + +function testMessageSequence_WriteAndRead_RemoteObject() { + console.log(`into func testMessageSequence_WriteAndRead_RemoteObject`); + try { let data = rpc.MessageSequence.create(); console.log(`data gen`); @@ -712,12 +696,6 @@ function testMessageSequence_WriteAndRead_RemoteObject() { function test_getcallxxx_in_remoteObject() { console.log(`into func test_getcallxxx_in_remoteObject`); - - class TestRemoteObject extends rpc.RemoteObject { - constructor(descriptor: string) { - super(descriptor); - } - } try { console.log(`tyr to zhijie on`); let testRemtoeObject = new TestRemoteObject("testObject"); @@ -737,15 +715,15 @@ function testMessageSequence_WriteAndRead_Proxy() { console.log(`out func testMessageSequence_WriteAndRead_Proxy`); } +class MyDeathRecipient implements rpc.DeathRecipient { + onRemoteDied(): void { + console.log(`into MyDeathRecipient onRemoteDied in js`); + } +} + function test_registerDeathRecipient() { console.log(`into func test_registerDeathRecipient`); - class MyDeathRecipient implements rpc.DeathRecipient { - onRemoteDied(): void { - console.log(`into MyDeathRecipient onRemoteDied in js`); - } - } - try { console.log(`new MyDeathRecipient`); let deathRecipient = new MyDeathRecipient(); @@ -766,31 +744,29 @@ function test_registerDeathRecipient() { console.log(`out func test_registerDeathRecipient`); } -function test_RemoteObject_SendMessageRequest() { - console.log(`into func test_RemoteObject_SendMessageRequest`); - - class TestRemoteObject extends rpc.RemoteObject { - constructor(descriptor: string) { - super(descriptor); - } +class TestRemoteObject extends rpc.RemoteObject { + constructor(descriptor: string) { + super(descriptor); + } - onRemoteMessageRequest(code: int, data: rpc.MessageSequence, reply: rpc.MessageSequence, options: rpc.MessageOption): boolean { - console.log(`into onRemoteMessageRequest`); - if (code == 1) { - console.log(`code is 1`); - let res = data.readInt(); - console.log(`in onRemoteMessageRequest readInt success, res is : `, res); - reply.writeInt(999); - console.log(`in onRemoteMessageRequest writeInt success`); - return true; - } else { - console.log(`code others: `, code); - return false; - } + onRemoteMessageRequest(code: int, data: rpc.MessageSequence, reply: rpc.MessageSequence, options: rpc.MessageOption): boolean { + console.log(`into onRemoteMessageRequest`); + if (code == 1) { + console.log(`code is 1`); + let res = data.readInt(); + console.log(`in onRemoteMessageRequest readInt success, res is : `, res); + reply.writeInt(999); + console.log(`in onRemoteMessageRequest writeInt success`); + return true; + } else { + console.log(`code others: `, code); + return false; } } +} - +function test_RemoteObject_SendMessageRequest() { + console.log(`into func test_RemoteObject_SendMessageRequest`); try { let testRemtoeObject = new TestRemoteObject("testObject"); @@ -818,6 +794,968 @@ function test_RemoteObject_SendMessageRequest() { console.log(`out func test_RemoteObject_SendMessageRequest`); } +function testMessageSequence_ReadCharArray() { + console.println(`into func testMessageSequence_ReadCharArray`); + let data = rpc.MessageSequence.create(); + console.println(`data gen`); + try { + let res = data.readCharArray(); + console.println(`readCharArray success`); + } catch (err) { + console.println(`catch error ${err}`); + } + + console.println(`out func testMessageSequence_ReadCharArray`); +} + +function testMessageSequence_ReadFloatArray() { + console.println(`into func testMessageSequence_ReadFloatArray`); + let data = rpc.MessageSequence.create(); + console.println(`data gen`); + try { + let res = data.readFloatArray(); + console.println(`ReadFloatArray success`); + } catch (err) { + console.println(`catch error ${err}`); + } + + console.println(`out func testMessageSequence_ReadFloatArray`); +} + +function testMessageSequence_ReadLongArray() { + console.println(`into func testMessageSequence_ReadLongArray`); + let data = rpc.MessageSequence.create(); + console.println(`data gen`); + try { + let res = data.readLongArray(); + console.println(`ReadLongArray success`); + } catch (err) { + console.println(`catch error ${err}`); + } + + console.println(`out func testMessageSequence_ReadLongArray`); +} + +function testMessageSequence_ReadShortArray() { + console.println(`into func testMessageSequence_ReadShortArray`); + let data = rpc.MessageSequence.create(); + console.println(`data gen`); + try { + let res = data.readShortArray(); + console.println(`ReadShortArray success`); + } catch (err) { + console.println(`catch error ${err}`); + } + + console.println(`out func testMessageSequence_ReadShortArray`); +} + +function testMessageSequence_ReadChar() { + console.println(`into func testMessageSequence_ReadChar`); + let data = rpc.MessageSequence.create(); + console.println(`data gen`); + try { + let res = data.readChar(); + console.println(`ReadChar success`); + } catch (err) { + console.println(`catch error ${err}`); + } + + console.println(`out func testMessageSequence_ReadChar`); +} + +function testMessageSequence_ReadDouble() { + console.println(`into func testMessageSequence_ReadDouble`); + let data = rpc.MessageSequence.create(); + console.println(`data gen`); + try { + let res = data.readDouble(); + console.println(`ReadDouble success`); + } catch (err) { + console.println(`catch error ${err}`); + } + + console.println(`out func testMessageSequence_ReadDouble`); +} + +function testMessageSequence_ReadFloat() { + console.println(`into func testMessageSequence_ReadFloat`); + let data = rpc.MessageSequence.create(); + console.println(`data gen`); + try { + let res = data.readFloat(); + console.println(`ReadDouble success`); + } catch (err) { + console.println(`catch error ${err}`); + } + + console.println(`out func testMessageSequence_ReadFloat`); +} + +function testMessageSequence_ReadShort() { + console.println(`into func testMessageSequence_ReadShort`); + let data = rpc.MessageSequence.create(); + console.println(`data gen`); + try { + let res = data.readShort(); + console.println(`ReadShort success`); + } catch (err) { + console.println(`catch error ${err}`); + } + + console.println(`out func testMessageSequence_ReadShort`); +} + +function testMessageSequence_ReadByte() { + console.println(`into func testMessageSequence_ReadByte`); + let data = rpc.MessageSequence.create(); + console.println(`data gen`); + try { + let res = data.readByte(); + console.println(`ReadByte success`); + } catch (err) { + console.println(`catch error ${err}`); + } + + console.println(`out func testMessageSequence_ReadByte`); +} + +function testMessageSequence_ReadByteArray() { + console.println(`into func testMessageSequence_ReadByteArray`); + let data = rpc.MessageSequence.create(); + console.println(`data gen`); + try { + let res = data.readByteArray(); + console.println(`ReadByteArray success`); + } catch (err) { + console.println(`catch error ${err}`); + } + + console.println(`out func testMessageSequence_ReadByteArray`) +} + +function testMessageSequence_RewindWriteAndRead() { + console.println(`into func testMessageSequence_RewindWriteAndRead`); + let data = rpc.MessageSequence.create(); + console.println(`data gen`); + try { + data.rewindRead(0); + console.println(`RewindRead success`); + data.rewindWrite(0); + console.println(`RewindWrite success`); + } catch (err) { + console.println(`catch error ${err}`); + } + + console.println(`out func testMessageSequence_RewindWriteAndRead`) +} + +function testMessageSequence_SetSize() { + console.println(`into func testMessageSequence_SetSize`); + let data = rpc.MessageSequence.create(); + console.println(`data gen`); + try { + data.setSize(100000); + console.println(`SetSize success`); + } catch (err) { + console.println(`catch error ${err}`); + } + + console.println(`out func testMessageSequence_SetSize`) +} + +function testMessageSequence_duplFileDescriptor() { + console.println(`into func testMessageSequence_duplFileDescriptor`); + console.println(`data gen`); + try { + let res = rpc.MessageSequence.dupFileDescriptor(1); + console.println(`DupFileDescriptor success`); + } catch (err) { + console.println(`catch error ${err}`); + } + + console.println(`out func testMessageSequence_duplFileDescriptor`) +} + +function testMessageSequence_ReadRemoteObjectArray() { + console.println(`into func testMessageSequence_ReadRemoteObjectArray`); + let data = rpc.MessageSequence.create(); + console.println(`data gen`); + try { + //let res: array<::ohos::rpc::rpc::IRemoteObjectUnion> = data.readRemoteObjectArray(); + let res = data.readRemoteObjectArray(); + console.println(`readRemoteObjectArray success`); + } catch (err) { + console.println(`catch error ${err}`); + } + + console.println(`out func testMessageSequence_ReadRemoteObjectArray`) +} + +function testAshmen_SetProtectionType() { + console.println(`into func SetProtectionType`); + + try { + let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); + ashmem.mapReadWriteAshmem(); + ashmem.setProtectionType(3); + console.println(`SetProtectionType finshed`); + let buffer = new ArrayBuffer(1 * 10); + let int8View = new Int8Array(buffer); + for (let i = 0; i < int8View.length; i++) { + int8View[i] = i * 2 + 1; + if (i % 2 != 0) { + int8View[i] = -int8View[i]; + } + } + console.println(`write data is ${int8View}`); + let length : int = 10; + let offset : int = 0; + ashmem.writeDataToAshmem(buffer, length, offset); + console.println(`WriteDataToAshMem finshed`); + let result = ashmem.readDataFromAshmem(length, offset); + let readInt8View = new Int8Array(result); + console.println(`readDataFromAshmem success`); + console.println(`read data is ${readInt8View}`); + ashmem.closeAshmem(); + + } catch (err) { + console.println(`catch error, ${err}`); + } + + console.println(`out func SetProtectionType`); +} + +function testAshmen_MapReadOnlyAshmem() { + console.println(`into func MapReadOnlyAshmem`); + + try { + let ashmem = rpc.Ashmem.create("ashmem1", 1024*1024); + ashmem.mapTypedAshmem(7); + ashmem.mapReadonlyAshmem(); + console.println(`MapReadOnlyAshmem finshed`); + } catch (err) { + console.println(`catch error, ${err}`); + } + + console.println(`out func MapReadOnlyAshmem`); +} + +function testAshmen_MapTypedAshmem() { + console.println(`into func MapTypedAshmem`); + + try { + let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); + ashmem.mapTypedAshmem(1); + console.println(`MapTypedAshmem finshed`); + } catch (err) { + console.println(`catch error, ${err}`); + } + + console.println(`out func MapTypedAshmem`); +} + +function testAshmen_CloseAshmem() { + console.println(`into func CloseAshmem`); + + try { + let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); + ashmem.closeAshmem(); + console.println(`CloseAshmem finshed`); + } catch (err) { + console.println(`catch error, ${err}`); + } + + console.println(`out func CloseAshmem`); +} + +function testAshmen_UnMapAshmem() { + console.println(`into func UnMapAshmem`); + + try { + let ashmem = rpc.Ashmem.create("ashmem", 1024*1024); + ashmem.mapTypedAshmem(3); + console.println(`mapTypedAshmem finshed`); + ashmem.unmapAshmem(); + console.println(`UnMapAshmem finshed`); + } catch (err) { + console.println(`catch error, ${err}`); + } + + console.println(`out func UnMapAshmem`); +} + +function testMessageOption_GetFlagsAndSetFlags() { + console.println(`into func testMessageOption_GetFlagsAndSetFlags`); + let option = new rpc.MessageOption(); + + try { + let res = option.getFlags(); + console.println(`GetFlags return ${res} `); + option.setFlags(res); + console.println(`SetFlags finished`); + } catch (err) { + console.println(`catch error, ${err}`); + } + console.println(`out func testMessageOption_GetFlagsAndSetFlags`); +} + +function testMessageOption_GetWaitTimeAndSetWaitTime() { + console.println(`into func testMessageOption_GetFlags`); + let option = new rpc.MessageOption(); + + try { + let res = option.getWaitTime(); + console.println(`GetWaitTime return ${res} `); + option.setWaitTime(res); + console.println(`SetWaitTime finshed`); + } catch (err) { + console.println(`catch error, ${err}`); + } + console.println(`out func testMessageOption_GetFlags`); +} + +function testMessageSequence_GetSize() { + console.println(`into func testMessageSequence_GetSize`); + let data = rpc.MessageSequence.create(); + console.println(`data gen`); + try { + let size: long = 123; + data.setSize(size); + console.println(`setSize success`); + let res: long = data.getSize(); + console.println(`getSize success, res is ` + res); + } catch (err) { + let error: BusinessError = err as BusinessError; + console.println(`catch error, code is ${error.code}, msg is ${error.message}`); + } + console.println(`out func testMessageSequence_GetSize`); +} + +function testMessageSequence_GetWritableBytes() { + console.println(`into func testMessageSequence_GetWritableBytes`); + let data = rpc.MessageSequence.create(); + console.println(`data gen`); + try { + let size: long = data.getWritableBytes(); + console.println(`getWritableBytes success, size is ` + size); + } catch (err) { + let error: BusinessError = err as BusinessError; + console.println(`catch error, code is ${error.code}, msg is ${error.message}`); + } + console.println(`out func testMessageSequence_GetWritableBytes`); +} + +function testMessageSequence_GetReadableBytes() { + console.println(`into func testMessageSequence_GetReadableBytes`); + let data = rpc.MessageSequence.create(); + console.println(`data gen`); + try { + let size: long = data.getReadableBytes(); + console.println(`getReadableBytes success, size is ` + size); + } catch (err) { + let error: BusinessError = err as BusinessError; + console.println(`catch error, code is ${error.code}, msg is ${error.message}`); + } + console.println(`out func testMessageSequence_GetReadableBytes`); +} + +function testMessageSequence_GetReadPosition() { + console.println(`into func testMessageSequence_GetReadPosition`); + let data = rpc.MessageSequence.create(); + console.println(`data gen`); + try { + let size: long = data.getReadPosition(); + console.println(`getReadPosition success, size is ` + size); + } catch (err) { + let error: BusinessError = err as BusinessError; + console.println(`catch error, code is ${error.code}, msg is ${error.message}`); + } + console.println(`out func testMessageSequence_GetReadPosition`); +} + +function testMessageSequence_GetWritePosition() { + console.println(`into func testMessageSequence_GetWritePosition`); + let data = rpc.MessageSequence.create(); + console.println(`data gen`); + try { + let size: long = data.getWritePosition(); + console.println(`getWritePosition success, size is ` + size); + } catch (err) { + let error: BusinessError = err as BusinessError; + console.println(`catch error, code is ${error.code}, msg is ${error.message}`); + } + console.println(`out func testMessageSequence_GetWritePosition`); +} + +function testMessageSequence_ContainFileDescriptors() { + console.println(`into func testMessageSequence_ContainFileDescriptors`); + let data = rpc.MessageSequence.create(); + console.println(`data gen`); + try { + let ret: boolean = data.containFileDescriptors(); + console.println(`containFileDescriptors success, ret is ` + ret); + } catch (err) { + let error: BusinessError = err as BusinessError; + console.println(`catch error, code is ${error.code}, msg is ${error.message}`); + } + console.println(`out func testMessageSequence_ContainFileDescriptors`); +} + +function testMessageSequence_GetRawDataCapacity() { + console.println(`into func testMessageSequence_GetRawDataCapacity`); + let data = rpc.MessageSequence.create(); + console.println(`data gen`); + try { + let capacity: long = data.getRawDataCapacity(); + console.println(`getRawDataCapacity success, capacity is ` + capacity); + } catch (err) { + let error: BusinessError = err as BusinessError; + console.println(`catch error, code is ${error.code}, msg is ${error.message}`); + } + console.println(`out func testMessageSequence_GetRawDataCapacity`); +} + +function testMessageSequence_WriteByte() { + console.println(`into func testMessageSequence_WriteByte`); + let data = rpc.MessageSequence.create(); + console.println(`data gen`); + try { + data.writeByte(97); + let res = data.readByte(); + console.println(`readByte success, res is ` + res); + } catch (err) { + let error: BusinessError = err as BusinessError; + console.println(`catch error, code is ${error.code}, msg is ${error.message}`); + } + console.println(`out func testMessageSequence_WriteByte`); +} + +function testMessageSequence_WriteShort() { + console.println(`into func testMessageSequence_WriteShort`); + let data = rpc.MessageSequence.create(); + console.println(`data gen`); + try { + //let test : short = 123; + data.writeShort(123); + let res = data.readShort(); + console.println(`readShort success, res is ` + res); + } catch (err) { + let error: BusinessError = err as BusinessError; + console.println(`catch error, code is ${error.code}, msg is ${error.message}`); + } + console.println(`out func testMessageSequence_WriteShort`); +} + +function testMessageSequence_WriteFloat() { + console.println(`into func testMessageSequence_WriteFloat`); + let data = rpc.MessageSequence.create(); + console.println(`data gen`); + try { + data.writeFloat(123456); + let res = data.readFloat(); + console.println(`readFloat success, res is ` + res); + } catch (err) { + let error: BusinessError = err as BusinessError; + console.println(`catch error, code is ${error.code}, msg is ${error.message}`); + } + console.println(`out func testMessageSequence_WriteFloat`); +} + +function testMessageSequence_WriteAndRead_ArrayBuffer_Int8() { + console.println(`into func testMessageSequence_WriteAndRead_ArrayBuffer_Int8`); + let data = rpc.MessageSequence.create(); + console.println(`data gen`); + let buffer = new ArrayBuffer(1 * 10); + let int8View = new Int8Array(buffer); + for (let i = 0; i < int8View.length; i++) { + int8View[i] = i * 2 + 1; + if (i % 2 != 0) { + int8View[i] = -int8View[i]; + } + } + + try { + data.writeArrayBuffer(buffer, rpc.TypeCode.INT8_ARRAY); + console.println(`writeArrayBuffer success`); + console.println(`write ArrayBuffer is :` + int8View); + } catch (err) { + let error: BusinessError = err as BusinessError; + console.println(`catch error, code is ${error.code}, msg is ${error.message}`); + } + + try { + let result = data.readArrayBuffer(rpc.TypeCode.INT8_ARRAY); + let readInt8View = new Int8Array(result); + console.println(`ArrayBuffer success`); + console.println(`read data is :` + readInt8View); + } catch (err) { + let error: BusinessError = err as BusinessError; + console.println(`catch error, code is ${error.code}, msg is ${error.message}`); + } + + console.println(`out func testMessageSequence_WriteAndRead_ArrayBuffer_Int8`); +} + +function testMessageSequence_WriteAndRead_ArrayBuffer_UInt8() { + console.println(`into func testMessageSequence_WriteAndRead_ArrayBuffer_UInt8`); + let data = rpc.MessageSequence.create(); + console.println(`data gen`); + let buffer = new ArrayBuffer(1 * 10); + let uint8View = new Uint8Array(buffer); + for (let i = 0; i < uint8View.length; i++) { + uint8View[i] = i * 2 + 1; + } + + try { + data.writeArrayBuffer(buffer, rpc.TypeCode.UINT8_ARRAY); + console.println(`writeArrayBuffer success`); + console.println(`write ArrayBuffer is :` + uint8View); + } catch (err) { + let error: BusinessError = err as BusinessError; + console.println(`catch error, code is ${error.code}, msg is ${error.message}`); + } + + try { + let result = data.readArrayBuffer(rpc.TypeCode.UINT8_ARRAY); + let readUInt8View = new Uint8Array(result); + console.println(`ArrayBuffer success`); + console.println(`read data is :` + readUInt8View); + } catch (err) { + let error: BusinessError = err as BusinessError; + console.println(`catch error, code is ${error.code}, msg is ${error.message}`); + } + + console.println(`out func testMessageSequence_WriteAndRead_ArrayBuffer_UInt8`); +} + +function testMessageSequence_WriteAndRead_ArrayBuffer_Int16() { + console.println(`into func testMessageSequence_WriteAndRead_ArrayBuffer_Int16`); + let data = rpc.MessageSequence.create(); + console.println(`data gen`); + let buffer = new ArrayBuffer(2 * 10); + let int16View = new Int16Array(buffer); + for (let i = 0; i < int16View.length; i++) { + int16View[i] = i * 2 + 1; + if (i % 2 != 0) { + int16View[i] = -int16View[i]; + } + } + + try { + data.writeArrayBuffer(buffer, rpc.TypeCode.INT16_ARRAY); + console.println(`writeArrayBuffer success`); + console.println(`write ArrayBuffer is :` + int16View); + } catch (err) { + let error: BusinessError = err as BusinessError; + console.println(`catch error, code is ${error.code}, msg is ${error.message}`); + } + + try { + let result = data.readArrayBuffer(rpc.TypeCode.INT16_ARRAY); + let readInt16View = new Int16Array(result); + console.println(`ArrayBuffer success`); + console.println(`read data is :` + readInt16View); + } catch (err) { + let error: BusinessError = err as BusinessError; + console.println(`catch error, code is ${error.code}, msg is ${error.message}`); + } + + console.println(`out func testMessageSequence_WriteAndRead_ArrayBuffer_Int16`); +} + +function testMessageSequence_WriteAndRead_ArrayBuffer_UInt16() { + console.println(`into func testMessageSequence_WriteAndRead_ArrayBuffer_UInt16`); + let data = rpc.MessageSequence.create(); + console.println(`data gen`); + let buffer = new ArrayBuffer(2 * 10); + let uint16View = new Uint16Array(buffer); + for (let i = 0; i < uint16View.length; i++) { + uint16View[i] = i * 2 + 1; + } + + try { + data.writeArrayBuffer(buffer, rpc.TypeCode.UINT16_ARRAY); + console.println(`writeArrayBuffer success`); + console.println(`write ArrayBuffer is :` + uint16View); + } catch (err) { + let error: BusinessError = err as BusinessError; + console.println(`catch error, code is ${error.code}, msg is ${error.message}`); + } + + try { + let result = data.readArrayBuffer(rpc.TypeCode.UINT16_ARRAY); + let readUInt16View = new Uint16Array(result); + console.println(`ArrayBuffer success`); + console.println(`read data is :` + readUInt16View); + } catch (err) { + let error: BusinessError = err as BusinessError; + console.println(`catch error, code is ${error.code}, msg is ${error.message}`); + } + + console.println(`out func testMessageSequence_WriteAndRead_ArrayBuffer_UInt16`); +} + +function testMessageSequence_WriteAndRead_ArrayBuffer_Int32() { + console.println(`into func testMessageSequence_WriteAndRead_ArrayBuffer_Int32`); + let data = rpc.MessageSequence.create(); + console.println(`data gen`); + let buffer = new ArrayBuffer(4 * 10); + let int32View = new Int32Array(buffer); + for (let i = 0; i < int32View.length; i++) { + int32View[i] = i * 2 + 1; + if (i % 2 != 0) { + int32View[i] = -int32View[i]; + } + } + + try { + data.writeArrayBuffer(buffer, rpc.TypeCode.INT32_ARRAY); + console.println(`writeArrayBuffer success`); + console.println(`write ArrayBuffer is :` + int32View); + } catch (err) { + let error: BusinessError = err as BusinessError; + console.println(`catch error, code is ${error.code}, msg is ${error.message}`); + } + + try { + let result = data.readArrayBuffer(rpc.TypeCode.INT32_ARRAY); + let readInt32View = new Int32Array(result); + console.println(`readArrayBuffer success`); + console.println(`read ArrayBuffer is :` + readInt32View); + } catch (err) { + let error: BusinessError = err as BusinessError; + console.println(`catch error, code is ${error.code}, msg is ${error.message}`); + } + + console.println(`out func testMessageSequence_WriteAndRead_ArrayBuffer_Int32`); +} + +function testMessageSequence_WriteAndRead_ArrayBuffer_UInt32() { + console.println(`into func testMessageSequence_WriteAndRead_ArrayBuffer_UInt32`); + let data = rpc.MessageSequence.create(); + console.println(`data gen`); + let buffer = new ArrayBuffer(4 * 10); + let uint32View = new Uint32Array(buffer); + for (let i = 0; i < uint32View.length; i++) { + uint32View[i] = i * 2 + 1; + } + + try { + data.writeArrayBuffer(buffer, rpc.TypeCode.UINT32_ARRAY); + console.println(`writeArrayBuffer success`); + console.println(`write ArrayBuffer is :` + uint32View); + } catch (err) { + let error: BusinessError = err as BusinessError; + console.println(`catch error, code is ${error.code}, msg is ${error.message}`); + } + + try { + let result = data.readArrayBuffer(rpc.TypeCode.UINT32_ARRAY); + let readUInt32View = new Uint32Array(result); + console.println(`ArrayBuffer success`); + console.println(`read data is :` + readUInt32View); + } catch (err) { + let error: BusinessError = err as BusinessError; + console.println(`catch error, code is ${error.code}, msg is ${error.message}`); + } + + console.println(`out func testMessageSequence_WriteAndRead_ArrayBuffer_UInt32`); +} + +function testMessageSequence_WriteAndRead_ArrayBuffer_Float32() { + console.println(`into func testMessageSequence_WriteAndRead_ArrayBuffer_Float32`); + let data = rpc.MessageSequence.create(); + console.println(`data gen`); + let buffer = new ArrayBuffer(4 * 10); + let float32View = new Float32Array(buffer); + for (let i = 0; i < float32View.length; i++) { + float32View[i] = i * 2 + i * 1.0 / 10; + if (i % 2 != 0) { + float32View[i] = -float32View[i]; + } + } + + try { + data.writeArrayBuffer(buffer, rpc.TypeCode.FLOAT32_ARRAY); + console.println(`writeArrayBuffer success`); + console.println(`write data is :` + float32View); + } catch (err) { + let error: BusinessError = err as BusinessError; + console.println(`catch error, code is ${error.code}, msg is ${error.message}`); + } + + try { + let result = data.readArrayBuffer(rpc.TypeCode.FLOAT32_ARRAY); + let readFloat32View = new Float32Array(result); + console.println(`readFloat32View success`); + console.println(`ArrayBuffer is :` + readFloat32View); + } catch (err) { + let error: BusinessError = err as BusinessError; + console.println(`catch error, code is ${error.code}, msg is ${error.message}`); + } + + console.println(`out func testMessageSequence_WriteAndRead_ArrayBuffer_Float32`); +} + +function testMessageSequence_WriteAndRead_ArrayBuffer_Float64() { + console.println(`into func testMessageSequence_WriteAndRead_ArrayBuffer_Float64`); + let data = rpc.MessageSequence.create(); + console.println(`data gen`); + let buffer = new ArrayBuffer(8 * 10); + let float64View = new Float64Array(buffer); + for (let i = 0; i < float64View.length; i++) { + float64View[i] = i * 2 + i * 1.0 / 10; + if (i % 2 != 0) { + float64View[i] = -float64View[i]; + } + } + + try { + data.writeArrayBuffer(buffer, rpc.TypeCode.FLOAT64_ARRAY); + console.println(`writeArrayBuffer success`); + console.println(`write data is :` + float64View); + } catch (err) { + let error: BusinessError = err as BusinessError; + console.println(`catch error, code is ${error.code}, msg is ${error.message}`); + } + + try { + let result = data.readArrayBuffer(rpc.TypeCode.FLOAT64_ARRAY); + let readFloat64View = new Float64Array(result); + console.println(`readArrayBuffer success`); + console.println(`ArrayBuffer is :` + readFloat64View); + } catch (err) { + let error: BusinessError = err as BusinessError; + console.println(`catch error, code is ${error.code}, msg is ${error.message}`); + } + + console.println(`out func testMessageSequence_WriteAndRead_ArrayBuffer_Float64`); +} + +function testMessageSequence_WriteAndRead_ArrayBuffer_Int64() { + console.println(`into func testMessageSequence_WriteAndRead_ArrayBuffer_Int64`); + let data = rpc.MessageSequence.create(); + console.println(`data gen`); + let buffer = new ArrayBuffer(8 * 10); + let bInt64View = new BigInt64Array(buffer); + for (let i = 0; i < bInt64View.length; i++) { + bInt64View[i] = i * 2 + 1; + if (i % 2 != 0) { + bInt64View[i] = -bInt64View[i]; + } + } + + try { + data.writeArrayBuffer(buffer, rpc.TypeCode.BIGINT64_ARRAY); + console.println(`writeArrayBuffer success`); + console.println(`write data is :` + bInt64View); + } catch (err) { + let error: BusinessError = err as BusinessError; + console.println(`catch error, code is ${error.code}, msg is ${error.message}`); + } + + try { + let result = data.readArrayBuffer(rpc.TypeCode.BIGINT64_ARRAY); + let readInt64View = new BigInt64Array(result); + console.println(`readArrayBuffer success`); + console.println(`read data is :` + readInt64View); + } catch (err) { + let error: BusinessError = err as BusinessError; + console.println(`catch error, code is ${error.code}, msg is ${error.message}`); + } + + console.println(`out func testMessageSequence_WriteAndRead_ArrayBuffer_Int64`); +} + +function testMessageSequence_WriteAndRead_ArrayBuffer_UInt64() { + console.println(`into func testMessageSequence_WriteAndRead_ArrayBuffer_UInt64`); + let data = rpc.MessageSequence.create(); + console.println(`data gen`); + let buffer = new ArrayBuffer(8 * 10); + let bUInt64View = new BigInt64Array(buffer); + for (let i = 0; i < bUInt64View.length; i++) { + bUInt64View[i] = i * 2 + 1; + } + + try { + data.writeArrayBuffer(buffer, rpc.TypeCode.BIGUINT64_ARRAY); + console.println(`writeArrayBuffer success`); + console.println(`write data is :` + bUInt64View); + } catch (err) { + let error: BusinessError = err as BusinessError; + console.println(`catch error, code is ${error.code}, msg is ${error.message}`); + } + + try { + let result = data.readArrayBuffer(rpc.TypeCode.BIGUINT64_ARRAY); + let readUInt64View = new BigInt64Array(result); + console.println(`readArrayBuffer success`); + console.println(`read data is :` + readUInt64View); + } catch (err) { + let error: BusinessError = err as BusinessError; + console.println(`catch error, code is ${error.code}, msg is ${error.message}`); + } + + console.println(`out func testMessageSequence_WriteAndRead_ArrayBuffer_UInt64`); +} + +function testMessageSequence_writeRemoteObjectArray() { + console.println(`into func testMessageSequence_writeRemoteObjectArray`); + try { + let data = rpc.MessageSequence.create(); + console.println(`data gen`); + let testRemtoeObject = new TestRemoteObject("testObject"); + let testRemtoeObject1 = new TestRemoteObject("testObject1"); + let a: Array = [testRemtoeObject, testRemtoeObject1]; + console.println(`testRemoteObject gen`); + data.writeRemoteObjectArray(a); + console.println(`writeRemoteObject success`); + //let res = data.readRemoteObjectArray(); + console.println(`readRemoteObjectArray success`); + } catch (err) { + console.println(`catch error:` + err); + } + console.println(`out func testMessageSequence_writeRemoteObjectArray`); +} + +function testMessageSequence_writeCharArray() { + console.println(`into func testMessageSequence_writeCharArray`); + try { + let data = rpc.MessageSequence.create(); + console.println(`data gen`); + let a: Array = [97, 98]; + data.writeCharArray(a); + console.println(`writeRemoteObject success`); + let res = data.readCharArray(); + console.println(`readCharArray success` + res); + } catch (err) { + console.println(`catch error:` + err); + } + console.println(`out func testMessageSequence_writeCharArray`); +} + +function testMessageSequence_writeLongArray() { + console.println(`into func testMessageSequence_writeLongArray`); + try { + let data = rpc.MessageSequence.create(); + console.println(`data gen`); + let b : long = 2; + let c : long = 3; + let a: Array = [b, c]; + data.writeLongArray(a); + console.println(`writeRemoteObject success`); + let res = data.readLongArray(); + console.println(`ReadLongArray success` + res); + } catch (err) { + console.println(`catch error:` + err); + } + console.println(`out func testMessageSequence_writeLongArray`); +} + +function testMessageSequence_writeShortArray() { + console.println(`into func testMessageSequence_writeShortArray`); + try { + let data = rpc.MessageSequence.create(); + console.println(`data gen`); + let a: Array = [100, 101]; + data.writeShortArray(a); + console.println(`writeRemoteObject success`); + let res = data.readShortArray(); + console.println(`ReadShortArray success` + res); + } catch (err) { + console.println(`catch error:` + err); + } + console.println(`out func testMessageSequence_writeShortArray`); +} + +function testMessageSequence_writeChar() { + console.println(`into func testMessageSequence_writeChar`); + try { + let data = rpc.MessageSequence.create(); + console.println(`data gen`); + let a: byte = 97; + data.writeChar(a); + console.println(`writeRemoteObject success`); + let res = data.readChar(); + console.println(`ReadChar success` + res); + } catch (err) { + console.println(`catch error:` + err); + } + console.println(`out func testMessageSequence_writeChar`); +} + +function testMessageSequence_writeDouble() { + console.println(`into func testMessageSequence_writeDouble`); + try { + let data = rpc.MessageSequence.create(); + console.println(`data gen`); + let a: double = 9.7; + data.writeDouble(a); + console.println(`writeRemoteObject success`); + let res = data.readDouble(); + console.println(`ReadDouble success` + res); + } catch (err) { + console.println(`catch error:` + err); + } + console.println(`out func testMessageSequence_writeDouble`); +} + +function testIPCSkeleton_resetCallingIdentity() { + console.println(`into func testIPCSkeleton_resetCallingIdentity`); + + let identity = rpc.IPCSkeleton.resetCallingIdentity(); + console.println(`identity is : ` + identity); + + console.println(`out func testIPCSkeleton_resetCallingIdentity`); +} + +function testIPCSkeleton_restoreCallingIdentity() { + console.println(`into func testIPCSkeleton_restoreCallingIdentity`); + + let identity = rpc.IPCSkeleton.resetCallingIdentity(); + rpc.IPCSkeleton.restoreCallingIdentity(identity); + console.println(`identity is : ` + identity); + + console.println(`out func testIPCSkeleton_restoreCallingIdentity`); +} + +function testIPCSkeleton_getCallingDeviceID() { + console.println(`into func testIPCSkeleton_getCallingDeviceID`); + try { + let deviceID = rpc.IPCSkeleton.getCallingDeviceID(); + console.println(`deviceID is : ` + deviceID); + } catch (err) { + console.println(`catch error:` + err); + } + console.println(`out func testIPCSkeleton_getCallingDeviceID`); +} + +function testIPCSkeleton_getLocalDeviceID() { + console.println(`into func testIPCSkeleton_getLocalDeviceID`); + + let deviceID = rpc.IPCSkeleton.getLocalDeviceID(); + console.println(`deviceID is : ` + deviceID); + + console.println(`out func testIPCSkeleton_getLocalDeviceID`); +} + +function testIPCSkeleton_isLocalCalling() { + console.println(`into func testIPCSkeleton_isLocalCalling`); + let ret = rpc.IPCSkeleton.isLocalCalling(); + console.println(`ret is : ` + ret); + + console.println(`out func testIPCSkeleton_isLocalCalling`); +} + +function testIPCSkeleton_flushCmdBuffer() { + console.println(`into func testIPCSkeleton_flushCmdBuffer`); + let a: rpc.IRemoteObject = new TestRemoteObject("testObject"); + + rpc.IPCSkeleton.flushCmdBuffer(a); + console.println(`out func testIPCSkeleton_flushCmdBuffer`); +} + function main() { console.log(`into main`); console.log(`*************************************************`); @@ -880,5 +1818,110 @@ function main() { //testMessageSequence_WriteAndRead_FileDescriptor(); console.log(`*************************************************`); //testMessageSequence_closeFileDescriptor(); + console.println(`*************************************************`); + testMessageSequence_ReadCharArray(); + console.println(`*************************************************`); + testMessageSequence_ReadFloatArray(); + console.println(`*************************************************`); + testMessageSequence_ReadLongArray(); + console.println(`*************************************************`); + testMessageSequence_ReadShortArray(); + console.println(`*************************************************`); + testMessageSequence_ReadChar(); + console.println(`*************************************************`); + testMessageSequence_ReadDouble(); + console.println(`*************************************************`); + testMessageSequence_ReadFloat(); + console.println(`*************************************************`); + testMessageSequence_ReadShort(); + console.println(`*************************************************`); + testMessageSequence_ReadByte(); + console.println(`*************************************************`); + testMessageSequence_ReadByteArray(); + console.println(`*************************************************`); + testMessageSequence_RewindWriteAndRead(); + console.println(`*************************************************`); + testMessageSequence_SetSize(); + console.println(`*************************************************`); + testMessageSequence_duplFileDescriptor(); + console.println(`*************************************************`); + testMessageSequence_ReadRemoteObjectArray(); + console.println(`*************************************************`); + testAshmen_SetProtectionType(); + console.println(`*************************************************`); + testAshmen_MapReadOnlyAshmem(); + console.println(`*************************************************`); + testAshmen_MapTypedAshmem(); + console.println(`*************************************************`); + testAshmen_CloseAshmem(); + console.println(`*************************************************`); + testAshmen_UnMapAshmem(); + console.println(`*************************************************`); + testMessageOption_GetFlagsAndSetFlags(); + console.println(`*************************************************`); + testMessageOption_GetWaitTimeAndSetWaitTime(); + console.println(`*************************************************`); + testMessageSequence_GetSize(); + console.println(`*************************************************`); + testMessageSequence_GetWritableBytes(); + console.println(`*************************************************`); + testMessageSequence_GetReadableBytes(); + console.println(`*************************************************`); + testMessageSequence_GetReadPosition(); + console.println(`*************************************************`); + testMessageSequence_GetWritePosition(); + console.println(`*************************************************`); + testMessageSequence_ContainFileDescriptors(); + console.println(`*************************************************`); + testMessageSequence_GetRawDataCapacity(); + console.println(`*************************************************`); + testMessageSequence_WriteShort(); + console.println(`*************************************************`); + testMessageSequence_WriteFloat(); + console.println(`*************************************************`); + testMessageSequence_WriteAndRead_ArrayBuffer_Int8(); + console.println(`*************************************************`); + testMessageSequence_WriteAndRead_ArrayBuffer_UInt8(); + console.println(`*************************************************`); + testMessageSequence_WriteAndRead_ArrayBuffer_Int16(); + console.println(`*************************************************`); + testMessageSequence_WriteAndRead_ArrayBuffer_UInt16(); + console.println(`*************************************************`); + testMessageSequence_WriteAndRead_ArrayBuffer_Int32(); + console.println(`*************************************************`) + testMessageSequence_WriteAndRead_ArrayBuffer_UInt32(); + console.println(`*************************************************`) + testMessageSequence_WriteAndRead_ArrayBuffer_Float32(); + console.println(`*************************************************`) + testMessageSequence_WriteAndRead_ArrayBuffer_Float64(); + console.println(`*************************************************`) + testMessageSequence_WriteAndRead_ArrayBuffer_Int64(); + console.println(`*************************************************`) + testMessageSequence_WriteAndRead_ArrayBuffer_UInt64(); + console.println(`*************************************************`); + testMessageSequence_writeRemoteObjectArray(); + console.println(`*************************************************`); + testMessageSequence_writeCharArray(); + console.println(`*************************************************`); + testMessageSequence_writeLongArray(); + console.println(`*************************************************`); + testMessageSequence_writeShortArray(); + console.println(`*************************************************`); + testMessageSequence_writeChar(); + console.println(`*************************************************`); + testMessageSequence_writeDouble(); + console.println(`*************************************************`); + testIPCSkeleton_getLocalDeviceID(); + console.println(`*************************************************`); + testIPCSkeleton_isLocalCalling(); + console.println(`*************************************************`); + testIPCSkeleton_resetCallingIdentity(); + console.println(`*************************************************`); + testIPCSkeleton_restoreCallingIdentity(); + console.println(`*************************************************`); + testIPCSkeleton_getCallingDeviceID(); + console.println(`*************************************************`); + testIPCSkeleton_flushCmdBuffer(); + console.println(`*************************************************`); console.log(`out main`); } \ No newline at end of file -- Gitee From 1474f8cbc3f3845815be8313b9845a772ad7fc25 Mon Sep 17 00:00:00 2001 From: kangpeng9771 Date: Sat, 13 Sep 2025 11:01:54 +0800 Subject: [PATCH 2/4] hl Signed-off-by: kangpeng9771 --- ipc/native/src/taihe/inc/taihe_message_sequence.h | 9 +++++++++ ipc/native/src/taihe/src/ohos.rpc.rpc.impl.cpp | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/ipc/native/src/taihe/inc/taihe_message_sequence.h b/ipc/native/src/taihe/inc/taihe_message_sequence.h index bc56c5a8..9df738ca 100644 --- a/ipc/native/src/taihe/inc/taihe_message_sequence.h +++ b/ipc/native/src/taihe/inc/taihe_message_sequence.h @@ -194,11 +194,17 @@ public: bool ContainFileDescriptors(); int64_t GetRawDataCapacity(); + void RewindRead(int64_t pos); + void RewindWrite(int64_t pos); + void SetSize(int64_t size); + ::taihe::array<::ohos::rpc::rpc::IRemoteObjectUnion> ReadRemoteObjectArrayImpl(); + ::taihe::array ReadByteArrayGet(); + void ReadByteArrayIn(::taihe::array_view dataIn); void WriteByte(int8_t val); @@ -208,8 +214,11 @@ public: void WriteFloat(double val); static int32_t DupFileDescriptor(int32_t fd); + static ::ohos::rpc::rpc::MessageSequence RpcTransferStaicImpl(uintptr_t input); + static uintptr_t RpcTransferDynamicImpl(::ohos::rpc::rpc::MessageSequence obj); + static void CreateJsMessageSequence(napi_env jsenv, napi_status status, napi_value global, napi_value* jsMessageSequence); diff --git a/ipc/native/src/taihe/src/ohos.rpc.rpc.impl.cpp b/ipc/native/src/taihe/src/ohos.rpc.rpc.impl.cpp index f17b9a42..17f8d10d 100644 --- a/ipc/native/src/taihe/src/ohos.rpc.rpc.impl.cpp +++ b/ipc/native/src/taihe/src/ohos.rpc.rpc.impl.cpp @@ -378,7 +378,7 @@ void AshmemImpl::MapTypedAshmem(int32_t mapType) CHECK_NATIVE_OBJECT(ashmem_, OHOS::RpcTaiheErrorCode::TAIHE_OS_MMAP_ERROR); if (mapType > MAP_PROT_MAX) { ZLOGE(LOG_LABEL, "napiAshmem mapType error"); - return; + RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::TAIHE_CHECK_PARAM_ERROR); } ashmem_->MapAshmem(mapType); } -- Gitee From af6e05910a41f944117b80f308d4b25c082a85ef Mon Sep 17 00:00:00 2001 From: kangpeng9771 Date: Sat, 13 Sep 2025 16:31:54 +0800 Subject: [PATCH 3/4] hl Signed-off-by: kangpeng9771 --- ipc/native/src/taihe/idl/ohos.rpc.rpc.taihe | 4 ++-- ipc/native/src/taihe/inc/taihe_message_sequence.h | 4 ++-- ipc/native/src/taihe/src/ohos.rpc.rpc.impl.cpp | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/ipc/native/src/taihe/idl/ohos.rpc.rpc.taihe b/ipc/native/src/taihe/idl/ohos.rpc.rpc.taihe index 3ace6966..f541da30 100644 --- a/ipc/native/src/taihe/idl/ohos.rpc.rpc.taihe +++ b/ipc/native/src/taihe/idl/ohos.rpc.rpc.taihe @@ -130,7 +130,7 @@ interface MessageSequence { WriteLong(val: i64): void; WriteDouble(val: f64): void; WriteBoolean(val: bool): void; - WriteChar(val: u8): void; + WriteChar(val: i16): void; WriteString(val: String): void; WriteParcelable(val: Parcelable): void; @@ -141,7 +141,7 @@ interface MessageSequence { WriteFloatArray(floatArray: Array): void; WriteDoubleArray(doubleArray: Array): void; WriteBooleanArray(booleanArray: Array): void; - WriteCharArray(charArray: Array): void; + WriteCharArray(charArray: Array): void; WriteStringArray(stringArray: Array): void; WriteParcelableArray(parcelableArray: Array): void; WriteRemoteObjectArray(objectArray: Array): void; diff --git a/ipc/native/src/taihe/inc/taihe_message_sequence.h b/ipc/native/src/taihe/inc/taihe_message_sequence.h index 9df738ca..851be62e 100644 --- a/ipc/native/src/taihe/inc/taihe_message_sequence.h +++ b/ipc/native/src/taihe/inc/taihe_message_sequence.h @@ -66,7 +66,7 @@ public: void WriteDouble(double val); - void WriteChar(uint8_t val); + void WriteChar(int16_t val); void WriteBoolean(bool val); @@ -88,7 +88,7 @@ public: void WriteBooleanArray(::taihe::array_view booleanArray); - void WriteCharArray(::taihe::array_view charArray); + void WriteCharArray(::taihe::array_view charArray); void WriteStringArray(::taihe::array_view<::taihe::string> stringArray); diff --git a/ipc/native/src/taihe/src/ohos.rpc.rpc.impl.cpp b/ipc/native/src/taihe/src/ohos.rpc.rpc.impl.cpp index 17f8d10d..d618963a 100644 --- a/ipc/native/src/taihe/src/ohos.rpc.rpc.impl.cpp +++ b/ipc/native/src/taihe/src/ohos.rpc.rpc.impl.cpp @@ -922,10 +922,10 @@ void MessageSequenceImpl::WriteBoolean(bool val) } } -void MessageSequenceImpl::WriteChar(uint8_t val) +void MessageSequenceImpl::WriteChar(int16_t val) { CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::TAIHE_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); - bool result = nativeParcel_->WriteUint8(val); + bool result = nativeParcel_->WriteUint8(static_cast(val)); if (!result) { ZLOGE(LOG_LABEL, "write uint8 failed"); RPC_TAIHE_ERROR(OHOS::RpcTaiheErrorCode::TAIHE_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); @@ -1115,7 +1115,7 @@ void MessageSequenceImpl::WriteBooleanArray(::taihe::array_view booleanArr } } -void MessageSequenceImpl::WriteCharArray(::taihe::array_view charArray) +void MessageSequenceImpl::WriteCharArray(::taihe::array_view charArray) { CHECK_NATIVE_OBJECT(nativeParcel_, OHOS::RpcTaiheErrorCode::TAIHE_WRITE_DATA_TO_MESSAGE_SEQUENCE_ERROR); uint32_t arrayLength = charArray.size(); -- Gitee From d1daac1c7a8926922d961df1053f4205d8586379 Mon Sep 17 00:00:00 2001 From: kangpeng9771 Date: Sat, 13 Sep 2025 17:20:02 +0800 Subject: [PATCH 4/4] hl Signed-off-by: kangpeng9771 --- ipc/native/src/taihe/idl/ohos.rpc.rpc.taihe | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ipc/native/src/taihe/idl/ohos.rpc.rpc.taihe b/ipc/native/src/taihe/idl/ohos.rpc.rpc.taihe index f541da30..733a03ee 100644 --- a/ipc/native/src/taihe/idl/ohos.rpc.rpc.taihe +++ b/ipc/native/src/taihe/idl/ohos.rpc.rpc.taihe @@ -46,7 +46,7 @@ struct RequestResult { reply: MessageSequence; } -enum ErrorCode { +enum ErrorCode : i32 { CHECK_PARAM_ERROR = 401, OS_MMAP_ERROR = 1900001, OS_IOCTL_ERROR = 1900002, -- Gitee