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_ble_peripheral_observer_stub.h"
17 #include "bluetooth_log.h"
18 #include "ipc_types.h"
19 #include "string_ex.h"
20
21 namespace OHOS {
22 namespace Bluetooth {
BluetoothBlePeripheralObserverStub()23 BluetoothBlePeripheralObserverStub::BluetoothBlePeripheralObserverStub()
24 {
25 HILOGD("start.");
26 memberFuncMap_ = {
27 {static_cast<uint32_t>(BluetoothBlePeripheralObserverInterfaceCode::BLE_ON_READ_REMOTE_RSSI_EVENT),
28 BluetoothBlePeripheralObserverStub::OnReadRemoteRssiEventInner},
29 {static_cast<uint32_t>(BluetoothBlePeripheralObserverInterfaceCode::BLE_PAIR_STATUS_CHANGED),
30 BluetoothBlePeripheralObserverStub::OnPairStatusChangedInner},
31 {static_cast<uint32_t>(BluetoothBlePeripheralObserverInterfaceCode::BLE_ACL_STATE_CHANGED),
32 BluetoothBlePeripheralObserverStub::OnAclStateChangedInner},
33 };
34 }
35
~BluetoothBlePeripheralObserverStub()36 BluetoothBlePeripheralObserverStub::~BluetoothBlePeripheralObserverStub()
37 {
38 HILOGD("start.");
39 memberFuncMap_.clear();
40 }
41
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)42 int BluetoothBlePeripheralObserverStub::OnRemoteRequest(
43 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
44 {
45 HILOGD("BleCentralManagerCallBackStub::OnRemoteRequest, cmd = %{public}d, flags= %{public}d",
46 code, option.GetFlags());
47 if (BluetoothBlePeripheralObserverStub::GetDescriptor() != data.ReadInterfaceToken()) {
48 HILOGI("local descriptor is not equal to remote");
49 return ERR_INVALID_STATE;
50 }
51
52 auto itFunc = memberFuncMap_.find(code);
53 if (itFunc != memberFuncMap_.end()) {
54 auto memberFunc = itFunc->second;
55 if (memberFunc != nullptr) {
56 return memberFunc(this, data, reply);
57 }
58 }
59 HILOGW("BleCentralManagerCallBackStub::OnRemoteRequest, default case, need check.");
60 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
61 }
62
OnReadRemoteRssiEventInner(BluetoothBlePeripheralObserverStub * stub,MessageParcel & data,MessageParcel & reply)63 ErrCode BluetoothBlePeripheralObserverStub::OnReadRemoteRssiEventInner(
64 BluetoothBlePeripheralObserverStub *stub, MessageParcel &data, MessageParcel &reply)
65 {
66 std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
67 if (!device) {
68 return TRANSACTION_ERR;
69 }
70 const int32_t rssi = static_cast<int32_t>(data.ReadInt32());
71 const int32_t status = static_cast<int32_t>(data.ReadInt32());
72
73 stub->OnReadRemoteRssiEvent(*device, rssi, status);
74 return NO_ERROR;
75 }
76
OnPairStatusChangedInner(BluetoothBlePeripheralObserverStub * stub,MessageParcel & data,MessageParcel & reply)77 ErrCode BluetoothBlePeripheralObserverStub::OnPairStatusChangedInner(
78 BluetoothBlePeripheralObserverStub *stub, MessageParcel &data, MessageParcel &reply)
79 {
80 const int32_t transport = static_cast<int32_t>(data.ReadInt32());
81 std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
82 if (!device) {
83 return TRANSACTION_ERR;
84 }
85 const int32_t status = static_cast<int32_t>(data.ReadInt32());
86 const int32_t cause = static_cast<int32_t>(data.ReadInt32());
87 stub->OnPairStatusChanged(transport, *device, status, cause);
88 return NO_ERROR;
89 }
90
OnAclStateChangedInner(BluetoothBlePeripheralObserverStub * stub,MessageParcel & data,MessageParcel & reply)91 ErrCode BluetoothBlePeripheralObserverStub::OnAclStateChangedInner(
92 BluetoothBlePeripheralObserverStub *stub, MessageParcel &data, MessageParcel &reply)
93 {
94 std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
95 if (!device) {
96 return TRANSACTION_ERR;
97 }
98 const int32_t state = static_cast<int32_t>(data.ReadInt32());
99 const uint32_t reason = static_cast<uint32_t>(data.ReadUint32());
100
101 stub->OnAclStateChanged(*device, state, reason);
102 return NO_ERROR;
103 }
104 } // namespace Bluetooth
105 } // namespace OHOS