1 /*
2 * Copyright (C) 2021 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_host_observer_proxy.h"
17 #include "bluetooth_log.h"
18
19 namespace OHOS {
20 namespace Bluetooth {
OnStateChanged(int32_t transport,int32_t status)21 void BluetoothHostObserverProxy::OnStateChanged(int32_t transport, int32_t status)
22 {
23 MessageParcel data;
24
25 if (!data.WriteInterfaceToken(BluetoothHostObserverProxy::GetDescriptor())) {
26 HILOGE("BluetoothHostObserverProxy::OnStateChanged WriteInterfaceToken error");
27 return;
28 }
29
30 if (!data.WriteInt32(transport)) {
31 HILOGE("BluetoothHostObserverProxy::OnStateChanged transport error");
32 return;
33 }
34
35 if (!data.WriteInt32(status)) {
36 HILOGE("BluetoothHostObserverProxy::OnStateChanged status error");
37 return;
38 }
39
40 MessageParcel reply;
41 MessageOption option = {MessageOption::TF_ASYNC};
42
43 ErrCode result = InnerTransact(BT_HOST_OBSERVER_STATE_CHANGE, option, data, reply);
44 if (result != NO_ERROR) {
45 HILOGE("BluetoothHostProxy::OnStateChanged done fail, error: %{public}d", result);
46 return;
47 }
48 }
49
50 // ON_DIS_STA_CHANGE_CODE
OnDiscoveryStateChanged(int32_t status)51 void BluetoothHostObserverProxy::OnDiscoveryStateChanged(int32_t status)
52 {
53 MessageParcel data;
54
55 if (!data.WriteInterfaceToken(BluetoothHostObserverProxy::GetDescriptor())) {
56 HILOGE("BluetoothHostObserverProxy::OnDiscoveryStateChanged WriteInterfaceToken error");
57 return;
58 }
59
60 if (!data.WriteInt32(status)) {
61 HILOGE("BluetoothHostObserverProxy::OnDiscoveryStateChanged status error");
62 return;
63 }
64
65 MessageParcel reply;
66 MessageOption option = {MessageOption::TF_ASYNC};
67
68 ErrCode result = InnerTransact(BT_HOST_OBSERVER_DISCOVERY_STATE_CHANGE, option, data, reply);
69 if (result != NO_ERROR) {
70 HILOGE("BluetoothHostProxy::OnDiscoveryStateChanged done fail, error: %{public}d", result);
71 return;
72 }
73 }
74
OnDiscoveryResult(const BluetoothRawAddress & device,int rssi,const std::string deviceName,int deviceClass)75 void BluetoothHostObserverProxy::OnDiscoveryResult(
76 const BluetoothRawAddress &device, int rssi, const std::string deviceName, int deviceClass)
77 {
78 MessageParcel data;
79
80 if (!data.WriteInterfaceToken(BluetoothHostObserverProxy::GetDescriptor())) {
81 HILOGE("BluetoothHostObserverProxy::OnDiscoveryResult WriteInterfaceToken error");
82 return;
83 }
84
85 if (!data.WriteParcelable(&device)) {
86 HILOGE("BluetoothHostObserverProxy::OnDiscoveryResult device error");
87 return;
88 }
89
90 if (!data.WriteInt32(rssi)) {
91 HILOGE("BluetoothHostObserverProxy::OnDiscoveryResult rssi error");
92 return;
93 }
94
95 if (!data.WriteString(deviceName)) {
96 HILOGE("BluetoothHostObserverProxy::OnDiscoveryResult status error");
97 return;
98 }
99
100 if (!data.WriteInt32(deviceClass)) {
101 HILOGE("BluetoothHostObserverProxy::OnDiscoveryResult deviceClass error");
102 return;
103 }
104 MessageParcel reply;
105 MessageOption option = {MessageOption::TF_ASYNC};
106
107 ErrCode result = InnerTransact(BT_HOST_OBSERVER_DISCOVERY_RESULT, option, data, reply);
108 if (result != NO_ERROR) {
109 HILOGE("BluetoothHostProxy::OnDiscoveryResult done fail, error: %{public}d", result);
110 return;
111 }
112 }
113
OnPairRequested(const int32_t transport,const BluetoothRawAddress & device)114 void BluetoothHostObserverProxy::OnPairRequested(const int32_t transport, const BluetoothRawAddress &device)
115 {
116 MessageParcel data;
117
118 if (!data.WriteInterfaceToken(BluetoothHostObserverProxy::GetDescriptor())) {
119 HILOGE("BluetoothHostObserverProxy::OnPairRequested WriteInterfaceToken error");
120 return;
121 }
122
123 if (!data.WriteInt32(transport)) {
124 HILOGE("BluetoothHostObserverProxy::OnPairRequested transport error");
125 return;
126 }
127
128 if (!data.WriteParcelable(&device)) {
129 HILOGE("BluetoothHostObserverProxy::OnPairRequested device error");
130 return;
131 }
132
133 MessageParcel reply;
134 MessageOption option = {MessageOption::TF_ASYNC};
135
136 ErrCode result = InnerTransact(BT_HOST_OBSERVER_PAIR_REQUESTED, option, data, reply);
137 if (result != NO_ERROR) {
138 HILOGE("BluetoothHostProxy::OnPairRequested done fail, error: %{public}d", result);
139 return;
140 }
141 }
142
OnPairConfirmed(const int32_t transport,const BluetoothRawAddress & device,int reqType,int number)143 void BluetoothHostObserverProxy::OnPairConfirmed(
144 const int32_t transport, const BluetoothRawAddress &device, int reqType, int number)
145 {
146 MessageParcel data;
147
148 if (!data.WriteInterfaceToken(BluetoothHostObserverProxy::GetDescriptor())) {
149 HILOGE("BluetoothHostObserverProxy::OnPairConfirmed WriteInterfaceToken error");
150 return;
151 }
152
153 if (!data.WriteInt32(transport)) {
154 HILOGE("BluetoothHostObserverProxy::OnPairConfirmed transport error");
155 return;
156 }
157
158 if (!data.WriteParcelable(&device)) {
159 HILOGE("BluetoothHostObserverProxy::OnPairConfirmed device error");
160 return;
161 }
162
163 if (!data.WriteInt32(reqType)) {
164 HILOGE("BluetoothHostObserverProxy::OnPairConfirmed transport error");
165 return;
166 }
167
168 if (!data.WriteInt32(number)) {
169 HILOGE("BluetoothHostObserverProxy::OnPairConfirmed transport error");
170 return;
171 }
172
173 MessageParcel reply;
174 MessageOption option = {MessageOption::TF_ASYNC};
175
176 ErrCode result = InnerTransact(BT_HOST_OBSERVER_PAIR_CONFIRMED, option, data, reply);
177 if (result != NO_ERROR) {
178 HILOGE("BluetoothHostProxy::OnPairConfirmed done fail, error: %{public}d", result);
179 return;
180 }
181 }
182
OnScanModeChanged(int32_t mode)183 void BluetoothHostObserverProxy::OnScanModeChanged(int32_t mode)
184 {
185 MessageParcel data;
186 if (!data.WriteInterfaceToken(BluetoothHostObserverProxy::GetDescriptor())) {
187 HILOGE("BluetoothHostObserverProxy::OnScanModeChanged WriteInterfaceToken error");
188 return;
189 }
190
191 if (!data.WriteInt32(mode)) {
192 HILOGE("BluetoothHostObserverProxy::OnScanModeChanged status error");
193 return;
194 }
195
196 MessageParcel reply;
197 MessageOption option = {MessageOption::TF_ASYNC};
198
199 ErrCode result = InnerTransact(BT_HOST_OBSERVER_SCAN_MODE_CHANGED, option, data, reply);
200 if (result != NO_ERROR) {
201 HILOGE("BluetoothHostProxy::OnScanModeChanged done fail, error: %{public}d", result);
202 return;
203 }
204 }
205
OnDeviceNameChanged(const std::string & deviceName)206 void BluetoothHostObserverProxy::OnDeviceNameChanged(const std::string &deviceName)
207 {
208 MessageParcel data;
209 if (!data.WriteInterfaceToken(BluetoothHostObserverProxy::GetDescriptor())) {
210 HILOGE("BluetoothHostObserverProxy::OnDeviceNameChanged WriteInterfaceToken error");
211 return;
212 }
213
214 if (!data.WriteString(deviceName)) {
215 HILOGE("BluetoothHostObserverProxy::OnDeviceNameChanged status error");
216 return;
217 }
218
219 MessageParcel reply;
220 MessageOption option = {MessageOption::TF_ASYNC};
221
222 ErrCode result = InnerTransact(BT_HOST_OBSERVER_DEVICE_NAME_CHANGED, option, data, reply);
223 if (result != NO_ERROR) {
224 HILOGE("BluetoothHostProxy::OnDeviceNameChanged done fail, error: %{public}d", result);
225 return;
226 }
227 }
228
OnDeviceAddrChanged(const std::string & address)229 void BluetoothHostObserverProxy::OnDeviceAddrChanged(const std::string &address)
230 {
231 MessageParcel data;
232 if (!data.WriteInterfaceToken(BluetoothHostObserverProxy::GetDescriptor())) {
233 HILOGE("BluetoothHostObserverProxy::OnDeviceAddrChanged WriteInterfaceToken error");
234 return;
235 }
236
237 if (!data.WriteString(address)) {
238 HILOGE("BluetoothHostObserverProxy::OnDeviceAddrChanged status error");
239 return;
240 }
241
242 MessageParcel reply;
243 MessageOption option = {MessageOption::TF_ASYNC};
244
245 ErrCode result = InnerTransact(BT_HOST_OBSERVER_DEVICE_ADDR_CHANGED, option, data, reply);
246 if (result != NO_ERROR) {
247 HILOGE("BluetoothHostProxy::OnDeviceAddrChanged done fail, error: %{public}d", result);
248 return;
249 }
250 }
251
OnBluetoothStateChanged(int32_t state)252 void BluetoothHostObserverProxy::OnBluetoothStateChanged(int32_t state)
253 {
254 MessageParcel data;
255 if (!data.WriteInterfaceToken(BluetoothHostObserverProxy::GetDescriptor())) {
256 HILOGE("BluetoothHostObserverProxy::OnBluetoothStateChanged WriteInterfaceToken error");
257 return;
258 }
259
260 if (!data.WriteInt32(state)) {
261 HILOGE("BluetoothHostObserverProxy::OnBluetoothStateChanged WriteInt32 error");
262 return;
263 }
264
265 MessageParcel reply;
266 MessageOption option = {MessageOption::TF_ASYNC};
267 ErrCode result = InnerTransact(BT_HOST_OBSERVER_STATE_CHANGE_V2, option, data, reply);
268 if (result != NO_ERROR) {
269 HILOGE("BluetoothHostProxy::OnBluetoothStateChanged done fail, error: %{public}d", result);
270 return;
271 }
272 }
273
InnerTransact(uint32_t code,MessageOption & flags,MessageParcel & data,MessageParcel & reply)274 ErrCode BluetoothHostObserverProxy::InnerTransact(
275 uint32_t code, MessageOption &flags, MessageParcel &data, MessageParcel &reply)
276 {
277 auto remote = Remote();
278 if (remote == nullptr) {
279 HILOGE("[InnerTransact] fail: get Remote fail code %{public}d", code);
280 return ERR_DEAD_OBJECT;
281 }
282 int err = remote->SendRequest(code, data, reply, flags);
283 switch (err) {
284 case NO_ERROR: {
285 return ERR_OK;
286 }
287 case DEAD_OBJECT: {
288 HILOGE("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
289 return ERR_DEAD_OBJECT;
290 }
291 default: {
292 HILOGE("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
293 return TRANSACTION_ERR;
294 }
295 }
296 }
297 } // namespace Bluetooth
298 } // namespace OHOS
299