xref: /aosp_15_r20/external/pigweed/pw_bluetooth_sapphire/host/testing/fake_sdp_server.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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_sdp_server.h"
16 
17 #include "pw_bluetooth_sapphire/internal/host/testing/fake_l2cap.h"
18 
19 namespace bt::testing {
20 
FakeSdpServer(pw::async::Dispatcher & pw_dispatcher)21 FakeSdpServer::FakeSdpServer(pw::async::Dispatcher& pw_dispatcher)
22     : l2cap_(std::make_unique<l2cap::testing::FakeL2cap>(pw_dispatcher)),
23       server_(l2cap_.get()) {}
24 
RegisterWithL2cap(FakeL2cap * l2cap)25 void FakeSdpServer::RegisterWithL2cap(FakeL2cap* l2cap) {
26   auto channel_cb = [this](FakeDynamicChannel::WeakPtr channel) {
27     auto handle_sdu = [this, channel](auto& request) {
28       if (channel.is_alive()) {
29         HandleSdu(channel, request);
30       }
31     };
32     channel->set_packet_handler_callback(handle_sdu);
33   };
34   l2cap->RegisterService(l2cap::kSDP, channel_cb);
35 }
36 
HandleSdu(FakeDynamicChannel::WeakPtr channel,const ByteBuffer & sdu)37 void FakeSdpServer::HandleSdu(FakeDynamicChannel::WeakPtr channel,
38                               const ByteBuffer& sdu) {
39   PW_CHECK(channel->opened());
40   auto response = server()->HandleRequest(
41       std::make_unique<DynamicByteBuffer>(sdu), l2cap::kDefaultMTU);
42   if (response) {
43     auto& callback = channel->send_packet_callback();
44     return callback(*response.value());
45   }
46 }
47 
48 }  // namespace bt::testing
49