mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
Added utility methods to handle winrt::guid and nearby::uuid
PiperOrigin-RevId: 509583159
This commit is contained in:
committed by
Copybara-Service
parent
1e229e31c5
commit
7fd2515b4e
@@ -26,6 +26,7 @@
|
||||
|
||||
// Third party headers
|
||||
#include "absl/strings/ascii.h"
|
||||
#include "absl/strings/str_cat.h"
|
||||
#include "absl/strings/str_format.h"
|
||||
|
||||
// Nearby connections headers
|
||||
@@ -34,8 +35,10 @@
|
||||
#include "internal/platform/byte_array.h"
|
||||
#include "internal/platform/implementation/crypto.h"
|
||||
#include "internal/platform/logging.h"
|
||||
#include "internal/platform/uuid.h"
|
||||
#include "winrt/Windows.Foundation.Collections.h"
|
||||
#include "winrt/Windows.Networking.Connectivity.h"
|
||||
#include "winrt/base.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace windows {
|
||||
@@ -173,6 +176,49 @@ std::vector<std::string> Get4BytesIpv4Addresses() {
|
||||
return result;
|
||||
}
|
||||
|
||||
Uuid winrt_guid_to_nearby_uuid(const ::winrt::guid& guid) {
|
||||
int64_t data1 = guid.Data1;
|
||||
int64_t data2 = guid.Data2;
|
||||
int64_t data3 = guid.Data3;
|
||||
|
||||
int64_t msb = ((data1 >> 24) & 0xff) << 56 | ((data1 >> 16) & 0xff) << 48 |
|
||||
((data1 >> 8) & 0xff) << 40 | ((data1)&0xff) << 32 |
|
||||
((data2 >> 8) & 0xff) << 24 | ((data2)&0xff) << 16 |
|
||||
((data3 >> 8) & 0xff) << 8 | (data3 & 0xff);
|
||||
|
||||
int64_t lsb =
|
||||
((int64_t)guid.Data4[0]) << 56 | ((int64_t)guid.Data4[1]) << 48 |
|
||||
((int64_t)guid.Data4[2]) << 40 | ((int64_t)guid.Data4[3]) << 32 |
|
||||
((int64_t)guid.Data4[4]) << 24 | ((int64_t)guid.Data4[5]) << 16 |
|
||||
((int64_t)guid.Data4[6]) << 8 | (int64_t)guid.Data4[7];
|
||||
|
||||
return Uuid(msb, lsb);
|
||||
}
|
||||
|
||||
winrt::guid nearby_uuid_to_winrt_guid(Uuid uuid) {
|
||||
winrt::guid guid;
|
||||
uint64_t msb = uuid.GetMostSigBits();
|
||||
guid.Data1 = ((msb >> 56) & 0xff) << 24 | ((msb >> 48) & 0xff) << 16 |
|
||||
((msb >> 40) & 0xff) << 8 | ((msb >> 32) & 0xff);
|
||||
guid.Data2 = ((msb >> 24) & 0xff) << 8 | ((msb >> 16) & 0xff);
|
||||
guid.Data3 = ((msb >> 8) & 0xff) << 8 | (msb & 0xff);
|
||||
uint64_t lsb = uuid.GetLeastSigBits();
|
||||
guid.Data4[0] = (lsb >> 56) & 0xff;
|
||||
guid.Data4[1] = (lsb >> 48) & 0xff;
|
||||
guid.Data4[2] = (lsb >> 40) & 0xff;
|
||||
guid.Data4[3] = (lsb >> 32) & 0xff;
|
||||
guid.Data4[4] = (lsb >> 24) & 0xff;
|
||||
guid.Data4[5] = (lsb >> 16) & 0xff;
|
||||
guid.Data4[6] = (lsb >> 8) & 0xff;
|
||||
guid.Data4[7] = lsb & 0xff;
|
||||
return guid;
|
||||
}
|
||||
|
||||
bool is_nearby_uuid_equal_to_winrt_guid(const Uuid& uuid,
|
||||
const ::winrt::guid& guid) {
|
||||
return uuid == winrt_guid_to_nearby_uuid(guid);
|
||||
}
|
||||
|
||||
ByteArray Sha256(absl::string_view input, size_t size) {
|
||||
ByteArray hash = nearby::Crypto::Sha256(input);
|
||||
return ByteArray{hash.data(), size};
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "internal/platform/byte_array.h"
|
||||
#include "internal/platform/uuid.h"
|
||||
#include "winrt/Windows.Foundation.h"
|
||||
#include "winrt/base.h"
|
||||
|
||||
@@ -46,6 +47,14 @@ ByteArray Sha256(absl::string_view input, size_t size);
|
||||
std::vector<std::string> GetIpv4Addresses();
|
||||
std::vector<std::string> Get4BytesIpv4Addresses();
|
||||
|
||||
// Help methods to convert between Uuid and winrt::guid
|
||||
Uuid winrt_guid_to_nearby_uuid(const ::winrt::guid& guid);
|
||||
winrt::guid nearby_uuid_to_winrt_guid(Uuid uuid);
|
||||
|
||||
// Check whether Uuid and guid is the same value.
|
||||
bool is_nearby_uuid_equal_to_winrt_guid(const Uuid& uuid,
|
||||
const ::winrt::guid& guid);
|
||||
|
||||
namespace Constants {
|
||||
// The Id of the Service Name SDP attribute
|
||||
const uint16_t SdpServiceNameAttributeId = 0x100;
|
||||
|
||||
@@ -15,11 +15,11 @@
|
||||
#include "internal/platform/implementation/windows/utils.h"
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
// using ::nearby::windows::uint64_to_mac_address_string;
|
||||
#include "winrt/base.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace windows {
|
||||
@@ -39,7 +39,7 @@ TEST(UtilsTests, MacAddressToString) {
|
||||
TEST(UtilsTests, StringToMacAddress) {
|
||||
// Arrange
|
||||
std::string input = "34:36:3B:C7:8C:71";
|
||||
const uint64_t expected = 0x000034363bc78c71;
|
||||
const uint64_t expected = 0x000034363bc78c71;
|
||||
|
||||
// Act
|
||||
uint64_t result = mac_address_string_to_uint64(input);
|
||||
@@ -66,5 +66,21 @@ TEST(UtilsTests, IpDotdecimalTo4Bytes) {
|
||||
EXPECT_EQ(result, std::string(kIp4Bytes, 4));
|
||||
}
|
||||
|
||||
TEST(UtilsTests, ConvertBetweenWinrtGuidAndNearbyUuidSuccessfully) {
|
||||
Uuid uuid(0x123e4567e89b12d3, 0xa456426614174000);
|
||||
winrt::guid guid("{123e4567-e89b-12d3-a456-426614174000}");
|
||||
|
||||
EXPECT_EQ(uuid, winrt_guid_to_nearby_uuid(guid));
|
||||
EXPECT_EQ(nearby_uuid_to_winrt_guid(uuid), guid);
|
||||
EXPECT_TRUE(is_nearby_uuid_equal_to_winrt_guid(uuid, guid));
|
||||
}
|
||||
|
||||
TEST(UtilsTests, CompareWinrtGuidAndNearbyUuidSuccessfully) {
|
||||
Uuid uuid(0x123e4567e89b12d3, 0xa456426614174000);
|
||||
winrt::guid guid("123e4567-e89b-12d3-a456-426614074000");
|
||||
|
||||
EXPECT_NE(uuid, winrt_guid_to_nearby_uuid(guid));
|
||||
}
|
||||
|
||||
} // namespace windows
|
||||
} // namespace nearby
|
||||
|
||||
@@ -110,4 +110,6 @@ bool Uuid::operator==(const Uuid& rhs) const {
|
||||
GetLeastSigBits() == rhs.GetLeastSigBits();
|
||||
}
|
||||
|
||||
bool Uuid::operator!=(const Uuid& rhs) const { return !(*this == rhs); }
|
||||
|
||||
} // namespace nearby
|
||||
|
||||
@@ -59,6 +59,7 @@ class Uuid final {
|
||||
|
||||
// Hashable
|
||||
bool operator==(const Uuid &rhs) const;
|
||||
bool operator!=(const Uuid &rhs) const;
|
||||
template <typename H>
|
||||
friend H AbslHashValue(H h, const Uuid &b) {
|
||||
return H::combine(std::move(h), b.most_sig_bits_, b.least_sig_bits_);
|
||||
|
||||
Reference in New Issue
Block a user