1 /*
2  * Copyright (c) 2022-2024 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_source_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 "DAudioSourceProxy"
25 
26 namespace OHOS {
27 namespace DistributedHardware {
InitSource(const std::string & params,const sptr<IDAudioIpcCallback> & callback)28 int32_t DAudioSourceProxy::InitSource(const std::string &params, const sptr<IDAudioIpcCallback> &callback)
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 || callback == nullptr) {
38         DHLOGE("remote service or callback is null.");
39         return ERR_DH_AUDIO_NULLPTR;
40     }
41 
42     if (!data.WriteString(params) || !data.WriteRemoteObject(callback->AsObject())) {
43         return ERR_DH_AUDIO_SA_WRITE_PARAM_FAIED;
44     }
45 
46     Remote()->SendRequest(static_cast<uint32_t>(IDAudioSourceInterfaceCode::INIT_SOURCE), data, reply, option);
47     int32_t ret = reply.ReadInt32();
48     return ret;
49 }
50 
ReleaseSource()51 int32_t DAudioSourceProxy::ReleaseSource()
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>(IDAudioSourceInterfaceCode::RELEASE_SOURCE), data, reply, option);
65     int32_t ret = reply.ReadInt32();
66     return ret;
67 }
68 
RegisterDistributedHardware(const std::string & devId,const std::string & dhId,const EnableParam & param,const std::string & reqId)69 int32_t DAudioSourceProxy::RegisterDistributedHardware(const std::string &devId, const std::string &dhId,
70     const EnableParam &param, const std::string &reqId)
71 {
72     MessageParcel data;
73     MessageParcel reply;
74     MessageOption option;
75     if (!data.WriteInterfaceToken(GetDescriptor())) {
76         return ERR_DH_AUDIO_SA_WRITE_INTERFACE_TOKEN_FAILED;
77     }
78     if (devId.length() > DAUDIO_MAX_DEVICE_ID_LEN || dhId.length() > DAUDIO_MAX_DEVICE_ID_LEN ||
79         reqId.length() > DAUDIO_MAX_DEVICE_ID_LEN) {
80         return ERR_DH_AUDIO_SA_DEVID_ILLEGAL;
81     }
82     if (!data.WriteString(devId) || !data.WriteString(dhId) || !data.WriteString(param.sinkVersion) ||
83         !data.WriteString(param.sinkAttrs) || !data.WriteString(reqId)) {
84         return ERR_DH_AUDIO_SA_WRITE_PARAM_FAIED;
85     }
86 
87     if (Remote() == nullptr) {
88         DHLOGE("remote service is null.");
89         return ERR_DH_AUDIO_NULLPTR;
90     }
91     Remote()->SendRequest(static_cast<uint32_t>(IDAudioSourceInterfaceCode::REGISTER_DISTRIBUTED_HARDWARE),
92         data, reply, option);
93     int32_t ret = reply.ReadInt32();
94     return ret;
95 }
96 
UnregisterDistributedHardware(const std::string & devId,const std::string & dhId,const std::string & reqId)97 int32_t DAudioSourceProxy::UnregisterDistributedHardware(const std::string &devId, const std::string &dhId,
98     const std::string &reqId)
99 {
100     MessageParcel data;
101     MessageParcel reply;
102     MessageOption option;
103     if (!data.WriteInterfaceToken(GetDescriptor())) {
104         return ERR_DH_AUDIO_SA_WRITE_INTERFACE_TOKEN_FAILED;
105     }
106     if (devId.length() > DAUDIO_MAX_DEVICE_ID_LEN || dhId.length() > DAUDIO_MAX_DEVICE_ID_LEN ||
107         reqId.length() > DAUDIO_MAX_DEVICE_ID_LEN) {
108         return ERR_DH_AUDIO_SA_DEVID_ILLEGAL;
109     }
110     if (!data.WriteString(devId) || !data.WriteString(dhId) || !data.WriteString(reqId)) {
111         return ERR_DH_AUDIO_SA_WRITE_PARAM_FAIED;
112     }
113 
114     if (Remote() == nullptr) {
115         DHLOGE("remote service is null.");
116         return ERR_DH_AUDIO_NULLPTR;
117     }
118     Remote()->SendRequest(static_cast<uint32_t>(IDAudioSourceInterfaceCode::UNREGISTER_DISTRIBUTED_HARDWARE),
119         data, reply, option);
120     int32_t ret = reply.ReadInt32();
121     return ret;
122 }
123 
ConfigDistributedHardware(const std::string & devId,const std::string & dhId,const std::string & key,const std::string & value)124 int32_t DAudioSourceProxy::ConfigDistributedHardware(const std::string &devId, const std::string &dhId,
125     const std::string &key, const std::string &value)
126 {
127     MessageParcel data;
128     MessageParcel reply;
129     MessageOption option;
130     if (!data.WriteInterfaceToken(GetDescriptor())) {
131         return ERR_DH_AUDIO_SA_WRITE_INTERFACE_TOKEN_FAILED;
132     }
133     if (devId.length() > DAUDIO_MAX_DEVICE_ID_LEN || dhId.length() > DAUDIO_MAX_DEVICE_ID_LEN) {
134         return ERR_DH_AUDIO_SA_DEVID_ILLEGAL;
135     }
136     if (!data.WriteString(devId) || !data.WriteString(dhId) || !data.WriteString(key) || !data.WriteString(value)) {
137         return ERR_DH_AUDIO_SA_WRITE_PARAM_FAIED;
138     }
139 
140     if (Remote() == nullptr) {
141         DHLOGE("remote service is null.");
142         return ERR_DH_AUDIO_NULLPTR;
143     }
144     Remote()->SendRequest(static_cast<uint32_t>(IDAudioSourceInterfaceCode::CONFIG_DISTRIBUTED_HARDWARE),
145         data, reply, option);
146     int32_t ret = reply.ReadInt32();
147     return ret;
148 }
149 
DAudioNotify(const std::string & devId,const std::string & dhId,const int32_t eventType,const std::string & eventContent)150 void DAudioSourceProxy::DAudioNotify(const std::string &devId, const std::string &dhId, const int32_t eventType,
151     const std::string &eventContent)
152 {
153     MessageParcel data;
154     MessageParcel reply;
155     MessageOption option;
156     if (!data.WriteInterfaceToken(GetDescriptor())) {
157         return;
158     }
159     if (devId.length() > DAUDIO_MAX_DEVICE_ID_LEN || dhId.length() > DAUDIO_MAX_DEVICE_ID_LEN) {
160         return;
161     }
162     if (!data.WriteString(devId) || !data.WriteString(dhId) || !data.WriteInt32(eventType) ||
163         !data.WriteString(eventContent)) {
164         return;
165     }
166 
167     if (Remote() == nullptr) {
168         DHLOGE("remote service is null.");
169         return;
170     }
171     Remote()->SendRequest(static_cast<uint32_t>(IDAudioSourceInterfaceCode::DAUDIO_NOTIFY),
172         data, reply, option);
173 }
174 } // namespace DistributedHardware
175 } // namespace OHOS