Skip to content

Commit

Permalink
Remove synchronous calls that can block from TV code (#32010)
Browse files Browse the repository at this point in the history
* Remove synchronous calls

* move blocking calls to use a (future) separate thread pool

* cleanup and fix ci

* Restyled by clang-format (#32011)

Co-authored-by: Restyled.io <[email protected]>

* address comments

* address comments

* Address comments

* Fix build

* Address comments

* cleanup

* cleanup

* Fix rotating id

* address comments

* fix CI

* Address comments

---------

Co-authored-by: restyled-io[bot] <32688539+restyled-io[bot]@users.noreply.github.com>
Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
3 people authored and pull[bot] committed May 7, 2024
1 parent 0983ede commit 2716225
Show file tree
Hide file tree
Showing 11 changed files with 440 additions and 106 deletions.
8 changes: 2 additions & 6 deletions examples/tv-app/android/java/MyUserPrompterResolver-JNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ using namespace chip;

JNI_METHOD(void, OnPinCodeEntered)(JNIEnv *, jobject, jint jPinCode)
{
#if CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
chip::DeviceLayer::StackLock lock;
uint32_t pinCode = (uint32_t) jPinCode;
uint32_t pinCode = static_cast<uint32_t>(jPinCode);
ChipLogProgress(Zcl, "OnPinCodeEntered %d", pinCode);
#if CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
GetCommissionerDiscoveryController()->CommissionWithPasscode(pinCode);
#endif
}
Expand Down Expand Up @@ -67,11 +67,7 @@ JNI_METHOD(void, OnPromptDeclined)(JNIEnv *, jobject)

JNI_METHOD(void, OnCommissionerPasscodeOK)(JNIEnv *, jobject)
{
#if CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
chip::DeviceLayer::StackLock lock;
ChipLogProgress(Zcl, "OnCommissionerPasscodeOK");
// GetCommissionerDiscoveryController()->Ok();
#endif
}

JNI_METHOD(void, OnCommissionerPasscodeCancel)(JNIEnv *, jobject)
Expand Down
33 changes: 28 additions & 5 deletions examples/tv-app/android/java/TVApp-JNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,28 @@ JNI_METHOD(void, setChipDeviceEventProvider)(JNIEnv *, jobject, jobject provider
#if CHIP_DEVICE_CONFIG_APP_PLATFORM_ENABLED
class MyPincodeService : public PasscodeService
{
bool HasTargetContentApp(uint16_t vendorId, uint16_t productId, chip::CharSpan rotatingId,
chip::Protocols::UserDirectedCommissioning::TargetAppInfo & info, uint32_t & passcode) override
void LookupTargetContentApp(uint16_t vendorId, uint16_t productId, chip::CharSpan rotatingId,
chip::Protocols::UserDirectedCommissioning::TargetAppInfo & info) override
{
return ContentAppPlatform::GetInstance().HasTargetContentApp(vendorId, productId, rotatingId, info, passcode);
uint32_t passcode;
bool foundApp = ContentAppPlatform::GetInstance().HasTargetContentApp(vendorId, productId, rotatingId, info, passcode);
if (!foundApp)
{
info.checkState = chip::Controller::TargetAppCheckState::kAppNotFound;
}
else if (passcode != 0)
{
info.checkState = chip::Controller::TargetAppCheckState::kAppFoundPasscodeReturned;
}
else
{
info.checkState = chip::Controller::TargetAppCheckState::kAppFoundNoPasscode;
}
CommissionerDiscoveryController * cdc = GetCommissionerDiscoveryController();
if (cdc != nullptr)
{
cdc->HandleTargetContentAppCheck(info, passcode);
}
}

uint32_t GetCommissionerPasscode(uint16_t vendorId, uint16_t productId, chip::CharSpan rotatingId) override
Expand All @@ -209,9 +227,14 @@ class MyPincodeService : public PasscodeService
return 12345678;
}

uint32_t FetchCommissionPasscodeFromContentApp(uint16_t vendorId, uint16_t productId, CharSpan rotatingId) override
void FetchCommissionPasscodeFromContentApp(uint16_t vendorId, uint16_t productId, CharSpan rotatingId) override
{
return ContentAppPlatform::GetInstance().GetPasscodeFromContentApp(vendorId, productId, rotatingId);
uint32_t passcode = ContentAppPlatform::GetInstance().GetPasscodeFromContentApp(vendorId, productId, rotatingId);
CommissionerDiscoveryController * cdc = GetCommissionerDiscoveryController();
if (cdc != nullptr)
{
cdc->HandleContentAppPasscodeResponse(passcode);
}
}
};
MyPincodeService gMyPincodeService;
Expand Down
3 changes: 3 additions & 0 deletions examples/tv-app/tv-common/include/CHIPProjectAppConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
// TVs need to be commissioners and likely want to be discoverable
#define CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY 1

// TVs can handle the memory impact of supporting a larger list
#define CHIP_DEVICE_CONFIG_UDC_MAX_TARGET_APPS 10

// TVs will often enable this feature
#define CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_PASSCODE 1

Expand Down
33 changes: 28 additions & 5 deletions examples/tv-app/tv-common/src/AppTv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,28 @@ MyUserPrompter gMyUserPrompter;

class MyPasscodeService : public PasscodeService
{
bool HasTargetContentApp(uint16_t vendorId, uint16_t productId, chip::CharSpan rotatingId,
chip::Protocols::UserDirectedCommissioning::TargetAppInfo & info, uint32_t & passcode) override
void LookupTargetContentApp(uint16_t vendorId, uint16_t productId, chip::CharSpan rotatingId,
chip::Protocols::UserDirectedCommissioning::TargetAppInfo & info) override
{
return ContentAppPlatform::GetInstance().HasTargetContentApp(vendorId, productId, rotatingId, info, passcode);
uint32_t passcode = 0;
bool foundApp = ContentAppPlatform::GetInstance().HasTargetContentApp(vendorId, productId, rotatingId, info, passcode);
if (!foundApp)
{
info.checkState = TargetAppCheckState::kAppNotFound;
}
else if (passcode != 0)
{
info.checkState = TargetAppCheckState::kAppFoundPasscodeReturned;
}
else
{
info.checkState = TargetAppCheckState::kAppFoundNoPasscode;
}
CommissionerDiscoveryController * cdc = GetCommissionerDiscoveryController();
if (cdc != nullptr)
{
cdc->HandleTargetContentAppCheck(info, passcode);
}
}

uint32_t GetCommissionerPasscode(uint16_t vendorId, uint16_t productId, chip::CharSpan rotatingId) override
Expand All @@ -114,9 +132,14 @@ class MyPasscodeService : public PasscodeService
return 12345678;
}

uint32_t FetchCommissionPasscodeFromContentApp(uint16_t vendorId, uint16_t productId, CharSpan rotatingId) override
void FetchCommissionPasscodeFromContentApp(uint16_t vendorId, uint16_t productId, CharSpan rotatingId) override
{
return ContentAppPlatform::GetInstance().GetPasscodeFromContentApp(vendorId, productId, rotatingId);
uint32_t passcode = ContentAppPlatform::GetInstance().GetPasscodeFromContentApp(vendorId, productId, rotatingId);
CommissionerDiscoveryController * cdc = GetCommissionerDiscoveryController();
if (cdc != nullptr)
{
cdc->HandleContentAppPasscodeResponse(passcode);
}
}
};
MyPasscodeService gMyPasscodeService;
Expand Down
19 changes: 14 additions & 5 deletions src/app/server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -645,13 +645,22 @@ CHIP_ERROR Server::SendUserDirectedCommissioningRequest(chip::Transport::PeerAdd
#if CHIP_ENABLE_ROTATING_DEVICE_ID && defined(CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID)
if (id.GetRotatingIdLength() == 0)
{
char rotatingDeviceIdHexBuffer[RotatingDeviceId::kHexMaxLength];
AdditionalDataPayloadGeneratorParams additionalDataPayloadParams;
uint8_t rotatingDeviceIdUniqueId[chip::DeviceLayer::ConfigurationManager::kRotatingDeviceIDUniqueIDLength];
MutableByteSpan rotatingDeviceIdUniqueIdSpan(rotatingDeviceIdUniqueId);

ReturnErrorOnFailure(
chip::DeviceLayer::GetDeviceInstanceInfoProvider()->GetRotatingDeviceIdUniqueId(rotatingDeviceIdUniqueIdSpan));
ReturnErrorOnFailure(
app::DnssdServer::Instance().GenerateRotatingDeviceId(rotatingDeviceIdHexBuffer, ArraySize(rotatingDeviceIdHexBuffer)));
chip::DeviceLayer::ConfigurationMgr().GetLifetimeCounter(additionalDataPayloadParams.rotatingDeviceIdLifetimeCounter));
additionalDataPayloadParams.rotatingDeviceIdUniqueId = rotatingDeviceIdUniqueIdSpan;

uint8_t rotatingDeviceIdInternalBuffer[RotatingDeviceId::kMaxLength];
MutableByteSpan rotatingDeviceIdBufferTemp(rotatingDeviceIdInternalBuffer);
ReturnErrorOnFailure(AdditionalDataPayloadGenerator().generateRotatingDeviceIdAsBinary(additionalDataPayloadParams,
rotatingDeviceIdBufferTemp));

uint8_t * rotatingId = reinterpret_cast<uint8_t *>(rotatingDeviceIdHexBuffer);
size_t rotatingIdLen = strlen(rotatingDeviceIdHexBuffer);
id.SetRotatingId(rotatingId, rotatingIdLen);
id.SetRotatingId(rotatingDeviceIdInternalBuffer, RotatingDeviceId::kMaxLength);
}
#endif

Expand Down
Loading

0 comments on commit 2716225

Please sign in to comment.