1 /*
2 * Copyright (C) 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 #include "bluetooth_errorcode.h"
16 #include "bluetooth_pan_proxy.h"
17 #include "bluetooth_log.h"
18 #include "refbase.h"
19
20 namespace OHOS {
21 namespace Bluetooth {
Disconnect(const BluetoothRawAddress & device)22 int32_t BluetoothPanProxy::Disconnect(const BluetoothRawAddress &device)
23 {
24 MessageParcel data;
25 if (!data.WriteInterfaceToken(BluetoothPanProxy::GetDescriptor())) {
26 HILOGE("BluetoothPanProxy::Disconnect WriteInterfaceToken error");
27 return BT_ERR_IPC_TRANS_FAILED;
28 }
29 if (!data.WriteParcelable(&device)) {
30 HILOGE("BluetoothPanProxy::Disconnect write device error");
31 return BT_ERR_IPC_TRANS_FAILED;
32 }
33
34 MessageParcel reply;
35 MessageOption option {
36 MessageOption::TF_SYNC
37 };
38
39 int error = Remote()->SendRequest(
40 static_cast<uint32_t>(BluetoothPanInterfaceCode::COMMAND_DISCONNECT), data, reply, option);
41 if (error != BT_NO_ERROR) {
42 HILOGE("BluetoothPanProxy::Disconnect done fail, error: %{public}d", error);
43 return BT_ERR_IPC_TRANS_FAILED;
44 }
45 return reply.ReadInt32();
46 }
47
GetDeviceState(const BluetoothRawAddress & device,int32_t & state)48 int32_t BluetoothPanProxy::GetDeviceState(const BluetoothRawAddress &device, int32_t &state)
49 {
50 MessageParcel data;
51 if (!data.WriteInterfaceToken(BluetoothPanProxy::GetDescriptor())) {
52 HILOGE("WriteInterfaceToken error");
53 return BT_ERR_IPC_TRANS_FAILED;
54 }
55 if (!data.WriteParcelable(&device)) {
56 HILOGE("write device error");
57 return BT_ERR_IPC_TRANS_FAILED;
58 }
59
60 MessageParcel reply;
61 MessageOption option {
62 MessageOption::TF_SYNC
63 };
64
65 int error = Remote()->SendRequest(
66 static_cast<uint32_t>(BluetoothPanInterfaceCode::COMMAND_GET_DEVICE_STATE), data, reply, option);
67 if (error != BT_NO_ERROR) {
68 HILOGE("done fail, error: %{public}d", error);
69 return BT_ERR_IPC_TRANS_FAILED;
70 }
71 // read error code
72 int32_t errCode = reply.ReadInt32();
73 if (errCode != BT_NO_ERROR) {
74 HILOGE("reply errCode: %{public}d", errCode);
75 return errCode;
76 }
77 // read state
78 state = reply.ReadInt32();
79 return BT_NO_ERROR;
80 }
81
GetDevicesByStates(const std::vector<int32_t> & states,std::vector<BluetoothRawAddress> & result)82 int32_t BluetoothPanProxy::GetDevicesByStates(const std::vector<int32_t> &states,
83 std::vector<BluetoothRawAddress>& result)
84 {
85 MessageParcel data;
86 if (!data.WriteInterfaceToken(BluetoothPanProxy::GetDescriptor())) {
87 HILOGE("WriteInterfaceToken error");
88 return BT_ERR_IPC_TRANS_FAILED;
89 }
90 if (!WriteParcelableInt32Vector(states, data)) {
91 HILOGE("write result failed");
92 return BT_ERR_IPC_TRANS_FAILED;
93 }
94
95 MessageParcel reply;
96 MessageOption option = {MessageOption::TF_SYNC};
97 int error = Remote()->SendRequest(
98 static_cast<uint32_t>(BluetoothPanInterfaceCode::COMMAND_GET_DEVICES_BY_STATES), data, reply, option);
99 if (error != BT_NO_ERROR) {
100 HILOGE("SendRequest failed, error: %{public}d", error);
101 return BT_ERR_IPC_TRANS_FAILED;
102 }
103 // read error code
104 int32_t errCode = reply.ReadInt32();
105 if (errCode != BT_NO_ERROR) {
106 HILOGE("reply errCode: %{public}d", errCode);
107 return errCode;
108 }
109
110 // read size
111 int32_t rawAddsSize = reply.ReadInt32();
112
113 // read devices
114 for (int i = 0; i < rawAddsSize; i++) {
115 std::unique_ptr<BluetoothRawAddress> address(reply.ReadParcelable<BluetoothRawAddress>());
116 result.push_back(*address);
117 }
118 return BT_NO_ERROR;
119 }
120
RegisterObserver(const sptr<IBluetoothPanObserver> observer)121 ErrCode BluetoothPanProxy::RegisterObserver(const sptr<IBluetoothPanObserver> observer)
122 {
123 MessageParcel data;
124 if (!data.WriteInterfaceToken(BluetoothPanProxy::GetDescriptor())) {
125 HILOGE("BluetoothPanProxy::RegisterObserver WriteInterfaceToken error");
126 return IPC_PROXY_TRANSACTION_ERR;
127 }
128 if (!data.WriteRemoteObject(observer->AsObject())) {
129 HILOGE("BluetoothPanProxy::RegisterObserver error");
130 return INVALID_DATA;
131 }
132
133 MessageParcel reply;
134 MessageOption option {
135 MessageOption::TF_ASYNC
136 };
137
138 int error = Remote()->SendRequest(
139 static_cast<uint32_t>(BluetoothPanInterfaceCode::COMMAND_REGISTER_OBSERVER), data, reply, option);
140 if (error != NO_ERROR) {
141 HILOGE("BluetoothPanProxy::RegisterObserver done fail, error: %{public}d", error);
142 return INVALID_DATA;
143 }
144 return error;
145 }
146
DeregisterObserver(const sptr<IBluetoothPanObserver> observer)147 ErrCode BluetoothPanProxy::DeregisterObserver(const sptr<IBluetoothPanObserver> observer)
148 {
149 MessageParcel data;
150 if (!data.WriteInterfaceToken(BluetoothPanProxy::GetDescriptor())) {
151 HILOGE("BluetoothPanProxy::DeregisterObserver WriteInterfaceToken error");
152 return IPC_PROXY_TRANSACTION_ERR;
153 }
154 if (!data.WriteRemoteObject(observer->AsObject())) {
155 HILOGE("BluetoothPanProxy::DeregisterObserver error");
156 return INVALID_DATA;
157 }
158
159 MessageParcel reply;
160 MessageOption option {
161 MessageOption::TF_ASYNC
162 };
163
164 int error = Remote()->SendRequest(
165 static_cast<uint32_t>(BluetoothPanInterfaceCode::COMMAND_DEREGISTER_OBSERVER), data, reply, option);
166 if (error != NO_ERROR) {
167 HILOGE("BluetoothPanProxy::DeregisterObserver done fail, error: %{public}d", error);
168 return INVALID_DATA;
169 }
170 return error;
171 }
172
SetTethering(const bool value)173 int32_t BluetoothPanProxy::SetTethering(const bool value)
174 {
175 MessageParcel data;
176 if (!data.WriteInterfaceToken(BluetoothPanProxy::GetDescriptor())) {
177 HILOGE("BluetoothPanProxy::SetTethering WriteInterfaceToken error");
178 return BT_ERR_IPC_TRANS_FAILED;
179 }
180 if (!data.WriteBool(value)) {
181 HILOGE("BluetoothPanProxy::SetTethering error");
182 return BT_ERR_IPC_TRANS_FAILED;
183 }
184
185 MessageParcel reply;
186 MessageOption option {
187 MessageOption::TF_SYNC
188 };
189
190 int error = Remote()->SendRequest(
191 static_cast<uint32_t>(BluetoothPanInterfaceCode::COMMAND_SET_TETHERING), data, reply, option);
192 if (error != BT_NO_ERROR) {
193 HILOGE("BluetoothPanProxy::SetTethering done fail, error: %{public}d", error);
194 return BT_ERR_IPC_TRANS_FAILED;
195 }
196 return reply.ReadInt32();
197 }
198
IsTetheringOn(bool & result)199 int32_t BluetoothPanProxy::IsTetheringOn(bool &result)
200 {
201 MessageParcel data;
202 if (!data.WriteInterfaceToken(BluetoothPanProxy::GetDescriptor())) {
203 HILOGE("BluetoothPanProxy::IsTetheringOn WriteInterfaceToken error");
204 return BT_ERR_IPC_TRANS_FAILED;
205 }
206
207 MessageParcel reply;
208 MessageOption option {
209 MessageOption::TF_SYNC
210 };
211
212 int error = Remote()->SendRequest(
213 static_cast<uint32_t>(BluetoothPanInterfaceCode::COMMAND_IS_TETHERING_ON), data, reply, option);
214 if (error != BT_NO_ERROR) {
215 HILOGE("BluetoothPanProxy::IsTetheringOn done fail, error: %{public}d", error);
216 return BT_ERR_IPC_TRANS_FAILED;
217 }
218
219 int32_t ret = reply.ReadInt32();
220 if (ret != BT_NO_ERROR) {
221 HILOGE("internal error. ret:%{public}d", ret);
222 return ret;
223 }
224
225 result = reply.ReadInt32();
226 return BT_NO_ERROR;
227 }
228
WriteParcelableInt32Vector(const std::vector<int32_t> & parcelableVector,Parcel & reply)229 bool BluetoothPanProxy::WriteParcelableInt32Vector(const std::vector<int32_t> &parcelableVector, Parcel &reply)
230 {
231 if (!reply.WriteInt32(parcelableVector.size())) {
232 HILOGE("write ParcelableVector failed");
233 return false;
234 }
235
236 for (auto parcelable : parcelableVector) {
237 if (!reply.WriteInt32(parcelable)) {
238 HILOGE("write ParcelableVector failed");
239 return false;
240 }
241 }
242 return true;
243 }
244 } // namespace Bluetooth
245 } // namespace OHOS