Skip to content

Commit

Permalink
Merge branch 'master' into idm-4.2-troubleshoot
Browse files Browse the repository at this point in the history
  • Loading branch information
raul-marquez-csa authored May 23, 2024
2 parents e65e4cd + c3ef110 commit 5433f31
Show file tree
Hide file tree
Showing 26 changed files with 416 additions and 256 deletions.
84 changes: 35 additions & 49 deletions examples/fabric-bridge-app/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <app/ConcreteAttributePath.h>
#include <app/EventLogging.h>
#include <app/reporting/reporting.h>
#include <app/server/Server.h>
#include <app/util/af-types.h>
#include <app/util/attribute-storage.h>
#include <app/util/endpoint-config-api.h>
Expand All @@ -39,16 +40,14 @@
#include <setup_payload/QRCodeSetupPayloadGenerator.h>
#include <setup_payload/SetupPayload.h>

#include <pthread.h>
#include <sys/ioctl.h>

#include "CommissionableInit.h"
#include "Device.h"
#include <app/server/Server.h>

#include <cassert>
#include <iostream>
#include <string>
#include <sys/ioctl.h>
#include <thread>

using namespace chip;
using namespace chip::app;
Expand All @@ -58,12 +57,41 @@ using namespace chip::Transport;
using namespace chip::DeviceLayer;
using namespace chip::app::Clusters;

#define POLL_INTERVAL_MS (100)

namespace {

EndpointId gCurrentEndpointId;
EndpointId gFirstDynamicEndpointId;
Device * gDevices[CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT + 1];

bool KeyboardHit()
{
int bytesWaiting;
ioctl(0, FIONREAD, &bytesWaiting);
return bytesWaiting > 0;
}

void BridgePollingThread()
{
while (true)
{
if (KeyboardHit())
{
int ch = getchar();
if (ch == 'e')
{
ChipLogProgress(DeviceLayer, "Exiting.....");
exit(0);
}
continue;
}

// Sleep to avoid tight loop reading commands
usleep(POLL_INTERVAL_MS * 1000);
}
}

} // namespace

// REVISION DEFINITIONS:
Expand Down Expand Up @@ -245,42 +273,6 @@ Protocols::InteractionModel::Status emberAfExternalAttributeWriteCallback(Endpoi
return ret;
}

#define POLL_INTERVAL_MS (100)
uint8_t poll_prescale = 0;

bool kbhit()
{
int byteswaiting;
ioctl(0, FIONREAD, &byteswaiting);
return byteswaiting > 0;
}

const int16_t oneDegree = 100;

void * bridge_polling_thread(void * context)
{
while (true)
{
if (kbhit())
{
int ch = getchar();

// Commands used for the actions bridge test plan.
if (ch == 'e')
{
ChipLogProgress(DeviceLayer, "Exiting.....");
exit(0);
}
continue;
}

// Sleep to avoid tight loop reading commands
usleep(POLL_INTERVAL_MS * 1000);
}

return nullptr;
}

void ApplicationInit()
{
// Clear out the device database
Expand All @@ -292,15 +284,9 @@ void ApplicationInit()
static_cast<int>(emberAfEndpointFromIndex(static_cast<uint16_t>(emberAfFixedEndpointCount() - 1))) + 1);
gCurrentEndpointId = gFirstDynamicEndpointId;

{
pthread_t poll_thread;
int res = pthread_create(&poll_thread, nullptr, bridge_polling_thread, nullptr);
if (res)
{
printf("Error creating polling thread: %d\n", res);
exit(1);
}
}
// Start a thread for bridge polling
std::thread pollingThread(BridgePollingThread);
pollingThread.detach();
}

void ApplicationShutdown() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,8 @@ JNI_METHOD(jobject, verifyOrEstablishConnection)
matter::casting::core::ConnectionCallbacks connectionCallbacks;
connectionCallbacks.mOnConnectionComplete = connectCallback;

// TODO: Verify why commissioningWindowTimeoutSec is a "unsigned long long int" type. Seems too big.
castingPlayer->VerifyOrEstablishConnection(connectionCallbacks,
static_cast<unsigned long long int>(commissioningWindowTimeoutSec), idOptions);
castingPlayer->VerifyOrEstablishConnection(connectionCallbacks, static_cast<uint16_t>(commissioningWindowTimeoutSec),
idOptions);
return support::convertMatterErrorFromCppToJava(CHIP_NO_ERROR);
}

Expand Down
80 changes: 66 additions & 14 deletions examples/tv-casting-app/linux/simple-app-helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@

// VendorId of the Endpoint on the CastingPlayer that the CastingApp desires to interact with after connection
const uint16_t kDesiredEndpointVendorId = 65521;
// EndpointId of the Endpoint on the CastingPlayer that the CastingApp desires to interact with after connection using the
// Commissioner-Generated passcode commissioning flow
const uint8_t kDesiredEndpointId = 1;
// Indicates that the Commissioner-Generated passcode commissioning flow is in progress.
bool gCommissionerGeneratedPasscodeFlowRunning = false;

DiscoveryDelegateImpl * DiscoveryDelegateImpl::_discoveryDelegateImpl = nullptr;
bool gAwaitingCommissionerPasscodeInput = false;
Expand Down Expand Up @@ -244,6 +249,16 @@ CHIP_ERROR InitCommissionableDataProvider(LinuxCommissionableDataProvider & prov
options.payload.discriminator.GetLongValue());
}

void LogEndpointsDetails(const std::vector<matter::casting::memory::Strong<matter::casting::core::Endpoint>> & endpoints)
{
ChipLogProgress(AppServer, "simple-app-helper.cpp::LogEndpointsDetails() Number of Endpoints: %d",
static_cast<int>(endpoints.size()));
for (const auto & endpoint : endpoints)
{
endpoint->LogDetail();
}
}

void ConnectionHandler(CHIP_ERROR err, matter::casting::core::CastingPlayer * castingPlayer)
{
ChipLogProgress(AppServer, "simple-app-helper.cpp::ConnectionHandler()");
Expand All @@ -256,23 +271,51 @@ void ConnectionHandler(CHIP_ERROR err, matter::casting::core::CastingPlayer * ca
"simple-app-helper.cpp::ConnectionHandler(): Failed to connect to CastingPlayer (ID: %s) with err %" CHIP_ERROR_FORMAT,
targetCastingPlayer->GetId(), err.Format()));

ChipLogProgress(AppServer, "simple-app-helper.cpp::ConnectionHandler(): Successfully connected to CastingPlayer (ID: %s)",
castingPlayer->GetId());
ChipLogProgress(AppServer,
"simple-app-helper.cpp::ConnectionHandler(): Triggering demo interactions with CastingPlayer (ID: %s)",
castingPlayer->GetId());
if (gCommissionerGeneratedPasscodeFlowRunning)
{
ChipLogProgress(AppServer,
"simple-app-helper.cpp::ConnectionHandler(): Successfully connected to CastingPlayer (ID: %s) using "
"Commissioner-Generated passcode",
castingPlayer->GetId());
ChipLogProgress(AppServer, "simple-app-helper.cpp::ConnectionHandler(): Desired Endpoint ID for demo interactions: 1");
}
else
{
ChipLogProgress(AppServer, "simple-app-helper.cpp::ConnectionHandler(): Successfully connected to CastingPlayer (ID: %s)",
castingPlayer->GetId());
ChipLogProgress(AppServer,
"simple-app-helper.cpp::ConnectionHandler(): Desired Endpoint Vendor ID for demo interactions: %d",
kDesiredEndpointVendorId);
}

ChipLogProgress(AppServer, "simple-app-helper.cpp::ConnectionHandler(): Getting endpoints avaiable for demo interactions");
std::vector<matter::casting::memory::Strong<matter::casting::core::Endpoint>> endpoints = castingPlayer->GetEndpoints();
LogEndpointsDetails(endpoints);

// Find the desired Endpoint and auto-trigger some Matter Casting demo interactions
auto it = std::find_if(endpoints.begin(), endpoints.end(),
[](const matter::casting::memory::Strong<matter::casting::core::Endpoint> & endpoint) {
if (gCommissionerGeneratedPasscodeFlowRunning)
{
// For the example Commissioner-Generated passcode commissioning flow, run demo interactions with
// the Endpoint with ID 1. For this flow, we commissioned with the Target Content Application
// with Vendor ID 1111. Since this target content application does not report its Endpoint's
// Vendor IDs, we find the desired endpoint based on the Endpoint ID. See
// connectedhomeip/examples/tv-app/tv-common/include/AppTv.h.
return endpoint->GetId() == kDesiredEndpointId;
}
return endpoint->GetVendorId() == kDesiredEndpointVendorId;
});
if (it != endpoints.end())
{
// The desired endpoint is endpoints[index]
unsigned index = (unsigned int) std::distance(endpoints.begin(), it);

ChipLogProgress(
AppServer,
"simple-app-helper.cpp::ConnectionHandler(): Triggering demo interactions with CastingPlayer (ID: %s). Endpoint ID: %d",
castingPlayer->GetId(), endpoints[index]->GetId());

// demonstrate invoking a command
InvokeContentLauncherLaunchURL(endpoints[index]);

Expand All @@ -286,8 +329,8 @@ void ConnectionHandler(CHIP_ERROR err, matter::casting::core::CastingPlayer * ca
{
ChipLogError(
AppServer,
"simple-app-helper.cpp::ConnectionHandler():Desired Endpoint Vendor Id (%d) not found on the CastingPlayer (ID: %s)",
kDesiredEndpointVendorId, castingPlayer->GetId());
"simple-app-helper.cpp::ConnectionHandler():Desired Endpoint Vendor ID not found on the CastingPlayer (ID: %s)",
castingPlayer->GetId());
}
}

Expand Down Expand Up @@ -350,9 +393,14 @@ CHIP_ERROR CommandHandler(int argc, char ** argv)
ChipLogError(AppServer, "Invalid casting player index provided: %lu", index));
targetCastingPlayer = castingPlayers.at(index);

gCommissionerGeneratedPasscodeFlowRunning = false;
matter::casting::core::IdentificationDeclarationOptions idOptions;
chip::Protocols::UserDirectedCommissioning::TargetAppInfo targetAppInfo;
targetAppInfo.vendorId = kDesiredEndpointVendorId;

if (argc == 3)
{

if (strcmp(argv[2], "commissioner-generated-passcode") == 0)
{
// Attempt Commissioner-Generated Passcode (commissioner-generated-passcode) commissioning flow only if the
Expand All @@ -364,6 +412,14 @@ CHIP_ERROR CommandHandler(int argc, char ** argv)
"Commissioner-Generated Passcode commissioning flow",
index);
idOptions.mCommissionerPasscode = true;

// For the example Commissioner-Generated passcode commissioning flow, override the default Target Content
// Application Vendor ID, which is configured on the tv-app. This Target Content Application Vendor ID (1111),
// does not implement the AccountLogin cluster, which would otherwise auto commission using the
// Commissionee-Generated passcode upon recieving the IdentificationDeclaration Message. See
// connectedhomeip/examples/tv-app/tv-common/include/AppTv.h.
targetAppInfo.vendorId = 1111;
gCommissionerGeneratedPasscodeFlowRunning = true;
}
else
{
Expand All @@ -374,9 +430,8 @@ CHIP_ERROR CommandHandler(int argc, char ** argv)
}
}
}
chip::Protocols::UserDirectedCommissioning::TargetAppInfo targetAppInfo;
targetAppInfo.vendorId = kDesiredEndpointVendorId;
CHIP_ERROR result = idOptions.addTargetAppInfo(targetAppInfo);

CHIP_ERROR result = idOptions.addTargetAppInfo(targetAppInfo);
if (result != CHIP_NO_ERROR)
{
ChipLogError(AppServer, "CommandHandler() request, failed to add targetAppInfo: %" CHIP_ERROR_FORMAT, result.Format());
Expand Down Expand Up @@ -430,11 +485,8 @@ CHIP_ERROR CommandHandler(int argc, char ** argv)
err.Format());
}

matter::casting::core::ConnectionCallbacks connectionCallbacks;
connectionCallbacks.mOnConnectionComplete = ConnectionHandler;

// Continue Connecting to the target CastingPlayer with the user entered Commissioner-generated Passcode.
targetCastingPlayer->ContinueConnecting(connectionCallbacks, matter::casting::core::kCommissioningWindowTimeoutSec);
targetCastingPlayer->ContinueConnecting();
}
else
{
Expand Down
Loading

0 comments on commit 5433f31

Please sign in to comment.