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 "pw_bluetooth_sapphire/internal/host/common/byte_buffer.h" 17 #include "pw_bluetooth_sapphire/internal/host/l2cap/command_handler.h" 18 #include "pw_bluetooth_sapphire/internal/host/l2cap/l2cap_defs.h" 19 20 namespace bt::l2cap::internal { 21 class LowEnergyCommandHandler final : public CommandHandler { 22 public: 23 class ConnectionParameterUpdateResponse final : public Response { 24 public: 25 using PayloadT = ConnectionParameterUpdateResponsePayload; 26 static constexpr const char* kName = "Connection Parameter Update Response"; 27 28 using Response::Response; // Inherit ctor 29 bool Decode(const ByteBuffer& payload_buf); 30 result()31 ConnectionParameterUpdateResult result() const { return result_; } 32 33 private: 34 friend class LowEnergyCommandHandler; 35 36 ConnectionParameterUpdateResult result_; 37 }; 38 39 class ConnectionParameterUpdateResponder final : public Responder { 40 public: 41 explicit ConnectionParameterUpdateResponder( 42 SignalingChannel::Responder* sig_responder); 43 44 void Send(ConnectionParameterUpdateResult result); 45 }; 46 47 // |sig| must be valid for the lifetime of this object. 48 // |command_failed_callback| is called if an outbound request timed out with 49 // RTX or ERTX timers after retransmission (if configured). The call may come 50 // after the lifetime of this object. 51 explicit LowEnergyCommandHandler( 52 SignalingChannelInterface* sig, 53 fit::closure request_fail_callback = nullptr); 54 ~LowEnergyCommandHandler() = default; 55 BT_DISALLOW_COPY_AND_ASSIGN_ALLOW_MOVE(LowEnergyCommandHandler); 56 57 // Outbound request sending methods. Response callbacks are required to be 58 // non-empty. The callbacks are wrapped and moved into the SignalingChannel 59 // and may outlive LowEnergyCommandHandler. 60 61 using ConnectionParameterUpdateResponseCallback = 62 fit::function<void(const ConnectionParameterUpdateResponse& rsp)>; 63 bool SendConnectionParameterUpdateRequest( 64 uint16_t interval_min, 65 uint16_t interval_max, 66 uint16_t peripheral_latency, 67 uint16_t timeout_multiplier, 68 ConnectionParameterUpdateResponseCallback cb); 69 70 // Inbound request delegate registration methods. The callbacks are wrapped 71 // and moved into the SignalingChannel and may outlive 72 // LowEnergyCommandHandler. It is expected that any request delegates 73 // registered will span the lifetime of its signaling channel and hence link, 74 // so no unregistration is provided. However each call to register will 75 // replace any currently registered request delegate. 76 77 using ConnectionParameterUpdateRequestCallback = 78 fit::function<void(uint16_t interval_min, 79 uint16_t interval_max, 80 uint16_t peripheral_latency, 81 uint16_t timeout_multiplier, 82 ConnectionParameterUpdateResponder* responder)>; 83 void ServeConnectionParameterUpdateRequest( 84 ConnectionParameterUpdateRequestCallback cb); 85 }; 86 } // namespace bt::l2cap::internal 87