• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2023 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #include "pw_bluetooth_sapphire/internal/host/hci-spec/util.h"
16 
17 #include "pw_unit_test/framework.h"
18 
19 namespace bt::hci_spec {
20 namespace {
21 
22 // Bit values used in this test are given in a table in Core Spec Volume 4, Part
23 // E, Section 7.8.53.
TEST(UtilTest,AdvertisingTypeToEventBits)24 TEST(UtilTest, AdvertisingTypeToEventBits) {
25   std::optional<hci_spec::AdvertisingEventBits> bits =
26       AdvertisingTypeToEventBits(pw::bluetooth::emboss::LEAdvertisingType::
27                                      CONNECTABLE_AND_SCANNABLE_UNDIRECTED);
28   ASSERT_TRUE(bits);
29   EXPECT_EQ(0b00010011, bits.value());
30 
31   bits = AdvertisingTypeToEventBits(pw::bluetooth::emboss::LEAdvertisingType::
32                                         CONNECTABLE_LOW_DUTY_CYCLE_DIRECTED);
33   ASSERT_TRUE(bits);
34   EXPECT_EQ(0b00010101, bits.value());
35 
36   bits = AdvertisingTypeToEventBits(pw::bluetooth::emboss::LEAdvertisingType::
37                                         CONNECTABLE_HIGH_DUTY_CYCLE_DIRECTED);
38   ASSERT_TRUE(bits);
39   EXPECT_EQ(0b00011101, bits.value());
40 
41   bits = AdvertisingTypeToEventBits(
42       pw::bluetooth::emboss::LEAdvertisingType::SCANNABLE_UNDIRECTED);
43   ASSERT_TRUE(bits);
44   EXPECT_EQ(0b00010010, bits.value());
45 
46   bits = AdvertisingTypeToEventBits(
47       pw::bluetooth::emboss::LEAdvertisingType::NOT_CONNECTABLE_UNDIRECTED);
48   ASSERT_TRUE(bits);
49   EXPECT_EQ(0b00010000, bits.value());
50 }
51 
TEST(UtilTest,LinkKeyTypeToString)52 TEST(UtilTest, LinkKeyTypeToString) {
53   std::string link_key_type =
54       LinkKeyTypeToString(hci_spec::LinkKeyType::kCombination);
55   EXPECT_EQ("kCombination", link_key_type);
56 
57   link_key_type = LinkKeyTypeToString(hci_spec::LinkKeyType::kLocalUnit);
58   EXPECT_EQ("kLocalUnit", link_key_type);
59 
60   link_key_type = LinkKeyTypeToString(hci_spec::LinkKeyType::kRemoteUnit);
61   EXPECT_EQ("kRemoteUnit", link_key_type);
62 
63   link_key_type = LinkKeyTypeToString(hci_spec::LinkKeyType::kDebugCombination);
64   EXPECT_EQ("kDebugCombination", link_key_type);
65 
66   link_key_type = LinkKeyTypeToString(
67       hci_spec::LinkKeyType::kUnauthenticatedCombination192);
68   EXPECT_EQ("kUnauthenticatedCombination192", link_key_type);
69 
70   link_key_type =
71       LinkKeyTypeToString(hci_spec::LinkKeyType::kAuthenticatedCombination192);
72   EXPECT_EQ("kAuthenticatedCombination192", link_key_type);
73 
74   link_key_type =
75       LinkKeyTypeToString(hci_spec::LinkKeyType::kChangedCombination);
76   EXPECT_EQ("kChangedCombination", link_key_type);
77 
78   link_key_type = LinkKeyTypeToString(
79       hci_spec::LinkKeyType::kUnauthenticatedCombination256);
80   EXPECT_EQ("kUnauthenticatedCombination256", link_key_type);
81 
82   link_key_type =
83       LinkKeyTypeToString(hci_spec::LinkKeyType::kAuthenticatedCombination256);
84   EXPECT_EQ("kAuthenticatedCombination256", link_key_type);
85 
86   // Unknown link key type
87   link_key_type = LinkKeyTypeToString(static_cast<hci_spec::LinkKeyType>(0xFF));
88   EXPECT_EQ("(Unknown)", link_key_type);
89 }
90 
91 }  // namespace
92 }  // namespace bt::hci_spec
93