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 cd96e0d32fea78..d7c1712baf9672 100644 --- a/src/app/clusters/bindings/bindings.cpp +++ b/src/app/clusters/bindings/bindings.cpp @@ -259,9 +259,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 9d4f293cd3c3b0..cbab1d9817aed2 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..23b626033593ef 100644 --- a/src/include/platform/internal/GenericConnectivityManagerImpl_Thread.ipp +++ b/src/include/platform/internal/GenericConnectivityManagerImpl_Thread.ipp @@ -70,11 +70,10 @@ void GenericConnectivityManagerImpl_Thread::UpdateServiceConnectivity mFlags.Set(Flags::kHaveServiceConnectivity, haveServiceConnectivity); { - ChipDeviceEvent event; - event.Clear(); - event.Type = DeviceEventType::kServiceConnectivityChange; - event.ServiceConnectivityChange.ViaThread.Result = - (haveServiceConnectivity) ? kConnectivity_Established : kConnectivity_Lost; + ChipDeviceEvent event{ .Type = DeviceEventType::kServiceConnectivityChange, + .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) diff --git a/src/lib/core/CHIPError.h b/src/lib/core/CHIPError.h index 38b76bca539c2a..37001c8c6971a2 100644 --- a/src/lib/core/CHIPError.h +++ b/src/lib/core/CHIPError.h @@ -35,6 +35,10 @@ #include #include +#if __cplusplus >= 202002L +#include +#endif // __cplusplus >= 202002L + namespace chip { /** @@ -112,9 +116,13 @@ class ChipError // Helper for declaring constructors without too much repetition. #if CHIP_CONFIG_ERROR_SOURCE -#define CHIP_INITIALIZE_ERROR_SOURCE(f, l) , mFile((f)), mLine((l)) -#else // CHIP_CONFIG_ERROR_SOURCE -#define CHIP_INITIALIZE_ERROR_SOURCE(f, l) +#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, loc) , mFile((f)), mLine((l)) +#endif // __cplusplus >= 202002L +#else // CHIP_CONFIG_ERROR_SOURCE +#define CHIP_INITIALIZE_ERROR_SOURCE(f, l, loc) #endif // CHIP_CONFIG_ERROR_SOURCE /** @@ -123,12 +131,17 @@ class ChipError * @note * The result is valid only if CanEncapsulate() is true. */ - 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) : ChipError(range, value, /*file=*/nullptr, /*line=*/0) {} +#if __cplusplus >= 202002L + 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, 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 /** * Construct a CHIP_ERROR for SdkPart @a part with @a code. @@ -136,10 +149,17 @@ 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) : mError(MakeInteger(part, code)) CHIP_INITIALIZE_ERROR_SOURCE(nullptr, 0) {} + constexpr ChipError(SdkPart part, uint8_t code) : ChipError(part, code, /*file=*/nullptr, /*line=*/0) {} +#if __cplusplus >= 202002L + 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, 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 /** * Construct a CHIP_ERROR constant for SdkPart @a part with @a code at the current source line. @@ -159,10 +179,17 @@ class ChipError * @note * This is intended to be used only in foreign function interfaces. */ - explicit constexpr ChipError(StorageType error) : mError(error) CHIP_INITIALIZE_ERROR_SOURCE(nullptr, 0) {} + explicit constexpr ChipError(StorageType error) : ChipError(error, /*file=*/nullptr, /*line=*/0) {} +#if __cplusplus >= 202002L + 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, 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 #undef CHIP_INITIALIZE_ERROR_SOURCE @@ -299,6 +326,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: @@ -365,6 +398,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: diff --git a/src/lib/core/tests/BUILD.gn b/src/lib/core/tests/BUILD.gn index 3092ac36809f59..30d50ee9493699 100644 --- a/src/lib/core/tests/BUILD.gn +++ b/src/lib/core/tests/BUILD.gn @@ -24,6 +24,7 @@ chip_test_suite("tests") { test_sources = [ "TestCATValues.cpp", "TestCHIPCallback.cpp", + "TestCHIPError.cpp", "TestCHIPErrorStr.cpp", "TestOTAImageHeader.cpp", "TestOptional.cpp", diff --git a/src/lib/core/tests/TestCHIPError.cpp b/src/lib/core/tests/TestCHIPError.cpp new file mode 100644 index 00000000000000..ba0c25906623a8 --- /dev/null +++ b/src/lib/core/tests/TestCHIPError.cpp @@ -0,0 +1,72 @@ +/* + * + * 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 + +#include + +#include + +namespace chip { +namespace { + +TEST(ChipErrorTest, RangeConstructor) +{ + ChipError error(ChipError::Range::kSDK, /*value=*/1, __FILE__, __LINE__); +#if CHIP_CONFIG_ERROR_SOURCE + EXPECT_EQ(error.GetFile(), __FILE__); + EXPECT_EQ(error.GetLine(), 30u); +#if __cplusplus >= 202002L + std::source_location location = error.GetSourceLocation(); + EXPECT_EQ(location.line(), 30u); + EXPECT_EQ(location.file_name(), __FILE__); +#endif // __cplusplus >= 202002L +#endif // CHIP_CONFIG_ERROR_SOURCE +} + +TEST(ChipErrorTest, SdkPartConstructor) +{ + ChipError error(ChipError::SdkPart::kCore, /*code=*/1, __FILE__, __LINE__); +#if CHIP_CONFIG_ERROR_SOURCE + EXPECT_EQ(error.GetFile(), __FILE__); + EXPECT_EQ(error.GetLine(), 44u); +#if __cplusplus >= 202002L + std::source_location location = error.GetSourceLocation(); + EXPECT_EQ(location.line(), 44u); + EXPECT_EQ(location.file_name(), __FILE__); +#endif // __cplusplus >= 202002L +#endif // CHIP_CONFIG_ERROR_SOURCE +} + +TEST(ChipErrorTest, StorageTypeConstructor) +{ + ChipError error(/*error=*/1, __FILE__, __LINE__); + EXPECT_EQ(error.AsInteger(), 1u); +#if CHIP_CONFIG_ERROR_SOURCE + EXPECT_EQ(error.GetFile(), __FILE__); + EXPECT_EQ(error.GetLine(), 58u); +#if __cplusplus >= 202002L + std::source_location location = error.GetSourceLocation(); + EXPECT_EQ(location.line(), 58u); + EXPECT_EQ(location.file_name(), __FILE__); +#endif // __cplusplus >= 202002L +#endif // CHIP_CONFIG_ERROR_SOURCE +} + +} // namespace +} // namespace chip 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 a5bd19f877a6a8..a61c6e2403de02 100644 --- a/src/platform/Linux/BLEManagerImpl.cpp +++ b/src/platform/Linux/BLEManagerImpl.cpp @@ -204,8 +204,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; @@ -464,9 +463,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); } } @@ -475,27 +473,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); } @@ -506,12 +500,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: @@ -530,11 +524,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); } @@ -551,10 +543,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); } } @@ -566,9 +556,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() ? static_cast(DeviceEventType::kCHIPoBLESubscribe) + : static_cast(DeviceEventType::kCHIPoBLEUnsubscribe), + .CHIPoBLESubscribe = { .ConId = conId } }; PlatformMgr().PostEventOrDie(&event); ChipLogProgress(DeviceLayer, "CHIPoBLE %s received", @@ -578,9 +568,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); } @@ -835,50 +823,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/OpenThread/GenericThreadStackManagerImpl_OpenThread.hpp b/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.hpp index 7c3111a2082339..f4548e67829582 100644 --- a/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.hpp +++ b/src/platform/OpenThread/GenericThreadStackManagerImpl_OpenThread.hpp @@ -123,13 +123,14 @@ NetworkCommissioning::otScanResponseIterator void GenericThreadStackManagerImpl_OpenThread::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) @@ -211,11 +212,10 @@ void GenericThreadStackManagerImpl_OpenThread::_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; @@ -317,9 +317,8 @@ CHIP_ERROR GenericThreadStackManagerImpl_OpenThread::_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); } 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); }