Skip to content

Commit

Permalink
Added ChipDeviceEventHandler impl
Browse files Browse the repository at this point in the history
  • Loading branch information
sharadb-amazon committed Oct 10, 2023
1 parent 2f02f3b commit 13712ef
Show file tree
Hide file tree
Showing 7 changed files with 212 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@
#include "../JNIDACProvider.h"
#include "../support/ErrorConverter-JNI.h"
#include "../support/RotatingDeviceIdUniqueIdProvider-JNI.h"
#include "core/CastingApp.h" // from tv-casting-common

// from tv-casting-common
#include "core/CastingApp.h"
#include "support/ChipDeviceEventHandler.h"

#include <app/clusters/bindings/BindingManager.h>
#include <app/server/Server.h>
Expand Down Expand Up @@ -79,20 +82,25 @@ JNI_METHOD(jobject, finishStartup)(JNIEnv *, jobject)
{
chip::DeviceLayer::StackLock lock;
ChipLogProgress(AppServer, "JNI_METHOD CastingAppJNI.finishStartup called");
auto & server = chip::Server::GetInstance();

CHIP_ERROR err = CHIP_NO_ERROR;
auto & server = chip::Server::GetInstance();

// TODO: Set AppDelegate
// &server.GetCommissioningWindowManager().SetAppDelegate(??);

// Initialize binding handlers
chip::BindingManager::GetInstance().Init(
err = chip::BindingManager::GetInstance().Init(
{ &server.GetFabricTable(), server.GetCASESessionManager(), &server.GetPersistentStorage() });
VerifyOrReturnValue(err == CHIP_NO_ERROR, support::createJMatterError(err),
ChipLogError(AppServer, "Failed to init BindingManager %" CHIP_ERROR_FORMAT, err.Format()));

// TODO: Set FabricDelegate
// chip::Server::GetInstance().GetFabricTable().AddFabricDelegate(&mPersistenceManager);

// TODO: Add DeviceEvent Handler
// ReturnErrorOnFailure(DeviceLayer::PlatformMgrImpl().AddEventHandler(DeviceEventCallback, 0));
err = chip::DeviceLayer::PlatformMgrImpl().AddEventHandler(support::ChipDeviceEventHandler::Handle, 0);
VerifyOrReturnValue(err == CHIP_NO_ERROR, support::createJMatterError(err),
ChipLogError(AppServer, "Failed to register ChipDeviceEventHandler %" CHIP_ERROR_FORMAT, err.Format()));

return support::createJMatterError(CHIP_NO_ERROR);
}
Expand Down
41 changes: 22 additions & 19 deletions examples/tv-casting-app/linux/simple-app-helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,31 +38,32 @@ DiscoveryDelegateImpl * DiscoveryDelegateImpl::GetInstance()
return _discoveryDelegateImpl;
}

void DiscoveryDelegateImpl::HandleOnAdded(matter::casting::memory::Strong<matter::casting::core::CastingPlayer> player)
void DiscoveryDelegateImpl::HandleOnAdded(matter::casting::memory::Strong<matter::casting::core::CastingPlayer> player)
{
if (commissionersCount == 0)
{
if (commissionersCount == 0)
{
ChipLogProgress(AppServer, "Select discovered Casting Player (start index = 0) to request commissioning");
ChipLogProgress(AppServer, "Select discovered Casting Player (start index = 0) to request commissioning");

ChipLogProgress(AppServer, "Example: cast request 0");
}
ChipLogProgress(AppServer, "Discovered CastingPlayer #%d", commissionersCount);
++commissionersCount;
player->LogDetail();
ChipLogProgress(AppServer, "Example: cast request 0");
}
ChipLogProgress(AppServer, "Discovered CastingPlayer #%d", commissionersCount);
++commissionersCount;
player->LogDetail();
}

void DiscoveryDelegateImpl::HandleOnUpdated(matter::casting::memory::Strong<matter::casting::core::CastingPlayer> player)
{
ChipLogProgress(AppServer, "Updated CastingPlayer with ID: %s", player->GetId());
}
void DiscoveryDelegateImpl::HandleOnUpdated(matter::casting::memory::Strong<matter::casting::core::CastingPlayer> player)
{
ChipLogProgress(AppServer, "Updated CastingPlayer with ID: %s", player->GetId());
}

static CHIP_ERROR PrintAllCommands()
{
chip::Shell::streamer_t * sout = chip::Shell::streamer_get();
streamer_printf(sout, " help Usage: cast <subcommand>\r\n");
streamer_printf(sout, " print-bindings Usage: cast print-bindings\r\n");
streamer_printf(sout, " print-fabrics Usage: cast print-fabrics\r\n");
streamer_printf(sout,
streamer_printf(
sout,
" delete-fabric <index> Delete a fabric from the casting client's fabric store. Usage: cast delete-fabric 1\r\n");
streamer_printf(sout, " discover Discover Casting Players. Usage: cast discover\r\n");
streamer_printf(sout, " stop-discovery Stop Discovery of Casting Players. Usage: cast stop-discovery\r\n");
Expand Down Expand Up @@ -104,9 +105,9 @@ void PrintFabrics()
}
}

void ConnectionHandler(matter::casting::core::ConnectionError err)
void ConnectionHandler(CHIP_ERROR err, matter::casting::core::CastingPlayer * castingPlayer)
{
ChipLogProgress(AppServer, "ConnectionHandler called with %d", err);
ChipLogProgress(AppServer, "ConnectionHandler called with %" CHIP_ERROR_FORMAT, err.Format());
}

static CHIP_ERROR CommandHandler(int argc, char ** argv)
Expand Down Expand Up @@ -136,9 +137,10 @@ static CHIP_ERROR CommandHandler(int argc, char ** argv)
}
char * eptr;
unsigned long index = static_cast<unsigned long>(strtol(argv[1], &eptr, 10));
std::vector<matter::casting::memory::Strong<matter::casting::core::CastingPlayer>> castingPlayers = matter::casting::core::CastingPlayerDiscovery::GetInstance()->GetCastingPlayers();
std::vector<matter::casting::memory::Strong<matter::casting::core::CastingPlayer>> castingPlayers =
matter::casting::core::CastingPlayerDiscovery::GetInstance()->GetCastingPlayers();
VerifyOrReturnValue(0 <= index && index < castingPlayers.size(), CHIP_ERROR_INVALID_ARGUMENT,
ChipLogError(AppServer, "Invalid casting player index provided: %lu", index));
ChipLogError(AppServer, "Invalid casting player index provided: %lu", index));
std::shared_ptr<matter::casting::core::CastingPlayer> targetCastingPlayer = castingPlayers.at(index);
targetCastingPlayer->Connect(ConnectionHandler);
return CHIP_NO_ERROR;
Expand All @@ -165,7 +167,8 @@ static CHIP_ERROR CommandHandler(int argc, char ** argv)

void RegisterCommands()
{
static const chip::Shell::shell_command_t sDeviceComand = { &CommandHandler, "cast", "Casting commands. Usage: cast [command_name]" };
static const chip::Shell::shell_command_t sDeviceComand = { &CommandHandler, "cast",
"Casting commands. Usage: cast [command_name]" };

// Register the root `device` command with the top-level shell.
chip::Shell::Engine::Root().RegisterCommands(&sDeviceComand, 1);
Expand Down
2 changes: 1 addition & 1 deletion examples/tv-casting-app/linux/simple-app-helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
class DiscoveryDelegateImpl : public matter::casting::core::DiscoveryDelegate
{
private:
DiscoveryDelegateImpl() {};
DiscoveryDelegateImpl(){};
static DiscoveryDelegateImpl * _discoveryDelegateImpl;
int commissionersCount = 0;

Expand Down
23 changes: 20 additions & 3 deletions examples/tv-casting-app/tv-casting-common/core/CastingPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,29 @@ namespace matter {
namespace casting {
namespace core {

CastingPlayer * CastingPlayer::mTargetCastingPlayer = nullptr;

void CastingPlayer::Connect(ConnectCallback onCompleted, const chip::System::Clock::Seconds16 commissioningWindowTimeout)
{
chip::Inet::IPAddress * ipAddressToUse = GetIpAddressForUDCRequest();
ReturnOnFailure(chip::Server::GetInstance().SendUserDirectedCommissioningRequest(chip::Transport::PeerAddress::UDP(
*ipAddressToUse, mAttributes.port, mAttributes.interfaceId)));
ReturnOnFailure(chip::Server::GetInstance().GetCommissioningWindowManager().OpenBasicCommissioningWindow(kCommissioningWindowTimeout));
CHIP_ERROR err = ipAddressToUse != nullptr ? CHIP_NO_ERROR : CHIP_ERROR_INCORRECT_STATE;
VerifyOrExit(ipAddressToUse != nullptr, ChipLogError(AppServer, "No IP Address found to send UDC request to"));

SuccessOrExit(err = support::ChipDeviceEventHandler::SetUdcStatus(true));
mOnCompleted = onCompleted;
mTargetCastingPlayer = this;

SuccessOrExit(err = chip::Server::GetInstance().SendUserDirectedCommissioningRequest(
chip::Transport::PeerAddress::UDP(*ipAddressToUse, mAttributes.port, mAttributes.interfaceId)));
SuccessOrExit(err = chip::Server::GetInstance().GetCommissioningWindowManager().OpenBasicCommissioningWindow(
kCommissioningWindowTimeout));

exit:
if (err != CHIP_NO_ERROR)
{
support::ChipDeviceEventHandler::SetUdcStatus(false);
ChipLogError(AppServer, "CastingPlayer::Connect failed with %" CHIP_ERROR_FORMAT, err.Format());
}
}

void CastingPlayer::LogDetail() const
Expand Down
19 changes: 11 additions & 8 deletions examples/tv-casting-app/tv-casting-common/core/CastingPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
#pragma once

#include "Types.h"
#include "lib/support/logging/CHIPLogging.h"
#include "support/ChipDeviceEventHandler.h"

#include "lib/support/logging/CHIPLogging.h"
#include <inet/IPAddress.h>
#include <inet/InetInterface.h>
#include <string.h>
Expand All @@ -30,13 +31,8 @@ namespace matter {
namespace casting {
namespace core {

enum ConnectionError
{
NO_CONNECTION_ERROR = 0,
FAILED_TO_CONNECT = 1
};

using ConnectCallback = std::function<void(ConnectionError)>;
class CastingPlayer;
using ConnectCallback = std::function<void(CHIP_ERROR, CastingPlayer *)>;

const int kPortMaxLength = 5; // port is uint16_t
// +1 for the : between the hostname and the port.
Expand Down Expand Up @@ -69,11 +65,18 @@ class CastingPlayer : public std::enable_shared_from_this<CastingPlayer>
// std::vector<memory::Strong<Endpoint>> endpoints;
bool mConnected = false;
CastingPlayerAttributes mAttributes;
static CastingPlayer * mTargetCastingPlayer;
ConnectCallback mOnCompleted;

chip::Inet::IPAddress * GetIpAddressForUDCRequest();

friend class support::ChipDeviceEventHandler;

public:
CastingPlayer(CastingPlayerAttributes playerAttributes) { mAttributes = playerAttributes; }

static CastingPlayer * GetTargetCastingPlayer() { return mTargetCastingPlayer; }

const char * GetId() const { return mAttributes.id; }

const char * GetDeviceName() const { return mAttributes.deviceName; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,138 @@

#include "ChipDeviceEventHandler.h"

#include "core/CastingPlayer.h"
#include "core/Types.h"

#include "app/clusters/bindings/BindingManager.h"

namespace matter {
namespace casting {
namespace support {

using namespace matter::casting::core;

bool ChipDeviceEventHandler::sUdcInProgress = false;

void ChipDeviceEventHandler::Handle(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg)
{
ChipLogProgress(AppServer, "ChipDeviceEventHandler::Handle called");

bool runPostCommissioning = false;
chip::NodeId targetPeerNodeId = 0;
chip::FabricIndex targetFabricIndex = 0;
if (event->Type == chip::DeviceLayer::DeviceEventType::kBindingsChangedViaCluster &&
CastingPlayer::GetTargetCastingPlayer() != nullptr)
{
HandleBindingsChangedViaCluster(event, arg, runPostCommissioning, targetPeerNodeId, targetFabricIndex);
}
else if (event->Type == chip::DeviceLayer::DeviceEventType::kCommissioningComplete)
{
HandleCommissioningComplete(event, arg, runPostCommissioning, targetPeerNodeId, targetFabricIndex);
}

if (runPostCommissioning)
{
ChipLogProgress(AppServer, "ChipDeviceEventHandler::Handle will connect with nodeId=0x" ChipLogFormatX64 " fabricIndex=%d",
ChipLogValueX64(targetPeerNodeId), targetFabricIndex);
// TODO
/*CHIP_ERROR err = CastingServer::GetInstance()->GetActiveTargetVideoPlayer()->Initialize(
targetPeerNodeId, targetFabricIndex, CastingServer::GetInstance()->mOnConnectionSuccessClientCallback,
CastingServer::GetInstance()->mOnConnectionFailureClientCallback,
CastingServer::GetInstance()->mTargetVideoPlayerVendorId, CastingServer::GetInstance()->mTargetVideoPlayerProductId,
CastingServer::GetInstance()->mTargetVideoPlayerDeviceType, CastingServer::GetInstance()->mTargetVideoPlayerDeviceName,
CastingServer::GetInstance()->mTargetVideoPlayerHostName, CastingServer::GetInstance()->mTargetVideoPlayerNumIPs,
CastingServer::GetInstance()->mTargetVideoPlayerIpAddress);
if (err != CHIP_NO_ERROR)
{
ChipLogError(AppServer, "Failed to initialize target video player");
}
err = CastingServer::GetInstance()->mPersistenceManager.AddVideoPlayer(
&CastingServer::GetInstance()->mActiveTargetVideoPlayerInfo);
if (err != CHIP_NO_ERROR)
{
ChipLogError(AppServer, "AddVideoPlayer(ToCache) error: %" CHIP_ERROR_FORMAT, err.Format());
}
if (CastingServer::GetInstance()->mCommissioningCallbacks.commissioningComplete)
{
CastingServer::GetInstance()->mCommissioningCallbacks.commissioningComplete(err);
}*/
}
}

void ChipDeviceEventHandler::HandleBindingsChangedViaCluster(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg,
bool & runPostCommissioning, chip::NodeId & targetPeerNodeId,
chip::FabricIndex & targetFabricIndex)
{
ChipLogProgress(AppServer, "ChipDeviceEventHandler::HandleBindingsChangedViaCluster called");

if (CastingPlayer::GetTargetCastingPlayer()->IsConnected())
{
ChipLogProgress(
AppServer,
"ChipDeviceEventHandler::HandleBindingsChangedViaCluster already connected to video player, reading server clusters");
/* TODO
CastingServer::GetInstance()->ReadServerClustersForNode(
CastingServer::GetInstance()->GetActiveTargetVideoPlayer()->GetNodeId());*/
}
else if (sUdcInProgress)
{
ChipLogProgress(AppServer,
"ChipDeviceEventHandler::HandleBindingsChangedViaCluster UDC is in progress while handling "
"kBindingsChangedViaCluster with "
"fabricIndex: %d",
event->BindingsChanged.fabricIndex);
sUdcInProgress = false;

// find targetPeerNodeId from binding table by matching the binding's fabricIndex with the accessing fabricIndex
// received in BindingsChanged event
for (const auto & binding : chip::BindingTable::GetInstance())
{
ChipLogProgress(AppServer,
"ChipDeviceEventHandler::HandleBindingsChangedViaCluster Read cached binding type=%d fabrixIndex=%d "
"nodeId=0x" ChipLogFormatX64
" groupId=%d local endpoint=%d remote endpoint=%d cluster=" ChipLogFormatMEI,
binding.type, binding.fabricIndex, ChipLogValueX64(binding.nodeId), binding.groupId, binding.local,
binding.remote, ChipLogValueMEI(binding.clusterId.ValueOr(0)));
if (binding.type == EMBER_UNICAST_BINDING && event->BindingsChanged.fabricIndex == binding.fabricIndex)
{
ChipLogProgress(AppServer,
"ChipDeviceEventHandler::HandleBindingsChangedViaCluster Matched accessingFabricIndex with "
"nodeId=0x" ChipLogFormatX64,
ChipLogValueX64(binding.nodeId));
targetPeerNodeId = binding.nodeId;
targetFabricIndex = binding.fabricIndex;
runPostCommissioning = true;
break;
}
}

if (targetPeerNodeId == 0 && runPostCommissioning == false)
{
ChipLogError(AppServer,
"ChipDeviceEventHandler::HandleBindingsChangedViaCluster accessingFabricIndex: %d did not match bindings",
event->BindingsChanged.fabricIndex);
CastingPlayer::GetTargetCastingPlayer()->mOnCompleted(CHIP_ERROR_INCORRECT_STATE,
CastingPlayer::GetTargetCastingPlayer());
return;
}
}
}

void ChipDeviceEventHandler::HandleCommissioningComplete(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg,
bool & runPostCommissioning, chip::NodeId & targetPeerNodeId,
chip::FabricIndex & targetFabricIndex)
{
ChipLogProgress(AppServer, "ChipDeviceEventHandler::HandleCommissioningComplete called");
sUdcInProgress = false;
targetPeerNodeId = event->CommissioningComplete.nodeId;
targetFabricIndex = event->CommissioningComplete.fabricIndex;
runPostCommissioning = true;
}

}; // namespace support
}; // namespace casting
}; // namespace matter
}; // namespace matter
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,27 @@ class ChipDeviceEventHandler
{
public:
static void Handle(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg);
static CHIP_ERROR SetUdcStatus(bool udcInProgress)
{
if (sUdcInProgress == udcInProgress)
{
ChipLogError(AppServer, "UDC in progress state is already %d", sUdcInProgress);
return CHIP_ERROR_INCORRECT_STATE;
}
sUdcInProgress = udcInProgress;
return CHIP_NO_ERROR;
}

private:
static void HandleBindingsChangedViaCluster(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg,
bool & runPostCommissioning, chip::NodeId & targetPeerNodeId,
chip::FabricIndex & targetFabricIndex);

static void HandleCommissioningComplete(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg,
bool & runPostCommissioning, chip::NodeId & targetPeerNodeId,
chip::FabricIndex & targetFabricIndex);

static bool sUdcInProgress;
};

}; // namespace support
Expand Down

0 comments on commit 13712ef

Please sign in to comment.