1/* 2 * Copyright (C) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16#include "rpc_test.h" 17#include "iremote_object.h" 18#include "ipc_skeleton.h" 19#include "ipc_types.h" 20 21namespace OHOS { 22std::string IRpcFooTest::GetFooName() 23{ 24 return fooName_; 25} 26 27int RpcFooStub::OnRemoteRequest(uint32_t code, 28 MessageParcel& data, MessageParcel& reply, MessageOption& option) 29{ 30 int result = ERR_NONE; 31 32 switch (code) { 33 case GET_FOO_NAME: { 34 reply.WriteString(TestGetFooName()); 35 break; 36 } 37 case GET_TOKENID: { 38 result = TestAccessToken(data, reply); 39 break; 40 } 41 case TEST_REMOTE_OBJECT: { 42 result = TestRemoteObject(data, reply); 43 break; 44 } 45 case TEST_ADD: { 46 result = TestAdd(data, reply); 47 break; 48 } 49 default: 50 return IPCObjectStub::OnRemoteRequest(code, data, reply, option); 51 } 52 53 return result; 54} 55 56std::string RpcFooStub::TestGetFooName(void) 57{ 58 return GetFooName(); 59} 60 61int32_t RpcFooStub::TestAccessToken(MessageParcel &data, MessageParcel &reply) 62{ 63 uint32_t featureSet = 0; 64 uint32_t tokenId = IPCSkeleton::GetCallingTokenID(); 65 if (tokenId != INVAL_TOKEN_ID) { 66 featureSet = RPC_ACCESS_TOKEN_FLAG; 67 } 68 reply.WriteUint32(featureSet); 69 reply.WriteUint32(tokenId); 70 return ERR_NONE; 71} 72 73int32_t RpcFooStub::TestRemoteObject(MessageParcel &data, MessageParcel &reply) 74{ 75 sptr<RpcFooProxy> rpcProxy = iface_cast<RpcFooProxy>(data.ReadRemoteObject()); 76 if (rpcProxy == nullptr) { 77 reply.WriteInt32(ERR_NULL_OBJECT); 78 return ERR_NULL_OBJECT; 79 } 80 MessageParcel dataParcel, replyParcel; 81 int32_t err = rpcProxy->TestAdd(dataParcel, replyParcel); 82 reply.WriteInt32(err); 83 return err; 84} 85 86int32_t RpcFooStub::TestAdd(MessageParcel &data, MessageParcel &reply) 87{ 88 int32_t a = data.ReadInt32(); 89 int32_t b = data.ReadInt32(); 90 reply.WriteInt32(a + b); 91 return ERR_NONE; 92} 93 94RpcFooProxy::RpcFooProxy(const sptr<IRemoteObject> &impl) 95 : IRemoteProxy<IRpcFooTest>(impl) 96{ 97} 98 99std::string RpcFooProxy::TestGetFooName(void) 100{ 101 MessageOption option; 102 MessageParcel dataParcel, replyParcel; 103 int err = Remote()->SendRequest(GET_FOO_NAME, dataParcel, replyParcel, option); 104 if (err != 0) { 105 return ""; 106 } 107 return replyParcel.ReadString(); 108} 109 110int32_t RpcFooProxy::TestAccessToken(MessageParcel &data, MessageParcel &reply) 111{ 112 MessageOption option; 113 int32_t err = Remote()->SendRequest(GET_TOKENID, data, reply, option); 114 return err; 115} 116 117int32_t RpcFooProxy::TestRemoteObject(MessageParcel &data, MessageParcel &reply) 118{ 119 MessageOption option; 120 int32_t err = Remote()->SendRequest(TEST_REMOTE_OBJECT, data, reply, option); 121 return err; 122} 123 124int32_t RpcFooProxy::TestAdd(MessageParcel &data, MessageParcel &reply) 125{ 126 int32_t a = 223; 127 int32_t b = 513; 128 data.WriteInt32(a); 129 data.WriteInt32(b); 130 MessageOption option; 131 int32_t err = Remote()->SendRequest(TEST_ADD, data, reply, option); 132 if (err != ERR_NONE) { 133 return err; 134 } 135 return reply.ReadInt32() == (a + b) ? ERR_NONE : ERR_INVALID_DATA; 136} 137 138RpcDeathRecipient::RpcDeathRecipient() 139{ 140} 141 142RpcDeathRecipient::~RpcDeathRecipient() 143{ 144} 145 146void RpcDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote) 147{ 148 return; 149} 150} // namespace OHOS