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 #pragma once 16 #include <lib/fit/function.h> 17 18 #include "pw_bluetooth_sapphire/internal/host/gatt/gatt_defs.h" 19 #include "pw_bluetooth_sapphire/internal/host/gatt/server.h" 20 21 namespace bt::gatt::testing { 22 using UpdateHandler = fit::function<void(IdType service_id, 23 IdType chrc_id, 24 BufferView value, 25 IndicationCallback indicate_cb)>; 26 27 // A mock implementation of a gatt::Server object. Can be used to mock outbound 28 // notifications/ indications without a production att::Bearer in tests. 29 class MockServer : public Server { 30 public: 31 MockServer(PeerId peer_id, LocalServiceManager::WeakPtr local_services); 32 set_update_handler(UpdateHandler handler)33 void set_update_handler(UpdateHandler handler) { 34 update_handler_ = std::move(handler); 35 } 36 37 using WeakPtr = WeakSelf<MockServer>::WeakPtr; AsMockWeakPtr()38 MockServer::WeakPtr AsMockWeakPtr() { return weak_self_.GetWeakPtr(); } 39 was_shut_down()40 bool was_shut_down() const { return was_shut_down_; } 41 42 private: 43 // Server overrides: 44 void SendUpdate(IdType service_id, 45 IdType chrc_id, 46 BufferView value, 47 IndicationCallback indicate_cb) override; ShutDown()48 void ShutDown() override { was_shut_down_ = true; } 49 50 PeerId peer_id_; 51 LocalServiceManager::WeakPtr local_services_; 52 UpdateHandler update_handler_ = nullptr; 53 bool was_shut_down_ = false; 54 WeakSelf<MockServer> weak_self_; 55 }; 56 57 } // namespace bt::gatt::testing 58