1 // Copyright 2021 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 #pragma once 15 16 #include <bit> 17 18 #include "pw_bluetooth_hci/packet.h" 19 #include "pw_bytes/span.h" 20 #include "pw_function/function.h" 21 #include "pw_status/status_with_size.h" 22 23 namespace pw::bluetooth_hci { 24 25 // The HCI UART Transport Layer supports uses the following packet indicator 26 // bytes to decode the HCI packet type as defined by the Bluetooth Core 27 // Specification version 5.3 "Host Controller Interface Transport Layer" volume 28 // 4, part A: 29 constexpr inline std::byte kUartCommandPacketIndicator = std::byte{0x01}; 30 constexpr inline std::byte kUartAsyncDataPacketIndicator = std::byte{0x02}; 31 constexpr inline std::byte kUartSyncDataPacketIndicator = std::byte{0x03}; 32 constexpr inline std::byte kUartEventPacketIndicator = std::byte{0x04}; 33 34 // The HCI UART Transport Layer may be invoked with the following packet types, 35 // as defined by Bluetooth Core Specification version 5.3 "Host Controller 36 // Interface Transport Layer" volume 4, part A: 37 // PacketType::kCommandPacket 38 // PacketType::kAsyncDataPacket 39 // PacketType::kSyncDataPacket 40 // PacketType::kEventPacket 41 using DecodedPacketCallback = Function<void(const Packet& packet)>; 42 43 // Parses the HCI Packets out of a HCI UART Transport Layer buffer. 44 // 45 // Parses as many complete HCI packets out of the provided buffer based on the 46 // HCI UART Transport Layer as defined by Bluetooth Core Specification version 47 // 5.3 "Host Controller Interface Transport Layer" volume 4, part A. 48 // 49 // The HciPacketCallback is invoked for each full HCI packet. 50 // 51 // Returns the number of bytes processed and a status based on: 52 // OK - No invalid packet indicator found. 53 // DATA_LOSS - An invalid packet indicator was detected between packets. 54 // Synchronization has been lost. The caller is responsible for 55 // regaining synchronization 56 // 57 // Note: The caller is responsible for detecting the lack of progress due to 58 // an undersized data buffer and/or an invalid length field in case a full 59 // buffer is passed and no bytes are processed. 60 StatusWithSize DecodeHciUartData(ConstByteSpan data, 61 const DecodedPacketCallback& packet_callback); 62 63 } // namespace pw::bluetooth_hci 64