[notifier] simplify 'EventToString()' (#5657)

This commit changes `EventToString()` to use an array instead of
`switch`. This helps with code size due to enumerator cases being
`uint32_t` bitmask values and therefore compliers may not be able to
optimize the `switch` statement as a table.
This commit is contained in:
Abtin Keshavarzian
2020-10-19 10:25:07 -07:00
committed by Jonathan Hui
parent 7b570c7fc1
commit 80d4cb01f7
2 changed files with 39 additions and 121 deletions
+38 -120
View File
@@ -239,128 +239,46 @@ const char *Notifier::EventToString(Event aEvent) const
// To ensure no clipping of flag names in the logs, the returned
// strings from this method should have shorter length than
// `kMaxFlagNameLength` value.
static const char *kEventStrings[] = {
"Ip6+", // kEventIp6AddressAdded (1 << 0)
"Ip6-", // kEventIp6AddressRemoved (1 << 1)
"Role", // kEventThreadRoleChanged (1 << 2)
"LLAddr", // kEventThreadLinkLocalAddrChanged (1 << 3)
"MLAddr", // kEventThreadMeshLocalAddrChanged (1 << 4)
"Rloc+", // kEventThreadRlocAdded (1 << 5)
"Rloc-", // kEventThreadRlocRemoved (1 << 6)
"PartitionId", // kEventThreadPartitionIdChanged (1 << 7)
"KeySeqCntr", // kEventThreadKeySeqCounterChanged (1 << 8)
"NetData", // kEventThreadNetdataChanged (1 << 9)
"Child+", // kEventThreadChildAdded (1 << 10)
"Child-", // kEventThreadChildRemoved (1 << 11)
"Ip6Mult+", // kEventIp6MulticastSubscribed (1 << 12)
"Ip6Mult-", // kEventIp6MulticastUnsubscribed (1 << 13)
"Channel", // kEventThreadChannelChanged (1 << 14)
"PanId", // kEventThreadPanIdChanged (1 << 15)
"NetName", // kEventThreadNetworkNameChanged (1 << 16)
"ExtPanId", // kEventThreadExtPanIdChanged (1 << 17)
"MstrKey", // kEventMasterKeyChanged (1 << 18)
"PSKc", // kEventPskcChanged (1 << 19)
"SecPolicy", // kEventSecurityPolicyChanged (1 << 20)
"CMNewChan", // kEventChannelManagerNewChannelChanged (1 << 21)
"ChanMask", // kEventSupportedChannelMaskChanged (1 << 22)
"CommissionerState", // kEventCommissionerStateChanged (1 << 23)
"NetifState", // kEventThreadNetifStateChanged (1 << 24)
"BbrState", // kEventThreadBackboneRouterStateChanged (1 << 25)
"BbrLocal", // kEventThreadBackboneRouterLocalChanged (1 << 26)
"JoinerState", // kEventJoinerStateChanged (1 << 27)
"ActDset", // kEventActiveDatasetChanged (1 << 28)
"PndDset", // kEventPendingDatasetChanged (1 << 29)
};
switch (aEvent)
for (uint8_t index = 0; index < OT_ARRAY_LENGTH(kEventStrings); index++)
{
case kEventIp6AddressAdded:
retval = "Ip6+";
break;
case kEventIp6AddressRemoved:
retval = "Ip6-";
break;
case kEventThreadRoleChanged:
retval = "Role";
break;
case kEventThreadLinkLocalAddrChanged:
retval = "LLAddr";
break;
case kEventThreadMeshLocalAddrChanged:
retval = "MLAddr";
break;
case kEventThreadRlocAdded:
retval = "Rloc+";
break;
case kEventThreadRlocRemoved:
retval = "Rloc-";
break;
case kEventThreadPartitionIdChanged:
retval = "PartitionId";
break;
case kEventThreadKeySeqCounterChanged:
retval = "KeySeqCntr";
break;
case kEventThreadNetdataChanged:
retval = "NetData";
break;
case kEventThreadChildAdded:
retval = "Child+";
break;
case kEventThreadChildRemoved:
retval = "Child-";
break;
case kEventIp6MulticastSubscribed:
retval = "Ip6Mult+";
break;
case kEventIp6MulticastUnsubscribed:
retval = "Ip6Mult-";
break;
case kEventThreadChannelChanged:
retval = "Channel";
break;
case kEventThreadPanIdChanged:
retval = "PanId";
break;
case kEventThreadNetworkNameChanged:
retval = "NetName";
break;
case kEventThreadExtPanIdChanged:
retval = "ExtPanId";
break;
case kEventMasterKeyChanged:
retval = "MstrKey";
break;
case kEventPskcChanged:
retval = "PSKc";
break;
case kEventSecurityPolicyChanged:
retval = "SecPolicy";
break;
case kEventChannelManagerNewChannelChanged:
retval = "CMNewChan";
break;
case kEventSupportedChannelMaskChanged:
retval = "ChanMask";
break;
case kEventCommissionerStateChanged:
retval = "CommissionerState";
break;
case kEventThreadNetifStateChanged:
retval = "NetifState";
break;
case kEventThreadBackboneRouterStateChanged:
retval = "BbrState";
break;
case kEventThreadBackboneRouterLocalChanged:
retval = "BbrLocal";
break;
case kEventJoinerStateChanged:
retval = "JoinerState";
break;
case kEventActiveDatasetChanged:
retval = "ActDset";
break;
case kEventPendingDatasetChanged:
retval = "PndDset";
break;
if (static_cast<uint32_t>(aEvent) == (1U << index))
{
retval = kEventStrings[index];
break;
}
}
return retval;