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