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/common/device_address.h"
18 #include "pw_bluetooth_sapphire/internal/host/gap/gap.h"
19 #include "pw_bluetooth_sapphire/internal/host/hci-spec/constants.h"
20 #include "pw_bluetooth_sapphire/internal/host/hci-spec/protocol.h"
21 #include "pw_bluetooth_sapphire/internal/host/l2cap/a2dp_offload_manager.h"
22 #include "pw_bluetooth_sapphire/internal/host/l2cap/l2cap_defs.h"
23 #include "pw_bluetooth_sapphire/internal/host/transport/emboss_packet.h"
24 
25 namespace bt::testing {
26 
27 // This module contains functionality to create arbitrary HCI packets defining
28 // common behaviors with respect to expected devices and connections.
29 // This allows easily defining expected packets to be sent or received for
30 // given transactions such as connection establishment or discovery
31 
32 DynamicByteBuffer AcceptConnectionRequestPacket(DeviceAddress address);
33 
34 DynamicByteBuffer AuthenticationRequestedPacket(
35     hci_spec::ConnectionHandle conn);
36 
37 DynamicByteBuffer CommandCompletePacket(
38     hci_spec::OpCode opcode,
39     pw::bluetooth::emboss::StatusCode =
40         pw::bluetooth::emboss::StatusCode::SUCCESS);
41 
42 DynamicByteBuffer CommandStatusPacket(
43     hci_spec::OpCode op_code,
44     pw::bluetooth::emboss::StatusCode status_code =
45         pw::bluetooth::emboss::StatusCode::SUCCESS,
46     uint8_t num_packets = 0xF0);
47 
48 DynamicByteBuffer ConnectionCompletePacket(
49     DeviceAddress address,
50     hci_spec::ConnectionHandle conn,
51     pw::bluetooth::emboss::StatusCode status =
52         pw::bluetooth::emboss::StatusCode::SUCCESS);
53 
54 DynamicByteBuffer ConnectionRequestPacket(
55     DeviceAddress address,
56     hci_spec::LinkType link_type = hci_spec::LinkType::kACL);
57 
58 DynamicByteBuffer CreateConnectionPacket(DeviceAddress address);
59 
60 DynamicByteBuffer CreateConnectionCancelPacket(DeviceAddress address);
61 
62 DynamicByteBuffer DisconnectionCompletePacket(
63     hci_spec::ConnectionHandle conn,
64     pw::bluetooth::emboss::StatusCode reason =
65         pw::bluetooth::emboss::StatusCode::REMOTE_USER_TERMINATED_CONNECTION);
66 DynamicByteBuffer DisconnectPacket(
67     hci_spec::ConnectionHandle conn,
68     pw::bluetooth::emboss::StatusCode reason =
69         pw::bluetooth::emboss::StatusCode::REMOTE_USER_TERMINATED_CONNECTION);
70 DynamicByteBuffer DisconnectStatusResponsePacket();
71 
72 DynamicByteBuffer EmptyCommandPacket(hci_spec::OpCode opcode);
73 
74 DynamicByteBuffer EncryptionChangeEventPacket(
75     pw::bluetooth::emboss::StatusCode status_code,
76     hci_spec::ConnectionHandle conn,
77     hci_spec::EncryptionStatus encryption_enabled);
78 
79 DynamicByteBuffer EnhancedAcceptSynchronousConnectionRequestPacket(
80     DeviceAddress peer_address,
81     bt::StaticPacket<
82         pw::bluetooth::emboss::SynchronousConnectionParametersWriter> params);
83 
84 DynamicByteBuffer EnhancedSetupSynchronousConnectionPacket(
85     hci_spec::ConnectionHandle conn,
86     bt::StaticPacket<
87         pw::bluetooth::emboss::SynchronousConnectionParametersWriter> params);
88 
89 DynamicByteBuffer InquiryCommandPacket(
90     uint16_t inquiry_length = gap::kInquiryLengthDefault);
91 
92 DynamicByteBuffer IoCapabilityRequestNegativeReplyPacket(
93     DeviceAddress address, pw::bluetooth::emboss::StatusCode status_code);
94 DynamicByteBuffer IoCapabilityRequestNegativeReplyResponse(
95     DeviceAddress address);
96 DynamicByteBuffer IoCapabilityRequestPacket(DeviceAddress address);
97 DynamicByteBuffer IoCapabilityRequestReplyPacket(
98     DeviceAddress address,
99     pw::bluetooth::emboss::IoCapability io_cap,
100     pw::bluetooth::emboss::AuthenticationRequirements auth_req);
101 DynamicByteBuffer IoCapabilityRequestReplyResponse(DeviceAddress address);
102 DynamicByteBuffer IoCapabilityResponsePacket(
103     DeviceAddress address,
104     pw::bluetooth::emboss::IoCapability io_cap,
105     pw::bluetooth::emboss::AuthenticationRequirements auth_req);
106 
107 // Create an ISO data packet containing a complete SDU with no timestamp and a
108 // simple repeating pattern of 2, 4, 6, 8, 10, ... 254, 0, ...
109 DynamicByteBuffer IsoDataPacket(
110     size_t frame_total_size,
111     hci_spec::ConnectionHandle connection_handle = 0x123,
112     uint16_t packet_sequence_number = 0x22);
113 
114 DynamicByteBuffer LEReadRemoteFeaturesCompletePacket(
115     hci_spec::ConnectionHandle conn, hci_spec::LESupportedFeatures le_features);
116 DynamicByteBuffer LEReadRemoteFeaturesPacket(hci_spec::ConnectionHandle conn);
117 
118 DynamicByteBuffer LECisRequestEventPacket(
119     hci_spec::ConnectionHandle acl_connection_handle,
120     hci_spec::ConnectionHandle cis_connection_handle,
121     uint8_t cig_id,
122     uint8_t cis_id);
123 
124 DynamicByteBuffer LEAcceptCisRequestCommandPacket(
125     hci_spec::ConnectionHandle cis_handle);
126 
127 DynamicByteBuffer LERejectCisRequestCommandPacket(
128     hci_spec::ConnectionHandle cis_handle,
129     pw::bluetooth::emboss::StatusCode reason);
130 
131 DynamicByteBuffer LECisEstablishedEventPacket(
132     pw::bluetooth::emboss::StatusCode status,
133     hci_spec::ConnectionHandle connection_handle,
134     uint32_t cig_sync_delay_us,
135     uint32_t cis_sync_delay_us,
136     uint32_t transport_latency_c_to_p_us,
137     uint32_t transport_latency_p_to_c_us,
138     pw::bluetooth::emboss::IsoPhyType phy_c_to_p,
139     pw::bluetooth::emboss::IsoPhyType phy_p_to_c,
140     uint8_t nse,        // maximum number of subevents
141     uint8_t bn_c_to_p,  // burst number C => P
142     uint8_t bn_p_to_c,  // burst number P => C
143     uint8_t
144         ft_c_to_p,  // flush timeout (in multiples of the ISO_Interval) C => P
145     uint8_t
146         ft_p_to_c,  // flush timeout (in multiples of the ISO_Interval) P => C
147     uint16_t max_pdu_c_to_p,
148     uint16_t max_pdu_p_to_c,
149     uint16_t iso_interval);
150 
151 DynamicByteBuffer LESetupIsoDataPathPacket(
152     hci_spec::ConnectionHandle connection_handle,
153     pw::bluetooth::emboss::DataPathDirection direction,
154     uint8_t data_path_id,
155     bt::StaticPacket<pw::bluetooth::emboss::CodecIdWriter> codec_id,
156     uint32_t controller_delay,
157     const std::optional<std::vector<uint8_t>>& codec_configuration);
158 DynamicByteBuffer LESetupIsoDataPathResponse(
159     pw::bluetooth::emboss::StatusCode status,
160     hci_spec::ConnectionHandle connection_handle);
161 
162 DynamicByteBuffer LERequestPeerScaCompletePacket(
163     hci_spec::ConnectionHandle conn,
164     pw::bluetooth::emboss::LESleepClockAccuracyRange sca);
165 DynamicByteBuffer LERequestPeerScaPacket(hci_spec::ConnectionHandle conn);
166 
167 DynamicByteBuffer LEStartEncryptionPacket(hci_spec::ConnectionHandle,
168                                           uint64_t random_number,
169                                           uint16_t encrypted_diversifier,
170                                           UInt128 ltk);
171 
172 DynamicByteBuffer LinkKeyNotificationPacket(DeviceAddress address,
173                                             UInt128 link_key,
174                                             hci_spec::LinkKeyType key_type);
175 
176 DynamicByteBuffer LinkKeyRequestPacket(DeviceAddress address);
177 DynamicByteBuffer LinkKeyRequestNegativeReplyPacket(DeviceAddress address);
178 DynamicByteBuffer LinkKeyRequestNegativeReplyResponse(DeviceAddress address);
179 DynamicByteBuffer LinkKeyRequestReplyPacket(DeviceAddress address,
180                                             UInt128 link_key);
181 DynamicByteBuffer LinkKeyRequestReplyResponse(DeviceAddress address);
182 
183 DynamicByteBuffer NumberOfCompletedPacketsPacket(
184     hci_spec::ConnectionHandle conn, uint16_t num_packets);
185 
186 DynamicByteBuffer PinCodeRequestPacket(DeviceAddress address);
187 DynamicByteBuffer PinCodeRequestNegativeReplyPacket(DeviceAddress address);
188 DynamicByteBuffer PinCodeRequestNegativeReplyResponse(DeviceAddress address);
189 DynamicByteBuffer PinCodeRequestReplyPacket(DeviceAddress address,
190                                             uint8_t pin_length,
191                                             std::string pin_code);
192 DynamicByteBuffer PinCodeRequestReplyResponse(DeviceAddress address);
193 
194 // The ReadRemoteExtended*CompletePacket packets report a max page number of 3,
195 // even though there are only 2 pages, in order to test this behavior seen in
196 // real devices.
197 DynamicByteBuffer ReadRemoteExtended1CompletePacket(
198     hci_spec::ConnectionHandle conn);
199 DynamicByteBuffer ReadRemoteExtended1CompletePacketNoSsp(
200     hci_spec::ConnectionHandle conn);
201 DynamicByteBuffer ReadRemoteExtended1Packet(hci_spec::ConnectionHandle conn);
202 DynamicByteBuffer ReadRemoteExtended2CompletePacket(
203     hci_spec::ConnectionHandle conn);
204 DynamicByteBuffer ReadRemoteExtended2Packet(hci_spec::ConnectionHandle conn);
205 
206 DynamicByteBuffer ReadRemoteVersionInfoCompletePacket(
207     hci_spec::ConnectionHandle conn);
208 DynamicByteBuffer ReadRemoteVersionInfoPacket(hci_spec::ConnectionHandle conn);
209 
210 DynamicByteBuffer ReadRemoteSupportedFeaturesCompletePacket(
211     hci_spec::ConnectionHandle conn, bool extended_features);
212 DynamicByteBuffer ReadRemoteSupportedFeaturesPacket(
213     hci_spec::ConnectionHandle conn);
214 
215 DynamicByteBuffer ReadScanEnable();
216 DynamicByteBuffer ReadScanEnableResponse(uint8_t scan_enable);
217 
218 DynamicByteBuffer RejectConnectionRequestPacket(
219     DeviceAddress address, pw::bluetooth::emboss::StatusCode reason);
220 
221 DynamicByteBuffer RejectSynchronousConnectionRequest(
222     DeviceAddress address, pw::bluetooth::emboss::StatusCode status_code);
223 
224 DynamicByteBuffer RemoteNameRequestCompletePacket(
225     DeviceAddress address, const std::string& name = "Fuchsia��");
226 DynamicByteBuffer RemoteNameRequestPacket(DeviceAddress address);
227 
228 DynamicByteBuffer RoleChangePacket(
229     DeviceAddress address,
230     pw::bluetooth::emboss::ConnectionRole role,
231     pw::bluetooth::emboss::StatusCode status =
232         pw::bluetooth::emboss::StatusCode::SUCCESS);
233 
234 DynamicByteBuffer ScoDataPacket(
235     hci_spec::ConnectionHandle conn,
236     hci_spec::SynchronousDataPacketStatusFlag flag,
237     const BufferView& payload,
238     std::optional<uint8_t> payload_length_override = std::nullopt);
239 
240 DynamicByteBuffer SetConnectionEncryption(hci_spec::ConnectionHandle conn,
241                                           bool enable);
242 
243 DynamicByteBuffer SimplePairingCompletePacket(
244     DeviceAddress address, pw::bluetooth::emboss::StatusCode status_code);
245 
246 DynamicByteBuffer StartA2dpOffloadRequest(
247     const l2cap::A2dpOffloadManager::Configuration& config,
248     hci_spec::ConnectionHandle connection_handle,
249     l2cap::ChannelId l2cap_channel_id,
250     uint16_t l2cap_mtu_size);
251 
252 DynamicByteBuffer StopA2dpOffloadRequest();
253 
254 DynamicByteBuffer SynchronousConnectionCompletePacket(
255     hci_spec::ConnectionHandle conn,
256     DeviceAddress address,
257     hci_spec::LinkType link_type,
258     pw::bluetooth::emboss::StatusCode status);
259 
260 DynamicByteBuffer UserConfirmationRequestNegativeReplyPacket(
261     DeviceAddress address);
262 DynamicByteBuffer UserConfirmationRequestPacket(DeviceAddress address,
263                                                 uint32_t passkey);
264 DynamicByteBuffer UserConfirmationRequestReplyPacket(DeviceAddress address);
265 
266 DynamicByteBuffer UserPasskeyNotificationPacket(DeviceAddress address,
267                                                 uint32_t passkey);
268 
269 DynamicByteBuffer UserPasskeyRequestNegativeReply(DeviceAddress address);
270 DynamicByteBuffer UserPasskeyRequestNegativeReplyResponse(
271     DeviceAddress address);
272 
273 DynamicByteBuffer UserPasskeyRequestPacket(DeviceAddress address);
274 DynamicByteBuffer UserPasskeyRequestReplyPacket(DeviceAddress address,
275                                                 uint32_t passkey);
276 DynamicByteBuffer UserPasskeyRequestReplyResponse(DeviceAddress address);
277 
278 DynamicByteBuffer WriteAutomaticFlushTimeoutPacket(
279     hci_spec::ConnectionHandle conn, uint16_t flush_timeout);
280 
281 DynamicByteBuffer WriteInquiryScanActivity(uint16_t scan_interval,
282                                            uint16_t scan_window);
283 
284 DynamicByteBuffer WritePageScanActivityPacket(uint16_t scan_interval,
285                                               uint16_t scan_window);
286 
287 DynamicByteBuffer WritePageScanTypePacket(uint8_t scan_type);
288 DynamicByteBuffer WritePageScanTypeResponse();
289 
290 DynamicByteBuffer WritePageTimeoutPacket(uint16_t page_timeout);
291 
292 DynamicByteBuffer WritePinTypePacket(uint8_t pin_type);
293 
294 DynamicByteBuffer WriteScanEnable(uint8_t scan_enable);
295 
296 }  // namespace bt::testing
297