1 /*
2  * Copyright (C) 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 #include "bluetooth_errorcode.h"
16 #include "bluetooth_opp_observer_stub.h"
17 #include "bluetooth_log.h"
18 
19 namespace OHOS {
20 namespace Bluetooth {
21 
BluetoothOppObserverStub()22 BluetoothOppObserverStub::BluetoothOppObserverStub()
23 {
24     HILOGI("start.");
25     memberFuncMap_[static_cast<uint32_t>(
26         BluetoothOppObserverInterfaceCode::OPP_ON_RECEIVE_INCOMING_FILE_CHANGED)] =
27         BluetoothOppObserverStub::OnReceiveIncomingFileChangedInner;
28     memberFuncMap_[static_cast<uint32_t>(
29         BluetoothOppObserverInterfaceCode::OPP_ON_TRANSFER_STATE_CHANGED)] =
30         BluetoothOppObserverStub::OnTransferStateChangedInner;
31 }
32 
~BluetoothOppObserverStub()33 BluetoothOppObserverStub::~BluetoothOppObserverStub()
34 {
35     HILOGI("start.");
36     memberFuncMap_.clear();
37 }
38 
OnRemoteRequest( uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)39 int32_t BluetoothOppObserverStub::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 (BluetoothOppObserverStub::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 memberFunc(this, data, reply);
53         }
54     }
55     HILOGW("OnRemoteRequest, default case, need check.");
56     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
57 }
58 
OnReceiveIncomingFileChangedInner(BluetoothOppObserverStub *stub, MessageParcel &data, MessageParcel &reply)59 int32_t BluetoothOppObserverStub::OnReceiveIncomingFileChangedInner(BluetoothOppObserverStub *stub,
60     MessageParcel &data, MessageParcel &reply)
61 {
62     HILOGD("Enter.");
63     std::shared_ptr<BluetoothIOppTransferInformation> oppInformation(
64         data.ReadParcelable<BluetoothIOppTransferInformation>());
65     CHECK_AND_RETURN_LOG_RET((oppInformation != nullptr), BT_ERR_INTERNAL_ERROR, "Read oppInformation error");
66     stub->OnReceiveIncomingFileChanged(*oppInformation);
67     return BT_NO_ERROR;
68 }
69 
OnTransferStateChangedInner(BluetoothOppObserverStub *stub, MessageParcel &data, MessageParcel &reply)70 int32_t BluetoothOppObserverStub::OnTransferStateChangedInner(BluetoothOppObserverStub *stub,
71     MessageParcel &data, MessageParcel &reply)
72 {
73     HILOGD("Enter.");
74     std::shared_ptr<BluetoothIOppTransferInformation> oppInformation(
75         data.ReadParcelable<BluetoothIOppTransferInformation>());
76     CHECK_AND_RETURN_LOG_RET((oppInformation != nullptr), BT_ERR_INTERNAL_ERROR, "Read oppInformation error");
77     stub->OnTransferStateChanged(*oppInformation);
78     return BT_NO_ERROR;
79 }
80 }  // namespace Bluetooth
81 }  // namespace OHOS