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/transport/control_packets.h"
16
17 #include "pw_unit_test/framework.h"
18
19 namespace bt::hci {
20
21 const size_t BITS_IN_BYTE = 8;
22
TEST(EmbossControlPackets,TooSmallCommandCompleteEventWhenReadingStatus)23 TEST(EmbossControlPackets, TooSmallCommandCompleteEventWhenReadingStatus) {
24 StaticByteBuffer buffer(
25 hci_spec::kCommandCompleteEventCode,
26 0x03, // size
27 0x01, // num hci packets
28 0x02,
29 0x03 // command opcode
30 // There should be a status field here, but there isn't
31 );
32 EventPacket packet = EventPacket::New(buffer.size());
33 packet.mutable_data().Write(buffer);
34 EXPECT_FALSE(packet.StatusCode().has_value());
35 }
36
TEST(EmbossControlPackets,CommandCompleteEventWithStatus)37 TEST(EmbossControlPackets, CommandCompleteEventWithStatus) {
38 StaticByteBuffer buffer(hci_spec::kCommandCompleteEventCode,
39 0x04, // size
40 0x01, // num hci packets
41 0x02,
42 0x03, // command opcode
43 0x00 // status (success)
44 );
45 EventPacket packet = EventPacket::New(buffer.size());
46 packet.mutable_data().Write(buffer);
47 ASSERT_TRUE(packet.StatusCode().has_value());
48 EXPECT_EQ(packet.StatusCode().value(),
49 pw::bluetooth::emboss::StatusCode::SUCCESS);
50 }
51
TEST(EmbossControlPackets,ArrayFieldWithVariableLengthElements)52 TEST(EmbossControlPackets, ArrayFieldWithVariableLengthElements) {
53 // Size of `data` arrays in the two Advertising Reports.
54 constexpr size_t first_report_data_size = 3, second_report_data_size = 2;
55 // Size of `reports` array in the Advertising Report Subevent.
56 size_t reports_size =
57 first_report_data_size + second_report_data_size +
58 2ul * pw::bluetooth::emboss::LEExtendedAdvertisingReportData::
59 MinSizeInBytes();
60 // Size of LEMetaEvent containing the Advertising Report Subevent.
61 size_t packet_size =
62 reports_size + pw::bluetooth::emboss::
63 LEExtendedAdvertisingReportSubevent::MinSizeInBytes();
64
65 auto packet = EventPacket::New<
66 pw::bluetooth::emboss::LEExtendedAdvertisingReportSubeventWriter>(
67 hci_spec::kLEMetaEventCode, packet_size);
68 auto view = packet.view_t();
69 view.num_reports().Write(2);
70 ASSERT_TRUE(view.Ok());
71 EXPECT_EQ(view.reports().SizeInBytes(), reports_size);
72
73 // Create view over the first Report in the `reports` array of the subevent.
74 auto first_report_view =
75 pw::bluetooth::emboss::MakeLEExtendedAdvertisingReportDataView(
76 view.reports().BackingStorage().data(),
77 pw::bluetooth::emboss::LEExtendedAdvertisingReportData::
78 MinSizeInBytes() +
79 first_report_data_size);
80 first_report_view.data_length().Write(
81 static_cast<int32_t>(first_report_data_size));
82 first_report_view.data()
83 .BackingStorage()
84 .WriteBigEndianUInt<first_report_data_size * BITS_IN_BYTE>(0x123456);
85 ASSERT_TRUE(first_report_view.Ok());
86 EXPECT_EQ(first_report_view.data().SizeInBytes(), first_report_data_size);
87 EXPECT_EQ(first_report_view.data()[0].Read(), 0x12);
88 EXPECT_EQ(first_report_view.data()[1].Read(), 0x34);
89 EXPECT_EQ(first_report_view.data()[2].Read(), 0x56);
90
91 // Create view over the second Report in the `reports` array of the subevent.
92 auto second_report_view =
93 pw::bluetooth::emboss::MakeLEExtendedAdvertisingReportDataView(
94 view.reports().BackingStorage().data() + first_report_data_size,
95 pw::bluetooth::emboss::LEExtendedAdvertisingReportData::
96 MinSizeInBytes() +
97 second_report_data_size);
98 second_report_view.data_length().Write(
99 static_cast<int32_t>(second_report_data_size));
100 second_report_view.data()
101 .BackingStorage()
102 .WriteBigEndianUInt<second_report_data_size * BITS_IN_BYTE>(0x1234);
103 ASSERT_TRUE(second_report_view.Ok());
104 EXPECT_EQ(second_report_view.data().SizeInBytes(), second_report_data_size);
105 EXPECT_EQ(second_report_view.data()[0].Read(), 0x12);
106 EXPECT_EQ(second_report_view.data()[1].Read(), 0x34);
107 }
108
109 } // namespace bt::hci
110