mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
nearby: snapshot of cl/304428753
Signed-off-by: Alexey Polyudov <apolyudov@google.com> Change-Id: I0023ac6e40456fc4a8169c3173bcbff3b5ccc476
This commit is contained in:
@@ -10,8 +10,6 @@ namespace nearby {
|
||||
namespace connections {
|
||||
namespace mediums {
|
||||
|
||||
namespace {
|
||||
|
||||
class SampleSystemClock : public SystemClock {
|
||||
public:
|
||||
SampleSystemClock() {}
|
||||
@@ -30,13 +28,24 @@ class SamplePlatform {
|
||||
}
|
||||
};
|
||||
|
||||
// We keep a copy of these constants because this is an old-school test (so we
|
||||
// can't delare it as a friend class of AdvertisementReadResult).
|
||||
constexpr char kAdvertisementBytes[] = {0x0A, 0x0B, 0x0C};
|
||||
|
||||
// Default values may be too big and impractical to wait for in the test.
|
||||
// For the test platform, we redefine them to some reasonable values.
|
||||
const absl::Duration kAdvertisementBaseBackoffDuration =
|
||||
absl::Milliseconds(1000); // 1 second
|
||||
const absl::Duration kAdvertisementMaxBackoffDuration =
|
||||
absl::Milliseconds(6000); // 6 seconds
|
||||
const char kAdvertisementBytes[] = {0x0A, 0x0B, 0x0C};
|
||||
|
||||
template <>
|
||||
const std::int64_t AdvertisementReadResult<
|
||||
SamplePlatform>::kAdvertisementMaxBackoffDurationMillis =
|
||||
ToInt64Milliseconds(kAdvertisementMaxBackoffDuration);
|
||||
template <>
|
||||
const std::int64_t
|
||||
AdvertisementReadResult<
|
||||
SamplePlatform>::kAdvertisementBaseBackoffDurationMillis =
|
||||
ToInt64Milliseconds(kAdvertisementBaseBackoffDuration);
|
||||
|
||||
TEST(AdvertisementReadResultTest, AdvertisementExists) {
|
||||
AdvertisementReadResult<SamplePlatform> advertisement_read_result;
|
||||
@@ -141,7 +150,6 @@ TEST(AdvertisementReadResultTest, GetDurationSinceRead) {
|
||||
ASSERT_GE(advertisement_read_result.getDurationSinceReadMillis(), sleepTime);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace mediums
|
||||
} // namespace connections
|
||||
} // namespace nearby
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "core/internal/mediums/ble_advertisement.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
namespace location {
|
||||
@@ -224,9 +226,10 @@ TEST(BLEAdvertisementTest, DeserializationWorksWithExtraBytes) {
|
||||
|
||||
// Copy the bytes into a new array with extra bytes. We must explicitly
|
||||
// define how long our array is because we can't use variable length arrays.
|
||||
char raw_ble_advertisement_bytes[kLongAdvertisementLength];
|
||||
char raw_ble_advertisement_bytes[kLongAdvertisementLength] {};
|
||||
memcpy(raw_ble_advertisement_bytes, scoped_ble_advertisement_bytes->getData(),
|
||||
kLongAdvertisementLength);
|
||||
std::min(sizeof(raw_ble_advertisement_bytes),
|
||||
scoped_ble_advertisement_bytes->size()));
|
||||
|
||||
// Re-parse the BLE advertisement using our extra long advertisement bytes.
|
||||
ScopedPtr<ConstPtr<ByteArray> > scoped_long_ble_advertisement_bytes(
|
||||
|
||||
@@ -41,6 +41,38 @@ class BLEPacket {
|
||||
ScopedPtr<ConstPtr<ByteArray> > data_;
|
||||
};
|
||||
|
||||
// Represents the format of data sent over BLE sockets.
|
||||
//
|
||||
// [SERVICE_ID_HASH][DATA]
|
||||
//
|
||||
// See go/nearby-ble-design for more information.
|
||||
class BlePacket {
|
||||
public:
|
||||
static BlePacket FromBytes(const ByteArray& bytes);
|
||||
|
||||
static ByteArray ToBytes(const ByteArray& service_id_hash,
|
||||
const ByteArray& data);
|
||||
|
||||
static const uint32_t kServiceIdHashLength;
|
||||
|
||||
~BlePacket();
|
||||
|
||||
ByteArray GetServiceIdHash() const;
|
||||
ByteArray GetData() const;
|
||||
|
||||
private:
|
||||
static size_t ComputeDataSize(const ByteArray& ble_packet_bytes);
|
||||
static size_t ComputePacketLength(const ByteArray& data);
|
||||
|
||||
static const uint32_t kMinPacketLength;
|
||||
static const uint32_t kMaxDataSize;
|
||||
|
||||
BlePacket(const ByteArray& service_id_hash, const ByteArray& data);
|
||||
|
||||
ByteArray service_id_hash_;
|
||||
ByteArray data_;
|
||||
};
|
||||
|
||||
} // namespace mediums
|
||||
} // namespace connections
|
||||
} // namespace nearby
|
||||
|
||||
@@ -22,6 +22,21 @@ class BLEPeripheral {
|
||||
ScopedPtr<ConstPtr<ByteArray>> id_;
|
||||
};
|
||||
|
||||
|
||||
// Represents BLE peripheral for testing.
|
||||
class BlePeripheral {
|
||||
public:
|
||||
explicit BlePeripheral(const ByteArray& id) : id_(id) {}
|
||||
~BlePeripheral() = default;
|
||||
|
||||
const ByteArray& GetId() const { return id_; }
|
||||
|
||||
private:
|
||||
// A unique identifier for this peripheral. It can be the BLE advertisement it
|
||||
// was found on, or even simply the BLE MAC address.
|
||||
const ByteArray id_;
|
||||
};
|
||||
|
||||
} // namespace mediums
|
||||
} // namespace connections
|
||||
} // namespace nearby
|
||||
|
||||
@@ -405,7 +405,7 @@ bool BLEV2<Platform>::startScanning(
|
||||
fast_advertisement_service_uuid);
|
||||
// Avoid leaks.
|
||||
ScopedPtr<Ptr<ScanCallbackFacade>> scan_callback_facade(
|
||||
new ScanCallbackFacade(MakePtr(this)));
|
||||
new ScanCallbackFacade(self_));
|
||||
std::set<string> service_uuids;
|
||||
service_uuids.insert(kCopresenceServiceUuid);
|
||||
if (!ble_medium_->startScanning(service_uuids, power_mode,
|
||||
@@ -427,7 +427,7 @@ void BLEV2<Platform>::onAdvertisementFoundImpl(
|
||||
ConstPtr<BLEAdvertisementData> advertisement_data) {
|
||||
offloadFromPlatformThread(
|
||||
MakePtr(new ble_v2::OnAdvertisementFoundRunnable<Platform>(
|
||||
MakePtr(this), ble_peripheral, advertisement_data)));
|
||||
self_, ble_peripheral, advertisement_data)));
|
||||
}
|
||||
|
||||
// This method is synchronized because it affects class state, but is called
|
||||
@@ -461,11 +461,6 @@ void BLEV2<Platform>::stopScanning() {
|
||||
// TODO(b/112199086) Change to RecurringCancelableAlarm
|
||||
template <typename Platform>
|
||||
Ptr<CancelableAlarm<Platform>> BLEV2<Platform>::createOnLostAlarm() {
|
||||
// return MakePtr(new CancelableAlarm<Platform>(
|
||||
// "BluetoothLowEnergy.startScanning() onLost",
|
||||
// MakePtr(new
|
||||
// ble_v2::ProcessOnLostRunnable<Platform>(MakePtr(this))),
|
||||
// kOnLostTimeoutMillis, on_lost_executor_.get()));
|
||||
return Ptr<CancelableAlarm<Platform>>();
|
||||
}
|
||||
|
||||
@@ -606,7 +601,7 @@ bool BLEV2<Platform>::internalStartAdvertisementGattServer(
|
||||
|
||||
ScopedPtr<Ptr<ServerGATTConnectionLifecycleCallbackFacade>>
|
||||
connection_lifecycle_callback(
|
||||
new ServerGATTConnectionLifecycleCallbackFacade(MakePtr(this)));
|
||||
new ServerGATTConnectionLifecycleCallbackFacade(self_));
|
||||
ScopedPtr<Ptr<GATTServer>> gatt_server(
|
||||
ble_medium_->startGATTServer(connection_lifecycle_callback.get()));
|
||||
if (gatt_server.isNull()) {
|
||||
@@ -737,7 +732,7 @@ BLEV2<Platform>::internalReadFromAdvertisementGattServer(
|
||||
|
||||
ScopedPtr<Ptr<ClientGATTConnectionLifecycleCallbackFacade>>
|
||||
connection_lifecycle_callback(
|
||||
new ClientGATTConnectionLifecycleCallbackFacade(MakePtr(this)));
|
||||
new ClientGATTConnectionLifecycleCallbackFacade(self_));
|
||||
ScopedPtr<Ptr<ClientGATTConnection>> gatt_connection(
|
||||
ble_medium_->connectToGATTServer(peripheral, kDefaultMtu,
|
||||
BLEMediumV2::PowerMode::HIGH,
|
||||
|
||||
@@ -300,6 +300,7 @@ class BLEV2 {
|
||||
Ptr<AdvertisingInfo> advertising_info_;
|
||||
Ptr<GATTServerInfo> gatt_server_info_;
|
||||
Ptr<AcceptingConnectionsInfo> accepting_connections_info_;
|
||||
std::shared_ptr<BLEV2> self_{this, [](void*){}};
|
||||
};
|
||||
|
||||
} // namespace mediums
|
||||
|
||||
@@ -71,8 +71,7 @@ TEST(BloomFilterTest, AddMultipleArgsReturnsNonemptyArray) {
|
||||
ScopedPtr<ConstPtr<ByteArray>> scoped_bloom_filter_bytes(
|
||||
scoped_bloom_filter->asBytes());
|
||||
std::string empty_string(kByteArrayLength, '\0');
|
||||
ASSERT_NE(0, memcmp(scoped_bloom_filter_bytes->getData(), empty_string.data(),
|
||||
empty_string.size()));
|
||||
ASSERT_NE(scoped_bloom_filter_bytes->asString(), empty_string);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user