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)28 void BluetoothRemoteDeviceObserverproxy::OnPairStatusChanged(
29 const int32_t transport, const BluetoothRawAddress &device, int32_t status)
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
49 MessageParcel reply;
50 MessageOption option = {MessageOption::TF_ASYNC};
51 int32_t error =
52 InnerTransact(IBluetoothRemoteDeviceObserver::Code::BT_REMOTE_DEVICE_OBSERVER_PSIR_STATUS, option, data, reply);
53 if (error != NO_ERROR) {
54 HILOGE("BluetoothRemoteDeviceObserverproxy::OnPairStatusChanged done fail, error: %{public}d", error);
55 return;
56 }
57 }
OnRemoteUuidChanged(const BluetoothRawAddress & device,const std::vector<bluetooth::Uuid> uuids)58 void BluetoothRemoteDeviceObserverproxy::OnRemoteUuidChanged(
59 const BluetoothRawAddress &device, const std::vector<bluetooth::Uuid> uuids)
60 {
61 if (uuids.empty()) {
62 HILOGE("[OnRemoteUuidChanged] fail: uuids is empty.");
63 return;
64 }
65
66 MessageParcel data;
67 if (!data.WriteInterfaceToken(BluetoothRemoteDeviceObserverproxy::GetDescriptor())) {
68 HILOGE("[OnRemoteUuidChanged] fail: write interface token failed.");
69 return;
70 }
71
72 if (!data.WriteParcelable(&device)) {
73 HILOGE("[OnRemoteUuidChanged] fail: write device failed");
74 return;
75 }
76
77 size_t uuidSize = uuids.size();
78 if (uuidSize > UINT16_MAX) {
79 HILOGE("[OnRemoteUuidChanged], uuidSize = %{public}zu exceeds the maximum number.", uuidSize);
80 return;
81 }
82 if (!data.WriteUint16(uuidSize)) {
83 HILOGE("[OnRemoteUuidChanged] fail: write uuids size failed");
84 return;
85 }
86 for (auto uuid : uuids) {
87 BluetoothUuid btUuid = BluetoothUuid(uuid);
88 if (!data.WriteParcelable(&btUuid)) {
89 HILOGE("[OnRemoteUuidChanged] faild: write uuid error");
90 return;
91 }
92 }
93 MessageParcel reply;
94 MessageOption option = {MessageOption::TF_ASYNC};
95 int32_t error =
96 InnerTransact(IBluetoothRemoteDeviceObserver::Code::BT_REMOTE_DEVICE_OBSERVER_REMOTE_UUID, option, data, reply);
97 if (error != NO_ERROR) {
98 HILOGE("BluetoothRemoteDeviceObserverproxy::OnRemoteUuidChanged done fail, error: %{public}d", error);
99 return;
100 }
101 }
102
OnRemoteNameChanged(const BluetoothRawAddress & device,const std::string deviceName)103 void BluetoothRemoteDeviceObserverproxy::OnRemoteNameChanged(
104 const BluetoothRawAddress &device, const std::string deviceName)
105 {
106 MessageParcel data;
107 if (!data.WriteInterfaceToken(BluetoothRemoteDeviceObserverproxy::GetDescriptor())) {
108 HILOGE("[OnRemoteNameChanged] fail: write interface token failed.");
109 return;
110 }
111 if (!data.WriteParcelable(&device)) {
112 HILOGE("[OnRemoteNameChanged] fail: write device failed");
113 return;
114 }
115 if (!data.WriteString(deviceName)) {
116 HILOGE("[OnRemoteNameChanged] fail: write deviceName failed.");
117 return;
118 }
119
120 MessageParcel reply;
121 MessageOption option = {MessageOption::TF_ASYNC};
122 int32_t error =
123 InnerTransact(IBluetoothRemoteDeviceObserver::Code::BT_REMOTE_DEVICE_OBSERVER_REMOTE_NAME, option, data, reply);
124 if (error != NO_ERROR) {
125 HILOGE("BluetoothRemoteDeviceObserverproxy::OnRemoteNameChanged done fail, error: %{public}d", error);
126 return;
127 }
128 }
129
OnRemoteAliasChanged(const BluetoothRawAddress & device,const std::string alias)130 void BluetoothRemoteDeviceObserverproxy::OnRemoteAliasChanged(
131 const BluetoothRawAddress &device, const std::string alias)
132 {
133 MessageParcel data;
134 if (!data.WriteInterfaceToken(BluetoothRemoteDeviceObserverproxy::GetDescriptor())) {
135 HILOGE("[OnRemoteAliasChanged] fail: write interface token failed.");
136 return;
137 }
138 if (!data.WriteParcelable(&device)) {
139 HILOGE("[OnRemoteAliasChanged] fail: write device failed");
140 return;
141 }
142 if (!data.WriteString(alias)) {
143 HILOGE("[OnRemoteAliasChanged] fail: write alias failed.");
144 return;
145 }
146
147 MessageParcel reply;
148 MessageOption option = {MessageOption::TF_ASYNC};
149 int32_t error = InnerTransact(
150 IBluetoothRemoteDeviceObserver::Code::BT_REMOTE_DEVICE_OBSERVER_ALIAS_CHANGED, option, data, reply);
151 if (error != NO_ERROR) {
152 HILOGE("BluetoothRemoteDeviceObserverproxy::OnRemoteAliasChanged done fail, error: %{public}d", error);
153 return;
154 }
155 }
156
OnRemoteCodChanged(const BluetoothRawAddress & device,int32_t cod)157 void BluetoothRemoteDeviceObserverproxy::OnRemoteCodChanged(const BluetoothRawAddress &device, int32_t cod)
158 {
159 MessageParcel data;
160 if (!data.WriteInterfaceToken(BluetoothRemoteDeviceObserverproxy::GetDescriptor())) {
161 HILOGE("[OnRemoteCodChanged] fail: write interface token failed.");
162 return;
163 }
164 if (!data.WriteParcelable(&device)) {
165 HILOGE("[OnRemoteCodChanged] fail: write device failed");
166 return;
167 }
168 if (!data.WriteInt32(cod)) {
169 HILOGE("[OnRemoteCodChanged] fail: write cod failed.");
170 return;
171 }
172
173 MessageParcel reply;
174 MessageOption option = {MessageOption::TF_ASYNC};
175 int32_t error =
176 InnerTransact(IBluetoothRemoteDeviceObserver::Code::BT_REMOTE_DEVICE_OBSERVER_REMOTE_COD, option, data, reply);
177 if (error != NO_ERROR) {
178 HILOGE("BluetoothRemoteDeviceObserverproxy::OnRemoteCodChanged done fail, error: %{public}d", error);
179 return;
180 }
181 }
182
OnRemoteBatteryLevelChanged(const BluetoothRawAddress & device,int32_t batteryLevel)183 void BluetoothRemoteDeviceObserverproxy::OnRemoteBatteryLevelChanged(
184 const BluetoothRawAddress &device, int32_t batteryLevel)
185 {
186 MessageParcel data;
187 if (!data.WriteInterfaceToken(BluetoothRemoteDeviceObserverproxy::GetDescriptor())) {
188 HILOGE("[OnRemoteBatteryLevelChanged] fail: write interface token failed.");
189 return;
190 }
191 if (!data.WriteParcelable(&device)) {
192 HILOGE("[OnRemoteBatteryLevelChanged] fail: write device failed");
193 return;
194 }
195 if (!data.WriteInt32(batteryLevel)) {
196 HILOGE("[OnRemoteBatteryLevelChanged] fail: write status failed.");
197 return;
198 }
199
200 MessageParcel reply;
201 MessageOption option = {MessageOption::TF_ASYNC};
202 int32_t error = InnerTransact(
203 IBluetoothRemoteDeviceObserver::Code::BT_REMOTE_DEVICE_OBSERVER_REMOTE_BATTERY_LEVEL, option, data, reply);
204 if (error != NO_ERROR) {
205 HILOGE("BluetoothRemoteDeviceObserverproxy::OnRemoteBatteryLevelChanged done fail, error: %{public}d", error);
206 return;
207 }
208 }
209
InnerTransact(uint32_t code,MessageOption & flags,MessageParcel & data,MessageParcel & reply)210 ErrCode BluetoothRemoteDeviceObserverproxy::InnerTransact(
211 uint32_t code, MessageOption &flags, MessageParcel &data, MessageParcel &reply)
212 {
213 auto remote = Remote();
214 if (remote == nullptr) {
215 HILOGW("[InnerTransact] fail: get Remote fail code %{public}d", code);
216 return OBJECT_NULL;
217 }
218 int err = remote->SendRequest(code, data, reply, flags);
219 switch (err) {
220 case NO_ERROR: {
221 return NO_ERROR;
222 }
223 case DEAD_OBJECT: {
224 HILOGW("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
225 return DEAD_OBJECT;
226 }
227 default: {
228 HILOGW("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
229 return TRANSACTION_ERR;
230 }
231 }
232 }
233 } // namespace Bluetooth
234 } // namespace OHOS