This module encapsulates BTstack’s Security Manager (SM) configuration and event handling. It provides a structured C++ interface for pairing, authentication, and authorization workflows.
Core Classes
Design Principles
- Explicit configuration: Security settings must be applied before connections are established.
- Validation: Configuration is checked to ensure it can satisfy GATT database requirements.
- Event‑driven: The application decides how to respond to pairing prompts.
Assumed Usage Scenarios
- Devices that require encryption or authenticated access to specific characteristics.
- Devices that need bonding or Secure Connections policies.
Typical Flow
sm->Configure(params);
@ kMitmProtection
Require MITM protection.
@ kDisplayYesNo
Device can display and confirm yes/no.
static SecurityManager * GetInstance()
Get the singleton instance.
Cached security configuration parameters.
Definition security_manager.hpp:284
IoCapability io_capability
IO capability used to select pairing method.
Definition security_manager.hpp:288
uint8_t min_encryption_key_size
Minimum encryption key size (7..16).
Definition security_manager.hpp:296
uint8_t max_encryption_key_size
Maximum encryption key size (7..16).
Definition security_manager.hpp:300
AuthenticationRequirement authentication
Authentication requirement bitfield.
Definition security_manager.hpp:292
Key Notes
- Security configuration should match GATT permissions (READ/WRITE + READ_AUTHENTICATED/WRITE_AUTHENTICATED).
- Authorization decisions are tracked per connection and used by
AttributeServer for descriptor writes.
Configuration Validation
SecurityManager provides a validation step to ensure the requested security level is achievable with the current IO capability and pairing configuration. Use ValidateConfiguration(authentication_required, authorization_required, encryption_required) to check:
- Whether MITM protection can be satisfied given the selected IO capability.
- Whether Secure Connections requirements are compatible with the current configuration.
- Whether encryption and key size constraints can be met.
This is intended to be called before connections are established, so the application can fail fast or adjust configuration before clients connect.
Interaction with AttributeServer
The Security Manager and AttributeServer cooperate at runtime to enforce GATT permissions:
- SecurityManager configures pairing policy (IO capability, MITM, SC, bonding) and handles pairing/authorization events.
- AttributeServer enforces the required security level for attribute access, especially for descriptor writes (CCCD/SCCD).
Shared Security State
At runtime, the platform glue updates the AttributeServer with the current security state derived from SM/GAP events:
- Connection handle (
SetConnectionHandle) is stored on the AttributeServer and propagated to characteristics.
- Security level (
SetSecurityLevel) reflects encryption/authentication state.
- Authorization status (
SetAuthorizationGranted) records application‑level authorization decisions.
This cached state is used by Characteristic descriptor handlers (e.g., CCCD/SCCD writes) to accept or reject operations based on the configured security requirements.
Practical Flow
- SecurityManager configures authentication requirements before connections.
- When pairing completes or security level changes, the platform layer updates AttributeServer.
- AttributeServer uses the cached security level to validate reads/writes against GATT permissions.
Event Handling in Detail
How to Register
- Implement
c7222::SecurityManager::EventHandler and override the callbacks you need.
- Register with
c7222::SecurityManager::AddEventHandler(handler) before pairing can be triggered (typically before c7222::Ble::TurnOn()).
- Handlers are stored as raw pointers. The handler object must outlive the
SecurityManager singleton.
When Handlers Are Called
Handlers are invoked when SM‑related HCI events are dispatched into the stack (via c7222::Ble::DispatchBleHciPacket()):
- Just Works confirmation requests
- Numeric comparison requests
- Passkey display/input requests
- Pairing complete events
- Re‑encryption complete events
- Authorization requests and results
If the platform layer does not forward HCI events, handlers will never be called.
What Type of Objects Are Passed
c7222::SecurityManager::EventHandler callbacks receive strongly‑typed values such as:
These values are only guaranteed to be valid during the callback.
Multiple Handlers
- You can register more than one handler with
c7222::SecurityManager::AddEventHandler().
- Internally, the module stores a list of handler pointers and iterates through them in registration order.
- All registered handlers receive the event; no handler short‑circuits the others.
- You can remove a handler with
RemoveEventHandler() if needed.
Typical Pattern
public:
}
(void)passkey;
}
};
static MySmHandler handler;
sm->AddEventHandler(handler);
BleError ConfirmJustWorks(ConnectionHandle con_handle)
Confirm a "Just Works" pairing request.
uint16_t ConnectionHandle
Definition gap.hpp:23
Security Manager event callback interface.
Definition security_manager.hpp:331
virtual void OnPasskeyDisplay(ConnectionHandle connection_handle, uint32_t passkey) const
Called when the stack requests passkey display.
Definition security_manager.hpp:349
virtual void OnJustWorksRequest(ConnectionHandle connection_handle) const
Called when "Just Works" confirmation is requested.
Definition security_manager.hpp:336