Skip to content

Commit

Permalink
Review comments...
Browse files Browse the repository at this point in the history
  • Loading branch information
amitnj committed Aug 30, 2022
1 parent 41b2104 commit f45d458
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 55 deletions.
135 changes: 85 additions & 50 deletions src/app/app-platform/ContentAppPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ namespace AppPlatform {

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

Expand All @@ -107,69 +107,104 @@ EndpointId ContentAppPlatform::AddContentApp(ContentApp * app, EmberAfEndpointTy
index++;
}

if (desiredEndpointId >= emberAfFixedEndpointCount() &&
emberAfGetDynamicIndexFromEndpoint(desiredEndpointId) != kEmberInvalidEndpointIndex)
{
// an endpoint already exists with the desiredEndpointId. return error.
ChipLogError(DeviceLayer, "Failed to add dynamic endpoint: desired endpointID already in use!");
return kNoCurrentEndpointId;
}

index = 0;
while (index < CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT)
{
if (nullptr == mContentApps[index])
if (mContentApps[index] != nullptr)
{
mContentApps[index] = app;
EmberAfStatus ret;

if (desiredEndpointId >= emberAfFixedEndpointCount())
index++;
continue;
}
mContentApps[index] = app;
EmberAfStatus ret;
EndpointId initEndpointId = mCurrentEndpointId;

do {
ret = emberAfSetDynamicEndpoint(index, mCurrentEndpointId, ep, dataVersionStorage, deviceTypeList);
if (ret == EMBER_ZCL_STATUS_SUCCESS)
{
ret = emberAfSetDynamicEndpoint(index, desiredEndpointId, ep, dataVersionStorage, deviceTypeList);
if (ret == EMBER_ZCL_STATUS_SUCCESS)
{
ChipLogProgress(DeviceLayer, "Added ContentApp %s to dynamic endpoint %d (index=%d)", vendorApp.applicationId,
desiredEndpointId, index);
app->SetEndpointId(desiredEndpointId);
return app->GetEndpointId();
}
else
{
ChipLogError(DeviceLayer, "Adding ContentApp error=%d", ret);
return kNoCurrentEndpointId;
}
ChipLogProgress(DeviceLayer, "Added ContentApp %s to dynamic endpoint %d (index=%d)",
vendorApp.applicationId, mCurrentEndpointId, index);
app->SetEndpointId(mCurrentEndpointId);
IncrementCurrentEndpointID();
return app->GetEndpointId();
}
else
else if (ret != EMBER_ZCL_STATUS_DUPLICATE_EXISTS)
{
while (1)
{
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;
}
}
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;
}
mContentApps[index] = app;
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);
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
20 changes: 15 additions & 5 deletions src/app/app-platform/ContentAppPlatform.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,20 @@ 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
// If a desiredEndpointId is passed (cannot be less that Fixed endpoint count)
// framework will attempt to add the endpoint and assign the desiredEndpointId
// if desiredEndpointId is already taken endpoint is not added and 0 is returned.
// returns the global endpoint for this app, or 0 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, EndpointId desiredEndpointId = 0);
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 0 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
Expand Down Expand Up @@ -157,6 +163,10 @@ 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 f45d458

Please sign in to comment.