Skip to content

Commit

Permalink
Allow endpoint ID to be specified when adding content app dynamically (
Browse files Browse the repository at this point in the history
…#22199)

* Allow endpoint ID sto be specified when adding content app dynamically

* Restyled by whitespace

* Restyled by clang-format

* Review comments...

* Restyled by whitespace

* Restyled by clang-format

* Update content apps array only when adding endpoint succeeds. updated documentation.

* Restyled by clang-format

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
2 people authored and pull[bot] committed Feb 13, 2024
1 parent 6bbb1ee commit 353e3e2
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 25 deletions.
2 changes: 2 additions & 0 deletions examples/tv-app/android/java/MyUserPrompterResolver-JNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ JNI_METHOD(void, OnPinCodeDeclined)(JNIEnv *, jobject)
JNI_METHOD(void, OnPromptAccepted)(JNIEnv *, jobject)
{
#if CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
chip::DeviceLayer::StackLock lock;
ChipLogProgress(Zcl, "OnPromptAccepted");
GetCommissionerDiscoveryController()->Ok();
#endif
Expand All @@ -56,6 +57,7 @@ JNI_METHOD(void, OnPromptAccepted)(JNIEnv *, jobject)
JNI_METHOD(void, OnPromptDeclined)(JNIEnv *, jobject)
{
#if CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
chip::DeviceLayer::StackLock lock;
ChipLogProgress(Zcl, "OnPromptDeclined");
GetCommissionerDiscoveryController()->Cancel();
#endif
Expand Down
1 change: 1 addition & 0 deletions examples/tv-app/android/java/TVApp-JNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ JNI_METHOD(void, setChipDeviceEventProvider)(JNIEnv *, jobject, jobject provider
JNI_METHOD(jint, addContentApp)
(JNIEnv *, jobject, jstring vendorName, jint vendorId, jstring appName, jint productId, jstring appVersion, jobject manager)
{
chip::DeviceLayer::StackLock lock;
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();

JniUtfString vName(env, vendorName);
Expand Down
112 changes: 89 additions & 23 deletions src/app/app-platform/ContentAppPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ EndpointId ContentAppPlatform::AddContentApp(ContentApp * app, EmberAfEndpointTy
if (mContentApps[index] == app)
{
ChipLogProgress(DeviceLayer, "Already added");
// already added, return endpointId of already added endpoint.
// desired endpointId does not have any impact
return app->GetEndpointId();
}
index++;
Expand All @@ -108,38 +110,102 @@ EndpointId ContentAppPlatform::AddContentApp(ContentApp * app, EmberAfEndpointTy
index = 0;
while (index < CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT)
{
if (nullptr == mContentApps[index])
if (mContentApps[index] != nullptr)
{
mContentApps[index] = app;
EmberAfStatus ret;
while (1)
index++;
continue;
}
EmberAfStatus ret;
EndpointId initEndpointId = mCurrentEndpointId;

do
{
ret = emberAfSetDynamicEndpoint(index, mCurrentEndpointId, ep, dataVersionStorage, deviceTypeList);
if (ret == EMBER_ZCL_STATUS_SUCCESS)
{
ret = emberAfSetDynamicEndpoint(index, mCurrentEndpointId, ep, dataVersionStorage, deviceTypeList);
if (ret == EMBER_ZCL_STATUS_SUCCESS)
{
ChipLogProgress(DeviceLayer, "Added ContentApp %s to dynamic endpoint %d (index=%d)", vendorApp.applicationId,
mCurrentEndpointId, index);
app->SetEndpointId(mCurrentEndpointId);
return app->GetEndpointId();
}
else if (ret != EMBER_ZCL_STATUS_DUPLICATE_EXISTS)
{
ChipLogError(DeviceLayer, "Adding ContentApp error=%d", ret);
return kNoCurrentEndpointId;
}
// Handle wrap condition
if (++mCurrentEndpointId < mFirstDynamicEndpointId)
{
mCurrentEndpointId = mFirstDynamicEndpointId;
}
ChipLogProgress(DeviceLayer, "Added ContentApp %s to dynamic endpoint %d (index=%d)", vendorApp.applicationId,
mCurrentEndpointId, index);
app->SetEndpointId(mCurrentEndpointId);
mContentApps[index] = app;
IncrementCurrentEndpointID();
return app->GetEndpointId();
}
else if (ret != EMBER_ZCL_STATUS_DUPLICATE_EXISTS)
{
ChipLogError(DeviceLayer, "Adding ContentApp error=%d", ret);
return kNoCurrentEndpointId;
}
IncrementCurrentEndpointID();
} while (initEndpointId != mCurrentEndpointId);
ChipLogError(DeviceLayer, "Failed to add dynamic endpoint: No endpoints available!");
return kNoCurrentEndpointId;
}
ChipLogError(DeviceLayer, "Failed to add dynamic endpoint: max endpoint count reached!");
return kNoCurrentEndpointId;
}

EndpointId ContentAppPlatform::AddContentApp(ContentApp * app, EmberAfEndpointType * ep,
const Span<DataVersion> & dataVersionStorage,
const Span<const EmberAfDeviceType> & deviceTypeList, EndpointId desiredEndpointId)
{
CatalogVendorApp vendorApp = app->GetApplicationBasicDelegate()->GetCatalogVendorApp();

ChipLogProgress(DeviceLayer, "Adding ContentApp with appid %s ", vendorApp.applicationId);
uint8_t index = 0;
// check if already loaded
while (index < CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT)
{
if (mContentApps[index] == app)
{
ChipLogProgress(DeviceLayer, "Already added");
// already added, return endpointId of already added endpoint.
// desired endpointId does not have any impact
return app->GetEndpointId();
}
index++;
}
ChipLogError(DeviceLayer, "Failed to add dynamic endpoint: No endpoints available!");

if (desiredEndpointId < FIXED_ENDPOINT_COUNT ||
emberAfGetDynamicIndexFromEndpoint(desiredEndpointId) != kEmberInvalidEndpointIndex)
{
// invalid desiredEndpointId
ChipLogError(DeviceLayer, "Failed to add dynamic endpoint: desired endpointID is invalid!");
return kNoCurrentEndpointId;
}

index = 0;
while (index < CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT)
{
if (mContentApps[index] != nullptr)
{
index++;
continue;
}
EmberAfStatus ret = emberAfSetDynamicEndpoint(index, desiredEndpointId, ep, dataVersionStorage, deviceTypeList);
if (ret != EMBER_ZCL_STATUS_SUCCESS)
{
ChipLogError(DeviceLayer, "Adding ContentApp error=%d", ret);
return kNoCurrentEndpointId;
}
ChipLogProgress(DeviceLayer, "Added ContentApp %s to dynamic endpoint %d (index=%d)", vendorApp.applicationId,
desiredEndpointId, index);
app->SetEndpointId(desiredEndpointId);
mContentApps[index] = app;
return app->GetEndpointId();
}
ChipLogError(DeviceLayer, "Failed to add dynamic endpoint: max endpoint count reached!");
return kNoCurrentEndpointId;
}

void ContentAppPlatform::IncrementCurrentEndpointID()
{
// Handle wrap condition
if (++mCurrentEndpointId < mFirstDynamicEndpointId)
{
mCurrentEndpointId = mFirstDynamicEndpointId;
}
}

EndpointId ContentAppPlatform::RemoveContentApp(ContentApp * app)
{
uint8_t index = 0;
Expand Down
15 changes: 13 additions & 2 deletions src/app/app-platform/ContentAppPlatform.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,21 @@ class DLL_EXPORT ContentAppPlatform

// add apps to the platform.
// This will assign the app to an endpoint (if it is not already added) and make it accessible via Matter
// returns the global endpoint for this app, or 0 if an error occurred
//
// returns the global endpoint for this app, or kNoCurrentEndpointId if an error occurred.
// dataVersionStorage.size() needs to be at least as big as the number of
// server clusters in the EmberAfEndpointType passed in.
EndpointId AddContentApp(ContentApp * app, EmberAfEndpointType * ep, const Span<DataVersion> & dataVersionStorage,
const Span<const EmberAfDeviceType> & deviceTypeList);

// add apps to the platform.
// This will assign the app to the desiredEndpointId (if it is not already used)
// and make it accessible via Matter, return the global endpoint for this app(if app is already added)
// , or kNoCurrentEndpointId if an error occurred. desiredEndpointId cannot be less that Fixed endpoint count
// dataVersionStorage.size() needs to be at least as big as the number of
// server clusters in the EmberAfEndpointType passed in.
EndpointId AddContentApp(ContentApp * app, EmberAfEndpointType * ep, const Span<DataVersion> & dataVersionStorage,
const Span<const EmberAfDeviceType> & deviceTypeList, EndpointId desiredEndpointId);

// remove app from the platform.
// returns the endpoint id where the app was, or 0 if app was not loaded
EndpointId RemoveContentApp(ContentApp * app);
Expand Down Expand Up @@ -155,6 +163,9 @@ class DLL_EXPORT ContentAppPlatform
EndpointId mCurrentEndpointId;
EndpointId mFirstDynamicEndpointId;
ContentApp * mContentApps[CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT];

private:
void IncrementCurrentEndpointID();
};

} // namespace AppPlatform
Expand Down

0 comments on commit 353e3e2

Please sign in to comment.