1 /*
2 * Copyright (C) 2024-2024 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_resource_manager_observer_stub.h"
17 #include "bluetooth_log.h"
18 #include "bluetooth_errorcode.h"
19
20 namespace OHOS {
21 namespace Bluetooth {
BluetoothResourceManagerObserverStub()22 BluetoothResourceManagerObserverStub::BluetoothResourceManagerObserverStub()
23 {
24 HILOGD("start.");
25 memberFuncMap_[static_cast<uint32_t>(
26 BluetoothResourceManagerObserverInterfaceCode::SENSING_STATE_CHANGED)] =
27 &BluetoothResourceManagerObserverStub::OnSensingStateChangedInner;
28 memberFuncMap_[static_cast<uint32_t>(
29 BluetoothResourceManagerObserverInterfaceCode::BLUETOOTH_RESOURCE_DECISION)] =
30 &BluetoothResourceManagerObserverStub::OnBluetoothResourceDecisionInner;
31 }
32
~BluetoothResourceManagerObserverStub()33 BluetoothResourceManagerObserverStub::~BluetoothResourceManagerObserverStub()
34 {
35 HILOGD("start.");
36 memberFuncMap_.clear();
37 }
38
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)39 int BluetoothResourceManagerObserverStub::OnRemoteRequest(
40 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
41 {
42 HILOGD("cmd = %{public}d, flags= %{public}d", code, option.GetFlags());
43 if (BluetoothResourceManagerObserverStub::GetDescriptor() != data.ReadInterfaceToken()) {
44 HILOGI("local descriptor is not equal to remote");
45 return ERR_INVALID_STATE;
46 }
47
48 auto itFunc = memberFuncMap_.find(code);
49 if (itFunc != memberFuncMap_.end()) {
50 auto memberFunc = itFunc->second;
51 if (memberFunc != nullptr) {
52 return (this->*memberFunc)(data, reply);
53 }
54 }
55 HILOGW("OnRemoteRequest, default case, need check.");
56 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
57 }
58
OnSensingStateChangedInner(MessageParcel & data,MessageParcel & reply)59 ErrCode BluetoothResourceManagerObserverStub::OnSensingStateChangedInner(MessageParcel &data, MessageParcel &reply)
60 {
61 uint8_t eventId = data.ReadUint8();
62 std::shared_ptr<BluetoothSensingInfo> info(data.ReadParcelable<BluetoothSensingInfo>());
63 if (!info) {
64 return TRANSACTION_ERR;
65 }
66 OnSensingStateChanged(eventId, *info);
67 return NO_ERROR;
68 }
69
OnBluetoothResourceDecisionInner(MessageParcel & data,MessageParcel & reply)70 ErrCode BluetoothResourceManagerObserverStub::OnBluetoothResourceDecisionInner(
71 MessageParcel &data, MessageParcel &reply)
72 {
73 uint8_t eventId = data.ReadUint8();
74 std::shared_ptr<BluetoothSensingInfo> info(data.ReadParcelable<BluetoothSensingInfo>());
75 if (!info) {
76 return TRANSACTION_ERR;
77 }
78 uint32_t result = bluetooth::CONNECTION_ACCEPT;
79 OnBluetoothResourceDecision(eventId, *info, result);
80 if (!reply.WriteUint32(result)) {
81 HILOGE("reply write failed.");
82 return BT_ERR_IPC_TRANS_FAILED;
83 }
84 return NO_ERROR;
85 }
86 }
87 }