From 403f8379858c557ecbf19482e509d0f7b3732b09 Mon Sep 17 00:00:00 2001 From: Andrei Litvin Date: Tue, 26 Mar 2024 11:59:51 -0400 Subject: [PATCH] Remove weak-linkage usage for matter callbacks for pre/post attribute read/write and command invocation (#32673) * Move placeholder commands to use actual callback classes * Remove the weak redirects ... they don't seem used --- .../linux/include/MatterCallbacks.h | 40 ----------- examples/placeholder/linux/main.cpp | 40 ++++++++++- src/app/CommandHandler.cpp | 2 +- src/app/util/MatterCallbacks.cpp | 67 +------------------ 4 files changed, 42 insertions(+), 107 deletions(-) delete mode 100644 examples/placeholder/linux/include/MatterCallbacks.h diff --git a/examples/placeholder/linux/include/MatterCallbacks.h b/examples/placeholder/linux/include/MatterCallbacks.h deleted file mode 100644 index d7ba67e2b38200..00000000000000 --- a/examples/placeholder/linux/include/MatterCallbacks.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * - * Copyright (c) 2021 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. - */ - -#pragma once - -#include "InteractiveServer.h" - -#include -#include - -void MatterPostCommandReceivedCallback(const chip::app::ConcreteCommandPath & commandPath, - const chip::Access::SubjectDescriptor & subjectDescriptor) -{ - VerifyOrReturn(!InteractiveServer::GetInstance().Command(commandPath)); -} - -void MatterPostAttributeReadCallback(const chip::app::ConcreteAttributePath & attributePath) -{ - VerifyOrReturn(!InteractiveServer::GetInstance().ReadAttribute(attributePath)); -} - -void MatterPostAttributeWriteCallback(const chip::app::ConcreteAttributePath & attributePath) -{ - VerifyOrReturn(!InteractiveServer::GetInstance().WriteAttribute(attributePath)); -} diff --git a/examples/placeholder/linux/main.cpp b/examples/placeholder/linux/main.cpp index ab5fa266dd6392..6485044a7ed6d8 100644 --- a/examples/placeholder/linux/main.cpp +++ b/examples/placeholder/linux/main.cpp @@ -18,7 +18,44 @@ #include "AppMain.h" #include "AppOptions.h" -#include "MatterCallbacks.h" +#include "InteractiveServer.h" + +#include + +namespace { +class InteractiveServerRedirectCallbacks : public chip::DataModelCallbacks +{ +public: + void AttributeOperation(OperationType operation, OperationOrder order, const chip::app::ConcreteAttributePath & path) override + { + if (order != OperationOrder::Post) + { + return; + } + + // TODO: is there any value in checking the return of read/write attributes? + // they seem to only return true/false based on isRead (i.e. commissioning complete) + switch (operation) + { + case OperationType::Read: + (void) InteractiveServer::GetInstance().ReadAttribute(path); + break; + case OperationType::Write: + (void) InteractiveServer::GetInstance().WriteAttribute(path); + break; + } + } + + void PostCommandReceived(const chip::app::ConcreteCommandPath & commandPath, + const chip::Access::SubjectDescriptor & subjectDescriptor) override + { + (void) InteractiveServer::GetInstance().Command(commandPath); + } +}; + +InteractiveServerRedirectCallbacks gDmCallbacks; + +} // namespace void ApplicationInit() {} @@ -36,6 +73,7 @@ int main(int argc, char * argv[]) server.Run(AppOptions::GetInteractiveModePort()); } + chip::DataModelCallbacks::SetInstance(&gDmCallbacks); ChipLinuxAppMainLoop(); return 0; diff --git a/src/app/CommandHandler.cpp b/src/app/CommandHandler.cpp index f80f41e9a63c61..d9bd50251ab250 100644 --- a/src/app/CommandHandler.cpp +++ b/src/app/CommandHandler.cpp @@ -564,7 +564,7 @@ Status CommandHandler::ProcessGroupCommandDataIB(CommandDataIB::Parser & aComman else { ChipLogError(DataManagement, - "Error when calling MatterPreCommandReceivedCallback for Endpoint=%u Cluster=" ChipLogFormatMEI + "Error when calling PreCommandReceived for Endpoint=%u Cluster=" ChipLogFormatMEI " Command=" ChipLogFormatMEI " : %" CHIP_ERROR_FORMAT, mapping.endpoint_id, ChipLogValueMEI(clusterId), ChipLogValueMEI(commandId), err.Format()); continue; diff --git a/src/app/util/MatterCallbacks.cpp b/src/app/util/MatterCallbacks.cpp index 71ab7fdc9df65d..76576c86223242 100644 --- a/src/app/util/MatterCallbacks.cpp +++ b/src/app/util/MatterCallbacks.cpp @@ -15,74 +15,11 @@ */ #include "MatterCallbacks.h" -// The defines below are using link-time callback and should be removed -// -// TODO: applications should be converted to use DataModelCallbacks instead -// of relying on weak linkage -void __attribute__((weak)) MatterPreAttributeReadCallback(const chip::app::ConcreteAttributePath & attributePath) {} -void __attribute__((weak)) MatterPostAttributeReadCallback(const chip::app::ConcreteAttributePath & attributePath) {} -void __attribute__((weak)) MatterPreAttributeWriteCallback(const chip::app::ConcreteAttributePath & attributePath) {} -void __attribute__((weak)) MatterPostAttributeWriteCallback(const chip::app::ConcreteAttributePath & attributePath) {} -CHIP_ERROR __attribute__((weak)) MatterPreCommandReceivedCallback(const chip::app::ConcreteCommandPath & commandPath, - const chip::Access::SubjectDescriptor & subjectDescriptor) -{ - return CHIP_NO_ERROR; -} -void __attribute__((weak)) MatterPostCommandReceivedCallback(const chip::app::ConcreteCommandPath & commandPath, - const chip::Access::SubjectDescriptor & subjectDescriptor) -{} - namespace chip { namespace { -class WeakRedirectCallbacks : public DataModelCallbacks -{ -public: - void AttributeOperation(OperationType operation, OperationOrder order, const chip::app::ConcreteAttributePath & path) override - { - switch (operation) - { - case OperationType::Read: - switch (order) - { - case OperationOrder::Pre: - MatterPreAttributeReadCallback(path); - break; - case OperationOrder::Post: - MatterPostAttributeReadCallback(path); - break; - } - break; - case OperationType::Write: - switch (order) - { - case OperationOrder::Pre: - MatterPreAttributeWriteCallback(path); - break; - case OperationOrder::Post: - MatterPostAttributeWriteCallback(path); - break; - } - break; - } - } - - CHIP_ERROR PreCommandReceived(const chip::app::ConcreteCommandPath & commandPath, - const chip::Access::SubjectDescriptor & subjectDescriptor) override - { - return MatterPreCommandReceivedCallback(commandPath, subjectDescriptor); - } - - void PostCommandReceived(const chip::app::ConcreteCommandPath & commandPath, - const chip::Access::SubjectDescriptor & subjectDescriptor) override - { - - MatterPostCommandReceivedCallback(commandPath, subjectDescriptor); - } -}; - -WeakRedirectCallbacks gWeakCallbacks; -DataModelCallbacks * gInstance = &gWeakCallbacks; +DataModelCallbacks gNoopCallbacks; +DataModelCallbacks * gInstance = &gNoopCallbacks; } // namespace