| /* |
| * |
| * Copyright (c) 2022 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. |
| */ |
| |
| /** |
| * @file DeviceCallbacks.cpp |
| * |
| * Implements all the callbacks to the application from the CHIP Stack |
| * |
| **/ |
| |
| #include "DeviceCallbacks.h" |
| |
| #include <common/CHIPDeviceManager.h> |
| |
| #include <app-common/zap-generated/ids/Clusters.h> |
| #include <app/CommandHandler.h> |
| #include <app/server/Dnssd.h> |
| #include <app/util/basic-types.h> |
| #include <app/util/util.h> |
| #include <lib/dnssd/Advertiser.h> |
| #include <support/CodeUtils.h> |
| #include <support/logging/CHIPLogging.h> |
| #include <support/logging/Constants.h> |
| |
| static const char TAG[] = "app-devicecallbacks"; |
| |
| using namespace ::chip; |
| using namespace ::chip::Inet; |
| using namespace ::chip::System; |
| using namespace ::chip::DeviceLayer; |
| using namespace ::chip::DeviceManager; |
| using namespace ::chip::Logging; |
| |
| uint32_t identifyTimerCount; |
| constexpr uint32_t kIdentifyTimerDelayMS = 250; |
| |
| void AppDeviceCallbacks::PostAttributeChangeCallback(EndpointId endpointId, ClusterId clusterId, AttributeId attributeId, |
| uint8_t type, uint16_t size, uint8_t * value) |
| { |
| switch (clusterId) |
| { |
| case app::Clusters::OnOff::Id: |
| OnOnOffPostAttributeChangeCallback(endpointId, attributeId, value); |
| break; |
| |
| case app::Clusters::Identify::Id: |
| OnIdentifyPostAttributeChangeCallback(endpointId, attributeId, value); |
| break; |
| |
| default: |
| ChipLogProgress(Zcl, "Unknown cluster ID: " ChipLogFormatMEI, ChipLogValueMEI(clusterId)); |
| break; |
| } |
| } |
| |
| void AppDeviceCallbacks::OnOnOffPostAttributeChangeCallback(EndpointId endpointId, AttributeId attributeId, uint8_t * value) |
| { |
| VerifyOrExit(attributeId == app::Clusters::OnOff |
| : Attributes::OnOff::Id, ChipLogError(DeviceLayer, TAG, "Unhandled Attribute ID: '0x%04x", attributeId)); |
| VerifyOrExit(endpointId == 1 || endpointId == 2, |
| ChipLogError(DeviceLayer, TAG, "Unexpected EndPoint ID: `0x%02x'", endpointId)); |
| |
| // At this point we can assume that value points to a bool value. |
| // statusLED1.Set(*value); |
| |
| exit: |
| return; |
| } |
| |
| void IdentifyTimerHandler(Layer * systemLayer, void * appState) |
| { |
| // statusLED1.Animate(); |
| |
| if (identifyTimerCount) |
| { |
| systemLayer->StartTimer(Clock::Milliseconds32(kIdentifyTimerDelayMS), IdentifyTimerHandler, appState); |
| // Decrement the timer count. |
| identifyTimerCount--; |
| } |
| } |
| |
| void AppDeviceCallbacks::OnIdentifyPostAttributeChangeCallback(EndpointId endpointId, AttributeId attributeId, uint8_t * value) |
| { |
| VerifyOrExit(attributeId == app::Clusters::Identify::Attributes::IdentifyTime::Id, |
| ChipLogError(DeviceLayer, "[%s] Unhandled Attribute ID: '0x%04x", TAG, attributeId)); |
| VerifyOrExit(endpointId == 1, ChipLogError(DeviceLayer, "[%s] Unexpected EndPoint ID: `0x%02x'", TAG, endpointId)); |
| |
| // timerCount represents the number of callback executions before we stop the timer. |
| // value is expressed in seconds and the timer is fired every 250ms, so just multiply value by 4. |
| // Also, we want timerCount to be odd number, so the ligth state ends in the same state it starts. |
| identifyTimerCount = (*value) * 4; |
| |
| DeviceLayer::SystemLayer().CancelTimer(IdentifyTimerHandler, this); |
| DeviceLayer::SystemLayer().StartTimer(Clock::Milliseconds32(kIdentifyTimerDelayMS), IdentifyTimerHandler, this); |
| |
| exit: |
| return; |
| } |