Skip to content

Commit

Permalink
Link attribute reads to Content Apps, refactor content app platform t…
Browse files Browse the repository at this point in the history
…o leverage latest cluster lib conventions (project-chip#12969)

* link attribute reads to Content Apps

* address some comments

* route attribute reads and writes to per-cluster handlers

* refactor content app

* straggler

* use MEI log format commands

* fix tv-app test suites

* fix tv-app test suites

* fix CI test suites

* fix CI test suites

* add bindings call

* migrate content app to use new account login delegate

* refactor ContentApp to use delegates

* fix CI errors

* fix CI errors

* fix CI errors

* more ContentApp to app-platform directory

* address issue 13144

* address issue 12543

* address comments

* address comments

* fix handling of application launcher, application basic status and content platform

* fix build

* fix android build

* fix android build

* add Commissioner Discovery Controller to manage UDC workflow

* address comments

* address comments

* fix builds

* fix builds

* fix builds

* address comments

* fix potential leak on commands

* fix potential leak on attribute reads

* fix potential leak on attribute reads

* argument and naming consistency

* fix android build

* fix android build

* fix android build

* fix android build

* address easy comments

* fix android build

* fix android

* fix merge issue

* optimize image size

* fix build

* attempt to fix CI build

* attempt to fix CI build

* attempt to fix CI build

* reduce dependencies

Co-authored-by: Christopher DeCenzo <decenzo@amazon.com>
  • Loading branch information
2 people authored and selissia committed Jan 28, 2022
1 parent 2f28cae commit c16b50c
Show file tree
Hide file tree
Showing 80 changed files with 2,602 additions and 1,780 deletions.
141 changes: 100 additions & 41 deletions examples/platform/linux/AppMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ static constexpr uint8_t kWiFiStartCheckAttempts = 5;
#endif

namespace {
void EventHandler(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg)
void EventHandler(const DeviceLayer::ChipDeviceEvent * event, intptr_t arg)
{
(void) arg;
if (event->Type == chip::DeviceLayer::DeviceEventType::kCHIPoBLEConnectionEstablished)
if (event->Type == DeviceLayer::DeviceEventType::kCHIPoBLEConnectionEstablished)
{
ChipLogProgress(DeviceLayer, "Receive kCHIPoBLEConnectionEstablished");
}
Expand All @@ -120,35 +120,35 @@ static bool EnsureWiFiIsStarted()
{
for (int cnt = 0; cnt < kWiFiStartCheckAttempts; cnt++)
{
if (chip::DeviceLayer::ConnectivityMgrImpl().IsWiFiManagementStarted())
if (DeviceLayer::ConnectivityMgrImpl().IsWiFiManagementStarted())
{
return true;
}

usleep(kWiFiStartCheckTimeUsec);
}

return chip::DeviceLayer::ConnectivityMgrImpl().IsWiFiManagementStarted();
return DeviceLayer::ConnectivityMgrImpl().IsWiFiManagementStarted();
}
#endif

int ChipLinuxAppInit(int argc, char ** argv)
{
CHIP_ERROR err = CHIP_NO_ERROR;
#if CONFIG_NETWORK_LAYER_BLE
chip::RendezvousInformationFlags rendezvousFlags = chip::RendezvousInformationFlag::kBLE;
RendezvousInformationFlags rendezvousFlags = RendezvousInformationFlag::kBLE;
#else // CONFIG_NETWORK_LAYER_BLE
chip::RendezvousInformationFlag rendezvousFlags = RendezvousInformationFlag::kOnNetwork;
RendezvousInformationFlag rendezvousFlags = RendezvousInformationFlag::kOnNetwork;
#endif // CONFIG_NETWORK_LAYER_BLE

#ifdef CONFIG_RENDEZVOUS_MODE
rendezvousFlags = static_cast<chip::RendezvousInformationFlags>(CONFIG_RENDEZVOUS_MODE);
rendezvousFlags = static_cast<RendezvousInformationFlags>(CONFIG_RENDEZVOUS_MODE);
#endif

err = chip::Platform::MemoryInit();
err = Platform::MemoryInit();
SuccessOrExit(err);

err = chip::DeviceLayer::PlatformMgr().InitChipStack();
err = DeviceLayer::PlatformMgr().InitChipStack();
SuccessOrExit(err);

err = GetSetupPayload(LinuxDeviceOptions::GetInstance().payload, rendezvousFlags);
Expand All @@ -164,22 +164,22 @@ int ChipLinuxAppInit(int argc, char ** argv)
Access::Examples::SetAccessControlDelegateStorage(&gAclStorageDelegate);

#if defined(PW_RPC_ENABLED)
chip::rpc::Init();
rpc::Init();
ChipLogProgress(NotSpecified, "PW_RPC initialized.");
#endif // defined(PW_RPC_ENABLED)

chip::DeviceLayer::PlatformMgrImpl().AddEventHandler(EventHandler, 0);
DeviceLayer::PlatformMgrImpl().AddEventHandler(EventHandler, 0);

#if CONFIG_NETWORK_LAYER_BLE
chip::DeviceLayer::ConnectivityMgr().SetBLEDeviceName(nullptr); // Use default device name (CHIP-XXXX)
chip::DeviceLayer::Internal::BLEMgrImpl().ConfigureBle(LinuxDeviceOptions::GetInstance().mBleDevice, false);
chip::DeviceLayer::ConnectivityMgr().SetBLEAdvertisingEnabled(true);
DeviceLayer::ConnectivityMgr().SetBLEDeviceName(nullptr); // Use default device name (CHIP-XXXX)
DeviceLayer::Internal::BLEMgrImpl().ConfigureBle(LinuxDeviceOptions::GetInstance().mBleDevice, false);
DeviceLayer::ConnectivityMgr().SetBLEAdvertisingEnabled(true);
#endif

#if CHIP_DEVICE_CONFIG_ENABLE_WPA
if (LinuxDeviceOptions::GetInstance().mWiFi)
{
chip::DeviceLayer::ConnectivityMgrImpl().StartWiFiManagement();
DeviceLayer::ConnectivityMgrImpl().StartWiFiManagement();
if (!EnsureWiFiIsStarted())
{
ChipLogError(NotSpecified, "Wi-Fi Management taking too long to start - device configuration will be reset.");
Expand All @@ -190,7 +190,7 @@ int ChipLinuxAppInit(int argc, char ** argv)
#if CHIP_ENABLE_OPENTHREAD
if (LinuxDeviceOptions::GetInstance().mThread)
{
SuccessOrExit(err = chip::DeviceLayer::ThreadStackMgrImpl().InitThreadStack());
SuccessOrExit(err = DeviceLayer::ThreadStackMgrImpl().InitThreadStack());
ChipLogProgress(NotSpecified, "Thread initialized.");
}
#endif // CHIP_ENABLE_OPENTHREAD
Expand Down Expand Up @@ -236,17 +236,26 @@ class MyServerStorageDelegate : public PersistentStorageDelegate
}
};

class MyCommissionerCallback : public CommissionerCallback
{
void ReadyForCommissioning(uint32_t pincode, uint16_t longDiscriminator, PeerAddress peerAddress) override
{
CommissionerPairOnNetwork(pincode, longDiscriminator, peerAddress);
}
};

DeviceCommissioner gCommissioner;
CommissionerDiscoveryController gCommissionerDiscoveryController;
MyCommissionerCallback gCommissionerCallback;
MyServerStorageDelegate gServerStorage;
chip::SimpleFabricStorage gFabricStorage;
SimpleFabricStorage gFabricStorage;
ExampleOperationalCredentialsIssuer gOpCredsIssuer;
NodeId gLocalId = kPlaceholderNodeId;

CHIP_ERROR InitCommissioner()
{
NodeId localId = chip::kPlaceholderNodeId;

chip::Controller::FactoryInitParams factoryParams;
chip::Controller::SetupParams params;
Controller::FactoryInitParams factoryParams;
Controller::SetupParams params;

ReturnErrorOnFailure(gFabricStorage.Initialize(&gServerStorage));

Expand All @@ -264,49 +273,60 @@ CHIP_ERROR InitCommissioner()

// Initialize device attestation verifier
// TODO: Replace testingRootStore with a AttestationTrustStore that has the necessary official PAA roots available
const chip::Credentials::AttestationTrustStore * testingRootStore = chip::Credentials::GetTestAttestationTrustStore();
const Credentials::AttestationTrustStore * testingRootStore = Credentials::GetTestAttestationTrustStore();
SetDeviceAttestationVerifier(GetDefaultDACVerifier(testingRootStore));

chip::Platform::ScopedMemoryBuffer<uint8_t> noc;
VerifyOrReturnError(noc.Alloc(chip::Controller::kMaxCHIPDERCertLength), CHIP_ERROR_NO_MEMORY);
chip::MutableByteSpan nocSpan(noc.Get(), chip::Controller::kMaxCHIPDERCertLength);
Platform::ScopedMemoryBuffer<uint8_t> noc;
VerifyOrReturnError(noc.Alloc(Controller::kMaxCHIPDERCertLength), CHIP_ERROR_NO_MEMORY);
MutableByteSpan nocSpan(noc.Get(), Controller::kMaxCHIPDERCertLength);

chip::Platform::ScopedMemoryBuffer<uint8_t> icac;
VerifyOrReturnError(icac.Alloc(chip::Controller::kMaxCHIPDERCertLength), CHIP_ERROR_NO_MEMORY);
chip::MutableByteSpan icacSpan(icac.Get(), chip::Controller::kMaxCHIPDERCertLength);
Platform::ScopedMemoryBuffer<uint8_t> icac;
VerifyOrReturnError(icac.Alloc(Controller::kMaxCHIPDERCertLength), CHIP_ERROR_NO_MEMORY);
MutableByteSpan icacSpan(icac.Get(), Controller::kMaxCHIPDERCertLength);

chip::Platform::ScopedMemoryBuffer<uint8_t> rcac;
VerifyOrReturnError(rcac.Alloc(chip::Controller::kMaxCHIPDERCertLength), CHIP_ERROR_NO_MEMORY);
chip::MutableByteSpan rcacSpan(rcac.Get(), chip::Controller::kMaxCHIPDERCertLength);
Platform::ScopedMemoryBuffer<uint8_t> rcac;
VerifyOrReturnError(rcac.Alloc(Controller::kMaxCHIPDERCertLength), CHIP_ERROR_NO_MEMORY);
MutableByteSpan rcacSpan(rcac.Get(), Controller::kMaxCHIPDERCertLength);

chip::Crypto::P256Keypair ephemeralKey;
Crypto::P256Keypair ephemeralKey;
ReturnErrorOnFailure(ephemeralKey.Initialize());

ReturnErrorOnFailure(
gOpCredsIssuer.GenerateNOCChainAfterValidation(localId, 0, ephemeralKey.Pubkey(), rcacSpan, icacSpan, nocSpan));
gOpCredsIssuer.GenerateNOCChainAfterValidation(gLocalId, 0, ephemeralKey.Pubkey(), rcacSpan, icacSpan, nocSpan));

params.operationalKeypair = &ephemeralKey;
params.controllerRCAC = rcacSpan;
params.controllerICAC = icacSpan;
params.controllerNOC = nocSpan;

auto & factory = chip::Controller::DeviceControllerFactory::GetInstance();
auto & factory = Controller::DeviceControllerFactory::GetInstance();
ReturnErrorOnFailure(factory.Init(factoryParams));
ReturnErrorOnFailure(factory.SetupCommissioner(params, gCommissioner));
gCommissionerDiscoveryController.SetUserDirectedCommissioningServer(gCommissioner.GetUserDirectedCommissioningServer());
gCommissionerDiscoveryController.SetCommissionerCallback(&gCommissionerCallback);

return CHIP_NO_ERROR;
}

CHIP_ERROR ShutdownCommissioner()
{
UserDirectedCommissioningServer * udcServer = gCommissioner.GetUserDirectedCommissioningServer();
if (udcServer != nullptr)
{
udcServer->SetUserConfirmationProvider(nullptr);
}

gCommissioner.Shutdown();
return CHIP_NO_ERROR;
}

class PairingCommand : public chip::Controller::DevicePairingDelegate, public chip::Controller::DeviceAddressUpdateDelegate
class PairingCommand : public Controller::DevicePairingDelegate, public Controller::DeviceAddressUpdateDelegate
{
public:
PairingCommand() : mSuccessCallback(OnSuccessResponse, this), mFailureCallback(OnFailureResponse, this){};

/////////// DevicePairingDelegate Interface /////////
void OnStatusUpdate(chip::Controller::DevicePairingDelegate::Status status) override;
void OnStatusUpdate(Controller::DevicePairingDelegate::Status status) override;
void OnPairingComplete(CHIP_ERROR error) override;
void OnPairingDeleted(CHIP_ERROR error) override;
void OnCommissioningComplete(NodeId deviceId, CHIP_ERROR error) override;
Expand All @@ -315,6 +335,14 @@ class PairingCommand : public chip::Controller::DevicePairingDelegate, public ch
void OnAddressUpdateComplete(NodeId nodeId, CHIP_ERROR error) override;

CHIP_ERROR UpdateNetworkAddress();

/* Callback when command results in success */
static void OnSuccessResponse(void * context);
/* Callback when command results in failure */
static void OnFailureResponse(void * context, uint8_t status);

Callback::Callback<DefaultSuccessCallback> mSuccessCallback;
Callback::Callback<DefaultFailureCallback> mFailureCallback;
};

PairingCommand gPairingCommand;
Expand Down Expand Up @@ -373,19 +401,45 @@ void PairingCommand::OnPairingDeleted(CHIP_ERROR err)
}
}

void PairingCommand::OnSuccessResponse(void * context)
{
ChipLogProgress(Controller, "OnSuccessResponse");
}

void PairingCommand::OnFailureResponse(void * context, uint8_t status)
{
ChipLogProgress(Controller, "OnFailureResponse");
}

void PairingCommand::OnCommissioningComplete(NodeId nodeId, CHIP_ERROR err)
{
if (err == CHIP_NO_ERROR)
{
ChipLogProgress(AppServer, "Device commissioning completed with success");

// TODO:
// - this code needs to be conditional based upon Content App Platform enablement
// - the endpointId chosen should come from the App Platform (determined based upon vid/pid of node)
// - the cluster(s) chosen should come from the App Platform
constexpr EndpointId kBindingClusterEndpoint = 0;

Callback::Cancelable * successCallback = mSuccessCallback.Cancel();
Callback::Cancelable * failureCallback = mFailureCallback.Cancel();

GroupId groupId = kUndefinedGroupId;
EndpointId endpointId = 1;
ClusterId clusterId = kInvalidClusterId;

gCommissioner.CreateBindingWithCallback(nodeId, kBindingClusterEndpoint, gLocalId, groupId, endpointId, clusterId,
successCallback, failureCallback);
}
else
{
ChipLogProgress(AppServer, "Device commissioning Failure: %s", ErrorStr(err));
}
}

CHIP_ERROR CommissionerPairOnNetwork(uint32_t pincode, uint16_t disc, chip::Transport::PeerAddress address)
CHIP_ERROR CommissionerPairOnNetwork(uint32_t pincode, uint16_t disc, Transport::PeerAddress address)
{
RendezvousParameters params = RendezvousParameters().SetSetupPINCode(pincode).SetDiscriminator(disc).SetPeerAddress(address);

Expand Down Expand Up @@ -419,14 +473,19 @@ DeviceCommissioner * GetDeviceCommissioner()
return &gCommissioner;
}

CommissionerDiscoveryController * GetCommissionerDiscoveryController()
{
return &gCommissionerDiscoveryController;
}

#endif // CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE

void ChipLinuxAppMainLoop()
{
#if defined(ENABLE_CHIP_SHELL)
Engine::Root().Init();
std::thread shellThread([]() { Engine::Root().RunMainLoop(); });
chip::Shell::RegisterCommissioneeCommands();
Shell::RegisterCommissioneeCommands();
#endif
uint16_t securePort = CHIP_PORT;
uint16_t unsecurePort = CHIP_UDC_PORT;
Expand All @@ -438,7 +497,7 @@ void ChipLinuxAppMainLoop()
#endif // CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE

// Init ZCL Data Model and CHIP App Server
chip::Server::GetInstance().Init(nullptr, securePort, unsecurePort);
Server::GetInstance().Init(nullptr, securePort, unsecurePort);

// Now that the server has started and we are done with our startup logging,
// log our discovery/onboarding information again so it's not lost in the
Expand All @@ -453,13 +512,13 @@ void ChipLinuxAppMainLoop()
#if CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
InitCommissioner();
#if defined(ENABLE_CHIP_SHELL)
chip::Shell::RegisterControllerCommands();
Shell::RegisterControllerCommands();
#endif // defined(ENABLE_CHIP_SHELL)
#endif // CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE

ApplicationInit();

chip::DeviceLayer::PlatformMgr().RunEventLoop();
DeviceLayer::PlatformMgr().RunEventLoop();

#if CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
ShutdownCommissioner();
Expand Down
8 changes: 5 additions & 3 deletions examples/platform/linux/AppMain.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <thread>

#include <controller/CHIPDeviceController.h>
#include <controller/CommissionerDiscoveryController.h>
#include <lib/core/CHIPError.h>
#include <platform/CHIPDeviceLayer.h>
#include <platform/PlatformManager.h>
Expand All @@ -32,13 +33,14 @@ void ChipLinuxAppMainLoop();

#if CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE

using namespace chip::Transport;
using namespace ::chip::Controller;
using chip::Controller::DeviceCommissioner;
using chip::Transport::PeerAddress;

CHIP_ERROR CommissionerPairOnNetwork(uint32_t pincode, uint16_t disc, chip::Transport::PeerAddress address);
CHIP_ERROR CommissionerPairOnNetwork(uint32_t pincode, uint16_t disc, PeerAddress address);
CHIP_ERROR CommissionerPairUDC(uint32_t pincode, size_t index);

DeviceCommissioner * GetDeviceCommissioner();
CommissionerDiscoveryController * GetCommissionerDiscoveryController();

#endif // CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE

Expand Down
Loading

0 comments on commit c16b50c

Please sign in to comment.