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