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