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

Commit 3802152e authored by Chris Manton's avatar Chris Manton
Browse files

Pretty print class of device dev_class_text

Bug: 281420281
Test: net_test_stack_btm64

Change-Id: Ie33d8b1e37bca29dc26a2049e5dfebe79e9bce39
parent 3d376cf6
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -1106,6 +1106,7 @@ cc_test {
        "test/btm/sco_hci_test.cc",
        "test/btm/stack_btm_test.cc",
        "test/common/mock_eatt.cc",
        "test/stack_include_test.cc",
    ],
    static_libs: [
        "libbt-common",
+22 −7
Original line number Diff line number Diff line
@@ -96,11 +96,17 @@ inline constexpr DEV_CLASS kDevClassEmpty = {};
#define BTM_COD_SERVICE_TELEPHONY 0x4000
#define BTM_COD_SERVICE_INFORMATION 0x8000

/* the COD masks */
#define BTM_COD_MINOR_CLASS_MASK 0xFC
#define BTM_COD_MAJOR_CLASS_MASK 0x1F
#define BTM_COD_SERVICE_CLASS_LO_B 0x00E0
#define BTM_COD_SERVICE_CLASS_MASK 0xFFE0

/* class of device field macros */
#define BTM_COD_MINOR_CLASS(u8, pd) \
  { (u8) = (pd)[2] & 0xFC; }
  { (u8) = (pd)[2] & BTM_COD_MINOR_CLASS_MASK; }
#define BTM_COD_MAJOR_CLASS(u8, pd) \
  { (u8) = (pd)[1] & 0x1F; }
  { (u8) = (pd)[1] & BTM_COD_MAJOR_CLASS_MASK; }
#define BTM_COD_SERVICE_CLASS(u16, pd) \
  {                                    \
    (u16) = (pd)[0];                   \
@@ -116,11 +122,20 @@ inline constexpr DEV_CLASS kDevClassEmpty = {};
    (pd)[0] = (sv) >> 8;                                \
  }

/* the COD masks */
#define BTM_COD_MINOR_CLASS_MASK 0xFC
#define BTM_COD_MAJOR_CLASS_MASK 0x1F
#define BTM_COD_SERVICE_CLASS_LO_B 0x00E0
#define BTM_COD_SERVICE_CLASS_MASK 0xFFE0
#ifdef __cplusplus
#include <sstream>
inline std::string dev_class_text(const DEV_CLASS& dev_class) {
  std::ostringstream oss;
  uint8_t mj, mn;
  uint16_t sv;
  BTM_COD_MINOR_CLASS(mn, dev_class);
  BTM_COD_MAJOR_CLASS(mj, dev_class);
  BTM_COD_SERVICE_CLASS(sv, dev_class);
  oss << std::to_string(mj) << "-" << std::to_string(mn) << "-"
      << std::to_string(sv);
  return oss.str();
}
#endif  // __cplusplus

#define DEVCLASS_TO_STREAM(p, a)                      \
  {                                                   \
+64 −0
Original line number Diff line number Diff line
/*
 * Copyright 2023 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 "os/log.h"
#include "stack/include/bt_dev_class.h"

class StackIncludeTest : public ::testing::Test {
 protected:
  void SetUp() override {}

  void TearDown() override {}
};

TEST_F(StackIncludeTest, dev_class_simple_zeros) {
  int mn = 0;
  int mj = 0;
  int sv = 0;
  DEV_CLASS dev_class{0xff};
  FIELDS_TO_COD(dev_class, mn, mj, sv);
  BTM_COD_MINOR_CLASS(mn, dev_class);
  BTM_COD_MAJOR_CLASS(mj, dev_class);
  BTM_COD_SERVICE_CLASS(sv, dev_class);
  ASSERT_EQ(0, mn);
  ASSERT_EQ(0, mj);
  ASSERT_EQ(0, sv);
}

TEST_F(StackIncludeTest, dev_class_simple_ones) {
  int mn = 0xff;
  int mj = 0xff;
  int sv = 0xffff;
  DEV_CLASS dev_class{0x00};
  FIELDS_TO_COD(dev_class, mn, mj, sv);
  BTM_COD_MINOR_CLASS(mn, dev_class);
  BTM_COD_MAJOR_CLASS(mj, dev_class);
  BTM_COD_SERVICE_CLASS(sv, dev_class);
  ASSERT_EQ(252, mn & BTM_COD_MINOR_CLASS_MASK);
  ASSERT_EQ(31, mj & BTM_COD_MAJOR_CLASS_MASK);
  ASSERT_EQ(65472, sv);
}

TEST_F(StackIncludeTest, dev_class_text) {
  int mn = 0xff;
  int mj = 0xff;
  int sv = 0xffff;
  DEV_CLASS dev_class;
  FIELDS_TO_COD(dev_class, mn, mj, sv);
  ASSERT_STREQ("31-252-65472", dev_class_text(dev_class).c_str());
}