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_ble_central_manager_callback_proxy.h"
17
18 #include "bluetooth_log.h"
19 #include "ipc_types.h"
20
21 namespace OHOS {
22 namespace Bluetooth {
BluetoothBleCentralManagerCallBackProxy(const sptr<IRemoteObject> & impl)23 BluetoothBleCentralManagerCallBackProxy::BluetoothBleCentralManagerCallBackProxy(const sptr<IRemoteObject> &impl)
24 : IRemoteProxy<IBluetoothBleCentralManagerCallback>(impl)
25 {}
~BluetoothBleCentralManagerCallBackProxy()26 BluetoothBleCentralManagerCallBackProxy::~BluetoothBleCentralManagerCallBackProxy()
27 {}
28
OnScanCallback(const BluetoothBleScanResult & result)29 void BluetoothBleCentralManagerCallBackProxy::OnScanCallback(const BluetoothBleScanResult &result)
30 {
31 MessageParcel data;
32 if (!data.WriteInterfaceToken(BluetoothBleCentralManagerCallBackProxy::GetDescriptor())) {
33 HILOGE("[OnStartResultEvent] fail: write interface token failed.");
34 return;
35 }
36
37 if (!data.WriteParcelable(&result)) {
38 HILOGE("[OnStartResultEvent] fail: write result failed");
39 return;
40 }
41
42 MessageParcel reply;
43 MessageOption option = {MessageOption::TF_ASYNC};
44 int error = InnerTransact(
45 BluetoothBleCentralManagerCallbackInterfaceCode::BT_BLE_CENTRAL_MANAGER_CALLBACK, option, data, reply);
46 if (error != NO_ERROR) {
47 HILOGE("BluetoothBleCentralManagerCallBackProxy::OnScanCallback done fail, error: %{public}d", error);
48 return;
49 }
50 }
51
OnBleBatchScanResultsEvent(std::vector<BluetoothBleScanResult> & results)52 void BluetoothBleCentralManagerCallBackProxy::OnBleBatchScanResultsEvent(std::vector<BluetoothBleScanResult> &results)
53 {
54 MessageParcel data;
55 if (!data.WriteInterfaceToken(BluetoothBleCentralManagerCallBackProxy::GetDescriptor())) {
56 HILOGE("[OnBleBatchScanResultsEvent] fail: write interface token failed.");
57 return;
58 }
59
60 int32_t size = results.size();
61 if (!data.WriteInt32(size)) {
62 HILOGE("[OnBleBatchScanResultsEvent] fail: write size token failed.");
63 return;
64 }
65
66 for (auto &result : results) {
67 if (!data.WriteParcelable(&result)) {
68 HILOGE("write ParcelableVector failed");
69 return;
70 }
71 }
72
73 MessageParcel reply;
74 MessageOption option = {MessageOption::TF_ASYNC};
75
76 int error = InnerTransact(
77 BluetoothBleCentralManagerCallbackInterfaceCode::BT_BLE_CENTRAL_MANAGER_BLE_BATCH_CALLBACK,
78 option, data, reply);
79 if (error != NO_ERROR) {
80 HILOGE(
81 "BluetoothBleCentralManagerCallBackProxy::OnBleBatchScanResultsEvent done fail, error: %{public}d", error);
82 return;
83 }
84 }
OnStartOrStopScanEvent(int resultCode,bool isStartScan)85 void BluetoothBleCentralManagerCallBackProxy::OnStartOrStopScanEvent(int resultCode, bool isStartScan)
86 {
87 MessageParcel data;
88 if (!data.WriteInterfaceToken(BluetoothBleCentralManagerCallBackProxy::GetDescriptor())) {
89 HILOGE("write interface token failed.");
90 return;
91 }
92
93 if (!data.WriteInt32(resultCode)) {
94 HILOGE("write resultCode failed");
95 return;
96 }
97 if (!data.WriteBool(isStartScan)) {
98 HILOGE("write isStartScan failed");
99 return;
100 }
101 MessageParcel reply;
102 MessageOption option = {MessageOption::TF_ASYNC};
103 int error = InnerTransact(
104 BluetoothBleCentralManagerCallbackInterfaceCode::BT_BLE_CENTRAL_MANAGER_CALLBACK_SCAN_FAILED,
105 option, data, reply);
106 if (error != NO_ERROR) {
107 HILOGE("InnerTransact error: %{public}d", error);
108 return;
109 }
110 }
111
OnNotifyMsgReportFromLpDevice(const bluetooth::Uuid & uuid,int msgType,const std::vector<uint8_t> & notifyValue)112 void BluetoothBleCentralManagerCallBackProxy::OnNotifyMsgReportFromLpDevice(const bluetooth::Uuid &uuid, int msgType,
113 const std::vector<uint8_t> ¬ifyValue)
114 {
115 return;
116 }
117
118
InnerTransact(uint32_t code,MessageOption & flags,MessageParcel & data,MessageParcel & reply)119 ErrCode BluetoothBleCentralManagerCallBackProxy::InnerTransact(
120 uint32_t code, MessageOption &flags, MessageParcel &data, MessageParcel &reply)
121 {
122 auto remote = Remote();
123 if (remote == nullptr) {
124 HILOGW("[InnerTransact] fail: get Remote fail code %{public}d", code);
125 return OBJECT_NULL;
126 }
127 int err = remote->SendRequest(code, data, reply, flags);
128 switch (err) {
129 case NO_ERROR: {
130 return NO_ERROR;
131 }
132 case DEAD_OBJECT: {
133 HILOGW("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
134 return DEAD_OBJECT;
135 }
136 default: {
137 HILOGW("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
138 return TRANSACTION_ERR;
139 }
140 }
141 }
142 } // namespace Bluetooth
143 } // namespace OHOS