Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9b377dcf authored by Myles Watson's avatar Myles Watson
Browse files

test_vendor: Add a BtAddress class



The class provides IsValidBtAddress(string), functions to convert
from vectors and strings, and some operators.

Change-Id: I553c47e698ec46ed0e24656f89141c23f7369380
Signed-off-by: default avatarMyles Watson <mylesgw@google.com>
parent 1453168d
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ BT_DIR := $(TOP_DIR)packages/modules/Bluetooth/system

LOCAL_SRC_FILES := \
    src/async_manager.cc \
    src/bt_address.cc \
    src/bt_vendor.cc \
    src/command_packet.cc \
    src/dual_mode_controller.cc \
+86 −0
Original line number Diff line number Diff line
//
// Copyright 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#pragma once

#include <cstdint>
#include <vector>

namespace test_vendor_lib {

// Encapsulate handling for Bluetooth Addresses:
// - store the address
// - convert to/from strings and vectors of bytes
// - check strings to see if they represent a valid address
class BtAddress {
 public:
  // Conversion constants
  static const size_t kStringLength = 17;  // "XX:XX:XX:XX:XX:XX"
  static const size_t kOctets = 6;         // "X0:X1:X2:X3:X4:X5"

  BtAddress() : address_(0){};
  virtual ~BtAddress() = default;

  // Returns true if |addr| has the form "XX:XX:XX:XX:XX:XX":
  // - the length of |addr| is >= kStringLength
  // - every third character is ':'
  // - each remaining character is a hexadecimal digit
  static bool IsValid(const std::string& addr);

  inline bool operator==(const BtAddress& right) {
    return address_ == right.address_;
  }
  inline bool operator!=(const BtAddress& right) {
    return address_ != right.address_;
  }
  inline bool operator<(const BtAddress& right) {
    return address_ < right.address_;
  }
  inline bool operator>(const BtAddress& right) {
    return address_ > right.address_;
  }
  inline bool operator<=(const BtAddress& right) {
    return address_ <= right.address_;
  }
  inline bool operator>=(const BtAddress& right) {
    return address_ >= right.address_;
  }

  inline void operator=(const BtAddress& right) { address_ = right.address_; }
  inline void operator|=(const BtAddress& right) { address_ |= right.address_; }
  inline void operator&=(const BtAddress& right) { address_ &= right.address_; }

  // Set the address to the address represented by |str|.
  // returns true if |str| represents a valid address, otherwise false.
  bool FromString(const std::string& str);

  // Set the address to the address represented by |str|.
  // returns true if octets.size() >= kOctets, otherwise false.
  bool FromVector(const std::vector<uint8_t>& octets);

  // Appends the Bluetooth address to the vector |octets|.
  void ToVector(std::vector<uint8_t>& octets) const;

  // Return a string representation of the Bluetooth address, in this format:
  // "xx:xx:xx:xx:xx:xx", where x represents a lowercase hexadecimal digit.
  std::string ToString() const;

 private:
  // The Bluetooth Address is stored in the lower 48 bits of a 64-bit value
  uint64_t address_;
};

}  // namespace test_vendor_lib
+91 −0
Original line number Diff line number Diff line
//
// Copyright 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#include "bt_address.h"
#include <assert.h>
#include <iomanip>
#include <vector>
using std::vector;

#include "base/logging.h"

namespace test_vendor_lib {

bool BtAddress::IsValid(const std::string& addr) {
  if (addr.length() < kStringLength)
    return false;

  for (size_t i = 0; i < kStringLength; i++) {
    if (i % 3 == 2) {  // Every third character must be ':'
      if (addr[i] != ':')
        return false;
    } else {  // The rest must be hexadecimal digits
      if (!isxdigit(addr[i]))
        return false;
    }
  }
  return true;
}

bool BtAddress::FromString(const std::string& str) {
  std::string tok;
  std::istringstream iss(str);

  if (IsValid(str) == false)
    return false;

  address_ = 0;
  for (size_t i = 0; i < kOctets; i++) {
    getline(iss, tok, ':');
    uint64_t octet = std::stoi(tok, nullptr, 16);
    address_ |= (octet << (8 * ((kOctets - 1) - i)));
  }
  return true;
}

bool BtAddress::FromVector(const vector<uint8_t>& octets) {
  if (octets.size() < kOctets)
    return false;

  address_ = 0;
  for (size_t i = 0; i < kOctets; i++) {
    uint64_t to_shift = octets[i];
    address_ |= to_shift << (8 * i);
  }
  return true;
}

void BtAddress::ToVector(vector<uint8_t>& octets) const {
  for (size_t i = 0; i < kOctets; i++) {
    octets.push_back(address_ >> (8 * i));
  }
}

std::string BtAddress::ToString() const {
  std::stringstream ss;

  ss << std::hex << std::setfill('0') << std::setw(2);
  for (size_t i = 0; i < kOctets; i++) {
    uint64_t octet = (address_ >> (8 * ((kOctets - 1) - i))) & 0xff;
    ss << std::hex << std::setfill('0') << std::setw(2) << octet;
    if (i != kOctets - 1)
      ss << std::string(":");
  }

  return ss.str();
}

}  // namespace test_vendor_lib