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