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 "dscreen_sink_proxy.h"
17
18 #include "iremote_object.h"
19 #include "message_option.h"
20 #include "message_parcel.h"
21
22 #include "dscreen_errcode.h"
23 #include "dscreen_log.h"
24 #include "dscreen_util.h"
25
26 namespace OHOS {
27 namespace DistributedHardware {
InitSink(const std::string & params)28 int32_t DScreenSinkProxy::InitSink(const std::string ¶ms)
29 {
30 if (params.empty() || params.size() > PARAM_MAX_SIZE) {
31 DHLOGE("InitSink error: invalid parameter.");
32 return ERR_DH_SCREEN_INPUT_PARAM_INVALID;
33 }
34 sptr<IRemoteObject> remote = Remote();
35 if (remote == nullptr) {
36 DHLOGE("DScreenSinkProxy remote service null");
37 return DSCREEN_BAD_VALUE;
38 }
39 MessageParcel data;
40 MessageParcel reply;
41 MessageOption option;
42 if (!data.WriteInterfaceToken(GetDescriptor())) {
43 DHLOGE("WriteInterfaceToken failed");
44 return ERR_DH_SCREEN_SA_WRITEINTERFACETOKEN_FAILED;
45 }
46
47 if (!data.WriteString(params)) {
48 DHLOGE("Write param failed.");
49 return ERR_DH_SCREEN_SA_WRITEPARAM_FAILED;
50 }
51
52 remote->SendRequest(INIT_SINK, data, reply, option);
53 int32_t ret = reply.ReadInt32();
54 return ret;
55 }
56
ReleaseSink()57 int32_t DScreenSinkProxy::ReleaseSink()
58 {
59 sptr<IRemoteObject> remote = Remote();
60 if (remote == nullptr) {
61 DHLOGE("DScreenSinkProxy remote service null");
62 return DSCREEN_BAD_VALUE;
63 }
64 MessageParcel data;
65 MessageParcel reply;
66 MessageOption option;
67 if (!data.WriteInterfaceToken(GetDescriptor())) {
68 DHLOGE("WriteInterfaceToken failed");
69 return ERR_DH_SCREEN_SA_WRITEINTERFACETOKEN_FAILED;
70 }
71
72 remote->SendRequest(RELEASE_SINK, data, reply, option);
73 int32_t ret = reply.ReadInt32();
74 return ret;
75 }
76
SubscribeLocalHardware(const std::string & dhId,const std::string & param)77 int32_t DScreenSinkProxy::SubscribeLocalHardware(const std::string &dhId, const std::string ¶m)
78 {
79 if (dhId.empty() || dhId.size() > DID_MAX_SIZE || param.empty() || param.size() > PARAM_MAX_SIZE) {
80 DHLOGE("SubscribeLocalHardware error: invalid parameter.");
81 return ERR_DH_SCREEN_INPUT_PARAM_INVALID;
82 }
83 sptr<IRemoteObject> remote = Remote();
84 if (remote == nullptr) {
85 DHLOGE("DScreenSinkProxy remote service null");
86 return DSCREEN_BAD_VALUE;
87 }
88 MessageParcel data;
89 MessageParcel reply;
90 MessageOption option;
91 if (!data.WriteInterfaceToken(GetDescriptor())) {
92 DHLOGE("WriteInterfaceToken failed");
93 return ERR_DH_SCREEN_SA_WRITEINTERFACETOKEN_FAILED;
94 }
95
96 if (!data.WriteString(dhId) || !data.WriteString(param)) {
97 DHLOGE("Write param failed.");
98 return ERR_DH_SCREEN_SA_WRITEPARAM_FAILED;
99 }
100
101 remote->SendRequest(SUBSCRIBE_DISTRIBUTED_HARDWARE, data, reply, option);
102 int32_t ret = reply.ReadInt32();
103 return ret;
104 }
105
UnsubscribeLocalHardware(const std::string & dhId)106 int32_t DScreenSinkProxy::UnsubscribeLocalHardware(const std::string &dhId)
107 {
108 if (dhId.empty() || dhId.size() > DID_MAX_SIZE) {
109 DHLOGE("UnsubscribeLocalHardware error: invalid parameter.");
110 return ERR_DH_SCREEN_INPUT_PARAM_INVALID;
111 }
112 sptr<IRemoteObject> remote = Remote();
113 if (remote == nullptr) {
114 DHLOGE("DScreenSinkProxy remote service null");
115 return DSCREEN_BAD_VALUE;
116 }
117 MessageParcel data;
118 MessageParcel reply;
119 MessageOption option;
120 if (!data.WriteInterfaceToken(GetDescriptor())) {
121 DHLOGE("WriteInterfaceToken failed");
122 return ERR_DH_SCREEN_SA_WRITEINTERFACETOKEN_FAILED;
123 }
124
125 if (!data.WriteString(dhId)) {
126 DHLOGE("Write param failed.");
127 return ERR_DH_SCREEN_SA_WRITEPARAM_FAILED;
128 }
129
130 remote->SendRequest(UNSUBSCRIBE_DISTRIBUTED_HARDWARE, data, reply, option);
131 int32_t ret = reply.ReadInt32();
132 return ret;
133 }
134
DScreenNotify(const std::string & devId,int32_t eventCode,const std::string & eventContent)135 void DScreenSinkProxy::DScreenNotify(const std::string &devId, int32_t eventCode, const std::string &eventContent)
136 {
137 if (devId.empty() || devId.size() > DID_MAX_SIZE || eventContent.empty() ||
138 eventContent.size() > PARAM_MAX_SIZE) {
139 DHLOGE("DScreenNotify error: invalid parameter.");
140 return;
141 }
142 sptr<IRemoteObject> remote = Remote();
143 if (remote == nullptr) {
144 DHLOGE("DScreenSinkProxy remote service null");
145 return;
146 }
147 MessageParcel data;
148 MessageParcel reply;
149 MessageOption option = { MessageOption::TF_ASYNC };
150 if (!data.WriteInterfaceToken(GetDescriptor())) {
151 DHLOGE("WriteInterfaceToken failed");
152 return;
153 }
154
155 if (!data.WriteString(devId) || !data.WriteInt32(eventCode) || !data.WriteString(eventContent)) {
156 DHLOGE("Write param failed.");
157 return;
158 }
159
160 remote->SendRequest(DSCREEN_NOTIFY, data, reply, option);
161 }
162 } // namespace DistributedHardware
163 } // namespace OHOS