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

Commit 60d8af15 authored by Myles Watson's avatar Myles Watson
Browse files

Add a ClassOfDevice type

Test: net_test_types
Change-Id: If1f2e294ba46e39219e0c60dd1795d34123714c8
parent f69439ec
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@ cc_library_static {
    ],
    host_supported: true,
    srcs: [
        "class_of_device.cc",
        "raw_address.cc",
        "bluetooth/uuid.cc",
    ],
@@ -30,6 +31,7 @@ cc_test {
    defaults: ["fluoride_defaults"],
    host_supported: true,
    srcs: [
        "test/class_of_device_unittest.cc",
        "test/raw_address_unittest.cc",
        "test/bluetooth/uuid_unittest.cc",
    ],
+78 −0
Original line number Diff line number Diff line
/******************************************************************************
 *
 *  Copyright 2018 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 "class_of_device.h"

#include <base/strings/string_split.h>
#include <base/strings/stringprintf.h>
#include <stdint.h>
#include <algorithm>
#include <vector>

static_assert(sizeof(ClassOfDevice) == ClassOfDevice::kLength,
              "ClassOfDevice must be 3 bytes long!");

ClassOfDevice::ClassOfDevice(const uint8_t (&class_of_device)[kLength]) {
  std::copy(class_of_device, class_of_device + kLength, cod);
};

std::string ClassOfDevice::ToString() const {
  return base::StringPrintf("%03x-%01x-%02x",
                            (static_cast<uint16_t>(cod[2]) << 4) | cod[1] >> 4,
                            cod[1] & 0x0f, cod[0]);
}

bool ClassOfDevice::FromString(const std::string& from, ClassOfDevice& to) {
  ClassOfDevice new_cod;
  if (from.length() != 8) return false;

  std::vector<std::string> byte_tokens =
      base::SplitString(from, "-", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);

  if (byte_tokens.size() != 3) return false;
  if (byte_tokens[0].length() != 3) return false;
  if (byte_tokens[1].length() != 1) return false;
  if (byte_tokens[2].length() != 2) return false;

  uint16_t values[3];

  for (size_t i = 0; i < kLength; i++) {
    const auto& token = byte_tokens[i];

    char* temp = nullptr;
    values[i] = strtol(token.c_str(), &temp, 16);
    if (*temp != '\0') return false;
  }

  new_cod.cod[0] = values[2];
  new_cod.cod[1] = values[1] | ((values[0] & 0xf) << 4);
  new_cod.cod[2] = values[0] >> 4;

  to = new_cod;
  return true;
}

size_t ClassOfDevice::FromOctets(const uint8_t* from) {
  std::copy(from, from + kLength, cod);
  return kLength;
};

bool ClassOfDevice::IsValid(const std::string& cod) {
  ClassOfDevice tmp;
  return ClassOfDevice::FromString(cod, tmp);
}
+54 −0
Original line number Diff line number Diff line
/******************************************************************************
 *
 *  Copyright 2018 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 <string>

/** Bluetooth Class of Device */
class ClassOfDevice final {
 public:
  static constexpr unsigned int kLength = 3;

  uint8_t cod[kLength];

  ClassOfDevice() = default;
  ClassOfDevice(const uint8_t (&class_of_device)[kLength]);

  bool operator==(const ClassOfDevice& rhs) const {
    return (std::memcmp(cod, rhs.cod, sizeof(cod)) == 0);
  }

  std::string ToString() const;

  // Converts |string| to ClassOfDevice and places it in |to|. If |from| does
  // not represent a Class of Device, |to| is not modified and this function
  // returns false. Otherwise, it returns true.
  static bool FromString(const std::string& from, ClassOfDevice& to);

  // Copies |from| raw Class of Device octets to the local object.
  // Returns the number of copied octets (always ClassOfDevice::kLength)
  size_t FromOctets(const uint8_t* from);

  static bool IsValid(const std::string& class_of_device);
};

inline std::ostream& operator<<(std::ostream& os, const ClassOfDevice& c) {
  os << c.ToString();
  return os;
}
+96 −0
Original line number Diff line number Diff line
/******************************************************************************
 *
 *  Copyright 2018 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 <gtest/gtest.h>

#include "class_of_device.h"

static const char* test_class = "efc-d-ab";
static const uint8_t test_bytes[]{0xab, 0xcd, 0xef};

TEST(ClassOfDeviceUnittest, test_constructor_array) {
  ClassOfDevice cod(test_bytes);

  ASSERT_EQ(test_bytes[0], cod.cod[0]);
  ASSERT_EQ(test_bytes[1], cod.cod[1]);
  ASSERT_EQ(test_bytes[2], cod.cod[2]);

  std::string ret = cod.ToString();

  ASSERT_STREQ(test_class, ret.c_str());
}

TEST(ClassOfDeviceUnittest, test_to_from_str) {
  ClassOfDevice cod;
  ClassOfDevice::FromString(test_class, cod);

  ASSERT_EQ(test_bytes[0], cod.cod[0]);
  ASSERT_EQ(test_bytes[1], cod.cod[1]);
  ASSERT_EQ(test_bytes[2], cod.cod[2]);

  std::string ret = cod.ToString();

  ASSERT_STREQ(test_class, ret.c_str());
}

TEST(ClassOfDeviceUnittest, test_from_octets) {
  ClassOfDevice cod;
  size_t expected_result = ClassOfDevice::kLength;
  ASSERT_EQ(expected_result, cod.FromOctets(test_bytes));

  ASSERT_EQ(test_bytes[0], cod.cod[0]);
  ASSERT_EQ(test_bytes[1], cod.cod[1]);
  ASSERT_EQ(test_bytes[2], cod.cod[2]);

  std::string ret = cod.ToString();

  ASSERT_STREQ(test_class, ret.c_str());
}

TEST(ClassOfDeviceTest, test_copy) {
  ClassOfDevice cod1;
  ClassOfDevice cod2;
  ClassOfDevice::FromString(test_class, cod1);
  cod2 = cod1;

  ASSERT_EQ(cod1.cod[0], cod2.cod[0]);
  ASSERT_EQ(cod1.cod[1], cod2.cod[1]);
  ASSERT_EQ(cod1.cod[2], cod2.cod[2]);
}

TEST(ClassOfDeviceTest, IsValid) {
  EXPECT_FALSE(ClassOfDevice::IsValid(""));
  EXPECT_FALSE(ClassOfDevice::IsValid("000000"));
  EXPECT_FALSE(ClassOfDevice::IsValid("00-00-00"));
  EXPECT_FALSE(ClassOfDevice::IsValid("000-0-0"));
  EXPECT_TRUE(ClassOfDevice::IsValid("000-0-00"));
  EXPECT_TRUE(ClassOfDevice::IsValid("ABc-d-00"));
  EXPECT_TRUE(ClassOfDevice::IsValid("aBc-D-eF"));
}

TEST(ClassOfDeviceTest, classOfDeviceFromString) {
  ClassOfDevice cod;

  EXPECT_TRUE(ClassOfDevice::FromString("000-0-00", cod));
  const ClassOfDevice result0 = {{0x00, 0x00, 0x00}};
  EXPECT_EQ(0, memcmp(&cod, &result0, sizeof(cod)));

  EXPECT_TRUE(ClassOfDevice::FromString("ab2-1-4C", cod));
  const ClassOfDevice result1 = {{0x4c, 0x21, 0xab}};
  EXPECT_EQ(0, memcmp(&cod, &result1, sizeof(cod)));
}