• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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)63 void BluetoothBlePeripheralObserverProxy::OnPairStatusChanged(
64     const int32_t transport, const BluetoothRawAddress &device, int status)
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     MessageParcel reply;
88     MessageOption option = {MessageOption::TF_ASYNC};
89     int error = InnerTransact(BluetoothBlePeripheralObserverInterfaceCode::BLE_PAIR_STATUS_CHANGED,
90         option, data, reply);
91     if (error != NO_ERROR) {
92         HILOGE("BleCentralManagerCallBackProxy::OnScanCallback done fail, error: %{public}d", error);
93         return;
94     }
95 }
96 
OnAclStateChanged(const BluetoothRawAddress & device,int state,unsigned int reason)97 void BluetoothBlePeripheralObserverProxy::OnAclStateChanged(const BluetoothRawAddress &device,
98     int state, unsigned int reason)
99 {
100     return;
101 }
102 
InnerTransact(uint32_t code,MessageOption & flags,MessageParcel & data,MessageParcel & reply)103 ErrCode BluetoothBlePeripheralObserverProxy::InnerTransact(
104     uint32_t code, MessageOption &flags, MessageParcel &data, MessageParcel &reply)
105 {
106     auto remote = Remote();
107     if (remote == nullptr) {
108         HILOGW("[InnerTransact] fail: get Remote fail code %{public}d", code);
109         return OBJECT_NULL;
110     }
111     int err = remote->SendRequest(code, data, reply, flags);
112     switch (err) {
113         case NO_ERROR: {
114             return NO_ERROR;
115         }
116         case DEAD_OBJECT: {
117             HILOGW("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
118             return DEAD_OBJECT;
119         }
120         default: {
121             HILOGW("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
122             return TRANSACTION_ERR;
123         }
124     }
125 }
126 }  // namespace Bluetooth
127 }  // namespace OHOS