mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
@@ -18,6 +18,12 @@
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
<<<<<<< HEAD
|
||||
=======
|
||||
#include "core_v2/internal/mediums/ble_v2/ble_advertisement.h"
|
||||
#include "core_v2/internal/mediums/utils.h"
|
||||
#include "platform_v2/base/prng.h"
|
||||
>>>>>>> release
|
||||
#include "platform_v2/public/logging.h"
|
||||
#include "platform_v2/public/mutex_lock.h"
|
||||
|
||||
@@ -25,6 +31,18 @@ namespace location {
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
|
||||
<<<<<<< HEAD
|
||||
=======
|
||||
ByteArray Ble::GenerateHash(const std::string& source, size_t size) {
|
||||
return Utils::Sha256Hash(source, size);
|
||||
}
|
||||
|
||||
ByteArray Ble::GenerateDeviceToken() {
|
||||
return Utils::Sha256Hash(std::to_string(Prng().NextUint32()),
|
||||
mediums::BleAdvertisement::kDeviceTokenLength);
|
||||
}
|
||||
|
||||
>>>>>>> release
|
||||
Ble::Ble(BluetoothRadio& radio) : radio_(radio) {}
|
||||
|
||||
bool Ble::IsAvailable() const {
|
||||
@@ -77,7 +95,27 @@ bool Ble::StartAdvertising(const std::string& service_id,
|
||||
<< ", service id=" << service_id
|
||||
<< ", fast advertisement service uuid="
|
||||
<< fast_advertisement_service_uuid;
|
||||
<<<<<<< HEAD
|
||||
if (!medium_.StartAdvertising(service_id, advertisement_bytes,
|
||||
=======
|
||||
|
||||
// Wrap the connections advertisement to the medium advertisement.
|
||||
const bool fast_advertisement = !fast_advertisement_service_uuid.empty();
|
||||
ByteArray service_id_hash{GenerateHash(
|
||||
service_id, mediums::BleAdvertisement::kServiceIdHashLength)};
|
||||
ByteArray medium_advertisement_bytes{mediums::BleAdvertisement{
|
||||
mediums::BleAdvertisement::Version::kV2,
|
||||
mediums::BleAdvertisement::SocketVersion::kV2,
|
||||
fast_advertisement ? ByteArray{} : service_id_hash, advertisement_bytes,
|
||||
GenerateDeviceToken()}};
|
||||
if (medium_advertisement_bytes.Empty()) {
|
||||
NEARBY_LOGS(INFO) << "Failed to BLE advertise because we could not "
|
||||
"create a medium advertisement.";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!medium_.StartAdvertising(service_id, medium_advertisement_bytes,
|
||||
>>>>>>> release
|
||||
fast_advertisement_service_uuid)) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Failed to turn on BLE advertising with advertisement bytes="
|
||||
@@ -124,6 +162,11 @@ bool Ble::StartScanning(const std::string& service_id,
|
||||
DiscoveredPeripheralCallback callback) {
|
||||
MutexLock lock(&mutex_);
|
||||
|
||||
<<<<<<< HEAD
|
||||
=======
|
||||
discovered_peripheral_callback_ = std::move(callback);
|
||||
|
||||
>>>>>>> release
|
||||
if (service_id.empty()) {
|
||||
NEARBY_LOGS(INFO)
|
||||
<< "Refusing to start BLE scanning with empty service id.";
|
||||
@@ -148,8 +191,34 @@ bool Ble::StartScanning(const std::string& service_id,
|
||||
return false;
|
||||
}
|
||||
|
||||
<<<<<<< HEAD
|
||||
if (!medium_.StartScanning(service_id, fast_advertisement_service_uuid,
|
||||
callback)) {
|
||||
=======
|
||||
if (!medium_.StartScanning(
|
||||
service_id, fast_advertisement_service_uuid,
|
||||
{
|
||||
.peripheral_discovered_cb =
|
||||
[this](BlePeripheral& peripheral,
|
||||
const std::string& service_id,
|
||||
const ByteArray& medium_advertisement_bytes,
|
||||
bool fast_advertisement) {
|
||||
// Unwrap connection BleAdvertisement from medium
|
||||
// BleAdvertisement.
|
||||
auto connection_advertisement_bytes =
|
||||
UnwrapAdvertisementBytes(medium_advertisement_bytes);
|
||||
discovered_peripheral_callback_.peripheral_discovered_cb(
|
||||
peripheral, service_id, connection_advertisement_bytes,
|
||||
fast_advertisement);
|
||||
},
|
||||
.peripheral_lost_cb =
|
||||
[this](BlePeripheral& peripheral,
|
||||
const std::string& service_id) {
|
||||
discovered_peripheral_callback_.peripheral_lost_cb(
|
||||
peripheral, service_id);
|
||||
},
|
||||
})) {
|
||||
>>>>>>> release
|
||||
NEARBY_LOGS(INFO) << "Failed to start scan of BLE services.";
|
||||
return false;
|
||||
}
|
||||
@@ -286,6 +355,19 @@ BleSocket Ble::Connect(BlePeripheral& peripheral,
|
||||
return socket;
|
||||
}
|
||||
|
||||
<<<<<<< HEAD
|
||||
=======
|
||||
ByteArray Ble::UnwrapAdvertisementBytes(
|
||||
const ByteArray& medium_advertisement_data) {
|
||||
mediums::BleAdvertisement medium_ble_advertisement{medium_advertisement_data};
|
||||
if (!medium_ble_advertisement.IsValid()) {
|
||||
return ByteArray{};
|
||||
}
|
||||
|
||||
return medium_ble_advertisement.GetData();
|
||||
}
|
||||
|
||||
>>>>>>> release
|
||||
} // namespace connections
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
Reference in New Issue
Block a user