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 {BluetoothBleCentralManagerCallbackInterfaceCode::BT_BLE_CENTRAL_MANAGER_CALLBACK,
29 std::bind(&BluetoothBleCentralManagerCallBackStub::OnScanCallbackInner, std::placeholders::_1,
30 std::placeholders::_2, std::placeholders::_3)},
31 {BluetoothBleCentralManagerCallbackInterfaceCode::BT_BLE_CENTRAL_MANAGER_BLE_BATCH_CALLBACK,
32 std::bind(&BluetoothBleCentralManagerCallBackStub::OnBleBatchScanResultsEventInner, std::placeholders::_1,
33 std::placeholders::_2, std::placeholders::_3)},
34 {BluetoothBleCentralManagerCallbackInterfaceCode::BT_BLE_CENTRAL_MANAGER_CALLBACK_SCAN_FAILED,
35 std::bind(&BluetoothBleCentralManagerCallBackStub::OnStartOrStopScanEventInner, std::placeholders::_1,
36 std::placeholders::_2, std::placeholders::_3)},
37 {BluetoothBleCentralManagerCallbackInterfaceCode::BT_BLE_LPDEVICE_CALLBACK_NOTIFY_MSG_REPORT,
38 std::bind(&BluetoothBleCentralManagerCallBackStub::OnNotifyMsgReportFromLpDeviceInner,
39 std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)},
40 };
41
BluetoothBleCentralManagerCallBackStub()42 BluetoothBleCentralManagerCallBackStub::BluetoothBleCentralManagerCallBackStub()
43 {
44 HILOGI("start.");
45 }
46
~BluetoothBleCentralManagerCallBackStub()47 BluetoothBleCentralManagerCallBackStub::~BluetoothBleCentralManagerCallBackStub()
48 {
49 HILOGI("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
OnScanCallbackInner(MessageParcel & data,MessageParcel & reply)72 ErrCode BluetoothBleCentralManagerCallBackStub::OnScanCallbackInner(MessageParcel &data, MessageParcel &reply)
73 {
74 std::shared_ptr<BluetoothBleScanResult> result(data.ReadParcelable<BluetoothBleScanResult>());
75 if (!result) {
76 return TRANSACTION_ERR;
77 }
78 OnScanCallback(*result);
79 return NO_ERROR;
80 }
81
OnBleBatchScanResultsEventInner(MessageParcel & data,MessageParcel & reply)82 ErrCode BluetoothBleCentralManagerCallBackStub::OnBleBatchScanResultsEventInner(
83 MessageParcel &data, MessageParcel &reply)
84 {
85 int32_t infoSize = 0;
86 if (!data.ReadInt32(infoSize) || infoSize > BLE_CENTRAL_MANAGER_READ_DATA_SIZE_MAX_LEN) {
87 HILOGE("read Parcelable size failed.");
88 return ERR_INVALID_STATE;
89 }
90
91 std::vector<BluetoothBleScanResult> parcelableInfos;
92 for (int32_t index = 0; index < infoSize; index++) {
93 sptr<BluetoothBleScanResult> info(data.ReadParcelable<BluetoothBleScanResult>());
94 if (info == nullptr) {
95 HILOGI("read Parcelable infos failed.");
96 return ERR_INVALID_STATE;
97 }
98 parcelableInfos.emplace_back(*info);
99 }
100 OnBleBatchScanResultsEvent(parcelableInfos);
101 return NO_ERROR;
102 }
103
OnStartOrStopScanEventInner(MessageParcel & data,MessageParcel & reply)104 ErrCode BluetoothBleCentralManagerCallBackStub::OnStartOrStopScanEventInner(MessageParcel &data, MessageParcel &reply)
105 {
106 int32_t resultCode = data.ReadInt32();
107 bool isStartScan = data.ReadBool();
108 OnStartOrStopScanEvent(resultCode, isStartScan);
109 return NO_ERROR;
110 }
111
OnNotifyMsgReportFromLpDeviceInner(MessageParcel & data,MessageParcel & reply)112 ErrCode BluetoothBleCentralManagerCallBackStub::OnNotifyMsgReportFromLpDeviceInner(MessageParcel &data,
113 MessageParcel &reply)
114 {
115 std::shared_ptr<BluetoothUuid> uuid(data.ReadParcelable<BluetoothUuid>());
116 if (uuid == nullptr) {
117 HILOGE("[OnNotifyMsgReportFromLpDeviceInner] read uuid failed");
118 return ERR_INVALID_VALUE;
119 }
120 bluetooth::Uuid btUuid = bluetooth::Uuid(*uuid);
121 int32_t msgType = data.ReadInt32();
122 std::vector<uint8_t> dataValue;
123 if (!data.ReadUInt8Vector(&dataValue)) {
124 HILOGE("[OnNotifyMsgReportFromLpDeviceInner] read dataValue failed");
125 return ERR_INVALID_VALUE;
126 }
127 OnNotifyMsgReportFromLpDevice(btUuid, msgType, dataValue);
128 return NO_ERROR;
129 }
130
131 } // namespace Bluetooth
132 } // namespace OHOS
133