SubscriptionsCommands.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (c) 2022 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. #include <lib/core/CHIPError.h>
  20. #include <commands/common/CHIPCommand.h>
  21. #include <commands/common/Commands.h>
  22. class ShutdownSubscription : public CHIPCommand
  23. {
  24. public:
  25. ShutdownSubscription(CredentialIssuerCommands * credsIssuerConfig) :
  26. CHIPCommand("shutdown-one", credsIssuerConfig,
  27. "Shut down a single subscription, identified by its subscription id and target node id.")
  28. {
  29. AddArgument("subscription-id", 0, UINT32_MAX, &mSubscriptionId);
  30. AddArgument("node-id", 0, UINT64_MAX, &mNodeId,
  31. "The node id, scoped to the commissioner name the command is running under.");
  32. }
  33. /////////// CHIPCommand Interface /////////
  34. CHIP_ERROR RunCommand() override
  35. {
  36. CHIP_ERROR err = chip::app::InteractionModelEngine::GetInstance()->ShutdownSubscription(
  37. chip::ScopedNodeId(mNodeId, CurrentCommissioner().GetFabricIndex()), mSubscriptionId);
  38. SetCommandExitStatus(err);
  39. return CHIP_NO_ERROR;
  40. }
  41. chip::System::Clock::Timeout GetWaitDuration() const override { return chip::System::Clock::Seconds16(10); }
  42. private:
  43. chip::SubscriptionId mSubscriptionId;
  44. chip::NodeId mNodeId;
  45. };
  46. class ShutdownSubscriptionsForNode : public CHIPCommand
  47. {
  48. public:
  49. ShutdownSubscriptionsForNode(CredentialIssuerCommands * credsIssuerConfig) :
  50. CHIPCommand("shutdown-all-for-node", credsIssuerConfig, "Shut down all subscriptions targeting a given node.")
  51. {
  52. AddArgument("node-id", 0, UINT64_MAX, &mNodeId,
  53. "The node id, scoped to the commissioner name the command is running under.");
  54. }
  55. /////////// CHIPCommand Interface /////////
  56. CHIP_ERROR RunCommand() override
  57. {
  58. chip::app::InteractionModelEngine::GetInstance()->ShutdownSubscriptions(CurrentCommissioner().GetFabricIndex(), mNodeId);
  59. SetCommandExitStatus(CHIP_NO_ERROR);
  60. return CHIP_NO_ERROR;
  61. }
  62. chip::System::Clock::Timeout GetWaitDuration() const override { return chip::System::Clock::Seconds16(10); }
  63. private:
  64. chip::NodeId mNodeId;
  65. };
  66. class ShutdownAllSubscriptions : public CHIPCommand
  67. {
  68. public:
  69. ShutdownAllSubscriptions(CredentialIssuerCommands * credsIssuerConfig) :
  70. CHIPCommand("shutdown-all", credsIssuerConfig, "Shut down all subscriptions to all nodes.")
  71. {}
  72. /////////// CHIPCommand Interface /////////
  73. CHIP_ERROR RunCommand() override
  74. {
  75. chip::app::InteractionModelEngine::GetInstance()->ShutdownAllSubscriptions();
  76. SetCommandExitStatus(CHIP_NO_ERROR);
  77. return CHIP_NO_ERROR;
  78. }
  79. chip::System::Clock::Timeout GetWaitDuration() const override { return chip::System::Clock::Seconds16(10); }
  80. private:
  81. };
  82. void registerCommandsSubscriptions(Commands & commands, CredentialIssuerCommands * credsIssuerConfig)
  83. {
  84. const char * clusterName = "Subscriptions";
  85. commands_list clusterCommands = {
  86. make_unique<ShutdownSubscription>(credsIssuerConfig), //
  87. make_unique<ShutdownSubscriptionsForNode>(credsIssuerConfig), //
  88. make_unique<ShutdownAllSubscriptions>(credsIssuerConfig), //
  89. };
  90. commands.RegisterCommandSet(clusterName, clusterCommands, "Commands for shutting down subscriptions.");
  91. }