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