Skip to content

Commit

Permalink
Enabled Bluetooth connection in android wrapper
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 628218719
  • Loading branch information
ggli-google authored and tkiela1 committed Apr 26, 2024
1 parent f4bad55 commit ed2b94f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
54 changes: 53 additions & 1 deletion internal/platform/uuid.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,20 @@

#include "internal/platform/uuid.h"

#include <array>
#include <cstdint>
#include <iomanip>
#include <ios>
#include <optional>
#include <ostream>
#include <sstream>
#include <string>
#include <vector>

#include "absl/strings/escaping.h"
#include "absl/strings/numbers.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_split.h"
#include "absl/strings/string_view.h"
#include "internal/platform/implementation/crypto.h"

namespace nearby {
Expand All @@ -32,6 +41,49 @@ std::ostream& write_hex(std::ostream& os, absl::string_view data) {
}
} // namespace

// Based on the Java implementation
// http://androidxref.com/8.0.0_r4/xref/libcore/ojluni/src/main/java/java/util/UUID.java#191
std::optional<Uuid> Uuid::FromString(absl::string_view data) {
std::vector<std::string> components = absl::StrSplit(data, '-');
if (components.size() != 5) {
return std::nullopt;
}

for (std::string& component : components) {
component = absl::StrCat("0x", component);
}

int64_t most_sig_bits = 0L;
int64_t least_sig_bits = 0L;
int64_t temp;
if (!absl::SimpleHexAtoi(components[0], &temp)) {
return std::nullopt;
}
most_sig_bits = temp;
most_sig_bits <<= 16;
if (!absl::SimpleHexAtoi(components[1], &temp)) {
return std::nullopt;
}
most_sig_bits |= temp;
most_sig_bits <<= 16;
if (!absl::SimpleHexAtoi(components[2], &temp)) {
return std::nullopt;
}
most_sig_bits |= temp;

if (!absl::SimpleHexAtoi(components[3], &temp)) {
return std::nullopt;
}
least_sig_bits = temp;
least_sig_bits <<= 48;
if (!absl::SimpleHexAtoi(components[4], &temp)) {
return std::nullopt;
}
least_sig_bits |= temp;

return Uuid(most_sig_bits, least_sig_bits);
}

Uuid::Uuid(absl::string_view data) {
// Based on the Java counterpart at
// http://androidxref.com/8.0.0_r4/xref/libcore/ojluni/src/main/java/java/util/UUID.java#162.
Expand Down
5 changes: 5 additions & 0 deletions internal/platform/uuid.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <array>
#include <cstdint>
#include <optional>
#include <string>

#include "absl/strings/string_view.h"
Expand All @@ -32,6 +33,10 @@ class Uuid final {
public:
Uuid() = default;

// Constructs a UUID from the canonical string format as
// xxxxxABCD-xxxx-xxxx-xxxxxxxxxxxxxx
static std::optional<Uuid> FromString(absl::string_view data);

// Constructs a type 3 (name based) UUID based on the input string.
explicit Uuid(absl::string_view data);
// Constructs a new UUID using the specified most_sig_bits for the most
Expand Down
12 changes: 12 additions & 0 deletions internal/platform/uuid_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@

#include "internal/platform/uuid.h"

#include <array>
#include <cstdint>
#include <optional>
#include <string>

#include "gmock/gmock.h"
#include "protobuf-matchers/protocol-buffer-matchers.h"
#include "gtest/gtest.h"
#include "absl/strings/string_view.h"
#include "internal/platform/crypto.h"
#include "internal/platform/logging.h"

Expand Down Expand Up @@ -131,5 +135,13 @@ TEST(UuidTest, OperatorGreaterThan) {
EXPECT_TRUE(a < b);
}

TEST(UuidTest, ConstructUuidFromString) {
std::optional<Uuid> a =
Uuid::FromString("12345678-1234-1234-1234-123456789012");

ASSERT_TRUE(a.has_value());
EXPECT_EQ(std::string(*a), "12345678-1234-1234-1234-123456789012");
}

} // namespace
} // namespace nearby

0 comments on commit ed2b94f

Please sign in to comment.