CommissioneeShellCommands.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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/Dnssd.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. Server::GetInstance().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, " sendudc <address> <port> Send UDC message to address. Usage: commissionee sendudc ::1 5543\r\n");
  54. #endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT
  55. // TODO: Figure out whether setdiscoverytimeout is a reasonable thing to do
  56. // at all, and if so what semantics it should have. Presumably it should
  57. // affect BLE discovery too, not just DNS-SD. How should it interact with
  58. // explicit timeouts specified in OpenCommissioningWindow?
  59. #if 0
  60. streamer_printf(
  61. sout, " setdiscoverytimeout <timeout> Set discovery timeout in seconds. Usage: commissionee setdiscoverytimeout 30\r\n");
  62. #endif
  63. #if CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY
  64. streamer_printf(sout,
  65. " setextendeddiscoverytimeout <timeout> Set extendeddiscovery timeout in seconds. Usage: commissionee "
  66. "setextendeddiscoverytimeout 30\r\n");
  67. #endif // CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY
  68. streamer_printf(sout,
  69. " restartmdns <commissioningMode> (disabled|enabled_basic|enabled_enhanced) Start Mdns with given "
  70. "settings. Usage: commissionee "
  71. "restartmdns enabled_basic\r\n");
  72. streamer_printf(sout, " startbcm Start basic commissioning mode. Usage: commissionee startbcm\r\n");
  73. streamer_printf(sout, "\r\n");
  74. return CHIP_NO_ERROR;
  75. }
  76. static CHIP_ERROR CommissioneeHandler(int argc, char ** argv)
  77. {
  78. if (argc == 0 || strcmp(argv[0], "help") == 0)
  79. {
  80. return PrintAllCommands();
  81. }
  82. #if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT
  83. if (strcmp(argv[0], "sendudc") == 0)
  84. {
  85. char * eptr;
  86. chip::Inet::IPAddress commissioner;
  87. chip::Inet::IPAddress::FromString(argv[1], commissioner);
  88. uint16_t port = (uint16_t) strtol(argv[2], &eptr, 10);
  89. return SendUDC(true, chip::Transport::PeerAddress::UDP(commissioner, port));
  90. }
  91. #endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT
  92. // TODO: Figure out whether setdiscoverytimeout is a reasonable thing to do
  93. // at all, and if so what semantics it should have. Presumably it should
  94. // affect BLE discovery too, not just DNS-SD. How should it interact with
  95. // explicit timeouts specified in OpenCommissioningWindow?
  96. #if 0
  97. if (strcmp(argv[0], "setdiscoverytimeout") == 0)
  98. {
  99. char * eptr;
  100. int16_t timeout = (int16_t) strtol(argv[1], &eptr, 10);
  101. chip::app::DnssdServer::Instance().SetDiscoveryTimeoutSecs(timeout);
  102. return CHIP_NO_ERROR;
  103. }
  104. #endif
  105. #if CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY
  106. if (strcmp(argv[0], "setextendeddiscoverytimeout") == 0)
  107. {
  108. char * eptr;
  109. int16_t timeout = (int16_t) strtol(argv[1], &eptr, 10);
  110. chip::app::DnssdServer::Instance().SetExtendedDiscoveryTimeoutSecs(timeout);
  111. return CHIP_NO_ERROR;
  112. }
  113. #endif // CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY
  114. if (strcmp(argv[0], "restartmdns") == 0)
  115. {
  116. if (argc < 2)
  117. {
  118. return PrintAllCommands();
  119. }
  120. if (strcmp(argv[1], "disabled") == 0)
  121. {
  122. chip::app::DnssdServer::Instance().StartServer(chip::Dnssd::CommissioningMode::kDisabled);
  123. return CHIP_NO_ERROR;
  124. }
  125. if (strcmp(argv[1], "enabled_basic") == 0)
  126. {
  127. chip::app::DnssdServer::Instance().StartServer(chip::Dnssd::CommissioningMode::kEnabledBasic);
  128. return CHIP_NO_ERROR;
  129. }
  130. if (strcmp(argv[1], "enabled_enhanced") == 0)
  131. {
  132. chip::app::DnssdServer::Instance().StartServer(chip::Dnssd::CommissioningMode::kEnabledEnhanced);
  133. return CHIP_NO_ERROR;
  134. }
  135. return PrintAllCommands();
  136. }
  137. if (strcmp(argv[0], "startbcm") == 0)
  138. {
  139. Server::GetInstance().GetCommissioningWindowManager().OpenBasicCommissioningWindow();
  140. return CHIP_NO_ERROR;
  141. }
  142. return CHIP_ERROR_INVALID_ARGUMENT;
  143. }
  144. void RegisterCommissioneeCommands()
  145. {
  146. static const shell_command_t sDeviceComand = { &CommissioneeHandler, "commissionee",
  147. "Commissionee commands. Usage: commissionee [command_name]" };
  148. // Register the root `device` command with the top-level shell.
  149. Engine::Root().RegisterCommands(&sDeviceComand, 1);
  150. }
  151. } // namespace Shell
  152. } // namespace chip