blob: 769f1ec420f154313a02763c77b4c3e534e6d77a [file] [log] [blame]
/*
*
* Copyright (c) 2023 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.
*/
#include "Device.h"
#include <cstdio>
#include <lib/support/CHIPMemString.h>
#include <platform/CHIPDeviceLayer.h>
#include <string>
using namespace ::chip::Platform;
Device::Device(const char * szDeviceName, const char * szLocation)
{
CopyString(mName, sizeof(mName), szDeviceName);
CopyString(mLocation, sizeof(mLocation), szLocation);
mState = kState_Off;
mReachable = false;
mEndpointId = 0;
mChanged_CB = nullptr;
}
bool Device::IsOn() const
{
return mState == kState_On;
}
bool Device::IsReachable() const
{
return mReachable;
}
void Device::SetOnOff(bool aOn)
{
bool changed;
if (aOn)
{
changed = (mState != kState_On);
mState = kState_On;
ChipLogProgress(DeviceLayer, "Device[%s]: ON", mName);
}
else
{
changed = (mState != kState_Off);
mState = kState_Off;
ChipLogProgress(DeviceLayer, "Device[%s]: OFF", mName);
}
if (changed && mChanged_CB)
{
mChanged_CB(this, kChanged_State);
}
}
void Device::SetReachable(bool aReachable)
{
bool changed = (mReachable != aReachable);
mReachable = aReachable;
if (aReachable)
{
ChipLogProgress(DeviceLayer, "Device[%s]: ONLINE", mName);
}
else
{
ChipLogProgress(DeviceLayer, "Device[%s]: OFFLINE", mName);
}
if (changed && mChanged_CB)
{
mChanged_CB(this, kChanged_Reachable);
}
}
void Device::SetName(const char * szName)
{
bool changed = (strncmp(mName, szName, sizeof(mName)) != 0);
ChipLogProgress(DeviceLayer, "Device[%s]: New Name=\"%s\"", mName, szName);
CopyString(mName, sizeof(mName), szName);
if (changed && mChanged_CB)
{
mChanged_CB(this, kChanged_Name);
}
}
void Device::SetLocation(const char * szLocation)
{
bool changed = (strncmp(mLocation, szLocation, sizeof(mLocation)) != 0);
CopyString(mLocation, sizeof(mLocation), szLocation);
ChipLogProgress(DeviceLayer, "Device[%s]: Location=\"%s\"", mName, mLocation);
if (changed && mChanged_CB)
{
mChanged_CB(this, kChanged_Location);
}
}
void Device::SetChangeCallback(DeviceCallback_fn aChanged_CB)
{
mChanged_CB = aChanged_CB;
}
DeviceTempSensor::DeviceTempSensor(const char * szDeviceName, std::string szLocation, int16_t min, int16_t max,
int16_t measuredValue) :
Device(szDeviceName, szLocation.c_str()),
mMin(min), mMax(max), mMeasurement(measuredValue)
{}
void DeviceTempSensor::SetMeasuredValue(int16_t measurement)
{
// Limit measurement based on the min and max.
if (measurement < mMin)
{
measurement = mMin;
}
else if (measurement > mMax)
{
measurement = mMax;
}
bool changed = mMeasurement != measurement;
ChipLogProgress(DeviceLayer, "TempSensorDevice new measurement=\"%d\"", measurement);
mMeasurement = measurement;
if (changed && mChanged_CB)
{
mChanged_CB(this, kChanged_MeasurementValue);
}
}
void DeviceTempSensor::SetChangeCallback(DeviceCallback_fn aChanged_CB)
{
mChanged_CB = aChanged_CB;
}
void DeviceTempSensor::HandleDeviceChange(Device * device, Device::Changed_t changeMask)
{
if (mChanged_CB)
{
mChanged_CB(this, (DeviceTempSensor::Changed_t) changeMask);
}
}