• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_host_observer_stub.h"
17 #include "bluetooth_log.h"
18 
19 namespace OHOS {
20 namespace Bluetooth {
BluetoothHostObserverStub()21 BluetoothHostObserverStub::BluetoothHostObserverStub()
22 {
23     HILOGI("start.");
24     memberFuncMap_[static_cast<uint32_t>(BluetoothHostObserverInterfaceCode::BT_HOST_OBSERVER_STATE_CHANGE)] =
25         &BluetoothHostObserverStub::OnStateChangedInner;
26     memberFuncMap_[static_cast<uint32_t>(BluetoothHostObserverInterfaceCode::BT_HOST_OBSERVER_DISCOVERY_STATE_CHANGE)] =
27         &BluetoothHostObserverStub::OnDiscoveryStateChangedInner;
28     memberFuncMap_[static_cast<uint32_t>(BluetoothHostObserverInterfaceCode::BT_HOST_OBSERVER_DISCOVERY_RESULT)] =
29         &BluetoothHostObserverStub::OnDiscoveryResultInner;
30     memberFuncMap_[static_cast<uint32_t>(BluetoothHostObserverInterfaceCode::BT_HOST_OBSERVER_PAIR_REQUESTED)] =
31         &BluetoothHostObserverStub::OnPairRequestedInner;
32     memberFuncMap_[static_cast<uint32_t>(BluetoothHostObserverInterfaceCode::BT_HOST_OBSERVER_PAIR_CONFIRMED)] =
33         &BluetoothHostObserverStub::OnPairConfirmedInner;
34     memberFuncMap_[static_cast<uint32_t>(BluetoothHostObserverInterfaceCode::BT_HOST_OBSERVER_SCAN_MODE_CHANGED)] =
35         &BluetoothHostObserverStub::OnScanModeChangedInner;
36     memberFuncMap_[static_cast<uint32_t>(BluetoothHostObserverInterfaceCode::BT_HOST_OBSERVER_DEVICE_NAME_CHANGED)] =
37         &BluetoothHostObserverStub::OnDeviceNameChangedInner;
38     memberFuncMap_[static_cast<uint32_t>(BluetoothHostObserverInterfaceCode::BT_HOST_OBSERVER_DEVICE_ADDR_CHANGED)] =
39         &BluetoothHostObserverStub::OnDeviceAddrChangedInner;
40 }
41 
~BluetoothHostObserverStub()42 BluetoothHostObserverStub::~BluetoothHostObserverStub()
43 {
44     HILOGI("start.");
45     memberFuncMap_.clear();
46 }
47 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)48 int32_t BluetoothHostObserverStub::OnRemoteRequest(
49     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
50 {
51     HILOGD("BluetoothHostObserverStub: transaction of code: %{public}d is received", code);
52     if (BluetoothHostObserverStub::GetDescriptor() != data.ReadInterfaceToken()) {
53         HILOGE("BluetoothHostObserverStub::OnRemoteRequest, local descriptor is not equal to remote");
54         return ERR_INVALID_STATE;
55     }
56 
57     auto itFunc = memberFuncMap_.find(code);
58     if (itFunc != memberFuncMap_.end()) {
59         auto memberFunc = itFunc->second;
60         if (memberFunc != nullptr) {
61             return (this->*memberFunc)(data, reply);
62         }
63     }
64 
65     HILOGW("BluetoothHostObserverStub::OnRemoteRequest, default case, need check.");
66     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
67 }
68 
OnStateChangedInner(MessageParcel & data,MessageParcel & reply)69 ErrCode BluetoothHostObserverStub::OnStateChangedInner(MessageParcel &data, MessageParcel &reply)
70 {
71     int32_t transport = data.ReadInt32();
72     int32_t status = data.ReadInt32();
73 
74     HILOGD("BluetoothHostObserverStub::OnStateChangedInner starts");
75     OnStateChanged(transport, status);
76 
77     return NO_ERROR;
78 }
79 
80 // ON_DIS_STA_CHANGE_CODE
OnDiscoveryStateChangedInner(MessageParcel & data,MessageParcel & reply)81 ErrCode BluetoothHostObserverStub::OnDiscoveryStateChangedInner(MessageParcel &data, MessageParcel &reply)
82 {
83     int32_t status = data.ReadInt32();
84 
85     HILOGI("BluetoothHostObserverStub::OnDiscoveryStateChangedInner starts");
86     OnDiscoveryStateChanged(status);
87 
88     return NO_ERROR;
89 }
90 
OnDiscoveryResultInner(MessageParcel & data,MessageParcel & reply)91 ErrCode BluetoothHostObserverStub::OnDiscoveryResultInner(MessageParcel &data, MessageParcel &reply)
92 {
93     std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
94     if (!device) {
95         return TRANSACTION_ERR;
96     }
97 
98     HILOGD("BluetoothHostObserverStub::OnDiscoveryResultInner starts");
99     OnDiscoveryResult(*device);
100 
101     return NO_ERROR;
102 }
103 
OnPairRequestedInner(MessageParcel & data,MessageParcel & reply)104 ErrCode BluetoothHostObserverStub::OnPairRequestedInner(MessageParcel &data, MessageParcel &reply)
105 {
106     int32_t transport = data.ReadInt32();
107     std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
108     if (!device) {
109         return TRANSACTION_ERR;
110     }
111 
112     HILOGI("BluetoothHostObserverStub::OnPairRequestedInner starts");
113     OnPairRequested(transport, *device);
114 
115     return NO_ERROR;
116 }
117 
OnPairConfirmedInner(MessageParcel & data,MessageParcel & reply)118 ErrCode BluetoothHostObserverStub::OnPairConfirmedInner(MessageParcel &data, MessageParcel &reply)
119 {
120     int32_t transport = data.ReadInt32();
121     std::shared_ptr<BluetoothRawAddress> device(data.ReadParcelable<BluetoothRawAddress>());
122     if (!device) {
123         return TRANSACTION_ERR;
124     }
125     int32_t reqType = data.ReadInt32();
126     int32_t number = data.ReadInt32();
127 
128     HILOGI("BluetoothHostObserverStub::OnPairConfirmedInner starts");
129     OnPairConfirmed(transport, *device, reqType, number);
130 
131     return NO_ERROR;
132 }
133 
OnScanModeChangedInner(MessageParcel & data,MessageParcel & reply)134 ErrCode BluetoothHostObserverStub::OnScanModeChangedInner(MessageParcel &data, MessageParcel &reply)
135 {
136     int32_t mode = data.ReadInt32();
137 
138     HILOGI("BluetoothHostObserverStub::OnScanModeChangedInner starts");
139     OnScanModeChanged(mode);
140 
141     return NO_ERROR;
142 }
143 
OnDeviceNameChangedInner(MessageParcel & data,MessageParcel & reply)144 ErrCode BluetoothHostObserverStub::OnDeviceNameChangedInner(MessageParcel &data, MessageParcel &reply)
145 {
146     std::string deviceName = data.ReadString();
147 
148     HILOGI("BluetoothHostObserverStub::OnDeviceNameChangedInner starts");
149     OnDeviceNameChanged(deviceName);
150 
151     return NO_ERROR;
152 }
153 
OnDeviceAddrChangedInner(MessageParcel & data,MessageParcel & reply)154 ErrCode BluetoothHostObserverStub::OnDeviceAddrChangedInner(MessageParcel &data, MessageParcel &reply)
155 {
156     std::string address = data.ReadString();
157 
158     HILOGI("BluetoothHostObserverStub::OnDeviceAddrChangedInner starts");
159     OnDeviceAddrChanged(address);
160 
161     return NO_ERROR;
162 }
163 }  // namespace Bluetooth
164 }  // namespace OHOS
165