1 /*
2 * Copyright (C) 2021 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_proxy.h"
17 #include "bluetooth_log.h"
18
19 namespace OHOS {
20 namespace Bluetooth {
OnStateChanged(int32_t transport,int32_t status)21 void BluetoothHostObserverProxy::OnStateChanged(int32_t transport, int32_t status)
22 {
23 MessageParcel data;
24
25 if (!data.WriteInterfaceToken(BluetoothHostObserverProxy::GetDescriptor())) {
26 HILOGE("BluetoothHostObserverProxy::OnStateChanged WriteInterfaceToken error");
27 return;
28 }
29
30 if (!data.WriteInt32(transport)) {
31 HILOGE("BluetoothHostObserverProxy::OnStateChanged transport error");
32 return;
33 }
34
35 if (!data.WriteInt32(status)) {
36 HILOGE("BluetoothHostObserverProxy::OnStateChanged status error");
37 return;
38 }
39
40 MessageParcel reply;
41 MessageOption option = {MessageOption::TF_ASYNC};
42
43 ErrCode result = InnerTransact(BT_HOST_OBSERVER_STATE_CHANGE, option, data, reply);
44 if (result != NO_ERROR) {
45 HILOGE("BluetoothHostProxy::OnStateChanged done fail, error: %{public}d", result);
46 return;
47 }
48 }
49
50 // ON_DIS_STA_CHANGE_CODE
OnDiscoveryStateChanged(int32_t status)51 void BluetoothHostObserverProxy::OnDiscoveryStateChanged(int32_t status)
52 {
53 MessageParcel data;
54
55 if (!data.WriteInterfaceToken(BluetoothHostObserverProxy::GetDescriptor())) {
56 HILOGE("BluetoothHostObserverProxy::OnDiscoveryStateChanged WriteInterfaceToken error");
57 return;
58 }
59
60 if (!data.WriteInt32(status)) {
61 HILOGE("BluetoothHostObserverProxy::OnDiscoveryStateChanged status error");
62 return;
63 }
64
65 MessageParcel reply;
66 MessageOption option = {MessageOption::TF_ASYNC};
67
68 ErrCode result = InnerTransact(BT_HOST_OBSERVER_DISCOVERY_STATE_CHANGE, option, data, reply);
69 if (result != NO_ERROR) {
70 HILOGE("BluetoothHostProxy::OnDiscoveryStateChanged done fail, error: %{public}d", result);
71 return;
72 }
73 }
74
OnDiscoveryResult(const BluetoothRawAddress & device)75 void BluetoothHostObserverProxy::OnDiscoveryResult(const BluetoothRawAddress &device)
76 {
77 MessageParcel data;
78
79 if (!data.WriteInterfaceToken(BluetoothHostObserverProxy::GetDescriptor())) {
80 HILOGE("BluetoothHostObserverProxy::OnDiscoveryResult WriteInterfaceToken error");
81 return;
82 }
83
84 if (!data.WriteParcelable(&device)) {
85 HILOGE("BluetoothHostObserverProxy::OnDiscoveryResult device error");
86 return;
87 }
88
89 MessageParcel reply;
90 MessageOption option = {MessageOption::TF_ASYNC};
91
92 ErrCode result = InnerTransact(BT_HOST_OBSERVER_DISCOVERY_RESULT, option, data, reply);
93 if (result != NO_ERROR) {
94 HILOGE("BluetoothHostProxy::OnDiscoveryResult done fail, error: %{public}d", result);
95 return;
96 }
97 }
98
OnPairRequested(const int32_t transport,const BluetoothRawAddress & device)99 void BluetoothHostObserverProxy::OnPairRequested(const int32_t transport, const BluetoothRawAddress &device)
100 {
101 MessageParcel data;
102
103 if (!data.WriteInterfaceToken(BluetoothHostObserverProxy::GetDescriptor())) {
104 HILOGE("BluetoothHostObserverProxy::OnPairRequested WriteInterfaceToken error");
105 return;
106 }
107
108 if (!data.WriteInt32(transport)) {
109 HILOGE("BluetoothHostObserverProxy::OnPairRequested transport error");
110 return;
111 }
112
113 if (!data.WriteParcelable(&device)) {
114 HILOGE("BluetoothHostObserverProxy::OnPairRequested device error");
115 return;
116 }
117
118 MessageParcel reply;
119 MessageOption option = {MessageOption::TF_ASYNC};
120
121 ErrCode result = InnerTransact(BT_HOST_OBSERVER_PAIR_REQUESTED, option, data, reply);
122 if (result != NO_ERROR) {
123 HILOGE("BluetoothHostProxy::OnPairRequested done fail, error: %{public}d", result);
124 return;
125 }
126 }
127
OnPairConfirmed(const int32_t transport,const BluetoothRawAddress & device,int reqType,int number)128 void BluetoothHostObserverProxy::OnPairConfirmed(
129 const int32_t transport, const BluetoothRawAddress &device, int reqType, int number)
130 {
131 MessageParcel data;
132
133 if (!data.WriteInterfaceToken(BluetoothHostObserverProxy::GetDescriptor())) {
134 HILOGE("BluetoothHostObserverProxy::OnPairConfirmed WriteInterfaceToken error");
135 return;
136 }
137
138 if (!data.WriteInt32(transport)) {
139 HILOGE("BluetoothHostObserverProxy::OnPairConfirmed transport error");
140 return;
141 }
142
143 if (!data.WriteParcelable(&device)) {
144 HILOGE("BluetoothHostObserverProxy::OnPairConfirmed device error");
145 return;
146 }
147
148 if (!data.WriteInt32(reqType)) {
149 HILOGE("BluetoothHostObserverProxy::OnPairConfirmed transport error");
150 return;
151 }
152
153 if (!data.WriteInt32(number)) {
154 HILOGE("BluetoothHostObserverProxy::OnPairConfirmed transport error");
155 return;
156 }
157
158 MessageParcel reply;
159 MessageOption option = {MessageOption::TF_ASYNC};
160
161 ErrCode result = InnerTransact(BT_HOST_OBSERVER_PAIR_CONFIRMED, option, data, reply);
162 if (result != NO_ERROR) {
163 HILOGE("BluetoothHostProxy::OnPairConfirmed done fail, error: %{public}d", result);
164 return;
165 }
166 }
167
OnScanModeChanged(int32_t mode)168 void BluetoothHostObserverProxy::OnScanModeChanged(int32_t mode)
169 {
170 MessageParcel data;
171 if (!data.WriteInterfaceToken(BluetoothHostObserverProxy::GetDescriptor())) {
172 HILOGE("BluetoothHostObserverProxy::OnScanModeChanged WriteInterfaceToken error");
173 return;
174 }
175
176 if (!data.WriteInt32(mode)) {
177 HILOGE("BluetoothHostObserverProxy::OnScanModeChanged status error");
178 return;
179 }
180
181 MessageParcel reply;
182 MessageOption option = {MessageOption::TF_ASYNC};
183
184 ErrCode result = InnerTransact(BT_HOST_OBSERVER_SCAN_MODE_CHANGED, option, data, reply);
185 if (result != NO_ERROR) {
186 HILOGE("BluetoothHostProxy::OnScanModeChanged done fail, error: %{public}d", result);
187 return;
188 }
189 }
190
OnDeviceNameChanged(const std::string & deviceName)191 void BluetoothHostObserverProxy::OnDeviceNameChanged(const std::string &deviceName)
192 {
193 MessageParcel data;
194 if (!data.WriteInterfaceToken(BluetoothHostObserverProxy::GetDescriptor())) {
195 HILOGE("BluetoothHostObserverProxy::OnDeviceNameChanged WriteInterfaceToken error");
196 return;
197 }
198
199 if (!data.WriteString(deviceName)) {
200 HILOGE("BluetoothHostObserverProxy::OnDeviceNameChanged status error");
201 return;
202 }
203
204 MessageParcel reply;
205 MessageOption option = {MessageOption::TF_ASYNC};
206
207 ErrCode result = InnerTransact(BT_HOST_OBSERVER_DEVICE_NAME_CHANGED, option, data, reply);
208 if (result != NO_ERROR) {
209 HILOGE("BluetoothHostProxy::OnDeviceNameChanged done fail, error: %{public}d", result);
210 return;
211 }
212 }
213
OnDeviceAddrChanged(const std::string & address)214 void BluetoothHostObserverProxy::OnDeviceAddrChanged(const std::string &address)
215 {
216 MessageParcel data;
217 if (!data.WriteInterfaceToken(BluetoothHostObserverProxy::GetDescriptor())) {
218 HILOGE("BluetoothHostObserverProxy::OnDeviceAddrChanged WriteInterfaceToken error");
219 return;
220 }
221
222 if (!data.WriteString(address)) {
223 HILOGE("BluetoothHostObserverProxy::OnDeviceAddrChanged status error");
224 return;
225 }
226
227 MessageParcel reply;
228 MessageOption option = {MessageOption::TF_ASYNC};
229
230 ErrCode result = InnerTransact(BT_HOST_OBSERVER_DEVICE_ADDR_CHANGED, option, data, reply);
231 if (result != NO_ERROR) {
232 HILOGE("BluetoothHostProxy::OnDeviceAddrChanged done fail, error: %{public}d", result);
233 return;
234 }
235 }
236
InnerTransact(uint32_t code,MessageOption & flags,MessageParcel & data,MessageParcel & reply)237 ErrCode BluetoothHostObserverProxy::InnerTransact(
238 uint32_t code, MessageOption &flags, MessageParcel &data, MessageParcel &reply)
239 {
240 auto remote = Remote();
241 if (remote == nullptr) {
242 HILOGE("[InnerTransact] fail: get Remote fail code %{public}d", code);
243 return ERR_DEAD_OBJECT;
244 }
245 int err = remote->SendRequest(code, data, reply, flags);
246 switch (err) {
247 case NO_ERROR: {
248 return ERR_OK;
249 }
250 case DEAD_OBJECT: {
251 HILOGE("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
252 return ERR_DEAD_OBJECT;
253 }
254 default: {
255 HILOGE("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
256 return TRANSACTION_ERR;
257 }
258 }
259 }
260 } // namespace Bluetooth
261 } // namespace OHOS
262