ModelCommand.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. #pragma once
  19. #ifdef CONFIG_USE_LOCAL_STORAGE
  20. #include <controller/ExamplePersistentStorage.h>
  21. #endif // CONFIG_USE_LOCAL_STORAGE
  22. #include "../common/CHIPCommand.h"
  23. #include <lib/core/CHIPEncoding.h>
  24. class ModelCommand : public CHIPCommand
  25. {
  26. public:
  27. ModelCommand(const char * commandName, CredentialIssuerCommands * credsIssuerConfig, bool supportsMultipleEndpoints = false) :
  28. CHIPCommand(commandName, credsIssuerConfig), mOnDeviceConnectedCallback(OnDeviceConnectedFn, this),
  29. mOnDeviceConnectionFailureCallback(OnDeviceConnectionFailureFn, this), mSupportsMultipleEndpoints(supportsMultipleEndpoints)
  30. {}
  31. void AddArguments(bool skipEndpoints = false)
  32. {
  33. AddArgument(
  34. "destination-id", 0, UINT64_MAX, &mDestinationId,
  35. "64-bit node or group identifier.\n Group identifiers are detected by being in the 0xFFFF'FFFF'FFFF'xxxx range.");
  36. if (skipEndpoints == false)
  37. {
  38. if (mSupportsMultipleEndpoints)
  39. {
  40. AddArgument("endpoint-ids", 0, UINT16_MAX, &mEndPointId,
  41. "Comma-separated list of endpoint ids (e.g. \"1\" or \"1,2,3\").\n Allowed to be 0xFFFF to indicate a "
  42. "wildcard endpoint.");
  43. }
  44. else
  45. {
  46. AddArgument("endpoint-id-ignored-for-group-commands", 0, UINT16_MAX, &mEndPointId,
  47. "Endpoint the command is targeted at.");
  48. }
  49. }
  50. AddArgument("timeout", 0, UINT16_MAX, &mTimeout);
  51. }
  52. /////////// CHIPCommand Interface /////////
  53. CHIP_ERROR RunCommand() override;
  54. chip::System::Clock::Timeout GetWaitDuration() const override { return chip::System::Clock::Seconds16(mTimeout.ValueOr(20)); }
  55. virtual CHIP_ERROR SendCommand(chip::DeviceProxy * device, std::vector<chip::EndpointId> endPointIds) = 0;
  56. virtual CHIP_ERROR SendGroupCommand(chip::GroupId groupId, chip::FabricIndex fabricIndex) { return CHIP_ERROR_BAD_REQUEST; };
  57. void Shutdown() override;
  58. protected:
  59. chip::Optional<uint16_t> mTimeout;
  60. private:
  61. chip::NodeId mDestinationId;
  62. std::vector<chip::EndpointId> mEndPointId;
  63. static void OnDeviceConnectedFn(void * context, chip::Messaging::ExchangeManager & exchangeMgr,
  64. const chip::SessionHandle & sessionHandle);
  65. static void OnDeviceConnectionFailureFn(void * context, const chip::ScopedNodeId & peerId, CHIP_ERROR error);
  66. chip::Callback::Callback<chip::OnDeviceConnected> mOnDeviceConnectedCallback;
  67. chip::Callback::Callback<chip::OnDeviceConnectionFailure> mOnDeviceConnectionFailureCallback;
  68. const bool mSupportsMultipleEndpoints;
  69. };