1 /*
2 * Copyright (c) 2023 Shenzhen Kaihong Digital Industry Development 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 "common/sharing_log.h"
17 #include "inter_ipc_proxy.h"
18 #include "ipc_msg_encoder.h"
19 #include "ipc_msg_decoder.h"
20 namespace OHOS {
21 namespace Sharing {
22
InterIpcProxy(const sptr<IRemoteObject> & impl)23 InterIpcProxy::InterIpcProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IInterIpc>(impl)
24 {
25 SHARING_LOGD("trace.");
26 }
27
SetListenerObject(std::string key,const sptr<IRemoteObject> & object)28 int32_t InterIpcProxy::SetListenerObject(std::string key, const sptr<IRemoteObject> &object)
29 {
30 SHARING_LOGD("trace.");
31 MessageParcel data;
32 MessageParcel reply;
33 MessageOption option;
34 (void)data.WriteString(key);
35 (void)data.WriteRemoteObject(object);
36
37 int error = Remote()->SendRequest(InterIpcMsg::SET_LISTENER_OBJ, data, reply, option);
38 if (error != ERR_NONE) {
39 SHARING_LOGE("set listener object failed: %{public}d.", error);
40 return error;
41 }
42
43 return reply.ReadInt32();
44 }
45
46
GetSubSystemAbility(std::string key,std::string className)47 sptr<IRemoteObject> InterIpcProxy::GetSubSystemAbility(std::string key, std::string className)
48 {
49 SHARING_LOGD("trace.");
50 MessageParcel data;
51 MessageParcel reply;
52 MessageOption option;
53 data.WriteString(key);
54 data.WriteString(className);
55
56 int error = Remote()->SendRequest(InterIpcMsg::GET_SUBSYSTEM, data, reply, option);
57 if (error != ERR_NONE) {
58 SHARING_LOGE("create sub proxy failed, error: %{public}d.", error);
59 return nullptr;
60 }
61
62 return reply.ReadRemoteObject();
63 }
64
65
DoIpcCommand(std::shared_ptr<BaseMsg> msg,std::shared_ptr<BaseMsg> & replyMsg)66 int32_t InterIpcProxy::DoIpcCommand(std::shared_ptr<BaseMsg> msg, std::shared_ptr<BaseMsg> &replyMsg)
67 {
68 SHARING_LOGD("trace.");
69 MessageParcel data;
70 MessageParcel reply;
71 MessageOption option;
72 IpcMsgEncoder::GetInstance().MsgEncode(data, msg);
73
74 int error = Remote()->SendRequest(InterIpcMsg::INTER_IPC_MSG, data, reply, option);
75 if (error != ERR_NONE) {
76 SHARING_LOGE("do ipc command failed %{public}d.", error);
77 return error;
78 }
79
80 IpcMsgDecoder::GetInstance().MsgDecode(replyMsg, reply);
81 return reply.ReadInt32();
82 }
83
84 }
85 }
86
87