[mac] move key material update to Init() (#12590)

This commit introduces an `Init()` method to the `Mac` class and
moves the invocation of `KeyManager::UpdateKeyMaterial()` from the
`Mac` constructor into this new method. `Mac::Init()` is then called
from `Instance::AfterInit()`, immediately after `KeyManager` is
initialized.

This ensures that `KeyManager` and other OpenThread core components
are fully constructed and properly initialized before attempting to
update the key material. The key material update interacts with
`SubMac` to configure the MAC keys. Performing this operation during
the `Mac` constructor phase can be problematic because the `KeyManager`
(under `OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE`) may not be
fully initialized and ready yet.
This commit is contained in:
Abtin Keshavarzian
2026-03-02 13:30:30 -06:00
committed by GitHub
parent 07e79ba539
commit 9682126aab
3 changed files with 11 additions and 1 deletions
+1
View File
@@ -433,6 +433,7 @@ void Instance::AfterInit(void)
#if OPENTHREAD_MTD || OPENTHREAD_FTD
Get<KeyManager>().Init();
Get<Mac::Mac>().Init();
// Restore datasets and network information
+2 -1
View File
@@ -115,7 +115,6 @@ Mac::Mac(Instance &aInstance)
SetEnabled(true);
Get<KeyManager>().UpdateKeyMaterial();
SetPanId(mPanId);
SetExtAddress(randomExtAddress);
SetShortAddress(GetShortAddress());
@@ -126,6 +125,8 @@ Mac::Mac(Instance &aInstance)
mMode2KeyMaterial.SetFrom(AsCoreType(&sMode2Key));
}
void Mac::Init(void) { Get<KeyManager>().UpdateKeyMaterial(); }
void Mac::SetEnabled(bool aEnable)
{
mEnabled = aEnable;
+8
View File
@@ -130,6 +130,14 @@ public:
*/
~Mac(void) { ClearMode2Key(); }
/**
* Initializes the `Mac`.
*
* This method MUST be called after OpenThread `Instance` is fully initialized (from `Instance::AfterInit()`) and
* only after `KeyManager` is also fully initialized.
*/
void Init(void);
/**
* Starts an IEEE 802.15.4 Active Scan.
*