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 // clang-format off
17 
18 #include <array>
19 #include <cstddef>
20 #include <cstdint>
21 
22 #include <pw_bluetooth/hci_commands.emb.h>
23 #include <pw_chrono/system_clock.h>
24 #include "pw_bluetooth_sapphire/internal/host/common/advertising_data.h"
25 
26 // This file contains constants and numbers used in HCI packet payloads.
27 
28 namespace bt::hci_spec {
29 
30 // Bitmask values for the 8-octet Local Supported LMP Features bit-field. See
31 // Core Specv5.0, Volume 2, Part C, Section 3.3 "Feature Mask Definition".
32 enum class LMPFeature : uint64_t {
33   // Extended features (Page 0): Octet 0
34   k3SlotPackets   = (1 << 0),
35   k5SlotPackets   = (1 << 1),
36   kEncryption     = (1 << 2),
37   kSlotOffset     = (1 << 3),
38   kTimingAccuracy = (1 << 4),
39   kRoleSwitch     = (1 << 5),
40   kHoldMode       = (1 << 6),
41   kSniffMode      = (1 << 7),
42 
43   // Extended features (Page 0): Octet 1
44   // kPreviouslyUsed               = (1ull << 8),
45   kPowerControlRequests         = (1ull << 9),
46   kChannelQualityDrivenDataRate = (1ull << 10),
47   kSCOLink                      = (1ull << 11),
48   kHV2Packets                   = (1ull << 12),
49   kHV3Packets                   = (1ull << 13),
50   kmLawLogSynchronousData       = (1ull << 14),
51   kALawLogSynchronousData       = (1ull << 15),
52 
53   // Extended features (Page 0): Octet 2
54   kCVSDSynchronousData        = (1ull << 16),
55   kPagingParameterNegotiation = (1ull << 17),
56   kPowerControl               = (1ull << 18),
57   kTransparentSynchronousData = (1ull << 19),
58   kFCLLeastSignificantBit     = (1ull << 20),
59   kFCLMiddleBit               = (1ull << 21),
60   kFCLMostSignificantBit      = (1ull << 22),
61   kBroadcastEncryption        = (1ull << 23),
62 
63   // Extended features (Page 0): Octet 3
64   // Reserved (1ull << 24)
65   kEDRACL2MbitMode        = (1ull << 25),
66   kEDRACL3MbitMode        = (1ull << 26),
67   kEnhancedInquiryScan    = (1ull << 27),
68   kInterlacedInquiryScan  = (1ull << 28),
69   kInterlacedPageScan     = (1ull << 29),
70   kRSSIwithInquiryResults = (1ull << 30),
71   kEV3Packets             = (1ull << 31),
72 
73   // Extended features (Page 0): Octet 4
74   kEV4Packets                  = (1ull << 32),
75   kEV5Packets                  = (1ull << 33),
76   // Reserved
77   kAFHCapablePeripheral        = (1ull << 35),
78   kAFHClassificationPeripheral = (1ull << 36),
79   kBREDRNotSupported           = (1ull << 37),
80   kLESupportedController       = (1ull << 38),
81   k3SlotEDRACLPackets          = (1ull << 39),
82 
83   // Extended features (Page 0): Octet 5
84   k5SlotEDRACLPackets       = (1ull << 40),
85   kSniffSubrating           = (1ull << 41),
86   kPauseEncryption          = (1ull << 42),
87   kAFHCapableCentral        = (1ull << 43),
88   kAFHClassificationCentral = (1ull << 44),
89   kEDReSCO2MbitMode         = (1ull << 45),
90   kEDReSCO3MbitMode         = (1ull << 46),
91   k3SlotEDReSCOPackets      = (1ull << 47),
92 
93   // Extended features (Page 0): Octet 6
94   kExtendedInquiryResponse              = (1ull << 48),
95   kSimultaneousLEAndBREDR               = (1ull << 49),
96   // Reserved
97   kSecureSimplePairingControllerSupport = (1ull << 51),
98   kEncapsulatedPDU                      = (1ull << 52),
99   kErroneousDataReporting               = (1ull << 53),
100   kNonflushablePacketBoundaryFlag       = (1ull << 54),
101   // Reserved
102 
103   // Extended features (Page 0): Octet 7
104   kLinkSupervisionTimeoutChangedEvent = (1ull << 56),
105   kVariableInquiryTxPowerLevel        = (1ull << 57),
106   kEnhancedPowerControl               = (1ull << 58),
107   // Reserved
108   // Reserved
109   // Reserved
110   // Reserved
111   kExtendedFeatures                   = (1ull << 63),
112 
113   // Extended features (Page 1): Octet 0
114   kSecureSimplePairingHostSupport = (1ull << 0),
115   kLESupportedHost                = (1ull << 1),
116   kSimultaneousLEAndBREDRHost     = (1ull << 2),
117   kSecureConnectionsHostSupport   = (1ull << 3),
118 
119   // Extended features (Page 2): Octet 0
120   kCPBTransmitterOperation          = (1ull << 0),
121   kCPBReceiverOperation             = (1ull << 1),
122   kSynchronizationTrain             = (1ull << 2),
123   kSynchronizationScan              = (1ull << 3),
124   kInquiryResponseNotificationEvent = (1ull << 4),
125   kGeneralizedInterlacedScan        = (1ull << 5),
126   kCoarseClockAdjustment            = (1ull << 6),
127   // Reserved
128 
129   // Extended features (Page 2): Octet 1
130   kSecureConnectionsControllerSupport = (1ull << 8),
131   kPing                               = (1ull << 9),
132   kSlotAvailabilityMask               = (1ull << 10),
133   kTrainNudging                       = (1ull << 11)
134 };
135 
136 // Bitmask of 8-octet LE supported features field. See Core Spec
137 // v5.0, Volume 6, Part B, Section 4.6 "Feature Support".
138 struct LESupportedFeatures {
139   uint64_t le_features;
140 } __attribute__((packed));
141 
142 // Bitmask values for the 8-octet LE Supported Features bit-field. See Core Spec
143 // v5.0, Volume 6, Part B, Section 4.6 "Feature Support".
144 enum class LESupportedFeature : uint64_t {
145   kLEEncryption                         = (1 << 0),
146   kConnectionParametersRequestProcedure = (1 << 1),
147   kExtendedRejectIndication             = (1 << 2),
148   kPeripheralInitiatedFeaturesExchange       = (1 << 3),
149   kLEPing                               = (1 << 4),
150   kLEDataPacketLengthExtension          = (1 << 5),
151   kLLPrivacy                            = (1 << 6),
152   kExtendedScannerFilterPolicies        = (1 << 7),
153 
154   // Added in 5.0
155   kLE2MPHY                                  = (1 << 8),
156   kStableModulationIndexTransmitter         = (1 << 9),
157   kStableModulationIndexReceiver            = (1 << 10),
158   kLECodedPHY                               = (1 << 11),
159   kLEExtendedAdvertising                    = (1 << 12),
160   kLEPeriodicAdvertising                    = (1 << 13),
161   kChannelSelectionAlgorithm2               = (1 << 14),
162   kLEPowerClass1                            = (1 << 15),
163   kMinimumNumberOfUsedChannelsProcedure     = (1 << 16),
164 
165   // Added in 5.1
166   kConnectionCTERequest                     = (1 << 17),
167   kConnectionCTEResponse                    = (1 << 18),
168   kConnectionlessCTETransmitter             = (1 << 19),
169   kConnectionlessCTEReceiver                = (1 << 20),
170   kAntennaSwitchingDuringCTETransmission    = (1 << 21),
171   kAntennaSwitchingDuringCTEReception       = (1 << 22),
172   kReceivingConstantToneExtensions          = (1 << 23),
173   kPeriodicAdvertisingSyncTransferSender    = (1 << 24),
174   kPeriodicAdvertisingSyncTransferRecipient = (1 << 25),
175   kSleepClockAccuracyUpdates                = (1 << 26),
176   kRemotePublicKeyValidation                = (1 << 27),
177 
178   // Added in 5.2
179   kConnectedIsochronousStreamCentral     = (1 << 28),
180   kConnectedIsochronousStreamPeripheral  = (1 << 29),
181   kIsochronousBoradcaster                = (1 << 30),
182   kSynchronizedReceiver                  = (1ull << 31),
183   kConnectedIsochronousStreamHostSupport = (1ull << 32),
184   kLEPowerControlRequest                 = (1ull << 33),
185   kLEPowerChangeIndication               = (1ull << 34),
186   kLEPathLossMonitoring                  = (1ull << 35),
187 
188   // The rest is reserved for future use.
189 };
190 
191 // Bit positions for constants LE Supported Features that are controlled by the host
192 // for use in the LE Set Host Feature command
193 enum class LESupportedFeatureBitPos : uint8_t {
194   kConnectedIsochronousStreamHostSupport = 32,
195 };
196 
197 // Bitmask values for the 8-octet HCI_Set_Event_Mask command parameter.
198 enum class EventMask : uint64_t {
199   kInquiryCompleteEvent                         = (1 << 0),
200   kInquiryResultEvent                           = (1 << 1),
201   kConnectionCompleteEvent                      = (1 << 2),
202   kConnectionRequestEvent                       = (1 << 3),
203   kDisconnectionCompleteEvent                   = (1 << 4),
204   kAuthenticationCompleteEvent                  = (1 << 5),
205   kRemoteNameRequestCompleteEvent               = (1 << 6),
206   kEncryptionChangeEvent                        = (1 << 7),
207   kChangeConnectionLinkKeyCompleteEvent         = (1 << 8),
208   kLinkKeyTypeChangedEvent                      = (1 << 9),
209   kReadRemoteSupportedFeaturesCompleteEvent     = (1 << 10),
210   kReadRemoteVersionInformationCompleteEvent    = (1 << 11),
211   kQoSSetupCompleteEvent                        = (1 << 12),
212   // Reserved For Future Use: (1 << 13)
213   // Reserved For Future Use: (1 << 14)
214   kHardwareErrorEvent                           = (1 << 15),
215   kFlushOccurredEvent                           = (1 << 16),
216   kRoleChangeEvent                              = (1 << 17),
217   // Reserved For Future Use: (1 << 18)
218   kModeChangeEvent                              = (1 << 19),
219   kReturnLinkKeysEvent                          = (1 << 20),
220   kPINCodeRequestEvent                          = (1 << 21),
221   kLinkKeyRequestEvent                          = (1 << 22),
222   kLinkKeyNotificationEvent                     = (1 << 23),
223   kLoopbackCommandEvent                         = (1 << 24),
224   kDataBufferOverflowEvent                      = (1 << 25),
225   kMaxSlotsChangeEvent                          = (1 << 26),
226   kReadClockOffsetCompleteEvent                 = (1 << 27),
227   kConnectionPacketTypeChangedEvent             = (1 << 28),
228   kQoSViolationEvent                            = (1 << 29),
229   kPageScanModeChangeEvent                      = (1 << 30),  // deprecated
230   kPageScanRepetitionModeChangeEvent            = (1ull << 31),
231   kFlowSpecificationCompleteEvent               = (1ull << 32),
232   kInquiryResultWithRSSIEvent                   = (1ull << 33),
233   kReadRemoteExtendedFeaturesCompleteEvent      = (1ull << 34),
234   // Reserved For Future Use: (1ull << 35)
235   // Reserved For Future Use: (1ull << 36)
236   // Reserved For Future Use: (1ull << 37)
237   // Reserved For Future Use: (1ull << 38)
238   // Reserved For Future Use: (1ull << 39)
239   // Reserved For Future Use: (1ull << 40)
240   // Reserved For Future Use: (1ull << 41)
241   // Reserved For Future Use: (1ull << 42)
242   kSynchronousConnectionCompleteEvent           = (1ull << 43),
243   kSynchronousConnectionChangedEvent            = (1ull << 44),
244   kSniffSubratingEvent                          = (1ull << 45),
245   kExtendedInquiryResultEvent                   = (1ull << 46),
246   kEncryptionKeyRefreshCompleteEvent            = (1ull << 47),
247   kIOCapabilityRequestEvent                     = (1ull << 48),
248   kIOCapabilityResponseEvent                    = (1ull << 49),
249   kUserConfirmationRequestEvent                 = (1ull << 50),
250   kUserPasskeyRequestEvent                      = (1ull << 51),
251   kRemoteOOBDataRequestEvent                    = (1ull << 52),
252   kSimplePairingCompleteEvent                   = (1ull << 53),
253   // Reserved For Future Use: (1ull << 54)
254   kLinkSupervisionTimeoutChangedEvent           = (1ull << 55),
255   kEnhancedFlushCompleteEvent                   = (1ull << 56),
256   // Reserved For Future Use: (1ull << 57)
257   kUserPasskeyNotificationEvent                 = (1ull << 58),
258   kKeypressNotificationEvent                    = (1ull << 59),
259   kRemoteHostSupportedFeaturesNotificationEvent = (1ull << 60),
260   kLEMetaEvent                                  = (1ull << 61),
261   // Reserved For Future Use: (1ull << 62)
262   // Reserved For Future Use: (1ull << 63)
263 };
264 
265 // Bitmask values for the 8-octet HCI_Set_Event_Mask_Page_2 command parameter.
266 enum class EventMaskPage2 : uint64_t {
267   kPhysicalLinkCompleteEvent                              = (1 << 0),
268   kChannelSelectedEvent                                   = (1 << 1),
269   kDisconnectionPhysicalLinkCompleteEvent                 = (1 << 2),
270   kPhysicalLinkLossEarlyWarningEvent                      = (1 << 3),
271   kPhysicalLinkRecoveryEvent                              = (1 << 4),
272   kLogicalLinkCompleteEvent                               = (1 << 5),
273   kDisconnectionLogicalLinkCompleteEvent                  = (1 << 6),
274   kFlowSpecModifyCompleteEvent                            = (1 << 7),
275   kNumberOfCompletedDataBlocksEvent                       = (1 << 8),
276   kAMPStartTestEvent                                      = (1 << 9),
277   kAMPTestEndEvent                                        = (1 << 10),
278   kAMPReceiverReportEvent                                 = (1 << 11),
279   kShortRangeModeChangeCompleteEvent                      = (1 << 12),
280   kAMPStatusChangeEvent                                   = (1 << 13),
281   kTriggeredClockCaptureEvent                             = (1 << 14),
282   kSynchronizationTrainCompleteEvent                      = (1 << 15),
283   kSynchronizationTrainReceivedEvent                      = (1 << 16),
284   kConnectionlessPeripheralBroadcastReceiveEvent          = (1 << 17),
285   kConnectionlessPeripheralBroadcastTimeoutEvent          = (1 << 18),
286   kTruncatedPageCompleteEvent                             = (1 << 19),
287   kPeripheralPageResponseTimeoutEvent                     = (1 << 20),
288   kConnectionlessPeripheralBroadcastChannelMapChangeEvent = (1 << 21),
289   kInquiryResponseNotificationEvent                       = (1 << 22),
290   kAuthenticatedPayloadTimeoutExpiredEvent                = (1 << 23),
291   kSAMStatusChangeEvent                                   = (1 << 24),
292 };
293 
294 // Bitmask values for the 8-octet HCI_LE_Set_Event_Mask command parameter.
295 enum class LEEventMask : uint64_t {
296   kLEConnectionComplete                        = (1ull << 0),
297   kLEAdvertisingReport                         = (1ull << 1),
298   kLEConnectionUpdateComplete                  = (1ull << 2),
299   kLEReadRemoteFeaturesComplete                = (1ull << 3),
300   kLELongTermKeyRequest                        = (1ull << 4),
301   kLERemoteConnectionParameterRequest          = (1ull << 5),
302   kLEDataLengthChange                          = (1ull << 6),
303   kLEReadLocalP256PublicKeyComplete            = (1ull << 7),
304   kLEGenerateDHKeyComplete                     = (1ull << 8),
305   kLEEnhancedConnectionComplete                = (1ull << 9),
306   kLEDirectedAdvertisingReport                 = (1ull << 10),
307   kLEPHYUpdateComplete                         = (1ull << 11),
308   kLEExtendedAdvertisingReport                 = (1ull << 12),
309   kLEPeriodicAdvertisingSyncEstablished        = (1ull << 13),
310   kLEPeriodicAdvertisingReport                 = (1ull << 14),
311   kLEPeriodicAdvertisingSyncLost               = (1ull << 15),
312   kLEExtendedScanTimeout                       = (1ull << 16),
313   kLEExtendedAdvertisingSetTerminated          = (1ull << 17),
314   kLEScanRequestReceived                       = (1ull << 18),
315   kLEChannelSelectionAlgorithm                 = (1ull << 19),
316   kLEConnectionlessIQReport                    = (1ull << 20),
317   kLEConnectionIQReport                        = (1ull << 21),
318   kLECTERequestFailed                          = (1ull << 22),
319   kLEPeriodicAdvertisingSyncTransferReceived   = (1ull << 23),
320   kLECISEstablished                            = (1ull << 24),
321   kLECISRequest                                = (1ull << 25),
322   kLECreateBIGComplete                         = (1ull << 26),
323   kLETerminateBIGComplete                      = (1ull << 27),
324   kLEBIGSyncEstablished                        = (1ull << 28),
325   kLEBIGSyncLost                               = (1ull << 29),
326   kLERequestPeerSCAComplete                    = (1ull << 30),
327   kLEPathLossThreshold                         = (1ull << 31),
328   kLETransmitPowerReporting                    = (1ull << 32),
329   kLEBIGInfoAdvertisingReport                  = (1ull << 33),
330   kLESubrateChange                             = (1ull << 34),
331   kLEPeriodicAdvertisingSyncEstablishedV2      = (1ull << 35),
332   kLEPeriodicAdvertisingReportV2               = (1ull << 36),
333   kLEPeriodicAdvertisingSyncTransferReceivedV2 = (1ull << 37),
334   kLEPeriodicAdvertisingSubeventDataRequest    = (1ull << 38),
335   kLEPeriodicAdvertisingResponseReport         = (1ull << 39),
336   kLEEnhancedConnectionCompleteV2              = (1ull << 40),
337 };
338 
339 // Values that can be passed to the Type parameter in a
340 // HCI_Read_Transmit_Power_Level command.
341 enum class ReadTransmitPowerType : uint8_t {
342   // Read Current Transmit Power Level.
343   kCurrent = 0x00,
344 
345   // Read Maximum Transmit Power Level.
346   kMax = 0x01,
347 };
348 
349 // Possible values for the Encryption_Enabled parameter in a HCI_Encryption_Change event
350 // (see Vol 2, Part E, 7.7.8).
351 enum class EncryptionStatus : uint8_t {
352   // Link Level Encryption is OFF.
353   kOff = 0x00,
354 
355   // Link Level Encryption is ON with E0 for BR/EDR and AES-CCM for LE.
356   kOn = 0x01,
357 
358   // Link Level Encryption is ON with AES-CCM for BR/EDR.
359   kBredrSecureConnections = 0x02,
360 };
361 
362 // HCI command timeout interval (milliseconds)
363 // TODO(fxbug.dev/42070690, fxbug.dev/42070801) This was
364 // increased to handle flaking integration tests. We may want to reduce this
365 // to something lower again once we have a bette resolution to this issue.
366 constexpr pw::chrono::SystemClock::duration kCommandTimeout = std::chrono::duration_cast<pw::chrono::SystemClock::duration>(std::chrono::seconds(10));
367 
368 // The minimum and maximum range values for the LE advertising interval
369 // parameters.
370 // (see Core Spec v5.0, Vol 2, Part E, Section 7.8.5)
371 constexpr uint16_t kLEAdvertisingIntervalMin = 0x0020;
372 constexpr uint16_t kLEAdvertisingIntervalMax = 0x4000;
373 
374 // The minimum and maximum range values for the LE periodic advertising interval
375 // parameters.
376 // (see Core Spec v5.0, Vol 2, Part E, Section 7.8.61)
377 constexpr uint16_t kLEPeriodicAdvertisingIntervalMin = 0x0006;
378 constexpr uint16_t kLEPeriodicAdvertisingIntervalMax = 0xFFFF;
379 
380 // The minimum and maximum range values for the LE extended advertising interval
381 // parameters.
382 constexpr uint32_t kLEExtendedAdvertisingIntervalMin = 0x000020;
383 constexpr uint32_t kLEExtendedAdvertisingIntervalMax = 0xFFFFFF;
384 
385 // The default LE advertising interval parameter value, corresponding to 1.28
386 // seconds (see Core Spec v5.0, Vol 2, Part E, Section 7.8.5).
387 constexpr uint16_t kLEAdvertisingIntervalDefault = 0x0800;
388 
389 // The minimum and maximum range values for the LE scan interval parameters.
390 // (see Core Spec v5.0, Vol 2, Part E, Section 7.8.10)
391 constexpr uint16_t kLEScanIntervalMin = 0x0004;
392 constexpr uint16_t kLEScanIntervalMax = 0x4000;
393 
394 // The minimum and maximum range values for the LE extended scan interval
395 // parameters.
396 // (see Core Spec v5.0, Vol 2, Part E, Section 7.8.64)
397 constexpr uint16_t kLEExtendedScanIntervalMin = 0x0004;
398 constexpr uint16_t kLEExtendedScanIntervalMax = 0xFFFF;
399 
400 // The default LE scan interval parameter value, corresponding to 10
401 // milliseconds (see Core Spec v5.0, Vol 2, Part E, Section 7.8.10).
402 constexpr uint16_t kLEScanIntervalDefault = 0x0010;
403 
404 // The minimum and maximum range values for the LE connection interval parameters.
405 // (see Core Spec v5.0, Vol 2, Part E, Section 7.8.12)
406 constexpr uint16_t kLEConnectionIntervalMin = 0x0006;
407 constexpr uint16_t kLEConnectionIntervalMax = 0x0C80;
408 
409 // The maximum value that can be used for the |conn_latency| parameter in a
410 // HCI_LE_Create_Connection
411 // command (see Core Spec v5.0, Vol 2, Part E, Section 7.8.12).
412 constexpr uint16_t kLEConnectionLatencyMax = 0x01F3;
413 
414 // The minimum and maximum range values for LE connection supervision timeout
415 // parameters.
416 constexpr uint16_t kLEConnectionSupervisionTimeoutMin = 0x000A;
417 constexpr uint16_t kLEConnectionSupervisionTimeoutMax = 0x0C80;
418 
419 // The minimum and maximum range values for LE link layer tx PDU used on
420 // connections.
421 constexpr uint16_t kLEMaxTxOctetsMin = 0x001B;
422 constexpr uint16_t kLEMaxTxOctetsMax = 0x00FB;
423 
424 // The minimum and maximum range values for LE link layer tx maximum packet
425 // transmission time used on connections.
426 constexpr uint16_t kLEMaxTxTimeMin = 0x0148;
427 constexpr uint16_t kLEMaxTxTimeMax = 0x4290;
428 
429 // Minimum, maximum, default values for the Resolvable Private Address timeout
430 // parameter.
431 constexpr uint16_t kLERPATimeoutMin = 0x0001;      // 1 second
432 constexpr uint16_t kLERPATimeoutMax = 0xA1B8;      // Approx. 11.5 hours
433 constexpr uint16_t kLERPATimeoutDefault = 0x0384;  // 900 seconds or 15 minutes.
434 
435 // The maximum length of advertising data that can get passed to the
436 // HCI_LE_Set_Advertising_Data command.
437 //
438 // This constant should be used on pre-5.0 controllers. On controllers that
439 // support 5.0+ the host should use the
440 // HCI_LE_Read_Maximum_Advertising_Data_Length command to obtain this
441 // information.
442 constexpr size_t kMaxLEAdvertisingDataLength = 0x1F;  // (31)
443 
444 // Core Spec Version 5.4, Volume 6, Part B, Section 2.3.4.9: the total
445 // amount of Host Advertising Data before fragmentation shall not exceed 1650
446 // octets.
447 constexpr size_t kMaxLEExtendedAdvertisingDataLength = 1650;
448 
449 // Tx Power values, See Core Spec v5.0 Vol 4, Part E, 7.8.6.
450 constexpr int8_t kTxPowerInvalid = 127;
451 constexpr int8_t kLEAdvertisingTxPowerMin = -127;
452 constexpr int8_t kLEAdvertisingTxPowerMax = 20;
453 constexpr int8_t kLEExtendedAdvertisingTxPowerNoPreference = 0x7F; // Vol 4, Part E, 7.8.53
454 
455 // Values used in enabling extended advertising. See Core Spec v5.0 Vol 4, Part E, 7.8.56.
456 constexpr uint8_t kMaxAdvertisingHandle = 0xEF;
457 constexpr uint8_t kNoMaxExtendedAdvertisingEvents = 0;
458 constexpr uint8_t kNoAdvertisingDuration = 0;
459 
460 // Values used in enabling extended scanning. See Core Spec v5.4 Vol 4, Part E, 7.8.64.
461 constexpr uint16_t kNoScanningDuration = 0;
462 constexpr uint16_t kNoScanningPeriod = 0;
463 
464 // LE Advertising event types that can be reported in a LE Advertising Report
465 // event.
466 enum class LEAdvertisingEventType : uint8_t {
467   // Connectable and scannable undirected advertising (ADV_IND)
468   kAdvInd = 0x00,
469 
470   // Connectable directed advertising (ADV_DIRECT_IND)
471   kAdvDirectInd = 0x01,
472 
473   // Scannable undirected advertising (ADV_SCAN_IND)
474   kAdvScanInd = 0x02,
475 
476   // Non connectable undirected advertising (ADV_NONCONN_IND)
477   kAdvNonConnInd = 0x03,
478 
479   // Scan Response (SCAN_RSP)
480   kScanRsp = 0x04,
481 
482   // The rest is reserved for future use
483 };
484 
485 // Possible values that can be reported for the |address_type| parameter in a LE
486 // Advertising Report event.
487 enum class LEAddressType : uint8_t {
488   // Public Device Address
489   kPublic = 0x00,
490 
491   // Random Device Address
492   kRandom = 0x01,
493 
494   // Public Identity Address (Corresponds to Resolved Private Address)
495   kPublicIdentity = 0x02,
496 
497   // Random (static) Identity Address (Corresponds to Resolved Private Address)
498   kRandomIdentity = 0x03,
499 
500   // This is a special value used in LE Extended Advertising Report events to
501   // indicate a random address that the controller was unable to resolve.
502   kRandomUnresolved = 0xFE,
503 
504   // This is a special value that is only used in LE Directed Advertising Report
505   // events.
506   // Meaning: No address provided (anonymous advertisement)
507   kAnonymous = 0xFF,
508 };
509 
510 // Possible values that can be used for the |peer_address_type| parameter in a
511 // HCI_LE_Set_Advertising_Parameters command.
512 // (see Core Spec v5.0, Vol 2, Part E, Section 7.8.5)
513 enum class LEPeerAddressType : uint8_t {
514   // Public Device Address (default) or Public Identity Address
515   kPublic = 0x00,
516 
517   // Random Device Address or Random (static) Identity Address
518   kRandom = 0x01,
519 
520   // This is a special value that should only be used with the
521   // HCI_LE_Add_Device_To_Filter_Accept_List and HCI_LE_Remove_Device_From_Filter_Accept_List
522   // commands for peer devices sending anonymous advertisements.
523   kAnonymous = 0xFF,
524 };
525 
526 // Possible values that can be used for the |adv_channel_map| bitfield in a
527 // HCI_LE_Set_Advertising_Parameters command.
528 // (see Core Spec v5.0, Vol 2, Part E, Section 7.8.5)
529 constexpr uint8_t kLEAdvertisingChannel37 = 0x01;
530 constexpr uint8_t kLEAdvertisingChannel38 = 0x02;
531 constexpr uint8_t kLEAdvertisingChannel39 = 0x04;
532 constexpr uint8_t kLEAdvertisingChannelAll = 0x07;
533 
534 // Possible values that can be used for the Filter_Policy parameter in a
535 // HCI_LE_Periodic_Advertising_Create_Sync command.
536 // (see Core Spec v5.0, Vol 2, Part E, Section 7.8.67)
537 enum class LEPeriodicAdvFilterPolicy : uint8_t {
538   // Use the Advertising_SID, Advertising_Address_Type, and Advertising_Address
539   // parameters to determine which advertiser to listen to.
540   kListNotUsed = 0x00,
541 
542   // Use the Periodic Advertiser List to determine which advertiser to listen to.
543   kUsePeriodicAdvList = 0x01,
544 };
545 
546 // The PHY bitfield values that can be used in HCI_LE_Set_PHY and
547 // HCI_LE_Set_Default_PHY commands that can be used for the TX_PHYS and RX_PHYS
548 // parameters.
549 constexpr uint8_t kLEPHYBit1M = (1 << 0);
550 constexpr uint8_t kLEPHYBit2M = (1 << 1);
551 constexpr uint8_t kLEPHYBitCoded = (1 << 2);
552 
553 // The PHY bitfield values that can be used in HCI_LE_Set_PHY and
554 // HCI_LE_Set_Default_PHY commands that can be used for the ALL_PHYS parameter.
555 constexpr uint8_t kLEAllPHYSBitTxNoPreference = (1 << 0);
556 constexpr uint8_t kLEAllPHYSBitRxNoPreference = (1 << 1);
557 
558 // Potential values that can be used for the LE PHY parameters in HCI commands
559 // and events.
560 enum class LEPHY : uint8_t {
561   kLE1M = 0x01,
562   kLE2M = 0x02,
563 
564   // Only for the HCI_LE_Enhanced_Transmitter_Test command this value implies
565   // S=8 data coding. Otherwise this refers to general LE Coded PHY.
566   kLECoded = 0x03,
567 
568   // This should ony be used with the HCI_LE_Enhanced_Transmitter_Test command.
569   kLECodedS2 = 0x04,
570 
571   // Some HCI events may use this value to indicate that no packets were sent
572   // on a particular PHY, specifically the LE Extended Advertising Report event.
573   kNone = 0x00,
574 };
575 
576 // Potential values that can be used for the PHY_options parameter in a
577 // HCI_LE_Set_PHY command.
578 enum class LEPHYOptions : uint16_t {
579   kNoPreferredEncoding = 0x00,
580   kPreferS2Coding = 0x01,
581   kPreferS8Coding = 0x02,
582 };
583 
584 // Potential values that can be passed for the Modulation_Index parameter in a
585 // HCI_LE_Enhanced_Receiver_Test command.
586 enum class LETestModulationIndex : uint8_t {
587   kAssumeStandard = 0x00,
588   kAssumeStable = 0x01,
589 };
590 
591 // Potential values for the Operation parameter in a
592 // HCI_LE_Set_Extended_Advertising_Data command.
593 enum class LESetExtendedAdvDataOp : uint8_t {
594   // Intermediate fragment of fragmented extended advertising data.
595   kIntermediateFragment = 0x00,
596 
597   // First fragment of fragmented extended advertising data.
598   kFirstFragment = 0x01,
599 
600   // Last fragment of fragmented extended advertising data.
601   kLastFragment = 0x02,
602 
603   // Complete extended advertising data.
604   kComplete = 0x03,
605 
606   // Unchanged data (just update the Advertising DID)
607   kUnchangedData = 0x04,
608 };
609 
610 // Potential values for the Fragment_Preference parameter in a
611 // HCI_LE_Set_Extended_Advertising_Data command.
612 enum class LEExtendedAdvFragmentPreference : uint8_t {
613   // The Controller may fragment all Host advertising data
614   kMayFragment = 0x00,
615 
616   // The Controller should not fragment or should minimize fragmentation of Host
617   // advertising data
618   kShouldNotFragment = 0x01,
619 };
620 
621 // The Advertising_Event_Properties bitfield values used in a
622 // HCI_LE_Set_Extended_Advertising_Parameters command.
623 constexpr uint16_t kLEAdvEventPropBitConnectable                      = (1 << 0);
624 constexpr uint16_t kLEAdvEventPropBitScannable                        = (1 << 1);
625 constexpr uint16_t kLEAdvEventPropBitDirected                         = (1 << 2);
626 constexpr uint16_t kLEAdvEventPropBitHighDutyCycleDirectedConnectable = (1 << 3);
627 constexpr uint16_t kLEAdvEventPropBitUseLegacyPDUs                    = (1 << 4);
628 constexpr uint16_t kLEAdvEventPropBitAnonymousAdvertising             = (1 << 5);
629 constexpr uint16_t kLEAdvEventPropBitIncludeTxPower                   = (1 << 6);
630 
631 // The Event_Type bitfield values reported in a LE Extended Advertising Report Event.
632 constexpr uint16_t kLEExtendedAdvEventTypeConnectable  = (1 << 0);
633 constexpr uint16_t kLEExtendedAdvEventTypeScannable    = (1 << 1);
634 constexpr uint16_t kLEExtendedAdvEventTypeDirected     = (1 << 2);
635 constexpr uint16_t kLEExtendedAdvEventTypeScanResponse = (1 << 3);
636 constexpr uint16_t kLEExtendedAdvEventTypeLegacy       = (1 << 4);
637 
638 // LE Advertising data status properties stored in bits 5 and 6 of the
639 // Event_Type bitfield of a LE Extended Advertising Report event and in a LE
640 // Periodic Advertising Report event.
641 enum class LEAdvertisingDataStatus : uint16_t {
642   // Data is complete.
643   kComplete = 0x00,
644 
645   // Data is incomplete, more data to come in future events.
646   kIncomplete = 0x01,
647 
648   // Data is incomplete and truncated, no more data to come.
649   kIncompleteTruncated = 0x02,
650 };
651 
652 // The Periodic_Advertising_Properties bitfield used in a
653 // HCI_LE_Set_Periodic_Advertising_Parameters command.
654 constexpr uint16_t kLEPeriodicAdvPropBitIncludeTxPower = (1 << 6);
655 
656 // The maximum length of LE data packets when the LE Data Packet Length Extension
657 // feature is supported. See v5.0, Vol 6, Part B, 4.5.10, Table 4.3.
658 constexpr size_t kMaxLEExtendedDataLength = 251;
659 
660 // Maximum value of the Advertising SID subfield in the ADI field of the PDU
661 constexpr uint8_t kLEAdvertsingSIDMax = 0xEF;
662 
663 // Invalid RSSI value.
664 constexpr int8_t kRSSIInvalid = 127;
665 
666 // Invalid advertising sid value
667 constexpr uint8_t kAdvertisingSidInvalid = 0xFF;
668 
669 // The maximum length of a friendly name that can be assigned to a BR/EDR
670 // controller, in octets.
671 constexpr size_t kMaxNameLength = bt::kMaxNameLength;
672 
673 // The maximum number of bytes in a HCI Command Packet payload, excluding the
674 // header. See Core Spec v5.0 Vol 2, Part E, 5.4.1, paragraph 2.
675 constexpr size_t kMaxCommandPacketPayloadSize = 255;
676 
677 // The maximum number of bytes in a HCI event Packet payload, excluding the
678 // header. See Core Spec v5.0 Vol 2, Part E, 5.4.4, paragraph 1.
679 constexpr size_t kMaxEventPacketPayloadSize = 255;
680 
681 // The maximum number of bytes in a HCI ACL data packet payload supported by our
682 // stack.
683 constexpr size_t kMaxACLPayloadSize = 1024;
684 
685 // The maximum number of bytes in a HCI Synchronous Data packet payload.
686 // This is based on the maximum value of the 1-byte Data_Total_Length field of a Synchronous Data packet.
687 constexpr size_t kMaxSynchronousDataPacketPayloadSize = 255;
688 
689 // The maximum number of bytes in an Isochronous data packet payload, based on
690 // the maximum size (12 bits) of the data_total_length field of an Isochronous
691 // data packet.
692 // See Core Spec v5.4, Vol 4, Part E, Section 5.4.5
693 constexpr size_t kMaxIsochronousDataPacketPayloadSize = 16384;
694 
695 // Values that can be used in HCI Read|WriteFlowControlMode commands.
696 enum class FlowControlMode : uint8_t {
697   // Packet based data flow control mode (default for a Primary Controller)
698   kPacketBased = 0x00,
699 
700   // Data block based flow control mode (default for an AMP Controller)
701   kDataBlockBased = 0x01
702 };
703 
704 // The Packet Boundary Flag is contained in bits 4 and 5 in the second octet of
705 // a HCI ACL Data packet.
706 enum class ACLPacketBoundaryFlag : uint8_t {
707   kFirstNonFlushable  = 0x00,
708   kContinuingFragment = 0x01,
709   kFirstFlushable     = 0x02,
710   kCompletePDU        = 0x03,
711 };
712 
713 // The Broadcast Flag is contained in bits 6 and 7 in the second octet of a HCI
714 // ACL Data packet.
715 enum class ACLBroadcastFlag : uint8_t {
716   kPointToPoint              = 0x00,
717   kActivePeripheralBroadcast = 0x01,
718 };
719 
720 // The Packet Status Flag is contained in bits 4 to 5 of the second octet of
721 // a HCI Synchronous Data packet (Core Spec v5.2, Vol 4, Part E, Sec 5.4.3).
722 enum class SynchronousDataPacketStatusFlag : uint8_t {
723   kCorrectlyReceived  = 0x00,
724   kPossiblyInvalid  = 0x01,
725   kNoDataReceived  = 0x02,
726   kDataPartiallyLost = 0x03,
727 };
728 
729 // Possible values that can be reported in a LE Channel Selection Algorithm event.
730 enum class LEChannelSelectionAlgorithm : uint8_t {
731   kAlgorithm1 = 0x00,
732   kAlgorithm2 = 0x01,
733 };
734 
735 // "Hosts and Controllers shall be able to accept HCI ACL Data Packets with up
736 // to 27 bytes of data excluding the HCI ACL Data Packet header on Connection
737 // Handles associated with an LE-U logical link." (See Core Spec v5.0, Volume 2,
738 // Part E, Section 5.4.2)
739 constexpr size_t kMinLEACLDataBufferLength = 27;
740 
741 // The maximum value that can be used for a 12-bit connection handle.
742 constexpr uint16_t kConnectionHandleMax = 0x0EFF;
743 
744 // The maximum value that can ve used for a 8-bit advertising set handle.
745 constexpr uint8_t kAdvertisingHandleMax = 0xEF;
746 
747 // The maximum value that can be set for the length of an Inquiry
748 constexpr uint8_t kInquiryLengthMax = 0x30;
749 
750 // Bit 15, or "Clock_Offset_Valid_Flag" of the 16-bit clock offset field.
751 // Some HCI commands that require a clock offset expect this bit to be set (e.g.
752 // see HCI_Remote_Name_Request command, Vol 2, Part E, 7.1.19).
753 constexpr uint16_t kClockOffsetValidFlagBit = 0x8000;
754 
755 // Masks the lower 15 bits of a Clock_Offset, excluding the bit 15 - the reserved/validity bit.
756 constexpr uint16_t kClockOffsetMask = 0x7FFF;
757 
758 // Bitmask Values for the Scan_Enable parameter in a
759 // HCI_(Read,Write)_Scan_Enable command.
760 enum class ScanEnableBit : uint8_t {
761   kInquiry  = (1 << 0), // Inquiry scan enabled
762   kPage     = (1 << 1), // Page scan enabled.
763 };
764 
765 using ScanEnableType = uint8_t;
766 
767 // Constant values for common scanning modes
768 // See Spec 5.0, Vol 3, Part C, Section 4.2.2.1, Table 4.2
769 constexpr uint16_t kPageScanR0Interval = 0x0800; // 1.28s
770 constexpr uint16_t kPageScanR0Window = 0x0800; // 1.28s
771 constexpr uint16_t kPageScanR1Interval = 0x0800; // 1.28s
772 constexpr uint16_t kPageScanR1Window = 0x0011; // 10.625ms
773 constexpr uint16_t kPageScanR2Interval = 0x1000; // 2.56s
774 constexpr uint16_t kPageScanR2Window = 0x0011; // 10.625ms
775 
776 enum class InquiryScanType : uint8_t {
777   kStandardScan = 0x00, // Standard scan (default) (mandatory)
778   kInterlacedScan = 0x01, // Interlaced scan
779 };
780 
781 // Link Types for BR/EDR connections.
782 enum class LinkType : uint8_t {
783   kSCO = 0x00,         // SCO
784   kACL = 0x01,         // ACL (data channel)
785   kExtendedSCO = 0x02, // eSCO
786 };
787 
788 // Length of the Extended Inquiry Response data. (Vol 3, Part C, Section 8)
789 constexpr size_t kExtendedInquiryResponseBytes = 240;
790 
791 // Maximum length of a local name in the Extended Inquiry Response data.
792 // Length: 1 byte, DataType: 1 byte, Remaining buffer: 238 bytes.
793 // (Vol 3, Part C, Section 8)
794 constexpr size_t kExtendedInquiryResponseMaxNameBytes = kExtendedInquiryResponseBytes - 2;
795 
796 // Minimum supported encryption key size for ACL-U links, as queried by Read
797 // Encryption Key Size. This isn't specified so the value is taken from the LE
798 // limit for SM Long Term Keys (v5.0 Vol 3, Part H, 2.3.4). This limit applies
799 // to the per-session encryption key, not the semi-permanent Link Key (v5.0
800 // Vol 2, Part H, 1).
801 constexpr uint8_t kMinEncryptionKeySize = 7;
802 
803 // inclusive-language: ignore
804 // Ignore inclusive language check to match the language used in the spec
805 //
806 // Key types for BR/EDR link encryption as reported to the host using the Link
807 // Key Notification event upon pairing or key changes (v5.0 Vol 2, Part E,
808 // Section 7.7.24).
809 //
810 // "Combination" refers to keys created from contributions of two devices
811 // according to v5.0 Vol 2, Part H, Section 3.2.4 and as opposed to "unit" keys
812 // that are generated on a single device but used by both parties (Section 3.2.3
813 // and deprecated in Section 3.1).
814 //
815 // Authenticated keys were generated using a challenge-response scheme described
816 // inclusive-language: ignore
817 // in v5.0 Vol 2, Part H, Section 5 to protect against man-in-the-middle (MITM)
818 // attacks.
819 //
820 // When Secure Connections is used, the key exchange uses keys generated from
821 // points on a 256-bit elliptic curve (v5.0 Vol 2, Part H, Section 7.1) and
822 // authentication uses Secure Authentication procedures described in Section 5.
823 enum class LinkKeyType : uint8_t {
824   // Legacy pairing (pre-v2.1) key types
825   kCombination = 0x00,
826   kLocalUnit = 0x01,
827   kRemoteUnit = 0x02,
828 
829   // Secure Simple Pairing key types
830   kDebugCombination = 0x03,
831   kUnauthenticatedCombination192 = 0x04,
832   kAuthenticatedCombination192 = 0x05,
833 
834   // Special value indicating key generated due to a Change Connection Link Key
835   // command. The actual key type is the same as before the change.
836   kChangedCombination = 0x06,
837 
838   // Secure Simple Pairing with Secure Connections key types
839   kUnauthenticatedCombination256 = 0x07,
840   kAuthenticatedCombination256 = 0x08,
841 };
842 
843 // Bitmask values for supported Packet Types
844 // Used for HCI_Create_Connection and HCI_Change_Connection_Packet_Type
845 enum class PacketTypeBits : uint16_t {
846   // Reserved (1 << 0)
847   kDisable2DH1 = (1 << 1),
848   kDisable3DH1 = (1 << 2),
849   kEnableDM1 = (1 << 3), // Note: always on in >= v1.2
850   kEnableDH1 = (1 << 4),
851   // Reserved (1 << 5)
852   // Reserved (1 << 6)
853   // Reserved (1 << 7)
854   kDisable2DH3 = (1 << 8),
855   kDisable3DH3 = (1 << 9),
856   kEnableDM3 = (1 << 10),
857   kEnableDH3 = (1 << 11),
858   kDisable2DH5 = (1 << 12),
859   kDisable3DH5 = (1 << 13),
860   kEnableDM5 = (1 << 14),
861   kEnableDH5 = (1 << 15),
862 };
863 
864 using PacketTypeType = uint16_t;
865 
866 enum class RoleSwitchBits : uint8_t {
867   kDisallowRoleSwitch = 0x0,
868   kAllowRoleSwitch = 0x1
869 };
870 
871 enum class ScoRetransmissionEffort : uint8_t {
872   // SCO or eSCO
873   kNone = 0x00,
874 
875   // eSCO only
876   kPowerOptimized  = 0x01,
877 
878   // eSCO only
879   kQualityOptimized  = 0x02,
880 
881   // SCO or eSCO
882   kDontCare = 0xFF,
883 };
884 
885 // Flush Timeout = N * 0.625ms (Core Spec v5.2, Vol 4, Part E, Sec 7.3.30).
886 constexpr float kFlushTimeoutCommandParameterToMillisecondsConversionFactor = 0.625f;
887 constexpr float kFlushTimeoutMsToCommandParameterConversionFactor = 1.0f / kFlushTimeoutCommandParameterToMillisecondsConversionFactor;
888 
889 // See Core Spec v5.2, Vol 4, Part E, Sec 7.3.30
890 constexpr uint16_t kMaxAutomaticFlushTimeoutCommandParameterValue = 0x07FF;
891 constexpr pw::chrono::SystemClock::duration kMaxAutomaticFlushTimeoutDuration = std::chrono::milliseconds(static_cast<int64_t>(kMaxAutomaticFlushTimeoutCommandParameterValue * kFlushTimeoutCommandParameterToMillisecondsConversionFactor));
892 
893 // Page Timeout = N * 0.625 ms (Core Spec v5.2, Vol 4, Part E, Sec 7.3.16).
894 // The default is 5.12 sec.
895 constexpr pw::chrono::SystemClock::duration kDurationPerPageTimeoutUnit = std::chrono::duration_cast<pw::chrono::SystemClock::duration>(std::chrono::microseconds(625));
896 constexpr pw::chrono::SystemClock::duration kMinPageTimeoutDuration = kDurationPerPageTimeoutUnit * static_cast<uint16_t>(pw::bluetooth::emboss::PageTimeout::MIN);
897 constexpr pw::chrono::SystemClock::duration kDefaultPageTimeoutDuration = kDurationPerPageTimeoutUnit * static_cast<uint16_t>(pw::bluetooth::emboss::PageTimeout::DEFAULT);
898 constexpr pw::chrono::SystemClock::duration kMaxPageTimeoutDuration = kDurationPerPageTimeoutUnit * static_cast<uint16_t>(pw::bluetooth::emboss::PageTimeout::MAX);
899 
900 }  // namespace bt::hci_spec
901