TVApp-JNI.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (c) 2021 Project CHIP Authors
  3. * All rights reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include "TvApp-JNI.h"
  19. #include "AppImpl.h"
  20. #include "ChannelManager.h"
  21. #include "ContentLauncherManager.h"
  22. #include "DeviceCallbacks.h"
  23. #include "JNIDACProvider.h"
  24. #include "KeypadInputManager.h"
  25. #include "LevelManager.h"
  26. #include "LowPowerManager.h"
  27. #include "MediaInputManager.h"
  28. #include "MediaPlaybackManager.h"
  29. #include "OnOffManager.h"
  30. #include "WakeOnLanManager.h"
  31. #include "credentials/DeviceAttestationCredsProvider.h"
  32. #include <app/server/Dnssd.h>
  33. #include <app/server/java/AndroidAppServerWrapper.h>
  34. #include <credentials/DeviceAttestationCredsProvider.h>
  35. #include <credentials/examples/DeviceAttestationCredsExample.h>
  36. #include <jni.h>
  37. #include <lib/core/CHIPError.h>
  38. #include <lib/support/CHIPJNIError.h>
  39. #include <lib/support/JniReferences.h>
  40. #include <lib/support/JniTypeWrappers.h>
  41. using namespace chip;
  42. using namespace chip::app;
  43. using namespace chip::AppPlatform;
  44. using namespace chip::Credentials;
  45. #define JNI_METHOD(RETURN, METHOD_NAME) extern "C" JNIEXPORT RETURN JNICALL Java_com_tcl_chip_tvapp_TvApp_##METHOD_NAME
  46. TvAppJNI TvAppJNI::sInstance;
  47. void TvAppJNI::InitializeWithObjects(jobject app)
  48. {
  49. JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
  50. VerifyOrReturn(env != nullptr, ChipLogError(Zcl, "Failed to GetEnvForCurrentThread for TvAppJNI"));
  51. mTvAppObject = env->NewGlobalRef(app);
  52. VerifyOrReturn(mTvAppObject != nullptr, ChipLogError(Zcl, "Failed to NewGlobalRef TvAppJNI"));
  53. jclass managerClass = env->GetObjectClass(mTvAppObject);
  54. VerifyOrReturn(managerClass != nullptr, ChipLogError(Zcl, "Failed to get TvAppJNI Java class"));
  55. mPostClusterInitMethod = env->GetMethodID(managerClass, "postClusterInit", "(II)V");
  56. if (mPostClusterInitMethod == nullptr)
  57. {
  58. ChipLogError(Zcl, "Failed to access ChannelManager 'postClusterInit' method");
  59. env->ExceptionClear();
  60. }
  61. }
  62. void TvAppJNI::PostClusterInit(int clusterId, int endpoint)
  63. {
  64. JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
  65. VerifyOrReturn(env != nullptr, ChipLogError(Zcl, "Failed to GetEnvForCurrentThread for TvAppJNI::PostClusterInit"));
  66. VerifyOrReturn(mTvAppObject != nullptr, ChipLogError(Zcl, "TvAppJNI::mTvAppObject null"));
  67. VerifyOrReturn(mPostClusterInitMethod != nullptr, ChipLogError(Zcl, "TvAppJNI::mPostClusterInitMethod null"));
  68. env->CallVoidMethod(mTvAppObject, mPostClusterInitMethod, static_cast<jint>(clusterId), static_cast<jint>(endpoint));
  69. if (env->ExceptionCheck())
  70. {
  71. ChipLogError(Zcl, "Failed to call TvAppJNI 'postClusterInit' method");
  72. env->ExceptionClear();
  73. }
  74. }
  75. jint JNI_OnLoad(JavaVM * jvm, void * reserved)
  76. {
  77. return AndroidAppServerJNI_OnLoad(jvm, reserved);
  78. }
  79. void JNI_OnUnload(JavaVM * jvm, void * reserved)
  80. {
  81. return AndroidAppServerJNI_OnUnload(jvm, reserved);
  82. }
  83. JNI_METHOD(void, nativeInit)(JNIEnv *, jobject app)
  84. {
  85. TvAppJNIMgr().InitializeWithObjects(app);
  86. }
  87. JNI_METHOD(void, setKeypadInputManager)(JNIEnv *, jobject, jint endpoint, jobject manager)
  88. {
  89. KeypadInputManager::NewManager(endpoint, manager);
  90. }
  91. JNI_METHOD(void, setWakeOnLanManager)(JNIEnv *, jobject, jint endpoint, jobject manager)
  92. {
  93. WakeOnLanManager::NewManager(endpoint, manager);
  94. }
  95. JNI_METHOD(void, setMediaInputManager)(JNIEnv *, jobject, jint endpoint, jobject manager)
  96. {
  97. MediaInputManager::NewManager(endpoint, manager);
  98. }
  99. JNI_METHOD(void, setContentLaunchManager)(JNIEnv *, jobject, jint endpoint, jobject manager)
  100. {
  101. ContentLauncherManager::NewManager(endpoint, manager);
  102. }
  103. JNI_METHOD(void, setLowPowerManager)(JNIEnv *, jobject, jint endpoint, jobject manager)
  104. {
  105. LowPowerManager::NewManager(endpoint, manager);
  106. }
  107. JNI_METHOD(void, setMediaPlaybackManager)(JNIEnv *, jobject, jint endpoint, jobject manager)
  108. {
  109. MediaPlaybackManager::NewManager(endpoint, manager);
  110. }
  111. JNI_METHOD(void, setChannelManager)(JNIEnv *, jobject, jint endpoint, jobject manager)
  112. {
  113. ChannelManager::NewManager(endpoint, manager);
  114. }
  115. JNI_METHOD(void, setDACProvider)(JNIEnv *, jobject, jobject provider)
  116. {
  117. if (!chip::Credentials::IsDeviceAttestationCredentialsProviderSet())
  118. {
  119. JNIDACProvider * p = new JNIDACProvider(provider);
  120. chip::Credentials::SetDeviceAttestationCredentialsProvider(p);
  121. }
  122. }
  123. JNI_METHOD(void, preServerInit)(JNIEnv *, jobject app)
  124. {
  125. chip::DeviceLayer::StackLock lock;
  126. ChipLogProgress(Zcl, "TvAppJNI::preServerInit");
  127. PreServerInit();
  128. }
  129. JNI_METHOD(void, postServerInit)(JNIEnv *, jobject app)
  130. {
  131. chip::DeviceLayer::StackLock lock;
  132. ChipLogProgress(Zcl, "TvAppJNI::postServerInit");
  133. InitVideoPlayerPlatform();
  134. }
  135. JNI_METHOD(void, setOnOffManager)(JNIEnv *, jobject, jint endpoint, jobject manager)
  136. {
  137. OnOffManager::NewManager(endpoint, manager);
  138. }
  139. JNI_METHOD(jboolean, setOnOff)(JNIEnv *, jobject, jint endpoint, jboolean value)
  140. {
  141. return OnOffManager::SetOnOff(endpoint, value);
  142. }
  143. JNI_METHOD(void, setLevelManager)(JNIEnv *, jobject, jint endpoint, jobject manager)
  144. {
  145. LevelManager::NewManager(endpoint, manager);
  146. }
  147. JNI_METHOD(jboolean, setCurrentLevel)(JNIEnv *, jobject, jint endpoint, jboolean value)
  148. {
  149. return LevelManager::SetLevel(endpoint, value);
  150. }
  151. JNI_METHOD(void, setChipDeviceEventProvider)(JNIEnv *, jobject, jobject provider)
  152. {
  153. DeviceCallbacks::NewManager(provider);
  154. }
  155. JNI_METHOD(jint, addContentApp)
  156. (JNIEnv *, jobject, jstring vendorName, jint vendorId, jstring appName, jint productId, jstring appVersion, jobject manager)
  157. {
  158. JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
  159. JniUtfString vName(env, vendorName);
  160. JniUtfString aName(env, appName);
  161. JniUtfString aVersion(env, appVersion);
  162. EndpointId epId = AddContentApp(vName.c_str(), static_cast<uint16_t>(vendorId), aName.c_str(), static_cast<uint16_t>(productId),
  163. aVersion.c_str(), manager);
  164. return static_cast<uint16_t>(epId);
  165. }
  166. JNI_METHOD(void, sendTestMessage)(JNIEnv *, jobject, jint endpoint, jstring message)
  167. {
  168. JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
  169. const char * nmessage = env->GetStringUTFChars(message, 0);
  170. ChipLogProgress(Zcl, "TvApp-JNI SendTestMessage called with message %s", nmessage);
  171. SendTestMessage(static_cast<EndpointId>(endpoint), nmessage);
  172. }