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/testing/fake_controller.h"
16
17 #include <pw_async/fake_dispatcher_fixture.h>
18 #include <pw_bluetooth/hci_events.emb.h>
19
20 #include <chrono>
21 #include <cstddef>
22 #include <cstdint>
23
24 #include "pw_bluetooth_sapphire/internal/host/hci-spec/protocol.h"
25 #include "pw_unit_test/framework.h"
26
27 namespace bt::testing {
28
29 using FakeControllerTest = pw::async::test::FakeDispatcherFixture;
30
TEST_F(FakeControllerTest,TestInquiryCommand)31 TEST_F(FakeControllerTest, TestInquiryCommand) {
32 FakeController controller(dispatcher());
33
34 int event_cb_count = 0;
35 controller.SetEventFunction([&event_cb_count](
36 pw::span<const std::byte> packet_bytes) {
37 auto header_view = pw::bluetooth::emboss::MakeEventHeaderView(
38 reinterpret_cast<const uint8_t*>(packet_bytes.data()),
39 packet_bytes.size());
40
41 if (header_view.event_code().Read() == hci_spec::kCommandStatusEventCode) {
42 std::unique_ptr<hci::EventPacket> event = hci::EventPacket::New(
43 packet_bytes.size() - sizeof(hci_spec::EventHeader));
44 event->mutable_view()->mutable_data().Write(
45 reinterpret_cast<const uint8_t*>(packet_bytes.data()),
46 packet_bytes.size());
47 event->InitializeFromBuffer();
48 pw::bluetooth::emboss::StatusCode status;
49 event->ToStatusCode(&status);
50 EXPECT_EQ(status, pw::bluetooth::emboss::StatusCode::SUCCESS);
51 ++event_cb_count;
52 }
53
54 else if (header_view.event_code().Read() ==
55 hci_spec::kInquiryCompleteEventCode) {
56 auto inquiry_complete_view =
57 pw::bluetooth::emboss::MakeInquiryCompleteEventView(
58 reinterpret_cast<const uint8_t*>(packet_bytes.data()),
59 packet_bytes.size());
60 EXPECT_EQ(inquiry_complete_view.status().Read(),
61 pw::bluetooth::emboss::StatusCode::SUCCESS);
62 ++event_cb_count;
63 }
64
65 else {
66 ADD_FAILURE() << "Unexpected Event packet received";
67 }
68 });
69
70 auto inquiry = hci::EmbossCommandPacket::New<
71 pw::bluetooth::emboss::InquiryCommandWriter>(hci_spec::kInquiry);
72 auto view = inquiry.view_t();
73 view.lap().Write(pw::bluetooth::emboss::InquiryAccessCode::GIAC);
74 view.inquiry_length().Write(8);
75 view.num_responses().Write(0);
76 controller.SendCommand(inquiry.data().subspan());
77
78 // The maximum amount of time before Inquiry is halted is calculated as
79 // inquiry_length * 1.28 s. FakeController:OnInquiry simulates this by posting
80 // the InquiryCompleteEvent to be returned after this duration.
81 RunFor(std::chrono::milliseconds(
82 static_cast<int64_t>(view.inquiry_length().Read()) * 1280));
83
84 EXPECT_EQ(event_cb_count, 2);
85 }
86
87 } // namespace bt::testing
88