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_remote_device_observer_proxy.h"
17 #include "bluetooth_bt_uuid.h"
18 #include "bluetooth_log.h"
19
20 namespace OHOS {
21 namespace Bluetooth {
BluetoothRemoteDeviceObserverproxy(const sptr<IRemoteObject> & impl)22 BluetoothRemoteDeviceObserverproxy::BluetoothRemoteDeviceObserverproxy(const sptr<IRemoteObject> &impl)
23 : IRemoteProxy<IBluetoothRemoteDeviceObserver>(impl)
24 {}
~BluetoothRemoteDeviceObserverproxy()25 BluetoothRemoteDeviceObserverproxy::~BluetoothRemoteDeviceObserverproxy()
26 {}
27
OnPairStatusChanged(const int32_t transport,const BluetoothRawAddress & device,int32_t status,int32_t cause)28 void BluetoothRemoteDeviceObserverproxy::OnPairStatusChanged(
29 const int32_t transport, const BluetoothRawAddress &device, int32_t status, int32_t cause)
30 {
31 MessageParcel data;
32 if (!data.WriteInterfaceToken(BluetoothRemoteDeviceObserverproxy::GetDescriptor())) {
33 HILOGE("[OnPairStatusChanged] fail: write interface token failed.");
34 return;
35 }
36 if (!data.WriteInt32(transport)) {
37 HILOGE("[OnPairStatusChanged] fail: write transport failed.");
38 return;
39 }
40 if (!data.WriteParcelable(&device)) {
41 HILOGE("[OnPairStatusChanged] fail: write device failed");
42 return;
43 }
44 if (!data.WriteInt32(status)) {
45 HILOGE("[OnPairStatusChanged] fail: write status failed.");
46 return;
47 }
48 if (!data.WriteInt32(cause)) {
49 HILOGE("[OnPairStatusChanged] fail: write cause failed.");
50 return;
51 }
52
53 MessageParcel reply;
54 MessageOption option = {MessageOption::TF_ASYNC};
55 int32_t error =
56 InnerTransact(BluetoothRemoteDeviceObserverInterfaceCode::BT_REMOTE_DEVICE_OBSERVER_PAIR_STATUS,
57 option, data, reply);
58 if (error != NO_ERROR) {
59 HILOGE("BluetoothRemoteDeviceObserverproxy::OnPairStatusChanged done fail, error: %{public}d", error);
60 return;
61 }
62 }
OnRemoteUuidChanged(const BluetoothRawAddress & device,const std::vector<bluetooth::Uuid> uuids)63 void BluetoothRemoteDeviceObserverproxy::OnRemoteUuidChanged(
64 const BluetoothRawAddress &device, const std::vector<bluetooth::Uuid> uuids)
65 {
66 if (uuids.empty()) {
67 HILOGE("[OnRemoteUuidChanged] fail: uuids is empty.");
68 return;
69 }
70
71 MessageParcel data;
72 if (!data.WriteInterfaceToken(BluetoothRemoteDeviceObserverproxy::GetDescriptor())) {
73 HILOGE("[OnRemoteUuidChanged] fail: write interface token failed.");
74 return;
75 }
76
77 if (!data.WriteParcelable(&device)) {
78 HILOGE("[OnRemoteUuidChanged] fail: write device failed");
79 return;
80 }
81
82 size_t uuidSize = uuids.size();
83 if (uuidSize > UINT16_MAX) {
84 HILOGE("[OnRemoteUuidChanged], uuidSize = %{public}zu exceeds the maximum number.", uuidSize);
85 return;
86 }
87 if (!data.WriteUint16(uuidSize)) {
88 HILOGE("[OnRemoteUuidChanged] fail: write uuids size failed");
89 return;
90 }
91 for (auto uuid : uuids) {
92 BluetoothUuid btUuid = BluetoothUuid(uuid);
93 if (!data.WriteParcelable(&btUuid)) {
94 HILOGE("[OnRemoteUuidChanged] faild: write uuid error");
95 return;
96 }
97 }
98 MessageParcel reply;
99 MessageOption option = {MessageOption::TF_ASYNC};
100 int32_t error = InnerTransact(BluetoothRemoteDeviceObserverInterfaceCode::BT_REMOTE_DEVICE_OBSERVER_REMOTE_UUID,
101 option, data, reply);
102 if (error != NO_ERROR) {
103 HILOGE("BluetoothRemoteDeviceObserverproxy::OnRemoteUuidChanged done fail, error: %{public}d", error);
104 return;
105 }
106 }
107
OnRemoteNameChanged(const BluetoothRawAddress & device,const std::string deviceName)108 void BluetoothRemoteDeviceObserverproxy::OnRemoteNameChanged(
109 const BluetoothRawAddress &device, const std::string deviceName)
110 {
111 MessageParcel data;
112 if (!data.WriteInterfaceToken(BluetoothRemoteDeviceObserverproxy::GetDescriptor())) {
113 HILOGE("[OnRemoteNameChanged] fail: write interface token failed.");
114 return;
115 }
116 if (!data.WriteParcelable(&device)) {
117 HILOGE("[OnRemoteNameChanged] fail: write device failed");
118 return;
119 }
120 if (!data.WriteString(deviceName)) {
121 HILOGE("[OnRemoteNameChanged] fail: write deviceName failed.");
122 return;
123 }
124
125 MessageParcel reply;
126 MessageOption option = {MessageOption::TF_ASYNC};
127 int32_t error = InnerTransact(BluetoothRemoteDeviceObserverInterfaceCode::BT_REMOTE_DEVICE_OBSERVER_REMOTE_NAME,
128 option, data, reply);
129 if (error != NO_ERROR) {
130 HILOGE("BluetoothRemoteDeviceObserverproxy::OnRemoteNameChanged done fail, error: %{public}d", error);
131 return;
132 }
133 }
134
OnRemoteAliasChanged(const BluetoothRawAddress & device,const std::string alias)135 void BluetoothRemoteDeviceObserverproxy::OnRemoteAliasChanged(
136 const BluetoothRawAddress &device, const std::string alias)
137 {
138 MessageParcel data;
139 if (!data.WriteInterfaceToken(BluetoothRemoteDeviceObserverproxy::GetDescriptor())) {
140 HILOGE("[OnRemoteAliasChanged] fail: write interface token failed.");
141 return;
142 }
143 if (!data.WriteParcelable(&device)) {
144 HILOGE("[OnRemoteAliasChanged] fail: write device failed");
145 return;
146 }
147 if (!data.WriteString(alias)) {
148 HILOGE("[OnRemoteAliasChanged] fail: write alias failed.");
149 return;
150 }
151
152 MessageParcel reply;
153 MessageOption option = {MessageOption::TF_ASYNC};
154 int32_t error = InnerTransact(
155 BluetoothRemoteDeviceObserverInterfaceCode::BT_REMOTE_DEVICE_OBSERVER_REMOTE_ALIAS, option, data, reply);
156 if (error != NO_ERROR) {
157 HILOGE("BluetoothRemoteDeviceObserverproxy::OnRemoteAliasChanged done fail, error: %{public}d", error);
158 return;
159 }
160 }
161
OnRemoteCodChanged(const BluetoothRawAddress & device,int32_t cod)162 void BluetoothRemoteDeviceObserverproxy::OnRemoteCodChanged(const BluetoothRawAddress &device, int32_t cod)
163 {
164 MessageParcel data;
165 if (!data.WriteInterfaceToken(BluetoothRemoteDeviceObserverproxy::GetDescriptor())) {
166 HILOGE("[OnRemoteCodChanged] fail: write interface token failed.");
167 return;
168 }
169 if (!data.WriteParcelable(&device)) {
170 HILOGE("[OnRemoteCodChanged] fail: write device failed");
171 return;
172 }
173 if (!data.WriteInt32(cod)) {
174 HILOGE("[OnRemoteCodChanged] fail: write cod failed.");
175 return;
176 }
177
178 MessageParcel reply;
179 MessageOption option = {MessageOption::TF_ASYNC};
180 int32_t error = InnerTransact(BluetoothRemoteDeviceObserverInterfaceCode::BT_REMOTE_DEVICE_OBSERVER_REMOTE_COD,
181 option, data, reply);
182 if (error != NO_ERROR) {
183 HILOGE("BluetoothRemoteDeviceObserverproxy::OnRemoteCodChanged done fail, error: %{public}d", error);
184 return;
185 }
186 }
187
OnRemoteBatteryChanged(const BluetoothRawAddress & device,const BluetoothBatteryInfo & batteryInfo)188 void BluetoothRemoteDeviceObserverproxy::OnRemoteBatteryChanged(
189 const BluetoothRawAddress &device, const BluetoothBatteryInfo &batteryInfo)
190 {
191 MessageParcel data;
192 CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(BluetoothRemoteDeviceObserverproxy::GetDescriptor()),
193 "[OnRemoteBatteryLevelChanged] fail: write interface token failed.");
194
195 CHECK_AND_RETURN_LOG(data.WriteParcelable(&device),
196 "[OnRemoteBatteryLevelChanged] fail: write device failed.");
197 CHECK_AND_RETURN_LOG(data.WriteParcelable(&batteryInfo),
198 "[OnRemoteBatteryLevelChanged] fail: write value failed.");
199
200 MessageParcel reply;
201 MessageOption option = {MessageOption::TF_ASYNC};
202 int32_t error = InnerTransact(
203 BluetoothRemoteDeviceObserverInterfaceCode::BT_REMOTE_DEVICE_OBSERVER_REMOTE_BATTERY_LEVEL,
204 option, data, reply);
205 CHECK_AND_RETURN_LOG(error == NO_ERROR,
206 "InnerTransact fail, error: %{public}d", error);
207 }
208
OnRemoteDeviceCommonInfoReport(const BluetoothRawAddress & device,const std::vector<uint8_t> & value)209 void BluetoothRemoteDeviceObserverproxy::OnRemoteDeviceCommonInfoReport(const BluetoothRawAddress &device,
210 const std::vector<uint8_t> &value)
211 {
212 MessageParcel data;
213 CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(BluetoothRemoteDeviceObserverproxy::GetDescriptor()),
214 "[OnRemoteDeviceCommonInfoReport] fail: write interface token failed.");
215
216 CHECK_AND_RETURN_LOG(data.WriteParcelable(&device),
217 "[OnRemoteDeviceCommonInfoReport] fail: write device failed.");
218 CHECK_AND_RETURN_LOG(data.WriteUInt8Vector(value),
219 "[OnRemoteDeviceCommonInfoReport] fail: write value failed.");
220
221 MessageParcel reply;
222 MessageOption option = {MessageOption::TF_ASYNC};
223 int32_t error = InnerTransact(
224 BluetoothRemoteDeviceObserverInterfaceCode::BT_REMOTE_DEVICE_OBSERVER_REMOTE_BATTERY_INFO_REPORT,
225 option, data, reply);
226 CHECK_AND_RETURN_LOG(error == NO_ERROR,
227 "InnerTransact fail, error: %{public}d", error);
228 }
229
OnAclStateChanged(const BluetoothRawAddress & device,int state,unsigned int reason)230 void BluetoothRemoteDeviceObserverproxy::OnAclStateChanged(const BluetoothRawAddress &device,
231 int state, unsigned int reason)
232 {
233 return;
234 }
235
InnerTransact(uint32_t code,MessageOption & flags,MessageParcel & data,MessageParcel & reply)236 ErrCode BluetoothRemoteDeviceObserverproxy::InnerTransact(
237 uint32_t code, MessageOption &flags, MessageParcel &data, MessageParcel &reply)
238 {
239 auto remote = Remote();
240 if (remote == nullptr) {
241 HILOGW("[InnerTransact] fail: get Remote fail code %{public}d", code);
242 return OBJECT_NULL;
243 }
244 int err = remote->SendRequest(code, data, reply, flags);
245 switch (err) {
246 case NO_ERROR: {
247 return NO_ERROR;
248 }
249 case DEAD_OBJECT: {
250 HILOGW("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
251 return DEAD_OBJECT;
252 }
253 default: {
254 HILOGW("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
255 return TRANSACTION_ERR;
256 }
257 }
258 }
259 } // namespace Bluetooth
260 } // namespace OHOS