diff --git a/examples/tv-app/android/java/MyUserPrompterResolver-JNI.cpp b/examples/tv-app/android/java/MyUserPrompterResolver-JNI.cpp index 41f2c8f32ebec6..4dc95a2b16b684 100644 --- a/examples/tv-app/android/java/MyUserPrompterResolver-JNI.cpp +++ b/examples/tv-app/android/java/MyUserPrompterResolver-JNI.cpp @@ -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 @@ -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 diff --git a/examples/tv-app/android/java/TVApp-JNI.cpp b/examples/tv-app/android/java/TVApp-JNI.cpp index aec9759c1f56db..ac1ef7abfd4f66 100644 --- a/examples/tv-app/android/java/TVApp-JNI.cpp +++ b/examples/tv-app/android/java/TVApp-JNI.cpp @@ -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); diff --git a/src/app/app-platform/ContentAppPlatform.cpp b/src/app/app-platform/ContentAppPlatform.cpp index de8e53faabcb3b..7865c87c71d0a3 100644 --- a/src/app/app-platform/ContentAppPlatform.cpp +++ b/src/app/app-platform/ContentAppPlatform.cpp @@ -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++; @@ -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 & 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; + } + 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; diff --git a/src/app/app-platform/ContentAppPlatform.h b/src/app/app-platform/ContentAppPlatform.h index cac2441d46382e..3a39365e95c754 100644 --- a/src/app/app-platform/ContentAppPlatform.h +++ b/src/app/app-platform/ContentAppPlatform.h @@ -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 & dataVersionStorage, 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 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 & 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 EndpointId RemoveContentApp(ContentApp * app); @@ -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