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(
36 [&event_cb_count](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_uint().Read() ==
42 hci_spec::kCommandStatusEventCode) {
43 auto command_status_view =
44 pw::bluetooth::emboss::MakeCommandStatusEventView(
45 reinterpret_cast<const uint8_t*>(packet_bytes.data()),
46 packet_bytes.size());
47 EXPECT_EQ(command_status_view.status().Read(),
48 pw::bluetooth::emboss::StatusCode::SUCCESS);
49 ++event_cb_count;
50 } else if (header_view.event_code_uint().Read() ==
51 hci_spec::kInquiryCompleteEventCode) {
52 auto inquiry_complete_view =
53 pw::bluetooth::emboss::MakeInquiryCompleteEventView(
54 reinterpret_cast<const uint8_t*>(packet_bytes.data()),
55 packet_bytes.size());
56 EXPECT_EQ(inquiry_complete_view.status().Read(),
57 pw::bluetooth::emboss::StatusCode::SUCCESS);
58 ++event_cb_count;
59 }
60
61 else {
62 ADD_FAILURE() << "Unexpected Event packet received";
63 }
64 });
65
66 auto inquiry =
67 hci::CommandPacket::New<pw::bluetooth::emboss::InquiryCommandWriter>(
68 hci_spec::kInquiry);
69 auto view = inquiry.view_t();
70 view.lap().Write(pw::bluetooth::emboss::InquiryAccessCode::GIAC);
71 view.inquiry_length().Write(8);
72 view.num_responses().Write(0);
73 controller.SendCommand(inquiry.data().subspan());
74
75 // The maximum amount of time before Inquiry is halted is calculated as
76 // inquiry_length * 1.28 s. FakeController:OnInquiry simulates this by posting
77 // the InquiryCompleteEvent to be returned after this duration.
78 RunFor(std::chrono::milliseconds(
79 static_cast<int64_t>(view.inquiry_length().Read()) * 1280));
80
81 EXPECT_EQ(event_cb_count, 2);
82 }
83
84 } // namespace bt::testing
85