forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChipDeviceScanner.cpp
281 lines (231 loc) · 9.71 KB
/
ChipDeviceScanner.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
*
* Copyright (c) 2021 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file
* Provides an implementation of the Matter Device Scanner
* for the Tizen platforms
*/
#include "ChipDeviceScanner.h"
#include <cstdint>
#include <cstring>
#include <strings.h>
#include <utility>
#include <bluetooth.h>
#include <bluetooth_internal.h>
#include <ble/BleUUID.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/Span.h>
#include <lib/support/logging/CHIPLogging.h>
#include <platform/GLibTypeDeleter.h>
#include <platform/PlatformManager.h>
namespace chip {
namespace DeviceLayer {
namespace Internal {
ChipDeviceScanner::ChipDeviceScanner(ChipDeviceScannerDelegate * delegate) : mDelegate(delegate) {}
ChipDeviceScanner::~ChipDeviceScanner()
{
// In case scan is ongoing
StopChipScan();
mDelegate = nullptr;
}
std::unique_ptr<ChipDeviceScanner> ChipDeviceScanner::Create(ChipDeviceScannerDelegate * delegate)
{
return std::make_unique<ChipDeviceScanner>(delegate);
}
static void __PrintLEScanData(const bt_adapter_le_service_data_s & data)
{
// Print Service UUID in the Service Data
ChipLogDetail(DeviceLayer, "======Service UUID========");
ChipLogDetail(DeviceLayer, "Service UUID::[%s]", data.service_uuid);
// Print Service Data
ChipLogDetail(DeviceLayer, "======Service Data========");
ChipLogDetail(DeviceLayer, "Service Data Length::[%d]", data.service_data_len);
ChipLogByteSpan(DeviceLayer, ByteSpan(reinterpret_cast<uint8_t *>(data.service_data), data.service_data_len));
}
static bool __IsChipThingDevice(bt_adapter_le_device_scan_result_info_s * info,
chip::Ble::ChipBLEDeviceIdentificationInfo & aDeviceInfo)
{
VerifyOrReturnError(info != nullptr, false);
int count = 0;
bt_adapter_le_service_data_s * dataList = nullptr;
bool isChipDevice = false;
if (bt_adapter_le_get_scan_result_service_data_list(info, BT_ADAPTER_LE_PACKET_ADVERTISING, &dataList, &count) == BT_ERROR_NONE)
{
for (int i = 0; i < count; i++)
{
if (strcasecmp(dataList[i].service_uuid, chip::Ble::CHIP_BLE_SERVICE_LONG_UUID_STR) == 0 ||
strcasecmp(dataList[i].service_uuid, chip::Ble::CHIP_BLE_SERVICE_SHORT_UUID_STR) == 0)
{
__PrintLEScanData(dataList[i]);
memcpy(&aDeviceInfo, dataList[i].service_data, dataList[i].service_data_len);
isChipDevice = true;
break;
}
}
}
bt_adapter_le_free_service_data_list(dataList, count);
return isChipDevice;
}
void ChipDeviceScanner::LeScanResultCb(int result, bt_adapter_le_device_scan_result_info_s * info, void * userData)
{
VerifyOrReturn(info != nullptr);
auto self = reinterpret_cast<ChipDeviceScanner *>(userData);
chip::Ble::ChipBLEDeviceIdentificationInfo deviceInfo;
ChipLogProgress(DeviceLayer, "LE device reported: %s", info->remote_address);
VerifyOrReturn(__IsChipThingDevice(info, deviceInfo),
ChipLogDetail(Ble, "Device %s does not look like a CHIP device", info->remote_address));
// Report probable CHIP device to BLEMgrImp class
self->mDelegate->OnChipDeviceScanned(info, deviceInfo);
}
gboolean ChipDeviceScanner::TimerExpiredCb(gpointer userData)
{
auto self = reinterpret_cast<ChipDeviceScanner *>(userData);
ChipLogProgress(DeviceLayer, "Scan Timer expired!!");
self->StopChipScan();
return G_SOURCE_REMOVE;
}
CHIP_ERROR ChipDeviceScanner::TriggerScan(ChipDeviceScanner * self)
{
GAutoPtr<GSource> idleSource;
int ret;
// Trigger LE Scan
ret = bt_adapter_le_start_scan(LeScanResultCb, self);
VerifyOrReturnValue(ret == BT_ERROR_NONE, CHIP_ERROR_INTERNAL,
ChipLogError(DeviceLayer, "bt_adapter_le_start_scan() failed: %s", get_error_message(ret)));
self->mIsScanning = true;
// Setup timer for scan timeout
idleSource = GAutoPtr<GSource>(g_timeout_source_new(self->mScanTimeoutMs));
g_source_set_callback(idleSource.get(), TimerExpiredCb, self, nullptr);
g_source_set_priority(idleSource.get(), G_PRIORITY_HIGH_IDLE);
g_source_attach(idleSource.get(), g_main_context_get_thread_default());
return CHIP_NO_ERROR;
}
static bool __IsScanFilterSupported()
{
bool is_supported;
int ret = bt_adapter_le_is_scan_filter_supported(&is_supported);
VerifyOrReturnValue(ret == BT_ERROR_NONE, false,
ChipLogError(DeviceLayer, "bt_adapter_le_is_scan_filter_supported() failed: %s", get_error_message(ret)));
return is_supported;
}
int ChipDeviceScanner::SetupScanFilter(ScanFilterType filterType, const ScanFilterData & filterData)
{
VerifyOrReturnValue(__IsScanFilterSupported(), BT_ERROR_NONE, ChipLogError(DeviceLayer, "BLE scan filter not supported"));
int ret = CreateLEScanFilter(filterType);
VerifyOrExit(ret == BT_ERROR_NONE,
ChipLogError(DeviceLayer, "BLE scan filter creation failed: %s. Do Normal Scan", get_error_message(ret)));
ret = RegisterScanFilter(filterType, filterData);
VerifyOrExit(ret == BT_ERROR_NONE,
ChipLogError(DeviceLayer, "BLE scan filter registration failed: %s. Do Normal Scan", get_error_message(ret)));
return ret;
exit:
UnRegisterScanFilter();
return ret;
}
CHIP_ERROR ChipDeviceScanner::StartChipScan(System::Clock::Timeout timeout, ScanFilterType filterType,
const ScanFilterData & filterData)
{
CHIP_ERROR err = CHIP_NO_ERROR;
ReturnErrorCodeIf(mIsScanning, CHIP_ERROR_INCORRECT_STATE);
// Scan Filter Setup if supported: silently bypass error & do filterless scan in case of error
SetupScanFilter(filterType, filterData);
mScanTimeoutMs = System::Clock::Milliseconds32(timeout).count();
// All set to trigger LE Scan
ChipLogProgress(DeviceLayer, "Start CHIP BLE scan: timeout=%ums", mScanTimeoutMs);
err = PlatformMgrImpl().GLibMatterContextInvokeSync(TriggerScan, this);
SuccessOrExit(err);
return CHIP_NO_ERROR;
exit:
ChipLogError(DeviceLayer, "Start CHIP Scan could not succeed fully! Stop Scan...");
StopChipScan();
UnRegisterScanFilter();
return err;
}
CHIP_ERROR ChipDeviceScanner::StopChipScan()
{
int ret = BT_ERROR_NONE;
ReturnErrorCodeIf(!mIsScanning, CHIP_ERROR_INCORRECT_STATE);
ret = bt_adapter_le_stop_scan();
if (ret != BT_ERROR_NONE)
{
ChipLogError(DeviceLayer, "bt_adapter_le_stop_scan() failed: %s", get_error_message(ret));
}
ChipLogProgress(DeviceLayer, "CHIP Scanner Async Thread Quit Done..Wait for Thread Windup...!");
UnRegisterScanFilter();
// Report to Impl class
mDelegate->OnScanComplete();
mIsScanning = false;
return CHIP_NO_ERROR;
}
void ChipDeviceScanner::UnRegisterScanFilter()
{
if (mScanFilter)
{
bt_adapter_le_scan_filter_unregister(mScanFilter);
mScanFilter = nullptr;
}
}
int ChipDeviceScanner::RegisterScanFilter(ScanFilterType filterType, const ScanFilterData & filterData)
{
int ret = BT_ERROR_NONE;
switch (filterType)
{
case ScanFilterType::kAddress: {
ChipLogProgress(DeviceLayer, "Register BLE scan filter: Address");
ret = bt_adapter_le_scan_filter_set_device_address(mScanFilter, filterData.address);
VerifyOrExit(
ret == BT_ERROR_NONE,
ChipLogError(DeviceLayer, "bt_adapter_le_scan_filter_set_device_address() failed: %s", get_error_message(ret)));
break;
}
case ScanFilterType::kServiceUUID: {
ChipLogProgress(DeviceLayer, "Register BLE scan filter: Service UUID");
ret = bt_adapter_le_scan_filter_set_service_uuid(mScanFilter, filterData.service_uuid);
VerifyOrExit(ret == BT_ERROR_NONE,
ChipLogError(DeviceLayer, "bt_adapter_le_scan_filter_set_service_uuid() failed: %s", get_error_message(ret)));
break;
}
case ScanFilterType::kServiceData: {
ChipLogProgress(DeviceLayer, "Register BLE scan filter: Service Data");
ret = bt_adapter_le_scan_filter_set_service_data(mScanFilter, filterData.service_uuid, filterData.service_data,
filterData.service_data_len);
VerifyOrExit(ret == BT_ERROR_NONE,
ChipLogError(DeviceLayer, "bt_adapter_le_scan_filter_set_service_data() failed: %s", get_error_message(ret)));
break;
}
case ScanFilterType::kNoFilter:
default:
goto exit;
}
ret = bt_adapter_le_scan_filter_register(mScanFilter);
VerifyOrExit(ret == BT_ERROR_NONE,
ChipLogError(DeviceLayer, "bt_adapter_le_scan_filter_register() failed: %s", get_error_message(ret)));
exit:
return ret;
}
int ChipDeviceScanner::CreateLEScanFilter(ScanFilterType filterType)
{
int ret = bt_adapter_le_scan_filter_create(&mScanFilter);
VerifyOrExit(ret == BT_ERROR_NONE,
ChipLogError(DeviceLayer, "bt_adapter_le_scan_filter_create() failed: %s", get_error_message(ret)));
ChipLogProgress(DeviceLayer, "BLE scan filter created successfully");
exit:
return ret;
}
} // namespace Internal
} // namespace DeviceLayer
} // namespace chip