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_ble_peripheral_observer_proxy.h"
17 
18 #include "bluetooth_raw_address.h"
19 #include "bluetooth_log.h"
20 #include "ipc_types.h"
21 
22 namespace OHOS {
23 namespace Bluetooth {
BluetoothBlePeripheralObserverProxy(const sptr<IRemoteObject> & impl)24 BluetoothBlePeripheralObserverProxy::BluetoothBlePeripheralObserverProxy(const sptr<IRemoteObject> &impl)
25     : IRemoteProxy<IBluetoothBlePeripheralObserver>(impl)
26 {}
27 
~BluetoothBlePeripheralObserverProxy()28 BluetoothBlePeripheralObserverProxy::~BluetoothBlePeripheralObserverProxy()
29 {}
30 
OnReadRemoteRssiEvent(const BluetoothRawAddress & device,int rssi,int status)31 void BluetoothBlePeripheralObserverProxy::OnReadRemoteRssiEvent(const BluetoothRawAddress &device, int rssi, int status)
32 {
33     MessageParcel data;
34     if (!data.WriteInterfaceToken(BluetoothBlePeripheralObserverProxy::GetDescriptor())) {
35         HILOGE("[OnReadRemoteRssiEvent] fail: write interface token failed.");
36         return;
37     }
38 
39     if (!data.WriteParcelable(&device)) {
40         HILOGE("BluetoothBlePeripheralObserverProxy::OnReadRemoteRssiEvent device error");
41         return;
42     }
43 
44     if (!data.WriteInt32(rssi)) {
45         HILOGE("BluetoothBlePeripheralObserverProxy::OnReadRemoteRssiEvent rssi error");
46         return;
47     }
48 
49     if (!data.WriteInt32(status)) {
50         HILOGE("BluetoothBlePeripheralObserverProxy::OnReadRemoteRssiEvent device status");
51         return;
52     }
53 
54     MessageParcel reply;
55     MessageOption option = {MessageOption::TF_ASYNC};
56     int error =
57         InnerTransact(BluetoothBlePeripheralObserverInterfaceCode::BLE_ON_READ_REMOTE_RSSI_EVENT, option, data, reply);
58     if (error != NO_ERROR) {
59         HILOGE("BleCentralManagerCallBackProxy::OnScanCallback done fail, error: %{public}d", error);
60         return;
61     }
62 }
OnPairStatusChanged(const int32_t transport,const BluetoothRawAddress & device,int status,int cause)63 void BluetoothBlePeripheralObserverProxy::OnPairStatusChanged(
64     const int32_t transport, const BluetoothRawAddress &device, int status, int cause)
65 {
66     MessageParcel data;
67     if (!data.WriteInterfaceToken(BluetoothBlePeripheralObserverProxy::GetDescriptor())) {
68         HILOGE("[OnStartResultEvent] fail: write interface token failed.");
69         return;
70     }
71 
72     if (!data.WriteInt32(transport)) {
73         HILOGE("[OnStartResultEvent] fail: write result failed");
74         return;
75     }
76 
77     if (!data.WriteParcelable(&device)) {
78         HILOGE("[OnStartResultEvent] fail: write result failed");
79         return;
80     }
81 
82     if (!data.WriteInt32(status)) {
83         HILOGE("BluetoothBlePeripheralObserverProxy::OnReadRemoteRssiEvent device status");
84         return;
85     }
86 
87     if (!data.WriteInt32(cause)) {
88         HILOGE("[OnPairStatusChanged] fail: write cause failed");
89         return;
90     }
91 
92     MessageParcel reply;
93     MessageOption option = {MessageOption::TF_ASYNC};
94     int error = InnerTransact(BluetoothBlePeripheralObserverInterfaceCode::BLE_PAIR_STATUS_CHANGED,
95         option, data, reply);
96     if (error != NO_ERROR) {
97         HILOGE("BleCentralManagerCallBackProxy::OnScanCallback done fail, error: %{public}d", error);
98         return;
99     }
100 }
101 
OnAclStateChanged(const BluetoothRawAddress & device,int state,unsigned int reason)102 void BluetoothBlePeripheralObserverProxy::OnAclStateChanged(const BluetoothRawAddress &device,
103     int state, unsigned int reason)
104 {
105     return;
106 }
107 
InnerTransact(uint32_t code,MessageOption & flags,MessageParcel & data,MessageParcel & reply)108 ErrCode BluetoothBlePeripheralObserverProxy::InnerTransact(
109     uint32_t code, MessageOption &flags, MessageParcel &data, MessageParcel &reply)
110 {
111     auto remote = Remote();
112     if (remote == nullptr) {
113         HILOGW("[InnerTransact] fail: get Remote fail code %{public}d", code);
114         return OBJECT_NULL;
115     }
116     int err = remote->SendRequest(code, data, reply, flags);
117     switch (err) {
118         case NO_ERROR: {
119             return NO_ERROR;
120         }
121         case DEAD_OBJECT: {
122             HILOGW("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
123             return DEAD_OBJECT;
124         }
125         default: {
126             HILOGW("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
127             return TRANSACTION_ERR;
128         }
129     }
130 }
131 }  // namespace Bluetooth
132 }  // namespace OHOS