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

Commit 6ccf2639 authored by Henri Chataing's avatar Henri Chataing
Browse files

RootCanal: Remove custom PDL field for class of device

The custom field type is not actually used in RootCanal;
was just inherited from the Gd definitions.

Bug: 279465240
Test: m root-canal
Change-Id: I3d1b78e55d72ccff8cc1543ab8771b598f508db5
parent c74eee81
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -67,7 +67,6 @@ filegroup {
    srcs: [
        "lib/crypto/crypto.cc",
        "lib/hci/address.cc",
        "lib/hci/class_of_device.cc",
        "lib/hci/pcap_filter.cc",
        "lib/log.cc",
    ],
+0 −1
Original line number Diff line number Diff line
@@ -251,7 +251,6 @@ android_add_library(
      ${RootCanalGeneratedPackets_h}
      lib/crypto/crypto.cc
      lib/hci/address.cc
      lib/hci/class_of_device.cc
      lib/hci/pcap_filter.cc
      lib/log.cc
      model/controller/acl_connection.cc
+0 −73
Original line number Diff line number Diff line
/******************************************************************************
 *
 *  Copyright 2022 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 <array>
#include <optional>
#include <string>

#include "packet/custom_field_fixed_size_interface.h"

namespace bluetooth {
namespace hci {

class ClassOfDevice final
    : public packet::CustomFieldFixedSizeInterface<ClassOfDevice> {
 public:
  static constexpr size_t kLength = 3;

  std::array<uint8_t, kLength> cod = {};

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

  // packet::CustomFieldFixedSizeInterface methods
  inline uint8_t* data() override { return cod.data(); }
  inline const uint8_t* data() const override { return cod.data(); }

  uint32_t ToUint32() const;
  std::string ToString() const;
  static std::optional<ClassOfDevice> FromString(const std::string& str);

  bool operator<(const ClassOfDevice& rhs) const { return cod < rhs.cod; }
  bool operator==(const ClassOfDevice& rhs) const { return cod == rhs.cod; }
  bool operator>(const ClassOfDevice& rhs) const { return (rhs < *this); }
  bool operator<=(const ClassOfDevice& rhs) const { return !(*this > rhs); }
  bool operator>=(const ClassOfDevice& rhs) const { return !(*this < rhs); }
  bool operator!=(const ClassOfDevice& rhs) const { return !(*this == rhs); }

  // 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;
}

}  // namespace hci
}  // namespace bluetooth
+0 −112
Original line number Diff line number Diff line
/******************************************************************************
 *
 *  Copyright 2022 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 "hci/class_of_device.h"

#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <sstream>
#include <vector>

#include "log.h"

namespace bluetooth {
namespace hci {

// ClassOfDevice cannot initialize member variables as it is a POD type
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
ClassOfDevice::ClassOfDevice(const uint8_t (&class_of_device)[kLength]) {
  std::copy(class_of_device, class_of_device + kLength, cod.data());
}

uint32_t ClassOfDevice::ToUint32() const {
  return (cod[2]) | (cod[1] << 8) | (cod[0] << 16);
}

std::string ClassOfDevice::ToString() const {
  char buffer[] = "000-0-00";
  std::snprintf(&buffer[0], sizeof(buffer), "%03x-%01x-%02x",
                (static_cast<uint16_t>(cod[2]) << 4) | cod[1] >> 4,
                cod[1] & 0x0f, cod[0]);
  std::string str(buffer);
  return str;
}

std::optional<ClassOfDevice> ClassOfDevice::FromString(const std::string& str) {
  if (str.length() != 8) {
    return std::nullopt;
  }

  std::istringstream stream(str);
  std::string token;
  int index = 0;
  uint16_t values[3];

  ClassOfDevice new_cod{};
  while (getline(stream, token, '-')) {
    if (index >= 3) {
      return std::nullopt;
    }

    if (index == 0 && token.length() != 3) {
      return std::nullopt;
    }
    if (index == 1 && token.length() != 1) {
      return std::nullopt;
    }
    if (index == 2 && token.length() != 2) {
      return std::nullopt;
    }
    char* temp = nullptr;
    values[index] = std::strtol(token.c_str(), &temp, 16);
    if (*temp != '\0') {
      return std::nullopt;
    }

    index++;
  }

  if (index != 3) {
    return std::nullopt;
  }

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

  return new_cod;
}

bool ClassOfDevice::FromString(const std::string& from, ClassOfDevice& to) {
  auto new_cod = FromString(from);
  if (!new_cod) {
    to = {};
    return false;
  }
  to = std::move(*new_cod);
  return true;
}

bool ClassOfDevice::IsValid(const std::string& class_of_device) {
  return ClassOfDevice::FromString(class_of_device).has_value();
}

}  // namespace hci
}  // namespace bluetooth
+1 −2
Original line number Diff line number Diff line
@@ -1200,8 +1200,7 @@ void DualModeController::WriteClassOfDevice(CommandView command) {
  ASSERT(command_view.IsValid());

  DEBUG(id_, "<< Write Class of Device");
  DEBUG(id_, "   class_of_device={}",
        command_view.GetClassOfDevice().ToString());
  DEBUG(id_, "   class_of_device={}", command_view.GetClassOfDevice());

  link_layer_controller_.SetClassOfDevice(command_view.GetClassOfDevice());
  send_event_(bluetooth::hci::WriteClassOfDeviceCompleteBuilder::Create(
Loading