From aa3d8d2ddb761c1a4ea65d1ab71841d5cf24ca28 Mon Sep 17 00:00:00 2001 From: Matt Swartwout <mwswartwout@google.com> Date: Wed, 10 Apr 2024 14:58:42 -0700 Subject: [PATCH 01/10] Add std::source_location to ChipError for C++20 builds --- src/lib/core/CHIPError.h | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/lib/core/CHIPError.h b/src/lib/core/CHIPError.h index 38b76bca539c2a..728ce4d7e16bf4 100644 --- a/src/lib/core/CHIPError.h +++ b/src/lib/core/CHIPError.h @@ -35,6 +35,10 @@ #include <limits> #include <type_traits> +#if __cplusplus >= 202002L +#include <source_location> +#endif // __cplusplus >= 202002L + namespace chip { /** @@ -112,7 +116,11 @@ class ChipError // Helper for declaring constructors without too much repetition. #if CHIP_CONFIG_ERROR_SOURCE +#if __cplusplus >= 202002L +#define CHIP_INITIALIZE_ERROR_SOURCE(f, l, loc) , mFile((f)), mLine((l)), mSourceLocation((loc)) +#else #define CHIP_INITIALIZE_ERROR_SOURCE(f, l) , mFile((f)), mLine((l)) +#endif // __cplusplus >= 202002L #else // CHIP_CONFIG_ERROR_SOURCE #define CHIP_INITIALIZE_ERROR_SOURCE(f, l) #endif // CHIP_CONFIG_ERROR_SOURCE @@ -123,12 +131,21 @@ class ChipError * @note * The result is valid only if CanEncapsulate() is true. */ +#if __cplusplus >= 202002L + constexpr ChipError(Range range, ValueType value) : + mError(MakeInteger(range, (value & MakeMask(0, kValueLength)))) CHIP_INITIALIZE_ERROR_SOURCE(nullptr, 0, std::source_location()) + {} + constexpr ChipError(Range range, ValueType value, const char * file, unsigned int line, std::source_location location = std::source_location::current()) : + mError(MakeInteger(range, (value & MakeMask(0, kValueLength)))) CHIP_INITIALIZE_ERROR_SOURCE(file, line, location) + {} +#else constexpr ChipError(Range range, ValueType value) : mError(MakeInteger(range, (value & MakeMask(0, kValueLength)))) CHIP_INITIALIZE_ERROR_SOURCE(nullptr, 0) {} constexpr ChipError(Range range, ValueType value, const char * file, unsigned int line) : mError(MakeInteger(range, (value & MakeMask(0, kValueLength)))) CHIP_INITIALIZE_ERROR_SOURCE(file, line) {} +#endif // __cplusplus >= 202002L /** * Construct a CHIP_ERROR for SdkPart @a part with @a code. @@ -136,10 +153,17 @@ class ChipError * @note * The macro version CHIP_SDK_ERROR checks that the numeric value is constant and well-formed. */ +#if __cplusplus >= 202002L + constexpr ChipError(SdkPart part, uint8_t code) : mError(MakeInteger(part, code)) CHIP_INITIALIZE_ERROR_SOURCE(nullptr, 0, std::source_location()) {} + constexpr ChipError(SdkPart part, uint8_t code, const char * file, unsigned int line, std::source_location location = std::source_location::current()) : + mError(MakeInteger(part, code)) CHIP_INITIALIZE_ERROR_SOURCE(file, line, location) + {} +#else constexpr ChipError(SdkPart part, uint8_t code) : mError(MakeInteger(part, code)) CHIP_INITIALIZE_ERROR_SOURCE(nullptr, 0) {} constexpr ChipError(SdkPart part, uint8_t code, const char * file, unsigned int line) : mError(MakeInteger(part, code)) CHIP_INITIALIZE_ERROR_SOURCE(file, line) {} +#endif // __cplusplus >= 202002L /** * Construct a CHIP_ERROR constant for SdkPart @a part with @a code at the current source line. @@ -159,10 +183,17 @@ class ChipError * @note * This is intended to be used only in foreign function interfaces. */ +#if __cplusplus >= 202002L + explicit constexpr ChipError(StorageType error) : mError(error) CHIP_INITIALIZE_ERROR_SOURCE(nullptr, 0, std::source_location()) {} + explicit constexpr ChipError(StorageType error, const char * file, unsigned int line, std::source_location location = std::source_location::current()) : + mError(error) CHIP_INITIALIZE_ERROR_SOURCE(file, line, location) + {} +#else explicit constexpr ChipError(StorageType error) : mError(error) CHIP_INITIALIZE_ERROR_SOURCE(nullptr, 0) {} explicit constexpr ChipError(StorageType error, const char * file, unsigned int line) : mError(error) CHIP_INITIALIZE_ERROR_SOURCE(file, line) {} +#endif // __cplusplus >= 202002L #undef CHIP_INITIALIZE_ERROR_SOURCE @@ -365,6 +396,9 @@ class ChipError #if CHIP_CONFIG_ERROR_SOURCE const char * mFile; unsigned int mLine; +#if __cplusplus >= 202002L + std::source_location mSourceLocation; +#endif // __cplusplus >= 202002L #endif // CHIP_CONFIG_ERROR_SOURCE public: From b9feff9047851f0d6d54092fdf7ec6a14d33c8ad Mon Sep 17 00:00:00 2001 From: Matt Swartwout <mwswartwout@google.com> Date: Wed, 17 Apr 2024 21:55:42 +0000 Subject: [PATCH 02/10] Fix errors due to CHIP_ERROR default constructor no longer being trivial --- src/app/FailSafeContext.cpp | 11 ++- src/app/clusters/bindings/bindings.cpp | 5 +- src/include/platform/CHIPDeviceEvent.h | 1 - .../GenericConnectivityManagerImpl_Thread.ipp | 13 ++- src/lib/core/CHIPError.h | 34 +++++--- src/platform/DeviceControlServer.cpp | 24 +++--- src/platform/Linux/BLEManagerImpl.cpp | 82 ++++++++----------- .../Linux/ConnectivityManagerImpl.cpp | 8 +- src/platform/Linux/PlatformManagerImpl.cpp | 7 +- src/platform/Linux/ThreadStackManagerImpl.cpp | 18 ++-- src/platform/PlatformEventSupport.cpp | 4 +- 11 files changed, 90 insertions(+), 117 deletions(-) diff --git a/src/app/FailSafeContext.cpp b/src/app/FailSafeContext.cpp index 699fcb8c0ce280..95a5b267f2aa5e 100644 --- a/src/app/FailSafeContext.cpp +++ b/src/app/FailSafeContext.cpp @@ -85,12 +85,11 @@ void FailSafeContext::ScheduleFailSafeCleanup(FabricIndex fabricIndex, bool addN SetFailSafeArmed(false); - ChipDeviceEvent event; - event.Type = DeviceEventType::kFailSafeTimerExpired; - event.FailSafeTimerExpired.fabricIndex = fabricIndex; - event.FailSafeTimerExpired.addNocCommandHasBeenInvoked = addNocCommandInvoked; - event.FailSafeTimerExpired.updateNocCommandHasBeenInvoked = updateNocCommandInvoked; - CHIP_ERROR status = PlatformMgr().PostEvent(&event); + ChipDeviceEvent event{ .Type = DeviceEventType::kFailSafeTimerExpired, + .FailSafeTimerExpired = { .fabricIndex = fabricIndex, + .addNocCommandHasBeenInvoked = addNocCommandInvoked, + .updateNocCommandHasBeenInvoked = updateNocCommandInvoked } }; + CHIP_ERROR status = PlatformMgr().PostEvent(&event); if (status != CHIP_NO_ERROR) { diff --git a/src/app/clusters/bindings/bindings.cpp b/src/app/clusters/bindings/bindings.cpp index 63b085d7ee6de4..8404dee7a7899f 100644 --- a/src/app/clusters/bindings/bindings.cpp +++ b/src/app/clusters/bindings/bindings.cpp @@ -258,9 +258,8 @@ CHIP_ERROR BindingTableAccess::WriteBindingTable(const ConcreteDataAttributePath CHIP_ERROR BindingTableAccess::NotifyBindingsChanged() { - DeviceLayer::ChipDeviceEvent event; - event.Type = DeviceLayer::DeviceEventType::kBindingsChangedViaCluster; - event.BindingsChanged.fabricIndex = mAccessingFabricIndex; + DeviceLayer::ChipDeviceEvent event{ .Type = DeviceLayer::DeviceEventType::kBindingsChangedViaCluster, + .BindingsChanged = { .fabricIndex = mAccessingFabricIndex } }; return chip::DeviceLayer::PlatformMgr().PostEvent(&event); } diff --git a/src/include/platform/CHIPDeviceEvent.h b/src/include/platform/CHIPDeviceEvent.h index dae127ceba1f06..729937e1c07fc0 100644 --- a/src/include/platform/CHIPDeviceEvent.h +++ b/src/include/platform/CHIPDeviceEvent.h @@ -530,7 +530,6 @@ struct ChipDeviceEvent final } OtaStateChanged; }; - void Clear() { memset(this, 0, sizeof(*this)); } bool IsPublic() const { return DeviceEventType::IsPublic(Type); } bool IsInternal() const { return DeviceEventType::IsInternal(Type); } bool IsPlatformSpecific() const { return DeviceEventType::IsPlatformSpecific(Type); } diff --git a/src/include/platform/internal/GenericConnectivityManagerImpl_Thread.ipp b/src/include/platform/internal/GenericConnectivityManagerImpl_Thread.ipp index 403ddd643b7193..b2ee27a424c730 100644 --- a/src/include/platform/internal/GenericConnectivityManagerImpl_Thread.ipp +++ b/src/include/platform/internal/GenericConnectivityManagerImpl_Thread.ipp @@ -70,13 +70,12 @@ void GenericConnectivityManagerImpl_Thread<ImplClass>::UpdateServiceConnectivity mFlags.Set(Flags::kHaveServiceConnectivity, haveServiceConnectivity); { - ChipDeviceEvent event; - event.Clear(); - event.Type = DeviceEventType::kServiceConnectivityChange; - event.ServiceConnectivityChange.ViaThread.Result = - (haveServiceConnectivity) ? kConnectivity_Established : kConnectivity_Lost; - event.ServiceConnectivityChange.Overall.Result = event.ServiceConnectivityChange.ViaThread.Result; - CHIP_ERROR status = PlatformMgr().PostEvent(&event); + ChipDeviceEvent event{ .Type = DeviceEventType::kServiceConnectivityChange, + .ServiceConnectivityChange = { + .Overall = { .Result = event.ServiceConnectivityChange.ViaThread.Result }, + .ViaThread = { .Result = (haveServiceConnectivity) ? kConnectivity_Established + : kConnectivity_Lost } } }; + CHIP_ERROR status = PlatformMgr().PostEvent(&event); if (status != CHIP_NO_ERROR) { ChipLogError(DeviceLayer, "Failed to post thread connectivity change: %" CHIP_ERROR_FORMAT, status.Format()); diff --git a/src/lib/core/CHIPError.h b/src/lib/core/CHIPError.h index 728ce4d7e16bf4..65a23a0765f77b 100644 --- a/src/lib/core/CHIPError.h +++ b/src/lib/core/CHIPError.h @@ -37,7 +37,7 @@ #if __cplusplus >= 202002L #include <source_location> -#endif // __cplusplus >= 202002L +#endif // __cplusplus >= 202002L namespace chip { @@ -120,8 +120,8 @@ class ChipError #define CHIP_INITIALIZE_ERROR_SOURCE(f, l, loc) , mFile((f)), mLine((l)), mSourceLocation((loc)) #else #define CHIP_INITIALIZE_ERROR_SOURCE(f, l) , mFile((f)), mLine((l)) -#endif // __cplusplus >= 202002L -#else // CHIP_CONFIG_ERROR_SOURCE +#endif // __cplusplus >= 202002L +#else // CHIP_CONFIG_ERROR_SOURCE #define CHIP_INITIALIZE_ERROR_SOURCE(f, l) #endif // CHIP_CONFIG_ERROR_SOURCE @@ -133,9 +133,11 @@ class ChipError */ #if __cplusplus >= 202002L constexpr ChipError(Range range, ValueType value) : - mError(MakeInteger(range, (value & MakeMask(0, kValueLength)))) CHIP_INITIALIZE_ERROR_SOURCE(nullptr, 0, std::source_location()) + mError(MakeInteger(range, (value & MakeMask(0, kValueLength)))) + CHIP_INITIALIZE_ERROR_SOURCE(nullptr, 0, std::source_location()) {} - constexpr ChipError(Range range, ValueType value, const char * file, unsigned int line, std::source_location location = std::source_location::current()) : + constexpr ChipError(Range range, ValueType value, const char * file, unsigned int line, + std::source_location location = std::source_location::current()) : mError(MakeInteger(range, (value & MakeMask(0, kValueLength)))) CHIP_INITIALIZE_ERROR_SOURCE(file, line, location) {} #else @@ -154,15 +156,17 @@ class ChipError * The macro version CHIP_SDK_ERROR checks that the numeric value is constant and well-formed. */ #if __cplusplus >= 202002L - constexpr ChipError(SdkPart part, uint8_t code) : mError(MakeInteger(part, code)) CHIP_INITIALIZE_ERROR_SOURCE(nullptr, 0, std::source_location()) {} - constexpr ChipError(SdkPart part, uint8_t code, const char * file, unsigned int line, std::source_location location = std::source_location::current()) : + constexpr ChipError(SdkPart part, uint8_t code) : + mError(MakeInteger(part, code)) CHIP_INITIALIZE_ERROR_SOURCE(nullptr, 0, std::source_location()) + {} + constexpr ChipError(SdkPart part, uint8_t code, const char * file, unsigned int line, + std::source_location location = std::source_location::current()) : mError(MakeInteger(part, code)) CHIP_INITIALIZE_ERROR_SOURCE(file, line, location) {} #else constexpr ChipError(SdkPart part, uint8_t code) : mError(MakeInteger(part, code)) CHIP_INITIALIZE_ERROR_SOURCE(nullptr, 0) {} constexpr ChipError(SdkPart part, uint8_t code, const char * file, unsigned int line) : - mError(MakeInteger(part, code)) CHIP_INITIALIZE_ERROR_SOURCE(file, line) - {} + mError(MakeInteger(part, code)) CHIP_INITIALIZE_ERROR_SOURCE(file, line){} #endif // __cplusplus >= 202002L /** @@ -184,12 +188,16 @@ class ChipError * This is intended to be used only in foreign function interfaces. */ #if __cplusplus >= 202002L - explicit constexpr ChipError(StorageType error) : mError(error) CHIP_INITIALIZE_ERROR_SOURCE(nullptr, 0, std::source_location()) {} - explicit constexpr ChipError(StorageType error, const char * file, unsigned int line, std::source_location location = std::source_location::current()) : + explicit constexpr ChipError(StorageType error) : mError(error) CHIP_INITIALIZE_ERROR_SOURCE(nullptr, 0, std::source_location()) + {} + explicit constexpr ChipError(StorageType error, const char * file, unsigned int line, + std::source_location location = std::source_location::current()) : mError(error) CHIP_INITIALIZE_ERROR_SOURCE(file, line, location) {} #else - explicit constexpr ChipError(StorageType error) : mError(error) CHIP_INITIALIZE_ERROR_SOURCE(nullptr, 0) {} + explicit constexpr ChipError(StorageType error) : mError(error) CHIP_INITIALIZE_ERROR_SOURCE(nullptr, 0) + { + } explicit constexpr ChipError(StorageType error, const char * file, unsigned int line) : mError(error) CHIP_INITIALIZE_ERROR_SOURCE(file, line) {} @@ -398,7 +406,7 @@ class ChipError unsigned int mLine; #if __cplusplus >= 202002L std::source_location mSourceLocation; -#endif // __cplusplus >= 202002L +#endif // __cplusplus >= 202002L #endif // CHIP_CONFIG_ERROR_SOURCE public: diff --git a/src/platform/DeviceControlServer.cpp b/src/platform/DeviceControlServer.cpp index ec348669aec213..584ddbc08f056e 100644 --- a/src/platform/DeviceControlServer.cpp +++ b/src/platform/DeviceControlServer.cpp @@ -36,11 +36,11 @@ DeviceControlServer & DeviceControlServer::DeviceControlSvr() CHIP_ERROR DeviceControlServer::PostCommissioningCompleteEvent(NodeId peerNodeId, FabricIndex accessingFabricIndex) { - ChipDeviceEvent event; + ChipDeviceEvent event{ - event.Type = DeviceEventType::kCommissioningComplete; - event.CommissioningComplete.nodeId = peerNodeId; - event.CommissioningComplete.fabricIndex = accessingFabricIndex; + .Type = DeviceEventType::kCommissioningComplete, + .CommissioningComplete = { .nodeId = peerNodeId, .fabricIndex = accessingFabricIndex } + }; return PlatformMgr().PostEvent(&event); } @@ -66,31 +66,27 @@ CHIP_ERROR DeviceControlServer::SetRegulatoryConfig(uint8_t location, const Char CHIP_ERROR DeviceControlServer::PostConnectedToOperationalNetworkEvent(ByteSpan networkID) { - ChipDeviceEvent event; - event.Type = DeviceEventType::kOperationalNetworkEnabled; - // TODO(cecille): This should be some way to specify thread or wifi. - event.OperationalNetwork.network = 0; + ChipDeviceEvent event{ .Type = DeviceEventType::kOperationalNetworkEnabled, + // TODO(cecille): This should be some way to specify thread or wifi. + .OperationalNetwork = { .network = 0 } }; return PlatformMgr().PostEvent(&event); } CHIP_ERROR DeviceControlServer::PostCloseAllBLEConnectionsToOperationalNetworkEvent() { - ChipDeviceEvent event; - event.Type = DeviceEventType::kCloseAllBleConnections; + ChipDeviceEvent event{ .Type = DeviceEventType::kCloseAllBleConnections }; return PlatformMgr().PostEvent(&event); } CHIP_ERROR DeviceControlServer::PostWiFiDeviceAvailableNetworkEvent() { - ChipDeviceEvent event; - event.Type = DeviceEventType::kWiFiDeviceAvailable; + ChipDeviceEvent event{ .Type = DeviceEventType::kWiFiDeviceAvailable }; return PlatformMgr().PostEvent(&event); } CHIP_ERROR DeviceControlServer::PostOperationalNetworkStartedEvent() { - ChipDeviceEvent event; - event.Type = DeviceEventType::kOperationalNetworkStarted; + ChipDeviceEvent event{ .Type = DeviceEventType::kOperationalNetworkStarted }; return PlatformMgr().PostEvent(&event); } diff --git a/src/platform/Linux/BLEManagerImpl.cpp b/src/platform/Linux/BLEManagerImpl.cpp index 7f0ad2bcd54212..33e1592826bfc6 100644 --- a/src/platform/Linux/BLEManagerImpl.cpp +++ b/src/platform/Linux/BLEManagerImpl.cpp @@ -218,8 +218,7 @@ void BLEManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event) case DeviceEventType::kCHIPoBLESubscribe: HandleSubscribeReceived(event->CHIPoBLESubscribe.ConId, &CHIP_BLE_SVC_ID, &ChipUUID_CHIPoBLEChar_TX); { - ChipDeviceEvent connectionEvent; - connectionEvent.Type = DeviceEventType::kCHIPoBLEConnectionEstablished; + ChipDeviceEvent connectionEvent{ .Type = DeviceEventType::kCHIPoBLEConnectionEstablished }; PlatformMgr().PostEventOrDie(&connectionEvent); } break; @@ -465,9 +464,8 @@ void BLEManagerImpl::HandleNewConnection(BLE_CONNECTION_OBJECT conId) { if (sInstance.mIsCentral) { - ChipDeviceEvent event; - event.Type = DeviceEventType::kPlatformLinuxBLECentralConnected; - event.Platform.BLECentralConnected.mConnection = conId; + ChipDeviceEvent event{ .Type = DeviceEventType::kPlatformLinuxBLECentralConnected, + .Platform = { .BLECentralConnected = { .mConnection = conId } } }; PlatformMgr().PostEventOrDie(&event); } } @@ -476,27 +474,23 @@ void BLEManagerImpl::HandleConnectFailed(CHIP_ERROR error) { if (sInstance.mIsCentral) { - ChipDeviceEvent event; - event.Type = DeviceEventType::kPlatformLinuxBLECentralConnectFailed; - event.Platform.BLECentralConnectFailed.mError = error; + ChipDeviceEvent event{ .Type = DeviceEventType::kPlatformLinuxBLECentralConnectFailed, + .Platform = { .BLECentralConnectFailed = { .mError = error } } }; PlatformMgr().PostEventOrDie(&event); } } void BLEManagerImpl::HandleWriteComplete(BLE_CONNECTION_OBJECT conId) { - ChipDeviceEvent event; - event.Type = DeviceEventType::kPlatformLinuxBLEWriteComplete; - event.Platform.BLEWriteComplete.mConnection = conId; + ChipDeviceEvent event{ .Type = DeviceEventType::kPlatformLinuxBLEWriteComplete, + .Platform = { .BLEWriteComplete = { .mConnection = conId } } }; PlatformMgr().PostEventOrDie(&event); } void BLEManagerImpl::HandleSubscribeOpComplete(BLE_CONNECTION_OBJECT conId, bool subscribed) { - ChipDeviceEvent event; - event.Type = DeviceEventType::kPlatformLinuxBLESubscribeOpComplete; - event.Platform.BLESubscribeOpComplete.mConnection = conId; - event.Platform.BLESubscribeOpComplete.mIsSubscribed = subscribed; + ChipDeviceEvent event{ .Type = DeviceEventType::kPlatformLinuxBLESubscribeOpComplete, + .Platform = { .BLESubscribeOpComplete = { .mConnection = conId, .mIsSubscribed = subscribed } } }; PlatformMgr().PostEventOrDie(&event); } @@ -507,12 +501,12 @@ void BLEManagerImpl::HandleTXCharChanged(BLE_CONNECTION_OBJECT conId, const uint ChipLogDetail(DeviceLayer, "Indication received, conn = %p", conId); + ChipDeviceEvent event{ .Type = DeviceEventType::kPlatformLinuxBLEIndicationReceived, + .Platform = { .BLEIndicationReceived = { .mConnection = conId } } }; + VerifyOrExit(!buf.IsNull(), err = CHIP_ERROR_NO_MEMORY); - ChipDeviceEvent event; - event.Type = DeviceEventType::kPlatformLinuxBLEIndicationReceived; - event.Platform.BLEIndicationReceived.mConnection = conId; - event.Platform.BLEIndicationReceived.mData = std::move(buf).UnsafeRelease(); + event.Platform.BLEIndicationReceived.mData = std::move(buf).UnsafeRelease(); PlatformMgr().PostEventOrDie(&event); exit: @@ -531,11 +525,9 @@ void BLEManagerImpl::HandleRXCharWrite(BLE_CONNECTION_OBJECT conId, const uint8_ // Post an event to the Chip queue to deliver the data into the Chip stack. { - ChipDeviceEvent event; - event.Type = DeviceEventType::kCHIPoBLEWriteReceived; ChipLogProgress(Ble, "Write request received debug %p", conId); - event.CHIPoBLEWriteReceived.ConId = conId; - event.CHIPoBLEWriteReceived.Data = std::move(buf).UnsafeRelease(); + ChipDeviceEvent event{ .Type = DeviceEventType::kCHIPoBLEWriteReceived, + .CHIPoBLEWriteReceived = { .ConId = conId, .Data = std::move(buf).UnsafeRelease() } }; PlatformMgr().PostEventOrDie(&event); } @@ -552,10 +544,8 @@ void BLEManagerImpl::CHIPoBluez_ConnectionClosed(BLE_CONNECTION_OBJECT conId) // If this was a CHIPoBLE connection, post an event to deliver a connection error to the CHIPoBLE layer. { - ChipDeviceEvent event; - event.Type = DeviceEventType::kCHIPoBLEConnectionError; - event.CHIPoBLEConnectionError.ConId = conId; - event.CHIPoBLEConnectionError.Reason = BLE_ERROR_REMOTE_DEVICE_DISCONNECTED; + ChipDeviceEvent event{ .Type = DeviceEventType::kCHIPoBLEConnectionError, + .CHIPoBLEConnectionError = { .ConId = conId, .Reason = BLE_ERROR_REMOTE_DEVICE_DISCONNECTED } }; PlatformMgr().PostEventOrDie(&event); } } @@ -567,9 +557,9 @@ void BLEManagerImpl::HandleTXCharCCCDWrite(BLE_CONNECTION_OBJECT conId) // Post an event to the Chip queue to process either a CHIPoBLE Subscribe or Unsubscribe based on // whether the client is enabling or disabling indications. - ChipDeviceEvent event; - event.Type = conId->IsNotifyAcquired() ? DeviceEventType::kCHIPoBLESubscribe : DeviceEventType::kCHIPoBLEUnsubscribe; - event.CHIPoBLESubscribe.ConId = conId; + ChipDeviceEvent event{ .Type = conId->IsNotifyAcquired() ? DeviceEventType::kCHIPoBLESubscribe + : DeviceEventType::kCHIPoBLEUnsubscribe, + .CHIPoBLESubscribe = { .ConId = conId } }; PlatformMgr().PostEventOrDie(&event); ChipLogProgress(DeviceLayer, "CHIPoBLE %s received", @@ -579,9 +569,7 @@ void BLEManagerImpl::HandleTXCharCCCDWrite(BLE_CONNECTION_OBJECT conId) void BLEManagerImpl::HandleTXComplete(BLE_CONNECTION_OBJECT conId) { // Post an event to the Chip queue to process the indicate confirmation. - ChipDeviceEvent event; - event.Type = DeviceEventType::kCHIPoBLEIndicateConfirm; - event.CHIPoBLEIndicateConfirm.ConId = conId; + ChipDeviceEvent event{ .Type = DeviceEventType::kCHIPoBLEIndicateConfirm, .CHIPoBLEIndicateConfirm = { .ConId = conId } }; PlatformMgr().PostEventOrDie(&event); } @@ -822,50 +810,44 @@ CHIP_ERROR BLEManagerImpl::CancelConnection() void BLEManagerImpl::NotifyBLEAdapterAdded(unsigned int aAdapterId, const char * aAdapterAddress) { - ChipDeviceEvent event; - event.Type = DeviceEventType::kPlatformLinuxBLEAdapterAdded; - event.Platform.BLEAdapter.mAdapterId = aAdapterId; + ChipDeviceEvent event{ .Type = DeviceEventType::kPlatformLinuxBLEAdapterAdded, + .Platform = { .BLEAdapter = { .mAdapterId = aAdapterId } } }; Platform::CopyString(event.Platform.BLEAdapter.mAdapterAddress, aAdapterAddress); PlatformMgr().PostEventOrDie(&event); } void BLEManagerImpl::NotifyBLEAdapterRemoved(unsigned int aAdapterId, const char * aAdapterAddress) { - ChipDeviceEvent event; - event.Type = DeviceEventType::kPlatformLinuxBLEAdapterRemoved; - event.Platform.BLEAdapter.mAdapterId = aAdapterId; + ChipDeviceEvent event{ .Type = DeviceEventType::kPlatformLinuxBLEAdapterRemoved, + .Platform = { .BLEAdapter = { .mAdapterId = aAdapterId } } }; Platform::CopyString(event.Platform.BLEAdapter.mAdapterAddress, aAdapterAddress); PlatformMgr().PostEventOrDie(&event); } void BLEManagerImpl::NotifyBLEPeripheralRegisterAppComplete(CHIP_ERROR error) { - ChipDeviceEvent event; - event.Type = DeviceEventType::kPlatformLinuxBLEPeripheralRegisterAppComplete; - event.Platform.BLEPeripheralRegisterAppComplete.mError = error; + ChipDeviceEvent event{ .Type = DeviceEventType::kPlatformLinuxBLEPeripheralRegisterAppComplete, + .Platform = { .BLEPeripheralRegisterAppComplete = { .mError = error } } }; PlatformMgr().PostEventOrDie(&event); } void BLEManagerImpl::NotifyBLEPeripheralAdvStartComplete(CHIP_ERROR error) { - ChipDeviceEvent event; - event.Type = DeviceEventType::kPlatformLinuxBLEPeripheralAdvStartComplete; - event.Platform.BLEPeripheralAdvStartComplete.mError = error; + ChipDeviceEvent event{ .Type = DeviceEventType::kPlatformLinuxBLEPeripheralAdvStartComplete, + .Platform = { .BLEPeripheralAdvStartComplete = { .mError = error } } }; PlatformMgr().PostEventOrDie(&event); } void BLEManagerImpl::NotifyBLEPeripheralAdvStopComplete(CHIP_ERROR error) { - ChipDeviceEvent event; - event.Type = DeviceEventType::kPlatformLinuxBLEPeripheralAdvStopComplete; - event.Platform.BLEPeripheralAdvStopComplete.mError = error; + ChipDeviceEvent event{ .Type = DeviceEventType::kPlatformLinuxBLEPeripheralAdvStopComplete, + .Platform = { .BLEPeripheralAdvStopComplete = { .mError = error } } }; PlatformMgr().PostEventOrDie(&event); } void BLEManagerImpl::NotifyBLEPeripheralAdvReleased() { - ChipDeviceEvent event; - event.Type = DeviceEventType::kPlatformLinuxBLEPeripheralAdvReleased; + ChipDeviceEvent event{ .Type = DeviceEventType::kPlatformLinuxBLEPeripheralAdvReleased }; PlatformMgr().PostEventOrDie(&event); } diff --git a/src/platform/Linux/ConnectivityManagerImpl.cpp b/src/platform/Linux/ConnectivityManagerImpl.cpp index f7edd474e2d7ea..cd3922baaf2b02 100644 --- a/src/platform/Linux/ConnectivityManagerImpl.cpp +++ b/src/platform/Linux/ConnectivityManagerImpl.cpp @@ -1278,11 +1278,9 @@ void ConnectivityManagerImpl::PostNetworkConnect() chip::Inet::IPAddress addr; if ((it.GetAddress(addr) == CHIP_NO_ERROR) && addr.IsIPv4()) { - ChipDeviceEvent event; - event.Type = DeviceEventType::kInternetConnectivityChange; - event.InternetConnectivityChange.IPv4 = kConnectivity_Established; - event.InternetConnectivityChange.IPv6 = kConnectivity_NoChange; - event.InternetConnectivityChange.ipAddress = addr; + ChipDeviceEvent event{ .Type = DeviceEventType::kInternetConnectivityChange, + .InternetConnectivityChange = { + .IPv4 = kConnectivity_Established, .IPv6 = kConnectivity_NoChange, .ipAddress = addr } }; char ipStrBuf[chip::Inet::IPAddress::kMaxStringLength] = { 0 }; addr.ToString(ipStrBuf); diff --git a/src/platform/Linux/PlatformManagerImpl.cpp b/src/platform/Linux/PlatformManagerImpl.cpp index 401d72bbe413bd..68f3d58c1ac87b 100644 --- a/src/platform/Linux/PlatformManagerImpl.cpp +++ b/src/platform/Linux/PlatformManagerImpl.cpp @@ -123,10 +123,9 @@ gboolean WiFiIPChangeListener(GIOChannel * ch, GIOCondition /* condition */, voi inet_ntop(AF_INET, RTA_DATA(routeInfo), ipStrBuf, sizeof(ipStrBuf)); ChipLogDetail(DeviceLayer, "Got IP address on interface: %s IP: %s", name, ipStrBuf); - ChipDeviceEvent event; - event.Type = DeviceEventType::kInternetConnectivityChange; - event.InternetConnectivityChange.IPv4 = kConnectivity_Established; - event.InternetConnectivityChange.IPv6 = kConnectivity_NoChange; + ChipDeviceEvent event{ .Type = DeviceEventType::kInternetConnectivityChange, + .InternetConnectivityChange = { .IPv4 = kConnectivity_Established, + .IPv6 = kConnectivity_NoChange } }; if (!chip::Inet::IPAddress::FromString(ipStrBuf, event.InternetConnectivityChange.ipAddress)) { diff --git a/src/platform/Linux/ThreadStackManagerImpl.cpp b/src/platform/Linux/ThreadStackManagerImpl.cpp index 40bd31ecaeee44..126d30164492fe 100644 --- a/src/platform/Linux/ThreadStackManagerImpl.cpp +++ b/src/platform/Linux/ThreadStackManagerImpl.cpp @@ -148,13 +148,11 @@ void ThreadStackManagerImpl::ThreadDeviceRoleChangedHandler(const gchar * role) { bool attached = strcmp(role, kOpenthreadDeviceRoleDetached) != 0 && strcmp(role, kOpenthreadDeviceRoleDisabled) != 0; - ChipDeviceEvent event = ChipDeviceEvent{}; - if (attached != mAttached) { - event.Type = DeviceEventType::kThreadConnectivityChange; - event.ThreadConnectivityChange.Result = - attached ? ConnectivityChange::kConnectivity_Established : ConnectivityChange::kConnectivity_Lost; + ChipDeviceEvent event{ .Type = DeviceEventType::kThreadConnectivityChange, + .ThreadConnectivityChange = { .Result = attached ? ConnectivityChange::kConnectivity_Established + : ConnectivityChange::kConnectivity_Lost } }; CHIP_ERROR status = PlatformMgr().PostEvent(&event); if (status != CHIP_NO_ERROR) { @@ -163,9 +161,8 @@ void ThreadStackManagerImpl::ThreadDeviceRoleChangedHandler(const gchar * role) } mAttached = attached; - event.Type = DeviceEventType::kThreadStateChange; - event.ThreadStateChange.RoleChanged = true; - CHIP_ERROR status = PlatformMgr().PostEvent(&event); + ChipDeviceEvent event{ .Type = DeviceEventType::kThreadStateChange, .ThreadStateChange = { .RoleChanged = true } }; + CHIP_ERROR status = PlatformMgr().PostEvent(&event); if (status != CHIP_NO_ERROR) { ChipLogError(DeviceLayer, "Failed to post thread state change: %" CHIP_ERROR_FORMAT, status.Format()); @@ -257,9 +254,8 @@ CHIP_ERROR ThreadStackManagerImpl::_SetThreadProvision(ByteSpan netInfo) VerifyOrReturnError(err == CHIP_NO_ERROR, err, ChipLogError(DeviceLayer, "openthread: failed to set active dataset")); // post an event alerting other subsystems about change in provisioning state - ChipDeviceEvent event; - event.Type = DeviceEventType::kServiceProvisioningChange; - event.ServiceProvisioningChange.IsServiceProvisioned = true; + ChipDeviceEvent event{ .Type = DeviceEventType::kServiceProvisioningChange, + .ServiceProvisioningChange = { .IsServiceProvisioned = true } }; return PlatformMgr().PostEvent(&event); } diff --git a/src/platform/PlatformEventSupport.cpp b/src/platform/PlatformEventSupport.cpp index 1572a3da865457..947a7f2fe691f8 100644 --- a/src/platform/PlatformEventSupport.cpp +++ b/src/platform/PlatformEventSupport.cpp @@ -34,9 +34,7 @@ using namespace ::chip::DeviceLayer; CHIP_ERROR PlatformEventing::ScheduleLambdaBridge(System::Layer & aLayer, LambdaBridge && bridge) { - ChipDeviceEvent event; - event.Type = DeviceEventType::kChipLambdaEvent; - event.LambdaEvent = std::move(bridge); + ChipDeviceEvent event{ .Type = DeviceEventType::kChipLambdaEvent, .LambdaEvent = std::move(bridge) }; return PlatformMgr().PostEvent(&event); } From 321b7d7a7acd5012aa904c52cd1cf74cf000df2e Mon Sep 17 00:00:00 2001 From: "Restyled.io" <commits@restyled.io> Date: Wed, 17 Apr 2024 22:01:39 +0000 Subject: [PATCH 03/10] Restyled by clang-format --- src/lib/core/CHIPError.h | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/lib/core/CHIPError.h b/src/lib/core/CHIPError.h index 65a23a0765f77b..7e1b9c72ed8f2e 100644 --- a/src/lib/core/CHIPError.h +++ b/src/lib/core/CHIPError.h @@ -166,7 +166,8 @@ class ChipError #else constexpr ChipError(SdkPart part, uint8_t code) : mError(MakeInteger(part, code)) CHIP_INITIALIZE_ERROR_SOURCE(nullptr, 0) {} constexpr ChipError(SdkPart part, uint8_t code, const char * file, unsigned int line) : - mError(MakeInteger(part, code)) CHIP_INITIALIZE_ERROR_SOURCE(file, line){} + mError(MakeInteger(part, code)) CHIP_INITIALIZE_ERROR_SOURCE(file, line) + {} #endif // __cplusplus >= 202002L /** @@ -195,9 +196,7 @@ class ChipError mError(error) CHIP_INITIALIZE_ERROR_SOURCE(file, line, location) {} #else - explicit constexpr ChipError(StorageType error) : mError(error) CHIP_INITIALIZE_ERROR_SOURCE(nullptr, 0) - { - } + explicit constexpr ChipError(StorageType error) : mError(error) CHIP_INITIALIZE_ERROR_SOURCE(nullptr, 0) {} explicit constexpr ChipError(StorageType error, const char * file, unsigned int line) : mError(error) CHIP_INITIALIZE_ERROR_SOURCE(file, line) {} From fc4a2e5cefee4168092baacd732d0142aa6de232 Mon Sep 17 00:00:00 2001 From: Matt Swartwout <mwswartwout@google.com> Date: Wed, 17 Apr 2024 23:20:55 +0000 Subject: [PATCH 04/10] A few more fixes --- .../GenericConnectivityManagerImpl_Thread.ipp | 10 +++--- ...nericThreadStackManagerImpl_OpenThread.hpp | 32 ++++++++++--------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/include/platform/internal/GenericConnectivityManagerImpl_Thread.ipp b/src/include/platform/internal/GenericConnectivityManagerImpl_Thread.ipp index b2ee27a424c730..23b626033593ef 100644 --- a/src/include/platform/internal/GenericConnectivityManagerImpl_Thread.ipp +++ b/src/include/platform/internal/GenericConnectivityManagerImpl_Thread.ipp @@ -71,11 +71,11 @@ void GenericConnectivityManagerImpl_Thread<ImplClass>::UpdateServiceConnectivity { ChipDeviceEvent event{ .Type = DeviceEventType::kServiceConnectivityChange, - .ServiceConnectivityChange = { - .Overall = { .Result = event.ServiceConnectivityChange.ViaThread.Result }, - .ViaThread = { .Result = (haveServiceConnectivity) ? kConnectivity_Established - : kConnectivity_Lost } } }; - CHIP_ERROR status = PlatformMgr().PostEvent(&event); + .ServiceConnectivityChange = { .ViaThread = { .Result = (haveServiceConnectivity) + ? kConnectivity_Established + : kConnectivity_Lost } } }; + event.ServiceConnectivityChange.Overall.Result = event.ServiceConnectivityChange.ViaThread.Result; + CHIP_ERROR status = PlatformMgr().PostEvent(&event); if (status != CHIP_NO_ERROR) { ChipLogError(DeviceLayer, "Failed to post thread connectivity change: %" CHIP_ERROR_FORMAT, status.Format()); diff --git a/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.hpp b/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.hpp index 15f9f80115cd3a..7183263d32d3ae 100644 --- a/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.hpp +++ b/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.hpp @@ -122,13 +122,17 @@ NetworkCommissioning::otScanResponseIterator<NetworkCommissioning::ThreadScanRes template <class ImplClass> void GenericThreadStackManagerImpl_OpenThread<ImplClass>::OnOpenThreadStateChange(uint32_t flags, void * context) { - ChipDeviceEvent event; - event.Type = DeviceEventType::kThreadStateChange; - event.ThreadStateChange.RoleChanged = (flags & OT_CHANGED_THREAD_ROLE) != 0; - event.ThreadStateChange.AddressChanged = (flags & (OT_CHANGED_IP6_ADDRESS_ADDED | OT_CHANGED_IP6_ADDRESS_REMOVED)) != 0; - event.ThreadStateChange.NetDataChanged = (flags & OT_CHANGED_THREAD_NETDATA) != 0; - event.ThreadStateChange.ChildNodesChanged = (flags & (OT_CHANGED_THREAD_CHILD_ADDED | OT_CHANGED_THREAD_CHILD_REMOVED)) != 0; - event.ThreadStateChange.OpenThread.Flags = flags; + ChipDeviceEvent event + { + .Type = DeviceEventType::kThreadStateChange; + .ThreadStateChange = { + .RoleChanged = (flags & OT_CHANGED_THREAD_ROLE) != 0, + .AddressChanged = (flags & (OT_CHANGED_IP6_ADDRESS_ADDED | OT_CHANGED_IP6_ADDRESS_REMOVED)) != 0, + .NetDataChanged = (flags & OT_CHANGED_THREAD_NETDATA) != 0, + .ChildNodesChanged = (flags & (OT_CHANGED_THREAD_CHILD_ADDED | OT_CHANGED_THREAD_CHILD_REMOVED)) != 0, + .OpenThread = { .Flags = flags } + } + }; CHIP_ERROR status = PlatformMgr().PostEvent(&event); if (status != CHIP_NO_ERROR) @@ -210,11 +214,10 @@ void GenericThreadStackManagerImpl_OpenThread<ImplClass>::_OnPlatformEvent(const // Avoid sending muliple events if the attachement state didn't change (Child->router or disable->Detached) if (event->ThreadStateChange.RoleChanged && (isThreadAttached != mIsAttached)) { - ChipDeviceEvent attachEvent; - attachEvent.Clear(); - attachEvent.Type = DeviceEventType::kThreadConnectivityChange; - attachEvent.ThreadConnectivityChange.Result = (isThreadAttached) ? kConnectivity_Established : kConnectivity_Lost; - CHIP_ERROR status = PlatformMgr().PostEvent(&attachEvent); + ChipDeviceEvent attachEvent{ .Type = DeviceEventType::kThreadConnectivityChange, + .ThreadConnectivityChange = { .Result = (isThreadAttached) ? kConnectivity_Established + : kConnectivity_Lost } }; + CHIP_ERROR status = PlatformMgr().PostEvent(&attachEvent); if (status == CHIP_NO_ERROR) { mIsAttached = isThreadAttached; @@ -300,9 +303,8 @@ CHIP_ERROR GenericThreadStackManagerImpl_OpenThread<ImplClass>::_SetThreadProvis } // post an event alerting other subsystems about change in provisioning state - ChipDeviceEvent event; - event.Type = DeviceEventType::kServiceProvisioningChange; - event.ServiceProvisioningChange.IsServiceProvisioned = true; + ChipDeviceEvent event{ .Type = DeviceEventType::kServiceProvisioningChange, + .ServiceProvisioningChange = { .IsServiceProvisioned = true } }; return PlatformMgr().PostEvent(&event); } From 6e3eced623a54937e3b305c60c1d161945a2aa25 Mon Sep 17 00:00:00 2001 From: Matt Swartwout <mwswartwout@google.com> Date: Thu, 18 Apr 2024 16:11:03 +0000 Subject: [PATCH 05/10] Replace semicolon with comma --- ...nericThreadStackManagerImpl_OpenThread.hpp | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.hpp b/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.hpp index 7183263d32d3ae..e0fb3df5ef7336 100644 --- a/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.hpp +++ b/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.hpp @@ -122,17 +122,14 @@ NetworkCommissioning::otScanResponseIterator<NetworkCommissioning::ThreadScanRes template <class ImplClass> void GenericThreadStackManagerImpl_OpenThread<ImplClass>::OnOpenThreadStateChange(uint32_t flags, void * context) { - ChipDeviceEvent event - { - .Type = DeviceEventType::kThreadStateChange; - .ThreadStateChange = { - .RoleChanged = (flags & OT_CHANGED_THREAD_ROLE) != 0, - .AddressChanged = (flags & (OT_CHANGED_IP6_ADDRESS_ADDED | OT_CHANGED_IP6_ADDRESS_REMOVED)) != 0, - .NetDataChanged = (flags & OT_CHANGED_THREAD_NETDATA) != 0, - .ChildNodesChanged = (flags & (OT_CHANGED_THREAD_CHILD_ADDED | OT_CHANGED_THREAD_CHILD_REMOVED)) != 0, - .OpenThread = { .Flags = flags } - } - }; + ChipDeviceEvent event{ .Type = DeviceEventType::kThreadStateChange, + .ThreadStateChange = { + .RoleChanged = (flags & OT_CHANGED_THREAD_ROLE) != 0, + .AddressChanged = (flags & (OT_CHANGED_IP6_ADDRESS_ADDED | OT_CHANGED_IP6_ADDRESS_REMOVED)) != 0, + .NetDataChanged = (flags & OT_CHANGED_THREAD_NETDATA) != 0, + .ChildNodesChanged = + (flags & (OT_CHANGED_THREAD_CHILD_ADDED | OT_CHANGED_THREAD_CHILD_REMOVED)) != 0, + .OpenThread = { .Flags = flags } } }; CHIP_ERROR status = PlatformMgr().PostEvent(&event); if (status != CHIP_NO_ERROR) From ede91b37c9794f10d07e285c669d695cba635491 Mon Sep 17 00:00:00 2001 From: Matt Swartwout <mwswartwout@google.com> Date: Thu, 18 Apr 2024 17:16:46 +0000 Subject: [PATCH 06/10] Add enum->int cast --- src/platform/Linux/BLEManagerImpl.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/platform/Linux/BLEManagerImpl.cpp b/src/platform/Linux/BLEManagerImpl.cpp index 33e1592826bfc6..714b55d07ec20b 100644 --- a/src/platform/Linux/BLEManagerImpl.cpp +++ b/src/platform/Linux/BLEManagerImpl.cpp @@ -557,8 +557,8 @@ void BLEManagerImpl::HandleTXCharCCCDWrite(BLE_CONNECTION_OBJECT conId) // Post an event to the Chip queue to process either a CHIPoBLE Subscribe or Unsubscribe based on // whether the client is enabling or disabling indications. - ChipDeviceEvent event{ .Type = conId->IsNotifyAcquired() ? DeviceEventType::kCHIPoBLESubscribe - : DeviceEventType::kCHIPoBLEUnsubscribe, + ChipDeviceEvent event{ .Type = conId->IsNotifyAcquired() ? static_cast<uint16_t>(DeviceEventType::kCHIPoBLESubscribe) + : static_cast<uint16_t>(DeviceEventType::kCHIPoBLEUnsubscribe), .CHIPoBLESubscribe = { .ConId = conId } }; PlatformMgr().PostEventOrDie(&event); From a6b54c1578c2f0a7e3353538434b1dfad4b3f195 Mon Sep 17 00:00:00 2001 From: Matt Swartwout <mwswartwout@google.com> Date: Tue, 23 Apr 2024 18:30:09 +0000 Subject: [PATCH 07/10] Add tests and simplify constructor --- src/lib/core/CHIPError.h | 34 +++++----- src/lib/core/tests/BUILD.gn | 1 + src/lib/core/tests/TestCHIPError.cpp | 93 ++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 20 deletions(-) create mode 100644 src/lib/core/tests/TestCHIPError.cpp diff --git a/src/lib/core/CHIPError.h b/src/lib/core/CHIPError.h index 7e1b9c72ed8f2e..6c9c58e66dce24 100644 --- a/src/lib/core/CHIPError.h +++ b/src/lib/core/CHIPError.h @@ -119,10 +119,10 @@ class ChipError #if __cplusplus >= 202002L #define CHIP_INITIALIZE_ERROR_SOURCE(f, l, loc) , mFile((f)), mLine((l)), mSourceLocation((loc)) #else -#define CHIP_INITIALIZE_ERROR_SOURCE(f, l) , mFile((f)), mLine((l)) +#define CHIP_INITIALIZE_ERROR_SOURCE(f, l, loc) , mFile((f)), mLine((l)) #endif // __cplusplus >= 202002L #else // CHIP_CONFIG_ERROR_SOURCE -#define CHIP_INITIALIZE_ERROR_SOURCE(f, l) +#define CHIP_INITIALIZE_ERROR_SOURCE(f, l, loc) #endif // CHIP_CONFIG_ERROR_SOURCE /** @@ -131,21 +131,15 @@ class ChipError * @note * The result is valid only if CanEncapsulate() is true. */ + constexpr ChipError(Range range, ValueType value) : ChipError(range, value, /*file=*/nullptr, /*line=*/0) {} #if __cplusplus >= 202002L - constexpr ChipError(Range range, ValueType value) : - mError(MakeInteger(range, (value & MakeMask(0, kValueLength)))) - CHIP_INITIALIZE_ERROR_SOURCE(nullptr, 0, std::source_location()) - {} constexpr ChipError(Range range, ValueType value, const char * file, unsigned int line, std::source_location location = std::source_location::current()) : mError(MakeInteger(range, (value & MakeMask(0, kValueLength)))) CHIP_INITIALIZE_ERROR_SOURCE(file, line, location) {} #else - constexpr ChipError(Range range, ValueType value) : - mError(MakeInteger(range, (value & MakeMask(0, kValueLength)))) CHIP_INITIALIZE_ERROR_SOURCE(nullptr, 0) - {} constexpr ChipError(Range range, ValueType value, const char * file, unsigned int line) : - mError(MakeInteger(range, (value & MakeMask(0, kValueLength)))) CHIP_INITIALIZE_ERROR_SOURCE(file, line) + mError(MakeInteger(range, (value & MakeMask(0, kValueLength)))) CHIP_INITIALIZE_ERROR_SOURCE(file, line, /*loc=*/nullptr) {} #endif // __cplusplus >= 202002L @@ -155,19 +149,15 @@ class ChipError * @note * The macro version CHIP_SDK_ERROR checks that the numeric value is constant and well-formed. */ + constexpr ChipError(SdkPart part, uint8_t code) : ChipError(part, code, /*file=*/nullptr, /*line=*/0) {} #if __cplusplus >= 202002L - constexpr ChipError(SdkPart part, uint8_t code) : - mError(MakeInteger(part, code)) CHIP_INITIALIZE_ERROR_SOURCE(nullptr, 0, std::source_location()) - {} constexpr ChipError(SdkPart part, uint8_t code, const char * file, unsigned int line, std::source_location location = std::source_location::current()) : mError(MakeInteger(part, code)) CHIP_INITIALIZE_ERROR_SOURCE(file, line, location) {} #else - constexpr ChipError(SdkPart part, uint8_t code) : mError(MakeInteger(part, code)) CHIP_INITIALIZE_ERROR_SOURCE(nullptr, 0) {} constexpr ChipError(SdkPart part, uint8_t code, const char * file, unsigned int line) : - mError(MakeInteger(part, code)) CHIP_INITIALIZE_ERROR_SOURCE(file, line) - {} + mError(MakeInteger(part, code)) CHIP_INITIALIZE_ERROR_SOURCE(file, line, /*loc=*/nullptr){} #endif // __cplusplus >= 202002L /** @@ -188,17 +178,15 @@ class ChipError * @note * This is intended to be used only in foreign function interfaces. */ + explicit constexpr ChipError(StorageType error) : ChipError(error, /*file=*/nullptr, /*line=*/0) {} #if __cplusplus >= 202002L - explicit constexpr ChipError(StorageType error) : mError(error) CHIP_INITIALIZE_ERROR_SOURCE(nullptr, 0, std::source_location()) - {} explicit constexpr ChipError(StorageType error, const char * file, unsigned int line, std::source_location location = std::source_location::current()) : mError(error) CHIP_INITIALIZE_ERROR_SOURCE(file, line, location) {} #else - explicit constexpr ChipError(StorageType error) : mError(error) CHIP_INITIALIZE_ERROR_SOURCE(nullptr, 0) {} explicit constexpr ChipError(StorageType error, const char * file, unsigned int line) : - mError(error) CHIP_INITIALIZE_ERROR_SOURCE(file, line) + mError(error) CHIP_INITIALIZE_ERROR_SOURCE(file, line, /*loc=*/nullptr) {} #endif // __cplusplus >= 202002L @@ -337,6 +325,12 @@ class ChipError */ unsigned int GetLine() const { return mLine; } +#if __cplusplus >= 202002L + /** + * Get the source_location of the point where the error occurred. + */ + const std::source_location & GetSourceLocation() { return mSourceLocation; } +#endif // __cplusplus >= 202002L #endif // CHIP_CONFIG_ERROR_SOURCE private: diff --git a/src/lib/core/tests/BUILD.gn b/src/lib/core/tests/BUILD.gn index 1e3373d1aff623..f04aa91c9baa5e 100644 --- a/src/lib/core/tests/BUILD.gn +++ b/src/lib/core/tests/BUILD.gn @@ -26,6 +26,7 @@ chip_test_suite_using_nltest("tests") { "TestCATValues.cpp", "TestCHIPCallback.cpp", "TestCHIPErrorStr.cpp", + "TestCHIPError.cpp", "TestOTAImageHeader.cpp", "TestOptional.cpp", "TestReferenceCounted.cpp", diff --git a/src/lib/core/tests/TestCHIPError.cpp b/src/lib/core/tests/TestCHIPError.cpp new file mode 100644 index 00000000000000..d7424e2d0ef394 --- /dev/null +++ b/src/lib/core/tests/TestCHIPError.cpp @@ -0,0 +1,93 @@ +/* + * + * Copyright (c) 2024 Project CHIP Authors + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <string> + +#include <lib/core/CHIPError.h> +#include <lib/support/UnitTestContext.h> +#include <lib/support/UnitTestRegistration.h> + +#include <nlunit-test.h> + +namespace { + +using namespace chip; + +void RangeConstructor(nlTestSuite * inSuite, void * /*inContext*/) +{ + ChipError error(ChipError::Range::kSDK, /*value=*/1, "file_name", /*line=*/2); +#if CHIP_CONFIG_ERROR_SOURCE + NL_TEST_ASSERT(inSuite, std::string(error.GetFile()) == "file_name"); + NL_TEST_ASSERT(inSuite, error.GetLine() == 2); +#if __cplusplus >= 202002L + std::source_location location = error.GetSourceLocation(); + NL_TEST_ASSERT(inSuite, error.line() == 37); + NL_TEST_ASSERT(inSuite, error.file_name() == "TestCHIPError.cpp"); +#endif // __cplusplus >= 202002L +#endif // CHIP_CONFIG_ERROR_SOURCE +} + +void SdkPartConstructor(nlTestSuite * inSuite, void * /*inContext*/) +{ + ChipError error(ChipError::SdkPart::kCore, /*code=*/1, "file_name", /*line=*/2); +#if CHIP_CONFIG_ERROR_SOURCE + NL_TEST_ASSERT(inSuite, std::string(error.GetFile()) == "file_name"); + NL_TEST_ASSERT(inSuite, error.GetLine() == 2); +#if __cplusplus >= 202002L + std::source_location location = error.GetSourceLocation(); + NL_TEST_ASSERT(inSuite, error.line() == 49); + NL_TEST_ASSERT(inSuite, error.file_name() == "TestCHIPError.cpp"); +#endif // __cplusplus >= 202002L +#endif // CHIP_CONFIG_ERROR_SOURCE +} + +void StorageTypeConstructor(nlTestSuite * inSuite, void * /*inContext*/) +{ + ChipError error(/*error=*/1, "file_name", /*line=*/2); + NL_TEST_ASSERT(inSuite, error.AsInteger() == 1); +#if CHIP_CONFIG_ERROR_SOURCE + NL_TEST_ASSERT(inSuite, std::string(error.GetFile()) == "file_name"); + NL_TEST_ASSERT(inSuite, error.GetLine() == 2); +#if __cplusplus >= 202002L + std::source_location location = error.GetSourceLocation(); + NL_TEST_ASSERT(inSuite, error.line() == 61); + NL_TEST_ASSERT(inSuite, error.file_name() == "TestCHIPError.cpp"); +#endif // __cplusplus >= 202002L +#endif // CHIP_CONFIG_ERROR_SOURCE +} + +/** + * Test Suite. It lists all the test functions. + */ + +static const nlTest sTests[] = { NL_TEST_DEF("RangeConstructor", RangeConstructor), + NL_TEST_DEF("SdkPartConstructor", SdkPartConstructor), + NL_TEST_DEF("StorageTypeConstructor", StorageTypeConstructor), NL_TEST_SENTINEL() }; + +} // namespace + +int TestCHIPError() +{ + nlTestSuite theSuite = { "Test CHIP_ERROR string conversions", &sTests[0], nullptr, nullptr }; + + nlTestRunner(&theSuite, nullptr); + + return nlTestRunnerStats(&theSuite); +} + +CHIP_REGISTER_TEST_SUITE(TestCHIPError) From 8bc2b7499c7fb495b02564e73ee920a950d8307d Mon Sep 17 00:00:00 2001 From: Matt Swartwout <mwswartwout@google.com> Date: Thu, 25 Apr 2024 02:42:52 +0000 Subject: [PATCH 08/10] Convert to gtest --- src/lib/core/tests/TestCHIPError.cpp | 65 ++++++++++------------------ 1 file changed, 22 insertions(+), 43 deletions(-) diff --git a/src/lib/core/tests/TestCHIPError.cpp b/src/lib/core/tests/TestCHIPError.cpp index d7424e2d0ef394..ba0c25906623a8 100644 --- a/src/lib/core/tests/TestCHIPError.cpp +++ b/src/lib/core/tests/TestCHIPError.cpp @@ -19,75 +19,54 @@ #include <string> #include <lib/core/CHIPError.h> -#include <lib/support/UnitTestContext.h> -#include <lib/support/UnitTestRegistration.h> -#include <nlunit-test.h> +#include <gtest/gtest.h> +namespace chip { namespace { -using namespace chip; - -void RangeConstructor(nlTestSuite * inSuite, void * /*inContext*/) +TEST(ChipErrorTest, RangeConstructor) { - ChipError error(ChipError::Range::kSDK, /*value=*/1, "file_name", /*line=*/2); + ChipError error(ChipError::Range::kSDK, /*value=*/1, __FILE__, __LINE__); #if CHIP_CONFIG_ERROR_SOURCE - NL_TEST_ASSERT(inSuite, std::string(error.GetFile()) == "file_name"); - NL_TEST_ASSERT(inSuite, error.GetLine() == 2); + EXPECT_EQ(error.GetFile(), __FILE__); + EXPECT_EQ(error.GetLine(), 30u); #if __cplusplus >= 202002L std::source_location location = error.GetSourceLocation(); - NL_TEST_ASSERT(inSuite, error.line() == 37); - NL_TEST_ASSERT(inSuite, error.file_name() == "TestCHIPError.cpp"); + EXPECT_EQ(location.line(), 30u); + EXPECT_EQ(location.file_name(), __FILE__); #endif // __cplusplus >= 202002L #endif // CHIP_CONFIG_ERROR_SOURCE } -void SdkPartConstructor(nlTestSuite * inSuite, void * /*inContext*/) +TEST(ChipErrorTest, SdkPartConstructor) { - ChipError error(ChipError::SdkPart::kCore, /*code=*/1, "file_name", /*line=*/2); + ChipError error(ChipError::SdkPart::kCore, /*code=*/1, __FILE__, __LINE__); #if CHIP_CONFIG_ERROR_SOURCE - NL_TEST_ASSERT(inSuite, std::string(error.GetFile()) == "file_name"); - NL_TEST_ASSERT(inSuite, error.GetLine() == 2); + EXPECT_EQ(error.GetFile(), __FILE__); + EXPECT_EQ(error.GetLine(), 44u); #if __cplusplus >= 202002L std::source_location location = error.GetSourceLocation(); - NL_TEST_ASSERT(inSuite, error.line() == 49); - NL_TEST_ASSERT(inSuite, error.file_name() == "TestCHIPError.cpp"); + EXPECT_EQ(location.line(), 44u); + EXPECT_EQ(location.file_name(), __FILE__); #endif // __cplusplus >= 202002L #endif // CHIP_CONFIG_ERROR_SOURCE } -void StorageTypeConstructor(nlTestSuite * inSuite, void * /*inContext*/) +TEST(ChipErrorTest, StorageTypeConstructor) { - ChipError error(/*error=*/1, "file_name", /*line=*/2); - NL_TEST_ASSERT(inSuite, error.AsInteger() == 1); + ChipError error(/*error=*/1, __FILE__, __LINE__); + EXPECT_EQ(error.AsInteger(), 1u); #if CHIP_CONFIG_ERROR_SOURCE - NL_TEST_ASSERT(inSuite, std::string(error.GetFile()) == "file_name"); - NL_TEST_ASSERT(inSuite, error.GetLine() == 2); + EXPECT_EQ(error.GetFile(), __FILE__); + EXPECT_EQ(error.GetLine(), 58u); #if __cplusplus >= 202002L std::source_location location = error.GetSourceLocation(); - NL_TEST_ASSERT(inSuite, error.line() == 61); - NL_TEST_ASSERT(inSuite, error.file_name() == "TestCHIPError.cpp"); + EXPECT_EQ(location.line(), 58u); + EXPECT_EQ(location.file_name(), __FILE__); #endif // __cplusplus >= 202002L #endif // CHIP_CONFIG_ERROR_SOURCE } -/** - * Test Suite. It lists all the test functions. - */ - -static const nlTest sTests[] = { NL_TEST_DEF("RangeConstructor", RangeConstructor), - NL_TEST_DEF("SdkPartConstructor", SdkPartConstructor), - NL_TEST_DEF("StorageTypeConstructor", StorageTypeConstructor), NL_TEST_SENTINEL() }; - } // namespace - -int TestCHIPError() -{ - nlTestSuite theSuite = { "Test CHIP_ERROR string conversions", &sTests[0], nullptr, nullptr }; - - nlTestRunner(&theSuite, nullptr); - - return nlTestRunnerStats(&theSuite); -} - -CHIP_REGISTER_TEST_SUITE(TestCHIPError) +} // namespace chip From 9c7fcf1dc70f5758e70b9f129b57574b2ee10ba5 Mon Sep 17 00:00:00 2001 From: "Restyled.io" <commits@restyled.io> Date: Thu, 25 Apr 2024 02:43:27 +0000 Subject: [PATCH 09/10] Restyled by clang-format --- src/lib/core/CHIPError.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib/core/CHIPError.h b/src/lib/core/CHIPError.h index 6c9c58e66dce24..37001c8c6971a2 100644 --- a/src/lib/core/CHIPError.h +++ b/src/lib/core/CHIPError.h @@ -157,7 +157,8 @@ class ChipError {} #else constexpr ChipError(SdkPart part, uint8_t code, const char * file, unsigned int line) : - mError(MakeInteger(part, code)) CHIP_INITIALIZE_ERROR_SOURCE(file, line, /*loc=*/nullptr){} + mError(MakeInteger(part, code)) CHIP_INITIALIZE_ERROR_SOURCE(file, line, /*loc=*/nullptr) + {} #endif // __cplusplus >= 202002L /** From c2be8d2f9e6487c2bac683cad6a23fcd07c38537 Mon Sep 17 00:00:00 2001 From: "Restyled.io" <commits@restyled.io> Date: Thu, 25 Apr 2024 02:43:34 +0000 Subject: [PATCH 10/10] Restyled by gn --- src/lib/core/tests/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/core/tests/BUILD.gn b/src/lib/core/tests/BUILD.gn index 4c65db115ac399..30d50ee9493699 100644 --- a/src/lib/core/tests/BUILD.gn +++ b/src/lib/core/tests/BUILD.gn @@ -24,8 +24,8 @@ chip_test_suite("tests") { test_sources = [ "TestCATValues.cpp", "TestCHIPCallback.cpp", - "TestCHIPErrorStr.cpp", "TestCHIPError.cpp", + "TestCHIPErrorStr.cpp", "TestOTAImageHeader.cpp", "TestOptional.cpp", "TestReferenceCounted.cpp",