From f45d458d84f35f5755dea03546db77542f71e37a Mon Sep 17 00:00:00 2001 From: Amit Jain Date: Mon, 29 Aug 2022 23:38:25 -0700 Subject: [PATCH] Review comments... --- src/app/app-platform/ContentAppPlatform.cpp | 135 ++++++++++++-------- src/app/app-platform/ContentAppPlatform.h | 20 ++- 2 files changed, 100 insertions(+), 55 deletions(-) diff --git a/src/app/app-platform/ContentAppPlatform.cpp b/src/app/app-platform/ContentAppPlatform.cpp index afc08c1a03750b..b7816281d32200 100644 --- a/src/app/app-platform/ContentAppPlatform.cpp +++ b/src/app/app-platform/ContentAppPlatform.cpp @@ -88,7 +88,7 @@ namespace AppPlatform { EndpointId ContentAppPlatform::AddContentApp(ContentApp * app, EmberAfEndpointType * ep, const Span & dataVersionStorage, - const Span & deviceTypeList, EndpointId desiredEndpointId) + const Span & deviceTypeList) { CatalogVendorApp vendorApp = app->GetApplicationBasicDelegate()->GetCatalogVendorApp(); @@ -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 & dataVersionStorage, + const Span & 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; diff --git a/src/app/app-platform/ContentAppPlatform.h b/src/app/app-platform/ContentAppPlatform.h index e20d1164c32b67..a9fbb7c945cfa9 100644 --- a/src/app/app-platform/ContentAppPlatform.h +++ b/src/app/app-platform/ContentAppPlatform.h @@ -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 & dataVersionStorage, - const Span & deviceTypeList, EndpointId desiredEndpointId = 0); + const Span & 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 & dataVersionStorage, + const Span & deviceTypeList, EndpointId desiredEndpointId); // remove app from the platform. // returns the endpoint id where the app was, or 0 if app was not loaded @@ -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