Files
nearby/cpp/core_v2/internal/ble_advertisement.h
T
Alexey Polyudov 6b27a508ed nearby: snapshot of cl/317978613
Signed-off-by: Alexey Polyudov <apolyudov@google.com>
Change-Id: I4bf367877a986910bb03e1c54aa972b4bcd59942
2020-06-23 19:52:26 -07:00

86 lines
3.2 KiB
C++

#ifndef CORE_V2_INTERNAL_BLE_ADVERTISEMENT_H_
#define CORE_V2_INTERNAL_BLE_ADVERTISEMENT_H_
#include "core_v2/internal/pcp.h"
#include "platform_v2/base/byte_array.h"
namespace location {
namespace nearby {
namespace connections {
// Represents the format of the Connections Ble Advertisement used in
// Advertising + Discovery.
//
// <p>[VERSION][PCP][SERVICE_ID_HASH][ENDPOINT_ID][ENDPOINT_NAME_SIZE]
// [ENDPOINT_NAME][BLUETOOTH_MAC]
//
// <p>See go/connections-ble-advertisement for more information.
class BleAdvertisement {
public:
// Versions of the BleAdvertisement.
enum class Version {
kUndefined = 0,
kV1 = 1,
// Version is only allocated 3 bits in the BleAdvertisement, so this
// can never go beyond V7.
};
static constexpr int kServiceIdHashLength = 3;
static constexpr int kVersionAndPcpLength = 1;
// Should be defined as EndpointManager<Platform>::kEndpointIdLength, but that
// involves making BleAdvertisement templatized on Platform just for
// that one little thing, so forget it (at least for now).
static constexpr int kEndpointIdLength = 4;
static constexpr int kEndpointNameSizeLength = 1;
static constexpr int kBluetoothMacAddressLength = 6;
static constexpr int kMinAdvertisementLength =
kVersionAndPcpLength + kServiceIdHashLength + kEndpointIdLength +
kEndpointNameSizeLength + kBluetoothMacAddressLength;
static constexpr int kMaxEndpointNameLength = 131;
static constexpr int kVersionBitmask = 0x0E0;
static constexpr int kPcpBitmask = 0x01F;
static constexpr int kEndpointNameLengthBitmask = 0x0FF;
BleAdvertisement() = default;
BleAdvertisement(Version version, Pcp pcp, const ByteArray& service_id_hash,
const std::string& endpoint_id,
const std::string& endpoint_name,
const std::string& bluetooth_mac_address);
explicit BleAdvertisement(const ByteArray& ble_advertisement_bytes);
BleAdvertisement(const BleAdvertisement&) = default;
BleAdvertisement& operator=(const BleAdvertisement&) = default;
BleAdvertisement(BleAdvertisement&&) = default;
BleAdvertisement& operator=(BleAdvertisement&&) = default;
~BleAdvertisement() = default;
explicit operator ByteArray() const;
bool IsValid() const { return !endpoint_id_.empty(); }
Version GetVersion() const { return version_; }
Pcp GetPcp() const { return pcp_; }
ByteArray GetServiceIdHash() const { return service_id_hash_; }
std::string GetEndpointId() const { return endpoint_id_; }
std::string GetEndpointName() const { return endpoint_name_; }
std::string GetBluetoothMacAddress() const { return bluetooth_mac_address_; }
private:
ByteArray BluetoothMacAddressHexStringToBytes(
const std::string& bluetooth_mac_address) const;
std::string HexBytesToColonDelimitedString(const ByteArray& hex_bytes) const;
bool IsBluetoothMacAddressUnset(
const ByteArray& bluetooth_mac_address_bytes) const;
Version version_ = Version::kUndefined;
Pcp pcp_ = Pcp::kUnknown;
ByteArray service_id_hash_;
std::string endpoint_id_;
std::string endpoint_name_;
std::string bluetooth_mac_address_;
};
} // namespace connections
} // namespace nearby
} // namespace location
#endif // CORE_V2_INTERNAL_BLE_ADVERTISEMENT_H_