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