blob: b5c98c1a4f9c17df2549e41816a0ff21f1822afa [file] [log] [blame]
/*
*
* Copyright (c) 2020 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 "Globals.h"
#include "TemperatureManager.h"
#if DLPS_EN
#include "matter_gpio.h"
#endif
#include <app-common/zap-generated/attributes/Accessors.h>
#include <app-common/zap-generated/ids/Attributes.h>
#include <app-common/zap-generated/ids/Clusters.h>
#include <app/ConcreteAttributePath.h>
#include <app/data-model/Nullable.h>
#include <app/server/Dnssd.h>
#include <lib/core/DataModelTypes.h>
#include <lib/support/logging/CHIPLogging.h>
#if CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR
#include <ota/OTAInitializer.h>
#endif
using namespace ::chip;
using namespace ::chip::app;
using namespace ::chip::app::Clusters;
using namespace ::chip::Inet;
using namespace ::chip::System;
using namespace ::chip::DeviceLayer;
using namespace ::chip::DeviceManager;
constexpr uint32_t kInitOTARequestorDelaySec = 3;
bool sIsNetworkProvisioned = false;
bool sIsNetworkEnabled = false;
bool sHaveBLEConnections = false;
namespace LedConsts {
namespace StatusLed {
namespace Unprovisioned {
constexpr uint32_t kOn_ms{ 100 };
constexpr uint32_t kOff_ms{ kOn_ms };
} /* namespace Unprovisioned */
namespace Provisioned {
constexpr uint32_t kOn_ms{ 50 };
constexpr uint32_t kOff_ms{ 950 };
} /* namespace Provisioned */
} /* namespace StatusLed */
} /* namespace LedConsts */
#if CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR
static bool isOTAInitialized = false;
void InitOTARequestorHandler(System::Layer * systemLayer, void * appState)
{
ChipLogProgress(Zcl, "InitOTARequestorHandler");
OTAInitializer::Instance().InitOTARequestor();
}
#endif
void DeviceCallbacks::UpdateStatusLED()
{
// Update the status LED.
//
// If IPv6 network and service provisioned, keep the LED Off constantly.
//
// If the system has ble connection(s) uptill the stage above, THEN blink the LED at an even
// rate of 100ms.
//
// Otherwise, blink the LED for a very short time.
if (sIsNetworkProvisioned && sIsNetworkEnabled)
{
systemStatusLED.Set(false);
}
else if (sHaveBLEConnections)
{
systemStatusLED.Blink(LedConsts::StatusLed::Unprovisioned::kOn_ms, LedConsts::StatusLed::Unprovisioned::kOff_ms);
}
else
{
// not commisioned yet
systemStatusLED.Blink(LedConsts::StatusLed::Provisioned::kOn_ms, LedConsts::StatusLed::Provisioned::kOff_ms);
}
}
void DeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, intptr_t arg)
{
// ChipLogProgress(Zcl, "DeviceEventCallback event_type 0x%x", event->Type);
switch (event->Type)
{
case DeviceEventType::kCHIPoBLEAdvertisingChange:
sHaveBLEConnections = ConnectivityMgr().NumBLEConnections() != 0;
UpdateStatusLED();
break;
case DeviceEventType::kInternetConnectivityChange:
OnInternetConnectivityChange(event);
break;
case DeviceEventType::kInterfaceIpAddressChanged:
if ((event->InterfaceIpAddressChanged.Type == InterfaceIpChangeType::kIpV4_Assigned) ||
(event->InterfaceIpAddressChanged.Type == InterfaceIpChangeType::kIpV6_Assigned))
{
// MDNS server restart on any ip assignment: if link local ipv6 is configured, that
// will not trigger a 'internet connectivity change' as there is no internet
// connectivity. MDNS still wants to refresh its listening interfaces to include the
// newly selected address.
chip::app::DnssdServer::Instance().StartServer();
}
break;
case DeviceEventType::kThreadConnectivityChange:
break;
case DeviceEventType::kThreadStateChange:
sIsNetworkProvisioned = ConnectivityMgr().IsThreadProvisioned();
sIsNetworkEnabled = ConnectivityMgr().IsThreadEnabled();
UpdateStatusLED();
break;
case DeviceEventType::kCommissioningComplete:
break;
case DeviceEventType::kServerReady:
#if DLPS_EN
matter_gpio_allow_to_enter_dlps();
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_OTA_REQUESTOR
if (!isOTAInitialized)
{
chip::DeviceLayer::SystemLayer().StartTimer(chip::System::Clock::Seconds32(kInitOTARequestorDelaySec),
InitOTARequestorHandler, nullptr);
isOTAInitialized = true;
}
#endif
break;
}
}
void DeviceCallbacks::OnInternetConnectivityChange(const ChipDeviceEvent * event)
{
if (event->InternetConnectivityChange.IPv4 == kConnectivity_Established)
{
ChipLogProgress(Zcl, "IPv4 Server ready...");
chip::app::DnssdServer::Instance().StartServer();
}
else if (event->InternetConnectivityChange.IPv4 == kConnectivity_Lost)
{
ChipLogProgress(Zcl, "Lost IPv4 connectivity...");
}
if (event->InternetConnectivityChange.IPv6 == kConnectivity_Established)
{
ChipLogProgress(Zcl, "IPv6 Server ready...");
chip::app::DnssdServer::Instance().StartServer();
}
else if (event->InternetConnectivityChange.IPv6 == kConnectivity_Lost)
{
ChipLogProgress(Zcl, "Lost IPv6 connectivity...");
}
}
void MatterPostAttributeChangeCallback(const chip::app::ConcreteAttributePath & attributePath, uint8_t type, uint16_t size,
uint8_t * value)
{
ClusterId clusterId = attributePath.mClusterId;
AttributeId attributeId = attributePath.mAttributeId;
// ChipLogProgress(Zcl, "Cluster callback: " ChipLogFormatMEI, ChipLogValueMEI(clusterId));
switch (clusterId)
{
case Identify::Id:
ChipLogProgress(Zcl, "Identify attribute ID: " ChipLogFormatMEI " Type: %u Value: %u, length %u",
ChipLogValueMEI(attributeId), type, *value, size);
break;
case Thermostat::Id:
TemperatureManager::Instance().AttributeChangeHandler(attributePath.mEndpointId, attributeId, value, size);
default:
ChipLogProgress(Zcl, "Unknown cluster ID: " ChipLogFormatMEI, ChipLogValueMEI(clusterId));
break;
}
}