ModelCommand.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * Copyright (c) 2020 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 "ModelCommand.h"
  19. #include <app/InteractionModelEngine.h>
  20. #include <inttypes.h>
  21. using namespace ::chip;
  22. CHIP_ERROR ModelCommand::RunCommand()
  23. {
  24. if (IsGroupId(mDestinationId))
  25. {
  26. FabricIndex fabricIndex = CurrentCommissioner().GetFabricIndex();
  27. ChipLogProgress(chipTool, "Sending command to group 0x%x", GroupIdFromNodeId(mDestinationId));
  28. return SendGroupCommand(GroupIdFromNodeId(mDestinationId), fabricIndex);
  29. }
  30. ChipLogProgress(chipTool, "Sending command to node 0x%" PRIx64, mDestinationId);
  31. CommissioneeDeviceProxy * commissioneeDeviceProxy = nullptr;
  32. if (CHIP_NO_ERROR == CurrentCommissioner().GetDeviceBeingCommissioned(mDestinationId, &commissioneeDeviceProxy))
  33. {
  34. return SendCommand(commissioneeDeviceProxy, mEndPointId);
  35. }
  36. return CurrentCommissioner().GetConnectedDevice(mDestinationId, &mOnDeviceConnectedCallback,
  37. &mOnDeviceConnectionFailureCallback);
  38. }
  39. void ModelCommand::OnDeviceConnectedFn(void * context, chip::Messaging::ExchangeManager & exchangeMgr,
  40. const chip::SessionHandle & sessionHandle)
  41. {
  42. ModelCommand * command = reinterpret_cast<ModelCommand *>(context);
  43. VerifyOrReturn(command != nullptr, ChipLogError(chipTool, "OnDeviceConnectedFn: context is null"));
  44. chip::OperationalDeviceProxy device(&exchangeMgr, sessionHandle);
  45. CHIP_ERROR err = command->SendCommand(&device, command->mEndPointId);
  46. VerifyOrReturn(CHIP_NO_ERROR == err, command->SetCommandExitStatus(err));
  47. }
  48. void ModelCommand::OnDeviceConnectionFailureFn(void * context, const chip::ScopedNodeId & peerId, CHIP_ERROR err)
  49. {
  50. LogErrorOnFailure(err);
  51. ModelCommand * command = reinterpret_cast<ModelCommand *>(context);
  52. VerifyOrReturn(command != nullptr, ChipLogError(chipTool, "OnDeviceConnectionFailureFn: context is null"));
  53. command->SetCommandExitStatus(err);
  54. }
  55. void ModelCommand::Shutdown()
  56. {
  57. mOnDeviceConnectedCallback.Cancel();
  58. mOnDeviceConnectionFailureCallback.Cancel();
  59. CHIPCommand::Shutdown();
  60. }