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