1 /*
2 * Copyright (C) 2021 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 "bluetooth_map_mse_proxy.h"
17 #include "bluetooth_log.h"
18 #include "bluetooth_errorcode.h"
19
20 namespace OHOS {
21 namespace Bluetooth {
RegisterObserver(const sptr<IBluetoothMapMseObserver> & observer)22 void BluetoothMapMseProxy::RegisterObserver(
23 const sptr<IBluetoothMapMseObserver> &observer)
24 {
25 HILOGI("BluetoothMapMseProxy::RegisterObserver Triggered!");
26 MessageParcel data;
27 if (!data.WriteInterfaceToken(BluetoothMapMseProxy::GetDescriptor())) {
28 HILOGE("BluetoothMapMseProxy::RegisterObserver WriteInterfaceToken error");
29 return;
30 }
31 if (!data.WriteRemoteObject(observer->AsObject())) {
32 HILOGE("BluetoothMapMseProxy::RegisterObserver error");
33 return;
34 }
35 MessageParcel reply;
36 MessageOption option {
37 MessageOption::TF_ASYNC
38 };
39 int error = Remote()->SendRequest(
40 IBluetoothMapMse::Code::MSE_REGISTER_OBSERVER, data, reply, option);
41 if (error != NO_ERROR) {
42 HILOGE("BluetoothMapMseProxy::RegisterObserver done fail, error: %{public}d", error);
43 return;
44 }
45 }
46
DeregisterObserver(const sptr<IBluetoothMapMseObserver> & observer)47 void BluetoothMapMseProxy::DeregisterObserver(
48 const sptr<IBluetoothMapMseObserver> &observer)
49 {
50 HILOGI("BluetoothMapMseProxy::DeregisterObserver Triggered!");
51 MessageParcel data;
52 if (!data.WriteInterfaceToken(BluetoothMapMseProxy::GetDescriptor())) {
53 HILOGE("BluetoothMapMseProxy::DeregisterObserver WriteInterfaceToken error");
54 return;
55 }
56 if (!data.WriteRemoteObject(observer->AsObject())) {
57 HILOGE("BluetoothMapMseProxy::DeregisterObserver error");
58 return;
59 }
60 MessageParcel reply;
61 MessageOption option {
62 MessageOption::TF_ASYNC
63 };
64 int error = Remote()->SendRequest(
65 IBluetoothMapMse::Code::MSE_DE_REGISTER_OBSERVER, data, reply, option);
66 if (error != NO_ERROR) {
67 HILOGE("BluetoothMapMseProxy::DeregisterObserver done fail, error: %{public}d", error);
68 return;
69 }
70 }
71
GetState(int32_t & ret)72 void BluetoothMapMseProxy::GetState(
73 int32_t &ret)
74 {
75 HILOGI("BluetoothMapMseProxy::Connect Triggered!");
76 MessageParcel data;
77 if (!data.WriteInterfaceToken(BluetoothMapMseProxy::GetDescriptor())) {
78 HILOGE("BluetoothMapMseProxy::Connect WriteInterfaceToken error");
79 return;
80 }
81 MessageParcel reply;
82 MessageOption option {
83 MessageOption::TF_ASYNC
84 };
85 int error = Remote()->SendRequest(
86 IBluetoothMapMse::Code::MSE_GET_STATE, data, reply, option);
87 if (error != NO_ERROR) {
88 HILOGE("BluetoothMapMseProxy::Connect done fail, error: %{public}d", error);
89 return;
90 }
91 ret = reply.ReadInt32();
92 }
93
Disconnect(const BluetoothRawAddress & device,int32_t & ret)94 void BluetoothMapMseProxy::Disconnect(
95 const BluetoothRawAddress &device, int32_t &ret)
96 {
97 HILOGI("BluetoothMapMseProxy::Disconnect Triggered!");
98 MessageParcel data;
99 if (!data.WriteInterfaceToken(BluetoothMapMseProxy::GetDescriptor())) {
100 HILOGE("BluetoothMapMseProxy::Disconnect WriteInterfaceToken error");
101 return;
102 }
103 if (!data.WriteParcelable(&device)) {
104 HILOGE("BluetoothMapMseProxy::Disconnect error");
105 return;
106 }
107 MessageParcel reply;
108 MessageOption option {
109 MessageOption::TF_ASYNC
110 };
111 int error = Remote()->SendRequest(
112 IBluetoothMapMse::Code::MSE_DISCONNECT, data, reply, option);
113 if (error != NO_ERROR) {
114 HILOGE("BluetoothMapMseProxy::Disconnect done fail, error: %{public}d", error);
115 return;
116 }
117 ret = reply.ReadInt32();
118 }
119
IsConnected(const BluetoothRawAddress & device,bool & ret)120 void BluetoothMapMseProxy::IsConnected(
121 const BluetoothRawAddress &device, bool &ret)
122 {
123 HILOGI("BluetoothMapMseProxy::IsConnected Triggered!");
124 MessageParcel data;
125 if (!data.WriteInterfaceToken(BluetoothMapMseProxy::GetDescriptor())) {
126 HILOGE("BluetoothMapMseProxy::IsConnected WriteInterfaceToken error");
127 return;
128 }
129 if (!data.WriteParcelable(&device)) {
130 HILOGE("BluetoothMapMseProxy::IsConnected error");
131 return;
132 }
133 MessageParcel reply;
134 MessageOption option {
135 MessageOption::TF_ASYNC
136 };
137 int error = Remote()->SendRequest(
138 IBluetoothMapMse::Code::MSE_ISCONNECTED, data, reply, option);
139 if (error != NO_ERROR) {
140 HILOGE("BluetoothMapMseProxy::IsConnected done fail, error: %{public}d", error);
141 return;
142 }
143 ret = reply.ReadBool();
144 }
145
GetConnectedDevices(std::vector<BluetoothRawAddress> & devices)146 void BluetoothMapMseProxy::GetConnectedDevices(
147 std::vector<BluetoothRawAddress> &devices)
148 {
149 HILOGI("BluetoothMapMseProxy::GetConnectDevices Triggered!");
150 MessageParcel data;
151 if (!data.WriteInterfaceToken(BluetoothMapMseProxy::GetDescriptor())) {
152 HILOGE("BluetoothMapMseProxy::GetConnectDevices WriteInterfaceToken error");
153 return;
154 }
155 MessageParcel reply;
156 MessageOption option {
157 MessageOption::TF_ASYNC
158 };
159 int error = Remote()->SendRequest(
160 IBluetoothMapMse::Code::MSE_GET_CONNECTED_DEVICES, data, reply, option);
161 if (error != NO_ERROR) {
162 HILOGE("BluetoothMapMseProxy::GetConnectDevices done fail, error: %{public}d", error);
163 return;
164 }
165 int dev_num = reply.ReadInt32();
166 for (int i = dev_num; i > 0; i--) {
167 std::unique_ptr<BluetoothRawAddress> dev(reply.ReadParcelable<BluetoothRawAddress>());
168 devices.push_back(*dev);
169 }
170 }
171
GetDevicesByStates(const std::vector<int> & statusList,std::vector<BluetoothRawAddress> & devices)172 void BluetoothMapMseProxy::GetDevicesByStates(
173 const std::vector<int> &statusList, std::vector<BluetoothRawAddress> &devices)
174 {
175 HILOGI("BluetoothMapMseProxy::GetDevicesByStates Triggered!");
176 MessageParcel data;
177 if (!data.WriteInterfaceToken(BluetoothMapMseProxy::GetDescriptor())) {
178 HILOGE("BluetoothMapMseProxy::GetDevicesByStates WriteInterfaceToken error");
179 return;
180 }
181 if (!data.WriteInt32Vector(statusList)) {
182 HILOGE("BluetoothMapMseProxy::GetDevicesByStates transport error");
183 return;
184 }
185 MessageParcel reply;
186 MessageOption option {
187 MessageOption::TF_ASYNC
188 };
189 int error = Remote()->SendRequest(
190 IBluetoothMapMse::Code::MSE_GET_DEVICES_BY_STATES, data, reply, option);
191 if (error != NO_ERROR) {
192 HILOGE("BluetoothMapMseProxy::GetDevicesByStates done fail, error: %{public}d", error);
193 return;
194 }
195 int dev_num = reply.ReadInt32();
196 for (int i = dev_num; i > 0; i--) {
197 std::unique_ptr<BluetoothRawAddress> dev(reply.ReadParcelable<BluetoothRawAddress>());
198 devices.push_back(*dev);
199 }
200 }
201
GetConnectionState(const BluetoothRawAddress & device,int32_t & ret)202 void BluetoothMapMseProxy::GetConnectionState(
203 const BluetoothRawAddress &device, int32_t &ret)
204 {
205 HILOGI("BluetoothMapMseProxy::GetConnectionState Triggered!");
206 MessageParcel data;
207 if (!data.WriteInterfaceToken(BluetoothMapMseProxy::GetDescriptor())) {
208 HILOGE("BluetoothMapMseProxy::GetConnectionState WriteInterfaceToken error");
209 return;
210 }
211 if (!data.WriteParcelable(&device)) {
212 HILOGE("BluetoothMapMseProxy::IsConnected error");
213 return;
214 }
215 MessageParcel reply;
216 MessageOption option {
217 MessageOption::TF_ASYNC
218 };
219 int error = Remote()->SendRequest(
220 IBluetoothMapMse::Code::MSE_GET_CONNECTION_STATE, data, reply, option);
221 if (error != NO_ERROR) {
222 HILOGE("BluetoothMapMseProxy::GetConnectionState done fail, error: %{public}d", error);
223 return;
224 }
225 ret = reply.ReadInt32();
226 }
227
SetConnectionStrategy(const BluetoothRawAddress & device,int strategy,bool & ret)228 void BluetoothMapMseProxy::SetConnectionStrategy(
229 const BluetoothRawAddress &device, int strategy, bool &ret)
230 {
231 HILOGI("BluetoothMapMseProxy::SetConnectionStrategy Triggered!");
232 MessageParcel data;
233 if (!data.WriteInterfaceToken(BluetoothMapMseProxy::GetDescriptor())) {
234 HILOGE("BluetoothMapMseProxy::SetConnectionStrategy WriteInterfaceToken error");
235 return;
236 }
237 if (!data.WriteParcelable(&device)) {
238 HILOGE("BluetoothMapMseProxy::SetConnectionStrategy error");
239 return;
240 }
241 if (!data.WriteInt32(strategy)) {
242 HILOGE("BluetoothMapMseProxy::SetConnectionStrategy transport error");
243 return;
244 }
245 MessageParcel reply;
246 MessageOption option {
247 MessageOption::TF_ASYNC
248 };
249 int error = Remote()->SendRequest(
250 IBluetoothMapMse::Code::MSE_SET_CONNECTION_STRATEGY, data, reply, option);
251 if (error != NO_ERROR) {
252 HILOGE("BluetoothMapMseProxy::GetDevicesByStates done fail, error: %{public}d", error);
253 return;
254 }
255 ret = reply.ReadBool();
256 }
257
GetConnectionStrategy(const BluetoothRawAddress & device,int32_t & ret)258 void BluetoothMapMseProxy::GetConnectionStrategy(
259 const BluetoothRawAddress &device, int32_t &ret)
260 {
261 HILOGI("BluetoothMapMseProxy::GetConnectionStrategy Triggered!");
262 MessageParcel data;
263 if (!data.WriteInterfaceToken(BluetoothMapMseProxy::GetDescriptor())) {
264 HILOGE("BluetoothMapMseProxy::GetConnectionStrategy WriteInterfaceToken error");
265 return;
266 }
267 if (!data.WriteParcelable(&device)) {
268 HILOGE("BluetoothMapMseProxy::GetConnectionStrategy error");
269 return;
270 }
271 MessageParcel reply;
272 MessageOption option {
273 MessageOption::TF_ASYNC
274 };
275 int error = Remote()->SendRequest(
276 IBluetoothMapMse::Code::MSE_GET_CONNECTION_STRATEGY, data, reply, option);
277 if (error != NO_ERROR) {
278 HILOGE("BluetoothMapMseProxy::GetConnectionStrategy done fail, error: %{public}d", error);
279 return;
280 }
281 ret = reply.ReadInt32();
282 }
283
GrantPermission(const BluetoothRawAddress & device,bool allow,bool save)284 void BluetoothMapMseProxy::GrantPermission(
285 const BluetoothRawAddress &device, bool allow, bool save)
286 {
287 HILOGI("BluetoothMapMseProxy::GrantPermission Triggered!");
288 MessageParcel data;
289 if (!data.WriteInterfaceToken(BluetoothMapMseProxy::GetDescriptor())) {
290 HILOGE("BluetoothMapMseProxy::GrantPermission WriteInterfaceToken error");
291 return;
292 }
293 if (!data.WriteParcelable(&device)) {
294 HILOGE("BluetoothMapMseProxy::GrantPermission error");
295 return;
296 }
297 if (!data.WriteBool(allow)) {
298 HILOGE("BluetoothMapMseProxy::GrantPermission transport error");
299 return;
300 }
301 if (!data.WriteBool(save)) {
302 HILOGE("BluetoothMapMseProxy::GrantPermission transport error");
303 return;
304 }
305 MessageParcel reply;
306 MessageOption option {
307 MessageOption::TF_ASYNC
308 };
309 int error = Remote()->SendRequest(
310 IBluetoothMapMse::Code::MSE_GRANT_PERMISSION, data, reply, option);
311 if (error != NO_ERROR) {
312 HILOGE("BluetoothMapMseProxy::GrantPermission done fail, error: %{public}d", error);
313 return;
314 }
315 }
316 } // namespace Bluetooth
317 } // namespace OHOS
318