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