/** * * Copyright (c) 2021 Project CHIP Authors * * 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. */ /**************************************************************************** * @file * @brief Routines for the Keypad Input plugin, the *server implementation of the Keypad Input cluster. ******************************************************************************* ******************************************************************************/ #include #include #include #include #include #include #include #include #include #include #include #if CHIP_DEVICE_CONFIG_APP_PLATFORM_ENABLED #include #endif // CHIP_DEVICE_CONFIG_APP_PLATFORM_ENABLED using namespace chip; using namespace chip::app::Clusters; using namespace chip::app::Clusters::KeypadInput; #if CHIP_DEVICE_CONFIG_APP_PLATFORM_ENABLED using namespace chip::AppPlatform; #endif // CHIP_DEVICE_CONFIG_APP_PLATFORM_ENABLED using chip::Protocols::InteractionModel::Status; static constexpr size_t kKeypadInputDelegateTableSize = MATTER_DM_KEYPAD_INPUT_CLUSTER_SERVER_ENDPOINT_COUNT + CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT; static_assert(kKeypadInputDelegateTableSize < kEmberInvalidEndpointIndex, "KeypadInput Delegate table size error"); // ----------------------------------------------------------------------------- // Delegate Implementation using chip::app::Clusters::KeypadInput::Delegate; namespace { Delegate * gDelegateTable[kKeypadInputDelegateTableSize] = { nullptr }; Delegate * GetDelegate(EndpointId endpoint) { #if CHIP_DEVICE_CONFIG_APP_PLATFORM_ENABLED ContentApp * app = ContentAppPlatform::GetInstance().GetContentApp(endpoint); if (app != nullptr) { ChipLogProgress(Zcl, "KeypadInput returning ContentApp delegate for endpoint:%u", endpoint); return app->GetKeypadInputDelegate(); } #endif // CHIP_DEVICE_CONFIG_APP_PLATFORM_ENABLED ChipLogProgress(Zcl, "KeypadInput NOT returning ContentApp delegate for endpoint:%u", endpoint); uint16_t ep = emberAfGetClusterServerEndpointIndex(endpoint, KeypadInput::Id, MATTER_DM_KEYPAD_INPUT_CLUSTER_SERVER_ENDPOINT_COUNT); return (ep >= kKeypadInputDelegateTableSize ? nullptr : gDelegateTable[ep]); } bool isDelegateNull(Delegate * delegate, EndpointId endpoint) { if (delegate == nullptr) { ChipLogProgress(Zcl, "Keypad Input has no delegate set for endpoint:%u", endpoint); return true; } return false; } } // namespace namespace chip { namespace app { namespace Clusters { namespace KeypadInput { void SetDefaultDelegate(EndpointId endpoint, Delegate * delegate) { uint16_t ep = emberAfGetClusterServerEndpointIndex(endpoint, KeypadInput::Id, MATTER_DM_KEYPAD_INPUT_CLUSTER_SERVER_ENDPOINT_COUNT); // if endpoint is found if (ep < kKeypadInputDelegateTableSize) { gDelegateTable[ep] = delegate; } else { } } bool Delegate::HasFeature(EndpointId endpoint, Feature feature) { uint32_t featureMap = GetFeatureMap(endpoint); return (featureMap & chip::to_underlying(feature)); } } // namespace KeypadInput } // namespace Clusters } // namespace app } // namespace chip // ----------------------------------------------------------------------------- // Attribute Accessor Implementation namespace { class KeypadInputAttrAccess : public app::AttributeAccessInterface { public: KeypadInputAttrAccess() : app::AttributeAccessInterface(Optional::Missing(), KeypadInput::Id) {} CHIP_ERROR Read(const app::ConcreteReadAttributePath & aPath, app::AttributeValueEncoder & aEncoder) override; private: CHIP_ERROR ReadFeatureFlagAttribute(EndpointId endpoint, app::AttributeValueEncoder & aEncoder, Delegate * delegate); }; KeypadInputAttrAccess gKeypadInputAttrAccess; CHIP_ERROR KeypadInputAttrAccess::Read(const app::ConcreteReadAttributePath & aPath, app::AttributeValueEncoder & aEncoder) { EndpointId endpoint = aPath.mEndpointId; Delegate * delegate = GetDelegate(endpoint); if (isDelegateNull(delegate, endpoint)) { return CHIP_NO_ERROR; } switch (aPath.mAttributeId) { case app::Clusters::KeypadInput::Attributes::FeatureMap::Id: { return ReadFeatureFlagAttribute(endpoint, aEncoder, delegate); } default: { break; } } return CHIP_NO_ERROR; } CHIP_ERROR KeypadInputAttrAccess::ReadFeatureFlagAttribute(EndpointId endpoint, app::AttributeValueEncoder & aEncoder, Delegate * delegate) { uint32_t featureFlag = delegate->GetFeatureMap(endpoint); return aEncoder.Encode(featureFlag); } } // anonymous namespace // ----------------------------------------------------------------------------- // Matter Framework Callbacks Implementation bool emberAfKeypadInputClusterSendKeyCallback(app::CommandHandler * command, const app::ConcreteCommandPath & commandPath, const Commands::SendKey::DecodableType & commandData) { CHIP_ERROR err = CHIP_NO_ERROR; EndpointId endpoint = commandPath.mEndpointId; auto & keyCode = commandData.keyCode; app::CommandResponseHelper responder(command, commandPath); Delegate * delegate = GetDelegate(endpoint); VerifyOrExit(isDelegateNull(delegate, endpoint) != true, err = CHIP_ERROR_INCORRECT_STATE); { delegate->HandleSendKey(responder, keyCode); } exit: if (err != CHIP_NO_ERROR) { ChipLogError(Zcl, "emberAfKeypadInputClusterSendKeyCallback error: %s", err.AsString()); command->AddStatus(commandPath, Status::Failure); } return true; } void MatterKeypadInputPluginServerInitCallback() { app::AttributeAccessInterfaceRegistry::Instance().Register(&gKeypadInputAttrAccess); }