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 "daudio_sink_proxy.h"
17 
18 #include "daudio_constants.h"
19 #include "daudio_errorcode.h"
20 #include "daudio_ipc_interface_code.h"
21 #include "daudio_log.h"
22 
23 #undef DH_LOG_TAG
24 #define DH_LOG_TAG "DAudioSinkProxy"
25 
26 namespace OHOS {
27 namespace DistributedHardware {
InitSink(const std::string & params,const sptr<IDAudioSinkIpcCallback> & sinkCallback)28 int32_t DAudioSinkProxy::InitSink(const std::string &params, const sptr<IDAudioSinkIpcCallback> &sinkCallback)
29 {
30     MessageParcel data;
31     MessageParcel reply;
32     MessageOption option;
33     if (!data.WriteInterfaceToken(GetDescriptor())) {
34         return ERR_DH_AUDIO_SA_WRITE_INTERFACE_TOKEN_FAILED;
35     }
36 
37     if (Remote() == nullptr || sinkCallback == nullptr) {
38         DHLOGE("remote service or sinkCallback is null.");
39         return ERR_DH_AUDIO_NULLPTR;
40     }
41 
42     if (!data.WriteString(params) || !data.WriteRemoteObject(sinkCallback->AsObject())) {
43         return ERR_DH_AUDIO_SA_WRITE_PARAM_FAIED;
44     }
45 
46     Remote()->SendRequest(static_cast<uint32_t>(IDAudioSinkInterfaceCode::INIT_SINK), data, reply, option);
47     int32_t ret = reply.ReadInt32();
48     return ret;
49 }
50 
ReleaseSink()51 int32_t DAudioSinkProxy::ReleaseSink()
52 {
53     MessageParcel data;
54     MessageParcel reply;
55     MessageOption option;
56     if (!data.WriteInterfaceToken(GetDescriptor())) {
57         return ERR_DH_AUDIO_SA_WRITE_INTERFACE_TOKEN_FAILED;
58     }
59 
60     if (Remote() == nullptr) {
61         DHLOGE("remote service is null.");
62         return ERR_DH_AUDIO_NULLPTR;
63     }
64     Remote()->SendRequest(static_cast<uint32_t>(IDAudioSinkInterfaceCode::RELEASE_SINK), data, reply, option);
65     int32_t ret = reply.ReadInt32();
66     return ret;
67 }
68 
SubscribeLocalHardware(const std::string & dhId,const std::string & param)69 int32_t DAudioSinkProxy::SubscribeLocalHardware(const std::string &dhId, const std::string &param)
70 {
71     MessageParcel data;
72     MessageParcel reply;
73     MessageOption option;
74     if (!data.WriteInterfaceToken(GetDescriptor())) {
75         return ERR_DH_AUDIO_SA_WRITE_INTERFACE_TOKEN_FAILED;
76     }
77     if (dhId.length() > DAUDIO_MAX_DEVICE_ID_LEN) {
78         return ERR_DH_AUDIO_SA_DEVID_ILLEGAL;
79     }
80     if (!data.WriteString(dhId) || !data.WriteString(param)) {
81         return ERR_DH_AUDIO_SA_WRITE_PARAM_FAIED;
82     }
83 
84     if (Remote() == nullptr) {
85         DHLOGE("remote service is null.");
86         return ERR_DH_AUDIO_NULLPTR;
87     }
88     Remote()->SendRequest(static_cast<uint32_t>(IDAudioSinkInterfaceCode::SUBSCRIBE_LOCAL_HARDWARE),
89         data, reply, option);
90     int32_t ret = reply.ReadInt32();
91     return ret;
92 }
93 
UnsubscribeLocalHardware(const std::string & dhId)94 int32_t DAudioSinkProxy::UnsubscribeLocalHardware(const std::string &dhId)
95 {
96     MessageParcel data;
97     MessageParcel reply;
98     MessageOption option;
99     if (!data.WriteInterfaceToken(GetDescriptor())) {
100         return ERR_DH_AUDIO_SA_WRITE_INTERFACE_TOKEN_FAILED;
101     }
102     if (dhId.length() > DAUDIO_MAX_DEVICE_ID_LEN) {
103         return ERR_DH_AUDIO_SA_DEVID_ILLEGAL;
104     }
105     if (!data.WriteString(dhId)) {
106         return ERR_DH_AUDIO_SA_WRITE_PARAM_FAIED;
107     }
108 
109     if (Remote() == nullptr) {
110         DHLOGE("remote service is null.");
111         return ERR_DH_AUDIO_NULLPTR;
112     }
113     Remote()->SendRequest(static_cast<uint32_t>(IDAudioSinkInterfaceCode::UNSUBSCRIBE_LOCAL_HARDWARE),
114         data, reply, option);
115     int32_t ret = reply.ReadInt32();
116     return ret;
117 }
118 
DAudioNotify(const std::string & devId,const std::string & dhId,const int32_t eventType,const std::string & eventContent)119 void DAudioSinkProxy::DAudioNotify(const std::string &devId, const std::string &dhId, const int32_t eventType,
120     const std::string &eventContent)
121 {
122     MessageParcel data;
123     MessageParcel reply;
124     MessageOption option;
125     if (!data.WriteInterfaceToken(GetDescriptor())) {
126         return;
127     }
128     if (devId.length() > DAUDIO_MAX_DEVICE_ID_LEN || dhId.length() > DAUDIO_MAX_DEVICE_ID_LEN) {
129         return;
130     }
131     if (!data.WriteString(devId) || !data.WriteString(dhId) || !data.WriteInt32(eventType) ||
132         !data.WriteString(eventContent)) {
133         return;
134     }
135 
136     if (Remote() == nullptr) {
137         DHLOGE("remote service is null.");
138         return;
139     }
140     Remote()->SendRequest(static_cast<uint32_t>(IDAudioSinkInterfaceCode::DAUDIO_NOTIFY), data, reply, option);
141 }
142 
PauseDistributedHardware(const std::string & networkId)143 int32_t DAudioSinkProxy::PauseDistributedHardware(const std::string &networkId)
144 {
145     MessageParcel data;
146     MessageParcel reply;
147     MessageOption option;
148     if (!data.WriteInterfaceToken(GetDescriptor())) {
149         return ERR_DH_AUDIO_SA_WRITE_INTERFACE_TOKEN_FAILED;
150     }
151 
152     if (!data.WriteString(networkId)) {
153         return ERR_DH_AUDIO_SA_WRITE_PARAM_FAIED;
154     }
155 
156     if (Remote() == nullptr) {
157         DHLOGE("remote service is null.");
158         return ERR_DH_AUDIO_NULLPTR;
159     }
160     Remote()->SendRequest(static_cast<uint32_t>(IDAudioSinkInterfaceCode::PAUSE_DISTRIBUTED_HARDWARE),
161         data, reply, option);
162     return reply.ReadInt32();
163 }
164 
ResumeDistributedHardware(const std::string & networkId)165 int32_t DAudioSinkProxy::ResumeDistributedHardware(const std::string &networkId)
166 {
167     MessageParcel data;
168     MessageParcel reply;
169     MessageOption option;
170     if (!data.WriteInterfaceToken(GetDescriptor())) {
171         return ERR_DH_AUDIO_SA_WRITE_INTERFACE_TOKEN_FAILED;
172     }
173 
174     if (!data.WriteString(networkId)) {
175         return ERR_DH_AUDIO_SA_WRITE_PARAM_FAIED;
176     }
177 
178     if (Remote() == nullptr) {
179         DHLOGE("remote service is null.");
180         return ERR_DH_AUDIO_NULLPTR;
181     }
182     Remote()->SendRequest(static_cast<uint32_t>(IDAudioSinkInterfaceCode::RESUME_DISTRIBUTED_HARDWARE),
183         data, reply, option);
184     return reply.ReadInt32();
185 }
186 
StopDistributedHardware(const std::string & networkId)187 int32_t DAudioSinkProxy::StopDistributedHardware(const std::string &networkId)
188 {
189     MessageParcel data;
190     MessageParcel reply;
191     MessageOption option;
192     if (!data.WriteInterfaceToken(GetDescriptor())) {
193         return ERR_DH_AUDIO_SA_WRITE_INTERFACE_TOKEN_FAILED;
194     }
195 
196     if (!data.WriteString(networkId)) {
197         return ERR_DH_AUDIO_SA_WRITE_PARAM_FAIED;
198     }
199 
200     if (Remote() == nullptr) {
201         DHLOGE("remote service is null.");
202         return ERR_DH_AUDIO_NULLPTR;
203     }
204     Remote()->SendRequest(static_cast<uint32_t>(IDAudioSinkInterfaceCode::STOP_DISTRIBUTED_HARDWARE),
205         data, reply, option);
206     return reply.ReadInt32();
207 }
208 } // namespace DistributedHardware
209 } // namespace OHOS