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_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 ¶ms, 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 (!data.WriteString(params) || !data.WriteRemoteObject(callback->AsObject())) {
38 return ERR_DH_AUDIO_SA_WRITE_PARAM_FAIED;
39 }
40
41 Remote()->SendRequest(static_cast<uint32_t>(IDAudioSourceInterfaceCode::INIT_SOURCE), data, reply, option);
42 int32_t ret = reply.ReadInt32();
43 return ret;
44 }
45
ReleaseSource()46 int32_t DAudioSourceProxy::ReleaseSource()
47 {
48 MessageParcel data;
49 MessageParcel reply;
50 MessageOption option;
51 if (!data.WriteInterfaceToken(GetDescriptor())) {
52 return ERR_DH_AUDIO_SA_WRITE_INTERFACE_TOKEN_FAILED;
53 }
54
55 Remote()->SendRequest(static_cast<uint32_t>(IDAudioSourceInterfaceCode::RELEASE_SOURCE), data, reply, option);
56 int32_t ret = reply.ReadInt32();
57 return ret;
58 }
59
RegisterDistributedHardware(const std::string & devId,const std::string & dhId,const EnableParam & param,const std::string & reqId)60 int32_t DAudioSourceProxy::RegisterDistributedHardware(const std::string &devId, const std::string &dhId,
61 const EnableParam ¶m, const std::string &reqId)
62 {
63 MessageParcel data;
64 MessageParcel reply;
65 MessageOption option;
66 if (!data.WriteInterfaceToken(GetDescriptor())) {
67 return ERR_DH_AUDIO_SA_WRITE_INTERFACE_TOKEN_FAILED;
68 }
69 if (devId.length() > DAUDIO_MAX_DEVICE_ID_LEN || dhId.length() > DAUDIO_MAX_DEVICE_ID_LEN ||
70 reqId.length() > DAUDIO_MAX_DEVICE_ID_LEN) {
71 return ERR_DH_AUDIO_SA_DEVID_ILLEGAL;
72 }
73 if (!data.WriteString(devId) || !data.WriteString(dhId) || !data.WriteString(param.version) ||
74 !data.WriteString(param.attrs) || !data.WriteString(reqId)) {
75 return ERR_DH_AUDIO_SA_WRITE_PARAM_FAIED;
76 }
77
78 Remote()->SendRequest(static_cast<uint32_t>(IDAudioSourceInterfaceCode::REGISTER_DISTRIBUTED_HARDWARE),
79 data, reply, option);
80 int32_t ret = reply.ReadInt32();
81 return ret;
82 }
83
UnregisterDistributedHardware(const std::string & devId,const std::string & dhId,const std::string & reqId)84 int32_t DAudioSourceProxy::UnregisterDistributedHardware(const std::string &devId, const std::string &dhId,
85 const std::string &reqId)
86 {
87 MessageParcel data;
88 MessageParcel reply;
89 MessageOption option;
90 if (!data.WriteInterfaceToken(GetDescriptor())) {
91 return ERR_DH_AUDIO_SA_WRITE_INTERFACE_TOKEN_FAILED;
92 }
93 if (devId.length() > DAUDIO_MAX_DEVICE_ID_LEN || dhId.length() > DAUDIO_MAX_DEVICE_ID_LEN ||
94 reqId.length() > DAUDIO_MAX_DEVICE_ID_LEN) {
95 return ERR_DH_AUDIO_SA_DEVID_ILLEGAL;
96 }
97 if (!data.WriteString(devId) || !data.WriteString(dhId) || !data.WriteString(reqId)) {
98 return ERR_DH_AUDIO_SA_WRITE_PARAM_FAIED;
99 }
100
101 Remote()->SendRequest(static_cast<uint32_t>(IDAudioSourceInterfaceCode::UNREGISTER_DISTRIBUTED_HARDWARE),
102 data, reply, option);
103 int32_t ret = reply.ReadInt32();
104 return ret;
105 }
106
ConfigDistributedHardware(const std::string & devId,const std::string & dhId,const std::string & key,const std::string & value)107 int32_t DAudioSourceProxy::ConfigDistributedHardware(const std::string &devId, const std::string &dhId,
108 const std::string &key, const std::string &value)
109 {
110 MessageParcel data;
111 MessageParcel reply;
112 MessageOption option;
113 if (!data.WriteInterfaceToken(GetDescriptor())) {
114 return ERR_DH_AUDIO_SA_WRITE_INTERFACE_TOKEN_FAILED;
115 }
116 if (devId.length() > DAUDIO_MAX_DEVICE_ID_LEN || dhId.length() > DAUDIO_MAX_DEVICE_ID_LEN) {
117 return ERR_DH_AUDIO_SA_DEVID_ILLEGAL;
118 }
119 if (!data.WriteString(devId) || !data.WriteString(dhId) || !data.WriteString(key) || !data.WriteString(value)) {
120 return ERR_DH_AUDIO_SA_WRITE_PARAM_FAIED;
121 }
122
123 Remote()->SendRequest(static_cast<uint32_t>(IDAudioSourceInterfaceCode::CONFIG_DISTRIBUTED_HARDWARE),
124 data, reply, option);
125 int32_t ret = reply.ReadInt32();
126 return ret;
127 }
128
DAudioNotify(const std::string & devId,const std::string & dhId,const int32_t eventType,const std::string & eventContent)129 void DAudioSourceProxy::DAudioNotify(const std::string &devId, const std::string &dhId, const int32_t eventType,
130 const std::string &eventContent)
131 {
132 MessageParcel data;
133 MessageParcel reply;
134 MessageOption option;
135 if (!data.WriteInterfaceToken(GetDescriptor())) {
136 return;
137 }
138 if (devId.length() > DAUDIO_MAX_DEVICE_ID_LEN || dhId.length() > DAUDIO_MAX_DEVICE_ID_LEN) {
139 return;
140 }
141 if (!data.WriteString(devId) || !data.WriteString(dhId) || !data.WriteInt32(eventType) ||
142 !data.WriteString(eventContent)) {
143 return;
144 }
145
146 Remote()->SendRequest(static_cast<uint32_t>(IDAudioSourceInterfaceCode::DAUDIO_NOTIFY),
147 data, reply, option);
148 }
149 } // namespace DistributedHardware
150 } // namespace OHOS