InteractiveCommands.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 "../clusters/DataModelLogger.h"
  20. #include "../common/CHIPCommand.h"
  21. #include "../common/Commands.h"
  22. #include <websocket-server/WebSocketServer.h>
  23. class Commands;
  24. class InteractiveCommand : public CHIPCommand
  25. {
  26. public:
  27. InteractiveCommand(const char * name, Commands * commandsHandler, const char * helpText,
  28. CredentialIssuerCommands * credsIssuerConfig) :
  29. CHIPCommand(name, credsIssuerConfig, helpText),
  30. mHandler(commandsHandler)
  31. {
  32. AddArgument("advertise-operational", 0, 1, &mAdvertiseOperational,
  33. "Advertise operational node over DNS-SD and accept incoming CASE sessions.");
  34. }
  35. /////////// CHIPCommand Interface /////////
  36. chip::System::Clock::Timeout GetWaitDuration() const override { return chip::System::Clock::Seconds16(0); }
  37. bool NeedsOperationalAdvertising() override;
  38. bool ParseCommand(char * command, int * status);
  39. private:
  40. Commands * mHandler = nullptr;
  41. chip::Optional<bool> mAdvertiseOperational;
  42. };
  43. class InteractiveStartCommand : public InteractiveCommand
  44. {
  45. public:
  46. InteractiveStartCommand(Commands * commandsHandler, CredentialIssuerCommands * credsIssuerConfig) :
  47. InteractiveCommand("start", commandsHandler, "Start an interactive shell that can then run other commands.",
  48. credsIssuerConfig)
  49. {}
  50. /////////// CHIPCommand Interface /////////
  51. CHIP_ERROR RunCommand() override;
  52. };
  53. class InteractiveServerCommand : public InteractiveCommand, public WebSocketServerDelegate, public RemoteDataModelLoggerDelegate
  54. {
  55. public:
  56. InteractiveServerCommand(Commands * commandsHandler, CredentialIssuerCommands * credsIssuerConfig) :
  57. InteractiveCommand("server", commandsHandler, "Start a websocket server that can receive commands sent by another process.",
  58. credsIssuerConfig)
  59. {
  60. AddArgument("port", 0, UINT16_MAX, &mPort, "Port the websocket will listen to. Defaults to 9002.");
  61. }
  62. /////////// CHIPCommand Interface /////////
  63. CHIP_ERROR RunCommand() override;
  64. /////////// WebSocketServerDelegate Interface /////////
  65. bool OnWebSocketMessageReceived(char * msg) override;
  66. /////////// RemoteDataModelLoggerDelegate interface /////////
  67. CHIP_ERROR LogJSON(const char * json) override;
  68. private:
  69. WebSocketServer mWebSocketServer;
  70. chip::Optional<uint16_t> mPort;
  71. };