• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2024 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 #pragma once
16 
17 #include <lib/async/dispatcher.h>
18 #include <pw_assert/check.h>
19 
20 #include <cstdint>
21 
22 #include "pw_bluetooth_sapphire/fuchsia/host/fidl/fake_hci_transport_server.h"
23 
24 namespace bt::fidl::testing {
25 
26 class FakeVendorServer final
27     : public ::fidl::Server<fuchsia_hardware_bluetooth::Vendor> {
28  public:
FakeVendorServer(::fidl::ServerEnd<fuchsia_hardware_bluetooth::Vendor> server_end,async_dispatcher_t * dispatcher)29   FakeVendorServer(
30       ::fidl::ServerEnd<fuchsia_hardware_bluetooth::Vendor> server_end,
31       async_dispatcher_t* dispatcher)
32       : binding_(::fidl::BindServer(dispatcher, std::move(server_end), this)),
33         dispatcher_(dispatcher) {}
34 
Unbind()35   void Unbind() { binding_.Unbind(); }
36 
hci_server()37   fidl::testing::FakeHciTransportServer* hci_server() {
38     return &fake_hci_server_.value();
39   }
40 
set_open_hci_error(bool val)41   void set_open_hci_error(bool val) { open_hci_error_ = val; }
42 
43  private:
GetFeatures(GetFeaturesCompleter::Sync & completer)44   void GetFeatures(GetFeaturesCompleter::Sync& completer) override {
45     fuchsia_hardware_bluetooth::VendorFeatures features;
46     features.acl_priority_command(true);
47     completer.Reply(features);
48   }
49 
EncodeCommand(EncodeCommandRequest & request,EncodeCommandCompleter::Sync & completer)50   void EncodeCommand(EncodeCommandRequest& request,
51                      EncodeCommandCompleter::Sync& completer) override {
52     PW_CHECK(request.set_acl_priority()->priority().has_value());
53     PW_CHECK(request.set_acl_priority()->direction().has_value());
54     std::vector<uint8_t> tmp{static_cast<unsigned char>(
55         WhichSetAclPriority(request.set_acl_priority()->priority().value(),
56                             request.set_acl_priority()->direction().value()))};
57     completer.Reply(fit::success(tmp));
58   }
59 
60   // Not supported
OpenHci(OpenHciCompleter::Sync & completer)61   void OpenHci(OpenHciCompleter::Sync& completer) override {
62     PW_CRASH("OpenHci not supported");
63   }
64 
OpenSnoop(OpenSnoopCompleter::Sync & completer)65   void OpenSnoop(OpenSnoopCompleter::Sync& completer) override {
66     PW_CRASH("OpenSnoop not supported");
67   }
68 
OpenHciTransport(OpenHciTransportCompleter::Sync & completer)69   void OpenHciTransport(OpenHciTransportCompleter::Sync& completer) override {
70     if (open_hci_error_) {
71       completer.Reply(fit::error(ZX_ERR_INTERNAL));
72       return;
73     }
74 
75     auto [hci_client_end, hci_server_end] =
76         ::fidl::Endpoints<fuchsia_hardware_bluetooth::HciTransport>::Create();
77 
78     fake_hci_server_.emplace(std::move(hci_server_end), dispatcher_);
79     completer.Reply(fit::success(std::move(hci_client_end)));
80   }
81 
handle_unknown_method(::fidl::UnknownMethodMetadata<fuchsia_hardware_bluetooth::Vendor> metadata,::fidl::UnknownMethodCompleter::Sync & completer)82   void handle_unknown_method(
83       ::fidl::UnknownMethodMetadata<fuchsia_hardware_bluetooth::Vendor>
84           metadata,
85       ::fidl::UnknownMethodCompleter::Sync& completer) override {
86     // Not implemented
87   }
88 
WhichSetAclPriority(fuchsia_hardware_bluetooth::VendorAclPriority priority,fuchsia_hardware_bluetooth::VendorAclDirection direction)89   uint8_t WhichSetAclPriority(
90       fuchsia_hardware_bluetooth::VendorAclPriority priority,
91       fuchsia_hardware_bluetooth::VendorAclDirection direction) {
92     if (priority == fuchsia_hardware_bluetooth::VendorAclPriority::kHigh) {
93       if (direction ==
94           fuchsia_hardware_bluetooth::VendorAclDirection::kSource) {
95         return static_cast<uint8_t>(pw::bluetooth::AclPriority::kSource);
96       }
97       return static_cast<uint8_t>(pw::bluetooth::AclPriority::kSink);
98     }
99     return static_cast<uint8_t>(pw::bluetooth::AclPriority::kNormal);
100   }
101 
102   // Flag for testing. |OpenHci()| returns an error when set to true
103   bool open_hci_error_ = false;
104 
105   std::optional<fidl::testing::FakeHciTransportServer> fake_hci_server_;
106 
107   ::fidl::ServerBindingRef<fuchsia_hardware_bluetooth::Vendor> binding_;
108 
109   async_dispatcher_t* dispatcher_;
110 };
111 
112 }  // namespace bt::fidl::testing
113