• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <endian.h>
18 
19 #include "pw_bluetooth_sapphire/internal/host/testing/fake_l2cap.h"
20 
21 #pragma clang diagnostic ignored "-Wshadow"
22 
23 namespace bt::testing {
24 
FakeSdpServer(pw::async::Dispatcher & pw_dispatcher)25 FakeSdpServer::FakeSdpServer(pw::async::Dispatcher& pw_dispatcher)
26     : l2cap_(std::make_unique<l2cap::testing::FakeL2cap>(pw_dispatcher)),
27       server_(l2cap_.get()) {}
28 
RegisterWithL2cap(FakeL2cap * l2cap_)29 void FakeSdpServer::RegisterWithL2cap(FakeL2cap* l2cap_) {
30   auto channel_cb = [this](FakeDynamicChannel::WeakPtr channel) {
31     auto handle_sdu = [this, channel](auto& request) {
32       if (channel.is_alive()) {
33         HandleSdu(channel, request);
34       }
35     };
36     channel->set_packet_handler_callback(handle_sdu);
37   };
38   l2cap_->RegisterService(l2cap::kSDP, channel_cb);
39 }
40 
HandleSdu(FakeDynamicChannel::WeakPtr channel,const ByteBuffer & sdu)41 void FakeSdpServer::HandleSdu(FakeDynamicChannel::WeakPtr channel,
42                               const ByteBuffer& sdu) {
43   BT_ASSERT(channel->opened());
44   auto response = server()->HandleRequest(
45       std::make_unique<DynamicByteBuffer>(sdu), l2cap::kDefaultMTU);
46   if (response) {
47     auto& callback = channel->send_packet_callback();
48     return callback(*response.value());
49   }
50 }
51 
52 }  // namespace bt::testing
53