CommissioneeShellCommands.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. *
  3. * Copyright (c) 2021 Project CHIP Authors
  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. * @file Contains shell commands for a commissionee (eg. end device) related to commissioning.
  19. */
  20. #include <CommissioneeShellCommands.h>
  21. #include <app/server/Mdns.h>
  22. #include <app/server/Server.h>
  23. #include <inttypes.h>
  24. #include <lib/core/CHIPCore.h>
  25. #include <lib/shell/Commands.h>
  26. #include <lib/shell/Engine.h>
  27. #include <lib/shell/commands/Help.h>
  28. #include <lib/support/CHIPArgParser.hpp>
  29. #include <lib/support/CHIPMem.h>
  30. #include <lib/support/CodeUtils.h>
  31. #include <platform/CHIPDeviceLayer.h>
  32. #include <protocols/user_directed_commissioning/UserDirectedCommissioning.h>
  33. namespace chip {
  34. namespace Shell {
  35. #if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT
  36. static CHIP_ERROR SendUDC(bool printHeader, chip::Transport::PeerAddress commissioner)
  37. {
  38. streamer_t * sout = streamer_get();
  39. if (printHeader)
  40. {
  41. streamer_printf(sout, "SendUDC: ");
  42. }
  43. SendUserDirectedCommissioningRequest(commissioner);
  44. streamer_printf(sout, "done\r\n");
  45. return CHIP_NO_ERROR;
  46. }
  47. #endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT
  48. static CHIP_ERROR PrintAllCommands()
  49. {
  50. streamer_t * sout = streamer_get();
  51. streamer_printf(sout, " help Usage: commissionee <subcommand>\r\n");
  52. #if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT
  53. streamer_printf(sout,
  54. " sendudc <address> <port> Send UDC message to address. Usage: commissionee sendudc 127.0.0.1 5543\r\n");
  55. #endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT
  56. streamer_printf(sout,
  57. " restartmdns <commissioningMode> (disabled|enabled_basic|enabled_enhanced) Start Mdns with given "
  58. "settings. Usage: commissionee "
  59. "restartmdns enabled_basic\r\n");
  60. streamer_printf(sout, "\r\n");
  61. return CHIP_NO_ERROR;
  62. }
  63. static CHIP_ERROR CommissioneeHandler(int argc, char ** argv)
  64. {
  65. CHIP_ERROR error = CHIP_NO_ERROR;
  66. if (argc == 0 || strcmp(argv[0], "help") == 0)
  67. {
  68. return PrintAllCommands();
  69. }
  70. #if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT
  71. else if (strcmp(argv[0], "sendudc") == 0)
  72. {
  73. char * eptr;
  74. chip::Inet::IPAddress commissioner;
  75. chip::Inet::IPAddress::FromString(argv[1], commissioner);
  76. uint16_t port = (uint16_t) strtol(argv[2], &eptr, 10);
  77. return error = SendUDC(true, chip::Transport::PeerAddress::UDP(commissioner, port));
  78. }
  79. #endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT
  80. else if (strcmp(argv[0], "restartmdns") == 0)
  81. {
  82. if (argc < 2)
  83. {
  84. return PrintAllCommands();
  85. }
  86. if (strcmp(argv[1], "disabled") == 0)
  87. {
  88. chip::app::Mdns::StartServer(chip::app::Mdns::CommissioningMode::kDisabled);
  89. return CHIP_NO_ERROR;
  90. }
  91. if (strcmp(argv[1], "enabled_basic") == 0)
  92. {
  93. chip::app::Mdns::StartServer(chip::app::Mdns::CommissioningMode::kEnabledBasic);
  94. return CHIP_NO_ERROR;
  95. }
  96. else if (strcmp(argv[1], "enabled_enhanced") == 0)
  97. {
  98. chip::app::Mdns::StartServer(chip::app::Mdns::CommissioningMode::kEnabledEnhanced);
  99. return CHIP_NO_ERROR;
  100. }
  101. return PrintAllCommands();
  102. }
  103. else
  104. {
  105. return CHIP_ERROR_INVALID_ARGUMENT;
  106. }
  107. return error;
  108. }
  109. void RegisterCommissioneeCommands()
  110. {
  111. static const shell_command_t sDeviceComand = { &CommissioneeHandler, "commissionee",
  112. "Commissionee commands. Usage: commissionee [command_name]" };
  113. // Register the root `device` command with the top-level shell.
  114. Engine::Root().RegisterCommands(&sDeviceComand, 1);
  115. return;
  116. }
  117. } // namespace Shell
  118. } // namespace chip