1 // Copyright 2024 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 17 #include <optional> 18 19 #include "pw_function/function.h" 20 #include "pw_multibuf/multibuf.h" 21 #include "pw_status/status.h" 22 23 namespace pw::bluetooth::proxy { 24 25 /// Events returned from all client-facing channel objects in their `event_fn` 26 /// callback. 27 enum class L2capChannelEvent { 28 /// The channel was closed by something other than `ProxyHost` or due to 29 /// `ProxyHost` shutdown. The channel is now `State::kClosed` and should be 30 /// cleaned up. See logs for details. 31 kChannelClosedByOther, 32 /// An invalid packet was received. The channel is now `State::kStopped` and 33 /// should be closed. See error logs for details. 34 kRxInvalid, 35 /// During Rx, the channel ran out of memory. The channel is now 36 /// `State::kStopped` and should be closed. 37 kRxOutOfMemory, 38 /// The channel has received a packet while in the `State::kStopped` state. 39 /// The channel should have been closed. 40 kRxWhileStopped, 41 /// `ProxyHost` has been reset. As a result, the channel is now 42 /// `State::kClosed`. (All channels are `State::kClosed` on a reset.) 43 kReset, 44 /// Write space is now available after a previous Write on this channel 45 /// returned UNAVAILABLE. 46 kWriteAvailable, 47 }; 48 49 /// Event callback from channels. 50 using ChannelEventCallback = pw::InlineFunction< 51 void(L2capChannelEvent event), 52 // Set size to at least two words so we can accept lambdas 53 // that have two pointers in their capture (e.g. callee and a 54 // pointer argument). If platform has defined an even larger 55 // PW_FUNCTION_INLINE_CALLABLE_SIZE use that. 56 std::max(sizeof(void*) * 2, PW_FUNCTION_INLINE_CALLABLE_SIZE)>; 57 58 /// Result object with status and optional MultiBuf that is only present if the 59 /// status is NOT `ok()`. 60 // `pw::Result` can't be used because it only has a value for `ok()` status. 61 // `std::expected` can't be used because it only has a value OR a status. 62 struct StatusWithMultiBuf { 63 pw::Status status; 64 std::optional<pw::multibuf::MultiBuf> buf = std::nullopt; 65 }; 66 67 /// Alias for a client provided callback function for that can receive data from 68 /// a channel and optionally own the handling that data. 69 /// 70 /// @param[in] payload The payload being passed to the client. 71 /// 72 /// 73 /// @returns If the client will own handling the payload then std::nullopt 74 /// should be returned. If the client will not own handling the payload then the 75 /// payload MultiBuf should be returned (unaltered). 76 using OptionalPayloadReceiveCallback = 77 Function<std::optional<multibuf::MultiBuf>(multibuf::MultiBuf&& payload)>; 78 79 } // namespace pw::bluetooth::proxy 80