Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabled Bluetooth connection in android wrapper #2494

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading