Skip to content

Commit

Permalink
Actions cluster test support (#19487)
Browse files Browse the repository at this point in the history
* Added support for action clusters tests to the Linux bridge-app example.

Commands were added to support the actions cluster test plan
for renaming rooms, adding and removing rooms, and changing
rooms so that they are not visible in the endpoint lists.

* Whitespace updates.

* Restyled by clang-format

* Build fixes and removed unused capture.

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
jwinder-ca and restyled-commits authored Jun 16, 2022
1 parent 75d0257 commit 1e3f120
Show file tree
Hide file tree
Showing 6 changed files with 252 additions and 21 deletions.
1 change: 1 addition & 0 deletions examples/bridge-app/linux/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ executable("chip-bridge-app") {
"${chip_root}/examples/tv-app/tv-common/include/CHIPProjectAppConfig.h",
"Device.cpp",
"include/Device.h",
"include/main.h",
"main.cpp",
]

Expand Down
47 changes: 39 additions & 8 deletions examples/bridge-app/linux/Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@
#include <cstdio>
#include <platform/CHIPDeviceLayer.h>

using namespace chip::app::Clusters::BridgedActions;

// LightingManager LightingManager::sLight;

Device::Device(const char * szDeviceName, const char * szLocation)
Device::Device(const char * szDeviceName, std::string szLocation)
{
strncpy(mName, szDeviceName, sizeof(mName));
strncpy(mLocation, szLocation, sizeof(mLocation));
mLocation = szLocation;
mReachable = false;
mEndpointId = 0;
}
Expand Down Expand Up @@ -72,21 +74,21 @@ void Device::SetName(const char * szName)
}
}

void Device::SetLocation(const char * szLocation)
void Device::SetLocation(std::string szLocation)
{
bool changed = (strncmp(mLocation, szLocation, sizeof(mLocation)) != 0);
bool changed = (mLocation.compare(szLocation) != 0);

strncpy(mLocation, szLocation, sizeof(mLocation));
mLocation = szLocation;

ChipLogProgress(DeviceLayer, "Device[%s]: Location=\"%s\"", mName, mLocation);
ChipLogProgress(DeviceLayer, "Device[%s]: Location=\"%s\"", mName, mLocation.c_str());

if (changed)
{
HandleDeviceChange(this, kChanged_Location);
}
}

DeviceOnOff::DeviceOnOff(const char * szDeviceName, const char * szLocation) : Device(szDeviceName, szLocation)
DeviceOnOff::DeviceOnOff(const char * szDeviceName, std::string szLocation) : Device(szDeviceName, szLocation)
{
mOn = false;
}
Expand Down Expand Up @@ -129,7 +131,7 @@ void DeviceOnOff::HandleDeviceChange(Device * device, Device::Changed_t changeMa
}
}

DeviceSwitch::DeviceSwitch(const char * szDeviceName, const char * szLocation, uint32_t aFeatureMap) :
DeviceSwitch::DeviceSwitch(const char * szDeviceName, std::string szLocation, uint32_t aFeatureMap) :
Device(szDeviceName, szLocation)
{
mNumberOfPositions = 2;
Expand Down Expand Up @@ -231,3 +233,32 @@ void DevicePowerSource::SetDescription(std::string aDescription)
mChanged_CB(this, kChanged_Description);
}
}

EndpointListInfo::EndpointListInfo(uint16_t endpointListId, std::string name, EndpointListTypeEnum type)
{
mEndpointListId = endpointListId;
mName = name;
mType = type;
}

EndpointListInfo::EndpointListInfo(uint16_t endpointListId, std::string name, EndpointListTypeEnum type,
chip::EndpointId endpointId)
{
mEndpointListId = endpointListId;
mName = name;
mType = type;
mEndpoints.push_back(endpointId);
}

void EndpointListInfo::AddEndpointId(chip::EndpointId endpointId)
{
mEndpoints.push_back(endpointId);
}

Room::Room(std::string name, uint16_t endpointListId, EndpointListTypeEnum type, bool isVisible)
{
mName = name;
mEndpointListId = endpointListId;
mType = type;
mIsVisible = isVisible;
}
21 changes: 19 additions & 2 deletions examples/bridge-app/linux/bridged-actions-stub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
#include <lib/support/CodeUtils.h>
#include <lib/support/logging/CHIPLogging.h>

#include <vector>

#include "Device.h"
#include "main.h"

using namespace chip;
using namespace chip::app;
using namespace chip::app::Clusters;
Expand Down Expand Up @@ -58,8 +63,20 @@ CHIP_ERROR BridgedActionsAttrAccess::ReadActionListAttribute(EndpointId endpoint

CHIP_ERROR BridgedActionsAttrAccess::ReadEndpointListAttribute(EndpointId endpoint, AttributeValueEncoder & aEncoder)
{
// Just return an empty list
return aEncoder.EncodeEmptyList();
std::vector<EndpointListInfo> infoList = GetEndpointListInfo(endpoint);

CHIP_ERROR err = aEncoder.EncodeList([&infoList](const auto & encoder) -> CHIP_ERROR {
for (auto info : infoList)
{
BridgedActions::Structs::EndpointListStruct::Type endpointListStruct = {
info.GetEndpointListId(), CharSpan::fromCharString(info.GetName().c_str()), info.GetType(),
DataModel::List<chip::EndpointId>(info.GetEndpointListData(), info.GetEndpointListSize())
};
ReturnErrorOnFailure(encoder.Encode(endpointListStruct));
}
return CHIP_NO_ERROR;
});
return err;
}

CHIP_ERROR BridgedActionsAttrAccess::ReadSetupUrlAttribute(EndpointId endpoint, AttributeValueEncoder & aEncoder)
Expand Down
66 changes: 56 additions & 10 deletions examples/bridge-app/linux/include/Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@
#include <stdint.h>

#include <functional>
#include <vector>

using namespace chip::app::Clusters;
using namespace chip::app::Clusters::BridgedActions;

class Device
{
public:
static const int kDeviceNameSize = 32;
static const int kDeviceLocationSize = 32;
static const int kDeviceNameSize = 32;

enum Changed_t
{
Expand All @@ -39,26 +42,32 @@ class Device
kChanged_Last = kChanged_Name,
} Changed;

Device(const char * szDeviceName, const char * szLocation);
Device(const char * szDeviceName, std::string szLocation);
virtual ~Device() {}

bool IsReachable();
void SetReachable(bool aReachable);
void SetName(const char * szDeviceName);
void SetLocation(const char * szLocation);
void SetLocation(std::string szLocation);
inline void SetEndpointId(chip::EndpointId id) { mEndpointId = id; };
inline chip::EndpointId GetEndpointId() { return mEndpointId; };
inline void SetParentEndpointId(chip::EndpointId id) { mParentEndpointId = id; };
inline chip::EndpointId GetParentEndpointId() { return mParentEndpointId; };
inline char * GetName() { return mName; };
inline char * GetLocation() { return mLocation; };
inline std::string GetLocation() { return mLocation; };
inline std::string GetZone() { return mZone; };
inline void SetZone(std::string zone) { mZone = zone; };

private:
virtual void HandleDeviceChange(Device * device, Device::Changed_t changeMask) = 0;

protected:
bool mReachable;
char mName[kDeviceNameSize];
char mLocation[kDeviceLocationSize];
std::string mLocation;
chip::EndpointId mEndpointId;
chip::EndpointId mParentEndpointId;
std::string mZone;
};

class DeviceOnOff : public Device
Expand All @@ -69,7 +78,7 @@ class DeviceOnOff : public Device
kChanged_OnOff = kChanged_Last << 1,
} Changed;

DeviceOnOff(const char * szDeviceName, const char * szLocation);
DeviceOnOff(const char * szDeviceName, std::string szLocation);

bool IsOn();
void SetOnOff(bool aOn);
Expand All @@ -96,7 +105,7 @@ class DeviceSwitch : public Device
kChanged_MultiPressMax = kChanged_Last << 3,
} Changed;

DeviceSwitch(const char * szDeviceName, const char * szLocation, uint32_t aFeatureMap);
DeviceSwitch(const char * szDeviceName, std::string szLocation, uint32_t aFeatureMap);

void SetNumberOfPositions(uint8_t aNumberOfPositions);
void SetCurrentPosition(uint8_t aCurrentPosition);
Expand Down Expand Up @@ -124,7 +133,7 @@ class DeviceSwitch : public Device
class ComposedDevice : public Device
{
public:
ComposedDevice(const char * szDeviceName, const char * szLocation) : Device(szDeviceName, szLocation){};
ComposedDevice(const char * szDeviceName, std::string szLocation) : Device(szDeviceName, szLocation){};

using DeviceCallback_fn = std::function<void(ComposedDevice *, ComposedDevice::Changed_t)>;

Expand All @@ -146,7 +155,7 @@ class DevicePowerSource : public Device
kChanged_Description = kChanged_Last << 2,
} Changed;

DevicePowerSource(const char * szDeviceName, const char * szLocation, uint32_t aFeatureMap) :
DevicePowerSource(const char * szDeviceName, std::string szLocation, uint32_t aFeatureMap) :
Device(szDeviceName, szLocation), mFeatureMap(aFeatureMap){};

using DeviceCallback_fn = std::function<void(DevicePowerSource *, DevicePowerSource::Changed_t)>;
Expand All @@ -172,3 +181,40 @@ class DevicePowerSource : public Device
uint32_t mFeatureMap;
DeviceCallback_fn mChanged_CB;
};

class EndpointListInfo
{
public:
EndpointListInfo(uint16_t endpointListId, std::string name, EndpointListTypeEnum type);
EndpointListInfo(uint16_t endpointListId, std::string name, EndpointListTypeEnum type, chip::EndpointId endpointId);
void AddEndpointId(chip::EndpointId endpointId);
inline uint16_t GetEndpointListId() { return mEndpointListId; };
std::string GetName() { return mName; };
inline EndpointListTypeEnum GetType() { return mType; };
inline chip::EndpointId * GetEndpointListData() { return mEndpoints.data(); };
inline size_t GetEndpointListSize() { return mEndpoints.size(); };

private:
uint16_t mEndpointListId = static_cast<uint16_t>(0);
std::string mName;
EndpointListTypeEnum mType = static_cast<EndpointListTypeEnum>(0);
std::vector<chip::EndpointId> mEndpoints;
};

class Room
{
public:
Room(std::string name, uint16_t endpointListId, EndpointListTypeEnum type, bool isVisible);
inline void setIsVisible(bool isVisible) { mIsVisible = isVisible; };
inline bool getIsVisible() { return mIsVisible; };
inline void setName(std::string name) { mName = name; };
inline std::string getName() { return mName; };
inline EndpointListTypeEnum getType() { return mType; };
inline uint16_t getEndpointListId() { return mEndpointListId; };

private:
bool mIsVisible;
std::string mName;
uint16_t mEndpointListId;
EndpointListTypeEnum mType;
};
21 changes: 21 additions & 0 deletions examples/bridge-app/linux/include/main.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
*
* 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.
*/

#pragma once

std::vector<EndpointListInfo> GetEndpointListInfo(chip::EndpointId parentId);
Loading

0 comments on commit 1e3f120

Please sign in to comment.