ContentAppCommandDelegate.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. *
  3. * Copyright (c) 2021 Project CHIP Authors
  4. * All rights reserved.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. /**
  19. * @brief Contains Implementation of the ContentAppCommandDelegate
  20. */
  21. #include "ContentAppCommandDelegate.h"
  22. #include <jni.h>
  23. #include <lib/support/CHIPJNIError.h>
  24. #include <lib/support/JniReferences.h>
  25. #include <lib/support/JniTypeWrappers.h>
  26. namespace chip {
  27. namespace AppPlatform {
  28. const char * ContentAppCommandDelegate::sendCommand(chip::EndpointId epID, std::string commandPayload)
  29. {
  30. // to support the hardcoded sample apps.
  31. if (mSendCommandMethod == nullptr)
  32. {
  33. return "Failed";
  34. }
  35. JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
  36. UtfString jCommandPayload(env, commandPayload.c_str());
  37. ChipLogProgress(Zcl, "ContentAppCommandDelegate::sendCommand with payload %s", commandPayload.c_str());
  38. jstring resp = (jstring) env->CallObjectMethod(mContentAppEndpointManager, mSendCommandMethod, static_cast<jint>(epID),
  39. jCommandPayload.jniValue());
  40. if (env->ExceptionCheck())
  41. {
  42. ChipLogError(Zcl, "Java exception in ContentAppCommandDelegate::sendCommand");
  43. env->ExceptionDescribe();
  44. env->ExceptionClear();
  45. // TODO : Need to have proper errors passed back.
  46. return "Failed";
  47. }
  48. const char * ret = env->GetStringUTFChars(resp, 0);
  49. return ret;
  50. }
  51. } // namespace AppPlatform
  52. } // namespace chip