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