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