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/sco_data_packet.h"
16
17 #include "pw_unit_test/framework.h"
18
19 namespace bt::hci {
20 namespace {
21
TEST(ScoPacketTest,NewWithConnectionHandle)22 TEST(ScoPacketTest, NewWithConnectionHandle) {
23 const hci_spec::ConnectionHandle handle = 0x000F;
24 const std::unique_ptr<const ScoDataPacket> packet =
25 ScoDataPacket::New(/*connection_handle=*/handle, /*payload_size=*/1);
26 ASSERT_TRUE(packet);
27 EXPECT_EQ(packet->connection_handle(), handle);
28 EXPECT_EQ(packet->packet_status_flag(),
29 hci_spec::SynchronousDataPacketStatusFlag::kCorrectlyReceived);
30 }
31
TEST(ScoPacketTest,ReadFromBufferWithStatusFlag)32 TEST(ScoPacketTest, ReadFromBufferWithStatusFlag) {
33 StaticByteBuffer bytes(0x02, // handle
34 0x00, // status flag: correctly received data
35 0x01, // data total length
36 0x09 // payload
37 );
38 std::unique_ptr<ScoDataPacket> packet =
39 ScoDataPacket::New(/*payload_size=*/1);
40 ASSERT_TRUE(packet);
41 packet->mutable_view()->mutable_data().Write(bytes);
42 packet->InitializeFromBuffer();
43 EXPECT_EQ(packet->connection_handle(), 0x0002);
44 EXPECT_EQ(packet->packet_status_flag(),
45 hci_spec::SynchronousDataPacketStatusFlag::kCorrectlyReceived);
46 EXPECT_EQ(packet->view().payload_size(), 1u);
47
48 // Set packet status byte to kPossiblyInvalid
49 packet->mutable_view()->mutable_data()[1] = 0b0001'0000;
50 EXPECT_EQ(packet->packet_status_flag(),
51 hci_spec::SynchronousDataPacketStatusFlag::kPossiblyInvalid);
52
53 // Set packet status byte to kNoDataReceived
54 packet->mutable_view()->mutable_data()[1] = 0b0010'0000;
55 EXPECT_EQ(packet->packet_status_flag(),
56 hci_spec::SynchronousDataPacketStatusFlag::kNoDataReceived);
57
58 // Set packet status byte to kDataPartiallyLost
59 packet->mutable_view()->mutable_data()[1] = 0b0011'0000;
60 EXPECT_EQ(packet->packet_status_flag(),
61 hci_spec::SynchronousDataPacketStatusFlag::kDataPartiallyLost);
62 }
63
64 } // namespace
65 } // namespace bt::hci
66