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 #ifndef LOG_TAG
16 #define LOG_TAG "bt_ipc_pan_proxy"
17 #endif
18
19 #include "bluetooth_errorcode.h"
20 #include "bluetooth_pan_proxy.h"
21 #include "bluetooth_log.h"
22 #include "refbase.h"
23
24 namespace OHOS {
25 namespace Bluetooth {
Disconnect(const BluetoothRawAddress & device)26 int32_t BluetoothPanProxy::Disconnect(const BluetoothRawAddress &device)
27 {
28 MessageParcel data;
29 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothPanProxy::GetDescriptor()),
30 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
31 CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_IPC_TRANS_FAILED, "write device error");
32
33 MessageParcel reply;
34 MessageOption option(MessageOption::TF_SYNC);
35
36 SEND_IPC_REQUEST_RETURN_RESULT(static_cast<uint32_t>(BluetoothPanInterfaceCode::COMMAND_DISCONNECT),
37 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
38
39 return reply.ReadInt32();
40 }
41
GetDeviceState(const BluetoothRawAddress & device,int32_t & state)42 int32_t BluetoothPanProxy::GetDeviceState(const BluetoothRawAddress &device, int32_t &state)
43 {
44 MessageParcel data;
45 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothPanProxy::GetDescriptor()),
46 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
47 CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_IPC_TRANS_FAILED, "write device error");
48
49 MessageParcel reply;
50 MessageOption option(MessageOption::TF_SYNC);
51
52 SEND_IPC_REQUEST_RETURN_RESULT(static_cast<uint32_t>(BluetoothPanInterfaceCode::COMMAND_GET_DEVICE_STATE),
53 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
54
55 // read error code
56 int32_t errCode = reply.ReadInt32();
57 if (errCode != BT_NO_ERROR) {
58 HILOGE("reply errCode: %{public}d", errCode);
59 return errCode;
60 }
61 // read state
62 state = reply.ReadInt32();
63 return BT_NO_ERROR;
64 }
65
GetDevicesByStates(const std::vector<int32_t> & states,std::vector<BluetoothRawAddress> & result)66 int32_t BluetoothPanProxy::GetDevicesByStates(const std::vector<int32_t> &states,
67 std::vector<BluetoothRawAddress>& result)
68 {
69 MessageParcel data;
70 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothPanProxy::GetDescriptor()),
71 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
72 CHECK_AND_RETURN_LOG_RET(WriteParcelableInt32Vector(states, data), BT_ERR_IPC_TRANS_FAILED, "write device error");
73
74 MessageParcel reply;
75 MessageOption option(MessageOption::TF_SYNC);
76
77 SEND_IPC_REQUEST_RETURN_RESULT(static_cast<uint32_t>(BluetoothPanInterfaceCode::COMMAND_GET_DEVICES_BY_STATES),
78 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
79
80 // read error code
81 int32_t errCode = reply.ReadInt32();
82 if (errCode != BT_NO_ERROR) {
83 HILOGE("reply errCode: %{public}d", errCode);
84 return errCode;
85 }
86
87 // read size
88 int32_t rawAddsSize = reply.ReadInt32();
89 const int32_t maxSize = 100;
90 if (rawAddsSize > maxSize) {
91 return BT_ERR_INVALID_PARAM;
92 }
93
94 // read devices
95 for (int i = 0; i < rawAddsSize; i++) {
96 std::unique_ptr<BluetoothRawAddress> address(reply.ReadParcelable<BluetoothRawAddress>());
97 if (!address) {
98 return TRANSACTION_ERR;
99 }
100 result.push_back(*address);
101 }
102 return BT_NO_ERROR;
103 }
104
RegisterObserver(const sptr<IBluetoothPanObserver> observer)105 int32_t BluetoothPanProxy::RegisterObserver(const sptr<IBluetoothPanObserver> observer)
106 {
107 MessageParcel data;
108 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothPanProxy::GetDescriptor()),
109 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
110 CHECK_AND_RETURN_LOG_RET(data.WriteRemoteObject(observer->AsObject()),
111 BT_ERR_IPC_TRANS_FAILED, "Write object error");
112
113 MessageParcel reply;
114 MessageOption option(MessageOption::TF_ASYNC);
115
116 SEND_IPC_REQUEST_RETURN_RESULT(static_cast<uint32_t>(BluetoothPanInterfaceCode::COMMAND_REGISTER_OBSERVER),
117 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
118
119 return BT_NO_ERROR;
120 }
121
DeregisterObserver(const sptr<IBluetoothPanObserver> observer)122 int32_t BluetoothPanProxy::DeregisterObserver(const sptr<IBluetoothPanObserver> observer)
123 {
124 MessageParcel data;
125 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothPanProxy::GetDescriptor()),
126 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
127 CHECK_AND_RETURN_LOG_RET(data.WriteRemoteObject(observer->AsObject()),
128 BT_ERR_IPC_TRANS_FAILED, "Write object error");
129
130 MessageParcel reply;
131 MessageOption option(MessageOption::TF_ASYNC);
132
133 SEND_IPC_REQUEST_RETURN_RESULT(static_cast<uint32_t>(BluetoothPanInterfaceCode::COMMAND_DEREGISTER_OBSERVER),
134 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
135
136 return BT_NO_ERROR;
137 }
138
SetTethering(const bool value)139 int32_t BluetoothPanProxy::SetTethering(const bool value)
140 {
141 MessageParcel data;
142 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothPanProxy::GetDescriptor()),
143 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
144 CHECK_AND_RETURN_LOG_RET(data.WriteBool(value), BT_ERR_IPC_TRANS_FAILED, "write value error");
145
146 MessageParcel reply;
147 MessageOption option(MessageOption::TF_SYNC);
148
149 SEND_IPC_REQUEST_RETURN_RESULT(static_cast<uint32_t>(BluetoothPanInterfaceCode::COMMAND_SET_TETHERING),
150 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
151
152 return reply.ReadInt32();
153 }
154
IsTetheringOn(bool & result)155 int32_t BluetoothPanProxy::IsTetheringOn(bool &result)
156 {
157 MessageParcel data;
158 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothPanProxy::GetDescriptor()),
159 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
160
161 MessageParcel reply;
162 MessageOption option(MessageOption::TF_SYNC);
163
164 SEND_IPC_REQUEST_RETURN_RESULT(static_cast<uint32_t>(BluetoothPanInterfaceCode::COMMAND_IS_TETHERING_ON),
165 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
166
167 int32_t ret = reply.ReadInt32();
168 if (ret != BT_NO_ERROR) {
169 HILOGE("internal error. ret:%{public}d", ret);
170 return ret;
171 }
172
173 result = reply.ReadInt32();
174 return BT_NO_ERROR;
175 }
176
WriteParcelableInt32Vector(const std::vector<int32_t> & parcelableVector,Parcel & reply)177 bool BluetoothPanProxy::WriteParcelableInt32Vector(const std::vector<int32_t> &parcelableVector, Parcel &reply)
178 {
179 if (!reply.WriteInt32(parcelableVector.size())) {
180 HILOGE("write ParcelableVector failed");
181 return false;
182 }
183
184 for (auto parcelable : parcelableVector) {
185 if (!reply.WriteInt32(parcelable)) {
186 HILOGE("write ParcelableVector failed");
187 return false;
188 }
189 }
190 return true;
191 }
192 } // namespace Bluetooth
193 } // namespace OHOS