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 #ifndef LOG_TAG
16 #define LOG_TAG "bt_ipc_ble_central_cb_stub"
17 #endif
18
19 #include "bluetooth_ble_central_manager_callback_stub.h"
20 #include "bluetooth_bt_uuid.h"
21 #include "bluetooth_log.h"
22 #include "ipc_types.h"
23 #include "string_ex.h"
24
25 namespace OHOS {
26 namespace Bluetooth {
27 const int32_t BLE_CENTRAL_MANAGER_READ_DATA_SIZE_MAX_LEN = 0x100;
28 const std::map<uint32_t,
29 std::function<ErrCode(BluetoothBleCentralManagerCallBackStub *, MessageParcel &, MessageParcel &)>>
30 BluetoothBleCentralManagerCallBackStub::memberFuncMap_ = {
31 {static_cast<uint32_t>(
32 BluetoothBleCentralManagerCallbackInterfaceCode::BT_BLE_CENTRAL_MANAGER_CALLBACK),
33 BluetoothBleCentralManagerCallBackStub::OnScanCallbackInner},
34 {static_cast<uint32_t>(
35 BluetoothBleCentralManagerCallbackInterfaceCode::BT_BLE_CENTRAL_MANAGER_BLE_BATCH_CALLBACK),
36 BluetoothBleCentralManagerCallBackStub::OnBleBatchScanResultsEventInner},
37 {static_cast<uint32_t>(
38 BluetoothBleCentralManagerCallbackInterfaceCode::BT_BLE_CENTRAL_MANAGER_CALLBACK_SCAN_FAILED),
39 BluetoothBleCentralManagerCallBackStub::OnStartOrStopScanEventInner},
40 {static_cast<uint32_t>(
41 BluetoothBleCentralManagerCallbackInterfaceCode::BT_BLE_LPDEVICE_CALLBACK_NOTIFY_MSG_REPORT),
42 BluetoothBleCentralManagerCallBackStub::OnNotifyMsgReportFromLpDeviceInner},
43 };
44
BluetoothBleCentralManagerCallBackStub()45 BluetoothBleCentralManagerCallBackStub::BluetoothBleCentralManagerCallBackStub()
46 {
47 HILOGD("start.");
48 }
49
~BluetoothBleCentralManagerCallBackStub()50 BluetoothBleCentralManagerCallBackStub::~BluetoothBleCentralManagerCallBackStub()
51 {
52 HILOGD("start.");
53 }
54
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)55 int BluetoothBleCentralManagerCallBackStub::OnRemoteRequest(
56 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
57 {
58 HILOGD("BluetoothBleCentralManagerCallBackStub::OnRemoteRequest, cmd = %d, flags= %d", code, option.GetFlags());
59 if (BluetoothBleCentralManagerCallBackStub::GetDescriptor() != data.ReadInterfaceToken()) {
60 HILOGI("local descriptor is not equal to remote");
61 return ERR_INVALID_STATE;
62 }
63
64 auto itFunc = memberFuncMap_.find(code);
65 if (itFunc != memberFuncMap_.end()) {
66 auto memberFunc = itFunc->second;
67 if (memberFunc != nullptr) {
68 return memberFunc(this, data, reply);
69 }
70 }
71 HILOGW("BluetoothBleCentralManagerCallBackStub::OnRemoteRequest, default case, need check.");
72 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
73 }
74
75 __attribute__((no_sanitize("cfi")))
OnScanCallbackInner(BluetoothBleCentralManagerCallBackStub * stub,MessageParcel & data,MessageParcel & reply)76 ErrCode BluetoothBleCentralManagerCallBackStub::OnScanCallbackInner(
77 BluetoothBleCentralManagerCallBackStub *stub, MessageParcel &data, MessageParcel &reply)
78 {
79 std::shared_ptr<BluetoothBleScanResult> result(data.ReadParcelable<BluetoothBleScanResult>());
80 if (!result) {
81 return TRANSACTION_ERR;
82 }
83 uint8_t callbackType = data.ReadUint8();
84 stub->OnScanCallback(*result, callbackType);
85 return NO_ERROR;
86 }
87
88 __attribute__((no_sanitize("cfi")))
OnBleBatchScanResultsEventInner(BluetoothBleCentralManagerCallBackStub * stub,MessageParcel & data,MessageParcel & reply)89 ErrCode BluetoothBleCentralManagerCallBackStub::OnBleBatchScanResultsEventInner(
90 BluetoothBleCentralManagerCallBackStub *stub, MessageParcel &data, MessageParcel &reply)
91 {
92 int32_t infoSize = 0;
93 if (!data.ReadInt32(infoSize) || infoSize > BLE_CENTRAL_MANAGER_READ_DATA_SIZE_MAX_LEN) {
94 HILOGE("read Parcelable size failed.");
95 return ERR_INVALID_STATE;
96 }
97
98 std::vector<BluetoothBleScanResult> parcelableInfos;
99 for (int32_t index = 0; index < infoSize; index++) {
100 sptr<BluetoothBleScanResult> info(data.ReadParcelable<BluetoothBleScanResult>());
101 if (info == nullptr) {
102 HILOGI("read Parcelable infos failed.");
103 return ERR_INVALID_STATE;
104 }
105 parcelableInfos.emplace_back(*info);
106 }
107 stub->OnBleBatchScanResultsEvent(parcelableInfos);
108 return NO_ERROR;
109 }
110
111 __attribute__((no_sanitize("cfi")))
OnStartOrStopScanEventInner(BluetoothBleCentralManagerCallBackStub * stub,MessageParcel & data,MessageParcel & reply)112 ErrCode BluetoothBleCentralManagerCallBackStub::OnStartOrStopScanEventInner(
113 BluetoothBleCentralManagerCallBackStub *stub, MessageParcel &data, MessageParcel &reply)
114 {
115 int32_t resultCode = data.ReadInt32();
116 bool isStartScan = data.ReadBool();
117 stub->OnStartOrStopScanEvent(resultCode, isStartScan);
118 return NO_ERROR;
119 }
120
121 __attribute__((no_sanitize("cfi")))
OnNotifyMsgReportFromLpDeviceInner(BluetoothBleCentralManagerCallBackStub * stub,MessageParcel & data,MessageParcel & reply)122 ErrCode BluetoothBleCentralManagerCallBackStub::OnNotifyMsgReportFromLpDeviceInner(
123 BluetoothBleCentralManagerCallBackStub *stub, MessageParcel &data,
124 MessageParcel &reply)
125 {
126 std::shared_ptr<BluetoothUuid> uuid(data.ReadParcelable<BluetoothUuid>());
127 if (uuid == nullptr) {
128 HILOGE("[OnNotifyMsgReportFromLpDeviceInner] read uuid failed");
129 return ERR_INVALID_VALUE;
130 }
131 bluetooth::Uuid btUuid = bluetooth::Uuid(*uuid);
132 int32_t msgType = data.ReadInt32();
133 std::vector<uint8_t> dataValue;
134 if (!data.ReadUInt8Vector(&dataValue)) {
135 HILOGE("[OnNotifyMsgReportFromLpDeviceInner] read dataValue failed");
136 return ERR_INVALID_VALUE;
137 }
138 stub->OnNotifyMsgReportFromLpDevice(btUuid, msgType, dataValue);
139 return NO_ERROR;
140 }
141
142 } // namespace Bluetooth
143 } // namespace OHOS
144