ControllerShellCommands.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 for performing discovery (eg. of commissionable nodes) related to commissioning.
  19. */
  20. #include <ControllerShellCommands.h>
  21. #include <inttypes.h>
  22. #include <lib/core/CHIPCore.h>
  23. #include <lib/shell/Commands.h>
  24. #include <lib/shell/Engine.h>
  25. #include <lib/shell/commands/Help.h>
  26. #include <lib/support/CHIPArgParser.hpp>
  27. #include <lib/support/CHIPMem.h>
  28. #include <lib/support/CodeUtils.h>
  29. #include <platform/CHIPDeviceLayer.h>
  30. #include <protocols/user_directed_commissioning/UserDirectedCommissioning.h>
  31. namespace chip {
  32. namespace Shell {
  33. using namespace ::chip::Controller;
  34. DeviceCommissioner * gCommissioner;
  35. #if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY
  36. static CHIP_ERROR ResetUDC(bool printHeader)
  37. {
  38. streamer_t * sout = streamer_get();
  39. if (printHeader)
  40. {
  41. streamer_printf(sout, "resetudc: ");
  42. }
  43. gCommissioner->GetUserDirectedCommissioningServer()->ResetUDCClientProcessingStates();
  44. streamer_printf(sout, "done\r\n");
  45. return CHIP_NO_ERROR;
  46. }
  47. static CHIP_ERROR discover(bool printHeader)
  48. {
  49. streamer_t * sout = streamer_get();
  50. if (printHeader)
  51. {
  52. streamer_printf(sout, "Discover: ");
  53. }
  54. Mdns::DiscoveryFilter filter(Mdns::DiscoveryFilterType::kNone, (uint16_t) 0);
  55. gCommissioner->DiscoverCommissionableNodes(filter);
  56. streamer_printf(sout, "done\r\n");
  57. return CHIP_NO_ERROR;
  58. }
  59. static CHIP_ERROR discover(bool printHeader, char * instance)
  60. {
  61. streamer_t * sout = streamer_get();
  62. if (printHeader)
  63. {
  64. streamer_printf(sout, "Discover Instance: ");
  65. }
  66. Mdns::DiscoveryFilter filter(Mdns::DiscoveryFilterType::kInstanceName, instance);
  67. gCommissioner->DiscoverCommissionableNodes(filter);
  68. streamer_printf(sout, "done\r\n");
  69. return CHIP_NO_ERROR;
  70. }
  71. static CHIP_ERROR display(bool printHeader)
  72. {
  73. streamer_t * sout = streamer_get();
  74. if (printHeader)
  75. {
  76. streamer_printf(sout, "Display:\r\n");
  77. }
  78. for (int i = 0; i < 10; i++)
  79. {
  80. const chip::Mdns::DiscoveredNodeData * next = gCommissioner->GetDiscoveredDevice(i);
  81. if (next == nullptr)
  82. {
  83. streamer_printf(sout, " Entry %d null\r\n", i);
  84. }
  85. else
  86. {
  87. streamer_printf(sout, " Entry %d instanceName=%s host=%s longDiscriminator=%d vendorId=%d productId=%d\r\n", i,
  88. next->instanceName, next->hostName, next->longDiscriminator, next->vendorId, next->productId);
  89. }
  90. }
  91. streamer_printf(sout, "done\r\n");
  92. return CHIP_NO_ERROR;
  93. }
  94. #endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY
  95. static CHIP_ERROR PrintAllCommands()
  96. {
  97. streamer_t * sout = streamer_get();
  98. streamer_printf(sout, " help Usage: discover <subcommand>\r\n");
  99. #if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY
  100. streamer_printf(
  101. sout, " resetudc Clear all pending UDC sessions from this UDC server. Usage: commission resetudc\r\n");
  102. streamer_printf(sout, " commissionable Discover all commissionable nodes. Usage: commission discover\r\n");
  103. streamer_printf(
  104. sout,
  105. " commissionable-instance <name> Discover all commissionable node with given instance name. Usage: commission "
  106. "commissionable-instance DC514873944A5CFF\r\n");
  107. streamer_printf(sout,
  108. " display Display all discovered commissionable nodes. Usage: commission display\r\n");
  109. #endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY
  110. streamer_printf(sout, "\r\n");
  111. return CHIP_NO_ERROR;
  112. }
  113. static CHIP_ERROR DiscoverHandler(int argc, char ** argv)
  114. {
  115. CHIP_ERROR error = CHIP_NO_ERROR;
  116. if (argc == 0 || strcmp(argv[0], "help") == 0)
  117. {
  118. return PrintAllCommands();
  119. }
  120. #if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY
  121. else if (strcmp(argv[0], "resetudc") == 0)
  122. {
  123. return error = ResetUDC(true);
  124. }
  125. #endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY
  126. #if CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
  127. else if (strcmp(argv[0], "commissionable") == 0)
  128. {
  129. return error = discover(true);
  130. }
  131. else if (strcmp(argv[0], "commissionable-instance") == 0)
  132. {
  133. return error = discover(true, argv[1]);
  134. }
  135. else if (strcmp(argv[0], "display") == 0)
  136. {
  137. return error = display(true);
  138. }
  139. #endif // CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE
  140. else
  141. {
  142. return CHIP_ERROR_INVALID_ARGUMENT;
  143. }
  144. return error;
  145. }
  146. void RegisterDiscoverCommands(chip::Controller::DeviceCommissioner * commissioner)
  147. {
  148. gCommissioner = commissioner;
  149. static const shell_command_t sDeviceComand = { &DiscoverHandler, "discover",
  150. "Discover commands. Usage: discover [command_name]" };
  151. // Register the root `device` command with the top-level shell.
  152. Engine::Root().RegisterCommands(&sDeviceComand, 1);
  153. return;
  154. }
  155. } // namespace Shell
  156. } // namespace chip